4 <script src=
"../../../resources/js-test.js"></script>
9 description("This tests the constructor for the MouseEvent DOM class.");
11 var testObject
= {nyannyan
: 123};
12 var testDiv
= document
.createElement("div");
13 var xhr
= new XMLHttpRequest
;
15 // No initializer is passed.
16 shouldBe("new MouseEvent('eventType').bubbles", "false");
17 shouldBe("new MouseEvent('eventType').cancelable", "false");
18 shouldBe("new MouseEvent('eventType').view", "null");
19 shouldBe("new MouseEvent('eventType').detail", "0");
20 shouldBe("new MouseEvent('eventType').screenX", "0");
21 shouldBe("new MouseEvent('eventType').screenY", "0");
22 shouldBe("new MouseEvent('eventType').clientX", "0");
23 shouldBe("new MouseEvent('eventType').clientY", "0");
24 shouldBe("new MouseEvent('eventType').ctrlKey", "false");
25 shouldBe("new MouseEvent('eventType').shiftKey", "false");
26 shouldBe("new MouseEvent('eventType').altKey", "false");
27 shouldBe("new MouseEvent('eventType').metaKey", "false");
28 shouldBe("new MouseEvent('eventType').button", "0");
29 shouldBe("new MouseEvent('eventType').buttons", "0");
30 shouldBe("new MouseEvent('eventType').relatedTarget", "null");
33 shouldBe("new MouseEvent('eventType', { bubbles: false }).bubbles", "false");
34 shouldBe("new MouseEvent('eventType', { bubbles: true }).bubbles", "true");
36 // cancelable is passed.
37 shouldBe("new MouseEvent('eventType', { cancelable: false }).cancelable", "false");
38 shouldBe("new MouseEvent('eventType', { cancelable: true }).cancelable", "true");
42 shouldBe("new MouseEvent('eventType', { view: window }).view", "window");
43 shouldBe("new MouseEvent('eventType', { view: this }).view", "this");
45 // Non-window objects.
46 shouldThrow("new MouseEvent('eventType', { view: testObject }).view");
47 shouldThrow("new MouseEvent('eventType', { view: document }).view");
48 shouldBe("new MouseEvent('eventType', { view: undefined }).view", "null");
49 shouldBe("new MouseEvent('eventType', { view: null }).view", "null");
50 shouldThrow("new MouseEvent('eventType', { view: false }).view");
51 shouldThrow("new MouseEvent('eventType', { view: true }).view");
52 shouldThrow("new MouseEvent('eventType', { view: '' }).view");
53 shouldThrow("new MouseEvent('eventType', { view: 'chocolate' }).view");
54 shouldThrow("new MouseEvent('eventType', { view: 12345 }).view");
55 shouldThrow("new MouseEvent('eventType', { view: 18446744073709551615 }).view");
56 shouldThrow("new MouseEvent('eventType', { view: NaN }).view");
57 // Note that valueOf() is not called, when the left hand side is evaluated.
58 shouldThrow("new MouseEvent('eventType', { view: {valueOf: function () { return window; } } }).view == window");
59 shouldBe("new MouseEvent('eventType', { get view() { return window; } }).view", "window");
60 shouldThrow("new MouseEvent('eventType', { get view() { return 123; } }).view");
61 shouldThrow("new MouseEvent('eventType', { get view() { throw 'MouseEvent Error'; } })");
63 // Max/min for client positions (LayoutUnit max/min).
64 var MAX_CLIENT_SIZE
= "33554431";
65 var MIN_CLIENT_SIZE
= "-33554432";
67 // detail, screenX, screenY, clientX and clientY are passed.
68 ["detail", "screenX", "screenY", "clientX", "clientY"].forEach(function (attr
) {
69 // numbers within the long range.
70 shouldBe("new MouseEvent('eventType', { " + attr
+ ": 0 })." + attr
, "0");
71 shouldBe("new MouseEvent('eventType', { " + attr
+ ": 2147483647 })." + attr
, attr
== "clientX" || attr
== "clientY" ? MAX_CLIENT_SIZE
: "2147483647");
72 shouldBe("new MouseEvent('eventType', { " + attr
+ ": -1 })." + attr
, "-1");
73 shouldBe("new MouseEvent('eventType', { " + attr
+ ": -2147483648 })." + attr
, attr
== "clientX" || attr
== "clientY" ? MIN_CLIENT_SIZE
: "-2147483648");
75 // numbers out of the long range.
76 shouldBe("new MouseEvent('eventType', { " + attr
+ ": 4294967295 })." + attr
, "-1");
77 // 2^{53}-1, the largest number that can be exactly represented by double.
78 shouldBe("new MouseEvent('eventType', { " + attr
+ ": 9007199254740991 })." + attr
, "-1");
80 shouldBe("new MouseEvent('eventType', { " + attr
+ ": 18446744073709551615 })." + attr
, "0");
81 shouldBe("new MouseEvent('eventType', { " + attr
+ ": 123.45 })." + attr
, "123");
82 shouldBe("new MouseEvent('eventType', { " + attr
+ ": NaN })." + attr
, "0");
84 // Non-numeric values.
85 shouldBe("new MouseEvent('eventType', { " + attr
+ ": undefined })." + attr
, "0");
86 shouldBe("new MouseEvent('eventType', { " + attr
+ ": null })." + attr
, "0");
87 shouldBe("new MouseEvent('eventType', { " + attr
+ ": '' })." + attr
, "0");
88 shouldBe("new MouseEvent('eventType', { " + attr
+ ": '12345' })." + attr
, "12345");
89 shouldBe("new MouseEvent('eventType', { " + attr
+ ": '12345a' })." + attr
, "0");
90 shouldBe("new MouseEvent('eventType', { " + attr
+ ": 'abc' })." + attr
, "0");
91 shouldBe("new MouseEvent('eventType', { " + attr
+ ": [] })." + attr
, "0");
92 shouldBe("new MouseEvent('eventType', { " + attr
+ ": [12345] })." + attr
, "12345");
93 shouldBe("new MouseEvent('eventType', { " + attr
+ ": [12345, 67890] })." + attr
, "0");
94 shouldBe("new MouseEvent('eventType', { " + attr
+ ": {} })." + attr
, "0");
95 shouldBe("new MouseEvent('eventType', { " + attr
+ ": {moemoe: 12345} })." + attr
, "0");
96 shouldBe("new MouseEvent('eventType', { " + attr
+ ": {valueOf: function () { return 12345; }} })." + attr
, "12345");
99 // ctrlKey, altKey, shiftKey and metaKey are passed.
100 ["ctrlKey", "altKey", "shiftKey", "metaKey"].forEach(function (attr
) {
101 shouldBe("new MouseEvent('eventType', { " + attr
+ ": false })." + attr
, "false");
102 shouldBe("new MouseEvent('eventType', { " + attr
+ ": true })." + attr
, "true");
106 // Numbers within the short range.
107 shouldBe("new MouseEvent('eventType', { button: 0 }).button", "0");
108 shouldBe("new MouseEvent('eventType', { button: 1 }).button", "1");
109 shouldBe("new MouseEvent('eventType', { button: -2 }).button", "-2");
110 shouldBe("new MouseEvent('eventType', { button: -32768 }).button", "-32768");
111 shouldBe("new MouseEvent('eventType', { button: 32767 }).button", "32767");
113 // Numbers that are equal to -1 should be treated as 0.
114 shouldBe("new MouseEvent('eventType', { button: 65535 }).button", "0");
115 shouldBe("new MouseEvent('eventType', { button: 9007199254740991 }).button", "0");
116 shouldBe("new MouseEvent('eventType', { button: -1 }).button", "0");
118 // Numbers out of the short range.
120 shouldBe("new MouseEvent('eventType', { button: 18446744073709551615 }).button", "0");
121 shouldBe("new MouseEvent('eventType', { button: 12345678901234567890 }).button", "2048");
122 shouldBe("new MouseEvent('eventType', { button: 123.45 }).button", "123");
123 shouldBe("new MouseEvent('eventType', { button: NaN }).button", "0");
124 shouldBe("new MouseEvent('eventType', { button: 65534 }).button", "-2");
125 shouldBe("new MouseEvent('eventType', { button: -32769 }).button", "32767");
126 shouldBe("new MouseEvent('eventType', { button: 32768 }).button", "-32768");
129 // Non-numeric values.
130 shouldBe("new MouseEvent('eventType', { button: undefined }).button", "0");
131 shouldBe("new MouseEvent('eventType', { button: null }).button", "0");
132 shouldBe("new MouseEvent('eventType', { button: '' }).button", "0");
133 shouldBe("new MouseEvent('eventType', { button: '12345' }).button", "12345");
134 shouldBe("new MouseEvent('eventType', { button: '12345a' }).button", "0");
135 shouldBe("new MouseEvent('eventType', { button: 'abc' }).button", "0");
136 shouldBe("new MouseEvent('eventType', { button: [] }).button", "0");
137 shouldBe("new MouseEvent('eventType', { button: [12345] }).button", "12345");
138 shouldBe("new MouseEvent('eventType', { button: [12345, 67890] }).button", "0");
139 shouldBe("new MouseEvent('eventType', { button: {} }).button", "0");
140 shouldBe("new MouseEvent('eventType', { button: {moemoe: 12345} }).button", "0");
141 shouldBe("new MouseEvent('eventType', { button: {valueOf: function () { return 12345; }} }).button", "12345");
143 // buttons is passed.
144 // Numbers within the unsigned short range.
145 shouldBe("new MouseEvent('eventType', { buttons: 0 }).buttons", "0");
146 shouldBe("new MouseEvent('eventType', { buttons: 1 }).buttons", "1");
147 shouldBe("new MouseEvent('eventType', { buttons: 65534 }).buttons", "65534");
149 // Numbers out of the unsigned short range.
151 shouldBe("new MouseEvent('eventType', { buttons: 65535 }).buttons", "65535");
152 shouldBe("new MouseEvent('eventType', { buttons: 9007199254740991 }).buttons", "65535");
153 shouldBe("new MouseEvent('eventType', { buttons: -1 }).buttons", "65535");
154 shouldBe("new MouseEvent('eventType', { buttons: 18446744073709551615 }).buttons", "0");
155 shouldBe("new MouseEvent('eventType', { buttons: 12345678901234567890 }).buttons", "2048");
156 shouldBe("new MouseEvent('eventType', { buttons: 123.45 }).buttons", "123");
157 shouldBe("new MouseEvent('eventType', { buttons: NaN }).buttons", "0");
159 // Non-numeric values.
160 shouldBe("new MouseEvent('eventType', { buttons: undefined }).buttons", "0");
161 shouldBe("new MouseEvent('eventType', { buttons: null }).buttons", "0");
162 shouldBe("new MouseEvent('eventType', { buttons: '' }).buttons", "0");
163 shouldBe("new MouseEvent('eventType', { buttons: '12345' }).buttons", "12345");
164 shouldBe("new MouseEvent('eventType', { buttons: '12345a' }).buttons", "0");
165 shouldBe("new MouseEvent('eventType', { buttons: 'abc' }).buttons", "0");
166 shouldBe("new MouseEvent('eventType', { buttons: [] }).buttons", "0");
167 shouldBe("new MouseEvent('eventType', { buttons: [12345] }).buttons", "12345");
168 shouldBe("new MouseEvent('eventType', { buttons: [12345, 67890] }).buttons", "0");
169 shouldBe("new MouseEvent('eventType', { buttons: {} }).buttons", "0");
170 shouldBe("new MouseEvent('eventType', { buttons: {moemoe: 12345} }).buttons", "0");
171 shouldBe("new MouseEvent('eventType', { buttons: {valueOf: function () { return 12345; }} }).buttons", "12345");
173 // relatedTarget is passed.
175 shouldBe("new MouseEvent('eventType', { relatedTarget: testDiv }).relatedTarget", "testDiv");
176 shouldBe("new MouseEvent('eventType', { relatedTarget: document }).relatedTarget", "document");
177 shouldBe("new MouseEvent('eventType', { relatedTarget: xhr }).relatedTarget", "xhr");
178 shouldBe("new MouseEvent('eventType', { relatedTarget: window }).relatedTarget", "window");
181 shouldThrow("new MouseEvent('eventType', { relatedTarget: testObject }).relatedTarget");
182 shouldBe("new MouseEvent('eventType', { relatedTarget: undefined }).relatedTarget", "null");
183 shouldBe("new MouseEvent('eventType', { relatedTarget: null }).relatedTarget", "null");
184 shouldThrow("new MouseEvent('eventType', { relatedTarget: false }).relatedTarget");
185 shouldThrow("new MouseEvent('eventType', { relatedTarget: true }).relatedTarget");
186 shouldThrow("new MouseEvent('eventType', { relatedTarget: '' }).relatedTarget");
187 shouldThrow("new MouseEvent('eventType', { relatedTarget: 'chocolate' }).relatedTarget");
188 shouldThrow("new MouseEvent('eventType', { relatedTarget: 12345 }).relatedTarget");
189 shouldThrow("new MouseEvent('eventType', { relatedTarget: 18446744073709551615 }).relatedTarget");
190 shouldThrow("new MouseEvent('eventType', { relatedTarget: NaN }).relatedTarget");
191 // Note that valueOf() is not called, when the left hand side is evaluated.
192 shouldThrow("new MouseEvent('eventType', { relatedTarget: {valueOf: function () { return testDiv; } } }).relatedTarget == testDiv");
193 shouldBe("new MouseEvent('eventType', { get relatedTarget() { return testDiv; } }).relatedTarget", "testDiv");
194 shouldThrow("new MouseEvent('eventType', { get relatedTarget() { return 123; } }).relatedTarget");
195 shouldThrow("new MouseEvent('eventType', { get relatedTarget() { throw 'MouseEvent Error'; } })");
197 // All initializers are passed.
198 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).bubbles", "true");
199 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).cancelable", "true");
200 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).view", "window");
201 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).detail", "111");
202 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).screenX", "222");
203 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).screenY", "333");
204 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).clientX", "444");
205 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).clientY", "555");
206 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).ctrlKey", "true");
207 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).shiftKey", "true");
208 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).altKey", "true");
209 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).metaKey", "true");
210 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).button", "666");
211 shouldBe("new MouseEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, screenX: 222, screenY: 333, clientX: 444, clientY: 555, ctrlKey: true, shiftKey: true, altKey: true, metaKey: true, button: 666, buttons: 777, relatedTarget: testDiv }).relatedTarget", "testDiv");