OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library EventCustomEventTest; |
| 6 |
| 7 import 'package:unittest/unittest.dart'; |
| 8 import 'package:unittest/html_config.dart'; |
| 9 import 'dart:html'; |
| 10 import 'dart:js' as js; |
| 11 |
| 12 class DartPayloadData { |
| 13 final dartValue; |
| 14 |
| 15 DartPayloadData(this.dartValue); |
| 16 } |
| 17 |
| 18 main() { |
| 19 useHtmlConfiguration(); |
| 20 |
| 21 test('custom events', () { |
| 22 var provider = new EventStreamProvider<CustomEvent>('foo'); |
| 23 var el = new DivElement(); |
| 24 |
| 25 var fired = false; |
| 26 provider.forTarget(el).listen((ev) { |
| 27 fired = true; |
| 28 expect(ev.detail, {'type': 'detail'}); |
| 29 }); |
| 30 |
| 31 var ev = new CustomEvent('foo', canBubble: false, cancelable: false, |
| 32 detail: {'type': 'detail'}); |
| 33 el.dispatchEvent(ev); |
| 34 expect(fired, isTrue); |
| 35 }); |
| 36 |
| 37 test('custom events from JS', () { |
| 38 var scriptContents = ''' |
| 39 var event = document.createEvent("CustomEvent"); |
| 40 event.initCustomEvent("js_custom_event", true, true, {type: "detail"}); |
| 41 window.dispatchEvent(event); |
| 42 '''; |
| 43 |
| 44 var fired = false; |
| 45 window.on['js_custom_event'].listen((ev) { |
| 46 fired = true; |
| 47 expect(ev.detail, {'type': 'detail'}); |
| 48 }); |
| 49 |
| 50 var script = new ScriptElement(); |
| 51 script.text = scriptContents; |
| 52 document.body.append(script); |
| 53 |
| 54 expect(fired, isTrue); |
| 55 }); |
| 56 |
| 57 test('custom events to JS', () { |
| 58 expect(js.context['gotDartEvent'], isNull); |
| 59 var scriptContents = ''' |
| 60 window.addEventListener('dart_custom_event', function(e) { |
| 61 if (e.detail == 'dart_message') { |
| 62 e.preventDefault(); |
| 63 window.gotDartEvent = true; |
| 64 } |
| 65 window.console.log('here' + e.detail); |
| 66 }, false);'''; |
| 67 |
| 68 document.body.append(new ScriptElement()..text = scriptContents); |
| 69 |
| 70 var event = new CustomEvent('dart_custom_event', detail: 'dart_message'); |
| 71 window.dispatchEvent(event); |
| 72 expect(js.context['gotDartEvent'], isTrue); |
| 73 }); |
| 74 |
| 75 test('custom data to Dart', () { |
| 76 var data = new DartPayloadData(666); |
| 77 var event = new CustomEvent('dart_custom_data_event', detail: data); |
| 78 |
| 79 var future = window.on['dart_custom_data_event'].first.then((_) { |
| 80 expect(event.detail.dartValue, 666); |
| 81 }); |
| 82 |
| 83 document.body.dispatchEvent(event); |
| 84 return future; |
| 85 }); |
| 86 } |
OLD | NEW |