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:js'; | 9 import 'dart:js'; |
10 | 10 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 this.f3 = p3; | 117 this.f3 = p3; |
118 this.f4 = p4; | 118 this.f4 = p4; |
119 this.f5 = p5; | 119 this.f5 = p5; |
120 this.f6 = p6; | 120 this.f6 = p6; |
121 this.f7 = p7; | 121 this.f7 = p7; |
122 this.f8 = p8; | 122 this.f8 = p8; |
123 this.f9 = p9; | 123 this.f9 = p9; |
124 this.f10 = p10; | 124 this.f10 = p10; |
125 this.f11 = p11; | 125 this.f11 = p11; |
126 } | 126 } |
| 127 |
| 128 function identical(o1, o2) { |
| 129 return o1 === o2; |
| 130 } |
127 """; | 131 """; |
128 document.body.append(script); | 132 document.body.append(script); |
129 } | 133 } |
130 | 134 |
131 class Foo implements Serializable<JsObject> { | 135 class Foo implements Serializable<JsObject> { |
132 final JsObject _proxy; | 136 final JsObject _proxy; |
133 | 137 |
134 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); | 138 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); |
135 | 139 |
136 JsObject toJs() => _proxy; | 140 JsObject toJs() => _proxy; |
137 | 141 |
138 num get a => _proxy['a']; | 142 num get a => _proxy['a']; |
139 num bar() => _proxy.callMethod('bar'); | 143 num bar() => _proxy.callMethod('bar'); |
140 } | 144 } |
141 | 145 |
142 class Color implements Serializable<String> { | 146 class Color implements Serializable<String> { |
143 static final RED = new Color._("red"); | 147 static final RED = new Color._("red"); |
144 static final BLUE = new Color._("blue"); | 148 static final BLUE = new Color._("blue"); |
145 String _value; | 149 String _value; |
146 Color._(this._value); | 150 Color._(this._value); |
147 String toJs() => this._value; | 151 String toJs() => this._value; |
148 } | 152 } |
149 | 153 |
| 154 class TestDartObject {} |
| 155 |
150 main() { | 156 main() { |
151 _injectJs(); | 157 _injectJs(); |
152 useHtmlConfiguration(); | 158 useHtmlConfiguration(); |
153 | 159 |
154 test('context instances should be identical', () { | 160 group('identity', () { |
155 var c1 = context; | 161 |
156 var c2 = context; | 162 test('context instances should be identical', () { |
157 expect(identical(c1, c2), isTrue); | 163 var c1 = context; |
| 164 var c2 = context; |
| 165 expect(identical(c1, c2), isTrue); |
| 166 }); |
| 167 |
| 168 test('identical JS objects should have identical proxies', () { |
| 169 var o1 = new JsObject(context['Foo'], [1]); |
| 170 context['f1'] = o1; |
| 171 var o2 = context['f1']; |
| 172 expect(identical(o1, o2), isTrue); |
| 173 }); |
| 174 |
| 175 test('identical JS functions should have identical proxies', () { |
| 176 var f1 = context['Object']; |
| 177 var f2 = context['Object']; |
| 178 expect(identical(f1, f2), isTrue); |
| 179 }); |
| 180 |
| 181 test('identical Dart objects should have identical proxies', () { |
| 182 var o1 = new TestDartObject(); |
| 183 expect(context.callMethod('identical', [o1, o1]), isTrue); |
| 184 }); |
| 185 |
| 186 test('identical Dart functions should have identical proxies', () { |
| 187 var f1 = () => print("I'm a Function!"); |
| 188 expect(context.callMethod('identical', [f1, f1]), isTrue); |
| 189 }); |
| 190 |
| 191 // TODO(justinfagnani): old tests duplicate checks above, remove |
| 192 // on test next cleanup pass |
| 193 test('test proxy equality', () { |
| 194 var foo1 = new JsObject(context['Foo'], [1]); |
| 195 var foo2 = new JsObject(context['Foo'], [2]); |
| 196 context['foo1'] = foo1; |
| 197 context['foo2'] = foo2; |
| 198 expect(foo1, isNot(equals(context['foo2']))); |
| 199 expect(foo2, same(context['foo2'])); |
| 200 context.deleteProperty('foo1'); |
| 201 context.deleteProperty('foo2'); |
| 202 }); |
| 203 |
| 204 test('retrieve same dart Object', () { |
| 205 final date = new DateTime.now(); |
| 206 context['dartDate'] = date; |
| 207 expect(context['dartDate'], same(date)); |
| 208 context.deleteProperty('dartDate'); |
| 209 }); |
| 210 |
158 }); | 211 }); |
159 | 212 |
160 test('read global field', () { | 213 test('read global field', () { |
161 expect(context['x'], equals(42)); | 214 expect(context['x'], equals(42)); |
162 expect(context['y'], isNull); | 215 expect(context['y'], isNull); |
163 }); | 216 }); |
164 | 217 |
165 test('read global field with underscore', () { | 218 test('read global field with underscore', () { |
166 expect(context['_x'], equals(123)); | 219 expect(context['_x'], equals(123)); |
167 expect(context['y'], isNull); | 220 expect(context['y'], isNull); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 final bufView = new JsObject(context['Uint8Array'], [buf]); | 321 final bufView = new JsObject(context['Uint8Array'], [buf]); |
269 for (var i = 0; i < codeUnits.length; i++) { | 322 for (var i = 0; i < codeUnits.length; i++) { |
270 bufView[i] = codeUnits[i]; | 323 bufView[i] = codeUnits[i]; |
271 } | 324 } |
272 } | 325 } |
273 }); | 326 }); |
274 | 327 |
275 test('js instantiation : >10 parameters', () { | 328 test('js instantiation : >10 parameters', () { |
276 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]); | 329 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]); |
277 for (var i = 1; i <= 11; i++) { | 330 for (var i = 1; i <= 11; i++) { |
278 o["f$i"] = i; | 331 expect(o["f$i"], i); |
279 } | 332 } |
| 333 expect(o['constructor'], same(context['Baz'])); |
280 }); | 334 }); |
281 | 335 |
282 test('write global field', () { | 336 test('write global field', () { |
283 context['y'] = 42; | 337 context['y'] = 42; |
284 expect(context['y'], equals(42)); | 338 expect(context['y'], equals(42)); |
285 }); | 339 }); |
286 | 340 |
287 test('get JS JsFunction', () { | 341 test('get JS JsFunction', () { |
288 var razzle = context['razzle']; | 342 var razzle = context['razzle']; |
289 expect(razzle.apply(context), equals(42)); | 343 expect(razzle.apply(context), equals(42)); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'); | 458 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'); |
405 expect(context.callMethod('invokeCallbackWith11params'), | 459 expect(context.callMethod('invokeCallbackWith11params'), |
406 equals('1234567891011')); | 460 equals('1234567891011')); |
407 }); | 461 }); |
408 | 462 |
409 test('return a JS proxy to JavaScript', () { | 463 test('return a JS proxy to JavaScript', () { |
410 var result = context.callMethod('testJsMap', [() => jsify({'value': 42})]); | 464 var result = context.callMethod('testJsMap', [() => jsify({'value': 42})]); |
411 expect(result, 42); | 465 expect(result, 42); |
412 }); | 466 }); |
413 | 467 |
414 test('test proxy equality', () { | |
415 var foo1 = new JsObject(context['Foo'], [1]); | |
416 var foo2 = new JsObject(context['Foo'], [2]); | |
417 context['foo'] = foo1; | |
418 context['foo'] = foo2; | |
419 expect(foo1, isNot(equals(context['foo']))); | |
420 expect(foo2, equals(context['foo'])); | |
421 }); | |
422 | |
423 test('test instanceof', () { | 468 test('test instanceof', () { |
424 var foo = new JsObject(context['Foo'], [1]); | 469 var foo = new JsObject(context['Foo'], [1]); |
425 expect(foo.instanceof(context['Foo']), isTrue); | 470 expect(foo.instanceof(context['Foo']), isTrue); |
426 expect(foo.instanceof(context['Object']), isTrue); | 471 expect(foo.instanceof(context['Object']), isTrue); |
427 expect(foo.instanceof(context['String']), isFalse); | 472 expect(foo.instanceof(context['String']), isFalse); |
428 }); | 473 }); |
429 | 474 |
430 test('test deleteProperty', () { | 475 test('test deleteProperty', () { |
431 var object = jsify({}); | 476 var object = jsify({}); |
432 object['a'] = 1; | 477 object['a'] = 1; |
(...skipping 21 matching lines...) Expand all Loading... |
454 final foo = new JsObject(context['Foo'], [1]); | 499 final foo = new JsObject(context['Foo'], [1]); |
455 foo["getAge"] = () => 10; | 500 foo["getAge"] = () => 10; |
456 expect(foo.callMethod('getAge'), equals(10)); | 501 expect(foo.callMethod('getAge'), equals(10)); |
457 }); | 502 }); |
458 | 503 |
459 test('access a property of a function', () { | 504 test('access a property of a function', () { |
460 expect(context.callMethod('Bar'), "ret_value"); | 505 expect(context.callMethod('Bar'), "ret_value"); |
461 expect(context['Bar']['foo'], "property_value"); | 506 expect(context['Bar']['foo'], "property_value"); |
462 }); | 507 }); |
463 | 508 |
464 test('retrieve same dart Object', () { | |
465 final date = new DateTime.now(); | |
466 context['dartDate'] = date; | |
467 expect(context['dartDate'], equals(date)); | |
468 }); | |
469 | |
470 test('usage of Serializable', () { | 509 test('usage of Serializable', () { |
471 final red = Color.RED; | 510 final red = Color.RED; |
472 context['color'] = red; | 511 context['color'] = red; |
473 expect(context['color'], equals(red._value)); | 512 expect(context['color'], equals(red._value)); |
474 }); | 513 }); |
475 } | 514 } |
OLD | NEW |