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:js'; | 7 import 'dart:js'; |
8 | 8 |
9 // TODO(jmesserly): get tests from package(s) instead. | 9 // TODO(jmesserly): get tests from package(s) instead. |
10 import 'dom.dart'; | 10 import 'dom.dart'; |
(...skipping 19 matching lines...) Expand all Loading... |
30 } | 30 } |
31 | 31 |
32 class TestDartObject {} | 32 class TestDartObject {} |
33 | 33 |
34 class Callable { | 34 class Callable { |
35 call() => 'called'; | 35 call() => 'called'; |
36 } | 36 } |
37 | 37 |
38 main() { | 38 main() { |
39 group('identity', () { | 39 group('identity', () { |
40 | |
41 test('context instances should be identical', () { | 40 test('context instances should be identical', () { |
42 var c1 = context; | 41 var c1 = context; |
43 var c2 = context; | 42 var c2 = context; |
44 expect(identical(c1, c2), true); | 43 expect(identical(c1, c2), true); |
45 }); | 44 }); |
46 | 45 |
47 // TODO(jacobr): switch from equals to identical when dartium supports | 46 // TODO(jacobr): switch from equals to identical when dartium supports |
48 // maintaining proxy equality. | 47 // maintaining proxy equality. |
49 test('identical JS functions should have equal proxies', () { | 48 test('identical JS functions should have equal proxies', () { |
50 var f1 = context['Object']; | 49 var f1 = context['Object']; |
(...skipping 24 matching lines...) Expand all Loading... |
75 group('caching', () { | 74 group('caching', () { |
76 test('JS->Dart', () { | 75 test('JS->Dart', () { |
77 // Test that we are not pulling cached proxy from the prototype | 76 // Test that we are not pulling cached proxy from the prototype |
78 // when asking for a proxy for the object. | 77 // when asking for a proxy for the object. |
79 final proto = context['someProto']; | 78 final proto = context['someProto']; |
80 expect(proto['role'], 'proto'); | 79 expect(proto['role'], 'proto'); |
81 final obj = context['someObject']; | 80 final obj = context['someObject']; |
82 expect(obj['role'], 'object'); | 81 expect(obj['role'], 'object'); |
83 }); | 82 }); |
84 }); | 83 }); |
85 | |
86 }); | 84 }); |
87 | 85 |
88 group('context', () { | 86 group('context', () { |
89 | |
90 test('read global field', () { | 87 test('read global field', () { |
91 expect(context['x'], 42); | 88 expect(context['x'], 42); |
92 expect(context['y'], null); | 89 expect(context['y'], null); |
93 }); | 90 }); |
94 | 91 |
95 test('read global field with underscore', () { | 92 test('read global field with underscore', () { |
96 expect(context['_x'], 123); | 93 expect(context['_x'], 123); |
97 expect(context['y'], null); | 94 expect(context['y'], null); |
98 }); | 95 }); |
99 | 96 |
100 test('write global field', () { | 97 test('write global field', () { |
101 context['y'] = 42; | 98 context['y'] = 42; |
102 expect(context['y'], 42); | 99 expect(context['y'], 42); |
103 }); | 100 }); |
104 | |
105 }); | 101 }); |
106 | 102 |
107 group('new JsObject()', () { | 103 group('new JsObject()', () { |
108 | |
109 test('new Foo()', () { | 104 test('new Foo()', () { |
110 var foo = new JsObject(context['Foo'], [42]); | 105 var foo = new JsObject(context['Foo'], [42]); |
111 expect(foo['a'], 42); | 106 expect(foo['a'], 42); |
112 expect(foo.callMethod('bar'), 42); | 107 expect(foo.callMethod('bar'), 42); |
113 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); | 108 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); |
114 }); | 109 }); |
115 | 110 |
116 test('new container.Foo()', () { | 111 test('new container.Foo()', () { |
117 final Foo2 = context['container']['Foo']; | 112 final Foo2 = context['container']['Foo']; |
118 final foo = new JsObject(Foo2, [42]); | 113 final foo = new JsObject(Foo2, [42]); |
(...skipping 21 matching lines...) Expand all Loading... |
140 final a = new JsObject(context['Date']); | 135 final a = new JsObject(context['Date']); |
141 expect(a.callMethod('getTime'), isNotNull); | 136 expect(a.callMethod('getTime'), isNotNull); |
142 }); | 137 }); |
143 | 138 |
144 test('new Date(12345678)', () { | 139 test('new Date(12345678)', () { |
145 final a = new JsObject(context['Date'], [12345678]); | 140 final a = new JsObject(context['Date'], [12345678]); |
146 expect(a.callMethod('getTime'), 12345678); | 141 expect(a.callMethod('getTime'), 12345678); |
147 }); | 142 }); |
148 | 143 |
149 test('new Date("December 17, 1995 03:24:00 GMT")', () { | 144 test('new Date("December 17, 1995 03:24:00 GMT")', () { |
150 final a = new JsObject(context['Date'], | 145 final a = |
151 ["December 17, 1995 03:24:00 GMT"]); | 146 new JsObject(context['Date'], ["December 17, 1995 03:24:00 GMT"]); |
152 expect(a.callMethod('getTime'), 819170640000); | 147 expect(a.callMethod('getTime'), 819170640000); |
153 }); | 148 }); |
154 | 149 |
155 test('new Date(1995,11,17)', () { | 150 test('new Date(1995,11,17)', () { |
156 // Note: JS Date counts months from 0 while Dart counts from 1. | 151 // Note: JS Date counts months from 0 while Dart counts from 1. |
157 final a = new JsObject(context['Date'], [1995, 11, 17]); | 152 final a = new JsObject(context['Date'], [1995, 11, 17]); |
158 final b = new DateTime(1995, 12, 17); | 153 final b = new DateTime(1995, 12, 17); |
159 expect(a.callMethod('getTime'), b.millisecondsSinceEpoch); | 154 expect(a.callMethod('getTime'), b.millisecondsSinceEpoch); |
160 }); | 155 }); |
161 | 156 |
162 test('new Date(1995,11,17,3,24,0)', () { | 157 test('new Date(1995,11,17,3,24,0)', () { |
163 // Note: JS Date counts months from 0 while Dart counts from 1. | 158 // Note: JS Date counts months from 0 while Dart counts from 1. |
164 final a = new JsObject(context['Date'], | 159 final a = new JsObject(context['Date'], [1995, 11, 17, 3, 24, 0]); |
165 [1995, 11, 17, 3, 24, 0]); | |
166 final b = new DateTime(1995, 12, 17, 3, 24, 0); | 160 final b = new DateTime(1995, 12, 17, 3, 24, 0); |
167 expect(a.callMethod('getTime'), b.millisecondsSinceEpoch); | 161 expect(a.callMethod('getTime'), b.millisecondsSinceEpoch); |
168 }); | 162 }); |
169 | 163 |
170 test('new Object()', () { | 164 test('new Object()', () { |
171 final a = new JsObject(context['Object']); | 165 final a = new JsObject(context['Object']); |
172 expect(a, isNotNull); | 166 expect(a, isNotNull); |
173 | 167 |
174 a['attr'] = "value"; | 168 a['attr'] = "value"; |
175 expect(a['attr'], "value"); | 169 expect(a['attr'], "value"); |
(...skipping 18 matching lines...) Expand all Loading... |
194 a.callMethod('pop'); | 188 a.callMethod('pop'); |
195 expect(a['length'], 0); | 189 expect(a['length'], 0); |
196 }); | 190 }); |
197 | 191 |
198 test('js instantiation via map notation : new Date()', () { | 192 test('js instantiation via map notation : new Date()', () { |
199 final a = new JsObject(context['Date']); | 193 final a = new JsObject(context['Date']); |
200 expect(a.callMethod('getTime'), isNotNull); | 194 expect(a.callMethod('getTime'), isNotNull); |
201 }); | 195 }); |
202 | 196 |
203 test('>10 parameters', () { | 197 test('>10 parameters', () { |
204 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]); | 198 final o = |
| 199 new JsObject(context['Baz'], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); |
205 for (var i = 1; i <= 11; i++) { | 200 for (var i = 1; i <= 11; i++) { |
206 expect(o["f$i"], i); | 201 expect(o["f$i"], i); |
207 } | 202 } |
208 expect(o['constructor'], context['Baz']); | 203 expect(o['constructor'], context['Baz']); |
209 }); | 204 }); |
210 }); | 205 }); |
211 | 206 |
212 group('JsFunction and callMethod', () { | 207 group('JsFunction and callMethod', () { |
213 | |
214 test('new JsObject can return a JsFunction', () { | 208 test('new JsObject can return a JsFunction', () { |
215 var f = new JsObject(context['Function']); | 209 var f = new JsObject(context['Function']); |
216 expect(f, (a) => a is JsFunction); | 210 expect(f, (a) => a is JsFunction); |
217 }); | 211 }); |
218 | 212 |
219 test('JsFunction.apply on a function defined in JS', () { | 213 test('JsFunction.apply on a function defined in JS', () { |
220 expect(context['razzle'].apply([]), 42); | 214 expect(context['razzle'].apply([]), 42); |
221 }); | 215 }); |
222 | 216 |
223 test('JsFunction.apply on a function that uses "this"', () { | 217 test('JsFunction.apply on a function that uses "this"', () { |
224 var object = new Object(); | 218 var object = new Object(); |
225 expect(context['returnThis'].apply([], thisArg: object), same(object)); | 219 expect(context['returnThis'].apply([], thisArg: object), same(object)); |
226 }); | 220 }); |
227 | 221 |
228 test('JsObject.callMethod on a function defined in JS', () { | 222 test('JsObject.callMethod on a function defined in JS', () { |
229 expect(context.callMethod('razzle'), 42); | 223 expect(context.callMethod('razzle'), 42); |
230 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); | 224 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); |
231 }); | 225 }); |
232 | 226 |
233 test('callMethod with many arguments', () { | 227 test('callMethod with many arguments', () { |
234 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), | 228 expect( |
235 55); | 229 context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 55); |
236 }); | 230 }); |
237 | 231 |
238 test('access a property of a function', () { | 232 test('access a property of a function', () { |
239 expect(context.callMethod('Bar'), "ret_value"); | 233 expect(context.callMethod('Bar'), "ret_value"); |
240 expect(context['Bar']['foo'], "property_value"); | 234 expect(context['Bar']['foo'], "property_value"); |
241 }); | 235 }); |
242 | |
243 }); | 236 }); |
244 | 237 |
245 group('JsArray', () { | 238 group('JsArray', () { |
246 | |
247 test('new JsArray()', () { | 239 test('new JsArray()', () { |
248 var array = new JsArray(); | 240 var array = new JsArray(); |
249 var arrayType = context['Array']; | 241 var arrayType = context['Array']; |
250 expect(array.instanceof(arrayType), true); | 242 expect(array.instanceof(arrayType), true); |
251 expect(array, []); | 243 expect(array, []); |
252 // basic check that it behaves like a List | 244 // basic check that it behaves like a List |
253 array.addAll([1, 2, 3]); | 245 array.addAll([1, 2, 3]); |
254 expect(array, [1, 2, 3]); | 246 expect(array, [1, 2, 3]); |
255 }); | 247 }); |
256 | 248 |
257 test('new JsArray.from()', () { | 249 test('new JsArray.from()', () { |
258 var array = new JsArray.from([1, 2, 3]); | 250 var array = new JsArray.from([1, 2, 3]); |
259 var arrayType = context['Array']; | 251 var arrayType = context['Array']; |
260 expect(array.instanceof(arrayType), true); | 252 expect(array.instanceof(arrayType), true); |
261 expect(array, [1, 2, 3]); | 253 expect(array, [1, 2, 3]); |
262 }); | 254 }); |
263 | 255 |
264 test('get Array from JS', () { | 256 test('get Array from JS', () { |
265 context['a'] = new JsObject(context['Array'], [1, 2, 3]); | 257 context['a'] = new JsObject(context['Array'], [1, 2, 3]); |
266 expect(context.callMethod('isPropertyInstanceOf', | 258 expect( |
267 ['a', context['Array']]), true); | 259 context.callMethod('isPropertyInstanceOf', ['a', context['Array']]), |
| 260 true); |
268 var a = context['a']; | 261 var a = context['a']; |
269 expect(a, (a) => a is JsArray); | 262 expect(a, (a) => a is JsArray); |
270 expect(a, [1, 2, 3]); | 263 expect(a, [1, 2, 3]); |
271 context.deleteProperty('a'); | 264 context.deleteProperty('a'); |
272 }); | 265 }); |
273 | 266 |
274 test('pass Array to JS', () { | 267 test('pass Array to JS', () { |
275 context['a'] = [1, 2, 3]; | 268 context['a'] = [1, 2, 3]; |
276 expect(context.callMethod('isPropertyInstanceOf', | 269 expect( |
277 ['a', context['Array']]), false); | 270 context.callMethod('isPropertyInstanceOf', ['a', context['Array']]), |
| 271 false); |
278 var a = context['a']; | 272 var a = context['a']; |
279 expect(a, (a) => a is List); | 273 expect(a, (a) => a is List); |
280 expect(a, isNot((a) => a is JsArray)); | 274 expect(a, isNot((a) => a is JsArray)); |
281 expect(a, [1, 2, 3]); | 275 expect(a, [1, 2, 3]); |
282 context.deleteProperty('a'); | 276 context.deleteProperty('a'); |
283 }); | 277 }); |
284 | 278 |
285 test('[]', () { | 279 test('[]', () { |
286 var array = new JsArray.from([1, 2]); | 280 var array = new JsArray.from([1, 2]); |
287 expect(array[0], 1); | 281 expect(array[0], 1); |
288 expect(array[1], 2); | 282 expect(array[1], 2); |
289 expect(() => array[-1], throwsA(isRangeError)); | 283 expect(() => array[-1], throwsA(isRangeError)); |
290 expect(() => array[2], throwsA(isRangeError)); | 284 expect(() => array[2], throwsA(isRangeError)); |
291 }); | 285 }); |
292 | 286 |
293 test('[]=', () { | 287 test('[]=', () { |
294 var array = new JsArray.from([1, 2]); | 288 var array = new JsArray.from([1, 2]); |
295 array[0] = 'd'; | 289 array[0] = 'd'; |
296 array[1] = 'e'; | 290 array[1] = 'e'; |
297 expect(array, ['d', 'e']); | 291 expect(array, ['d', 'e']); |
298 expect(() => array[-1] = 3, throwsA(isRangeError)); | 292 expect(() => array[-1] = 3, throwsA(isRangeError)); |
299 expect(() => array[2] = 3, throwsA(isRangeError)); | 293 expect(() => array[2] = 3, throwsA(isRangeError)); |
300 }); | 294 }); |
301 | 295 |
302 test('length', () { | 296 test('length', () { |
303 var array = new JsArray.from([1, 2, 3]); | 297 var array = new JsArray.from([1, 2, 3]); |
304 expect(array.length, 3); | 298 expect(array.length, 3); |
305 array.add(4); | 299 array.add(4); |
306 expect(array.length, 4); | 300 expect(array.length, 4); |
307 array.length = 2; | 301 array.length = 2; |
308 expect(array, [1, 2]); | 302 expect(array, [1, 2]); |
309 array.length = 3; | 303 array.length = 3; |
310 expect(array, [1, 2, null]); | 304 expect(array, [1, 2, null]); |
311 }); | 305 }); |
312 | 306 |
313 test('add', () { | 307 test('add', () { |
314 var array = new JsArray(); | 308 var array = new JsArray(); |
315 array.add('a'); | 309 array.add('a'); |
316 expect(array, ['a']); | 310 expect(array, ['a']); |
317 array.add('b'); | 311 array.add('b'); |
318 expect(array, ['a', 'b']); | 312 expect(array, ['a', 'b']); |
319 }); | 313 }); |
320 | 314 |
321 test('addAll', () { | 315 test('addAll', () { |
322 var array = new JsArray(); | 316 var array = new JsArray(); |
323 array.addAll(['a', 'b']); | 317 array.addAll(['a', 'b']); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 var array = new JsArray.from(['c', 'a', 'b']); | 370 var array = new JsArray.from(['c', 'a', 'b']); |
377 array.sort(); | 371 array.sort(); |
378 expect(array, ['a', 'b', 'c']); | 372 expect(array, ['a', 'b', 'c']); |
379 }); | 373 }); |
380 | 374 |
381 test('sort with a Comparator', () { | 375 test('sort with a Comparator', () { |
382 var array = new JsArray.from(['c', 'a', 'b']); | 376 var array = new JsArray.from(['c', 'a', 'b']); |
383 array.sort((a, b) => -(a.compareTo(b))); | 377 array.sort((a, b) => -(a.compareTo(b))); |
384 expect(array, ['c', 'b', 'a']); | 378 expect(array, ['c', 'b', 'a']); |
385 }); | 379 }); |
386 | |
387 }); | 380 }); |
388 | 381 |
389 group('JsObject.fromBrowserObject()', () { | 382 group('JsObject.fromBrowserObject()', () { |
390 | |
391 test('Nodes are proxied', () { | 383 test('Nodes are proxied', () { |
392 var node = new JsObject.fromBrowserObject(document.createElement('div')); | 384 var node = new JsObject.fromBrowserObject(document.createElement('div')); |
393 context.callMethod('addTestProperty', [node]); | 385 context.callMethod('addTestProperty', [node]); |
394 expect(node is JsObject, true); | 386 expect(node is JsObject, true); |
395 expect(node.instanceof(context['HTMLDivElement']), true); | 387 expect(node.instanceof(context['HTMLDivElement']), true); |
396 expect(node['testProperty'], 'test'); | 388 expect(node['testProperty'], 'test'); |
397 }); | 389 }); |
398 | 390 |
399 test('primitives and null throw ArgumentError', () { | 391 test('primitives and null throw ArgumentError', () { |
400 for (var v in ['a', 1, 2.0, true, null]) { | 392 for (var v in ['a', 1, 2.0, true, null]) { |
401 expect(() => new JsObject.fromBrowserObject(v), | 393 expect(() => new JsObject.fromBrowserObject(v), |
402 throwsA((a) => a is ArgumentError)); | 394 throwsA((a) => a is ArgumentError)); |
403 } | 395 } |
404 }); | 396 }); |
405 | |
406 }); | 397 }); |
407 | 398 |
408 group('Dart functions', () { | 399 group('Dart functions', () { |
409 test('invoke Dart callback from JS', () { | 400 test('invoke Dart callback from JS', () { |
410 expect(() => context.callMethod('invokeCallback'), throws); | 401 expect(() => context.callMethod('invokeCallback'), throws); |
411 | 402 |
412 context['callback'] = () => 42; | 403 context['callback'] = () => 42; |
413 expect(context.callMethod('invokeCallback'), 42); | 404 expect(context.callMethod('invokeCallback'), 42); |
414 | 405 |
415 context.deleteProperty('callback'); | 406 context.deleteProperty('callback'); |
416 }); | 407 }); |
417 | 408 |
418 test('callback as parameter', () { | 409 test('callback as parameter', () { |
419 expect(context.callMethod('getTypeOf', [context['razzle']]), | 410 expect(context.callMethod('getTypeOf', [context['razzle']]), "function"); |
420 "function"); | |
421 }); | 411 }); |
422 | 412 |
423 test('invoke Dart callback from JS with this', () { | 413 test('invoke Dart callback from JS with this', () { |
424 // A JavaScript constructor implemented in Dart using 'this' | 414 // A JavaScript constructor implemented in Dart using 'this' |
425 final constructor = new JsFunction.withThis(($this, arg1) { | 415 final constructor = new JsFunction.withThis(($this, arg1) { |
426 $this['a'] = 42; | 416 $this['a'] = 42; |
427 }); | 417 }); |
428 var o = new JsObject(constructor, ["b"]); | 418 var o = new JsObject(constructor, ["b"]); |
429 expect(o['a'], 42); | 419 expect(o['a'], 42); |
430 }); | 420 }); |
431 | 421 |
432 test('invoke Dart callback from JS with 11 parameters', () { | 422 test('invoke Dart callback from JS with 11 parameters', () { |
433 context['callbackWith11params'] = (p1, p2, p3, p4, p5, p6, p7, | 423 context['callbackWith11params'] = |
434 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'; | 424 (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11) => |
435 expect(context.callMethod('invokeCallbackWith11params'), | 425 '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'; |
436 '1234567891011'); | 426 expect(context.callMethod('invokeCallbackWith11params'), '1234567891011'); |
437 }); | 427 }); |
438 | 428 |
439 test('return a JS proxy to JavaScript', () { | 429 test('return a JS proxy to JavaScript', () { |
440 var result = context.callMethod('testJsMap', | 430 var result = context.callMethod('testJsMap', [ |
441 [() => new JsObject.jsify({'value': 42})]); | 431 () => new JsObject.jsify({'value': 42}) |
| 432 ]); |
442 expect(result, 42); | 433 expect(result, 42); |
443 }); | 434 }); |
444 | 435 |
445 test('emulated functions should be callable in JS', () { | 436 test('emulated functions should be callable in JS', () { |
446 context['callable'] = new Callable(); | 437 context['callable'] = new Callable(); |
447 var result = context.callMethod('callable'); | 438 var result = context.callMethod('callable'); |
448 expect(result, 'called'); | 439 expect(result, 'called'); |
449 context.deleteProperty('callable'); | 440 context.deleteProperty('callable'); |
450 }); | 441 }); |
451 | |
452 }); | 442 }); |
453 | 443 |
454 group('JsObject.jsify()', () { | 444 group('JsObject.jsify()', () { |
455 | |
456 test('convert a List', () { | 445 test('convert a List', () { |
457 final list = [1, 2, 3, 4, 5, 6, 7, 8]; | 446 final list = [1, 2, 3, 4, 5, 6, 7, 8]; |
458 var array = new JsObject.jsify(list); | 447 var array = new JsObject.jsify(list); |
459 expect(context.callMethod('isArray', [array]), true); | 448 expect(context.callMethod('isArray', [array]), true); |
460 expect(array['length'], list.length); | 449 expect(array['length'], list.length); |
461 for (var i = 0; i < list.length ; i++) { | 450 for (var i = 0; i < list.length; i++) { |
462 expect(array[i], list[i]); | 451 expect(array[i], list[i]); |
463 } | 452 } |
464 }); | 453 }); |
465 | 454 |
466 test('convert an Iterable', () { | 455 test('convert an Iterable', () { |
467 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); | 456 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); |
468 var array = new JsObject.jsify(set); | 457 var array = new JsObject.jsify(set); |
469 expect(context.callMethod('isArray', [array]), true); | 458 expect(context.callMethod('isArray', [array]), true); |
470 expect(array['length'], set.length); | 459 expect(array['length'], set.length); |
471 for (var i = 0; i < array['length'] ; i++) { | 460 for (var i = 0; i < array['length']; i++) { |
472 expect(set.contains(array[i]), true); | 461 expect(set.contains(array[i]), true); |
473 } | 462 } |
474 }); | 463 }); |
475 | 464 |
476 test('convert a Map', () { | 465 test('convert a Map', () { |
477 var map = {'a': 1, 'b': 2, 'c': 3}; | 466 var map = {'a': 1, 'b': 2, 'c': 3}; |
478 var jsMap = new JsObject.jsify(map); | 467 var jsMap = new JsObject.jsify(map); |
479 expect(!context.callMethod('isArray', [jsMap]), true); | 468 expect(!context.callMethod('isArray', [jsMap]), true); |
480 for (final key in map.keys) { | 469 for (final key in map.keys) { |
481 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), true); | 470 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), true); |
482 } | 471 } |
483 }); | 472 }); |
484 | 473 |
485 test('deep convert a complex object', () { | 474 test('deep convert a complex object', () { |
486 final object = { | 475 final object = { |
487 'a': [1, [2, 3]], | 476 'a': [ |
| 477 1, |
| 478 [2, 3] |
| 479 ], |
488 'b': { | 480 'b': { |
489 'c': 3, | 481 'c': 3, |
490 'd': new JsObject(context['Foo'], [42]) | 482 'd': new JsObject(context['Foo'], [42]) |
491 }, | 483 }, |
492 'e': null | 484 'e': null |
493 }; | 485 }; |
494 var jsObject = new JsObject.jsify(object); | 486 var jsObject = new JsObject.jsify(object); |
495 expect(jsObject['a'][0], object['a'][0]); | 487 expect(jsObject['a'][0], object['a'][0]); |
496 expect(jsObject['a'][1][0], object['a'][1][0]); | 488 expect(jsObject['a'][1][0], object['a'][1][0]); |
497 expect(jsObject['a'][1][1], object['a'][1][1]); | 489 expect(jsObject['a'][1][1], object['a'][1][1]); |
498 expect(jsObject['b']['c'], object['b']['c']); | 490 expect(jsObject['b']['c'], object['b']['c']); |
499 expect(jsObject['b']['d'], object['b']['d']); | 491 expect(jsObject['b']['d'], object['b']['d']); |
500 expect(jsObject['b']['d'].callMethod('bar'), 42); | 492 expect(jsObject['b']['d'].callMethod('bar'), 42); |
501 expect(jsObject['e'], null); | 493 expect(jsObject['e'], null); |
502 }); | 494 }); |
503 | 495 |
504 test('throws if object is not a Map or Iterable', () { | 496 test('throws if object is not a Map or Iterable', () { |
505 expect(() => new JsObject.jsify('a'), | 497 expect(() => new JsObject.jsify('a'), throwsA((a) => a is ArgumentError)); |
506 throwsA((a) => a is ArgumentError)); | |
507 }); | 498 }); |
508 }); | 499 }); |
509 | 500 |
510 group('JsObject methods', () { | 501 group('JsObject methods', () { |
511 | |
512 test('hashCode and ==', () { | 502 test('hashCode and ==', () { |
513 final o1 = context['Object']; | 503 final o1 = context['Object']; |
514 final o2 = context['Object']; | 504 final o2 = context['Object']; |
515 expect(o1 == o2, true); | 505 expect(o1 == o2, true); |
516 expect(o1.hashCode == o2.hashCode, true); | 506 expect(o1.hashCode == o2.hashCode, true); |
517 final d = context['document']; | 507 final d = context['document']; |
518 expect(o1 == d, false); | 508 expect(o1 == d, false); |
519 }); | 509 }); |
520 | 510 |
521 test('toString', () { | 511 test('toString', () { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 expect(myArray['length'], 1); | 549 expect(myArray['length'], 1); |
560 expect(myArray[0], "value1"); | 550 expect(myArray[0], "value1"); |
561 myArray[0] = "value2"; | 551 myArray[0] = "value2"; |
562 expect(myArray['length'], 1); | 552 expect(myArray['length'], 1); |
563 expect(myArray[0], "value2"); | 553 expect(myArray[0], "value2"); |
564 | 554 |
565 final foo = new JsObject(context['Foo'], [1]); | 555 final foo = new JsObject(context['Foo'], [1]); |
566 foo["getAge"] = () => 10; | 556 foo["getAge"] = () => 10; |
567 expect(foo.callMethod('getAge'), 10); | 557 expect(foo.callMethod('getAge'), 10); |
568 }); | 558 }); |
569 | |
570 }); | 559 }); |
571 | 560 |
572 group('transferrables', () { | 561 group('transferrables', () { |
573 | |
574 group('JS->Dart', () { | 562 group('JS->Dart', () { |
575 | |
576 test('DateTime', () { | 563 test('DateTime', () { |
577 var date = context.callMethod('getNewDate'); | 564 var date = context.callMethod('getNewDate'); |
578 expect(date is DateTime, true); | 565 expect(date is DateTime, true); |
579 }); | 566 }); |
580 | 567 |
581 test('window', () { | 568 test('window', () { |
582 expect(context['window'] is Window, true); | 569 expect(context['window'] is Window, true); |
583 }); | 570 }); |
584 | 571 |
585 test('foreign browser objects should be proxied', () { | 572 test('foreign browser objects should be proxied', () { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 | 613 |
627 test('Event', () { | 614 test('Event', () { |
628 var event = context.callMethod('getNewEvent'); | 615 var event = context.callMethod('getNewEvent'); |
629 expect(event is Event, true); | 616 expect(event is Event, true); |
630 }); | 617 }); |
631 | 618 |
632 test('ImageData', () { | 619 test('ImageData', () { |
633 var node = context.callMethod('getNewImageData'); | 620 var node = context.callMethod('getNewImageData'); |
634 expect(node is ImageData, true); | 621 expect(node is ImageData, true); |
635 }); | 622 }); |
636 | |
637 }); | 623 }); |
638 | 624 |
639 group('Dart->JS', () { | 625 group('Dart->JS', () { |
640 | |
641 test('Date', () { | 626 test('Date', () { |
642 context['o'] = new DateTime(1995, 12, 17); | 627 context['o'] = new DateTime(1995, 12, 17); |
643 var dateType = context['Date']; | 628 var dateType = context['Date']; |
644 expect(context.callMethod('isPropertyInstanceOf', ['o', dateType]), | 629 expect( |
645 true); | 630 context.callMethod('isPropertyInstanceOf', ['o', dateType]), true); |
646 context.deleteProperty('o'); | 631 context.deleteProperty('o'); |
647 }); | 632 }); |
648 | 633 |
649 test('window', () { | 634 test('window', () { |
650 context['o'] = window; | 635 context['o'] = window; |
651 var windowType = context['Window']; | 636 var windowType = context['Window']; |
652 expect(context.callMethod('isPropertyInstanceOf', ['o', windowType]), | 637 expect(context.callMethod('isPropertyInstanceOf', ['o', windowType]), |
653 true); | 638 true); |
654 context.deleteProperty('o'); | 639 context.deleteProperty('o'); |
655 }); | 640 }); |
656 | 641 |
657 test('document', () { | 642 test('document', () { |
658 context['o'] = document; | 643 context['o'] = document; |
659 var documentType = context['Document']; | 644 var documentType = context['Document']; |
660 expect(context.callMethod('isPropertyInstanceOf', ['o', documentType]), | 645 expect(context.callMethod('isPropertyInstanceOf', ['o', documentType]), |
661 true); | 646 true); |
662 context.deleteProperty('o'); | 647 context.deleteProperty('o'); |
663 }); | 648 }); |
664 | 649 |
665 test('Blob', () { | 650 test('Blob', () { |
666 var fileParts = ['<a id="a"><b id="b">hey!</b></a>']; | 651 var fileParts = ['<a id="a"><b id="b">hey!</b></a>']; |
667 context['o'] = new Blob(fileParts, type: 'text/html'); | 652 context['o'] = new Blob(fileParts, type: 'text/html'); |
668 var blobType = context['Blob']; | 653 var blobType = context['Blob']; |
669 expect(context.callMethod('isPropertyInstanceOf', ['o', blobType]), | 654 expect( |
670 true); | 655 context.callMethod('isPropertyInstanceOf', ['o', blobType]), true); |
671 context.deleteProperty('o'); | 656 context.deleteProperty('o'); |
672 }); | 657 }); |
673 | 658 |
674 test('unattached DivElement', () { | 659 test('unattached DivElement', () { |
675 context['o'] = document.createElement('div'); | 660 context['o'] = document.createElement('div'); |
676 var divType = context['HTMLDivElement']; | 661 var divType = context['HTMLDivElement']; |
677 expect(context.callMethod('isPropertyInstanceOf', ['o', divType]), | 662 expect( |
678 true); | 663 context.callMethod('isPropertyInstanceOf', ['o', divType]), true); |
679 context.deleteProperty('o'); | 664 context.deleteProperty('o'); |
680 }); | 665 }); |
681 | 666 |
682 test('Event', () { | 667 test('Event', () { |
683 context['o'] = new CustomEvent('test'); | 668 context['o'] = new CustomEvent('test'); |
684 var eventType = context['Event']; | 669 var eventType = context['Event']; |
685 expect(context.callMethod('isPropertyInstanceOf', ['o', eventType]), | 670 expect( |
686 true); | 671 context.callMethod('isPropertyInstanceOf', ['o', eventType]), true); |
687 context.deleteProperty('o'); | 672 context.deleteProperty('o'); |
688 }); | 673 }); |
689 | 674 |
690 // this test fails in IE9 for very weird, but unknown, reasons | 675 // this test fails in IE9 for very weird, but unknown, reasons |
691 // the expression context['ImageData'] fails if useHtmlConfiguration() | 676 // the expression context['ImageData'] fails if useHtmlConfiguration() |
692 // is called, or if the other tests in this file are enabled | 677 // is called, or if the other tests in this file are enabled |
693 test('ImageData', () { | 678 test('ImageData', () { |
694 CanvasElement canvas = document.createElement('canvas'); | 679 CanvasElement canvas = document.createElement('canvas'); |
695 var ctx = canvas.getContext('2d') as CanvasRenderingContext2D; | 680 var ctx = canvas.getContext('2d') as CanvasRenderingContext2D; |
696 context['o'] = ctx.createImageData(1, 1); | 681 context['o'] = ctx.createImageData(1, 1); |
697 var imageDataType = context['ImageData']; | 682 var imageDataType = context['ImageData']; |
698 expect(context.callMethod('isPropertyInstanceOf', ['o', imageDataType]), | 683 expect(context.callMethod('isPropertyInstanceOf', ['o', imageDataType]), |
699 true); | 684 true); |
700 context.deleteProperty('o'); | 685 context.deleteProperty('o'); |
701 }); | 686 }); |
702 | |
703 }); | 687 }); |
704 }); | 688 }); |
705 } | 689 } |
OLD | NEW |