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