tor-browser

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

browser_dbg-link-reload.js (2134B)


      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
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 /**
      6 * Test reload via an href link, which refers to the same document.
      7 * It seems to cause different codepath compared to F5.
      8 */
      9 
     10 "use strict";
     11 
     12 add_task(async function () {
     13  const dbg = await initDebugger(
     14    "doc-reload-link.html",
     15    "doc-reload-link.html"
     16  );
     17 
     18  info("Add a breakpoint that will be hit on reload");
     19  await addBreakpoint(dbg, "doc-reload-link.html", 3);
     20 
     21  for (let i = 0; i < 5; i++) {
     22    const onReloaded = waitForReload(dbg.commands);
     23 
     24    info(
     25      "Reload via a link, this causes special race condition different from F5"
     26    );
     27    await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     28      const reloadLink = content.document.querySelector("a");
     29      reloadLink.click();
     30    });
     31 
     32    info("Wait for paused\n");
     33    await waitForPaused(dbg);
     34 
     35    info("Check paused location\n");
     36    const source = findSource(dbg, "doc-reload-link.html");
     37    await assertPausedAtSourceAndLine(dbg, source.id, 3);
     38 
     39    await resume(dbg);
     40 
     41    info("Wait for completion of the page load");
     42    // This help ensure that the page loaded correctly and prevent pending request at teardown
     43    await onReloaded;
     44  }
     45 });
     46 
     47 async function waitForReload(commands) {
     48  let resolve;
     49  const onReloaded = new Promise(r => (resolve = r));
     50  const { resourceCommand } = commands;
     51  const { DOCUMENT_EVENT } = resourceCommand.TYPES;
     52  const onAvailable = resources => {
     53    if (resources.find(resource => resource.name == "dom-complete")) {
     54      resourceCommand.unwatchResources([DOCUMENT_EVENT], { onAvailable });
     55      resolve();
     56    }
     57  };
     58  // Wait for watchResources completion before reloading, otherwise we might miss the dom-complete event
     59  // if watchResources is still pending while the reload already started and finished loading the document early.
     60  await resourceCommand.watchResources([DOCUMENT_EVENT], {
     61    onAvailable,
     62    ignoreExistingResources: true,
     63  });
     64  return onReloaded;
     65 }