Bug 1344759 - add a unit test for Chrome bookmark imports, r?dao draft
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Mon, 13 Mar 2017 23:25:46 +0000
changeset 499128 6963af0d8467f5d6cbbdbc01e26134f1903c2057
parent 498697 8dd496fd015a2b6e99573070279d9d1593836ea9
child 499129 fd07782a8ae17d7a0de2e6b168c817fb51c53f84
push id49334
push userbmo:gijskruitbosch+bugs@gmail.com
push dateWed, 15 Mar 2017 11:57:23 +0000
reviewersdao
bugs1344759
milestone55.0a1
Bug 1344759 - add a unit test for Chrome bookmark imports, r?dao MozReview-Commit-ID: 8w1GnbDmOc5
browser/components/migration/tests/unit/test_Chrome_bookmarks.js
browser/components/migration/tests/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/browser/components/migration/tests/unit/test_Chrome_bookmarks.js
@@ -0,0 +1,105 @@
+"use strict";
+
+Cu.import("resource://gre/modules/AppConstants.jsm");
+
+add_task(function* () {
+  let rootDir = do_get_file("chromefiles/");
+  let pathId;
+  let subDirs = ["Google", "Chrome"];
+  if (AppConstants.platform == "macosx") {
+    subDirs.unshift("Application Support");
+    pathId = "ULibDir";
+  } else if (AppConstants.platform == "win") {
+    subDirs.push("User Data");
+    pathId = "LocalAppData";
+  } else {
+    subDirs = [".config", "google-chrome"];
+    pathId = "Home";
+  }
+  registerFakePath(pathId, rootDir);
+
+  let target = rootDir.clone();
+  // Pretend this is the default profile
+  subDirs.push("Default");
+  while (subDirs.length) {
+    target.append(subDirs.shift());
+  }
+  // We don't import osfile.jsm until after registering the fake path, because
+  // importing osfile will sometimes greedily fetch certain path identifiers
+  // from the dir service, which means they get cached, which means we can't
+  // register a fake path for them anymore.
+  Cu.import("resource://gre/modules/osfile.jsm"); /* globals OS */
+  yield OS.File.makeDir(target.path, {from: rootDir.parent.path, ignoreExisting: true});
+
+  target.append("Bookmarks");
+  yield OS.File.remove(target.path, {ignoreAbsent: true});
+
+  let bookmarksData = {roots: {bookmark_bar: {children: []}, other: {children: []}}};
+  const MAX_BMS = 100;
+  let barKids = bookmarksData.roots.bookmark_bar.children;
+  let menuKids = bookmarksData.roots.other.children;
+  let currentMenuKids = menuKids;
+  let currentBarKids = barKids;
+  for (let i = 0; i < MAX_BMS; i++) {
+    currentBarKids.push({
+      url: "https://www.chrome-bookmark-bar-bookmark" + i + ".com",
+      name: "bookmark " + i,
+      type: "url",
+    });
+    currentMenuKids.push({
+      url: "https://www.chrome-menu-bookmark" + i + ".com",
+      name: "bookmark for menu " + i,
+      type: "url",
+    });
+    if (i % 20 == 19) {
+      let nextFolder = {
+        name: "toolbar folder " + Math.ceil(i / 20),
+        type: "folder",
+        children: [],
+      };
+      currentBarKids.push(nextFolder);
+      currentBarKids = nextFolder.children;
+
+      nextFolder = {
+        name: "menu folder " + Math.ceil(i / 20),
+        type: "folder",
+        children: [],
+      };
+      currentMenuKids.push(nextFolder);
+      currentMenuKids = nextFolder.children;
+    }
+  }
+
+  yield OS.File.writeAtomic(target.path, JSON.stringify(bookmarksData), {encoding: "utf-8"});
+
+  let migrator = MigrationUtils.getMigrator("chrome");
+  // Sanity check for the source.
+  Assert.ok(migrator.sourceExists);
+
+  let itemsSeen = {bookmarks: 0, folders: 0};
+  let bmObserver = {
+    onItemAdded(aItemId, aParentId, aIndex, aItemType, aURI, aTitle) {
+      if (!aTitle.includes("Chrome")) {
+        itemsSeen[aItemType == PlacesUtils.bookmarks.TYPE_FOLDER ? "folders" : "bookmarks"]++;
+      }
+    },
+    onBeginUpdateBatch() {},
+    onEndUpdateBatch() {},
+    onItemRemoved() {},
+    onItemChanged() {},
+    onItemVisited() {},
+    onItemMoved() {},
+  };
+
+  PlacesUtils.bookmarks.addObserver(bmObserver, false);
+  const PROFILE = {
+    id: "Default",
+    name: "Default",
+  };
+  yield promiseMigration(migrator, MigrationUtils.resourceTypes.BOOKMARKS, PROFILE);
+  PlacesUtils.bookmarks.removeObserver(bmObserver);
+
+  Assert.equal(itemsSeen.bookmarks, 200, "Should have seen 200 bookmarks.");
+  Assert.equal(itemsSeen.folders, 10, "Should have seen 10 folders.");
+  Assert.equal(MigrationUtils._importQuantities.bookmarks, itemsSeen.bookmarks + itemsSeen.folders, "Telemetry reporting correct.");
+});
--- a/browser/components/migration/tests/unit/xpcshell.ini
+++ b/browser/components/migration/tests/unit/xpcshell.ini
@@ -2,16 +2,17 @@
 head = head_migration.js
 firefox-appdir = browser
 skip-if = toolkit == 'android'
 support-files =
   Library/**
   AppData/**
 
 [test_automigration.js]
+[test_Chrome_bookmarks.js]
 [test_Chrome_cookies.js]
 skip-if = os != "mac" # Relies on ULibDir
 [test_Chrome_passwords.js]
 skip-if = os != "win"
 [test_Edge_availability.js]
 [test_Edge_db_migration.js]
 skip-if = os != "win" || os_version == "5.1" || os_version == "5.2" # Relies on post-XP bits of ESEDB
 [test_fx_telemetry.js]