summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/browser_toolbox_options.js
blob: 3b5d7f2661da24a6f8ba8b15c99e447e58ae724b (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests that changing preferences in the options panel updates the prefs
// and toggles appropriate things in the toolbox.

var doc = null,
  toolbox = null,
  panelWin = null,
  modifiedPrefs = [];
const L10N = new LocalizationHelper(
  "devtools/client/locales/toolbox.properties"
);
const { PrefObserver } = require("resource://devtools/client/shared/prefs.js");

add_task(async function () {
  const URL =
    "data:text/html;charset=utf8,test for dynamically registering " +
    "and unregistering tools";
  registerNewTool();
  const tab = await addTab(URL);
  toolbox = await gDevTools.showToolboxForTab(tab);

  doc = toolbox.doc;
  await registerNewPerToolboxTool();
  await testSelectTool();
  await testOptionsShortcut();
  await testOptions();
  await testToggleTools();

  // Test that registered WebExtensions becomes entries in the
  // options panel and toggling their checkbox toggle the related
  // preference.
  await registerNewWebExtensions();
  await testToggleWebExtensions();

  await cleanup();
});

function registerNewTool() {
  const toolDefinition = {
    id: "testTool",
    isToolSupported: () => true,
    visibilityswitch: "devtools.test-tool.enabled",
    url: "about:blank",
    label: "someLabel",
  };

  ok(gDevTools, "gDevTools exists");
  ok(
    !gDevTools.getToolDefinitionMap().has("testTool"),
    "The tool is not registered"
  );

  gDevTools.registerTool(toolDefinition);
  ok(
    gDevTools.getToolDefinitionMap().has("testTool"),
    "The tool is registered"
  );
}

// Register a fake WebExtension to check that it is
// listed in the toolbox options.
function registerNewWebExtensions() {
  // Register some fake extensions and init the related preferences
  // (similarly to ext-devtools.js).
  for (let i = 0; i < 2; i++) {
    const extPref = `devtools.webextensions.fakeExtId${i}.enabled`;
    Services.prefs.setBoolPref(extPref, true);

    toolbox.registerWebExtension(`fakeUUID${i}`, {
      name: `Fake WebExtension ${i}`,
      pref: extPref,
    });
  }
}

function registerNewPerToolboxTool() {
  const toolDefinition = {
    id: "test-pertoolbox-tool",
    isToolSupported: () => true,
    visibilityswitch: "devtools.test-pertoolbox-tool.enabled",
    url: "about:blank",
    label: "perToolboxSomeLabel",
  };

  ok(gDevTools, "gDevTools exists");
  ok(
    !gDevTools.getToolDefinitionMap().has("test-pertoolbox-tool"),
    "The per-toolbox tool is not registered globally"
  );

  ok(toolbox, "toolbox exists");
  ok(
    !toolbox.hasAdditionalTool("test-pertoolbox-tool"),
    "The per-toolbox tool is not yet registered to the toolbox"
  );

  toolbox.addAdditionalTool(toolDefinition);

  ok(
    !gDevTools.getToolDefinitionMap().has("test-pertoolbox-tool"),
    "The per-toolbox tool is not registered globally"
  );
  ok(
    toolbox.hasAdditionalTool("test-pertoolbox-tool"),
    "The per-toolbox tool has been registered to the toolbox"
  );
}

async function testSelectTool() {
  info("Checking to make sure that the options panel can be selected.");

  const onceSelected = toolbox.once("options-selected");
  toolbox.selectTool("options");
  await onceSelected;
  ok(true, "Toolbox selected via selectTool method");
}

async function testOptionsShortcut() {
  info("Selecting another tool, then reselecting options panel with keyboard.");

  await toolbox.selectTool("webconsole");
  is(toolbox.currentToolId, "webconsole", "webconsole is selected");
  synthesizeKeyShortcut(L10N.getStr("toolbox.help.key"));
  is(toolbox.currentToolId, "options", "Toolbox selected via shortcut key");
  synthesizeKeyShortcut(L10N.getStr("toolbox.help.key"));
  is(toolbox.currentToolId, "webconsole", "webconsole is reselected");
  synthesizeKeyShortcut(L10N.getStr("toolbox.help.key"));
  is(toolbox.currentToolId, "options", "Toolbox selected via shortcut key");
}

async function testOptions() {
  const tool = toolbox.getPanel("options");
  panelWin = tool.panelWin;
  const prefNodes = tool.panelDoc.querySelectorAll(
    "input[type=checkbox][data-pref]"
  );

  // Store modified pref names so that they can be cleared on error.
  for (const node of tool.panelDoc.querySelectorAll("[data-pref]")) {
    const pref = node.getAttribute("data-pref");
    modifiedPrefs.push(pref);
  }

  for (const node of prefNodes) {
    const prefValue = GetPref(node.getAttribute("data-pref"));

    // Test clicking the checkbox for each options pref
    await testMouseClick(node, prefValue);

    // Do again with opposite values to reset prefs
    await testMouseClick(node, !prefValue);
  }

  const prefSelects = tool.panelDoc.querySelectorAll("select[data-pref]");
  for (const node of prefSelects) {
    await testSelect(node);
  }
}

async function testSelect(select) {
  const pref = select.getAttribute("data-pref");
  const options = Array.from(select.options);
  info("Checking select for: " + pref);

  is(
    `${select.options[select.selectedIndex].value}`,
    `${GetPref(pref)}`,
    "select starts out selected"
  );

  for (const option of options) {
    if (options.indexOf(option) === select.selectedIndex) {
      continue;
    }

    const observer = new PrefObserver("devtools.");

    let changeSeen = false;
    const changeSeenPromise = new Promise(resolve => {
      observer.once(pref, () => {
        changeSeen = true;
        is(
          `${GetPref(pref)}`,
          `${option.value}`,
          "Preference been switched for " + pref
        );
        resolve();
      });
    });

    select.selectedIndex = options.indexOf(option);
    const changeEvent = new Event("change");
    select.dispatchEvent(changeEvent);

    await changeSeenPromise;

    ok(changeSeen, "Correct pref was changed");
    observer.destroy();
  }
}

async function testMouseClick(node, prefValue) {
  const observer = new PrefObserver("devtools.");

  const pref = node.getAttribute("data-pref");
  let changeSeen = false;
  const changeSeenPromise = new Promise(resolve => {
    observer.once(pref, () => {
      changeSeen = true;
      is(GetPref(pref), !prefValue, "New value is correct for " + pref);
      resolve();
    });
  });

  node.scrollIntoView();

  // We use executeSoon here to ensure that the element is in view and
  // clickable.
  executeSoon(function () {
    info("Click event synthesized for pref " + pref);
    EventUtils.synthesizeMouseAtCenter(node, {}, panelWin);
  });

  await changeSeenPromise;

  ok(changeSeen, "Correct pref was changed");
  observer.destroy();
}

async function testToggleWebExtensions() {
  const disabledExtensions = new Set();
  const toggleableWebExtensions = toolbox.listWebExtensions();

  function toggleWebExtension(node) {
    node.scrollIntoView();
    EventUtils.synthesizeMouseAtCenter(node, {}, panelWin);
  }

  function assertExpectedDisabledExtensions() {
    for (const ext of toggleableWebExtensions) {
      if (disabledExtensions.has(ext)) {
        ok(
          !toolbox.isWebExtensionEnabled(ext.uuid),
          `The WebExtension "${ext.name}" should be disabled`
        );
      } else {
        ok(
          toolbox.isWebExtensionEnabled(ext.uuid),
          `The WebExtension "${ext.name}" should  be enabled`
        );
      }
    }
  }

  function assertAllExtensionsDisabled() {
    const enabledUUIDs = toggleableWebExtensions
      .filter(ext => toolbox.isWebExtensionEnabled(ext.uuid))
      .map(ext => ext.uuid);

    Assert.deepEqual(
      enabledUUIDs,
      [],
      "All the registered WebExtensions should be disabled"
    );
  }

  function assertAllExtensionsEnabled() {
    const disabledUUIDs = toolbox
      .listWebExtensions()
      .filter(ext => !toolbox.isWebExtensionEnabled(ext.uuid))
      .map(ext => ext.uuid);

    Assert.deepEqual(
      disabledUUIDs,
      [],
      "All the registered WebExtensions should be enabled"
    );
  }

  function getWebExtensionNodes() {
    const toolNodes = panelWin.document.querySelectorAll(
      "#default-tools-box input[type=checkbox]:not([data-unsupported])," +
        "#additional-tools-box input[type=checkbox]:not([data-unsupported])"
    );

    return [...toolNodes].filter(node => {
      return toggleableWebExtensions.some(
        ({ uuid }) => node.getAttribute("id") === `webext-${uuid}`
      );
    });
  }

  let webExtensionNodes = getWebExtensionNodes();

  is(
    webExtensionNodes.length,
    toggleableWebExtensions.length,
    "There should be a toggle checkbox for every WebExtension registered"
  );

  for (const ext of toggleableWebExtensions) {
    ok(
      toolbox.isWebExtensionEnabled(ext.uuid),
      `The WebExtension "${ext.name}" is initially enabled`
    );
  }

  // Store modified pref names so that they can be cleared on error.
  for (const ext of toggleableWebExtensions) {
    modifiedPrefs.push(ext.pref);
  }

  // Turn each registered WebExtension to disabled.
  for (const node of webExtensionNodes) {
    toggleWebExtension(node);

    const toggledExt = toggleableWebExtensions.find(ext => {
      return node.id == `webext-${ext.uuid}`;
    });
    ok(toggledExt, "Found a WebExtension for the checkbox element");
    disabledExtensions.add(toggledExt);

    assertExpectedDisabledExtensions();
  }

  assertAllExtensionsDisabled();

  // Turn each registered WebExtension to enabled.
  for (const node of webExtensionNodes) {
    toggleWebExtension(node);

    const toggledExt = toggleableWebExtensions.find(ext => {
      return node.id == `webext-${ext.uuid}`;
    });
    ok(toggledExt, "Found a WebExtension for the checkbox element");
    disabledExtensions.delete(toggledExt);

    assertExpectedDisabledExtensions();
  }

  assertAllExtensionsEnabled();

  // Unregister the WebExtensions one by one, and check that only the expected
  // ones have been unregistered, and the remaining onea are still listed.
  for (const ext of toggleableWebExtensions) {
    ok(
      !!toolbox.listWebExtensions().length,
      "There should still be extensions registered"
    );
    toolbox.unregisterWebExtension(ext.uuid);

    const registeredUUIDs = toolbox.listWebExtensions().map(item => item.uuid);
    ok(
      !registeredUUIDs.includes(ext.uuid),
      `the WebExtension "${ext.name}" should have been unregistered`
    );

    webExtensionNodes = getWebExtensionNodes();

    const checkboxEl = webExtensionNodes.find(
      el => el.id === `webext-${ext.uuid}`
    );
    is(
      checkboxEl,
      undefined,
      "The unregistered WebExtension checkbox should have been removed"
    );

    is(
      registeredUUIDs.length,
      webExtensionNodes.length,
      "There should be the expected number of WebExtensions checkboxes"
    );
  }

  is(
    toolbox.listWebExtensions().length,
    0,
    "All WebExtensions have been unregistered"
  );

  webExtensionNodes = getWebExtensionNodes();

  is(
    webExtensionNodes.length,
    0,
    "There should not be any checkbox for the unregistered WebExtensions"
  );
}

function getToolNode(id) {
  return panelWin.document.getElementById(id);
}

async function testToggleTools() {
  const toolNodes = panelWin.document.querySelectorAll(
    "#default-tools-box input[type=checkbox]:not([data-unsupported])," +
      "#additional-tools-box input[type=checkbox]:not([data-unsupported])"
  );
  const toolNodeIds = [...toolNodes].map(node => node.id);
  const enabledToolIds = [...toolNodes]
    .filter(node => node.checked)
    .map(node => node.id);

  const toggleableTools = gDevTools
    .getDefaultTools()
    .filter(tool => {
      return tool.visibilityswitch;
    })
    .concat(gDevTools.getAdditionalTools())
    .concat(toolbox.getAdditionalTools());

  for (const node of toolNodes) {
    const id = node.getAttribute("id");
    ok(
      toggleableTools.some(tool => tool.id === id),
      "There should be a toggle checkbox for: " + id
    );
  }

  // Store modified pref names so that they can be cleared on error.
  for (const tool of toggleableTools) {
    const pref = tool.visibilityswitch;
    modifiedPrefs.push(pref);
  }

  // Toggle each tool
  for (const id of toolNodeIds) {
    await toggleTool(getToolNode(id));
  }

  // Toggle again to reset tool enablement state
  for (const id of toolNodeIds) {
    await toggleTool(getToolNode(id));
  }

  // Test that a tool can still be added when no tabs are present:
  // Disable all tools
  for (const id of enabledToolIds) {
    await toggleTool(getToolNode(id));
  }
  // Re-enable the tools which are enabled by default
  for (const id of enabledToolIds) {
    await toggleTool(getToolNode(id));
  }

  // Toggle first, middle, and last tools to ensure that toolbox tabs are
  // inserted in order
  const firstToolId = toolNodeIds[0];
  const middleToolId = toolNodeIds[(toolNodeIds.length / 2) | 0];
  const lastToolId = toolNodeIds[toolNodeIds.length - 1];

  await toggleTool(getToolNode(firstToolId));
  await toggleTool(getToolNode(firstToolId));
  await toggleTool(getToolNode(middleToolId));
  await toggleTool(getToolNode(middleToolId));
  await toggleTool(getToolNode(lastToolId));
  await toggleTool(getToolNode(lastToolId));
}

/**
 * Toggle tool node checkbox. Note: because toggling the checkbox will result in
 * re-rendering of the tool list, we must re-query the checkboxes every time.
 */
async function toggleTool(node) {
  const toolId = node.getAttribute("id");

  const registeredPromise = new Promise(resolve => {
    if (node.checked) {
      gDevTools.once(
        "tool-unregistered",
        checkUnregistered.bind(null, toolId, resolve)
      );
    } else {
      gDevTools.once(
        "tool-registered",
        checkRegistered.bind(null, toolId, resolve)
      );
    }
  });
  node.scrollIntoView();
  EventUtils.synthesizeMouseAtCenter(node, {}, panelWin);

  await registeredPromise;
}

function checkUnregistered(toolId, resolve, data) {
  if (data == toolId) {
    ok(true, "Correct tool removed");
    // checking tab on the toolbox
    ok(
      !doc.getElementById("toolbox-tab-" + toolId),
      "Tab removed for " + toolId
    );
  } else {
    ok(false, "Something went wrong, " + toolId + " was not unregistered");
  }
  resolve();
}

async function checkRegistered(toolId, resolve, data) {
  if (data == toolId) {
    ok(true, "Correct tool added back");
    // checking tab on the toolbox
    const button = await lookupButtonForToolId(toolId);
    ok(button, "Tab added back for " + toolId);
  } else {
    ok(false, "Something went wrong, " + toolId + " was not registered");
  }
  resolve();
}

function GetPref(name) {
  const type = Services.prefs.getPrefType(name);
  switch (type) {
    case Services.prefs.PREF_STRING:
      return Services.prefs.getCharPref(name);
    case Services.prefs.PREF_INT:
      return Services.prefs.getIntPref(name);
    case Services.prefs.PREF_BOOL:
      return Services.prefs.getBoolPref(name);
    default:
      throw new Error("Unknown type");
  }
}

/**
 * Find the button from specified toolId.
 * Generally, button which access to the tool panel is in toolbox or
 * tools menu(in the Chevron menu).
 */
async function lookupButtonForToolId(toolId) {
  let button = doc.getElementById("toolbox-tab-" + toolId);
  if (!button) {
    // search from the tools menu.
    await openChevronMenu(toolbox);
    button = doc.querySelector("#tools-chevron-menupopup-" + toolId);

    await closeChevronMenu(toolbox);
  }
  return button;
}

async function cleanup() {
  gDevTools.unregisterTool("testTool");
  await toolbox.destroy();
  gBrowser.removeCurrentTab();
  for (const pref of modifiedPrefs) {
    Services.prefs.clearUserPref(pref);
  }
  toolbox = doc = panelWin = modifiedPrefs = null;
}