File: test_moz_select.html

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; 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 (237 lines) | stat: -rw-r--r-- 8,997 bytes parent folder | download | duplicates (2)
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
<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>MozSelect 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 src="lit-test-helpers.js"></script>
    <script>
      let testHelpers = new InputTestHelpers();

      add_setup(async function setup() {
        await testHelpers.setupLit();
        let templateFn = (attrs, children) => testHelpers.html`
        <moz-select ${attrs}>
          <moz-option value="1" label="One"></moz-option>
          <moz-option value="value" label="Initial"></moz-option>
          <moz-option value="new value" label="Changed"></moz-option>
          <moz-option value="4" label="Four"></moz-option>
          ${children}
        </moz-select>
      `;
        testHelpers.setupTests({ templateFn });
      });

      add_task(async function testMozSelectProperties() {
        await testHelpers.testCommonInputProperties("moz-select");
      });

      add_task(async function testMozSelectChange() {
        let { trackEvent, verifyEvents } = testHelpers.getInputEventHelpers();
        let target = await testHelpers.renderTemplate();
        let select = target.querySelector("moz-select");
        await select.updateComplete;
        is(select.value, "1", "First option is selected");

        select.addEventListener("change", trackEvent);
        select.addEventListener("input", trackEvent);
        select.addEventListener("click", trackEvent);

        let changed = BrowserTestUtils.waitForEvent(select, "change");
        let topWindow = window.docShell.chromeEventHandler.ownerGlobal;
        let pickerOpened = BrowserTestUtils.waitForSelectPopupShown(topWindow);
        select.focus();
        synthesizeKey(" ");
        await pickerOpened;
        synthesizeKey("KEY_ArrowDown");
        synthesizeKey("KEY_Enter");
        let event = await changed;

        is(select.value, "value", "Second option is selected");
        is(event.target, select, "Change event is on moz-select");

        verifyEvents([
          { type: "input", localName: "moz-select", value: "value" },
          { type: "change", localName: "moz-select", value: "value" },
        ]);

        changed = BrowserTestUtils.waitForEvent(select, "change");
        pickerOpened = BrowserTestUtils.waitForSelectPopupShown(topWindow);
        synthesizeMouseAtCenter(select, {});
        let menupopup = await pickerOpened;
        let thirdOption = menupopup.querySelectorAll("menuitem")[2];
        synthesizeMouseAtCenter(thirdOption, {}, topWindow);
        event = await changed;

        is(select.value, "new value", "Second option is selected");
        is(event.target, select, "Change event is on moz-select");

        verifyEvents([
          { type: "click", localName: "moz-select", value: "value" },
          { type: "input", localName: "moz-select", value: "new value" },
          { type: "change", localName: "moz-select", value: "new value" },
          { type: "click", localName: "moz-select", value: "new value" },
        ]);
      });

      add_task(async function testMozSelectModifyOptions() {
        let target = await testHelpers.renderTemplate();
        let select = target.querySelector("moz-select");
        await select.updateComplete;

        let options = select.inputEl.querySelectorAll("option");
        let mozOptions = select.querySelectorAll("moz-option");

        is(select.value, "1", "moz-select value correct");
        is(options[0].textContent.trim(), "One", "First option text correct");
        is(options[0].value, "1", "First option value correct");
        is(
          options[1].textContent.trim(),
          "Initial",
          "Second option text correct"
        );
        is(options[1].value, "value", "Second option value correct");

        mozOptions[0].label = "First";
        mozOptions[0].value = "i";
        mozOptions[1].label = "Second";
        mozOptions[1].value = "ii";
        await TestUtils.waitForTick();
        await select.updateComplete;

        is(select.value, "i", "moz-select value updated");
        is(options[0].textContent.trim(), "First", "First option text updated");
        is(options[0].value, "i", "First option value updated");
        is(
          options[1].textContent.trim(),
          "Second",
          "Second option text updated"
        );
        is(options[1].value, "ii", "Second option value updated");
      });

      add_task(async function testMozSelectOptionWithIcon() {
        let target = await testHelpers.renderTemplate();
        let select = target.querySelector("moz-select");
        await select.updateComplete;

        is(select.value, "1", "First option is selected");

        let selectWrapper = select.shadowRoot.querySelector(".select-wrapper");
        ok(
          !selectWrapper.classList.contains("with-icon"),
          "Select doesn't have an icon"
        );

        let mozOptions = select.querySelectorAll("moz-option");
        mozOptions[0].iconSrc = "chrome://global/skin/icons/info.svg";
        mozOptions[1].iconSrc = "chrome://global/skin/icons/warning.svg";
        await TestUtils.waitForTick();
        await select.updateComplete;

        info("New iconSrc value was added to the selected option.");
        ok(selectWrapper.classList.contains("with-icon"), "Select has an icon");
        is(
          select.shadowRoot.querySelector(".select-option-icon").src,
          mozOptions[0].iconSrc,
          "Select displays the icon of the selected option"
        );

        select.value = "value";
        await select.updateComplete;
        is(select.value, "value", "Second option is selected");
        is(
          select.inputEl.value,
          select.value,
          "Select element value is updated"
        );
        is(
          select.shadowRoot.querySelector(".select-option-icon").src,
          mozOptions[1].iconSrc,
          "Select displays the icon of the newly selected option"
        );
      });

      function openSelectAndGetMenuPopup(select) {
        let topWindow = window.docShell.chromeEventHandler.ownerGlobal;
        let pickerOpened = BrowserTestUtils.waitForSelectPopupShown(topWindow);
        select.focus();
        synthesizeKey(" ");
        return pickerOpened;
      }

      /**
       * Test that hidden and disabled moz-option are reflected in the popup menu.
       */
      add_task(async function testMozSelectHiddenAndDisabledOptions() {
        let target = await testHelpers.renderTemplate();
        let select = target.querySelector("moz-select");
        await select.updateComplete;

        let mozOptions = select.querySelectorAll("moz-option");

        // Open select and verify all elements are visible and enabled.
        let popup = await openSelectAndGetMenuPopup(select);
        let popupItems = Array.from(popup.children);

        // Items that are hidden are not included in the popup's children.
        is(popupItems.length, mozOptions.length, "All options are shown");
        for (let i = 0; i < popupItems.length; i++) {
          ok(!popupItems[i].disabled, `Option ${i} is enabled`);
        }

        // Close popup.
        let hiddenPromise = BrowserTestUtils.waitForPopupEvent(popup, "hidden");
        popup.hidePopup();
        await hiddenPromise;

        await TestUtils.waitForTick();
        await select.updateComplete;

        // set first option to disabled and second option to hidden.
        mozOptions[0].disabled = true;
        mozOptions[1].hidden = true;

        await TestUtils.waitForTick();
        await select.updateComplete;

        // open select and verify first option is disabled and second option is hidden.
        popup = await openSelectAndGetMenuPopup(select);
        popupItems = Array.from(popup.children);

        is(
          popupItems.length,
          mozOptions.length - 1,
          "All but one option shown"
        );

        ok(popupItems[0].disabled, "First option is disabled");
        is(popupItems[0].label, mozOptions[0].label, "First option is visible");
        ok(!popupItems[1].disabled, "Second option is enabled");
        is(popupItems[1].label, mozOptions[2].label, "Third option is visible");
        ok(!popupItems[2].disabled, "Third option is enabled");
        is(
          popupItems[2].label,
          mozOptions[3].label,
          "Fourth option is visible"
        );

        // Close popup.
        hiddenPromise = BrowserTestUtils.waitForPopupEvent(popup, "hidden");
        popup.hidePopup();
        await hiddenPromise;

        // Reset options
        mozOptions[0].disabled = false;
        mozOptions[1].hidden = false;
      });
    </script>
  </head>
  <body></body>
</html>