Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library jsTest; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:html'; | |
| 9 import 'dart:js'; | |
| 10 | |
| 11 import '../../pkg/unittest/lib/unittest.dart'; | |
| 12 import '../../pkg/unittest/lib/html_config.dart'; | |
| 13 | |
| 14 _injectJs() { | |
| 15 final script = new ScriptElement(); | |
| 16 script.type = 'text/javascript'; | |
| 17 script.innerHtml = r""" | |
| 18 var x = 42; | |
| 19 | |
| 20 var _x = 123; | |
| 21 | |
| 22 var myArray = ["value1"]; | |
| 23 | |
| 24 var foreignDoc = (function(){ | |
| 25 var doc = document.implementation.createDocument("", "root", null); | |
| 26 var element = doc.createElement('element'); | |
| 27 element.setAttribute('id', 'abc'); | |
| 28 doc.documentElement.appendChild(element); | |
| 29 return doc; | |
| 30 })(); | |
| 31 | |
| 32 function razzle() { | |
| 33 return x; | |
| 34 } | |
| 35 | |
| 36 function getTypeOf(o) { | |
| 37 return typeof(o); | |
| 38 } | |
| 39 | |
| 40 function varArgs() { | |
| 41 var args = arguments; | |
| 42 var sum = 0; | |
| 43 for (var i = 0; i < args.length; ++i) { | |
| 44 sum += args[i]; | |
| 45 } | |
| 46 return sum; | |
| 47 } | |
| 48 | |
| 49 function Foo(a) { | |
| 50 this.a = a; | |
| 51 } | |
| 52 | |
| 53 Foo.b = 38; | |
| 54 | |
| 55 Foo.prototype.bar = function() { | |
| 56 return this.a; | |
| 57 } | |
| 58 Foo.prototype.toString = function() { | |
| 59 return "I'm a Foo a=" + this.a; | |
| 60 } | |
| 61 | |
| 62 var container = new Object(); | |
| 63 container.Foo = Foo; | |
| 64 | |
| 65 function isArray(a) { | |
| 66 return a instanceof Array; | |
| 67 } | |
| 68 | |
| 69 function checkMap(m, key, value) { | |
| 70 if (m.hasOwnProperty(key)) | |
| 71 return m[key] == value; | |
| 72 else | |
| 73 return false; | |
| 74 } | |
| 75 | |
| 76 function invokeCallback() { | |
| 77 return callback(); | |
| 78 } | |
| 79 | |
| 80 function invokeCallbackWith11params() { | |
| 81 return callbackWith11params(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); | |
| 82 } | |
| 83 | |
| 84 function returnElement(element) { | |
| 85 return element; | |
| 86 } | |
| 87 | |
| 88 function getElementAttribute(element, attr) { | |
| 89 return element.getAttribute(attr); | |
| 90 } | |
| 91 | |
| 92 function addClassAttributes(list) { | |
| 93 var result = ""; | |
| 94 for (var i=0; i<list.length; i++) { | |
| 95 result += list[i].getAttribute("class"); | |
| 96 } | |
| 97 return result; | |
| 98 } | |
| 99 | |
| 100 function getNewDivElement() { | |
| 101 return document.createElement("div"); | |
| 102 } | |
| 103 | |
| 104 function testJsMap(callback) { | |
| 105 var result = callback(); | |
| 106 return result['value']; | |
| 107 } | |
| 108 | |
| 109 function Bar() { | |
| 110 return "ret_value"; | |
| 111 } | |
| 112 Bar.foo = "property_value"; | |
| 113 | |
| 114 function Baz(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11) { | |
| 115 this.f1 = p1; | |
| 116 this.f2 = p2; | |
| 117 this.f3 = p3; | |
| 118 this.f4 = p4; | |
| 119 this.f5 = p5; | |
| 120 this.f6 = p6; | |
| 121 this.f7 = p7; | |
| 122 this.f8 = p8; | |
| 123 this.f9 = p9; | |
| 124 this.f10 = p10; | |
| 125 this.f11 = p11; | |
| 126 } | |
| 127 """; | |
| 128 document.body.append(script); | |
| 129 } | |
| 130 | |
| 131 class Foo implements Serializable<JsObject> { | |
| 132 final JsObject _proxy; | |
| 133 | |
| 134 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); | |
| 135 | |
| 136 JsObject toJs() => _proxy; | |
| 137 | |
| 138 num get a => _proxy['a']; | |
| 139 num bar() => _proxy.callMethod('bar'); | |
| 140 } | |
| 141 | |
| 142 class Color implements Serializable<String> { | |
| 143 static final RED = new Color._("red"); | |
| 144 static final BLUE = new Color._("blue"); | |
| 145 String _value; | |
| 146 Color._(this._value); | |
| 147 String toJs() => this._value; | |
| 148 } | |
| 149 | |
| 150 main() { | |
| 151 _injectJs(); | |
| 152 useHtmlConfiguration(); | |
| 153 | |
| 154 test('read global field', () { | |
| 155 expect(context['x'], equals(42)); | |
| 156 expect(context['y'], isNull); | |
| 157 }); | |
| 158 | |
| 159 test('read global field with underscore', () { | |
| 160 expect(context['_x'], equals(123)); | |
| 161 expect(context['y'], isNull); | |
| 162 }); | |
| 163 | |
| 164 test('hashCode and operator==(other)', () { | |
| 165 final o1 = context['Object']; | |
| 166 final o2 = context['Object']; | |
| 167 expect(o1 == o2, isTrue); | |
| 168 expect(o1.hashCode == o2.hashCode, isTrue); | |
| 169 final d = context['document']; | |
| 170 expect(o1 == d, isFalse); | |
| 171 expect(o1.hashCode == d.hashCode, isFalse); | |
|
vsm
2013/07/15 13:58:45
Note: this is breaking with your latest. It's not
alexandre.ardhuin
2013/07/15 14:53:58
Good catch ! Removed.
| |
| 172 }); | |
| 173 | |
| 174 test('js instantiation : new Foo()', () { | |
| 175 final Foo2 = context['container']['Foo']; | |
| 176 final foo = new JsObject(Foo2, [42]); | |
| 177 expect(foo['a'], 42); | |
| 178 expect(Foo2['b'], 38); | |
| 179 }); | |
| 180 | |
| 181 test('js instantiation : new Array()', () { | |
| 182 final a = new JsObject(context['Array']); | |
| 183 expect(a, isNotNull); | |
| 184 expect(a['length'], equals(0)); | |
| 185 | |
| 186 a.callMethod('push', ["value 1"]); | |
| 187 expect(a['length'], equals(1)); | |
| 188 expect(a[0], equals("value 1")); | |
| 189 | |
| 190 a.callMethod('pop'); | |
| 191 expect(a['length'], equals(0)); | |
| 192 }); | |
| 193 | |
| 194 test('js instantiation : new Date()', () { | |
| 195 final a = new JsObject(context['Date']); | |
| 196 expect(a.callMethod('getTime'), isNotNull); | |
| 197 }); | |
| 198 | |
| 199 test('js instantiation : new Date(12345678)', () { | |
| 200 final a = new JsObject(context['Date'], [12345678]); | |
| 201 expect(a.callMethod('getTime'), equals(12345678)); | |
| 202 }); | |
| 203 | |
| 204 test('js instantiation : new Date("December 17, 1995 03:24:00 GMT+01:00")', | |
| 205 () { | |
| 206 final a = new JsObject(context['Date'], | |
| 207 ["December 17, 1995 03:24:00 GMT+01:00"]); | |
| 208 expect(a.callMethod('getTime'), equals(819167040000)); | |
| 209 }); | |
| 210 | |
| 211 test('js instantiation : new Date(1995,11,17)', () { | |
| 212 // Note: JS Date counts months from 0 while Dart counts from 1. | |
| 213 final a = new JsObject(context['Date'], [1995, 11, 17]); | |
| 214 final b = new DateTime(1995, 12, 17); | |
| 215 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); | |
| 216 }); | |
| 217 | |
| 218 test('js instantiation : new Date(1995,11,17,3,24,0)', () { | |
| 219 // Note: JS Date counts months from 0 while Dart counts from 1. | |
| 220 final a = new JsObject(context['Date'], | |
| 221 [1995, 11, 17, 3, 24, 0]); | |
| 222 final b = new DateTime(1995, 12, 17, 3, 24, 0); | |
| 223 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); | |
| 224 }); | |
| 225 | |
| 226 test('js instantiation : new Object()', () { | |
| 227 final a = new JsObject(context['Object']); | |
| 228 expect(a, isNotNull); | |
| 229 | |
| 230 a['attr'] = "value"; | |
| 231 expect(a['attr'], equals("value")); | |
| 232 }); | |
| 233 | |
| 234 test(r'js instantiation : new RegExp("^\w+$")', () { | |
| 235 final a = new JsObject(context['RegExp'], [r'^\w+$']); | |
| 236 expect(a, isNotNull); | |
| 237 expect(a.callMethod('test', ['true']), isTrue); | |
| 238 expect(a.callMethod('test', [' false']), isFalse); | |
| 239 }); | |
| 240 | |
| 241 test('js instantiation via map notation : new Array()', () { | |
| 242 final a = new JsObject(context['Array']); | |
| 243 expect(a, isNotNull); | |
| 244 expect(a['length'], equals(0)); | |
| 245 | |
| 246 a['push'].apply(a, ["value 1"]); | |
| 247 expect(a['length'], equals(1)); | |
| 248 expect(a[0], equals("value 1")); | |
| 249 | |
| 250 a['pop'].apply(a); | |
| 251 expect(a['length'], equals(0)); | |
| 252 }); | |
| 253 | |
| 254 test('js instantiation via map notation : new Date()', () { | |
| 255 final a = new JsObject(context['Date']); | |
| 256 expect(a['getTime'].apply(a), isNotNull); | |
| 257 }); | |
| 258 | |
| 259 test('js instantiation : typed array', () { | |
| 260 final codeUnits = "test".codeUnits; | |
| 261 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); | |
| 262 final bufView = new JsObject(context['Uint8Array'], [buf]); | |
| 263 for (var i = 0; i < codeUnits.length; i++) { | |
| 264 bufView[i] = codeUnits[i]; | |
| 265 } | |
| 266 }); | |
| 267 | |
| 268 test('js instantiation : >10 parameters', () { | |
| 269 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]); | |
| 270 for (var i = 1; i <= 11; i++) { | |
| 271 o["f$i"] = i; | |
| 272 } | |
| 273 }); | |
| 274 | |
| 275 test('write global field', () { | |
| 276 context['y'] = 42; | |
| 277 expect(context['y'], equals(42)); | |
| 278 }); | |
| 279 | |
| 280 test('get JS JsFunction', () { | |
| 281 var razzle = context['razzle']; | |
| 282 expect(razzle.apply(context), equals(42)); | |
| 283 }); | |
| 284 | |
| 285 test('call JS function', () { | |
| 286 expect(context.callMethod('razzle'), equals(42)); | |
| 287 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); | |
| 288 }); | |
| 289 | |
| 290 test('call JS function via map notation', () { | |
| 291 expect(context['razzle'].apply(context), equals(42)); | |
| 292 expect(() => context['dazzle'].apply(context), | |
| 293 throwsA(isNoSuchMethodError)); | |
| 294 }); | |
| 295 | |
| 296 test('call JS function with varargs', () { | |
| 297 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), | |
| 298 equals(55)); | |
| 299 }); | |
| 300 | |
| 301 test('allocate JS object', () { | |
| 302 var foo = new JsObject(context['Foo'], [42]); | |
| 303 expect(foo['a'], equals(42)); | |
| 304 expect(foo.callMethod('bar'), equals(42)); | |
| 305 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); | |
| 306 }); | |
| 307 | |
| 308 test('call toString()', () { | |
| 309 var foo = new JsObject(context['Foo'], [42]); | |
| 310 expect(foo.toString(), equals("I'm a Foo a=42")); | |
| 311 var container = context['container']; | |
| 312 expect(container.toString(), equals("[object Object]")); | |
| 313 }); | |
| 314 | |
| 315 test('allocate simple JS array', () { | |
| 316 final list = [1, 2, 3, 4, 5, 6, 7, 8]; | |
| 317 var array = jsify(list); | |
| 318 expect(context.callMethod('isArray', [array]), isTrue); | |
| 319 expect(array['length'], equals(list.length)); | |
| 320 for (var i = 0; i < list.length ; i++) { | |
| 321 expect(array[i], equals(list[i])); | |
| 322 } | |
| 323 }); | |
| 324 | |
| 325 test('allocate JS array with iterable', () { | |
| 326 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); | |
| 327 var array = jsify(set); | |
| 328 expect(context.callMethod('isArray', [array]), isTrue); | |
| 329 expect(array['length'], equals(set.length)); | |
| 330 for (var i = 0; i < array['length'] ; i++) { | |
| 331 expect(set.contains(array[i]), isTrue); | |
| 332 } | |
| 333 }); | |
| 334 | |
| 335 test('allocate simple JS map', () { | |
| 336 var map = {'a': 1, 'b': 2, 'c': 3}; | |
| 337 var jsMap = jsify(map); | |
| 338 expect(!context.callMethod('isArray', [jsMap]), isTrue); | |
| 339 for (final key in map.keys) { | |
| 340 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue); | |
| 341 } | |
| 342 }); | |
| 343 | |
| 344 test('allocate complex JS object', () { | |
| 345 final object = | |
| 346 { | |
| 347 'a': [1, [2, 3]], | |
| 348 'b': { | |
| 349 'c': 3, | |
| 350 'd': new JsObject(context['Foo'], [42]) | |
| 351 }, | |
| 352 'e': null | |
| 353 }; | |
| 354 var jsObject = jsify(object); | |
| 355 expect(jsObject['a'][0], equals(object['a'][0])); | |
| 356 expect(jsObject['a'][1][0], equals(object['a'][1][0])); | |
| 357 expect(jsObject['a'][1][1], equals(object['a'][1][1])); | |
| 358 expect(jsObject['b']['c'], equals(object['b']['c'])); | |
| 359 expect(jsObject['b']['d'], equals(object['b']['d'])); | |
| 360 expect(jsObject['b']['d'].callMethod('bar'), equals(42)); | |
| 361 expect(jsObject['e'], isNull); | |
| 362 }); | |
| 363 | |
| 364 test('invoke Dart callback from JS', () { | |
| 365 expect(() => context.callMethod('invokeCallback'), throws); | |
| 366 | |
| 367 context['callback'] = new Callback(() => 42); | |
| 368 expect(context.callMethod('invokeCallback'), equals(42)); | |
| 369 | |
| 370 context.deleteProperty('callback'); | |
| 371 expect(() => context.callMethod('invokeCallback'), throws); | |
| 372 | |
| 373 context['callback'] = () => 42; | |
| 374 expect(context.callMethod('invokeCallback'), equals(42)); | |
| 375 | |
| 376 context.deleteProperty('callback'); | |
| 377 }); | |
| 378 | |
| 379 test('callback as parameter', () { | |
| 380 expect(context.callMethod('getTypeOf', [context['razzle']]), | |
| 381 equals("function")); | |
| 382 }); | |
| 383 | |
| 384 test('invoke Dart callback from JS with this', () { | |
| 385 final constructor = new Callback.withThis(($this, arg1) { | |
| 386 $this['a'] = 42; | |
| 387 $this['b'] = jsify(["a", arg1]); | |
| 388 }); | |
| 389 var o = new JsObject(constructor, ["b"]); | |
| 390 expect(o['a'], equals(42)); | |
| 391 expect(o['b'][0], equals("a")); | |
| 392 expect(o['b'][1], equals("b")); | |
| 393 }); | |
| 394 | |
| 395 test('invoke Dart callback from JS with 11 parameters', () { | |
| 396 context['callbackWith11params'] = new Callback((p1, p2, p3, p4, p5, p6, p7, | |
| 397 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'); | |
| 398 expect(context.callMethod('invokeCallbackWith11params'), | |
| 399 equals('1234567891011')); | |
| 400 }); | |
| 401 | |
| 402 test('return a JS proxy to JavaScript', () { | |
| 403 var result = context.callMethod('testJsMap', [() => jsify({'value': 42})]); | |
| 404 expect(result, 42); | |
| 405 }); | |
| 406 | |
| 407 test('test proxy equality', () { | |
| 408 var foo1 = new JsObject(context['Foo'], [1]); | |
| 409 var foo2 = new JsObject(context['Foo'], [2]); | |
| 410 context['foo'] = foo1; | |
| 411 context['foo'] = foo2; | |
| 412 expect(foo1, isNot(equals(context['foo']))); | |
| 413 expect(foo2, equals(context['foo'])); | |
| 414 }); | |
| 415 | |
| 416 test('test instanceof', () { | |
| 417 var foo = new JsObject(context['Foo'], [1]); | |
| 418 expect(foo.instanceof(context['Foo']), isTrue); | |
| 419 expect(foo.instanceof(context['Object']), isTrue); | |
| 420 expect(foo.instanceof(context['String']), isFalse); | |
| 421 }); | |
| 422 | |
| 423 test('test deleteProperty', () { | |
| 424 var object = jsify({}); | |
| 425 object['a'] = 1; | |
| 426 expect(context['Object'].callMethod('keys', [object])['length'], 1); | |
| 427 expect(context['Object'].callMethod('keys', [object])[0], "a"); | |
| 428 object.deleteProperty("a"); | |
| 429 expect(context['Object'].callMethod('keys', [object])['length'], 0); | |
| 430 }); | |
| 431 | |
| 432 test('test hasProperty', () { | |
| 433 var object = jsify({}); | |
| 434 object['a'] = 1; | |
| 435 expect(object.hasProperty('a'), isTrue); | |
| 436 expect(object.hasProperty('b'), isFalse); | |
| 437 }); | |
| 438 | |
| 439 test('test index get and set', () { | |
| 440 final myArray = context['myArray']; | |
| 441 expect(myArray['length'], equals(1)); | |
| 442 expect(myArray[0], equals("value1")); | |
| 443 myArray[0] = "value2"; | |
| 444 expect(myArray['length'], equals(1)); | |
| 445 expect(myArray[0], equals("value2")); | |
| 446 | |
| 447 final foo = new JsObject(context['Foo'], [1]); | |
| 448 foo["getAge"] = () => 10; | |
| 449 expect(foo.callMethod('getAge'), equals(10)); | |
| 450 }); | |
| 451 | |
| 452 test('access a property of a function', () { | |
| 453 expect(context.callMethod('Bar'), "ret_value"); | |
| 454 expect(context['Bar']['foo'], "property_value"); | |
| 455 }); | |
| 456 | |
| 457 test('retrieve same dart Object', () { | |
| 458 final date = new DateTime.now(); | |
| 459 context['dartDate'] = date; | |
| 460 expect(context['dartDate'], equals(date)); | |
| 461 }); | |
| 462 | |
| 463 test('usage of Serializable', () { | |
| 464 final red = Color.RED; | |
| 465 context['color'] = red; | |
| 466 expect(context['color'], equals(red._value)); | |
| 467 }); | |
| 468 } | |
| OLD | NEW |