Bug 744961 - Tests for history expiration. r=lucasr
authorWes Johnston <wjohnston@mozilla.com>
Fri, 19 Oct 2012 17:35:45 -0700
changeset 110980 65d560d91cb08925819f815b6ae6efe70ce8003f
parent 110979 5d9a4a7b8818bd6947b912eea257a27e5ba6abbe
child 110981 55b904ef7211774588e6640c1d651125cf9fb4bf
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewerslucasr
bugs744961
milestone19.0a1
Bug 744961 - Tests for history expiration. r=lucasr
mobile/android/base/db/BrowserProvider.java.in
mobile/android/base/tests/testBrowserProvider.java.in
--- a/mobile/android/base/db/BrowserProvider.java.in
+++ b/mobile/android/base/db/BrowserProvider.java.in
@@ -2290,17 +2290,19 @@ public class BrowserProvider extends Con
             History.URL,   // 1
             History.VISITS // 2
         };
 
         Cursor cursor = db.query(TABLE_HISTORY, historyProjection, selection,
                 selectionArgs, null, null, null);
 
         try {
-            values.put(History.DATE_MODIFIED, System.currentTimeMillis());
+            if (!values.containsKey(Bookmarks.DATE_MODIFIED)) {
+                values.put(Bookmarks.DATE_MODIFIED,  System.currentTimeMillis());
+            }
 
             boolean updatingUrl = values.containsKey(History.URL);
             String url = null;
 
             if (updatingUrl)
                 url = values.getAsString(History.URL);
 
             ContentValues imageValues = extractImageValues(values, url);
--- a/mobile/android/base/tests/testBrowserProvider.java.in
+++ b/mobile/android/base/tests/testBrowserProvider.java.in
@@ -32,16 +32,17 @@ public class testBrowserProvider extends
     private String TAGS_FOLDER_GUID;
     private String TOOLBAR_FOLDER_GUID;
     private String UNFILED_FOLDER_GUID;
     private String READING_LIST_FOLDER_GUID;
 
     private Uri mBookmarksUri;
     private Uri mBookmarksPositionUri;
     private Uri mHistoryUri;
+    private Uri mHistoryOldUri;
     private Uri mImagesUri;
     private Uri mCombinedUri;
 
     private String mBookmarksIdCol;
     private String mBookmarksTitleCol;
     private String mBookmarksUrlCol;
     private String mBookmarksFaviconCol;
     private String mBookmarksThumbnailCol;
@@ -91,16 +92,17 @@ public class testBrowserProvider extends
     @Override
     protected int getTestType() {
         return TEST_MOCHITEST;
     }
 
     private void loadContractInfo() throws Exception {
         mBookmarksUri = getContentUri("Bookmarks");
         mHistoryUri = getContentUri("History");
+        mHistoryOldUri = getUriColumn("History", "CONTENT_OLD_URI");
         mImagesUri = getContentUri("Images");
         mCombinedUri = getContentUri("Combined");
 
         mBookmarksPositionUri = getUriColumn("Bookmarks", "POSITIONS_CONTENT_URI");
 
         PLACES_FOLDER_GUID = getStringColumn("Bookmarks", "PLACES_FOLDER_GUID");
         MOBILE_FOLDER_GUID = getStringColumn("Bookmarks", "MOBILE_FOLDER_GUID");
         MENU_FOLDER_GUID = getStringColumn("Bookmarks", "MENU_FOLDER_GUID");
@@ -318,16 +320,17 @@ public class testBrowserProvider extends
         mTests.add(new TestUpdateOrInsertHistory());
 
         mTests.add(new TestBatchOperations());
 
         mTests.add(new TestCombinedView());
         mTests.add(new TestCombinedViewDisplay());
         mTests.add(new TestCombinedViewWithDeletedBookmark());
         mTests.add(new TestCombinedViewWithDeletedReadingListItem());
+        mTests.add(new TestExpireHistory());
     }
 
     public void testBrowserProvider() throws Exception {
         loadMobileFolderId();
 
         for (int i = 0; i < mTests.size(); i++) {
             Runnable test = mTests.get(i);
 
@@ -1581,9 +1584,79 @@ public class testBrowserProvider extends
 
             mAsserter.is(c.moveToFirst(), true, "Found combined entry without bookmark id");
             mAsserter.is(new Long(c.getLong(c.getColumnIndex(mCombinedBookmarkIdCol))), new Long(0),
                          "Bookmark id should not be set to removed bookmark id");
             mAsserter.is(new Long(c.getLong(c.getColumnIndex(mCombinedDisplayCol))), new Long(mCombinedDisplayNormal),
                          "Combined entry should have reader display type");
         }
     }
+
+    class TestExpireHistory extends Test {
+        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(mHistoryTitleCol, "Test " + i);
+                allVals[i].put(mHistoryUrlCol, "http://www.test.org/" + i);
+                allVals[i].put(mHistoryVisitsCol, i);
+                allVals[i].put(mHistoryLastVisitedCol, time);
+            }
+
+            int inserts = mProvider.bulkInsert(mHistoryUri, allVals);
+            mAsserter.is(inserts, count, "Excepted 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(mHistoryDateCreatedCol, time);
+                cv.put(mHistoryDateModifiedCol, time);
+                mProvider.update(mHistoryUri, cv, mHistoryUrlCol + " = ?",
+                                 new String[] { "http://www.test.org/" + i });
+            }
+
+            Cursor c = mProvider.query(mHistoryUri, null, "", null, null);
+            mAsserter.is(c.getCount(), count, count + " history entries found");
+        }
+
+        public void test() throws Exception {
+            final int count = 3000;
+
+            // insert a bunch of new entries
+            createFakeHistory(0, count);
+
+            // expiring with a normal priority should not delete new entries
+            Uri url = appendUriParam(mHistoryOldUri, "PARAM_EXPIRE_PRIORITY", "NORMAL");
+            mProvider.delete(url, null, null);
+            Cursor c = mProvider.query(mHistoryUri, null, "", null, null);
+            mAsserter.is(c.getCount(), count, count + " history entries found");
+
+            // expiring with a aggressive priority should leave 500 entries
+            url = appendUriParam(mHistoryOldUri, "PARAM_EXPIRE_PRIORITY", "AGGRESSIVE");
+            mProvider.delete(url, null, null);
+            c = mProvider.query(mHistoryUri, null, "", null, null);
+            mAsserter.is(c.getCount(), 500, "500 history entries 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(mHistoryOldUri, "PARAM_EXPIRE_PRIORITY", "NORMAL");
+            mProvider.delete(url, null, null);
+            c = mProvider.query(mHistoryUri, null, "", null, null);
+            mAsserter.is(c.getCount(), 2000, "2000 history entries found");
+
+            // expiring with an agressive priority should remove old
+            // entries leaving at least 500
+            url = appendUriParam(mHistoryOldUri, "PARAM_EXPIRE_PRIORITY", "AGGRESSIVE");
+            mProvider.delete(url, null, null);
+            c = mProvider.query(mHistoryUri, null, "", null, null);
+            mAsserter.is(c.getCount(), 500, "500 history entries found");
+        }
+    }
 }