From e3093a68aea1cfab1f2041f9c33fd4b203511ae8 Mon Sep 17 00:00:00 2001 From: Eric Edens Date: Mon, 21 Jul 2014 13:36:23 -0700 Subject: [PATCH] Bug 1040994 - Add LIMIT support for search history content provider. r=rnewman --HG-- extra : rebase_source : b210034222a4a47d23a441a1915461374827471e --- .../base/db/SearchHistoryProvider.java | 8 +- .../base/tests/testSearchHistoryProvider.java | 79 ++++++++++++++++++- 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/mobile/android/base/db/SearchHistoryProvider.java b/mobile/android/base/db/SearchHistoryProvider.java index 06ecd40389a3..05d31fefd6aa 100644 --- a/mobile/android/base/db/SearchHistoryProvider.java +++ b/mobile/android/base/db/SearchHistoryProvider.java @@ -4,6 +4,7 @@ package org.mozilla.gecko.db; +import org.mozilla.gecko.db.BrowserContract; import org.mozilla.gecko.db.BrowserContract.SearchHistory; import android.content.ContentUris; @@ -110,10 +111,11 @@ public class SearchHistoryProvider extends SharedBrowserDatabaseProvider { @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; } diff --git a/mobile/android/base/tests/testSearchHistoryProvider.java b/mobile/android/base/tests/testSearchHistoryProvider.java index c1d487706ba6..e0ae0d871c4c 100644 --- a/mobile/android/base/tests/testSearchHistoryProvider.java +++ b/mobile/android/base/tests/testSearchHistoryProvider.java @@ -13,11 +13,13 @@ 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 @@ -95,6 +97,7 @@ public class testSearchHistoryProvider extends ContentProviderTest { mTests.add(new TestInsert()); mTests.add(new TestUnicodeQuery()); mTests.add(new TestTimestamp()); + mTests.add(new TestLimit()); mTests.add(new TestDelete()); mTests.add(new TestIncrement()); } @@ -111,6 +114,80 @@ public class testSearchHistoryProvider extends ContentProviderTest { } } + /** + * 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. */