summaryrefslogtreecommitdiffstats
path: root/dom/html/test/forms/test_input_file_picker.html
blob: f59c9e759c8763562959b6210ede13d806b5732d (plain)
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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for &lt;input type='file'&gt; file picker</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=377624">Mozilla Bug 36619</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=377624">Mozilla Bug 377624</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=565274">Mozilla Bug 565274</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=701353">Mozilla Bug 701353</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=826176">Mozilla Bug 826176</a>
<p id="display"></p>
<div id="content">
  <input id='a' type='file' accept="image/*">
  <input id='b' type='file' accept="audio/*">
  <input id='c' type='file' accept="video/*">
  <input id='d' type='file' accept="image/*, audio/* ">
  <input id='e' type='file' accept=" image/*,video/*">
  <input id='f' type='file' accept="audio/*,video/*">
  <input id='g' type='file' accept="image/*, audio/* ,video/*">
  <input id='h' type='file' accept="foo/baz,image/*,bogus/duh">
  <input id='i' type='file' accept="mime/type;parameter,video/*">
  <input id='j' type='file' accept="audio/*, audio/*, audio/*">
  <input id='k' type="file" accept="image/gif,image/png">
  <input id='l' type="file" accept="image/*,image/gif,image/png">
  <input id='m' type="file" accept="image/gif,image/gif">
  <input id='n' type="file" accept="">
  <input id='o' type="file" accept=".test">
  <input id='p' type="file" accept="image/gif,.csv">
  <input id='q' type="file" accept="image/gif,.gif">
  <input id='r' type="file" accept=".prefix,.prefixPlusSomething">
  <input id='s' type="file" accept=".xls,.xlsx">
  <input id='t' type="file" accept=".mp3,.wav,.flac">
  <input id='u' type="file" accept=".xls, .xlsx">
  <input id='v' type="file" accept=".xlsx,  .xls">
  <input id='w' type="file" accept=".xlsx;  .xls">
  <input id='x' type="file" accept=".xls,  .xlsx">
  <input id='y' type="file" accept=".xlsx, .xls">
  <input id='z' type='file' accept="i/am,a,pathological,;,,,,test/case">
  <input id='A' type="file" accept=".xlsx, .xls*">
  <input id='mix-ref' type="file" accept="image/jpeg">
  <input id='mix' type="file" accept="image/jpeg,.jpg">
  <input id='hidden' hidden type='file'>
  <input id='untrusted-click' type='file'>
  <input id='prevent-default' type='file'>
  <input id='prevent-default-false' type='file'>
  <input id='right-click' type='file'>
  <input id='middle-click' type='file'>
  <input id='left-click' type='file'>
  <label id='label-1'>foo<input type='file'></label>
  <label id='label-2' for='labeled-2'>foo</label><input id='labeled-2' type='file'></label>
  <label id='label-3'>foo<input type='file'></label>
  <label id='label-4' for='labeled-4'>foo</label><input id='labeled-4' type='file'></label>
  <input id='by-button' type='file'>
  <button id='button-click' onclick="document.getElementById('by-button').click();">foo</button>
  <button id='button-down' onclick="document.getElementById('by-button').click();">foo</button>
  <button id='button-up' onclick="document.getElementById('by-button').click();">foo</button>
  <div id='div-click' onclick="document.getElementById('by-button').click();" tabindex='1'>foo</div>
  <div id='div-click-on-demand' onclick="var i=document.createElement('input'); i.type='file'; i.click();" tabindex='1'>foo</div>
  <div id='div-keydown' onkeydown="document.getElementById('by-button').click();" tabindex='1'>foo</div>
  <a id='link-click' href="javascript:document.getElementById('by-button').click();" tabindex='1'>foo</a>
  <input id='show-picker' type='file'>
</div>
<pre id="test">
<script type="application/javascript">

/**
 * This test checks various scenarios and make sure that a file picker is being
 * shown in all of them (minus a few exceptions).
 * |testData| defines the tests to do and |launchNextTest| can be used to have
 * specific behaviour for some tests. Everything else should just work.
 */

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");

var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window);

// The following lists are from toolkit/content/filepicker.properties which is used by filePicker
var imageExtensionList = "*.jpe; *.jpg; *.jpeg; *.gif; *.png; *.bmp; *.ico; *.svg; *.svgz; *.tif; *.tiff; *.ai; *.drw; *.pct; *.psp; *.xcf; *.psd; *.raw; *.webp"

var audioExtensionList = "*.aac; *.aif; *.flac; *.iff; *.m4a; *.m4b; *.mid; *.midi; *.mp3; *.mpa; *.mpc; *.oga; *.ogg; *.opus; *.ra; *.ram; *.snd; *.wav; *.wma"

var videoExtensionList = "*.avi; *.divx; *.flv; *.m4v; *.mkv; *.mov; *.mp4; *.mpeg; *.mpg; *.ogm; *.ogv; *.ogx; *.rm; *.rmvb; *.smil; *.webm; *.wmv; *.xvid"

// [ element name | number of filters | extension list or filter mask | filter index ]
var testData = [["a", 1, MockFilePicker.filterImages, 1],
                ["b", 1, MockFilePicker.filterAudio, 1],
                ["c", 1, MockFilePicker.filterVideo, 1],
                ["d", 3, imageExtensionList + "; " + audioExtensionList, 1],
                ["e", 3, imageExtensionList + "; " + videoExtensionList, 1],
                ["f", 3, audioExtensionList + "; " + videoExtensionList, 1],
                ["g", 4, imageExtensionList + "; " + audioExtensionList + "; " + videoExtensionList, 1],
                ["h", 1, MockFilePicker.filterImages, 1],
                ["i", 1, MockFilePicker.filterVideo, 1],
                ["j", 1, MockFilePicker.filterAudio, 1],
                ["k", 3, "*.gif; *.png", 1],
                ["l", 4, imageExtensionList + "; " + "*.gif; *.png", 1],
                ["m", 1, "*.gif", 1],
                ["n", 0, undefined, 0],
                ["o", 1, "*.test", 1],
                ["p", 3, "*.gif; *.csv", 1],
                ["q", 1, "*.gif", 1],
                ["r", 3, "*.prefix; *.prefixPlusSomething", 1],
                ["s", 3, "*.xls; *.xlsx", 1],
                ["t", 4, "*.mp3; *.wav; *.flac", 1],
                ["u", 3, "*.xls; *.xlsx", 1],
                ["v", 3, "*.xlsx; *.xls", 1],
                ["w", 0, undefined, 0],
                ["x", 3, "*.xls; *.xlsx", 1],
                ["y", 3, "*.xlsx; *.xls", 1],
                ["z", 0, undefined, 0],
                ["A", 1, "*.xlsx", 1],
                // Note: mix and mix-ref tests extension lists are checked differently: see SimpleTest.executeSoon below
                ["mix-ref", undefined, undefined, undefined],
                ["mix", 1, undefined, 1],
                ["hidden", 0, undefined, 0],
                ["untrusted-click", 0, undefined, 0],
                ["prevent-default", 0, undefined, 0, true],
                ["prevent-default-false", 0, undefined, 0, true],
                ["right-click", 0, undefined, 0, true],
                ["middle-click", 0, undefined, 0, true],
                ["left-click", 0, undefined, 0],
                ["label-1", 0, undefined, 0],
                ["label-2", 0, undefined, 0],
                ["label-3", 0, undefined, 0],
                ["label-4", 0, undefined, 0],
                ["button-click", 0, undefined, 0],
                ["button-down", 0, undefined, 0],
                ["button-up", 0, undefined, 0],
                ["div-click", 0, undefined, 0],
                ["div-click-on-demand", 0, undefined, 0],
                ["div-keydown", 0, undefined, 0],
                ["link-click", 0, undefined, 0],
                ["show-picker", 0, undefined, 0],
               ];

var currentTest = 0;
var filterAllAdded;
var filters;
var filterIndex;
var mixRefExtensionList;

// Make sure picker works with popup blocker enabled and no allowed events
SpecialPowers.pushPrefEnv({'set': [["dom.popup_allowed_events", ""]]}, runTests);

function launchNextTest() {
  MockFilePicker.shown = false;
  filterAllAdded = false;
  filters = [];
  filterIndex = 0;

  // Focusing the element will scroll them into view so making sure the clicks
  // will work.
  document.getElementById(testData[currentTest][0]).focus();

  if (testData[currentTest][0] == "untrusted-click") {
    var e = document.createEvent('MouseEvents');
    e.initEvent('click', true, false);
    document.getElementById(testData[currentTest][0]).dispatchEvent(e);
  // All tests that should *NOT* show a file picker.
  } else if (testData[currentTest][0] == "prevent-default" ||
             testData[currentTest][0] == "prevent-default-false" ||
             testData[currentTest][0] == "right-click" ||
             testData[currentTest][0] == "middle-click") {
    if (testData[currentTest][0] == "right-click" ||
        testData[currentTest][0] == "middle-click") {
      var b = testData[currentTest][0] == "middle-click" ? 1 : 2;
      synthesizeMouseAtCenter(document.getElementById(testData[currentTest][0]),
                              { button: b });
    } else {
      if (testData[currentTest][0] == "prevent-default-false") {
        document.getElementById(testData[currentTest][0]).onclick = function() {
          return false;
        };
      } else {
        document.getElementById(testData[currentTest][0]).onclick = function(event) {
          event.preventDefault();
        };
      }
      document.getElementById(testData[currentTest][0]).click();
    }

    // Wait a bit and assume we can continue. If the file picker shows later,
    // behaviour is uncertain but that would be a random green, no big deal...
    setTimeout(function() {
      ok(true, "we should be there without a file picker being opened");
      ++currentTest;
      launchNextTest();
    }, 500);
  } else if (testData[currentTest][0] == 'label-3' ||
             testData[currentTest][0] == 'label-4') {
    synthesizeMouse(document.getElementById(testData[currentTest][0]), 5, 5, {});
  } else if (testData[currentTest][0] == 'button-click' ||
             testData[currentTest][0] == 'button-down' ||
             testData[currentTest][0] == 'button-up' ||
             testData[currentTest][0] == 'div-click' ||
             testData[currentTest][0] == 'div-click-on-demand' ||
             testData[currentTest][0] == 'link-click') {
    synthesizeMouseAtCenter(document.getElementById(testData[currentTest][0]), {});
  } else if (testData[currentTest][0] == 'div-keydown') {
    sendString("a");
  } else if (testData[currentTest][0] == 'show-picker') {
    SpecialPowers.wrap(document).notifyUserGestureActivation();
    document.getElementById(testData[currentTest][0]).showPicker();
  } else {
    document.getElementById(testData[currentTest][0]).click();
  }
}

function runTests() {
  MockFilePicker.appendFilterCallback = function(filepicker, title, val) {
    filters.push(val);
  };
  MockFilePicker.appendFiltersCallback = function(filepicker, val) {
    if (val === MockFilePicker.filterAll) {
      filterAllAdded = true;
    } else {
      filters.push(val);
    }
  };
  MockFilePicker.showCallback = function(filepicker) {
    if (testData[currentTest][4]) {
      ok(false, "we shouldn't have a file picker showing!");
      return;
    }

    filterIndex = filepicker.filterIndex;
    testName = testData[currentTest][0];
    SimpleTest.executeSoon(function () {
      ok(MockFilePicker.shown,
         "File picker show method should have been called (" + testName + ")");
      ok(filterAllAdded,
         "filterAll is missing (" + testName + ")");
      if (testName == "mix-ref") {
        // Used only for reference for next test: nothing to be tested here
        mixRefExtensionList = filters[0];
        if (mixRefExtensionList == undefined) {
          mixRefExtensionList = "";
        }
      } else {
        if (testName == "mix") {
          // Mixing mime type and file extension filters ("image/jpeg" and
          // ".jpg" here) shouldn't restrict the list but only extend it, if file
          // extension filter isn't a duplicate
          ok(filters[0].includes(mixRefExtensionList),
             "Mixing mime types and file extension filters shouldn't restrict extension list: " +
             mixRefExtensionList + " | " + filters[0]);
          ok(filters[0].includes("*.jpg"),
             "Filter should contain '.jpg' extension. Filter was:" + filters[0]);
        } else {
          is(filters[0], testData[currentTest][2],
             "Correct filters should have been added (" + testName + ")");
          is(filters.length, testData[currentTest][1],
             "appendFilters not called as often as expected (" + testName + ")");
        }
        is(filterIndex, testData[currentTest][3],
           "File picker should show the correct filter index (" + testName + ")");
      }

      if (++currentTest == testData.length) {
        MockFilePicker.cleanup();
        SimpleTest.finish();
      } else {
        launchNextTest();
      }
    });
  };

  launchNextTest();
}

</script>
</pre>
</body>
</html>