summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/events/Event-initEvent.html
blob: 85abdff2fe7bdb94bee9dfc6b5f992e1da840d6f (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
<!DOCTYPE html>
<title>Event.initEvent</title>
<link rel="author" title="Ms2ger" href="mailto:Ms2ger@gmail.com">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
var booleans = [true, false];
booleans.forEach(function(bubbles) {
  booleans.forEach(function(cancelable) {
    test(function() {
      var e = document.createEvent("Event")
      e.initEvent("type", bubbles, cancelable)

      // Step 3.
      // Stop (immediate) propagation flag is tested later
      assert_equals(e.defaultPrevented, false, "defaultPrevented")
      // Step 4.
      assert_equals(e.isTrusted, false, "isTrusted")
      // Step 5.
      assert_equals(e.target, null, "target")
      // Step 6.
      assert_equals(e.type, "type", "type")
      // Step 7.
      assert_equals(e.bubbles, bubbles, "bubbles")
      // Step 8.
      assert_equals(e.cancelable, cancelable, "cancelable")
    }, "Properties of initEvent(type, " + bubbles + ", " + cancelable + ")")
  })
})

test(function() {
  var e = document.createEvent("Event")
  e.initEvent("type 1", true, false)
  assert_equals(e.type, "type 1", "type (first init)")
  assert_equals(e.bubbles, true, "bubbles (first init)")
  assert_equals(e.cancelable, false, "cancelable (first init)")

  e.initEvent("type 2", false, true)
  assert_equals(e.type, "type 2", "type (second init)")
  assert_equals(e.bubbles, false, "bubbles (second init)")
  assert_equals(e.cancelable, true, "cancelable (second init)")
}, "Calling initEvent multiple times (getting type).")

test(function() {
  // https://bugzilla.mozilla.org/show_bug.cgi?id=998809
  var e = document.createEvent("Event")
  e.initEvent("type 1", true, false)
  assert_equals(e.bubbles, true, "bubbles (first init)")
  assert_equals(e.cancelable, false, "cancelable (first init)")

  e.initEvent("type 2", false, true)
  assert_equals(e.type, "type 2", "type (second init)")
  assert_equals(e.bubbles, false, "bubbles (second init)")
  assert_equals(e.cancelable, true, "cancelable (second init)")
}, "Calling initEvent multiple times (not getting type).")

// Step 2.
async_test(function() {
  // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17715

  var e = document.createEvent("Event")
  e.initEvent("type", false, false)
  assert_equals(e.type, "type", "type (first init)")
  assert_equals(e.bubbles, false, "bubbles (first init)")
  assert_equals(e.cancelable, false, "cancelable (first init)")

  var target = document.createElement("div")
  target.addEventListener("type", this.step_func(function() {
    e.initEvent("fail", true, true)
    assert_equals(e.type, "type", "type (second init)")
    assert_equals(e.bubbles, false, "bubbles (second init)")
    assert_equals(e.cancelable, false, "cancelable (second init)")
  }), false)

  assert_equals(target.dispatchEvent(e), true, "dispatchEvent must return true")

  this.done()
}, "Calling initEvent must not have an effect during dispatching.")

test(function() {
  var e = document.createEvent("Event")
  e.stopPropagation()
  e.initEvent("type", false, false)
  var target = document.createElement("div")
  var called = false
  target.addEventListener("type", function() { called = true }, false)
  assert_true(target.dispatchEvent(e), "dispatchEvent must return true")
  assert_true(called, "Listener must be called")
}, "Calling initEvent must unset the stop propagation flag.")

test(function() {
  var e = document.createEvent("Event")
  e.stopImmediatePropagation()
  e.initEvent("type", false, false)
  var target = document.createElement("div")
  var called = false
  target.addEventListener("type", function() { called = true }, false)
  assert_true(target.dispatchEvent(e), "dispatchEvent must return true")
  assert_true(called, "Listener must be called")
}, "Calling initEvent must unset the stop immediate propagation flag.")

async_test(function() {
  var e = document.createEvent("Event")
  e.initEvent("type", false, false)

  var target = document.createElement("div")
  target.addEventListener("type", this.step_func(function() {
    e.initEvent("type2", true, true);
    assert_equals(e.type, "type", "initEvent type setter should short-circuit");
    assert_false(e.bubbles, "initEvent bubbles setter should short-circuit");
    assert_false(e.cancelable, "initEvent cancelable setter should short-circuit");
  }), false)
  assert_equals(target.dispatchEvent(e), true, "dispatchEvent must return true")

  this.done()
}, "Calling initEvent during propagation.")
</script>