File: test_moz_toggle.html

package info (click to toggle)
firefox-esr 128.13.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,230,012 kB
  • sloc: cpp: 7,103,971; javascript: 6,088,450; ansic: 3,653,980; python: 1,212,330; xml: 594,604; asm: 420,652; java: 182,969; sh: 71,124; makefile: 20,747; perl: 13,449; objc: 12,399; yacc: 4,583; cs: 3,846; pascal: 2,973; lex: 1,720; ruby: 1,194; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (85 lines) | stat: -rw-r--r-- 3,243 bytes parent folder | download | duplicates (11)
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>