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 @JS() | 5 @JS() |
6 library js_native_test; | 6 library js_native_test; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:html'; | 9 import 'dart:html'; |
10 import 'dart:typed_data' show ByteBuffer, Int32List; | 10 import 'dart:typed_data' show ByteBuffer, Int32List; |
11 import 'dart:indexed_db' show IdbFactory, KeyRange; | 11 import 'dart:indexed_db' show IdbFactory, KeyRange; |
12 | 12 |
13 import 'package:js/js.dart'; | 13 import 'package:js/js.dart'; |
14 import 'package:js/js_util.dart' as js_util; | 14 import 'package:js/js_util.dart' as js_util; |
15 | 15 import 'package:minitest/minitest.dart'; |
16 import 'package:unittest/unittest.dart'; | |
17 import 'package:unittest/html_individual_config.dart'; | |
18 | 16 |
19 _injectJs() { | 17 _injectJs() { |
20 final script = new ScriptElement(); | 18 final script = new ScriptElement(); |
21 script.type = 'text/javascript'; | 19 script.type = 'text/javascript'; |
22 script.innerHtml = r""" | 20 script.innerHtml = r""" |
23 var x = 42; | 21 var x = 42; |
24 | 22 |
25 var _x = 123; | 23 var _x = 123; |
26 | 24 |
27 var myArray = ["value1"]; | 25 var myArray = ["value1"]; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 105 |
108 @JS("Object.prototype.hasOwnProperty") | 106 @JS("Object.prototype.hasOwnProperty") |
109 external get _hasOwnProperty; | 107 external get _hasOwnProperty; |
110 | 108 |
111 bool hasOwnProperty(o, String name) { | 109 bool hasOwnProperty(o, String name) { |
112 return js_util.callMethod(_hasOwnProperty, 'call', [o, name]); | 110 return js_util.callMethod(_hasOwnProperty, 'call', [o, name]); |
113 } | 111 } |
114 | 112 |
115 main() { | 113 main() { |
116 _injectJs(); | 114 _injectJs(); |
117 useHtmlIndividualConfiguration(); | |
118 | 115 |
119 group('js_util.jsify()', () { | 116 group('js_util.jsify()', () { |
120 test('convert a List', () { | 117 test('convert a List', () { |
121 final list = [1, 2, 3, 4, 5, 6, 7, 8]; | 118 final list = [1, 2, 3, 4, 5, 6, 7, 8]; |
122 var array = js_util.jsify(list); | 119 var array = js_util.jsify(list); |
123 expect(array is List, isTrue); | 120 expect(array is List, isTrue); |
124 expect(identical(array, list), isFalse); | 121 expect(identical(array, list), isFalse); |
125 expect(array.length, equals(list.length)); | 122 expect(array.length, equals(list.length)); |
126 for (var i = 0; i < list.length; i++) { | 123 for (var i = 0; i < list.length; i++) { |
127 expect(array[i], equals(list[i])); | 124 expect(array[i], equals(list[i])); |
(...skipping 13 matching lines...) Expand all Loading... |
141 test('convert a Map', () { | 138 test('convert a Map', () { |
142 var map = {'a': 1, 'b': 2, 'c': 3}; | 139 var map = {'a': 1, 'b': 2, 'c': 3}; |
143 var jsMap = js_util.jsify(map); | 140 var jsMap = js_util.jsify(map); |
144 expect(jsMap is! List, isTrue); | 141 expect(jsMap is! List, isTrue); |
145 for (var key in map.keys) { | 142 for (var key in map.keys) { |
146 expect(checkMap(jsMap, key, map[key]), isTrue); | 143 expect(checkMap(jsMap, key, map[key]), isTrue); |
147 } | 144 } |
148 }); | 145 }); |
149 | 146 |
150 test('deep convert a complex object', () { | 147 test('deep convert a complex object', () { |
151 final object = { | 148 dynamic object = { |
152 'a': [ | 149 'a': [ |
153 1, | 150 1, |
154 [2, 3] | 151 [2, 3] |
155 ], | 152 ], |
156 'b': {'c': 3, 'd': new Foo(42)}, | 153 'b': {'c': 3, 'd': new Foo(42)}, |
157 'e': null | 154 'e': null |
158 }; | 155 }; |
159 var jsObject = js_util.jsify(object); | 156 var jsObject = js_util.jsify(object); |
160 expect(js_util.getProperty(jsObject, 'a')[0], equals(object['a'][0])); | 157 expect(js_util.getProperty(jsObject, 'a')[0], equals(object['a'][0])); |
161 expect( | 158 expect( |
162 js_util.getProperty(jsObject, 'a')[1][0], equals(object['a'][1][0])); | 159 js_util.getProperty(jsObject, 'a')[1][0], equals(object['a'][1][0])); |
163 expect( | 160 expect( |
164 js_util.getProperty(jsObject, 'a')[1][1], equals(object['a'][1][1])); | 161 js_util.getProperty(jsObject, 'a')[1][1], equals(object['a'][1][1])); |
165 expect(js_util.getProperty(js_util.getProperty(jsObject, 'b'), 'c'), | 162 expect(js_util.getProperty(js_util.getProperty(jsObject, 'b'), 'c'), |
166 equals(object['b']['c'])); | 163 equals(object['b']['c'])); |
167 expect(js_util.getProperty(js_util.getProperty(jsObject, 'b'), 'd'), | 164 expect(js_util.getProperty(js_util.getProperty(jsObject, 'b'), 'd'), |
168 equals(object['b']['d'])); | 165 equals(object['b']['d'])); |
169 expect( | 166 expect( |
170 js_util.callMethod( | 167 js_util.callMethod( |
171 js_util.getProperty(js_util.getProperty(jsObject, 'b'), 'd'), | 168 js_util.getProperty(js_util.getProperty(jsObject, 'b'), 'd'), |
172 'bar', []), | 169 'bar', []), |
173 equals(42)); | 170 equals(42)); |
174 expect(js_util.getProperty(jsObject, 'e'), isNull); | 171 expect(js_util.getProperty(jsObject, 'e'), isNull); |
175 }); | 172 }); |
176 | 173 |
177 test('throws if object is not a Map or Iterable', () { | 174 test('throws if object is not a Map or Iterable', () { |
178 expect(() => js_util.jsify('a'), | 175 expect(() => js_util.jsify('a'), throwsArgumentError); |
179 throwsA(new isInstanceOf<ArgumentError>())); | |
180 }); | 176 }); |
181 }); | 177 }); |
182 | 178 |
183 group('js_util.newObject', () { | 179 group('js_util.newObject', () { |
184 test('create', () { | 180 test('create', () { |
185 expect(identical(js_util.newObject(), js_util.newObject()), isFalse); | 181 expect(identical(js_util.newObject(), js_util.newObject()), isFalse); |
186 }); | 182 }); |
187 | 183 |
188 test('callMethod', () { | 184 test('callMethod', () { |
189 var o = js_util.newObject(); | 185 var o = js_util.newObject(); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 expect(identical(l.a, list), isTrue); | 276 expect(identical(l.a, list), isTrue); |
281 l.JS$class = 42; | 277 l.JS$class = 42; |
282 expect(l.JS$class, equals(42)); | 278 expect(l.JS$class, equals(42)); |
283 js_util.setProperty(l, 'class', 100); | 279 js_util.setProperty(l, 'class', 100); |
284 expect(l.JS$class, equals(100)); | 280 expect(l.JS$class, equals(100)); |
285 }); | 281 }); |
286 }); | 282 }); |
287 | 283 |
288 group('callMethod', () { | 284 group('callMethod', () { |
289 test('html object', () { | 285 test('html object', () { |
290 var canvas = new Element.tag('canvas'); | 286 var canvas = new CanvasElement(); |
291 expect( | 287 expect( |
292 identical(canvas.getContext('2d'), | 288 identical(canvas.getContext('2d'), |
293 js_util.callMethod(canvas, 'getContext', ['2d'])), | 289 js_util.callMethod(canvas, 'getContext', ['2d'])), |
294 isTrue); | 290 isTrue); |
295 }); | 291 }); |
296 | 292 |
297 test('typed object', () { | 293 test('typed object', () { |
298 var f = new Foo(42); | 294 var f = new Foo(42); |
299 expect(js_util.callMethod(f, 'bar', []), equals(42)); | 295 expect(js_util.callMethod(f, 'bar', []), equals(42)); |
300 }); | 296 }); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 expect(textNode is Text, isTrue); | 334 expect(textNode is Text, isTrue); |
339 expect(textNode.text, equals('foo')); | 335 expect(textNode.text, equals('foo')); |
340 }); | 336 }); |
341 | 337 |
342 test('typed object', () { | 338 test('typed object', () { |
343 Foo f = js_util.callConstructor(JSFooType, [42]); | 339 Foo f = js_util.callConstructor(JSFooType, [42]); |
344 expect(f.a, equals(42)); | 340 expect(f.a, equals(42)); |
345 }); | 341 }); |
346 }); | 342 }); |
347 } | 343 } |
OLD | NEW |