summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/events/Event-init-while-dispatching.html
blob: 2aa1f6701c4c4bae28c6308d95bd4c6edbf13026 (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Re-initializing events while dispatching them</title>
<link rel="author" title="Josh Matthews" href="mailto:josh@joshmatthews.net">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
var events = {
  'KeyboardEvent': {
    'constructor': function() { return new KeyboardEvent("type", {key: "A"}); },
    'init': function(ev) { ev.initKeyboardEvent("type2", true, true, null, "a", 1, "", true, "") },
    'check': function(ev) {
       assert_equals(ev.key, "A", "initKeyboardEvent key setter should short-circuit");
       assert_false(ev.repeat, "initKeyboardEvent repeat setter should short-circuit");
       assert_equals(ev.location, 0, "initKeyboardEvent location setter should short-circuit");
     }
  },
  'MouseEvent': {
    'constructor': function() { return new MouseEvent("type"); },
    'init': function(ev) { ev.initMouseEvent("type2", true, true, null, 0, 1, 1, 1, 1, true, true, true, true, 1, null) },
    'check': function(ev) {
      assert_equals(ev.screenX, 0, "initMouseEvent screenX setter should short-circuit");
      assert_equals(ev.screenY, 0, "initMouseEvent screenY setter should short-circuit");
      assert_equals(ev.clientX, 0, "initMouseEvent clientX setter should short-circuit");
      assert_equals(ev.clientY, 0, "initMouseEvent clientY setter should short-circuit");
      assert_false(ev.ctrlKey, "initMouseEvent ctrlKey setter should short-circuit");
      assert_false(ev.altKey, "initMouseEvent altKey setter should short-circuit");
      assert_false(ev.shiftKey, "initMouseEvent shiftKey setter should short-circuit");
      assert_false(ev.metaKey, "initMouseEvent metaKey setter should short-circuit");
      assert_equals(ev.button, 0, "initMouseEvent button setter should short-circuit");
    }
  },
  'CustomEvent': {
    'constructor': function() { return new CustomEvent("type") },
    'init': function(ev) { ev.initCustomEvent("type2", true, true, 1) },
    'check': function(ev) {
      assert_equals(ev.detail, null, "initCustomEvent detail setter should short-circuit");
    }
  },
  'UIEvent': {
    'constructor': function() { return new UIEvent("type") },
    'init': function(ev) { ev.initUIEvent("type2", true, true, window, 1) },
    'check': function(ev) {
      assert_equals(ev.view, null, "initUIEvent view setter should short-circuit");
      assert_equals(ev.detail, 0, "initUIEvent detail setter should short-circuit");
    }
  },
  'Event': {
    'constructor': function() { return new Event("type") },
    'init': function(ev) { ev.initEvent("type2", true, true) },
    'check': function(ev) {
      assert_equals(ev.bubbles, false, "initEvent bubbles setter should short-circuit");
      assert_equals(ev.cancelable, false, "initEvent cancelable setter should short-circuit");
      assert_equals(ev.type, "type", "initEvent type setter should short-circuit");
    }
  }
};

var names = Object.keys(events);
for (var i = 0; i < names.length; i++) {
  var t = async_test("Calling init" + names[i] + " while dispatching.");
  t.step(function() {
    var e = events[names[i]].constructor();

    var target = document.createElement("div")
    target.addEventListener("type", t.step_func(function() {
      events[names[i]].init(e);

      var o = e;
      while ((o = Object.getPrototypeOf(o))) {
        if (!(o.constructor.name in events)) {
          break;
        }
        events[o.constructor.name].check(e);
      }
    }), false);

    assert_equals(target.dispatchEvent(e), true, "dispatchEvent must return true")
  });
  t.done();
}
</script>