tor-browser

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

browser_NavigationManager_no_navigation.js (2634B)


      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 file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 const { TabManager } = ChromeUtils.importESModule(
      6  "chrome://remote/content/shared/TabManager.sys.mjs"
      7 );
      8 
      9 add_task(async function testDocumentOpenWriteClose() {
     10  const events = [];
     11  const onEvent = (name, data) => events.push({ name, data });
     12 
     13  const navigationManager = new NavigationManager();
     14  navigationManager.on("fragment-navigated", onEvent);
     15  navigationManager.on("history-updated", onEvent);
     16  navigationManager.on("navigation-started", onEvent);
     17  navigationManager.on("navigation-stopped", onEvent);
     18  navigationManager.on("same-document-changed", onEvent);
     19 
     20  const url = "https://example.com/document-builder.sjs?html=test";
     21 
     22  const tab = await addTabAndWaitForNavigated(gBrowser, url);
     23  const browser = tab.linkedBrowser;
     24 
     25  navigationManager.startMonitoring();
     26  is(events.length, 0, "No event recorded");
     27 
     28  info("Replace the document");
     29  await SpecialPowers.spawn(browser, [], async () => {
     30    // Note: we need to use eval here to have reduced permissions and avoid
     31    // security errors.
     32    content.eval(`
     33      document.open();
     34      document.write("<h1 class='replaced'>Replaced</h1>");
     35      document.close();
     36    `);
     37 
     38    await ContentTaskUtils.waitForCondition(() =>
     39      content.document.querySelector(".replaced")
     40    );
     41  });
     42 
     43  is(events.length, 1, "No event recorded after replacing the document");
     44  is(
     45    events[0].name,
     46    "history-updated",
     47    "Received a single history-updated event"
     48  );
     49  is(
     50    events[0].data.navigationId,
     51    undefined,
     52    "history-updated event should not have a navigation id set"
     53  );
     54  is(events[0].data.url, url, "history-updated has the expected url");
     55 
     56  info("Reload the page, which should trigger a navigation");
     57  await loadURL(browser, url);
     58 
     59  info("Wait until 3 events have been received");
     60  await BrowserTestUtils.waitForCondition(() => events.length >= 3);
     61 
     62  is(events.length, 3, "Recorded 3 navigation events");
     63  is(
     64    events[1].name,
     65    "navigation-started",
     66    "Received a navigation-started event"
     67  );
     68  is(
     69    events[2].name,
     70    "navigation-stopped",
     71    "Received a navigation-stopped event"
     72  );
     73  navigationManager.off("fragment-navigated", onEvent);
     74  navigationManager.off("history-updated", onEvent);
     75  navigationManager.off("navigation-started", onEvent);
     76  navigationManager.off("navigation-stopped", onEvent);
     77  navigationManager.off("same-document-changed", onEvent);
     78 
     79  navigationManager.stopMonitoring();
     80 });