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