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