Bug 862127 - Fix migrator tests to use async interfaces r?Gijs draft
authorDoug Thayer <dothayer@mozilla.com>
Thu, 11 Jan 2018 15:35:05 -0800
changeset 723850 b90c554cb6b15609d989e4409830e397338c6f49
parent 723849 909545b0096eb6b5092ab279bd1d4967b559f90f
child 746976 b96c2fc01d2703062c22b708b20dceab303aeb8d
push id96559
push userbmo:dothayer@mozilla.com
push dateTue, 23 Jan 2018 23:30:16 +0000
reviewersGijs
bugs862127
milestone60.0a1
Bug 862127 - Fix migrator tests to use async interfaces r?Gijs MozReview-Commit-ID: 9qFA4y8viSW
browser/components/migration/tests/unit/head_migration.js
browser/components/migration/tests/unit/test_360se_bookmarks.js
browser/components/migration/tests/unit/test_ChromeMigrationUtils.js
browser/components/migration/tests/unit/test_Chrome_bookmarks.js
browser/components/migration/tests/unit/test_Chrome_cookies.js
browser/components/migration/tests/unit/test_Chrome_passwords.js
browser/components/migration/tests/unit/test_IE_bookmarks.js
browser/components/migration/tests/unit/test_IE_cookies.js
browser/components/migration/tests/unit/test_Safari_bookmarks.js
browser/components/migration/tests/unit/test_automigration.js
--- a/browser/components/migration/tests/unit/head_migration.js
+++ b/browser/components/migration/tests/unit/head_migration.js
@@ -25,19 +25,19 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 var gProfD = do_get_profile();
 
 Cu.import("resource://testing-common/AppInfo.jsm");
 updateAppInfo();
 
 /**
  * Migrates the requested resource and waits for the migration to be complete.
  */
-function promiseMigration(migrator, resourceType, aProfile = null) {
+async function promiseMigration(migrator, resourceType, aProfile = null) {
   // Ensure resource migration is available.
-  let availableSources = migrator.getMigrateData(aProfile, false);
+  let availableSources = await migrator.getMigrateData(aProfile, false);
   Assert.ok((availableSources & resourceType) > 0, "Resource supported by migrator");
 
   return new Promise(resolve => {
     Services.obs.addObserver(function onMigrationEnded() {
       Services.obs.removeObserver(onMigrationEnded, "Migration:Ended");
       resolve();
     }, "Migration:Ended");
 
--- a/browser/components/migration/tests/unit/test_360se_bookmarks.js
+++ b/browser/components/migration/tests/unit/test_360se_bookmarks.js
@@ -1,18 +1,18 @@
 "use strict";
 
 add_task(async function() {
   registerFakePath("AppData", do_get_file("AppData/Roaming/"));
 
-  let migrator = MigrationUtils.getMigrator("360se");
+  let migrator = await MigrationUtils.getMigrator("360se");
   // Sanity check for the source.
-  Assert.ok(migrator.sourceExists);
+  Assert.ok(await migrator.isSourceAvailable());
 
-  let profiles = migrator.sourceProfiles;
+  let profiles = await migrator.getSourceProfiles();
   Assert.equal(profiles.length, 2, "Should present two profiles");
   Assert.equal(profiles[0].name, "test@firefox.com.cn", "Current logged in user should be the first");
   Assert.equal(profiles[profiles.length - 1].name, "Default", "Default user should be the last");
 
   // Wait for the imported bookmarks.  Check that "From 360 Secure Browser"
   // folders are created on the toolbar.
   let source = MigrationUtils.getLocalizedString("sourceName360se");
   let label = MigrationUtils.getLocalizedString("importedBookmarksFolder", [source]);
--- a/browser/components/migration/tests/unit/test_ChromeMigrationUtils.js
+++ b/browser/components/migration/tests/unit/test_ChromeMigrationUtils.js
@@ -39,11 +39,11 @@ add_task(async function test_getLocaleSt
 });
 
 add_task(async function test_isExtensionInstalled_function() {
   let isInstalled = await ChromeMigrationUtils.isExtensionInstalled("fake-extension-1", "Default");
   Assert.ok(isInstalled, "The fake-extension-1 extension should be installed.");
 });
 
 add_task(async function test_getLastUsedProfileId_function() {
-  let profileId = ChromeMigrationUtils.getLastUsedProfileId();
+  let profileId = await ChromeMigrationUtils.getLastUsedProfileId();
   Assert.equal(profileId, "Default", "The last used profile ID should be Default.");
 });
--- a/browser/components/migration/tests/unit/test_Chrome_bookmarks.js
+++ b/browser/components/migration/tests/unit/test_Chrome_bookmarks.js
@@ -67,19 +67,19 @@ add_task(async function() {
       };
       currentMenuKids.push(nextFolder);
       currentMenuKids = nextFolder.children;
     }
   }
 
   await OS.File.writeAtomic(target.path, JSON.stringify(bookmarksData), {encoding: "utf-8"});
 
-  let migrator = MigrationUtils.getMigrator("chrome");
+  let migrator = await MigrationUtils.getMigrator("chrome");
   // Sanity check for the source.
-  Assert.ok(migrator.sourceExists);
+  Assert.ok(await migrator.isSourceAvailable());
 
   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"]++;
       }
     },
--- a/browser/components/migration/tests/unit/test_Chrome_cookies.js
+++ b/browser/components/migration/tests/unit/test_Chrome_cookies.js
@@ -1,17 +1,17 @@
 "use strict";
 
 Cu.import("resource://gre/modules/ForgetAboutSite.jsm");
 
 add_task(async function() {
   registerFakePath("ULibDir", do_get_file("Library/"));
-  let migrator = MigrationUtils.getMigrator("chrome");
+  let migrator = await MigrationUtils.getMigrator("chrome");
 
-  Assert.ok(migrator.sourceExists, "Sanity check the source exists");
+  Assert.ok(await migrator.isSourceAvailable(), "Sanity check the source exists");
 
   const COOKIE = {
     expiry: 2145934800,
     host: "unencryptedcookie.invalid",
     isHttpOnly: false,
     isSession: false,
     name: "testcookie",
     path: "/",
--- a/browser/components/migration/tests/unit/test_Chrome_passwords.js
+++ b/browser/components/migration/tests/unit/test_Chrome_passwords.js
@@ -138,18 +138,18 @@ add_task(async function setup() {
   });
 });
 
 add_task(async function test_importIntoEmptyDB() {
   for (let login of TEST_LOGINS) {
     await promiseSetPassword(login);
   }
 
-  let migrator = MigrationUtils.getMigrator("chrome");
-  Assert.ok(migrator.sourceExists, "Sanity check the source exists");
+  let migrator = await MigrationUtils.getMigrator("chrome");
+  Assert.ok(await migrator.isSourceAvailable(), "Sanity check the source exists");
 
   let logins = Services.logins.getAllLogins({});
   Assert.equal(logins.length, 0, "There are no logins initially");
 
   // Migrate the logins.
   await promiseMigration(migrator, MigrationUtils.resourceTypes.PASSWORDS, PROFILE);
 
   logins = Services.logins.getAllLogins({});
@@ -159,18 +159,18 @@ add_task(async function test_importIntoE
 
   for (let i = 0; i < TEST_LOGINS.length; i++) {
     checkLoginsAreEqual(logins[i], TEST_LOGINS[i], i + 1);
   }
 });
 
 // Test that existing logins for the same primary key don't get overwritten
 add_task(async function test_importExistingLogins() {
-  let migrator = MigrationUtils.getMigrator("chrome");
-  Assert.ok(migrator.sourceExists, "Sanity check the source exists");
+  let migrator = await MigrationUtils.getMigrator("chrome");
+  Assert.ok(await migrator.isSourceAvailable(), "Sanity check the source exists");
 
   Services.logins.removeAllLogins();
   let logins = Services.logins.getAllLogins({});
   Assert.equal(logins.length, 0, "There are no logins after removing all of them");
 
   let newLogins = [];
 
   // Create 3 new logins that are different but where the key properties are still the same.
--- a/browser/components/migration/tests/unit/test_IE_bookmarks.js
+++ b/browser/components/migration/tests/unit/test_IE_bookmarks.js
@@ -1,14 +1,14 @@
 "use strict";
 
 add_task(async function() {
-  let migrator = MigrationUtils.getMigrator("ie");
+  let migrator = await MigrationUtils.getMigrator("ie");
   // Sanity check for the source.
-  Assert.ok(migrator.sourceExists);
+  Assert.ok(await migrator.isSourceAvailable());
 
   // Wait for the imported bookmarks.  Check that "From Internet Explorer"
   // folders are created in the menu and on the toolbar.
   let source = MigrationUtils.getLocalizedString("sourceNameIE");
   let label = MigrationUtils.getLocalizedString("importedBookmarksFolder", [source]);
 
   let expectedParents = [ PlacesUtils.bookmarksMenuFolderId,
                           PlacesUtils.toolbarFolderId ];
--- a/browser/components/migration/tests/unit/test_IE_cookies.js
+++ b/browser/components/migration/tests/unit/test_IE_cookies.js
@@ -1,17 +1,17 @@
 "use strict";
 
 XPCOMUtils.defineLazyModuleGetter(this, "ctypes",
                                   "resource://gre/modules/ctypes.jsm");
 
 add_task(async function() {
-  let migrator = MigrationUtils.getMigrator("ie");
+  let migrator = await MigrationUtils.getMigrator("ie");
   // Sanity check for the source.
-  Assert.ok(migrator.sourceExists);
+  Assert.ok(await migrator.isSourceAvailable());
 
   const BOOL = ctypes.bool;
   const LPCTSTR = ctypes.char16_t.ptr;
   const DWORD = ctypes.uint32_t;
   const LPDWORD = DWORD.ptr;
 
   let wininet = ctypes.open("Wininet");
 
--- a/browser/components/migration/tests/unit/test_Safari_bookmarks.js
+++ b/browser/components/migration/tests/unit/test_Safari_bookmarks.js
@@ -1,16 +1,16 @@
 "use strict";
 
 add_task(async function() {
   registerFakePath("ULibDir", do_get_file("Library/"));
 
-  let migrator = MigrationUtils.getMigrator("safari");
+  let migrator = await MigrationUtils.getMigrator("safari");
   // Sanity check for the source.
-  Assert.ok(migrator.sourceExists);
+  Assert.ok(await migrator.isSourceAvailable());
 
   // Wait for the imported bookmarks.  Check that "From Safari"
   // folders are created on the toolbar.
   let source = MigrationUtils.getLocalizedString("sourceNameSafari");
   let label = MigrationUtils.getLocalizedString("importedBookmarksFolder", [source]);
 
   let expectedParents = [ PlacesUtils.toolbarFolderId ];
   let itemCount = 0;
--- a/browser/components/migration/tests/unit/test_automigration.js
+++ b/browser/components/migration/tests/unit/test_automigration.js
@@ -38,120 +38,134 @@ async function visitsForURL(url) {
     `SELECT count(*) FROM moz_historyvisits v
      JOIN moz_places h ON h.id = v.place_id
      WHERE url_hash = hash(:url) AND url = :url`,
      {url});
   visitCount = visitCount[0].getInt64(0);
   return visitCount;
 }
 
+async function promiseThrows(fn) {
+  let failed = false;
+  try {
+    await fn();
+  } catch (e) {
+    failed = true;
+  }
+  Assert.ok(failed);
+}
 
 /**
  * Test automatically picking a browser to migrate from
  */
 add_task(async function checkMigratorPicking() {
-  Assert.throws(() => AutoMigrate.pickMigrator("firefox"),
-                /Can't automatically migrate from Firefox/,
-                "Should throw when explicitly picking Firefox.");
+  await promiseThrows(() => AutoMigrate.pickMigrator("firefox"),
+                      /Can't automatically migrate from Firefox/,
+                      "Should throw when explicitly picking Firefox.");
 
-  Assert.throws(() => AutoMigrate.pickMigrator("gobbledygook"),
-                /migrator object is not available/,
-                "Should throw when passing unknown migrator key");
+  await promiseThrows(() => AutoMigrate.pickMigrator("gobbledygook"),
+                      /migrator object is not available/,
+                      "Should throw when passing unknown migrator key");
   gShimmedMigratorKeyPicker = function() {
     return "firefox";
   };
-  Assert.throws(() => AutoMigrate.pickMigrator(),
-                /Can't automatically migrate from Firefox/,
-                "Should throw when implicitly picking Firefox.");
+  await promiseThrows(() => AutoMigrate.pickMigrator(),
+                      /Can't automatically migrate from Firefox/,
+                      "Should throw when implicitly picking Firefox.");
   gShimmedMigratorKeyPicker = function() {
     return "gobbledygook";
   };
-  Assert.throws(() => AutoMigrate.pickMigrator(),
-                /migrator object is not available/,
-                "Should throw when an unknown migrator is the default");
+  await promiseThrows(() => AutoMigrate.pickMigrator(),
+                      /migrator object is not available/,
+                      "Should throw when an unknown migrator is the default");
   gShimmedMigratorKeyPicker = function() {
     return "";
   };
-  Assert.throws(() => AutoMigrate.pickMigrator(),
-                /Could not determine default browser key/,
-                "Should throw when an unknown migrator is the default");
+  await promiseThrows(() => AutoMigrate.pickMigrator(),
+                      /Could not determine default browser key/,
+                      "Should throw when an unknown migrator is the default");
 });
 
 
 /**
  * Test automatically picking a profile to migrate from
  */
 add_task(async function checkProfilePicking() {
-  let fakeMigrator = {sourceProfiles: [{id: "a"}, {id: "b"}]};
-  let profB = fakeMigrator.sourceProfiles[1];
-  Assert.throws(() => AutoMigrate.pickProfile(fakeMigrator),
-                /Don't know how to pick a profile when more/,
-                "Should throw when there are multiple profiles.");
-  Assert.throws(() => AutoMigrate.pickProfile(fakeMigrator, "c"),
-                /Profile specified was not found/,
-                "Should throw when the profile supplied doesn't exist.");
-  let profileToMigrate = AutoMigrate.pickProfile(fakeMigrator, "b");
+  let fakeMigrator = {
+    _sourceProfiles: [{id: "a"}, {id: "b"}],
+    getSourceProfiles() {
+      return this._sourceProfiles;
+    },
+  };
+  let profB = fakeMigrator._sourceProfiles[1];
+  await promiseThrows(() => AutoMigrate.pickProfile(fakeMigrator),
+                      /Don't know how to pick a profile when more/,
+                      "Should throw when there are multiple profiles.");
+  await promiseThrows(() => AutoMigrate.pickProfile(fakeMigrator, "c"),
+                      /Profile specified was not found/,
+                      "Should throw when the profile supplied doesn't exist.");
+  let profileToMigrate = await AutoMigrate.pickProfile(fakeMigrator, "b");
   Assert.equal(profileToMigrate, profB, "Should return profile supplied");
 
-  fakeMigrator.sourceProfiles = null;
-  Assert.throws(() => AutoMigrate.pickProfile(fakeMigrator, "c"),
-                /Profile specified but only a default profile found./,
-                "Should throw when the profile supplied doesn't exist.");
-  profileToMigrate = AutoMigrate.pickProfile(fakeMigrator);
+  fakeMigrator._sourceProfiles = null;
+  await promiseThrows(() => AutoMigrate.pickProfile(fakeMigrator, "c"),
+                      /Profile specified but only a default profile found./,
+                      "Should throw when the profile supplied doesn't exist.");
+  profileToMigrate = await AutoMigrate.pickProfile(fakeMigrator);
   Assert.equal(profileToMigrate, null, "Should return default profile when that's the only one.");
 
-  fakeMigrator.sourceProfiles = [];
-  Assert.throws(() => AutoMigrate.pickProfile(fakeMigrator),
-                /No profile data found/,
-                "Should throw when no profile data is present.");
+  fakeMigrator._sourceProfiles = [];
+  await promiseThrows(() => AutoMigrate.pickProfile(fakeMigrator),
+                      /No profile data found/,
+                      "Should throw when no profile data is present.");
 
-  fakeMigrator.sourceProfiles = [{id: "a"}];
-  let profA = fakeMigrator.sourceProfiles[0];
-  profileToMigrate = AutoMigrate.pickProfile(fakeMigrator);
+  fakeMigrator._sourceProfiles = [{id: "a"}];
+  let profA = fakeMigrator._sourceProfiles[0];
+  profileToMigrate = await AutoMigrate.pickProfile(fakeMigrator);
   Assert.equal(profileToMigrate, profA, "Should return the only profile if only one is present.");
 });
 
 /**
  * Test the complete automatic process including browser and profile selection,
  * and actual migration (which implies startup)
  */
 add_task(async function checkIntegration() {
   gShimmedMigrator = {
-    get sourceProfiles() {
+    getSourceProfiles() {
       info("Read sourceProfiles");
       return null;
     },
     getMigrateData(profileToMigrate) {
       this._getMigrateDataArgs = profileToMigrate;
       return Ci.nsIBrowserProfileMigrator.BOOKMARKS;
     },
     migrate(types, startup, profileToMigrate) {
       this._migrateArgs = [types, startup, profileToMigrate];
     },
   };
   gShimmedMigratorKeyPicker = function() {
     return "gobbledygook";
   };
-  AutoMigrate.migrate("startup");
+  await AutoMigrate.migrate("startup");
   Assert.strictEqual(gShimmedMigrator._getMigrateDataArgs, null,
                      "getMigrateData called with 'null' as a profile");
 
   let {BOOKMARKS, HISTORY, PASSWORDS} = Ci.nsIBrowserProfileMigrator;
   let expectedTypes = BOOKMARKS | HISTORY | PASSWORDS;
   Assert.deepEqual(gShimmedMigrator._migrateArgs, [expectedTypes, "startup", null],
                    "migrate called with 'null' as a profile");
 });
 
 /**
  * Test the undo preconditions and a no-op undo in the automigrator.
  */
 add_task(async function checkUndoPreconditions() {
   let shouldAddData = false;
   gShimmedMigrator = {
-    get sourceProfiles() {
+    getSourceProfiles() {
       info("Read sourceProfiles");
       return null;
     },
     getMigrateData(profileToMigrate) {
       this._getMigrateDataArgs = profileToMigrate;
       return Ci.nsIBrowserProfileMigrator.BOOKMARKS;
     },
     migrate(types, startup, profileToMigrate) {
@@ -169,17 +183,17 @@ add_task(async function checkUndoPrecond
         Services.obs.notifyObservers(null, "Migration:Ended", undefined);
       });
     },
   };
 
   gShimmedMigratorKeyPicker = function() {
     return "gobbledygook";
   };
-  AutoMigrate.migrate("startup");
+  await AutoMigrate.migrate("startup");
   let migrationFinishedPromise = TestUtils.topicObserved("Migration:Ended");
   Assert.strictEqual(gShimmedMigrator._getMigrateDataArgs, null,
                      "getMigrateData called with 'null' as a profile");
 
   let {BOOKMARKS, HISTORY, PASSWORDS} = Ci.nsIBrowserProfileMigrator;
   let expectedTypes = BOOKMARKS | HISTORY | PASSWORDS;
   Assert.deepEqual(gShimmedMigrator._migrateArgs, [expectedTypes, "startup", null],
                    "migrate called with 'null' as a profile");
@@ -188,17 +202,17 @@ add_task(async function checkUndoPrecond
   Assert.ok(Preferences.has("browser.migrate.automigrate.browser"),
             "Should have set browser pref");
   Assert.ok(!(await AutoMigrate.canUndo()), "Should not be able to undo migration, as there's no data");
   gShimmedMigrator._migrateArgs = null;
   gShimmedMigrator._getMigrateDataArgs = null;
   Preferences.reset("browser.migrate.automigrate.browser");
   shouldAddData = true;
 
-  AutoMigrate.migrate("startup");
+  await AutoMigrate.migrate("startup");
   migrationFinishedPromise = TestUtils.topicObserved("Migration:Ended");
   Assert.strictEqual(gShimmedMigrator._getMigrateDataArgs, null,
                      "getMigrateData called with 'null' as a profile");
   Assert.deepEqual(gShimmedMigrator._migrateArgs, [expectedTypes, "startup", null],
                    "migrate called with 'null' as a profile");
 
   await migrationFinishedPromise;
   let storedLogins = Services.logins.findLogins({}, "www.mozilla.org",