Chromium Code Reviews| 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: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; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 } | 92 } |
| 91 | 93 |
| 92 function addClassAttributes(list) { | 94 function addClassAttributes(list) { |
| 93 var result = ""; | 95 var result = ""; |
| 94 for (var i=0; i < list.length; i++) { | 96 for (var i=0; i < list.length; i++) { |
| 95 result += list[i].getAttribute("class"); | 97 result += list[i].getAttribute("class"); |
| 96 } | 98 } |
| 97 return result; | 99 return result; |
| 98 } | 100 } |
| 99 | 101 |
| 102 function getNewDate() { | |
| 103 return new Date(1995, 11, 17); | |
| 104 } | |
| 105 | |
| 100 function getNewDivElement() { | 106 function getNewDivElement() { |
| 101 return document.createElement("div"); | 107 return document.createElement("div"); |
| 102 } | 108 } |
| 103 | 109 |
| 110 function getNewBlob() { | |
| 111 var fileParts = ['<a id="a"><b id="b">hey!</b></a>']; | |
| 112 return new Blob(fileParts, {type : 'text/html'}); | |
| 113 } | |
| 114 | |
| 115 function getNewIDBKeyRange() { | |
| 116 return IDBKeyRange.only(1); | |
| 117 } | |
| 118 | |
| 119 function getNewImageData() { | |
| 120 var canvas = document.createElement('canvas'); | |
| 121 var context = canvas.getContext('2d'); | |
| 122 return context.createImageData(1, 1); | |
| 123 } | |
| 124 | |
| 125 function getNewInt32Array() { | |
| 126 return new Int32Array([1, 2, 3, 4, 5, 6, 7, 8]); | |
| 127 } | |
| 128 | |
| 129 function getNewArrayBuffer() { | |
| 130 return new ArrayBuffer(8); | |
| 131 } | |
| 132 | |
| 133 function isPropertyInstanceOf(property, type) { | |
| 134 return window[property] instanceof type; | |
| 135 } | |
| 136 | |
| 104 function testJsMap(callback) { | 137 function testJsMap(callback) { |
| 105 var result = callback(); | 138 var result = callback(); |
| 106 return result['value']; | 139 return result['value']; |
| 107 } | 140 } |
| 108 | 141 |
| 109 function Bar() { | 142 function Bar() { |
| 110 return "ret_value"; | 143 return "ret_value"; |
| 111 } | 144 } |
| 112 Bar.foo = "property_value"; | 145 Bar.foo = "property_value"; |
| 113 | 146 |
| 114 function Baz(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11) { | 147 function Baz(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11) { |
| 115 this.f1 = p1; | 148 this.f1 = p1; |
| 116 this.f2 = p2; | 149 this.f2 = p2; |
| 117 this.f3 = p3; | 150 this.f3 = p3; |
| 118 this.f4 = p4; | 151 this.f4 = p4; |
| 119 this.f5 = p5; | 152 this.f5 = p5; |
| 120 this.f6 = p6; | 153 this.f6 = p6; |
| 121 this.f7 = p7; | 154 this.f7 = p7; |
| 122 this.f8 = p8; | 155 this.f8 = p8; |
| 123 this.f9 = p9; | 156 this.f9 = p9; |
| 124 this.f10 = p10; | 157 this.f10 = p10; |
| 125 this.f11 = p11; | 158 this.f11 = p11; |
| 126 } | 159 } |
| 127 | 160 |
| 161 function Liar(){} | |
| 162 | |
| 163 Liar.prototype.toString = function() { | |
| 164 return 1; | |
| 165 } | |
| 166 | |
| 128 function identical(o1, o2) { | 167 function identical(o1, o2) { |
| 129 return o1 === o2; | 168 return o1 === o2; |
| 130 } | 169 } |
| 131 """; | 170 """; |
| 132 document.body.append(script); | 171 document.body.append(script); |
| 133 } | 172 } |
| 134 | 173 |
| 135 class Foo implements Serializable<JsObject> { | 174 class Foo { |
| 136 final JsObject _proxy; | 175 final JsObject _proxy; |
| 137 | 176 |
| 138 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); | 177 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); |
| 139 | 178 |
| 140 JsObject toJs() => _proxy; | 179 JsObject toJs() => _proxy; |
| 141 | 180 |
| 142 num get a => _proxy['a']; | 181 num get a => _proxy['a']; |
| 143 num bar() => _proxy.callMethod('bar'); | 182 num bar() => _proxy.callMethod('bar'); |
| 144 } | 183 } |
| 145 | 184 |
| 146 class Color implements Serializable<String> { | 185 class Color { |
| 147 static final RED = new Color._("red"); | 186 static final RED = new Color._("red"); |
| 148 static final BLUE = new Color._("blue"); | 187 static final BLUE = new Color._("blue"); |
| 149 String _value; | 188 String _value; |
| 150 Color._(this._value); | 189 Color._(this._value); |
| 151 String toJs() => this._value; | 190 String toJs() => this._value; |
| 152 } | 191 } |
| 153 | 192 |
| 154 class TestDartObject {} | 193 class TestDartObject {} |
| 155 | 194 |
| 156 main() { | 195 main() { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 var foo2 = new JsObject(context['Foo'], [2]); | 234 var foo2 = new JsObject(context['Foo'], [2]); |
| 196 context['foo1'] = foo1; | 235 context['foo1'] = foo1; |
| 197 context['foo2'] = foo2; | 236 context['foo2'] = foo2; |
| 198 expect(foo1, isNot(equals(context['foo2']))); | 237 expect(foo1, isNot(equals(context['foo2']))); |
| 199 expect(foo2, same(context['foo2'])); | 238 expect(foo2, same(context['foo2'])); |
| 200 context.deleteProperty('foo1'); | 239 context.deleteProperty('foo1'); |
| 201 context.deleteProperty('foo2'); | 240 context.deleteProperty('foo2'); |
| 202 }); | 241 }); |
| 203 | 242 |
| 204 test('retrieve same dart Object', () { | 243 test('retrieve same dart Object', () { |
| 205 final date = new DateTime.now(); | 244 final obj = new Object(); |
| 206 context['dartDate'] = date; | 245 context['obj'] = obj; |
| 207 expect(context['dartDate'], same(date)); | 246 expect(context['obj'], same(obj)); |
| 208 context.deleteProperty('dartDate'); | 247 context.deleteProperty('obj'); |
| 209 }); | 248 }); |
| 210 | 249 |
| 211 }); | 250 }); |
| 212 | 251 |
| 213 test('read global field', () { | 252 group('context', () { |
| 214 expect(context['x'], equals(42)); | 253 |
| 215 expect(context['y'], isNull); | 254 test('read global field', () { |
| 216 }); | 255 expect(context['x'], equals(42)); |
| 217 | 256 expect(context['y'], isNull); |
| 218 test('read global field with underscore', () { | 257 }); |
| 219 expect(context['_x'], equals(123)); | 258 |
| 220 expect(context['y'], isNull); | 259 test('read global field with underscore', () { |
| 221 }); | 260 expect(context['_x'], equals(123)); |
| 222 | 261 expect(context['y'], isNull); |
| 223 test('hashCode and operator==(other)', () { | 262 }); |
| 224 final o1 = context['Object']; | 263 |
| 225 final o2 = context['Object']; | 264 test('write global field', () { |
| 226 expect(o1 == o2, isTrue); | 265 context['y'] = 42; |
| 227 expect(o1.hashCode == o2.hashCode, isTrue); | 266 expect(context['y'], equals(42)); |
| 228 final d = context['document']; | 267 }); |
| 229 expect(o1 == d, isFalse); | 268 |
| 230 }); | 269 }); |
| 231 | 270 |
| 232 test('js instantiation : new Foo()', () { | 271 group('new JsObject()', () { |
| 233 final Foo2 = context['container']['Foo']; | 272 |
| 234 final foo = new JsObject(Foo2, [42]); | 273 test('new Foo()', () { |
| 235 expect(foo['a'], 42); | 274 var foo = new JsObject(context['Foo'], [42]); |
| 236 expect(Foo2['b'], 38); | 275 expect(foo['a'], equals(42)); |
| 237 }); | 276 expect(foo.callMethod('bar'), equals(42)); |
| 238 | 277 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); |
| 239 test('js instantiation : new Array()', () { | 278 }); |
| 240 final a = new JsObject(context['Array']); | 279 |
| 241 expect(a, isNotNull); | 280 test('new container.Foo()', () { |
| 242 expect(a['length'], equals(0)); | 281 final Foo2 = context['container']['Foo']; |
| 243 | 282 final foo = new JsObject(Foo2, [42]); |
| 244 a.callMethod('push', ["value 1"]); | 283 expect(foo['a'], 42); |
| 245 expect(a['length'], equals(1)); | 284 expect(Foo2['b'], 38); |
| 246 expect(a[0], equals("value 1")); | 285 }); |
| 247 | 286 |
| 248 a.callMethod('pop'); | 287 test('new Array()', () { |
| 249 expect(a['length'], equals(0)); | 288 final a = new JsObject(context['Array']); |
| 250 }); | 289 expect(a, isNotNull); |
| 251 | 290 expect(a['length'], equals(0)); |
| 252 test('js instantiation : new Date()', () { | 291 |
| 253 final a = new JsObject(context['Date']); | 292 a.callMethod('push', ["value 1"]); |
| 254 expect(a.callMethod('getTime'), isNotNull); | 293 expect(a['length'], equals(1)); |
| 255 }); | 294 expect(a[0], equals("value 1")); |
| 256 | 295 |
| 257 test('js instantiation : new Date(12345678)', () { | 296 a.callMethod('pop'); |
| 258 final a = new JsObject(context['Date'], [12345678]); | 297 expect(a['length'], equals(0)); |
| 259 expect(a.callMethod('getTime'), equals(12345678)); | 298 }); |
| 260 }); | 299 |
| 261 | 300 test('new Date()', () { |
| 262 test('js instantiation : new Date("December 17, 1995 03:24:00 GMT")', | 301 final a = new JsObject(context['Date']); |
| 263 () { | 302 expect(a.callMethod('getTime'), isNotNull); |
| 264 final a = new JsObject(context['Date'], | 303 }); |
| 265 ["December 17, 1995 03:24:00 GMT"]); | 304 |
| 266 expect(a.callMethod('getTime'), equals(819170640000)); | 305 test('new Date(12345678)', () { |
| 267 }); | 306 final a = new JsObject(context['Date'], [12345678]); |
| 268 | 307 expect(a.callMethod('getTime'), equals(12345678)); |
| 269 test('js instantiation : new Date(1995,11,17)', () { | 308 }); |
| 270 // Note: JS Date counts months from 0 while Dart counts from 1. | 309 |
| 271 final a = new JsObject(context['Date'], [1995, 11, 17]); | 310 test('new Date("December 17, 1995 03:24:00 GMT")', |
| 272 final b = new DateTime(1995, 12, 17); | 311 () { |
| 273 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); | 312 final a = new JsObject(context['Date'], |
| 274 }); | 313 ["December 17, 1995 03:24:00 GMT"]); |
| 275 | 314 expect(a.callMethod('getTime'), equals(819170640000)); |
| 276 test('js instantiation : new Date(1995,11,17,3,24,0)', () { | 315 }); |
| 277 // Note: JS Date counts months from 0 while Dart counts from 1. | 316 |
| 278 final a = new JsObject(context['Date'], | 317 test('new Date(1995,11,17)', () { |
| 279 [1995, 11, 17, 3, 24, 0]); | 318 // Note: JS Date counts months from 0 while Dart counts from 1. |
| 280 final b = new DateTime(1995, 12, 17, 3, 24, 0); | 319 final a = new JsObject(context['Date'], [1995, 11, 17]); |
| 281 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); | 320 final b = new DateTime(1995, 12, 17); |
| 282 }); | 321 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); |
| 283 | 322 }); |
| 284 test('js instantiation : new Object()', () { | 323 |
| 285 final a = new JsObject(context['Object']); | 324 test('new Date(1995,11,17,3,24,0)', () { |
| 286 expect(a, isNotNull); | 325 // Note: JS Date counts months from 0 while Dart counts from 1. |
| 287 | 326 final a = new JsObject(context['Date'], |
| 288 a['attr'] = "value"; | 327 [1995, 11, 17, 3, 24, 0]); |
| 289 expect(a['attr'], equals("value")); | 328 final b = new DateTime(1995, 12, 17, 3, 24, 0); |
| 290 }); | 329 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); |
| 291 | 330 }); |
| 292 test(r'js instantiation : new RegExp("^\w+$")', () { | 331 |
| 293 final a = new JsObject(context['RegExp'], [r'^\w+$']); | 332 test('new Object()', () { |
| 294 expect(a, isNotNull); | 333 final a = new JsObject(context['Object']); |
| 295 expect(a.callMethod('test', ['true']), isTrue); | 334 expect(a, isNotNull); |
| 296 expect(a.callMethod('test', [' false']), isFalse); | 335 |
| 297 }); | 336 a['attr'] = "value"; |
| 298 | 337 expect(a['attr'], equals("value")); |
| 299 test('js instantiation via map notation : new Array()', () { | 338 }); |
| 300 final a = new JsObject(context['Array']); | 339 |
| 301 expect(a, isNotNull); | 340 test(r'new RegExp("^\w+$")', () { |
| 302 expect(a['length'], equals(0)); | 341 final a = new JsObject(context['RegExp'], [r'^\w+$']); |
| 303 | 342 expect(a, isNotNull); |
| 304 a['push'].apply(a, ["value 1"]); | 343 expect(a.callMethod('test', ['true']), isTrue); |
| 305 expect(a['length'], equals(1)); | 344 expect(a.callMethod('test', [' false']), isFalse); |
| 306 expect(a[0], equals("value 1")); | 345 }); |
| 307 | 346 |
| 308 a['pop'].apply(a); | 347 test('js instantiation via map notation : new Array()', () { |
| 309 expect(a['length'], equals(0)); | 348 final a = new JsObject(context['Array']); |
| 310 }); | 349 expect(a, isNotNull); |
| 311 | 350 expect(a['length'], equals(0)); |
| 312 test('js instantiation via map notation : new Date()', () { | 351 |
| 313 final a = new JsObject(context['Date']); | 352 a['push'].apply(a, ["value 1"]); |
| 314 expect(a['getTime'].apply(a), isNotNull); | 353 expect(a['length'], equals(1)); |
| 315 }); | 354 expect(a[0], equals("value 1")); |
| 316 | 355 |
| 317 test('js instantiation : typed array', () { | 356 a['pop'].apply(a); |
| 318 if (Platform.supportsTypedData) { | 357 expect(a['length'], equals(0)); |
| 319 // Safari's ArrayBuffer is not a Function and so doesn't support bind | 358 }); |
| 320 // which JsObject's constructor relies on. | 359 |
| 321 // bug: https://bugs.webkit.org/show_bug.cgi?id=122976 | 360 test('js instantiation via map notation : new Date()', () { |
| 322 if (context['ArrayBuffer']['bind'] != null) { | 361 final a = new JsObject(context['Date']); |
| 323 final codeUnits = "test".codeUnits; | 362 expect(a['getTime'].apply(a), isNotNull); |
| 324 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); | 363 }); |
| 325 final bufView = new JsObject(context['Uint8Array'], [buf]); | 364 |
| 326 for (var i = 0; i < codeUnits.length; i++) { | 365 test('typed array', () { |
| 327 bufView[i] = codeUnits[i]; | 366 if (Platform.supportsTypedData) { |
| 367 // Safari's ArrayBuffer is not a Function and so doesn't support bind | |
| 368 // which JsObject's constructor relies on. | |
| 369 // bug: https://bugs.webkit.org/show_bug.cgi?id=122976 | |
| 370 if (context['ArrayBuffer']['bind'] != null) { | |
| 371 final codeUnits = "test".codeUnits; | |
| 372 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); | |
| 373 final bufView = new JsObject(context['Uint8Array'], [buf]); | |
| 374 for (var i = 0; i < codeUnits.length; i++) { | |
| 375 bufView[i] = codeUnits[i]; | |
| 376 } | |
| 328 } | 377 } |
| 329 } | 378 } |
| 330 } | 379 }); |
| 331 }); | 380 |
| 332 | 381 test('>10 parameters', () { |
| 333 test('js instantiation : >10 parameters', () { | 382 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]); | 383 for (var i = 1; i <= 11; i++) { |
| 335 for (var i = 1; i <= 11; i++) { | 384 expect(o["f$i"], i); |
| 336 expect(o["f$i"], i); | 385 } |
| 337 } | 386 expect(o['constructor'], same(context['Baz'])); |
| 338 expect(o['constructor'], same(context['Baz'])); | 387 }); |
| 339 }); | 388 }); |
| 340 | 389 |
| 341 test('write global field', () { | 390 group('JsFunction and callMethod', () { |
| 342 context['y'] = 42; | 391 |
| 343 expect(context['y'], equals(42)); | 392 test('JsFunction.apply on a function defined in JS', () { |
| 344 }); | 393 expect(context['razzle'].apply(context), equals(42)); |
| 345 | 394 }); |
| 346 test('get JS JsFunction', () { | 395 |
| 347 var razzle = context['razzle']; | 396 test('JsObject.callMethod on a function defined in JS', () { |
| 348 expect(razzle.apply(context), equals(42)); | 397 expect(context.callMethod('razzle'), equals(42)); |
| 349 }); | 398 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); |
| 350 | 399 }); |
| 351 test('call JS function', () { | 400 |
| 352 expect(context.callMethod('razzle'), equals(42)); | 401 // all this tests is that null doesn't have the method apply? |
| 353 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); | 402 skip_test('call JS function via map notation', () { |
| 354 }); | 403 expect(() => context['dazzle'].apply(context), |
| 355 | 404 throwsA(isNoSuchMethodError)); |
| 356 test('call JS function via map notation', () { | 405 }); |
| 357 expect(context['razzle'].apply(context), equals(42)); | 406 |
| 358 expect(() => context['dazzle'].apply(context), | 407 test('callMethod with many arguments', () { |
| 359 throwsA(isNoSuchMethodError)); | 408 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), |
| 360 }); | 409 equals(55)); |
| 361 | 410 }); |
| 362 test('call JS function with varargs', () { | 411 |
| 363 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), | 412 test('access a property of a function', () { |
| 364 equals(55)); | 413 expect(context.callMethod('Bar'), "ret_value"); |
| 365 }); | 414 expect(context['Bar']['foo'], "property_value"); |
| 366 | 415 }); |
| 367 test('allocate JS object', () { | 416 |
| 368 var foo = new JsObject(context['Foo'], [42]); | 417 test('callMethod throws if name is not a String or num', () { |
| 369 expect(foo['a'], equals(42)); | 418 expect(() => context.callMethod(true), |
| 370 expect(foo.callMethod('bar'), equals(42)); | 419 throwsA(new isInstanceOf<ArgumentError>())); |
| 371 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); | 420 }); |
| 372 }); | 421 |
| 373 | 422 }); |
| 374 test('call toString()', () { | 423 |
| 375 var foo = new JsObject(context['Foo'], [42]); | 424 group('JsObject.fromBrowserObject()', () { |
| 376 expect(foo.toString(), equals("I'm a Foo a=42")); | 425 |
| 377 var container = context['container']; | 426 test('Nodes are proxied', () { |
| 378 expect(container.toString(), equals("[object Object]")); | 427 var node = new JsObject.fromBrowserObject(new DivElement()); |
|
vsm
2013/10/18 21:34:16
Test that you can get to a JS expando?
justinfagnani
2013/10/18 22:38:41
Done.
| |
| 379 }); | 428 expect(node is JsObject, isTrue); |
| 380 | 429 expect(node.instanceof(context['HTMLDivElement']), isTrue); |
| 381 test('allocate simple JS array', () { | 430 }); |
| 382 final list = [1, 2, 3, 4, 5, 6, 7, 8]; | 431 |
| 383 var array = jsify(list); | 432 test('primitives and null throw ArgumentError', () { |
| 384 expect(context.callMethod('isArray', [array]), isTrue); | 433 for (var v in ['a', 1, 2.0, true, null]) { |
| 385 expect(array['length'], equals(list.length)); | 434 expect(() => new JsObject.fromBrowserObject(v), |
| 386 for (var i = 0; i < list.length ; i++) { | 435 throwsA(new isInstanceOf<ArgumentError>())); |
| 387 expect(array[i], equals(list[i])); | 436 } |
| 388 } | 437 }); |
| 389 }); | 438 |
| 390 | 439 }); |
| 391 test('allocate JS array with iterable', () { | 440 |
| 392 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); | 441 group('Dart callback', () { |
| 393 var array = jsify(set); | 442 test('invoke Dart callback from JS', () { |
| 394 expect(context.callMethod('isArray', [array]), isTrue); | 443 expect(() => context.callMethod('invokeCallback'), throws); |
| 395 expect(array['length'], equals(set.length)); | 444 |
| 396 for (var i = 0; i < array['length'] ; i++) { | 445 context['callback'] = () => 42; |
| 397 expect(set.contains(array[i]), isTrue); | 446 expect(context.callMethod('invokeCallback'), equals(42)); |
| 398 } | 447 |
| 399 }); | 448 context.deleteProperty('callback'); |
| 400 | 449 }); |
| 401 test('allocate simple JS map', () { | 450 |
| 402 var map = {'a': 1, 'b': 2, 'c': 3}; | 451 test('callback as parameter', () { |
| 403 var jsMap = jsify(map); | 452 expect(context.callMethod('getTypeOf', [context['razzle']]), |
| 404 expect(!context.callMethod('isArray', [jsMap]), isTrue); | 453 equals("function")); |
| 405 for (final key in map.keys) { | 454 }); |
| 406 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue); | 455 |
| 407 } | 456 test('invoke Dart callback from JS with this', () { |
| 408 }); | 457 // A JavaScript constructor function implemented in Dart which |
| 409 | 458 // uses 'this' |
| 410 test('allocate complex JS object', () { | 459 final constructor = new JsFunction.withThis(($this, arg1) { |
| 411 final object = | 460 var t = $this; |
| 412 { | 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()', () { | |
| 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 if (IdbFactory.supported) { |
| 475 expect(foo.instanceof(context['Foo']), isTrue); | 649 var node = context.callMethod('getNewIDBKeyRange'); |
| 476 expect(foo.instanceof(context['Object']), isTrue); | 650 expect(node is KeyRange, isTrue); |
| 477 expect(foo.instanceof(context['String']), isFalse); | 651 } |
| 478 }); | 652 }); |
| 479 | 653 |
| 480 test('test deleteProperty', () { | 654 test('ImageData', () { |
| 481 var object = jsify({}); | 655 var node = context.callMethod('getNewImageData'); |
| 482 object['a'] = 1; | 656 expect(node is ImageData, isTrue); |
| 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 test('typed data: Int32Array', () { |
| 486 expect(context['Object'].callMethod('keys', [object])['length'], 0); | 660 var list = context.callMethod('getNewInt32Array'); |
| 487 }); | 661 print(list); |
| 488 | 662 expect(list is Int32List, isTrue); |
|
vsm
2013/10/18 21:34:16
Consider tests for indexed access and write:
- lis
Jacob
2013/10/18 22:03:43
// should add an expando
list[1000000000000] = 1
x
justinfagnani
2013/10/18 22:38:41
[2.5], [-1], and [34] all should throw because thi
justinfagnani
2013/10/18 22:38:41
they should throw, correct? should I use fromBrows
| |
| 489 test('test hasProperty', () { | 663 expect(list, orderedEquals([1, 2, 3, 4, 5, 6, 7, 8])); |
| 490 var object = jsify({}); | 664 }); |
| 491 object['a'] = 1; | 665 |
| 492 expect(object.hasProperty('a'), isTrue); | 666 }); |
| 493 expect(object.hasProperty('b'), isFalse); | 667 |
| 494 }); | 668 group('Dart->JS', () { |
| 495 | 669 |
| 496 test('test index get and set', () { | 670 test('Date', () { |
| 497 final myArray = context['myArray']; | 671 context['o'] = new DateTime(1995, 12, 17); |
| 498 expect(myArray['length'], equals(1)); | 672 var dateType = context['Date']; |
| 499 expect(myArray[0], equals("value1")); | 673 expect(context.callMethod('isPropertyInstanceOf', ['o', dateType]), |
| 500 myArray[0] = "value2"; | 674 isTrue); |
| 501 expect(myArray['length'], equals(1)); | 675 context.deleteProperty('o'); |
| 502 expect(myArray[0], equals("value2")); | 676 }); |
| 503 | 677 |
| 504 final foo = new JsObject(context['Foo'], [1]); | 678 test('window', () { |
| 505 foo["getAge"] = () => 10; | 679 context['o'] = window; |
| 506 expect(foo.callMethod('getAge'), equals(10)); | 680 var windowType = context['Window']; |
| 507 }); | 681 expect(context.callMethod('isPropertyInstanceOf', ['o', windowType]), |
| 508 | 682 isFalse); |
| 509 test('access a property of a function', () { | 683 context.deleteProperty('o'); |
| 510 expect(context.callMethod('Bar'), "ret_value"); | 684 }); |
| 511 expect(context['Bar']['foo'], "property_value"); | 685 |
| 512 }); | 686 test('document', () { |
| 513 | 687 context['o'] = document; |
| 514 test('usage of Serializable', () { | 688 var documentType = context['Document']; |
| 515 final red = Color.RED; | 689 expect(context.callMethod('isPropertyInstanceOf', ['o', documentType]), |
| 516 context['color'] = red; | 690 isTrue); |
| 517 expect(context['color'], equals(red._value)); | 691 context.deleteProperty('o'); |
| 692 }); | |
| 693 | |
| 694 test('Blob', () { | |
| 695 var fileParts = ['<a id="a"><b id="b">hey!</b></a>']; | |
| 696 context['o'] = new Blob(fileParts, 'text/html'); | |
| 697 var blobType = context['Blob']; | |
| 698 expect(context.callMethod('isPropertyInstanceOf', ['o', blobType]), | |
| 699 isTrue); | |
| 700 context.deleteProperty('o'); | |
| 701 }); | |
| 702 | |
| 703 test('unattached DivElement', () { | |
| 704 context['o'] = new DivElement(); | |
| 705 var divType = context['HTMLDivElement']; | |
| 706 expect(context.callMethod('isPropertyInstanceOf', ['o', divType]), | |
| 707 isTrue); | |
| 708 context.deleteProperty('o'); | |
| 709 }); | |
| 710 | |
| 711 test('KeyRange', () { | |
| 712 if (IdbFactory.supported) { | |
| 713 context['o'] = new KeyRange.only(1); | |
| 714 var keyRangeType = context['IDBKeyRange']; | |
| 715 expect(context.callMethod('isPropertyInstanceOf', ['o', keyRangeType]) , | |
| 716 isTrue); | |
| 717 context.deleteProperty('o'); | |
| 718 } | |
| 719 }); | |
| 720 | |
| 721 test('ImageData', () { | |
| 722 var canvas = new CanvasElement(); | |
| 723 var ctx = canvas.getContext('2d'); | |
| 724 context['o'] = ctx.createImageData(1, 1); | |
| 725 var imageDataType = context['ImageData']; | |
| 726 expect(context.callMethod('isPropertyInstanceOf', ['o', imageDataType]), | |
| 727 isTrue); | |
| 728 context.deleteProperty('o'); | |
| 729 }); | |
| 730 | |
| 731 test('typed data: Int32List', () { | |
| 732 context['o'] = new Int32List.fromList([1, 2, 3, 4]); | |
| 733 var listType = context['Int32Array']; | |
| 734 expect(context.callMethod('isPropertyInstanceOf', ['o', listType]), | |
| 735 isTrue); | |
| 736 context.deleteProperty('o'); | |
| 737 }); | |
| 738 | |
| 739 }); | |
| 518 }); | 740 }); |
| 519 } | 741 } |
| OLD | NEW |