Bug 1234315 - Introduce test for url to cache-filename r=nalexander draft
authorAndrzej Hunt <ahunt@mozilla.com>
Mon, 04 Apr 2016 15:12:04 -0700
changeset 347679 8cde4237ebfbe8794b1cfc54f3f5eec2e2a85fd0
parent 347678 d5664c873a19ee790f45afc6d6f78bbf0fc68633
child 517687 773ab764ea7ce909c51ec6ad82f64ba2ab82aa67
push id14644
push userahunt@mozilla.com
push dateTue, 05 Apr 2016 17:10:39 +0000
reviewersnalexander
bugs1234315
milestone48.0a1
Bug 1234315 - Introduce test for url to cache-filename r=nalexander We hash the page URL to produce a filename for each cached reader view page. For the purposes of the rl-migration we need a Java implementation, however the canonical implementation lives in the current JS readercache (which might be removed in future...). This test should help ensure that we don't lose saved items during the migration. We could extract getReaderCacheFileNameForURL(), however it seems safer to make it public instead - this makes it obvious that this is a migration-only method that shouldn't be modified/reused in future. MozReview-Commit-ID: GNg20nszMh9
mobile/android/base/java/org/mozilla/gecko/db/BrowserDatabaseHelper.java
mobile/android/tests/browser/robocop/robocop.ini
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testReaderCacheMigration.java
mobile/android/tests/browser/robocop/testReaderCacheMigration.js
--- a/mobile/android/base/java/org/mozilla/gecko/db/BrowserDatabaseHelper.java
+++ b/mobile/android/base/java/org/mozilla/gecko/db/BrowserDatabaseHelper.java
@@ -13,16 +13,17 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.mozilla.apache.commons.codec.binary.Base32;
 import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
+import org.mozilla.gecko.annotation.RobocopTarget;
 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
 import org.mozilla.gecko.db.BrowserContract.Combined;
 import org.mozilla.gecko.db.BrowserContract.Favicons;
 import org.mozilla.gecko.db.BrowserContract.History;
 import org.mozilla.gecko.db.BrowserContract.Numbers;
 import org.mozilla.gecko.db.BrowserContract.ReadingListItems;
 import org.mozilla.gecko.db.BrowserContract.SearchHistory;
 import org.mozilla.gecko.db.BrowserContract.Thumbnails;
@@ -1162,17 +1163,19 @@ public final class BrowserDatabaseHelper
         createLoginsTable(db, TABLE_LOGINS);
         createLoginsTableIndices(db, TABLE_LOGINS);
     }
 
     // Get the cache path for a URL, based on the storage format in place during the 27to28 transition.
     // This is a reimplementation of _toHashedPath from ReaderMode.jsm - given that we're likely
     // to migrate the SavedReaderViewHelper implementation at some point, it seems safest to have a local
     // implementation here - moreover this is probably faster than calling into JS.
-    private static String getReaderCacheFileNameForURL(String url) {
+    // This is public only to allow for testing.
+    @RobocopTarget
+    public static String getReaderCacheFileNameForURL(String url) {
         try {
             // On KitKat and above we can use java.nio.charset.StandardCharsets.UTF_8 in place of "UTF8"
             // which avoids having to handle UnsupportedCodingException
             byte[] utf8 = url.getBytes("UTF8");
 
             final MessageDigest digester = MessageDigest.getInstance("MD5");
             byte[] hash = digester.digest(utf8);
 
--- a/mobile/android/tests/browser/robocop/robocop.ini
+++ b/mobile/android/tests/browser/robocop/robocop.ini
@@ -98,16 +98,17 @@ skip-if = android_version == "18"
 [src/org/mozilla/gecko/tests/testAboutHomeVisibility.java]
 [src/org/mozilla/gecko/tests/testAppMenuPathways.java]
 [src/org/mozilla/gecko/tests/testBackButtonInEditMode.java]
 [src/org/mozilla/gecko/tests/testBrowserDatabaseHelperUpgrades.java]
 [src/org/mozilla/gecko/tests/testEventDispatcher.java]
 [src/org/mozilla/gecko/tests/testGeckoRequest.java]
 [src/org/mozilla/gecko/tests/testInputConnection.java]
 [src/org/mozilla/gecko/tests/testJavascriptBridge.java]
+[src/org/mozilla/gecko/tests/testReaderCacheMigration.java]
 [src/org/mozilla/gecko/tests/testReadingListToBookmarksMigration.java]
 [src/org/mozilla/gecko/tests/testNativeCrypto.java]
 [src/org/mozilla/gecko/tests/testReaderModeTitle.java]
 [src/org/mozilla/gecko/tests/testSessionHistory.java]
 [src/org/mozilla/gecko/tests/testStateWhileLoading.java]
 [src/org/mozilla/gecko/tests/testUnifiedTelemetryClientId.java]
 
 [src/org/mozilla/gecko/tests/testAccessibleCarets.java]
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testReaderCacheMigration.java
@@ -0,0 +1,62 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.tests;
+
+import org.mozilla.gecko.GeckoProfile;
+import org.mozilla.gecko.db.BrowserDatabaseHelper;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertTrue;
+
+/**
+ * Tests that our readercache-migration works correctly.
+ *
+ * Our main concern is ensuring that the hashed path for a given url is the same in Java
+ * as it was in JS, or else our (Java-based) migration will lose track of valid cached items.
+ */
+public class testReaderCacheMigration extends JavascriptBridgeTest {
+
+    private final String[] TEST_DOMAINS = new String[] {
+            "",
+            "http://mozilla.org",
+            "https://bugzilla.mozilla.org/show_bug.cgi?id=1234315#c41",
+            "http://www.llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch.com/"
+    };
+
+    private static final String TEST_JS = "testReaderCacheMigration.js";
+
+    /**
+     * We compute the path-name in Java, and pass this through to JS, which conducts the actual
+     * equality check. Our JavascriptBridge doesn't seem to support return values, so we need
+     * to instead pass the computed path-name in at least one direction.
+     */
+    private void checkPathMatches(final String pageURL, final File cacheDir) {
+        final String hashedName = BrowserDatabaseHelper.getReaderCacheFileNameForURL(pageURL);
+
+        final File cacheFile = new File(cacheDir, hashedName);
+
+        try {
+            // We have to use the canonical path to match what the JS side will use. We could
+            // instead just match on the file name, and not the path, but this helps
+            // ensure that we've not broken any of the path finding either.
+            getJS().syncCall("check_hashed_path_matches", pageURL, cacheFile.getCanonicalPath());
+        } catch (IOException e) {
+            fAssertTrue("Unable to getCanonicalPath(), this should never happen", false);
+        }
+
+    }
+
+    public void testReaderCacheMigration() {
+        blockForReadyAndLoadJS(TEST_JS);
+
+        final File cacheDir = new File(GeckoProfile.get(getActivity()).getDir(), "readercache");
+
+        for (final String URL : TEST_DOMAINS) {
+            checkPathMatches(URL, cacheDir);
+        }
+    }
+}
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/browser/robocop/testReaderCacheMigration.js
@@ -0,0 +1,23 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+Components.utils.import("resource://gre/modules/ReaderMode.jsm");
+
+var java = new JavaBridge(this);
+
+do_register_cleanup(() => {
+  java.disconnect();
+});
+do_test_pending();
+
+function check_hashed_path_matches(url, hashedPath) {
+  var jsHashedPath = ReaderMode._toHashedPath(url);
+  do_check_eq(hashedPath, jsHashedPath);
+}
+
+function finish_test() {
+  do_test_finished();
+}
\ No newline at end of file