Bug 1197866 - Part 3: Add simple test for GlobalHistory. r=nalexander
☠☠ backed out by 4fc03405a420 ☠ ☠
authorAndrew Gaul <andrew@gaul.org>
Mon, 30 Apr 2018 21:14:54 -0700
changeset 490971 32db311632da28ba478fe6d25f39ebbd365f7ae7
parent 490970 a80113dba9c130824111bb48f9fde2b9c4406fcd
child 490973 e2a6c1b95477cba193170f8a9e5b98c8f74d32ce
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersnalexander
bugs1197866
milestone65.0a1
Bug 1197866 - Part 3: Add simple test for GlobalHistory. r=nalexander
mobile/android/app/src/test/java/org/mozilla/gecko/GlobalPageMetadataTest.java
mobile/android/base/java/org/mozilla/gecko/GlobalHistory.java
--- a/mobile/android/app/src/test/java/org/mozilla/gecko/GlobalPageMetadataTest.java
+++ b/mobile/android/app/src/test/java/org/mozilla/gecko/GlobalPageMetadataTest.java
@@ -1,13 +1,15 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 package org.mozilla.gecko;
 
+import java.lang.ref.SoftReference;
+
 import android.content.ContentProvider;
 import android.content.ContentProviderClient;
 import android.content.ContentValues;
 import android.database.Cursor;
 import android.os.RemoteException;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -103,16 +105,29 @@ public class GlobalPageMetadataTest {
 
             assertPageMetadataCountForGUID(0, "guid2", pageMetadataClient);
 
         } finally {
             provider.shutdown();
         }
     }
 
+    @Test
+    public void testGlobalHistory() throws Exception {
+        GlobalHistory history = GlobalHistory.getInstance();
+        // Hold reference to prevent racing with GC.
+        SimpleLongOpenHashSet set = new SimpleLongOpenHashSet();
+        history.mVisitedCache = new SoftReference<>(set);
+        String exampleDotCom = "http://example.com/";
+        assertFalse(history.containsUri(exampleDotCom));
+        history.addUri(exampleDotCom);
+        assertTrue(history.containsUri(exampleDotCom));
+        assertFalse(history.containsUri(exampleDotCom + "2.html"));
+    }
+
     /**
      * Expects cursor to be at the correct position.
      */
     private void assertCursorValues(Cursor cursor, String json, int hasImage, String guid) {
         assertNotNull(cursor);
         assertEquals(json, cursor.getString(cursor.getColumnIndexOrThrow(PageMetadata.JSON)));
         assertEquals(hasImage, cursor.getInt(cursor.getColumnIndexOrThrow(PageMetadata.HAS_IMAGE)));
         assertEquals(guid, cursor.getString(cursor.getColumnIndexOrThrow(PageMetadata.HISTORY_GUID)));
--- a/mobile/android/base/java/org/mozilla/gecko/GlobalHistory.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GlobalHistory.java
@@ -118,22 +118,37 @@ class GlobalHistory {
 
     private GlobalHistory() {
         mHandler = ThreadUtils.getBackgroundHandler();
         mPendingUris = new LinkedList<String>();
         mVisitedCache = new SoftReference<SimpleLongOpenHashSet>(null);
     }
 
     public void addToGeckoOnly(String uri) {
+        addUri(uri);
+        GeckoAppShell.notifyUriVisited(uri);
+    }
+
+    // visible for testing
+    void addUri(String uri) {
         MurmurHash3.LongPair pair = new MurmurHash3.LongPair();
         SimpleLongOpenHashSet visitedSet = mVisitedCache.get();
         if (visitedSet != null) {
             visitedSet.add(hashUrl(uri.getBytes(StringUtils.UTF_8), pair));
         }
-        GeckoAppShell.notifyUriVisited(uri);
+    }
+
+    // visible for testing
+    boolean containsUri(String uri) {
+        MurmurHash3.LongPair pair = new MurmurHash3.LongPair();
+        SimpleLongOpenHashSet visitedSet = mVisitedCache.get();
+        if (visitedSet == null) {
+            return false;
+        }
+        return visitedSet.contains(hashUrl(uri.getBytes(StringUtils.UTF_8), pair));
     }
 
     public void add(final Context context, final BrowserDB db, String uri) {
         ThreadUtils.assertOnBackgroundThread();
         final long start = SystemClock.uptimeMillis();
 
         // stripAboutReaderUrl only removes about:reader if present, in all other cases the original string is returned
         final String uriToStore = ReaderModeUtils.stripAboutReaderUrl(uri);