OLD | NEW |
---|---|
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 Loading... | |
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 |
114 function Baz(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11) { | 138 function Baz(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11) { |
115 this.f1 = p1; | 139 this.f1 = p1; |
116 this.f2 = p2; | 140 this.f2 = p2; |
117 this.f3 = p3; | 141 this.f3 = p3; |
118 this.f4 = p4; | 142 this.f4 = p4; |
119 this.f5 = p5; | 143 this.f5 = p5; |
120 this.f6 = p6; | 144 this.f6 = p6; |
121 this.f7 = p7; | 145 this.f7 = p7; |
122 this.f8 = p8; | 146 this.f8 = p8; |
123 this.f9 = p9; | 147 this.f9 = p9; |
124 this.f10 = p10; | 148 this.f10 = p10; |
125 this.f11 = p11; | 149 this.f11 = p11; |
126 } | 150 } |
127 | 151 |
152 function Liar(){} | |
153 | |
154 Liar.prototype.toString = function() { | |
155 return 1; | |
156 } | |
157 | |
128 function identical(o1, o2) { | 158 function identical(o1, o2) { |
129 return o1 === o2; | 159 return o1 === o2; |
130 } | 160 } |
131 """; | 161 """; |
132 document.body.append(script); | 162 document.body.append(script); |
133 } | 163 } |
134 | 164 |
135 class Foo implements Serializable<JsObject> { | 165 class Foo { |
136 final JsObject _proxy; | 166 final JsObject _proxy; |
137 | 167 |
138 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); | 168 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); |
139 | 169 |
140 JsObject toJs() => _proxy; | 170 JsObject toJs() => _proxy; |
141 | 171 |
142 num get a => _proxy['a']; | 172 num get a => _proxy['a']; |
143 num bar() => _proxy.callMethod('bar'); | 173 num bar() => _proxy.callMethod('bar'); |
144 } | 174 } |
145 | 175 |
146 class Color implements Serializable<String> { | 176 class Color { |
147 static final RED = new Color._("red"); | 177 static final RED = new Color._("red"); |
148 static final BLUE = new Color._("blue"); | 178 static final BLUE = new Color._("blue"); |
149 String _value; | 179 String _value; |
150 Color._(this._value); | 180 Color._(this._value); |
151 String toJs() => this._value; | 181 String toJs() => this._value; |
152 } | 182 } |
153 | 183 |
154 class TestDartObject {} | 184 class TestDartObject {} |
155 | 185 |
156 main() { | 186 main() { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 var foo1 = new JsObject(context['Foo'], [1]); | 224 var foo1 = new JsObject(context['Foo'], [1]); |
195 var foo2 = new JsObject(context['Foo'], [2]); | 225 var foo2 = new JsObject(context['Foo'], [2]); |
196 context['foo1'] = foo1; | 226 context['foo1'] = foo1; |
197 context['foo2'] = foo2; | 227 context['foo2'] = foo2; |
198 expect(foo1, isNot(equals(context['foo2']))); | 228 expect(foo1, isNot(equals(context['foo2']))); |
199 expect(foo2, same(context['foo2'])); | 229 expect(foo2, same(context['foo2'])); |
200 context.deleteProperty('foo1'); | 230 context.deleteProperty('foo1'); |
201 context.deleteProperty('foo2'); | 231 context.deleteProperty('foo2'); |
202 }); | 232 }); |
203 | 233 |
204 test('retrieve same dart Object', () { | 234 // TODO(justinfagnani): appears to be flaky? I didn't actually |
235 // expect this to work at all after allowing DateTimes to transfer | |
236 // by simply returning the JS Date object. | |
237 skip_test('retrieve same dart Object', () { | |
205 final date = new DateTime.now(); | 238 final date = new DateTime.now(); |
alexandre.ardhuin
2013/10/18 07:03:45
You can replace `new DateTime.now()` with `new Obj
justinfagnani
2013/10/18 22:38:41
Done.
| |
206 context['dartDate'] = date; | 239 context['dartDate'] = date; |
207 expect(context['dartDate'], same(date)); | 240 expect(context['dartDate'], same(date)); |
208 context.deleteProperty('dartDate'); | 241 context.deleteProperty('dartDate'); |
209 }); | 242 }); |
210 | 243 |
211 }); | 244 }); |
212 | 245 |
213 test('read global field', () { | 246 group('context', () { |
214 expect(context['x'], equals(42)); | 247 |
215 expect(context['y'], isNull); | 248 test('read global field', () { |
216 }); | 249 expect(context['x'], equals(42)); |
217 | 250 expect(context['y'], isNull); |
218 test('read global field with underscore', () { | 251 }); |
219 expect(context['_x'], equals(123)); | 252 |
220 expect(context['y'], isNull); | 253 test('read global field with underscore', () { |
221 }); | 254 expect(context['_x'], equals(123)); |
222 | 255 expect(context['y'], isNull); |
223 test('hashCode and operator==(other)', () { | 256 }); |
224 final o1 = context['Object']; | 257 |
225 final o2 = context['Object']; | 258 test('write global field', () { |
226 expect(o1 == o2, isTrue); | 259 context['y'] = 42; |
227 expect(o1.hashCode == o2.hashCode, isTrue); | 260 expect(context['y'], equals(42)); |
228 final d = context['document']; | 261 }); |
229 expect(o1 == d, isFalse); | 262 |
230 }); | 263 }); |
231 | 264 |
232 test('js instantiation : new Foo()', () { | 265 group('new JsObject()', () { |
233 final Foo2 = context['container']['Foo']; | 266 |
234 final foo = new JsObject(Foo2, [42]); | 267 test('new Foo()', () { |
235 expect(foo['a'], 42); | 268 var foo = new JsObject(context['Foo'], [42]); |
236 expect(Foo2['b'], 38); | 269 expect(foo['a'], equals(42)); |
237 }); | 270 expect(foo.callMethod('bar'), equals(42)); |
238 | 271 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); |
239 test('js instantiation : new Array()', () { | 272 }); |
240 final a = new JsObject(context['Array']); | 273 |
241 expect(a, isNotNull); | 274 test('new container.Foo()', () { |
242 expect(a['length'], equals(0)); | 275 final Foo2 = context['container']['Foo']; |
243 | 276 final foo = new JsObject(Foo2, [42]); |
244 a.callMethod('push', ["value 1"]); | 277 expect(foo['a'], 42); |
245 expect(a['length'], equals(1)); | 278 expect(Foo2['b'], 38); |
246 expect(a[0], equals("value 1")); | 279 }); |
247 | 280 |
248 a.callMethod('pop'); | 281 test('new Array()', () { |
249 expect(a['length'], equals(0)); | 282 final a = new JsObject(context['Array']); |
250 }); | 283 expect(a, isNotNull); |
251 | 284 expect(a['length'], equals(0)); |
252 test('js instantiation : new Date()', () { | 285 |
253 final a = new JsObject(context['Date']); | 286 a.callMethod('push', ["value 1"]); |
254 expect(a.callMethod('getTime'), isNotNull); | 287 expect(a['length'], equals(1)); |
255 }); | 288 expect(a[0], equals("value 1")); |
256 | 289 |
257 test('js instantiation : new Date(12345678)', () { | 290 a.callMethod('pop'); |
258 final a = new JsObject(context['Date'], [12345678]); | 291 expect(a['length'], equals(0)); |
259 expect(a.callMethod('getTime'), equals(12345678)); | 292 }); |
260 }); | 293 |
261 | 294 test('new Date()', () { |
262 test('js instantiation : new Date("December 17, 1995 03:24:00 GMT")', | 295 final a = new JsObject(context['Date']); |
263 () { | 296 expect(a.callMethod('getTime'), isNotNull); |
264 final a = new JsObject(context['Date'], | 297 }); |
265 ["December 17, 1995 03:24:00 GMT"]); | 298 |
266 expect(a.callMethod('getTime'), equals(819170640000)); | 299 test('new Date(12345678)', () { |
267 }); | 300 final a = new JsObject(context['Date'], [12345678]); |
268 | 301 expect(a.callMethod('getTime'), equals(12345678)); |
269 test('js instantiation : new Date(1995,11,17)', () { | 302 }); |
270 // Note: JS Date counts months from 0 while Dart counts from 1. | 303 |
271 final a = new JsObject(context['Date'], [1995, 11, 17]); | 304 test('new Date("December 17, 1995 03:24:00 GMT")', |
272 final b = new DateTime(1995, 12, 17); | 305 () { |
273 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); | 306 final a = new JsObject(context['Date'], |
274 }); | 307 ["December 17, 1995 03:24:00 GMT"]); |
275 | 308 expect(a.callMethod('getTime'), equals(819170640000)); |
276 test('js instantiation : new Date(1995,11,17,3,24,0)', () { | 309 }); |
277 // Note: JS Date counts months from 0 while Dart counts from 1. | 310 |
278 final a = new JsObject(context['Date'], | 311 test('new Date(1995,11,17)', () { |
279 [1995, 11, 17, 3, 24, 0]); | 312 // Note: JS Date counts months from 0 while Dart counts from 1. |
280 final b = new DateTime(1995, 12, 17, 3, 24, 0); | 313 final a = new JsObject(context['Date'], [1995, 11, 17]); |
281 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); | 314 final b = new DateTime(1995, 12, 17); |
282 }); | 315 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); |
283 | 316 }); |
284 test('js instantiation : new Object()', () { | 317 |
285 final a = new JsObject(context['Object']); | 318 test('new Date(1995,11,17,3,24,0)', () { |
286 expect(a, isNotNull); | 319 // Note: JS Date counts months from 0 while Dart counts from 1. |
287 | 320 final a = new JsObject(context['Date'], |
288 a['attr'] = "value"; | 321 [1995, 11, 17, 3, 24, 0]); |
289 expect(a['attr'], equals("value")); | 322 final b = new DateTime(1995, 12, 17, 3, 24, 0); |
290 }); | 323 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); |
291 | 324 }); |
292 test(r'js instantiation : new RegExp("^\w+$")', () { | 325 |
293 final a = new JsObject(context['RegExp'], [r'^\w+$']); | 326 test('new Object()', () { |
294 expect(a, isNotNull); | 327 final a = new JsObject(context['Object']); |
295 expect(a.callMethod('test', ['true']), isTrue); | 328 expect(a, isNotNull); |
296 expect(a.callMethod('test', [' false']), isFalse); | 329 |
297 }); | 330 a['attr'] = "value"; |
298 | 331 expect(a['attr'], equals("value")); |
299 test('js instantiation via map notation : new Array()', () { | 332 }); |
300 final a = new JsObject(context['Array']); | 333 |
301 expect(a, isNotNull); | 334 test(r'new RegExp("^\w+$")', () { |
302 expect(a['length'], equals(0)); | 335 final a = new JsObject(context['RegExp'], [r'^\w+$']); |
303 | 336 expect(a, isNotNull); |
304 a['push'].apply(a, ["value 1"]); | 337 expect(a.callMethod('test', ['true']), isTrue); |
305 expect(a['length'], equals(1)); | 338 expect(a.callMethod('test', [' false']), isFalse); |
306 expect(a[0], equals("value 1")); | 339 }); |
307 | 340 |
308 a['pop'].apply(a); | 341 test('js instantiation via map notation : new Array()', () { |
309 expect(a['length'], equals(0)); | 342 final a = new JsObject(context['Array']); |
310 }); | 343 expect(a, isNotNull); |
311 | 344 expect(a['length'], equals(0)); |
312 test('js instantiation via map notation : new Date()', () { | 345 |
313 final a = new JsObject(context['Date']); | 346 a['push'].apply(a, ["value 1"]); |
314 expect(a['getTime'].apply(a), isNotNull); | 347 expect(a['length'], equals(1)); |
315 }); | 348 expect(a[0], equals("value 1")); |
316 | 349 |
317 test('js instantiation : typed array', () { | 350 a['pop'].apply(a); |
318 if (Platform.supportsTypedData) { | 351 expect(a['length'], equals(0)); |
319 // Safari's ArrayBuffer is not a Function and so doesn't support bind | 352 }); |
320 // which JsObject's constructor relies on. | 353 |
321 // bug: https://bugs.webkit.org/show_bug.cgi?id=122976 | 354 test('js instantiation via map notation : new Date()', () { |
322 if (context['ArrayBuffer']['bind'] != null) { | 355 final a = new JsObject(context['Date']); |
323 final codeUnits = "test".codeUnits; | 356 expect(a['getTime'].apply(a), isNotNull); |
324 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); | 357 }); |
325 final bufView = new JsObject(context['Uint8Array'], [buf]); | 358 |
326 for (var i = 0; i < codeUnits.length; i++) { | 359 test('typed array', () { |
327 bufView[i] = codeUnits[i]; | 360 if (Platform.supportsTypedData) { |
361 // Safari's ArrayBuffer is not a Function and so doesn't support bind | |
362 // which JsObject's constructor relies on. | |
363 // bug: https://bugs.webkit.org/show_bug.cgi?id=122976 | |
364 if (context['ArrayBuffer']['bind'] != null) { | |
365 final codeUnits = "test".codeUnits; | |
366 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); | |
367 final bufView = new JsObject(context['Uint8Array'], [buf]); | |
368 for (var i = 0; i < codeUnits.length; i++) { | |
369 bufView[i] = codeUnits[i]; | |
370 } | |
328 } | 371 } |
329 } | 372 } |
330 } | 373 }); |
331 }); | 374 |
332 | 375 test('>10 parameters', () { |
333 test('js instantiation : >10 parameters', () { | 376 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]); | 377 for (var i = 1; i <= 11; i++) { |
335 for (var i = 1; i <= 11; i++) { | 378 expect(o["f$i"], i); |
336 expect(o["f$i"], i); | 379 } |
337 } | 380 expect(o['constructor'], same(context['Baz'])); |
338 expect(o['constructor'], same(context['Baz'])); | 381 }); |
339 }); | 382 }); |
340 | 383 |
341 test('write global field', () { | 384 group('JsFunction and callMethod', () { |
342 context['y'] = 42; | 385 |
343 expect(context['y'], equals(42)); | 386 test('JsFunction.apply on a function defined in JS', () { |
344 }); | 387 expect(context['razzle'].apply(context), equals(42)); |
345 | 388 }); |
346 test('get JS JsFunction', () { | 389 |
347 var razzle = context['razzle']; | 390 test('JsObject.callMethod on a function defined in JS', () { |
348 expect(razzle.apply(context), equals(42)); | 391 expect(context.callMethod('razzle'), equals(42)); |
349 }); | 392 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); |
350 | 393 }); |
351 test('call JS function', () { | 394 |
352 expect(context.callMethod('razzle'), equals(42)); | 395 // all this tests is that null doesn't have the method apply? |
alexandre.ardhuin
2013/10/18 07:03:45
This test can be removed.
justinfagnani
2013/10/18 22:38:41
Done.
| |
353 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); | 396 skip_test('call JS function via map notation', () { |
354 }); | 397 expect(() => context['dazzle'].apply(context), |
355 | 398 throwsA(isNoSuchMethodError)); |
356 test('call JS function via map notation', () { | 399 }); |
357 expect(context['razzle'].apply(context), equals(42)); | 400 |
358 expect(() => context['dazzle'].apply(context), | 401 test('callMethod with many arguments', () { |
359 throwsA(isNoSuchMethodError)); | 402 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), |
360 }); | 403 equals(55)); |
361 | 404 }); |
362 test('call JS function with varargs', () { | 405 |
363 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), | 406 test('access a property of a function', () { |
364 equals(55)); | 407 expect(context.callMethod('Bar'), "ret_value"); |
365 }); | 408 expect(context['Bar']['foo'], "property_value"); |
366 | 409 }); |
367 test('allocate JS object', () { | 410 |
368 var foo = new JsObject(context['Foo'], [42]); | 411 test('callMethod throws if name is not a String or num', () { |
369 expect(foo['a'], equals(42)); | 412 expect(() => context.callMethod(true), |
370 expect(foo.callMethod('bar'), equals(42)); | 413 throwsA(new isInstanceOf<ArgumentError>())); |
371 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); | 414 }); |
372 }); | 415 |
373 | 416 }); |
374 test('call toString()', () { | 417 |
375 var foo = new JsObject(context['Foo'], [42]); | 418 group('JsObject.fromBrowserObject()', () { |
376 expect(foo.toString(), equals("I'm a Foo a=42")); | 419 |
377 var container = context['container']; | 420 test('Nodes are proxied', () { |
378 expect(container.toString(), equals("[object Object]")); | 421 var node = new JsObject.fromBrowserObject(new DivElement()); |
379 }); | 422 expect(node is JsObject, isTrue); |
380 | 423 expect(node.instanceof(context['HTMLDivElement']), isTrue); |
381 test('allocate simple JS array', () { | 424 }); |
382 final list = [1, 2, 3, 4, 5, 6, 7, 8]; | 425 |
383 var array = jsify(list); | 426 test('primitives and null throw ArgumentError', () { |
384 expect(context.callMethod('isArray', [array]), isTrue); | 427 for (var v in ['a', 1, 2.0, true, null]) { |
385 expect(array['length'], equals(list.length)); | 428 expect(() => new JsObject.fromBrowserObject(v), |
386 for (var i = 0; i < list.length ; i++) { | 429 throwsA(new isInstanceOf<ArgumentError>())); |
387 expect(array[i], equals(list[i])); | 430 } |
388 } | 431 }); |
389 }); | 432 |
390 | 433 }); |
391 test('allocate JS array with iterable', () { | 434 |
392 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); | 435 group('Dart callback', () { |
393 var array = jsify(set); | 436 test('invoke Dart callback from JS', () { |
394 expect(context.callMethod('isArray', [array]), isTrue); | 437 expect(() => context.callMethod('invokeCallback'), throws); |
395 expect(array['length'], equals(set.length)); | 438 |
396 for (var i = 0; i < array['length'] ; i++) { | 439 // context['callback'] = new Callback(() => 42); |
397 expect(set.contains(array[i]), isTrue); | 440 // expect(context.callMethod('invokeCallback'), equals(42)); |
398 } | 441 |
399 }); | 442 // context.deleteProperty('callback'); |
400 | 443 // expect(() => context.callMethod('invokeCallback'), throws); |
401 test('allocate simple JS map', () { | 444 |
402 var map = {'a': 1, 'b': 2, 'c': 3}; | 445 context['callback'] = () => 42; |
403 var jsMap = jsify(map); | 446 expect(context.callMethod('invokeCallback'), equals(42)); |
404 expect(!context.callMethod('isArray', [jsMap]), isTrue); | 447 |
405 for (final key in map.keys) { | 448 context.deleteProperty('callback'); |
406 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue); | 449 }); |
407 } | 450 |
408 }); | 451 test('callback as parameter', () { |
409 | 452 expect(context.callMethod('getTypeOf', [context['razzle']]), |
410 test('allocate complex JS object', () { | 453 equals("function")); |
411 final object = | 454 }); |
412 { | 455 |
456 test('invoke Dart callback from JS with this', () { | |
457 // A JavaScript constructor function implemented in Dart which | |
458 // uses 'this' | |
459 final constructor = new JsFunction.withThis(($this, arg1) { | |
460 var t = $this; | |
461 $this['a'] = 42; | |
462 }); | |
463 var o = new JsObject(constructor, ["b"]); | |
464 expect(o['a'], equals(42)); | |
465 }); | |
466 | |
467 test('invoke Dart callback from JS with 11 parameters', () { | |
468 context['callbackWith11params'] = (p1, p2, p3, p4, p5, p6, p7, | |
469 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'; | |
470 expect(context.callMethod('invokeCallbackWith11params'), | |
471 equals('1234567891011')); | |
472 }); | |
473 | |
474 test('return a JS proxy to JavaScript', () { | |
475 var result = context.callMethod('testJsMap', [() => new JsObject.jsify({'v alue': 42})]); | |
476 expect(result, 42); | |
477 }); | |
478 | |
479 }); | |
480 | |
481 group('JsObject.jsify()', () { | |
alexandre.ardhuin
2013/10/18 07:03:45
Add a test for cycle handling ?
| |
482 | |
483 test('convert a List', () { | |
484 final list = [1, 2, 3, 4, 5, 6, 7, 8]; | |
485 var array = new JsObject.jsify(list); | |
486 expect(context.callMethod('isArray', [array]), isTrue); | |
487 expect(array['length'], equals(list.length)); | |
488 for (var i = 0; i < list.length ; i++) { | |
489 expect(array[i], equals(list[i])); | |
490 } | |
491 }); | |
492 | |
493 test('convert an Iterable', () { | |
494 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); | |
495 var array = new JsObject.jsify(set); | |
496 expect(context.callMethod('isArray', [array]), isTrue); | |
497 expect(array['length'], equals(set.length)); | |
498 for (var i = 0; i < array['length'] ; i++) { | |
499 expect(set.contains(array[i]), isTrue); | |
500 } | |
501 }); | |
502 | |
503 test('convert a Map', () { | |
504 var map = {'a': 1, 'b': 2, 'c': 3}; | |
505 var jsMap = new JsObject.jsify(map); | |
506 expect(!context.callMethod('isArray', [jsMap]), isTrue); | |
507 for (final key in map.keys) { | |
508 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue); | |
509 } | |
510 }); | |
511 | |
512 test('deep convert a complex object', () { | |
513 final object = { | |
413 'a': [1, [2, 3]], | 514 'a': [1, [2, 3]], |
414 'b': { | 515 'b': { |
415 'c': 3, | 516 'c': 3, |
416 'd': new JsObject(context['Foo'], [42]) | 517 'd': new JsObject(context['Foo'], [42]) |
417 }, | 518 }, |
418 'e': null | 519 'e': null |
419 }; | 520 }; |
420 var jsObject = jsify(object); | 521 var jsObject = new JsObject.jsify(object); |
421 expect(jsObject['a'][0], equals(object['a'][0])); | 522 expect(jsObject['a'][0], equals(object['a'][0])); |
422 expect(jsObject['a'][1][0], equals(object['a'][1][0])); | 523 expect(jsObject['a'][1][0], equals(object['a'][1][0])); |
423 expect(jsObject['a'][1][1], equals(object['a'][1][1])); | 524 expect(jsObject['a'][1][1], equals(object['a'][1][1])); |
424 expect(jsObject['b']['c'], equals(object['b']['c'])); | 525 expect(jsObject['b']['c'], equals(object['b']['c'])); |
425 expect(jsObject['b']['d'], equals(object['b']['d'])); | 526 expect(jsObject['b']['d'], equals(object['b']['d'])); |
426 expect(jsObject['b']['d'].callMethod('bar'), equals(42)); | 527 expect(jsObject['b']['d'].callMethod('bar'), equals(42)); |
427 expect(jsObject['e'], isNull); | 528 expect(jsObject['e'], isNull); |
529 }); | |
530 | |
531 test('throws if object is not a Map or Iterable', () { | |
532 expect(() => new JsObject.jsify('a'), | |
533 throwsA(new isInstanceOf<ArgumentError>())); | |
534 }); | |
428 }); | 535 }); |
429 | 536 |
430 test('invoke Dart callback from JS', () { | 537 group('JsObject methods', () { |
431 expect(() => context.callMethod('invokeCallback'), throws); | 538 |
432 | 539 test('hashCode and ==', () { |
433 context['callback'] = new Callback(() => 42); | 540 final o1 = context['Object']; |
434 expect(context.callMethod('invokeCallback'), equals(42)); | 541 final o2 = context['Object']; |
435 | 542 expect(o1 == o2, isTrue); |
436 context.deleteProperty('callback'); | 543 expect(o1.hashCode == o2.hashCode, isTrue); |
437 expect(() => context.callMethod('invokeCallback'), throws); | 544 final d = context['document']; |
438 | 545 expect(o1 == d, isFalse); |
439 context['callback'] = () => 42; | 546 }); |
440 expect(context.callMethod('invokeCallback'), equals(42)); | 547 |
441 | 548 test('toString', () { |
442 context.deleteProperty('callback'); | 549 var foo = new JsObject(context['Foo'], [42]); |
550 expect(foo.toString(), equals("I'm a Foo a=42")); | |
551 var container = context['container']; | |
552 expect(container.toString(), equals("[object Object]")); | |
553 }); | |
554 | |
555 test('toString returns a String even if the JS object does not', () { | |
556 var foo = new JsObject(context['Liar']); | |
557 expect(foo.callMethod('toString'), 1); | |
558 expect(foo.toString(), '1'); | |
559 }); | |
560 | |
561 test('instanceof', () { | |
562 var foo = new JsObject(context['Foo'], [1]); | |
563 expect(foo.instanceof(context['Foo']), isTrue); | |
564 expect(foo.instanceof(context['Object']), isTrue); | |
565 expect(foo.instanceof(context['String']), isFalse); | |
566 }); | |
567 | |
568 test('deleteProperty', () { | |
569 var object = new JsObject.jsify({}); | |
570 object['a'] = 1; | |
571 expect(context['Object'].callMethod('keys', [object])['length'], 1); | |
572 expect(context['Object'].callMethod('keys', [object])[0], "a"); | |
573 object.deleteProperty("a"); | |
574 expect(context['Object'].callMethod('keys', [object])['length'], 0); | |
575 }); | |
576 | |
577 test('deleteProperty throws if name is not a String or num', () { | |
578 var object = new JsObject.jsify({}); | |
579 expect(() => object.deleteProperty(true), | |
580 throwsA(new isInstanceOf<ArgumentError>())); | |
581 }); | |
582 | |
583 test('hasProperty', () { | |
584 var object = new JsObject.jsify({}); | |
585 object['a'] = 1; | |
586 expect(object.hasProperty('a'), isTrue); | |
587 expect(object.hasProperty('b'), isFalse); | |
588 }); | |
589 | |
590 test('hasProperty throws if name is not a String or num', () { | |
591 var object = new JsObject.jsify({}); | |
592 expect(() => object.hasProperty(true), | |
593 throwsA(new isInstanceOf<ArgumentError>())); | |
594 }); | |
595 | |
596 test('[] and []=', () { | |
597 final myArray = context['myArray']; | |
598 expect(myArray['length'], equals(1)); | |
599 expect(myArray[0], equals("value1")); | |
600 myArray[0] = "value2"; | |
601 expect(myArray['length'], equals(1)); | |
602 expect(myArray[0], equals("value2")); | |
603 | |
604 final foo = new JsObject(context['Foo'], [1]); | |
605 foo["getAge"] = () => 10; | |
606 expect(foo.callMethod('getAge'), equals(10)); | |
607 }); | |
608 | |
609 test('[] and []= throw if name is not a String or num', () { | |
610 var object = new JsObject.jsify({}); | |
611 expect(() => object[true], | |
612 throwsA(new isInstanceOf<ArgumentError>())); | |
613 expect(() => object[true] = 1, | |
614 throwsA(new isInstanceOf<ArgumentError>())); | |
615 }); | |
616 | |
443 }); | 617 }); |
444 | 618 |
445 test('callback as parameter', () { | 619 group('transferrables', () { |
446 expect(context.callMethod('getTypeOf', [context['razzle']]), | 620 |
447 equals("function")); | 621 group('JS->Dart', () { |
448 }); | 622 |
449 | 623 test('Date', () { |
450 test('invoke Dart callback from JS with this', () { | 624 var date = context.callMethod('getNewDate'); |
451 final constructor = new Callback.withThis(($this, arg1) { | 625 expect(date is Date, isTrue); |
452 $this['a'] = 42; | 626 }); |
453 $this['b'] = jsify(["a", arg1]); | 627 |
454 }); | 628 test('window', () { |
455 var o = new JsObject(constructor, ["b"]); | 629 expect(context['window'] is Window, isFalse); |
456 expect(o['a'], equals(42)); | 630 }); |
457 expect(o['b'][0], equals("a")); | 631 |
458 expect(o['b'][1], equals("b")); | 632 test('document', () { |
459 }); | 633 expect(context['document'] is Document, isTrue); |
460 | 634 }); |
461 test('invoke Dart callback from JS with 11 parameters', () { | 635 |
462 context['callbackWith11params'] = new Callback((p1, p2, p3, p4, p5, p6, p7, | 636 test('Blob', () { |
463 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'); | 637 var blob = context.callMethod('getNewBlob'); |
464 expect(context.callMethod('invokeCallbackWith11params'), | 638 expect(blob is Blob, isTrue); |
465 equals('1234567891011')); | 639 expect(blob.type, equals('text/html')); |
466 }); | 640 }); |
467 | 641 |
468 test('return a JS proxy to JavaScript', () { | 642 test('unattached DivElement', () { |
469 var result = context.callMethod('testJsMap', [() => jsify({'value': 42})]); | 643 var node = context.callMethod('getNewDivElement'); |
470 expect(result, 42); | 644 expect(node is Div, isTrue); |
471 }); | 645 }); |
472 | 646 |
473 test('test instanceof', () { | 647 test('KeyRange', () { |
474 var foo = new JsObject(context['Foo'], [1]); | 648 var node = context.callMethod('getNewIDBKeyRange'); |
475 expect(foo.instanceof(context['Foo']), isTrue); | 649 expect(node is KeyRange, isTrue); |
476 expect(foo.instanceof(context['Object']), isTrue); | 650 }); |
477 expect(foo.instanceof(context['String']), isFalse); | 651 |
478 }); | 652 test('ImageData', () { |
479 | 653 var node = context.callMethod('getNewImageData'); |
480 test('test deleteProperty', () { | 654 expect(node is ImageData, isTrue); |
481 var object = jsify({}); | 655 }); |
482 object['a'] = 1; | 656 |
483 expect(context['Object'].callMethod('keys', [object])['length'], 1); | 657 }); |
484 expect(context['Object'].callMethod('keys', [object])[0], "a"); | 658 |
485 object.deleteProperty("a"); | 659 group('Dart->JS', () { |
486 expect(context['Object'].callMethod('keys', [object])['length'], 0); | 660 |
487 }); | 661 test('Date', () { |
488 | 662 context['o'] = new DateTime(1995, 12, 17); |
489 test('test hasProperty', () { | 663 var dateType = context['Date']; |
490 var object = jsify({}); | 664 expect(context.callMethod('isPropertyInstanceOf', ['o', dateType]), |
491 object['a'] = 1; | 665 isTrue); |
492 expect(object.hasProperty('a'), isTrue); | 666 context.deleteProperty('o'); |
493 expect(object.hasProperty('b'), isFalse); | 667 }); |
494 }); | 668 |
495 | 669 test('window', () { |
496 test('test index get and set', () { | 670 context['o'] = window; |
497 final myArray = context['myArray']; | 671 var windowType = context['Window']; |
498 expect(myArray['length'], equals(1)); | 672 expect(context.callMethod('isPropertyInstanceOf', ['o', windowType]), |
499 expect(myArray[0], equals("value1")); | 673 isFalse); |
500 myArray[0] = "value2"; | 674 context.deleteProperty('o'); |
501 expect(myArray['length'], equals(1)); | 675 }); |
502 expect(myArray[0], equals("value2")); | 676 |
503 | 677 test('document', () { |
504 final foo = new JsObject(context['Foo'], [1]); | 678 context['o'] = document; |
505 foo["getAge"] = () => 10; | 679 var documentType = context['Document']; |
506 expect(foo.callMethod('getAge'), equals(10)); | 680 expect(context.callMethod('isPropertyInstanceOf', ['o', documentType]), |
507 }); | 681 isTrue); |
508 | 682 context.deleteProperty('o'); |
509 test('access a property of a function', () { | 683 }); |
510 expect(context.callMethod('Bar'), "ret_value"); | 684 |
511 expect(context['Bar']['foo'], "property_value"); | 685 test('Blob', () { |
512 }); | 686 var fileParts = ['<a id="a"><b id="b">hey!</b></a>']; |
513 | 687 context['o'] = new Blob(fileParts, 'text/html'); |
514 test('usage of Serializable', () { | 688 var blobType = context['Blob']; |
515 final red = Color.RED; | 689 expect(context.callMethod('isPropertyInstanceOf', ['o', blobType]), |
516 context['color'] = red; | 690 isTrue); |
517 expect(context['color'], equals(red._value)); | 691 context.deleteProperty('o'); |
692 }); | |
693 | |
694 test('unattached DivElement', () { | |
695 context['o'] = new DivElement(); | |
696 var divType = context['HTMLDivElement']; | |
697 expect(context.callMethod('isPropertyInstanceOf', ['o', divType]), | |
698 isTrue); | |
699 context.deleteProperty('o'); | |
700 }); | |
701 | |
702 test('KeyRange', () { | |
703 context['o'] = new KeyRange.only(1); | |
704 var keyRangeType = context['IDBKeyRange']; | |
705 expect(context.callMethod('isPropertyInstanceOf', ['o', keyRangeType]), | |
706 isTrue); | |
707 context.deleteProperty('o'); | |
708 }); | |
709 | |
710 test('ImageData', () { | |
711 var canvas = new CanvasElement(); | |
712 var ctx = canvas.getContext('2d'); | |
713 context['o'] = ctx.createImageData(1, 1); | |
714 var imageDataType = context['ImageData']; | |
715 expect(context.callMethod('isPropertyInstanceOf', ['o', imageDataType]), | |
716 isTrue); | |
717 context.deleteProperty('o'); | |
718 }); | |
719 }); | |
518 }); | 720 }); |
519 } | 721 } |
OLD | NEW |