|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 const TEST_URL = "data:text/html;charset=utf-8,<input%20id=txt>" + |
|
8 "<input%20type=checkbox%20id=chk>"; |
|
9 |
|
10 /** |
|
11 * This test ensures that closing a window is a reversible action. We will |
|
12 * close the the window, restore it and check that all data has been restored. |
|
13 * This includes window-specific data as well as form data for tabs. |
|
14 */ |
|
15 function test() { |
|
16 waitForExplicitFinish(); |
|
17 |
|
18 let uniqueKey = "bug 394759"; |
|
19 let uniqueValue = "unik" + Date.now(); |
|
20 let uniqueText = "pi != " + Math.random(); |
|
21 |
|
22 // Clear the list of closed windows. |
|
23 while (SessionStore.getClosedWindowCount()) { |
|
24 SessionStore.forgetClosedWindow(0); |
|
25 } |
|
26 |
|
27 provideWindow(function onTestURLLoaded(newWin) { |
|
28 newWin.gBrowser.addTab().linkedBrowser.stop(); |
|
29 |
|
30 // mark the window with some unique data to be restored later on |
|
31 ss.setWindowValue(newWin, uniqueKey, uniqueValue); |
|
32 let [txt, chk] = newWin.content.document.querySelectorAll("#txt, #chk"); |
|
33 txt.value = uniqueText; |
|
34 |
|
35 let browser = newWin.gBrowser.selectedBrowser; |
|
36 setInputChecked(browser, {id: "chk", checked: true}).then(() => { |
|
37 newWin.close(); |
|
38 |
|
39 // Now give it time to close |
|
40 executeSoon(function() { |
|
41 is(ss.getClosedWindowCount(), 1, |
|
42 "The closed window was added to Recently Closed Windows"); |
|
43 let data = JSON.parse(ss.getClosedWindowData())[0]; |
|
44 ok(data.title == TEST_URL && JSON.stringify(data).indexOf(uniqueText) > -1, |
|
45 "The closed window data was stored correctly"); |
|
46 |
|
47 // reopen the closed window and ensure its integrity |
|
48 let newWin2 = ss.undoCloseWindow(0); |
|
49 |
|
50 ok(newWin2 instanceof ChromeWindow, |
|
51 "undoCloseWindow actually returned a window"); |
|
52 is(ss.getClosedWindowCount(), 0, |
|
53 "The reopened window was removed from Recently Closed Windows"); |
|
54 |
|
55 // SSTabRestored will fire more than once, so we need to make sure we count them |
|
56 let restoredTabs = 0; |
|
57 let expectedTabs = data.tabs.length; |
|
58 newWin2.addEventListener("SSTabRestored", function sstabrestoredListener(aEvent) { |
|
59 ++restoredTabs; |
|
60 info("Restored tab " + restoredTabs + "/" + expectedTabs); |
|
61 if (restoredTabs < expectedTabs) { |
|
62 return; |
|
63 } |
|
64 |
|
65 is(restoredTabs, expectedTabs, "correct number of tabs restored"); |
|
66 newWin2.removeEventListener("SSTabRestored", sstabrestoredListener, true); |
|
67 |
|
68 is(newWin2.gBrowser.tabs.length, 2, |
|
69 "The window correctly restored 2 tabs"); |
|
70 is(newWin2.gBrowser.currentURI.spec, TEST_URL, |
|
71 "The window correctly restored the URL"); |
|
72 |
|
73 let [txt, chk] = newWin2.content.document.querySelectorAll("#txt, #chk"); |
|
74 ok(txt.value == uniqueText && chk.checked, |
|
75 "The window correctly restored the form"); |
|
76 is(ss.getWindowValue(newWin2, uniqueKey), uniqueValue, |
|
77 "The window correctly restored the data associated with it"); |
|
78 |
|
79 // clean up |
|
80 newWin2.close(); |
|
81 finish(); |
|
82 }, true); |
|
83 }); |
|
84 }); |
|
85 }, TEST_URL); |
|
86 } |
|
87 |
|
88 function setInputChecked(browser, data) { |
|
89 return sendMessage(browser, "ss-test:setInputChecked", data); |
|
90 } |