summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg-slow-script.js
blob: 83b35e1009d44c8c25f6b926ff087194411c7ae6 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* 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/>. */

"use strict";

// Tests the slow script warning

add_task(async function openDebuggerFirst() {
  // In mochitest, the timeout is disable, so set it to a short, but non zero duration
  await pushPref("dom.max_script_run_time", 1);
  // Prevents having to click on the page to have the dialog to appear
  await pushPref("dom.max_script_run_time.require_critical_input", false);

  const dbg = await initDebugger("doc-slow-script.html");

  const alert = BrowserTestUtils.waitForGlobalNotificationBar(
    window,
    "process-hang"
  );

  info("Execute an infinite loop");
  invokeInTab("infiniteLoop");

  info("Wait for the slow script warning");
  const notification = await alert;

  info("Click on the debug script button");
  const buttons = notification.buttonContainer.getElementsByTagName("button");
  // The first button is "stop", the second is "debug script"
  buttons[1].click();

  info("Waiting for the debugger to be paused");
  await waitForPaused(dbg);
  const source = findSource(dbg, "doc-slow-script.html");
  assertPausedAtSourceAndLine(dbg, source.id, 14);

  info("Close toolbox and tab");
  await dbg.toolbox.closeToolbox();
  await removeTab(gBrowser.selectedTab);
});

add_task(async function openDebuggerFromDialog() {
  const tab = await addTab(EXAMPLE_URL + "doc-slow-script.html");

  const alert = BrowserTestUtils.waitForGlobalNotificationBar(
    window,
    "process-hang"
  );

  // /!\ Hack this attribute in order to force showing the "debug script" button
  //     on all channels. Otherwise it is only displayed in dev edition.
  tab.linkedBrowser.browsingContext.watchedByDevTools = true;

  info("Execute an infinite loop");
  // Note that spawn will return a promise that may be rejected because of the infinite loop
  // And mochitest may consider this as an error. So ignore any rejection.
  SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    content.wrappedJSObject.infiniteLoop();
  }).catch(e => {});

  info("Wait for the slow script warning");
  const notification = await alert;

  info("Click on the debug script button");
  const buttons = notification.buttonContainer.getElementsByTagName("button");
  // The first button is "stop", the second is "debug script"
  buttons[1].click();

  info("Wait for the toolbox to appear and have the debugger initialized");
  await waitFor(async () => {
    const tb = gDevTools.getToolboxForTab(gBrowser.selectedTab);
    if (tb) {
      await tb.getPanelWhenReady("jsdebugger");
      return true;
    }
    return false;
  });
  const toolbox = gDevTools.getToolboxForTab(gBrowser.selectedTab);
  ok(toolbox, "Got a toolbox");
  const dbg = createDebuggerContext(toolbox);

  info("Waiting for the debugger to be paused");
  await waitForPaused(dbg);
  const source = findSource(dbg, "doc-slow-script.html");
  assertPausedAtSourceAndLine(dbg, source.id, 14);

  info("Close toolbox and tab");
  await dbg.toolbox.closeToolbox();
  await removeTab(gBrowser.selectedTab);
});