OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library EventTest; | 5 library EventTest; |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 import 'package:unittest/html_config.dart'; | 8 import 'package:unittest/html_config.dart'; |
9 import 'dart:html'; | 9 import 'dart:html'; |
10 | 10 |
11 // TODO(nweiz): Make this private to testEvents when Frog supports closures with | 11 // TODO(nweiz): Make this private to testEvents when Frog supports closures with |
12 // optional arguments. | 12 // optional arguments. |
13 eventTest(String name, Event eventFn(), void validate(Event), | 13 eventTest(String name, Event eventFn(), void validate(Event), |
14 [String type = 'foo']) { | 14 [String type = 'foo']) { |
15 test(name, () { | 15 test(name, () { |
16 final el = new Element.tag('div'); | 16 final el = new Element.tag('div'); |
17 var fired = false; | 17 var fired = false; |
18 el.on[type].add((ev) { | 18 el.on[type].listen((ev) { |
19 fired = true; | 19 fired = true; |
20 validate(ev); | 20 validate(ev); |
21 }); | 21 }); |
22 el.on[type].dispatch(eventFn()); | 22 el.dispatchEvent(eventFn()); |
23 expect(fired, isTrue, reason: 'Expected event to be dispatched.'); | 23 expect(fired, isTrue, reason: 'Expected event to be dispatched.'); |
24 }); | 24 }); |
25 } | 25 } |
26 | 26 |
27 main() { | 27 main() { |
28 useHtmlConfiguration(); | 28 useHtmlConfiguration(); |
29 | 29 |
30 // Issue 1005. | 30 // Issue 1005. |
31 // eventTest('AnimationEvent', () => new AnimationEvent('foo', 'color', 0.5), | 31 // eventTest('AnimationEvent', () => new AnimationEvent('foo', 'color', 0.5), |
32 // (ev) { | 32 // (ev) { |
33 // expect(ev.animationName, 'color'); | 33 // expect(ev.animationName, 'color'); |
34 // expect(ev.elapsedTime, 0.5); | 34 // expect(ev.elapsedTime, 0.5); |
35 // }); | 35 // }); |
36 | 36 |
37 // Issue 1005. | 37 // Issue 1005. |
38 // eventTest('BeforeLoadEvent', | 38 // eventTest('BeforeLoadEvent', |
39 // () => new BeforeLoadEvent('foo', 'http://example.url'), | 39 // () => new BeforeLoadEvent('foo', 'http://example.url'), |
40 // (ev) { expect(ev.url, 'http://example.url'); }); | 40 // (ev) { expect(ev.url, 'http://example.url'); }); |
41 | 41 |
42 // Issue 1005. | 42 // Issue 1005. |
43 // eventTest('CloseEvent', | 43 // eventTest('CloseEvent', |
44 // () => new CloseEvent('foo', 5, 'reason', wasClean: true), | 44 // () => new CloseEvent('foo', 5, 'reason', wasClean: true), |
45 // (ev) { | 45 // (ev) { |
46 // expect(ev.code, 5); | 46 // expect(ev.code, 5); |
47 // expect(ev.reason, 'reason'); | 47 // expect(ev.reason, 'reason'); |
48 // expect(ev.wasClean, isTrue); | 48 // expect(ev.wasClean, isTrue); |
49 // }); | 49 // }); |
50 | 50 |
51 eventTest('CompositionEvent', | 51 eventTest('CompositionEvent', |
52 () => new CompositionEvent('compositionstart', window, 'data'), | 52 () => new CompositionEvent('compositionstart', view: window, data: 'data')
, |
53 (ev) { expect(ev.data, 'data'); }, | 53 (ev) { expect(ev.data, 'data'); }, |
54 type: 'compositionstart'); | 54 'compositionstart'); |
55 | 55 |
56 // initCustomEvent is not yet implemented | 56 // initCustomEvent is not yet implemented |
57 // eventTest('CustomEvent', | 57 // eventTest('CustomEvent', |
58 // () => new CustomEvent('foo', false, false, 'detail'), | 58 // () => new CustomEvent('foo', false, false, 'detail'), |
59 // (ev) { expect(ev.detail, 'detail'); }); | 59 // (ev) { expect(ev.detail, 'detail'); }); |
60 | 60 |
61 // DeviceMotionEvent has no properties to itself, so just test that it doesn't | 61 // DeviceMotionEvent has no properties to itself, so just test that it doesn't |
62 // error out on creation and can be dispatched. | 62 // error out on creation and can be dispatched. |
63 eventTest('DeviceMotionEvent', () => new DeviceMotionEvent('foo'), (ev) {}); | 63 // Suppress. DeviceMotion has no constructor, and I don't think it can be |
| 64 // created on a non-mobile device. Issue 23321 |
| 65 // eventTest('DeviceMotionEvent', () => new DeviceMotionEvent('foo'), (ev) {})
; |
64 | 66 |
65 // initDeviceOrientationEvent is not yet implemented | 67 // initDeviceOrientationEvent is not yet implemented |
66 // eventTest('DeviceOrientationEvent', | 68 // eventTest('DeviceOrientationEvent', |
67 // () => new DeviceOrientationEvent('foo', 0.1, 0.2, 0.3), | 69 // () => new DeviceOrientationEvent('foo', 0.1, 0.2, 0.3), |
68 // (ev) { | 70 // (ev) { |
69 // expect(ev.alpha, 0.1); | 71 // expect(ev.alpha, 0.1); |
70 // expect(ev.beta, 0.2); | 72 // expect(ev.beta, 0.2); |
71 // expect(ev.gamma, 0.3); | 73 // expect(ev.gamma, 0.3); |
72 // }); | 74 // }); |
73 | 75 |
74 // Issue 1005. | 76 // Issue 1005. |
75 // eventTest('ErrorEvent', | 77 // eventTest('ErrorEvent', |
76 // () => new ErrorEvent('foo', 'message', 'filename', 10), | 78 // () => new ErrorEvent('foo', 'message', 'filename', 10), |
77 // (ev) { | 79 // (ev) { |
78 // expect('message', ev.message); | 80 // expect('message', ev.message); |
79 // expect('filename', ev.filename); | 81 // expect('filename', ev.filename); |
80 // expect(ev.lineno, 10); | 82 // expect(ev.lineno, 10); |
81 // }); | 83 // }); |
82 | 84 |
83 eventTest('Event', | 85 eventTest('Event', |
84 () => new Event('foo', canBubble: false, cancelable: false), | 86 () => new Event('foo', canBubble: false, cancelable: false), |
85 (ev) { | 87 (ev) { |
86 expect(ev.type, equals('foo')); | 88 expect(ev.type, equals('foo')); |
87 expect(ev.bubbles, isFalse); | 89 expect(ev.bubbles, isFalse); |
88 expect(ev.cancelable, isFalse); | 90 expect(ev.cancelable, isFalse); |
89 }); | 91 }); |
90 | 92 |
91 eventTest('HashChangeEvent', | 93 eventTest('HashChangeEvent', |
92 () => new HashChangeEvent('foo', 'http://old.url', 'http://new.url'), | 94 () => new HashChangeEvent('foo', oldUrl: 'http://old.url', |
93 (ev) { | 95 newUrl: 'http://new.url'), (ev) { |
94 expect(ev.oldUrl, equals('http//old.url')); | 96 expect(ev.oldUrl, equals('http://old.url')); |
95 expect(ev.newUrl, equals('http://new.url')); | 97 expect(ev.newUrl, equals('http://new.url')); |
96 }); | 98 }); |
97 | 99 |
98 eventTest('KeyboardEvent', | 100 // KeyboardEvent has its own test file, and has cross-browser issues. |
99 () => new KeyboardEvent('foo', window, 'key', 10, ctrlKey: true, | |
100 altKey: true, shiftKey: true, metaKey: true, altGraphKey: true), | |
101 (ev) { | |
102 expect.equals(ev.keyIdentifier, equals('key')); | |
103 expect.equals(ev.keyLocation, equals(10)); | |
104 expect(ev.ctrlKey, isTrue); | |
105 expect(ev.altKey, isTrue); | |
106 expect(ev.shiftKey, isTrue); | |
107 expect(ev.metaKey, isTrue); | |
108 expect(ev.altGraphKey, isTrue); | |
109 }); | |
110 | 101 |
111 eventTest('MouseEvent', | 102 eventTest('MouseEvent', |
112 // canBubble and cancelable are currently required to avoid dartc | 103 // canBubble and cancelable are currently required to avoid dartc |
113 // complaining about the types of the named arguments. | 104 // complaining about the types of the named arguments. |
114 () => new MouseEvent('foo', window, 1, 2, 3, 4, 5, 6, canBubble: true, | 105 () => new MouseEvent('foo', view: window, detail: 1, screenX: 2, |
| 106 screenY: 3, clientX: 4, clientY: 5, button: 6, canBubble: true, |
115 cancelable: true, ctrlKey: true, altKey: true, shiftKey: true, | 107 cancelable: true, ctrlKey: true, altKey: true, shiftKey: true, |
116 metaKey: true, relatedTarget: new Element.tag('div')), | 108 metaKey: true, relatedTarget: document.body), |
117 (ev) { | 109 (ev) { |
118 expect(ev.detail, 1); | 110 expect(ev.detail, 1); |
119 expect(ev.screen.x, 2); | 111 expect(ev.screen.x, 2); |
120 expect(ev.screen.y, 3); | 112 expect(ev.screen.y, 3); |
121 expect(ev.client.x, 4); | 113 expect(ev.client.x, 4); |
122 expect(ev.client.y, 5); | 114 expect(ev.client.y, 5); |
123 expect(ev.offset.x, 4); // Same as clientX. | 115 expect(ev.offset.x, 4); // Same as clientX. |
124 expect(ev.offset.y, 5); // Same as clientY. | 116 expect(ev.offset.y, 5); // Same as clientY. |
125 expect(ev.button, 6); | 117 expect(ev.button, 6); |
126 expect(ev.ctrlKey, isTrue); | 118 expect(ev.ctrlKey, isTrue); |
127 expect(ev.altKey, isTrue); | 119 expect(ev.altKey, isTrue); |
128 expect(ev.shiftKey, isTrue); | 120 expect(ev.shiftKey, isTrue); |
129 expect(ev.metaKey, isTrue); | 121 expect(ev.metaKey, isTrue); |
130 expect(ev.relatedTarget.tagName, 'DIV'); | 122 expect(ev.relatedTarget, document.body); |
131 }); | |
132 | |
133 eventTest('MutationEvent', | |
134 () => new MutationEvent('foo', new Element.tag('div'), 'red', 'blue', | |
135 'color', MutationEvent.MODIFICATION), | |
136 (ev) { | |
137 expect(ev.relatedNode.tagName, 'DIV'); | |
138 expect.equals(ev.prevValue, 'red'); | |
139 expect.equals(ev.newValue, 'blue'); | |
140 expect.equals(ev.attrName, 'color'); | |
141 expect.equals(ev.attrChange, equals(MutationEvent.MODIFICATION)); | |
142 }); | |
143 | |
144 test('DOMMutationEvent', () { | |
145 var div = new DivElement(); | |
146 div.on['DOMSubtreeModified'].add(expectAsync((DOMMutationEvent e) {})); | |
147 div.append(new SpanElement()); | |
148 }); | 123 }); |
149 | 124 |
150 // Issue 1005. | 125 // Issue 1005. |
151 // eventTest('OverflowEvent', | 126 // eventTest('OverflowEvent', |
152 // () => new OverflowEvent(OverflowEvent.BOTH, true, true), | 127 // () => new OverflowEvent(OverflowEvent.BOTH, true, true), |
153 // (ev) { | 128 // (ev) { |
154 // expect(ev.orient, OverflowEvent.BOTH); | 129 // expect(ev.orient, OverflowEvent.BOTH); |
155 // expect(ev.horizontalOverflow, isTrue); | 130 // expect(ev.horizontalOverflow, isTrue); |
156 // expect(ev.verticalOverflow, isTrue); | 131 // expect(ev.verticalOverflow, isTrue); |
157 // }, type: 'overflowchanged'); | 132 // }, type: 'overflowchanged'); |
(...skipping 13 matching lines...) Expand all Loading... |
171 // // complaining about the types of the named arguments. | 146 // // complaining about the types of the named arguments. |
172 // () => new ProgressEvent('foo', 5, canBubble: true, cancelable: true, | 147 // () => new ProgressEvent('foo', 5, canBubble: true, cancelable: true, |
173 // lengthComputable: true, total: 10), | 148 // lengthComputable: true, total: 10), |
174 // (ev) { | 149 // (ev) { |
175 // expect(ev.loaded, 5); | 150 // expect(ev.loaded, 5); |
176 // expect(ev.lengthComputable, isTrue); | 151 // expect(ev.lengthComputable, isTrue); |
177 // expect(ev.total, 10); | 152 // expect(ev.total, 10); |
178 // }); | 153 // }); |
179 | 154 |
180 eventTest('StorageEvent', | 155 eventTest('StorageEvent', |
181 () => new StorageEvent('foo', 'key', 'http://example.url', | 156 () => new StorageEvent('foo', key: 'key', url: 'http://example.url', |
182 window.localStorage, canBubble: true, cancelable: true, | 157 storageArea: window.localStorage, canBubble: true, cancelable: true, |
183 oldValue: 'old', newValue: 'new'), | 158 oldValue: 'old', newValue: 'new'), |
184 (ev) { | 159 (ev) { |
185 expect(ev.key, 'key'); | 160 expect(ev.key, 'key'); |
186 expect(ev.url, 'http://example.url'); | 161 expect(ev.url, 'http://example.url'); |
187 // Equality isn't preserved for storageArea | 162 // Equality isn't preserved for storageArea |
188 expect.isNotNull(ev.storageArea); | 163 expect(ev.storageArea, isNotNull); |
189 expect(ev.oldValue, 'old'); | 164 expect(ev.oldValue, 'old'); |
190 expect(ev.newValue, 'new'); | 165 expect(ev.newValue, 'new'); |
191 }); | 166 }); |
192 | 167 |
193 eventTest('TextEvent', () => new TextEvent('foo', window, 'data'), | |
194 (ev) { expect(ev.data, 'data'); }); | |
195 | |
196 // Issue 1005. | 168 // Issue 1005. |
197 // eventTest('TransitionEvent', () => new TransitionEvent('foo', 'color', 0.5)
, | 169 // eventTest('TransitionEvent', () => new TransitionEvent('foo', 'color', 0.5)
, |
198 // (ev) { | 170 // (ev) { |
199 // expect(ev.propertyName, 'color'); | 171 // expect(ev.propertyName, 'color'); |
200 // expect(ev.elapsedTime, 0.5); | 172 // expect(ev.elapsedTime, 0.5); |
201 // }); | 173 // }); |
202 | 174 |
203 eventTest('UIEvent', () => new UIEvent('foo', window, 12), | 175 eventTest('UIEvent', () => new UIEvent('foo', view: window, detail: 12), |
204 (ev) { | 176 (ev) { |
205 expect(window, ev.view, window); | 177 expect(window, ev.view); |
206 expect(12, ev.detail, 12); | 178 expect(12, ev.detail); |
207 }); | 179 }); |
208 | 180 |
209 eventTest('WheelEvent', | 181 eventTest('WheelEvent', |
210 () => new WheelEvent(1, 2, window, 3, 4, 5, 6, ctrlKey: true, | 182 () => new WheelEvent("mousewheel", view: window, deltaX: 1, deltaY: 0, |
211 altKey: true, shiftKey: true, metaKey: true), | 183 detail: 4, screenX: 3, screenY: 4, clientX: 5, clientY: 6, |
| 184 ctrlKey: true, altKey: true, shiftKey: true, metaKey: true), |
212 (ev) { | 185 (ev) { |
213 // wheelDelta* properties are multiplied by 120 for some reason | 186 expect(ev.deltaX, 1); |
214 expect(ev.wheelDeltaX, 120); | 187 expect(ev.deltaY, 0); |
215 expect(ev.wheelDeltaY, 240); | |
216 expect(ev.screen.x, 3); | 188 expect(ev.screen.x, 3); |
217 expect(ev.screen.y, 4); | 189 expect(ev.screen.y, 4); |
218 expect(ev.client.x, 5); | 190 expect(ev.client.x, 5); |
219 expect(ev.client.y, 6); | 191 expect(ev.client.y, 6); |
220 expect(ev.ctrlKey, isTrue); | 192 expect(ev.ctrlKey, isTrue); |
221 expect(ev.altKey, isTrue); | 193 expect(ev.altKey, isTrue); |
222 expect(ev.shiftKey, isTrue); | 194 expect(ev.shiftKey, isTrue); |
223 expect(ev.metaKey, isTrue); | 195 expect(ev.metaKey, isTrue); |
224 }, type: 'mousewheel'); | 196 }, 'mousewheel'); |
225 } | 197 } |
OLD | NEW |