File: select-synthetic-events.html

package info (click to toggle)
firefox 145.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,528 kB
  • sloc: cpp: 7,594,999; javascript: 6,459,658; ansic: 3,752,909; python: 1,403,455; xml: 629,809; asm: 438,679; java: 186,421; sh: 67,287; makefile: 19,169; objc: 13,086; perl: 12,982; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (102 lines) | stat: -rw-r--r-- 3,849 bytes parent folder | download | duplicates (12)
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
<!DOCTYPE html>
<link rel=author href="mailto:jarhar@chromium.org">
<link rel=help href="https://github.com/whatwg/html/issues/10762">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>

<style>
select, ::picker(select) {
  appearance: base-select;
}
</style>

<select id=defaultbutton>
  <option class=one>one</option>
  <option class=two>two</option>
</select>

<select id=custombutton>
  <button>button</button>
  <option class=one>one</option>
  <option class=two>two</option>
</select>

<script>
const keyCodes = ['Enter', 'Space', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'];

function dispatchKeyboardEvents(element, code) {
  const key = code == 'Space' ? ' ' : code;
  element.dispatchEvent(new KeyboardEvent('keydown', {key, code}));
  element.dispatchEvent(new KeyboardEvent('keypress', {key, code}));
  element.dispatchEvent(new KeyboardEvent('keyup', {key, code}));
}

function dispatchMouseEvents(element) {
  element.dispatchEvent(new MouseEvent('pointerdown'));
  element.dispatchEvent(new MouseEvent('mousedown'));
  element.dispatchEvent(new MouseEvent('pointerup'));
  element.dispatchEvent(new MouseEvent('mouseup'));
  element.dispatchEvent(new MouseEvent('click'));
}

['defaultbutton', 'custombutton'].forEach(id => {
  promise_test(async () => {
    assert_true(CSS.supports('appearance', 'base-select'),
      'This test requires appearance:base-select in order to run.');

    const select = document.getElementById(id);
    const firstOption = select.querySelector('option.one');
    const secondOption = select.querySelector('option.two');

    select.click();
    assert_false(select.matches(':open'),
      'select.click() should not open the picker.');

    dispatchMouseEvents(select);
    assert_false(select.matches(':open'),
      'Synthetic mouse/pointer events should not open the picker.');

    for (const keyCode of keyCodes) {
      dispatchKeyboardEvents(select, keyCode);
      assert_false(select.matches(':open'),
        `Synthetic ${keyCode} events should not open the picker.`);
      assert_equals(select.value, 'one',
        `Synthetic ${keyCode} events should not change the selects value.`);
    }

    await test_driver.click(select);
    assert_true(select.matches(':open'),
      'Select should open after a real click occurs.');
    assert_equals(document.activeElement, firstOption,
      'Selected <option> should be focused after opening the picker.');

    secondOption.click();
    assert_true(select.matches(':open'),
      'option.click() should not close the picker.');
    assert_equals(select.value, 'one',
      'option.click() should not change select.value.');

    dispatchMouseEvents(secondOption);
    assert_true(select.matches(':open'),
      'Synthetic mouse/pointer events should not close the picker.');
    assert_equals(select.value, 'one',
      'Synthetic mouse/pointer events should not change select.value.');

    for (const keyCode of keyCodes) {
      dispatchKeyboardEvents(firstOption, keyCode);
      assert_true(select.matches(':open'),
        `Synthetic ${keyCode} events on selected <option> should not close the picker.`);
      assert_equals(select.value, 'one',
        `Synthetic ${keyCode} events on selected <option> should not change select.value.`);

      dispatchKeyboardEvents(secondOption, keyCode);
      assert_true(select.matches(':open'),
        `Synthetic ${keyCode} events on non-selected <option> should not close the picker.`);
      assert_equals(select.value, 'one',
        `Synthetic ${keyCode} events on non-selected <option> should not change select.value.`);
    }
  }, `${id}: Synthetic events should not trigger behaviors of select element.`);
});
</script>