browser/components/sessionstore/test/browser_394759_basic.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sessionstore/test/browser_394759_basic.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const TEST_URL = "data:text/html;charset=utf-8,<input%20id=txt>" +
    1.11 +                 "<input%20type=checkbox%20id=chk>";
    1.12 +
    1.13 +/**
    1.14 + * This test ensures that closing a window is a reversible action. We will
    1.15 + * close the the window, restore it and check that all data has been restored.
    1.16 + * This includes window-specific data as well as form data for tabs.
    1.17 + */
    1.18 +function test() {
    1.19 +  waitForExplicitFinish();
    1.20 +
    1.21 +  let uniqueKey = "bug 394759";
    1.22 +  let uniqueValue = "unik" + Date.now();
    1.23 +  let uniqueText = "pi != " + Math.random();
    1.24 +
    1.25 +  // Clear the list of closed windows.
    1.26 +  while (SessionStore.getClosedWindowCount()) {
    1.27 +    SessionStore.forgetClosedWindow(0);
    1.28 +  }
    1.29 +
    1.30 +  provideWindow(function onTestURLLoaded(newWin) {
    1.31 +    newWin.gBrowser.addTab().linkedBrowser.stop();
    1.32 +
    1.33 +    // mark the window with some unique data to be restored later on
    1.34 +    ss.setWindowValue(newWin, uniqueKey, uniqueValue);
    1.35 +    let [txt, chk] = newWin.content.document.querySelectorAll("#txt, #chk");
    1.36 +    txt.value = uniqueText;
    1.37 +
    1.38 +    let browser = newWin.gBrowser.selectedBrowser;
    1.39 +    setInputChecked(browser, {id: "chk", checked: true}).then(() => {
    1.40 +      newWin.close();
    1.41 +
    1.42 +      // Now give it time to close
    1.43 +      executeSoon(function() {
    1.44 +        is(ss.getClosedWindowCount(), 1,
    1.45 +           "The closed window was added to Recently Closed Windows");
    1.46 +        let data = JSON.parse(ss.getClosedWindowData())[0];
    1.47 +        ok(data.title == TEST_URL && JSON.stringify(data).indexOf(uniqueText) > -1,
    1.48 +           "The closed window data was stored correctly");
    1.49 +
    1.50 +        // reopen the closed window and ensure its integrity
    1.51 +        let newWin2 = ss.undoCloseWindow(0);
    1.52 +
    1.53 +        ok(newWin2 instanceof ChromeWindow,
    1.54 +           "undoCloseWindow actually returned a window");
    1.55 +        is(ss.getClosedWindowCount(), 0,
    1.56 +           "The reopened window was removed from Recently Closed Windows");
    1.57 +
    1.58 +        // SSTabRestored will fire more than once, so we need to make sure we count them
    1.59 +        let restoredTabs = 0;
    1.60 +        let expectedTabs = data.tabs.length;
    1.61 +        newWin2.addEventListener("SSTabRestored", function sstabrestoredListener(aEvent) {
    1.62 +          ++restoredTabs;
    1.63 +          info("Restored tab " + restoredTabs + "/" + expectedTabs);
    1.64 +          if (restoredTabs < expectedTabs) {
    1.65 +            return;
    1.66 +          }
    1.67 +
    1.68 +          is(restoredTabs, expectedTabs, "correct number of tabs restored");
    1.69 +          newWin2.removeEventListener("SSTabRestored", sstabrestoredListener, true);
    1.70 +
    1.71 +          is(newWin2.gBrowser.tabs.length, 2,
    1.72 +             "The window correctly restored 2 tabs");
    1.73 +          is(newWin2.gBrowser.currentURI.spec, TEST_URL,
    1.74 +             "The window correctly restored the URL");
    1.75 +
    1.76 +          let [txt, chk] = newWin2.content.document.querySelectorAll("#txt, #chk");
    1.77 +          ok(txt.value == uniqueText && chk.checked,
    1.78 +             "The window correctly restored the form");
    1.79 +          is(ss.getWindowValue(newWin2, uniqueKey), uniqueValue,
    1.80 +             "The window correctly restored the data associated with it");
    1.81 +
    1.82 +          // clean up
    1.83 +          newWin2.close();
    1.84 +          finish();
    1.85 +        }, true);
    1.86 +      });
    1.87 +    });
    1.88 +  }, TEST_URL);
    1.89 +}
    1.90 +
    1.91 +function setInputChecked(browser, data) {
    1.92 +  return sendMessage(browser, "ss-test:setInputChecked", data);
    1.93 +}

mercurial