675bc2576b4aca3e7f8566beaefeba91916c2bb2
[WebKit-https.git] / LayoutTests / fast / events / constructors / keyboard-event-constructor.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../js/resources/js-test-pre.js"></script>
5 </head>
6 <body>
7 <script>
8
9 description("This tests the constructor for the KeyboardEvent DOM class.");
10
11 var testObject = {nyannyan: 123};
12
13 // No initializer is passed.
14 shouldBe("new KeyboardEvent('eventType').bubbles", "false");
15 shouldBe("new KeyboardEvent('eventType').cancelable", "false");
16 shouldBe("new KeyboardEvent('eventType').view", "null");
17 shouldBe("new KeyboardEvent('eventType').detail", "0");
18 shouldBeEqualToString("new KeyboardEvent('eventType').keyIdentifier", "");
19 shouldBe("new KeyboardEvent('eventType').location", "0");
20 shouldBe("new KeyboardEvent('eventType').ctrlKey", "false");
21 shouldBe("new KeyboardEvent('eventType').altKey", "false");
22 shouldBe("new KeyboardEvent('eventType').shiftKey", "false");
23 shouldBe("new KeyboardEvent('eventType').metaKey", "false");
24
25 // bubbles is passed.
26 shouldBe("new KeyboardEvent('eventType', { bubbles: false }).bubbles", "false");
27 shouldBe("new KeyboardEvent('eventType', { bubbles: true }).bubbles", "true");
28
29 // cancelable is passed.
30 shouldBe("new KeyboardEvent('eventType', { cancelable: false }).cancelable", "false");
31 shouldBe("new KeyboardEvent('eventType', { cancelable: true }).cancelable", "true");
32
33 // view is passed.
34 // Window objects.
35 shouldBe("new KeyboardEvent('eventType', { view: window }).view", "window");
36 shouldBe("new KeyboardEvent('eventType', { view: this }).view", "this");
37
38 // Non-window objects.
39 shouldBe("new KeyboardEvent('eventType', { view: testObject }).view", "null");
40 shouldBe("new KeyboardEvent('eventType', { view: document }).view", "null");
41 shouldBe("new KeyboardEvent('eventType', { view: undefined }).view", "null");
42 shouldBe("new KeyboardEvent('eventType', { view: null }).view", "null");
43 shouldBe("new KeyboardEvent('eventType', { view: false }).view", "null");
44 shouldBe("new KeyboardEvent('eventType', { view: true }).view", "null");
45 shouldBe("new KeyboardEvent('eventType', { view: '' }).view", "null");
46 shouldBe("new KeyboardEvent('eventType', { view: 'chocolate' }).view", "null");
47 shouldBe("new KeyboardEvent('eventType', { view: 12345 }).view", "null");
48 shouldBe("new KeyboardEvent('eventType', { view: 18446744073709551615 }).view", "null");
49 shouldBe("new KeyboardEvent('eventType', { view: NaN }).view", "null");
50 // Note that valueOf() is not called, when the left hand side is evaluated.
51 shouldBeFalse("new KeyboardEvent('eventType', { view: {valueOf: function () { return window; } } }).view == window");
52 shouldBe("new KeyboardEvent('eventType', { get view() { return 123; } }).view", "null");
53 shouldThrow("new KeyboardEvent('eventType', { get view() { throw 'KeyboardEvent Error'; } })");
54
55 // keyIdentifier is passed.
56 // Strings.
57 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: 'koakuma' }).keyIdentifier", "koakuma");
58 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: '' }).keyIdentifier", "");
59
60 // Non-strings.
61 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: undefined }).keyIdentifier", "undefined");
62 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: null }).keyIdentifier", "null");
63 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: false }).keyIdentifier", "false");
64 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: true }).keyIdentifier", "true");
65 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: 12345 }).keyIdentifier", "12345");
66 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: 18446744073709551615 }).keyIdentifier", "18446744073709552000");
67 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: NaN }).keyIdentifier", "NaN");
68 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: [] }).keyIdentifier", "");
69 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: [1, 2, 3] }).keyIdentifier", "1,2,3");
70 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: {koakuma: 12345} }).keyIdentifier", "[object Object]");
71 shouldBeEqualToString("new KeyboardEvent('eventType', { keyIdentifier: {valueOf: function () { return 'koakuma'; } } }).keyIdentifier", "[object Object]");
72
73 // location / keyLocation is passed.
74 ["location", "keyLocation"].forEach(function (propertyName) {
75     // numbers within the unsigned long range.
76     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": KeyboardEvent.DOM_KEY_LOCATION_STANDARD })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
77     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": KeyboardEvent.DOM_KEY_LOCATION_LEFT })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_LEFT");
78     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": 4294967294 })." + propertyName, "4294967294");
79     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": 4294967295 })." + propertyName, "4294967295");
80
81     // numbers out of the unsigned long range.
82     // 2^{53}-1, the largest number that can be exactly represented by double.
83     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": 9007199254740991 })." + propertyName, "4294967295");
84     // 2^{64}-1
85     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": 18446744073709551615 })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
86     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": 12345678901234567890 })." + propertyName, "3944679424");
87     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": -1 })." + propertyName, "4294967295");
88     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": 123.45 })." + propertyName, "123");
89     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": NaN })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
90
91     // Non-numeric values.
92     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": undefined })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
93     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": null })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
94     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": '' })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
95     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": '12345' })." + propertyName, "12345");
96     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": '12345a' })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
97     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": 'abc' })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
98     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": [] })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
99     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": [12345] })." + propertyName, "12345");
100     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": [12345, 67890] })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
101     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": {} })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
102     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": {moemoe: 12345} })." + propertyName, "KeyboardEvent.DOM_KEY_LOCATION_STANDARD");
103     shouldBe("new KeyboardEvent('eventType', { " + propertyName + ": {valueOf: function () { return 12345; }} })." + propertyName, "12345");
104 });
105 shouldBe("new KeyboardEvent('eventType', { keyLocation: KeyboardEvent.DOM_KEY_LOCATION_LEFT }).location", "KeyboardEvent.DOM_KEY_LOCATION_LEFT");
106 shouldBe("new KeyboardEvent('eventType', { location: KeyboardEvent.DOM_KEY_LOCATION_LEFT }).keyLocation", "KeyboardEvent.DOM_KEY_LOCATION_LEFT");
107
108 // ctrlKey, altKey, shiftKey and metaKey are passed.
109 ["ctrlKey", "altKey", "shiftKey", "metaKey"].forEach(function (attr) {
110     shouldBe("new KeyboardEvent('eventType', { " + attr + ": false })." + attr, "false");
111     shouldBe("new KeyboardEvent('eventType', { " + attr + ": true })." + attr, "true");
112 });
113
114 // All initializers are passed.
115 shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, keyIdentifier: 'chocolate', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true }).bubbles", "true");
116 shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, keyIdentifier: 'chocolate', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true }).cancelable", "true");
117 shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, keyIdentifier: 'chocolate', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true }).view", "window");
118 shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, keyIdentifier: 'chocolate', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true }).detail", "111");
119 shouldBeEqualToString("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, keyIdentifier: 'chocolate', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true }).keyIdentifier", "chocolate");
120 shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, keyIdentifier: 'chocolate', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true }).location", "222");
121 shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, keyIdentifier: 'chocolate', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true }).ctrlKey", "true");
122 shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, keyIdentifier: 'chocolate', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true }).altKey", "true");
123 shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, keyIdentifier: 'chocolate', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true }).shiftKey", "true");
124 shouldBe("new KeyboardEvent('eventType', { bubbles: true, cancelable: true, view: window, detail: 111, keyIdentifier: 'chocolate', location: 222, ctrlKey: true, altKey: true, shiftKey: true, metaKey: true }).metaKey", "true");
125 </script>
126 <script src="../../js/resources/js-test-post.js"></script>
127 </body>
128 </html>