| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, 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 jsArrayTest; | |
| 6 | |
| 7 import 'dart:html'; | |
| 8 import 'dart:js'; | |
| 9 | |
| 10 import 'package:unittest/unittest.dart'; | |
| 11 import 'package:unittest/html_config.dart'; | |
| 12 | |
| 13 _injectJs() { | |
| 14 document.body.append(new ScriptElement() | |
| 15 ..type = 'text/javascript' | |
| 16 ..innerHtml = r""" | |
| 17 var foo = { | |
| 18 x: 3, | |
| 19 z: 40, // Not specified in typed Dart API so should fail in checked mode. | |
| 20 multiplyByX: function(arg) { return arg * this.x; }, | |
| 21 // This function can be torn off without having to bind this. | |
| 22 multiplyBy2: function(arg) { return arg * 2; } | |
| 23 }; | |
| 24 | |
| 25 var foob = { | |
| 26 x: 8, | |
| 27 y: "why", | |
| 28 multiplyByX: function(arg) { return arg * this.x; } | |
| 29 }; | |
| 30 | |
| 31 var bar = { | |
| 32 x: "foo", | |
| 33 multiplyByX: true | |
| 34 }; | |
| 35 | |
| 36 var selection = ["a", "b", "c", foo, bar]; | |
| 37 selection.doubleLength = function() { return this.length * 2; }; | |
| 38 """); | |
| 39 } | |
| 40 | |
| 41 abstract class Foo { | |
| 42 int get x; | |
| 43 set x(int v); | |
| 44 num multiplyByX(num y); | |
| 45 num multiplyBy2(num y); | |
| 46 } | |
| 47 | |
| 48 abstract class Foob extends Foo { | |
| 49 final String y; | |
| 50 } | |
| 51 | |
| 52 abstract class Bar { | |
| 53 String get x; | |
| 54 bool get multiplyByX; | |
| 55 } | |
| 56 | |
| 57 class Baz {} | |
| 58 | |
| 59 // This class shows the pattern used by APIs such as jQuery that add methods | |
| 60 // to Arrays. | |
| 61 abstract class Selection implements List { | |
| 62 num doubleLength(); | |
| 63 } | |
| 64 | |
| 65 Foo get foo => context['foo']; | |
| 66 Foob get foob => context['foob']; | |
| 67 Bar get bar => context['bar']; | |
| 68 Selection get selection => context['selection']; | |
| 69 | |
| 70 main() { | |
| 71 // Call experimental API to register Dart interfaces implemented by | |
| 72 // JavaScript classes. | |
| 73 registerJsInterfaces([Foo, Foob, Bar, Selection]); | |
| 74 | |
| 75 _injectJs(); | |
| 76 | |
| 77 useHtmlConfiguration(); | |
| 78 | |
| 79 group('property', () { | |
| 80 test('get', () { | |
| 81 expect(foo.x, equals(3)); | |
| 82 expect(foob.x, equals(8)); | |
| 83 expect(foob.y, equals("why")); | |
| 84 | |
| 85 // Exists in JS but not in API. | |
| 86 expect(() => foo.z, throws); | |
| 87 expect(bar.multiplyByX, isTrue); | |
| 88 }); | |
| 89 test('set', () { | |
| 90 foo.x = 42; | |
| 91 expect(foo.x, equals(42)); | |
| 92 // Property tagged as read only in typed API. | |
| 93 expect(() => foob.y = "bla", throws); | |
| 94 expect(() => foo.unknownName = 42, throws); | |
| 95 }); | |
| 96 }); | |
| 97 | |
| 98 group('method', () { | |
| 99 test('call', () { | |
| 100 foo.x = 100; | |
| 101 expect(foo.multiplyByX(4), equals(400)); | |
| 102 foob.x = 10; | |
| 103 expect(foob.multiplyByX(4), equals(40)); | |
| 104 }); | |
| 105 | |
| 106 test('tearoff', () { | |
| 107 foo.x = 10; | |
| 108 // TODO(jacobr): should we automatically bind "this" for tearoffs of JS | |
| 109 // objects? | |
| 110 JsFunction multiplyBy2 = foo.multiplyBy2; | |
| 111 expect(multiplyBy2(5), equals(10)); | |
| 112 }); | |
| 113 }); | |
| 114 | |
| 115 group('type check', () { | |
| 116 test('js interfaces', () { | |
| 117 expect(foo is JsObject, isTrue); | |
| 118 // Cross-casts are allowed. | |
| 119 expect(foo is Bar, isTrue); | |
| 120 expect(selection is JsArray, isTrue); | |
| 121 | |
| 122 // We do know at runtime whether something is a JsArray or not. | |
| 123 expect(foo is JsArray, isFalse); | |
| 124 }); | |
| 125 | |
| 126 test('dart interfaces', () { | |
| 127 expect(foo is Function, isFalse); | |
| 128 expect(selection is List, isTrue); | |
| 129 }); | |
| 130 }); | |
| 131 | |
| 132 group("registration", () { | |
| 133 test('repeated fails', () { | |
| 134 // The experimental registerJsInterfaces API has already been called so | |
| 135 // it cannot be called a second time. | |
| 136 expect(() => registerJsInterfaces([Baz]), throws); | |
| 137 }); | |
| 138 }); | |
| 139 } | |
| OLD | NEW |