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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 } | 96 } |
97 return result; | 97 return result; |
98 } | 98 } |
99 | 99 |
100 function getNewDivElement() { | 100 function getNewDivElement() { |
101 return document.createElement("div"); | 101 return document.createElement("div"); |
102 } | 102 } |
103 | 103 |
104 function testJsMap(callback) { | 104 function testJsMap(callback) { |
105 var result = callback(); | 105 var result = callback(); |
106 console.log('result = ' + result); | |
alexandre.ardhuin
2013/10/11 13:52:53
To remove ?
justinfagnani
2013/10/11 18:47:09
Done.
| |
106 return result['value']; | 107 return result['value']; |
107 } | 108 } |
108 | 109 |
109 function Bar() { | 110 function Bar() { |
110 return "ret_value"; | 111 return "ret_value"; |
111 } | 112 } |
112 Bar.foo = "property_value"; | 113 Bar.foo = "property_value"; |
113 | 114 |
114 function Baz(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11) { | 115 function Baz(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11) { |
115 this.f1 = p1; | 116 this.f1 = p1; |
116 this.f2 = p2; | 117 this.f2 = p2; |
117 this.f3 = p3; | 118 this.f3 = p3; |
118 this.f4 = p4; | 119 this.f4 = p4; |
119 this.f5 = p5; | 120 this.f5 = p5; |
120 this.f6 = p6; | 121 this.f6 = p6; |
121 this.f7 = p7; | 122 this.f7 = p7; |
122 this.f8 = p8; | 123 this.f8 = p8; |
123 this.f9 = p9; | 124 this.f9 = p9; |
124 this.f10 = p10; | 125 this.f10 = p10; |
125 this.f11 = p11; | 126 this.f11 = p11; |
126 } | 127 } |
128 | |
129 function identical(o1, o2) { | |
130 return o1 === o2; | |
131 } | |
127 """; | 132 """; |
128 document.body.append(script); | 133 document.body.append(script); |
129 } | 134 } |
130 | 135 |
131 class Foo implements Serializable<JsObject> { | 136 class Foo implements Serializable<JsObject> { |
132 final JsObject _proxy; | 137 final JsObject _proxy; |
133 | 138 |
134 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); | 139 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); |
135 | 140 |
136 JsObject toJs() => _proxy; | 141 JsObject toJs() => _proxy; |
137 | 142 |
138 num get a => _proxy['a']; | 143 num get a => _proxy['a']; |
139 num bar() => _proxy.callMethod('bar'); | 144 num bar() => _proxy.callMethod('bar'); |
140 } | 145 } |
141 | 146 |
142 class Color implements Serializable<String> { | 147 class Color implements Serializable<String> { |
143 static final RED = new Color._("red"); | 148 static final RED = new Color._("red"); |
144 static final BLUE = new Color._("blue"); | 149 static final BLUE = new Color._("blue"); |
145 String _value; | 150 String _value; |
146 Color._(this._value); | 151 Color._(this._value); |
147 String toJs() => this._value; | 152 String toJs() => this._value; |
148 } | 153 } |
149 | 154 |
155 class TestDartObject {} | |
156 | |
150 main() { | 157 main() { |
151 _injectJs(); | 158 _injectJs(); |
152 useHtmlConfiguration(); | 159 useHtmlConfiguration(); |
153 | 160 |
154 test('context instances should be identical', () { | 161 group('identity', () { |
155 var c1 = context; | 162 |
156 var c2 = context; | 163 test('context instances should be identical', () { |
157 expect(identical(c1, c2), isTrue); | 164 var c1 = context; |
165 var c2 = context; | |
166 expect(identical(c1, c2), isTrue); | |
167 }); | |
168 | |
169 test('identical JS objects should have identical proxies', () { | |
170 var o1 = context['location']; | |
171 var o2 = context['location']; | |
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 236 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'); | 457 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'); |
405 expect(context.callMethod('invokeCallbackWith11params'), | 458 expect(context.callMethod('invokeCallbackWith11params'), |
406 equals('1234567891011')); | 459 equals('1234567891011')); |
407 }); | 460 }); |
408 | 461 |
409 test('return a JS proxy to JavaScript', () { | 462 test('return a JS proxy to JavaScript', () { |
410 var result = context.callMethod('testJsMap', [() => jsify({'value': 42})]); | 463 var result = context.callMethod('testJsMap', [() => jsify({'value': 42})]); |
411 expect(result, 42); | 464 expect(result, 42); |
412 }); | 465 }); |
413 | 466 |
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', () { | 467 test('test instanceof', () { |
424 var foo = new JsObject(context['Foo'], [1]); | 468 var foo = new JsObject(context['Foo'], [1]); |
425 expect(foo.instanceof(context['Foo']), isTrue); | 469 expect(foo.instanceof(context['Foo']), isTrue); |
426 expect(foo.instanceof(context['Object']), isTrue); | 470 expect(foo.instanceof(context['Object']), isTrue); |
427 expect(foo.instanceof(context['String']), isFalse); | 471 expect(foo.instanceof(context['String']), isFalse); |
428 }); | 472 }); |
429 | 473 |
430 test('test deleteProperty', () { | 474 test('test deleteProperty', () { |
431 var object = jsify({}); | 475 var object = jsify({}); |
432 object['a'] = 1; | 476 object['a'] = 1; |
(...skipping 21 matching lines...) Expand all Loading... | |
454 final foo = new JsObject(context['Foo'], [1]); | 498 final foo = new JsObject(context['Foo'], [1]); |
455 foo["getAge"] = () => 10; | 499 foo["getAge"] = () => 10; |
456 expect(foo.callMethod('getAge'), equals(10)); | 500 expect(foo.callMethod('getAge'), equals(10)); |
457 }); | 501 }); |
458 | 502 |
459 test('access a property of a function', () { | 503 test('access a property of a function', () { |
460 expect(context.callMethod('Bar'), "ret_value"); | 504 expect(context.callMethod('Bar'), "ret_value"); |
461 expect(context['Bar']['foo'], "property_value"); | 505 expect(context['Bar']['foo'], "property_value"); |
462 }); | 506 }); |
463 | 507 |
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', () { | 508 test('usage of Serializable', () { |
471 final red = Color.RED; | 509 final red = Color.RED; |
472 context['color'] = red; | 510 context['color'] = red; |
473 expect(context['color'], equals(red._value)); | 511 expect(context['color'], equals(red._value)); |
474 }); | 512 }); |
475 } | 513 } |
OLD | NEW |