OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 '''Unit tests for grit.format.html_inline''' | |
7 | |
8 | |
9 import os | |
10 import re | |
11 import sys | |
12 if __name__ == '__main__': | |
13 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) | |
14 | |
15 import unittest | |
16 | |
17 from grit import util | |
18 from grit.format import html_inline | |
19 | |
20 | |
21 class HtmlInlineUnittest(unittest.TestCase): | |
22 '''Unit tests for HtmlInline.''' | |
23 | |
24 def testGetResourceFilenames(self): | |
25 '''Tests that all included files are returned by GetResourceFilenames.''' | |
26 | |
27 files = { | |
28 'index.html': ''' | |
29 <!DOCTYPE HTML> | |
30 <html> | |
31 <head> | |
32 <link rel="stylesheet" href="test.css"> | |
33 <link rel="stylesheet" | |
34 href="really-long-long-long-long-long-test.css"> | |
35 </head> | |
36 <body> | |
37 <include src="test.html"> | |
38 <include | |
39 src="really-long-long-long-long-long-test-file-omg-so-long.html"> | |
40 <iron-icon src="[[icon]]"></iron-icon><!-- Should be ignored. --> | |
41 <iron-icon src="{{src}}"></iron-icon><!-- Also ignored. --> | |
42 </body> | |
43 </html> | |
44 ''', | |
45 | |
46 'test.html': ''' | |
47 <include src="test2.html"> | |
48 ''', | |
49 | |
50 'really-long-long-long-long-long-test-file-omg-so-long.html': ''' | |
51 <!-- This really long named resource should be included. --> | |
52 ''', | |
53 | |
54 'test2.html': ''' | |
55 <!-- This second level resource should also be included. --> | |
56 ''', | |
57 | |
58 'test.css': ''' | |
59 .image { | |
60 background: url('test.png'); | |
61 } | |
62 ''', | |
63 | |
64 'really-long-long-long-long-long-test.css': ''' | |
65 a:hover { | |
66 font-weight: bold; /* Awesome effect is awesome! */ | |
67 } | |
68 ''', | |
69 | |
70 'test.png': 'PNG DATA', | |
71 } | |
72 | |
73 source_resources = set() | |
74 tmp_dir = util.TempDir(files) | |
75 for filename in files: | |
76 source_resources.add(tmp_dir.GetPath(filename)) | |
77 | |
78 resources = html_inline.GetResourceFilenames(tmp_dir.GetPath('index.html')) | |
79 resources.add(tmp_dir.GetPath('index.html')) | |
80 self.failUnlessEqual(resources, source_resources) | |
81 tmp_dir.CleanUp() | |
82 | |
83 def testUnmatchedEndIfBlock(self): | |
84 '''Tests that an unmatched </if> raises an exception.''' | |
85 | |
86 files = { | |
87 'index.html': ''' | |
88 <!DOCTYPE HTML> | |
89 <html> | |
90 <if expr="lang == 'fr'"> | |
91 bonjour | |
92 </if> | |
93 </if> | |
94 </html> | |
95 ''', | |
96 } | |
97 | |
98 tmp_dir = util.TempDir(files) | |
99 | |
100 with self.assertRaises(Exception) as cm: | |
101 html_inline.GetResourceFilenames(tmp_dir.GetPath('index.html')) | |
102 self.failUnlessEqual(cm.exception.message, 'Unmatched </if>') | |
103 tmp_dir.CleanUp() | |
104 | |
105 def testCompressedJavaScript(self): | |
106 '''Tests that ".src=" doesn't treat as a tag.''' | |
107 | |
108 files = { | |
109 'index.js': ''' | |
110 if(i<j)a.src="hoge.png"; | |
111 ''', | |
112 } | |
113 | |
114 source_resources = set() | |
115 tmp_dir = util.TempDir(files) | |
116 for filename in files: | |
117 source_resources.add(tmp_dir.GetPath(filename)) | |
118 | |
119 resources = html_inline.GetResourceFilenames(tmp_dir.GetPath('index.js')) | |
120 resources.add(tmp_dir.GetPath('index.js')) | |
121 self.failUnlessEqual(resources, source_resources) | |
122 tmp_dir.CleanUp() | |
123 | |
124 def testInlineCSSImports(self): | |
125 '''Tests that @import directives in inlined CSS files are inlined too. | |
126 ''' | |
127 | |
128 files = { | |
129 'index.html': ''' | |
130 <html> | |
131 <head> | |
132 <link rel="stylesheet" href="css/test.css"> | |
133 </head> | |
134 </html> | |
135 ''', | |
136 | |
137 'css/test.css': ''' | |
138 @import url('test2.css'); | |
139 blink { | |
140 display: none; | |
141 } | |
142 ''', | |
143 | |
144 'css/test2.css': ''' | |
145 .image { | |
146 background: url('../images/test.png'); | |
147 } | |
148 '''.strip(), | |
149 | |
150 'images/test.png': 'PNG DATA' | |
151 } | |
152 | |
153 expected_inlined = ''' | |
154 <html> | |
155 <head> | |
156 <style> | |
157 .image { | |
158 background: url('data:image/png;base64,UE5HIERBVEE='); | |
159 } | |
160 blink { | |
161 display: none; | |
162 } | |
163 </style> | |
164 </head> | |
165 </html> | |
166 ''' | |
167 | |
168 source_resources = set() | |
169 tmp_dir = util.TempDir(files) | |
170 for filename in files: | |
171 source_resources.add(tmp_dir.GetPath(util.normpath(filename))) | |
172 | |
173 result = html_inline.DoInline(tmp_dir.GetPath('index.html'), None) | |
174 resources = result.inlined_files | |
175 resources.add(tmp_dir.GetPath('index.html')) | |
176 self.failUnlessEqual(resources, source_resources) | |
177 self.failUnlessEqual(expected_inlined, | |
178 util.FixLineEnd(result.inlined_data, '\n')) | |
179 | |
180 tmp_dir.CleanUp() | |
181 | |
182 def testInlineCSSLinks(self): | |
183 '''Tests that only CSS files referenced via relative URLs are inlined.''' | |
184 | |
185 files = { | |
186 'index.html': ''' | |
187 <html> | |
188 <head> | |
189 <link rel="stylesheet" href="foo.css"> | |
190 <link rel="stylesheet" href="chrome://resources/bar.css"> | |
191 </head> | |
192 </html> | |
193 ''', | |
194 | |
195 'foo.css': ''' | |
196 @import url(chrome://resources/blurp.css); | |
197 blink { | |
198 display: none; | |
199 } | |
200 ''', | |
201 } | |
202 | |
203 expected_inlined = ''' | |
204 <html> | |
205 <head> | |
206 <style> | |
207 @import url(chrome://resources/blurp.css); | |
208 blink { | |
209 display: none; | |
210 } | |
211 </style> | |
212 <link rel="stylesheet" href="chrome://resources/bar.css"> | |
213 </head> | |
214 </html> | |
215 ''' | |
216 | |
217 source_resources = set() | |
218 tmp_dir = util.TempDir(files) | |
219 for filename in files: | |
220 source_resources.add(tmp_dir.GetPath(filename)) | |
221 | |
222 result = html_inline.DoInline(tmp_dir.GetPath('index.html'), None) | |
223 resources = result.inlined_files | |
224 resources.add(tmp_dir.GetPath('index.html')) | |
225 self.failUnlessEqual(resources, source_resources) | |
226 self.failUnlessEqual(expected_inlined, | |
227 util.FixLineEnd(result.inlined_data, '\n')) | |
228 | |
229 def testFilenameVariableExpansion(self): | |
230 '''Tests that variables are expanded in filenames before inlining.''' | |
231 | |
232 files = { | |
233 'index.html': ''' | |
234 <html> | |
235 <head> | |
236 <link rel="stylesheet" href="style[WHICH].css"> | |
237 <script src="script[WHICH].js"></script> | |
238 </head> | |
239 <include src="tmpl[WHICH].html"> | |
240 <img src="img[WHICH].png"> | |
241 </html> | |
242 ''', | |
243 'style1.css': '''h1 {}''', | |
244 'tmpl1.html': '''<h1></h1>''', | |
245 'script1.js': '''console.log('hello');''', | |
246 'img1.png': '''abc''', | |
247 } | |
248 | |
249 expected_inlined = ''' | |
250 <html> | |
251 <head> | |
252 <style>h1 {}</style> | |
253 <script>console.log('hello');</script> | |
254 </head> | |
255 <h1></h1> | |
256 <img src="data:image/png;base64,YWJj"> | |
257 </html> | |
258 ''' | |
259 | |
260 source_resources = set() | |
261 tmp_dir = util.TempDir(files) | |
262 for filename in files: | |
263 source_resources.add(tmp_dir.GetPath(filename)) | |
264 | |
265 def replacer(var, repl): | |
266 return lambda filename: filename.replace('[%s]' % var, repl) | |
267 | |
268 # Test normal inlining. | |
269 result = html_inline.DoInline( | |
270 tmp_dir.GetPath('index.html'), | |
271 None, | |
272 filename_expansion_function=replacer('WHICH', '1')) | |
273 resources = result.inlined_files | |
274 resources.add(tmp_dir.GetPath('index.html')) | |
275 self.failUnlessEqual(resources, source_resources) | |
276 self.failUnlessEqual(expected_inlined, | |
277 util.FixLineEnd(result.inlined_data, '\n')) | |
278 | |
279 # Test names-only inlining. | |
280 result = html_inline.DoInline( | |
281 tmp_dir.GetPath('index.html'), | |
282 None, | |
283 names_only=True, | |
284 filename_expansion_function=replacer('WHICH', '1')) | |
285 resources = result.inlined_files | |
286 resources.add(tmp_dir.GetPath('index.html')) | |
287 self.failUnlessEqual(resources, source_resources) | |
288 | |
289 def testWithCloseTags(self): | |
290 '''Tests that close tags are removed.''' | |
291 | |
292 files = { | |
293 'index.html': ''' | |
294 <html> | |
295 <head> | |
296 <link rel="stylesheet" href="style1.css"></link> | |
297 <link rel="stylesheet" href="style2.css"> | |
298 </link> | |
299 <link rel="stylesheet" href="style2.css" | |
300 > | |
301 </link> | |
302 <script src="script1.js"></script> | |
303 </head> | |
304 <include src="tmpl1.html"></include> | |
305 <include src="tmpl2.html"> | |
306 </include> | |
307 <include src="tmpl2.html" | |
308 > | |
309 </include> | |
310 <img src="img1.png"> | |
311 </html> | |
312 ''', | |
313 'style1.css': '''h1 {}''', | |
314 'style2.css': '''h2 {}''', | |
315 'tmpl1.html': '''<h1></h1>''', | |
316 'tmpl2.html': '''<h2></h2>''', | |
317 'script1.js': '''console.log('hello');''', | |
318 'img1.png': '''abc''', | |
319 } | |
320 | |
321 expected_inlined = ''' | |
322 <html> | |
323 <head> | |
324 <style>h1 {}</style> | |
325 <style>h2 {}</style> | |
326 <style>h2 {}</style> | |
327 <script>console.log('hello');</script> | |
328 </head> | |
329 <h1></h1> | |
330 <h2></h2> | |
331 <h2></h2> | |
332 <img src="data:image/png;base64,YWJj"> | |
333 </html> | |
334 ''' | |
335 | |
336 source_resources = set() | |
337 tmp_dir = util.TempDir(files) | |
338 for filename in files: | |
339 source_resources.add(tmp_dir.GetPath(filename)) | |
340 | |
341 # Test normal inlining. | |
342 result = html_inline.DoInline( | |
343 tmp_dir.GetPath('index.html'), | |
344 None) | |
345 resources = result.inlined_files | |
346 resources.add(tmp_dir.GetPath('index.html')) | |
347 self.failUnlessEqual(resources, source_resources) | |
348 self.failUnlessEqual(expected_inlined, | |
349 util.FixLineEnd(result.inlined_data, '\n')) | |
350 | |
351 if __name__ == '__main__': | |
352 unittest.main() | |
OLD | NEW |