summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg-link-reload.js
blob: c374fc6bbafc644042d0fdafbce445cb8938f72e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

/**
 * Test reload via an href link, which refers to the same document.
 * It seems to cause different codepath compared to F5.
 */

"use strict";

add_task(async function () {
  const dbg = await initDebugger(
    "doc-reload-link.html",
    "doc-reload-link.html"
  );

  info("Add a breakpoint that will be hit on reload");
  await addBreakpoint(dbg, "doc-reload-link.html", 3);

  for (let i = 0; i < 5; i++) {
    const onReloaded = waitForReload(dbg.commands);

    info(
      "Reload via a link, this causes special race condition different from F5"
    );
    await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
      const reloadLink = content.document.querySelector("a");
      reloadLink.click();
    });

    info("Wait for paused\n");
    await waitForPaused(dbg);

    info("Check paused location\n");
    const source = findSource(dbg, "doc-reload-link.html");
    assertPausedAtSourceAndLine(dbg, source.id, 3);

    await resume(dbg);

    info("Wait for completion of the page load");
    // This help ensure that the page loaded correctly and prevent pending request at teardown
    await onReloaded;
  }
});

async function waitForReload(commands) {
  let resolve;
  const onReloaded = new Promise(r => (resolve = r));
  const { resourceCommand } = commands;
  const { DOCUMENT_EVENT } = resourceCommand.TYPES;
  const onAvailable = resources => {
    if (resources.find(resource => resource.name == "dom-complete")) {
      resourceCommand.unwatchResources([DOCUMENT_EVENT], { onAvailable });
      resolve();
    }
  };
  // Wait for watchResources completion before reloading, otherwise we might miss the dom-complete event
  // if watchResources is still pending while the reload already started and finished loading the document early.
  await resourceCommand.watchResources([DOCUMENT_EVENT], {
    onAvailable,
    ignoreExistingResources: true,
  });
  return onReloaded;
}