Bug 1378703 - Make browser_tab_label_during_restore.js more robust and clean up browser_label_and_icon.js. r?cpeterson draft
authorDão Gottwald <dao@mozilla.com>
Thu, 06 Jul 2017 11:40:42 +0200
changeset 604698 99ece66fa6ebda29a872d70478652b87829500cc
parent 604481 af0466865a212c84fbbab343c9cbb984c6132920
child 636275 04244329a9289d56be37549a53998c3e8c4fb366
push id67170
push userdgottwald@mozilla.com
push dateThu, 06 Jul 2017 09:41:20 +0000
reviewerscpeterson
bugs1378703
milestone56.0a1
Bug 1378703 - Make browser_tab_label_during_restore.js more robust and clean up browser_label_and_icon.js. r?cpeterson MozReview-Commit-ID: JaGqbumotIn
browser/components/sessionstore/test/browser_label_and_icon.js
browser/components/sessionstore/test/browser_tab_label_during_restore.js
--- a/browser/components/sessionstore/test/browser_label_and_icon.js
+++ b/browser/components/sessionstore/test/browser_label_and_icon.js
@@ -1,31 +1,23 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
-const {classes: Cc, interfaces: Ci} = Components;
-
-/**
- * Make sure that tabs are restored on demand as otherwise the tab will start
- * loading immediately and we can't check its icon and label.
- */
-add_task(function setup() {
-  Services.prefs.setBoolPref("browser.sessionstore.restore_on_demand", true);
-
-  registerCleanupFunction(() => {
-    Services.prefs.clearUserPref("browser.sessionstore.restore_on_demand");
-  });
-});
-
 /**
  * Ensure that a pending tab has label and icon correctly set.
  */
 add_task(async function test_label_and_icon() {
+  // Make sure that tabs are restored on demand as otherwise the tab will start
+  // loading immediately and we can't check its icon and label.
+  await SpecialPowers.pushPrefEnv({
+    set: [["browser.sessionstore.restore_on_demand", true]],
+  });
+
   // Create a new tab.
   let tab = BrowserTestUtils.addTab(gBrowser, "about:robots");
   let browser = tab.linkedBrowser;
   await promiseBrowserLoaded(browser);
 
   // Retrieve the tab state.
   await TabStateFlusher.flush(browser);
   let state = ss.getTabState(tab);
--- a/browser/components/sessionstore/test/browser_tab_label_during_restore.js
+++ b/browser/components/sessionstore/test/browser_tab_label_during_restore.js
@@ -15,29 +15,27 @@ add_task(async function() {
     ]
   });
 
   const BACKUP_STATE = SessionStore.getBrowserState();
   const TEST_URL = "http://www.example.com/";
   const ABOUT_ROBOTS_URI = "about:robots";
   const ABOUT_ROBOTS_TITLE = "Gort! Klaatu barada nikto!";
 
-  function observeLabelChanges(tab) {
-    info("observing tab label changes. initial label: " + tab.label);
-    let labelChangeCount = 0;
+  function observeLabelChanges(tab, expectedLabels) {
+    let seenLabels = [tab.label];
     function TabAttrModifiedListener(event) {
-      if (event.detail.changed.some(attr => { return attr == "label" })) {
-        info("tab label change: " + tab.label);
-        labelChangeCount++;
+      if (event.detail.changed.some(attr => attr == "label")) {
+        seenLabels.push(tab.label);
       }
     }
     tab.addEventListener("TabAttrModified", TabAttrModifiedListener);
-    return (expectedCount) => {
+    return () => {
       tab.removeEventListener("TabAttrModified", TabAttrModifiedListener);
-      is(labelChangeCount, expectedCount, "observed tab label changes");
+      is(JSON.stringify(seenLabels), JSON.stringify(expectedLabels || []), "observed tab label changes");
     }
   }
 
   info("setting test browser state");
   let browserLoadedPromise = BrowserTestUtils.firstBrowserLoaded(window, false);
   await promiseBrowserState({
     windows: [{
       tabs: [
@@ -54,61 +52,55 @@ add_task(async function() {
   const CONTENT_TITLE = firstTab.linkedBrowser.contentTitle;
   is(firstTab.linkedBrowser.currentURI.spec, TEST_URL, "correct URL loaded in first tab");
   is(typeof CONTENT_TITLE, "string", "content title is a string");
   isnot(CONTENT_TITLE.length, 0, "content title isn't empty");
   isnot(CONTENT_TITLE, TEST_URL, "content title is different from the URL");
   is(firstTab.label, CONTENT_TITLE, "first tab displays content title");
   ok(document.title.startsWith(CONTENT_TITLE), "title bar displays content title");
   ok(secondTab.hasAttribute("pending"), "second tab is pending");
+  ok(thirdTab.hasAttribute("pending"), "third tab is pending");
+
+  info("selecting the second tab");
   // The fix for bug 1364127 caused about: pages' initial tab titles to show
   // their about: URIs until their actual page titles are known, e.g.
   // "about:addons" -> "Add-ons Manager". This is bug 1371896. Previously,
   // about: pages' initial tab titles were blank until the page title was known.
-  is(secondTab.label, ABOUT_ROBOTS_URI, "second tab displays URI as its initial title");
-  ok(thirdTab.hasAttribute("pending"), "third tab is pending");
-  is(thirdTab.label, "example.com", "third tab displays hostname without www as its initial title");
-
-  info("selecting the second tab");
-  let checkLabelChangeCount = observeLabelChanges(secondTab);
+  let finishObservingLabelChanges = observeLabelChanges(secondTab, [ABOUT_ROBOTS_URI, ABOUT_ROBOTS_TITLE]);
   browserLoadedPromise = BrowserTestUtils.browserLoaded(secondTab.linkedBrowser, false, ABOUT_ROBOTS_URI);
   gBrowser.selectedTab = secondTab;
   await browserLoadedPromise;
   ok(!secondTab.hasAttribute("pending"), "second tab isn't pending anymore");
-  is(secondTab.label, ABOUT_ROBOTS_TITLE, "second tab displays content title");
   ok(document.title.startsWith(ABOUT_ROBOTS_TITLE), "title bar displays content title");
-  checkLabelChangeCount(1); // ABOUT_ROBOTS_URI -> ABOUT_ROBOTS_TITLE
+  finishObservingLabelChanges();
 
   info("selecting the third tab");
-  checkLabelChangeCount = observeLabelChanges(thirdTab);
+  finishObservingLabelChanges = observeLabelChanges(thirdTab, ["example.com", CONTENT_TITLE]);
   browserLoadedPromise = BrowserTestUtils.browserLoaded(thirdTab.linkedBrowser, false, TEST_URL);
   gBrowser.selectedTab = thirdTab;
   await browserLoadedPromise;
   ok(!thirdTab.hasAttribute("pending"), "third tab isn't pending anymore");
-  is(thirdTab.label, CONTENT_TITLE, "third tab displays content title");
   ok(document.title.startsWith(CONTENT_TITLE), "title bar displays content title");
-  checkLabelChangeCount(1); // TEST_URL -> CONTENT_TITLE
+  finishObservingLabelChanges();
 
   info("restoring the modified browser state");
   await TabStateFlusher.flushWindow(window);
   await promiseBrowserState(SessionStore.getBrowserState());
   [firstTab, secondTab, thirdTab] = gBrowser.tabs;
   is(thirdTab, gBrowser.selectedTab, "third tab is selected after restoring");
   ok(document.title.startsWith(CONTENT_TITLE), "title bar displays content title");
   ok(firstTab.hasAttribute("pending"), "first tab is pending after restoring");
-  is(firstTab.label, CONTENT_TITLE, "first tab displays content title in pending state");
   ok(secondTab.hasAttribute("pending"), "second tab is pending after restoring");
   is(secondTab.label, ABOUT_ROBOTS_TITLE, "second tab displays content title");
   ok(!thirdTab.hasAttribute("pending"), "third tab is not pending after restoring");
   is(thirdTab.label, CONTENT_TITLE, "third tab displays content title in pending state");
 
   info("selecting the first tab");
-  checkLabelChangeCount = observeLabelChanges(firstTab);
+  finishObservingLabelChanges = observeLabelChanges(firstTab, [CONTENT_TITLE]);
   let tabContentRestored = TestUtils.topicObserved("sessionstore-debug-tab-restored");
   gBrowser.selectedTab = firstTab;
   ok(document.title.startsWith(CONTENT_TITLE), "title bar displays content title");
   await tabContentRestored;
   ok(!firstTab.hasAttribute("pending"), "first tab isn't pending anymore");
-  checkLabelChangeCount(0);
-  is(firstTab.label, CONTENT_TITLE, "first tab displays content title after restoring content");
+  finishObservingLabelChanges();
 
   await promiseBrowserState(BACKUP_STATE);
 });