File: test_moz_input_folder.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 (247 lines) | stat: -rw-r--r-- 8,200 bytes parent folder | download | duplicates (7)
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
<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>MozInputFolder 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"
    />
    <link rel="localization" href="toolkit/global/mozInputFolder.ftl" />
    <script src="lit-test-helpers.js"></script>
    <script class="testbody" type="application/javascript">
      let testHelpers = new InputTestHelpers();
      let html;
      const { MockFilePicker } = SpecialPowers;

      add_setup(async function setup() {
        ({ html } = await testHelpers.setupLit());
        testHelpers.setupTests({
          templateFn: (attrs, children) =>
            html`<moz-input-folder ${attrs}>${children}</moz-input-folder>`,
        });
      });

      add_task(async function testMozInputFolderProperties() {
        await testHelpers.testCommonInputProperties("moz-input-folder");
      });

      add_task(async function testDisabledStates() {
        let inputFolder = document.querySelector("moz-input-folder");
        let chooseFolderButton = inputFolder.chooseFolderButtonEl;
        ok(chooseFolderButton, "Choose folder button is present.");

        inputFolder.disabled = true;
        await inputFolder.updateComplete;
        ok(inputFolder.disabled, "Input is disabled.");
        ok(
          chooseFolderButton.disabled,
          "Disabled state is propagated to the button."
        );

        inputFolder.disabled = false;
        await inputFolder.updateComplete;
        ok(!inputFolder.disabled, "Input can be re-enabled.");
        ok(
          !chooseFolderButton.disabled,
          "Disabled state is propagated to the button."
        );
      });

      add_task(async function testMozInputFolderFunctionality() {
        let { trackEvent, verifyEvents } = testHelpers.getInputEventHelpers();
        let target = await testHelpers.renderTemplate();
        let inputFolder = target.querySelector("moz-input-folder");
        let inputElement = inputFolder.inputEl;
        let chooseFolderButton = inputFolder.chooseFolderButtonEl;

        inputFolder.addEventListener("input", trackEvent);
        inputFolder.addEventListener("change", trackEvent);

        // Set up the file picker.
        const mockCustomParentDir = await IOUtils.createUniqueDirectory(
          PathUtils.tempDir,
          "input-folder-custom-dir-test"
        );
        const dummyFile = Cc["@mozilla.org/file/local;1"].createInstance(
          Ci.nsIFile
        );
        dummyFile.initWithPath(mockCustomParentDir);
        MockFilePicker.init(window.browsingContext);
        let filePickerShownPromise = new Promise(resolve => {
          MockFilePicker.showCallback = () => {
            ok(true, "Filepicker shown");
            MockFilePicker.setFiles([dummyFile]);
            resolve();
          };
        });
        MockFilePicker.returnValue = MockFilePicker.returnOK;

        //Check initial values.
        is(
          inputFolder.value,
          "",
          "The initial value of the component is an empty string."
        );
        is(
          inputElement.value,
          "",
          "The initial value of the input element is an empty string."
        );
        is(inputFolder.folder, null, "The folder is null.");
        ok(
          !inputElement.classList.contains("with-icon"),
          "The input element has no background image."
        );

        //Choose a new folder.
        let folderChanged = BrowserTestUtils.waitForEvent(
          inputFolder,
          "change"
        );
        chooseFolderButton.click();
        await filePickerShownPromise;
        await folderChanged;
        await inputFolder.updateComplete;
        await TestUtils.waitForTick();

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

        //Check if the values were updated.
        is(
          inputFolder.folder.path,
          mockCustomParentDir,
          "The folder path is the same as the path to the temporary folder."
        );
        is(
          inputFolder.value,
          mockCustomParentDir,
          "The value of the moz-input-folder is set as a path to the temporary folder."
        );
        is(
          inputElement.value,
          mockCustomParentDir,
          "The value of the input element is set as a path to the temporary folder."
        );
        ok(
          inputElement.classList.contains("with-icon"),
          "The input element has a background image."
        );

        // Ensure selecting the same folder does not send any events.
        let filePickerClosed = BrowserTestUtils.waitForEvent(
          inputFolder,
          "moz-input-folder-picker-close"
        );
        chooseFolderButton.click();
        await filePickerClosed;
        await TestUtils.waitForTick();

        verifyEvents([]);

        // Ensure cancelling the file picker does not send any events.
        MockFilePicker.returnValue = MockFilePicker.returnCancel;
        filePickerClosed = BrowserTestUtils.waitForEvent(
          inputFolder,
          "moz-input-folder-picker-close"
        );
        chooseFolderButton.click();
        await filePickerClosed;
        await TestUtils.waitForTick();

        verifyEvents([]);

        MockFilePicker.cleanup();
      });

      add_task(async function testInputFolderDisplayValue() {
        let target = await testHelpers.renderTemplate();
        let inputFolder = target.querySelector("moz-input-folder");
        let inputElement = inputFolder.inputEl;
        let chooseFolderButton = inputFolder.chooseFolderButtonEl;
        inputFolder.addEventListener("change", () => {
          inputFolder.displayValue = "My Test Folder";
        });

        // Set up the file picker.
        const mockCustomParentDir = await IOUtils.createUniqueDirectory(
          PathUtils.tempDir,
          "input-folder-custom-dir-test"
        );
        const dummyFile = Cc["@mozilla.org/file/local;1"].createInstance(
          Ci.nsIFile
        );
        dummyFile.initWithPath(mockCustomParentDir);
        MockFilePicker.init(window.browsingContext);
        MockFilePicker.showCallback = () => {
          ok(true, "Filepicker shown");
          MockFilePicker.setFiles([dummyFile]);
        };
        MockFilePicker.returnValue = MockFilePicker.returnOK;

        //Check initial values.
        is(
          inputFolder.value,
          "",
          "The initial value of the component is an empty string."
        );
        is(
          inputFolder.displayValue,
          "",
          "The initial displayValue of the component is an empty string."
        );
        is(
          inputElement.value,
          "",
          "The initial value of the input element is an empty string."
        );

        //Choose a new folder.
        let folderChanged = BrowserTestUtils.waitForEvent(
          inputFolder,
          "change"
        );
        chooseFolderButton.click();
        await folderChanged;

        //Check if the values were updated.
        is(
          inputFolder.value,
          mockCustomParentDir,
          "The value of the moz-input-folder is set as a path to the temporary folder."
        );
        is(
          inputFolder.displayValue,
          "My Test Folder",
          "The component has the expected displayValue."
        );
        is(
          inputElement.value,
          inputFolder.displayValue,
          "The value of the input element is the same as the displayValue."
        );

        MockFilePicker.cleanup();
      });
    </script>
  </head>
  <body>
    <p id="display"></p>
    <div id="content" style="display: none"></div>
    <pre id="test"></pre>
  </body>
</html>