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('js instantiation : new Foo()', () { |
| 165 final Foo2 = context['container']['Foo']; |
| 166 final foo = new JsObject(Foo2, [42]); |
| 167 expect(foo['a'], 42); |
| 168 expect(Foo2['b'], 38); |
| 169 }); |
| 170 |
| 171 test('js instantiation : new Array()', () { |
| 172 final a = new JsObject(context['Array']); |
| 173 expect(a, isNotNull); |
| 174 expect(a['length'], equals(0)); |
| 175 |
| 176 a.callMethod('push', ["value 1"]); |
| 177 expect(a['length'], equals(1)); |
| 178 expect(a[0], equals("value 1")); |
| 179 |
| 180 a.callMethod('pop'); |
| 181 expect(a['length'], equals(0)); |
| 182 }); |
| 183 |
| 184 test('js instantiation : new Date()', () { |
| 185 final a = new JsObject(context['Date']); |
| 186 expect(a.callMethod('getTime'), isNotNull); |
| 187 }); |
| 188 |
| 189 test('js instantiation : new Date(12345678)', () { |
| 190 final a = new JsObject(context['Date'], [12345678]); |
| 191 expect(a.callMethod('getTime'), equals(12345678)); |
| 192 }); |
| 193 |
| 194 test('js instantiation : new Date("December 17, 1995 03:24:00 GMT+01:00")', |
| 195 () { |
| 196 final a = new JsObject(context['Date'], |
| 197 ["December 17, 1995 03:24:00 GMT+01:00"]); |
| 198 expect(a.callMethod('getTime'), equals(819167040000)); |
| 199 }); |
| 200 |
| 201 test('js instantiation : new Date(1995,11,17)', () { |
| 202 // Note: JS Date counts months from 0 while Dart counts from 1. |
| 203 final a = new JsObject(context['Date'], [1995, 11, 17]); |
| 204 final b = new DateTime(1995, 12, 17); |
| 205 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); |
| 206 }); |
| 207 |
| 208 test('js instantiation : new Date(1995,11,17,3,24,0)', () { |
| 209 // Note: JS Date counts months from 0 while Dart counts from 1. |
| 210 final a = new JsObject(context['Date'], |
| 211 [1995, 11, 17, 3, 24, 0]); |
| 212 final b = new DateTime(1995, 12, 17, 3, 24, 0); |
| 213 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); |
| 214 }); |
| 215 |
| 216 test('js instantiation : new Object()', () { |
| 217 final a = new JsObject(context['Object']); |
| 218 expect(a, isNotNull); |
| 219 |
| 220 a['attr'] = "value"; |
| 221 expect(a['attr'], equals("value")); |
| 222 }); |
| 223 |
| 224 test(r'js instantiation : new RegExp("^\w+$")', () { |
| 225 final a = new JsObject(context['RegExp'], [r'^\w+$']); |
| 226 expect(a, isNotNull); |
| 227 expect(a.callMethod('test', ['true']), isTrue); |
| 228 expect(a.callMethod('test', [' false']), isFalse); |
| 229 }); |
| 230 |
| 231 test('js instantiation via map notation : new Array()', () { |
| 232 final a = new JsObject(context['Array']); |
| 233 expect(a, isNotNull); |
| 234 expect(a['length'], equals(0)); |
| 235 |
| 236 a['push'].apply(a, ["value 1"]); |
| 237 expect(a['length'], equals(1)); |
| 238 expect(a[0], equals("value 1")); |
| 239 |
| 240 a['pop'].apply(a); |
| 241 expect(a['length'], equals(0)); |
| 242 }); |
| 243 |
| 244 test('js instantiation via map notation : new Date()', () { |
| 245 final a = new JsObject(context['Date']); |
| 246 expect(a['getTime'].apply(a), isNotNull); |
| 247 }); |
| 248 |
| 249 test('js instantiation : typed array', () { |
| 250 final codeUnits = "test".codeUnits; |
| 251 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); |
| 252 final bufView = new JsObject(context['Uint8Array'], [buf]); |
| 253 for (var i = 0; i < codeUnits.length; i++) { |
| 254 bufView[i] = codeUnits[i]; |
| 255 } |
| 256 }); |
| 257 |
| 258 test('js instantiation : >10 parameters', () { |
| 259 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]); |
| 260 for (var i = 1; i <= 11; i++) { |
| 261 o["f$i"] = i; |
| 262 } |
| 263 }); |
| 264 |
| 265 test('write global field', () { |
| 266 context['y'] = 42; |
| 267 expect(context['y'], equals(42)); |
| 268 }); |
| 269 |
| 270 test('get JS JsFunction', () { |
| 271 var razzle = context['razzle']; |
| 272 expect(razzle.apply(context), equals(42)); |
| 273 }); |
| 274 |
| 275 test('call JS function', () { |
| 276 expect(context.callMethod('razzle'), equals(42)); |
| 277 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); |
| 278 }); |
| 279 |
| 280 test('call JS function via map notation', () { |
| 281 expect(context['razzle'].apply(context), equals(42)); |
| 282 expect(() => context['dazzle'].apply(context), |
| 283 throwsA(isNoSuchMethodError)); |
| 284 }); |
| 285 |
| 286 test('call JS function with varargs', () { |
| 287 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), |
| 288 equals(55)); |
| 289 }); |
| 290 |
| 291 test('allocate JS object', () { |
| 292 var foo = new JsObject(context['Foo'], [42]); |
| 293 expect(foo['a'], equals(42)); |
| 294 expect(foo.callMethod('bar'), equals(42)); |
| 295 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); |
| 296 }); |
| 297 |
| 298 test('call toString()', () { |
| 299 var foo = new JsObject(context['Foo'], [42]); |
| 300 expect(foo.toString(), equals("I'm a Foo a=42")); |
| 301 var container = context['container']; |
| 302 expect(container.toString(), equals("[object Object]")); |
| 303 }); |
| 304 |
| 305 test('allocate simple JS array', () { |
| 306 final list = [1, 2, 3, 4, 5, 6, 7, 8]; |
| 307 var array = jsify(list); |
| 308 expect(context.callMethod('isArray', [array]), isTrue); |
| 309 expect(array['length'], equals(list.length)); |
| 310 for (var i = 0; i < list.length ; i++) { |
| 311 expect(array[i], equals(list[i])); |
| 312 } |
| 313 }); |
| 314 |
| 315 test('allocate JS array with iterable', () { |
| 316 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); |
| 317 var array = jsify(set); |
| 318 expect(context.callMethod('isArray', [array]), isTrue); |
| 319 expect(array['length'], equals(set.length)); |
| 320 for (var i = 0; i < array['length'] ; i++) { |
| 321 expect(set.contains(array[i]), isTrue); |
| 322 } |
| 323 }); |
| 324 |
| 325 test('allocate simple JS map', () { |
| 326 var map = {'a': 1, 'b': 2, 'c': 3}; |
| 327 var jsMap = jsify(map); |
| 328 expect(!context.callMethod('isArray', [jsMap]), isTrue); |
| 329 for (final key in map.keys) { |
| 330 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue); |
| 331 } |
| 332 }); |
| 333 |
| 334 test('allocate complex JS object', () { |
| 335 final object = |
| 336 { |
| 337 'a': [1, [2, 3]], |
| 338 'b': { |
| 339 'c': 3, |
| 340 'd': new JsObject(context['Foo'], [42]) |
| 341 }, |
| 342 'e': null |
| 343 }; |
| 344 var jsObject = jsify(object); |
| 345 expect(jsObject['a'][0], equals(object['a'][0])); |
| 346 expect(jsObject['a'][1][0], equals(object['a'][1][0])); |
| 347 expect(jsObject['a'][1][1], equals(object['a'][1][1])); |
| 348 expect(jsObject['b']['c'], equals(object['b']['c'])); |
| 349 expect(jsObject['b']['d'], equals(object['b']['d'])); |
| 350 expect(jsObject['b']['d'].callMethod('bar'), equals(42)); |
| 351 expect(jsObject['e'], isNull); |
| 352 }); |
| 353 |
| 354 test('invoke Dart callback from JS', () { |
| 355 expect(() => context.callMethod('invokeCallback'), throws); |
| 356 |
| 357 context['callback'] = new Callback(() => 42); |
| 358 expect(context.callMethod('invokeCallback'), equals(42)); |
| 359 |
| 360 context.deleteProperty('callback'); |
| 361 expect(() => context.callMethod('invokeCallback'), throws); |
| 362 |
| 363 context['callback'] = () => 42; |
| 364 expect(context.callMethod('invokeCallback'), equals(42)); |
| 365 |
| 366 context.deleteProperty('callback'); |
| 367 }); |
| 368 |
| 369 test('callback as parameter', () { |
| 370 expect(context.callMethod('getTypeOf', [context['razzle']]), |
| 371 equals("function")); |
| 372 }); |
| 373 |
| 374 test('invoke Dart callback from JS with this', () { |
| 375 final constructor = new Callback.withThis(($this, arg1) { |
| 376 $this['a'] = 42; |
| 377 $this['b'] = jsify(["a", arg1]); |
| 378 }); |
| 379 var o = new JsObject(constructor, ["b"]); |
| 380 expect(o['a'], equals(42)); |
| 381 expect(o['b'][0], equals("a")); |
| 382 expect(o['b'][1], equals("b")); |
| 383 }); |
| 384 |
| 385 test('invoke Dart callback from JS with 11 parameters', () { |
| 386 context['callbackWith11params'] = new Callback((p1, p2, p3, p4, p5, p6, p7, |
| 387 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'); |
| 388 expect(context.callMethod('invokeCallbackWith11params'), |
| 389 equals('1234567891011')); |
| 390 }); |
| 391 |
| 392 test('return a JS proxy to JavaScript', () { |
| 393 var result = context.callMethod('testJsMap', [() => jsify({'value': 42})]); |
| 394 expect(result, 42); |
| 395 }); |
| 396 |
| 397 test('test proxy equality', () { |
| 398 var foo1 = new JsObject(context['Foo'], [1]); |
| 399 var foo2 = new JsObject(context['Foo'], [2]); |
| 400 context['foo'] = foo1; |
| 401 context['foo'] = foo2; |
| 402 expect(foo1, isNot(equals(context['foo']))); |
| 403 expect(foo2, equals(context['foo'])); |
| 404 }); |
| 405 |
| 406 test('test instanceof', () { |
| 407 var foo = new JsObject(context['Foo'], [1]); |
| 408 expect(foo.instanceof(context['Foo']), isTrue); |
| 409 expect(foo.instanceof(context['Object']), isTrue); |
| 410 expect(foo.instanceof(context['String']), isFalse); |
| 411 }); |
| 412 |
| 413 test('test deleteProperty', () { |
| 414 var object = jsify({}); |
| 415 object['a'] = 1; |
| 416 expect(context['Object'].callMethod('keys', [object])['length'], 1); |
| 417 expect(context['Object'].callMethod('keys', [object])[0], "a"); |
| 418 object.deleteProperty("a"); |
| 419 expect(context['Object'].callMethod('keys', [object])['length'], 0); |
| 420 }); |
| 421 |
| 422 test('test hasProperty', () { |
| 423 var object = jsify({}); |
| 424 object['a'] = 1; |
| 425 expect(object.hasProperty('a'), isTrue); |
| 426 expect(object.hasProperty('b'), isFalse); |
| 427 }); |
| 428 |
| 429 test('test index get and set', () { |
| 430 final myArray = context['myArray']; |
| 431 expect(myArray['length'], equals(1)); |
| 432 expect(myArray[0], equals("value1")); |
| 433 myArray[0] = "value2"; |
| 434 expect(myArray['length'], equals(1)); |
| 435 expect(myArray[0], equals("value2")); |
| 436 |
| 437 final foo = new JsObject(context['Foo'], [1]); |
| 438 foo["getAge"] = () => 10; |
| 439 expect(foo.callMethod('getAge'), equals(10)); |
| 440 }); |
| 441 |
| 442 test('access a property of a function', () { |
| 443 expect(context.callMethod('Bar'), "ret_value"); |
| 444 expect(context['Bar']['foo'], "property_value"); |
| 445 }); |
| 446 |
| 447 test('retrieve same dart Object', () { |
| 448 final date = new DateTime.now(); |
| 449 context['dartDate'] = date; |
| 450 expect(context['dartDate'], equals(date)); |
| 451 }); |
| 452 |
| 453 test('usage of Serializable', () { |
| 454 final red = Color.RED; |
| 455 context['color'] = red; |
| 456 expect(context['color'], equals(red._value)); |
| 457 }); |
| 458 } |
OLD | NEW |