Bug 1335442 - Deal correctly with not importing anything. r=jaws, a=jcristau
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Fri, 03 Feb 2017 14:04:23 +0000
changeset 378605 5f643a348c3319f17db7ad787d6e626f76298571
parent 378604 13e44cc8e7c03838153da95a913e03ffb4eeb1e4
child 378606 67d78fdd865dedbc6049cb8da3ab2e4841ea88ca
push id1419
push userjlund@mozilla.com
push dateMon, 10 Apr 2017 20:44:07 +0000
treeherdermozilla-release@5e6801b73ef6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjaws, jcristau
bugs1335442
milestone53.0a2
Bug 1335442 - Deal correctly with not importing anything. r=jaws, a=jcristau 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");