summaryrefslogtreecommitdiffstats
path: root/dom/events/test/test_all_synthetic_events.html
blob: 9b3827e01062a53e0e3029efc54f6aad43edaf9e (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
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
<!DOCTYPE html>
<html>
<head>
  <title>Test all synthetic events</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>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">

/**
 * kEventConstructors is a helper and database of all events.
 * The sort order of the definition is by A to Z (ignore the Event postfix).
 *
 * XXX: should we move this into EventUtils.js?
 *
 * create: function or null.  If this is null, it's impossible to create untrusted event for it.
 *         Otherwise, create(aName, aProps) returns an instance of the event initialized with aProps.
 *         aName specifies the event's type name.  See each create() code for the detail of aProps.
 */
const kEventConstructors = {
  Event:                                     { create (aName, aProps) {
                                                         return new Event(aName, aProps);
                                                       },
                                             },
  AnimationEvent:                            { create (aName, aProps) {
                                                         return new AnimationEvent(aName, aProps);
                                                       },
                                             },
  AnimationPlaybackEvent:                    { create (aName, aProps) {
                                                         return new AnimationPlaybackEvent(aName, aProps);
                                                       },
                                             },
  AudioProcessingEvent:                      { create: null, // Cannot create untrusted event from JS.
                                             },
  BeforeUnloadEvent:                         { create (aName, aProps) {
                                                         var e = document.createEvent("beforeunloadevent");
                                                         e.initEvent(aName, aProps.bubbles, aProps.cancelable);
                                                         return e;
                                                       },
                                             },
  BlobEvent:                                 { create (aName, aProps) {
                                                         return new BlobEvent(aName, {
                                                           data: new Blob([]),
                                                         });
                                                       },
                                             },
  CallEvent:                                 { create (aName, aProps) {
                                                          return new CallEvent(aName, aProps);
                                                       },
                                             },
  CallGroupErrorEvent:                       { create (aName, aProps) {
                                                          return new CallGroupErrorEvent(aName, aProps);
                                                       },
                                             },
  CFStateChangeEvent:                        { create (aName, aProps) {
                                                          return new CFStateChangeEvent(aName, aProps);
                                                       },
                                             },
  CloseEvent:                                { create (aName, aProps) {
                                                         return new CloseEvent(aName, aProps);
                                                       },
                                             },
  ClipboardEvent:                            { create (aName, aProps) {
                                                         return new ClipboardEvent(aName, aProps);
                                                       },
                                             },
  CompositionEvent:                          { create (aName, aProps) {
                                                         var e = document.createEvent("compositionevent");
                                                         e.initCompositionEvent(aName, aProps.bubbles, aProps.cancelable,
                                                                                aProps.view, aProps.data, aProps.locale);
                                                         return e;
                                                       },
                                             },
  CustomEvent:                               { create (aName, aProps) {
                                                         return new CustomEvent(aName, aProps);
                                                       },
                                             },
  DataErrorEvent:                            { create (aName, aProps) {
                                                          return new DataErrorEvent(aName, aProps);
                                                       },
                                             },
  DeviceLightEvent:                          { create (aName, aProps) {
                                                         return new DeviceLightEvent(aName, aProps);
                                                       },
                                             },
  DeviceMotionEvent:                         { create (aName, aProps) {
                                                         var e = document.createEvent("devicemotionevent");
                                                         e.initDeviceMotionEvent(aName, aProps.bubbles, aProps.cancelable, aProps.acceleration,
                                                                                 aProps.accelerationIncludingGravity, aProps.rotationRate,
                                                                                 aProps.interval || 0.0);
                                                         return e;
                                                       },
                                             },
  DeviceOrientationEvent:                    { create (aName, aProps) {
                                                         return new DeviceOrientationEvent(aName, aProps);
                                                       },
                                             },
  DeviceProximityEvent:                      { create (aName, aProps) {
                                                         return new DeviceProximityEvent(aName, aProps);
                                                       },
                                             },
  DownloadEvent:                             { create (aName, aProps) {
                                                         return new DownloadEvent(aName, aProps);
                                                       },
                                             },
  DragEvent:                                 { create (aName, aProps) {
                                                         var e = document.createEvent("dragevent");
                                                         e.initDragEvent(aName, aProps.bubbles, aProps.cancelable,
                                                                         aProps.view, aProps.detail,
                                                                         aProps.screenX, aProps.screenY,
                                                                         aProps.clientX, aProps.clientY,
                                                                         aProps.ctrlKey, aProps.altKey, aProps.shiftKey, aProps.metaKey,
                                                                         aProps.button, aProps.relatedTarget, aProps.dataTransfer);
                                                         return e;
                                                       },
                                             },
  ErrorEvent:                                { create (aName, aProps) {
                                                         return new ErrorEvent(aName, aProps);
                                                       },
  },
  FocusEvent:                                { create (aName, aProps) {
                                                         return new FocusEvent(aName, aProps);
                                                       },
                                             },
  FontFaceSetLoadEvent:                      { create (aName, aProps) {
                                                         return new FontFaceSetLoadEvent(aName, aProps);
                                                       },
                                             },
  FormDataEvent:                             { create (aName, aProps) {
                                                        return new FormDataEvent(aName, {
                                                            formData: new FormData()
                                                        });
                                                      },
                                             },
  GamepadEvent:                              { create (aName, aProps) {
                                                         return new GamepadEvent(aName, aProps);
                                                       },
                                             },
  GamepadAxisMoveEvent:                      { create (aName, aProps) {
                                                         return new GamepadAxisMoveEvent(aName, aProps);
                                                       },
                                             },
  GamepadButtonEvent:                        { create (aName, aProps) {
                                                         return new GamepadButtonEvent(aName, aProps);
                                                       },
                                             },
  GPUUncapturedErrorEvent:                   { create: null, //TODO: constructor test
                                             },
  HashChangeEvent:                           { create (aName, aProps) {
                                                         return new HashChangeEvent(aName, aProps);
                                                       },
                                             },
  IDBVersionChangeEvent:                     { create (aName, aProps) {
                                                         return new IDBVersionChangeEvent(aName, aProps);
                                                       },
                                             },
  ImageCaptureErrorEvent:                                { create (aName, aProps) {
                                                         return new ImageCaptureErrorEvent(aName, aProps);
                                                       },
                                             },
  InputEvent:                                { create (aName, aProps) {
                                                         return new InputEvent(aName, aProps);
                                                       },
                                             },
  KeyEvent:                                  { create (aName, aProps) {
                                                         return new KeyboardEvent(aName, aProps);
                                                       },
                                             },
  KeyboardEvent:                             { create (aName, aProps) {
                                                         return new KeyboardEvent(aName, aProps);
                                                       },
                                             },
  MediaEncryptedEvent:                       { create (aName, aProps) {
                                                         return new MediaEncryptedEvent(aName, aProps);
                                                       },
                                             },
  MediaKeyMessageEvent:                      { create (aName, aProps) {
                                                         return new MediaKeyMessageEvent(aName, {
                                                           messageType: "license-request",
                                                           message: new ArrayBuffer(0)
                                                         });
                                                       },
                                             },
  MediaQueryListEvent:                       { create (aName, aProps) {
                                                         return new MediaQueryListEvent(aName, aProps);
                                                       },
                                             },
  MediaRecorderErrorEvent:                   { create (aName, aProps) {
                                                         aProps.error = new DOMException();
                                                         return new MediaRecorderErrorEvent(aName, aProps);
                                                       },
                                             },
  MediaStreamEvent:                          { create (aName, aProps) {
                                                         return new MediaStreamEvent(aName, aProps);
                                                       },
                                             },
  MediaStreamTrackEvent:                     {
                                               // Difficult to test required arguments.
                                             },
  MessageEvent:                              { create (aName, aProps) {
                                                         var e = new MessageEvent("messageevent", { bubbles: aProps.bubbles,
                                                             cancelable: aProps.cancelable, data: aProps.data, origin: aProps.origin,
                                                             lastEventId: aProps.lastEventId, source: aProps.source });
                                                         return e;
                                                       },
                                             },
  MouseEvent:                                { create (aName, aProps) {
                                                         return new MouseEvent(aName, aProps);
                                                       },
                                             },
  MouseScrollEvent:                          { create: null
                                               // Cannot create untrusted event from JS
                                             },
  MozApplicationEvent:                       { create (aName, aProps) {
                                                         return new MozApplicationEvent(aName, aProps);
                                                       },
                                             },
  MozClirModeEvent:                          { create (aName, aProps) {
                                                         return new MozClirModeEvent(aName, aProps);
                                                       },
                                             },
  MozContactChangeEvent:                     { create (aName, aProps) {
                                                         return new MozContactChangeEvent(aName, aProps);
                                                       },
                                             },
  MozEmergencyCbModeEvent:                   { create (aName, aProps) {
                                                          return new MozEmergencyCbModeEvent(aName, aProps);
                                                       },
                                             },
  MozMessageDeletedEvent:                    { create (aName, aProps) {
                                                         return new MozMessageDeletedEvent(aName, aProps);
                                                       },
                                             },
  MozMmsEvent:                               { create (aName, aProps) {
                                                         return new MozMmsEvent(aName, aProps);
                                                       },
                                             },
  MozOtaStatusEvent:                         { create (aName, aProps) {
                                                          return new MozOtaStatusEvent(aName, aProps);
                                                       },
                                             },
  MozSmsEvent:                               { create (aName, aProps) {
                                                         return new MozSmsEvent(aName, aProps);
                                                       },
                                             },
  MozStkCommandEvent:                        { create (aName, aProps) {
                                                          return new MozStkCommandEvent(aName, aProps);
                                                       },
                                             },
  MutationEvent:                             { create (aName, aProps) {
                                                         var e = document.createEvent("mutationevent");
                                                         e.initMutationEvent(aName, aProps.bubbles, aProps.cancelable,
                                                                             aProps.relatedNode, aProps.prevValue, aProps.newValue,
                                                                             aProps.attrName, aProps.attrChange);
                                                         return e;
                                                       },
                                             },
  OfflineAudioCompletionEvent:               { create: "AudioContext" in self
                                                        ? function (aName, aProps) {
                                                            var ac = new AudioContext();
                                                            var ab = new AudioBuffer({ length: 42, sampleRate: ac.sampleRate });
                                                            aProps.renderedBuffer = ab;
                                                            return new OfflineAudioCompletionEvent(aName, aProps);
                                                          }
                                                        : null,
                                             },
  PageTransitionEvent:                       { create (aName, aProps) {
                                                         return new PageTransitionEvent(aName, aProps);
                                                       },
                                             },
  PointerEvent:                              { create (aName, aProps) {
                                                         return new PointerEvent(aName, aProps);
                                                       },
                                             },
  PopStateEvent:                             { create (aName, aProps) {
                                                         return new PopStateEvent(aName, aProps);
                                                       },
                                             },
  PopupBlockedEvent:                         { create (aName, aProps) {
                                                         return new PopupBlockedEvent(aName, aProps);
                                                       },
                                             },
  ProgressEvent:                             { create (aName, aProps) {
                                                         return new ProgressEvent(aName, aProps);
                                                       },
                                             },
  PromiseRejectionEvent:                     { create (aName, aProps) {
                                                         aProps.promise = new Promise(() => {});
                                                         return new PromiseRejectionEvent(aName, aProps);
                                                       },
                                             },
  RTCDataChannelEvent:                       { create (aName, aProps) {
                                                         let pc = new RTCPeerConnection();
                                                         aProps.channel = pc.createDataChannel("foo");
                                                         let e = new RTCDataChannelEvent(aName, aProps);
                                                         aProps.channel.close();
                                                         pc.close();
                                                         return e;
                                                       },
                                             },
  RTCDTMFToneChangeEvent:                       { create (aName, aProps) {
                                                         return new RTCDTMFToneChangeEvent(aName, aProps);
                                                       },
                                             },
  RTCPeerConnectionIceEvent:                 { create (aName, aProps) {
                                                         return new RTCPeerConnectionIceEvent(aName, aProps);
                                                       },
                                             },
  RTCTrackEvent:                             {
                                               // Difficult to test required arguments.
                                             },
  ScrollAreaEvent:                           { create: null
                                               // Cannot create untrusted event from JS
                                             },
  SpeechRecognitionError:                    { create (aName, aProps) {
                                                         return new SpeechRecognitionError(aName, aProps);
                                                       },
                                             },
  SpeechRecognitionEvent:                    { create (aName, aProps) {
                                                         return new SpeechRecognitionEvent(aName, aProps);
                                                       },
                                             },
  SpeechSynthesisErrorEvent:                 { create (aName, aProps) {
                                                         aProps.error = "synthesis-unavailable";
                                                         aProps.utterance = new SpeechSynthesisUtterance("Hello World");
                                                         return new SpeechSynthesisErrorEvent(aName, aProps);
                                                       },
                                             },
  SpeechSynthesisEvent:                      { create (aName, aProps) {
                                                         aProps.utterance = new SpeechSynthesisUtterance("Hello World");
                                                         return new SpeechSynthesisEvent(aName, aProps);
                                                       },
                                             },
  StorageEvent:                              { create (aName, aProps) {
                                                         return new StorageEvent(aName, aProps);
                                                       },
                                             },
  StyleSheetApplicableStateChangeEvent:      { create (aName, aProps) {
                                                         return new StyleSheetApplicableStateChangeEvent(aName, aProps);
                                                       },
                                               chromeOnly: true,
                                             },
  SubmitEvent:                               { create (aName, aProps) {
                                                         return new SubmitEvent(aName, aProps);
                                                       },
                                             },
  TaskPriorityChangeEvent:                   { create (aName, aProps) {
                                                         aProps.previousPriority = "user-blocking";
                                                         return new TaskPriorityChangeEvent(aName, aProps);
                                                       },
                                             },
  TCPSocketErrorEvent:                       { create(aName, aProps) {
                                                         return new TCPSocketErrorEvent(aName, aProps);
                                                       },
                                             },
  TCPSocketEvent:                            { create(aName, aProps) {
                                                         return new TCPSocketEvent(aName, aProps);
                                                       },
                                             },
  TCPServerSocketEvent:                      { create(aName, aProps) {
                                                         return new TCPServerSocketEvent(aName, aProps);
                                                       },
                                             },
  TimeEvent:                                 { create: null
                                               // Cannot create untrusted event from JS
                                             },
  TouchEvent:                                { create (aName, aProps) {
                                                         var e = document.createEvent("touchevent");
                                                         e.initTouchEvent(aName, aProps.bubbles, aProps.cancelable,
                                                                          aProps.view, aProps.detail,
                                                                          aProps.ctrlKey, aProps.altKey, aProps.shiftKey, aProps.metaKey,
                                                                          aProps.touches, aProps.targetTouches, aProps.changedTouches);
                                                         return e;
                                                       },
                                             },
  TrackEvent:                                { create (aName, aProps) {
                                                         return new TrackEvent(aName, aProps);
                                                       },
                                             },
  TransitionEvent:                           { create (aName, aProps) {
                                                         return new TransitionEvent(aName, aProps);
                                                       },
                                             },
  UIEvent:                                   { create (aName, aProps) {
                                                         return new UIEvent(aName, aProps);
                                                       },
                                             },
  UserProximityEvent:                        { create (aName, aProps) {
                                                         return new UserProximityEvent(aName, aProps);
                                                       },
                                             },
  USSDReceivedEvent:                         { create (aName, aProps) {
                                                          return new USSDReceivedEvent(aName, aProps);
                                                       },
                                             },
  VRDisplayEvent:                            { create: null,
                                               // Required argument expects a VRDisplay that can not
                                               // be created from Javascript without physical VR hardware
                                               // connected.  When Bug 1229480 lands, this test can be
                                               // updated to use the puppet VR device.
                                             },
  WheelEvent:                                { create (aName, aProps) {
                                                         return new WheelEvent(aName, aProps);
                                                       },
                                             },
  WebGLContextEvent:                         { create (aName, aProps) {
                                                         return new WebGLContextEvent(aName, aProps);
                                                       },
                                             },
  SecurityPolicyViolationEvent:              { create (aName, aProps) {
                                                         return new SecurityPolicyViolationEvent(aName, aProps);
                                                       },
                                             },
};

function test() {
  for (var name of Object.keys(kEventConstructors)) {
    if (!kEventConstructors[name].chromeOnly) {
      continue;
    }
    if (window[name]) {
      ok(false, name + " should be chrome only.");
    }
    window[name] = SpecialPowers.unwrap(SpecialPowers.wrap(window)[name]);
  }

  var props = Object.getOwnPropertyNames(window);
  for (var i = 0; i < props.length; i++) {
    // Assume that event object must be named as "FooBarEvent".
    if (!props[i].match(/^([A-Z][a-zA-Z]+)?Event$/)) {
      continue;
    }
    if (!kEventConstructors[props[i]]) {
      ok(false, "Unknown event found: " + props[i]);
      continue;
    }
    if (!kEventConstructors[props[i]].create) {
      todo(false, "Cannot create untrusted event of " + props[i]);
      continue;
    }
    ok(true, "Creating " + props[i] + "...");
    var event = kEventConstructors[props[i]].create("foo", {});
    if (!event) {
      ok(false, "Failed to create untrusted event: " + props[i]);
      continue;
    }
    if (typeof(event.getModifierState) == "function") {
      const kModifiers = [ "Shift", "Control", "Alt", "AltGr", "Meta", "CapsLock", "ScrollLock", "NumLock", "OS", "Fn", "FnLock", "Symbol", "SymbolLock" ];
      for (var j = 0; j < kModifiers.length; j++) {
        ok(true, "Calling " + props[i] + ".getModifierState(" + kModifiers[j] + ")...");
        var modifierState = event.getModifierState(kModifiers[j]);
        ok(true, props[i] + ".getModifierState(" + kModifiers[j] + ") = " + modifierState);
      }
    }
  }
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv(
  {"set": [["dom.w3c_touch_events.legacy_apis.enabled", true]]},
  function() {
    test();
    SimpleTest.finish();
  });
</script>
</pre>
</body>
</html>