summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/browser_toolbox_options_multiple_tabs.js
blob: 74c0983d4e8f9ef5103e0b0075e5eba483835857 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const URL =
  "data:text/html;charset=utf8,test for dynamically registering " +
  "and unregistering tools across multiple tabs";

let tab1, tab2, modifiedPref;

add_task(async function () {
  tab1 = await openToolboxOptionsInNewTab();
  tab2 = await openToolboxOptionsInNewTab();

  await testToggleTools();
  await cleanup();
});

async function openToolboxOptionsInNewTab() {
  const tab = await addTab(URL);
  const toolbox = await gDevTools.showToolboxForTab(tab);
  const doc = toolbox.doc;
  const panel = await toolbox.selectTool("options");
  const { id } = panel.panelDoc.querySelector(
    "#default-tools-box input[type=checkbox]:not([data-unsupported], [checked])"
  );

  return {
    tab,
    toolbox,
    doc,
    panelWin: panel.panelWin,
    // This is a getter becuse toolbox tools list gets re-setup every time there
    // is a tool-registered or tool-undregistered event.
    get checkbox() {
      return panel.panelDoc.getElementById(id);
    },
  };
}

async function testToggleTools() {
  is(tab1.checkbox.id, tab2.checkbox.id, "Default tool box should be in sync.");

  const toolId = tab1.checkbox.id;
  const testTool = gDevTools.getDefaultTools().find(tool => tool.id === toolId);
  // Store modified pref names so that they can be cleared on error.
  modifiedPref = testTool.visibilityswitch;

  info(`Registering tool ${toolId} in the first tab.`);
  await toggleTool(tab1, toolId);

  info(`Unregistering tool ${toolId} in the first tab.`);
  await toggleTool(tab1, toolId);

  info(`Registering tool ${toolId} in the second tab.`);
  await toggleTool(tab2, toolId);

  info(`Unregistering tool ${toolId} in the second tab.`);
  await toggleTool(tab2, toolId);

  info(`Registering tool ${toolId} in the first tab.`);
  await toggleTool(tab1, toolId);

  info(`Unregistering tool ${toolId} in the second tab.`);
  await toggleTool(tab2, toolId);
}

async function toggleTool({ doc, panelWin, checkbox, tab }, toolId) {
  const prevChecked = checkbox.checked;

  (prevChecked ? checkRegistered : checkUnregistered)(toolId);

  const onToggleTool = gDevTools.once(
    `tool-${prevChecked ? "unregistered" : "registered"}`
  );
  EventUtils.sendMouseEvent({ type: "click" }, checkbox, panelWin);
  const id = await onToggleTool;

  is(id, toolId, `Correct event for ${toolId} was fired`);
  // await new Promise(resolve => setTimeout(resolve, 60000));
  (prevChecked ? checkUnregistered : checkRegistered)(toolId);
}

async function checkUnregistered(toolId) {
  ok(
    !getToolboxTab(tab1.doc, toolId),
    `Tab for unregistered tool ${toolId} is not present in first toolbox`
  );
  ok(
    !tab1.checkbox.checked,
    `Checkbox for unregistered tool ${toolId} is not checked in first toolbox`
  );
  ok(
    !getToolboxTab(tab2.doc, toolId),
    `Tab for unregistered tool ${toolId} is not present in second toolbox`
  );
  ok(
    !tab2.checkbox.checked,
    `Checkbox for unregistered tool ${toolId} is not checked in second toolbox`
  );
}

function checkRegistered(toolId) {
  ok(
    getToolboxTab(tab1.doc, toolId),
    `Tab for registered tool ${toolId} is present in first toolbox`
  );
  ok(
    tab1.checkbox.checked,
    `Checkbox for registered tool ${toolId} is checked in first toolbox`
  );
  ok(
    getToolboxTab(tab2.doc, toolId),
    `Tab for registered tool ${toolId} is present in second toolbox`
  );
  ok(
    tab2.checkbox.checked,
    `Checkbox for registered tool ${toolId} is checked in second toolbox`
  );
}

async function cleanup() {
  await tab1.toolbox.destroy();
  await tab2.toolbox.destroy();
  gBrowser.removeCurrentTab();
  gBrowser.removeCurrentTab();
  Services.prefs.clearUserPref(modifiedPref);
  tab1 = tab2 = modifiedPref = null;
}