Bug 1040994 - Add LIMIT support for search history content provider. r=rnewman
authorEric Edens <eedens@mozilla.com>
Mon, 21 Jul 2014 13:36:23 -0700
changeset 195530 8a4691f767ea2c85302ffd65c5b5230ab6521d85
parent 195529 d62df3aa085bf9259143c7b873d4ee179b2a7f88
child 195531 0fadd5050d678b523bfd37c29f9914479b69b5d8
push id27185
push userkwierso@gmail.com
push dateWed, 23 Jul 2014 01:05:43 +0000
treeherdermozilla-central@5683746bac22 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs1040994
milestone34.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1040994 - Add LIMIT support for search history content provider. r=rnewman
mobile/android/base/db/SearchHistoryProvider.java
mobile/android/base/tests/testSearchHistoryProvider.java
--- a/mobile/android/base/db/SearchHistoryProvider.java
+++ b/mobile/android/base/db/SearchHistoryProvider.java
@@ -1,14 +1,15 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.db;
 
+import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserContract.SearchHistory;
 
 import android.content.ContentUris;
 import android.content.ContentValues;
 import android.database.Cursor;
 import android.database.SQLException;
 import android.database.sqlite.SQLiteDatabase;
 import android.net.Uri;
@@ -105,20 +106,21 @@ public class SearchHistoryProvider exten
     public int updateInTransaction(Uri uri, ContentValues values, String selection,
             String[] selectionArgs) {
         throw new UnsupportedOperationException("This content provider does not support updating items");
     }
 
     @Override
     public Cursor query(Uri uri, String[] projection, String selection,
             String[] selectionArgs, String sortOrder) {
-        String groupBy = null;
-        String having = null;
+        final String groupBy = null;
+        final String having = null;
+        final String limit = uri.getQueryParameter(BrowserContract.PARAM_LIMIT);
         final Cursor cursor = getReadableDatabase(uri).query(SearchHistory.TABLE_NAME, projection,
-                selection, selectionArgs, groupBy, having, sortOrder);
+                selection, selectionArgs, groupBy, having, sortOrder, limit);
         cursor.setNotificationUri(getContext().getContentResolver(), uri);
         return cursor;
     }
 
     @Override
     public String getType(Uri uri) {
         return SearchHistory.CONTENT_TYPE;
     }
--- a/mobile/android/base/tests/testSearchHistoryProvider.java
+++ b/mobile/android/base/tests/testSearchHistoryProvider.java
@@ -8,21 +8,23 @@ import java.util.concurrent.Callable;
 
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserContract.SearchHistory;
 import org.mozilla.gecko.db.SearchHistoryProvider;
 
 import android.content.ContentProvider;
 import android.content.ContentValues;
 import android.database.Cursor;
+import android.net.Uri;
 
 public class testSearchHistoryProvider extends ContentProviderTest {
 
     // Translations of "United Kingdom" in several different languages
-    private static final String[] testStrings = {"An Ríocht Aontaithe", // Irish
+    private static final String[] testStrings = {
+            "An Ríocht Aontaithe", // Irish
             "Angli", // Albanian
             "Britanniarum Regnum", // Latin
             "Britio", // Esperanto
             "Büyük Britanya", // Turkish
             "Egyesült Királyság", // Hungarian
             "Erresuma Batua", // Basque
             "Inggris Raya", // Indonesian
             "Ir-Renju Unit", // Maltese
@@ -90,16 +92,17 @@ public class testSearchHistoryProvider e
             };
 
     @Override
     public void setUp() throws Exception {
         super.setUp(sProviderFactory, BrowserContract.SEARCH_HISTORY_AUTHORITY, DB_NAME);
         mTests.add(new TestInsert());
         mTests.add(new TestUnicodeQuery());
         mTests.add(new TestTimestamp());
+        mTests.add(new TestLimit());
         mTests.add(new TestDelete());
         mTests.add(new TestIncrement());
     }
 
     public void testSearchHistory() throws Exception {
         for (Runnable test : mTests) {
             String testName = test.getClass().getSimpleName();
             setTestName(testName);
@@ -107,16 +110,90 @@ public class testSearchHistoryProvider e
                     "testBrowserProvider: Database empty - Starting " + testName + ".");
             // Clear the db
             mProvider.delete(SearchHistory.CONTENT_URI, null, null);
             test.run();
         }
     }
 
     /**
+     * Verify that we can pass a LIMIT clause using a query parameter.
+     */
+    private class TestLimit extends TestCase {
+        @Override
+        public void test() throws Exception {
+            ContentValues cv;
+            for (int i = 0; i < testStrings.length; i++) {
+                cv = new ContentValues();
+                cv.put(SearchHistory.QUERY, testStrings[i]);
+                mProvider.insert(SearchHistory.CONTENT_URI, cv);
+            }
+
+            final int limit = 5;
+
+            // Test 1: Handle proper input.
+
+            Uri uri = SearchHistory.CONTENT_URI
+                                   .buildUpon()
+                                   .appendQueryParameter(BrowserContract.PARAM_LIMIT, String.valueOf(limit))
+                                   .build();
+
+            Cursor c = mProvider.query(uri, null, null, null, null);
+            try {
+                mAsserter.is(c.getCount(), limit,
+                             String.format("Should have %d results", limit));
+            } finally {
+                c.close();
+            }
+
+            // Test 2: Empty input yields all results.
+
+            uri = SearchHistory.CONTENT_URI
+                                   .buildUpon()
+                                   .appendQueryParameter(BrowserContract.PARAM_LIMIT, "")
+                                   .build();
+
+            c = mProvider.query(uri, null, null, null, null);
+            try {
+                mAsserter.is(c.getCount(), testStrings.length, "Should have all results");
+            } finally {
+                c.close();
+            }
+
+            // Test 3: Illegal params.
+
+            String[] illegalParams = new String[] {"a", "-1"};
+            boolean success = true;
+
+            for (String param : illegalParams) {
+                success = true;
+
+                uri = SearchHistory.CONTENT_URI
+                                   .buildUpon()
+                                   .appendQueryParameter(BrowserContract.PARAM_LIMIT, param)
+                                   .build();
+
+                try {
+                    c = mProvider.query(uri, null, null, null, null);
+                    success = false;
+                } catch(IllegalArgumentException e) {
+                    // noop.
+                } finally {
+                    if (c != null) {
+                        c.close();
+                    }
+                }
+
+                mAsserter.ok(success, "LIMIT", param + " should have been an invalid argument");
+            }
+
+        }
+    }
+
+    /**
      * Verify that we can insert values into the DB, including unicode.
      */
     private class TestInsert extends TestCase {
         @Override
         public void test() throws Exception {
             ContentValues cv;
             for (int i = 0; i < testStrings.length; i++) {
                 cv = new ContentValues();