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 27514003: A few API changes: (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: PTAL 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
« 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;
10 import 'dart:indexed_db' show IdbFactory, KeyRange;
9 import 'dart:js'; 11 import 'dart:js';
10 12
11 import '../../pkg/unittest/lib/unittest.dart'; 13 import '../../pkg/unittest/lib/unittest.dart';
12 import '../../pkg/unittest/lib/html_config.dart'; 14 import '../../pkg/unittest/lib/html_config.dart';
13 15
14 _injectJs() { 16 _injectJs() {
15 final script = new ScriptElement(); 17 final script = new ScriptElement();
16 script.type = 'text/javascript'; 18 script.type = 'text/javascript';
17 script.innerHtml = r""" 19 script.innerHtml = r"""
18 var x = 42; 20 var x = 42;
19 21
20 var _x = 123; 22 var _x = 123;
21 23
22 var myArray = ["value1"]; 24 var myArray = ["value1"];
23 25
24 var foreignDoc = (function(){ 26 var foreignDoc = (function(){
25 var doc = document.implementation.createDocument("", "root", null); 27 var doc = document.implementation.createDocument("", "root", null);
26 var element = doc.createElement('element'); 28 var element = doc.createElement('element');
27 element.setAttribute('id', 'abc'); 29 element.setAttribute('id', 'abc');
28 doc.documentElement.appendChild(element); 30 doc.documentElement.appendChild(element);
29 return doc; 31 return doc;
30 })(); 32 })();
31 33
32 function razzle() { 34 function razzle() {
33 return x; 35 return x;
34 } 36 }
35 37
38 function returnThis() {
39 return this;
40 }
41
36 function getTypeOf(o) { 42 function getTypeOf(o) {
37 return typeof(o); 43 return typeof(o);
38 } 44 }
39 45
40 function varArgs() { 46 function varArgs() {
41 var args = arguments; 47 var args = arguments;
42 var sum = 0; 48 var sum = 0;
43 for (var i = 0; i < args.length; ++i) { 49 for (var i = 0; i < args.length; ++i) {
44 sum += args[i]; 50 sum += args[i];
45 } 51 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 } 96 }
91 97
92 function addClassAttributes(list) { 98 function addClassAttributes(list) {
93 var result = ""; 99 var result = "";
94 for (var i=0; i < list.length; i++) { 100 for (var i=0; i < list.length; i++) {
95 result += list[i].getAttribute("class"); 101 result += list[i].getAttribute("class");
96 } 102 }
97 return result; 103 return result;
98 } 104 }
99 105
106 function getNewDate() {
107 return new Date(1995, 11, 17);
108 }
109
100 function getNewDivElement() { 110 function getNewDivElement() {
101 return document.createElement("div"); 111 return document.createElement("div");
102 } 112 }
103 113
114 function getNewBlob() {
115 var fileParts = ['<a id="a"><b id="b">hey!</b></a>'];
116 return new Blob(fileParts, {type : 'text/html'});
117 }
118
119 function getNewIDBKeyRange() {
120 return IDBKeyRange.only(1);
121 }
122
123 function getNewImageData() {
124 var canvas = document.createElement('canvas');
125 var context = canvas.getContext('2d');
126 return context.createImageData(1, 1);
127 }
128
129 function getNewInt32Array() {
130 return new Int32Array([1, 2, 3, 4, 5, 6, 7, 8]);
131 }
132
133 function getNewArrayBuffer() {
134 return new ArrayBuffer(8);
135 }
136
137 function isPropertyInstanceOf(property, type) {
138 return window[property] instanceof type;
139 }
140
104 function testJsMap(callback) { 141 function testJsMap(callback) {
105 var result = callback(); 142 var result = callback();
106 return result['value']; 143 return result['value'];
107 } 144 }
108 145
146 function addTestProperty(o) {
147 o.testProperty = "test";
148 }
149
109 function Bar() { 150 function Bar() {
110 return "ret_value"; 151 return "ret_value";
111 } 152 }
112 Bar.foo = "property_value"; 153 Bar.foo = "property_value";
113 154
114 function Baz(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11) { 155 function Baz(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11) {
115 this.f1 = p1; 156 this.f1 = p1;
116 this.f2 = p2; 157 this.f2 = p2;
117 this.f3 = p3; 158 this.f3 = p3;
118 this.f4 = p4; 159 this.f4 = p4;
119 this.f5 = p5; 160 this.f5 = p5;
120 this.f6 = p6; 161 this.f6 = p6;
121 this.f7 = p7; 162 this.f7 = p7;
122 this.f8 = p8; 163 this.f8 = p8;
123 this.f9 = p9; 164 this.f9 = p9;
124 this.f10 = p10; 165 this.f10 = p10;
125 this.f11 = p11; 166 this.f11 = p11;
126 } 167 }
127 168
169 function Liar(){}
170
171 Liar.prototype.toString = function() {
172 return 1;
173 }
174
128 function identical(o1, o2) { 175 function identical(o1, o2) {
129 return o1 === o2; 176 return o1 === o2;
130 } 177 }
131 """; 178 """;
132 document.body.append(script); 179 document.body.append(script);
133 } 180 }
134 181
135 class Foo implements Serializable<JsObject> { 182 class Foo {
136 final JsObject _proxy; 183 final JsObject _proxy;
137 184
138 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); 185 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]);
139 186
140 JsObject toJs() => _proxy; 187 JsObject toJs() => _proxy;
141 188
142 num get a => _proxy['a']; 189 num get a => _proxy['a'];
143 num bar() => _proxy.callMethod('bar'); 190 num bar() => _proxy.callMethod('bar');
144 } 191 }
145 192
146 class Color implements Serializable<String> { 193 class Color {
147 static final RED = new Color._("red"); 194 static final RED = new Color._("red");
148 static final BLUE = new Color._("blue"); 195 static final BLUE = new Color._("blue");
149 String _value; 196 String _value;
150 Color._(this._value); 197 Color._(this._value);
151 String toJs() => this._value; 198 String toJs() => this._value;
152 } 199 }
153 200
154 class TestDartObject {} 201 class TestDartObject {}
155 202
156 main() { 203 main() {
157 _injectJs(); 204 _injectJs();
158 useHtmlConfiguration(); 205 useHtmlConfiguration();
159 206
160 group('identity', () { 207 group('identity', () {
161 208
162 test('context instances should be identical', () { 209 test('context instances should be identical', () {
163 var c1 = context; 210 var c1 = context;
164 var c2 = context; 211 var c2 = context;
165 expect(identical(c1, c2), isTrue); 212 expect(identical(c1, c2), isTrue);
166 }); 213 });
167 214
215 /* TODO(jacobr): enable these tests again once DartVM supports it.
168 test('identical JS objects should have identical proxies', () { 216 test('identical JS objects should have identical proxies', () {
169 var o1 = new JsObject(context['Foo'], [1]); 217 var o1 = new JsObject(context['Foo'], [1]);
170 context['f1'] = o1; 218 context['f1'] = o1;
171 var o2 = context['f1']; 219 var o2 = context['f1'];
172 expect(identical(o1, o2), isTrue); 220 expect(identical(o1, o2), isTrue);
173 }); 221 });
174 222
175 test('identical JS functions should have identical proxies', () { 223 test('identical JS functions should have identical proxies', () {
176 var f1 = context['Object']; 224 var f1 = context['Object'];
177 var f2 = context['Object']; 225 var f2 = context['Object'];
(...skipping 11 matching lines...) Expand all
189 }); 237 });
190 238
191 // TODO(justinfagnani): old tests duplicate checks above, remove 239 // TODO(justinfagnani): old tests duplicate checks above, remove
192 // on test next cleanup pass 240 // on test next cleanup pass
193 test('test proxy equality', () { 241 test('test proxy equality', () {
194 var foo1 = new JsObject(context['Foo'], [1]); 242 var foo1 = new JsObject(context['Foo'], [1]);
195 var foo2 = new JsObject(context['Foo'], [2]); 243 var foo2 = new JsObject(context['Foo'], [2]);
196 context['foo1'] = foo1; 244 context['foo1'] = foo1;
197 context['foo2'] = foo2; 245 context['foo2'] = foo2;
198 expect(foo1, isNot(equals(context['foo2']))); 246 expect(foo1, isNot(equals(context['foo2'])));
199 expect(foo2, same(context['foo2'])); 247 expect(foo2, same(context['foo2']));
justinfagnani 2013/10/19 04:18:46 if you change same() to equals() this should still
200 context.deleteProperty('foo1'); 248 context.deleteProperty('foo1');
201 context.deleteProperty('foo2'); 249 context.deleteProperty('foo2');
202 }); 250 });
251 */
203 252
204 test('retrieve same dart Object', () { 253 test('retrieve same dart Object', () {
205 final date = new DateTime.now(); 254 final obj = new Object();
206 context['dartDate'] = date; 255 context['obj'] = obj;
207 expect(context['dartDate'], same(date)); 256 expect(context['obj'], same(obj));
208 context.deleteProperty('dartDate'); 257 context.deleteProperty('obj');
209 }); 258 });
210 259
211 }); 260 });
212 261
213 test('read global field', () { 262 group('context', () {
214 expect(context['x'], equals(42)); 263
215 expect(context['y'], isNull); 264 test('read global field', () {
216 }); 265 expect(context['x'], equals(42));
217 266 expect(context['y'], isNull);
218 test('read global field with underscore', () { 267 });
219 expect(context['_x'], equals(123)); 268
220 expect(context['y'], isNull); 269 test('read global field with underscore', () {
221 }); 270 expect(context['_x'], equals(123));
222 271 expect(context['y'], isNull);
223 test('hashCode and operator==(other)', () { 272 });
224 final o1 = context['Object']; 273
225 final o2 = context['Object']; 274 test('write global field', () {
226 expect(o1 == o2, isTrue); 275 context['y'] = 42;
227 expect(o1.hashCode == o2.hashCode, isTrue); 276 expect(context['y'], equals(42));
228 final d = context['document']; 277 });
229 expect(o1 == d, isFalse); 278
230 }); 279 });
231 280
232 test('js instantiation : new Foo()', () { 281 group('new JsObject()', () {
233 final Foo2 = context['container']['Foo']; 282
234 final foo = new JsObject(Foo2, [42]); 283 test('new Foo()', () {
235 expect(foo['a'], 42); 284 var foo = new JsObject(context['Foo'], [42]);
236 expect(Foo2['b'], 38); 285 expect(foo['a'], equals(42));
237 }); 286 expect(foo.callMethod('bar'), equals(42));
238 287 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError));
239 test('js instantiation : new Array()', () { 288 });
240 final a = new JsObject(context['Array']); 289
241 expect(a, isNotNull); 290 test('new container.Foo()', () {
242 expect(a['length'], equals(0)); 291 final Foo2 = context['container']['Foo'];
243 292 final foo = new JsObject(Foo2, [42]);
244 a.callMethod('push', ["value 1"]); 293 expect(foo['a'], 42);
245 expect(a['length'], equals(1)); 294 expect(Foo2['b'], 38);
246 expect(a[0], equals("value 1")); 295 });
247 296
248 a.callMethod('pop'); 297 test('new Array()', () {
249 expect(a['length'], equals(0)); 298 final a = new JsObject(context['Array']);
250 }); 299 expect(a, isNotNull);
251 300 expect(a['length'], equals(0));
252 test('js instantiation : new Date()', () { 301
253 final a = new JsObject(context['Date']); 302 a.callMethod('push', ["value 1"]);
254 expect(a.callMethod('getTime'), isNotNull); 303 expect(a['length'], equals(1));
255 }); 304 expect(a[0], equals("value 1"));
256 305
257 test('js instantiation : new Date(12345678)', () { 306 a.callMethod('pop');
258 final a = new JsObject(context['Date'], [12345678]); 307 expect(a['length'], equals(0));
259 expect(a.callMethod('getTime'), equals(12345678)); 308 });
260 }); 309
261 310 test('new Date()', () {
262 test('js instantiation : new Date("December 17, 1995 03:24:00 GMT")', 311 final a = new JsObject(context['Date']);
263 () { 312 expect(a.callMethod('getTime'), isNotNull);
264 final a = new JsObject(context['Date'], 313 });
265 ["December 17, 1995 03:24:00 GMT"]); 314
266 expect(a.callMethod('getTime'), equals(819170640000)); 315 test('new Date(12345678)', () {
267 }); 316 final a = new JsObject(context['Date'], [12345678]);
268 317 expect(a.callMethod('getTime'), equals(12345678));
269 test('js instantiation : new Date(1995,11,17)', () { 318 });
270 // Note: JS Date counts months from 0 while Dart counts from 1. 319
271 final a = new JsObject(context['Date'], [1995, 11, 17]); 320 test('new Date("December 17, 1995 03:24:00 GMT")',
272 final b = new DateTime(1995, 12, 17); 321 () {
273 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); 322 final a = new JsObject(context['Date'],
274 }); 323 ["December 17, 1995 03:24:00 GMT"]);
275 324 expect(a.callMethod('getTime'), equals(819170640000));
276 test('js instantiation : new Date(1995,11,17,3,24,0)', () { 325 });
277 // Note: JS Date counts months from 0 while Dart counts from 1. 326
278 final a = new JsObject(context['Date'], 327 test('new Date(1995,11,17)', () {
279 [1995, 11, 17, 3, 24, 0]); 328 // Note: JS Date counts months from 0 while Dart counts from 1.
280 final b = new DateTime(1995, 12, 17, 3, 24, 0); 329 final a = new JsObject(context['Date'], [1995, 11, 17]);
281 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); 330 final b = new DateTime(1995, 12, 17);
282 }); 331 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch));
283 332 });
284 test('js instantiation : new Object()', () { 333
285 final a = new JsObject(context['Object']); 334 test('new Date(1995,11,17,3,24,0)', () {
286 expect(a, isNotNull); 335 // Note: JS Date counts months from 0 while Dart counts from 1.
287 336 final a = new JsObject(context['Date'],
288 a['attr'] = "value"; 337 [1995, 11, 17, 3, 24, 0]);
289 expect(a['attr'], equals("value")); 338 final b = new DateTime(1995, 12, 17, 3, 24, 0);
290 }); 339 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch));
291 340 });
292 test(r'js instantiation : new RegExp("^\w+$")', () { 341
293 final a = new JsObject(context['RegExp'], [r'^\w+$']); 342 test('new Object()', () {
294 expect(a, isNotNull); 343 final a = new JsObject(context['Object']);
295 expect(a.callMethod('test', ['true']), isTrue); 344 expect(a, isNotNull);
296 expect(a.callMethod('test', [' false']), isFalse); 345
297 }); 346 a['attr'] = "value";
298 347 expect(a['attr'], equals("value"));
299 test('js instantiation via map notation : new Array()', () { 348 });
300 final a = new JsObject(context['Array']); 349
301 expect(a, isNotNull); 350 test(r'new RegExp("^\w+$")', () {
302 expect(a['length'], equals(0)); 351 final a = new JsObject(context['RegExp'], [r'^\w+$']);
303 352 expect(a, isNotNull);
304 a['push'].apply(a, ["value 1"]); 353 expect(a.callMethod('test', ['true']), isTrue);
305 expect(a['length'], equals(1)); 354 expect(a.callMethod('test', [' false']), isFalse);
306 expect(a[0], equals("value 1")); 355 });
307 356
308 a['pop'].apply(a); 357 test('js instantiation via map notation : new Array()', () {
309 expect(a['length'], equals(0)); 358 final a = new JsObject(context['Array']);
310 }); 359 expect(a, isNotNull);
311 360 expect(a['length'], equals(0));
312 test('js instantiation via map notation : new Date()', () { 361
313 final a = new JsObject(context['Date']); 362 a.callMethod('push', ["value 1"]);
314 expect(a['getTime'].apply(a), isNotNull); 363 expect(a['length'], equals(1));
315 }); 364 expect(a[0], equals("value 1"));
316 365
317 test('js instantiation : typed array', () { 366 a.callMethod('pop');
318 if (Platform.supportsTypedData) { 367 expect(a['length'], equals(0));
319 // Safari's ArrayBuffer is not a Function and so doesn't support bind 368 });
320 // which JsObject's constructor relies on. 369
321 // bug: https://bugs.webkit.org/show_bug.cgi?id=122976 370 test('js instantiation via map notation : new Date()', () {
322 if (context['ArrayBuffer']['bind'] != null) { 371 final a = new JsObject(context['Date']);
323 final codeUnits = "test".codeUnits; 372 expect(a.callMethod('getTime'), isNotNull);
324 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); 373 });
325 final bufView = new JsObject(context['Uint8Array'], [buf]); 374
326 for (var i = 0; i < codeUnits.length; i++) { 375 test('typed array', () {
327 bufView[i] = codeUnits[i]; 376 if (Platform.supportsTypedData) {
377 // Safari's ArrayBuffer is not a Function and so doesn't support bind
378 // which JsObject's constructor relies on.
379 // bug: https://bugs.webkit.org/show_bug.cgi?id=122976
380 if (context['ArrayBuffer']['bind'] != null) {
381 final codeUnits = "test".codeUnits;
382 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]);
383 final bufView = new JsObject(context['Uint8Array'], [buf]);
384 for (var i = 0; i < codeUnits.length; i++) {
385 bufView[i] = codeUnits[i];
386 }
328 } 387 }
329 } 388 }
330 } 389 });
331 }); 390
332 391 test('>10 parameters', () {
333 test('js instantiation : >10 parameters', () { 392 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]); 393 for (var i = 1; i <= 11; i++) {
335 for (var i = 1; i <= 11; i++) { 394 expect(o["f$i"], i);
336 expect(o["f$i"], i); 395 }
337 } 396 expect(o['constructor'], equals(context['Baz']));
338 expect(o['constructor'], same(context['Baz'])); 397 });
339 }); 398 });
340 399
341 test('write global field', () { 400 group('JsFunction and callMethod', () {
342 context['y'] = 42; 401
343 expect(context['y'], equals(42)); 402 test('JsFunction.apply on a function defined in JS', () {
344 }); 403 expect(context['razzle'].apply([]), equals(42));
345 404 });
346 test('get JS JsFunction', () { 405
347 var razzle = context['razzle']; 406 test('JsFunction.apply on a function that uses "this"', () {
348 expect(razzle.apply(context), equals(42)); 407 var object = new Object();
349 }); 408 expect(context['returnThis'].apply([], thisArg: object), same(object));
350 409 });
351 test('call JS function', () { 410
352 expect(context.callMethod('razzle'), equals(42)); 411 test('JsObject.callMethod on a function defined in JS', () {
353 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); 412 expect(context.callMethod('razzle'), equals(42));
354 }); 413 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError));
355 414 });
356 test('call JS function via map notation', () { 415
357 expect(context['razzle'].apply(context), equals(42)); 416 test('callMethod with many arguments', () {
358 expect(() => context['dazzle'].apply(context), 417 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
359 throwsA(isNoSuchMethodError)); 418 equals(55));
360 }); 419 });
361 420
362 test('call JS function with varargs', () { 421 test('access a property of a function', () {
363 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 422 expect(context.callMethod('Bar'), "ret_value");
364 equals(55)); 423 expect(context['Bar']['foo'], "property_value");
365 }); 424 });
366 425 /*
367 test('allocate JS object', () { 426 TODO(jacobr): evaluate whether we should be in the business of throwing
368 var foo = new JsObject(context['Foo'], [42]); 427 ArgumentError outside of checked mode. In unchecked mode this should just
369 expect(foo['a'], equals(42)); 428 return a NoSuchMethodError as the class lacks a method "true".
370 expect(foo.callMethod('bar'), equals(42)); 429
371 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); 430 test('callMethod throws if name is not a String or num', () {
372 }); 431 expect(() => context.callMethod(true),
373 432 throwsA(new isInstanceOf<ArgumentError>()));
374 test('call toString()', () { 433 });
375 var foo = new JsObject(context['Foo'], [42]); 434 */
376 expect(foo.toString(), equals("I'm a Foo a=42")); 435 });
377 var container = context['container']; 436
378 expect(container.toString(), equals("[object Object]")); 437 group('JsObject.fromBrowserObject()', () {
379 }); 438
380 439 test('Nodes are proxied', () {
381 test('allocate simple JS array', () { 440 var node = new JsObject.fromBrowserObject(new DivElement());
382 final list = [1, 2, 3, 4, 5, 6, 7, 8]; 441 context['addTestProperty'].apply([node]);
383 var array = jsify(list); 442 expect(node is JsObject, isTrue);
384 expect(context.callMethod('isArray', [array]), isTrue); 443 expect(node.instanceof(context['HTMLDivElement']), isTrue);
385 expect(array['length'], equals(list.length)); 444 expect(node['testProperty'], 'test');
386 for (var i = 0; i < list.length ; i++) { 445 });
387 expect(array[i], equals(list[i])); 446
388 } 447 test('primitives and null throw ArgumentError', () {
389 }); 448 for (var v in ['a', 1, 2.0, true, null]) {
390 449 expect(() => new JsObject.fromBrowserObject(v),
391 test('allocate JS array with iterable', () { 450 throwsA(new isInstanceOf<ArgumentError>()));
392 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); 451 }
393 var array = jsify(set); 452 });
394 expect(context.callMethod('isArray', [array]), isTrue); 453
395 expect(array['length'], equals(set.length)); 454 });
396 for (var i = 0; i < array['length'] ; i++) { 455
397 expect(set.contains(array[i]), isTrue); 456 group('Dart callback', () {
398 } 457 test('invoke Dart callback from JS', () {
399 }); 458 expect(() => context.callMethod('invokeCallback'), throws);
400 459
401 test('allocate simple JS map', () { 460 context['callback'] = () => 42;
402 var map = {'a': 1, 'b': 2, 'c': 3}; 461 expect(context.callMethod('invokeCallback'), equals(42));
403 var jsMap = jsify(map); 462
404 expect(!context.callMethod('isArray', [jsMap]), isTrue); 463 context.deleteProperty('callback');
405 for (final key in map.keys) { 464 });
406 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue); 465
407 } 466 test('callback as parameter', () {
408 }); 467 expect(context.callMethod('getTypeOf', [context['razzle']]),
409 468 equals("function"));
410 test('allocate complex JS object', () { 469 });
411 final object = 470
412 { 471 test('invoke Dart callback from JS with this', () {
472 // A JavaScript constructor function implemented in Dart which
473 // uses 'this'
474 final constructor = new JsFunction.withThis(($this, arg1) {
475 var t = $this;
476 $this['a'] = 42;
477 });
478 var o = new JsObject(constructor, ["b"]);
479 expect(o['a'], equals(42));
480 });
481
482 test('invoke Dart callback from JS with 11 parameters', () {
483 context['callbackWith11params'] = (p1, p2, p3, p4, p5, p6, p7,
484 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11';
485 expect(context.callMethod('invokeCallbackWith11params'),
486 equals('1234567891011'));
487 });
488
489 test('return a JS proxy to JavaScript', () {
490 var result = context.callMethod('testJsMap', [() => new JsObject.jsify({'v alue': 42})]);
491 expect(result, 42);
492 });
493
494 });
495
496 group('JsObject.jsify()', () {
497
498 test('convert a List', () {
499 final list = [1, 2, 3, 4, 5, 6, 7, 8];
500 var array = new JsObject.jsify(list);
501 expect(context.callMethod('isArray', [array]), isTrue);
502 expect(array['length'], equals(list.length));
503 for (var i = 0; i < list.length ; i++) {
504 expect(array[i], equals(list[i]));
505 }
506 });
507
508 test('convert an Iterable', () {
509 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]);
510 var array = new JsObject.jsify(set);
511 expect(context.callMethod('isArray', [array]), isTrue);
512 expect(array['length'], equals(set.length));
513 for (var i = 0; i < array['length'] ; i++) {
514 expect(set.contains(array[i]), isTrue);
515 }
516 });
517
518 test('convert a Map', () {
519 var map = {'a': 1, 'b': 2, 'c': 3};
520 var jsMap = new JsObject.jsify(map);
521 expect(!context.callMethod('isArray', [jsMap]), isTrue);
522 for (final key in map.keys) {
523 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue);
524 }
525 });
526
527 test('deep convert a complex object', () {
528 final object = {
413 'a': [1, [2, 3]], 529 'a': [1, [2, 3]],
414 'b': { 530 'b': {
415 'c': 3, 531 'c': 3,
416 'd': new JsObject(context['Foo'], [42]) 532 'd': new JsObject(context['Foo'], [42])
417 }, 533 },
418 'e': null 534 'e': null
419 }; 535 };
420 var jsObject = jsify(object); 536 var jsObject = new JsObject.jsify(object);
421 expect(jsObject['a'][0], equals(object['a'][0])); 537 expect(jsObject['a'][0], equals(object['a'][0]));
422 expect(jsObject['a'][1][0], equals(object['a'][1][0])); 538 expect(jsObject['a'][1][0], equals(object['a'][1][0]));
423 expect(jsObject['a'][1][1], equals(object['a'][1][1])); 539 expect(jsObject['a'][1][1], equals(object['a'][1][1]));
424 expect(jsObject['b']['c'], equals(object['b']['c'])); 540 expect(jsObject['b']['c'], equals(object['b']['c']));
425 expect(jsObject['b']['d'], equals(object['b']['d'])); 541 expect(jsObject['b']['d'], equals(object['b']['d']));
426 expect(jsObject['b']['d'].callMethod('bar'), equals(42)); 542 expect(jsObject['b']['d'].callMethod('bar'), equals(42));
427 expect(jsObject['e'], isNull); 543 expect(jsObject['e'], isNull);
544 });
545
546 test('throws if object is not a Map or Iterable', () {
547 expect(() => new JsObject.jsify('a'),
548 throwsA(new isInstanceOf<ArgumentError>()));
549 });
428 }); 550 });
429 551
430 test('invoke Dart callback from JS', () { 552 group('JsObject methods', () {
431 expect(() => context.callMethod('invokeCallback'), throws); 553
432 554 test('hashCode and ==', () {
433 context['callback'] = new Callback(() => 42); 555 final o1 = context['Object'];
434 expect(context.callMethod('invokeCallback'), equals(42)); 556 final o2 = context['Object'];
435 557 expect(o1 == o2, isTrue);
436 context.deleteProperty('callback'); 558 expect(o1.hashCode == o2.hashCode, isTrue);
437 expect(() => context.callMethod('invokeCallback'), throws); 559 final d = context['document'];
438 560 expect(o1 == d, isFalse);
439 context['callback'] = () => 42; 561 });
440 expect(context.callMethod('invokeCallback'), equals(42)); 562
441 563 test('toString', () {
442 context.deleteProperty('callback'); 564 var foo = new JsObject(context['Foo'], [42]);
565 expect(foo.toString(), equals("I'm a Foo a=42"));
566 var container = context['container'];
567 expect(container.toString(), equals("[object Object]"));
568 });
569
570 test('toString returns a String even if the JS object does not', () {
571 var foo = new JsObject(context['Liar']);
572 expect(foo.callMethod('toString'), 1);
573 expect(foo.toString(), '1');
574 });
575
576 test('instanceof', () {
577 var foo = new JsObject(context['Foo'], [1]);
578 expect(foo.instanceof(context['Foo']), isTrue);
579 expect(foo.instanceof(context['Object']), isTrue);
580 expect(foo.instanceof(context['String']), isFalse);
581 });
582
583 test('deleteProperty', () {
584 var object = new JsObject.jsify({});
585 object['a'] = 1;
586 expect(context['Object'].callMethod('keys', [object])['length'], 1);
587 expect(context['Object'].callMethod('keys', [object])[0], "a");
588 object.deleteProperty("a");
589 expect(context['Object'].callMethod('keys', [object])['length'], 0);
590 });
591
592 /* TODO(jacobr): this is another test that is inconsistent with JS semantics.
593 test('deleteProperty throws if name is not a String or num', () {
594 var object = new JsObject.jsify({});
595 expect(() => object.deleteProperty(true),
596 throwsA(new isInstanceOf<ArgumentError>()));
597 });
598 */
599
600 test('hasProperty', () {
601 var object = new JsObject.jsify({});
602 object['a'] = 1;
603 expect(object.hasProperty('a'), isTrue);
604 expect(object.hasProperty('b'), isFalse);
605 });
606
607 /* TODO(jacobr): remove this test as it is a dupe and not the correct checked
justinfagnani 2013/10/19 04:18:46 what's it a dupe of?
Jacob 2013/10/19 04:35:54 nevermind... there were other tests verifying that
608 mode behavior.
609 test('hasProperty throws if name is not a String or num', () {
610 var object = new JsObject.jsify({});
611 expect(() => object.hasProperty(true),
612 throwsA(new isInstanceOf<ArgumentError>()));
613 });
614 */
615
616 test('[] and []=', () {
617 final myArray = context['myArray'];
618 expect(myArray['length'], equals(1));
619 expect(myArray[0], equals("value1"));
620 myArray[0] = "value2";
621 expect(myArray['length'], equals(1));
622 expect(myArray[0], equals("value2"));
623
624 final foo = new JsObject(context['Foo'], [1]);
625 foo["getAge"] = () => 10;
626 expect(foo.callMethod('getAge'), equals(10));
627 });
628
629 /* TODO(jacobr): remove as we should only throw this in checked mode.
630 test('[] and []= throw if name is not a String or num', () {
631 var object = new JsObject.jsify({});
632 expect(() => object[true],
633 throwsA(new isInstanceOf<ArgumentError>()));
634 expect(() => object[true] = 1,
635 throwsA(new isInstanceOf<ArgumentError>()));
636 });
637 */
443 }); 638 });
444 639
445 test('callback as parameter', () { 640 group('transferrables', () {
446 expect(context.callMethod('getTypeOf', [context['razzle']]), 641
447 equals("function")); 642 group('JS->Dart', () {
448 }); 643
449 644 test('Date', () {
450 test('invoke Dart callback from JS with this', () { 645 var date = context.callMethod('getNewDate');
451 final constructor = new Callback.withThis(($this, arg1) { 646 expect(date is Date, isTrue);
452 $this['a'] = 42; 647 });
453 $this['b'] = jsify(["a", arg1]); 648
454 }); 649 test('window', () {
455 var o = new JsObject(constructor, ["b"]); 650 expect(context['window'] is Window, isFalse);
456 expect(o['a'], equals(42)); 651 });
457 expect(o['b'][0], equals("a")); 652
458 expect(o['b'][1], equals("b")); 653 test('document', () {
459 }); 654 expect(context['document'] is Document, isTrue);
460 655 });
461 test('invoke Dart callback from JS with 11 parameters', () { 656
462 context['callbackWith11params'] = new Callback((p1, p2, p3, p4, p5, p6, p7, 657 test('Blob', () {
463 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'); 658 var blob = context.callMethod('getNewBlob');
464 expect(context.callMethod('invokeCallbackWith11params'), 659 expect(blob is Blob, isTrue);
465 equals('1234567891011')); 660 expect(blob.type, equals('text/html'));
466 }); 661 });
467 662
468 test('return a JS proxy to JavaScript', () { 663 test('unattached DivElement', () {
469 var result = context.callMethod('testJsMap', [() => jsify({'value': 42})]); 664 var node = context.callMethod('getNewDivElement');
470 expect(result, 42); 665 expect(node is Div, isTrue);
471 }); 666 });
472 667
473 test('test instanceof', () { 668 test('KeyRange', () {
474 var foo = new JsObject(context['Foo'], [1]); 669 if (IdbFactory.supported) {
475 expect(foo.instanceof(context['Foo']), isTrue); 670 var node = context.callMethod('getNewIDBKeyRange');
476 expect(foo.instanceof(context['Object']), isTrue); 671 expect(node is KeyRange, isTrue);
477 expect(foo.instanceof(context['String']), isFalse); 672 }
478 }); 673 });
479 674
480 test('test deleteProperty', () { 675 test('ImageData', () {
481 var object = jsify({}); 676 var node = context.callMethod('getNewImageData');
482 object['a'] = 1; 677 expect(node is ImageData, isTrue);
483 expect(context['Object'].callMethod('keys', [object])['length'], 1); 678 });
484 expect(context['Object'].callMethod('keys', [object])[0], "a"); 679
485 object.deleteProperty("a"); 680 test('typed data: Int32Array', () {
486 expect(context['Object'].callMethod('keys', [object])['length'], 0); 681 var list = context.callMethod('getNewInt32Array');
487 }); 682 print(list);
488 683 expect(list is Int32List, isTrue);
489 test('test hasProperty', () { 684 expect(list, orderedEquals([1, 2, 3, 4, 5, 6, 7, 8]));
490 var object = jsify({}); 685 });
491 object['a'] = 1; 686
492 expect(object.hasProperty('a'), isTrue); 687 });
493 expect(object.hasProperty('b'), isFalse); 688
494 }); 689 group('Dart->JS', () {
495 690
496 test('test index get and set', () { 691 test('Date', () {
497 final myArray = context['myArray']; 692 context['o'] = new DateTime(1995, 12, 17);
498 expect(myArray['length'], equals(1)); 693 var dateType = context['Date'];
499 expect(myArray[0], equals("value1")); 694 expect(context.callMethod('isPropertyInstanceOf', ['o', dateType]),
500 myArray[0] = "value2"; 695 isTrue);
501 expect(myArray['length'], equals(1)); 696 context.deleteProperty('o');
502 expect(myArray[0], equals("value2")); 697 });
503 698
504 final foo = new JsObject(context['Foo'], [1]); 699 test('window', () {
505 foo["getAge"] = () => 10; 700 context['o'] = window;
506 expect(foo.callMethod('getAge'), equals(10)); 701 var windowType = context['Window'];
507 }); 702 expect(context.callMethod('isPropertyInstanceOf', ['o', windowType]),
508 703 isFalse);
509 test('access a property of a function', () { 704 context.deleteProperty('o');
510 expect(context.callMethod('Bar'), "ret_value"); 705 });
511 expect(context['Bar']['foo'], "property_value"); 706
512 }); 707 test('document', () {
513 708 context['o'] = document;
514 test('usage of Serializable', () { 709 var documentType = context['Document'];
515 final red = Color.RED; 710 expect(context.callMethod('isPropertyInstanceOf', ['o', documentType]),
516 context['color'] = red; 711 isTrue);
517 expect(context['color'], equals(red._value)); 712 context.deleteProperty('o');
713 });
714
715 test('Blob', () {
716 var fileParts = ['<a id="a"><b id="b">hey!</b></a>'];
717 context['o'] = new Blob(fileParts, 'text/html');
718 var blobType = context['Blob'];
719 expect(context.callMethod('isPropertyInstanceOf', ['o', blobType]),
720 isTrue);
721 context.deleteProperty('o');
722 });
723
724 test('unattached DivElement', () {
725 context['o'] = new DivElement();
726 var divType = context['HTMLDivElement'];
727 expect(context.callMethod('isPropertyInstanceOf', ['o', divType]),
728 isTrue);
729 context.deleteProperty('o');
730 });
731
732 test('KeyRange', () {
733 if (IdbFactory.supported) {
734 context['o'] = new KeyRange.only(1);
735 var keyRangeType = context['IDBKeyRange'];
736 expect(context.callMethod('isPropertyInstanceOf', ['o', keyRangeType]) ,
737 isTrue);
738 context.deleteProperty('o');
739 }
740 });
741
742 test('ImageData', () {
743 var canvas = new CanvasElement();
744 var ctx = canvas.getContext('2d');
745 context['o'] = ctx.createImageData(1, 1);
746 var imageDataType = context['ImageData'];
747 expect(context.callMethod('isPropertyInstanceOf', ['o', imageDataType]),
748 isTrue);
749 context.deleteProperty('o');
750 });
751
752 test('typed data: Int32List', () {
753 context['o'] = new Int32List.fromList([1, 2, 3, 4]);
754 var listType = context['Int32Array'];
755 // TODO(jacobr): make this test pass. Currently some type information
756 // is lost when typed arrays are passed between JS and Dart.
757 // expect(context.callMethod('isPropertyInstanceOf', ['o', listType]),
758 // isTrue);
759 context.deleteProperty('o');
760 });
761
762 });
518 }); 763 });
519 } 764 }
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