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

Side by Side Diff: tests/html/js_array_test.dart

Issue 1194643002: Enhance dart:js interop in a backwards compatible manner. List objects can now be passed back and f… (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 6 months 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
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
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 jsArrayTest;
6
7 import 'dart:async';
8 import 'dart:collection';
9 import 'dart:html';
10 import 'dart:js';
11
12 import 'package:unittest/unittest.dart';
13 import 'package:unittest/html_config.dart';
14
15 _injectJs() {
16 final script = new ScriptElement();
17 script.type = 'text/javascript';
18 script.innerHtml = r"""
19 function callJsMethod(jsObj, jsMethodName, args) {
20 return jsObj[jsMethodName].apply(jsObj, args);
21 }
22
23 function jsEnumerateIndices(obj) {
24 var ret = [];
25 for(var i in obj) {
26 ret.push(i);
27 }
28 return ret;
29 }
30
31 function setValue(obj, index, value) {
32 return obj[index] = value;
33 }
34
35 function getValue(obj, index) {
36 return obj[index];
37 }
38
39 function checkIsArray(obj) {
40 this.dartArray = obj;
41 return Array.isArray(obj);
42 }
43
44 function concatValues(obj) {
45 return obj.concat("a", "b", ["c", "d"], 42);
46 }
47
48 function concatOntoArray(obj) {
49 return [1,2,3].concat(obj, "foo");
50 }
51
52 function repeatedConcatOntoArray(obj) {
53 return [1,2,3].concat(obj, obj);
54 }
55
56 function everyGreaterThanZero(obj) {
57 return obj.every(function(currentValue, index, array) {
58 return currentValue > 0;
59 });
60 }
61
62 function everyGreaterThanZeroCheckThisArg(obj) {
63 var j = 0;
64 return obj.every(function(currentValue, index, array) {
65 if (j != index) {
66 throw "Unxpected index";
67 }
68 j++;
69 if (array !== obj) {
70 throw "Array argument doesn't match obj";
71 }
72 return currentValue > 0;
73 });
74 }
75
76 function filterGreater42(obj) {
77 return obj.filter(function(currentValue, index, array) {
78 return currentValue > 42;
79 });
80 }
81
82 function forEachCollectResult(array, callback) {
83 var result = [];
84 array.forEach(function(currentValue) {
85 result.push(currentValue * 2);
86 });
87 return result;
88 }
89
90 function someEqual42(array, callback) {
91 return array.some(function(currentValue) {
92 return currentValue == 42;
93 });
94 }
95
96 function sortNumbersBackwards(array) {
97 return array.sort(function(a, b) {
98 return b - a;
99 });
100 }
101
102 function spliceDummyItems(array) {
103 return array.splice(1, 2, "quick" ,"brown", "fox");
104 }
105
106 function spliceTestStringArgs(array) {
107 return array.splice("1.2", "2.01", "quick" ,"brown", "fox");
108 }
109
110 function splicePastEnd(array) {
111 return array.splice(1, 5332, "quick" ,"brown", "fox");
112 }
113
114 function callJsToString(array) {
115 return array.toString();
116 }
117
118 function mapAddIndexToEachElement(array) {
119 return array.map(function(currentValue, index) {
120 return currentValue + index;
121 });
122 }
123
124 function reduceSumDoubledElements(array) {
125 return array.reduce(function(previousValue, currentValue) { return previousVal ue + currentValue*2; }, 0);
126 }
127
128 // TODO(jacobr): add a test that distinguishes reduce from reduceRight.
129 function reduceRightSumDoubledElements(array) {
130 return array.reduceRight(function(previousValue, currentValue) { return previo usValue + currentValue*2; }, 0);
131 }
132
133 function identical(o1, o2) {
134 return o1 === o2;
135 }
136
137 function getOwnPropertyDescriptorJson(array, property) {
138 return JSON.stringify(Object.getOwnPropertyDescriptor(array, property));
139 }
140
141 """;
142 document.body.append(script);
143 }
144
145 class Foo {
146 }
147
148 callJsMethod(List array, String methodName, List args) =>
149 context.callMethod("callJsMethod", [array, methodName, args]);
150
151 callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]);
152 callLastIndexOf(List array, value) => callJsMethod(array, "lastIndexOf", [value] );
153
154 callPop(List array) => callJsMethod(array, "pop", []);
155 callPush(List array, element) => callJsMethod(array, "push", [element]);
156 callShift(List array) => callJsMethod(array, "shift", []);
157 callReverse(List array) => callJsMethod(array, "reverse", []);
158
159 main() {
160 _injectJs();
161 useHtmlConfiguration();
162
163 group('indexOf', () {
164 var div = new DivElement();
165 var list = [3, 42, "foo", 42, div];
166 test('found', () {
167 expect(callIndexOf(list, 3), equals(0));
168 expect(callIndexOf(list, 42), equals(1));
169 expect(callIndexOf(list, "foo"), equals(2));
170 expect(callIndexOf(list, div), equals(4));
171 });
172
173 test('missing', () {
174 expect(callIndexOf(list, 31), equals(-1));
175 expect(callIndexOf(list, "42"), equals(-1));
176 expect(callIndexOf(list, null), equals(-1));
177 });
178 });
179
180 group('join', () {
181 var list = [3, 42, "foo"];
182 var listWithDartClasses = [3, new Foo(), 42, "foo", new Object()];
183 test('default', () {
184 expect(callJsMethod(list, "join", []), equals("3,42,foo"));
185 expect(callJsMethod(listWithDartClasses, "join", []), equals("3,Instance o f 'Foo',42,foo,Instance of 'Object'"));
186 });
187
188 test('custom separator', () {
189 expect(callJsMethod(list, "join", ["##"]), equals("3##42##foo"));
190 });
191 });
192
193 group('lastIndexOf', () {
194 var list = [3, 42, "foo", 42];
195 test('found', () {
196 expect(callLastIndexOf(list, 3), equals(0));
197 expect(callLastIndexOf(list, 42), equals(3));
198 expect(callLastIndexOf(list, "foo"), equals(2));
199 });
200
201 test('missing', () {
202 expect(callLastIndexOf(list, 31), equals(-1));
203 expect(callLastIndexOf(list, "42"), equals(-1));
204 expect(callLastIndexOf(list, null), equals(-1));
205 });
206 });
207
208 group('pop', () {
209 test('all', () {
210 var foo = new Foo();
211 var div = new DivElement();
212 var list = [3, 42, "foo", foo, div];
213 expect(callPop(list), equals(div));
214 expect(list.length, equals(4));
215 expect(callPop(list), equals(foo));
216 expect(list.length, equals(3));
217 expect(callPop(list), equals("foo"));
218 expect(list.length, equals(2));
219 expect(callPop(list), equals(42));
220 expect(list.length, equals(1));
221 expect(callPop(list), equals(3));
222 expect(list.length, equals(0));
223 });
224 });
225
226 group('push', () {
227 test('strings', () {
228 var list = [];
229 expect(callPush(list, "foo"), equals(1));
230 expect(callPush(list, "bar"), equals(2));
231 expect(callPush(list, "baz"), equals(3));
232 expect(list.toString(), equals("[foo, bar, baz]"));
233 });
234 });
235
236 group('shift', () {
237 test('all', () {
238 var foo = new Foo();
239 var div = new DivElement();
240 var list = [3, 42, "foo", foo, div];
241 expect(callShift(list), equals(3));
242 expect(list.length, equals(4));
243 expect(callShift(list), equals(42));
244 expect(list.length, equals(3));
245 expect(callShift(list), equals("foo"));
246 expect(list.length, equals(2));
247 expect(callShift(list), equals(foo));
248 expect(list.length, equals(1));
249 expect(callShift(list), equals(div));
250 expect(list.length, equals(0));
251 });
252 });
253
254 group('reverse', () {
255 test('simple', () {
256 var foo = new Foo();
257 var div = new DivElement();
258 var list = [3, 42, "foo"];
259 callReverse(list);
260 expect(list.toString(), equals("[foo, 42, 3]"));
261 list = [3, 42];
262 callReverse(list);
263 expect(list.toString(), equals("[42, 3]"));
264 });
265 });
266
267 group('slice', () {
268 test('copy', () {
269 var foo = new Foo();
270 var div = new DivElement();
271 var list = [3, 42, "foo", foo, div];
272 var copy = callJsMethod(list, "slice", []);
273 expect(identical(list, copy), equals(false));
274 expect(copy.length, equals(list.length));
275 for (var i = 0; i < list.length; i++) {
276 expect(list[i], equals(copy[i]));
277 }
278 copy.add("dummy");
279 expect(list.length + 1, equals(copy.length));
280 });
281 test('specify start', () {
282 var list = [3, 42, "foo"];
283 var copy = callJsMethod(list, "slice", [1]);
284 expect(copy.first, equals(42));
285 });
286 test('specify start and end', () {
287 var list = [3, 42, 92, "foo"];
288 var copy = callJsMethod(list, "slice", [1,3]);
289 expect(copy.first, equals(42));
290 expect(copy.last, equals(92));
291 });
292 });
293
294 group("js snippet tests", () {
295 test("enumerate indices", () {
296 var list = ["a", "b", "c", "d"];
297 var indices = context.callMethod('jsEnumerateIndices', [list]);
298 expect(indices.length, equals(4));
299 for (int i = 0; i < 4; i++) {
300 expect(indices[i], equals('$i'));
301 }
302 });
303
304 test("set element", () {
305 var list = ["a", "b", "c", "d"];
306 context.callMethod('setValue', [list, 0, 42]);
307 expect(list[0], equals(42));
308 context.callMethod('setValue', [list, 1, 84]);
309 expect(list[1], equals(84));
310 context.callMethod('setValue', [list, 6, 100]); // Off the end of the list .
311 expect(list.length, equals(7));
312 expect(list[4], equals(null));
313 expect(list[6], equals(100));
314
315 // These tests have to be commented out because we don't persist
316 // JS proxies for Dart objects like we could/should.
317 // context.callMethod('setValue', [list, -1, "foo"]); // Not a valid array index
318 // expect(context.callMethod('getValue', [list, -1]), equals("foo"));
319 // expect(context.callMethod('getValue', [list, "-1"]), equals("foo"));
320 });
321
322 test("get element", () {
323 var list = ["a", "b", "c", "d"];
324 expect(context.callMethod('getValue', [list, 0]), equals("a"));
325 expect(context.callMethod('getValue', [list, 1]), equals("b"));
326 expect(context.callMethod('getValue', [list, 6]), equals(null));
327 expect(context.callMethod('getValue', [list, -1]), equals(null));
328
329 expect(context.callMethod('getValue', [list, "0"]), equals("a"));
330 expect(context.callMethod('getValue', [list, "1"]), equals("b"));
331 });
332
333 test("is array", () {
334 var list = ["a", "b"];
335 expect(context.callMethod("checkIsArray", [list]), isTrue);
336 });
337
338 test("property descriptors", () {
339 // This test matters to make behavior consistent with JS native arrays
340 // and to make devtools integration work well.
341 var list = ["a", "b"];
342 expect(context.callMethod("getOwnPropertyDescriptorJson", [list, 0]),
343 equals('{"value":"a","writable":true,"enumerable":true,"configurable":tr ue}'));
344
345 expect(context.callMethod("getOwnPropertyDescriptorJson", [list, "length"] ),
346 equals('{"value":2,"writable":true,"enumerable":false,"configurable":fal se}'));
347 });
348
349 test("concat js arrays", () {
350 var list = ["1", "2"];
351 var ret = context.callMethod("concatValues", [list]);
352 expect(list.length, equals(2));
353 expect(ret.length, equals(7));
354 expect(ret[0], equals("1"));
355 expect(ret[3], equals("b"));
356 expect(ret[5], equals("d"));
357 expect(ret[6], equals(42));
358 });
359
360 test("concat onto arrays", () {
361 // This test only passes if we have monkey patched the core Array object
362 // prototype to handle Dart Lists.
363 var list = ["a", "b"];
364 var ret = context.callMethod("concatOntoArray", [list]);
365 expect(list.length, equals(2));
366 expect(ret.length, equals(6));
367 expect(ret[0], equals(1));
368 expect(ret[3], equals("a"));
369 expect(ret[4], equals("b"));
370 expect(ret[5], equals("foo"));
371 });
372
373 test("every greater than zero", () {
374 expect(context.callMethod("everyGreaterThanZero", [[1, 5]]), equals(true)) ;
375 expect(context.callMethod("everyGreaterThanZeroCheckThisArg", [[1, 5]]), e quals(true));
376 expect(context.callMethod("everyGreaterThanZero", [[1, 0]]), equals(false) );
377 });
378
379 test("filter greater than 42", () {
380 expect(context.callMethod("filterGreater42", [[1, 5]]), equals([]));
381 expect(context.callMethod("filterGreater42", [[43, 5, 49]]), equals([43, 4 9]));
382 expect(context.callMethod("filterGreater42", [["43", "5", "49"]]), equals( ["43", "49"]));
383 });
384
385 test("for each collect result", () {
386 expect(context.callMethod("forEachCollectResult", [[1, 5, 7]]), equals([2, 10, 14]));
387 });
388
389 test("some", () {
390 expect(context.callMethod("someEqual42", [[1, 5, 9]]), equals(false));
391 expect(context.callMethod("someEqual42", [[1, 42, 9]]), equals(true));
392 });
393
394 test("sort backwards", () {
395 var arr = [1, 5, 9];
396 var ret = context.callMethod("sortNumbersBackwards", [arr]);
397 expect(identical(arr, ret), equals(true));
398 expect(ret, equals([9, 5, 1]));
399 });
400
401
402 test("splice dummy items", () {
403 var list = [1,2,3,4];
404 var removed = context.callMethod("spliceDummyItems", [list]);
405 expect(removed.length, equals(2));
406 expect(removed[0], equals(2));
407 expect(removed[1], equals(3));
408 expect(list.first, equals(1));
409 expect(list[1], equals("quick"));
410 expect(list[2], equals("brown"));
411 expect(list[3], equals("fox"));
412 expect(list.last, equals(4));
413 });
414
415 test("splice string args", () {
416 var list = [1,2,3,4];
417 var removed = context.callMethod("spliceTestStringArgs", [list]);
418 expect(removed.length, equals(2));
419 expect(removed[0], equals(2));
420 expect(removed[1], equals(3));
421 expect(list.first, equals(1));
422 expect(list[1], equals("quick"));
423 expect(list[2], equals("brown"));
424 expect(list[3], equals("fox"));
425 expect(list.last, equals(4));
426 });
427
428 test("splice pastEndOfArray", () {
429 var list = [1,2,3,4];
430 var removed = context.callMethod("splicePastEnd", [list]);
431 expect(removed.length, equals(3));
432 expect(list.first, equals(1));
433 expect(list.length, equals(4));
434 expect(list[1], equals("quick"));
435 expect(list[2], equals("brown"));
436 expect(list[3], equals("fox"));
437 });
438
439 test("splice both bounds past end of array", () {
440 var list = [1];
441 var removed = context.callMethod("splicePastEnd", [list]);
442 expect(removed.length, equals(0));
443 expect(list.first, equals(1));
444 expect(list.length, equals(4));
445 expect(list[1], equals("quick"));
446 expect(list[2], equals("brown"));
447 expect(list[3], equals("fox"));
448 });
449 });
450
451 // This test group is disabled until we figure out an efficient way to
452 // distinguish between "array" Dart List types and non-array Dart list types.
453 /*
454 group('Non-array Lists', () {
455 test('opaque proxy', () {
456 // Dartium could easily support making LinkedList and all other classes
457 // implementing List behave like a JavaScript array but that would
458 // be challenging to implement in dart2js until browsers support ES6.
459 var list = ["a", "b", "c", "d"];
460 var listView = new UnmodifiableListView(list.getRange(1,3));
461 expect(listView is List, equals(true));
462 expect(listView.length, equals(2));
463 expect(context.callMethod("checkIsArray", [listView]), isFalse);
464 expect(context.callMethod("checkIsArray", [listView.toList()]), isTrue);
465 expect(context.callMethod("getOwnPropertyDescriptorJson", [listView, "leng th"]), equals("null"));
466 });
467 });
468 */
469 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698