Bug 1305615 - Use correct database in tests, and close db connection in TestLegacyLoader r=ahunt
authorGrigory Kruglov <gkruglov@mozilla.com>
Mon, 26 Sep 2016 20:26:59 -0700
changeset 315369 1b79b95e1f3bd549bbc0644b4347c6a5b4a63168
parent 315368 e709712a33e7bdfd8f8e8a6cb82b200f95b8b285
child 315370 a5a3e0686adf3d021d1e7e9b7758c2890604c2c7
push id30748
push usercbook@mozilla.com
push dateWed, 28 Sep 2016 13:53:19 +0000
treeherdermozilla-central@8c84b7618840 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersahunt
bugs1305615
milestone52.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 1305615 - Use correct database in tests, and close db connection in TestLegacyLoader r=ahunt We need to ensure we can explicitely close any database connections we open in the TestLegacyLoader, so we initialize a BrowserProvider instance so that we can then call .shutdown() on it once we're done, which will close any open connections. Additionally, we need to make sure to use correct authority when registering (using AUTHORITY_URI produces dual mapping of providers, from content://org.fennec... and from org.fennec...), and wrap our providers in a DelegatingTestContentProvider, which will append PARAM_IS_TEST param to wrapped URIs - when present this ensure we won't try to load a per-profile database. MozReview-Commit-ID: LnzPhGOe6OY
mobile/android/tests/background/junit4/src/org/mozilla/gecko/db/BrowserProviderHistoryVisitsTestBase.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/icons/loader/TestLegacyLoader.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/sync/repositories/android/VisitsHelperTest.java
--- 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
@@ -5,16 +5,17 @@ package org.mozilla.gecko.db;
 
 import android.content.ContentProviderClient;
 import android.content.ContentValues;
 import android.net.Uri;
 import android.os.RemoteException;
 
 import org.junit.After;
 import org.junit.Before;
+import org.mozilla.gecko.background.db.DelegatingTestContentProvider;
 import org.mozilla.gecko.sync.repositories.android.BrowserContractHelpers;
 import org.robolectric.shadows.ShadowContentResolver;
 
 import java.util.UUID;
 
 public class BrowserProviderHistoryVisitsTestBase {
     /* package-private */ ShadowContentResolver contentResolver;
     /* package-private */ ContentProviderClient historyClient;
@@ -23,17 +24,17 @@ public class BrowserProviderHistoryVisit
     /* package-private */ Uri visitsTestUri;
 
     private BrowserProvider provider;
 
     @Before
     public void setUp() throws Exception {
         provider = new BrowserProvider();
         provider.onCreate();
-        ShadowContentResolver.registerProvider(BrowserContract.AUTHORITY_URI.toString(), provider);
+        ShadowContentResolver.registerProvider(BrowserContract.AUTHORITY, new DelegatingTestContentProvider(provider));
 
         contentResolver = new ShadowContentResolver();
         historyClient = contentResolver.acquireContentProviderClient(BrowserContractHelpers.HISTORY_CONTENT_URI);
         visitsClient = contentResolver.acquireContentProviderClient(BrowserContractHelpers.VISITS_CONTENT_URI);
 
         historyTestUri = testUri(BrowserContract.History.CONTENT_URI);
         visitsTestUri = testUri(BrowserContract.Visits.CONTENT_URI);
     }
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/icons/loader/TestLegacyLoader.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/icons/loader/TestLegacyLoader.java
@@ -1,54 +1,75 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 package org.mozilla.gecko.icons.loader;
 
 import android.graphics.Bitmap;
 
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mozilla.gecko.GeckoProfile;
+import org.mozilla.gecko.background.db.DelegatingTestContentProvider;
 import org.mozilla.gecko.background.testhelpers.TestRunner;
+import org.mozilla.gecko.db.BrowserContract;
+import org.mozilla.gecko.db.BrowserDB;
+import org.mozilla.gecko.db.BrowserProvider;
 import org.mozilla.gecko.icons.IconDescriptor;
 import org.mozilla.gecko.icons.IconRequest;
 import org.mozilla.gecko.icons.IconResponse;
 import org.mozilla.gecko.icons.Icons;
 import org.robolectric.RuntimeEnvironment;
+import org.robolectric.shadows.ShadowContentResolver;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
 import java.util.Iterator;
+import java.util.concurrent.ConcurrentHashMap;
 
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 
 @RunWith(TestRunner.class)
 public class TestLegacyLoader {
     private static final String TEST_PAGE_URL = "http://www.mozilla.org";
     private static final String TEST_ICON_URL = "https://example.org/favicon.ico";
     private static final String TEST_ICON_URL_2 = "https://example.com/page/favicon.ico";
     private static final String TEST_ICON_URL_3 = "https://example.net/icon/favicon.ico";
 
     @Test
     public void testDatabaseIsQueriesForNormalRequestsWithNetworkSkipped() {
-        final IconRequest request = Icons.with(RuntimeEnvironment.application)
-                .pageUrl(TEST_PAGE_URL)
-                .icon(IconDescriptor.createGenericIcon(TEST_ICON_URL))
-                .skipNetwork()
-                .build();
+        // We're going to query BrowserProvider via LegacyLoader, and will access a database.
+        // We need to ensure we close our db connection properly.
+        // This is the only test in this class that actually accesses a database. If that changes,
+        // move BrowserProvider registration into a @Before method, and provider.shutdown into @After.
+        final BrowserProvider provider = new BrowserProvider();
+        provider.onCreate();
+        ShadowContentResolver.registerProvider(BrowserContract.AUTHORITY, new DelegatingTestContentProvider(provider));
+        try {
+            final IconRequest request = Icons.with(RuntimeEnvironment.application)
+                    .pageUrl(TEST_PAGE_URL)
+                    .icon(IconDescriptor.createGenericIcon(TEST_ICON_URL))
+                    .skipNetwork()
+                    .build();
 
-        final LegacyLoader loader = spy(new LegacyLoader());
-        final IconResponse response = loader.load(request);
+            final LegacyLoader loader = spy(new LegacyLoader());
+            final IconResponse response = loader.load(request);
 
-        verify(loader).loadBitmapFromDatabase(request);
-
-        Assert.assertNull(response);
+            verify(loader).loadBitmapFromDatabase(request);
+            Assert.assertNull(response);
+        // Close any open db connections.
+        } finally {
+            provider.shutdown();
+        }
     }
 
     @Test
     public void testNothingIsLoadedIfNetworkIsNotSkipped() {
         final IconRequest request = Icons.with(RuntimeEnvironment.application)
                 .pageUrl(TEST_PAGE_URL)
                 .icon(IconDescriptor.createGenericIcon(TEST_ICON_URL))
                 .build();
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/sync/repositories/android/VisitsHelperTest.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/sync/repositories/android/VisitsHelperTest.java
@@ -8,16 +8,17 @@ import android.content.ContentValues;
 import android.net.Uri;
 
 import junit.framework.Assert;
 
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mozilla.gecko.background.db.DelegatingTestContentProvider;
 import org.mozilla.gecko.background.testhelpers.TestRunner;
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserProvider;
 import org.robolectric.shadows.ShadowContentResolver;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
@@ -53,17 +54,17 @@ public class VisitsHelperTest {
     @Test
     public void testGetRecentHistoryVisitsForGUID() throws Exception {
         Uri historyTestUri = testUri(BrowserContract.History.CONTENT_URI);
         Uri visitsTestUri = testUri(BrowserContract.Visits.CONTENT_URI);
 
         BrowserProvider provider = new BrowserProvider();
         try {
             provider.onCreate();
-            ShadowContentResolver.registerProvider(BrowserContract.AUTHORITY_URI.toString(), provider);
+            ShadowContentResolver.registerProvider(BrowserContract.AUTHORITY, new DelegatingTestContentProvider(provider));
 
             final ShadowContentResolver cr = new ShadowContentResolver();
             ContentProviderClient historyClient = cr.acquireContentProviderClient(BrowserContractHelpers.HISTORY_CONTENT_URI);
             ContentProviderClient visitsClient = cr.acquireContentProviderClient(BrowserContractHelpers.VISITS_CONTENT_URI);
 
             ContentValues historyItem = new ContentValues();
             historyItem.put(BrowserContract.History.URL, "https://www.mozilla.org");
             historyItem.put(BrowserContract.History.GUID, "testGUID");