| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 EventCustomEventTest; | 5 library EventCustomEventTest; |
| 6 | 6 |
| 7 import '../../pkg/unittest/lib/unittest.dart'; | 7 import '../../pkg/unittest/lib/unittest.dart'; |
| 8 import '../../pkg/unittest/lib/html_config.dart'; | 8 import '../../pkg/unittest/lib/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 | |
| 12 // optional arguments. | |
| 13 eventTest(String name, Event eventFn(), void validate(Event), | |
| 14 [String type = 'foo']) { | |
| 15 test(name, () { | |
| 16 final el = new Element.tag('div'); | |
| 17 var fired = false; | |
| 18 el.on[type].add((ev) { | |
| 19 fired = true; | |
| 20 validate(ev); | |
| 21 }); | |
| 22 el.on[type].dispatch(eventFn()); | |
| 23 expect(fired, isTrue, reason: 'Expected event to be dispatched.'); | |
| 24 }); | |
| 25 } | |
| 26 | |
| 27 main() { | 11 main() { |
| 28 useHtmlConfiguration(); | 12 useHtmlConfiguration(); |
| 29 | 13 |
| 30 test('custom events', () { | 14 test('custom events', () { |
| 31 var provider = new EventStreamProvider<CustomEvent>('foo'); | 15 var provider = new EventStreamProvider<CustomEvent>('foo'); |
| 32 var el = new DivElement(); | 16 var el = new DivElement(); |
| 33 | 17 |
| 34 var fired = false; | 18 var fired = false; |
| 35 provider.forTarget(el).listen((ev) { | 19 provider.forTarget(el).listen((ev) { |
| 36 fired = true; | 20 fired = true; |
| 37 expect(ev.detail, 'detail'); | 21 expect(ev.detail, {'type': 'detail'}); |
| 38 }); | 22 }); |
| 39 | 23 |
| 40 var ev = new CustomEvent('foo', canBubble: false, cancelable: false, | 24 var ev = new CustomEvent('foo', canBubble: false, cancelable: false, |
| 41 detail: 'detail'); | 25 detail: {'type': 'detail'}); |
| 42 el.dispatchEvent(ev); | 26 el.dispatchEvent(ev); |
| 43 expect(fired, isTrue); | 27 expect(fired, isTrue); |
| 44 }); | 28 }); |
| 29 |
| 30 test('custom events from JS', () { |
| 31 var scriptContents = ''' |
| 32 var event = document.createEvent("CustomEvent"); |
| 33 event.initCustomEvent("js_custom_event", true, true, {type: "detail"}); |
| 34 window.dispatchEvent(event); |
| 35 '''; |
| 36 |
| 37 var fired = false; |
| 38 window.on['js_custom_event'].listen((ev) { |
| 39 fired = true; |
| 40 expect(ev.detail, {'type': 'detail'}); |
| 41 }); |
| 42 |
| 43 var script = new ScriptElement(); |
| 44 script.text = scriptContents; |
| 45 document.body.append(script); |
| 46 |
| 47 expect(fired, isTrue); |
| 48 }); |
| 45 } | 49 } |
| OLD | NEW |