OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 jsTest; | 5 library jsTest; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:html'; | 8 import 'dart:html'; |
9 import 'dart:typed_data' show ByteBuffer, Int32List; | 9 import 'dart:typed_data' show ByteBuffer, Int32List; |
10 import 'dart:indexed_db' show IdbFactory, KeyRange; | 10 import 'dart:indexed_db' show IdbFactory, KeyRange; |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 Color._(this._value); | 221 Color._(this._value); |
222 String toJs() => this._value; | 222 String toJs() => this._value; |
223 } | 223 } |
224 | 224 |
225 class TestDartObject {} | 225 class TestDartObject {} |
226 | 226 |
227 class Callable { | 227 class Callable { |
228 call() => 'called'; | 228 call() => 'called'; |
229 } | 229 } |
230 | 230 |
| 231 typedef ThreeArgFunction(a, b, c); |
| 232 |
231 main() { | 233 main() { |
232 _injectJs(); | 234 _injectJs(); |
233 useHtmlConfiguration(); | 235 useHtmlConfiguration(); |
234 | 236 |
235 group('identity', () { | 237 group('identity', () { |
236 | 238 |
237 test('context instances should be identical', () { | 239 test('context instances should be identical', () { |
238 var c1 = context; | 240 var c1 = context; |
239 var c2 = context; | 241 var c2 = context; |
240 expect(identical(c1, c2), isTrue); | 242 expect(identical(c1, c2), isTrue); |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 group('Dart functions', () { | 654 group('Dart functions', () { |
653 test('invoke Dart callback from JS', () { | 655 test('invoke Dart callback from JS', () { |
654 expect(() => context.callMethod('invokeCallback'), throws); | 656 expect(() => context.callMethod('invokeCallback'), throws); |
655 | 657 |
656 context['callback'] = () => 42; | 658 context['callback'] = () => 42; |
657 expect(context.callMethod('invokeCallback'), equals(42)); | 659 expect(context.callMethod('invokeCallback'), equals(42)); |
658 | 660 |
659 context.deleteProperty('callback'); | 661 context.deleteProperty('callback'); |
660 }); | 662 }); |
661 | 663 |
| 664 test('invoke Dart callback from JS in a Zone', () { |
| 665 expect(() => context.callMethod('invokeCallback'), throws); |
| 666 |
| 667 var childZone = Zone.current.fork(); |
| 668 childZone.run(() { |
| 669 context['callback'] = () { |
| 670 expect(Zone.current, childZone); |
| 671 }; |
| 672 }); |
| 673 context.callMethod('invokeCallback'); |
| 674 |
| 675 context.deleteProperty('callback'); |
| 676 }); |
| 677 |
662 test('callback as parameter', () { | 678 test('callback as parameter', () { |
663 expect(context.callMethod('getTypeOf', [context['razzle']]), | 679 expect(context.callMethod('getTypeOf', [context['razzle']]), |
664 equals("function")); | 680 equals("function")); |
665 }); | 681 }); |
666 | 682 |
667 test('invoke Dart callback from JS with this', () { | 683 test('invoke Dart callback from JS with this', () { |
668 // A JavaScript constructor function implemented in Dart which | 684 // A JavaScript constructor function implemented in Dart which |
669 // uses 'this' | 685 // uses 'this' |
670 final constructor = new JsFunction.withThis(($this, arg1) { | 686 final constructor = new JsFunction.withThis(($this, arg1) { |
671 var t = $this; | 687 var t = $this; |
(...skipping 15 matching lines...) Expand all Loading... |
687 expect(result, 42); | 703 expect(result, 42); |
688 }); | 704 }); |
689 | 705 |
690 test('emulated functions should be callable in JS', () { | 706 test('emulated functions should be callable in JS', () { |
691 context['callable'] = new Callable(); | 707 context['callable'] = new Callable(); |
692 var result = context.callMethod('callable'); | 708 var result = context.callMethod('callable'); |
693 expect(result, 'called'); | 709 expect(result, 'called'); |
694 context.deleteProperty('callable'); | 710 context.deleteProperty('callable'); |
695 }); | 711 }); |
696 | 712 |
| 713 test('Dart closures should survive a round-trip to JS', () { |
| 714 context['callback'] = (a, b, c) => a + b + c; |
| 715 var f = context['callback']; |
| 716 expect(f, new isInstanceOf<ThreeArgFunction>()); |
| 717 context.deleteProperty('callback'); |
| 718 }); |
| 719 |
697 }); | 720 }); |
698 | 721 |
699 group('JsObject.jsify()', () { | 722 group('JsObject.jsify()', () { |
700 | 723 |
701 test('convert a List', () { | 724 test('convert a List', () { |
702 final list = [1, 2, 3, 4, 5, 6, 7, 8]; | 725 final list = [1, 2, 3, 4, 5, 6, 7, 8]; |
703 var array = new JsObject.jsify(list); | 726 var array = new JsObject.jsify(list); |
704 expect(context.callMethod('isArray', [array]), isTrue); | 727 expect(context.callMethod('isArray', [array]), isTrue); |
705 expect(array['length'], equals(list.length)); | 728 expect(array['length'], equals(list.length)); |
706 for (var i = 0; i < list.length ; i++) { | 729 for (var i = 0; i < list.length ; i++) { |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1005 // expect(context.callMethod('isPropertyInstanceOf', ['o', listType]), | 1028 // expect(context.callMethod('isPropertyInstanceOf', ['o', listType]), |
1006 // isTrue); | 1029 // isTrue); |
1007 context.deleteProperty('o'); | 1030 context.deleteProperty('o'); |
1008 } | 1031 } |
1009 }); | 1032 }); |
1010 | 1033 |
1011 }); | 1034 }); |
1012 }); | 1035 }); |
1013 | 1036 |
1014 } | 1037 } |
OLD | NEW |