test_all_synthetic_events.html 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Test all synthetic events</title>
  5. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  6. <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  7. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  8. </head>
  9. <body>
  10. <p id="display"></p>
  11. <div id="content" style="display: none">
  12. </div>
  13. <pre id="test">
  14. <script type="application/javascript">
  15. /**
  16. * kEventConstructors is a helper and database of all events.
  17. * The sort order of the definition is by A to Z (ignore the Event postfix).
  18. *
  19. * XXX: should we move this into EventUtils.js?
  20. *
  21. * create: function or null. If this is null, it's impossible to create untrusted event for it.
  22. * Otherwise, create(aName, aProps) returns an instance of the event initialized with aProps.
  23. * aName specifies the event's type name. See each create() code for the detail of aProps.
  24. */
  25. const kEventConstructors = {
  26. Event: { create: function (aName, aProps) {
  27. return new Event(aName, aProps);
  28. },
  29. },
  30. AnimationEvent: { create: function (aName, aProps) {
  31. return new AnimationEvent(aName, aProps);
  32. },
  33. },
  34. AnimationPlaybackEvent: { create: function (aName, aProps) {
  35. return new AnimationPlaybackEvent(aName, aProps);
  36. },
  37. },
  38. AudioProcessingEvent: { create: null, // Cannot create untrusted event from JS.
  39. },
  40. BeforeAfterKeyboardEvent: { create: function (aName, aProps) {
  41. return new BeforeAfterKeyboardEvent(aName, aProps);
  42. },
  43. },
  44. BeforeUnloadEvent: { create: function (aName, aProps) {
  45. var e = document.createEvent("beforeunloadevent");
  46. e.initEvent(aName, aProps.bubbles, aProps.cancelable);
  47. return e;
  48. },
  49. },
  50. BlobEvent: { create: function (aName, aProps) {
  51. return new BlobEvent(aName, aProps);
  52. },
  53. },
  54. CallEvent: { create: function (aName, aProps) {
  55. return new CallEvent(aName, aProps);
  56. },
  57. },
  58. CallGroupErrorEvent: { create: function (aName, aProps) {
  59. return new CallGroupErrorEvent(aName, aProps);
  60. },
  61. },
  62. CFStateChangeEvent: { create: function (aName, aProps) {
  63. return new CFStateChangeEvent(aName, aProps);
  64. },
  65. },
  66. CloseEvent: { create: function (aName, aProps) {
  67. return new CloseEvent(aName, aProps);
  68. },
  69. },
  70. ClipboardEvent: { create: function (aName, aProps) {
  71. return new ClipboardEvent(aName, aProps);
  72. },
  73. },
  74. CommandEvent: { create: function (aName, aProps) {
  75. var e = document.createEvent("commandevent");
  76. e.initCommandEvent(aName, aProps.bubbles, aProps.cancelable,
  77. aProps.command);
  78. return e;
  79. },
  80. },
  81. CompositionEvent: { create: function (aName, aProps) {
  82. var e = document.createEvent("compositionevent");
  83. e.initCompositionEvent(aName, aProps.bubbles, aProps.cancelable,
  84. aProps.view, aProps.data, aProps.locale);
  85. return e;
  86. },
  87. },
  88. CustomEvent: { create: function (aName, aProps) {
  89. return new CustomEvent(aName, aProps);
  90. },
  91. },
  92. DataErrorEvent: { create: function (aName, aProps) {
  93. return new DataErrorEvent(aName, aProps);
  94. },
  95. },
  96. DataContainerEvent: { create: function (aName, aProps) {
  97. var e = document.createEvent("datacontainerevent");
  98. e.initEvent(aName, aProps.bubbles, aProps.cancelable);
  99. return e;
  100. },
  101. },
  102. DeviceLightEvent: { create: function (aName, aProps) {
  103. return new DeviceLightEvent(aName, aProps);
  104. },
  105. },
  106. DeviceMotionEvent: { create: function (aName, aProps) {
  107. var e = document.createEvent("devicemotionevent");
  108. e.initDeviceMotionEvent(aName, aProps.bubbles, aProps.cancelable, aProps.acceleration,
  109. aProps.accelerationIncludingGravity, aProps.rotationRate,
  110. aProps.interval || 0.0);
  111. return e;
  112. },
  113. },
  114. DeviceOrientationEvent: { create: function (aName, aProps) {
  115. return new DeviceOrientationEvent(aName, aProps);
  116. },
  117. },
  118. DeviceProximityEvent: { create: function (aName, aProps) {
  119. return new DeviceProximityEvent(aName, aProps);
  120. },
  121. },
  122. DownloadEvent: { create: function (aName, aProps) {
  123. return new DownloadEvent(aName, aProps);
  124. },
  125. },
  126. DragEvent: { create: function (aName, aProps) {
  127. var e = document.createEvent("dragevent");
  128. e.initDragEvent(aName, aProps.bubbles, aProps.cancelable,
  129. aProps.view, aProps.detail,
  130. aProps.screenX, aProps.screenY,
  131. aProps.clientX, aProps.clientY,
  132. aProps.ctrlKey, aProps.altKey, aProps.shiftKey, aProps.metaKey,
  133. aProps.button, aProps.relatedTarget, aProps.dataTransfer);
  134. return e;
  135. },
  136. },
  137. ErrorEvent: { create: function (aName, aProps) {
  138. return new ErrorEvent(aName, aProps);
  139. },
  140. },
  141. FocusEvent: { create: function (aName, aProps) {
  142. return new FocusEvent(aName, aProps);
  143. },
  144. },
  145. FontFaceSetLoadEvent: { create: function (aName, aProps) {
  146. return new FontFaceSetLoadEvent(aName, aProps);
  147. },
  148. },
  149. GamepadEvent: { create: function (aName, aProps) {
  150. return new GamepadEvent(aName, aProps);
  151. },
  152. },
  153. GamepadAxisMoveEvent: { create: function (aName, aProps) {
  154. return new GamepadAxisMoveEvent(aName, aProps);
  155. },
  156. },
  157. GamepadButtonEvent: { create: function (aName, aProps) {
  158. return new GamepadButtonEvent(aName, aProps);
  159. },
  160. },
  161. HashChangeEvent: { create: function (aName, aProps) {
  162. return new HashChangeEvent(aName, aProps);
  163. },
  164. },
  165. IDBVersionChangeEvent: { create: function (aName, aProps) {
  166. return new IDBVersionChangeEvent(aName, aProps);
  167. },
  168. },
  169. ImageCaptureErrorEvent: { create: function (aName, aProps) {
  170. return new ImageCaptureErrorEvent(aName, aProps);
  171. },
  172. },
  173. InputEvent: { create: function (aName, aProps) {
  174. return new InputEvent(aName, aProps);
  175. },
  176. },
  177. KeyEvent: { create: function (aName, aProps) {
  178. return new KeyboardEvent(aName, aProps);
  179. },
  180. },
  181. KeyboardEvent: { create: function (aName, aProps) {
  182. return new KeyboardEvent(aName, aProps);
  183. },
  184. },
  185. MediaEncryptedEvent: { create: function (aName, aProps) {
  186. return new MediaEncryptedEvent(aName, aProps);
  187. },
  188. },
  189. MediaKeyMessageEvent: { create: function (aName, aProps) {
  190. return new MediaKeyMessageEvent(aName, {
  191. messageType: "license-request",
  192. message: new ArrayBuffer(0)
  193. });
  194. },
  195. },
  196. MediaStreamEvent: { create: function (aName, aProps) {
  197. return new MediaStreamEvent(aName, aProps);
  198. },
  199. },
  200. MediaStreamTrackEvent: {
  201. // Difficult to test required arguments.
  202. },
  203. MessageEvent: { create: function (aName, aProps) {
  204. var e = new MessageEvent("messageevent", { bubbles: aProps.bubbles,
  205. cancelable: aProps.cancelable, data: aProps.data, origin: aProps.origin,
  206. lastEventId: aProps.lastEventId, source: aProps.source });
  207. return e;
  208. },
  209. },
  210. MouseEvent: { create: function (aName, aProps) {
  211. return new MouseEvent(aName, aProps);
  212. },
  213. },
  214. MouseScrollEvent: { create: function (aName, aProps) {
  215. var e = document.createEvent("mousescrollevents");
  216. e.initMouseScrollEvent(aName, aProps.bubbles, aProps.cancelable,
  217. aProps.view, aProps.detail,
  218. aProps.screenX, aProps.screenY,
  219. aProps.clientX, aProps.clientY,
  220. aProps.ctrlKey, aProps.altKey, aProps.shiftKey, aProps.metaKey,
  221. aProps.button, aProps.relatedTarget, aProps.axis);
  222. return e;
  223. },
  224. },
  225. MozApplicationEvent: { create: function (aName, aProps) {
  226. return new MozApplicationEvent(aName, aProps);
  227. },
  228. },
  229. MozClirModeEvent: { create: function (aName, aProps) {
  230. return new MozClirModeEvent(aName, aProps);
  231. },
  232. },
  233. MozContactChangeEvent: { create: function (aName, aProps) {
  234. return new MozContactChangeEvent(aName, aProps);
  235. },
  236. },
  237. MozEmergencyCbModeEvent: { create: function (aName, aProps) {
  238. return new MozEmergencyCbModeEvent(aName, aProps);
  239. },
  240. },
  241. MozMessageDeletedEvent: { create: function (aName, aProps) {
  242. return new MozMessageDeletedEvent(aName, aProps);
  243. },
  244. },
  245. MozMmsEvent: { create: function (aName, aProps) {
  246. return new MozMmsEvent(aName, aProps);
  247. },
  248. },
  249. MozOtaStatusEvent: { create: function (aName, aProps) {
  250. return new MozOtaStatusEvent(aName, aProps);
  251. },
  252. },
  253. MozSettingsEvent: { create: function (aName, aProps) {
  254. return new MozSettingsEvent(aName, aProps);
  255. },
  256. },
  257. MozSettingsTransactionEvent: { create: function (aName, aProps) {
  258. return new MozSettingsTransactionEvent(aName, aProps);
  259. },
  260. },
  261. MozSmsEvent: { create: function (aName, aProps) {
  262. return new MozSmsEvent(aName, aProps);
  263. },
  264. },
  265. MozStkCommandEvent: { create: function (aName, aProps) {
  266. return new MozStkCommandEvent(aName, aProps);
  267. },
  268. },
  269. MozWifiConnectionInfoEvent: { create: function (aName, aProps) {
  270. return new MozWifiConnectionInfoEvent(aName, aProps);
  271. },
  272. },
  273. MozWifiStatusChangeEvent: { create: function (aName, aProps) {
  274. return new MozWifiStatusChangeEvent(aName, aProps);
  275. },
  276. },
  277. MozWifiStationInfoEvent: { create: function (aName, aProps) {
  278. return new MozWifiStationInfoEvent(aName, aProps);
  279. },
  280. },
  281. MutationEvent: { create: function (aName, aProps) {
  282. var e = document.createEvent("mutationevent");
  283. e.initMutationEvent(aName, aProps.bubbles, aProps.cancelable,
  284. aProps.relatedNode, aProps.prevValue, aProps.newValue,
  285. aProps.attrName, aProps.attrChange);
  286. return e;
  287. },
  288. },
  289. NotifyPaintEvent: { create: function (aName, aProps) {
  290. var e = document.createEvent("notifypaintevent");
  291. e.initEvent(aName, aProps.bubbles, aProps.cancelable);
  292. return e;
  293. },
  294. },
  295. OfflineAudioCompletionEvent: { create: null, // Cannot create untrusted event from JS.
  296. },
  297. PageTransitionEvent: { create: function (aName, aProps) {
  298. return new PageTransitionEvent(aName, aProps);
  299. },
  300. },
  301. PointerEvent: { create: function (aName, aProps) {
  302. return new PointerEvent(aName, aProps);
  303. },
  304. },
  305. PopStateEvent: { create: function (aName, aProps) {
  306. return new PopStateEvent(aName, aProps);
  307. },
  308. },
  309. PopupBlockedEvent: { create: function (aName, aProps) {
  310. return new PopupBlockedEvent(aName, aProps);
  311. },
  312. },
  313. ProgressEvent: { create: function (aName, aProps) {
  314. return new ProgressEvent(aName, aProps);
  315. },
  316. },
  317. RecordErrorEvent: { create: function (aName, aProps) {
  318. return new RecordErrorEvent(aName, aProps);
  319. },
  320. },
  321. RTCDataChannelEvent: { create: function (aName, aProps) {
  322. return new RTCDataChannelEvent(aName, aProps);
  323. },
  324. },
  325. RTCDTMFToneChangeEvent: { create: function (aName, aProps) {
  326. return new RTCDTMFToneChangeEvent(aName, aProps);
  327. },
  328. },
  329. RTCPeerConnectionIceEvent: { create: function (aName, aProps) {
  330. return new RTCPeerConnectionIceEvent(aName, aProps);
  331. },
  332. },
  333. RTCTrackEvent: {
  334. // Difficult to test required arguments.
  335. },
  336. ScrollAreaEvent: { create: function (aName, aProps) {
  337. var e = document.createEvent("scrollareaevent");
  338. e.initScrollAreaEvent(aName, aProps.bubbles, aProps.cancelable,
  339. aProps.view, aProps.details,
  340. aProps.x || 0.0, aProps.y || 0.0,
  341. aProps.width || 0.0, aProps.height || 0.0);
  342. return e;
  343. },
  344. },
  345. ServiceWorkerMessageEvent: { create: function (aName, aProps) {
  346. var e = new ServiceWorkerMessageEvent("serviceworkermessageevent", { bubbles: aProps.bubbles,
  347. cancelable: aProps.cancelable, data: aProps.data, origin: aProps.origin,
  348. lastEventId: aProps.lastEventId, source: aProps.source });
  349. return e;
  350. },
  351. },
  352. SimpleGestureEvent: { create: function (aName, aProps) {
  353. var e = document.createEvent("simplegestureevent");
  354. e.initSimpleGestureEvent(aName, aProps.bubbles, aProps.cancelable,
  355. aProps.view, aProps.detail,
  356. aProps.screenX, aProps.screenY,
  357. aProps.clientX, aProps.clientY,
  358. aProps.ctrlKey, aProps.altKey, aProps.shiftKey, aProps.metaKey,
  359. aProps.button, aProps.relatedTarget,
  360. aProps.allowedDirections, aProps.direction, aProps.delta || 0.0,
  361. aProps.clickCount);
  362. return e;
  363. },
  364. },
  365. SpeechRecognitionError: { create: function (aName, aProps) {
  366. return new SpeechRecognitionError(aName, aProps);
  367. },
  368. },
  369. SpeechRecognitionEvent: { create: function (aName, aProps) {
  370. return new SpeechRecognitionEvent(aName, aProps);
  371. },
  372. },
  373. SpeechSynthesisErrorEvent: { create: function (aName, aProps) {
  374. aProps.error = "synthesis-unavailable";
  375. aProps.utterance = new SpeechSynthesisUtterance("Hello World");
  376. return new SpeechSynthesisErrorEvent(aName, aProps);
  377. },
  378. },
  379. SpeechSynthesisEvent: { create: function (aName, aProps) {
  380. aProps.utterance = new SpeechSynthesisUtterance("Hello World");
  381. return new SpeechSynthesisEvent(aName, aProps);
  382. },
  383. },
  384. StorageEvent: { create: function (aName, aProps) {
  385. return new StorageEvent(aName, aProps);
  386. },
  387. },
  388. StyleRuleChangeEvent: { create: function (aName, aProps) {
  389. return new StyleRuleChangeEvent(aName, aProps);
  390. },
  391. chromeOnly: true,
  392. },
  393. StyleSheetApplicableStateChangeEvent: { create: function (aName, aProps) {
  394. return new StyleSheetApplicableStateChangeEvent(aName, aProps);
  395. },
  396. chromeOnly: true,
  397. },
  398. StyleSheetChangeEvent: { create: function (aName, aProps) {
  399. return new StyleSheetChangeEvent(aName, aProps);
  400. },
  401. chromeOnly: true,
  402. },
  403. SVGZoomEvent: { create: function (aName, aProps) {
  404. var e = document.createEvent("svgzoomevent");
  405. e.initUIEvent(aName, aProps.bubbles, aProps.cancelable,
  406. aProps.view, aProps.detail);
  407. return e;
  408. },
  409. },
  410. TCPSocketErrorEvent: { create: function(aName, aProps) {
  411. return new TCPSocketErrorEvent(aName, aProps);
  412. },
  413. },
  414. TCPSocketEvent: { create: function(aName, aProps) {
  415. return new TCPSocketEvent(aName, aProps);
  416. },
  417. },
  418. TCPServerSocketEvent: { create: function(aName, aProps) {
  419. return new TCPServerSocketEvent(aName, aProps);
  420. },
  421. },
  422. TimeEvent: { create: function (aName, aProps) {
  423. var e = document.createEvent("timeevent");
  424. e.initTimeEvent(aName, aProps.view, aProps.detail);
  425. return e;
  426. },
  427. },
  428. TouchEvent: { create: function (aName, aProps) {
  429. var e = document.createEvent("touchevent");
  430. e.initTouchEvent(aName, aProps.bubbles, aProps.cancelable,
  431. aProps.view, aProps.detail,
  432. aProps.ctrlKey, aProps.altKey, aProps.shiftKey, aProps.metaKey,
  433. aProps.touches, aProps.targetTouches, aProps.changedTouches);
  434. return e;
  435. },
  436. },
  437. TrackEvent: { create: function (aName, aProps) {
  438. return new TrackEvent(aName, aProps);
  439. },
  440. },
  441. TransitionEvent: { create: function (aName, aProps) {
  442. return new TransitionEvent(aName, aProps);
  443. },
  444. },
  445. UIEvent: { create: function (aName, aProps) {
  446. return new UIEvent(aName, aProps);
  447. },
  448. },
  449. UserProximityEvent: { create: function (aName, aProps) {
  450. return new UserProximityEvent(aName, aProps);
  451. },
  452. },
  453. USSDReceivedEvent: { create: function (aName, aProps) {
  454. return new USSDReceivedEvent(aName, aProps);
  455. },
  456. },
  457. WheelEvent: { create: function (aName, aProps) {
  458. return new WheelEvent(aName, aProps);
  459. },
  460. },
  461. WebGLContextEvent: { create: function (aName, aProps) {
  462. return new WebGLContextEvent(aName, aProps);
  463. },
  464. },
  465. };
  466. for (var name of Object.keys(kEventConstructors)) {
  467. if (!kEventConstructors[name].chromeOnly) {
  468. continue;
  469. }
  470. if (window[name]) {
  471. ok(false, name + " should be chrome only.");
  472. }
  473. window[name] = SpecialPowers.unwrap(SpecialPowers.wrap(window)[name]);
  474. }
  475. var props = Object.getOwnPropertyNames(window);
  476. for (var i = 0; i < props.length; i++) {
  477. // Assume that event object must be named as "FooBarEvent".
  478. if (!props[i].match(/^([A-Z][a-zA-Z]+)?Event$/)) {
  479. continue;
  480. }
  481. if (!kEventConstructors[props[i]]) {
  482. ok(false, "Unknown event found: " + props[i]);
  483. continue;
  484. }
  485. if (!kEventConstructors[props[i]].create) {
  486. todo(false, "Cannot create untrusted event of " + props[i]);
  487. continue;
  488. }
  489. ok(true, "Creating " + props[i] + "...");
  490. var event = kEventConstructors[props[i]].create("foo", {});
  491. if (!event) {
  492. ok(false, "Failed to create untrusted event: " + props[i]);
  493. continue;
  494. }
  495. if (typeof(event.getModifierState) == "function") {
  496. const kModifiers = [ "Shift", "Control", "Alt", "AltGr", "Meta", "CapsLock", "ScrollLock", "NumLock", "OS", "Fn", "FnLock", "Symbol", "SymbolLock" ];
  497. for (var j = 0; j < kModifiers.length; j++) {
  498. ok(true, "Calling " + props[i] + ".getModifierState(" + kModifiers[j] + ")...");
  499. var modifierState = event.getModifierState(kModifiers[j]);
  500. ok(true, props[i] + ".getModifierState(" + kModifiers[j] + ") = " + modifierState);
  501. }
  502. }
  503. }
  504. </script>
  505. </pre>
  506. </body>
  507. </html>