summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/browser_toolbox_races.js
blob: ede038e716a263d3bb2fa18c3ff158b8266af631 (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
92
93
94
95
96
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Toggling the toolbox three time can take more than 45s on slow test machine
requestLongerTimeout(2);

// Test toggling the toolbox quickly and see if there is any race breaking it.

const URL = "data:text/html;charset=utf-8,Toggling devtools quickly";
const {
  gDevToolsBrowser,
} = require("resource://devtools/client/framework/devtools-browser.js");

add_task(async function () {
  // Make sure this test starts with the selectedTool pref cleared. Previous
  // tests select various tools, and that sets this pref.
  Services.prefs.clearUserPref("devtools.toolbox.selectedTool");

  await addTab(URL);

  let ready = 0,
    destroy = 0,
    destroyed = 0;
  const onReady = () => {
    ready++;
  };
  const onDestroy = () => {
    destroy++;
  };
  const onDestroyed = () => {
    destroyed++;
  };
  gDevTools.on("toolbox-ready", onReady);
  gDevTools.on("toolbox-destroy", onDestroy);
  gDevTools.on("toolbox-destroyed", onDestroyed);

  // The current implementation won't toggle the toolbox many times,
  // instead it will ignore toggles that happens while the toolbox is still
  // creating or still destroying.

  info("Toggle the toolbox many times in a row");
  toggle();
  toggle();
  toggle();
  toggle();
  toggle();
  await wait(500);

  await waitFor(() => ready == 1);
  is(
    ready,
    1,
    "No matter how many times we called toggle, it will only open the toolbox once"
  );
  is(
    destroy,
    0,
    "All subsequent, synchronous call to toggle will be ignored and the toolbox won't be destroyed"
  );
  is(destroyed, 0);

  info("Retoggle the toolbox many times in a row");
  toggle();
  toggle();
  toggle();
  toggle();
  toggle();
  await wait(500);

  await waitFor(() => destroyed == 1);
  is(destroyed, 1, "Similarly, the toolbox will be closed");
  is(destroy, 1);
  is(
    ready,
    1,
    "and no other toolbox will be opened. The subsequent toggle will be ignored."
  );

  gDevTools.off("toolbox-ready", onReady);
  gDevTools.off("toolbox-destroy", onDestroy);
  gDevTools.off("toolbox-destroyed", onDestroyed);
  await wait(1000);

  gBrowser.removeCurrentTab();
});

function toggle() {
  // When enabling the input event prioritization, we'll reserve some time to
  // process input events in each frame. In that case, the synthesized input
  // events may delay the normal events. Replace synthesized key events by
  // toggleToolboxCommand to prevent the synthesized input events jam the
  // content process and cause the test timeout.
  gDevToolsBrowser.toggleToolboxCommand(window.gBrowser);
}