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 @TestOn('browser') |
5 library template_wrappers_test; | 5 library template_wrappers_test; |
6 | 6 |
7 import 'dart:html'; | 7 import 'dart:html'; |
8 import 'dart:async'; | |
9 import 'dart:js' show context, JsObject; | 8 import 'dart:js' show context, JsObject; |
10 import 'package:unittest/html_config.dart'; | 9 import 'package:test/test.dart'; |
11 import 'package:unittest/unittest.dart'; | |
12 import 'package:web_components/interop.dart'; | 10 import 'package:web_components/interop.dart'; |
13 import 'package:web_components/polyfill.dart'; | 11 import 'package:web_components/polyfill.dart'; |
14 | 12 |
15 main() { | 13 main() { |
16 useHtmlConfiguration(); | |
17 setUp(() => customElementsReady); | 14 setUp(() => customElementsReady); |
18 | 15 |
19 test('interop is supported', () { | 16 test('interop is supported', () { |
20 expect(isSupported, isTrue); | 17 expect(isSupported, isTrue); |
21 }); | 18 }); |
22 | 19 |
23 test('previously created elements are not upgraded', () { | 20 test('previously created elements are not upgraded', () { |
24 var a = document.querySelector('x-a'); | 21 var a = document.querySelector('x-a'); |
25 expect(a is HtmlElement, isTrue, reason: 'x-a is HtmlElement'); | 22 expect(a is HtmlElement, isTrue, reason: 'x-a is HtmlElement'); |
26 expect(a is XAWrapper, isFalse, reason: 'x-a should not be upgraded yet'); | 23 expect(a is XAWrapper, isFalse, reason: 'x-a should not be upgraded yet'); |
(...skipping 10 matching lines...) Expand all Loading... |
37 expect(_readX(d), 2); | 34 expect(_readX(d), 2); |
38 | 35 |
39 /// Note: this registration has a global side-effect and is assumed in the | 36 /// Note: this registration has a global side-effect and is assumed in the |
40 /// following tests. | 37 /// following tests. |
41 registerDartType('x-a', XAWrapper); | 38 registerDartType('x-a', XAWrapper); |
42 registerDartType('x-b', XBWrapper, extendsTag: 'div'); | 39 registerDartType('x-b', XBWrapper, extendsTag: 'div'); |
43 registerDartType('x-c', XCWrapper); | 40 registerDartType('x-c', XCWrapper); |
44 onlyUpgradeNewElements(); | 41 onlyUpgradeNewElements(); |
45 registerDartType('x-d', XDWrapper); // late on purpose. | 42 registerDartType('x-d', XDWrapper); // late on purpose. |
46 | 43 |
47 a = document.querySelector('x-a'); | 44 a = document.querySelector('x-a') as XAWrapper; |
48 expect(a is HtmlElement, isTrue, reason: 'x-a is HtmlElement'); | 45 expect(a is HtmlElement, isTrue, reason: 'x-a is HtmlElement'); |
49 expect(a is XAWrapper, isTrue, reason: 'x-a is upgraded to XAWrapper'); | 46 expect(a is XAWrapper, isTrue, reason: 'x-a is upgraded to XAWrapper'); |
50 expect(a.x, 0); | 47 expect(a.x, 0); |
51 expect(a.wrapperCount, 0); | 48 expect(a.wrapperCount, 0); |
52 | 49 |
53 b = document.querySelector('[is=x-b]'); | 50 b = document.querySelector('[is=x-b]') as XBWrapper; |
54 expect(b is DivElement, isTrue, reason: 'x-b is DivElement'); | 51 expect(b is DivElement, isTrue, reason: 'x-b is DivElement'); |
55 expect(b is XBWrapper, isTrue, reason: 'x-b is upgraded to XBWrapper'); | 52 expect(b is XBWrapper, isTrue, reason: 'x-b is upgraded to XBWrapper'); |
56 expect(b.x, 1); | 53 expect(b.x, 1); |
57 expect(b.wrapperCount, 1); | 54 expect(b.wrapperCount, 1); |
58 | 55 |
59 // x-d was not upgraded because its registration came after we stopped | 56 // x-d was not upgraded because its registration came after we stopped |
60 // upgrading old elements: | 57 // upgrading old elements: |
61 d = document.querySelector('x-d'); | 58 d = document.querySelector('x-d'); |
62 expect(d is HtmlElement, isTrue, reason: 'x-d is HtmlElement'); | 59 expect(d is HtmlElement, isTrue, reason: 'x-d is HtmlElement'); |
63 expect(d is XDWrapper, isFalse, reason: 'x-d should not be upgraded yet'); | 60 expect(d is XDWrapper, isFalse, reason: 'x-d should not be upgraded yet'); |
64 expect(_readX(d), 2); | 61 expect(_readX(d), 2); |
65 | 62 |
66 var c = document.querySelector('x-c'); | 63 var c = document.querySelector('x-c'); |
67 expect(c is HtmlElement, isTrue, reason: 'x-c is HtmlElement'); | 64 expect(c is HtmlElement, isTrue, reason: 'x-c is HtmlElement'); |
68 expect(c is XCWrapper, isFalse, reason: 'x-c should not be upgraded yet'); | 65 expect(c is XCWrapper, isFalse, reason: 'x-c should not be upgraded yet'); |
69 expect(_readX(c), null, reason: 'x-c has not been registered in JS yet'); | 66 expect(_readX(c), null, reason: 'x-c has not been registered in JS yet'); |
70 }); | 67 }, skip: 'https://github.com/dart-lang/web-components/issues/38'); |
71 | 68 |
72 test('anything created after registering Dart type is upgraded', () { | 69 test('anything created after registering Dart type is upgraded', () { |
73 context.callMethod('addA'); | 70 context.callMethod('addA'); |
74 var list = document.querySelectorAll('x-a'); | 71 var list = document.querySelectorAll('x-a'); |
75 expect(list.length, 2); | 72 expect(list.length, 2); |
76 var a = list[1]; | 73 var a = list[1]; |
77 expect(a is HtmlElement, isTrue, reason: 'x-a is HtmlElement'); | 74 expect(a is HtmlElement, isTrue, reason: 'x-a is HtmlElement'); |
78 expect(a is XAWrapper, isTrue, reason: 'x-a is upgraded to XAWrapper'); | 75 expect(a is XAWrapper, isTrue, reason: 'x-a is upgraded to XAWrapper'); |
79 expect(a.x, 3); | 76 expect(a.x, 3); |
80 expect(a.wrapperCount, 2); | 77 expect((a as XAWrapper).wrapperCount, 2); |
81 | 78 |
82 context.callMethod('addB'); | 79 context.callMethod('addB'); |
83 list = document.querySelectorAll('[is=x-b]'); | 80 list = document.querySelectorAll('[is=x-b]'); |
84 expect(list.length, 2); | 81 expect(list.length, 2); |
85 var b = list[1]; | 82 var b = list[1]; |
86 expect(b is DivElement, isTrue, reason: 'x-b is DivElement'); | 83 expect(b is DivElement, isTrue, reason: 'x-b is DivElement'); |
87 expect(b is XBWrapper, isTrue, reason: 'x-b is upgraded to XBWrapper'); | 84 expect(b is XBWrapper, isTrue, reason: 'x-b is upgraded to XBWrapper'); |
88 expect(b.x, 4); | 85 expect(b.x, 4); |
89 expect(b.wrapperCount, 3); | 86 expect((b as XBWrapper).wrapperCount, 3); |
90 | 87 |
91 // New instances of x-d should be upgraded regardless. | 88 // New instances of x-d should be upgraded regardless. |
92 context.callMethod('addD'); | 89 context.callMethod('addD'); |
93 list = document.querySelectorAll('x-d'); | 90 list = document.querySelectorAll('x-d'); |
94 expect(list.length, 2); | 91 expect(list.length, 2); |
95 var d = list[1]; | 92 var d = list[1]; |
96 expect(d is HtmlElement, isTrue, reason: 'x-d is HtmlElement'); | 93 expect(d is HtmlElement, isTrue, reason: 'x-d is HtmlElement'); |
97 expect(d is XDWrapper, isTrue, reason: 'x-d is upgraded to XDWrapper'); | 94 expect(d is XDWrapper, isTrue, reason: 'x-d is upgraded to XDWrapper'); |
98 expect(d.x, 5); | 95 expect(d.x, 5); |
99 expect(d.wrapperCount, 4); | 96 expect((d as XDWrapper).wrapperCount, 4); |
100 }); | 97 }, skip: 'https://github.com/dart-lang/web-components/issues/38'); |
101 | 98 |
102 test('events seen if Dart type is registered before registerElement', () { | 99 test('events seen if Dart type is registered before registerElement', () { |
103 var c = document.querySelector('x-c'); | 100 var c = document.querySelector('x-c'); |
104 expect(c is XCWrapper, isFalse); | 101 expect(c is XCWrapper, isFalse); |
105 expect(_readX(c), null, reason: 'x-c has not been registered in JS yet'); | 102 expect(_readX(c), null, reason: 'x-c has not been registered in JS yet'); |
106 | 103 |
107 context.callMethod('registerC'); | 104 context.callMethod('registerC'); |
108 c = document.querySelector('x-c'); | 105 c = document.querySelector('x-c'); |
109 expect(c is XCWrapper, isTrue); | 106 expect(c is XCWrapper, isTrue); |
110 expect(c.x, 6); | 107 expect(c.x, 6); |
111 expect(c.wrapperCount, 5); | 108 expect((c as XCWrapper).wrapperCount, 5); |
112 | 109 |
113 context.callMethod('addC'); | 110 context.callMethod('addC'); |
114 var list = document.querySelectorAll('x-c'); | 111 var list = document.querySelectorAll('x-c'); |
115 expect(list.length, 2); | 112 expect(list.length, 2); |
116 expect(list[0], c); | 113 expect(list[0], c); |
117 c = list[1]; | 114 c = list[1]; |
118 expect(c is HtmlElement, isTrue, reason: 'x-c is HtmlElement'); | 115 expect(c is HtmlElement, isTrue, reason: 'x-c is HtmlElement'); |
119 expect(c is XCWrapper, isTrue, reason: 'x-c is upgraded to XCWrapper'); | 116 expect(c is XCWrapper, isTrue, reason: 'x-c is upgraded to XCWrapper'); |
120 expect(c.x, 7); | 117 expect(c.x, 7); |
121 expect(c.wrapperCount, 6); | 118 expect((c as XCWrapper).wrapperCount, 6); |
122 }); | 119 }, skip: 'https://github.com/dart-lang/web-components/issues/38'); |
123 | 120 |
124 test('element can extend another element', () { | 121 test('element can extend another element', () { |
125 registerDartType('x-e', XEWrapper); | 122 registerDartType('x-e', XEWrapper); |
126 context.callMethod('addE'); | 123 context.callMethod('addE'); |
127 | 124 |
128 var e = document.querySelector('x-e'); | 125 var e = document.querySelector('x-e'); |
129 expect(e is XEWrapper, isTrue); | 126 expect(e is XEWrapper, isTrue); |
130 expect(e.x, 8); | 127 expect(e.x, 8); |
131 expect(e.y, 9); | 128 expect(e.y, 9); |
132 }); | 129 }, skip: 'https://github.com/dart-lang/web-components/issues/38'); |
133 } | 130 } |
| 131 |
134 int _count = 0; | 132 int _count = 0; |
135 | 133 |
136 abstract class Wrapper { | 134 abstract class Wrapper { |
137 int wrapperCount = _count++; | 135 int wrapperCount = _count++; |
138 int get x => _readX(this); | 136 int get x => _readX(this); |
139 } | 137 } |
140 | 138 |
141 _readX(e) => new JsObject.fromBrowserObject(e)['x']; | 139 _readX(e) => new JsObject.fromBrowserObject(e)['x']; |
142 | 140 |
143 class XAWrapper extends HtmlElement with Wrapper { | 141 class XAWrapper extends HtmlElement with Wrapper { |
(...skipping 10 matching lines...) Expand all Loading... |
154 | 152 |
155 class XDWrapper extends HtmlElement with Wrapper { | 153 class XDWrapper extends HtmlElement with Wrapper { |
156 XDWrapper.created() : super.created(); | 154 XDWrapper.created() : super.created(); |
157 } | 155 } |
158 | 156 |
159 class XEWrapper extends HtmlElement with Wrapper { | 157 class XEWrapper extends HtmlElement with Wrapper { |
160 XEWrapper.created() : super.created(); | 158 XEWrapper.created() : super.created(); |
161 | 159 |
162 int get y => new JsObject.fromBrowserObject(this)['y']; | 160 int get y => new JsObject.fromBrowserObject(this)['y']; |
163 } | 161 } |
OLD | NEW |