Bug 1335442 - deal correctly with not importing anything, r=jaws draft
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Fri, 03 Feb 2017 14:04:23 +0000
changeset 482223 f4ac27b2d9e26b8d9330e7b4839ccd05286c1098
parent 480952 f505911eb333d5ae8c2bf5c44f7b85add6450b53
child 482224 6e3818fe8f0e739ba01852f4e152950ed880aea3
push id45037
push usergijskruitbosch@gmail.com
push dateSat, 11 Feb 2017 18:17:53 +0000
reviewersjaws
bugs1335442
milestone54.0a1
Bug 1335442 - deal correctly with not importing anything, r=jaws MozReview-Commit-ID: 3WZCxXV48Ms
browser/components/migration/AutoMigrate.jsm
browser/components/migration/tests/unit/test_automigration.js
--- a/browser/components/migration/AutoMigrate.jsm
+++ b/browser/components/migration/AutoMigrate.jsm
@@ -412,32 +412,42 @@ const AutoMigrate = {
       logins: state.get("logins"),
       visits: state.get("visits"),
     };
     return JSON.stringify(serializableState);
   },
 
   _dejsonifyUndoState(state) {
     state = JSON.parse(state);
+    if (!state) {
+      return new Map();
+    }
     for (let bm of state.bookmarks) {
       bm.lastModified = new Date(bm.lastModified);
     }
     return new Map([
       ["bookmarks", state.bookmarks],
       ["logins", state.logins],
       ["visits", state.visits],
     ]);
   },
 
   _saveUndoStateTrackerForShutdown: "not running",
   saveUndoState: Task.async(function* () {
     let resolveSavingPromise;
     this._saveUndoStateTrackerForShutdown = "processing undo history";
     this._savingPromise = new Promise(resolve => { resolveSavingPromise = resolve });
     let state = yield MigrationUtils.stopAndRetrieveUndoData();
+
+    if (!state || ![...state.values()].some(ary => ary.length > 0)) {
+      // If we didn't import anything, abort now.
+      resolveSavingPromise();
+      return Promise.resolve();
+    }
+
     this._saveUndoStateTrackerForShutdown = "writing undo history";
     this._undoSavePromise = OS.File.writeAtomic(
       kUndoStateFullPath, this._jsonifyUndoState(state), {
         encoding: "utf-8",
         compression: "lz4",
         tmpPath: kUndoStateFullPath + ".tmp",
       });
     this._undoSavePromise.then(
--- a/browser/components/migration/tests/unit/test_automigration.js
+++ b/browser/components/migration/tests/unit/test_automigration.js
@@ -139,27 +139,37 @@ add_task(function* checkIntegration() {
   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(function* checkUndoPreconditions() {
+  let shouldAddData = false;
   gShimmedMigrator = {
     get sourceProfiles() {
       do_print("Read sourceProfiles");
       return null;
     },
     getMigrateData(profileToMigrate) {
       this._getMigrateDataArgs = profileToMigrate;
       return Ci.nsIBrowserProfileMigrator.BOOKMARKS;
     },
     migrate(types, startup, profileToMigrate) {
       this._migrateArgs = [types, startup, profileToMigrate];
+      if (shouldAddData) {
+        // Insert a login and check that that worked.
+        MigrationUtils.insertLoginWrapper({
+          hostname: "www.mozilla.org",
+          formSubmitURL: "http://www.mozilla.org",
+          username: "user",
+          password: "pass",
+        });
+      }
       TestUtils.executeSoon(function() {
         Services.obs.notifyObservers(null, "Migration:Ended", undefined);
       });
     },
   };
 
   gShimmedMigratorKeyPicker = function() {
     return "gobbledygook";
@@ -172,20 +182,45 @@ add_task(function* checkUndoPrecondition
   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");
 
   yield migrationFinishedPromise;
   Assert.ok(Preferences.has("browser.migrate.automigrate.browser"),
             "Should have set browser pref");
-  Assert.ok((yield AutoMigrate.canUndo()), "Should be able to undo migration");
+  Assert.ok(!(yield 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");
+  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");
+
+  yield migrationFinishedPromise;
+  let storedLogins = Services.logins.findLogins({}, "www.mozilla.org",
+                                                "http://www.mozilla.org", null);
+  Assert.equal(storedLogins.length, 1, "Should have 1 login");
+
+  Assert.ok(Preferences.has("browser.migrate.automigrate.browser"),
+            "Should have set browser pref");
+  Assert.ok((yield AutoMigrate.canUndo()), "Should be able to undo migration, as now there's data");
 
   yield AutoMigrate.undo();
   Assert.ok(true, "Should be able to finish an undo cycle.");
+
+  // Check that the undo removed the passwords:
+  storedLogins = Services.logins.findLogins({}, "www.mozilla.org",
+                                                "http://www.mozilla.org", null);
+  Assert.equal(storedLogins.length, 0, "Should have no logins");
 });
 
 /**
  * Fake a migration and then try to undo it to verify all data gets removed.
  */
 add_task(function* checkUndoRemoval() {
   MigrationUtils.initializeUndoData();
   Preferences.set("browser.migrate.automigrate.browser", "automationbrowser");