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 } | |
131 """; | 127 """; |
132 document.body.append(script); | 128 document.body.append(script); |
133 } | 129 } |
134 | 130 |
135 class Foo implements Serializable<JsObject> { | 131 class Foo implements Serializable<JsObject> { |
136 final JsObject _proxy; | 132 final JsObject _proxy; |
137 | 133 |
138 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); | 134 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); |
139 | 135 |
140 JsObject toJs() => _proxy; | 136 JsObject toJs() => _proxy; |
141 | 137 |
142 num get a => _proxy['a']; | 138 num get a => _proxy['a']; |
143 num bar() => _proxy.callMethod('bar'); | 139 num bar() => _proxy.callMethod('bar'); |
144 } | 140 } |
145 | 141 |
146 class Color implements Serializable<String> { | 142 class Color implements Serializable<String> { |
147 static final RED = new Color._("red"); | 143 static final RED = new Color._("red"); |
148 static final BLUE = new Color._("blue"); | 144 static final BLUE = new Color._("blue"); |
149 String _value; | 145 String _value; |
150 Color._(this._value); | 146 Color._(this._value); |
151 String toJs() => this._value; | 147 String toJs() => this._value; |
152 } | 148 } |
153 | 149 |
154 class TestDartObject {} | |
155 | |
156 main() { | 150 main() { |
157 _injectJs(); | 151 _injectJs(); |
158 useHtmlConfiguration(); | 152 useHtmlConfiguration(); |
159 | 153 |
160 group('identity', () { | 154 test('context instances should be identical', () { |
161 | 155 var c1 = context; |
162 test('context instances should be identical', () { | 156 var c2 = context; |
163 var c1 = context; | 157 expect(identical(c1, c2), isTrue); |
164 var c2 = context; | |
165 expect(identical(c1, c2), isTrue); | |
166 }); | |
167 | |
168 test('identical JS objects should have identical proxies', () { | |
169 var o1 = context['location']; | |
170 var o2 = context['location']; | |
171 expect(identical(o1, o2), isTrue); | |
172 }); | |
173 | |
174 test('identical JS functions should have identical proxies', () { | |
175 var f1 = context['Object']; | |
176 var f2 = context['Object']; | |
177 expect(identical(f1, f2), isTrue); | |
178 }); | |
179 | |
180 test('identical Dart objects should have identical proxies', () { | |
181 var o1 = new TestDartObject(); | |
182 expect(context.callMethod('identical', [o1, o1]), isTrue); | |
183 }); | |
184 | |
185 test('identical Dart functions should have identical proxies', () { | |
186 var f1 = () => print("I'm a Function!"); | |
187 expect(context.callMethod('identical', [f1, f1]), isTrue); | |
188 }); | |
189 | |
190 // TODO(justinfagnani): old tests duplicate checks above, remove | |
191 // on test next cleanup pass | |
192 test('test proxy equality', () { | |
193 var foo1 = new JsObject(context['Foo'], [1]); | |
194 var foo2 = new JsObject(context['Foo'], [2]); | |
195 context['foo1'] = foo1; | |
196 context['foo2'] = foo2; | |
197 expect(foo1, isNot(equals(context['foo2']))); | |
198 expect(foo2, same(context['foo2'])); | |
199 context.deleteProperty('foo1'); | |
200 context.deleteProperty('foo2'); | |
201 }); | |
202 | |
203 test('retrieve same dart Object', () { | |
204 final date = new DateTime.now(); | |
205 context['dartDate'] = date; | |
206 expect(context['dartDate'], same(date)); | |
207 context.deleteProperty('dartDate'); | |
208 }); | |
209 | |
210 }); | 158 }); |
211 | 159 |
212 test('read global field', () { | 160 test('read global field', () { |
213 expect(context['x'], equals(42)); | 161 expect(context['x'], equals(42)); |
214 expect(context['y'], isNull); | 162 expect(context['y'], isNull); |
215 }); | 163 }); |
216 | 164 |
217 test('read global field with underscore', () { | 165 test('read global field with underscore', () { |
218 expect(context['_x'], equals(123)); | 166 expect(context['_x'], equals(123)); |
219 expect(context['y'], isNull); | 167 expect(context['y'], isNull); |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'); | 404 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'); |
457 expect(context.callMethod('invokeCallbackWith11params'), | 405 expect(context.callMethod('invokeCallbackWith11params'), |
458 equals('1234567891011')); | 406 equals('1234567891011')); |
459 }); | 407 }); |
460 | 408 |
461 test('return a JS proxy to JavaScript', () { | 409 test('return a JS proxy to JavaScript', () { |
462 var result = context.callMethod('testJsMap', [() => jsify({'value': 42})]); | 410 var result = context.callMethod('testJsMap', [() => jsify({'value': 42})]); |
463 expect(result, 42); | 411 expect(result, 42); |
464 }); | 412 }); |
465 | 413 |
| 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 |
466 test('test instanceof', () { | 423 test('test instanceof', () { |
467 var foo = new JsObject(context['Foo'], [1]); | 424 var foo = new JsObject(context['Foo'], [1]); |
468 expect(foo.instanceof(context['Foo']), isTrue); | 425 expect(foo.instanceof(context['Foo']), isTrue); |
469 expect(foo.instanceof(context['Object']), isTrue); | 426 expect(foo.instanceof(context['Object']), isTrue); |
470 expect(foo.instanceof(context['String']), isFalse); | 427 expect(foo.instanceof(context['String']), isFalse); |
471 }); | 428 }); |
472 | 429 |
473 test('test deleteProperty', () { | 430 test('test deleteProperty', () { |
474 var object = jsify({}); | 431 var object = jsify({}); |
475 object['a'] = 1; | 432 object['a'] = 1; |
(...skipping 21 matching lines...) Expand all Loading... |
497 final foo = new JsObject(context['Foo'], [1]); | 454 final foo = new JsObject(context['Foo'], [1]); |
498 foo["getAge"] = () => 10; | 455 foo["getAge"] = () => 10; |
499 expect(foo.callMethod('getAge'), equals(10)); | 456 expect(foo.callMethod('getAge'), equals(10)); |
500 }); | 457 }); |
501 | 458 |
502 test('access a property of a function', () { | 459 test('access a property of a function', () { |
503 expect(context.callMethod('Bar'), "ret_value"); | 460 expect(context.callMethod('Bar'), "ret_value"); |
504 expect(context['Bar']['foo'], "property_value"); | 461 expect(context['Bar']['foo'], "property_value"); |
505 }); | 462 }); |
506 | 463 |
| 464 test('retrieve same dart Object', () { |
| 465 final date = new DateTime.now(); |
| 466 context['dartDate'] = date; |
| 467 expect(context['dartDate'], equals(date)); |
| 468 }); |
| 469 |
507 test('usage of Serializable', () { | 470 test('usage of Serializable', () { |
508 final red = Color.RED; | 471 final red = Color.RED; |
509 context['color'] = red; | 472 context['color'] = red; |
510 expect(context['color'], equals(red._value)); | 473 expect(context['color'], equals(red._value)); |
511 }); | 474 }); |
512 } | 475 } |
OLD | NEW |