OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
vsm
2013/06/11 16:17:44
2013 :-)
alexandre.ardhuin
2013/06/12 21:29:19
Done.
| |
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 js_tests; | |
vsm
2013/06/11 16:17:44
Can you rename this file to js_test.dart (removing
alexandre.ardhuin
2013/06/12 21:29:19
Done.
| |
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 class Foo implements Serializable<JsObject> { | |
15 final JsObject _proxy; | |
16 | |
17 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); | |
vsm
2013/06/11 16:17:44
The automated test framework needs the test self-c
alexandre.ardhuin
2013/06/12 21:29:19
Done.
| |
18 | |
19 JsObject toJs() => _proxy; | |
20 | |
21 num get a => _proxy['a']; | |
22 num bar() => _proxy.callMethod('bar'); | |
23 } | |
24 | |
25 class Color implements Serializable<String> { | |
26 static final RED = new Color._("red"); | |
27 static final BLUE = new Color._("blue"); | |
28 String _value; | |
29 Color._(this._value); | |
30 String toJs() => this._value; | |
31 } | |
32 | |
33 main() { | |
34 useHtmlConfiguration(); | |
35 | |
36 test('read global field', () { | |
37 expect(context['x'], equals(42)); | |
38 expect(context['y'], isNull); | |
39 }); | |
40 | |
41 test('read global field with underscore', () { | |
42 expect(context['_x'], equals(123)); | |
43 expect(context['y'], isNull); | |
44 }); | |
45 | |
46 test('js instantiation : new Foo()', () { | |
47 final Foo2 = context['container']['Foo']; | |
48 final foo = new JsObject(Foo2, [42]); | |
49 expect(foo['a'], 42); | |
50 expect(Foo2['b'], 38); | |
51 }); | |
52 | |
53 test('js instantiation : new Array()', () { | |
54 final a = new JsObject(context['Array']); | |
55 expect(a, isNotNull); | |
56 expect(a['length'], equals(0)); | |
57 | |
58 a.callMethod('push', ["value 1"]); | |
59 expect(a['length'], equals(1)); | |
60 expect(a[0], equals("value 1")); | |
61 | |
62 a.callMethod('pop'); | |
63 expect(a['length'], equals(0)); | |
64 }); | |
65 | |
66 test('js instantiation : new Date()', () { | |
67 final a = new JsObject(context['Date']); | |
68 expect(a.callMethod('getTime'), isNotNull); | |
69 }); | |
70 | |
71 test('js instantiation : new Date(12345678)', () { | |
72 final a = new JsObject(context['Date'], [12345678]); | |
73 expect(a.callMethod('getTime'), equals(12345678)); | |
74 }); | |
75 | |
76 test('js instantiation : new Date("December 17, 1995 03:24:00 GMT+01:00")', | |
77 () { | |
78 final a = new JsObject(context['Date'], | |
79 ["December 17, 1995 03:24:00 GMT+01:00"]); | |
80 expect(a.callMethod('getTime'), equals(819167040000)); | |
81 }); | |
82 | |
83 test('js instantiation : new Date(1995,11,17)', () { | |
84 // Note: JS Date counts months from 0 while Dart counts from 1. | |
85 final a = new JsObject(context['Date'], [1995, 11, 17]); | |
86 final b = new DateTime(1995, 12, 17); | |
87 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); | |
88 }); | |
89 | |
90 test('js instantiation : new Date(1995,11,17,3,24,0)', () { | |
91 // Note: JS Date counts months from 0 while Dart counts from 1. | |
92 final a = new JsObject(context['Date'], | |
93 [1995, 11, 17, 3, 24, 0]); | |
94 final b = new DateTime(1995, 12, 17, 3, 24, 0); | |
95 expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); | |
96 }); | |
97 | |
98 test('js instantiation : new Object()', () { | |
99 final a = new JsObject(context['Object']); | |
100 expect(a, isNotNull); | |
101 | |
102 a['attr'] = "value"; | |
103 expect(a['attr'], equals("value")); | |
104 }); | |
105 | |
106 test(r'js instantiation : new RegExp("^\w+$")', () { | |
107 final a = new JsObject(context['RegExp'], [r'^\w+$']); | |
108 expect(a, isNotNull); | |
109 expect(a.callMethod('test', ['true']), isTrue); | |
110 expect(a.callMethod('test', [' false']), isFalse); | |
111 }); | |
112 | |
113 test('js instantiation via map notation : new Array()', () { | |
114 final a = new JsObject(context['Array']); | |
115 expect(a, isNotNull); | |
116 expect(a['length'], equals(0)); | |
117 | |
118 a['push'].apply(a, ["value 1"]); | |
119 expect(a['length'], equals(1)); | |
120 expect(a[0], equals("value 1")); | |
121 | |
122 a['pop'].apply(a); | |
123 expect(a['length'], equals(0)); | |
124 }); | |
125 | |
126 test('js instantiation via map notation : new Date()', () { | |
127 final a = new JsObject(context['Date']); | |
128 expect(a['getTime'].apply(a), isNotNull); | |
129 }); | |
130 | |
131 test('js instantiation : typed array', () { | |
132 final codeUnits = "test".codeUnits; | |
133 final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); | |
134 final bufView = new JsObject(context['Uint8Array'], [buf]); | |
135 for (var i = 0; i < codeUnits.length; i++) { | |
136 bufView[i] = codeUnits[i]; | |
137 } | |
138 }); | |
139 | |
140 test('write global field', () { | |
141 context['y'] = 42; | |
142 expect(context['y'], equals(42)); | |
143 }); | |
144 | |
145 test('get JS JsFunction', () { | |
146 var razzle = context['razzle']; | |
147 expect(razzle.apply(context), equals(42)); | |
148 }); | |
149 | |
150 test('call JS function', () { | |
151 expect(context.callMethod('razzle'), equals(42)); | |
152 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); | |
153 }); | |
154 | |
155 test('call JS function via map notation', () { | |
156 expect(context['razzle'].apply(context), equals(42)); | |
157 expect(() => context['dazzle'].apply(context), | |
158 throwsA(isNoSuchMethodError)); | |
159 }); | |
160 | |
161 test('call JS function with varargs', () { | |
162 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), | |
163 equals(55)); | |
164 }); | |
165 | |
166 test('allocate JS object', () { | |
167 var foo = new JsObject(context['Foo'], [42]); | |
168 expect(foo['a'], equals(42)); | |
169 expect(foo.callMethod('bar'), equals(42)); | |
170 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); | |
171 }); | |
172 | |
173 test('call toString()', () { | |
174 var foo = new JsObject(context['Foo'], [42]); | |
175 expect(foo.toString(), equals("I'm a Foo a=42")); | |
176 var container = context['container']; | |
177 expect(container.toString(), equals("[object Object]")); | |
178 }); | |
179 | |
180 test('allocate simple JS array', () { | |
181 final list = [1, 2, 3, 4, 5, 6, 7, 8]; | |
182 var array = jsify(list); | |
183 expect(context.callMethod('isArray', [array]), isTrue); | |
184 expect(array['length'], equals(list.length)); | |
185 for (var i = 0; i < list.length ; i++) { | |
186 expect(array[i], equals(list[i])); | |
187 } | |
188 }); | |
189 | |
190 test('allocate JS array with iterable', () { | |
191 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); | |
192 var array = jsify(set); | |
193 expect(context.callMethod('isArray', [array]), isTrue); | |
194 expect(array['length'], equals(set.length)); | |
195 for (var i = 0; i < array['length'] ; i++) { | |
196 expect(set.contains(array[i]), isTrue); | |
197 } | |
198 }); | |
199 | |
200 test('allocate simple JS map', () { | |
201 var map = {'a': 1, 'b': 2, 'c': 3}; | |
202 var jsMap = jsify(map); | |
203 expect(!context.callMethod('isArray', [jsMap]), isTrue); | |
204 for (final key in map.keys) { | |
205 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue); | |
206 } | |
207 }); | |
208 | |
209 test('allocate complex JS object', () { | |
210 final object = | |
211 { | |
212 'a': [1, [2, 3]], | |
213 'b': { | |
214 'c': 3, | |
215 'd': new JsObject(context['Foo'], [42]) | |
216 }, | |
217 'e': null | |
218 }; | |
219 var jsObject = jsify(object); | |
220 expect(jsObject['a'][0], equals(object['a'][0])); | |
221 expect(jsObject['a'][1][0], equals(object['a'][1][0])); | |
222 expect(jsObject['a'][1][1], equals(object['a'][1][1])); | |
223 expect(jsObject['b']['c'], equals(object['b']['c'])); | |
224 expect(jsObject['b']['d'], equals(object['b']['d'])); | |
225 expect(jsObject['b']['d'].callMethod('bar'), equals(42)); | |
226 expect(jsObject['e'], isNull); | |
227 }); | |
228 | |
229 test('invoke Dart callback from JS', () { | |
230 expect(() => context.callMethod('invokeCallback'), throws); | |
231 | |
232 context['callback'] = new Callback.once(() => 42); | |
233 expect(context.callMethod('invokeCallback'), equals(42)); | |
234 }); | |
235 | |
236 test('callback as parameter', () { | |
237 expect(context.callMethod('getTypeOf', [context['razzle']]), | |
238 equals("function")); | |
239 }); | |
240 | |
241 test('invoke Dart callback from JS with this', () { | |
242 final constructor = new Callback.once(($this, arg1) { | |
243 $this['a'] = 42; | |
244 $this['b'] = jsify(["a", arg1]); | |
245 }, withThis: true); | |
246 var o = new JsObject(constructor, ["b"]); | |
247 expect(o['a'], equals(42)); | |
248 expect(o['b'][0], equals("a")); | |
249 expect(o['b'][1], equals("b")); | |
250 }); | |
251 | |
252 test('invoke Dart callback from JS with 11 parameters', () { | |
253 context['callbackWith11params'] = new Callback.once((p1, p2, p3, p4, | |
254 p5, p6, p7, p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10' | |
255 '$p11'); | |
256 expect(context.callMethod('invokeCallbackWith11params'), | |
257 equals('1234567891011')); | |
258 }); | |
259 | |
260 test('create a Dart callback outside a scope', () { | |
261 // Note, the test framework does not guarantee that each test runs as a | |
262 // separate event. This test creates a new asynchronous event and | |
263 // ensures that a callback can be created without a scope (i.e., that the | |
264 // scope is created on demand). | |
265 final subtest = () { | |
266 var callback = new Callback.once(() => 42); | |
267 context['callback'] = callback; | |
268 expect(context.callMethod('invokeCallback'), equals(42)); | |
269 }; | |
270 | |
271 runAsync(expectAsync0(subtest)); | |
272 }); | |
273 | |
274 test('return a JS proxy to JavaScript', () { | |
275 var result = context.callMethod('testJsMap', [ | |
276 new Callback.once(() => jsify({ 'value': 42 }))]); | |
277 expect(result, 42); | |
278 }); | |
279 | |
280 test('test proxy equality', () { | |
281 var foo1 = new JsObject(context['Foo'], [1]); | |
282 var foo2 = new JsObject(context['Foo'], [2]); | |
283 context['foo'] = foo1; | |
284 context['foo'] = foo2; | |
285 expect(foo1, isNot(equals(context['foo']))); | |
286 expect(foo2, equals(context['foo'])); | |
287 }); | |
288 | |
289 test('test instanceof', () { | |
290 var foo = new JsObject(context['Foo'], [1]); | |
291 expect(foo.instanceof(context['Foo']), isTrue); | |
292 expect(foo.instanceof(context['Object']), isTrue); | |
293 expect(foo.instanceof(context['String']), isFalse); | |
294 }); | |
295 | |
296 test('test deleteProperty', () { | |
297 var object = jsify({}); | |
298 object['a'] = 1; | |
299 expect(context['Object'].callMethod('keys', [object])['length'], 1); | |
300 expect(context['Object'].callMethod('keys', [object])[0], "a"); | |
301 object.deleteProperty("a"); | |
302 expect(context['Object'].callMethod('keys', [object])['length'], 0); | |
303 }); | |
304 | |
305 test('test hasProperty', () { | |
306 var object = jsify({}); | |
307 object['a'] = 1; | |
308 expect(object.hasProperty('a'), isTrue); | |
309 expect(object.hasProperty('b'), isFalse); | |
310 }); | |
311 | |
312 test('test index get and set', () { | |
313 final myArray = context['myArray']; | |
314 expect(myArray['length'], equals(1)); | |
315 expect(myArray[0], equals("value1")); | |
316 myArray[0] = "value2"; | |
317 expect(myArray['length'], equals(1)); | |
318 expect(myArray[0], equals("value2")); | |
319 | |
320 final foo = new JsObject(context['Foo'], [1]); | |
321 foo["getAge"] = new Callback.once(() => 10); | |
322 expect(foo.callMethod('getAge'), equals(10)); | |
323 }); | |
324 | |
325 test('access a property of a function', () { | |
326 expect(context.callMethod('Bar'), "ret_value"); | |
327 expect(context['Bar']['foo'], "property_value"); | |
328 }); | |
329 | |
330 test('retrieve same dart Object', () { | |
331 final date = new DateTime.now(); | |
332 context['dartDate'] = date; | |
333 expect(context['dartDate'], equals(date)); | |
334 }); | |
335 | |
336 test('usage of Serializable', () { | |
337 final red = Color.RED; | |
338 context['color'] = red; | |
339 expect(context['color'], equals(red._value)); | |
340 }); | |
341 | |
342 test('check for leaks', () { | |
343 // Verify that the number of live objects is zero. | |
344 final verifyNoLeaks = expectAsync0(() => expect(0, proxyCount())); | |
345 // Run this check asychnronously to ensure that any current scope is | |
346 // cleared first. | |
347 runAsync(verifyNoLeaks); | |
348 }); | |
349 } | |
OLD | NEW |