summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/rules/test/browser_rules_colorpicker-wrap-focus.js
blob: 36ef6f15cb47ecdfcd6e05bcdc7fe5b7b745fad3 (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
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests that focus stays inside color picker on TAB and Shift + TAB

const TEST_URI = `
  <style type="text/css">
    body {
      color: red;
      background-color: #ededed;
      background-image: url(chrome://branding/content/icon64.png);
      border: 2em solid rgba(120, 120, 120, .5);
    }
  </style>
  Testing the color picker tooltip!
`;

add_task(async function () {
  await addTab("data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI));
  const { view } = await openRuleView();

  info("Focus on the property value span");
  getRuleViewProperty(view, "body", "color").valueSpan.focus();

  const cPicker = view.tooltips.getTooltip("colorPicker");
  const onColorPickerReady = cPicker.once("ready");

  info(
    "Tab to focus on the color swatch and press enter to simulate a click event"
  );
  EventUtils.sendKey("Tab");
  EventUtils.sendKey("Return");

  await onColorPickerReady;
  const doc = cPicker.spectrum.element.ownerDocument;
  ok(
    doc.activeElement.classList.contains("spectrum-color"),
    "Focus is initially on the spectrum dragger when color picker is shown."
  );

  info("Test that tabbing should move focus to the next focusable elements.");
  testFocusOnTab(doc, "devtools-button");
  testFocusOnTab(doc, "spectrum-hue-input");
  testFocusOnTab(doc, "spectrum-alpha-input");
  testFocusOnTab(doc, "learn-more");

  info(
    "Test that tabbing on the last element wraps focus to the first element."
  );
  testFocusOnTab(doc, "spectrum-color");

  info(
    "Test that shift tabbing on the first element wraps focus to the last element."
  );
  testFocusOnTab(doc, "learn-more", true);

  info(
    "Test that shift tabbing should move focus to the previous focusable elements."
  );
  testFocusOnTab(doc, "spectrum-alpha-input", true);
  testFocusOnTab(doc, "spectrum-hue-input", true);
  testFocusOnTab(doc, "devtools-button", true);
  testFocusOnTab(doc, "spectrum-color", true);

  await hideTooltipAndWaitForRuleViewChanged(cPicker, view);
});

function testFocusOnTab(doc, expectedClass, shiftKey = false) {
  EventUtils.synthesizeKey("VK_TAB", { shiftKey });
  ok(
    doc.activeElement.classList.contains(expectedClass),
    "Focus is on the correct element."
  );
}