OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 '''Unit tests for grit.format.policy_templates.writers.doc_writer''' |
| 6 |
| 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 from xml.dom import minidom |
| 18 |
| 19 from grit.format import rc |
| 20 from grit.format.policy_templates.writers import writer_unittest_common |
| 21 from grit.format.policy_templates.writers import doc_writer |
| 22 from grit import grd_reader |
| 23 from grit import util |
| 24 from grit.tool import build |
| 25 |
| 26 class MockMessageDictionary: |
| 27 '''A mock dictionary passed to a writer as the dictionary of |
| 28 localized messages. |
| 29 ''' |
| 30 |
| 31 # Dictionary of messages. |
| 32 msg_dict = {} |
| 33 |
| 34 class DocWriterUnittest(writer_unittest_common.WriterUnittestCommon): |
| 35 '''Unit tests for DocWriter.''' |
| 36 |
| 37 def setUp(self): |
| 38 # Create a writer for the tests. |
| 39 self.writer = doc_writer.GetWriter( |
| 40 config={ |
| 41 'app_name': 'Chrome', |
| 42 'frame_name': 'Chrome Frame', |
| 43 'os_name': 'Chrome OS', |
| 44 'win_reg_key_name': 'MockKey', |
| 45 }) |
| 46 self.writer.messages = { |
| 47 'doc_back_to_top': {'text': '_test_back_to_top'}, |
| 48 'doc_data_type': {'text': '_test_data_type'}, |
| 49 'doc_description': {'text': '_test_description'}, |
| 50 'doc_description_column_title': { |
| 51 'text': '_test_description_column_title' |
| 52 }, |
| 53 'doc_example_value': {'text': '_test_example_value'}, |
| 54 'doc_feature_dynamic_refresh': {'text': '_test_feature_dynamic_refresh'}, |
| 55 'doc_intro': {'text': '_test_intro'}, |
| 56 'doc_mac_linux_pref_name': {'text': '_test_mac_linux_pref_name'}, |
| 57 'doc_note': {'text': '_test_note'}, |
| 58 'doc_name_column_title': {'text': '_test_name_column_title'}, |
| 59 'doc_not_supported': {'text': '_test_not_supported'}, |
| 60 'doc_since_version': {'text': '_test_since_version'}, |
| 61 'doc_supported': {'text': '_test_supported'}, |
| 62 'doc_supported_features': {'text': '_test_supported_features'}, |
| 63 'doc_supported_on': {'text': '_test_supported_on'}, |
| 64 'doc_win_reg_loc': {'text': '_test_win_reg_loc'}, |
| 65 |
| 66 'doc_bla': {'text': '_test_bla'}, |
| 67 } |
| 68 self.writer.Init() |
| 69 |
| 70 # It is not worth testing the exact content of style attributes. |
| 71 # Therefore we override them here with shorter texts. |
| 72 for key in self.writer._STYLE.keys(): |
| 73 self.writer._STYLE[key] = 'style_%s;' % key |
| 74 # Add some more style attributes for additional testing. |
| 75 self.writer._STYLE['key1'] = 'style1;' |
| 76 self.writer._STYLE['key2'] = 'style2;' |
| 77 |
| 78 # Create a DOM document for the tests. |
| 79 dom_impl = minidom.getDOMImplementation('') |
| 80 self.doc = dom_impl.createDocument(None, 'root', None) |
| 81 self.doc_root = self.doc.documentElement |
| 82 |
| 83 def testSkeleton(self): |
| 84 # Test if DocWriter creates the skeleton of the document correctly. |
| 85 self.writer.BeginTemplate() |
| 86 self.assertEquals( |
| 87 self.writer._main_div.toxml(), |
| 88 '<div>' |
| 89 '<div>' |
| 90 '<a name="top"/><br/>_test_intro<br/><br/><br/>' |
| 91 '<table style="style_table;">' |
| 92 '<thead><tr style="style_tr;">' |
| 93 '<td style="style_td;style_td.left;style_thead td;">' |
| 94 '_test_name_column_title' |
| 95 '</td>' |
| 96 '<td style="style_td;style_td.right;style_thead td;">' |
| 97 '_test_description_column_title' |
| 98 '</td>' |
| 99 '</tr></thead>' |
| 100 '<tbody/>' |
| 101 '</table>' |
| 102 '</div>' |
| 103 '<div/>' |
| 104 '</div>') |
| 105 |
| 106 def testGetLocalizedMessage(self): |
| 107 # Test if localized messages are retrieved correctly. |
| 108 self.writer.messages = { |
| 109 'doc_hello_world': {'text': 'hello, vilag!'} |
| 110 } |
| 111 self.assertEquals( |
| 112 self.writer._GetLocalizedMessage('hello_world'), |
| 113 'hello, vilag!') |
| 114 |
| 115 def testMapListToString(self): |
| 116 # Test function DocWriter.MapListToString() |
| 117 self.assertEquals( |
| 118 self.writer._MapListToString({'a1': 'a2', 'b1': 'b2'}, ['a1', 'b1']), |
| 119 'a2, b2') |
| 120 self.assertEquals( |
| 121 self.writer._MapListToString({'a1': 'a2', 'b1': 'b2'}, []), |
| 122 '') |
| 123 result = self.writer._MapListToString( |
| 124 {'a': '1', 'b': '2', 'c': '3', 'd': '4'}, ['b', 'd']) |
| 125 expected_result = '2, 4' |
| 126 self.assertEquals( |
| 127 result, |
| 128 expected_result) |
| 129 |
| 130 def testAddStyledElement(self): |
| 131 # Test function DocWriter.AddStyledElement() |
| 132 |
| 133 # Test the case of zero style. |
| 134 e1 = self.writer._AddStyledElement( |
| 135 self.doc_root, 'z', [], {'a': 'b'}, 'text') |
| 136 self.assertEquals( |
| 137 e1.toxml(), |
| 138 '<z a="b">text</z>') |
| 139 |
| 140 # Test the case of one style. |
| 141 e2 = self.writer._AddStyledElement( |
| 142 self.doc_root, 'z', ['key1'], {'a': 'b'}, 'text') |
| 143 self.assertEquals( |
| 144 e2.toxml(), |
| 145 '<z a="b" style="style1;">text</z>') |
| 146 |
| 147 # Test the case of two styles. |
| 148 e3 = self.writer._AddStyledElement( |
| 149 self.doc_root, 'z', ['key1', 'key2'], {'a': 'b'}, 'text') |
| 150 self.assertEquals( |
| 151 e3.toxml(), |
| 152 '<z a="b" style="style1;style2;">text</z>') |
| 153 |
| 154 def testAddDescriptionIntEnum(self): |
| 155 # Test if URLs are replaced and choices of 'int-enum' policies are listed |
| 156 # correctly. |
| 157 policy = { |
| 158 'type': 'int-enum', |
| 159 'items': [ |
| 160 {'value': 0, 'caption': 'Disable foo'}, |
| 161 {'value': 2, 'caption': 'Solve your problem'}, |
| 162 {'value': 5, 'caption': 'Enable bar'}, |
| 163 ], |
| 164 'desc': '''This policy disables foo, except in case of bar. |
| 165 See http://policy-explanation.example.com for more details. |
| 166 ''' |
| 167 } |
| 168 self.writer._AddDescription(self.doc_root, policy) |
| 169 self.assertEquals( |
| 170 self.doc_root.toxml(), |
| 171 '''<root>This policy disables foo, except in case of bar. |
| 172 See <a href="http://policy-explanation.example.com">http://policy-explanation.ex
ample.com</a> for more details. |
| 173 <ul><li>0 = Disable foo</li><li>2 = Solve your problem</li><li>5 = Enable bar</l
i></ul></root>''') |
| 174 |
| 175 def testAddDescriptionStringEnum(self): |
| 176 # Test if URLs are replaced and choices of 'int-enum' policies are listed |
| 177 # correctly. |
| 178 policy = { |
| 179 'type': 'string-enum', |
| 180 'items': [ |
| 181 {'value': "one", 'caption': 'Disable foo'}, |
| 182 {'value': "two", 'caption': 'Solve your problem'}, |
| 183 {'value': "three", 'caption': 'Enable bar'}, |
| 184 ], |
| 185 'desc': '''This policy disables foo, except in case of bar. |
| 186 See http://policy-explanation.example.com for more details. |
| 187 ''' |
| 188 } |
| 189 self.writer._AddDescription(self.doc_root, policy) |
| 190 self.assertEquals( |
| 191 self.doc_root.toxml(), |
| 192 '''<root>This policy disables foo, except in case of bar. |
| 193 See <a href="http://policy-explanation.example.com">http://policy-explanation.ex
ample.com</a> for more details. |
| 194 <ul><li>"one" = Disable foo</li><li>"two" = Solve your probl
em</li><li>"three" = Enable bar</li></ul></root>''') |
| 195 |
| 196 def testAddFeatures(self): |
| 197 # Test if the list of features of a policy is handled correctly. |
| 198 policy = { |
| 199 'features': {'spaceship_docking': 0, 'dynamic_refresh': 1} |
| 200 } |
| 201 self.writer._FEATURE_MAP = { |
| 202 'spaceship_docking': 'Spaceship Docking', |
| 203 'dynamic_refresh': 'Dynamic Refresh' |
| 204 } |
| 205 self.writer._AddFeatures(self.doc_root, policy) |
| 206 self.assertEquals( |
| 207 self.doc_root.toxml(), |
| 208 '<root>' |
| 209 'Dynamic Refresh: _test_supported, ' |
| 210 'Spaceship Docking: _test_not_supported' |
| 211 '</root>') |
| 212 |
| 213 def testAddListExample(self): |
| 214 policy = { |
| 215 'name': 'PolicyName', |
| 216 'example_value': ['Foo', 'Bar'] |
| 217 } |
| 218 self.writer._AddListExample(self.doc_root, policy) |
| 219 self.assertEquals( |
| 220 self.doc_root.toxml(), |
| 221 '<root>' |
| 222 '<dl style="style_dd dl;">' |
| 223 '<dt>Windows:</dt>' |
| 224 '<dd style="style_.monospace;style_.pre;">' |
| 225 'MockKey\\PolicyName\\1 = "Foo"\n' |
| 226 'MockKey\\PolicyName\\2 = "Bar"' |
| 227 '</dd>' |
| 228 '<dt>Linux:</dt>' |
| 229 '<dd style="style_.monospace;">' |
| 230 '["Foo", "Bar"]' |
| 231 '</dd>' |
| 232 '<dt>Mac:</dt>' |
| 233 '<dd style="style_.monospace;style_.pre;">' |
| 234 '<array>\n' |
| 235 ' <string>Foo</string>\n' |
| 236 ' <string>Bar</string>\n' |
| 237 '</array>' |
| 238 '</dd>' |
| 239 '</dl>' |
| 240 '</root>') |
| 241 |
| 242 def testBoolExample(self): |
| 243 # Test representation of boolean example values. |
| 244 policy = { |
| 245 'name': 'PolicyName', |
| 246 'type': 'main', |
| 247 'example_value': True |
| 248 } |
| 249 e1 = self.writer.AddElement(self.doc_root, 'e1') |
| 250 self.writer._AddExample(e1, policy) |
| 251 self.assertEquals( |
| 252 e1.toxml(), |
| 253 '<e1>0x00000001 (Windows), true (Linux), <true /> (Mac)</e1>') |
| 254 |
| 255 policy = { |
| 256 'name': 'PolicyName', |
| 257 'type': 'main', |
| 258 'example_value': False |
| 259 } |
| 260 e2 = self.writer.AddElement(self.doc_root, 'e2') |
| 261 self.writer._AddExample(e2, policy) |
| 262 self.assertEquals( |
| 263 e2.toxml(), |
| 264 '<e2>0x00000000 (Windows), false (Linux), <false /> (Mac)</e2>') |
| 265 |
| 266 def testIntEnumExample(self): |
| 267 # Test representation of 'int-enum' example values. |
| 268 policy = { |
| 269 'name': 'PolicyName', |
| 270 'type': 'int-enum', |
| 271 'example_value': 16 |
| 272 } |
| 273 self.writer._AddExample(self.doc_root, policy) |
| 274 self.assertEquals( |
| 275 self.doc_root.toxml(), |
| 276 '<root>0x00000010 (Windows), 16 (Linux/Mac)</root>') |
| 277 |
| 278 def testStringEnumExample(self): |
| 279 # Test representation of 'int-enum' example values. |
| 280 policy = { |
| 281 'name': 'PolicyName', |
| 282 'type': 'string-enum', |
| 283 'example_value': "wacky" |
| 284 } |
| 285 self.writer._AddExample(self.doc_root, policy) |
| 286 self.assertEquals( |
| 287 self.doc_root.toxml(), |
| 288 '<root>"wacky"</root>') |
| 289 |
| 290 def testStringExample(self): |
| 291 # Test representation of 'string' example values. |
| 292 policy = { |
| 293 'name': 'PolicyName', |
| 294 'type': 'string', |
| 295 'example_value': 'awesome-example' |
| 296 } |
| 297 self.writer._AddExample(self.doc_root, policy) |
| 298 self.assertEquals( |
| 299 self.doc_root.toxml(), |
| 300 '<root>"awesome-example"</root>') |
| 301 |
| 302 def testIntExample(self): |
| 303 # Test representation of 'int' example values. |
| 304 policy = { |
| 305 'name': 'PolicyName', |
| 306 'type': 'int', |
| 307 'example_value': 26 |
| 308 } |
| 309 self.writer._AddExample(self.doc_root, policy) |
| 310 self.assertEquals( |
| 311 self.doc_root.toxml(), |
| 312 '<root>0x0000001a (Windows), 26 (Linux/Mac)</root>') |
| 313 |
| 314 def testAddPolicyAttribute(self): |
| 315 # Test creating a policy attribute term-definition pair. |
| 316 self.writer._AddPolicyAttribute( |
| 317 self.doc_root, 'bla', 'hello, world', ['key1']) |
| 318 self.assertEquals( |
| 319 self.doc_root.toxml(), |
| 320 '<root>' |
| 321 '<dt style="style_dt;">_test_bla</dt>' |
| 322 '<dd style="style1;">hello, world</dd>' |
| 323 '</root>') |
| 324 |
| 325 def testAddPolicyDetails(self): |
| 326 # Test if the definition list (<dl>) of policy details is created correctly. |
| 327 policy = { |
| 328 'type': 'main', |
| 329 'name': 'TestPolicyName', |
| 330 'caption': 'TestPolicyCaption', |
| 331 'desc': 'TestPolicyDesc', |
| 332 'supported_on': [{ |
| 333 'product': 'chrome', |
| 334 'platforms': ['win'], |
| 335 'since_version': '8', |
| 336 'until_version': '', |
| 337 }], |
| 338 'features': {'dynamic_refresh': 0}, |
| 339 'example_value': False |
| 340 } |
| 341 self.writer.messages['doc_since_version'] = {'text': '...$6...'} |
| 342 self.writer._AddPolicyDetails(self.doc_root, policy) |
| 343 self.assertEquals( |
| 344 self.doc_root.toxml(), |
| 345 '<root><dl>' |
| 346 '<dt style="style_dt;">_test_data_type</dt><dd>Boolean (REG_DWORD)</dd>' |
| 347 '<dt style="style_dt;">_test_win_reg_loc</dt>' |
| 348 '<dd style="style_.monospace;">MockKey\TestPolicyName</dd>' |
| 349 '<dt style="style_dt;">_test_mac_linux_pref_name</dt>' |
| 350 '<dd style="style_.monospace;">TestPolicyName</dd>' |
| 351 '<dt style="style_dt;">_test_supported_on</dt>' |
| 352 '<dd>' |
| 353 '<ul style="style_ul;">' |
| 354 '<li>Chrome (Windows) ...8...</li>' |
| 355 '</ul>' |
| 356 '</dd>' |
| 357 '<dt style="style_dt;">_test_supported_features</dt>' |
| 358 '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>' |
| 359 '<dt style="style_dt;">_test_description</dt><dd>TestPolicyDesc</dd>' |
| 360 '<dt style="style_dt;">_test_example_value</dt>' |
| 361 '<dd>0x00000000 (Windows), false (Linux), <false /> (Mac)</dd>' |
| 362 '</dl></root>') |
| 363 |
| 364 def testAddPolicyNote(self): |
| 365 # TODO(jkummerow): The functionality tested by this test is currently not |
| 366 # used for anything and will probably soon be removed. |
| 367 # Test if nodes are correctly added to policies. |
| 368 policy = { |
| 369 'problem_href': 'http://www.example.com/5' |
| 370 } |
| 371 self.writer.messages['doc_note'] = {'text': '...$6...'} |
| 372 self.writer._AddPolicyNote(self.doc_root, policy) |
| 373 self.assertEquals( |
| 374 self.doc_root.toxml(), |
| 375 '<root><div style="style_div.note;">...' |
| 376 '<a href="http://www.example.com/5">http://www.example.com/5</a>' |
| 377 '...</div></root>') |
| 378 |
| 379 def testAddPolicyRow(self): |
| 380 # Test if policies are correctly added to the summary table. |
| 381 policy = { |
| 382 'name': 'PolicyName', |
| 383 'caption': 'PolicyCaption', |
| 384 'type': 'string', |
| 385 } |
| 386 self.writer._indent_level = 3 |
| 387 self.writer._AddPolicyRow(self.doc_root, policy) |
| 388 self.assertEquals( |
| 389 self.doc_root.toxml(), |
| 390 '<root><tr style="style_tr;">' |
| 391 '<td style="style_td;style_td.left;padding-left: 49px;">' |
| 392 '<a href="#PolicyName">PolicyName</a>' |
| 393 '</td>' |
| 394 '<td style="style_td;style_td.right;">PolicyCaption</td>' |
| 395 '</tr></root>') |
| 396 self.setUp() |
| 397 policy = { |
| 398 'name': 'PolicyName', |
| 399 'caption': 'PolicyCaption', |
| 400 'type': 'group', |
| 401 } |
| 402 self.writer._indent_level = 2 |
| 403 self.writer._AddPolicyRow(self.doc_root, policy) |
| 404 self.assertEquals( |
| 405 self.doc_root.toxml(), |
| 406 '<root><tr style="style_tr;">' |
| 407 '<td colspan="2" style="style_td;style_td.left;padding-left: 35px;">' |
| 408 '<a href="#PolicyName">PolicyCaption</a>' |
| 409 '</td>' |
| 410 '</tr></root>') |
| 411 |
| 412 def testAddPolicySection(self): |
| 413 # Test if policy details are correctly added to the document. |
| 414 policy = { |
| 415 'name': 'PolicyName', |
| 416 'caption': 'PolicyCaption', |
| 417 'desc': 'PolicyDesc', |
| 418 'type': 'string', |
| 419 'supported_on': [{ |
| 420 'product': 'chrome', |
| 421 'platforms': ['win'], |
| 422 'since_version': '7', |
| 423 'until_version': '', |
| 424 }], |
| 425 'features': {'dynamic_refresh': 0}, |
| 426 'example_value': False |
| 427 } |
| 428 self.writer.messages['doc_since_version'] = {'text': '..$6..'} |
| 429 self.writer._AddPolicySection(self.doc_root, policy) |
| 430 self.assertEquals( |
| 431 self.doc_root.toxml(), |
| 432 '<root>' |
| 433 '<div style="margin-left: 0px">' |
| 434 '<h3><a name="PolicyName"/>PolicyName</h3>' |
| 435 '<span>PolicyCaption</span>' |
| 436 '<dl>' |
| 437 '<dt style="style_dt;">_test_data_type</dt>' |
| 438 '<dd>String (REG_SZ)</dd>' |
| 439 '<dt style="style_dt;">_test_win_reg_loc</dt>' |
| 440 '<dd style="style_.monospace;">MockKey\\PolicyName</dd>' |
| 441 '<dt style="style_dt;">_test_mac_linux_pref_name</dt>' |
| 442 '<dd style="style_.monospace;">PolicyName</dd>' |
| 443 '<dt style="style_dt;">_test_supported_on</dt>' |
| 444 '<dd>' |
| 445 '<ul style="style_ul;">' |
| 446 '<li>Chrome (Windows) ..7..</li>' |
| 447 '</ul>' |
| 448 '</dd>' |
| 449 '<dt style="style_dt;">_test_supported_features</dt>' |
| 450 '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>' |
| 451 '<dt style="style_dt;">_test_description</dt>' |
| 452 '<dd>PolicyDesc</dd>' |
| 453 '<dt style="style_dt;">_test_example_value</dt>' |
| 454 '<dd>"False"</dd>' |
| 455 '</dl>' |
| 456 '<a href="#top">_test_back_to_top</a>' |
| 457 '</div>' |
| 458 '</root>') |
| 459 # Test for groups. |
| 460 self.setUp() |
| 461 policy['type'] = 'group' |
| 462 self.writer._AddPolicySection(self.doc_root, policy) |
| 463 self.assertEquals( |
| 464 self.doc_root.toxml(), |
| 465 '<root>' |
| 466 '<div style="margin-left: 0px">' |
| 467 '<h2><a name="PolicyName"/>PolicyCaption</h2>' |
| 468 '<div style="style_div.group_desc;">PolicyDesc</div>' |
| 469 '<a href="#top">_test_back_to_top</a>' |
| 470 '</div>' |
| 471 '</root>') |
| 472 |
| 473 |
| 474 if __name__ == '__main__': |
| 475 unittest.main() |
OLD | NEW |