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'; |
| 8 import 'dart:html'; |
| 9 import 'dart:typed_data' show ByteBuffer, Int32List; |
| 10 import 'dart:indexed_db' show IdbFactory, KeyRange; |
7 import 'dart:js'; | 11 import 'dart:js'; |
8 | 12 |
9 // TODO(jmesserly): get tests from package(s) instead. | 13 import 'package:unittest/unittest.dart'; |
10 import 'dom.dart'; | 14 import 'package:unittest/html_individual_config.dart'; |
11 import 'minitest.dart'; | 15 |
| 16 _injectJs() { |
| 17 final script = new ScriptElement(); |
| 18 script.type = 'text/javascript'; |
| 19 script.innerHtml = r""" |
| 20 var x = 42; |
| 21 |
| 22 var _x = 123; |
| 23 |
| 24 var myArray = ["value1"]; |
| 25 |
| 26 var foreignDoc = (function(){ |
| 27 var doc = document.implementation.createDocument("", "root", null); |
| 28 var element = doc.createElement('element'); |
| 29 element.setAttribute('id', 'abc'); |
| 30 doc.documentElement.appendChild(element); |
| 31 return doc; |
| 32 })(); |
| 33 |
| 34 function razzle() { |
| 35 return x; |
| 36 } |
| 37 |
| 38 function returnThis() { |
| 39 return this; |
| 40 } |
| 41 |
| 42 function getTypeOf(o) { |
| 43 return typeof(o); |
| 44 } |
| 45 |
| 46 function varArgs() { |
| 47 var args = arguments; |
| 48 var sum = 0; |
| 49 for (var i = 0; i < args.length; ++i) { |
| 50 sum += args[i]; |
| 51 } |
| 52 return sum; |
| 53 } |
| 54 |
| 55 function Foo(a) { |
| 56 this.a = a; |
| 57 } |
| 58 |
| 59 Foo.b = 38; |
| 60 |
| 61 Foo.prototype.bar = function() { |
| 62 return this.a; |
| 63 } |
| 64 Foo.prototype.toString = function() { |
| 65 return "I'm a Foo a=" + this.a; |
| 66 } |
| 67 |
| 68 var container = new Object(); |
| 69 container.Foo = Foo; |
| 70 |
| 71 function isArray(a) { |
| 72 return a instanceof Array; |
| 73 } |
| 74 |
| 75 function checkMap(m, key, value) { |
| 76 if (m.hasOwnProperty(key)) |
| 77 return m[key] == value; |
| 78 else |
| 79 return false; |
| 80 } |
| 81 |
| 82 function invokeCallback() { |
| 83 return callback(); |
| 84 } |
| 85 |
| 86 function invokeCallbackWith11params() { |
| 87 return callbackWith11params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); |
| 88 } |
| 89 |
| 90 function returnElement(element) { |
| 91 return element; |
| 92 } |
| 93 |
| 94 function getElementAttribute(element, attr) { |
| 95 return element.getAttribute(attr); |
| 96 } |
| 97 |
| 98 function addClassAttributes(list) { |
| 99 var result = ""; |
| 100 for (var i=0; i < list.length; i++) { |
| 101 result += list[i].getAttribute("class"); |
| 102 } |
| 103 return result; |
| 104 } |
| 105 |
| 106 function getNewDate() { |
| 107 return new Date(1995, 11, 17); |
| 108 } |
| 109 |
| 110 function getNewDivElement() { |
| 111 return document.createElement("div"); |
| 112 } |
| 113 |
| 114 function getNewEvent() { |
| 115 return new CustomEvent('test'); |
| 116 } |
| 117 |
| 118 function getNewBlob() { |
| 119 var fileParts = ['<a id="a"><b id="b">hey!</b></a>']; |
| 120 return new Blob(fileParts, {type : 'text/html'}); |
| 121 } |
| 122 |
| 123 function getNewIDBKeyRange() { |
| 124 return IDBKeyRange.only(1); |
| 125 } |
| 126 |
| 127 function getNewImageData() { |
| 128 var canvas = document.createElement('canvas'); |
| 129 var context = canvas.getContext('2d'); |
| 130 return context.createImageData(1, 1); |
| 131 } |
| 132 |
| 133 function getNewInt32Array() { |
| 134 return new Int32Array([1, 2, 3, 4, 5, 6, 7, 8]); |
| 135 } |
| 136 |
| 137 function getNewArrayBuffer() { |
| 138 return new ArrayBuffer(8); |
| 139 } |
| 140 |
| 141 function isPropertyInstanceOf(property, type) { |
| 142 return window[property] instanceof type; |
| 143 } |
| 144 |
| 145 function testJsMap(callback) { |
| 146 var result = callback(); |
| 147 return result['value']; |
| 148 } |
| 149 |
| 150 function addTestProperty(o) { |
| 151 o.testProperty = "test"; |
| 152 } |
| 153 |
| 154 function fireClickEvent(w) { |
| 155 var event = w.document.createEvent('Events'); |
| 156 event.initEvent('click', true, false); |
| 157 w.document.dispatchEvent(event); |
| 158 } |
| 159 |
| 160 function Bar() { |
| 161 return "ret_value"; |
| 162 } |
| 163 Bar.foo = "property_value"; |
| 164 |
| 165 function Baz(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11) { |
| 166 this.f1 = p1; |
| 167 this.f2 = p2; |
| 168 this.f3 = p3; |
| 169 this.f4 = p4; |
| 170 this.f5 = p5; |
| 171 this.f6 = p6; |
| 172 this.f7 = p7; |
| 173 this.f8 = p8; |
| 174 this.f9 = p9; |
| 175 this.f10 = p10; |
| 176 this.f11 = p11; |
| 177 } |
| 178 |
| 179 function Liar(){} |
| 180 |
| 181 Liar.prototype.toString = function() { |
| 182 return 1; |
| 183 } |
| 184 |
| 185 function identical(o1, o2) { |
| 186 return o1 === o2; |
| 187 } |
| 188 |
| 189 var someProto = { role: "proto" }; |
| 190 var someObject = Object.create(someProto); |
| 191 someObject.role = "object"; |
| 192 |
| 193 """; |
| 194 document.body.append(script); |
| 195 } |
| 196 |
| 197 // Some test are either causing other test to fail in IE9, or they are failing |
| 198 // for unknown reasons |
| 199 // useHtmlConfiguration+ImageData bug: dartbug.com/14355 |
| 200 skipIE9_test(String description, t()) { |
| 201 if (Platform.supportsTypedData) { |
| 202 test(description, t); |
| 203 } |
| 204 } |
12 | 205 |
13 class Foo { | 206 class Foo { |
14 final JsObject _proxy; | 207 final JsObject _proxy; |
15 | 208 |
16 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); | 209 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); |
17 | 210 |
18 JsObject toJs() => _proxy; | 211 JsObject toJs() => _proxy; |
19 | 212 |
20 num get a => _proxy['a']; | 213 num get a => _proxy['a']; |
21 num bar() => _proxy.callMethod('bar'); | 214 num bar() => _proxy.callMethod('bar'); |
22 } | 215 } |
23 | 216 |
24 class Color { | 217 class Color { |
25 static final RED = new Color._("red"); | 218 static final RED = new Color._("red"); |
26 static final BLUE = new Color._("blue"); | 219 static final BLUE = new Color._("blue"); |
27 String _value; | 220 String _value; |
28 Color._(this._value); | 221 Color._(this._value); |
29 String toJs() => this._value; | 222 String toJs() => this._value; |
30 } | 223 } |
31 | 224 |
32 class TestDartObject {} | 225 class TestDartObject {} |
33 | 226 |
34 class Callable { | 227 class Callable { |
35 call() => 'called'; | 228 call() => 'called'; |
36 } | 229 } |
37 | 230 |
38 main() { | 231 main() { |
| 232 _injectJs(); |
| 233 useHtmlIndividualConfiguration(); |
| 234 |
39 group('identity', () { | 235 group('identity', () { |
40 | 236 |
41 test('context instances should be identical', () { | 237 test('context instances should be identical', () { |
42 var c1 = context; | 238 var c1 = context; |
43 var c2 = context; | 239 var c2 = context; |
44 expect(identical(c1, c2), true); | 240 expect(identical(c1, c2), isTrue); |
45 }); | 241 }); |
46 | 242 |
47 // TODO(jacobr): switch from equals to identical when dartium supports | 243 test('identical JS objects should have identical proxies', () { |
48 // maintaining proxy equality. | 244 var o1 = new JsObject(context['Foo'], [1]); |
49 test('identical JS functions should have equal proxies', () { | 245 context['f1'] = o1; |
| 246 var o2 = context['f1']; |
| 247 expect(identical(o1, o2), isTrue); |
| 248 }); |
| 249 |
| 250 /* |
| 251 TODO(jacobr): enable this test when dartium supports maintaining proxy |
| 252 equality. |
| 253 test('identical Dart objects should have identical proxies', () { |
| 254 var o1 = new TestDartObject(); |
| 255 expect(context.callMethod('identical', [o1, o1]), isTrue); |
| 256 }); |
| 257 */ |
| 258 |
| 259 test('identical Dart functions should have identical proxies', () { |
| 260 var f1 = allowInterop(() => print("I'm a Function!")); |
| 261 expect(context.callMethod('identical', [f1, f1]), isTrue); |
| 262 }); |
| 263 |
| 264 test('identical JS functions should have identical proxies', () { |
50 var f1 = context['Object']; | 265 var f1 = context['Object']; |
51 var f2 = context['Object']; | 266 var f2 = context['Object']; |
52 expect(f1, equals(f2)); | 267 expect(identical(f1, f2), isTrue); |
53 }); | 268 }); |
54 | 269 |
55 // TODO(justinfagnani): old tests duplicate checks above, remove | 270 // TODO(justinfagnani): old tests duplicate checks above, remove |
56 // on test next cleanup pass | 271 // on test next cleanup pass |
57 test('test proxy equality', () { | 272 test('test proxy equality', () { |
58 var foo1 = new JsObject(context['Foo'], [1]); | 273 var foo1 = new JsObject(context['Foo'], [1]); |
59 var foo2 = new JsObject(context['Foo'], [2]); | 274 var foo2 = new JsObject(context['Foo'], [2]); |
60 context['foo1'] = foo1; | 275 context['foo1'] = foo1; |
61 context['foo2'] = foo2; | 276 context['foo2'] = foo2; |
62 expect(foo1, isNot(context['foo2'])); | 277 expect(foo1, isNot(equals(context['foo2']))); |
63 expect(foo2, context['foo2']); | 278 expect(foo2, equals(context['foo2'])); |
64 context.deleteProperty('foo1'); | 279 context.deleteProperty('foo1'); |
65 context.deleteProperty('foo2'); | 280 context.deleteProperty('foo2'); |
66 }); | 281 }); |
67 | 282 |
68 test('retrieve same dart Object', () { | 283 test('retrieve same dart Object', () { |
69 final obj = new Object(); | 284 final obj = new Object(); |
70 context['obj'] = obj; | 285 context['obj'] = obj; |
71 expect(context['obj'], same(obj)); | 286 expect(context['obj'], same(obj)); |
72 context.deleteProperty('obj'); | 287 context.deleteProperty('obj'); |
73 }); | 288 }); |
74 | 289 |
75 group('caching', () { | 290 group('caching', () { |
76 test('JS->Dart', () { | 291 test('JS->Dart', () { |
77 // Test that we are not pulling cached proxy from the prototype | 292 // Test that we are not pulling cached proxy from the prototype |
78 // when asking for a proxy for the object. | 293 // when asking for a proxy for the object. |
79 final proto = context['someProto']; | 294 final proto = context['someProto']; |
80 expect(proto['role'], 'proto'); | 295 expect(proto['role'], equals('proto')); |
81 final obj = context['someObject']; | 296 final obj = context['someObject']; |
82 expect(obj['role'], 'object'); | 297 expect(obj['role'], equals('object')); |
83 }); | 298 }); |
84 }); | 299 }); |
85 | 300 |
86 }); | 301 }); |
87 | 302 |
88 group('context', () { | 303 group('context', () { |
89 | 304 |
90 test('read global field', () { | 305 test('read global field', () { |
91 expect(context['x'], 42); | 306 expect(context['x'], equals(42)); |
92 expect(context['y'], null); | 307 expect(context['y'], isNull); |
93 }); | 308 }); |
94 | 309 |
95 test('read global field with underscore', () { | 310 test('read global field with underscore', () { |
96 expect(context['_x'], 123); | 311 expect(context['_x'], equals(123)); |
97 expect(context['y'], null); | 312 expect(context['y'], isNull); |
98 }); | 313 }); |
99 | 314 |
100 test('write global field', () { | 315 test('write global field', () { |
101 context['y'] = 42; | 316 context['y'] = 42; |
102 expect(context['y'], 42); | 317 expect(context['y'], equals(42)); |
103 }); | 318 }); |
104 | 319 |
105 }); | 320 }); |
106 | 321 |
107 group('new JsObject()', () { | 322 group('new_JsObject', () { |
108 | 323 |
109 test('new Foo()', () { | 324 test('new Foo()', () { |
110 var foo = new JsObject(context['Foo'], [42]); | 325 var foo = new JsObject(context['Foo'], [42]); |
111 expect(foo['a'], 42); | 326 expect(foo['a'], equals(42)); |
112 expect(foo.callMethod('bar'), 42); | 327 expect(foo.callMethod('bar'), equals(42)); |
113 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); | 328 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); |
114 }); | 329 }); |
115 | 330 |
116 test('new container.Foo()', () { | 331 test('new container.Foo()', () { |
117 final Foo2 = context['container']['Foo']; | 332 final Foo2 = context['container']['Foo']; |
118 final foo = new JsObject(Foo2, [42]); | 333 final foo = new JsObject(Foo2, [42]); |
119 expect(foo['a'], 42); | 334 expect(foo['a'], 42); |
120 expect(Foo2['b'], 38); | 335 expect(Foo2['b'], 38); |
121 }); | 336 }); |
122 | 337 |
123 test('new Array()', () { | 338 test('new Array()', () { |
124 var a = new JsObject(context['Array']); | 339 var a = new JsObject(context['Array']); |
125 expect(a, (a) => a is JsArray); | 340 expect(a, new isInstanceOf<JsArray>()); |
126 | 341 |
127 // Test that the object still behaves via the base JsObject interface. | 342 // Test that the object still behaves via the base JsObject interface. |
128 // JsArray specific tests are below. | 343 // JsArray specific tests are below. |
129 expect(a['length'], 0); | 344 expect(a['length'], 0); |
130 | 345 |
131 a.callMethod('push', ["value 1"]); | 346 a.callMethod('push', ["value 1"]); |
132 expect(a['length'], 1); | 347 expect(a['length'], 1); |
133 expect(a[0], "value 1"); | 348 expect(a[0], "value 1"); |
134 | 349 |
135 a.callMethod('pop'); | 350 a.callMethod('pop'); |
136 expect(a['length'], 0); | 351 expect(a['length'], 0); |
137 }); | 352 }); |
138 | 353 |
139 test('new Date()', () { | 354 test('new Date()', () { |
140 final a = new JsObject(context['Date']); | 355 final a = new JsObject(context['Date']); |
141 expect(a.callMethod('getTime'), isNotNull); | 356 expect(a.callMethod('getTime'), isNotNull); |
142 }); | 357 }); |
143 | 358 |
144 test('new Date(12345678)', () { | 359 test('new Date(12345678)', () { |
145 final a = new JsObject(context['Date'], [12345678]); | 360 final a = new JsObject(context['Date'], [12345678]); |
146 expect(a.callMethod('getTime'), 12345678); | 361 expect(a.callMethod('getTime'), equals(12345678)); |
147 }); | 362 }); |
148 | 363 |
149 test('new Date("December 17, 1995 03:24:00 GMT")', () { | 364 test('new Date("December 17, 1995 03:24:00 GMT")', () { |
150 final a = new JsObject(context['Date'], | 365 final a = new JsObject(context['Date'], |
151 ["December 17, 1995 03:24:00 GMT"]); | 366 ["December 17, 1995 03:24:00 GMT"]); |
152 expect(a.callMethod('getTime'), 819170640000); | 367 expect(a.callMethod('getTime'), equals(819170640000)); |
153 }); | 368 }); |
154 | 369 |
155 test('new Date(1995,11,17)', () { | 370 test('new Date(1995,11,17)', () { |
156 // Note: JS Date counts months from 0 while Dart counts from 1. | 371 // Note: JS Date counts months from 0 while Dart counts from 1. |
157 final a = new JsObject(context['Date'], [1995, 11, 17]); | 372 final a = new JsObject(context['Date'], [1995, 11, 17]); |
158 final b = new DateTime(1995, 12, 17); | 373 final b = new DateTime(1995, 12, 17); |
159 expect(a.callMethod('getTime'), b.millisecondsSinceEpoch); | 374 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); |
160 }); | 375 }); |
161 | 376 |
162 test('new Date(1995,11,17,3,24,0)', () { | 377 test('new Date(1995,11,17,3,24,0)', () { |
163 // Note: JS Date counts months from 0 while Dart counts from 1. | 378 // Note: JS Date counts months from 0 while Dart counts from 1. |
164 final a = new JsObject(context['Date'], | 379 final a = new JsObject(context['Date'], |
165 [1995, 11, 17, 3, 24, 0]); | 380 [1995, 11, 17, 3, 24, 0]); |
166 final b = new DateTime(1995, 12, 17, 3, 24, 0); | 381 final b = new DateTime(1995, 12, 17, 3, 24, 0); |
167 expect(a.callMethod('getTime'), b.millisecondsSinceEpoch); | 382 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); |
168 }); | 383 }); |
169 | 384 |
170 test('new Object()', () { | 385 test('new Object()', () { |
171 final a = new JsObject(context['Object']); | 386 final a = new JsObject(context['Object']); |
172 expect(a, isNotNull); | 387 expect(a, isNotNull); |
173 | 388 |
174 a['attr'] = "value"; | 389 a['attr'] = "value"; |
175 expect(a['attr'], "value"); | 390 expect(a['attr'], equals("value")); |
176 }); | 391 }); |
177 | 392 |
178 test(r'new RegExp("^\w+$")', () { | 393 test(r'new RegExp("^\w+$")', () { |
179 final a = new JsObject(context['RegExp'], [r'^\w+$']); | 394 final a = new JsObject(context['RegExp'], [r'^\w+$']); |
180 expect(a, isNotNull); | 395 expect(a, isNotNull); |
181 expect(a.callMethod('test', ['true']), true); | 396 expect(a.callMethod('test', ['true']), isTrue); |
182 expect(a.callMethod('test', [' false']), false); | 397 expect(a.callMethod('test', [' false']), isFalse); |
183 }); | 398 }); |
184 | 399 |
185 test('js instantiation via map notation : new Array()', () { | 400 test('js instantiation via map notation : new Array()', () { |
186 final a = new JsObject(context['Array']); | 401 final a = new JsObject(context['Array']); |
187 expect(a, isNotNull); | 402 expect(a, isNotNull); |
188 expect(a['length'], 0); | 403 expect(a['length'], equals(0)); |
189 | 404 |
190 a.callMethod('push', ["value 1"]); | 405 a.callMethod('push', ["value 1"]); |
191 expect(a['length'], 1); | 406 expect(a['length'], equals(1)); |
192 expect(a[0], "value 1"); | 407 expect(a[0], equals("value 1")); |
193 | 408 |
194 a.callMethod('pop'); | 409 a.callMethod('pop'); |
195 expect(a['length'], 0); | 410 expect(a['length'], equals(0)); |
196 }); | 411 }); |
197 | 412 |
198 test('js instantiation via map notation : new Date()', () { | 413 test('js instantiation via map notation : new Date()', () { |
199 final a = new JsObject(context['Date']); | 414 final a = new JsObject(context['Date']); |
200 expect(a.callMethod('getTime'), isNotNull); | 415 expect(a.callMethod('getTime'), isNotNull); |
201 }); | 416 }); |
202 | 417 |
| 418 test('typed array', () { |
| 419 if (Platform.supportsTypedData) { |
| 420 // Safari's ArrayBuffer is not a Function and so doesn't support bind |
| 421 // which JsObject's constructor relies on. |
| 422 // bug: https://bugs.webkit.org/show_bug.cgi?id=122976 |
| 423 if (context['ArrayBuffer']['bind'] != null) { |
| 424 final codeUnits = "test".codeUnits; |
| 425 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); |
| 426 final bufView = new JsObject(context['Uint8Array'], [buf]); |
| 427 for (var i = 0; i < codeUnits.length; i++) { |
| 428 bufView[i] = codeUnits[i]; |
| 429 } |
| 430 } |
| 431 } |
| 432 }); |
| 433 |
203 test('>10 parameters', () { | 434 test('>10 parameters', () { |
204 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]); | 435 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]); |
205 for (var i = 1; i <= 11; i++) { | 436 for (var i = 1; i <= 11; i++) { |
206 expect(o["f$i"], i); | 437 expect(o["f$i"], i); |
207 } | 438 } |
208 expect(o['constructor'], context['Baz']); | 439 expect(o['constructor'], equals(context['Baz'])); |
209 }); | 440 }); |
210 }); | 441 }); |
211 | 442 |
212 group('JsFunction and callMethod', () { | 443 group('JsFunction and callMethod', () { |
213 | 444 |
214 test('new JsObject can return a JsFunction', () { | 445 test('new JsObject can return a JsFunction', () { |
215 var f = new JsObject(context['Function']); | 446 var f = new JsObject(context['Function']); |
216 expect(f, (a) => a is JsFunction); | 447 expect(f, new isInstanceOf<JsFunction>()); |
217 }); | 448 }); |
218 | 449 |
219 test('JsFunction.apply on a function defined in JS', () { | 450 test('JsFunction.apply on a function defined in JS', () { |
220 expect(context['razzle'].apply([]), 42); | 451 expect(context['razzle'].apply([]), equals(42)); |
221 }); | 452 }); |
222 | 453 |
223 test('JsFunction.apply on a function that uses "this"', () { | 454 test('JsFunction.apply on a function that uses this', () { |
224 var object = new Object(); | 455 var object = new Object(); |
225 expect(context['returnThis'].apply([], thisArg: object), same(object)); | 456 expect(context['returnThis'].apply([], thisArg: object), same(object)); |
226 }); | 457 }); |
227 | 458 |
228 test('JsObject.callMethod on a function defined in JS', () { | 459 test('JsObject.callMethod on a function defined in JS', () { |
229 expect(context.callMethod('razzle'), 42); | 460 expect(context.callMethod('razzle'), equals(42)); |
230 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); | 461 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); |
231 }); | 462 }); |
232 | 463 |
233 test('callMethod with many arguments', () { | 464 test('callMethod with many arguments', () { |
234 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), | 465 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), |
235 55); | 466 equals(55)); |
236 }); | 467 }); |
237 | 468 |
238 test('access a property of a function', () { | 469 test('access a property of a function', () { |
239 expect(context.callMethod('Bar'), "ret_value"); | 470 expect(context.callMethod('Bar'), "ret_value"); |
240 expect(context['Bar']['foo'], "property_value"); | 471 expect(context['Bar']['foo'], "property_value"); |
241 }); | 472 }); |
| 473 /* |
| 474 TODO(jacobr): evaluate whether we should be in the business of throwing |
| 475 ArgumentError outside of checked mode. In unchecked mode this should just |
| 476 return a NoSuchMethodError as the class lacks a method "true". |
242 | 477 |
| 478 test('callMethod throws if name is not a String or num', () { |
| 479 expect(() => context.callMethod(true), |
| 480 throwsA(new isInstanceOf<ArgumentError>())); |
| 481 }); |
| 482 */ |
243 }); | 483 }); |
244 | 484 |
245 group('JsArray', () { | 485 group('JsArray', () { |
246 | 486 |
247 test('new JsArray()', () { | 487 test('new JsArray()', () { |
248 var array = new JsArray(); | 488 var array = new JsArray(); |
249 var arrayType = context['Array']; | 489 var arrayType = context['Array']; |
250 expect(array.instanceof(arrayType), true); | 490 expect(array.instanceof(arrayType), true); |
251 expect(array, []); | 491 expect(array, []); |
252 // basic check that it behaves like a List | 492 // basic check that it behaves like a List |
253 array.addAll([1, 2, 3]); | 493 array.addAll([1, 2, 3]); |
254 expect(array, [1, 2, 3]); | 494 expect(array, [1, 2, 3]); |
255 }); | 495 }); |
256 | 496 |
257 test('new JsArray.from()', () { | 497 test('new JsArray.from()', () { |
258 var array = new JsArray.from([1, 2, 3]); | 498 var array = new JsArray.from([1, 2, 3]); |
259 var arrayType = context['Array']; | 499 var arrayType = context['Array']; |
260 expect(array.instanceof(arrayType), true); | 500 expect(array.instanceof(arrayType), true); |
261 expect(array, [1, 2, 3]); | 501 expect(array, [1, 2, 3]); |
262 }); | 502 }); |
263 | 503 |
264 test('get Array from JS', () { | 504 test('get Array from JS', () { |
265 context['a'] = new JsObject(context['Array'], [1, 2, 3]); | 505 context['a'] = new JsObject(context['Array'], [1, 2, 3]); |
266 expect(context.callMethod('isPropertyInstanceOf', | 506 expect(context.callMethod('isPropertyInstanceOf', |
267 ['a', context['Array']]), true); | 507 ['a', context['Array']]), isTrue); |
268 var a = context['a']; | 508 var a = context['a']; |
269 expect(a, (a) => a is JsArray); | 509 expect(a, new isInstanceOf<JsArray>()); |
270 expect(a, [1, 2, 3]); | 510 expect(a, [1, 2, 3]); |
271 context.deleteProperty('a'); | 511 context.deleteProperty('a'); |
272 }); | 512 }); |
273 | 513 |
274 test('pass Array to JS', () { | 514 test('pass Array to JS', () { |
275 context['a'] = [1, 2, 3]; | 515 context['a'] = [1, 2, 3]; |
276 expect(context.callMethod('isPropertyInstanceOf', | 516 expect(context.callMethod('isPropertyInstanceOf', |
277 ['a', context['Array']]), false); | 517 ['a', context['Array']]), isTrue); |
278 var a = context['a']; | 518 var a = context['a']; |
279 expect(a, (a) => a is List); | 519 expect(a, new isInstanceOf<List>()); |
280 expect(a, isNot((a) => a is JsArray)); | 520 expect(a, isNot(new isInstanceOf<JsArray>())); |
281 expect(a, [1, 2, 3]); | 521 expect(a, [1, 2, 3]); |
282 context.deleteProperty('a'); | 522 context.deleteProperty('a'); |
283 }); | 523 }); |
284 | 524 |
285 test('[]', () { | 525 test('[]', () { |
286 var array = new JsArray.from([1, 2]); | 526 var array = new JsArray.from([1, 2]); |
287 expect(array[0], 1); | 527 expect(array[0], 1); |
288 expect(array[1], 2); | 528 expect(array[1], 2); |
289 expect(() => array[-1], throwsA(isRangeError)); | 529 expect(() => array[-1], throwsA(isRangeError)); |
290 expect(() => array[2], throwsA(isRangeError)); | 530 expect(() => array[2], throwsA(isRangeError)); |
(...skipping 11 matching lines...) Expand all Loading... |
302 test('length', () { | 542 test('length', () { |
303 var array = new JsArray.from([1, 2, 3]); | 543 var array = new JsArray.from([1, 2, 3]); |
304 expect(array.length, 3); | 544 expect(array.length, 3); |
305 array.add(4); | 545 array.add(4); |
306 expect(array.length, 4); | 546 expect(array.length, 4); |
307 array.length = 2; | 547 array.length = 2; |
308 expect(array, [1, 2]); | 548 expect(array, [1, 2]); |
309 array.length = 3; | 549 array.length = 3; |
310 expect(array, [1, 2, null]); | 550 expect(array, [1, 2, null]); |
311 }); | 551 }); |
312 | 552 |
313 test('add', () { | 553 test('add', () { |
314 var array = new JsArray(); | 554 var array = new JsArray(); |
315 array.add('a'); | 555 array.add('a'); |
316 expect(array, ['a']); | 556 expect(array, ['a']); |
317 array.add('b'); | 557 array.add('b'); |
318 expect(array, ['a', 'b']); | 558 expect(array, ['a', 'b']); |
319 }); | 559 }); |
320 | 560 |
321 test('addAll', () { | 561 test('addAll', () { |
322 var array = new JsArray(); | 562 var array = new JsArray(); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 var array = new JsArray.from(['c', 'a', 'b']); | 622 var array = new JsArray.from(['c', 'a', 'b']); |
383 array.sort((a, b) => -(a.compareTo(b))); | 623 array.sort((a, b) => -(a.compareTo(b))); |
384 expect(array, ['c', 'b', 'a']); | 624 expect(array, ['c', 'b', 'a']); |
385 }); | 625 }); |
386 | 626 |
387 }); | 627 }); |
388 | 628 |
389 group('JsObject.fromBrowserObject()', () { | 629 group('JsObject.fromBrowserObject()', () { |
390 | 630 |
391 test('Nodes are proxied', () { | 631 test('Nodes are proxied', () { |
392 var node = new JsObject.fromBrowserObject(document.createElement('div')); | 632 var node = new JsObject.fromBrowserObject(new DivElement()); |
393 context.callMethod('addTestProperty', [node]); | 633 context.callMethod('addTestProperty', [node]); |
394 expect(node is JsObject, true); | 634 expect(node is JsObject, isTrue); |
395 expect(node.instanceof(context['HTMLDivElement']), true); | 635 // TODO(justinfagnani): make this work in IE9 |
| 636 // expect(node.instanceof(context['HTMLDivElement']), isTrue); |
396 expect(node['testProperty'], 'test'); | 637 expect(node['testProperty'], 'test'); |
397 }); | 638 }); |
398 | 639 |
399 test('primitives and null throw ArgumentError', () { | 640 test('primitives and null throw ArgumentError', () { |
400 for (var v in ['a', 1, 2.0, true, null]) { | 641 for (var v in ['a', 1, 2.0, true, null]) { |
401 expect(() => new JsObject.fromBrowserObject(v), | 642 expect(() => new JsObject.fromBrowserObject(v), |
402 throwsA((a) => a is ArgumentError)); | 643 throwsA(new isInstanceOf<ArgumentError>())); |
403 } | 644 } |
404 }); | 645 }); |
405 | 646 |
406 }); | 647 }); |
407 | 648 |
408 group('Dart functions', () { | 649 group('Dart_functions', () { |
409 test('invoke Dart callback from JS', () { | 650 test('invoke Dart callback from JS', () { |
410 expect(() => context.callMethod('invokeCallback'), throws); | 651 expect(() => context.callMethod('invokeCallback'), throws); |
411 | 652 |
412 context['callback'] = () => 42; | 653 context['callback'] = () => 42; |
413 expect(context.callMethod('invokeCallback'), 42); | 654 expect(context.callMethod('invokeCallback'), equals(42)); |
414 | 655 |
415 context.deleteProperty('callback'); | 656 context.deleteProperty('callback'); |
416 }); | 657 }); |
417 | 658 |
418 test('callback as parameter', () { | 659 test('callback as parameter', () { |
419 expect(context.callMethod('getTypeOf', [context['razzle']]), | 660 expect(context.callMethod('getTypeOf', [context['razzle']]), |
420 "function"); | 661 equals("function")); |
421 }); | 662 }); |
422 | 663 |
423 test('invoke Dart callback from JS with this', () { | 664 test('invoke Dart callback from JS with this', () { |
424 // A JavaScript constructor implemented in Dart using 'this' | 665 // A JavaScript constructor function implemented in Dart which |
| 666 // uses 'this' |
425 final constructor = new JsFunction.withThis(($this, arg1) { | 667 final constructor = new JsFunction.withThis(($this, arg1) { |
| 668 var t = $this; |
426 $this['a'] = 42; | 669 $this['a'] = 42; |
427 }); | 670 }); |
428 var o = new JsObject(constructor, ["b"]); | 671 var o = new JsObject(constructor, ["b"]); |
429 expect(o['a'], 42); | 672 expect(o['a'], equals(42)); |
430 }); | 673 }); |
431 | 674 |
432 test('invoke Dart callback from JS with 11 parameters', () { | 675 test('invoke Dart callback from JS with 11 parameters', () { |
433 context['callbackWith11params'] = (p1, p2, p3, p4, p5, p6, p7, | 676 context['callbackWith11params'] = (p1, p2, p3, p4, p5, p6, p7, |
434 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'; | 677 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'; |
435 expect(context.callMethod('invokeCallbackWith11params'), | 678 expect(context.callMethod('invokeCallbackWith11params'), |
436 '1234567891011'); | 679 equals('1234567891011')); |
437 }); | 680 }); |
438 | 681 |
439 test('return a JS proxy to JavaScript', () { | 682 test('return a JS proxy to JavaScript', () { |
440 var result = context.callMethod('testJsMap', | 683 var result = context.callMethod('testJsMap', [() => new JsObject.jsify({'v
alue': 42})]); |
441 [() => new JsObject.jsify({'value': 42})]); | |
442 expect(result, 42); | 684 expect(result, 42); |
443 }); | 685 }); |
444 | 686 |
445 test('emulated functions should be callable in JS', () { | 687 test('emulated functions should be callable in JS', () { |
446 context['callable'] = new Callable(); | 688 context['callable'] = new Callable(); |
447 var result = context.callMethod('callable'); | 689 var result = context.callMethod('callable'); |
448 expect(result, 'called'); | 690 expect(result, 'called'); |
449 context.deleteProperty('callable'); | 691 context.deleteProperty('callable'); |
450 }, skip: "https://github.com/dart-lang/dev_compiler/issues/244"); | 692 }); |
451 | 693 |
452 }); | 694 }); |
453 | 695 |
454 group('JsObject.jsify()', () { | 696 group('JsObject.jsify()', () { |
455 | 697 |
456 test('convert a List', () { | 698 test('convert a List', () { |
457 final list = [1, 2, 3, 4, 5, 6, 7, 8]; | 699 final list = [1, 2, 3, 4, 5, 6, 7, 8]; |
458 var array = new JsObject.jsify(list); | 700 var array = new JsObject.jsify(list); |
459 expect(context.callMethod('isArray', [array]), true); | 701 expect(context.callMethod('isArray', [array]), isTrue); |
460 expect(array['length'], list.length); | 702 expect(array['length'], equals(list.length)); |
461 for (var i = 0; i < list.length ; i++) { | 703 for (var i = 0; i < list.length ; i++) { |
462 expect(array[i], list[i]); | 704 expect(array[i], equals(list[i])); |
463 } | 705 } |
464 }); | 706 }); |
465 | 707 |
466 test('convert an Iterable', () { | 708 test('convert an Iterable', () { |
467 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); | 709 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); |
468 var array = new JsObject.jsify(set); | 710 var array = new JsObject.jsify(set); |
469 expect(context.callMethod('isArray', [array]), true); | 711 expect(context.callMethod('isArray', [array]), isTrue); |
470 expect(array['length'], set.length); | 712 expect(array['length'], equals(set.length)); |
471 for (var i = 0; i < array['length'] ; i++) { | 713 for (var i = 0; i < array['length'] ; i++) { |
472 expect(set.contains(array[i]), true); | 714 expect(set.contains(array[i]), isTrue); |
473 } | 715 } |
474 }); | 716 }); |
475 | 717 |
476 test('convert a Map', () { | 718 test('convert a Map', () { |
477 var map = {'a': 1, 'b': 2, 'c': 3}; | 719 var map = {'a': 1, 'b': 2, 'c': 3}; |
478 var jsMap = new JsObject.jsify(map); | 720 var jsMap = new JsObject.jsify(map); |
479 expect(!context.callMethod('isArray', [jsMap]), true); | 721 expect(!context.callMethod('isArray', [jsMap]), isTrue); |
480 for (final key in map.keys) { | 722 for (final key in map.keys) { |
481 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), true); | 723 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue); |
482 } | 724 } |
483 }); | 725 }); |
484 | 726 |
485 test('deep convert a complex object', () { | 727 test('deep convert a complex object', () { |
486 final object = { | 728 final object = { |
487 'a': [1, [2, 3]], | 729 'a': [1, [2, 3]], |
488 'b': { | 730 'b': { |
489 'c': 3, | 731 'c': 3, |
490 'd': new JsObject(context['Foo'], [42]) | 732 'd': new JsObject(context['Foo'], [42]) |
491 }, | 733 }, |
492 'e': null | 734 'e': null |
493 }; | 735 }; |
494 var jsObject = new JsObject.jsify(object); | 736 var jsObject = new JsObject.jsify(object); |
495 expect(jsObject['a'][0], object['a'][0]); | 737 expect(jsObject['a'][0], equals(object['a'][0])); |
496 expect(jsObject['a'][1][0], object['a'][1][0]); | 738 expect(jsObject['a'][1][0], equals(object['a'][1][0])); |
497 expect(jsObject['a'][1][1], object['a'][1][1]); | 739 expect(jsObject['a'][1][1], equals(object['a'][1][1])); |
498 expect(jsObject['b']['c'], object['b']['c']); | 740 expect(jsObject['b']['c'], equals(object['b']['c'])); |
499 expect(jsObject['b']['d'], object['b']['d']); | 741 expect(jsObject['b']['d'], equals(object['b']['d'])); |
500 expect(jsObject['b']['d'].callMethod('bar'), 42); | 742 expect(jsObject['b']['d'].callMethod('bar'), equals(42)); |
501 expect(jsObject['e'], null); | 743 expect(jsObject['e'], isNull); |
502 }); | 744 }); |
503 | 745 |
504 test('throws if object is not a Map or Iterable', () { | 746 test('throws if object is not a Map or Iterable', () { |
505 expect(() => new JsObject.jsify('a'), | 747 expect(() => new JsObject.jsify('a'), |
506 throwsA((a) => a is ArgumentError)); | 748 throwsA(new isInstanceOf<ArgumentError>())); |
507 }); | 749 }); |
508 }); | 750 }); |
509 | 751 |
510 group('JsObject methods', () { | 752 group('JsObject_methods', () { |
511 | 753 |
512 test('hashCode and ==', () { | 754 test('hashCode and ==', () { |
513 final o1 = context['Object']; | 755 final o1 = context['Object']; |
514 final o2 = context['Object']; | 756 final o2 = context['Object']; |
515 expect(o1 == o2, true); | 757 expect(o1 == o2, isTrue); |
516 expect(o1.hashCode == o2.hashCode, true); | 758 expect(o1.hashCode == o2.hashCode, isTrue); |
517 final d = context['document']; | 759 final d = context['document']; |
518 expect(o1 == d, false); | 760 expect(o1 == d, isFalse); |
519 }); | 761 }); |
520 | 762 |
521 test('toString', () { | 763 test('toString', () { |
522 var foo = new JsObject(context['Foo'], [42]); | 764 var foo = new JsObject(context['Foo'], [42]); |
523 expect(foo.toString(), "I'm a Foo a=42"); | 765 expect(foo.toString(), equals("I'm a Foo a=42")); |
524 var container = context['container']; | 766 var container = context['container']; |
525 expect(container.toString(), "[object Object]"); | 767 expect(container.toString(), equals("[object Object]")); |
526 }); | 768 }); |
527 | 769 |
528 test('toString returns a String even if the JS object does not', () { | 770 test('toString returns a String even if the JS object does not', () { |
529 var foo = new JsObject(context['Liar']); | 771 var foo = new JsObject(context['Liar']); |
530 expect(foo.callMethod('toString'), 1); | 772 expect(foo.callMethod('toString'), 1); |
531 expect(foo.toString(), '1'); | 773 expect(foo.toString(), '1'); |
532 }); | 774 }); |
533 | 775 |
534 test('instanceof', () { | 776 test('instanceof', () { |
535 var foo = new JsObject(context['Foo'], [1]); | 777 var foo = new JsObject(context['Foo'], [1]); |
536 expect(foo.instanceof(context['Foo']), true); | 778 expect(foo.instanceof(context['Foo']), isTrue); |
537 expect(foo.instanceof(context['Object']), true); | 779 expect(foo.instanceof(context['Object']), isTrue); |
538 expect(foo.instanceof(context['String']), false); | 780 expect(foo.instanceof(context['String']), isFalse); |
539 }); | 781 }); |
540 | 782 |
541 test('deleteProperty', () { | 783 test('deleteProperty', () { |
542 var object = new JsObject.jsify({}); | 784 var object = new JsObject.jsify({}); |
543 object['a'] = 1; | 785 object['a'] = 1; |
544 expect(context['Object'].callMethod('keys', [object])['length'], 1); | 786 expect(context['Object'].callMethod('keys', [object])['length'], 1); |
545 expect(context['Object'].callMethod('keys', [object])[0], "a"); | 787 expect(context['Object'].callMethod('keys', [object])[0], "a"); |
546 object.deleteProperty("a"); | 788 object.deleteProperty("a"); |
547 expect(context['Object'].callMethod('keys', [object])['length'], 0); | 789 expect(context['Object'].callMethod('keys', [object])['length'], 0); |
548 }); | 790 }); |
549 | 791 |
| 792 /* TODO(jacobr): this is another test that is inconsistent with JS semantics. |
| 793 test('deleteProperty throws if name is not a String or num', () { |
| 794 var object = new JsObject.jsify({}); |
| 795 expect(() => object.deleteProperty(true), |
| 796 throwsA(new isInstanceOf<ArgumentError>())); |
| 797 }); |
| 798 */ |
| 799 |
550 test('hasProperty', () { | 800 test('hasProperty', () { |
551 var object = new JsObject.jsify({}); | 801 var object = new JsObject.jsify({}); |
552 object['a'] = 1; | 802 object['a'] = 1; |
553 expect(object.hasProperty('a'), true); | 803 expect(object.hasProperty('a'), isTrue); |
554 expect(object.hasProperty('b'), false); | 804 expect(object.hasProperty('b'), isFalse); |
555 }); | 805 }); |
556 | 806 |
| 807 /* TODO(jacobr): is this really the correct unchecked mode behavior? |
| 808 test('hasProperty throws if name is not a String or num', () { |
| 809 var object = new JsObject.jsify({}); |
| 810 expect(() => object.hasProperty(true), |
| 811 throwsA(new isInstanceOf<ArgumentError>())); |
| 812 }); |
| 813 */ |
| 814 |
557 test('[] and []=', () { | 815 test('[] and []=', () { |
558 final myArray = context['myArray']; | 816 final myArray = context['myArray']; |
559 expect(myArray['length'], 1); | 817 expect(myArray['length'], equals(1)); |
560 expect(myArray[0], "value1"); | 818 expect(myArray[0], equals("value1")); |
561 myArray[0] = "value2"; | 819 myArray[0] = "value2"; |
562 expect(myArray['length'], 1); | 820 expect(myArray['length'], equals(1)); |
563 expect(myArray[0], "value2"); | 821 expect(myArray[0], equals("value2")); |
564 | 822 |
565 final foo = new JsObject(context['Foo'], [1]); | 823 final foo = new JsObject(context['Foo'], [1]); |
566 foo["getAge"] = () => 10; | 824 foo["getAge"] = () => 10; |
567 expect(foo.callMethod('getAge'), 10); | 825 expect(foo.callMethod('getAge'), equals(10)); |
568 }); | 826 }); |
569 | 827 |
| 828 /* TODO(jacobr): remove as we should only throw this in checked mode. |
| 829 test('[] and []= throw if name is not a String or num', () { |
| 830 var object = new JsObject.jsify({}); |
| 831 expect(() => object[true], |
| 832 throwsA(new isInstanceOf<ArgumentError>())); |
| 833 expect(() => object[true] = 1, |
| 834 throwsA(new isInstanceOf<ArgumentError>())); |
| 835 }); |
| 836 */ |
570 }); | 837 }); |
571 | 838 |
572 group('transferrables', () { | 839 group('transferrables', () { |
573 | 840 |
574 group('JS->Dart', () { | 841 group('JS->Dart', () { |
575 | 842 |
576 test('DateTime', () { | 843 test('DateTime', () { |
577 var date = context.callMethod('getNewDate'); | 844 var date = context.callMethod('getNewDate'); |
578 expect(date is DateTime, true); | 845 expect(date is DateTime, isTrue); |
579 }); | 846 }); |
580 | 847 |
581 test('window', () { | 848 test('window', () { |
582 expect(context['window'] is Window, true); | 849 expect(context['window'] is Window, isTrue); |
583 }); | 850 }); |
584 | 851 |
| 852 // Bug: dartbug.com/24520 |
| 853 /* |
585 test('foreign browser objects should be proxied', () { | 854 test('foreign browser objects should be proxied', () { |
586 var iframe = document.createElement('iframe'); | 855 var iframe = new IFrameElement(); |
587 document.body.appendChild(iframe); | 856 document.body.children.add(iframe); |
588 var proxy = new JsObject.fromBrowserObject(iframe); | 857 var proxy = new JsObject.fromBrowserObject(iframe); |
589 | 858 |
590 // Window | 859 // Window |
591 var contentWindow = proxy['contentWindow']; | 860 var contentWindow = proxy['contentWindow']; |
592 expect(contentWindow, isNot((a) => a is Window)); | 861 expect(contentWindow, isNot(new isInstanceOf<Window>())); |
593 expect(contentWindow, (a) => a is JsObject); | 862 expect(contentWindow, new isInstanceOf<JsObject>()); |
594 | 863 |
595 // Node | 864 // Node |
596 var foreignDoc = contentWindow['document']; | 865 var foreignDoc = contentWindow['document']; |
597 expect(foreignDoc, isNot((a) => a is Node)); | 866 expect(foreignDoc, isNot(new isInstanceOf<Node>())); |
598 expect(foreignDoc, (a) => a is JsObject); | 867 expect(foreignDoc, new isInstanceOf<JsObject>()); |
599 | 868 |
600 // Event | 869 // Event |
601 var clicked = false; | 870 var clicked = false; |
602 foreignDoc['onclick'] = (e) { | 871 foreignDoc['onclick'] = (e) { |
603 expect(e, isNot((a) => a is Event)); | 872 expect(e, isNot(new isInstanceOf<Event>())); |
604 expect(e, (a) => a is JsObject); | 873 expect(e, new isInstanceOf<JsObject>()); |
605 clicked = true; | 874 clicked = true; |
606 }; | 875 }; |
607 | 876 |
608 context.callMethod('fireClickEvent', [contentWindow]); | 877 context.callMethod('fireClickEvent', [contentWindow]); |
609 expect(clicked, true); | 878 expect(clicked, isTrue); |
| 879 }); |
| 880 */ |
| 881 |
| 882 test('document', () { |
| 883 expect(context['document'] is Document, isTrue); |
610 }); | 884 }); |
611 | 885 |
612 test('document', () { | 886 skipIE9_test('Blob', () { |
613 expect(context['document'] is Document, true); | |
614 }); | |
615 | |
616 test('Blob', () { | |
617 var blob = context.callMethod('getNewBlob'); | 887 var blob = context.callMethod('getNewBlob'); |
618 expect(blob is Blob, true); | 888 expect(blob is Blob, isTrue); |
619 expect(blob.type, 'text/html'); | 889 expect(blob.type, equals('text/html')); |
620 }); | 890 }); |
621 | 891 |
622 test('unattached DivElement', () { | 892 test('unattached DivElement', () { |
623 var node = context.callMethod('getNewDivElement'); | 893 var node = context.callMethod('getNewDivElement'); |
624 expect(node is DivElement, true); | 894 expect(node is DivElement, isTrue); |
625 }); | 895 }); |
626 | 896 |
627 test('Event', () { | 897 test('Event', () { |
628 var event = context.callMethod('getNewEvent'); | 898 var event = context.callMethod('getNewEvent'); |
629 expect(event is Event, true); | 899 expect(event is Event, true); |
630 }); | 900 }); |
631 | 901 |
| 902 test('KeyRange', () { |
| 903 if (IdbFactory.supported) { |
| 904 var node = context.callMethod('getNewIDBKeyRange'); |
| 905 expect(node is KeyRange, isTrue); |
| 906 } |
| 907 }); |
| 908 |
632 test('ImageData', () { | 909 test('ImageData', () { |
633 var node = context.callMethod('getNewImageData'); | 910 var node = context.callMethod('getNewImageData'); |
634 expect(node is ImageData, true); | 911 expect(node is ImageData, isTrue); |
| 912 }); |
| 913 |
| 914 test('typed data: Int32Array', () { |
| 915 if (Platform.supportsTypedData) { |
| 916 var list = context.callMethod('getNewInt32Array'); |
| 917 print(list); |
| 918 expect(list is Int32List, isTrue); |
| 919 expect(list, orderedEquals([1, 2, 3, 4, 5, 6, 7, 8])); |
| 920 } |
635 }); | 921 }); |
636 | 922 |
637 }); | 923 }); |
638 | 924 |
639 group('Dart->JS', () { | 925 group('Dart->JS', () { |
640 | 926 |
641 test('Date', () { | 927 test('Date', () { |
642 context['o'] = new DateTime(1995, 12, 17); | 928 context['o'] = new DateTime(1995, 12, 17); |
643 var dateType = context['Date']; | 929 var dateType = context['Date']; |
644 expect(context.callMethod('isPropertyInstanceOf', ['o', dateType]), | 930 expect(context.callMethod('isPropertyInstanceOf', ['o', dateType]), |
645 true); | 931 isTrue); |
646 context.deleteProperty('o'); | 932 context.deleteProperty('o'); |
647 }); | 933 }); |
648 | 934 |
649 test('window', () { | 935 skipIE9_test('window', () { |
650 context['o'] = window; | 936 context['o'] = window; |
651 var windowType = context['Window']; | 937 var windowType = context['Window']; |
652 expect(context.callMethod('isPropertyInstanceOf', ['o', windowType]), | 938 expect(context.callMethod('isPropertyInstanceOf', ['o', windowType]), |
653 true); | 939 isTrue); |
654 context.deleteProperty('o'); | 940 context.deleteProperty('o'); |
655 }); | 941 }); |
656 | 942 |
657 test('document', () { | 943 skipIE9_test('document', () { |
658 context['o'] = document; | 944 context['o'] = document; |
659 var documentType = context['Document']; | 945 var documentType = context['Document']; |
660 expect(context.callMethod('isPropertyInstanceOf', ['o', documentType]), | 946 expect(context.callMethod('isPropertyInstanceOf', ['o', documentType]), |
661 true); | 947 isTrue); |
662 context.deleteProperty('o'); | 948 context.deleteProperty('o'); |
663 }); | 949 }); |
664 | 950 |
665 test('Blob', () { | 951 skipIE9_test('Blob', () { |
666 var fileParts = ['<a id="a"><b id="b">hey!</b></a>']; | 952 var fileParts = ['<a id="a"><b id="b">hey!</b></a>']; |
667 context['o'] = new Blob(fileParts, type: 'text/html'); | 953 context['o'] = new Blob(fileParts, 'text/html'); |
668 var blobType = context['Blob']; | 954 var blobType = context['Blob']; |
669 expect(context.callMethod('isPropertyInstanceOf', ['o', blobType]), | 955 expect(context.callMethod('isPropertyInstanceOf', ['o', blobType]), |
670 true); | 956 isTrue); |
671 context.deleteProperty('o'); | 957 context.deleteProperty('o'); |
672 }); | 958 }); |
673 | 959 |
674 test('unattached DivElement', () { | 960 test('unattached DivElement', () { |
675 context['o'] = document.createElement('div'); | 961 context['o'] = new DivElement(); |
676 var divType = context['HTMLDivElement']; | 962 var divType = context['HTMLDivElement']; |
677 expect(context.callMethod('isPropertyInstanceOf', ['o', divType]), | 963 expect(context.callMethod('isPropertyInstanceOf', ['o', divType]), |
678 true); | 964 isTrue); |
679 context.deleteProperty('o'); | 965 context.deleteProperty('o'); |
680 }); | 966 }); |
681 | 967 |
682 test('Event', () { | 968 test('Event', () { |
683 context['o'] = new CustomEvent('test'); | 969 context['o'] = new CustomEvent('test'); |
684 var eventType = context['Event']; | 970 var eventType = context['Event']; |
685 expect(context.callMethod('isPropertyInstanceOf', ['o', eventType]), | 971 expect(context.callMethod('isPropertyInstanceOf', ['o', eventType]), |
686 true); | 972 isTrue); |
687 context.deleteProperty('o'); | 973 context.deleteProperty('o'); |
688 }); | 974 }); |
689 | 975 |
| 976 test('KeyRange', () { |
| 977 if (IdbFactory.supported) { |
| 978 context['o'] = new KeyRange.only(1); |
| 979 var keyRangeType = context['IDBKeyRange']; |
| 980 expect(context.callMethod('isPropertyInstanceOf', ['o', keyRangeType])
, |
| 981 isTrue); |
| 982 context.deleteProperty('o'); |
| 983 } |
| 984 }); |
| 985 |
690 // this test fails in IE9 for very weird, but unknown, reasons | 986 // this test fails in IE9 for very weird, but unknown, reasons |
691 // the expression context['ImageData'] fails if useHtmlConfiguration() | 987 // the expression context['ImageData'] fails if useHtmlConfiguration() |
692 // is called, or if the other tests in this file are enabled | 988 // is called, or if the other tests in this file are enabled |
693 test('ImageData', () { | 989 skipIE9_test('ImageData', () { |
694 CanvasElement canvas = document.createElement('canvas'); | 990 var canvas = new CanvasElement(); |
695 var ctx = canvas.getContext('2d') as CanvasRenderingContext2D; | 991 var ctx = canvas.getContext('2d'); |
696 context['o'] = ctx.createImageData(1, 1); | 992 context['o'] = ctx.createImageData(1, 1); |
697 var imageDataType = context['ImageData']; | 993 var imageDataType = context['ImageData']; |
698 expect(context.callMethod('isPropertyInstanceOf', ['o', imageDataType]), | 994 expect(context.callMethod('isPropertyInstanceOf', ['o', imageDataType]), |
699 true); | 995 isTrue); |
700 context.deleteProperty('o'); | 996 context.deleteProperty('o'); |
701 }); | 997 }); |
702 | 998 |
| 999 test('typed data: Int32List', () { |
| 1000 if (Platform.supportsTypedData) { |
| 1001 context['o'] = new Int32List.fromList([1, 2, 3, 4]); |
| 1002 var listType = context['Int32Array']; |
| 1003 // TODO(jacobr): make this test pass. Currently some type information |
| 1004 // is lost when typed arrays are passed between JS and Dart. |
| 1005 // expect(context.callMethod('isPropertyInstanceOf', ['o', listType]), |
| 1006 // isTrue); |
| 1007 context.deleteProperty('o'); |
| 1008 } |
| 1009 }); |
| 1010 |
703 }); | 1011 }); |
704 }); | 1012 }); |
| 1013 |
705 } | 1014 } |
OLD | NEW |