File: test_moz_visual_picker.html

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (920 lines) | stat: -rw-r--r-- 30,588 bytes parent folder | download | duplicates (3)
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>MozVisualPicker Tests</title>
    <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
    <link rel="stylesheet" href="chrome://global/skin/in-content/common.css" />
    <link
      rel="stylesheet"
      href="chrome://mochikit/content/tests/SimpleTest/test.css"
    />
    <script
      type="module"
      src="chrome://global/content/elements/moz-visual-picker.mjs"
    ></script>
    <script src="lit-test-helpers.js"></script>
    <style>
      .slotted {
        height: 72px;
        width: 72px;
        border-radius: var(--border-radius-medium);
        background-color: var(--background-color-box);
        padding: var(--space-medium);
      }
    </style>
    <script>
      let html;
      let defaultTemplate;
      let testHelpers = new InputTestHelpers();

      async function keyboardNavigate(picker, direction) {
        let keyCode = `KEY_Arrow${
          direction.charAt(0).toUpperCase() + direction.slice(1)
        }`;
        synthesizeKey(keyCode);
        await picker.updateComplete;
      }

      async function renderVisualPicker(
        type = "radio",
        template = defaultTemplate
      ) {
        let renderTarget = await testHelpers.renderTemplate(template);
        let picker = renderTarget.querySelector("moz-visual-picker");
        let items = [
          ...renderTarget.querySelectorAll("moz-visual-picker-item"),
        ];
        let [firstItem, secondItem, thirdItem] = items;

        picker.type = type;
        await picker.updateComplete;

        return { picker, items, firstItem, secondItem, thirdItem };
      }

      add_setup(async function setup() {
        ({ html } = await testHelpers.setupLit());
        let templateFn = attrs => html`
          <moz-visual-picker ${attrs}>
            <moz-visual-picker-item checked value="first">
              <div class="slotted">In the slot</div>
            </moz-visual-picker-item>
            <moz-visual-picker-item value="second">
              <div class="slotted">In the slot</div>
            </moz-visual-picker-item>
            <moz-visual-picker-item value="third">
              <div class="slotted">In the slot</div>
            </moz-visual-picker-item>
          </moz-visual-picker>
        `;
        defaultTemplate = templateFn(
          testHelpers.spread({ label: "Visual picker label" })
        );
        testHelpers.setupTests({ templateFn });
      });

      add_task(async function testVisualPickerProperties() {
        const TEST_LABEL = "Testing...";
        const TEST_DESCRIPTION = "Testing description..";
        const TEST_SUPPORT_PAGE = "Testing support page..";

        let renderTarget = await testHelpers.renderTemplate(defaultTemplate);
        let picker = renderTarget.querySelector("moz-visual-picker");
        let items = [
          ...renderTarget.querySelectorAll("moz-visual-picker-item"),
        ];

        is(
          picker.fieldset.label,
          "Visual picker label",
          "Visual picker supports a label."
        );

        picker.label = TEST_LABEL;
        picker.description = TEST_DESCRIPTION;
        picker.supportPage = TEST_SUPPORT_PAGE;
        await picker.updateComplete;

        is(
          picker.fieldset.label,
          TEST_LABEL,
          "Visual picker label is updated."
        );
        is(
          picker.fieldset.description,
          TEST_DESCRIPTION,
          "Visual picker description text is set."
        );
        is(
          picker.fieldset.supportPage,
          TEST_SUPPORT_PAGE,
          "Visual picker support page is set."
        );
        is(
          picker.fieldset.getAttribute("role"),
          "radiogroup",
          "Visual picker uses the 'radiogroup' role by default."
        );
        items.forEach(item => {
          is(
            item.itemEl.getAttribute("role"),
            "radio",
            "Visual picker items use the 'radio' role by default."
          );
        });

        picker.type = "listbox";
        await picker.updateComplete;

        is(
          picker.fieldset.getAttribute("role"),
          "listbox",
          "Visual picker uses the 'listbox' role after the type is changed."
        );
        items.forEach(item => {
          is(
            item.itemEl.getAttribute("role"),
            "option",
            "Visual picker items use the 'option' role after the picker type is changed."
          );
        });
      });

      add_task(async function testVisualPickerItemLabel() {
        let labelledItemsTemplate = html`
          <moz-visual-picker
            type="listbox"
            label="Testing labels"
            value="first"
          >
            <moz-visual-picker-item value="first" label="first">
            </moz-visual-picker-item>
            <moz-visual-picker-item value="second" label="second">
            </moz-visual-picker-item>
            <moz-visual-picker-item value="third" label="third">
            </moz-visual-picker-item>
          </moz-visual-picker>
        `;
        let renderTarget = await testHelpers.renderTemplate(
          labelledItemsTemplate
        );
        let items = [
          ...renderTarget.querySelectorAll("moz-visual-picker-item"),
        ];
        let labels = ["first", "second", "third"];

        labels.forEach((label, i) => {
          is(
            items[i].shadowRoot.textContent.trim(),
            label,
            "Visual picker items support a visible label."
          );
        });

        items.forEach(async (item, i) => {
          let itemLabel = labels[i];

          // Verify that label is set via markup.
          is(
            items.shadowRoot.textContent.trim(),
            itemLabel,
            "Visual picker items support a visible label."
          );

          // Clear the visible label and set an aria label instead
          item.label = "";
          item.setAttribute("aria-label", itemLabel);
          await item.updateComplete;

          ok(
            !item.shadowRoot.textContent.trim(),
            "Visual picker item no longer has a visible label."
          );
          is(
            item.itemEl.getAttribute("aria-label"),
            itemLabel,
            "aria-label is set on the inner element."
          );

          // Clear the aria-label and rely on slotted content to provide a label.
          item.ariaLabel = "";
          let textEl = document.createElement("p");
          textEl.textContent = itemLabel;
          item.append(textEl);
          await item.updateComplete;

          is(
            item.shadowRoot.textContent.trim(),
            "Visual picker has a visible label again."
          );
          ok(
            item.ariaLabelledByElements.includes(textEl),
            "Visual picker item is labelled by the slotted content."
          );
        });
      });

      add_task(async function testChangingPickerValue() {
        let items, picker;

        async function verifySelectedPickerItem(selectedItem, focusedItem) {
          let ariaProperty =
            picker.type == "radio" ? "aria-checked" : "aria-selected";
          ok(selectedItem.checked, "The selected picker item is checked.");
          is(
            selectedItem.itemEl.getAttribute(ariaProperty),
            "true",
            `The checked item has the correct ${ariaProperty} value.`
          );

          if (focusedItem) {
            is(focusedItem.itemEl.tabIndex, 0, "The active item is focusable.");
            is(
              document.activeElement,
              focusedItem,
              `Expected ${focusedItem.value} item to be focused`
            );
          }

          items.forEach(item => {
            let isChecked = item == selectedItem;
            let tabIndex = item == (focusedItem ?? selectedItem) ? 0 : -1;
            is(
              item.checked,
              isChecked,
              `${item.value} item is ${isChecked ? "" : "not"} checked`
            );
            is(
              item.itemEl.getAttribute(ariaProperty),
              isChecked ? "true" : "false",
              `${item.value} item has the expected ${ariaProperty} value`
            );
            is(
              item.itemEl.tabIndex,
              tabIndex,
              `${item.value} item has the expected tabIndex`
            );
          });

          is(
            picker.value,
            selectedItem.value,
            "Picker value matches the value of the checked item."
          );
        }

        async function verifyValueChange(type) {
          ({ picker, items } = await renderVisualPicker(type));
          let [firstItem, secondItem, thirdItem] = items;

          // Ensure picker is visible in the test harness.
          picker.focus();
          picker.scrollIntoView();

          info(`Verifying ways of changing the value of a ${type} picker`);

          // Verify the initial state.
          verifySelectedPickerItem(firstItem);

          // Verify changing the checked property directly.
          secondItem.checked = true;
          await picker.updateComplete;
          verifySelectedPickerItem(secondItem);

          // Verify clicking on a picker item to change checked state.
          synthesizeMouseAtCenter(thirdItem, {});
          await picker.updateComplete;
          verifySelectedPickerItem(thirdItem, thirdItem);

          // Verify changing the picker value sets the selected state of child items.
          picker.value = "first";
          await picker.updateComplete;
          let focusedItem = type == "listbox" ? thirdItem : null;
          verifySelectedPickerItem(firstItem, focusedItem);
        }

        await verifyValueChange("radio");
        await verifyValueChange("listbox");
      });

      // Verify that settings a value on the group works as expected creating
      // the elements programmatically via document.createElement. We ran into
      // issues with this in moz-visual-picker-item-group.
      add_task(async function testProgrammaticVisualPickerCreation() {
        let visualPicker = document.createElement("moz-visual-picker");
        visualPicker.label = "Created with document.createElement";
        visualPicker.value = "second";

        let firstItem = document.createElement("moz-visual-picker-item");
        firstItem.value = "first";
        firstItem.label = "first";

        let secondItem = document.createElement("moz-visual-picker-item");
        secondItem.value = "second";
        secondItem.label = "second";

        visualPicker.append(firstItem, secondItem);
        testHelpers.render(html``, testHelpers.renderTarget);
        testHelpers.renderTarget.append(visualPicker);
        await visualPicker.updateComplete;

        ok(!firstItem.checked, "The first item is not checked.");
        ok(secondItem.checked, "The second item is checked.");

        visualPicker.value = "first";
        await visualPicker.updateComplete;

        ok(firstItem.checked, "The first item is checked.");
        ok(!secondItem.checked, "The second item is no longer checked.");
      });

      // Verify that keyboard navigation works as expected.
      add_task(async function testRadioKeyboardNavigation() {
        let { firstItem, secondItem, thirdItem, picker } =
          await renderVisualPicker();

        // Ensure picker is visible in the test harness.
        picker.focus();
        picker.scrollIntoView();

        const navigate = direction => keyboardNavigate(picker, direction);

        function validateActiveElement(item) {
          is(
            document.activeElement,
            item,
            "Focus moves to the expected picker item."
          );

          is(picker.value, item.value, "Visual picker value is updated.");
        }

        synthesizeMouseAtCenter(firstItem, {});
        await picker.updateComplete;

        await navigate("down");
        validateActiveElement(secondItem);

        await navigate("down");
        validateActiveElement(thirdItem);

        await navigate("down");
        validateActiveElement(firstItem);

        await navigate("up");
        validateActiveElement(thirdItem);

        await navigate("up");
        validateActiveElement(secondItem);

        await navigate("right");
        validateActiveElement(thirdItem);

        await navigate("right");
        validateActiveElement(firstItem);

        await navigate("left");
        validateActiveElement(thirdItem);

        await navigate("left");
        validateActiveElement(secondItem);

        // Validate that disabled items get skipped over.
        thirdItem.disabled = true;
        await picker.updateComplete;

        await navigate("down");
        validateActiveElement(firstItem);

        await navigate("up");
        validateActiveElement(secondItem);

        thirdItem.disabled = false;
        await picker.updateComplete;

        // Validate left/right keys have opposite effect for RTL locales.
        await SpecialPowers.pushPrefEnv({
          set: [["intl.l10n.pseudo", "bidi"]],
        });

        await navigate("left");
        validateActiveElement(thirdItem);

        await navigate("left");
        validateActiveElement(firstItem);

        await navigate("right");
        validateActiveElement(thirdItem);

        await navigate("right");
        validateActiveElement(secondItem);

        secondItem.click();
        await picker.updateComplete;
        validateActiveElement(secondItem);

        await SpecialPowers.popPrefEnv();
      });

      // Validate behavior when the picker has no value/no item is selected by default.
      add_task(async function testNoItemSelected() {
        let template = html`
          <button tabindex="0">Before picker</button>
          <moz-visual-picker name="test-name" label="No item selected">
            <moz-visual-picker-item value="first">
              <div class="slotted">In the slot</div>
            </moz-visual-picker-item>
            <moz-visual-picker-item value="second">
              <div class="slotted">In the slot</div>
            </moz-visual-picker-item>
            <moz-visual-picker-item value="third">
              <div class="slotted">In the slot</div>
            </moz-visual-picker-item>
          </moz-visual-picker>
          <button tabindex="0" id="after">After picker item></button>
        `;

        async function validateNoItemSelected(type) {
          let { picker, items, firstItem, secondItem, thirdItem } =
            await renderVisualPicker(type, template);
          let [beforeButton, afterButton] = document.querySelectorAll("button");

          info(
            `Verifying behavior when no item is selected for ${type} picker`
          );

          ok(!picker.value, "Visual picker does not have a value.");
          items.forEach(item =>
            ok(!item.checked, "All picker items are unselected.")
          );

          beforeButton.focus();
          synthesizeKey("KEY_Tab", {});
          is(
            document.activeElement,
            firstItem,
            "First picker item is tab focusable when all items un-checked."
          );
          [secondItem, thirdItem].forEach(item =>
            is(
              item.itemEl.getAttribute("tabindex"),
              "-1",
              "All other items are not tab focusable."
            )
          );

          synthesizeKey("KEY_Tab", {});
          is(
            document.activeElement,
            afterButton,
            "Tab moves focus out of the visual picker."
          );

          synthesizeKey("KEY_Tab", { shiftKey: true });
          is(
            document.activeElement,
            firstItem,
            "Focus moves back to the first picker item."
          );

          synthesizeKey("KEY_ArrowDown", {});
          is(
            document.activeElement,
            secondItem,
            "Focus moves to the second picker item with down arrow keypress."
          );
          if (type == "listbox") {
            synthesizeKey(" ");
          }
          is(
            picker.value,
            secondItem.value,
            "Picker value updates to second picker item value."
          );

          synthesizeKey("KEY_Tab");

          secondItem.checked = false;
          await picker.updateComplete;

          synthesizeKey("KEY_Tab", { shiftKey: true });

          ok(
            !picker.value,
            "Picker value is un-set when all picker items un-checked programmatically."
          );
          is(
            document.activeElement,
            firstItem,
            "First picker item becomes focusable again."
          );

          synthesizeKey(" ");
          is(
            picker.value,
            firstItem.value,
            "Hitting space selects the focused item."
          );

          synthesizeKey("KEY_Tab", { shiftKey: true });
          firstItem.disabled = true;
          secondItem.disabled = true;
          await picker.updateComplete;

          synthesizeKey("KEY_Tab");
          is(
            document.activeElement,
            thirdItem,
            "First non-disabled picker item is focusable when all items are un-checked."
          );

          synthesizeKey("KEY_Enter");
          is(
            picker.value,
            thirdItem.value,
            "Hitting enter selects the focused item."
          );
        }

        await validateNoItemSelected("radio");
        await validateNoItemSelected("listbox");
      });

      // Verify expected events emitted in the correct order.
      add_task(async function testPickerEvents() {
        async function validatePickerEvents(type) {
          let { picker, items, firstItem, secondItem, thirdItem } =
            await renderVisualPicker(type);
          let { trackEvent, verifyEvents } = testHelpers.getInputEventHelpers();

          // Ensure picker is visible in the test harness.
          picker.focus();
          picker.scrollIntoView();

          items.forEach(item => {
            item.addEventListener("click", trackEvent);
            item.addEventListener("input", trackEvent);
            item.addEventListener("change", trackEvent);
          });
          picker.addEventListener("change", trackEvent);
          picker.addEventListener("input", trackEvent);

          // Verify that clicking on a item emits the right events in the correct order.
          synthesizeMouseAtCenter(thirdItem.itemEl, {});
          await TestUtils.waitForTick();

          verifyEvents([
            {
              type: "click",
              value: "third",
              localName: "moz-visual-picker-item",
              checked: true,
            },
            {
              type: "input",
              value: "third",
              localName: "moz-visual-picker-item",
              checked: true,
            },
            { type: "input", value: "third", localName: "moz-visual-picker" },
            {
              type: "change",
              value: "third",
              localName: "moz-visual-picker-item",
              checked: true,
            },
            { type: "change", value: "third", localName: "moz-visual-picker" },
          ]);

          // Verify that keyboard navigation emits the right events in the correct order.
          synthesizeKey("KEY_ArrowUp", {});
          await picker.updateComplete;
          await TestUtils.waitForTick();

          if (type == "radio") {
            info(
              "For radio pickers arrow key navigation changes the value and emits change and input events."
            );
            is(picker.value, secondItem.value, "picker value is updated.");
            verifyEvents([
              {
                type: "input",
                value: "second",
                localName: "moz-visual-picker",
              },
              {
                type: "change",
                value: "second",
                localName: "moz-visual-picker",
              },
            ]);
          } else {
            info(
              "For listbox pickers arrow key navigation does not change the value or emit events."
            );
            is(picker.value, thirdItem.value, "picker value is updated.");
            verifyEvents([]);
          }

          // Verify that changing the group's value directly doesn't emit any events.
          picker.value = firstItem.value;
          await picker.updateComplete;
          ok(firstItem.checked, "Expected item is checked.");
          await TestUtils.waitForTick();
          verifyEvents([]);

          // Verify that changing a item's checked state directly doesn't emit any events.
          secondItem.checked = true;
          await picker.updateComplete;
          is(picker.value, secondItem.value, "Picker value is updated.");
          await TestUtils.waitForTick();
          verifyEvents([]);

          // Verify activating item with space emits proper events.
          picker.value = "";
          await picker.updateComplete;

          ok(!firstItem.checked, "The first item is not selected.");
          firstItem.focus();
          synthesizeKey(" ");
          await TestUtils.waitForTick();
          verifyEvents([
            {
              type: "click",
              value: "first",
              localName: "moz-visual-picker-item",
              checked: true,
            },
            {
              type: "input",
              value: "first",
              localName: "moz-visual-picker-item",
              checked: true,
            },
            { type: "input", value: "first", localName: "moz-visual-picker" },
            {
              type: "change",
              value: "first",
              localName: "moz-visual-picker-item",
              checked: true,
            },
            { type: "change", value: "first", localName: "moz-visual-picker" },
          ]);
        }

        await validatePickerEvents("radio");
        await validatePickerEvents("listbox");
      });

      // Verifies listbox picker roles, states, and properties meet these requirements:
      // https://www.w3.org/WAI/ARIA/apg/patterns/listbox/#wai-ariaroles,states,andproperties
      add_task(async function testListboxPickerRoleStatesProperties() {
        const LISTBOX_LABEL = "I'm a listbox";
        let listboxTemplate = html`
          <moz-visual-picker
            type="listbox"
            label=${LISTBOX_LABEL}
            value="first"
          >
            <moz-visual-picker-item value="first">
              <div class="slotted">In the slot</div>
            </moz-visual-picker-item>
            <moz-visual-picker-item value="second">
              <div class="slotted">In the slot</div>
            </moz-visual-picker-item>
            <moz-visual-picker-item value="third">
              <div class="slotted">In the slot</div>
            </moz-visual-picker-item>
          </moz-visual-picker>
        `;
        let renderTarget = await testHelpers.renderTemplate(listboxTemplate);
        let picker = renderTarget.querySelector("moz-visual-picker");
        let items = [
          ...renderTarget.querySelectorAll("moz-visual-picker-item"),
        ];
        let [firstItem, secondItem, thirdItem] = items;

        // Ensure picker is visible in the test harness.
        picker.focus();
        picker.scrollIntoView();

        is(
          picker.fieldset.getAttribute("role"),
          "listbox",
          "The top level picker element has a role of 'listbox'."
        );
        is(
          picker.fieldset.shadowRoot
            .querySelector("fieldset")
            .getAttribute("aria-orientation"),
          "horizontal",
          "The top level picker element's aria-orientation is set to 'horizontal'"
        );

        let slottedItems = picker.shadowRoot
          .querySelector("slot:not([name])")
          .assignedElements();

        items.forEach(item => {
          is(
            item.itemEl.getAttribute("role"),
            "option",
            "Each picker item has a role of 'option'."
          );
          ok(
            slottedItems.includes(item),
            "Item is contained by the listbox element."
          );
        });

        let legend = picker.fieldset.shadowRoot.querySelector("legend");
        is(legend.innerText, LISTBOX_LABEL, "The listbox element is labelled.");
        ok(BrowserTestUtils.isVisible(legend), "The listbox label is visible.");

        ok(firstItem.checked, "The first item is selected.");
        is(
          firstItem.itemEl.getAttribute("aria-selected"),
          "true",
          "aria-selected is set on the selected item."
        );
        [secondItem, thirdItem].forEach(item => {
          ok(!item.checked, "All other items are unchecked.");
          is(
            item.itemEl.getAttribute("aria-selected"),
            "false",
            "All other items have aria-selected set to false."
          );
        });

        synthesizeMouseAtCenter(thirdItem, {});
        await picker.updateComplete;

        ok(thirdItem.checked, "The third item is now selected.");
        is(
          thirdItem.itemEl.getAttribute("aria-selected"),
          "true",
          "aria-selected is set on the selected item."
        );
        [firstItem, secondItem].forEach(item => {
          ok(
            !item.checked,
            "All other items are unchecked because only one item can be selected at a time."
          );
          is(
            item.itemEl.getAttribute("aria-selected"),
            "false",
            "All other items have aria-selected set to false."
          );
        });
      });

      // Verifies listbox picker keyboard interactions meet these requirements:
      // https://www.w3.org/WAI/ARIA/apg/patterns/listbox/#keyboardinteraction
      add_task(async function testListboxPickerInteractions() {
        let listboxTemplate = html`
          <button tabindex="0">Before picker</button>
          <moz-visual-picker type="listbox" label="Another listbox">
            <moz-visual-picker-item value="first">
              <div class="slotted">In the slot</div>
            </moz-visual-picker-item>
            <moz-visual-picker-item value="second">
              <div class="slotted">In the slot</div>
            </moz-visual-picker-item>
            <moz-visual-picker-item value="third">
              <div class="slotted">In the slot</div>
            </moz-visual-picker-item>
          </moz-visual-picker>
        `;
        let renderTarget = await testHelpers.renderTemplate(listboxTemplate);
        let picker = renderTarget.querySelector("moz-visual-picker");
        let items = [
          ...renderTarget.querySelectorAll("moz-visual-picker-item"),
        ];
        let [firstItem, secondItem, thirdItem] = items;
        let beforeButton = renderTarget.querySelector("button");

        const navigate = direction => keyboardNavigate(picker, direction);

        beforeButton.focus();
        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          firstItem,
          "First item is focused when no items are selected and the picker receives focus."
        );

        synthesizeKey("KEY_Tab", { shiftKey: true });
        isnot(
          document.activeElement,
          firstItem,
          "Item loses focus when the picker is blurred."
        );

        picker.value = "third";
        await picker.updateComplete;
        synthesizeKey("KEY_Tab", {});

        ok(
          thirdItem.checked,
          "Third item is selected after picker value changes."
        );
        is(
          document.activeElement,
          thirdItem,
          "The selected item is focused when when the picker receives focus."
        );

        await navigate("down");
        is(
          document.activeElement,
          thirdItem,
          "Focus does not wrap, so the third item stays focused after hitting the down arrow."
        );
        ok(!firstItem.checked, "Selection does not follow focus.");
        is(picker.value, "third", "The picker value is unchanged.");

        await navigate("left");
        is(
          document.activeElement,
          secondItem,
          "The second item is focused after hitting the left arrow from the third item."
        );
        ok(!secondItem.checked, "Selection does not follow focus.");
        is(
          picker.value,
          "third",
          "The picker value is not changed by navigation."
        );

        await navigate("up");
        is(
          document.activeElement,
          firstItem,
          "The first item is focused after hitting the up arrow from the second item."
        );
        ok(!firstItem.checked, "Selection does not follow focus.");
        is(
          picker.value,
          "third",
          "The picker value is not changed by navigation."
        );

        await navigate("left");
        is(
          document.activeElement,
          firstItem,
          "Focus does not wrap, so the first item stays focused after hitting the left arrow."
        );
        ok(!firstItem.checked, "Selection does not follow focus.");
        is(
          picker.value,
          "third",
          "The picker value is not changed by navigation."
        );

        await navigate("right");
        is(
          document.activeElement,
          secondItem,
          "The second item is focused after hitting the right arrow from the first item."
        );
        ok(!secondItem.checked, "Selection does not follow focus.");
        is(
          picker.value,
          "third",
          "The picker value is not changed by navigation."
        );

        synthesizeKey(" ");
        ok(
          secondItem.checked,
          "Second item is selected when space bar is pressed."
        );
        is(
          picker.value,
          "second",
          "The picker value changes when a new item is selected."
        );

        await navigate("left");
        synthesizeKey("KEY_Enter");
        ok(
          firstItem.checked,
          "First item is selected when enter key is pressed."
        );
        is(
          picker.value,
          "first",
          "The picker value changes when a new item is selected."
        );
      });
    </script>
  </head>
  <body>
    <p id="display"></p>
    <div id="content" style="display: none"></div>
    <pre id="test"></pre>
  </body>
</html>