/testing/web-platform/tests/dom/events/Event-initEvent.html

https://github.com/rillian/firefox · HTML · 118 lines · 103 code · 15 blank · 0 comment · 0 complexity · 4d4dbadd477cdbdbaef849501d90d173 MD5 · raw file

  1. <!DOCTYPE html>
  2. <title>Event.initEvent</title>
  3. <link rel="author" title="Ms2ger" href="mailto:Ms2ger@gmail.com">
  4. <script src="/resources/testharness.js"></script>
  5. <script src="/resources/testharnessreport.js"></script>
  6. <div id="log"></div>
  7. <script>
  8. var booleans = [true, false];
  9. booleans.forEach(function(bubbles) {
  10. booleans.forEach(function(cancelable) {
  11. test(function() {
  12. var e = document.createEvent("Event")
  13. e.initEvent("type", bubbles, cancelable)
  14. // Step 3.
  15. // Stop (immediate) propagation flag is tested later
  16. assert_equals(e.defaultPrevented, false, "defaultPrevented")
  17. // Step 4.
  18. assert_equals(e.isTrusted, false, "isTrusted")
  19. // Step 5.
  20. assert_equals(e.target, null, "target")
  21. // Step 6.
  22. assert_equals(e.type, "type", "type")
  23. // Step 7.
  24. assert_equals(e.bubbles, bubbles, "bubbles")
  25. // Step 8.
  26. assert_equals(e.cancelable, cancelable, "cancelable")
  27. }, "Properties of initEvent(type, " + bubbles + ", " + cancelable + ")")
  28. })
  29. })
  30. test(function() {
  31. var e = document.createEvent("Event")
  32. e.initEvent("type 1", true, false)
  33. assert_equals(e.type, "type 1", "type (first init)")
  34. assert_equals(e.bubbles, true, "bubbles (first init)")
  35. assert_equals(e.cancelable, false, "cancelable (first init)")
  36. e.initEvent("type 2", false, true)
  37. assert_equals(e.type, "type 2", "type (second init)")
  38. assert_equals(e.bubbles, false, "bubbles (second init)")
  39. assert_equals(e.cancelable, true, "cancelable (second init)")
  40. }, "Calling initEvent multiple times (getting type).")
  41. test(function() {
  42. // https://bugzilla.mozilla.org/show_bug.cgi?id=998809
  43. var e = document.createEvent("Event")
  44. e.initEvent("type 1", true, false)
  45. assert_equals(e.bubbles, true, "bubbles (first init)")
  46. assert_equals(e.cancelable, false, "cancelable (first init)")
  47. e.initEvent("type 2", false, true)
  48. assert_equals(e.type, "type 2", "type (second init)")
  49. assert_equals(e.bubbles, false, "bubbles (second init)")
  50. assert_equals(e.cancelable, true, "cancelable (second init)")
  51. }, "Calling initEvent multiple times (not getting type).")
  52. // Step 2.
  53. async_test(function() {
  54. // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17715
  55. var e = document.createEvent("Event")
  56. e.initEvent("type", false, false)
  57. assert_equals(e.type, "type", "type (first init)")
  58. assert_equals(e.bubbles, false, "bubbles (first init)")
  59. assert_equals(e.cancelable, false, "cancelable (first init)")
  60. var target = document.createElement("div")
  61. target.addEventListener("type", this.step_func(function() {
  62. e.initEvent("fail", true, true)
  63. assert_equals(e.type, "type", "type (second init)")
  64. assert_equals(e.bubbles, false, "bubbles (second init)")
  65. assert_equals(e.cancelable, false, "cancelable (second init)")
  66. }), false)
  67. assert_equals(target.dispatchEvent(e), true, "dispatchEvent must return true")
  68. this.done()
  69. }, "Calling initEvent must not have an effect during dispatching.")
  70. test(function() {
  71. var e = document.createEvent("Event")
  72. e.stopPropagation()
  73. e.initEvent("type", false, false)
  74. var target = document.createElement("div")
  75. var called = false
  76. target.addEventListener("type", function() { called = true }, false)
  77. assert_true(target.dispatchEvent(e), "dispatchEvent must return true")
  78. assert_true(called, "Listener must be called")
  79. }, "Calling initEvent must unset the stop propagation flag.")
  80. test(function() {
  81. var e = document.createEvent("Event")
  82. e.stopImmediatePropagation()
  83. e.initEvent("type", false, false)
  84. var target = document.createElement("div")
  85. var called = false
  86. target.addEventListener("type", function() { called = true }, false)
  87. assert_true(target.dispatchEvent(e), "dispatchEvent must return true")
  88. assert_true(called, "Listener must be called")
  89. }, "Calling initEvent must unset the stop immediate propagation flag.")
  90. async_test(function() {
  91. var e = document.createEvent("Event")
  92. e.initEvent("type", false, false)
  93. var target = document.createElement("div")
  94. target.addEventListener("type", this.step_func(function() {
  95. e.initEvent("type2", true, true);
  96. assert_equals(e.type, "type", "initEvent type setter should short-circuit");
  97. assert_false(e.bubbles, "initEvent bubbles setter should short-circuit");
  98. assert_false(e.cancelable, "initEvent cancelable setter should short-circuit");
  99. }), false)
  100. assert_equals(target.dispatchEvent(e), true, "dispatchEvent must return true")
  101. this.done()
  102. }, "Calling initEvent during propagation.")
  103. </script>