File: test_moz_box_group.html

package info (click to toggle)
firefox 147.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,532 kB
  • sloc: cpp: 7,607,356; javascript: 6,533,348; ansic: 3,775,236; python: 1,415,508; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,760; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (700 lines) | stat: -rw-r--r-- 22,442 bytes parent folder | download
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
<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>MozBoxGroup 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://mochikit/content/tests/SimpleTest/test.css"
    />
    <link rel="stylesheet" href="chrome://global/skin/in-content/common.css" />
    <script
      type="module"
      src="chrome://global/content/elements/moz-box-group.mjs"
    ></script>
    <script src="lit-test-helpers.js"></script>
    <script>
      let html;
      let testHelpers = new LitTestHelpers();

      add_setup(async function setup() {
        ({ html } = await testHelpers.setupLit());
        let templateFn = () => html`
          <moz-box-group>
            <moz-box-item label="item"></moz-box-item>
            <moz-box-button label="button"></moz-box-button>
          </moz-box-group>
        `;
        testHelpers.setupTests({ templateFn });
      });

      add_task(async function testMozBoxGroupStyles() {
        let {
          children: [boxGroup],
        } = await testHelpers.renderTemplate();
        let [boxItem, boxButton] = boxGroup.shadowRoot
          .querySelector("slot:not([name])")
          .assignedElements();

        const normalizePx = pxVal => Math.round(parseFloat(pxVal));

        function verifyStyles(el, expectedStyles) {
          let styles = getComputedStyle(el);
          Object.entries(expectedStyles).forEach(([property, value]) => {
            is(
              normalizePx(styles[property]),
              normalizePx(value),
              `${property} is ${value}.`
            );
          });
        }

        const FIRST_ITEM_STYLES = {
          borderStartEndRadius: "7px",
          borderStartStartRadius: "7px",
          borderEndEndRadius: "0px",
          borderEndStartRadius: "0px",
          borderTopWidth: "0px",
        };

        const MIDDLE_ITEM_STYLES = {
          borderStartEndRadius: "0px",
          borderStartStartRadius: "0px",
          borderEndEndRadius: "0px",
          borderEndStartRadius: "0px",
          borderTopWidth: "1px",
        };

        const LAST_ITEM_STYLES = {
          borderStartEndRadius: "0px",
          borderStartStartRadius: "0px",
          borderEndEndRadius: "7px",
          borderEndStartRadius: "7px",
          borderTopWidth: "1px",
        };

        // Verify that two items use the first and last item styles.
        verifyStyles(boxItem, FIRST_ITEM_STYLES);
        verifyStyles(boxButton, LAST_ITEM_STYLES);

        // Change the last item and check that styles change accordingly.
        let slotChanged = BrowserTestUtils.waitForEvent(
          boxGroup.shadowRoot,
          "slotchange"
        );
        let secondButton = document.createElement("moz-box-button");
        secondButton.label = "second button";
        boxGroup.append(secondButton);
        await slotChanged;

        verifyStyles(boxItem, FIRST_ITEM_STYLES);
        verifyStyles(boxButton, MIDDLE_ITEM_STYLES);
        verifyStyles(secondButton, LAST_ITEM_STYLES);

        // Change the first item and verify that styles change accordingly.
        slotChanged = BrowserTestUtils.waitForEvent(
          boxGroup.shadowRoot,
          "slotchange"
        );
        let secondItem = document.createElement("moz-box-item");
        secondButton.label = "second item";
        boxGroup.prepend(secondItem);
        await slotChanged;

        verifyStyles(secondItem, FIRST_ITEM_STYLES);
        verifyStyles(boxItem, MIDDLE_ITEM_STYLES);
        verifyStyles(boxButton, MIDDLE_ITEM_STYLES);
        verifyStyles(secondButton, LAST_ITEM_STYLES);

        // Verify that hiding items doesn't break the box-group styles.
        secondItem.hidden = true;
        await TestUtils.waitForTick();

        verifyStyles(boxItem, FIRST_ITEM_STYLES);
        verifyStyles(boxButton, MIDDLE_ITEM_STYLES);
        verifyStyles(secondButton, LAST_ITEM_STYLES);
      });

      add_task(async function testMozBoxGroupList() {
        let listTemplate = html`<moz-box-group type="list">
          <moz-box-item label="header" slot="header"></moz-box-item>
          <moz-box-item label="item"></moz-box-item>
          <moz-box-item label="item 2"></moz-box-item>
          <moz-box-item label="item 3"></moz-box-item>
          <moz-box-button label="button"></moz-box-button>
          <moz-box-item label="footer" slot="footer"></moz-box-item>
        </moz-box-group>`;
        let {
          children: [boxGroup],
        } = await testHelpers.renderTemplate(listTemplate);
        let boxEls = boxGroup.querySelectorAll("moz-box-item, moz-box-button");
        const getListItems = () => boxGroup.shadowRoot.querySelectorAll("li");

        let list = boxGroup.shadowRoot.querySelector("ul");
        ok(list, "Box group renders items in a list element.");
        is(
          list.ariaOrientation,
          "vertical",
          "aria-orientation is set on the list element."
        );

        let listItems = getListItems();
        is(
          listItems.length,
          4,
          "Box group renders an li for each slotted moz-box-* element."
        );

        let [listHeader] = boxGroup.shadowRoot
          .querySelector('slot[name="header"]')
          .assignedElements();
        is(listHeader, boxEls[0], "List header is the first box element");

        let [listFooter] = boxGroup.shadowRoot
          .querySelector('slot[name="footer"]')
          .assignedElements();
        is(
          listFooter,
          boxEls[boxEls.length - 1],
          "List footer is the last box element"
        );

        listItems.forEach((item, i) => {
          let element = item.querySelector("slot").assignedElements()[0];
          is(
            element,
            boxGroup.listItems[i],
            "Each moz-box-* element is wrapped in an li."
          );
        });

        let slotChanged = BrowserTestUtils.waitForEvent(
          boxGroup.shadowRoot,
          "slotchange"
        );
        let newItem = document.createElement("moz-box-item");
        newItem.label = "create another item.";
        boxGroup.append(newItem);
        await slotChanged;

        listItems = getListItems();
        is(
          listItems.length,
          5,
          "Box group renders an additional li for the newly slotted element."
        );
        let lastElement = listItems[listItems.length - 1]
          .querySelector("slot")
          .assignedElements()[0];
        is(
          lastElement,
          newItem,
          "The new item is slotted in the last li element."
        );
      });

      add_task(async function testMozBoxGroupKeyboardInteraction() {
        let keyboardTemplate = html`<moz-button tabindex="0">
            Focus me!
          </moz-button>
          <moz-box-group>
            <moz-box-item label="header" slot="header"></moz-box-item>
            <moz-box-item label="item">
              <moz-button
                class="first-button"
                slot="actions-start"
                iconsrc="chrome://global/skin/icons/more.svg"
              ></moz-button>
              <moz-toggle slot="actions"></moz-toggle>
              <moz-button
                class="second-button"
                slot="actions"
                iconsrc="chrome://global/skin/icons/more.svg"
              ></moz-button>
            </moz-box-item>
            <moz-box-link label="link"></moz-box-link>
            <moz-box-button label="button"></moz-box-button>
            <moz-box-button label="footer" slot="footer"></moz-box-button>
          </moz-box-group>
          <moz-button tabindex="0">Focus me too!</moz-button>`;
        let {
          children: [beforeButton, boxGroup, afterButton],
        } = await testHelpers.renderTemplate(keyboardTemplate);
        let [, boxItem, boxLink, boxButton, footerButton] =
          boxGroup.querySelectorAll(
            "moz-box-item, moz-box-button, moz-box-link"
          );

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

        isnot(document.activeElement, boxGroup, "Box group is not focused.");
        beforeButton.focus();

        // Verify tab can be used to move through elements in a regular moz-box-group.
        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          boxItem.querySelector(".first-button"),
          "Focus moves to the first button action element in moz-box-item."
        );

        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          boxItem.querySelector("moz-toggle"),
          "Focus moves to the toggle action element in moz-box-item."
        );

        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          boxItem.querySelector(".second-button"),
          "Focus moves to the second button action element in the moz-box-item."
        );

        await keyboardNavigate("left");
        is(
          document.activeElement,
          boxItem.querySelector("moz-toggle"),
          "Focus moves to the toggle action element in moz-box-item."
        );

        await keyboardNavigate("left");
        is(
          document.activeElement,
          boxItem.querySelector(".first-button"),
          "Focus moves to the first button action element in moz-box-item."
        );

        await keyboardNavigate("right");
        is(
          document.activeElement,
          boxItem.querySelector("moz-toggle"),
          "Focus moves to the toggle action element in the moz-box-item."
        );

        await keyboardNavigate("right");
        is(
          document.activeElement,
          boxItem.querySelector(".second-button"),
          "Focus moves to the second button action element in the moz-box-item."
        );

        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          boxLink,
          "Focus moves to the moz-box-link element."
        );

        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          boxButton,
          "Focus moves to the moz-box-button element."
        );

        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          footerButton,
          "Focus moves to the footer moz-box-button element."
        );

        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          afterButton,
          "Focus moves out of the group after hitting tab."
        );

        synthesizeKey("KEY_Tab", { shiftKey: true });
        is(
          document.activeElement,
          footerButton,
          "Focus moves back to the moz-box-button element."
        );

        synthesizeKey("KEY_Tab", { shiftKey: true });
        is(
          document.activeElement,
          boxButton,
          "Focus moves back to the moz-box-button element."
        );

        synthesizeKey("KEY_Tab", { shiftKey: true });
        is(
          document.activeElement,
          boxLink,
          "Focus moves back to the moz-box-link element."
        );

        synthesizeKey("KEY_Tab", { shiftKey: true });
        is(
          document.activeElement,
          boxItem.querySelector(".second-button"),
          "Focus moves back to the second button action element in the moz-box-item."
        );

        synthesizeKey("KEY_Tab", { shiftKey: true });
        is(
          document.activeElement,
          boxItem.querySelector("moz-toggle"),
          "Focus moves back to the toggle action element in the moz-box-item."
        );

        synthesizeKey("KEY_Tab", { shiftKey: true });
        is(
          document.activeElement,
          boxItem.querySelector(".first-button"),
          "Focus moves back to the first button action element in the moz-box-item."
        );

        synthesizeKey("KEY_Tab", { shiftKey: true });
        is(
          document.activeElement,
          beforeButton,
          "Focus moves out of the group."
        );

        // Verify tab and arrow key behavior in moz-box-group of type="list".
        boxGroup.type = "list";
        await boxGroup.updateComplete;
        await Promise.all(boxGroup.listItems.map(item => item.updateComplete));

        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          boxItem.querySelector(".first-button"),
          "Focus moves to the first button action element in moz-box-item."
        );

        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          boxItem.querySelector("moz-toggle"),
          "Focus moves to the toggle action element in moz-box-item."
        );

        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          boxItem.querySelector(".second-button"),
          "Focus moves to the second button action element in the moz-box-item."
        );

        await keyboardNavigate("left");
        is(
          document.activeElement,
          boxItem.querySelector("moz-toggle"),
          "Focus moves to the toggle action element in moz-box-item."
        );

        await keyboardNavigate("left");
        is(
          document.activeElement,
          boxItem.querySelector(".first-button"),
          "Focus moves to the first button action element in moz-box-item."
        );

        await keyboardNavigate("right");
        is(
          document.activeElement,
          boxItem.querySelector("moz-toggle"),
          "Focus moves to the toggle action element in moz-box-item."
        );

        await keyboardNavigate("right");
        is(
          document.activeElement,
          boxItem.querySelector(".second-button"),
          "Focus moves to the second button action element in the moz-box-item."
        );

        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          footerButton,
          "Other list items are not tabbable, so focus moves to the footer button."
        );

        synthesizeKey("KEY_Tab", {});
        is(
          document.activeElement,
          afterButton,
          "Focus moves out of the group."
        );

        synthesizeKey("KEY_Tab", { shiftKey: true });
        is(
          document.activeElement,
          footerButton,
          "Focus moves to the last element in the group."
        );

        await keyboardNavigate("up");
        is(
          document.activeElement,
          footerButton,
          "Focus stays on the last element in the group."
        );

        synthesizeKey("KEY_Tab", { shiftKey: true });
        is(
          document.activeElement,
          boxButton,
          "Focus moves to the last element in the list."
        );

        await keyboardNavigate("up");
        is(
          document.activeElement,
          boxLink,
          "Focus moves to the moz-box-link element."
        );

        await keyboardNavigate("up");
        is(
          document.activeElement,
          boxItem.querySelector(".second-button"),
          "Focus moves to the second button action element in the moz-box-item."
        );

        await keyboardNavigate("left");
        is(
          document.activeElement,
          boxItem.querySelector("moz-toggle"),
          "Focus moves to the toggle action element in the moz-box-item."
        );

        await keyboardNavigate("up");
        is(
          document.activeElement,
          boxItem.querySelector("moz-toggle"),
          "Up arrow key does not move focus out of the group from the first item."
        );

        await keyboardNavigate("down");
        is(
          document.activeElement,
          boxLink,
          "Focus moves to the moz-box-link element."
        );

        await keyboardNavigate("down");
        is(
          document.activeElement,
          boxButton,
          "Focus moves to the moz-box-button element."
        );

        await keyboardNavigate("down");
        is(
          document.activeElement,
          boxButton,
          "Down arrow key does not move focus out of the group from the last item."
        );

        // Validate left/right keyboard navigation between action items for RTL.
        await SpecialPowers.pushPrefEnv({
          set: [["intl.l10n.pseudo", "bidi"]],
        });

        // Navigate up to focus moz-box-item
        await keyboardNavigate("up");
        await keyboardNavigate("up");

        is(
          document.activeElement,
          boxItem.querySelector(".second-button"),
          "Focus moves to the second button action element in the moz-box-item."
        );

        await keyboardNavigate("right");
        is(
          document.activeElement,
          boxItem.querySelector("moz-toggle"),
          "Focus moves to the toggle action element in the moz-box-item."
        );

        await keyboardNavigate("right");
        is(
          document.activeElement,
          boxItem.querySelector(".first-button"),
          "Focus moves to the first button action element in the moz-box-item."
        );

        await keyboardNavigate("left");
        is(
          document.activeElement,
          boxItem.querySelector("moz-toggle"),
          "Focus moves to the toggle action element in the moz-box-item."
        );

        await SpecialPowers.popPrefEnv();
      });

      add_task(async function testMozBoxGroupKeydownOnWhitespace() {
        // Test bug 2001746 - ignore keypresses that fire on the ul if whitespace
        // was clicked in moz-box-group
        let listTemplate = html`<moz-box-group type="list">
          <moz-box-item label="item 1"></moz-box-item>
          <moz-box-item label="item 2"></moz-box-item>
          <moz-box-item label="item 3"></moz-box-item>
        </moz-box-group>`;
        let {
          children: [boxGroup],
        } = await testHelpers.renderTemplate(listTemplate);

        let list = boxGroup.shadowRoot.querySelector("ul");
        ok(list, "Box group has a list element.");

        // Simulate clicking on the list container (whitespace) by focusing it
        list.focus();
        is(
          boxGroup.shadowRoot.activeElement,
          list,
          "The list container can be focused."
        );
        is(
          document.activeElement,
          boxGroup,
          "The moz-box-group is the activeElement."
        );

        // Press arrow keys - this should not throw an error
        let errorThrown = false;
        try {
          synthesizeKey("KEY_ArrowDown");
          await boxGroup.updateComplete;
          synthesizeKey("KEY_ArrowUp");
          await boxGroup.updateComplete;
        } catch (e) {
          errorThrown = true;
          ok(false, `Unexpected error: ${e.message}`);
        }

        ok(
          !errorThrown,
          "No error thrown when pressing keys on whitespace in moz-box-group."
        );
        is(
          document.activeElement,
          boxGroup,
          "Focus did not move after arrow key press."
        );
      });

      add_task(async function testMozBoxGroupReorderable() {
        let reorderableTemplate = html`<moz-box-group type="reorderable-list">
          <moz-box-item label="item 1"></moz-box-item>
          <moz-box-item label="item 2"></moz-box-item>
          <moz-box-item label="item 3"></moz-box-item>
          <moz-box-item label="footer" slot="footer"></moz-box-item>
        </moz-box-group>`;
        let {
          children: [boxGroup],
        } = await testHelpers.renderTemplate(reorderableTemplate);
        let boxItems = boxGroup.querySelectorAll("moz-box-item");
        let [item1, item2, item3, footer] = boxItems;

        async function moveItem(item, direction) {
          let handle = item.handleEl;
          if (!handle) {
            return;
          }
          let keydown = BrowserTestUtils.waitForEvent(handle, "keydown");
          handle.focus();
          synthesizeKey(
            `KEY_Arrow${direction.charAt(0).toUpperCase() + direction.slice(1)}`,
            { shiftKey: true, ctrlKey: true }
          );
          await keydown;
          await boxGroup.updateComplete;
        }

        ok(
          boxGroup.reorderableList,
          "Box group renders a moz-reorderable-list."
        );

        is(
          boxGroup.querySelector("moz-box-item:nth-of-type(2)"),
          item2,
          "item2 starts in the second position."
        );
        is(
          boxGroup.querySelector("moz-box-item:nth-of-type(3)"),
          item3,
          "item3 starts after item2 in the third position."
        );

        await moveItem(item3, "up");

        is(
          boxGroup.querySelector("moz-box-item:nth-of-type(2)"),
          item3,
          "item3 has been reordered to the second position."
        );
        is(
          boxGroup.querySelector("moz-box-item:nth-of-type(3)"),
          item2,
          "item2 has been reordered to the third position."
        );

        await moveItem(item1, "down");

        is(
          boxGroup.querySelector("moz-box-item:nth-of-type(1)"),
          item3,
          "item3 has been reordered to the first position."
        );
        is(
          boxGroup.querySelector("moz-box-item:nth-of-type(2)"),
          item1,
          "item1 has been reordered to the second position."
        );
        is(
          boxGroup.querySelector("moz-box-item:nth-of-type(3)"),
          item2,
          "item2 is still in the third position."
        );

        await moveItem(item2, "down");

        is(
          boxGroup.querySelector("moz-box-item:nth-of-type(3)"),
          item2,
          "item2 is still in the third position."
        );

        is(
          boxGroup.querySelector("moz-box-item:nth-of-type(4)"),
          footer,
          "footer is in the forth position."
        );

        await moveItem(footer, "up");

        is(
          boxGroup.querySelector("moz-box-item:nth-of-type(4)"),
          footer,
          "footer is still in the forth position."
        );
      });
    </script>
  </head>
  <body>
    <p id="display"></p>
    <div id="content" style="display: none"></div>
    <pre id="test"></pre>
  </body>
</html>