Chromium Code Reviews| Index: tests/html/js_tests.dart |
| diff --git a/tests/html/js_tests.dart b/tests/html/js_tests.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1c339e97f3fb3c4b86c2f86e21afec084077f8e3 |
| --- /dev/null |
| +++ b/tests/html/js_tests.dart |
| @@ -0,0 +1,349 @@ |
| +// 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.
|
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +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.
|
| + |
| +import 'dart:async'; |
| +import 'dart:html'; |
| +import 'dart:js'; |
| + |
| +import '../../pkg/unittest/lib/unittest.dart'; |
| +import '../../pkg/unittest/lib/html_config.dart'; |
| + |
| +class Foo implements Serializable<JsObject> { |
| + final JsObject _proxy; |
| + |
| + 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.
|
| + |
| + JsObject toJs() => _proxy; |
| + |
| + num get a => _proxy['a']; |
| + num bar() => _proxy.callMethod('bar'); |
| +} |
| + |
| +class Color implements Serializable<String> { |
| + static final RED = new Color._("red"); |
| + static final BLUE = new Color._("blue"); |
| + String _value; |
| + Color._(this._value); |
| + String toJs() => this._value; |
| +} |
| + |
| +main() { |
| + useHtmlConfiguration(); |
| + |
| + test('read global field', () { |
| + expect(context['x'], equals(42)); |
| + expect(context['y'], isNull); |
| + }); |
| + |
| + test('read global field with underscore', () { |
| + expect(context['_x'], equals(123)); |
| + expect(context['y'], isNull); |
| + }); |
| + |
| + test('js instantiation : new Foo()', () { |
| + final Foo2 = context['container']['Foo']; |
| + final foo = new JsObject(Foo2, [42]); |
| + expect(foo['a'], 42); |
| + expect(Foo2['b'], 38); |
| + }); |
| + |
| + test('js instantiation : new Array()', () { |
| + final a = new JsObject(context['Array']); |
| + expect(a, isNotNull); |
| + expect(a['length'], equals(0)); |
| + |
| + a.callMethod('push', ["value 1"]); |
| + expect(a['length'], equals(1)); |
| + expect(a[0], equals("value 1")); |
| + |
| + a.callMethod('pop'); |
| + expect(a['length'], equals(0)); |
| + }); |
| + |
| + test('js instantiation : new Date()', () { |
| + final a = new JsObject(context['Date']); |
| + expect(a.callMethod('getTime'), isNotNull); |
| + }); |
| + |
| + test('js instantiation : new Date(12345678)', () { |
| + final a = new JsObject(context['Date'], [12345678]); |
| + expect(a.callMethod('getTime'), equals(12345678)); |
| + }); |
| + |
| + test('js instantiation : new Date("December 17, 1995 03:24:00 GMT+01:00")', |
| + () { |
| + final a = new JsObject(context['Date'], |
| + ["December 17, 1995 03:24:00 GMT+01:00"]); |
| + expect(a.callMethod('getTime'), equals(819167040000)); |
| + }); |
| + |
| + test('js instantiation : new Date(1995,11,17)', () { |
| + // Note: JS Date counts months from 0 while Dart counts from 1. |
| + final a = new JsObject(context['Date'], [1995, 11, 17]); |
| + final b = new DateTime(1995, 12, 17); |
| + expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); |
| + }); |
| + |
| + test('js instantiation : new Date(1995,11,17,3,24,0)', () { |
| + // Note: JS Date counts months from 0 while Dart counts from 1. |
| + final a = new JsObject(context['Date'], |
| + [1995, 11, 17, 3, 24, 0]); |
| + final b = new DateTime(1995, 12, 17, 3, 24, 0); |
| + expect(a.callMethod('getTime'), equals(b.millisecondsSinceEpoch)); |
| + }); |
| + |
| + test('js instantiation : new Object()', () { |
| + final a = new JsObject(context['Object']); |
| + expect(a, isNotNull); |
| + |
| + a['attr'] = "value"; |
| + expect(a['attr'], equals("value")); |
| + }); |
| + |
| + test(r'js instantiation : new RegExp("^\w+$")', () { |
| + final a = new JsObject(context['RegExp'], [r'^\w+$']); |
| + expect(a, isNotNull); |
| + expect(a.callMethod('test', ['true']), isTrue); |
| + expect(a.callMethod('test', [' false']), isFalse); |
| + }); |
| + |
| + test('js instantiation via map notation : new Array()', () { |
| + final a = new JsObject(context['Array']); |
| + expect(a, isNotNull); |
| + expect(a['length'], equals(0)); |
| + |
| + a['push'].apply(a, ["value 1"]); |
| + expect(a['length'], equals(1)); |
| + expect(a[0], equals("value 1")); |
| + |
| + a['pop'].apply(a); |
| + expect(a['length'], equals(0)); |
| + }); |
| + |
| + test('js instantiation via map notation : new Date()', () { |
| + final a = new JsObject(context['Date']); |
| + expect(a['getTime'].apply(a), isNotNull); |
| + }); |
| + |
| + test('js instantiation : typed array', () { |
| + final codeUnits = "test".codeUnits; |
| + final buf = new JsObject(context['ArrayBuffer'], [codeUnits.length]); |
| + final bufView = new JsObject(context['Uint8Array'], [buf]); |
| + for (var i = 0; i < codeUnits.length; i++) { |
| + bufView[i] = codeUnits[i]; |
| + } |
| + }); |
| + |
| + test('write global field', () { |
| + context['y'] = 42; |
| + expect(context['y'], equals(42)); |
| + }); |
| + |
| + test('get JS JsFunction', () { |
| + var razzle = context['razzle']; |
| + expect(razzle.apply(context), equals(42)); |
| + }); |
| + |
| + test('call JS function', () { |
| + expect(context.callMethod('razzle'), equals(42)); |
| + expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); |
| + }); |
| + |
| + test('call JS function via map notation', () { |
| + expect(context['razzle'].apply(context), equals(42)); |
| + expect(() => context['dazzle'].apply(context), |
| + throwsA(isNoSuchMethodError)); |
| + }); |
| + |
| + test('call JS function with varargs', () { |
| + expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), |
| + equals(55)); |
| + }); |
| + |
| + test('allocate JS object', () { |
| + var foo = new JsObject(context['Foo'], [42]); |
| + expect(foo['a'], equals(42)); |
| + expect(foo.callMethod('bar'), equals(42)); |
| + expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); |
| + }); |
| + |
| + test('call toString()', () { |
| + var foo = new JsObject(context['Foo'], [42]); |
| + expect(foo.toString(), equals("I'm a Foo a=42")); |
| + var container = context['container']; |
| + expect(container.toString(), equals("[object Object]")); |
| + }); |
| + |
| + test('allocate simple JS array', () { |
| + final list = [1, 2, 3, 4, 5, 6, 7, 8]; |
| + var array = jsify(list); |
| + expect(context.callMethod('isArray', [array]), isTrue); |
| + expect(array['length'], equals(list.length)); |
| + for (var i = 0; i < list.length ; i++) { |
| + expect(array[i], equals(list[i])); |
| + } |
| + }); |
| + |
| + test('allocate JS array with iterable', () { |
| + final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); |
| + var array = jsify(set); |
| + expect(context.callMethod('isArray', [array]), isTrue); |
| + expect(array['length'], equals(set.length)); |
| + for (var i = 0; i < array['length'] ; i++) { |
| + expect(set.contains(array[i]), isTrue); |
| + } |
| + }); |
| + |
| + test('allocate simple JS map', () { |
| + var map = {'a': 1, 'b': 2, 'c': 3}; |
| + var jsMap = jsify(map); |
| + expect(!context.callMethod('isArray', [jsMap]), isTrue); |
| + for (final key in map.keys) { |
| + expect(context.callMethod('checkMap', [jsMap, key, map[key]]), isTrue); |
| + } |
| + }); |
| + |
| + test('allocate complex JS object', () { |
| + final object = |
| + { |
| + 'a': [1, [2, 3]], |
| + 'b': { |
| + 'c': 3, |
| + 'd': new JsObject(context['Foo'], [42]) |
| + }, |
| + 'e': null |
| + }; |
| + var jsObject = jsify(object); |
| + expect(jsObject['a'][0], equals(object['a'][0])); |
| + expect(jsObject['a'][1][0], equals(object['a'][1][0])); |
| + expect(jsObject['a'][1][1], equals(object['a'][1][1])); |
| + expect(jsObject['b']['c'], equals(object['b']['c'])); |
| + expect(jsObject['b']['d'], equals(object['b']['d'])); |
| + expect(jsObject['b']['d'].callMethod('bar'), equals(42)); |
| + expect(jsObject['e'], isNull); |
| + }); |
| + |
| + test('invoke Dart callback from JS', () { |
| + expect(() => context.callMethod('invokeCallback'), throws); |
| + |
| + context['callback'] = new Callback.once(() => 42); |
| + expect(context.callMethod('invokeCallback'), equals(42)); |
| + }); |
| + |
| + test('callback as parameter', () { |
| + expect(context.callMethod('getTypeOf', [context['razzle']]), |
| + equals("function")); |
| + }); |
| + |
| + test('invoke Dart callback from JS with this', () { |
| + final constructor = new Callback.once(($this, arg1) { |
| + $this['a'] = 42; |
| + $this['b'] = jsify(["a", arg1]); |
| + }, withThis: true); |
| + var o = new JsObject(constructor, ["b"]); |
| + expect(o['a'], equals(42)); |
| + expect(o['b'][0], equals("a")); |
| + expect(o['b'][1], equals("b")); |
| + }); |
| + |
| + test('invoke Dart callback from JS with 11 parameters', () { |
| + context['callbackWith11params'] = new Callback.once((p1, p2, p3, p4, |
| + p5, p6, p7, p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10' |
| + '$p11'); |
| + expect(context.callMethod('invokeCallbackWith11params'), |
| + equals('1234567891011')); |
| + }); |
| + |
| + test('create a Dart callback outside a scope', () { |
| + // Note, the test framework does not guarantee that each test runs as a |
| + // separate event. This test creates a new asynchronous event and |
| + // ensures that a callback can be created without a scope (i.e., that the |
| + // scope is created on demand). |
| + final subtest = () { |
| + var callback = new Callback.once(() => 42); |
| + context['callback'] = callback; |
| + expect(context.callMethod('invokeCallback'), equals(42)); |
| + }; |
| + |
| + runAsync(expectAsync0(subtest)); |
| + }); |
| + |
| + test('return a JS proxy to JavaScript', () { |
| + var result = context.callMethod('testJsMap', [ |
| + new Callback.once(() => jsify({ 'value': 42 }))]); |
| + expect(result, 42); |
| + }); |
| + |
| + test('test proxy equality', () { |
| + var foo1 = new JsObject(context['Foo'], [1]); |
| + var foo2 = new JsObject(context['Foo'], [2]); |
| + context['foo'] = foo1; |
| + context['foo'] = foo2; |
| + expect(foo1, isNot(equals(context['foo']))); |
| + expect(foo2, equals(context['foo'])); |
| + }); |
| + |
| + test('test instanceof', () { |
| + var foo = new JsObject(context['Foo'], [1]); |
| + expect(foo.instanceof(context['Foo']), isTrue); |
| + expect(foo.instanceof(context['Object']), isTrue); |
| + expect(foo.instanceof(context['String']), isFalse); |
| + }); |
| + |
| + test('test deleteProperty', () { |
| + var object = jsify({}); |
| + object['a'] = 1; |
| + expect(context['Object'].callMethod('keys', [object])['length'], 1); |
| + expect(context['Object'].callMethod('keys', [object])[0], "a"); |
| + object.deleteProperty("a"); |
| + expect(context['Object'].callMethod('keys', [object])['length'], 0); |
| + }); |
| + |
| + test('test hasProperty', () { |
| + var object = jsify({}); |
| + object['a'] = 1; |
| + expect(object.hasProperty('a'), isTrue); |
| + expect(object.hasProperty('b'), isFalse); |
| + }); |
| + |
| + test('test index get and set', () { |
| + final myArray = context['myArray']; |
| + expect(myArray['length'], equals(1)); |
| + expect(myArray[0], equals("value1")); |
| + myArray[0] = "value2"; |
| + expect(myArray['length'], equals(1)); |
| + expect(myArray[0], equals("value2")); |
| + |
| + final foo = new JsObject(context['Foo'], [1]); |
| + foo["getAge"] = new Callback.once(() => 10); |
| + expect(foo.callMethod('getAge'), equals(10)); |
| + }); |
| + |
| + test('access a property of a function', () { |
| + expect(context.callMethod('Bar'), "ret_value"); |
| + expect(context['Bar']['foo'], "property_value"); |
| + }); |
| + |
| + test('retrieve same dart Object', () { |
| + final date = new DateTime.now(); |
| + context['dartDate'] = date; |
| + expect(context['dartDate'], equals(date)); |
| + }); |
| + |
| + test('usage of Serializable', () { |
| + final red = Color.RED; |
| + context['color'] = red; |
| + expect(context['color'], equals(red._value)); |
| + }); |
| + |
| + test('check for leaks', () { |
| + // Verify that the number of live objects is zero. |
| + final verifyNoLeaks = expectAsync0(() => expect(0, proxyCount())); |
| + // Run this check asychnronously to ensure that any current scope is |
| + // cleared first. |
| + runAsync(verifyNoLeaks); |
| + }); |
| +} |