Bug 1016949 - Add unit test for enabling/disabling suggested sites (r=mfinkle)
authorLucas Rocha <lucasr@mozilla.com>
Thu, 29 May 2014 13:04:35 +0100
changeset 185701 7f416a63d557799955aca21f0a95900c9d0c698e
parent 185700 40046039330c06bd314ccd0a36ada68853f9e548
child 185702 4237886831c47dd84a9d2066b3923e61f785a2d3
push id26861
push userkwierso@gmail.com
push dateFri, 30 May 2014 00:57:35 +0000
treeherdermozilla-central@38c5e21a80fa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs1016949
milestone32.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 1016949 - Add unit test for enabling/disabling suggested sites (r=mfinkle)
mobile/android/tests/browser/junit3/src/tests/TestSuggestedSites.java
--- a/mobile/android/tests/browser/junit3/src/tests/TestSuggestedSites.java
+++ b/mobile/android/tests/browser/junit3/src/tests/TestSuggestedSites.java
@@ -1,45 +1,70 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 package org.mozilla.gecko.browser.tests;
 
 import android.content.Context;
 import android.content.res.Resources;
+import android.content.SharedPreferences;
 import android.database.Cursor;
-import android.test.mock.MockContext;
 import android.test.mock.MockResources;
+import android.test.RenamingDelegatingContext;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.SuggestedSites;
+import org.mozilla.gecko.GeckoSharedPrefs;
+import org.mozilla.gecko.preferences.GeckoPreferences;
 import org.mozilla.gecko.util.RawResource;
 
 public class TestSuggestedSites extends BrowserTestCase {
-    private static class TestContext extends MockContext {
-        private final Resources resources;
+    private static class TestContext extends RenamingDelegatingContext {
+        private static final String PREFIX = "TestSuggestedSites-";
 
-        public TestContext() {
+        private final Resources resources;
+        private final Set<String> usedPrefs;
+
+        public TestContext(Context context) {
+            super(context, PREFIX);
             resources = new TestResources();
+            usedPrefs = Collections.synchronizedSet(new HashSet<String>());
         }
 
         @Override
         public Resources getResources() {
             return resources;
         }
+
+        @Override
+        public SharedPreferences getSharedPreferences(String name, int mode) {
+            usedPrefs.add(name);
+            return super.getSharedPreferences(PREFIX + name, mode);
+        }
+
+        public void clearUsedPrefs() {
+            for (String prefsName : usedPrefs) {
+                getSharedPreferences(prefsName, 0).edit().clear().commit();
+            }
+
+            usedPrefs.clear();
+        }
     }
 
     private static class TestResources extends MockResources {
         private String suggestedSites;
 
         @Override
         public InputStream openRawResource(int id) {
             if (id == R.raw.suggestedsites && suggestedSites != null) {
@@ -86,20 +111,24 @@ public class TestSuggestedSites extends 
     private void checkCursorCount(String content, int expectedCount, int limit) {
         resources.setSuggestedSitesResource(content);
         Cursor c = new SuggestedSites(context).get(limit);
         assertEquals(expectedCount, c.getCount());
         c.close();
     }
 
     protected void setUp() {
-        context = new TestContext();
+        context = new TestContext(getApplicationContext());
         resources = (TestResources) context.getResources();
     }
 
+    protected void tearDown() {
+        context.clearUsedPrefs();
+    }
+
     public void testCount() {
         // Empty array = empty cursor
         checkCursorCount(generateSites(0), 0);
 
         // 2 items = cursor with 2 rows
         checkCursorCount(generateSites(2), 2);
 
         // 10 items with lower limit = cursor respects limit
@@ -162,9 +191,27 @@ public class TestSuggestedSites extends 
         while (c.moveToNext()) {
             String url = c.getString(c.getColumnIndexOrThrow(BrowserContract.SuggestedSites.URL));
             assertFalse(excludedUrls.contains(url));
             assertTrue(includedUrls.contains(url));
         }
 
         c.close();
     }
+
+    public void testDisabledState() {
+        resources.setSuggestedSitesResource(generateSites(3));
+
+        Cursor c = new SuggestedSites(context).get(DEFAULT_LIMIT);
+        assertEquals(3, c.getCount());
+        c.close();
+
+        // Disable suggested sites
+        GeckoSharedPrefs.forApp(context).edit()
+                                        .putBoolean(GeckoPreferences.PREFS_SUGGESTED_SITES, false)
+                                        .commit();
+
+        c = new SuggestedSites(context).get(DEFAULT_LIMIT);
+        assertNotNull(c);
+        assertEquals(0, c.getCount());
+        c.close();
+    }
 }