summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/events/Event-subclasses-constructors.html
blob: 08a5ded011635c66151d22a09330f9341eb48c91 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<meta charset="utf-8">
<title>Event constructors</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
function assert_props(iface, event, defaults) {
  assert_true(event instanceof self[iface]);
  expected[iface].properties.forEach(function(p) {
    var property = p[0], value = p[defaults ? 1 : 2];
    assert_true(property in event,
                "Event " + format_value(event) + " should have a " +
                property + " property");
    assert_equals(event[property], value,
                  "The value of the " + property + " property should be " +
                  format_value(value));
  });
  if ("parent" in expected[iface]) {
    assert_props(expected[iface].parent, event, defaults);
  }
}

// Class declarations don't go on the global by default, so put it there ourselves:

self.SubclassedEvent = class SubclassedEvent extends Event {
  constructor(name, props) {
    super(name, props);
    if (props && typeof(props) == "object" && "customProp" in props) {
      this.customProp = props.customProp;
    } else {
      this.customProp = 5;
    }
  }

  get fixedProp() {
    return 17;
  }
}

var EventModifierInit = [
  ["ctrlKey", false, true],
  ["shiftKey", false, true],
  ["altKey", false, true],
  ["metaKey", false, true],
];
var expected = {
  "Event": {
    "properties": [
      ["bubbles", false, true],
      ["cancelable", false, true],
      ["isTrusted", false, false],
    ],
  },

  "UIEvent": {
    "parent": "Event",
    "properties": [
      ["view", null, window],
      ["detail", 0, 7],
    ],
  },

  "FocusEvent": {
    "parent": "UIEvent",
    "properties": [
      ["relatedTarget", null, document],
    ],
  },

  "MouseEvent": {
    "parent": "UIEvent",
    "properties": EventModifierInit.concat([
      ["screenX", 0, 40],
      ["screenY", 0, 40],
      ["clientX", 0, 40],
      ["clientY", 0, 40],
      ["button", 0, 40],
      ["buttons", 0, 40],
      ["relatedTarget", null, document],
    ]),
  },

  "WheelEvent": {
    "parent": "MouseEvent",
    "properties": [
      ["deltaX", 0.0, 3.1],
      ["deltaY", 0.0, 3.1],
      ["deltaZ", 0.0, 3.1],
      ["deltaMode", 0, 40],
    ],
  },

  "KeyboardEvent": {
    "parent": "UIEvent",
    "properties": EventModifierInit.concat([
      ["key", "", "string"],
      ["code", "", "string"],
      ["location", 0, 7],
      ["repeat", false, true],
      ["isComposing", false, true],
      ["charCode", 0, 7],
      ["keyCode", 0, 7],
      ["which", 0, 7],
    ]),
  },

  "CompositionEvent": {
    "parent": "UIEvent",
    "properties": [
      ["data", "", "string"],
    ],
  },

  "SubclassedEvent": {
    "parent": "Event",
    "properties": [
      ["customProp", 5, 8],
      ["fixedProp", 17, 17],
    ],
  },
};

Object.keys(expected).forEach(function(iface) {
  test(function() {
    var event = new self[iface]("type");
    assert_props(iface, event, true);
  }, iface + " constructor (no argument)");

  test(function() {
    var event = new self[iface]("type", undefined);
    assert_props(iface, event, true);
  }, iface + " constructor (undefined argument)");

  test(function() {
    var event = new self[iface]("type", null);
    assert_props(iface, event, true);
  }, iface + " constructor (null argument)");

  test(function() {
    var event = new self[iface]("type", {});
    assert_props(iface, event, true);
  }, iface + " constructor (empty argument)");

  test(function() {
    var dictionary = {};
    expected[iface].properties.forEach(function(p) {
      var property = p[0], value = p[1];
      dictionary[property] = value;
    });
    var event = new self[iface]("type", dictionary);
    assert_props(iface, event, true);
  }, iface + " constructor (argument with default values)");

  test(function() {
    function fill_in(iface, dictionary) {
      if ("parent" in expected[iface]) {
        fill_in(expected[iface].parent, dictionary)
      }
      expected[iface].properties.forEach(function(p) {
        var property = p[0], value = p[2];
        dictionary[property] = value;
      });
    }

    var dictionary = {};
    fill_in(iface, dictionary);

    var event = new self[iface]("type", dictionary);
    assert_props(iface, event, false);
  }, iface + " constructor (argument with non-default values)");
});

test(function () {
  assert_throws_js(TypeError, function() {
    new UIEvent("x", { view: 7 })
  });
}, "UIEvent constructor (view argument with wrong type)")
</script>