summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/widgets/test_moz_toggle.html
blob: 62f248c5995b79c1b810bbabad966f21c08c1d61 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>MozToggle tests</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <!-- TODO: Bug 1798404 - in-content/common.css can be removed once we have a better
  solution for token variables for the new widgets -->
  <link rel="stylesheet" href="chrome://global/skin/in-content/common.css">
  <script type="module" src="chrome://global/content/elements/moz-toggle.mjs"></script>
</head>
<body>
<p id="display"></p>
<div id="content" style="max-width: fit-content">
  <moz-toggle label="Label" description="Description" pressed="true"></moz-toggle>
</div>
<pre id="test">
<script class="testbody" type="application/javascript">
  add_task(async function testMozToggleDisplay() {
    const mozToggle = document.querySelector("moz-toggle");
    ok(mozToggle, "moz-toggle is rendered");

    const label = mozToggle.labelEl;
    ok(label, "moz-toggle contains a label");
    ok(label.textContent.includes("Label"), "The expected label text is shown");

    const description = mozToggle.descriptionEl;
    ok(description, "moz-toggle contains a description");
    ok(description.textContent.includes("Description"), "The expected description text is shown");

    const button = mozToggle.buttonEl;
    ok(button, "moz-toggle contains a button");
    is(button.getAttribute("aria-pressed"), "true", "The button is pressed");
  });

  add_task(async function testMozToggleInteraction() {
    const mozToggle = document.querySelector("moz-toggle");
    const button = mozToggle.buttonEl;
    is(mozToggle.pressed, true, "moz-toggle is pressed initially");
    is(button.getAttribute("aria-pressed"), "true", "aria-pressed reflects the pressed state");

    synthesizeMouseAtCenter(button, {});
    await mozToggle.updateComplete;

    is(mozToggle.pressed, false, "The toggle pressed state changes on click");
    is(button.getAttribute("aria-pressed"), "false", "aria-pressed reflects this change");

    synthesizeMouseAtCenter(mozToggle.labelEl, {});
    await mozToggle.updateComplete;

    is(mozToggle.pressed, true, "The toggle pressed state changes on label click");
    is(button.getAttribute("aria-pressed"), "true", "aria-pressed reflects this change");

    mozToggle.focus();
    synthesizeKey(" ", {});
    await mozToggle.updateComplete;

    is(mozToggle.pressed, false, "The toggle pressed state can be changed via space bar");
    is(button.getAttribute("aria-pressed"), "false", "aria-pressed reflects this change");
  });

  add_task(async function testSupportsAccesskey() {
    const mozToggle = document.querySelector("moz-toggle");
    let nextAccesskey = "l";
    mozToggle.setAttribute("accesskey", nextAccesskey);

    synthesizeKey(
      nextAccesskey,
      navigator.platform.includes("Mac")
        ? { altKey: true, ctrlKey: true }
        : { altKey: true, shiftKey: true }
    );

    is(
      mozToggle.shadowRoot.activeElement,
      mozToggle.buttonEl,
      "Focus has moved to the toggle button."
    );
  });
</script>
</pre>
</body>
</html>