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 polymer.test.build.all_phases_test; | 5 library polymer.test.build.all_phases_test; |
6 | 6 |
7 import 'package:code_transformers/tests.dart' show testingDartSdkDirectory; | 7 import 'package:code_transformers/tests.dart' show testingDartSdkDirectory; |
8 import 'package:polymer/src/build/common.dart'; | 8 import 'package:polymer/src/build/common.dart'; |
| 9 import 'package:polymer/src/build/import_inliner.dart' show COMPONENT_WARNING; |
| 10 import 'package:polymer/src/build/linter.dart' show USE_POLYMER_HTML; |
9 import 'package:polymer/src/build/script_compactor.dart' show MAIN_HEADER; | 11 import 'package:polymer/src/build/script_compactor.dart' show MAIN_HEADER; |
10 import 'package:polymer/transformer.dart'; | 12 import 'package:polymer/transformer.dart'; |
11 import 'package:smoke/codegen/generator.dart' show DEFAULT_IMPORTS; | 13 import 'package:smoke/codegen/generator.dart' show DEFAULT_IMPORTS; |
12 import 'package:unittest/compact_vm_config.dart'; | 14 import 'package:unittest/compact_vm_config.dart'; |
13 | 15 |
14 import 'common.dart'; | 16 import 'common.dart'; |
15 | 17 |
16 void main() { | 18 void main() { |
17 useCompactVMConfiguration(); | 19 useCompactVMConfiguration(); |
18 var phases = createDeployPhases(new TransformOptions(), | 20 var phases = createDeployPhases(new TransformOptions(), |
19 sdkDir: testingDartSdkDirectory); | 21 sdkDir: testingDartSdkDirectory); |
20 | 22 |
21 testPhases('no changes', phases, { | 23 testPhases('no changes', phases, { |
22 'a|web/test.html': '<!DOCTYPE html><html></html>', | 24 'a|web/test.html': '<!DOCTYPE html><html></html>', |
23 }, {}, [ | 25 }, {}, [ |
24 'error: To run a polymer application, you need to call "initPolymer". You' | 26 'warning: $USE_POLYMER_HTML' |
25 ' can either include a generic script tag that does this for you:\'<' | |
26 'script type="application/dart">export "package:polymer/init.dart";' | |
27 '</script>\' or add your own script tag and call that function. Make sure' | |
28 ' the script tag is placed after all HTML imports.' | |
29 ]); | 27 ]); |
30 | 28 |
31 testPhases('observable changes', phases, { | 29 testPhases('observable changes', phases, { |
32 'a|web/test.dart': _sampleObservable('A', 'foo'), | 30 'a|web/test.dart': _sampleInput('A', 'foo'), |
33 'a|web/test2.dart': _sampleObservableOutput('B', 'bar'), | 31 'a|web/test2.dart': _sampleOutput('B', 'bar'), |
34 }, { | 32 }, { |
35 'a|web/test.dart': _sampleObservableOutput('A', 'foo'), | 33 'a|web/test.dart': _sampleOutput('A', 'foo'), |
36 'a|web/test2.dart': _sampleObservableOutput('B', 'bar'), | 34 'a|web/test2.dart': _sampleOutput('B', 'bar'), |
37 }); | 35 }); |
38 | 36 |
39 testPhases('single script', phases, { | 37 testPhases('single script', phases, { |
40 'a|web/test.html': | 38 'a|web/test.html': |
41 '<!DOCTYPE html><html><head>' | 39 '<!DOCTYPE html><html><head>' |
42 '<script type="application/dart" src="a.dart"></script>', | 40 '<link rel="import" href="packages/polymer/polymer.html">' |
43 'a|web/a.dart': _sampleObservable('A', 'foo'), | 41 '<script type="application/dart;component=1" src="a.dart"></script>', |
| 42 'a|web/a.dart': _sampleInput('A', 'foo'), |
44 }, { | 43 }, { |
45 'a|web/test.html': | 44 'a|web/test.html': |
46 '<!DOCTYPE html><html><head>' | 45 '<!DOCTYPE html><html><head>' |
47 '$WEB_COMPONENTS_TAG' | 46 '$WEB_COMPONENTS_TAG' |
48 '$INTEROP_TAG' | 47 '</head><body>' |
49 '<script src="test.html_bootstrap.dart.js"></script>' | 48 '<script src="test.html_bootstrap.dart.js"></script>' |
50 '</head><body></body></html>', | 49 '</body></html>', |
51 | 50 |
52 'a|web/test.html_bootstrap.dart': | 51 'a|web/test.html_bootstrap.dart': |
53 '''$MAIN_HEADER | 52 '''$MAIN_HEADER |
54 import 'a.dart' as i0; | 53 import 'a.dart' as i0; |
55 ${DEFAULT_IMPORTS.join('\n')} | 54 ${DEFAULT_IMPORTS.join('\n')} |
| 55 import 'a.dart' as smoke_0; |
| 56 import 'package:polymer/polymer.dart' as smoke_1; |
56 | 57 |
57 void main() { | 58 void main() { |
58 useGeneratedCode(new StaticConfiguration( | 59 useGeneratedCode(new StaticConfiguration( |
59 checkedMode: false)); | 60 checkedMode: false, |
60 configureForDeployment([ | 61 parents: { |
| 62 smoke_0.XA: smoke_1.PolymerElement, |
| 63 }, |
| 64 declarations: { |
| 65 smoke_0.XA: const {}, |
| 66 })); |
| 67 startPolymer([ |
| 68 i0.m_foo, |
| 69 () => Polymer.register('x-A', i0.XA), |
61 ]); | 70 ]); |
62 i0.main(); | |
63 } | 71 } |
64 '''.replaceAll('\n ', '\n'), | 72 '''.replaceAll('\n ', '\n'), |
65 'a|web/a.dart': _sampleObservableOutput('A', 'foo'), | 73 'a|web/a.dart': _sampleOutput('A', 'foo'), |
66 }); | 74 }); |
67 | 75 |
68 testPhases('single inline script', phases, { | 76 testPhases('single inline script', phases, { |
69 'a|web/test.html': | 77 'a|web/test.html': |
70 '<!DOCTYPE html><html><head>' | 78 '<!DOCTYPE html><html><head>' |
71 '<script type="application/dart">' | 79 '<link rel="import" href="packages/polymer/polymer.html">' |
72 '${_sampleObservable("B", "bar")}</script>', | 80 '<script type="application/dart;component=1">' |
| 81 '${_sampleInput("B", "bar")}</script>', |
73 }, { | 82 }, { |
74 'a|web/test.html': | 83 'a|web/test.html': |
75 '<!DOCTYPE html><html><head>' | 84 '<!DOCTYPE html><html><head>' |
76 '$WEB_COMPONENTS_TAG' | 85 '$WEB_COMPONENTS_TAG' |
77 '$INTEROP_TAG' | |
78 '</head><body>' | 86 '</head><body>' |
79 '<script src="test.html_bootstrap.dart.js"></script>' | 87 '<script src="test.html_bootstrap.dart.js"></script>' |
80 '</body></html>', | 88 '</body></html>', |
81 | 89 |
82 'a|web/test.html_bootstrap.dart': | 90 'a|web/test.html_bootstrap.dart': |
83 '''$MAIN_HEADER | 91 '''$MAIN_HEADER |
84 import 'test.html.0.dart' as i0; | 92 import 'test.html.0.dart' as i0; |
85 ${DEFAULT_IMPORTS.join('\n')} | 93 ${DEFAULT_IMPORTS.join('\n')} |
| 94 import 'test.html.0.dart' as smoke_0; |
| 95 import 'package:polymer/polymer.dart' as smoke_1; |
86 | 96 |
87 void main() { | 97 void main() { |
88 useGeneratedCode(new StaticConfiguration( | 98 useGeneratedCode(new StaticConfiguration( |
89 checkedMode: false)); | 99 checkedMode: false, |
90 configureForDeployment([ | 100 parents: { |
| 101 smoke_0.XB: smoke_1.PolymerElement, |
| 102 }, |
| 103 declarations: { |
| 104 smoke_0.XB: const {}, |
| 105 })); |
| 106 startPolymer([ |
| 107 i0.m_bar, |
| 108 () => Polymer.register('x-B', i0.XB), |
91 ]); | 109 ]); |
92 i0.main(); | |
93 } | 110 } |
94 '''.replaceAll('\n ', '\n'), | 111 '''.replaceAll('\n ', '\n'), |
95 'a|web/test.html.0.dart': | 112 'a|web/test.html.0.dart': |
96 _sampleObservableOutput("B", "bar"), | 113 _sampleOutput("B", "bar"), |
97 }); | 114 }); |
98 | 115 |
99 const onlyOne = 'warning: Only one "application/dart" script tag per document' | 116 testPhases('several application scripts', phases, { |
100 ' is allowed.'; | |
101 const moreNotSupported = | |
102 'warning: more than one Dart script per HTML document is not supported. ' | |
103 'Script will be ignored.'; | |
104 | |
105 testPhases('several scripts', phases, { | |
106 'a|web/test.html': | 117 'a|web/test.html': |
107 '<!DOCTYPE html><html><head>' | 118 '<!DOCTYPE html><html><head>' |
108 '<script type="application/dart" src="a.dart"></script>' | 119 '<link rel="import" href="packages/polymer/polymer.html">' |
109 // TODO(sigmund): provide a way to see logging warnings and errors. | 120 '<script type="application/dart;component=1" src="a.dart"></script>' |
110 // For example, these extra tags produce warnings and are then removed | |
111 // by the transformers. The test below checks that the output looks | |
112 // correct, but we should also validate the messages logged. | |
113 '<script type="application/dart">' | 121 '<script type="application/dart">' |
114 '${_sampleObservable("B", "bar")}</script>' | 122 '${_sampleInput("B", "bar")}</script>' |
115 '</head><body><div>' | 123 '</head><body><div>' |
116 '<script type="application/dart">' | 124 '<script type="application/dart">' |
117 '${_sampleObservable("C", "car")}</script>' | 125 '${_sampleInput("C", "car")}</script>' |
118 '</div>' | 126 '</div>' |
119 '<script type="application/dart" src="d.dart"></script>', | 127 '<script type="application/dart" src="d.dart"></script>', |
120 'a|web/a.dart': _sampleObservable('A', 'foo'), | 128 'a|web/a.dart': _sampleInput('A', 'foo'), |
121 }, { | 129 }, { |
122 'a|web/test.html': | 130 'a|web/test.html': |
123 '<!DOCTYPE html><html><head>' | 131 '<!DOCTYPE html><html><head>' |
124 '$WEB_COMPONENTS_TAG' | 132 '$WEB_COMPONENTS_TAG' |
125 '$INTEROP_TAG' | |
126 '</head><body>' | 133 '</head><body>' |
| 134 '<script src="test.html.0.dart.js"></script>' |
| 135 '<div>' |
| 136 '<script src="test.html.1.dart.js"></script>' |
| 137 '</div>' |
| 138 '<script src="d.dart.js"></script>' |
127 '<script src="test.html_bootstrap.dart.js"></script>' | 139 '<script src="test.html_bootstrap.dart.js"></script>' |
128 '<div></div>' | |
129 '</body></html>', | 140 '</body></html>', |
130 | 141 |
131 'a|web/test.html_bootstrap.dart': | 142 'a|web/test.html_bootstrap.dart': |
132 '''$MAIN_HEADER | 143 '''$MAIN_HEADER |
133 import 'a.dart' as i0; | 144 import 'a.dart' as i0; |
134 ${DEFAULT_IMPORTS.join('\n')} | 145 ${DEFAULT_IMPORTS.join('\n')} |
| 146 import 'a.dart' as smoke_0; |
| 147 import 'package:polymer/polymer.dart' as smoke_1; |
135 | 148 |
136 void main() { | 149 void main() { |
137 useGeneratedCode(new StaticConfiguration( | 150 useGeneratedCode(new StaticConfiguration( |
138 checkedMode: false)); | 151 checkedMode: false, |
139 configureForDeployment([ | 152 parents: { |
| 153 smoke_0.XA: smoke_1.PolymerElement, |
| 154 }, |
| 155 declarations: { |
| 156 smoke_0.XA: const {}, |
| 157 })); |
| 158 startPolymer([ |
| 159 i0.m_foo, |
| 160 () => Polymer.register('x-A', i0.XA), |
140 ]); | 161 ]); |
141 i0.main(); | |
142 } | 162 } |
143 '''.replaceAll('\n ', '\n'), | 163 '''.replaceAll('\n ', '\n'), |
144 'a|web/a.dart': _sampleObservableOutput('A', 'foo'), | 164 'a|web/a.dart': _sampleOutput('A', 'foo'), |
145 }, [ | 165 }, [ |
146 // These should not be emitted multiple times. See: | 166 // These should not be emitted multiple times. See: |
147 // https://code.google.com/p/dart/issues/detail?id=17197 | 167 // https://code.google.com/p/dart/issues/detail?id=17197 |
148 '$onlyOne (web/test.html 0 81)', | 168 'warning: $COMPONENT_WARNING (web/test.html 14 27)', |
149 '$onlyOne (web/test.html 8 27)', | 169 'warning: $COMPONENT_WARNING (web/test.html 28 15)' |
150 '$onlyOne (web/test.html 16 15)', | |
151 '$moreNotSupported (web/test.html 0 81)', | |
152 '$moreNotSupported (web/test.html 8 27)', | |
153 '$moreNotSupported (web/test.html 16 15)' | |
154 ]); | 170 ]); |
155 | 171 |
| 172 testPhases('several component scripts', phases, { |
| 173 'a|web/test.html': |
| 174 '<!DOCTYPE html><html><head>' |
| 175 '<link rel="import" href="packages/polymer/polymer.html">' |
| 176 '<script type="application/dart;component=1" src="a.dart"></script>' |
| 177 '<script type="application/dart;component=1">' |
| 178 '${_sampleInput("B", "bar")}</script>' |
| 179 '</head><body><div>' |
| 180 '<script type="application/dart;component=1">' |
| 181 '${_sampleInput("C", "car")}</script>' |
| 182 '</div>', |
| 183 'a|web/a.dart': _sampleInput('A', 'foo'), |
| 184 }, { |
| 185 'a|web/test.html': |
| 186 '<!DOCTYPE html><html><head>' |
| 187 '$WEB_COMPONENTS_TAG' |
| 188 '</head><body>' |
| 189 '<div></div>' |
| 190 '<script src="test.html_bootstrap.dart.js"></script>' |
| 191 '</body></html>', |
| 192 |
| 193 'a|web/test.html_bootstrap.dart': |
| 194 '''$MAIN_HEADER |
| 195 import 'a.dart' as i0; |
| 196 import 'test.html.0.dart' as i1; |
| 197 import 'test.html.1.dart' as i2; |
| 198 ${DEFAULT_IMPORTS.join('\n')} |
| 199 import 'a.dart' as smoke_0; |
| 200 import 'package:polymer/polymer.dart' as smoke_1; |
| 201 import 'test.html.0.dart' as smoke_2; |
| 202 import 'test.html.1.dart' as smoke_3; |
| 203 |
| 204 void main() { |
| 205 useGeneratedCode(new StaticConfiguration( |
| 206 checkedMode: false, |
| 207 parents: { |
| 208 smoke_0.XA: smoke_1.PolymerElement, |
| 209 smoke_2.XB: smoke_1.PolymerElement, |
| 210 smoke_3.XC: smoke_1.PolymerElement, |
| 211 }, |
| 212 declarations: { |
| 213 smoke_0.XA: const {}, |
| 214 smoke_2.XB: const {}, |
| 215 smoke_3.XC: const {}, |
| 216 })); |
| 217 startPolymer([ |
| 218 i0.m_foo, |
| 219 () => Polymer.register('x-A', i0.XA), |
| 220 i1.m_bar, |
| 221 () => Polymer.register('x-B', i1.XB), |
| 222 i2.m_car, |
| 223 () => Polymer.register('x-C', i2.XC), |
| 224 ]); |
| 225 } |
| 226 '''.replaceAll('\n ', '\n'), |
| 227 'a|web/a.dart': _sampleOutput('A', 'foo'), |
| 228 }, []); |
| 229 |
156 testPhases('with imports', phases, { | 230 testPhases('with imports', phases, { |
157 'a|web/index.html': | 231 'a|web/index.html': |
158 '<!DOCTYPE html><html><head>' | 232 '<!DOCTYPE html><html><head>' |
| 233 '<link rel="import" href="packages/polymer/polymer.html">' |
159 '<link rel="import" href="test2.html">' | 234 '<link rel="import" href="test2.html">' |
160 '</head><body>' | 235 '</head><body>' |
161 '<script type="application/dart" src="b.dart"></script>', | 236 '<script type="application/dart;component=1" src="b.dart"></script>', |
162 'a|web/b.dart': _sampleObservable('B', 'bar'), | 237 'a|web/b.dart': _sampleInput('B', 'bar'), |
163 'a|web/test2.html': | 238 'a|web/test2.html': |
164 '<!DOCTYPE html><html><head></head><body>' | 239 '<!DOCTYPE html><html><head></head><body>' |
165 '<polymer-element name="x-a">1' | 240 '<polymer-element name="x-a">1' |
166 '<script type="application/dart">' | 241 '<script type="application/dart;component=1">' |
167 '${_sampleObservable("A", "foo")}</script>' | 242 '${_sampleInput("A", "foo")}</script>' |
168 '</polymer-element></html>', | 243 '</polymer-element></html>', |
169 }, { | 244 }, { |
170 'a|web/index.html': | 245 'a|web/index.html': |
171 '<!DOCTYPE html><html><head>' | 246 '<!DOCTYPE html><html><head>' |
172 '$WEB_COMPONENTS_TAG' | 247 '$WEB_COMPONENTS_TAG' |
173 '$INTEROP_TAG' | |
174 '</head><body><polymer-element name="x-a">1</polymer-element>' | 248 '</head><body><polymer-element name="x-a">1</polymer-element>' |
175 '<script src="index.html_bootstrap.dart.js"></script>' | 249 '<script src="index.html_bootstrap.dart.js"></script>' |
176 '</body></html>', | 250 '</body></html>', |
177 'a|web/index.html_bootstrap.dart': | 251 'a|web/index.html_bootstrap.dart': |
178 '''$MAIN_HEADER | 252 '''$MAIN_HEADER |
179 import 'index.html.0.dart' as i0; | 253 import 'index.html.0.dart' as i0; |
180 import 'b.dart' as i1; | 254 import 'b.dart' as i1; |
181 ${DEFAULT_IMPORTS.join('\n')} | 255 ${DEFAULT_IMPORTS.join('\n')} |
| 256 import 'index.html.0.dart' as smoke_0; |
| 257 import 'package:polymer/polymer.dart' as smoke_1; |
| 258 import 'b.dart' as smoke_2; |
182 | 259 |
183 void main() { | 260 void main() { |
184 useGeneratedCode(new StaticConfiguration( | 261 useGeneratedCode(new StaticConfiguration( |
185 checkedMode: false)); | 262 checkedMode: false, |
186 configureForDeployment([ | 263 parents: { |
| 264 smoke_2.XB: smoke_1.PolymerElement, |
| 265 smoke_0.XA: smoke_1.PolymerElement, |
| 266 }, |
| 267 declarations: { |
| 268 smoke_2.XB: const {}, |
| 269 smoke_0.XA: const {}, |
| 270 })); |
| 271 startPolymer([ |
| 272 i0.m_foo, |
| 273 () => Polymer.register('x-A', i0.XA), |
| 274 i1.m_bar, |
| 275 () => Polymer.register('x-B', i1.XB), |
187 ]); | 276 ]); |
188 i1.main(); | |
189 } | 277 } |
190 '''.replaceAll('\n ', '\n'), | 278 '''.replaceAll('\n ', '\n'), |
191 'a|web/index.html.0.dart': _sampleObservableOutput("A", "foo"), | 279 'a|web/index.html.0.dart': _sampleOutput("A", "foo"), |
192 'a|web/b.dart': _sampleObservableOutput('B', 'bar'), | 280 'a|web/b.dart': _sampleOutput('B', 'bar'), |
193 }); | 281 }); |
194 } | 282 } |
195 | 283 |
196 String _sampleObservable(String className, String fieldName) => ''' | 284 String _sampleInput(String className, String fieldName) => ''' |
197 library ${className}_$fieldName; | 285 library ${className}_$fieldName; |
198 import 'package:observe/observe.dart'; | 286 import 'package:observe/observe.dart'; |
199 export 'package:polymer/init.dart'; | 287 import 'package:polymer/polymer.dart'; |
200 | 288 |
201 class $className extends Observable { | 289 class $className extends Observable { |
202 @observable int $fieldName; | 290 @observable int $fieldName; |
203 $className(this.$fieldName); | 291 $className(this.$fieldName); |
204 } | 292 } |
| 293 |
| 294 @CustomTag('x-$className') |
| 295 class X${className} extends PolymerElement { |
| 296 X${className}.created() : super.created(); |
| 297 } |
| 298 @initMethod m_$fieldName() {} |
205 '''; | 299 '''; |
206 | 300 |
207 String _sampleObservableOutput(String className, String field, | 301 |
208 {bool includeMain: false}) => | 302 String _sampleOutput(String className, String fieldName) { |
209 "library ${className}_$field;\n" | 303 var fieldReplacement = '@reflectable @observable ' |
210 "import 'package:observe/observe.dart';\n" | 304 'int get $fieldName => __\$$fieldName; ' |
211 "export 'package:polymer/init.dart';\n\n" | 305 'int __\$$fieldName; ' |
212 "class $className extends ChangeNotifier {\n" | 306 '@reflectable set $fieldName(int value) { ' |
213 " @reflectable @observable int get $field => __\$$field; " | 307 '__\$$fieldName = notifyPropertyChange(#$fieldName, ' |
214 "int __\$$field; " | 308 '__\$$fieldName, value); }'; |
215 "@reflectable set $field(int value) { " | 309 return ''' |
216 "__\$$field = notifyPropertyChange(#$field, __\$$field, value); " | 310 library ${className}_$fieldName; |
217 "}\n" | 311 import 'package:observe/observe.dart'; |
218 " $className($field) : __\$$field = $field;\n" | 312 import 'package:polymer/polymer.dart'; |
219 "}\n"; | 313 |
| 314 class $className extends ChangeNotifier { |
| 315 $fieldReplacement |
| 316 $className($fieldName) : __\$$fieldName = $fieldName; |
| 317 } |
| 318 |
| 319 @CustomTag('x-$className') |
| 320 class X${className} extends PolymerElement { |
| 321 X${className}.created() : super.created(); |
| 322 } |
| 323 @initMethod m_$fieldName() {} |
| 324 '''; |
| 325 } |
OLD | NEW |