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

Side by Side Diff: test/browser/js_test.dart

Issue 1243503007: fixes #221, initial sync*, async, async* implementation (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 4 months 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
« no previous file with comments | « test/browser/dom.dart ('k') | test/browser/js_test.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library jsTest;
6
7 import 'dart:js';
8
9 // TODO(jmesserly): get tests from package(s) instead.
10 import 'dom.dart';
11 import 'minitest.dart';
12
13 class Foo {
14 final JsObject _proxy;
15
16 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]);
17
18 JsObject toJs() => _proxy;
19
20 num get a => _proxy['a'];
21 num bar() => _proxy.callMethod('bar');
22 }
23
24 class Color {
25 static final RED = new Color._("red");
26 static final BLUE = new Color._("blue");
27 String _value;
28 Color._(this._value);
29 String toJs() => this._value;
30 }
31
32 class TestDartObject {}
33
34 class Callable {
35 call() => 'called';
36 }
37
38 main() {
39 group('identity', () {
40
41 test('context instances should be identical', () {
42 var c1 = context;
43 var c2 = context;
44 expect(identical(c1, c2), true);
45 });
46
47 // TODO(jacobr): switch from equals to identical when dartium supports
48 // maintaining proxy equality.
49 test('identical JS functions should have equal proxies', () {
50 var f1 = context['Object'];
51 var f2 = context['Object'];
52 expect(f1, equals(f2));
53 });
54
55 // TODO(justinfagnani): old tests duplicate checks above, remove
56 // on test next cleanup pass
57 test('test proxy equality', () {
58 var foo1 = new JsObject(context['Foo'], [1]);
59 var foo2 = new JsObject(context['Foo'], [2]);
60 context['foo1'] = foo1;
61 context['foo2'] = foo2;
62 expect(foo1, isNot(context['foo2']));
63 expect(foo2, context['foo2']);
64 context.deleteProperty('foo1');
65 context.deleteProperty('foo2');
66 });
67
68 test('retrieve same dart Object', () {
69 final obj = new Object();
70 context['obj'] = obj;
71 expect(context['obj'], same(obj));
72 context.deleteProperty('obj');
73 });
74
75 group('caching', () {
76 test('JS->Dart', () {
77 // Test that we are not pulling cached proxy from the prototype
78 // when asking for a proxy for the object.
79 final proto = context['someProto'];
80 expect(proto['role'], 'proto');
81 final obj = context['someObject'];
82 expect(obj['role'], 'object');
83 });
84 });
85
86 });
87
88 group('context', () {
89
90 test('read global field', () {
91 expect(context['x'], 42);
92 expect(context['y'], null);
93 });
94
95 test('read global field with underscore', () {
96 expect(context['_x'], 123);
97 expect(context['y'], null);
98 });
99
100 test('write global field', () {
101 context['y'] = 42;
102 expect(context['y'], 42);
103 });
104
105 });
106
107 group('new JsObject()', () {
108
109 test('new Foo()', () {
110 var foo = new JsObject(context['Foo'], [42]);
111 expect(foo['a'], 42);
112 expect(foo.callMethod('bar'), 42);
113 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError));
114 });
115
116 test('new container.Foo()', () {
117 final Foo2 = context['container']['Foo'];
118 final foo = new JsObject(Foo2, [42]);
119 expect(foo['a'], 42);
120 expect(Foo2['b'], 38);
121 });
122
123 test('new Array()', () {
124 var a = new JsObject(context['Array']);
125 expect(a, (a) => a is JsArray);
126
127 // Test that the object still behaves via the base JsObject interface.
128 // JsArray specific tests are below.
129 expect(a['length'], 0);
130
131 a.callMethod('push', ["value 1"]);
132 expect(a['length'], 1);
133 expect(a[0], "value 1");
134
135 a.callMethod('pop');
136 expect(a['length'], 0);
137 });
138
139 test('new Date()', () {
140 final a = new JsObject(context['Date']);
141 expect(a.callMethod('getTime'), isNotNull);
142 });
143
144 test('new Date(12345678)', () {
145 final a = new JsObject(context['Date'], [12345678]);
146 expect(a.callMethod('getTime'), 12345678);
147 });
148
149 test('new Date("December 17, 1995 03:24:00 GMT")', () {
150 final a = new JsObject(context['Date'],
151 ["December 17, 1995 03:24:00 GMT"]);
152 expect(a.callMethod('getTime'), 819170640000);
153 });
154
155 test('new Date(1995,11,17)', () {
156 // Note: JS Date counts months from 0 while Dart counts from 1.
157 final a = new JsObject(context['Date'], [1995, 11, 17]);
158 final b = new DateTime(1995, 12, 17);
159 expect(a.callMethod('getTime'), b.millisecondsSinceEpoch);
160 });
161
162 test('new Date(1995,11,17,3,24,0)', () {
163 // Note: JS Date counts months from 0 while Dart counts from 1.
164 final a = new JsObject(context['Date'],
165 [1995, 11, 17, 3, 24, 0]);
166 final b = new DateTime(1995, 12, 17, 3, 24, 0);
167 expect(a.callMethod('getTime'), b.millisecondsSinceEpoch);
168 });
169
170 test('new Object()', () {
171 final a = new JsObject(context['Object']);
172 expect(a, isNotNull);
173
174 a['attr'] = "value";
175 expect(a['attr'], "value");
176 });
177
178 test(r'new RegExp("^\w+$")', () {
179 final a = new JsObject(context['RegExp'], [r'^\w+$']);
180 expect(a, isNotNull);
181 expect(a.callMethod('test', ['true']), true);
182 expect(a.callMethod('test', [' false']), false);
183 });
184
185 test('js instantiation via map notation : new Array()', () {
186 final a = new JsObject(context['Array']);
187 expect(a, isNotNull);
188 expect(a['length'], 0);
189
190 a.callMethod('push', ["value 1"]);
191 expect(a['length'], 1);
192 expect(a[0], "value 1");
193
194 a.callMethod('pop');
195 expect(a['length'], 0);
196 });
197
198 test('js instantiation via map notation : new Date()', () {
199 final a = new JsObject(context['Date']);
200 expect(a.callMethod('getTime'), isNotNull);
201 });
202
203 test('>10 parameters', () {
204 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++) {
206 expect(o["f$i"], i);
207 }
208 expect(o['constructor'], context['Baz']);
209 });
210 });
211
212 group('JsFunction and callMethod', () {
213
214 test('new JsObject can return a JsFunction', () {
215 var f = new JsObject(context['Function']);
216 expect(f, (a) => a is JsFunction);
217 });
218
219 test('JsFunction.apply on a function defined in JS', () {
220 expect(context['razzle'].apply([]), 42);
221 });
222
223 test('JsFunction.apply on a function that uses "this"', () {
224 var object = new Object();
225 expect(context['returnThis'].apply([], thisArg: object), same(object));
226 });
227
228 test('JsObject.callMethod on a function defined in JS', () {
229 expect(context.callMethod('razzle'), 42);
230 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError));
231 });
232
233 test('callMethod with many arguments', () {
234 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
235 55);
236 });
237
238 test('access a property of a function', () {
239 expect(context.callMethod('Bar'), "ret_value");
240 expect(context['Bar']['foo'], "property_value");
241 });
242
243 });
244
245 group('JsArray', () {
246
247 test('new JsArray()', () {
248 var array = new JsArray();
249 var arrayType = context['Array'];
250 expect(array.instanceof(arrayType), true);
251 expect(array, []);
252 // basic check that it behaves like a List
253 array.addAll([1, 2, 3]);
254 expect(array, [1, 2, 3]);
255 });
256
257 test('new JsArray.from()', () {
258 var array = new JsArray.from([1, 2, 3]);
259 var arrayType = context['Array'];
260 expect(array.instanceof(arrayType), true);
261 expect(array, [1, 2, 3]);
262 });
263
264 test('get Array from JS', () {
265 context['a'] = new JsObject(context['Array'], [1, 2, 3]);
266 expect(context.callMethod('isPropertyInstanceOf',
267 ['a', context['Array']]), true);
268 var a = context['a'];
269 expect(a, (a) => a is JsArray);
270 expect(a, [1, 2, 3]);
271 context.deleteProperty('a');
272 });
273
274 test('pass Array to JS', () {
275 context['a'] = [1, 2, 3];
276 expect(context.callMethod('isPropertyInstanceOf',
277 ['a', context['Array']]), false);
278 var a = context['a'];
279 expect(a, (a) => a is List);
280 expect(a, isNot((a) => a is JsArray));
281 expect(a, [1, 2, 3]);
282 context.deleteProperty('a');
283 });
284
285 test('[]', () {
286 var array = new JsArray.from([1, 2]);
287 expect(array[0], 1);
288 expect(array[1], 2);
289 expect(() => array[-1], throwsA(isRangeError));
290 expect(() => array[2], throwsA(isRangeError));
291 });
292
293 test('[]=', () {
294 var array = new JsArray.from([1, 2]);
295 array[0] = 'd';
296 array[1] = 'e';
297 expect(array, ['d', 'e']);
298 expect(() => array[-1] = 3, throwsA(isRangeError));
299 expect(() => array[2] = 3, throwsA(isRangeError));
300 });
301
302 test('length', () {
303 var array = new JsArray.from([1, 2, 3]);
304 expect(array.length, 3);
305 array.add(4);
306 expect(array.length, 4);
307 array.length = 2;
308 expect(array, [1, 2]);
309 array.length = 3;
310 expect(array, [1, 2, null]);
311 });
312
313 test('add', () {
314 var array = new JsArray();
315 array.add('a');
316 expect(array, ['a']);
317 array.add('b');
318 expect(array, ['a', 'b']);
319 });
320
321 test('addAll', () {
322 var array = new JsArray();
323 array.addAll(['a', 'b']);
324 expect(array, ['a', 'b']);
325 // make sure addAll can handle Iterables
326 array.addAll(new Set.from(['c']));
327 expect(array, ['a', 'b', 'c']);
328 });
329
330 test('insert', () {
331 var array = new JsArray.from([]);
332 array.insert(0, 'b');
333 expect(array, ['b']);
334 array.insert(0, 'a');
335 expect(array, ['a', 'b']);
336 array.insert(2, 'c');
337 expect(array, ['a', 'b', 'c']);
338 expect(() => array.insert(4, 'e'), throwsA(isRangeError));
339 expect(() => array.insert(-1, 'e'), throwsA(isRangeError));
340 });
341
342 test('removeAt', () {
343 var array = new JsArray.from(['a', 'b', 'c']);
344 expect(array.removeAt(1), 'b');
345 expect(array, ['a', 'c']);
346 expect(() => array.removeAt(2), throwsA(isRangeError));
347 expect(() => array.removeAt(-1), throwsA(isRangeError));
348 });
349
350 test('removeLast', () {
351 var array = new JsArray.from(['a', 'b', 'c']);
352 expect(array.removeLast(), 'c');
353 expect(array, ['a', 'b']);
354 array.length = 0;
355 expect(() => array.removeLast(), throwsA(isRangeError));
356 });
357
358 test('removeRange', () {
359 var array = new JsArray.from(['a', 'b', 'c', 'd']);
360 array.removeRange(1, 3);
361 expect(array, ['a', 'd']);
362 expect(() => array.removeRange(-1, 2), throwsA(isRangeError));
363 expect(() => array.removeRange(0, 3), throwsA(isRangeError));
364 expect(() => array.removeRange(2, 1), throwsA(isRangeError));
365 });
366
367 test('setRange', () {
368 var array = new JsArray.from(['a', 'b', 'c', 'd']);
369 array.setRange(1, 3, ['e', 'f']);
370 expect(array, ['a', 'e', 'f', 'd']);
371 array.setRange(3, 4, ['g', 'h', 'i'], 1);
372 expect(array, ['a', 'e', 'f', 'h']);
373 });
374
375 test('sort', () {
376 var array = new JsArray.from(['c', 'a', 'b']);
377 array.sort();
378 expect(array, ['a', 'b', 'c']);
379 });
380
381 test('sort with a Comparator', () {
382 var array = new JsArray.from(['c', 'a', 'b']);
383 array.sort((a, b) => -(a.compareTo(b)));
384 expect(array, ['c', 'b', 'a']);
385 });
386
387 });
388
389 group('JsObject.fromBrowserObject()', () {
390
391 test('Nodes are proxied', () {
392 var node = new JsObject.fromBrowserObject(document.createElement('div'));
393 context.callMethod('addTestProperty', [node]);
394 expect(node is JsObject, true);
395 expect(node.instanceof(context['HTMLDivElement']), true);
396 expect(node['testProperty'], 'test');
397 });
398
399 test('primitives and null throw ArgumentError', () {
400 for (var v in ['a', 1, 2.0, true, null]) {
401 expect(() => new JsObject.fromBrowserObject(v),
402 throwsA((a) => a is ArgumentError));
403 }
404 });
405
406 });
407
408 group('Dart functions', () {
409 test('invoke Dart callback from JS', () {
410 expect(() => context.callMethod('invokeCallback'), throws);
411
412 context['callback'] = () => 42;
413 expect(context.callMethod('invokeCallback'), 42);
414
415 context.deleteProperty('callback');
416 });
417
418 test('callback as parameter', () {
419 expect(context.callMethod('getTypeOf', [context['razzle']]),
420 "function");
421 });
422
423 test('invoke Dart callback from JS with this', () {
424 // A JavaScript constructor implemented in Dart using 'this'
425 final constructor = new JsFunction.withThis(($this, arg1) {
426 $this['a'] = 42;
427 });
428 var o = new JsObject(constructor, ["b"]);
429 expect(o['a'], 42);
430 });
431
432 test('invoke Dart callback from JS with 11 parameters', () {
433 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';
435 expect(context.callMethod('invokeCallbackWith11params'),
436 '1234567891011');
437 });
438
439 test('return a JS proxy to JavaScript', () {
440 var result = context.callMethod('testJsMap',
441 [() => new JsObject.jsify({'value': 42})]);
442 expect(result, 42);
443 });
444
445 test('emulated functions should be callable in JS', () {
446 context['callable'] = new Callable();
447 var result = context.callMethod('callable');
448 expect(result, 'called');
449 context.deleteProperty('callable');
450 }, skip: "https://github.com/dart-lang/dev_compiler/issues/244");
451
452 });
453
454 group('JsObject.jsify()', () {
455
456 test('convert a List', () {
457 final list = [1, 2, 3, 4, 5, 6, 7, 8];
458 var array = new JsObject.jsify(list);
459 expect(context.callMethod('isArray', [array]), true);
460 expect(array['length'], list.length);
461 for (var i = 0; i < list.length ; i++) {
462 expect(array[i], list[i]);
463 }
464 });
465
466 test('convert an Iterable', () {
467 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]);
468 var array = new JsObject.jsify(set);
469 expect(context.callMethod('isArray', [array]), true);
470 expect(array['length'], set.length);
471 for (var i = 0; i < array['length'] ; i++) {
472 expect(set.contains(array[i]), true);
473 }
474 });
475
476 test('convert a Map', () {
477 var map = {'a': 1, 'b': 2, 'c': 3};
478 var jsMap = new JsObject.jsify(map);
479 expect(!context.callMethod('isArray', [jsMap]), true);
480 for (final key in map.keys) {
481 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), true);
482 }
483 });
484
485 test('deep convert a complex object', () {
486 final object = {
487 'a': [1, [2, 3]],
488 'b': {
489 'c': 3,
490 'd': new JsObject(context['Foo'], [42])
491 },
492 'e': null
493 };
494 var jsObject = new JsObject.jsify(object);
495 expect(jsObject['a'][0], object['a'][0]);
496 expect(jsObject['a'][1][0], object['a'][1][0]);
497 expect(jsObject['a'][1][1], object['a'][1][1]);
498 expect(jsObject['b']['c'], object['b']['c']);
499 expect(jsObject['b']['d'], object['b']['d']);
500 expect(jsObject['b']['d'].callMethod('bar'), 42);
501 expect(jsObject['e'], null);
502 });
503
504 test('throws if object is not a Map or Iterable', () {
505 expect(() => new JsObject.jsify('a'),
506 throwsA((a) => a is ArgumentError));
507 });
508 });
509
510 group('JsObject methods', () {
511
512 test('hashCode and ==', () {
513 final o1 = context['Object'];
514 final o2 = context['Object'];
515 expect(o1 == o2, true);
516 expect(o1.hashCode == o2.hashCode, true);
517 final d = context['document'];
518 expect(o1 == d, false);
519 });
520
521 test('toString', () {
522 var foo = new JsObject(context['Foo'], [42]);
523 expect(foo.toString(), "I'm a Foo a=42");
524 var container = context['container'];
525 expect(container.toString(), "[object Object]");
526 });
527
528 test('toString returns a String even if the JS object does not', () {
529 var foo = new JsObject(context['Liar']);
530 expect(foo.callMethod('toString'), 1);
531 expect(foo.toString(), '1');
532 });
533
534 test('instanceof', () {
535 var foo = new JsObject(context['Foo'], [1]);
536 expect(foo.instanceof(context['Foo']), true);
537 expect(foo.instanceof(context['Object']), true);
538 expect(foo.instanceof(context['String']), false);
539 });
540
541 test('deleteProperty', () {
542 var object = new JsObject.jsify({});
543 object['a'] = 1;
544 expect(context['Object'].callMethod('keys', [object])['length'], 1);
545 expect(context['Object'].callMethod('keys', [object])[0], "a");
546 object.deleteProperty("a");
547 expect(context['Object'].callMethod('keys', [object])['length'], 0);
548 });
549
550 test('hasProperty', () {
551 var object = new JsObject.jsify({});
552 object['a'] = 1;
553 expect(object.hasProperty('a'), true);
554 expect(object.hasProperty('b'), false);
555 });
556
557 test('[] and []=', () {
558 final myArray = context['myArray'];
559 expect(myArray['length'], 1);
560 expect(myArray[0], "value1");
561 myArray[0] = "value2";
562 expect(myArray['length'], 1);
563 expect(myArray[0], "value2");
564
565 final foo = new JsObject(context['Foo'], [1]);
566 foo["getAge"] = () => 10;
567 expect(foo.callMethod('getAge'), 10);
568 });
569
570 });
571
572 group('transferrables', () {
573
574 group('JS->Dart', () {
575
576 test('DateTime', () {
577 var date = context.callMethod('getNewDate');
578 expect(date is DateTime, true);
579 });
580
581 test('window', () {
582 expect(context['window'] is Window, true);
583 });
584
585 test('foreign browser objects should be proxied', () {
586 var iframe = document.createElement('iframe');
587 document.body.appendChild(iframe);
588 var proxy = new JsObject.fromBrowserObject(iframe);
589
590 // Window
591 var contentWindow = proxy['contentWindow'];
592 expect(contentWindow, isNot((a) => a is Window));
593 expect(contentWindow, (a) => a is JsObject);
594
595 // Node
596 var foreignDoc = contentWindow['document'];
597 expect(foreignDoc, isNot((a) => a is Node));
598 expect(foreignDoc, (a) => a is JsObject);
599
600 // Event
601 var clicked = false;
602 foreignDoc['onclick'] = (e) {
603 expect(e, isNot((a) => a is Event));
604 expect(e, (a) => a is JsObject);
605 clicked = true;
606 };
607
608 context.callMethod('fireClickEvent', [contentWindow]);
609 expect(clicked, true);
610 });
611
612 test('document', () {
613 expect(context['document'] is Document, true);
614 });
615
616 test('Blob', () {
617 var blob = context.callMethod('getNewBlob');
618 expect(blob is Blob, true);
619 expect(blob.type, 'text/html');
620 });
621
622 test('unattached DivElement', () {
623 var node = context.callMethod('getNewDivElement');
624 expect(node is DivElement, true);
625 });
626
627 test('Event', () {
628 var event = context.callMethod('getNewEvent');
629 expect(event is Event, true);
630 });
631
632 test('ImageData', () {
633 var node = context.callMethod('getNewImageData');
634 expect(node is ImageData, true);
635 });
636
637 });
638
639 group('Dart->JS', () {
640
641 test('Date', () {
642 context['o'] = new DateTime(1995, 12, 17);
643 var dateType = context['Date'];
644 expect(context.callMethod('isPropertyInstanceOf', ['o', dateType]),
645 true);
646 context.deleteProperty('o');
647 });
648
649 test('window', () {
650 context['o'] = window;
651 var windowType = context['Window'];
652 expect(context.callMethod('isPropertyInstanceOf', ['o', windowType]),
653 true);
654 context.deleteProperty('o');
655 });
656
657 test('document', () {
658 context['o'] = document;
659 var documentType = context['Document'];
660 expect(context.callMethod('isPropertyInstanceOf', ['o', documentType]),
661 true);
662 context.deleteProperty('o');
663 });
664
665 test('Blob', () {
666 var fileParts = ['<a id="a"><b id="b">hey!</b></a>'];
667 context['o'] = new Blob(fileParts, type: 'text/html');
668 var blobType = context['Blob'];
669 expect(context.callMethod('isPropertyInstanceOf', ['o', blobType]),
670 true);
671 context.deleteProperty('o');
672 });
673
674 test('unattached DivElement', () {
675 context['o'] = document.createElement('div');
676 var divType = context['HTMLDivElement'];
677 expect(context.callMethod('isPropertyInstanceOf', ['o', divType]),
678 true);
679 context.deleteProperty('o');
680 });
681
682 test('Event', () {
683 context['o'] = new CustomEvent('test');
684 var eventType = context['Event'];
685 expect(context.callMethod('isPropertyInstanceOf', ['o', eventType]),
686 true);
687 context.deleteProperty('o');
688 });
689
690 // this test fails in IE9 for very weird, but unknown, reasons
691 // the expression context['ImageData'] fails if useHtmlConfiguration()
692 // is called, or if the other tests in this file are enabled
693 test('ImageData', () {
694 CanvasElement canvas = document.createElement('canvas');
695 var ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
696 context['o'] = ctx.createImageData(1, 1);
697 var imageDataType = context['ImageData'];
698 expect(context.callMethod('isPropertyInstanceOf', ['o', imageDataType]),
699 true);
700 context.deleteProperty('o');
701 });
702
703 });
704 });
705 }
OLDNEW
« no previous file with comments | « test/browser/dom.dart ('k') | test/browser/js_test.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698