Bug 1442694 - Add tests for preopened pinned tabs r=Gijs
authorDoug Thayer <dothayer@mozilla.com>
Fri, 01 Mar 2019 18:29:00 +0000
changeset 520115 3bd9591627cebd542f3ba78648ef641dfd70e446
parent 520114 59c8fffe9e41a68f37f5f98f762425b4c4fac9a0
child 520116 8b3fe0426ffc1b3a2ad044ef6cdde6c4f736f8e2
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersGijs
bugs1442694
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1442694 - Add tests for preopened pinned tabs r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D19463
browser/components/sessionstore/SessionStore.jsm
browser/components/sessionstore/test/browser.ini
browser/components/sessionstore/test/browser_preopened_apptabs.js
browser/components/sessionstore/test/head.js
--- a/browser/components/sessionstore/SessionStore.jsm
+++ b/browser/components/sessionstore/SessionStore.jsm
@@ -244,18 +244,18 @@ var SessionStore = {
   setBrowserState: function ss_setBrowserState(aState) {
     SessionStoreInternal.setBrowserState(aState);
   },
 
   getWindowState: function ss_getWindowState(aWindow) {
     return SessionStoreInternal.getWindowState(aWindow);
   },
 
-  setWindowState: function ss_setWindowState(aWindow, aState, aOverwrite) {
-    SessionStoreInternal.setWindowState(aWindow, aState, aOverwrite);
+  setWindowState: function ss_setWindowState(aWindow, aState, aOverwrite, aFirstWindow) {
+    SessionStoreInternal.setWindowState(aWindow, aState, aOverwrite, aFirstWindow);
   },
 
   getTabState: function ss_getTabState(aTab) {
     return SessionStoreInternal.getTabState(aTab);
   },
 
   setTabState: function ss_setTabState(aTab, aState) {
     SessionStoreInternal.setTabState(aTab, aState);
@@ -2495,22 +2495,25 @@ var SessionStoreInternal = {
     if (DyingWindowCache.has(aWindow)) {
       let data = DyingWindowCache.get(aWindow);
       return JSON.stringify({ windows: [data] });
     }
 
     throw Components.Exception("Window is not tracked", Cr.NS_ERROR_INVALID_ARG);
   },
 
-  setWindowState: function ssi_setWindowState(aWindow, aState, aOverwrite) {
+  setWindowState: function ssi_setWindowState(aWindow, aState, aOverwrite, aFirstWindow) {
     if (!aWindow.__SSi) {
       throw Components.Exception("Window is not tracked", Cr.NS_ERROR_INVALID_ARG);
     }
 
-    this.restoreWindows(aWindow, aState, {overwriteTabs: aOverwrite});
+    this.restoreWindows(aWindow, aState, {
+      overwriteTabs: aOverwrite,
+      firstWindow: aFirstWindow,
+    });
 
     // Notify of changes to closed objects.
     this._notifyOfClosedObjectsChange();
   },
 
   getTabState: function ssi_getTabState(aTab) {
     if (!aTab || !aTab.ownerGlobal) {
       throw Components.Exception("Need a valid tab", Cr.NS_ERROR_INVALID_ARG);
--- a/browser/components/sessionstore/test/browser.ini
+++ b/browser/components/sessionstore/test/browser.ini
@@ -108,16 +108,17 @@ support-files = file_formdata_password.h
 skip-if = (verify && (os == 'win' || os == 'mac'))
 [browser_global_store.js]
 [browser_history_persist.js]
 [browser_label_and_icon.js]
 [browser_merge_closed_tabs.js]
 [browser_old_favicon.js]
 [browser_page_title.js]
 [browser_pending_tabs.js]
+[browser_preopened_apptabs.js]
 [browser_privatetabs.js]
 [browser_purge_shistory.js]
 skip-if = e10s # Bug 1271024
 [browser_replace_load.js]
 [browser_restore_redirect.js]
 [browser_restore_cookies_noOriginAttributes.js]
 [browser_scrollPositions.js]
 [browser_scrollPositionsReaderMode.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/sessionstore/test/browser_preopened_apptabs.js
@@ -0,0 +1,138 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const PREF_RESTORE_ON_DEMAND = "browser.sessionstore.restore_on_demand";
+const PREF_NUM_PINNED_TABS = "browser.tabs.firstWindowRestore.numPinnedTabs";
+
+function muffleMainWindowType() {
+  let oldWinType = document.documentElement.getAttribute("windowtype");
+  // Check if we've already done this to allow calling multiple times:
+  if (oldWinType != "navigator:testrunner") {
+    // Make the main test window not count as a browser window any longer
+    document.documentElement.setAttribute("windowtype", "navigator:testrunner");
+
+    registerCleanupFunction(() => {
+      document.documentElement.setAttribute("windowtype", oldWinType);
+    });
+  }
+}
+
+registerCleanupFunction(function() {
+  Services.prefs.clearUserPref(PREF_NUM_PINNED_TABS);
+});
+
+add_task(async function testPrefSynced() {
+  Services.prefs.setIntPref(PREF_NUM_PINNED_TABS, 3);
+
+  let state = { windows: [{ tabs: [
+    { entries: [{ url: "http://example.org/#1", triggeringPrincipal_base64 }], pinned: true },
+    { entries: [{ url: "http://example.org/#2", triggeringPrincipal_base64 }], pinned: true },
+    { entries: [{ url: "http://example.org/#3", triggeringPrincipal_base64 }], pinned: true },
+  ], selected: 3 }] };
+
+  muffleMainWindowType();
+  let win = await BrowserTestUtils.openNewBrowserWindow();
+  Assert.equal([...win.gBrowser.tabs].filter(t => t.pinned).length, 3);
+  Assert.equal([...win.gBrowser.tabs].filter(t => !!t.linkedPanel).length, 1);
+  await setWindowState(win, state, false, true);
+  Assert.equal([...win.gBrowser.tabs].filter(t => t.pinned).length, 3);
+  Assert.equal([...win.gBrowser.tabs].filter(t => !!t.linkedPanel).length, 4);
+  await BrowserTestUtils.closeWindow(win);
+});
+
+add_task(async function testPrefSyncedSelected() {
+  Services.prefs.setIntPref(PREF_NUM_PINNED_TABS, 3);
+
+  let state = { windows: [{ tabs: [
+    { entries: [{ url: "http://example.org/#1", triggeringPrincipal_base64 }], pinned: true },
+    { entries: [{ url: "http://example.org/#2", triggeringPrincipal_base64 }], pinned: true },
+    { entries: [{ url: "http://example.org/#3", triggeringPrincipal_base64 }], pinned: true },
+  ], selected: 1 }] };
+
+  muffleMainWindowType();
+  let win = await BrowserTestUtils.openNewBrowserWindow();
+  Assert.equal([...win.gBrowser.tabs].filter(t => t.pinned).length, 3);
+  Assert.equal([...win.gBrowser.tabs].filter(t => !!t.linkedPanel).length, 1);
+  await setWindowState(win, state, false, true);
+  Assert.equal([...win.gBrowser.tabs].filter(t => t.pinned).length, 3);
+  Assert.equal([...win.gBrowser.tabs].filter(t => !!t.linkedPanel).length, 4);
+  await BrowserTestUtils.closeWindow(win);
+});
+
+add_task(async function testPrefTooHigh() {
+  Services.prefs.setIntPref(PREF_NUM_PINNED_TABS, 5);
+
+  let state = { windows: [{ tabs: [
+    { entries: [{ url: "http://example.org/#1", triggeringPrincipal_base64 }], pinned: true },
+    { entries: [{ url: "http://example.org/#2", triggeringPrincipal_base64 }], pinned: true },
+    { entries: [{ url: "http://example.org/#3", triggeringPrincipal_base64 }], pinned: true },
+  ], selected: 3 }] };
+
+  muffleMainWindowType();
+  let win = await BrowserTestUtils.openNewBrowserWindow();
+  Assert.equal([...win.gBrowser.tabs].filter(t => t.pinned).length, 5);
+  Assert.equal([...win.gBrowser.tabs].filter(t => !!t.linkedPanel).length, 1);
+  await setWindowState(win, state, false, true);
+  Assert.equal([...win.gBrowser.tabs].filter(t => t.pinned).length, 3);
+  Assert.equal([...win.gBrowser.tabs].filter(t => !!t.linkedPanel).length, 4);
+  await BrowserTestUtils.closeWindow(win);
+});
+
+add_task(async function testPrefTooLow() {
+  Services.prefs.setIntPref(PREF_NUM_PINNED_TABS, 1);
+
+  let state = { windows: [{ tabs: [
+    { entries: [{ url: "http://example.org/#1", triggeringPrincipal_base64 }], pinned: true },
+    { entries: [{ url: "http://example.org/#2", triggeringPrincipal_base64 }], pinned: true },
+    { entries: [{ url: "http://example.org/#3", triggeringPrincipal_base64 }], pinned: true },
+  ], selected: 3 }] };
+
+  muffleMainWindowType();
+  let win = await BrowserTestUtils.openNewBrowserWindow();
+  Assert.equal([...win.gBrowser.tabs].filter(t => t.pinned).length, 1);
+  Assert.equal([...win.gBrowser.tabs].filter(t => !!t.linkedPanel).length, 1);
+  await setWindowState(win, state, false, true);
+  Assert.equal([...win.gBrowser.tabs].filter(t => t.pinned).length, 3);
+  Assert.equal([...win.gBrowser.tabs].filter(t => !!t.linkedPanel).length, 4);
+  await BrowserTestUtils.closeWindow(win);
+});
+
+add_task(async function testPrefTooHighSelected() {
+  Services.prefs.setIntPref(PREF_NUM_PINNED_TABS, 5);
+
+  let state = { windows: [{ tabs: [
+    { entries: [{ url: "http://example.org/#1", triggeringPrincipal_base64 }], pinned: true },
+    { entries: [{ url: "http://example.org/#2", triggeringPrincipal_base64 }], pinned: true },
+    { entries: [{ url: "http://example.org/#3", triggeringPrincipal_base64 }], pinned: true },
+  ], selected: 1 }] };
+
+  muffleMainWindowType();
+  let win = await BrowserTestUtils.openNewBrowserWindow();
+  Assert.equal([...win.gBrowser.tabs].filter(t => t.pinned).length, 5);
+  Assert.equal([...win.gBrowser.tabs].filter(t => !!t.linkedPanel).length, 1);
+  await setWindowState(win, state, false, true);
+  Assert.equal([...win.gBrowser.tabs].filter(t => t.pinned).length, 3);
+  Assert.equal([...win.gBrowser.tabs].filter(t => !!t.linkedPanel).length, 4);
+  await BrowserTestUtils.closeWindow(win);
+});
+
+add_task(async function testPrefTooLowSelected() {
+  Services.prefs.setIntPref(PREF_NUM_PINNED_TABS, 1);
+
+  let state = { windows: [{ tabs: [
+    { entries: [{ url: "http://example.org/#1", triggeringPrincipal_base64 }], pinned: true },
+    { entries: [{ url: "http://example.org/#2", triggeringPrincipal_base64 }], pinned: true },
+    { entries: [{ url: "http://example.org/#3", triggeringPrincipal_base64 }], pinned: true },
+  ], selected: 1 }] };
+
+  muffleMainWindowType();
+  let win = await BrowserTestUtils.openNewBrowserWindow();
+  Assert.equal([...win.gBrowser.tabs].filter(t => t.pinned).length, 1);
+  Assert.equal([...win.gBrowser.tabs].filter(t => !!t.linkedPanel).length, 1);
+  await setWindowState(win, state, false, true);
+  Assert.equal([...win.gBrowser.tabs].filter(t => t.pinned).length, 3);
+  Assert.equal([...win.gBrowser.tabs].filter(t => !!t.linkedPanel).length, 4);
+  await BrowserTestUtils.closeWindow(win);
+});
--- a/browser/components/sessionstore/test/head.js
+++ b/browser/components/sessionstore/test/head.js
@@ -185,18 +185,19 @@ function promiseWindowRestored(win) {
   return new Promise(resolve => win.addEventListener("SSWindowRestored", resolve, {once: true}));
 }
 
 async function setBrowserState(state, win = window) {
   ss.setBrowserState(typeof state != "string" ? JSON.stringify(state) : state);
   await promiseWindowRestored(win);
 }
 
-async function setWindowState(win, state, overwrite = false) {
-  ss.setWindowState(win, typeof state != "string" ? JSON.stringify(state) : state, overwrite);
+async function setWindowState(win, state, overwrite = false, firstWindow = false) {
+  ss.setWindowState(win, typeof state != "string" ? JSON.stringify(state) : state,
+                    overwrite, firstWindow);
   await promiseWindowRestored(win);
 }
 
 /**
  * Wait for a content -> chrome message.
  */
 function promiseContentMessage(browser, name) {
   let mm = browser.messageManager;