Bug 1322730 - Display undo migration message on about:newtab. r=Dolske, a=lizzard
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Wed, 04 Jan 2017 19:05:50 +0000
changeset 359312 fd57670a2f39d1d7c74548879dd378d3efe86e46
parent 359311 544087dfc01d20c574eeaa8a066089e21df98ea7
child 359313 d78b42d605ca8a6e074db2374978452df508a0d8
push id1324
push usermtabara@mozilla.com
push dateMon, 16 Jan 2017 13:07:44 +0000
treeherdermozilla-release@a01c49833940 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersDolske, lizzard
bugs1322730
milestone51.0
Bug 1322730 - Display undo migration message on about:newtab. r=Dolske, a=lizzard MozReview-Commit-ID: 8nIGdD9XyFH
browser/base/content/newtab/page.js
browser/components/migration/AutoMigrate.jsm
browser/components/migration/moz.build
browser/components/migration/tests/browser/.eslintrc.js
browser/components/migration/tests/browser/browser.ini
browser/components/migration/tests/browser/browser_undo_notification.js
browser/modules/AboutNewTab.jsm
--- a/browser/base/content/newtab/page.js
+++ b/browser/base/content/newtab/page.js
@@ -255,16 +255,18 @@ var gPage = {
     } else {
       addEventListener("load", this);
     }
   },
 
   onPageVisibleAndLoaded() {
     // Send the index of the last visible tile.
     this.reportLastVisibleTileIndex();
+    // Maybe tell the user they can undo an initial automigration
+    this.maybeShowAutoMigrationUndoNotification();
   },
 
   reportLastVisibleTileIndex() {
     let cwu = window.QueryInterface(Ci.nsIInterfaceRequestor)
                     .getInterface(Ci.nsIDOMWindowUtils);
 
     let rect = cwu.getBoundsWithoutFlushing(gGrid.node);
     let nodes = cwu.nodesFromRect(rect.left, rect.top, 0, rect.width,
@@ -282,10 +284,14 @@ var gPage = {
             // record that suggested tile is shown to use suggested-tiles-histogram
             this._suggestedTilePresent = true;
           }
         }
       }
     }
 
     DirectoryLinksProvider.reportSitesAction(sites, "view", lastIndex);
-  }
+  },
+
+  maybeShowAutoMigrationUndoNotification() {
+    sendAsyncMessage("NewTab:MaybeShowAutoMigrationUndoNotification");
+  },
 };
--- a/browser/components/migration/AutoMigrate.jsm
+++ b/browser/components/migration/AutoMigrate.jsm
@@ -11,17 +11,17 @@ const { classes: Cc, interfaces: Ci, res
 const kAutoMigrateEnabledPref = "browser.migrate.automigrate.enabled";
 const kUndoUIEnabledPref = "browser.migrate.automigrate.ui.enabled";
 
 const kAutoMigrateBrowserPref = "browser.migrate.automigrate.browser";
 
 const kAutoMigrateLastUndoPromptDateMsPref = "browser.migrate.automigrate.lastUndoPromptDateMs";
 const kAutoMigrateDaysToOfferUndoPref = "browser.migrate.automigrate.daysToOfferUndo";
 
-const kNotificationId = "abouthome-automigration-undo";
+const kNotificationId = "automigration-undo";
 
 Cu.import("resource:///modules/MigrationUtils.jsm");
 Cu.import("resource://gre/modules/Preferences.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/Task.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "AsyncShutdown",
                                   "resource://gre/modules/AsyncShutdown.jsm");
@@ -250,24 +250,25 @@ const AutoMigrate = {
   },
 
   maybeShowUndoNotification: Task.async(function* (target) {
     if (!(yield this.canUndo())) {
       return;
     }
 
     // The tab might have navigated since we requested the undo state:
-    if (target.currentURI.spec != "about:home" ||
+    let canUndoFromThisPage = ["about:home", "about:newtab"].includes(target.currentURI.spec);
+    if (!canUndoFromThisPage ||
         !Preferences.get(kUndoUIEnabledPref, false)) {
       return;
     }
 
     let win = target.ownerGlobal;
     let notificationBox = win.gBrowser.getNotificationBox(target);
-    if (!notificationBox || notificationBox.getNotificationWithValue("abouthome-automigration-undo")) {
+    if (!notificationBox || notificationBox.getNotificationWithValue(kNotificationId)) {
       return;
     }
 
     // At this stage we're committed to show the prompt - unless we shouldn't,
     // in which case we remove the undo prefs (which will cause canUndo() to
     // return false from now on.):
     if (!this.shouldStillShowUndoPrompt()) {
       this.removeUndoOption(this.UNDO_REMOVED_REASON_OFFER_EXPIRED);
--- a/browser/components/migration/moz.build
+++ b/browser/components/migration/moz.build
@@ -3,16 +3,18 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 XPCSHELL_TESTS_MANIFESTS += ['tests/unit/xpcshell.ini']
 
 MARIONETTE_UNIT_MANIFESTS += ['tests/marionette/manifest.ini']
 
+BROWSER_CHROME_MANIFESTS += [ 'tests/browser/browser.ini']
+
 JAR_MANIFESTS += ['jar.mn']
 
 XPIDL_SOURCES += [
     'nsIBrowserProfileMigrator.idl',
 ]
 
 XPIDL_MODULE = 'migration'
 
new file mode 100644
--- /dev/null
+++ b/browser/components/migration/tests/browser/.eslintrc.js
@@ -0,0 +1,9 @@
+"use strict";
+
+module.exports = {
+  "extends": [
+    "../../../../../testing/mochitest/browser.eslintrc.js",
+    "../../../../../testing/mochitest/mochitest.eslintrc.js",
+  ]
+};
+
new file mode 100644
--- /dev/null
+++ b/browser/components/migration/tests/browser/browser.ini
@@ -0,0 +1,1 @@
+[browser_undo_notification.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/migration/tests/browser/browser_undo_notification.js
@@ -0,0 +1,60 @@
+"use strict";
+
+let scope = {};
+Cu.import("resource:///modules/AutoMigrate.jsm", scope);
+let oldCanUndo = scope.AutoMigrate.canUndo;
+let oldUndo = scope.AutoMigrate.undo;
+registerCleanupFunction(function() {
+  Cu.reportError("Cleaning up");
+  scope.AutoMigrate.canUndo = oldCanUndo;
+  scope.AutoMigrate.undo = oldUndo;
+  Cu.reportError("Cleaned up");
+});
+
+const kExpectedNotificationId = "automigration-undo";
+
+add_task(function* autoMigrationUndoNotificationShows() {
+  let getNotification = browser =>
+    gBrowser.getNotificationBox(browser).getNotificationWithValue(kExpectedNotificationId);
+
+  scope.AutoMigrate.canUndo = () => true;
+  let undoCalled;
+  scope.AutoMigrate.undo = () => { undoCalled = true };
+  for (let url of ["about:newtab", "about:home"]) {
+    undoCalled = false;
+    // Can't use pushPrefEnv because of bug 1323779
+    Services.prefs.setCharPref("browser.migrate.automigrate.browser", "someunknownbrowser");
+    let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, url, false);
+    let browser = tab.linkedBrowser;
+    if (!getNotification(browser)) {
+      info(`Notification for ${url} not immediately present, waiting for it.`);
+      yield BrowserTestUtils.waitForNotificationBar(gBrowser, browser, kExpectedNotificationId);
+    }
+
+    ok(true, `Got notification for ${url}`);
+    let notification = getNotification(browser);
+    let notificationBox = notification.parentNode;
+    notification.querySelector("button.notification-button-default").click();
+    ok(!undoCalled, "Undo should not be called when clicking the default button");
+    is(notification, notificationBox._closedNotification, "Notification should be closing");
+    yield BrowserTestUtils.removeTab(tab);
+
+    undoCalled = false;
+    Services.prefs.setCharPref("browser.migrate.automigrate.browser", "someunknownbrowser");
+    tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, url, false);
+    browser = tab.linkedBrowser;
+    if (!getNotification(browser)) {
+      info(`Notification for ${url} not immediately present, waiting for it.`);
+      yield BrowserTestUtils.waitForNotificationBar(gBrowser, browser, kExpectedNotificationId);
+    }
+
+    ok(true, `Got notification for ${url}`);
+    notification = getNotification(browser);
+    notificationBox = notification.parentNode;
+    notification.querySelector("button:not(.notification-button-default)").click();
+    ok(undoCalled, "Undo should be called when clicking the non-default (Don't Keep) button");
+    is(notification, notificationBox._closedNotification, "Notification should be closing");
+    yield BrowserTestUtils.removeTab(tab);
+  }
+});
+
--- a/browser/modules/AboutNewTab.jsm
+++ b/browser/modules/AboutNewTab.jsm
@@ -8,28 +8,32 @@ var Cc = Components.classes;
 var Ci = Components.interfaces;
 var Cu = Components.utils;
 
 this.EXPORTED_SYMBOLS = [ "AboutNewTab" ];
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 
+XPCOMUtils.defineLazyModuleGetter(this, "AutoMigrate",
+  "resource:///modules/AutoMigrate.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "NewTabUtils",
+  "resource://gre/modules/NewTabUtils.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "RemotePages",
   "resource://gre/modules/RemotePageManager.jsm");
-XPCOMUtils.defineLazyModuleGetter(this, "NewTabUtils",
-  "resource://gre/modules/NewTabUtils.jsm");
 
 var AboutNewTab = {
 
   pageListener: null,
 
   init: function() {
     this.pageListener = new RemotePages("about:newtab");
     this.pageListener.addMessageListener("NewTab:Customize", this.customize.bind(this));
+    this.pageListener.addMessageListener("NewTab:MaybeShowAutoMigrationUndoNotification",
+      (msg) => AutoMigrate.maybeShowUndoNotification(msg.target.browser));
   },
 
   customize: function(message) {
     NewTabUtils.allPages.enabled = message.data.enabled;
     NewTabUtils.allPages.enhanced = message.data.enhanced;
   },
 
   uninit: function() {