Bug 1298783 - Add activity stream blocklist tests r=Grisha
authorAndrzej Hunt <ahunt@mozilla.com>
Tue, 27 Sep 2016 12:32:22 -0700
changeset 315777 1faeb0e59cad1c322ea11e7fb6ed7d5d5a920fed
parent 315776 818fa3036aef389fcc27e24f79a5edb130e15394
child 315778 0b9c9000ac93d1e0ddfb603dbeece6ad784754db
push id20634
push usercbook@mozilla.com
push dateFri, 30 Sep 2016 10:10:13 +0000
treeherderfx-team@afe79b010d13 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersGrisha
bugs1298783
milestone52.0a1
Bug 1298783 - Add activity stream blocklist tests r=Grisha MozReview-Commit-ID: 3W51oPtdsxw
mobile/android/tests/background/junit4/src/org/mozilla/gecko/db/BrowserProviderHighlightsTest.java
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/db/BrowserProviderHighlightsTest.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/db/BrowserProviderHighlightsTest.java
@@ -24,39 +24,56 @@ import java.util.concurrent.TimeUnit;
 import static org.mozilla.gecko.db.BrowserContract.PARAM_PROFILE;
 
 /**
  * Unit tests for the highlights query (Activity Stream).
  */
 @RunWith(TestRunner.class)
 public class BrowserProviderHighlightsTest extends BrowserProviderHistoryVisitsTestBase {
     private ContentProviderClient highlightsClient;
+    private ContentProviderClient activityStreamBlocklistClient;
     private ContentProviderClient bookmarksClient;
 
     private Uri highlightsTestUri;
+    private Uri activityStreamBlocklistTestUri;
     private Uri bookmarksTestUri;
 
+    private Uri expireHistoryNormalUri;
+
     @Before
     public void setUp() throws Exception {
         super.setUp();
 
         final Uri highlightsClientUri = BrowserContract.Highlights.CONTENT_URI.buildUpon()
                 .appendQueryParameter(PARAM_PROFILE, Constants.DEFAULT_PROFILE)
                 .build();
 
+        final Uri activityStreamBlocklistClientUri = BrowserContract.ActivityStreamBlocklist.CONTENT_URI.buildUpon()
+                .appendQueryParameter(PARAM_PROFILE, Constants.DEFAULT_PROFILE)
+                .build();
+
         highlightsClient = contentResolver.acquireContentProviderClient(highlightsClientUri);
+        activityStreamBlocklistClient = contentResolver.acquireContentProviderClient(activityStreamBlocklistClientUri);
         bookmarksClient = contentResolver.acquireContentProviderClient(BrowserContractHelpers.BOOKMARKS_CONTENT_URI);
 
         highlightsTestUri = testUri(BrowserContract.Highlights.CONTENT_URI);
+        activityStreamBlocklistTestUri = testUri(BrowserContract.ActivityStreamBlocklist.CONTENT_URI);
         bookmarksTestUri = testUri(BrowserContract.Bookmarks.CONTENT_URI);
+
+        expireHistoryNormalUri = testUri(BrowserContract.History.CONTENT_OLD_URI).buildUpon()
+                .appendQueryParameter(
+                        BrowserContract.PARAM_EXPIRE_PRIORITY,
+                        BrowserContract.ExpirePriority.NORMAL.toString()
+                ).build();
     }
 
     @After
     public void tearDown() {
         highlightsClient.release();
+        activityStreamBlocklistClient.release();
         bookmarksClient.release();
 
         super.tearDown();
     }
 
     /**
      * Scenario: Empty database, no history, no bookmarks.
      *
@@ -286,28 +303,103 @@ public class BrowserProviderHighlightsTe
         final Cursor cursor = highlightsClient.query(highlightsTestUri, null, null, null, null);
         Assert.assertNotNull(cursor);
 
         Assert.assertEquals(0, cursor.getCount());
 
         cursor.close();
     }
 
+    @Test
+    public void testBlocklistItemsAreNotSelected() throws Exception {
+        final long oneDayAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1);
+
+        final String blockURL = createUniqueUrl();
+
+        insertBookmarkItem(blockURL, createUniqueTitle(), oneDayAgo);
+
+        Cursor cursor = highlightsClient.query(highlightsTestUri, null, null, null, null);
+        Assert.assertNotNull(cursor);
+        Assert.assertEquals(1, cursor.getCount());
+        cursor.close();
+
+        insertBlocklistItem(blockURL);
+
+        cursor = highlightsClient.query(highlightsTestUri, null, null, null, null);
+        Assert.assertNotNull(cursor);
+        Assert.assertEquals(0, cursor.getCount());
+        cursor.close();
+    }
+
+    @Test
+    public void testBlocklistItemsExpire() throws Exception {
+        final long oneDayAgo = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1);
+
+        final String blockURL = createUniqueUrl();
+        final String blockTitle = createUniqueTitle();
+
+        insertBookmarkItem(blockURL, blockTitle, oneDayAgo);
+        insertBlocklistItem(blockURL);
+
+        {
+            final Cursor cursor = highlightsClient.query(highlightsTestUri, null, null, null, null);
+            Assert.assertNotNull(cursor);
+            Assert.assertEquals(0, cursor.getCount());
+            cursor.close();
+        }
+
+        // Add (2000 / 10) items in the loop -> 201 items total
+        int itemsNeeded = BrowserProvider.DEFAULT_EXPIRY_RETAIN_COUNT / BrowserProvider.ACTIVITYSTREAM_BLOCKLIST_EXPIRY_FACTOR;
+        for (int i = 0; i < itemsNeeded; i++) {
+            insertBlocklistItem(createUniqueUrl());
+        }
+
+        // We still have zero highlights: the item is still blocked
+        {
+            final Cursor cursor = highlightsClient.query(highlightsTestUri, null, null, null, null);
+            Assert.assertNotNull(cursor);
+            Assert.assertEquals(0, cursor.getCount());
+            cursor.close();
+        }
+
+        // expire the original blocked URL - only most recent 200 items are retained
+        historyClient.delete(expireHistoryNormalUri, null, null);
+
+        // And the original URL is now in highlights again (note: this shouldn't happen in real life,
+        // since the URL will no longer be eligible for highlights by the time we expire it)
+        {
+            final Cursor cursor = highlightsClient.query(highlightsTestUri, null, null, null, null);
+            Assert.assertNotNull(cursor);
+            Assert.assertEquals(1, cursor.getCount());
+
+            cursor.moveToFirst();
+            assertCursor(cursor, blockURL, blockTitle);
+            cursor.close();
+        }
+    }
+
     private void insertBookmarkItem(String url, String title, long createdAt) throws RemoteException {
         ContentValues values = new ContentValues();
 
         values.put(BrowserContract.Bookmarks.URL, url);
         values.put(BrowserContract.Bookmarks.TITLE, title);
         values.put(BrowserContract.Bookmarks.PARENT, 0);
         values.put(BrowserContract.Bookmarks.TYPE, BrowserContract.Bookmarks.TYPE_BOOKMARK);
         values.put(BrowserContract.Bookmarks.DATE_CREATED, createdAt);
 
         bookmarksClient.insert(bookmarksTestUri, values);
     }
 
+    private void insertBlocklistItem(String url) throws RemoteException {
+        final ContentValues values = new ContentValues();
+        values.put(BrowserContract.ActivityStreamBlocklist.URL, url);
+
+        activityStreamBlocklistClient.insert(activityStreamBlocklistTestUri, values);
+    }
+
     private void assertCursor(Cursor cursor, String url, String title) {
         final String actualTitle = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Combined.TITLE));
         Assert.assertEquals(title, actualTitle);
 
         final String actualUrl = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Combined.URL));
         Assert.assertEquals(url, actualUrl);
     }