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.gather.chrome_html''' |
| 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(sys.argv[0]), '../..')) |
| 14 |
| 15 import tempfile |
| 16 import unittest |
| 17 |
| 18 from grit import lazy_re |
| 19 from grit.gather import chrome_html |
| 20 |
| 21 |
| 22 _NEW_LINE = lazy_re.compile('(\r\n|\r|\n)', re.MULTILINE) |
| 23 |
| 24 |
| 25 def StandardizeHtml(text): |
| 26 '''Standardizes the newline format and png mime type in Html text.''' |
| 27 return _NEW_LINE.sub('\n', text).replace('data:image/x-png;', |
| 28 'data:image/png;') |
| 29 |
| 30 |
| 31 class TempDir(object): |
| 32 def __init__(self, file_data): |
| 33 self.files = [] |
| 34 self.dirs = [] |
| 35 self.tmp_dir_name = tempfile.gettempdir() |
| 36 for name in file_data: |
| 37 file_path = self.tmp_dir_name + '/' + name |
| 38 dir_path = os.path.split(file_path)[0] |
| 39 if not os.path.exists(dir_path): |
| 40 self.dirs.append(dir_path) |
| 41 os.makedirs(dir_path) |
| 42 self.files.append(file_path) |
| 43 f = open(file_path, 'w') |
| 44 f.write(file_data[name]) |
| 45 f.close() |
| 46 |
| 47 def CleanUp(self): |
| 48 self.dirs.reverse() |
| 49 for name in self.files: |
| 50 os.unlink(name) |
| 51 for name in self.dirs: |
| 52 os.removedirs(name) |
| 53 |
| 54 def GetPath(self, name): |
| 55 return self.tmp_dir_name + '/' + name |
| 56 |
| 57 |
| 58 class ChromeHtmlUnittest(unittest.TestCase): |
| 59 '''Unit tests for ChromeHtml.''' |
| 60 |
| 61 def testFileResources(self): |
| 62 '''Tests inlined image file resources with available high DPI assets.''' |
| 63 |
| 64 tmp_dir = TempDir({ |
| 65 'index.html': ''' |
| 66 <!DOCTYPE HTML> |
| 67 <html> |
| 68 <head> |
| 69 <link rel="stylesheet" href="test.css"> |
| 70 </head> |
| 71 <body> |
| 72 <!-- Don't need a body. --> |
| 73 </body> |
| 74 </html> |
| 75 ''', |
| 76 |
| 77 'test.css': ''' |
| 78 .image { |
| 79 background: url('test.png'); |
| 80 } |
| 81 ''', |
| 82 |
| 83 'test.png': 'PNG DATA', |
| 84 |
| 85 '1.4x/test.png': '1.4x PNG DATA', |
| 86 |
| 87 '1.8x/test.png': '1.8x PNG DATA', |
| 88 }) |
| 89 |
| 90 html = chrome_html.ChromeHtml(tmp_dir.GetPath('index.html')) |
| 91 html.SetDefines({'scale_factors': '1.4x,1.8x'}) |
| 92 html.Parse() |
| 93 self.failUnlessEqual(StandardizeHtml(html.GetData('en', 'utf-8')), |
| 94 StandardizeHtml(''' |
| 95 <!DOCTYPE HTML> |
| 96 <html> |
| 97 <head> |
| 98 <style> |
| 99 .image { |
| 100 background: -webkit-image-set(url("data:image/png;base64,UE5HIERBVEE=")
1x, url("data:image/png;base64,MS40eCBQTkcgREFUQQ==") 1.4x, url("data:image/png;
base64,MS44eCBQTkcgREFUQQ==") 1.8x); |
| 101 } |
| 102 </style> |
| 103 </head> |
| 104 <body> |
| 105 <!-- Don't need a body. --> |
| 106 </body> |
| 107 </html> |
| 108 ''')) |
| 109 tmp_dir.CleanUp() |
| 110 |
| 111 def testFileResourcesNoFile(self): |
| 112 '''Tests inlined image file resources without available high DPI assets.''' |
| 113 |
| 114 tmp_dir = TempDir({ |
| 115 'index.html': ''' |
| 116 <!DOCTYPE HTML> |
| 117 <html> |
| 118 <head> |
| 119 <link rel="stylesheet" href="test.css"> |
| 120 </head> |
| 121 <body> |
| 122 <!-- Don't need a body. --> |
| 123 </body> |
| 124 </html> |
| 125 ''', |
| 126 |
| 127 'test.css': ''' |
| 128 .image { |
| 129 background: url('test.png'); |
| 130 } |
| 131 ''', |
| 132 |
| 133 'test.png': 'PNG DATA', |
| 134 }) |
| 135 |
| 136 html = chrome_html.ChromeHtml(tmp_dir.GetPath('index.html')) |
| 137 html.SetDefines({'scale_factors': '2x'}) |
| 138 html.Parse() |
| 139 self.failUnlessEqual(StandardizeHtml(html.GetData('en', 'utf-8')), |
| 140 StandardizeHtml(''' |
| 141 <!DOCTYPE HTML> |
| 142 <html> |
| 143 <head> |
| 144 <style> |
| 145 .image { |
| 146 background: -webkit-image-set(url("data:image/png;base64,UE5HIERBVEE=")
1x); |
| 147 } |
| 148 </style> |
| 149 </head> |
| 150 <body> |
| 151 <!-- Don't need a body. --> |
| 152 </body> |
| 153 </html> |
| 154 ''')) |
| 155 tmp_dir.CleanUp() |
| 156 |
| 157 def testThemeResources(self): |
| 158 '''Tests inserting high DPI chrome://theme references.''' |
| 159 |
| 160 tmp_dir = TempDir({ |
| 161 'index.html': ''' |
| 162 <!DOCTYPE HTML> |
| 163 <html> |
| 164 <head> |
| 165 <link rel="stylesheet" href="test.css"> |
| 166 </head> |
| 167 <body> |
| 168 <!-- Don't need a body. --> |
| 169 </body> |
| 170 </html> |
| 171 ''', |
| 172 |
| 173 'test.css': ''' |
| 174 .image { |
| 175 background: url('chrome://theme/IDR_RESOURCE_NAME'); |
| 176 } |
| 177 ''', |
| 178 }) |
| 179 |
| 180 html = chrome_html.ChromeHtml(tmp_dir.GetPath('index.html')) |
| 181 html.SetDefines({'scale_factors': '2x'}) |
| 182 html.Parse() |
| 183 self.failUnlessEqual(StandardizeHtml(html.GetData('en', 'utf-8')), |
| 184 StandardizeHtml(''' |
| 185 <!DOCTYPE HTML> |
| 186 <html> |
| 187 <head> |
| 188 <style> |
| 189 .image { |
| 190 background: -webkit-image-set(url("chrome://theme/IDR_RESOURCE_NAME") 1x
, url("chrome://theme/IDR_RESOURCE_NAME@2x") 2x); |
| 191 } |
| 192 </style> |
| 193 </head> |
| 194 <body> |
| 195 <!-- Don't need a body. --> |
| 196 </body> |
| 197 </html> |
| 198 ''')) |
| 199 tmp_dir.CleanUp() |
| 200 |
| 201 |
| 202 if __name__ == '__main__': |
| 203 unittest.main() |
OLD | NEW |