fune/browser/components/sessionstore/test/browser_undoCloseById.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

119 lines
4.8 KiB
JavaScript

/* eslint-disable mozilla/no-arbitrary-setTimeout */
"use strict";
/**
* This test is for the undoCloseById function.
*/
const {SessionStore} = ChromeUtils.import("resource:///modules/sessionstore/SessionStore.jsm");
async function openAndCloseTab(window, url) {
let tab = BrowserTestUtils.addTab(window.gBrowser, url);
await promiseBrowserLoaded(tab.linkedBrowser, true, url);
await TabStateFlusher.flush(tab.linkedBrowser);
await promiseRemoveTabAndSessionState(tab);
}
async function openWindow(url) {
let win = await promiseNewWindowLoaded();
let flags = Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY;
BrowserTestUtils.loadURI(win.gBrowser.selectedBrowser, url, { flags });
await promiseBrowserLoaded(win.gBrowser.selectedBrowser, true, url);
return win;
}
async function closeWindow(win) {
await BrowserTestUtils.closeWindow(win);
// Wait 20 ms to allow SessionStorage a chance to register the closed window.
await new Promise(resolve => setTimeout(resolve, 20));
}
add_task(async function test_undoCloseById() {
// Clear the lists of closed windows and tabs.
forgetClosedWindows();
while (SessionStore.getClosedTabCount(window)) {
SessionStore.forgetClosedTab(window, 0);
}
// Open a new window.
let win = await openWindow("about:robots");
// Open and close a tab.
await openAndCloseTab(win, "about:mozilla");
is(SessionStore.lastClosedObjectType, "tab", "The last closed object is a tab");
// Record the first closedId created.
let initialClosedId = SessionStore.getClosedTabData(win, false)[0].closedId;
// Open and close another window.
let win2 = await openWindow("about:mozilla");
await closeWindow(win2); // closedId == initialClosedId + 1
is(SessionStore.lastClosedObjectType, "window", "The last closed object is a window");
// Open and close another tab in the first window.
await openAndCloseTab(win, "about:robots"); // closedId == initialClosedId + 2
is(SessionStore.lastClosedObjectType, "tab", "The last closed object is a tab");
// Undo closing the second tab.
let tab = SessionStore.undoCloseById(initialClosedId + 2);
await promiseBrowserLoaded(tab.linkedBrowser);
is(tab.linkedBrowser.currentURI.spec, "about:robots", "The expected tab was re-opened");
let notTab = SessionStore.undoCloseById(initialClosedId + 2);
is(notTab, undefined, "Re-opened tab cannot be unClosed again by closedId");
// Now the last closed object should be a window again.
is(SessionStore.lastClosedObjectType, "window", "The last closed object is a window");
// Undo closing the first tab.
let tab2 = SessionStore.undoCloseById(initialClosedId);
await promiseBrowserLoaded(tab2.linkedBrowser);
is(tab2.linkedBrowser.currentURI.spec, "about:mozilla", "The expected tab was re-opened");
// Close the two tabs we re-opened.
await promiseRemoveTabAndSessionState(tab); // closedId == initialClosedId + 3
is(SessionStore.lastClosedObjectType, "tab", "The last closed object is a tab");
await promiseRemoveTabAndSessionState(tab2); // closedId == initialClosedId + 4
is(SessionStore.lastClosedObjectType, "tab", "The last closed object is a tab");
// Open another new window.
let win3 = await openWindow("about:mozilla");
// Close both windows.
await closeWindow(win); // closedId == initialClosedId + 5
is(SessionStore.lastClosedObjectType, "window", "The last closed object is a window");
await closeWindow(win3); // closedId == initialClosedId + 6
is(SessionStore.lastClosedObjectType, "window", "The last closed object is a window");
// Undo closing the second window.
win = SessionStore.undoCloseById(initialClosedId + 6);
await BrowserTestUtils.waitForEvent(win, "load");
// Make sure we wait until this window is restored.
await BrowserTestUtils.waitForEvent(win.gBrowser.tabContainer,
"SSTabRestored");
is(win.gBrowser.selectedBrowser.currentURI.spec, "about:mozilla", "The expected window was re-opened");
let notWin = SessionStore.undoCloseById(initialClosedId + 6);
is(notWin, undefined, "Re-opened window cannot be unClosed again by closedId");
// Close the window again.
await closeWindow(win);
is(SessionStore.lastClosedObjectType, "window", "The last closed object is a window");
// Undo closing the first window.
win = SessionStore.undoCloseById(initialClosedId + 5);
await BrowserTestUtils.waitForEvent(win, "load");
// Make sure we wait until this window is restored.
await BrowserTestUtils.waitForEvent(win.gBrowser.tabContainer,
"SSTabRestored");
is(win.gBrowser.selectedBrowser.currentURI.spec, "about:robots", "The expected window was re-opened");
// Close the window again.
await closeWindow(win);
is(SessionStore.lastClosedObjectType, "window", "The last closed object is a window");
});