summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/browser_toolbox_tools_per_toolbox_registration.js
blob: 0e9009497fb4634a51696dd4c9880d50b7ec912a (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
131
132
133
134
135
136
137
138
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const TEST_URL = `data:text/html,<!DOCTYPE html>
  <html>
    <head>
      <meta charset="utf-8">
    </head>
    <body>
      test for registering and unregistering tools to a specific toolbox
    </body>
  </html>`;

const TOOL_ID = "test-toolbox-tool";
var toolbox;

function test() {
  addTab(TEST_URL).then(async tab => {
    gDevTools
      .showToolboxForTab(tab)
      .then(toolboxRegister)
      .then(testToolRegistered);
  });
}

var resolveToolInstanceBuild;
var waitForToolInstanceBuild = new Promise(resolve => {
  resolveToolInstanceBuild = resolve;
});

var resolveToolInstanceDestroyed;
var waitForToolInstanceDestroyed = new Promise(resolve => {
  resolveToolInstanceDestroyed = resolve;
});

function toolboxRegister(aToolbox) {
  toolbox = aToolbox;

  waitForToolInstanceBuild = new Promise(resolve => {
    resolveToolInstanceBuild = resolve;
  });

  info("add per-toolbox tool in the opened toolbox.");

  toolbox.addAdditionalTool({
    id: TOOL_ID,
    // The size of the label can make the test fail if it's too long.
    // See ok(tab, ...) assert below and Bug 1596345.
    label: "Test Tool",
    inMenu: true,
    isToolSupported: () => true,
    build() {
      info("per-toolbox tool has been built.");
      resolveToolInstanceBuild();

      return {
        destroy: () => {
          info("per-toolbox tool has been destroyed.");
          resolveToolInstanceDestroyed();
        },
      };
    },
    key: "t",
  });
}

function testToolRegistered() {
  ok(
    !gDevTools.getToolDefinitionMap().has(TOOL_ID),
    "per-toolbox tool is not registered globally"
  );
  ok(
    toolbox.hasAdditionalTool(TOOL_ID),
    "per-toolbox tool registered to the specific toolbox"
  );

  // Test that the tool appeared in the UI.
  const doc = toolbox.doc;
  const tab = getToolboxTab(doc, TOOL_ID);

  ok(tab, "new tool's tab exists in toolbox UI");

  const panel = doc.getElementById("toolbox-panel-" + TOOL_ID);
  ok(panel, "new tool's panel exists in toolbox UI");

  for (const win of getAllBrowserWindows()) {
    const key = win.document.getElementById("key_" + TOOL_ID);
    if (win.document == doc) {
      continue;
    }
    ok(!key, "key for new tool should not exists in the other browser windows");
    const menuitem = win.document.getElementById("menuitem_" + TOOL_ID);
    ok(!menuitem, "menu item should not exists in the other browser window");
  }

  // Test that the tool is built once selected and then test its unregistering.
  info("select per-toolbox tool in the opened toolbox.");
  gDevTools
    .showToolboxForTab(gBrowser.selectedTab, { toolId: TOOL_ID })
    .then(waitForToolInstanceBuild)
    .then(testUnregister);
}

function getAllBrowserWindows() {
  return Array.from(Services.wm.getEnumerator("navigator:browser"));
}

function testUnregister() {
  info("remove per-toolbox tool in the opened toolbox.");
  toolbox.removeAdditionalTool(TOOL_ID);

  Promise.all([waitForToolInstanceDestroyed]).then(toolboxToolUnregistered);
}

function toolboxToolUnregistered() {
  ok(
    !toolbox.hasAdditionalTool(TOOL_ID),
    "per-toolbox tool unregistered from the specific toolbox"
  );

  // test that it disappeared from the UI
  const doc = toolbox.doc;
  const tab = getToolboxTab(doc, TOOL_ID);
  ok(!tab, "tool's tab was removed from the toolbox UI");

  const panel = doc.getElementById("toolbox-panel-" + TOOL_ID);
  ok(!panel, "tool's panel was removed from toolbox UI");

  cleanup();
}

function cleanup() {
  toolbox.destroy().then(() => {
    toolbox = null;
    gBrowser.removeCurrentTab();
    finish();
  });
}