test_eventctors.html 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=675884
  5. -->
  6. <head>
  7. <title>Test for Bug 675884</title>
  8. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  10. </head>
  11. <body>
  12. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=675884">Mozilla Bug 675884</a>
  13. <p id="display"></p>
  14. <div id="content" style="display: none">
  15. </div>
  16. <pre id="test">
  17. <script type="application/javascript">
  18. /** Test for Bug 675884 **/
  19. var receivedEvent;
  20. document.addEventListener("hello", function(e) { receivedEvent = e; }, true);
  21. function isMethodResultInitializer(aPropName)
  22. {
  23. return aPropName.startsWith("modifier");
  24. }
  25. function getPropValue(aEvent, aPropName)
  26. {
  27. if (aPropName.startsWith("modifier")) {
  28. return aEvent.getModifierState(aPropName.substr("modifier".length));
  29. }
  30. return aEvent[aPropName];
  31. }
  32. // Event
  33. var e;
  34. var ex = false;
  35. try {
  36. e = new Event();
  37. } catch(exp) {
  38. ex = true;
  39. }
  40. ok(ex, "First parameter is required!");
  41. ex = false;
  42. try {
  43. e = new Event("foo", 123);
  44. } catch(exp) {
  45. ex = true;
  46. }
  47. ok(ex, "2nd parameter should be an object!");
  48. ex = false;
  49. try {
  50. e = new Event("foo", "asdf");
  51. } catch(exp) {
  52. ex = true;
  53. }
  54. ok(ex, "2nd parameter should be an object!");
  55. ex = false;
  56. try {
  57. e = new Event("foo", false);
  58. } catch(exp) {
  59. ex = true;
  60. }
  61. ok(ex, "2nd parameter should be an object!");
  62. ex = false;
  63. e = new Event("hello");
  64. is(e.type, "hello", "Wrong event type!");
  65. ok(!e.isTrusted, "Event shouldn't be trusted!");
  66. e.isTrusted = true;
  67. ok(!e.isTrusted, "Event shouldn't be trusted!");
  68. try {
  69. e.__defineGetter__("isTrusted", function() { return true });
  70. } catch (exp) {
  71. ex = true;
  72. }
  73. ok(ex, "Shouldn't be able to re-define the getter for isTrusted.");
  74. ex = false;
  75. ok(!e.isTrusted, "Event shouldn't be trusted!");
  76. ok(!("isTrusted" in Object.getPrototypeOf(e)))
  77. ok(!e.bubbles, "Event shouldn't bubble!");
  78. ok(!e.cancelable, "Event shouldn't be cancelable!");
  79. is(e.eventPhase, Event.NONE, "Wrong event phase");
  80. document.dispatchEvent(e);
  81. is(e.eventPhase, Event.NONE, "Wrong event phase");
  82. is(receivedEvent, e, "Wrong event!");
  83. e = new Event("hello", null);
  84. is(e.type, "hello", "Wrong event type!");
  85. ok(!e.isTrusted, "Event shouldn't be trusted!");
  86. ok(!e.bubbles, "Event shouldn't bubble!");
  87. ok(!e.cancelable, "Event shouldn't be cancelable!");
  88. is(e.eventPhase, Event.NONE, "Wrong event phase");
  89. document.dispatchEvent(e);
  90. is(e.eventPhase, Event.NONE, "Wrong event phase");
  91. is(receivedEvent, e, "Wrong event!");
  92. e = new Event("hello", undefined);
  93. is(e.type, "hello", "Wrong event type!");
  94. ok(!e.isTrusted, "Event shouldn't be trusted!");
  95. ok(!e.bubbles, "Event shouldn't bubble!");
  96. ok(!e.cancelable, "Event shouldn't be cancelable!");
  97. is(e.eventPhase, Event.NONE, "Wrong event phase");
  98. document.dispatchEvent(e);
  99. is(e.eventPhase, Event.NONE, "Wrong event phase");
  100. is(receivedEvent, e, "Wrong event!");
  101. e = new Event("hello", {});
  102. is(e.type, "hello", "Wrong event type!");
  103. ok(!e.isTrusted, "Event shouldn't be trusted!");
  104. ok(!e.bubbles, "Event shouldn't bubble!");
  105. ok(!e.cancelable, "Event shouldn't be cancelable!");
  106. is(e.eventPhase, Event.NONE, "Wrong event phase");
  107. document.dispatchEvent(e);
  108. is(e.eventPhase, Event.NONE, "Wrong event phase");
  109. is(receivedEvent, e, "Wrong event!");
  110. e = new Event("hello", { bubbles: true, cancelable: true });
  111. is(e.type, "hello", "Wrong event type!");
  112. ok(!e.isTrusted, "Event shouldn't be trusted!");
  113. ok(e.bubbles, "Event should bubble!");
  114. ok(e.cancelable, "Event should be cancelable!");
  115. document.dispatchEvent(e);
  116. is(receivedEvent, e, "Wrong event!");
  117. // CustomEvent
  118. try {
  119. e = new CustomEvent();
  120. } catch(exp) {
  121. ex = true;
  122. }
  123. ok(ex, "First parameter is required!");
  124. ex = false;
  125. e = new CustomEvent("hello");
  126. is(e.type, "hello", "Wrong event type!");
  127. ok(!e.isTrusted, "Event shouldn't be trusted!");
  128. ok(!e.bubbles, "Event shouldn't bubble!");
  129. ok(!e.cancelable, "Event shouldn't be cancelable!");
  130. document.dispatchEvent(e);
  131. is(receivedEvent, e, "Wrong event!");
  132. e = new CustomEvent("hello", { bubbles: true, cancelable: true, detail: window });
  133. is(e.type, "hello", "Wrong event type!");
  134. ok(!e.isTrusted, "Event shouldn't be trusted!");
  135. ok(e.bubbles, "Event should bubble!");
  136. ok(e.cancelable, "Event should be cancelable!");
  137. is(e.detail, window , "Wrong event.detail!");
  138. document.dispatchEvent(e);
  139. is(receivedEvent, e, "Wrong event!");
  140. e = new CustomEvent("hello", { cancelable: true, detail: window });
  141. is(e.type, "hello", "Wrong event type!");
  142. ok(!e.isTrusted, "Event shouldn't be trusted!");
  143. ok(!e.bubbles, "Event shouldn't bubble!");
  144. ok(e.cancelable, "Event should be cancelable!");
  145. is(e.detail, window , "Wrong event.detail!");
  146. e = new CustomEvent("hello", { detail: 123 });
  147. is(e.detail, 123, "Wrong event.detail!");
  148. ok(!e.bubbles, "Event shouldn't bubble!");
  149. ok(!e.cancelable, "Event shouldn't be cancelable!");
  150. var dict = { get detail() { return document.body } };
  151. e = new CustomEvent("hello", dict);
  152. is(e.detail, dict.detail, "Wrong event.detail!");
  153. ok(!e.bubbles, "Event shouldn't bubble!");
  154. ok(!e.cancelable, "Event shouldn't be cancelable!");
  155. var dict = { get detail() { throw "foo"; } };
  156. try {
  157. e = new CustomEvent("hello", dict);
  158. } catch (exp) {
  159. ex = true;
  160. }
  161. ok(ex, "Should have thrown an exception!");
  162. ex = false;
  163. // BlobEvent
  164. try {
  165. e = new BlobEvent();
  166. } catch(exp) {
  167. ex = true;
  168. }
  169. ok(ex, "First parameter is required!");
  170. ex = false;
  171. e = new BlobEvent("hello");
  172. is(e.type, "hello", "Wrong event type!");
  173. ok(!e.isTrusted, "Event shouldn't be trusted!");
  174. try {
  175. e.__defineGetter__("isTrusted", function() { return true });
  176. } catch (exp) {
  177. ex = true;
  178. }
  179. ok(ex, "Shouldn't be able to re-define the getter for isTrusted.");
  180. ex = false;
  181. ok(!e.isTrusted, "BlobEvent shouldn't be trusted!");
  182. ok(!e.bubbles, "Event shouldn't bubble!");
  183. ok(!e.cancelable, "Event shouldn't be cancelable!");
  184. document.dispatchEvent(e);
  185. is(receivedEvent, e, "Wrong event!");
  186. var blob = new Blob();
  187. e = new BlobEvent("hello", { bubbles: true, cancelable: true, data: blob });
  188. is(e.type, "hello", "Wrong event type!");
  189. ok(!e.isTrusted, "Event shouldn't be trusted!");
  190. ok(e.bubbles, "Event should bubble!");
  191. ok(e.cancelable, "Event should be cancelable!");
  192. is(e.data, blob , "Wrong event.data!");
  193. document.dispatchEvent(e);
  194. is(receivedEvent, e, "Wrong event!");
  195. e = new BlobEvent("hello", {data: blob});
  196. is(e.type, "hello", "Wrong event type!");
  197. ok(!e.isTrusted, "Event shouldn't be trusted!");
  198. ok(!e.bubbles, "Event shouldn't bubble!");
  199. ok(!e.cancelable, "Event should be cancelable1!");
  200. is(e.data, blob , "Wrong event.data!");
  201. e = new BlobEvent("hello", { data: null });
  202. is(e.data, null, "Wrong event.data!");
  203. ok(!e.bubbles, "Event shouldn't bubble!");
  204. ok(!e.cancelable, "Event shouldn't be cancelable!");
  205. blob = null;
  206. // CloseEvent
  207. try {
  208. e = new CloseEvent();
  209. } catch(exp) {
  210. ex = true;
  211. }
  212. ok(ex, "First parameter is required!");
  213. ex = false;
  214. e = new CloseEvent("hello");
  215. is(e.type, "hello", "Wrong event type!");
  216. ok(!e.isTrusted, "Event shouldn't be trusted!");
  217. ok(!e.bubbles, "Event shouldn't bubble!");
  218. ok(!e.cancelable, "Event shouldn't be cancelable!");
  219. is(e.wasClean, false, "wasClean should be false!");
  220. is(e.code, 0, "code should be 0!");
  221. is(e.reason, "", "reason should be ''!");
  222. document.dispatchEvent(e);
  223. is(receivedEvent, e, "Wrong event!");
  224. e = new CloseEvent("hello",
  225. { bubbles: true, cancelable: true, wasClean: true, code: 1, reason: "foo" });
  226. is(e.type, "hello", "Wrong event type!");
  227. ok(!e.isTrusted, "Event shouldn't be trusted!");
  228. ok(e.bubbles, "Event should bubble!");
  229. ok(e.cancelable, "Event should be cancelable!");
  230. is(e.wasClean, true, "wasClean should be true!");
  231. is(e.code, 1, "code should be 1!");
  232. is(e.reason, "foo", "reason should be 'foo'!");
  233. document.dispatchEvent(e);
  234. is(receivedEvent, e, "Wrong event!");
  235. e = new CloseEvent("hello",
  236. { bubbles: true, cancelable: true, wasClean: true, code: 1 });
  237. is(e.type, "hello", "Wrong event type!");
  238. ok(!e.isTrusted, "Event shouldn't be trusted!");
  239. ok(e.bubbles, "Event should bubble!");
  240. ok(e.cancelable, "Event should be cancelable!");
  241. is(e.wasClean, true, "wasClean should be true!");
  242. is(e.code, 1, "code should be 1!");
  243. is(e.reason, "", "reason should be ''!");
  244. document.dispatchEvent(e);
  245. is(receivedEvent, e, "Wrong event!");
  246. // HashChangeEvent
  247. try {
  248. e = new HashChangeEvent();
  249. } catch(exp) {
  250. ex = true;
  251. }
  252. ok(ex, "First parameter is required!");
  253. ex = false;
  254. e = new HashChangeEvent("hello");
  255. is(e.type, "hello", "Wrong event type!");
  256. ok(!e.isTrusted, "Event shouldn't be trusted!");
  257. ok(!e.bubbles, "Event shouldn't bubble!");
  258. ok(!e.cancelable, "Event shouldn't be cancelable!");
  259. is(e.oldURL, "", "oldURL should be ''");
  260. is(e.newURL, "", "newURL should be ''");
  261. document.dispatchEvent(e);
  262. is(receivedEvent, e, "Wrong event!");
  263. e = new HashChangeEvent("hello",
  264. { bubbles: true, cancelable: true, oldURL: "old", newURL: "new" });
  265. is(e.type, "hello", "Wrong event type!");
  266. ok(!e.isTrusted, "Event shouldn't be trusted!");
  267. ok(e.bubbles, "Event should bubble!");
  268. ok(e.cancelable, "Event should be cancelable!");
  269. is(e.oldURL, "old", "oldURL should be 'old'");
  270. is(e.newURL, "new", "newURL should be 'new'");
  271. document.dispatchEvent(e);
  272. is(receivedEvent, e, "Wrong event!");
  273. e = new HashChangeEvent("hello",
  274. { bubbles: true, cancelable: true, newURL: "new" });
  275. is(e.type, "hello", "Wrong event type!");
  276. ok(!e.isTrusted, "Event shouldn't be trusted!");
  277. ok(e.bubbles, "Event should bubble!");
  278. ok(e.cancelable, "Event should be cancelable!");
  279. is(e.oldURL, "", "oldURL should be ''");
  280. is(e.newURL, "new", "newURL should be 'new'");
  281. document.dispatchEvent(e);
  282. is(receivedEvent, e, "Wrong event!");
  283. // InputEvent
  284. e = new InputEvent("hello");
  285. is(e.type, "hello", "Wrong event type!");
  286. ok(!e.isTrusted, "Event shouldn't be trusted!");
  287. ok(!e.bubbles, "Event shouldn't bubble!");
  288. ok(!e.cancelable, "Event shouldn't be cancelable!");
  289. is(e.detail, 0, "detail should be 0");
  290. ok(!e.isComposing, "isComposing should be false");
  291. e = new InputEvent("hi!", { bubbles: true, detail: 5, isComposing: false });
  292. is(e.type, "hi!", "Wrong event type!");
  293. ok(!e.isTrusted, "Event shouldn't be trusted!");
  294. ok(e.bubbles, "Event should bubble!");
  295. ok(!e.cancelable, "Event shouldn't be cancelable!");
  296. is(e.detail, 5, "detail should be 5");
  297. ok(!e.isComposing, "isComposing should be false");
  298. e = new InputEvent("hi!", { cancelable: true, detail: 0, isComposing: true });
  299. is(e.type, "hi!", "Wrong event type!");
  300. ok(!e.isTrusted, "Event shouldn't be trusted!");
  301. ok(!e.bubbles, "Event shouldn't bubble!");
  302. ok(e.cancelable, "Event should be cancelable!");
  303. is(e.detail, 0, "detail should be 0");
  304. ok(e.isComposing, "isComposing should be true");
  305. // KeyboardEvent
  306. try {
  307. e = new KeyboardEvent();
  308. } catch(exp) {
  309. ex = true;
  310. }
  311. ok(ex, "KeyboardEvent: First parameter is required!");
  312. ex = false;
  313. e = new KeyboardEvent("hello");
  314. ok(e.type, "hello", "KeyboardEvent: Wrong event type!");
  315. ok(!e.isTrusted, "KeyboardEvent: Event shouldn't be trusted!");
  316. ok(!e.bubbles, "KeyboardEvent: Event shouldn't bubble!");
  317. ok(!e.cancelable, "KeyboardEvent: Event shouldn't be cancelable!");
  318. document.dispatchEvent(e);
  319. is(receivedEvent, e, "KeyboardEvent: Wrong event!");
  320. var keyboardEventProps =
  321. [
  322. { bubbles: false },
  323. { cancelable: false },
  324. { view: null },
  325. { detail: 0 },
  326. { key: "" },
  327. { code: "" },
  328. { location: 0 },
  329. { ctrlKey: false },
  330. { shiftKey: false },
  331. { altKey: false },
  332. { metaKey: false },
  333. { modifierAltGraph: false },
  334. { modifierCapsLock: false },
  335. { modifierFn: false },
  336. { modifierFnLock: false },
  337. { modifierNumLock: false },
  338. { modifierOS: false },
  339. { modifierScrollLock: false },
  340. { modifierSymbol: false },
  341. { modifierSymbolLock: false },
  342. { repeat: false },
  343. { isComposing: false },
  344. { charCode: 0 },
  345. { keyCode: 0 },
  346. { which: 0 },
  347. ];
  348. var testKeyboardProps =
  349. [
  350. { bubbles: true },
  351. { cancelable: true },
  352. { view: window },
  353. { detail: 1 },
  354. { key: "CustomKey" },
  355. { code: "CustomCode" },
  356. { location: 1 },
  357. { ctrlKey: true },
  358. { shiftKey: true },
  359. { altKey: true },
  360. { metaKey: true },
  361. { modifierAltGraph: true },
  362. { modifierCapsLock: true },
  363. { modifierFn: true },
  364. { modifierFnLock: true },
  365. { modifierNumLock: true },
  366. { modifierOS: true },
  367. { modifierScrollLock: true },
  368. { modifierSymbol: true },
  369. { modifierSymbolLock: true },
  370. { repeat: true },
  371. { isComposing: true },
  372. { charCode: 2 },
  373. { keyCode: 3 },
  374. { which: 4 },
  375. { charCode: 5, which: 6 },
  376. { keyCode: 7, which: 8 },
  377. { keyCode: 9, charCode: 10 },
  378. { keyCode: 11, charCode: 12, which: 13 },
  379. ];
  380. var codeEnabled = SpecialPowers.getBoolPref("dom.keyboardevent.code.enabled");
  381. var defaultKeyboardEventValues = {};
  382. for (var i = 0; i < keyboardEventProps.length; ++i) {
  383. for (prop in keyboardEventProps[i]) {
  384. if (!codeEnabled && prop == "code") {
  385. continue;
  386. }
  387. if (!isMethodResultInitializer(prop)) {
  388. ok(prop in e, "keyboardEvent: KeyboardEvent doesn't have property " + prop + "!");
  389. }
  390. defaultKeyboardEventValues[prop] = keyboardEventProps[i][prop];
  391. }
  392. }
  393. while (testKeyboardProps.length) {
  394. var p = testKeyboardProps.shift();
  395. e = new KeyboardEvent("foo", p);
  396. for (var def in defaultKeyboardEventValues) {
  397. if (!codeEnabled && def == "code") {
  398. continue;
  399. }
  400. if (!(def in p)) {
  401. is(getPropValue(e, def), defaultKeyboardEventValues[def],
  402. "KeyboardEvent: Wrong default value for " + def + "!");
  403. } else {
  404. is(getPropValue(e, def), p[def],
  405. "KeyboardEvent: Wrong event init value for " + def + "!");
  406. }
  407. }
  408. }
  409. // PageTransitionEvent
  410. try {
  411. e = new PageTransitionEvent();
  412. } catch(exp) {
  413. ex = true;
  414. }
  415. ok(ex, "First parameter is required!");
  416. ex = false;
  417. e = new PageTransitionEvent("hello");
  418. is(e.type, "hello", "Wrong event type!");
  419. ok(!e.isTrusted, "Event shouldn't be trusted!");
  420. ok(!e.bubbles, "Event shouldn't bubble!");
  421. ok(!e.cancelable, "Event shouldn't be cancelable!");
  422. is(e.persisted, false, "persisted should be false");
  423. document.dispatchEvent(e);
  424. is(receivedEvent, e, "Wrong event!");
  425. e = new PageTransitionEvent("hello",
  426. { bubbles: true, cancelable: true, persisted: true});
  427. is(e.type, "hello", "Wrong event type!");
  428. ok(!e.isTrusted, "Event shouldn't be trusted!");
  429. ok(e.bubbles, "Event should bubble!");
  430. ok(e.cancelable, "Event should be cancelable!");
  431. is(e.persisted, true, "persisted should be true");
  432. document.dispatchEvent(e);
  433. is(receivedEvent, e, "Wrong event!");
  434. e = new PageTransitionEvent("hello", { persisted: true});
  435. is(e.type, "hello", "Wrong event type!");
  436. ok(!e.isTrusted, "Event shouldn't be trusted!");
  437. ok(!e.bubbles, "Event shouldn't bubble!");
  438. ok(!e.cancelable, "Event shouldn't be cancelable!");
  439. is(e.persisted, true, "persisted should be true");
  440. document.dispatchEvent(e);
  441. is(receivedEvent, e, "Wrong event!");
  442. // PopStateEvent
  443. try {
  444. e = new PopStateEvent();
  445. } catch(exp) {
  446. ex = true;
  447. }
  448. ok(ex, "First parameter is required!");
  449. ex = false;
  450. e = new PopStateEvent("hello");
  451. is(e.type, "hello", "Wrong event type!");
  452. ok(!e.isTrusted, "Event shouldn't be trusted!");
  453. ok(!e.bubbles, "Event shouldn't bubble!");
  454. ok(!e.cancelable, "Event shouldn't be cancelable!");
  455. is(e.state, null, "persisted should be null");
  456. document.dispatchEvent(e);
  457. is(receivedEvent, e, "Wrong event!");
  458. e = new PopStateEvent("hello",
  459. { bubbles: true, cancelable: true, state: window});
  460. is(e.type, "hello", "Wrong event type!");
  461. ok(!e.isTrusted, "Event shouldn't be trusted!");
  462. ok(e.bubbles, "Event should bubble!");
  463. ok(e.cancelable, "Event should be cancelable!");
  464. is(e.state, window, "persisted should be window");
  465. document.dispatchEvent(e);
  466. is(receivedEvent, e, "Wrong event!");
  467. e = new PopStateEvent("hello", { state: window});
  468. is(e.type, "hello", "Wrong event type!");
  469. ok(!e.isTrusted, "Event shouldn't be trusted!");
  470. ok(!e.bubbles, "Event shouldn't bubble!");
  471. ok(!e.cancelable, "Event shouldn't be cancelable!");
  472. is(e.state, window, "persisted should be window");
  473. document.dispatchEvent(e);
  474. is(receivedEvent, e, "Wrong event!");
  475. // UIEvent
  476. try {
  477. e = new UIEvent();
  478. } catch(exp) {
  479. ex = true;
  480. }
  481. ok(ex, "First parameter is required!");
  482. ex = false;
  483. try {
  484. e = new UIEvent("foo", { view: {} });
  485. e.view.onunload;
  486. } catch(exp) {
  487. ex = true;
  488. }
  489. ok(ex, "{} isn't a valid value.");
  490. ex = false;
  491. try {
  492. e = new UIEvent("foo", { view: null });
  493. } catch(exp) {
  494. ex = true;
  495. }
  496. ok(!ex, "null is a valid value.");
  497. is(e.view, null);
  498. ex = false;
  499. e = new UIEvent("hello");
  500. is(e.type, "hello", "Wrong event type!");
  501. ok(!e.isTrusted, "Event shouldn't be trusted!");
  502. ok(!e.bubbles, "Event shouldn't bubble!");
  503. ok(!e.cancelable, "Event shouldn't be cancelable!");
  504. is(e.detail, 0, "detail should be 0");
  505. is(e.view, null, "view should be null");
  506. document.dispatchEvent(e);
  507. is(receivedEvent, e, "Wrong event!");
  508. e = new UIEvent("hello",
  509. { bubbles: true, cancelable: true, view: window, detail: 1});
  510. is(e.type, "hello", "Wrong event type!");
  511. ok(!e.isTrusted, "Event shouldn't be trusted!");
  512. ok(e.bubbles, "Event should bubble!");
  513. ok(e.cancelable, "Event should be cancelable!");
  514. is(e.detail, 1, "detail should be 1");
  515. is(e.view, window, "view should be window");
  516. document.dispatchEvent(e);
  517. is(receivedEvent, e, "Wrong event!");
  518. // StorageEvent
  519. e = document.createEvent("StorageEvent");
  520. ok(e, "Should have created an event!");
  521. try {
  522. e = new StorageEvent();
  523. } catch(exp) {
  524. ex = true;
  525. }
  526. ok(ex, "First parameter is required!");
  527. ex = false;
  528. e = new StorageEvent("hello");
  529. is(e.type, "hello", "Wrong event type!");
  530. ok(!e.isTrusted, "Event shouldn't be trusted!");
  531. ok(!e.bubbles, "Event shouldn't bubble!");
  532. ok(!e.cancelable, "Event shouldn't be cancelable!");
  533. is(e.key, null, "key should be null");
  534. is(e.oldValue, null, "oldValue should be null");
  535. is(e.newValue, null, "newValue should be null");
  536. is(e.url, "", "url should be ''");
  537. document.dispatchEvent(e);
  538. is(receivedEvent, e, "Wrong event!");
  539. e = new StorageEvent("hello",
  540. { bubbles: true, cancelable: true, key: "key",
  541. oldValue: "oldValue", newValue: "newValue", url: "url",
  542. storageArea: localStorage });
  543. is(e.type, "hello", "Wrong event type!");
  544. ok(!e.isTrusted, "Event shouldn't be trusted!");
  545. ok(e.bubbles, "Event should bubble!");
  546. ok(e.cancelable, "Event should be cancelable!");
  547. is(e.key, "key", "Wrong value");
  548. is(e.oldValue, "oldValue", "Wrong value");
  549. is(e.newValue, "newValue", "Wrong value");
  550. is(e.url, "url", "Wrong value");
  551. is(e.storageArea, localStorage, "Wrong value");
  552. document.dispatchEvent(e);
  553. is(receivedEvent, e, "Wrong event!");
  554. // DeviceProximityEvent
  555. e = new DeviceProximityEvent("hello", {min: 0, value: 1, max: 2});
  556. is(e.type, "hello", "Wrong event type!");
  557. ok(!e.isTrusted, "Event should not be trusted");
  558. is(e.value, 1, "value should be 1");
  559. is(e.min, 0, "min should be 0");
  560. is(e.max, 2, "max should be 2");
  561. document.dispatchEvent(e);
  562. is(receivedEvent, e, "Wrong event!");
  563. e = new DeviceProximityEvent("hello");
  564. is(e.value, Infinity, "Uninitialized value should be infinity");
  565. is(e.min, -Infinity, "Uninitialized min should be -infinity");
  566. is(e.max, Infinity, "Uninitialized max should be infinity");
  567. // UserProximityEvent
  568. e = new UserProximityEvent("hello", {near: true});
  569. is(e.type, "hello", "Wrong event type!");
  570. ok(!e.isTrusted, "Event should not be trusted");
  571. is(e.near, true, "near should be true");
  572. document.dispatchEvent(e);
  573. is(receivedEvent, e, "Wrong event!");
  574. // DeviceLightEvent
  575. e = new DeviceLightEvent("hello", {value: 1} );
  576. is(e.type, "hello", "Wrong event type!");
  577. ok(!e.isTrusted, "Event should not be trusted");
  578. is(e.value, 1, "value should be 1");
  579. document.dispatchEvent(e);
  580. is(receivedEvent, e, "Wrong event!");
  581. e = new DeviceLightEvent("hello", {value: Infinity} );
  582. is(e.value, Infinity, "value should be positive infinity");
  583. e = new DeviceLightEvent("hello", {value: -Infinity} );
  584. is(e.value, -Infinity, "value should be negative infinity");
  585. e = new DeviceLightEvent("hello");
  586. is(e.value, Infinity, "Uninitialized value should be positive infinity");
  587. // DeviceOrientationEvent
  588. e = new DeviceOrientationEvent("hello");
  589. is(e.type, "hello", "Wrong event type!");
  590. ok(!e.isTrusted, "Event should not be trusted");
  591. is(e.alpha, null);
  592. is(e.beta, null);
  593. is(e.gamma, null);
  594. is(e.absolute, false);
  595. e = new DeviceOrientationEvent("hello", { alpha: 1, beta: 2, gamma: 3, absolute: true } );
  596. is(e.type, "hello", "Wrong event type!");
  597. ok(!e.isTrusted, "Event should not be trusted");
  598. is(e.alpha, 1);
  599. is(e.beta, 2);
  600. is(e.gamma, 3);
  601. is(e.absolute, true);
  602. document.dispatchEvent(e);
  603. is(receivedEvent, e, "Wrong event!");
  604. // MouseEvent
  605. try {
  606. e = new MouseEvent();
  607. } catch(exp) {
  608. ex = true;
  609. }
  610. ok(ex, "MouseEvent: First parameter is required!");
  611. ex = false;
  612. e = new MouseEvent("hello", { buttons: 1, movementX: 2, movementY: 3});
  613. is(e.type, "hello", "MouseEvent: Wrong event type!");
  614. ok(!e.isTrusted, "MouseEvent: Event shouldn't be trusted!");
  615. ok(!e.bubbles, "MouseEvent: Event shouldn't bubble!");
  616. ok(!e.cancelable, "MouseEvent: Event shouldn't be cancelable!");
  617. is(e.buttons, 1);
  618. is(e.movementX, 2);
  619. is(e.movementY, 3);
  620. document.dispatchEvent(e);
  621. is(receivedEvent, e, "MouseEvent: Wrong event!");
  622. var mouseEventProps =
  623. [ { screenX: 0 },
  624. { screenY: 0 },
  625. { clientX: 0 },
  626. { clientY: 0 },
  627. { ctrlKey: false },
  628. { shiftKey: false },
  629. { altKey: false },
  630. { metaKey: false },
  631. { modifierAltGraph: false },
  632. { modifierCapsLock: false },
  633. { modifierFn: false },
  634. { modifierFnLock: false },
  635. { modifierNumLock: false },
  636. { modifierOS: false },
  637. { modifierScrollLock: false },
  638. { modifierSymbol: false },
  639. { modifierSymbolLock: false },
  640. { button: 0 },
  641. { buttons: 0 },
  642. { relatedTarget: null },
  643. ];
  644. var testProps =
  645. [
  646. { screenX: 1 },
  647. { screenY: 2 },
  648. { clientX: 3 },
  649. { clientY: 4 },
  650. { ctrlKey: true },
  651. { shiftKey: true },
  652. { altKey: true },
  653. { metaKey: true },
  654. { modifierAltGraph: true },
  655. { modifierCapsLock: true },
  656. { modifierFn: true },
  657. { modifierFnLock: true },
  658. { modifierNumLock: true },
  659. { modifierOS: true },
  660. { modifierScrollLock: true },
  661. { modifierSymbol: true },
  662. { modifierSymbolLock: true },
  663. { button: 5 },
  664. { buttons: 6 },
  665. { relatedTarget: window }
  666. ];
  667. var defaultMouseEventValues = {};
  668. for (var i = 0; i < mouseEventProps.length; ++i) {
  669. for (prop in mouseEventProps[i]) {
  670. if (!isMethodResultInitializer(prop)) {
  671. ok(prop in e, "MouseEvent: MouseEvent doesn't have property " + prop + "!");
  672. }
  673. defaultMouseEventValues[prop] = mouseEventProps[i][prop];
  674. }
  675. }
  676. while (testProps.length) {
  677. var p = testProps.shift();
  678. e = new MouseEvent("foo", p);
  679. for (var def in defaultMouseEventValues) {
  680. if (!(def in p)) {
  681. is(getPropValue(e, def), defaultMouseEventValues[def],
  682. "MouseEvent: Wrong default value for " + def + "!");
  683. } else {
  684. is(getPropValue(e, def), p[def], "MouseEvent: Wrong event init value for " + def + "!");
  685. }
  686. }
  687. }
  688. // PopupBlockedEvent
  689. try {
  690. e = new PopupBlockedEvent();
  691. } catch(exp) {
  692. ex = true;
  693. }
  694. ok(ex, "PopupBlockedEvent: First parameter is required!");
  695. ex = false;
  696. e = new PopupBlockedEvent("hello");
  697. is(e.type, "hello", "PopupBlockedEvent: Wrong event type!");
  698. ok(!e.isTrusted, "PopupBlockedEvent: Event shouldn't be trusted!");
  699. ok(!e.bubbles, "PopupBlockedEvent: Event shouldn't bubble!");
  700. ok(!e.cancelable, "PopupBlockedEvent: Event shouldn't be cancelable!");
  701. document.dispatchEvent(e);
  702. is(receivedEvent, e, "PopupBlockedEvent: Wrong event!");
  703. e = new PopupBlockedEvent("hello",
  704. { requestingWindow: window,
  705. popupWindowFeatures: "features",
  706. popupWindowName: "name"
  707. });
  708. is(e.requestingWindow, window);
  709. is(e.popupWindowFeatures, "features");
  710. is(e.popupWindowName, "name");
  711. // WheelEvent
  712. try {
  713. e = new WheelEvent();
  714. } catch(exp) {
  715. ex = true;
  716. }
  717. ok(ex, "WheelEvent: First parameter is required!");
  718. ex = false;
  719. e = new WheelEvent("hello", { buttons: 1, movementX: 2, movementY: 3});
  720. is(e.type, "hello", "WheelEvent: Wrong event type!");
  721. is(e.buttons, 1);
  722. is(e.movementX, 2);
  723. is(e.movementY, 3);
  724. ok(!e.isTrusted, "WheelEvent: Event shouldn't be trusted!");
  725. ok(!e.bubbles, "WheelEvent: Event shouldn't bubble!");
  726. ok(!e.cancelable, "WheelEvent: Event shouldn't be cancelable!");
  727. document.dispatchEvent(e);
  728. is(receivedEvent, e, "WheelEvent: Wrong event!");
  729. var wheelEventProps =
  730. [ { screenX: 0 },
  731. { screenY: 0 },
  732. { clientX: 0 },
  733. { clientY: 0 },
  734. { ctrlKey: false },
  735. { shiftKey: false },
  736. { altKey: false },
  737. { metaKey: false },
  738. { modifierAltGraph: false },
  739. { modifierCapsLock: false },
  740. { modifierFn: false },
  741. { modifierFnLock: false },
  742. { modifierNumLock: false },
  743. { modifierOS: false },
  744. { modifierScrollLock: false },
  745. { modifierSymbol: false },
  746. { modifierSymbolLock: false },
  747. { button: 0 },
  748. { buttons: 0 },
  749. { relatedTarget: null },
  750. { deltaX: 0.0 },
  751. { deltaY: 0.0 },
  752. { deltaZ: 0.0 },
  753. { deltaMode: 0 }
  754. ];
  755. var testWheelProps =
  756. [
  757. { screenX: 1 },
  758. { screenY: 2 },
  759. { clientX: 3 },
  760. { clientY: 4 },
  761. { ctrlKey: true },
  762. { shiftKey: true },
  763. { altKey: true },
  764. { metaKey: true },
  765. { modifierAltGraph: true },
  766. { modifierCapsLock: true },
  767. { modifierFn: true },
  768. { modifierFnLock: true },
  769. { modifierNumLock: true },
  770. { modifierOS: true },
  771. { modifierScrollLock: true },
  772. { modifierSymbol: true },
  773. { modifierSymbolLock: true },
  774. { button: 5 },
  775. { buttons: 6 },
  776. { relatedTarget: window },
  777. { deltaX: 7.8 },
  778. { deltaY: 9.1 },
  779. { deltaZ: 2.3 },
  780. { deltaMode: 4 }
  781. ];
  782. var defaultWheelEventValues = {};
  783. for (var i = 0; i < wheelEventProps.length; ++i) {
  784. for (prop in wheelEventProps[i]) {
  785. if (!isMethodResultInitializer(prop)) {
  786. ok(prop in e, "WheelEvent: WheelEvent doesn't have property " + prop + "!");
  787. }
  788. defaultWheelEventValues[prop] = wheelEventProps[i][prop];
  789. }
  790. }
  791. while (testWheelProps.length) {
  792. var p = testWheelProps.shift();
  793. e = new WheelEvent("foo", p);
  794. for (var def in defaultWheelEventValues) {
  795. if (!(def in p)) {
  796. is(getPropValue(e, def), defaultWheelEventValues[def],
  797. "WheelEvent: Wrong default value for " + def + "!");
  798. } else {
  799. is(getPropValue(e, def), p[def], "WheelEvent: Wrong event init value for " + def + "!");
  800. }
  801. }
  802. }
  803. // DragEvent
  804. try {
  805. e = new DragEvent();
  806. } catch(exp) {
  807. ex = true;
  808. }
  809. ok(ex, "DragEvent: First parameter is required!");
  810. ex = false;
  811. e = new DragEvent("hello", { buttons: 1, movementX: 2, movementY: 3});
  812. is(e.type, "hello", "DragEvent: Wrong event type!");
  813. is(e.buttons, 1);
  814. is(e.movementX, 2);
  815. is(e.movementY, 3);
  816. document.dispatchEvent(e);
  817. is(receivedEvent, e, "DragEvent: Wrong event!");
  818. // TransitionEvent
  819. e = new TransitionEvent("hello", { propertyName: "color", elapsedTime: 3.5, pseudoElement: "", foobar: "baz" })
  820. is("propertyName" in e, true, "Transition events have propertyName property");
  821. is("foobar" in e, false, "Transition events do not copy random properties from event init");
  822. is(e.propertyName, "color", "Transition event copies propertyName from TransitionEventInit");
  823. is(e.elapsedTime, 3.5, "Transition event copies elapsedTime from TransitionEventInit");
  824. is(e.pseudoElement, "", "Transition event copies pseudoElement from TransitionEventInit");
  825. is(e.bubbles, false, "Lack of bubbles property in TransitionEventInit");
  826. is(e.cancelable, false, "Lack of cancelable property in TransitionEventInit");
  827. is(e.type, "hello", "Wrong event type!");
  828. is(e.isTrusted, false, "Event shouldn't be trusted!");
  829. is(e.eventPhase, Event.NONE, "Wrong event phase");
  830. // AnimationEvent
  831. e = new AnimationEvent("hello", { animationName: "bounce3", elapsedTime: 3.5, pseudoElement: "", foobar: "baz" })
  832. is("animationName" in e, true, "Animation events have animationName property");
  833. is("foobar" in e, false, "Animation events do not copy random properties from event init");
  834. is(e.animationName, "bounce3", "Animation event copies animationName from AnimationEventInit");
  835. is(e.elapsedTime, 3.5, "Animation event copies elapsedTime from AnimationEventInit");
  836. is(e.pseudoElement, "", "Animation event copies pseudoElement from AnimationEventInit");
  837. is(e.bubbles, false, "Lack of bubbles property in AnimationEventInit");
  838. is(e.cancelable, false, "Lack of cancelable property in AnimationEventInit");
  839. is(e.type, "hello", "Wrong event type!");
  840. is(e.isTrusted, false, "Event shouldn't be trusted!");
  841. is(e.eventPhase, Event.NONE, "Wrong event phase");
  842. </script>
  843. </pre>
  844. </body>
  845. </html>