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> 8var booleans = [true, false]; 9booleans.forEach(function(bubbles) { 10 booleans.forEach(function(cancelable) { 11 test(function() { 12 var e = document.createEvent("Event") 13 e.initEvent("type", bubbles, cancelable) 14 15 // Step 2. 16 // Stop (immediate) propagation flag is tested later 17 assert_equals(e.defaultPrevented, false, "defaultPrevented") 18 assert_equals(e.returnValue, true, "returnValue") 19 // Step 3. 20 assert_equals(e.isTrusted, false, "isTrusted") 21 // Step 4. 22 assert_equals(e.target, null, "target") 23 assert_equals(e.srcElement, null, "srcElement") 24 // Step 5. 25 assert_equals(e.type, "type", "type") 26 // Step 6. 27 assert_equals(e.bubbles, bubbles, "bubbles") 28 // Step 7. 29 assert_equals(e.cancelable, cancelable, "cancelable") 30 }, "Properties of initEvent(type, " + bubbles + ", " + cancelable + ")") 31 }) 32}) 33 34test(function() { 35 var e = document.createEvent("Event") 36 e.initEvent("type 1", true, false) 37 assert_equals(e.type, "type 1", "type (first init)") 38 assert_equals(e.bubbles, true, "bubbles (first init)") 39 assert_equals(e.cancelable, false, "cancelable (first init)") 40 41 e.initEvent("type 2", false, true) 42 assert_equals(e.type, "type 2", "type (second init)") 43 assert_equals(e.bubbles, false, "bubbles (second init)") 44 assert_equals(e.cancelable, true, "cancelable (second init)") 45}, "Calling initEvent multiple times (getting type).") 46 47test(function() { 48 // https://bugzilla.mozilla.org/show_bug.cgi?id=998809 49 var e = document.createEvent("Event") 50 e.initEvent("type 1", true, false) 51 assert_equals(e.bubbles, true, "bubbles (first init)") 52 assert_equals(e.cancelable, false, "cancelable (first init)") 53 54 e.initEvent("type 2", false, true) 55 assert_equals(e.type, "type 2", "type (second init)") 56 assert_equals(e.bubbles, false, "bubbles (second init)") 57 assert_equals(e.cancelable, true, "cancelable (second init)") 58}, "Calling initEvent multiple times (not getting type).") 59 60// Step 2. 61async_test(function() { 62 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17715 63 64 var e = document.createEvent("Event") 65 e.initEvent("type", false, false) 66 assert_equals(e.type, "type", "type (first init)") 67 assert_equals(e.bubbles, false, "bubbles (first init)") 68 assert_equals(e.cancelable, false, "cancelable (first init)") 69 70 var target = document.createElement("div") 71 target.addEventListener("type", this.step_func(function() { 72 e.initEvent("fail", true, true) 73 assert_equals(e.type, "type", "type (second init)") 74 assert_equals(e.bubbles, false, "bubbles (second init)") 75 assert_equals(e.cancelable, false, "cancelable (second init)") 76 }), false) 77 78 assert_equals(target.dispatchEvent(e), true, "dispatchEvent must return true") 79 80 this.done() 81}, "Calling initEvent must not have an effect during dispatching.") 82 83test(function() { 84 var e = document.createEvent("Event") 85 e.stopPropagation() 86 e.initEvent("type", false, false) 87 var target = document.createElement("div") 88 var called = false 89 target.addEventListener("type", function() { called = true }, false) 90 assert_false(e.cancelBubble, "cancelBubble must be false") 91 assert_true(target.dispatchEvent(e), "dispatchEvent must return true") 92 assert_true(called, "Listener must be called") 93}, "Calling initEvent must unset the stop propagation flag.") 94 95test(function() { 96 var e = document.createEvent("Event") 97 e.stopImmediatePropagation() 98 e.initEvent("type", false, false) 99 var target = document.createElement("div") 100 var called = false 101 target.addEventListener("type", function() { called = true }, false) 102 assert_true(target.dispatchEvent(e), "dispatchEvent must return true") 103 assert_true(called, "Listener must be called") 104}, "Calling initEvent must unset the stop immediate propagation flag.") 105 106async_test(function() { 107 var e = document.createEvent("Event") 108 e.initEvent("type", false, false) 109 110 var target = document.createElement("div") 111 target.addEventListener("type", this.step_func(function() { 112 e.initEvent("type2", true, true); 113 assert_equals(e.type, "type", "initEvent type setter should short-circuit"); 114 assert_false(e.bubbles, "initEvent bubbles setter should short-circuit"); 115 assert_false(e.cancelable, "initEvent cancelable setter should short-circuit"); 116 }), false) 117 assert_equals(target.dispatchEvent(e), true, "dispatchEvent must return true") 118 119 this.done() 120}, "Calling initEvent during propagation.") 121 122test(function() { 123 var e = document.createEvent("Event") 124 assert_throws_js(TypeError, function() { 125 e.initEvent() 126 }) 127}, "First parameter to initEvent should be mandatory.") 128 129test(function() { 130 var e = document.createEvent("Event") 131 e.initEvent("type") 132 assert_equals(e.type, "type", "type") 133 assert_false(e.bubbles, "bubbles") 134 assert_false(e.cancelable, "cancelable") 135}, "Tests initEvent's default parameter values.") 136</script> 137