1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#!/usr/bin/env python
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import polymer
import os
import shutil
import tempfile
import unittest
_HERE_DIR = os.path.dirname(__file__)
class PolymerModulizerTest(unittest.TestCase):
def setUp(self):
self._out_folder = None
def tearDown(self):
if self._out_folder:
shutil.rmtree(self._out_folder)
def _read_out_file(self, file_name):
assert self._out_folder
return open(os.path.join(self._out_folder, file_name), 'rb').read()
def _run_test(self, html_type, html_file, js_file,
js_out_file, js_file_expected):
assert not self._out_folder
self._out_folder = tempfile.mkdtemp(dir=_HERE_DIR)
polymer.main([
'--in_folder', os.path.join(_HERE_DIR, 'tests'),
'--out_folder', self._out_folder,
'--js_file', js_file,
'--html_file', html_file,
'--html_type', html_type,
'--namespace_rewrites',
'Polymer.PaperRippleBehavior|PaperRippleBehavior',
'--ignore_imports',
'ui/webui/resources/html/ignore_me.html',
'--auto_imports',
'ui/webui/resources/html/polymer.html|Polymer,html',
'third_party/polymer/v1_0/components-chromium/paper-behaviors/paper-ripple-behavior.html|PaperRippleBehavior',
])
actual_js = self._read_out_file(js_out_file)
expected_js = open(os.path.join(
_HERE_DIR, 'tests', js_file_expected), 'rb').read()
self.assertEquals(expected_js, actual_js)
# Test case where HTML is extracted from a Polymer2 <dom-module>.
def testDomModule(self):
self._run_test(
'dom-module', 'dom_module.html', 'dom_module.js',
'dom_module.m.js', 'dom_module_expected.js')
# Test case where HTML is extracted from a Polymer2 <dom-module> that is
# wrapped in an IIFE function.
def testDomModuleIife(self):
self._run_test(
'dom-module', 'dom_module.html', 'dom_module_iife.js',
'dom_module_iife.m.js', 'dom_module_iife_expected.js')
# Test case where HTML is extracted from a Polymer2 <dom-module> that is
# wrapped in an arrow IIFE function.
def testDomModuleIifeArrow(self):
self._run_test(
'dom-module', 'dom_module.html', 'dom_module_iife_arrow.js',
'dom_module_iife_arrow.m.js', 'dom_module_iife_expected.js')
# Test case where HTML is extracted from a Polymer2 <dom-module> that also
# has a 'cr.define()' in its JS file.
def testDomModuleWithDefine(self):
self._run_test(
'dom-module', 'dom_module.html', 'dom_module_with_define.js',
'dom_module_with_define.m.js', 'dom_module_with_define_expected.js')
# Test case where HTML is extracted from a Polymer2 <dom-module> that has
# ignore annotations.
def testDomModuleWithIgnore(self):
self._run_test('dom-module', 'dom_module.html', 'dom_module_with_ignore.js',
'dom_module_with_ignore.m.js',
'dom_module_with_ignore_expected.js')
# Test case where HTML is extracted from a Polymer2 <dom-module> that also
# uses <if expr> for imports.
def testDomModuleWithConditionalImport(self):
self._run_test('dom-module', 'dom_module_with_if_expr.html',
'dom_module.js', 'dom_module.m.js',
'dom_module_with_if_expr_expected.js')
# Test case where HTML is extracted from a Polymer2 style module.
def testStyleModule(self):
self._run_test(
'style-module', 'style_module.html', 'style_module.m.js',
'style_module.m.js', 'style_module_expected.js')
return
# Test case where HTML is extracted from a Polymer2 <custom-style>.
def testCustomStyle(self):
self._run_test(
'custom-style', 'custom_style.html', 'custom_style.m.js',
'custom_style.m.js', 'custom_style_expected.js')
# Test case where HTML is extracted from a Polymer2 iron-iconset-svg file.
def testIronIconset(self):
self._run_test(
'iron-iconset', 'iron_iconset.html', 'iron_iconset.m.js',
'iron_iconset.m.js', 'iron_iconset_expected.js')
# Test case where the provided HTML is already in the form needed by Polymer3.
def testV3Ready(self):
self._run_test(
'v3-ready', 'v3_ready.html', 'v3_ready.js',
'v3_ready.js', 'v3_ready_expected.js')
# Test the |Dependency| class directly, which is responsible for converting
# HTML imports to JS imports.
def testImportsHtmlToJs(self):
_HERE = os.path.abspath(os.path.dirname(__file__))
_ROOT = os.path.normpath(os.path.join(_HERE, '..', '..'))
src = os.path.join(_ROOT, 'ui/webui/resources/foo/bar/baz.html')
auto_imports = {
'ui/webui/resources/html/polymer.html': ['Polymer', 'html'],
'ui/webui/resources/html/foo.html': ['Foo'],
}
def assert_html_to_js(html, expected_js):
actual_js = polymer.Dependency(src, html).to_js_import(auto_imports)
self.assertEquals(expected_js, actual_js)
cases = [
# Relative paths cases.
# Case where relative path to polymer.html is used.
['../../html/polymer.html',
'import {Polymer, html} from \'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js\';'
],
# Case where relative path to file in the same folder is used.
['foo.html', 'import \'./foo.m.js\';'],
# Case where relative path to file in the same subtree is used.
['path/to/subfolder/foo.html', 'import \'./path/to/subfolder/foo.m.js\';'],
# Case where relative path to file in ui/webui/resources/html/ is used.
['../../html/foo.html', 'import {Foo} from \'../../js/foo.m.js\';'],
# chrome:// paths cases.
# Case where absolute path to a Polymer UI element is used.
['chrome://resources/polymer/v1_0/path/to/folder/foo.html',
'import \'chrome://resources/polymer/v3_0/path/to/folder/foo.js\';'
],
# Case where chrome:// path to polymer.html is used.
['chrome://resources/html/polymer.html',
'import {Polymer, html} from \'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js\';'
],
# Case where chrome://resources/html/ path to something other than
# polymer.html is used.
['chrome://resources/html/bar.html',
'import \'chrome://resources/js/bar.m.js\';'
],
]
for [html, js_expected] in cases:
assert_html_to_js(html, js_expected)
if __name__ == '__main__':
unittest.main()
|