tor-browser

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

browser_ConsoleStorageAPITests.js (3671B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const TEST_URI =
      5  "http://example.com/browser/dom/tests/browser/test-console-api.html";
      6 
      7 function tearDown() {
      8  while (gBrowser.tabs.length > 1) {
      9    gBrowser.removeCurrentTab();
     10  }
     11 }
     12 
     13 add_task(async function () {
     14  // Don't cache removed tabs, so "clear console cache on tab close" triggers.
     15  await SpecialPowers.pushPrefEnv({ set: [["browser.tabs.max_tabs_undo", 0]] });
     16 
     17  registerCleanupFunction(tearDown);
     18 
     19  info(
     20    "Open a keepalive tab in the background to make sure we don't accidentally kill the content process"
     21  );
     22  var keepaliveTab = await BrowserTestUtils.addTab(
     23    gBrowser,
     24    "data:text/html,<meta charset=utf8>Keep Alive Tab"
     25  );
     26 
     27  info("Open the main tab to run the test in");
     28  var tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URI);
     29  var browser = gBrowser.selectedBrowser;
     30 
     31  const windowId = await ContentTask.spawn(browser, null, async function () {
     32    let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"].getService(
     33      Ci.nsIConsoleAPIStorage
     34    );
     35 
     36    let observerPromise = new Promise(resolve => {
     37      let apiCallCount = 0;
     38      function observe() {
     39        apiCallCount++;
     40        info(`Received ${apiCallCount} console log events`);
     41        if (apiCallCount == 4) {
     42          ConsoleAPIStorage.removeLogEventListener(observe);
     43          resolve();
     44        }
     45      }
     46 
     47      info("Setting up observer");
     48      ConsoleAPIStorage.addLogEventListener(
     49        observe,
     50        Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal)
     51      );
     52    });
     53 
     54    info("Emit a few console API logs");
     55    content.console.log("this", "is", "a", "log", "message");
     56    content.console.info("this", "is", "a", "info", "message");
     57    content.console.warn("this", "is", "a", "warn", "message");
     58    content.console.error("this", "is", "a", "error", "message");
     59 
     60    info("Wait for the corresponding log event");
     61    await observerPromise;
     62 
     63    const innerWindowId = content.windowGlobalChild.innerWindowId;
     64    const events = ConsoleAPIStorage.getEvents(innerWindowId).filter(
     65      message =>
     66        message.arguments[0] === "this" &&
     67        message.arguments[1] === "is" &&
     68        message.arguments[2] === "a" &&
     69        message.arguments[4] === "message"
     70    );
     71    is(events.length, 4, "The storage service got the messages we emitted");
     72 
     73    info("Ensure clearEvents does remove the events from storage");
     74    ConsoleAPIStorage.clearEvents();
     75    is(ConsoleAPIStorage.getEvents(innerWindowId).length, 0, "Cleared Storage");
     76 
     77    return content.windowGlobalChild.innerWindowId;
     78  });
     79 
     80  await SpecialPowers.spawn(browser, [], function () {
     81    // make sure a closed window's events are in fact removed from
     82    // the storage cache
     83    content.console.log("adding a new event");
     84  });
     85 
     86  info("Close the window");
     87  gBrowser.removeTab(tab, { animate: false });
     88  // Ensure actual window destruction is not delayed (too long).
     89  SpecialPowers.DOMWindowUtils.garbageCollect();
     90 
     91  // Spawn the check in the keepaliveTab, so that we can read the ConsoleAPIStorage correctly
     92  gBrowser.selectedTab = keepaliveTab;
     93  browser = gBrowser.selectedBrowser;
     94 
     95  // Spin the event loop to make sure everything is cleared.
     96  await SpecialPowers.spawn(browser, [], () => {});
     97 
     98  await SpecialPowers.spawn(browser, [windowId], function (windowId) {
     99    var ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"].getService(
    100      Ci.nsIConsoleAPIStorage
    101    );
    102    is(
    103      ConsoleAPIStorage.getEvents(windowId).length,
    104      0,
    105      "tab close is clearing the cache"
    106    );
    107  });
    108 });