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

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

Issue 27514003: A few API changes: (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Implement transferrables for Blob, Date, KeyRange, ImageData and Node Created 7 years, 2 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
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:indexed_db' show KeyRange;
9 import 'dart:js'; 10 import 'dart:js';
10 11
11 import '../../pkg/unittest/lib/unittest.dart'; 12 import '../../pkg/unittest/lib/unittest.dart';
12 import '../../pkg/unittest/lib/html_config.dart'; 13 import '../../pkg/unittest/lib/html_config.dart';
13 14
14 _injectJs() { 15 _injectJs() {
15 final script = new ScriptElement(); 16 final script = new ScriptElement();
16 script.type = 'text/javascript'; 17 script.type = 'text/javascript';
17 script.innerHtml = r""" 18 script.innerHtml = r"""
18 var x = 42; 19 var x = 42;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 } 91 }
91 92
92 function addClassAttributes(list) { 93 function addClassAttributes(list) {
93 var result = ""; 94 var result = "";
94 for (var i=0; i < list.length; i++) { 95 for (var i=0; i < list.length; i++) {
95 result += list[i].getAttribute("class"); 96 result += list[i].getAttribute("class");
96 } 97 }
97 return result; 98 return result;
98 } 99 }
99 100
101 function getNewDate() {
102 return new Date(1995, 11, 17);
103 }
104
100 function getNewDivElement() { 105 function getNewDivElement() {
101 return document.createElement("div"); 106 return document.createElement("div");
102 } 107 }
103 108
109 function getNewBlob() {
110 var fileParts = ['<a id="a"><b id="b">hey!</b></a>'];
111 return new Blob(fileParts, {type : 'text/html'});
112 }
113
114 function getNewIDBKeyRange() {
115 return IDBKeyRange.only(1);
116 }
117
118 function getNewImageData() {
119 var canvas = document.createElement('canvas');
120 var context = canvas.getContext('2d');
121 return context.createImageData(1, 1);
122 }
123
124 function isPropertyInstanceOf(property, type) {
125 return window[property] instanceof type;
126 }
127
104 function testJsMap(callback) { 128 function testJsMap(callback) {
105 var result = callback(); 129 var result = callback();
106 return result['value']; 130 return result['value'];
107 } 131 }
108 132
109 function Bar() { 133 function Bar() {
110 return "ret_value"; 134 return "ret_value";
111 } 135 }
112 Bar.foo = "property_value"; 136 Bar.foo = "property_value";
113 137
(...skipping 11 matching lines...) Expand all
125 this.f11 = p11; 149 this.f11 = p11;
126 } 150 }
127 151
128 function identical(o1, o2) { 152 function identical(o1, o2) {
129 return o1 === o2; 153 return o1 === o2;
130 } 154 }
131 """; 155 """;
132 document.body.append(script); 156 document.body.append(script);
133 } 157 }
134 158
135 class Foo implements Serializable<JsObject> { 159 class Foo {
136 final JsObject _proxy; 160 final JsObject _proxy;
137 161
138 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); 162 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]);
139 163
140 JsObject toJs() => _proxy; 164 JsObject toJs() => _proxy;
141 165
142 num get a => _proxy['a']; 166 num get a => _proxy['a'];
143 num bar() => _proxy.callMethod('bar'); 167 num bar() => _proxy.callMethod('bar');
144 } 168 }
145 169
146 class Color implements Serializable<String> { 170 class Color {
147 static final RED = new Color._("red"); 171 static final RED = new Color._("red");
148 static final BLUE = new Color._("blue"); 172 static final BLUE = new Color._("blue");
149 String _value; 173 String _value;
150 Color._(this._value); 174 Color._(this._value);
151 String toJs() => this._value; 175 String toJs() => this._value;
152 } 176 }
153 177
154 class TestDartObject {} 178 class TestDartObject {}
155 179
156 main() { 180 main() {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 227
204 test('retrieve same dart Object', () { 228 test('retrieve same dart Object', () {
205 final date = new DateTime.now(); 229 final date = new DateTime.now();
206 context['dartDate'] = date; 230 context['dartDate'] = date;
207 expect(context['dartDate'], same(date)); 231 expect(context['dartDate'], same(date));
208 context.deleteProperty('dartDate'); 232 context.deleteProperty('dartDate');
209 }); 233 });
210 234
211 }); 235 });
212 236
213 test('read global field', () { 237 group('context', () {
214 expect(context['x'], equals(42)); 238
215 expect(context['y'], isNull); 239 test('read global field', () {
216 }); 240 expect(context['x'], equals(42));
217 241 expect(context['y'], isNull);
218 test('read global field with underscore', () { 242 });
219 expect(context['_x'], equals(123)); 243
220 expect(context['y'], isNull); 244 test('read global field with underscore', () {
221 }); 245 expect(context['_x'], equals(123));
222 246 expect(context['y'], isNull);
223 test('hashCode and operator==(other)', () { 247 });
224 final o1 = context['Object']; 248
225 final o2 = context['Object']; 249 test('write global field', () {
226 expect(o1 == o2, isTrue); 250 context['y'] = 42;
227 expect(o1.hashCode == o2.hashCode, isTrue); 251 expect(context['y'], equals(42));
228 final d = context['document']; 252 });
229 expect(o1 == d, isFalse); 253
230 }); 254 });
231 255
232 test('js instantiation : new Foo()', () { 256 group('new JsObject()', () {
233 final Foo2 = context['container']['Foo']; 257
234 final foo = new JsObject(Foo2, [42]); 258 test('new Foo()', () {
235 expect(foo['a'], 42); 259 var foo = new JsObject(context['Foo'], [42]);
236 expect(Foo2['b'], 38); 260 expect(foo['a'], equals(42));
237 }); 261 expect(foo.callMethod('bar'), equals(42));
238 262 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError));
239 test('js instantiation : new Array()', () { 263 });
240 final a = new JsObject(context['Array']); 264
241 expect(a, isNotNull); 265 test('new container.Foo()', () {
242 expect(a['length'], equals(0)); 266 final Foo2 = context['container']['Foo'];
243 267 final foo = new JsObject(Foo2, [42]);
244 a.callMethod('push', ["value 1"]); 268 expect(foo['a'], 42);
245 expect(a['length'], equals(1)); 269 expect(Foo2['b'], 38);
246 expect(a[0], equals("value 1")); 270 });
247 271
248 a.callMethod('pop'); 272 test('new Array()', () {
249 expect(a['length'], equals(0)); 273 final a = new JsObject(context['Array']);
250 }); 274 expect(a, isNotNull);
251 275 expect(a['length'], equals(0));
252 test('js instantiation : new Date()', () { 276
253 final a = new JsObject(context['Date']); 277 a.callMethod('push', ["value 1"]);
254 expect(a.callMethod('getTime'), isNotNull); 278 expect(a['length'], equals(1));
255 }); 279 expect(a[0], equals("value 1"));
256 280
257 test('js instantiation : new Date(12345678)', () { 281 a.callMethod('pop');
258 final a = new JsObject(context['Date'], [12345678]); 282 expect(a['length'], equals(0));
259 expect(a.callMethod('getTime'), equals(12345678)); 283 });
260 }); 284
261 285 test('new Date()', () {
262 test('js instantiation : new Date("December 17, 1995 03:24:00 GMT")', 286 final a = new JsObject(context['Date']);
263 () { 287 expect(a.callMethod('getTime'), isNotNull);
264 final a = new JsObject(context['Date'], 288 });
265 ["December 17, 1995 03:24:00 GMT"]); 289
266 expect(a.callMethod('getTime'), equals(819170640000)); 290 test('new Date(12345678)', () {
267 }); 291 final a = new JsObject(context['Date'], [12345678]);
268 292 expect(a.callMethod('getTime'), equals(12345678));
269 test('js instantiation : new Date(1995,11,17)', () { 293 });
270 // Note: JS Date counts months from 0 while Dart counts from 1. 294
271 final a = new JsObject(context['Date'], [1995, 11, 17]); 295 test('new Date("December 17, 1995 03:24:00 GMT")',
272 final b = new DateTime(1995, 12, 17); 296 () {
273 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); 297 final a = new JsObject(context['Date'],
274 }); 298 ["December 17, 1995 03:24:00 GMT"]);
275 299 expect(a.callMethod('getTime'), equals(819170640000));
276 test('js instantiation : new Date(1995,11,17,3,24,0)', () { 300 });
277 // Note: JS Date counts months from 0 while Dart counts from 1. 301
278 final a = new JsObject(context['Date'], 302 test('new Date(1995,11,17)', () {
279 [1995, 11, 17, 3, 24, 0]); 303 // Note: JS Date counts months from 0 while Dart counts from 1.
280 final b = new DateTime(1995, 12, 17, 3, 24, 0); 304 final a = new JsObject(context['Date'], [1995, 11, 17]);
281 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); 305 final b = new DateTime(1995, 12, 17);
282 }); 306 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch));
283 307 });
284 test('js instantiation : new Object()', () { 308
285 final a = new JsObject(context['Object']); 309 test('new Date(1995,11,17,3,24,0)', () {
286 expect(a, isNotNull); 310 // Note: JS Date counts months from 0 while Dart counts from 1.
287 311 final a = new JsObject(context['Date'],
288 a['attr'] = "value"; 312 [1995, 11, 17, 3, 24, 0]);
289 expect(a['attr'], equals("value")); 313 final b = new DateTime(1995, 12, 17, 3, 24, 0);
290 }); 314 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch));
291 315 });
292 test(r'js instantiation : new RegExp("^\w+$")', () { 316
293 final a = new JsObject(context['RegExp'], [r'^\w+$']); 317 test('new Object()', () {
294 expect(a, isNotNull); 318 final a = new JsObject(context['Object']);
295 expect(a.callMethod('test', ['true']), isTrue); 319 expect(a, isNotNull);
296 expect(a.callMethod('test', [' false']), isFalse); 320
297 }); 321 a['attr'] = "value";
298 322 expect(a['attr'], equals("value"));
299 test('js instantiation via map notation : new Array()', () { 323 });
300 final a = new JsObject(context['Array']); 324
301 expect(a, isNotNull); 325 test(r'new RegExp("^\w+$")', () {
302 expect(a['length'], equals(0)); 326 final a = new JsObject(context['RegExp'], [r'^\w+$']);
303 327 expect(a, isNotNull);
304 a['push'].apply(a, ["value 1"]); 328 expect(a.callMethod('test', ['true']), isTrue);
305 expect(a['length'], equals(1)); 329 expect(a.callMethod('test', [' false']), isFalse);
306 expect(a[0], equals("value 1")); 330 });
307 331
308 a['pop'].apply(a); 332 test('js instantiation via map notation : new Array()', () {
309 expect(a['length'], equals(0)); 333 final a = new JsObject(context['Array']);
310 }); 334 expect(a, isNotNull);
311 335 expect(a['length'], equals(0));
312 test('js instantiation via map notation : new Date()', () { 336
313 final a = new JsObject(context['Date']); 337 a['push'].apply(a, ["value 1"]);
314 expect(a['getTime'].apply(a), isNotNull); 338 expect(a['length'], equals(1));
315 }); 339 expect(a[0], equals("value 1"));
316 340
317 test('js instantiation : typed array', () { 341 a['pop'].apply(a);
318 if (Platform.supportsTypedData) { 342 expect(a['length'], equals(0));
319 // Safari's ArrayBuffer is not a Function and so doesn't support bind 343 });
320 // which JsObject's constructor relies on. 344
321 // bug: https://bugs.webkit.org/show_bug.cgi?id=122976 345 test('js instantiation via map notation : new Date()', () {
322 if (context['ArrayBuffer']['bind'] != null) { 346 final a = new JsObject(context['Date']);
323 final codeUnits = "test".codeUnits; 347 expect(a['getTime'].apply(a), isNotNull);
324 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); 348 });
325 final bufView = new JsObject(context['Uint8Array'], [buf]); 349
326 for (var i = 0; i < codeUnits.length; i++) { 350 test('typed array', () {
327 bufView[i] = codeUnits[i]; 351 if (Platform.supportsTypedData) {
352 // Safari's ArrayBuffer is not a Function and so doesn't support bind
353 // which JsObject's constructor relies on.
354 // bug: https://bugs.webkit.org/show_bug.cgi?id=122976
355 if (context['ArrayBuffer']['bind'] != null) {
356 final codeUnits = "test".codeUnits;
357 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]);
358 final bufView = new JsObject(context['Uint8Array'], [buf]);
359 for (var i = 0; i < codeUnits.length; i++) {
360 bufView[i] = codeUnits[i];
361 }
328 } 362 }
329 } 363 }
330 } 364 });
331 }); 365
332 366 test('>10 parameters', () {
333 test('js instantiation : >10 parameters', () { 367 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]);
334 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]); 368 for (var i = 1; i <= 11; i++) {
335 for (var i = 1; i <= 11; i++) { 369 expect(o["f$i"], i);
336 expect(o["f$i"], i); 370 }
337 } 371 expect(o['constructor'], same(context['Baz']));
338 expect(o['constructor'], same(context['Baz'])); 372 });
339 }); 373 });
340 374
341 test('write global field', () { 375 group('JsFunction', () {
342 context['y'] = 42; 376
343 expect(context['y'], equals(42)); 377 test('get JS JsFunction', () {
344 }); 378 var razzle = context['razzle'];
345 379 expect(razzle.apply(context), equals(42));
346 test('get JS JsFunction', () { 380 });
347 var razzle = context['razzle']; 381
348 expect(razzle.apply(context), equals(42)); 382 test('call JS function', () {
349 }); 383 expect(context.callMethod('razzle'), equals(42));
350 384 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError));
351 test('call JS function', () { 385 });
352 expect(context.callMethod('razzle'), equals(42)); 386
353 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); 387 test('call JS function via map notation', () {
354 }); 388 expect(context['razzle'].apply(context), equals(42));
355 389 expect(() => context['dazzle'].apply(context),
356 test('call JS function via map notation', () { 390 throwsA(isNoSuchMethodError));
357 expect(context['razzle'].apply(context), equals(42)); 391 });
358 expect(() => context['dazzle'].apply(context), 392
359 throwsA(isNoSuchMethodError)); 393 test('call JS function with varargs', () {
360 }); 394 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
361 395 equals(55));
362 test('call JS function with varargs', () { 396 });
363 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 397
364 equals(55)); 398 test('access a property of a function', () {
365 }); 399 expect(context.callMethod('Bar'), "ret_value");
366 400 expect(context['Bar']['foo'], "property_value");
367 test('allocate JS object', () { 401 });
368 var foo = new JsObject(context['Foo'], [42]); 402
369 expect(foo['a'], equals(42)); 403 });
370 expect(foo.callMethod('bar'), equals(42)); 404
371 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); 405 group('Dart callback', () {
372 }); 406 test('invoke Dart callback from JS', () {
373 407 expect(() => context.callMethod('invokeCallback'), throws);
374 test('call toString()', () { 408
375 var foo = new JsObject(context['Foo'], [42]); 409 // context['callback'] = new Callback(() => 42);
376 expect(foo.toString(), equals("I'm a Foo a=42")); 410 // expect(context.callMethod('invokeCallback'), equals(42));
377 var container = context['container']; 411
378 expect(container.toString(), equals("[object Object]")); 412 // context.deleteProperty('callback');
379 }); 413 // expect(() => context.callMethod('invokeCallback'), throws);
380 414
381 test('allocate simple JS array', () { 415 context['callback'] = () => 42;
382 final list = [1, 2, 3, 4, 5, 6, 7, 8]; 416 expect(context.callMethod('invokeCallback'), equals(42));
383 var array = jsify(list); 417
384 expect(context.callMethod('isArray', [array]), isTrue); 418 context.deleteProperty('callback');
385 expect(array['length'], equals(list.length)); 419 });
386 for (var i = 0; i < list.length ; i++) { 420
387 expect(array[i], equals(list[i])); 421 test('callback as parameter', () {
388 } 422 expect(context.callMethod('getTypeOf', [context['razzle']]),
389 }); 423 equals("function"));
390 424 });
391 test('allocate JS array with iterable', () { 425
392 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); 426 test('invoke Dart callback from JS with this', () {
393 var array = jsify(set); 427 // A JavaScript constructor function implemented in Dart which
394 expect(context.callMethod('isArray', [array]), isTrue); 428 // uses 'this'
395 expect(array['length'], equals(set.length)); 429 final constructor = new JsFunction.withThis(($this, arg1) {
396 for (var i = 0; i < array['length'] ; i++) { 430 var t = $this;
397 expect(set.contains(array[i]), isTrue); 431 $this['a'] = 42;
398 } 432 });
399 }); 433 var o = new JsObject(constructor, ["b"]);
400 434 expect(o['a'], equals(42));
401 test('allocate simple JS map', () { 435 });
402 var map = {'a': 1, 'b': 2, 'c': 3}; 436
403 var jsMap = jsify(map); 437 test('invoke Dart callback from JS with 11 parameters', () {
404 expect(!context.callMethod('isArray', [jsMap]), isTrue); 438 context['callbackWith11params'] = (p1, p2, p3, p4, p5, p6, p7,
405 for (final key in map.keys) { 439 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11';
406 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue); 440 expect(context.callMethod('invokeCallbackWith11params'),
407 } 441 equals('1234567891011'));
408 }); 442 });
409 443
410 test('allocate complex JS object', () { 444 test('return a JS proxy to JavaScript', () {
411 final object = 445 var result = context.callMethod('testJsMap', [() => new JsObject.jsify({'v alue': 42})]);
412 { 446 expect(result, 42);
413 'a': [1, [2, 3]], 447 });
414 'b': { 448
415 'c': 3, 449 });
416 'd': new JsObject(context['Foo'], [42]) 450
417 }, 451 group('new JsObject.jsify', () {
418 'e': null 452
419 }; 453 test('allocate simple JS array', () {
420 var jsObject = jsify(object); 454 final list = [1, 2, 3, 4, 5, 6, 7, 8];
421 expect(jsObject['a'][0], equals(object['a'][0])); 455 var array = new JsObject.jsify(list);
422 expect(jsObject['a'][1][0], equals(object['a'][1][0])); 456 expect(context.callMethod('isArray', [array]), isTrue);
423 expect(jsObject['a'][1][1], equals(object['a'][1][1])); 457 expect(array['length'], equals(list.length));
424 expect(jsObject['b']['c'], equals(object['b']['c'])); 458 for (var i = 0; i < list.length ; i++) {
425 expect(jsObject['b']['d'], equals(object['b']['d'])); 459 expect(array[i], equals(list[i]));
426 expect(jsObject['b']['d'].callMethod('bar'), equals(42)); 460 }
427 expect(jsObject['e'], isNull); 461 });
428 }); 462
429 463 test('allocate JS array with iterable', () {
430 test('invoke Dart callback from JS', () { 464 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]);
431 expect(() => context.callMethod('invokeCallback'), throws); 465 var array = new JsObject.jsify(set);
432 466 expect(context.callMethod('isArray', [array]), isTrue);
433 context['callback'] = new Callback(() => 42); 467 expect(array['length'], equals(set.length));
434 expect(context.callMethod('invokeCallback'), equals(42)); 468 for (var i = 0; i < array['length'] ; i++) {
435 469 expect(set.contains(array[i]), isTrue);
436 context.deleteProperty('callback'); 470 }
437 expect(() => context.callMethod('invokeCallback'), throws); 471 });
438 472
439 context['callback'] = () => 42; 473 test('allocate simple JS map', () {
440 expect(context.callMethod('invokeCallback'), equals(42)); 474 var map = {'a': 1, 'b': 2, 'c': 3};
441 475 var jsMap = new JsObject.jsify(map);
442 context.deleteProperty('callback'); 476 expect(!context.callMethod('isArray', [jsMap]), isTrue);
443 }); 477 for (final key in map.keys) {
444 478 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue);
445 test('callback as parameter', () { 479 }
446 expect(context.callMethod('getTypeOf', [context['razzle']]), 480 });
447 equals("function")); 481
448 }); 482 test('allocate complex JS object', () {
449 483 final object =
450 test('invoke Dart callback from JS with this', () { 484 {
451 final constructor = new Callback.withThis(($this, arg1) { 485 'a': [1, [2, 3]],
452 $this['a'] = 42; 486 'b': {
453 $this['b'] = jsify(["a", arg1]); 487 'c': 3,
454 }); 488 'd': new JsObject(context['Foo'], [42])
455 var o = new JsObject(constructor, ["b"]); 489 },
456 expect(o['a'], equals(42)); 490 'e': null
457 expect(o['b'][0], equals("a")); 491 };
458 expect(o['b'][1], equals("b")); 492 var jsObject = new JsObject.jsify(object);
459 }); 493 expect(jsObject['a'][0], equals(object['a'][0]));
460 494 expect(jsObject['a'][1][0], equals(object['a'][1][0]));
461 test('invoke Dart callback from JS with 11 parameters', () { 495 expect(jsObject['a'][1][1], equals(object['a'][1][1]));
462 context['callbackWith11params'] = new Callback((p1, p2, p3, p4, p5, p6, p7, 496 expect(jsObject['b']['c'], equals(object['b']['c']));
463 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'); 497 expect(jsObject['b']['d'], equals(object['b']['d']));
464 expect(context.callMethod('invokeCallbackWith11params'), 498 expect(jsObject['b']['d'].callMethod('bar'), equals(42));
465 equals('1234567891011')); 499 expect(jsObject['e'], isNull);
466 }); 500 });
467 501
468 test('return a JS proxy to JavaScript', () { 502 });
469 var result = context.callMethod('testJsMap', [() => jsify({'value': 42})]); 503
470 expect(result, 42); 504 group('JsObject methods', () {
471 }); 505
472 506 test('hashCode and operator==(other)', () {
473 test('test instanceof', () { 507 final o1 = context['Object'];
474 var foo = new JsObject(context['Foo'], [1]); 508 final o2 = context['Object'];
475 expect(foo.instanceof(context['Foo']), isTrue); 509 expect(o1 == o2, isTrue);
476 expect(foo.instanceof(context['Object']), isTrue); 510 expect(o1.hashCode == o2.hashCode, isTrue);
477 expect(foo.instanceof(context['String']), isFalse); 511 final d = context['document'];
478 }); 512 expect(o1 == d, isFalse);
479 513 });
480 test('test deleteProperty', () { 514
481 var object = jsify({}); 515 test('toString()', () {
482 object['a'] = 1; 516 var foo = new JsObject(context['Foo'], [42]);
483 expect(context['Object'].callMethod('keys', [object])['length'], 1); 517 expect(foo.toString(), equals("I'm a Foo a=42"));
484 expect(context['Object'].callMethod('keys', [object])[0], "a"); 518 var container = context['container'];
485 object.deleteProperty("a"); 519 expect(container.toString(), equals("[object Object]"));
486 expect(context['Object'].callMethod('keys', [object])['length'], 0); 520 });
487 }); 521
488 522 test('instanceof', () {
489 test('test hasProperty', () { 523 var foo = new JsObject(context['Foo'], [1]);
490 var object = jsify({}); 524 expect(foo.instanceof(context['Foo']), isTrue);
491 object['a'] = 1; 525 expect(foo.instanceof(context['Object']), isTrue);
492 expect(object.hasProperty('a'), isTrue); 526 expect(foo.instanceof(context['String']), isFalse);
493 expect(object.hasProperty('b'), isFalse); 527 });
494 }); 528
495 529 test('deleteProperty', () {
496 test('test index get and set', () { 530 var object = new JsObject.jsify({});
497 final myArray = context['myArray']; 531 object['a'] = 1;
498 expect(myArray['length'], equals(1)); 532 expect(context['Object'].callMethod('keys', [object])['length'], 1);
499 expect(myArray[0], equals("value1")); 533 expect(context['Object'].callMethod('keys', [object])[0], "a");
500 myArray[0] = "value2"; 534 object.deleteProperty("a");
501 expect(myArray['length'], equals(1)); 535 expect(context['Object'].callMethod('keys', [object])['length'], 0);
502 expect(myArray[0], equals("value2")); 536 });
503 537
504 final foo = new JsObject(context['Foo'], [1]); 538 test('hasProperty', () {
505 foo["getAge"] = () => 10; 539 var object = new JsObject.jsify({});
506 expect(foo.callMethod('getAge'), equals(10)); 540 object['a'] = 1;
507 }); 541 expect(object.hasProperty('a'), isTrue);
508 542 expect(object.hasProperty('b'), isFalse);
509 test('access a property of a function', () { 543 });
510 expect(context.callMethod('Bar'), "ret_value"); 544
511 expect(context['Bar']['foo'], "property_value"); 545 test('index get and set', () {
512 }); 546 final myArray = context['myArray'];
513 547 expect(myArray['length'], equals(1));
514 test('usage of Serializable', () { 548 expect(myArray[0], equals("value1"));
515 final red = Color.RED; 549 myArray[0] = "value2";
516 context['color'] = red; 550 expect(myArray['length'], equals(1));
517 expect(context['color'], equals(red._value)); 551 expect(myArray[0], equals("value2"));
518 }); 552
553 final foo = new JsObject(context['Foo'], [1]);
554 foo["getAge"] = () => 10;
555 expect(foo.callMethod('getAge'), equals(10));
556 });
557
558 });
559
560 group('transferrables JS->Dart', () {
561
562 test('Date', () {
563 var date = context.callMethod('getNewDate');
564 expect(date is Date, isTrue);
565 });
566
567 test('window', () {
568 expect(context['window'] is Window, isFalse);
569 });
570
571 test('document', () {
572 expect(context['document'] is Document, isTrue);
573 });
574
575 test('Blob', () {
576 var blob = context.callMethod('getNewBlob');
577 expect(blob is Blob, isTrue);
578 expect(blob.type, equals('text/html'));
579 });
580
581 test('unattached DivElement', () {
582 var node = context.callMethod('getNewDivElement');
583 expect(node is Div, isTrue);
584 });
585
586 test('KeyRange', () {
587 var node = context.callMethod('getNewIDBKeyRange');
588 expect(node is KeyRange, isTrue);
589 });
590
591 test('ImageData', () {
592 var node = context.callMethod('getNewImageData');
593 expect(node is ImageData, isTrue);
594 });
595
596 });
597
598 group('transferrables Dart->JS', () {
599
600 test('Date', () {
601 context['o'] = new DateTime(1995, 12, 17);
602 var dateType = context['Date'];
603 expect(context.callMethod('isPropertyInstanceOf', ['o', dateType]),
604 isTrue);
605 context.deleteProperty('o');
606 });
607
608 test('window', () {
609 context['o'] = window;
610 var windowType = context['Window'];
611 expect(context.callMethod('isPropertyInstanceOf', ['o', windowType]),
612 isFalse);
613 context.deleteProperty('o');
614 });
615
616 test('document', () {
617 context['o'] = document;
618 var documentType = context['Document'];
619 expect(context.callMethod('isPropertyInstanceOf', ['o', documentType]),
620 isTrue);
621 context.deleteProperty('o');
622 });
623
624 test('Blob', () {
625 var fileParts = ['<a id="a"><b id="b">hey!</b></a>'];
626 context['o'] = new Blob(fileParts, 'text/html');
627 var blobType = context['Blob'];
628 expect(context.callMethod('isPropertyInstanceOf', ['o', blobType]),
629 isTrue);
630 context.deleteProperty('o');
631 });
632
633 test('unattached DivElement', () {
634 context['o'] = new DivElement();
635 var divType = context['HTMLDivElement'];
636 expect(context.callMethod('isPropertyInstanceOf', ['o', divType]),
637 isTrue);
638 context.deleteProperty('o');
639 });
640
641 test('KeyRange', () {
642 context['o'] = new KeyRange.only(1);
643 var keyRangeType = context['IDBKeyRange'];
644 expect(context.callMethod('isPropertyInstanceOf', ['o', keyRangeType]),
645 isTrue);
646 context.deleteProperty('o');
647 });
648
649 test('ImageData', () {
650 var canvas = new CanvasElement();
651 var ctx = canvas.getContext('2d');
652 context['o'] = ctx.createImageData(1, 1);
653 var imageDataType = context['ImageData'];
654 expect(context.callMethod('isPropertyInstanceOf', ['o', imageDataType]),
655 isTrue);
656 context.deleteProperty('o');
657 });
658
659 });
660
519 } 661 }
OLDNEW
« sdk/lib/js/dart2js/js_dart2js.dart ('K') | « sdk/lib/js/dart2js/js_dart2js.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698