tor-browser

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

reload.window.js (3734B)


      1 // This test tests for the nonexistence of a reload override buffer, which is
      2 // used in a previous version of the HTML Standard to make reloads of a
      3 // document.open()'d document load the written-to document rather than doing an
      4 // actual reload of the document's URL.
      5 //
      6 // This test has a somewhat interesting structure compared to the other tests
      7 // in this directory. It eschews the <iframe> structure used by other tests,
      8 // since when the child frame is reloaded it would adopt the URL of the test
      9 // page (the responsible document of the entry settings object), and the spec
     10 // forbids navigation in nested browsing contexts to the same URL as their
     11 // parent. To work around that, we use window.open() which does not suffer from
     12 // that restriction.
     13 //
     14 // In any case, this test as the caller of `document.open()` would be used both
     15 // as the test file and as part of the test file. The `if (window.name !==
     16 // "opened-dummy-window")` condition controls what role this file plays.
     17 
     18 if (window.name !== "opened-dummy-window") {
     19  async_test(t => {
     20    const testURL = document.URL;
     21    const dummyURL = new URL("resources/dummy.html", document.URL).href;
     22 
     23    // 1. Open an auxiliary window.
     24    const win = window.open("resources/dummy.html", "opened-dummy-window");
     25    t.add_cleanup(() => { win.close(); });
     26 
     27    win.addEventListener("load", t.step_func(() => {
     28      // The timeout seems to be necessary for Firefox, which when `load` is
     29      // called may still have an active parser.
     30      t.step_timeout(() => {
     31        const doc = win.document;
     32        assert_true(doc.body.textContent.includes("Dummy"), "precondition");
     33        assert_equals(doc.URL, dummyURL, "precondition");
     34 
     35        window.onChildLoad = t.step_func(message => {
     36          // 3. The dynamically overwritten content will trigger this function,
     37          // which puts in place the actual test.
     38 
     39          assert_equals(message, "Written", "script on written page is executed");
     40          assert_true(win.document.body.textContent.includes("Content"), "page is written to");
     41          assert_equals(win.document.URL, testURL, "postcondition: after document.write()");
     42          assert_equals(win.document, doc, "document.open should not change the document object");
     43          window.onChildLoad = t.step_func_done(message => {
     44            // 6. This function should be called from the if (opener) branch of
     45            // this file. It would throw an assertion error if the overwritten
     46            // content was executed instead.
     47            assert_equals(message, "Done!", "actual test");
     48            assert_true(win.document.body.textContent.includes("Back to the test"), "test is reloaded");
     49            assert_equals(win.document.URL, testURL, "postcondition: after reload");
     50            assert_not_equals(win.document, doc, "reload should change the document object");
     51          });
     52 
     53          // 4. Reload the pop-up window. Because of the doc.open() call, this
     54          // pop-up window will reload to the same URL as this test itself.
     55          win.location.reload();
     56        });
     57 
     58        // 2. When it is loaded, dynamically overwrite its content.
     59        assert_equals(doc.open(), doc);
     60        assert_equals(doc.URL, testURL, "postcondition: after document.open()");
     61        doc.write("<p>Content</p><script>opener.onChildLoad('Written');</script>");
     62        doc.close();
     63      }, 100);
     64    }), { once: true });
     65  }, "Reloading a document.open()'d page should reload the URL of the entry realm's responsible document");
     66 } else {
     67  document.write("<p>Back to the test</p>");
     68  // 5. Since this window is window.open()'d, opener refers to the test window.
     69  // Inform the opener that reload succeeded.
     70  opener.onChildLoad("Done!");
     71 }