| 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 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 | 228 |
| 229 class Callable { | 229 class Callable { |
| 230 call() => 'called'; | 230 call() => 'called'; |
| 231 } | 231 } |
| 232 | 232 |
| 233 main() { | 233 main() { |
| 234 _injectJs(); | 234 _injectJs(); |
| 235 useHtmlIndividualConfiguration(); | 235 useHtmlIndividualConfiguration(); |
| 236 | 236 |
| 237 group('identity', () { | 237 group('identity', () { |
| 238 | |
| 239 test('context instances should be identical', () { | 238 test('context instances should be identical', () { |
| 240 var c1 = context; | 239 var c1 = context; |
| 241 var c2 = context; | 240 var c2 = context; |
| 242 expect(identical(c1, c2), isTrue); | 241 expect(identical(c1, c2), isTrue); |
| 243 }); | 242 }); |
| 244 | 243 |
| 245 test('identical JS objects should have identical proxies', () { | 244 test('identical JS objects should have identical proxies', () { |
| 246 var o1 = new JsObject(context['Foo'], [1]); | 245 var o1 = new JsObject(context['Foo'], [1]); |
| 247 context['f1'] = o1; | 246 context['f1'] = o1; |
| 248 var o2 = context['f1']; | 247 var o2 = context['f1']; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 group('caching', () { | 291 group('caching', () { |
| 293 test('JS->Dart', () { | 292 test('JS->Dart', () { |
| 294 // Test that we are not pulling cached proxy from the prototype | 293 // Test that we are not pulling cached proxy from the prototype |
| 295 // when asking for a proxy for the object. | 294 // when asking for a proxy for the object. |
| 296 final proto = context['someProto']; | 295 final proto = context['someProto']; |
| 297 expect(proto['role'], equals('proto')); | 296 expect(proto['role'], equals('proto')); |
| 298 final obj = context['someObject']; | 297 final obj = context['someObject']; |
| 299 expect(obj['role'], equals('object')); | 298 expect(obj['role'], equals('object')); |
| 300 }); | 299 }); |
| 301 }); | 300 }); |
| 302 | |
| 303 }); | 301 }); |
| 304 | 302 |
| 305 group('context', () { | 303 group('context', () { |
| 306 | |
| 307 test('read global field', () { | 304 test('read global field', () { |
| 308 expect(context['x'], equals(42)); | 305 expect(context['x'], equals(42)); |
| 309 expect(context['y'], isNull); | 306 expect(context['y'], isNull); |
| 310 }); | 307 }); |
| 311 | 308 |
| 312 test('read global field with underscore', () { | 309 test('read global field with underscore', () { |
| 313 expect(context['_x'], equals(123)); | 310 expect(context['_x'], equals(123)); |
| 314 expect(context['y'], isNull); | 311 expect(context['y'], isNull); |
| 315 }); | 312 }); |
| 316 | 313 |
| 317 test('write global field', () { | 314 test('write global field', () { |
| 318 context['y'] = 42; | 315 context['y'] = 42; |
| 319 expect(context['y'], equals(42)); | 316 expect(context['y'], equals(42)); |
| 320 }); | 317 }); |
| 321 | |
| 322 }); | 318 }); |
| 323 | 319 |
| 324 group('new_JsObject', () { | 320 group('new_JsObject', () { |
| 325 | |
| 326 test('new Foo()', () { | 321 test('new Foo()', () { |
| 327 var foo = new JsObject(context['Foo'], [42]); | 322 var foo = new JsObject(context['Foo'], [42]); |
| 328 expect(foo['a'], equals(42)); | 323 expect(foo['a'], equals(42)); |
| 329 expect(foo.callMethod('bar'), equals(42)); | 324 expect(foo.callMethod('bar'), equals(42)); |
| 330 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); | 325 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); |
| 331 }); | 326 }); |
| 332 | 327 |
| 333 test('new container.Foo()', () { | 328 test('new container.Foo()', () { |
| 334 final Foo2 = context['container']['Foo']; | 329 final Foo2 = context['container']['Foo']; |
| 335 final foo = new JsObject(Foo2, [42]); | 330 final foo = new JsObject(Foo2, [42]); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 357 final a = new JsObject(context['Date']); | 352 final a = new JsObject(context['Date']); |
| 358 expect(a.callMethod('getTime'), isNotNull); | 353 expect(a.callMethod('getTime'), isNotNull); |
| 359 }); | 354 }); |
| 360 | 355 |
| 361 test('new Date(12345678)', () { | 356 test('new Date(12345678)', () { |
| 362 final a = new JsObject(context['Date'], [12345678]); | 357 final a = new JsObject(context['Date'], [12345678]); |
| 363 expect(a.callMethod('getTime'), equals(12345678)); | 358 expect(a.callMethod('getTime'), equals(12345678)); |
| 364 }); | 359 }); |
| 365 | 360 |
| 366 test('new Date("December 17, 1995 03:24:00 GMT")', () { | 361 test('new Date("December 17, 1995 03:24:00 GMT")', () { |
| 367 final a = new JsObject(context['Date'], | 362 final a = |
| 368 ["December 17, 1995 03:24:00 GMT"]); | 363 new JsObject(context['Date'], ["December 17, 1995 03:24:00 GMT"]); |
| 369 expect(a.callMethod('getTime'), equals(819170640000)); | 364 expect(a.callMethod('getTime'), equals(819170640000)); |
| 370 }); | 365 }); |
| 371 | 366 |
| 372 test('new Date(1995,11,17)', () { | 367 test('new Date(1995,11,17)', () { |
| 373 // Note: JS Date counts months from 0 while Dart counts from 1. | 368 // Note: JS Date counts months from 0 while Dart counts from 1. |
| 374 final a = new JsObject(context['Date'], [1995, 11, 17]); | 369 final a = new JsObject(context['Date'], [1995, 11, 17]); |
| 375 final b = new DateTime(1995, 12, 17); | 370 final b = new DateTime(1995, 12, 17); |
| 376 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); | 371 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); |
| 377 }); | 372 }); |
| 378 | 373 |
| 379 test('new Date(1995,11,17,3,24,0)', () { | 374 test('new Date(1995,11,17,3,24,0)', () { |
| 380 // Note: JS Date counts months from 0 while Dart counts from 1. | 375 // Note: JS Date counts months from 0 while Dart counts from 1. |
| 381 final a = new JsObject(context['Date'], | 376 final a = new JsObject(context['Date'], [1995, 11, 17, 3, 24, 0]); |
| 382 [1995, 11, 17, 3, 24, 0]); | |
| 383 final b = new DateTime(1995, 12, 17, 3, 24, 0); | 377 final b = new DateTime(1995, 12, 17, 3, 24, 0); |
| 384 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); | 378 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); |
| 385 }); | 379 }); |
| 386 | 380 |
| 387 test('new Object()', () { | 381 test('new Object()', () { |
| 388 final a = new JsObject(context['Object']); | 382 final a = new JsObject(context['Object']); |
| 389 expect(a, isNotNull); | 383 expect(a, isNotNull); |
| 390 | 384 |
| 391 a['attr'] = "value"; | 385 a['attr'] = "value"; |
| 392 expect(a['attr'], equals("value")); | 386 expect(a['attr'], equals("value")); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); | 421 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); |
| 428 final bufView = new JsObject(context['Uint8Array'], [buf]); | 422 final bufView = new JsObject(context['Uint8Array'], [buf]); |
| 429 for (var i = 0; i < codeUnits.length; i++) { | 423 for (var i = 0; i < codeUnits.length; i++) { |
| 430 bufView[i] = codeUnits[i]; | 424 bufView[i] = codeUnits[i]; |
| 431 } | 425 } |
| 432 } | 426 } |
| 433 } | 427 } |
| 434 }); | 428 }); |
| 435 | 429 |
| 436 test('>10 parameters', () { | 430 test('>10 parameters', () { |
| 437 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]); | 431 final o = |
| 432 new JsObject(context['Baz'], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); |
| 438 for (var i = 1; i <= 11; i++) { | 433 for (var i = 1; i <= 11; i++) { |
| 439 expect(o["f$i"], i); | 434 expect(o["f$i"], i); |
| 440 } | 435 } |
| 441 expect(o['constructor'], equals(context['Baz'])); | 436 expect(o['constructor'], equals(context['Baz'])); |
| 442 }); | 437 }); |
| 443 }); | 438 }); |
| 444 | 439 |
| 445 group('JsFunction and callMethod', () { | 440 group('JsFunction and callMethod', () { |
| 446 | |
| 447 test('new JsObject can return a JsFunction', () { | 441 test('new JsObject can return a JsFunction', () { |
| 448 var f = new JsObject(context['Function']); | 442 var f = new JsObject(context['Function']); |
| 449 expect(f, new isInstanceOf<JsFunction>()); | 443 expect(f, new isInstanceOf<JsFunction>()); |
| 450 }); | 444 }); |
| 451 | 445 |
| 452 test('JsFunction.apply on a function defined in JS', () { | 446 test('JsFunction.apply on a function defined in JS', () { |
| 453 expect(context['razzle'].apply([]), equals(42)); | 447 expect(context['razzle'].apply([]), equals(42)); |
| 454 }); | 448 }); |
| 455 | 449 |
| 456 test('JsFunction.apply on a function that uses this', () { | 450 test('JsFunction.apply on a function that uses this', () { |
| 457 var object = new Object(); | 451 var object = new Object(); |
| 458 expect(context['returnThis'].apply([], thisArg: object), same(object)); | 452 expect(context['returnThis'].apply([], thisArg: object), same(object)); |
| 459 }); | 453 }); |
| 460 | 454 |
| 461 test('JsObject.callMethod on a function defined in JS', () { | 455 test('JsObject.callMethod on a function defined in JS', () { |
| 462 expect(context.callMethod('razzle'), equals(42)); | 456 expect(context.callMethod('razzle'), equals(42)); |
| 463 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); | 457 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); |
| 464 }); | 458 }); |
| 465 | 459 |
| 466 test('callMethod with many arguments', () { | 460 test('callMethod with many arguments', () { |
| 467 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), | 461 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), |
| 468 equals(55)); | 462 equals(55)); |
| 469 }); | 463 }); |
| 470 | 464 |
| 471 test('access a property of a function', () { | 465 test('access a property of a function', () { |
| 472 expect(context.callMethod('Bar'), "ret_value"); | 466 expect(context.callMethod('Bar'), "ret_value"); |
| 473 expect(context['Bar']['foo'], "property_value"); | 467 expect(context['Bar']['foo'], "property_value"); |
| 474 }); | 468 }); |
| 475 /* | 469 /* |
| 476 TODO(jacobr): evaluate whether we should be in the business of throwing | 470 TODO(jacobr): evaluate whether we should be in the business of throwing |
| 477 ArgumentError outside of checked mode. In unchecked mode this should just | 471 ArgumentError outside of checked mode. In unchecked mode this should just |
| 478 return a NoSuchMethodError as the class lacks a method "true". | 472 return a NoSuchMethodError as the class lacks a method "true". |
| 479 | 473 |
| 480 test('callMethod throws if name is not a String or num', () { | 474 test('callMethod throws if name is not a String or num', () { |
| 481 expect(() => context.callMethod(true), | 475 expect(() => context.callMethod(true), |
| 482 throwsA(new isInstanceOf<ArgumentError>())); | 476 throwsA(new isInstanceOf<ArgumentError>())); |
| 483 }); | 477 }); |
| 484 */ | 478 */ |
| 485 }); | 479 }); |
| 486 | 480 |
| 487 group('JsArray', () { | 481 group('JsArray', () { |
| 488 | |
| 489 test('new JsArray()', () { | 482 test('new JsArray()', () { |
| 490 var array = new JsArray(); | 483 var array = new JsArray(); |
| 491 var arrayType = context['Array']; | 484 var arrayType = context['Array']; |
| 492 expect(array.instanceof(arrayType), true); | 485 expect(array.instanceof(arrayType), true); |
| 493 expect(array, []); | 486 expect(array, []); |
| 494 // basic check that it behaves like a List | 487 // basic check that it behaves like a List |
| 495 array.addAll([1, 2, 3]); | 488 array.addAll([1, 2, 3]); |
| 496 expect(array, [1, 2, 3]); | 489 expect(array, [1, 2, 3]); |
| 497 }); | 490 }); |
| 498 | 491 |
| 499 test('new JsArray.from()', () { | 492 test('new JsArray.from()', () { |
| 500 var array = new JsArray.from([1, 2, 3]); | 493 var array = new JsArray.from([1, 2, 3]); |
| 501 var arrayType = context['Array']; | 494 var arrayType = context['Array']; |
| 502 expect(array.instanceof(arrayType), true); | 495 expect(array.instanceof(arrayType), true); |
| 503 expect(array, [1, 2, 3]); | 496 expect(array, [1, 2, 3]); |
| 504 }); | 497 }); |
| 505 | 498 |
| 506 test('get Array from JS', () { | 499 test('get Array from JS', () { |
| 507 context['a'] = new JsObject(context['Array'], [1, 2, 3]); | 500 context['a'] = new JsObject(context['Array'], [1, 2, 3]); |
| 508 expect(context.callMethod('isPropertyInstanceOf', | 501 expect( |
| 509 ['a', context['Array']]), isTrue); | 502 context.callMethod('isPropertyInstanceOf', ['a', context['Array']]), |
| 503 isTrue); |
| 510 var a = context['a']; | 504 var a = context['a']; |
| 511 expect(a, new isInstanceOf<JsArray>()); | 505 expect(a, new isInstanceOf<JsArray>()); |
| 512 expect(a, [1, 2, 3]); | 506 expect(a, [1, 2, 3]); |
| 513 context.deleteProperty('a'); | 507 context.deleteProperty('a'); |
| 514 }); | 508 }); |
| 515 | 509 |
| 516 test('pass Array to JS', () { | 510 test('pass Array to JS', () { |
| 517 context['a'] = [1, 2, 3]; | 511 context['a'] = [1, 2, 3]; |
| 518 var a = context['a']; | 512 var a = context['a']; |
| 519 expect(a, new isInstanceOf<List>()); | 513 expect(a, new isInstanceOf<List>()); |
| 520 expect(a, isNot(new isInstanceOf<JsArray>())); | 514 expect(a, isNot(new isInstanceOf<JsArray>())); |
| 521 expect(a, [1, 2, 3]); | 515 expect(a, [1, 2, 3]); |
| 522 context.deleteProperty('a'); | 516 context.deleteProperty('a'); |
| 523 }); | 517 }); |
| 524 | 518 |
| 525 test('[]', () { | 519 test('[]', () { |
| 526 var array = new JsArray.from([1, 2]); | 520 var array = new JsArray.from([1, 2]); |
| 527 expect(array[0], 1); | 521 expect(array[0], 1); |
| 528 expect(array[1], 2); | 522 expect(array[1], 2); |
| 529 expect(() => array[-1], throwsA(isRangeError)); | 523 expect(() => array[-1], throwsA(isRangeError)); |
| 530 expect(() => array[2], throwsA(isRangeError)); | 524 expect(() => array[2], throwsA(isRangeError)); |
| 531 }); | 525 }); |
| 532 | 526 |
| 533 test('[]=', () { | 527 test('[]=', () { |
| 534 var array = new JsArray.from([1, 2]); | 528 var array = new JsArray.from([1, 2]); |
| 535 array[0] = 'd'; | 529 array[0] = 'd'; |
| 536 array[1] = 'e'; | 530 array[1] = 'e'; |
| 537 expect(array, ['d', 'e']); | 531 expect(array, ['d', 'e']); |
| 538 expect(() => array[-1] = 3, throwsA(isRangeError)); | 532 expect(() => array[-1] = 3, throwsA(isRangeError)); |
| 539 expect(() => array[2] = 3, throwsA(isRangeError)); | 533 expect(() => array[2] = 3, throwsA(isRangeError)); |
| 540 }); | 534 }); |
| 541 | 535 |
| 542 test('length', () { | 536 test('length', () { |
| 543 var array = new JsArray.from([1, 2, 3]); | 537 var array = new JsArray.from([1, 2, 3]); |
| 544 expect(array.length, 3); | 538 expect(array.length, 3); |
| 545 array.add(4); | 539 array.add(4); |
| 546 expect(array.length, 4); | 540 expect(array.length, 4); |
| 547 array.length = 2; | 541 array.length = 2; |
| 548 expect(array, [1, 2]); | 542 expect(array, [1, 2]); |
| 549 array.length = 3; | 543 array.length = 3; |
| 550 expect(array, [1, 2, null]); | 544 expect(array, [1, 2, null]); |
| 551 }); | 545 }); |
| 552 | 546 |
| 553 test('add', () { | 547 test('add', () { |
| 554 var array = new JsArray(); | 548 var array = new JsArray(); |
| 555 array.add('a'); | 549 array.add('a'); |
| 556 expect(array, ['a']); | 550 expect(array, ['a']); |
| 557 array.add('b'); | 551 array.add('b'); |
| 558 expect(array, ['a', 'b']); | 552 expect(array, ['a', 'b']); |
| 559 }); | 553 }); |
| 560 | 554 |
| 561 test('addAll', () { | 555 test('addAll', () { |
| 562 var array = new JsArray(); | 556 var array = new JsArray(); |
| 563 array.addAll(['a', 'b']); | 557 array.addAll(['a', 'b']); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 var array = new JsArray.from(['c', 'a', 'b']); | 610 var array = new JsArray.from(['c', 'a', 'b']); |
| 617 array.sort(); | 611 array.sort(); |
| 618 expect(array, ['a', 'b', 'c']); | 612 expect(array, ['a', 'b', 'c']); |
| 619 }); | 613 }); |
| 620 | 614 |
| 621 test('sort with a Comparator', () { | 615 test('sort with a Comparator', () { |
| 622 var array = new JsArray.from(['c', 'a', 'b']); | 616 var array = new JsArray.from(['c', 'a', 'b']); |
| 623 array.sort((a, b) => -(a.compareTo(b))); | 617 array.sort((a, b) => -(a.compareTo(b))); |
| 624 expect(array, ['c', 'b', 'a']); | 618 expect(array, ['c', 'b', 'a']); |
| 625 }); | 619 }); |
| 626 | |
| 627 }); | 620 }); |
| 628 | 621 |
| 629 group('JsObject.fromBrowserObject()', () { | 622 group('JsObject.fromBrowserObject()', () { |
| 630 | |
| 631 test('Nodes are proxied', () { | 623 test('Nodes are proxied', () { |
| 632 var node = new JsObject.fromBrowserObject(new DivElement()); | 624 var node = new JsObject.fromBrowserObject(new DivElement()); |
| 633 context.callMethod('addTestProperty', [node]); | 625 context.callMethod('addTestProperty', [node]); |
| 634 expect(node is JsObject, isTrue); | 626 expect(node is JsObject, isTrue); |
| 635 // TODO(justinfagnani): make this work in IE9 | 627 // TODO(justinfagnani): make this work in IE9 |
| 636 // expect(node.instanceof(context['HTMLDivElement']), isTrue); | 628 // expect(node.instanceof(context['HTMLDivElement']), isTrue); |
| 637 expect(node['testProperty'], 'test'); | 629 expect(node['testProperty'], 'test'); |
| 638 }); | 630 }); |
| 639 | 631 |
| 640 test('primitives and null throw ArgumentError', () { | 632 test('primitives and null throw ArgumentError', () { |
| 641 for (var v in ['a', 1, 2.0, true, null]) { | 633 for (var v in ['a', 1, 2.0, true, null]) { |
| 642 expect(() => new JsObject.fromBrowserObject(v), | 634 expect(() => new JsObject.fromBrowserObject(v), |
| 643 throwsA(new isInstanceOf<ArgumentError>())); | 635 throwsA(new isInstanceOf<ArgumentError>())); |
| 644 } | 636 } |
| 645 }); | 637 }); |
| 646 | |
| 647 }); | 638 }); |
| 648 | 639 |
| 649 group('Dart_functions', () { | 640 group('Dart_functions', () { |
| 650 test('invoke Dart callback from JS', () { | 641 test('invoke Dart callback from JS', () { |
| 651 expect(() => context.callMethod('invokeCallback'), throws); | 642 expect(() => context.callMethod('invokeCallback'), throws); |
| 652 | 643 |
| 653 context['callback'] = () => 42; | 644 context['callback'] = () => 42; |
| 654 expect(context.callMethod('invokeCallback'), equals(42)); | 645 expect(context.callMethod('invokeCallback'), equals(42)); |
| 655 | 646 |
| 656 context.deleteProperty('callback'); | 647 context.deleteProperty('callback'); |
| 657 }); | 648 }); |
| 658 | 649 |
| 659 test('callback as parameter', () { | 650 test('callback as parameter', () { |
| 660 expect(context.callMethod('getTypeOf', [context['razzle']]), | 651 expect(context.callMethod('getTypeOf', [context['razzle']]), |
| 661 equals("function")); | 652 equals("function")); |
| 662 }); | 653 }); |
| 663 | 654 |
| 664 test('invoke Dart callback from JS with this', () { | 655 test('invoke Dart callback from JS with this', () { |
| 665 // A JavaScript constructor function implemented in Dart which | 656 // A JavaScript constructor function implemented in Dart which |
| 666 // uses 'this' | 657 // uses 'this' |
| 667 final constructor = new JsFunction.withThis(($this, arg1) { | 658 final constructor = new JsFunction.withThis(($this, arg1) { |
| 668 var t = $this; | 659 var t = $this; |
| 669 $this['a'] = 42; | 660 $this['a'] = 42; |
| 670 }); | 661 }); |
| 671 var o = new JsObject(constructor, ["b"]); | 662 var o = new JsObject(constructor, ["b"]); |
| 672 expect(o['a'], equals(42)); | 663 expect(o['a'], equals(42)); |
| 673 }); | 664 }); |
| 674 | 665 |
| 675 test('invoke Dart callback from JS with 11 parameters', () { | 666 test('invoke Dart callback from JS with 11 parameters', () { |
| 676 context['callbackWith11params'] = (p1, p2, p3, p4, p5, p6, p7, | 667 context['callbackWith11params'] = |
| 677 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'; | 668 (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11) => |
| 669 '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'; |
| 678 expect(context.callMethod('invokeCallbackWith11params'), | 670 expect(context.callMethod('invokeCallbackWith11params'), |
| 679 equals('1234567891011')); | 671 equals('1234567891011')); |
| 680 }); | 672 }); |
| 681 | 673 |
| 682 test('return a JS proxy to JavaScript', () { | 674 test('return a JS proxy to JavaScript', () { |
| 683 var result = context.callMethod('testJsMap', [() => new JsObject.jsify({'v
alue': 42})]); | 675 var result = context.callMethod('testJsMap', [ |
| 676 () => new JsObject.jsify({'value': 42}) |
| 677 ]); |
| 684 expect(result, 42); | 678 expect(result, 42); |
| 685 }); | 679 }); |
| 686 | 680 |
| 687 test('emulated functions should be callable in JS', () { | 681 test('emulated functions should be callable in JS', () { |
| 688 context['callable'] = new Callable(); | 682 context['callable'] = new Callable(); |
| 689 var result = context.callMethod('callable'); | 683 var result = context.callMethod('callable'); |
| 690 expect(result, 'called'); | 684 expect(result, 'called'); |
| 691 context.deleteProperty('callable'); | 685 context.deleteProperty('callable'); |
| 692 }); | 686 }); |
| 693 | |
| 694 }); | 687 }); |
| 695 | 688 |
| 696 group('JsObject.jsify()', () { | 689 group('JsObject.jsify()', () { |
| 697 | |
| 698 test('convert a List', () { | 690 test('convert a List', () { |
| 699 final list = [1, 2, 3, 4, 5, 6, 7, 8]; | 691 final list = [1, 2, 3, 4, 5, 6, 7, 8]; |
| 700 var array = new JsObject.jsify(list); | 692 var array = new JsObject.jsify(list); |
| 701 expect(context.callMethod('isArray', [array]), isTrue); | 693 expect(context.callMethod('isArray', [array]), isTrue); |
| 702 expect(array['length'], equals(list.length)); | 694 expect(array['length'], equals(list.length)); |
| 703 for (var i = 0; i < list.length ; i++) { | 695 for (var i = 0; i < list.length; i++) { |
| 704 expect(array[i], equals(list[i])); | 696 expect(array[i], equals(list[i])); |
| 705 } | 697 } |
| 706 }); | 698 }); |
| 707 | 699 |
| 708 test('convert an Iterable', () { | 700 test('convert an Iterable', () { |
| 709 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); | 701 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); |
| 710 var array = new JsObject.jsify(set); | 702 var array = new JsObject.jsify(set); |
| 711 expect(context.callMethod('isArray', [array]), isTrue); | 703 expect(context.callMethod('isArray', [array]), isTrue); |
| 712 expect(array['length'], equals(set.length)); | 704 expect(array['length'], equals(set.length)); |
| 713 for (var i = 0; i < array['length'] ; i++) { | 705 for (var i = 0; i < array['length']; i++) { |
| 714 expect(set.contains(array[i]), isTrue); | 706 expect(set.contains(array[i]), isTrue); |
| 715 } | 707 } |
| 716 }); | 708 }); |
| 717 | 709 |
| 718 test('convert a Map', () { | 710 test('convert a Map', () { |
| 719 var map = {'a': 1, 'b': 2, 'c': 3}; | 711 var map = {'a': 1, 'b': 2, 'c': 3}; |
| 720 var jsMap = new JsObject.jsify(map); | 712 var jsMap = new JsObject.jsify(map); |
| 721 expect(!context.callMethod('isArray', [jsMap]), isTrue); | 713 expect(!context.callMethod('isArray', [jsMap]), isTrue); |
| 722 for (final key in map.keys) { | 714 for (final key in map.keys) { |
| 723 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue); | 715 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue); |
| 724 } | 716 } |
| 725 }); | 717 }); |
| 726 | 718 |
| 727 test('deep convert a complex object', () { | 719 test('deep convert a complex object', () { |
| 728 final object = { | 720 final object = { |
| 729 'a': [1, [2, 3]], | 721 'a': [ |
| 722 1, |
| 723 [2, 3] |
| 724 ], |
| 730 'b': { | 725 'b': { |
| 731 'c': 3, | 726 'c': 3, |
| 732 'd': new JsObject(context['Foo'], [42]) | 727 'd': new JsObject(context['Foo'], [42]) |
| 733 }, | 728 }, |
| 734 'e': null | 729 'e': null |
| 735 }; | 730 }; |
| 736 var jsObject = new JsObject.jsify(object); | 731 var jsObject = new JsObject.jsify(object); |
| 737 expect(jsObject['a'][0], equals(object['a'][0])); | 732 expect(jsObject['a'][0], equals(object['a'][0])); |
| 738 expect(jsObject['a'][1][0], equals(object['a'][1][0])); | 733 expect(jsObject['a'][1][0], equals(object['a'][1][0])); |
| 739 expect(jsObject['a'][1][1], equals(object['a'][1][1])); | 734 expect(jsObject['a'][1][1], equals(object['a'][1][1])); |
| 740 expect(jsObject['b']['c'], equals(object['b']['c'])); | 735 expect(jsObject['b']['c'], equals(object['b']['c'])); |
| 741 expect(jsObject['b']['d'], equals(object['b']['d'])); | 736 expect(jsObject['b']['d'], equals(object['b']['d'])); |
| 742 expect(jsObject['b']['d'].callMethod('bar'), equals(42)); | 737 expect(jsObject['b']['d'].callMethod('bar'), equals(42)); |
| 743 expect(jsObject['e'], isNull); | 738 expect(jsObject['e'], isNull); |
| 744 }); | 739 }); |
| 745 | 740 |
| 746 test('throws if object is not a Map or Iterable', () { | 741 test('throws if object is not a Map or Iterable', () { |
| 747 expect(() => new JsObject.jsify('a'), | 742 expect(() => new JsObject.jsify('a'), |
| 748 throwsA(new isInstanceOf<ArgumentError>())); | 743 throwsA(new isInstanceOf<ArgumentError>())); |
| 749 }); | 744 }); |
| 750 }); | 745 }); |
| 751 | 746 |
| 752 group('JsObject_methods', () { | 747 group('JsObject_methods', () { |
| 753 | |
| 754 test('hashCode and ==', () { | 748 test('hashCode and ==', () { |
| 755 final o1 = context['Object']; | 749 final o1 = context['Object']; |
| 756 final o2 = context['Object']; | 750 final o2 = context['Object']; |
| 757 expect(o1 == o2, isTrue); | 751 expect(o1 == o2, isTrue); |
| 758 expect(o1.hashCode == o2.hashCode, isTrue); | 752 expect(o1.hashCode == o2.hashCode, isTrue); |
| 759 final d = context['document']; | 753 final d = context['document']; |
| 760 expect(o1 == d, isFalse); | 754 expect(o1 == d, isFalse); |
| 761 }); | 755 }); |
| 762 | 756 |
| 763 test('toString', () { | 757 test('toString', () { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 830 var object = new JsObject.jsify({}); | 824 var object = new JsObject.jsify({}); |
| 831 expect(() => object[true], | 825 expect(() => object[true], |
| 832 throwsA(new isInstanceOf<ArgumentError>())); | 826 throwsA(new isInstanceOf<ArgumentError>())); |
| 833 expect(() => object[true] = 1, | 827 expect(() => object[true] = 1, |
| 834 throwsA(new isInstanceOf<ArgumentError>())); | 828 throwsA(new isInstanceOf<ArgumentError>())); |
| 835 }); | 829 }); |
| 836 */ | 830 */ |
| 837 }); | 831 }); |
| 838 | 832 |
| 839 group('transferrables', () { | 833 group('transferrables', () { |
| 840 | |
| 841 group('JS->Dart', () { | 834 group('JS->Dart', () { |
| 842 | |
| 843 test('DateTime', () { | 835 test('DateTime', () { |
| 844 var date = context.callMethod('getNewDate'); | 836 var date = context.callMethod('getNewDate'); |
| 845 expect(date is DateTime, isTrue); | 837 expect(date is DateTime, isTrue); |
| 846 }); | 838 }); |
| 847 | 839 |
| 848 test('window', () { | 840 test('window', () { |
| 849 expect(context['window'] is Window, isTrue); | 841 expect(context['window'] is Window, isTrue); |
| 850 }); | 842 }); |
| 851 | 843 |
| 852 // Bug: dartbug.com/24520 | 844 // Bug: dartbug.com/24520 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 912 }); | 904 }); |
| 913 | 905 |
| 914 test('typed data: Int32Array', () { | 906 test('typed data: Int32Array', () { |
| 915 if (Platform.supportsTypedData) { | 907 if (Platform.supportsTypedData) { |
| 916 var list = context.callMethod('getNewInt32Array'); | 908 var list = context.callMethod('getNewInt32Array'); |
| 917 print(list); | 909 print(list); |
| 918 expect(list is Int32List, isTrue); | 910 expect(list is Int32List, isTrue); |
| 919 expect(list, orderedEquals([1, 2, 3, 4, 5, 6, 7, 8])); | 911 expect(list, orderedEquals([1, 2, 3, 4, 5, 6, 7, 8])); |
| 920 } | 912 } |
| 921 }); | 913 }); |
| 922 | |
| 923 }); | 914 }); |
| 924 | 915 |
| 925 group('JavaScriptFunction', () { | 916 group('JavaScriptFunction', () { |
| 926 test('is check', () { | 917 test('is check', () { |
| 927 var fn = (String s) => true; | 918 var fn = (String s) => true; |
| 928 var jsFn = allowInterop(fn); | 919 var jsFn = allowInterop(fn); |
| 929 expect(fn is StringToBool, isTrue); | 920 expect(fn is StringToBool, isTrue); |
| 930 expect(jsFn is StringToBool, isTrue); | 921 expect(jsFn is StringToBool, isTrue); |
| 931 expect(jsFn is Function, isTrue); | 922 expect(jsFn is Function, isTrue); |
| 932 expect(jsFn is List, isFalse); | 923 expect(jsFn is List, isFalse); |
| 933 }); | 924 }); |
| 934 }); | 925 }); |
| 935 | 926 |
| 936 group('Dart->JS', () { | 927 group('Dart->JS', () { |
| 937 | |
| 938 test('Date', () { | 928 test('Date', () { |
| 939 context['o'] = new DateTime(1995, 12, 17); | 929 context['o'] = new DateTime(1995, 12, 17); |
| 940 var dateType = context['Date']; | 930 var dateType = context['Date']; |
| 941 expect(context.callMethod('isPropertyInstanceOf', ['o', dateType]), | 931 expect(context.callMethod('isPropertyInstanceOf', ['o', dateType]), |
| 942 isTrue); | 932 isTrue); |
| 943 context.deleteProperty('o'); | 933 context.deleteProperty('o'); |
| 944 }); | 934 }); |
| 945 | 935 |
| 946 skipIE9_test('window', () { | 936 skipIE9_test('window', () { |
| 947 context['o'] = window; | 937 context['o'] = window; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 964 context['o'] = new Blob(fileParts, 'text/html'); | 954 context['o'] = new Blob(fileParts, 'text/html'); |
| 965 var blobType = context['Blob']; | 955 var blobType = context['Blob']; |
| 966 expect(context.callMethod('isPropertyInstanceOf', ['o', blobType]), | 956 expect(context.callMethod('isPropertyInstanceOf', ['o', blobType]), |
| 967 isTrue); | 957 isTrue); |
| 968 context.deleteProperty('o'); | 958 context.deleteProperty('o'); |
| 969 }); | 959 }); |
| 970 | 960 |
| 971 test('unattached DivElement', () { | 961 test('unattached DivElement', () { |
| 972 context['o'] = new DivElement(); | 962 context['o'] = new DivElement(); |
| 973 var divType = context['HTMLDivElement']; | 963 var divType = context['HTMLDivElement']; |
| 974 expect(context.callMethod('isPropertyInstanceOf', ['o', divType]), | 964 expect( |
| 975 isTrue); | 965 context.callMethod('isPropertyInstanceOf', ['o', divType]), isTrue); |
| 976 context.deleteProperty('o'); | 966 context.deleteProperty('o'); |
| 977 }); | 967 }); |
| 978 | 968 |
| 979 test('Event', () { | 969 test('Event', () { |
| 980 context['o'] = new CustomEvent('test'); | 970 context['o'] = new CustomEvent('test'); |
| 981 var eventType = context['Event']; | 971 var eventType = context['Event']; |
| 982 expect(context.callMethod('isPropertyInstanceOf', ['o', eventType]), | 972 expect(context.callMethod('isPropertyInstanceOf', ['o', eventType]), |
| 983 isTrue); | 973 isTrue); |
| 984 context.deleteProperty('o'); | 974 context.deleteProperty('o'); |
| 985 }); | 975 }); |
| 986 | 976 |
| 987 test('KeyRange', () { | 977 test('KeyRange', () { |
| 988 if (IdbFactory.supported) { | 978 if (IdbFactory.supported) { |
| 989 context['o'] = new KeyRange.only(1); | 979 context['o'] = new KeyRange.only(1); |
| 990 var keyRangeType = context['IDBKeyRange']; | 980 var keyRangeType = context['IDBKeyRange']; |
| 991 expect(context.callMethod('isPropertyInstanceOf', ['o', keyRangeType])
, | 981 expect( |
| 982 context.callMethod('isPropertyInstanceOf', ['o', keyRangeType]), |
| 992 isTrue); | 983 isTrue); |
| 993 context.deleteProperty('o'); | 984 context.deleteProperty('o'); |
| 994 } | 985 } |
| 995 }); | 986 }); |
| 996 | 987 |
| 997 // this test fails in IE9 for very weird, but unknown, reasons | 988 // this test fails in IE9 for very weird, but unknown, reasons |
| 998 // the expression context['ImageData'] fails if useHtmlConfiguration() | 989 // the expression context['ImageData'] fails if useHtmlConfiguration() |
| 999 // is called, or if the other tests in this file are enabled | 990 // is called, or if the other tests in this file are enabled |
| 1000 skipIE9_test('ImageData', () { | 991 skipIE9_test('ImageData', () { |
| 1001 var canvas = new CanvasElement(); | 992 var canvas = new CanvasElement(); |
| 1002 var ctx = canvas.getContext('2d'); | 993 var ctx = canvas.getContext('2d'); |
| 1003 context['o'] = ctx.createImageData(1, 1); | 994 context['o'] = ctx.createImageData(1, 1); |
| 1004 var imageDataType = context['ImageData']; | 995 var imageDataType = context['ImageData']; |
| 1005 expect(context.callMethod('isPropertyInstanceOf', ['o', imageDataType]), | 996 expect(context.callMethod('isPropertyInstanceOf', ['o', imageDataType]), |
| 1006 isTrue); | 997 isTrue); |
| 1007 context.deleteProperty('o'); | 998 context.deleteProperty('o'); |
| 1008 }); | 999 }); |
| 1009 | 1000 |
| 1010 test('typed data: Int32List', () { | 1001 test('typed data: Int32List', () { |
| 1011 if (Platform.supportsTypedData) { | 1002 if (Platform.supportsTypedData) { |
| 1012 context['o'] = new Int32List.fromList([1, 2, 3, 4]); | 1003 context['o'] = new Int32List.fromList([1, 2, 3, 4]); |
| 1013 var listType = context['Int32Array']; | 1004 var listType = context['Int32Array']; |
| 1014 // TODO(jacobr): make this test pass. Currently some type information | 1005 // TODO(jacobr): make this test pass. Currently some type information |
| 1015 // is lost when typed arrays are passed between JS and Dart. | 1006 // is lost when typed arrays are passed between JS and Dart. |
| 1016 // expect(context.callMethod('isPropertyInstanceOf', ['o', listType]), | 1007 // expect(context.callMethod('isPropertyInstanceOf', ['o', listType]), |
| 1017 // isTrue); | 1008 // isTrue); |
| 1018 context.deleteProperty('o'); | 1009 context.deleteProperty('o'); |
| 1019 } | 1010 } |
| 1020 }); | 1011 }); |
| 1021 | |
| 1022 }); | 1012 }); |
| 1023 }); | 1013 }); |
| 1024 | |
| 1025 } | 1014 } |
| OLD | NEW |