tor-browser

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

browser_browsing_context_attached.js (4834B)


      1 "use strict";
      2 
      3 const TEST_PATH =
      4  getRootDirectory(gTestPath).replace(
      5    "chrome://mochitests/content",
      6    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      7    "http://example.com"
      8  ) + "dummy_page.html";
      9 
     10 const TOPIC = "browsing-context-attached";
     11 
     12 async function observeAttached(callback) {
     13  let attached = new Map();
     14 
     15  function observer(subject, topic, data) {
     16    is(topic, TOPIC, "observing correct topic");
     17    ok(BrowsingContext.isInstance(subject), "subject to be a BrowsingContext");
     18    is(typeof data, "string", "data to be a String");
     19    info(`*** bc id=${subject.id}, why=${data}`);
     20    attached.set(subject.id, { browsingContext: subject, why: data });
     21  }
     22 
     23  Services.obs.addObserver(observer, TOPIC);
     24  try {
     25    await callback();
     26    return attached;
     27  } finally {
     28    Services.obs.removeObserver(observer, TOPIC);
     29  }
     30 }
     31 
     32 add_task(async function toplevelForNewWindow() {
     33  let win;
     34 
     35  let attached = await observeAttached(async () => {
     36    win = await BrowserTestUtils.openNewBrowserWindow();
     37  });
     38 
     39  ok(
     40    attached.has(win.browsingContext.id),
     41    "got notification for window's chrome browsing context"
     42  );
     43  is(
     44    attached.get(win.browsingContext.id).why,
     45    "attach",
     46    "expected reason for chrome browsing context"
     47  );
     48 
     49  ok(
     50    attached.has(win.gBrowser.selectedBrowser.browsingContext.id),
     51    "got notification for toplevel browsing context"
     52  );
     53  is(
     54    attached.get(win.gBrowser.selectedBrowser.browsingContext.id).why,
     55    "attach",
     56    "expected reason for toplevel browsing context"
     57  );
     58 
     59  await BrowserTestUtils.closeWindow(win);
     60 });
     61 
     62 add_task(async function toplevelForNewTab() {
     63  let tab;
     64 
     65  let attached = await observeAttached(async () => {
     66    tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
     67  });
     68 
     69  ok(
     70    !attached.has(window.browsingContext.id),
     71    "no notification for the current window's chrome browsing context"
     72  );
     73  ok(
     74    attached.has(tab.linkedBrowser.browsingContext.id),
     75    "got notification for toplevel browsing context"
     76  );
     77  is(
     78    attached.get(tab.linkedBrowser.browsingContext.id).why,
     79    "attach",
     80    "expected reason for toplevel browsing context"
     81  );
     82 
     83  BrowserTestUtils.removeTab(tab);
     84 });
     85 
     86 add_task(async function subframe() {
     87  let browsingContext;
     88  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
     89 
     90  let attached = await observeAttached(async () => {
     91    browsingContext = await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
     92      let iframe = content.document.createElement("iframe");
     93      content.document.body.appendChild(iframe);
     94      iframe.contentWindow.location = "https://example.com/";
     95      return iframe.browsingContext;
     96    });
     97  });
     98 
     99  ok(
    100    !attached.has(window.browsingContext.id),
    101    "no notification for the current window's chrome browsing context"
    102  );
    103  ok(
    104    !attached.has(tab.linkedBrowser.browsingContext.id),
    105    "no notification for toplevel browsing context"
    106  );
    107  ok(
    108    attached.has(browsingContext.id),
    109    "got notification for frame's browsing context"
    110  );
    111  is(
    112    attached.get(browsingContext.id).why,
    113    "attach",
    114    "expected reason for frame's browsing context"
    115  );
    116 
    117  BrowserTestUtils.removeTab(tab);
    118 });
    119 
    120 add_task(async function toplevelReplacedBy() {
    121  let tab;
    122 
    123  let attached = await observeAttached(async () => {
    124    tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:robots");
    125  });
    126 
    127  const firstContext = tab.linkedBrowser.browsingContext;
    128  ok(
    129    attached.has(firstContext.id),
    130    "got notification for initial toplevel browsing context"
    131  );
    132  is(
    133    attached.get(firstContext.id).why,
    134    "attach",
    135    "expected reason for initial toplevel browsing context"
    136  );
    137 
    138  attached = await observeAttached(async () => {
    139    await loadURI(TEST_PATH);
    140  });
    141  const secondContext = tab.linkedBrowser.browsingContext;
    142  ok(
    143    attached.has(secondContext.id),
    144    "got notification for replaced toplevel browsing context"
    145  );
    146  isnot(secondContext, firstContext, "browsing context to be replaced");
    147  is(
    148    attached.get(secondContext.id).why,
    149    "replace",
    150    "expected reason for replaced toplevel browsing context"
    151  );
    152  is(
    153    secondContext.browserId,
    154    firstContext.browserId,
    155    "browserId has been kept"
    156  );
    157 
    158  attached = await observeAttached(async () => {
    159    await loadURI("about:robots");
    160  });
    161  const thirdContext = tab.linkedBrowser.browsingContext;
    162  ok(
    163    attached.has(thirdContext.id),
    164    "got notification for replaced toplevel browsing context"
    165  );
    166  isnot(thirdContext, secondContext, "browsing context to be replaced");
    167  is(
    168    attached.get(thirdContext.id).why,
    169    "replace",
    170    "expected reason for replaced toplevel browsing context"
    171  );
    172  is(
    173    thirdContext.browserId,
    174    secondContext.browserId,
    175    "browserId has been kept"
    176  );
    177 
    178  BrowserTestUtils.removeTab(tab);
    179 });