tor-browser

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

test_bug529119-2.html (3698B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4 <title>Test bug 529119</title>
      5 <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      7 
      8 <script class="testbody" type="text/javascript">
      9 
     10 SimpleTest.waitForExplicitFinish();
     11 SimpleTest.requestFlakyTimeout("untriaged");
     12 
     13 var workingURL = "http://mochi.test:8888/tests/docshell/test/mochitest/bug529119-window.html";
     14 // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     15 var faultyURL = "http://some-nonexistent-domain-27489274c892748217cn2384.test/";
     16 
     17 var w = null;
     18 var phase = 0;
     19 var isWindowLoaded = false;
     20 // Token that represents which page we currently have loaded.
     21 var token = 0;
     22 
     23 function delay(msec) {
     24  return new Promise(resolve => setTimeout(resolve, msec));
     25 }
     26 
     27 async function assignToken(tokenToAssign) {
     28  await SpecialPowers.spawn(w, [tokenToAssign],
     29                            newToken => { this.content.token = newToken });
     30 }
     31 
     32 // Returns when a new page is loaded and returns whether that page is an
     33 // error page.
     34 async function pollForPage(win) {
     35  while (true) {
     36    try {
     37      // When we do our navigation, there may be an interstitial about:blank
     38      // page if the navigation involves a process switch.  That about:blank
     39      // will exist between the new process's docshell being created and the
     40      // actual page that's being loaded loading (which can happen async from
     41      // the docshell creation).  We want to avoid treating the initial
     42      // about:blank as a new page.
     43      //
     44      // We could conceivably expose Document::IsInitialDocument() as a
     45      // ChromeOnly thing and use it here, but let's just filter out all
     46      // about:blank, since we don't expect any in this test.
     47      var haveNewPage = await SpecialPowers.spawn(w, [token],
     48        currentToken => this.content.token != currentToken &&
     49                        this.content.location.href != "about:blank");
     50 
     51      if (haveNewPage) {
     52        ++token;
     53        assignToken(token);
     54 
     55        // In this test, error pages are non-same-origin with us, and non-error
     56        // pages are same-origin.
     57        let haveErrorPage = false;
     58        try {
     59          win.document.title;
     60        } catch (ex) {
     61          haveErrorPage = true;
     62        }
     63        return haveErrorPage;
     64      }
     65    } catch (e) {
     66      // Something went wrong; just keep waiting.
     67    }
     68 
     69    await delay(100);
     70  }
     71 }
     72 
     73 async function windowLoaded() {
     74  // The code under here should only be run once
     75  // The test popup window workingURL was already opened
     76  if (isWindowLoaded)
     77    return;
     78  isWindowLoaded = true;
     79 
     80  assignToken(token);
     81 
     82  /* 2. We have successfully loaded a page, now go to a faulty URL */
     83  // XXX The test fails when we change the location synchronously
     84  window.setTimeout(function() {
     85    w.location.href = faultyURL;
     86  }, 0);
     87 
     88  ok(await pollForPage(w), "Waiting for error page succeeded");
     89  /* 3. now, while we are on the error page, navigate back */
     90  try {
     91    // We need the SpecialPowers bit, because this is a cross-origin window
     92    // and we normally can't touch .history on those.
     93    await SpecialPowers.spawn(w, [], () => this.content.history.back());
     94  } catch (ex) {
     95    ok(false, "w.history.back() threw " + ex);
     96  }
     97 
     98  ok(!await pollForPage(w), "Waiting for original page succeeded");
     99  /* 4-finish, check we are back at the original page */
    100  is(await SpecialPowers.spawn(w, [], () => this.content.location.href),
    101     workingURL,
    102     "Is on the previous page");
    103  w.close();
    104  SimpleTest.finish();
    105 }
    106 
    107 function startTest() {
    108  /* 1. load a URL that leads to an error page */
    109  w = window.open(workingURL);
    110 }
    111 
    112 </script>
    113 </head>
    114 <body onload="startTest();">
    115 </body>
    116 </html>