forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polymer_test.py
executable file
·168 lines (144 loc) · 3.62 KB
/
polymer_test.py
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
#!/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 HtmlToJsTest(unittest.TestCase):
def setUp(self):
self._out_folder = None
self._tmp_dirs = []
self._tmp_src_dir = None
def tearDown(self):
for tmp_dir in self._tmp_dirs:
shutil.rmtree(tmp_dir)
def _write_file_to_src_dir(self, file_path, file_contents):
if not self._tmp_src_dir:
self._tmp_src_dir = self._create_tmp_dir()
file_path_normalized = os.path.normpath(os.path.join(self._tmp_src_dir,
file_path))
file_dir = os.path.dirname(file_path_normalized)
if not os.path.exists(file_dir):
os.makedirs(file_dir)
with open(file_path_normalized, 'w') as tmp_file:
tmp_file.write(file_contents)
def _create_tmp_dir(self):
tmp_dir = tempfile.mkdtemp(dir=_HERE_DIR)
self._tmp_dirs.append(tmp_dir)
return tmp_dir
def _read_out_file(self, file_name):
assert self._out_folder
return open(os.path.join(self._out_folder, file_name), 'r').read()
def _run_html_to_js(self, js_file, html_file, html_type):
assert not self._out_folder
self._out_folder = self._create_tmp_dir()
polymer.main([
'--in_folder', self._tmp_src_dir,
'--out_folder', self._out_folder,
'--js_file', js_file,
'--html_file', html_file,
'--html_type', html_type,
])
def _run_test_(self, html_type, src_html, src_js, expected_js):
self._write_file_to_src_dir('foo.html', src_html)
self._write_file_to_src_dir('foo.js', src_js)
self._run_html_to_js('foo.js', 'foo.html', html_type)
actual_js = self._read_out_file('foo.js')
self.assertEquals(expected_js, actual_js)
# Test case where HTML is extracted from a Polymer2 <dom-module>.
def testSuccess_DomModule(self):
self._run_test_('dom-module', '''
<link rel="import" href="../../foo/bar.html">
<link rel="import" href="chrome://resources/foo/bar.html">
<dom-module id="cr-checkbox">
<template>
<style>
div {
font-size: 2rem;
}
</style>
<div>Hello world</div>
</template>
<script src="foo.js"></script>
</dom-module>
''', '''
Polymer({
is: 'cr-foo',
_template: html`
{__html_template__}
`,
});
''', '''
Polymer({
is: 'cr-foo',
_template: html`
<style>
div {
font-size: 2rem;
}
</style>
<div>Hello world</div>
`,
});
''')
# Test case where HTML is extracted from a Polymer2 <custom-style>.
def testSuccess_CustomStyle(self):
self._run_test_('custom-style', '''
<link rel="import" href="../../foo/bar.html">
<link rel="import" href="chrome://resources/foo/bar.html">
<custom-style>
<style>
html {
--foo-bar: 2rem;
}
</style>
</custom-style>
''', '''
$_documentContainer.innerHTML = `
{__html_template__}
`;
''', '''
$_documentContainer.innerHTML = `
<custom-style>
<style>
html {
--foo-bar: 2rem;
}
</style>
</custom-style>
`;
''')
# Test case where the provided HTML is already in the form needed by Polymer3.
def testSuccess_V3Ready(self):
self._run_test_('v3-ready', '''<style>
div {
font-size: 2rem;
}
</style>
<div>Hello world</div>
''', '''
Polymer({
is: 'cr-foo',
_template: html`
{__html_template__}
`,
});
''', '''
Polymer({
is: 'cr-foo',
_template: html`
<style>
div {
font-size: 2rem;
}
</style>
<div>Hello world</div>
`,
});
''')
if __name__ == '__main__':
unittest.main()