OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 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.rc''' |
| 7 |
| 8 import os |
| 9 import re |
| 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 import StringIO |
| 17 |
| 18 from grit.format import rc |
| 19 from grit import grd_reader |
| 20 from grit import util |
| 21 from grit.tool import build |
| 22 |
| 23 class DummyOutput(object): |
| 24 def __init__(self, type, language, file = 'hello.gif'): |
| 25 self.type = type |
| 26 self.language = language |
| 27 self.file = file |
| 28 def GetType(self): |
| 29 return self.type |
| 30 def GetLanguage(self): |
| 31 return self.language |
| 32 def GetOutputFilename(self): |
| 33 return self.file |
| 34 |
| 35 class FormatRcUnittest(unittest.TestCase): |
| 36 def testMessages(self): |
| 37 root = grd_reader.Parse(StringIO.StringIO(''' |
| 38 <messages> |
| 39 <message name="IDS_BTN_GO" desc="Button text" meaning="verb">Go!</mess
age> |
| 40 <message name="IDS_GREETING" desc="Printed to greet the currently logg
ed in user"> |
| 41 Hello <ph name="USERNAME">%s<ex>Joi</ex></ph>, how are you doing tod
ay? |
| 42 </message> |
| 43 <message name="BONGO" desc="Flippo nippo"> |
| 44 Howdie "Mr. Elephant", how are you doing? \'\'\' |
| 45 </message> |
| 46 <message name="IDS_WITH_LINEBREAKS"> |
| 47 Good day sir, |
| 48 I am a bee |
| 49 Sting sting |
| 50 </message> |
| 51 </messages> |
| 52 '''), flexible_root = True) |
| 53 util.FixRootForUnittest(root) |
| 54 |
| 55 buf = StringIO.StringIO() |
| 56 build.RcBuilder.ProcessNode(root, DummyOutput('rc_all', 'en'), buf) |
| 57 output = buf.getvalue() |
| 58 self.failUnless(output.strip() == u''' |
| 59 STRINGTABLE |
| 60 BEGIN |
| 61 IDS_BTN_GO "Go!" |
| 62 IDS_GREETING "Hello %s, how are you doing today?" |
| 63 BONGO "Howdie ""Mr. Elephant"", how are you doing? " |
| 64 IDS_WITH_LINEBREAKS "Good day sir,\\nI am a bee\\nSting sting" |
| 65 END'''.strip()) |
| 66 |
| 67 |
| 68 def testRcSection(self): |
| 69 root = grd_reader.Parse(StringIO.StringIO(''' |
| 70 <structures> |
| 71 <structure type="menu" name="IDC_KLONKMENU" file="grit\\testdata\klonk
.rc" encoding="utf-16" /> |
| 72 <structure type="dialog" name="IDD_ABOUTBOX" file="grit\\testdata\klon
k.rc" encoding="utf-16" /> |
| 73 <structure type="version" name="VS_VERSION_INFO" file="grit\\testdata\
klonk.rc" encoding="utf-16" /> |
| 74 </structures>'''), flexible_root = True) |
| 75 util.FixRootForUnittest(root) |
| 76 root.RunGatherers(recursive = True) |
| 77 |
| 78 buf = StringIO.StringIO() |
| 79 build.RcBuilder.ProcessNode(root, DummyOutput('rc_all', 'en'), buf) |
| 80 output = buf.getvalue().strip() |
| 81 expected = u''' |
| 82 IDC_KLONKMENU MENU |
| 83 BEGIN |
| 84 POPUP "&File" |
| 85 BEGIN |
| 86 MENUITEM "E&xit", IDM_EXIT |
| 87 MENUITEM "This be ""Klonk"" me like", ID_FILE_THISBE |
| 88 POPUP "gonk" |
| 89 BEGIN |
| 90 MENUITEM "Klonk && is ""good""", ID_GONK_KLONKIS |
| 91 END |
| 92 END |
| 93 POPUP "&Help" |
| 94 BEGIN |
| 95 MENUITEM "&About ...", IDM_ABOUT |
| 96 END |
| 97 END |
| 98 |
| 99 IDD_ABOUTBOX DIALOGEX 22, 17, 230, 75 |
| 100 STYLE DS_SETFONT | DS_MODALFRAME | WS_CAPTION | WS_SYSMENU |
| 101 CAPTION "About" |
| 102 FONT 8, "System", 0, 0, 0x0 |
| 103 BEGIN |
| 104 ICON IDI_KLONK,IDC_MYICON,14,9,20,20 |
| 105 LTEXT "klonk Version ""yibbee"" 1.0",IDC_STATIC,49,10,119,8, |
| 106 SS_NOPREFIX |
| 107 LTEXT "Copyright (C) 2005",IDC_STATIC,49,20,119,8 |
| 108 DEFPUSHBUTTON "OK",IDOK,195,6,30,11,WS_GROUP |
| 109 CONTROL "Jack ""Black"" Daniels",IDC_RADIO1,"Button", |
| 110 BS_AUTORADIOBUTTON,46,51,84,10 |
| 111 END |
| 112 |
| 113 VS_VERSION_INFO VERSIONINFO |
| 114 FILEVERSION 1,0,0,1 |
| 115 PRODUCTVERSION 1,0,0,1 |
| 116 FILEFLAGSMASK 0x17L |
| 117 #ifdef _DEBUG |
| 118 FILEFLAGS 0x1L |
| 119 #else |
| 120 FILEFLAGS 0x0L |
| 121 #endif |
| 122 FILEOS 0x4L |
| 123 FILETYPE 0x1L |
| 124 FILESUBTYPE 0x0L |
| 125 BEGIN |
| 126 BLOCK "StringFileInfo" |
| 127 BEGIN |
| 128 BLOCK "040904b0" |
| 129 BEGIN |
| 130 VALUE "FileDescription", "klonk Application" |
| 131 VALUE "FileVersion", "1, 0, 0, 1" |
| 132 VALUE "InternalName", "klonk" |
| 133 VALUE "LegalCopyright", "Copyright (C) 2005" |
| 134 VALUE "OriginalFilename", "klonk.exe" |
| 135 VALUE "ProductName", " klonk Application" |
| 136 VALUE "ProductVersion", "1, 0, 0, 1" |
| 137 END |
| 138 END |
| 139 BLOCK "VarFileInfo" |
| 140 BEGIN |
| 141 VALUE "Translation", 0x409, 1200 |
| 142 END |
| 143 END'''.strip() |
| 144 for expected_line, output_line in zip(expected.split(), output.split()): |
| 145 self.assertEqual(expected_line, output_line) |
| 146 |
| 147 def testRcIncludeStructure(self): |
| 148 root = grd_reader.Parse(StringIO.StringIO(''' |
| 149 <structures> |
| 150 <structure type="tr_html" name="IDR_HTML" file="bingo.html"/> |
| 151 <structure type="tr_html" name="IDR_HTML2" file="bingo2.html"/> |
| 152 </structures>'''), flexible_root = True) |
| 153 util.FixRootForUnittest(root, '/temp') |
| 154 # We do not run gatherers as it is not needed and wouldn't find the file |
| 155 |
| 156 buf = StringIO.StringIO() |
| 157 build.RcBuilder.ProcessNode(root, DummyOutput('rc_all', 'en'), buf) |
| 158 output = buf.getvalue() |
| 159 expected = (u'IDR_HTML HTML "%s"\n' |
| 160 u'IDR_HTML2 HTML "%s"' |
| 161 % (util.normpath('/temp/bingo.html').replace('\\', '\\\\'), |
| 162 util.normpath('/temp/bingo2.html').replace('\\', '\\\\'))) |
| 163 # hackety hack to work on win32&lin |
| 164 output = re.sub('"[c-zC-Z]:', '"', output) |
| 165 self.failUnless(output.strip() == expected) |
| 166 |
| 167 def testRcIncludeFile(self): |
| 168 root = grd_reader.Parse(StringIO.StringIO(''' |
| 169 <includes> |
| 170 <include type="TXT" name="TEXT_ONE" file="bingo.txt"/> |
| 171 <include type="TXT" name="TEXT_TWO" file="bingo2.txt" filenameonly="tru
e" /> |
| 172 </includes>'''), flexible_root = True) |
| 173 util.FixRootForUnittest(root, '/temp') |
| 174 |
| 175 buf = StringIO.StringIO() |
| 176 build.RcBuilder.ProcessNode(root, DummyOutput('rc_all', 'en'), buf) |
| 177 output = buf.getvalue() |
| 178 expected = (u'TEXT_ONE TXT "%s"\n' |
| 179 u'TEXT_TWO TXT "%s"' |
| 180 % (util.normpath('/temp/bingo.txt').replace('\\', '\\\\'), |
| 181 'bingo2.txt')) |
| 182 # hackety hack to work on win32&lin |
| 183 output = re.sub('"[c-zC-Z]:', '"', output) |
| 184 self.failUnless(output.strip() == expected) |
| 185 |
| 186 def testRcIncludeFlattenedHtmlFile(self): |
| 187 input_file = util.PathFromRoot('grit/testdata/include_test.html') |
| 188 output_file = '%s/HTML_FILE1_include_test.html' % tempfile.gettempdir() |
| 189 root = grd_reader.Parse(StringIO.StringIO(''' |
| 190 <includes> |
| 191 <include name="HTML_FILE1" flattenhtml="true" file="%s" type="BINDATA" /
> |
| 192 </includes>''' % input_file), flexible_root = True) |
| 193 util.FixRootForUnittest(root, '.') |
| 194 |
| 195 buf = StringIO.StringIO() |
| 196 build.RcBuilder.ProcessNode(root, DummyOutput('rc_all', 'en', output_file), |
| 197 buf) |
| 198 output = buf.getvalue() |
| 199 |
| 200 expected = u'HTML_FILE1 BINDATA "HTML_FILE1_include_test.
html"' |
| 201 # hackety hack to work on win32&lin |
| 202 output = re.sub('"[c-zC-Z]:', '"', output) |
| 203 self.failUnless(output.strip() == expected) |
| 204 |
| 205 fo = file(output_file) |
| 206 file_contents = fo.read() |
| 207 fo.close() |
| 208 |
| 209 # Check for the content added by the <include> tag. |
| 210 self.failUnless(file_contents.find('Hello Include!') != -1) |
| 211 # Check for the content that was removed by if tag. |
| 212 self.failUnless(file_contents.find('should be removed') == -1) |
| 213 # Check for the content that was kept in place by if. |
| 214 self.failUnless(file_contents.find('should be kept') != -1) |
| 215 self.failUnless(file_contents.find('in the middle...') != -1) |
| 216 self.failUnless(file_contents.find('at the end...') != -1) |
| 217 # Check for nested content that was kept |
| 218 self.failUnless(file_contents.find('nested true should be kept') != -1) |
| 219 self.failUnless(file_contents.find('silbing true should be kept') != -1) |
| 220 # Check for removed "<if>" and "</if>" tags. |
| 221 self.failUnless(file_contents.find('<if expr=') == -1) |
| 222 self.failUnless(file_contents.find('</if>') == -1) |
| 223 |
| 224 |
| 225 def testStructureNodeOutputfile(self): |
| 226 input_file = util.PathFromRoot('grit/testdata/simple.html') |
| 227 root = grd_reader.Parse(StringIO.StringIO( |
| 228 '<structure type="tr_html" name="IDR_HTML" file="%s" />' %input_file), |
| 229 flexible_root = True) |
| 230 util.FixRootForUnittest(root, '.') |
| 231 # We must run the gatherers since we'll be wanting the translation of the |
| 232 # file. The file exists in the location pointed to. |
| 233 root.RunGatherers(recursive=True) |
| 234 |
| 235 output_dir = tempfile.gettempdir() |
| 236 en_file = root.FileForLanguage('en', output_dir) |
| 237 self.failUnless(en_file == input_file) |
| 238 fr_file = root.FileForLanguage('fr', output_dir) |
| 239 self.failUnless(fr_file == os.path.join(output_dir, 'fr_simple.html')) |
| 240 |
| 241 fo = file(fr_file) |
| 242 contents = fo.read() |
| 243 fo.close() |
| 244 |
| 245 self.failUnless(contents.find('<p>') != -1) # should contain the markup |
| 246 self.failUnless(contents.find('Hello!') == -1) # should be translated |
| 247 |
| 248 |
| 249 def testFallbackToEnglish(self): |
| 250 root = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="U
TF-8"?> |
| 251 <grit latest_public_release="2" source_lang_id="en-US" current_release="3"
base_dir="."> |
| 252 <release seq="1" allow_pseudo="False"> |
| 253 <structures fallback_to_english="True"> |
| 254 <structure type="dialog" name="IDD_ABOUTBOX" file="grit\\testdata\kl
onk.rc" encoding="utf-16" /> |
| 255 </structures> |
| 256 </release> |
| 257 </grit>'''), util.PathFromRoot('.')) |
| 258 util.FixRootForUnittest(root) |
| 259 root.RunGatherers(recursive = True) |
| 260 |
| 261 node = root.GetNodeById("IDD_ABOUTBOX") |
| 262 formatter = node.ItemFormatter('rc_all') |
| 263 output = formatter.Format(node, 'bingobongo') |
| 264 self.failUnless(output.strip() == '''IDD_ABOUTBOX DIALOGEX 22, 17, 230, 75 |
| 265 STYLE DS_SETFONT | DS_MODALFRAME | WS_CAPTION | WS_SYSMENU |
| 266 CAPTION "About" |
| 267 FONT 8, "System", 0, 0, 0x0 |
| 268 BEGIN |
| 269 ICON IDI_KLONK,IDC_MYICON,14,9,20,20 |
| 270 LTEXT "klonk Version ""yibbee"" 1.0",IDC_STATIC,49,10,119,8, |
| 271 SS_NOPREFIX |
| 272 LTEXT "Copyright (C) 2005",IDC_STATIC,49,20,119,8 |
| 273 DEFPUSHBUTTON "OK",IDOK,195,6,30,11,WS_GROUP |
| 274 CONTROL "Jack ""Black"" Daniels",IDC_RADIO1,"Button", |
| 275 BS_AUTORADIOBUTTON,46,51,84,10 |
| 276 END''') |
| 277 |
| 278 |
| 279 def testRelativePath(self): |
| 280 ''' Verify that _MakeRelativePath works in some tricky cases.''' |
| 281 def TestRelativePathCombinations(base_path, other_path, expected_result): |
| 282 ''' Verify that the relative path function works for |
| 283 the given paths regardless of whether or not they end with |
| 284 a trailing slash.''' |
| 285 for path1 in [base_path, base_path + os.path.sep]: |
| 286 for path2 in [other_path, other_path + os.path.sep]: |
| 287 result = rc._MakeRelativePath(path1, path2) |
| 288 self.failUnless(result == expected_result) |
| 289 |
| 290 # set-up variables |
| 291 root_dir = 'c:%sa' % os.path.sep |
| 292 result1 = '..%sabc' % os.path.sep |
| 293 path1 = root_dir + 'bc' |
| 294 result2 = 'bc' |
| 295 path2 = '%s%s%s' % (root_dir, os.path.sep, result2) |
| 296 # run the tests |
| 297 TestRelativePathCombinations(root_dir, path1, result1) |
| 298 TestRelativePathCombinations(root_dir, path2, result2) |
| 299 |
| 300 |
| 301 if __name__ == '__main__': |
| 302 unittest.main() |
OLD | NEW |