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

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

Issue 15782009: RFC: introduce dart:js (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fix comments on patch 9 Created 7 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « sdk/lib/js/dartium/js_dartium.dart ('k') | tools/create_sdk.py » ('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:async';
8 import 'dart:html';
9 import 'dart:js';
10
11 import '../../pkg/unittest/lib/unittest.dart';
12 import '../../pkg/unittest/lib/html_config.dart';
13
14 _injectJs() {
15 final script = new ScriptElement();
16 script.type = 'text/javascript';
17 script.innerHtml = r"""
18 var x = 42;
19
20 var _x = 123;
21
22 var myArray = ["value1"];
23
24 var foreignDoc = (function(){
25 var doc = document.implementation.createDocument("", "root", null);
26 var element = doc.createElement('element');
27 element.setAttribute('id', 'abc');
28 doc.documentElement.appendChild(element);
29 return doc;
30 })();
31
32 function razzle() {
33 return x;
34 }
35
36 function getTypeOf(o) {
37 return typeof(o);
38 }
39
40 function varArgs() {
41 var args = arguments;
42 var sum = 0;
43 for (var i = 0; i < args.length; ++i) {
44 sum += args[i];
45 }
46 return sum;
47 }
48
49 function Foo(a) {
50 this.a = a;
51 }
52
53 Foo.b = 38;
54
55 Foo.prototype.bar = function() {
56 return this.a;
57 }
58 Foo.prototype.toString = function() {
59 return "I'm a Foo a=" + this.a;
60 }
61
62 var container = new Object();
63 container.Foo = Foo;
64
65 function isArray(a) {
66 return a instanceof Array;
67 }
68
69 function checkMap(m, key, value) {
70 if (m.hasOwnProperty(key))
71 return m[key] == value;
72 else
73 return false;
74 }
75
76 function invokeCallback() {
77 return callback();
78 }
79
80 function invokeCallbackWith11params() {
81 return callbackWith11params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
82 }
83
84 function returnElement(element) {
85 return element;
86 }
87
88 function getElementAttribute(element, attr) {
89 return element.getAttribute(attr);
90 }
91
92 function addClassAttributes(list) {
93 var result = "";
94 for (var i=0; i<list.length; i++) {
95 result += list[i].getAttribute("class");
96 }
97 return result;
98 }
99
100 function getNewDivElement() {
101 return document.createElement("div");
102 }
103
104 function testJsMap(callback) {
105 var result = callback();
106 return result['value'];
107 }
108
109 function Bar() {
110 return "ret_value";
111 }
112 Bar.foo = "property_value";
113
114 function Baz(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11) {
115 this.f1 = p1;
116 this.f2 = p2;
117 this.f3 = p3;
118 this.f4 = p4;
119 this.f5 = p5;
120 this.f6 = p6;
121 this.f7 = p7;
122 this.f8 = p8;
123 this.f9 = p9;
124 this.f10 = p10;
125 this.f11 = p11;
126 }
127 """;
128 document.body.append(script);
129 }
130
131 class Foo implements Serializable<JsObject> {
132 final JsObject _proxy;
133
134 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]);
135
136 JsObject toJs() => _proxy;
137
138 num get a => _proxy['a'];
139 num bar() => _proxy.callMethod('bar');
140 }
141
142 class Color implements Serializable<String> {
143 static final RED = new Color._("red");
144 static final BLUE = new Color._("blue");
145 String _value;
146 Color._(this._value);
147 String toJs() => this._value;
148 }
149
150 main() {
151 _injectJs();
152 useHtmlConfiguration();
153
154 test('read global field', () {
155 expect(context['x'], equals(42));
156 expect(context['y'], isNull);
157 });
158
159 test('read global field with underscore', () {
160 expect(context['_x'], equals(123));
161 expect(context['y'], isNull);
162 });
163
164 test('hashCode and operator==(other)', () {
165 final o1 = context['Object'];
166 final o2 = context['Object'];
167 expect(o1 == o2, isTrue);
168 expect(o1.hashCode == o2.hashCode, isTrue);
169 final d = context['document'];
170 expect(o1 == d, isFalse);
171 });
172
173 test('js instantiation : new Foo()', () {
174 final Foo2 = context['container']['Foo'];
175 final foo = new JsObject(Foo2, [42]);
176 expect(foo['a'], 42);
177 expect(Foo2['b'], 38);
178 });
179
180 test('js instantiation : new Array()', () {
181 final a = new JsObject(context['Array']);
182 expect(a, isNotNull);
183 expect(a['length'], equals(0));
184
185 a.callMethod('push', ["value 1"]);
186 expect(a['length'], equals(1));
187 expect(a[0], equals("value 1"));
188
189 a.callMethod('pop');
190 expect(a['length'], equals(0));
191 });
192
193 test('js instantiation : new Date()', () {
194 final a = new JsObject(context['Date']);
195 expect(a.callMethod('getTime'), isNotNull);
196 });
197
198 test('js instantiation : new Date(12345678)', () {
199 final a = new JsObject(context['Date'], [12345678]);
200 expect(a.callMethod('getTime'), equals(12345678));
201 });
202
203 test('js instantiation : new Date("December 17, 1995 03:24:00 GMT+01:00")',
204 () {
205 final a = new JsObject(context['Date'],
206 ["December 17, 1995 03:24:00 GMT+01:00"]);
207 expect(a.callMethod('getTime'), equals(819167040000));
208 });
209
210 test('js instantiation : new Date(1995,11,17)', () {
211 // Note: JS Date counts months from 0 while Dart counts from 1.
212 final a = new JsObject(context['Date'], [1995, 11, 17]);
213 final b = new DateTime(1995, 12, 17);
214 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch));
215 });
216
217 test('js instantiation : new Date(1995,11,17,3,24,0)', () {
218 // Note: JS Date counts months from 0 while Dart counts from 1.
219 final a = new JsObject(context['Date'],
220 [1995, 11, 17, 3, 24, 0]);
221 final b = new DateTime(1995, 12, 17, 3, 24, 0);
222 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch));
223 });
224
225 test('js instantiation : new Object()', () {
226 final a = new JsObject(context['Object']);
227 expect(a, isNotNull);
228
229 a['attr'] = "value";
230 expect(a['attr'], equals("value"));
231 });
232
233 test(r'js instantiation : new RegExp("^\w+$")', () {
234 final a = new JsObject(context['RegExp'], [r'^\w+$']);
235 expect(a, isNotNull);
236 expect(a.callMethod('test', ['true']), isTrue);
237 expect(a.callMethod('test', [' false']), isFalse);
238 });
239
240 test('js instantiation via map notation : new Array()', () {
241 final a = new JsObject(context['Array']);
242 expect(a, isNotNull);
243 expect(a['length'], equals(0));
244
245 a['push'].apply(a, ["value 1"]);
246 expect(a['length'], equals(1));
247 expect(a[0], equals("value 1"));
248
249 a['pop'].apply(a);
250 expect(a['length'], equals(0));
251 });
252
253 test('js instantiation via map notation : new Date()', () {
254 final a = new JsObject(context['Date']);
255 expect(a['getTime'].apply(a), isNotNull);
256 });
257
258 test('js instantiation : typed array', () {
259 final codeUnits = "test".codeUnits;
260 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]);
261 final bufView = new JsObject(context['Uint8Array'], [buf]);
262 for (var i = 0; i < codeUnits.length; i++) {
263 bufView[i] = codeUnits[i];
264 }
265 });
266
267 test('js instantiation : >10 parameters', () {
268 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]);
269 for (var i = 1; i <= 11; i++) {
270 o["f$i"] = i;
271 }
272 });
273
274 test('write global field', () {
275 context['y'] = 42;
276 expect(context['y'], equals(42));
277 });
278
279 test('get JS JsFunction', () {
280 var razzle = context['razzle'];
281 expect(razzle.apply(context), equals(42));
282 });
283
284 test('call JS function', () {
285 expect(context.callMethod('razzle'), equals(42));
286 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError));
287 });
288
289 test('call JS function via map notation', () {
290 expect(context['razzle'].apply(context), equals(42));
291 expect(() => context['dazzle'].apply(context),
292 throwsA(isNoSuchMethodError));
293 });
294
295 test('call JS function with varargs', () {
296 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
297 equals(55));
298 });
299
300 test('allocate JS object', () {
301 var foo = new JsObject(context['Foo'], [42]);
302 expect(foo['a'], equals(42));
303 expect(foo.callMethod('bar'), equals(42));
304 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError));
305 });
306
307 test('call toString()', () {
308 var foo = new JsObject(context['Foo'], [42]);
309 expect(foo.toString(), equals("I'm a Foo a=42"));
310 var container = context['container'];
311 expect(container.toString(), equals("[object Object]"));
312 });
313
314 test('allocate simple JS array', () {
315 final list = [1, 2, 3, 4, 5, 6, 7, 8];
316 var array = jsify(list);
317 expect(context.callMethod('isArray', [array]), isTrue);
318 expect(array['length'], equals(list.length));
319 for (var i = 0; i < list.length ; i++) {
320 expect(array[i], equals(list[i]));
321 }
322 });
323
324 test('allocate JS array with iterable', () {
325 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]);
326 var array = jsify(set);
327 expect(context.callMethod('isArray', [array]), isTrue);
328 expect(array['length'], equals(set.length));
329 for (var i = 0; i < array['length'] ; i++) {
330 expect(set.contains(array[i]), isTrue);
331 }
332 });
333
334 test('allocate simple JS map', () {
335 var map = {'a': 1, 'b': 2, 'c': 3};
336 var jsMap = jsify(map);
337 expect(!context.callMethod('isArray', [jsMap]), isTrue);
338 for (final key in map.keys) {
339 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue);
340 }
341 });
342
343 test('allocate complex JS object', () {
344 final object =
345 {
346 'a': [1, [2, 3]],
347 'b': {
348 'c': 3,
349 'd': new JsObject(context['Foo'], [42])
350 },
351 'e': null
352 };
353 var jsObject = jsify(object);
354 expect(jsObject['a'][0], equals(object['a'][0]));
355 expect(jsObject['a'][1][0], equals(object['a'][1][0]));
356 expect(jsObject['a'][1][1], equals(object['a'][1][1]));
357 expect(jsObject['b']['c'], equals(object['b']['c']));
358 expect(jsObject['b']['d'], equals(object['b']['d']));
359 expect(jsObject['b']['d'].callMethod('bar'), equals(42));
360 expect(jsObject['e'], isNull);
361 });
362
363 test('invoke Dart callback from JS', () {
364 expect(() => context.callMethod('invokeCallback'), throws);
365
366 context['callback'] = new Callback(() => 42);
367 expect(context.callMethod('invokeCallback'), equals(42));
368
369 context.deleteProperty('callback');
370 expect(() => context.callMethod('invokeCallback'), throws);
371
372 context['callback'] = () => 42;
373 expect(context.callMethod('invokeCallback'), equals(42));
374
375 context.deleteProperty('callback');
376 });
377
378 test('callback as parameter', () {
379 expect(context.callMethod('getTypeOf', [context['razzle']]),
380 equals("function"));
381 });
382
383 test('invoke Dart callback from JS with this', () {
384 final constructor = new Callback.withThis(($this, arg1) {
385 $this['a'] = 42;
386 $this['b'] = jsify(["a", arg1]);
387 });
388 var o = new JsObject(constructor, ["b"]);
389 expect(o['a'], equals(42));
390 expect(o['b'][0], equals("a"));
391 expect(o['b'][1], equals("b"));
392 });
393
394 test('invoke Dart callback from JS with 11 parameters', () {
395 context['callbackWith11params'] = new Callback((p1, p2, p3, p4, p5, p6, p7,
396 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11');
397 expect(context.callMethod('invokeCallbackWith11params'),
398 equals('1234567891011'));
399 });
400
401 test('return a JS proxy to JavaScript', () {
402 var result = context.callMethod('testJsMap', [() => jsify({'value': 42})]);
403 expect(result, 42);
404 });
405
406 test('test proxy equality', () {
407 var foo1 = new JsObject(context['Foo'], [1]);
408 var foo2 = new JsObject(context['Foo'], [2]);
409 context['foo'] = foo1;
410 context['foo'] = foo2;
411 expect(foo1, isNot(equals(context['foo'])));
412 expect(foo2, equals(context['foo']));
413 });
414
415 test('test instanceof', () {
416 var foo = new JsObject(context['Foo'], [1]);
417 expect(foo.instanceof(context['Foo']), isTrue);
418 expect(foo.instanceof(context['Object']), isTrue);
419 expect(foo.instanceof(context['String']), isFalse);
420 });
421
422 test('test deleteProperty', () {
423 var object = jsify({});
424 object['a'] = 1;
425 expect(context['Object'].callMethod('keys', [object])['length'], 1);
426 expect(context['Object'].callMethod('keys', [object])[0], "a");
427 object.deleteProperty("a");
428 expect(context['Object'].callMethod('keys', [object])['length'], 0);
429 });
430
431 test('test hasProperty', () {
432 var object = jsify({});
433 object['a'] = 1;
434 expect(object.hasProperty('a'), isTrue);
435 expect(object.hasProperty('b'), isFalse);
436 });
437
438 test('test index get and set', () {
439 final myArray = context['myArray'];
440 expect(myArray['length'], equals(1));
441 expect(myArray[0], equals("value1"));
442 myArray[0] = "value2";
443 expect(myArray['length'], equals(1));
444 expect(myArray[0], equals("value2"));
445
446 final foo = new JsObject(context['Foo'], [1]);
447 foo["getAge"] = () => 10;
448 expect(foo.callMethod('getAge'), equals(10));
449 });
450
451 test('access a property of a function', () {
452 expect(context.callMethod('Bar'), "ret_value");
453 expect(context['Bar']['foo'], "property_value");
454 });
455
456 test('retrieve same dart Object', () {
457 final date = new DateTime.now();
458 context['dartDate'] = date;
459 expect(context['dartDate'], equals(date));
460 });
461
462 test('usage of Serializable', () {
463 final red = Color.RED;
464 context['color'] = red;
465 expect(context['color'], equals(red._value));
466 });
467 }
OLDNEW
« no previous file with comments | « sdk/lib/js/dartium/js_dartium.dart ('k') | tools/create_sdk.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698