Bug 1265525 - Pre: move history expirations tests to junit4 r=mcomella
MozReview-Commit-ID: DtkjEFoYQMA
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/db/BrowserProviderHistoryTest.java
@@ -0,0 +1,191 @@
+/* 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 android.content.ContentProviderClient;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.RemoteException;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mozilla.gecko.background.testhelpers.TestRunner;
+import org.robolectric.shadows.ShadowContentResolver;
+
+import static org.junit.Assert.*;
+
+/**
+ * Testing functionality exposed by BrowserProvider ContentProvider (history, bookmarks, etc).
+ * This is WIP junit4 port of robocop tests at org.mozilla.gecko.tests.testBrowserProvider.
+ * See Bug 1269492
+ */
+@RunWith(TestRunner.class)
+public class BrowserProviderHistoryTest extends BrowserProviderHistoryVisitsTestBase {
+ private ContentProviderClient thumbnailClient;
+ private Uri thumbnailTestUri;
+ private Uri expireHistoryNormalUri;
+ private Uri expireHistoryAggressiveUri;
+
+ private static final long THREE_MONTHS = 1000L * 60L * 60L * 24L * 30L * 3L;
+
+ @Before
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ final ShadowContentResolver cr = new ShadowContentResolver();
+ thumbnailClient = cr.acquireContentProviderClient(BrowserContract.Thumbnails.CONTENT_URI);
+ thumbnailTestUri = testUri(BrowserContract.Thumbnails.CONTENT_URI);
+ expireHistoryNormalUri = testUri(BrowserContract.History.CONTENT_OLD_URI).buildUpon()
+ .appendQueryParameter(
+ BrowserContract.PARAM_EXPIRE_PRIORITY,
+ BrowserContract.ExpirePriority.NORMAL.toString()
+ ).build();
+ expireHistoryAggressiveUri = testUri(BrowserContract.History.CONTENT_OLD_URI).buildUpon()
+ .appendQueryParameter(
+ BrowserContract.PARAM_EXPIRE_PRIORITY,
+ BrowserContract.ExpirePriority.AGGRESSIVE.toString()
+ ).build();
+ }
+
+ /**
+ * Test aggressive expiration on new (recent) history items
+ */
+ @Test
+ public void testHistoryExpirationAggressiveNew() throws Exception {
+ final int historyItemsCount = 3000;
+ insertHistory(historyItemsCount, System.currentTimeMillis());
+
+ historyClient.delete(expireHistoryAggressiveUri, null, null);
+
+ /**
+ * Aggressive expiration should leave 500 history items
+ * See {@link BrowserProvider.AGGRESSIVE_EXPIRY_RETAIN_COUNT}
+ */
+ assertRowCount(historyClient, historyTestUri, 500);
+
+ /**
+ * Aggressive expiration should leave 15 thumbnails
+ * See {@link BrowserProvider.DEFAULT_EXPIRY_THUMBNAIL_COUNT}
+ */
+ assertRowCount(thumbnailClient, thumbnailTestUri, 15);
+ }
+
+ /**
+ * Test normal expiration on new (recent) history items
+ */
+ @Test
+ public void testHistoryExpirationNormalNew() throws Exception {
+ final int historyItemsCount = 3000;
+ insertHistory(historyItemsCount, System.currentTimeMillis());
+
+ historyClient.delete(expireHistoryNormalUri, null, null);
+
+ // Normal expiration shouldn't expire new items
+ assertRowCount(historyClient, historyTestUri, 3000);
+
+ /**
+ * Normal expiration should leave 15 thumbnails
+ * See {@link BrowserProvider.DEFAULT_EXPIRY_THUMBNAIL_COUNT}
+ */
+ assertRowCount(thumbnailClient, thumbnailTestUri, 15);
+ }
+
+ /**
+ * Test aggressive expiration on old history items
+ */
+ @Test
+ public void testHistoryExpirationAggressiveOld() throws Exception {
+ final int historyItemsCount = 3000;
+ insertHistory(historyItemsCount, System.currentTimeMillis() - THREE_MONTHS);
+
+ historyClient.delete(expireHistoryAggressiveUri, null, null);
+
+ /**
+ * Aggressive expiration should leave 500 history items
+ * See {@link BrowserProvider.AGGRESSIVE_EXPIRY_RETAIN_COUNT}
+ */
+ assertRowCount(historyClient, historyTestUri, 500);
+
+ /**
+ * Aggressive expiration should leave 15 thumbnails
+ * See {@link BrowserProvider.DEFAULT_EXPIRY_THUMBNAIL_COUNT}
+ */
+ assertRowCount(thumbnailClient, thumbnailTestUri, 15);
+ }
+
+ /**
+ * Test normal expiration on old history items
+ */
+ @Test
+ public void testHistoryExpirationNormalOld() throws Exception {
+ final int historyItemsCount = 3000;
+ insertHistory(historyItemsCount, System.currentTimeMillis() - THREE_MONTHS);
+
+ historyClient.delete(expireHistoryNormalUri, null, null);
+
+ /**
+ * Normal expiration of old items should retain at most 2000 items
+ * See {@link BrowserProvider.DEFAULT_EXPIRY_RETAIN_COUNT}
+ */
+ assertRowCount(historyClient, historyTestUri, 2000);
+
+ /**
+ * Normal expiration should leave 15 thumbnails
+ * See {@link BrowserProvider.DEFAULT_EXPIRY_THUMBNAIL_COUNT}
+ */
+ assertRowCount(thumbnailClient, thumbnailTestUri, 15);
+ }
+
+ /**
+ * Insert <code>count</code> history records with thumbnails, and for a third of records insert a visit.
+ * Inserting visits only for some of the history records is in order to ensure we're correctly JOIN-ing
+ * History and Visits tables in the Combined view.
+ * Will ensure that date_created and date_modified for new records are the same as last visited date.
+ *
+ * @param count number of history records to insert
+ * @param baseTime timestamp which will be used as a basis for last visited date
+ * @throws RemoteException
+ */
+ private void insertHistory(int count, long baseTime) throws RemoteException {
+ Uri incrementUri = historyTestUri.buildUpon()
+ .appendQueryParameter(BrowserContract.PARAM_INCREMENT_VISITS, "true").build();
+
+ for (int i = 0; i < count; i++) {
+ final String url = "https://www.mozilla" + i + ".org";
+ insertHistoryItem(url, "testGUID" + i, baseTime - i);
+ if (i % 3 == 0) {
+ assertEquals(1, historyClient.update(incrementUri, new ContentValues(), BrowserContract.History.URL + " = ?", new String[]{url}));
+ }
+
+ // inserting a new entry sets the date created and modified automatically, so let's reset them
+ ContentValues cv = new ContentValues();
+ cv.put(BrowserContract.History.DATE_CREATED, baseTime - i);
+ cv.put(BrowserContract.History.DATE_MODIFIED, baseTime - i);
+ assertEquals(1, historyClient.update(historyTestUri, cv, BrowserContract.History.URL + " = ?",
+ new String[] { "https://www.mozilla" + i + ".org" }));
+ }
+
+ // insert thumbnails for history items
+ ContentValues[] thumbs = new ContentValues[count];
+ for (int i = 0; i < count; i++) {
+ thumbs[i] = new ContentValues();
+ thumbs[i].put(BrowserContract.Thumbnails.DATA, i);
+ thumbs[i].put(BrowserContract.Thumbnails.URL, "https://www.mozilla" + i + ".org");
+ }
+ assertEquals(count, thumbnailClient.bulkInsert(thumbnailTestUri, thumbs));
+ }
+
+ private void assertRowCount(final ContentProviderClient client, final Uri uri, final int count) throws RemoteException {
+ final Cursor c = client.query(uri, null, null, null, null);
+ assertNotNull(c);
+ try {
+ assertEquals(count, c.getCount());
+ } finally {
+ c.close();
+ }
+ }
+}
\ No newline at end of file
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/db/BrowserProviderHistoryVisitsTestBase.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/db/BrowserProviderHistoryVisitsTestBase.java
@@ -41,15 +41,20 @@ public class BrowserProviderHistoryVisit
provider.shutdown();
}
protected Uri testUri(Uri baseUri) {
return baseUri.buildUpon().appendQueryParameter(BrowserContract.PARAM_IS_TEST, "1").build();
}
protected Uri insertHistoryItem(String url, String guid) throws RemoteException {
+ return insertHistoryItem(url, guid, System.currentTimeMillis());
+ }
+
+ protected Uri insertHistoryItem(String url, String guid, Long lastVisited) throws RemoteException {
ContentValues historyItem = new ContentValues();
historyItem.put(BrowserContract.History.URL, url);
historyItem.put(BrowserContract.History.GUID, guid);
+ historyItem.put(BrowserContract.History.DATE_LAST_VISITED, lastVisited);
return historyClient.insert(historyTestUri, historyItem);
}
}
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testBrowserProvider.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testBrowserProvider.java
@@ -270,17 +270,16 @@ public class testBrowserProvider extends
mTests.add(new TestInsertUrlAnnotations());
mTests.add(new TestBatchOperations());
mTests.add(new TestCombinedView());
mTests.add(new TestCombinedViewDisplay());
mTests.add(new TestCombinedViewWithDeletedBookmark());
- mTests.add(new TestExpireHistory());
mTests.add(new TestBrowserProviderNotifications());
}
public void testBrowserProvider() throws Exception {
loadMobileFolderId();
for (int i = 0; i < mTests.size(); i++) {
@@ -1682,131 +1681,16 @@ public class testBrowserProvider extends
mAsserter.is(c.moveToFirst(), true, "Found combined entry without bookmark id");
mAsserter.is(c.getLong(c.getColumnIndex(BrowserContract.Combined.BOOKMARK_ID)), 0L,
"Bookmark id should not be set to removed bookmark id");
c.close();
}
}
- private class TestExpireHistory extends TestCase {
- private void createFakeHistory(long timeShift, int count) {
- // Insert a bunch of very new entries
- ContentValues[] allVals = new ContentValues[count];
- long time = System.currentTimeMillis() - timeShift;
- for (int i = 0; i < count; i++) {
- allVals[i] = new ContentValues();
- allVals[i].put(BrowserContract.History.TITLE, "Test " + i);
- allVals[i].put(BrowserContract.History.URL, "http://www.test.org/" + i);
- allVals[i].put(BrowserContract.History.VISITS, i);
- allVals[i].put(BrowserContract.History.DATE_LAST_VISITED, time);
- }
-
- int inserts = mProvider.bulkInsert(BrowserContract.History.CONTENT_URI, allVals);
- mAsserter.is(inserts, count, "Expected number of inserts matches");
-
- // inserting a new entry sets the date created and modified automatically
- // reset all of them
- for (int i = 0; i < count; i++) {
- ContentValues cv = new ContentValues();
- cv.put(BrowserContract.History.DATE_CREATED, time);
- cv.put(BrowserContract.History.DATE_MODIFIED, time);
- mProvider.update(BrowserContract.History.CONTENT_URI, cv, BrowserContract.History.URL + " = ?",
- new String[] { "http://www.test.org/" + i });
- }
-
- Cursor c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
-
- assertCountIsAndClose(c, count, count + " history entries found");
-
- // add thumbnails for each entry
- allVals = new ContentValues[count];
- for (int i = 0; i < count; i++) {
- allVals[i] = new ContentValues();
- allVals[i].put(BrowserContract.Thumbnails.DATA, i);
- allVals[i].put(BrowserContract.Thumbnails.URL, "http://www.test.org/" + i);
- }
-
- inserts = mProvider.bulkInsert(BrowserContract.Thumbnails.CONTENT_URI, allVals);
- mAsserter.is(inserts, count, "Expected number of inserts matches");
-
- c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
- assertCountIsAndClose(c, count, count + " thumbnails entries found");
- }
-
- @Override
- public void test() throws Exception {
- final int count = 3000;
- final int thumbCount = 15;
-
- // insert a bunch of new entries
- createFakeHistory(0, count);
-
- // expiring with a normal priority should not delete new entries
- Uri url = appendUriParam(BrowserContract.History.CONTENT_OLD_URI, BrowserContract.PARAM_EXPIRE_PRIORITY, "NORMAL");
- mProvider.delete(url, null, null);
- Cursor c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
- assertCountIsAndClose(c, count, count + " history entries found");
-
- // expiring with a normal priority should delete all but 10 thumbnails
- c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
- assertCountIsAndClose(c, thumbCount, thumbCount + " thumbnails found");
-
- ensureEmptyDatabase();
-
- // Insert a bunch of new entries.
- createFakeHistory(0, count);
-
- // Expiring with a aggressive priority should leave 500 entries.
- url = appendUriParam(BrowserContract.History.CONTENT_OLD_URI, BrowserContract.PARAM_EXPIRE_PRIORITY, "AGGRESSIVE");
- mProvider.delete(url, null, null);
-
- c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
- assertCountIsAndClose(c, 500, "500 history entries found");
-
- // Expiring with a aggressive priority should delete all but 10 thumbnails.
- c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
- assertCountIsAndClose(c, thumbCount, thumbCount + " thumbnails found");
-
- ensureEmptyDatabase();
-
- // Insert a bunch of entries with an old time created/modified.
- long time = 1000L * 60L * 60L * 24L * 30L * 3L;
- createFakeHistory(time, count);
-
- // Expiring with an normal priority should remove at most 1000 entries,
- // entries leaving at least 2000.
- url = appendUriParam(BrowserContract.History.CONTENT_OLD_URI, BrowserContract.PARAM_EXPIRE_PRIORITY, "NORMAL");
- mProvider.delete(url, null, null);
-
- c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
- assertCountIsAndClose(c, 2000, "2000 history entries found");
-
- // Expiring with a normal priority should delete all but 10 thumbnails.
- c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
- assertCountIsAndClose(c, thumbCount, thumbCount + " thumbnails found");
-
- ensureEmptyDatabase();
- // insert a bunch of entries with an old time created/modified
- time = 1000L * 60L * 60L * 24L * 30L * 3L;
- createFakeHistory(time, count);
-
- // Expiring with an aggressive priority should remove old
- // entries, leaving at least 500.
- url = appendUriParam(BrowserContract.History.CONTENT_OLD_URI, BrowserContract.PARAM_EXPIRE_PRIORITY, "AGGRESSIVE");
- mProvider.delete(url, null, null);
- c = mProvider.query(BrowserContract.History.CONTENT_URI, null, "", null, null);
- assertCountIsAndClose(c, 500, "500 history entries found");
-
- // expiring with an aggressive priority should delete all but 10 thumbnails
- c = mProvider.query(BrowserContract.Thumbnails.CONTENT_URI, null, null, null, null);
- assertCountIsAndClose(c, thumbCount, thumbCount + " thumbnails found");
- }
- }
-
/*
* Verify that insert, update, delete, and bulkInsert operations
* notify the ambient content resolver. Each operation calls the
* content resolver notifyChange method synchronously, so it is
* okay to test sequentially.
*/
private class TestBrowserProviderNotifications extends TestCase {
public static final String LOGTAG = "TestBPNotifications";