tor-browser

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

browser_615394-SSWindowState_events_setBrowserState.js (4266B)


      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 const lameMultiWindowState = {
      6  windows: [
      7    {
      8      tabs: [
      9        {
     10          entries: [
     11            { url: "http://example.org#1", triggeringPrincipal_base64 },
     12          ],
     13          extData: { uniq: r() },
     14        },
     15        {
     16          entries: [
     17            { url: "http://example.org#2", triggeringPrincipal_base64 },
     18          ],
     19          extData: { uniq: r() },
     20        },
     21        {
     22          entries: [
     23            { url: "http://example.org#3", triggeringPrincipal_base64 },
     24          ],
     25          extData: { uniq: r() },
     26        },
     27        {
     28          entries: [
     29            { url: "http://example.org#4", triggeringPrincipal_base64 },
     30          ],
     31          extData: { uniq: r() },
     32        },
     33      ],
     34      selected: 1,
     35    },
     36    {
     37      tabs: [
     38        {
     39          entries: [
     40            { url: "http://example.com#1", triggeringPrincipal_base64 },
     41          ],
     42          extData: { uniq: r() },
     43        },
     44        {
     45          entries: [
     46            { url: "http://example.com#2", triggeringPrincipal_base64 },
     47          ],
     48          extData: { uniq: r() },
     49        },
     50        {
     51          entries: [
     52            { url: "http://example.com#3", triggeringPrincipal_base64 },
     53          ],
     54          extData: { uniq: r() },
     55        },
     56        {
     57          entries: [
     58            { url: "http://example.com#4", triggeringPrincipal_base64 },
     59          ],
     60          extData: { uniq: r() },
     61        },
     62      ],
     63      selected: 3,
     64    },
     65  ],
     66 };
     67 
     68 function getOuterWindowID(aWindow) {
     69  return aWindow.docShell.outerWindowID;
     70 }
     71 
     72 function test() {
     73  /** Test for Bug 615394 - Session Restore should notify when it is beginning and ending a restore */
     74  waitForExplicitFinish();
     75 
     76  // We'll track events per window so we are sure that they are each happening once
     77  // pre window.
     78  let windowEvents = {};
     79  windowEvents[getOuterWindowID(window)] = {
     80    busyEventCount: 0,
     81    readyEventCount: 0,
     82  };
     83 
     84  // waitForBrowserState does it's own observing for windows, but doesn't attach
     85  // the listeners we want here, so do it ourselves.
     86  let newWindow;
     87  function windowObserver(aSubject, aTopic) {
     88    if (aTopic == "domwindowopened") {
     89      Services.ww.unregisterNotification(windowObserver);
     90 
     91      newWindow = aSubject;
     92      newWindow.addEventListener(
     93        "load",
     94        function () {
     95          windowEvents[getOuterWindowID(newWindow)] = {
     96            busyEventCount: 0,
     97            readyEventCount: 0,
     98          };
     99 
    100          newWindow.addEventListener("SSWindowStateBusy", onSSWindowStateBusy);
    101          newWindow.addEventListener(
    102            "SSWindowStateReady",
    103            onSSWindowStateReady
    104          );
    105        },
    106        { once: true }
    107      );
    108    }
    109  }
    110 
    111  function onSSWindowStateBusy(aEvent) {
    112    windowEvents[getOuterWindowID(aEvent.originalTarget)].busyEventCount++;
    113  }
    114 
    115  function onSSWindowStateReady(aEvent) {
    116    windowEvents[getOuterWindowID(aEvent.originalTarget)].readyEventCount++;
    117  }
    118 
    119  window.addEventListener("SSWindowStateBusy", onSSWindowStateBusy);
    120  window.addEventListener("SSWindowStateReady", onSSWindowStateReady);
    121  Services.ww.registerNotification(windowObserver);
    122 
    123  waitForBrowserState(lameMultiWindowState, function () {
    124    let checkedWindows = 0;
    125    for (let id of Object.keys(windowEvents)) {
    126      let winEvents = windowEvents[id];
    127      is(
    128        winEvents.busyEventCount,
    129        1,
    130        "window" + id + " busy event count correct"
    131      );
    132      is(
    133        winEvents.readyEventCount,
    134        1,
    135        "window" + id + " ready event count correct"
    136      );
    137      checkedWindows++;
    138    }
    139    is(checkedWindows, 2, "checked 2 windows");
    140    window.removeEventListener("SSWindowStateBusy", onSSWindowStateBusy);
    141    window.removeEventListener("SSWindowStateReady", onSSWindowStateReady);
    142    newWindow.removeEventListener("SSWindowStateBusy", onSSWindowStateBusy);
    143    newWindow.removeEventListener("SSWindowStateReady", onSSWindowStateReady);
    144 
    145    newWindow.close();
    146    while (gBrowser.tabs.length > 1) {
    147      gBrowser.removeTab(gBrowser.tabs[1]);
    148    }
    149 
    150    finish();
    151  });
    152 }