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