| 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 class DartPayloadData { |
| 12 final dartValue; |
| 13 |
| 14 DartPayloadData(this.dartValue); |
| 15 } |
| 16 |
| 11 main() { | 17 main() { |
| 12 useHtmlConfiguration(); | 18 useHtmlConfiguration(); |
| 13 | 19 |
| 14 test('custom events', () { | 20 test('custom events', () { |
| 15 var provider = new EventStreamProvider<CustomEvent>('foo'); | 21 var provider = new EventStreamProvider<CustomEvent>('foo'); |
| 16 var el = new DivElement(); | 22 var el = new DivElement(); |
| 17 | 23 |
| 18 var fired = false; | 24 var fired = false; |
| 19 provider.forTarget(el).listen((ev) { | 25 provider.forTarget(el).listen((ev) { |
| 20 fired = true; | 26 fired = true; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 39 fired = true; | 45 fired = true; |
| 40 expect(ev.detail, {'type': 'detail'}); | 46 expect(ev.detail, {'type': 'detail'}); |
| 41 }); | 47 }); |
| 42 | 48 |
| 43 var script = new ScriptElement(); | 49 var script = new ScriptElement(); |
| 44 script.text = scriptContents; | 50 script.text = scriptContents; |
| 45 document.body.append(script); | 51 document.body.append(script); |
| 46 | 52 |
| 47 expect(fired, isTrue); | 53 expect(fired, isTrue); |
| 48 }); | 54 }); |
| 55 |
| 56 test('custom events to JS', () { |
| 57 var scriptContents = ''' |
| 58 window.addEventListener('dart_custom_event', function(e) { |
| 59 if (e.detail == 'dart_message') { |
| 60 e.preventDefault(); |
| 61 } |
| 62 }, false);'''; |
| 63 |
| 64 document.body.append(new ScriptElement()..text = scriptContents); |
| 65 |
| 66 var event = new CustomEvent('dart_custom_event', detail: 'dart_message'); |
| 67 window.dispatchEvent(event); |
| 68 expect(event.defaultPrevented, isTrue); |
| 69 }); |
| 70 |
| 71 test('custom data to Dart', () { |
| 72 var data = new DartPayloadData(666); |
| 73 var event = new CustomEvent('dart_custom_data_event', detail: data); |
| 74 |
| 75 var future = window.on['dart_custom_data_event'].first.then((_) { |
| 76 expect(event.detail.dartValue, 666); |
| 77 }); |
| 78 |
| 79 document.body.dispatchEvent(event); |
| 80 return future; |
| 81 }); |
| 49 } | 82 } |
| OLD | NEW |