Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Side by Side Diff: dart/third_party/pkg/js/test/js_wrapping/browser_tests.dart

Issue 57393002: Version 0.8.10.2 (Closed) Base URL: http://dart.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 library tests;
2
3 import 'dart:html';
4
5 import 'package:js/js.dart' as js;
6 import 'package:js/js_wrapping.dart' as jsw;
7 import 'package:unittest/unittest.dart';
8 import 'package:unittest/html_config.dart';
9
10 abstract class _Person {
11 String firstname;
12
13 String sayHello();
14 }
15 class PersonMP extends jsw.MagicProxy implements _Person {
16 PersonMP(String firstname, String lastname) :
17 super(js.context.Person, [firstname, lastname]);
18 PersonMP.fromProxy(js.Proxy proxy) : super.fromProxy(proxy);
19 }
20
21 class PersonTP extends jsw.TypedProxy {
22 static PersonTP cast(js.Proxy proxy) => proxy == null ? null :
23 new PersonTP.fromProxy(proxy);
24
25 PersonTP(String firstname, String lastname) :
26 super(js.context.Person, [firstname, lastname]);
27 PersonTP.fromProxy(js.Proxy proxy) : super.fromProxy(proxy);
28
29 set firstname(String firstname) => $unsafe.firstname = firstname;
30 String get firstname => $unsafe.firstname;
31
32 List<PersonTP> get children =>
33 jsw.JsArrayToListAdapter.castListOfSerializables($unsafe.children,
34 PersonTP.cast);
35 set father(PersonTP father) => $unsafe.father = father;
36 PersonTP get father => PersonTP.cast($unsafe.father);
37
38 String sayHello() => $unsafe.sayHello();
39 }
40
41 class Color implements js.Serializable<String> {
42 static final RED = new Color._("red");
43 static final BLUE = new Color._("blue");
44
45 final String _value;
46
47 Color._(this._value);
48
49 String toJs() => this._value;
50 operator ==(Color other) => this._value == other._value;
51 }
52
53 main() {
54 useHtmlConfiguration();
55
56 test('TypedProxy', () {
57 js.scoped(() {
58 final john = new PersonTP('John', 'Doe');
59 expect(john.firstname, equals('John'));
60 john.firstname = 'Joe';
61 expect(john.firstname, equals('Joe'));
62 });
63 });
64
65 test('MagicProxy', () {
66 js.scoped(() {
67 final john = new PersonMP('John', 'Doe');
68 expect(john.firstname, equals('John'));
69 expect(john['firstname'], equals('John'));
70 john.firstname = 'Joe';
71 expect(john.firstname, equals('Joe'));
72 expect(john['firstname'], equals('Joe'));
73 john['firstname'] = 'John';
74 expect(john.firstname, equals('John'));
75 expect(john['firstname'], equals('John'));
76 });
77 });
78
79 test('function call', () {
80 js.scoped(() {
81 final john = new PersonTP('John', 'Doe');
82 expect(john.sayHello(), equals("Hello, I'm John Doe"));
83 });
84 });
85
86 test('JsDateToDateTimeAdapter', () {
87 js.scoped(() {
88 final date = new DateTime.now();
89 final jsDate = new jsw.JsDateToDateTimeAdapter(date);
90 expect(jsDate.millisecondsSinceEpoch,
91 equals(date.millisecondsSinceEpoch));
92 jsDate.$unsafe.setFullYear(2000);
93 expect(jsDate.year, equals(2000));
94 });
95 });
96
97 group('JsArrayToListAdapter', () {
98 test('iterator', () {
99 js.scoped(() {
100 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
101 js.array(["e0", "e1", "e2"]));
102
103 final iterator = m.iterator;
104 iterator.moveNext();
105 expect(iterator.current, equals("e0"));
106 iterator.moveNext();
107 expect(iterator.current, equals("e1"));
108 iterator.moveNext();
109 expect(iterator.current, equals("e2"));
110 });
111 });
112 test('get length', () {
113 js.scoped(() {
114 final m1 = new jsw.JsArrayToListAdapter<String>.fromProxy(js.array([]));
115 expect(m1.length, equals(0));
116 final m2 = new jsw.JsArrayToListAdapter<String>.fromProxy(
117 js.array(["a", "b"]));
118 expect(m2.length, equals(2));
119 });
120 });
121 test('add', () {
122 js.scoped(() {
123 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(js.array([]));
124 expect(m.length, equals(0));
125 m.add("a");
126 expect(m.length, equals(1));
127 expect(m[0], equals("a"));
128 m.add("b");
129 expect(m.length, equals(2));
130 expect(m[0], equals("a"));
131 expect(m[1], equals("b"));
132 });
133 });
134 test('clear', () {
135 js.scoped(() {
136 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
137 js.array(["a", "b"]));
138 expect(m.length, equals(2));
139 m.clear();
140 expect(m.length, equals(0));
141 });
142 });
143 test('remove', () {
144 js.scoped(() {
145 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
146 js.array(["a", "b"]));
147 expect(m.length, equals(2));
148 m.remove("a");
149 expect(m.length, equals(1));
150 expect(m[0], equals("b"));
151 });
152 });
153 test('operator []', () {
154 js.scoped(() {
155 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
156 js.array(["a", "b"]));
157 expect(() => m[-1], throwsA(isRangeError));
158 expect(() => m[2], throwsA(isRangeError));
159 expect(m[0], equals("a"));
160 expect(m[1], equals("b"));
161 });
162 });
163 test('operator []=', () {
164 js.scoped(() {
165 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
166 js.array(["a", "b"]));
167 expect(() => m[-1] = "c", throwsA(isRangeError));
168 expect(() => m[2] = "c", throwsA(isRangeError));
169 m[0] = "d";
170 m[1] = "e";
171 expect(m[0], equals("d"));
172 expect(m[1], equals("e"));
173 });
174 });
175 test('set length', () {
176 js.scoped(() {
177 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
178 js.array(["a", "b"]));
179 m.length = 10;
180 expect(m.length, equals(10));
181 expect(m[5], equals(null));
182 m.length = 1;
183 expect(m.length, equals(1));
184 expect(m[0], equals("a"));
185 });
186 });
187 test('sort', () {
188 js.scoped(() {
189 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
190 js.array(["c", "a", "b"]));
191 m.sort();
192 expect(m.length, equals(3));
193 expect(m[0], equals("a"));
194 expect(m[1], equals("b"));
195 expect(m[2], equals("c"));
196 });
197 });
198 test('insert', () {
199 js.scoped(() {
200 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
201 js.array(["a", "b", "c"]));
202 m.insert(1, "d");
203 expect(m.length, equals(4));
204 expect(m[0], equals("a"));
205 expect(m[1], equals("d"));
206 expect(m[2], equals("b"));
207 expect(m[3], equals("c"));
208 });
209 });
210 test('removeAt', () {
211 js.scoped(() {
212 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
213 js.array(["a", "b", "c"]));
214 expect(m.removeAt(1), equals("b"));
215 expect(m.length, equals(2));
216 expect(m[0], equals("a"));
217 expect(m[1], equals("c"));
218 expect(() => m.removeAt(2), throwsA(isRangeError));
219 });
220 });
221 test('removeLast', () {
222 js.scoped(() {
223 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
224 js.array(["a", "b", "c", null]));
225 expect(m.removeLast(), isNull);
226 expect(m.removeLast(), equals("c"));
227 expect(m.removeLast(), equals("b"));
228 expect(m.removeLast(), equals("a"));
229 expect(m.length, equals(0));
230 });
231 });
232 test('sublist', () {
233 js.scoped(() {
234 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
235 js.array(["a", "b", "c", null]));
236 final sl1 = m.sublist(2);
237 expect(sl1.length, equals(2));
238 expect(sl1[0], equals("c"));
239 expect(sl1[1], isNull);
240 final sl2 = m.sublist(1, 3);
241 expect(sl2.length, equals(2));
242 expect(sl2[0], equals("b"));
243 expect(sl2[1], equals("c"));
244 });
245 });
246 test('setRange', () {
247 js.scoped(() {
248 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
249 js.array(["a", "b", "c", null]));
250 m.setRange(1, 2, [null, null]);
251 expect(m.length, equals(4));
252 expect(m[0], equals("a"));
253 expect(m[1], isNull);
254 expect(m[2], isNull);
255 expect(m[3], isNull);
256 m.setRange(3, 1, [null, "c", null], 1);
257 expect(m[0], equals("a"));
258 expect(m[1], isNull);
259 expect(m[2], isNull);
260 expect(m[3], equals("c"));
261 });
262 });
263 test('removeRange', () {
264 js.scoped(() {
265 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(
266 js.array(["a", "b", "c", null]));
267 m.removeRange(1, 3);
268 expect(m.length, equals(2));
269 expect(m[0], equals("a"));
270 expect(m[1], isNull);
271 });
272 });
273
274 test('bidirectionnal serialization of Proxy', () {
275 js.scoped(() {
276 js.context.myArray = js.array([]);
277 final myList = new jsw.JsArrayToListAdapter<PersonTP>.fromProxy(
278 js.context.myArray, new jsw.TranslatorForSerializable<PersonTP>(
279 (p) => new PersonTP.fromProxy(p)));
280
281 myList.add(new PersonTP('John', 'Doe'));
282 myList.add(null);
283 expect(myList[0].firstname, 'John');
284 expect(myList[1], isNull);
285 });
286 });
287
288 test('family', () {
289 js.scoped(() {
290 final father = new PersonTP("John", "Doe");
291 final child1 = new PersonTP("Lewis", "Doe")
292 ..father = father;
293 final child2 = new PersonTP("Andy", "Doe")
294 ..father = father;
295 father.children.addAll([child1, child2]);
296 expect(father.children.length, 2);
297 expect(father.children.map((p) => p.firstname).join(","), "Lewis,Andy");
298 expect(child1.father.firstname, "John");
299 });
300 });
301
302 test('bidirectionnal serialization of Serializable', () {
303 js.scoped(() {
304 js.context.myArray = js.array([]);
305 final myList = new jsw.JsArrayToListAdapter<Color>.fromProxy(
306 js.context.myArray,
307 new jsw.Translator<Color>(
308 (e) => e != null ? new Color._(e) : null,
309 (e) => e != null ? e.toJs() : null
310 )
311 );
312
313 myList.add(Color.BLUE);
314 myList.add(null);
315 expect(myList[0], Color.BLUE);
316 expect(myList[1], isNull);
317 });
318 });
319 });
320
321 group('JsObjectToMapAdapter', () {
322 test('get keys', () {
323 js.scoped(() {
324 final m = new jsw.JsObjectToMapAdapter<int>.fromProxy(
325 js.map({"a": 1, "b": 2}));
326 final keys = m.keys;
327 expect(keys.length, equals(2));
328 expect(keys, contains("a"));
329 expect(keys, contains("b"));
330 });
331 });
332 test('containsKey', () {
333 js.scoped(() {
334 final m = new jsw.JsObjectToMapAdapter.fromProxy(
335 js.map({"a": 1, "b": "c"}));
336 expect(m.containsKey("a"), equals(true));
337 expect(m.containsKey("d"), equals(false));
338 });
339 });
340 test('operator []', () {
341 js.scoped(() {
342 final m = new jsw.JsObjectToMapAdapter.fromProxy(
343 js.map({"a": 1, "b": "c"}));
344 expect(m["a"], equals(1));
345 expect(m["b"], equals("c"));
346 });
347 });
348 test('operator []=', () {
349 js.scoped(() {
350 final m = new jsw.JsObjectToMapAdapter.fromProxy(js.map({}));
351 m["a"] = 1;
352 expect(m["a"], equals(1));
353 expect(m.length, equals(1));
354 m["b"] = "c";
355 expect(m["b"], equals("c"));
356 expect(m.length, equals(2));
357 });
358 });
359 test('remove', () {
360 js.scoped(() {
361 final m = new jsw.JsObjectToMapAdapter.fromProxy(
362 js.map({"a": 1, "b": "c"}));
363 expect(m.remove("a"), equals(1));
364 expect(m["b"], equals("c"));
365 expect(m.length, equals(1));
366 });
367 });
368
369 test('bidirectionnal serialization of Proxy', () {
370 js.scoped(() {
371 final myMap = new jsw.JsObjectToMapAdapter<PersonTP>.fromProxy(
372 js.map({}), new jsw.TranslatorForSerializable<PersonTP>((p) =>
373 new PersonTP.fromProxy(p)));
374 myMap["a"] = new PersonTP('John', 'Doe');
375 expect(myMap["a"].firstname, 'John');
376 });
377 });
378 });
379
380 test('retain/release', () {
381 PersonTP john;
382 js.scoped(() {
383 john = new PersonTP('John', 'Doe');
384 });
385 js.scoped((){
386 expect(() => john.sayHello(), throws);
387 });
388 js.scoped(() {
389 john = new PersonTP('John', 'Doe');
390 js.retain(john);
391 });
392 js.scoped((){
393 expect(() => john.sayHello(), returnsNormally);
394 js.release(john);
395 });
396 js.scoped((){
397 expect(() => john.sayHello(), throws);
398 });
399 });
400 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698