tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_394759_basic.js (3898B)


      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 =
      8  "data:text/html;charset=utf-8,<input%20id=txt>" +
      9  "<input%20type=checkbox%20id=chk>";
     10 
     11 /**
     12 * This test ensures that closing a window is a reversible action. We will
     13 * close the the window, restore it and check that all data has been restored.
     14 * This includes window-specific data as well as form data for tabs.
     15 */
     16 function test() {
     17  waitForExplicitFinish();
     18 
     19  let uniqueKey = "bug 394759";
     20  let uniqueValue = "unik" + Date.now();
     21  let uniqueText = "pi != " + Math.random();
     22 
     23  // Clear the list of closed windows.
     24  forgetClosedWindows();
     25 
     26  provideWindow(function onTestURLLoaded(newWin) {
     27    BrowserTestUtils.addTab(newWin.gBrowser).linkedBrowser.stop();
     28 
     29    // Mark the window with some unique data to be restored later on.
     30    ss.setCustomWindowValue(newWin, uniqueKey, uniqueValue);
     31    let [txt] = newWin.content.document.querySelectorAll("#txt");
     32    txt.value = uniqueText;
     33 
     34    let browser = newWin.gBrowser.selectedBrowser;
     35 
     36    setPropertyOfFormField(browser, "#chk", "checked", true).then(() => {
     37      BrowserTestUtils.closeWindow(newWin).then(() => {
     38        is(
     39          ss.getClosedWindowCount(),
     40          1,
     41          "The closed window was added to Recently Closed Windows"
     42        );
     43 
     44        let data = SessionStore.getClosedWindowData();
     45 
     46        // Verify that non JSON serialized data is the same as JSON serialized data.
     47        is(
     48          JSON.stringify(data),
     49          ss.getClosedWindowData(),
     50          "Non-serialized data is the same as serialized data"
     51        );
     52 
     53        ok(
     54          data[0].title == TEST_URL &&
     55            JSON.stringify(data[0]).indexOf(uniqueText) > -1,
     56          "The closed window data was stored correctly"
     57        );
     58 
     59        // Reopen the closed window and ensure its integrity.
     60        let newWin2 = ss.undoCloseWindow(0);
     61 
     62        ok(
     63          newWin2.isChromeWindow,
     64          "undoCloseWindow actually returned a window"
     65        );
     66        is(
     67          ss.getClosedWindowCount(),
     68          0,
     69          "The reopened window was removed from Recently Closed Windows"
     70        );
     71 
     72        // SSTabRestored will fire more than once, so we need to make sure we count them.
     73        let restoredTabs = 0;
     74        let expectedTabs = data[0].tabs.length;
     75        newWin2.addEventListener(
     76          "SSTabRestored",
     77          function sstabrestoredListener() {
     78            ++restoredTabs;
     79            info("Restored tab " + restoredTabs + "/" + expectedTabs);
     80            if (restoredTabs < expectedTabs) {
     81              return;
     82            }
     83 
     84            is(restoredTabs, expectedTabs, "Correct number of tabs restored");
     85            newWin2.removeEventListener(
     86              "SSTabRestored",
     87              sstabrestoredListener,
     88              true
     89            );
     90 
     91            is(
     92              newWin2.gBrowser.tabs.length,
     93              2,
     94              "The window correctly restored 2 tabs"
     95            );
     96            is(
     97              newWin2.gBrowser.currentURI.spec,
     98              TEST_URL,
     99              "The window correctly restored the URL"
    100            );
    101 
    102            let chk;
    103            [txt, chk] =
    104              newWin2.content.document.querySelectorAll("#txt, #chk");
    105            ok(
    106              txt.value == uniqueText && chk.checked,
    107              "The window correctly restored the form"
    108            );
    109            is(
    110              ss.getCustomWindowValue(newWin2, uniqueKey),
    111              uniqueValue,
    112              "The window correctly restored the data associated with it"
    113            );
    114 
    115            // Clean up.
    116            BrowserTestUtils.closeWindow(newWin2).then(finish);
    117          },
    118          true
    119        );
    120      });
    121    });
    122  }, TEST_URL);
    123 }