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

Side by Side Diff: pkg/dev_compiler/test/codegen/js_test.dart

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

Powered by Google App Engine
This is Rietveld 408576698