Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: tests/html/js_test.dart

Issue 41163005: Add JsArray (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: landing jsarray Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/js/dartium/js_dartium.dart ('k') | tools/dom/scripts/systemnative.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 var foo1 = new JsObject(context['Foo'], [1]); 262 var foo1 = new JsObject(context['Foo'], [1]);
263 var foo2 = new JsObject(context['Foo'], [2]); 263 var foo2 = new JsObject(context['Foo'], [2]);
264 context['foo1'] = foo1; 264 context['foo1'] = foo1;
265 context['foo2'] = foo2; 265 context['foo2'] = foo2;
266 expect(foo1, isNot(equals(context['foo2']))); 266 expect(foo1, isNot(equals(context['foo2'])));
267 expect(foo2, equals(context['foo2'])); 267 expect(foo2, equals(context['foo2']));
268 context.deleteProperty('foo1'); 268 context.deleteProperty('foo1');
269 context.deleteProperty('foo2'); 269 context.deleteProperty('foo2');
270 }); 270 });
271 271
272
273 test('retrieve same dart Object', () { 272 test('retrieve same dart Object', () {
274 final obj = new Object(); 273 final obj = new Object();
275 context['obj'] = obj; 274 context['obj'] = obj;
276 expect(context['obj'], same(obj)); 275 expect(context['obj'], same(obj));
277 context.deleteProperty('obj'); 276 context.deleteProperty('obj');
278 }); 277 });
279 278
280 }); 279 });
281 280
282 group('context', () { 281 group('context', () {
(...skipping 25 matching lines...) Expand all
308 }); 307 });
309 308
310 test('new container.Foo()', () { 309 test('new container.Foo()', () {
311 final Foo2 = context['container']['Foo']; 310 final Foo2 = context['container']['Foo'];
312 final foo = new JsObject(Foo2, [42]); 311 final foo = new JsObject(Foo2, [42]);
313 expect(foo['a'], 42); 312 expect(foo['a'], 42);
314 expect(Foo2['b'], 38); 313 expect(Foo2['b'], 38);
315 }); 314 });
316 315
317 test('new Array()', () { 316 test('new Array()', () {
318 final a = new JsObject(context['Array']); 317 var a = new JsObject(context['Array']);
319 expect(a, isNotNull); 318 expect(a, new isInstanceOf<JsArray>());
320 expect(a['length'], equals(0)); 319
320 // Test that the object still behaves via the base JsObject interface.
321 // JsArray specific tests are below.
322 expect(a['length'], 0);
ngeoffray 2013/10/29 11:44:20 How is that supposed to work? JsArray[] is typed a
321 323
322 a.callMethod('push', ["value 1"]); 324 a.callMethod('push', ["value 1"]);
323 expect(a['length'], equals(1)); 325 expect(a['length'], 1);
324 expect(a[0], equals("value 1")); 326 expect(a[0], "value 1");
325 327
326 a.callMethod('pop'); 328 a.callMethod('pop');
327 expect(a['length'], equals(0)); 329 expect(a['length'], 0);
328 }); 330 });
329 331
330 test('new Date()', () { 332 test('new Date()', () {
331 final a = new JsObject(context['Date']); 333 final a = new JsObject(context['Date']);
332 expect(a.callMethod('getTime'), isNotNull); 334 expect(a.callMethod('getTime'), isNotNull);
333 }); 335 });
334 336
335 test('new Date(12345678)', () { 337 test('new Date(12345678)', () {
336 final a = new JsObject(context['Date'], [12345678]); 338 final a = new JsObject(context['Date'], [12345678]);
337 expect(a.callMethod('getTime'), equals(12345678)); 339 expect(a.callMethod('getTime'), equals(12345678));
338 }); 340 });
339 341
340 test('new Date("December 17, 1995 03:24:00 GMT")', 342 test('new Date("December 17, 1995 03:24:00 GMT")', () {
341 () {
342 final a = new JsObject(context['Date'], 343 final a = new JsObject(context['Date'],
343 ["December 17, 1995 03:24:00 GMT"]); 344 ["December 17, 1995 03:24:00 GMT"]);
344 expect(a.callMethod('getTime'), equals(819170640000)); 345 expect(a.callMethod('getTime'), equals(819170640000));
345 }); 346 });
346 347
347 test('new Date(1995,11,17)', () { 348 test('new Date(1995,11,17)', () {
348 // Note: JS Date counts months from 0 while Dart counts from 1. 349 // Note: JS Date counts months from 0 while Dart counts from 1.
349 final a = new JsObject(context['Date'], [1995, 11, 17]); 350 final a = new JsObject(context['Date'], [1995, 11, 17]);
350 final b = new DateTime(1995, 12, 17); 351 final b = new DateTime(1995, 12, 17);
351 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); 352 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch));
352 }); 353 });
353 354
354 test('new Date(1995,11,17,3,24,0)', () { 355 test('new Date(1995,11,17,3,24,0)', () {
355 // Note: JS Date counts months from 0 while Dart counts from 1. 356 // Note: JS Date counts months from 0 while Dart counts from 1.
356 final a = new JsObject(context['Date'], 357 final a = new JsObject(context['Date'],
357 [1995, 11, 17, 3, 24, 0]); 358 [1995, 11, 17, 3, 24, 0]);
358 final b = new DateTime(1995, 12, 17, 3, 24, 0); 359 final b = new DateTime(1995, 12, 17, 3, 24, 0);
359 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); 360 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch));
360 }); 361 });
361 362
362 test('new Object()', () { 363 test('new Object()', () {
363 final a = new JsObject(context['Object']); 364 final a = new JsObject(context['Object']);
364 expect(a, isNotNull); 365 expect(a, isNotNull);
365 366
366 a['attr'] = "value"; 367 a['attr'] = "value";
367 expect(a['attr'], equals("value")); 368 expect(a['attr'], equals("value"));
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]); 413 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]);
413 for (var i = 1; i <= 11; i++) { 414 for (var i = 1; i <= 11; i++) {
414 expect(o["f$i"], i); 415 expect(o["f$i"], i);
415 } 416 }
416 expect(o['constructor'], equals(context['Baz'])); 417 expect(o['constructor'], equals(context['Baz']));
417 }); 418 });
418 }); 419 });
419 420
420 group('JsFunction and callMethod', () { 421 group('JsFunction and callMethod', () {
421 422
423 test('new JsObject can return a JsFunction', () {
424 var f = new JsObject(context['Function']);
425 expect(f, new isInstanceOf<JsFunction>());
426 });
427
422 test('JsFunction.apply on a function defined in JS', () { 428 test('JsFunction.apply on a function defined in JS', () {
423 expect(context['razzle'].apply([]), equals(42)); 429 expect(context['razzle'].apply([]), equals(42));
424 }); 430 });
425 431
426 test('JsFunction.apply on a function that uses "this"', () { 432 test('JsFunction.apply on a function that uses "this"', () {
427 var object = new Object(); 433 var object = new Object();
428 expect(context['returnThis'].apply([], thisArg: object), same(object)); 434 expect(context['returnThis'].apply([], thisArg: object), same(object));
429 }); 435 });
430 436
431 test('JsObject.callMethod on a function defined in JS', () { 437 test('JsObject.callMethod on a function defined in JS', () {
(...skipping 15 matching lines...) Expand all
447 ArgumentError outside of checked mode. In unchecked mode this should just 453 ArgumentError outside of checked mode. In unchecked mode this should just
448 return a NoSuchMethodError as the class lacks a method "true". 454 return a NoSuchMethodError as the class lacks a method "true".
449 455
450 test('callMethod throws if name is not a String or num', () { 456 test('callMethod throws if name is not a String or num', () {
451 expect(() => context.callMethod(true), 457 expect(() => context.callMethod(true),
452 throwsA(new isInstanceOf<ArgumentError>())); 458 throwsA(new isInstanceOf<ArgumentError>()));
453 }); 459 });
454 */ 460 */
455 }); 461 });
456 462
463 group('JsArray', () {
464
465 test('new JsArray()', () {
466 var array = new JsArray();
467 var arrayType = context['Array'];
468 expect(array.instanceof(arrayType), true);
469 expect(array, []);
470 // basic check that it behaves like a List
471 array.addAll([1, 2, 3]);
472 expect(array, [1, 2, 3]);
473 });
474
475 test('new JsArray.from()', () {
476 var array = new JsArray.from([1, 2, 3]);
477 var arrayType = context['Array'];
478 expect(array.instanceof(arrayType), true);
479 expect(array, [1, 2, 3]);
480 });
481
482 test('get Array from JS', () {
483 context['a'] = new JsObject(context['Array'], [1, 2, 3]);
484 expect(context.callMethod('isPropertyInstanceOf',
485 ['a', context['Array']]), isTrue);
486 var a = context['a'];
487 expect(a, new isInstanceOf<JsArray>());
488 expect(a, [1, 2, 3]);
489 context.deleteProperty('a');
490 });
491
492 test('pass Array to JS', () {
493 context['a'] = [1, 2, 3];
494 expect(context.callMethod('isPropertyInstanceOf',
495 ['a', context['Array']]), isFalse);
496 var a = context['a'];
497 expect(a, new isInstanceOf<List>());
498 expect(a, isNot(new isInstanceOf<JsArray>()));
499 expect(a, [1, 2, 3]);
500 context.deleteProperty('a');
501 });
502
503 test('[]', () {
504 var array = new JsArray.from([1, 2]);
505 expect(array[0], 1);
506 expect(array[1], 2);
507 expect(() => array[-1], throwsA(isRangeError));
508 expect(() => array[2], throwsA(isRangeError));
509 });
510
511 test('[]=', () {
512 var array = new JsArray.from([1, 2]);
513 array[0] = 'd';
514 array[1] = 'e';
515 expect(array, ['d', 'e']);
516 expect(() => array[-1] = 3, throwsA(isRangeError));
517 expect(() => array[2] = 3, throwsA(isRangeError));
518 });
519
520 test('length', () {
521 var array = new JsArray.from([1, 2, 3]);
522 expect(array.length, 3);
523 array.add(4);
524 expect(array.length, 4);
525 array.length = 2;
526 expect(array, [1, 2]);
527 array.length = 3;
528 expect(array, [1, 2, null]);
529 });
530
531 test('add', () {
532 var array = new JsArray();
533 array.add('a');
534 expect(array, ['a']);
535 array.add('b');
536 expect(array, ['a', 'b']);
537 });
538
539 test('addAll', () {
540 var array = new JsArray();
541 array.addAll(['a', 'b']);
542 expect(array, ['a', 'b']);
543 // make sure addAll can handle Iterables
544 array.addAll(new Set.from(['c']));
545 expect(array, ['a', 'b', 'c']);
546 });
547
548 test('insert', () {
549 var array = new JsArray.from([]);
550 array.insert(0, 'b');
551 expect(array, ['b']);
552 array.insert(0, 'a');
553 expect(array, ['a', 'b']);
554 array.insert(2, 'c');
555 expect(array, ['a', 'b', 'c']);
556 expect(() => array.insert(4, 'e'), throwsA(isRangeError));
557 expect(() => array.insert(-1, 'e'), throwsA(isRangeError));
558 });
559
560 test('removeAt', () {
561 var array = new JsArray.from(['a', 'b', 'c']);
562 expect(array.removeAt(1), 'b');
563 expect(array, ['a', 'c']);
564 expect(() => array.removeAt(2), throwsA(isRangeError));
565 expect(() => array.removeAt(-1), throwsA(isRangeError));
566 });
567
568 test('removeLast', () {
569 var array = new JsArray.from(['a', 'b', 'c']);
570 expect(array.removeLast(), 'c');
571 expect(array, ['a', 'b']);
572 array.length = 0;
573 expect(() => array.removeLast(), throwsA(isRangeError));
574 });
575
576 test('removeRange', () {
577 var array = new JsArray.from(['a', 'b', 'c', 'd']);
578 array.removeRange(1, 3);
579 expect(array, ['a', 'd']);
580 expect(() => array.removeRange(-1, 2), throwsA(isRangeError));
581 expect(() => array.removeRange(0, 3), throwsA(isRangeError));
582 expect(() => array.removeRange(2, 1), throwsA(isRangeError));
583 });
584
585 test('setRange', () {
586 var array = new JsArray.from(['a', 'b', 'c', 'd']);
587 array.setRange(1, 3, ['e', 'f']);
588 expect(array, ['a', 'e', 'f', 'd']);
589 array.setRange(3, 4, ['g', 'h', 'i'], 1);
590 expect(array, ['a', 'e', 'f', 'h']);
591 });
592
593 test('sort', () {
594 var array = new JsArray.from(['c', 'a', 'b']);
595 array.sort();
596 expect(array, ['a', 'b', 'c']);
597 });
598
599 test('sort with a Comparator', () {
600 var array = new JsArray.from(['c', 'a', 'b']);
601 array.sort((a, b) => -(a.compareTo(b)));
602 expect(array, ['c', 'b', 'a']);
603 });
604
605 });
606
457 group('JsObject.fromBrowserObject()', () { 607 group('JsObject.fromBrowserObject()', () {
458 608
459 test('Nodes are proxied', () { 609 test('Nodes are proxied', () {
460 var node = new JsObject.fromBrowserObject(new DivElement()); 610 var node = new JsObject.fromBrowserObject(new DivElement());
461 context.callMethod('addTestProperty', [node]); 611 context.callMethod('addTestProperty', [node]);
462 expect(node is JsObject, isTrue); 612 expect(node is JsObject, isTrue);
463 // TODO(justinfagnani): make this work in IE9 613 // TODO(justinfagnani): make this work in IE9
464 // expect(node.instanceof(context['HTMLDivElement']), isTrue); 614 // expect(node.instanceof(context['HTMLDivElement']), isTrue);
465 expect(node['testProperty'], 'test'); 615 expect(node['testProperty'], 'test');
466 }); 616 });
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 // is lost when typed arrays are passed between JS and Dart. 939 // is lost when typed arrays are passed between JS and Dart.
790 // expect(context.callMethod('isPropertyInstanceOf', ['o', listType]), 940 // expect(context.callMethod('isPropertyInstanceOf', ['o', listType]),
791 // isTrue); 941 // isTrue);
792 context.deleteProperty('o'); 942 context.deleteProperty('o');
793 } 943 }
794 }); 944 });
795 945
796 }); 946 });
797 }); 947 });
798 } 948 }
OLDNEW
« no previous file with comments | « sdk/lib/js/dartium/js_dartium.dart ('k') | tools/dom/scripts/systemnative.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698