Bug 760956 - Part 6: Introduce basic topsites retrieval test r=rnewman
authorAndrzej Hunt <ahunt@mozilla.com>
Thu, 25 Feb 2016 16:59:07 -0800
changeset 322373 94b25f1233ddb1df2975a39e02ee8637846ba25b
parent 322372 86d110d3d0eb17bfbaa3f8d4a7287466621ad710
child 322374 045c87312d9a21b4b6b9c3fb8f84e680e6203ee8
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-beta@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs760956
milestone47.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 760956 - Part 6: Introduce basic topsites retrieval test r=rnewman This is a quick sanity check: a clean profile should return all the suggested sites directly. We should probably add further tests making sure that history items appear in front of the suggested sites, that pinned sites work correctly, and that suggested sites don't appear outside of the grid. MozReview-Commit-ID: Ai8gTkv9E9w
mobile/android/base/java/org/mozilla/gecko/db/StubBrowserDB.java
mobile/android/tests/background/junit3/background_junit3_sources.mozbuild
mobile/android/tests/background/junit3/src/org/mozilla/gecko/background/db/TestTopSites.java
--- a/mobile/android/base/java/org/mozilla/gecko/db/StubBrowserDB.java
+++ b/mobile/android/base/java/org/mozilla/gecko/db/StubBrowserDB.java
@@ -160,16 +160,17 @@ class StubUrlAnnotations implements UrlA
  * real implementations, see LocalBrowserDB.java.
  */
 public class StubBrowserDB implements BrowserDB {
     private final StubSearches searches = new StubSearches();
     private final StubTabsAccessor tabsAccessor = new StubTabsAccessor();
     private final StubURLMetadata urlMetadata = new StubURLMetadata();
     private final StubReadingListAccessor readingListAccessor = new StubReadingListAccessor();
     private final StubUrlAnnotations urlAnnotations = new StubUrlAnnotations();
+    private SuggestedSites suggestedSites = null;
 
     @Override
     public Searches getSearches() {
         return searches;
     }
 
     @Override
     public TabsAccessor getTabsAccessor() {
@@ -371,20 +372,21 @@ public class StubBrowserDB implements Br
     }
 
     @RobocopTarget
     public Cursor getBookmarkForUrl(ContentResolver cr, String url) {
         return null;
     }
 
     public void setSuggestedSites(SuggestedSites suggestedSites) {
+        this.suggestedSites = suggestedSites;
     }
 
     public SuggestedSites getSuggestedSites() {
-        return null;
+        return suggestedSites;
     }
 
     public boolean hasSuggestedImageUrl(String url) {
         return false;
     }
 
     public String getSuggestedImageUrlForUrl(String url) {
         return null;
--- a/mobile/android/tests/background/junit3/background_junit3_sources.mozbuild
+++ b/mobile/android/tests/background/junit3/background_junit3_sources.mozbuild
@@ -16,16 +16,17 @@ background_junit3_sources = [
     'src/org/mozilla/gecko/background/db/TestBookmarks.java',
     'src/org/mozilla/gecko/background/db/TestCachedSQLiteOpenHelper.java',
     'src/org/mozilla/gecko/background/db/TestClientsDatabase.java',
     'src/org/mozilla/gecko/background/db/TestClientsDatabaseAccessor.java',
     'src/org/mozilla/gecko/background/db/TestFennecTabsRepositorySession.java',
     'src/org/mozilla/gecko/background/db/TestFennecTabsStorage.java',
     'src/org/mozilla/gecko/background/db/TestFormHistoryRepositorySession.java',
     'src/org/mozilla/gecko/background/db/TestPasswordsRepository.java',
+    'src/org/mozilla/gecko/background/db/TestTopSites.java',
     'src/org/mozilla/gecko/background/fxa/authenticator/TestAccountPickler.java',
     'src/org/mozilla/gecko/background/fxa/TestAccountLoader.java',
     'src/org/mozilla/gecko/background/fxa/TestBrowserIDKeyPairGeneration.java',
     'src/org/mozilla/gecko/background/helpers/AndroidSyncTestCase.java',
     'src/org/mozilla/gecko/background/helpers/BackgroundServiceTestCase.java',
     'src/org/mozilla/gecko/background/helpers/DBHelpers.java',
     'src/org/mozilla/gecko/background/helpers/DBProviderTestCase.java',
     'src/org/mozilla/gecko/background/nativecode/test/TestNativeCrypto.java',
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/background/junit3/src/org/mozilla/gecko/background/db/TestTopSites.java
@@ -0,0 +1,91 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+package org.mozilla.gecko.background.db;
+
+
+import org.mozilla.gecko.GeckoProfile;
+import org.mozilla.gecko.db.BrowserContract;
+import org.mozilla.gecko.db.BrowserContract.Combined;
+import org.mozilla.gecko.db.SuggestedSites;
+import org.mozilla.gecko.sync.setup.Constants;
+
+import android.app.Activity;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.database.Cursor;
+import android.net.Uri;
+import android.test.ActivityInstrumentationTestCase2;
+
+/**
+ * Exercise BrowserDB's getTopSites
+ *
+ * @author ahunt
+ *
+ */
+public class TestTopSites extends ActivityInstrumentationTestCase2<Activity> {
+    Context mContext;
+    SuggestedSites mSuggestedSites;
+
+    public TestTopSites() {
+        super(Activity.class);
+    }
+
+    @Override
+    public void setUp() {
+        mContext = getInstrumentation().getTargetContext();
+        mSuggestedSites = new SuggestedSites(mContext);
+
+        // By default we're using StubBrowserDB which has no suggested sites available.
+        GeckoProfile.get(mContext, Constants.DEFAULT_PROFILE).getDB().setSuggestedSites(mSuggestedSites);
+    }
+
+    @Override
+    public void tearDown() {
+        GeckoProfile.get(mContext, Constants.DEFAULT_PROFILE).getDB().setSuggestedSites(null);
+    }
+
+    public void testGetTopSites() {
+        final int SUGGESTED_LIMIT = 6;
+        final int TOTAL_LIMIT = 50;
+
+        ContentResolver cr = mContext.getContentResolver();
+
+        final Uri uri = BrowserContract.TopSites.CONTENT_URI
+                                .buildUpon()
+                                .appendQueryParameter(BrowserContract.PARAM_PROFILE,
+                                                      Constants.DEFAULT_PROFILE)
+                                .appendQueryParameter(BrowserContract.PARAM_LIMIT,
+                                                      String.valueOf(SUGGESTED_LIMIT))
+                                .appendQueryParameter(BrowserContract.PARAM_SUGGESTEDSITES_LIMIT,
+                                                      String.valueOf(TOTAL_LIMIT))
+                                .build();
+
+        final Cursor c = cr.query(uri,
+                                  new String[] { Combined._ID,
+                                          Combined.URL,
+                                          Combined.TITLE,
+                                          Combined.BOOKMARK_ID,
+                                          Combined.HISTORY_ID },
+                                  null,
+                                  null,
+                                  null);
+
+        int suggestedCount = 0;
+        try {
+            while (c.moveToNext()) {
+                int type = c.getInt(c.getColumnIndexOrThrow(BrowserContract.Bookmarks.TYPE));
+                assertEquals(BrowserContract.TopSites.TYPE_SUGGESTED, type);
+                suggestedCount++;
+            }
+        } finally {
+            c.close();
+        }
+
+        Cursor suggestedSitesCursor = mSuggestedSites.get(SUGGESTED_LIMIT);
+
+        assertEquals(suggestedSitesCursor.getCount(), suggestedCount);
+
+        suggestedSitesCursor.close();
+    }
+}