gecko-dev/browser/components/migration/tests/unit/test_Chrome_bookmarks.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

99 lines
3.4 KiB
JavaScript

"use strict";
const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
add_task(async function() {
let rootDir = do_get_file("chromefiles/", true);
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.
const {OS} = ChromeUtils.import("resource://gre/modules/osfile.jsm");
await OS.File.makeDir(target.path, {from: rootDir.parent.path, ignoreExisting: true});
target.append("Bookmarks");
await 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;
}
}
await OS.File.writeAtomic(target.path, JSON.stringify(bookmarksData), {encoding: "utf-8"});
let migrator = await MigrationUtils.getMigrator("chrome");
// Sanity check for the source.
Assert.ok(await migrator.isSourceAvailable());
let itemsSeen = {bookmarks: 0, folders: 0};
let listener = events => {
for (let event of events) {
if (!event.title.includes("Chrome")) {
itemsSeen[event.itemType == PlacesUtils.bookmarks.TYPE_FOLDER ? "folders" : "bookmarks"]++;
}
}
};
PlacesUtils.observers.addListener(["bookmark-added"], listener);
const PROFILE = {
id: "Default",
name: "Default",
};
await promiseMigration(migrator, MigrationUtils.resourceTypes.BOOKMARKS, PROFILE);
PlacesUtils.observers.removeListener(["bookmark-added"], listener);
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.");
});