tor-browser

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

browser_NavigationManager_notify.js (5090B)


      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 { NavigableManager } = ChromeUtils.importESModule(
      6  "chrome://remote/content/shared/NavigableManager.sys.mjs"
      7 );
      8 const { notifyNavigationStarted, notifyNavigationStopped } =
      9  ChromeUtils.importESModule(
     10    "chrome://remote/content/shared/NavigationManager.sys.mjs"
     11  );
     12 
     13 const FIRST_URL = "https://example.com/document-builder.sjs?html=first";
     14 const SECOND_URL = "https://example.com/document-builder.sjs?html=second";
     15 
     16 add_task(async function test_notifyNavigationStartedStopped() {
     17  const tab = await addTabAndWaitForNavigated(gBrowser, FIRST_URL);
     18  const browser = tab.linkedBrowser;
     19 
     20  const events = [];
     21  const onEvent = (name, data) => events.push({ name, data });
     22 
     23  const navigationManager = new NavigationManager();
     24  navigationManager.on("navigation-started", onEvent);
     25  navigationManager.on("navigation-stopped", onEvent);
     26 
     27  navigationManager.startMonitoring();
     28 
     29  const navigableId = NavigableManager.getIdForBrowser(browser);
     30 
     31  info("Programmatically start a navigation");
     32  const startedNavigation = notifyNavigationStarted({
     33    contextDetails: {
     34      context: browser.browsingContext,
     35    },
     36    url: SECOND_URL,
     37  });
     38 
     39  const navigation = navigationManager.getNavigationForBrowsingContext(
     40    browser.browsingContext
     41  );
     42  assertNavigation(navigation, SECOND_URL);
     43 
     44  is(
     45    startedNavigation,
     46    navigation,
     47    "notifyNavigationStarted returned the expected navigation"
     48  );
     49  is(events.length, 1, "Only one event recorded");
     50 
     51  info("Attempt to start a navigation while another one is in progress");
     52  const alreadyStartedNavigation = notifyNavigationStarted({
     53    contextDetails: {
     54      context: browser.browsingContext,
     55    },
     56    url: SECOND_URL,
     57  });
     58  is(
     59    alreadyStartedNavigation,
     60    navigation,
     61    "notifyNavigationStarted returned the ongoing navigation"
     62  );
     63  is(events.length, 1, "Still only one event recorded");
     64 
     65  info("Programmatically stop the navigation");
     66  const stoppedNavigation = notifyNavigationStopped({
     67    contextDetails: {
     68      context: browser.browsingContext,
     69    },
     70    url: SECOND_URL,
     71  });
     72  is(
     73    stoppedNavigation,
     74    navigation,
     75    "notifyNavigationStopped returned the expected navigation"
     76  );
     77 
     78  is(events.length, 2, "Two events recorded");
     79  assertNavigationEvents(
     80    events,
     81    SECOND_URL,
     82    navigation.navigationId,
     83    navigableId
     84  );
     85 
     86  info("Attempt to stop an already stopped navigation");
     87  const alreadyStoppedNavigation = notifyNavigationStopped({
     88    contextDetails: {
     89      context: browser.browsingContext,
     90    },
     91    url: SECOND_URL,
     92  });
     93  is(
     94    alreadyStoppedNavigation,
     95    navigation,
     96    "notifyNavigationStopped returned the already stopped navigation"
     97  );
     98  is(events.length, 2, "Still only two events recorded");
     99 
    100  navigationManager.off("navigation-started", onEvent);
    101  navigationManager.off("navigation-stopped", onEvent);
    102  navigationManager.stopMonitoring();
    103 });
    104 
    105 add_task(async function test_notifyNavigationWithContextDetails() {
    106  const tab = await addTabAndWaitForNavigated(gBrowser, FIRST_URL);
    107  const browser = tab.linkedBrowser;
    108 
    109  const events = [];
    110  const onEvent = (name, data) => events.push({ name, data });
    111 
    112  const navigationManager = new NavigationManager();
    113  navigationManager.on("navigation-started", onEvent);
    114  navigationManager.on("navigation-stopped", onEvent);
    115 
    116  navigationManager.startMonitoring();
    117 
    118  const navigableId = NavigableManager.getIdForBrowser(browser);
    119 
    120  info("Programmatically start a navigation using browsing context details");
    121  const startedNavigation = notifyNavigationStarted({
    122    contextDetails: {
    123      browsingContextId: browser.browsingContext.id,
    124      browserId: browser.browsingContext.browserId,
    125      isTopBrowsingContext: browser.browsingContext.parent === null,
    126    },
    127    url: SECOND_URL,
    128  });
    129 
    130  const navigation = navigationManager.getNavigationForBrowsingContext(
    131    browser.browsingContext
    132  );
    133  assertNavigation(navigation, SECOND_URL);
    134 
    135  is(
    136    startedNavigation,
    137    navigation,
    138    "notifyNavigationStarted returned the expected navigation"
    139  );
    140  is(events.length, 1, "Only one event recorded");
    141 
    142  info("Programmatically stop the navigation using browsing context details");
    143  const stoppedNavigation = notifyNavigationStopped({
    144    contextDetails: {
    145      browsingContextId: browser.browsingContext.id,
    146      browserId: browser.browsingContext.browserId,
    147      isTopBrowsingContext: browser.browsingContext.parent === null,
    148    },
    149    url: SECOND_URL,
    150  });
    151  is(
    152    stoppedNavigation,
    153    navigation,
    154    "notifyNavigationStopped returned the expected navigation"
    155  );
    156 
    157  is(events.length, 2, "Two events recorded");
    158  assertNavigationEvents(
    159    events,
    160    SECOND_URL,
    161    navigation.navigationId,
    162    navigableId
    163  );
    164 
    165  navigationManager.off("navigation-started", onEvent);
    166  navigationManager.off("navigation-stopped", onEvent);
    167  navigationManager.stopMonitoring();
    168 });