Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / tools / grit / grit / format / policy_templates / writers / doc_writer_unittest.py
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.policy_templates.writers.doc_writer'''
7
8
9 import json
10 import os
11 import sys
12 if __name__ == '__main__':
13   sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..'))
14
15 import unittest
16 from xml.dom import minidom
17
18 from grit.format.policy_templates.writers import writer_unittest_common
19 from grit.format.policy_templates.writers import doc_writer
20
21
22 class MockMessageDictionary:
23   '''A mock dictionary passed to a writer as the dictionary of
24   localized messages.
25   '''
26
27   # Dictionary of messages.
28   msg_dict = {}
29
30 class DocWriterUnittest(writer_unittest_common.WriterUnittestCommon):
31   '''Unit tests for DocWriter.'''
32
33   def setUp(self):
34     # Create a writer for the tests.
35     self.writer = doc_writer.GetWriter(
36       config={
37         'app_name': 'Chrome',
38         'frame_name': 'Chrome Frame',
39         'os_name': 'Chrome OS',
40         'win_reg_mandatory_key_name': 'MockKey',
41         'win_reg_recommended_key_name': 'MockKeyRec',
42         'build': 'test_product',
43       })
44     self.writer.messages = {
45       'doc_back_to_top': {'text': '_test_back_to_top'},
46       'doc_complex_policies_on_windows': {'text': '_test_complex_policies_win'},
47       'doc_data_type': {'text': '_test_data_type'},
48       'doc_description': {'text': '_test_description'},
49       'doc_description_column_title': {
50         'text': '_test_description_column_title'
51       },
52       'doc_example_value': {'text': '_test_example_value'},
53       'doc_feature_dynamic_refresh': {'text': '_test_feature_dynamic_refresh'},
54       'doc_feature_can_be_recommended': {'text': '_test_feature_recommended'},
55       'doc_feature_can_be_mandatory': {'text': '_test_feature_mandatory'},
56       'doc_intro': {'text': '_test_intro'},
57       'doc_mac_linux_pref_name': {'text': '_test_mac_linux_pref_name'},
58       'doc_note': {'text': '_test_note'},
59       'doc_name_column_title': {'text': '_test_name_column_title'},
60       'doc_not_supported': {'text': '_test_not_supported'},
61       'doc_since_version': {'text': '_test_since_version'},
62       'doc_supported': {'text': '_test_supported'},
63       'doc_supported_features': {'text': '_test_supported_features'},
64       'doc_supported_on': {'text': '_test_supported_on'},
65       'doc_win_reg_loc': {'text': '_test_win_reg_loc'},
66
67       'doc_bla': {'text': '_test_bla'},
68     }
69     self.writer.Init()
70
71     # It is not worth testing the exact content of style attributes.
72     # Therefore we override them here with shorter texts.
73     for key in self.writer._STYLE.keys():
74       self.writer._STYLE[key] = 'style_%s;' % key
75     # Add some more style attributes for additional testing.
76     self.writer._STYLE['key1'] = 'style1;'
77     self.writer._STYLE['key2'] = 'style2;'
78
79     # Create a DOM document for the tests.
80     dom_impl = minidom.getDOMImplementation('')
81     self.doc = dom_impl.createDocument(None, 'root', None)
82     self.doc_root = self.doc.documentElement
83
84   def testSkeleton(self):
85     # Test if DocWriter creates the skeleton of the document correctly.
86     self.writer.BeginTemplate()
87     self.assertEquals(
88         self.writer._main_div.toxml(),
89         '<div>'
90           '<div>'
91             '<a name="top"/><br/>_test_intro<br/><br/><br/>'
92             '<table style="style_table;">'
93               '<thead><tr style="style_tr;">'
94                 '<td style="style_td;style_td.left;style_thead td;">'
95                   '_test_name_column_title'
96                 '</td>'
97                 '<td style="style_td;style_td.right;style_thead td;">'
98                   '_test_description_column_title'
99                 '</td>'
100               '</tr></thead>'
101               '<tbody/>'
102             '</table>'
103           '</div>'
104           '<div/>'
105         '</div>')
106
107   def testVersionAnnotation(self):
108     # Test if DocWriter creates the skeleton of the document correctly.
109     self.writer.config['version'] = '39.0.0.0'
110     self.writer.BeginTemplate()
111     self.assertEquals(
112         self.writer._main_div.toxml(),
113         '<div>'
114           '<!--test_product version: 39.0.0.0-->'
115           '<div>'
116             '<a name="top"/><br/>_test_intro<br/><br/><br/>'
117             '<table style="style_table;">'
118               '<thead><tr style="style_tr;">'
119                 '<td style="style_td;style_td.left;style_thead td;">'
120                   '_test_name_column_title'
121                 '</td>'
122                 '<td style="style_td;style_td.right;style_thead td;">'
123                   '_test_description_column_title'
124                 '</td>'
125               '</tr></thead>'
126               '<tbody/>'
127             '</table>'
128           '</div>'
129           '<div/>'
130         '</div>')
131
132   def testGetLocalizedMessage(self):
133     # Test if localized messages are retrieved correctly.
134     self.writer.messages = {
135       'doc_hello_world': {'text': 'hello, vilag!'}
136     }
137     self.assertEquals(
138         self.writer._GetLocalizedMessage('hello_world'),
139         'hello, vilag!')
140
141   def testMapListToString(self):
142     # Test function DocWriter.MapListToString()
143     self.assertEquals(
144         self.writer._MapListToString({'a1': 'a2', 'b1': 'b2'}, ['a1', 'b1']),
145         'a2, b2')
146     self.assertEquals(
147         self.writer._MapListToString({'a1': 'a2', 'b1': 'b2'}, []),
148         '')
149     result = self.writer._MapListToString(
150         {'a': '1', 'b': '2', 'c': '3', 'd': '4'}, ['b', 'd'])
151     expected_result = '2, 4'
152     self.assertEquals(
153         result,
154         expected_result)
155
156   def testAddStyledElement(self):
157     # Test function DocWriter.AddStyledElement()
158
159     # Test the case of zero style.
160     e1 = self.writer._AddStyledElement(
161         self.doc_root, 'z', [], {'a': 'b'}, 'text')
162     self.assertEquals(
163         e1.toxml(),
164         '<z a="b">text</z>')
165
166     # Test the case of one style.
167     e2 = self.writer._AddStyledElement(
168         self.doc_root, 'z', ['key1'], {'a': 'b'}, 'text')
169     self.assertEquals(
170         e2.toxml(),
171         '<z a="b" style="style1;">text</z>')
172
173     # Test the case of two styles.
174     e3 = self.writer._AddStyledElement(
175         self.doc_root, 'z', ['key1', 'key2'], {'a': 'b'}, 'text')
176     self.assertEquals(
177         e3.toxml(),
178         '<z a="b" style="style1;style2;">text</z>')
179
180   def testAddDescriptionIntEnum(self):
181     # Test if URLs are replaced and choices of 'int-enum' policies are listed
182     # correctly.
183     policy = {
184       'type': 'int-enum',
185       'items': [
186         {'value': 0, 'caption': 'Disable foo'},
187         {'value': 2, 'caption': 'Solve your problem'},
188         {'value': 5, 'caption': 'Enable bar'},
189       ],
190       'desc': '''This policy disables foo, except in case of bar.
191 See http://policy-explanation.example.com for more details.
192 '''
193     }
194     self.writer._AddDescription(self.doc_root, policy)
195     self.assertEquals(
196         self.doc_root.toxml(),
197         '''<root>This policy disables foo, except in case of bar.
198 See <a href="http://policy-explanation.example.com">http://policy-explanation.example.com</a> for more details.
199 <ul><li>0 = Disable foo</li><li>2 = Solve your problem</li><li>5 = Enable bar</li></ul></root>''')
200
201   def testAddDescriptionStringEnum(self):
202     # Test if URLs are replaced and choices of 'int-enum' policies are listed
203     # correctly.
204     policy = {
205       'type': 'string-enum',
206       'items': [
207         {'value': "one", 'caption': 'Disable foo'},
208         {'value': "two", 'caption': 'Solve your problem'},
209         {'value': "three", 'caption': 'Enable bar'},
210       ],
211       'desc': '''This policy disables foo, except in case of bar.
212 See http://policy-explanation.example.com for more details.
213 '''
214     }
215     self.writer._AddDescription(self.doc_root, policy)
216     self.assertEquals(
217         self.doc_root.toxml(),
218         '''<root>This policy disables foo, except in case of bar.
219 See <a href="http://policy-explanation.example.com">http://policy-explanation.example.com</a> for more details.
220 <ul><li>&quot;one&quot; = Disable foo</li><li>&quot;two&quot; = Solve your problem</li><li>&quot;three&quot; = Enable bar</li></ul></root>''')
221
222   def testAddFeatures(self):
223     # Test if the list of features of a policy is handled correctly.
224     policy = {
225       'features': {
226         'spaceship_docking': False,
227         'dynamic_refresh': True,
228         'can_be_recommended': True,
229       }
230     }
231     self.writer._FEATURE_MAP = {
232       'can_be_recommended': 'Can Be Recommended',
233       'dynamic_refresh': 'Dynamic Refresh',
234       'spaceship_docking': 'Spaceship Docking',
235     }
236     self.writer._AddFeatures(self.doc_root, policy)
237     self.assertEquals(
238         self.doc_root.toxml(),
239         '<root>'
240           'Can Be Recommended: _test_supported, '
241           'Dynamic Refresh: _test_supported, '
242           'Spaceship Docking: _test_not_supported'
243         '</root>')
244
245   def testAddListExample(self):
246     policy = {
247       'name': 'PolicyName',
248       'example_value': ['Foo', 'Bar'],
249       'supported_on': [ { 'platforms': ['win', 'mac', 'linux'] } ]
250     }
251     self.writer._AddListExample(self.doc_root, policy)
252     self.assertEquals(
253       self.doc_root.toxml(),
254       '<root>'
255         '<dl style="style_dd dl;">'
256           '<dt>Windows:</dt>'
257           '<dd style="style_.monospace;style_.pre;">'
258             'MockKey\\PolicyName\\1 = &quot;Foo&quot;\n'
259             'MockKey\\PolicyName\\2 = &quot;Bar&quot;'
260           '</dd>'
261           '<dt>Linux:</dt>'
262           '<dd style="style_.monospace;">'
263             '[&quot;Foo&quot;, &quot;Bar&quot;]'
264           '</dd>'
265           '<dt>Mac:</dt>'
266           '<dd style="style_.monospace;style_.pre;">'
267             '&lt;array&gt;\n'
268             '  &lt;string&gt;Foo&lt;/string&gt;\n'
269             '  &lt;string&gt;Bar&lt;/string&gt;\n'
270             '&lt;/array&gt;'
271           '</dd>'
272         '</dl>'
273       '</root>')
274
275   def testBoolExample(self):
276     # Test representation of boolean example values.
277     policy = {
278       'name': 'PolicyName',
279       'type': 'main',
280       'example_value': True,
281       'supported_on': [ { 'platforms': ['win', 'mac', 'linux'] } ]
282     }
283     e1 = self.writer.AddElement(self.doc_root, 'e1')
284     self.writer._AddExample(e1, policy)
285     self.assertEquals(
286         e1.toxml(),
287         '<e1>0x00000001 (Windows), true (Linux), &lt;true /&gt; (Mac)</e1>')
288
289     policy = {
290       'name': 'PolicyName',
291       'type': 'main',
292       'example_value': False,
293       'supported_on': [ { 'platforms': ['win', 'mac', 'linux'] } ]
294     }
295     e2 = self.writer.AddElement(self.doc_root, 'e2')
296     self.writer._AddExample(e2, policy)
297     self.assertEquals(
298         e2.toxml(),
299         '<e2>0x00000000 (Windows), false (Linux), &lt;false /&gt; (Mac)</e2>')
300
301   def testIntEnumExample(self):
302     # Test representation of 'int-enum' example values.
303     policy = {
304       'name': 'PolicyName',
305       'type': 'int-enum',
306       'example_value': 16,
307       'supported_on': [ { 'platforms': ['win', 'mac', 'linux'] } ]
308     }
309     self.writer._AddExample(self.doc_root, policy)
310     self.assertEquals(
311         self.doc_root.toxml(),
312         '<root>0x00000010 (Windows), 16 (Linux), 16 (Mac)</root>')
313
314   def testStringEnumExample(self):
315     # Test representation of 'string-enum' example values.
316     policy = {
317       'name': 'PolicyName',
318       'type': 'string-enum',
319       'example_value': "wacky"
320     }
321     self.writer._AddExample(self.doc_root, policy)
322     self.assertEquals(
323         self.doc_root.toxml(),
324         '<root>&quot;wacky&quot;</root>')
325
326   def testListExample(self):
327     # Test representation of 'list' example values.
328     policy = {
329       'name': 'PolicyName',
330       'type': 'list',
331       'example_value': ['one', 'two'],
332       'supported_on': [ { 'platforms': ['linux'] } ]
333     }
334     self.writer._AddExample(self.doc_root, policy)
335     self.assertEquals(
336         self.doc_root.toxml(),
337         '<root><dl style="style_dd dl;">'
338         '<dt>Linux:</dt>'
339         '<dd style="style_.monospace;">'
340         '[&quot;one&quot;, &quot;two&quot;]'
341         '</dd></dl></root>')
342
343   def testStringEnumListExample(self):
344     # Test representation of 'string-enum-list' example values.
345     policy = {
346       'name': 'PolicyName',
347       'type': 'string-enum-list',
348       'example_value': ['one', 'two'],
349       'supported_on': [ { 'platforms': ['linux'] } ]
350     }
351     self.writer._AddExample(self.doc_root, policy)
352     self.assertEquals(
353         self.doc_root.toxml(),
354         '<root><dl style="style_dd dl;">'
355         '<dt>Linux:</dt>'
356         '<dd style="style_.monospace;">'
357         '[&quot;one&quot;, &quot;two&quot;]'
358         '</dd></dl></root>')
359
360   def testStringExample(self):
361     # Test representation of 'string' example values.
362     policy = {
363       'name': 'PolicyName',
364       'type': 'string',
365       'example_value': 'awesome-example'
366     }
367     self.writer._AddExample(self.doc_root, policy)
368     self.assertEquals(
369         self.doc_root.toxml(),
370         '<root>&quot;awesome-example&quot;</root>')
371
372   def testIntExample(self):
373     # Test representation of 'int' example values.
374     policy = {
375       'name': 'PolicyName',
376       'type': 'int',
377       'example_value': 26,
378       'supported_on': [ { 'platforms': ['win', 'mac', 'linux'] } ]
379     }
380     self.writer._AddExample(self.doc_root, policy)
381     self.assertEquals(
382         self.doc_root.toxml(),
383         '<root>0x0000001a (Windows), 26 (Linux), 26 (Mac)</root>')
384
385   def testAddPolicyAttribute(self):
386     # Test creating a policy attribute term-definition pair.
387     self.writer._AddPolicyAttribute(
388         self.doc_root, 'bla', 'hello, world', ['key1'])
389     self.assertEquals(
390         self.doc_root.toxml(),
391         '<root>'
392           '<dt style="style_dt;">_test_bla</dt>'
393           '<dd style="style1;">hello, world</dd>'
394         '</root>')
395
396   def testAddPolicyDetails(self):
397     # Test if the definition list (<dl>) of policy details is created correctly.
398     policy = {
399       'type': 'main',
400       'name': 'TestPolicyName',
401       'caption': 'TestPolicyCaption',
402       'desc': 'TestPolicyDesc',
403       'supported_on': [{
404         'product': 'chrome',
405         'platforms': ['win', 'mac', 'linux'],
406         'since_version': '8',
407         'until_version': '',
408       }, {
409         'product': 'chrome',
410         'platforms': ['android'],
411         'since_version': '30',
412         'until_version': '',
413       }, {
414         'product': 'chrome',
415         'platforms': ['ios'],
416         'since_version': '34',
417         'until_version': '',
418       }],
419       'features': {'dynamic_refresh': False},
420       'example_value': False
421     }
422     self.writer.messages['doc_since_version'] = {'text': '...$6...'}
423     self.writer._AddPolicyDetails(self.doc_root, policy)
424     self.assertEquals(
425       self.doc_root.toxml(),
426       '<root><dl>'
427       '<dt style="style_dt;">_test_data_type</dt><dd>Boolean (REG_DWORD)</dd>'
428       '<dt style="style_dt;">_test_win_reg_loc</dt>'
429       '<dd style="style_.monospace;">MockKey\TestPolicyName</dd>'
430       '<dt style="style_dt;">_test_mac_linux_pref_name</dt>'
431         '<dd style="style_.monospace;">TestPolicyName</dd>'
432       '<dt style="style_dt;">_test_supported_on</dt>'
433       '<dd>'
434         '<ul style="style_ul;">'
435           '<li>Chrome (Windows, Mac, Linux) ...8...</li>'
436           '<li>Chrome (Android) ...30...</li>'
437           '<li>Chrome (iOS) ...34...</li>'
438         '</ul>'
439       '</dd>'
440       '<dt style="style_dt;">_test_supported_features</dt>'
441         '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>'
442       '<dt style="style_dt;">_test_description</dt><dd>TestPolicyDesc</dd>'
443       '<dt style="style_dt;">_test_example_value</dt>'
444         '<dd>0x00000000 (Windows), false (Linux), &lt;false /&gt; (Mac)</dd>'
445       '</dl></root>')
446
447   def testAddDictPolicyDetails(self):
448     # Test if the definition list (<dl>) of policy details is created correctly
449     # for 'dict' policies.
450     policy = {
451       'type': 'dict',
452       'name': 'TestPolicyName',
453       'caption': 'TestPolicyCaption',
454       'desc': 'TestPolicyDesc',
455       'supported_on': [{
456         'product': 'chrome',
457         'platforms': ['win', 'mac', 'linux'],
458         'since_version': '8',
459         'until_version': '',
460       }],
461       'features': {'dynamic_refresh': False},
462       'example_value': { 'foo': 123 }
463     }
464     self.writer.messages['doc_since_version'] = {'text': '...$6...'}
465     self.writer._AddPolicyDetails(self.doc_root, policy)
466     self.assertEquals(
467       self.doc_root.toxml(),
468       '<root><dl>'
469       '<dt style="style_dt;">_test_data_type</dt><dd>Dictionary (REG_SZ; _test_complex_policies_win)</dd>'
470       '<dt style="style_dt;">_test_win_reg_loc</dt>'
471       '<dd style="style_.monospace;">MockKey\TestPolicyName</dd>'
472       '<dt style="style_dt;">_test_mac_linux_pref_name</dt>'
473         '<dd style="style_.monospace;">TestPolicyName</dd>'
474       '<dt style="style_dt;">_test_supported_on</dt>'
475       '<dd>'
476         '<ul style="style_ul;">'
477           '<li>Chrome (Windows, Mac, Linux) ...8...</li>'
478         '</ul>'
479       '</dd>'
480       '<dt style="style_dt;">_test_supported_features</dt>'
481         '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>'
482       '<dt style="style_dt;">_test_description</dt><dd>TestPolicyDesc</dd>'
483       '<dt style="style_dt;">_test_example_value</dt>'
484         '<dd>'
485           '<dl style="style_dd dl;">'
486             '<dt>Windows:</dt>'
487             '<dd style="style_.monospace;style_.pre;">MockKey\TestPolicyName = {&quot;foo&quot;: 123}</dd>'
488             '<dt>Linux:</dt>'
489             '<dd style="style_.monospace;">TestPolicyName: {&quot;foo&quot;: 123}</dd>'
490             '<dt>Mac:</dt>'
491             '<dd style="style_.monospace;style_.pre;">'
492               '&lt;key&gt;TestPolicyName&lt;/key&gt;\n'
493               '&lt;dict&gt;\n'
494               '  &lt;key&gt;foo&lt;/key&gt;\n'
495               '  &lt;integer&gt;123&lt;/integer&gt;\n'
496               '&lt;/dict&gt;'
497             '</dd>'
498           '</dl>'
499         '</dd>'
500       '</dl></root>')
501
502   def testAddPolicyDetailsRecommendedOnly(self):
503     policy = {
504       'type': 'main',
505       'name': 'TestPolicyName',
506       'caption': 'TestPolicyCaption',
507       'desc': 'TestPolicyDesc',
508       'supported_on': [{
509         'product': 'chrome',
510         'platforms': ['win', 'mac', 'linux'],
511         'since_version': '8',
512         'until_version': '',
513       }, {
514         'product': 'chrome',
515         'platforms': ['android'],
516         'since_version': '30',
517         'until_version': '',
518       }, {
519         'product': 'chrome',
520         'platforms': ['ios'],
521         'since_version': '34',
522         'until_version': '',
523       }],
524       'features': {
525         'dynamic_refresh': False,
526         'can_be_mandatory': False,
527         'can_be_recommended': True
528       },
529       'example_value': False
530     }
531     self.writer.messages['doc_since_version'] = {'text': '...$6...'}
532     self.writer._AddPolicyDetails(self.doc_root, policy)
533     self.assertEquals(
534       self.doc_root.toxml(),
535       '<root><dl>'
536       '<dt style="style_dt;">_test_data_type</dt><dd>Boolean (REG_DWORD)</dd>'
537       '<dt style="style_dt;">_test_win_reg_loc</dt>'
538       '<dd style="style_.monospace;">MockKeyRec\TestPolicyName</dd>'
539       '<dt style="style_dt;">_test_mac_linux_pref_name</dt>'
540         '<dd style="style_.monospace;">TestPolicyName</dd>'
541       '<dt style="style_dt;">_test_supported_on</dt>'
542       '<dd>'
543         '<ul style="style_ul;">'
544           '<li>Chrome (Windows, Mac, Linux) ...8...</li>'
545           '<li>Chrome (Android) ...30...</li>'
546           '<li>Chrome (iOS) ...34...</li>'
547         '</ul>'
548       '</dd>'
549       '<dt style="style_dt;">_test_supported_features</dt>'
550         '<dd>_test_feature_mandatory: _test_not_supported,'
551         ' _test_feature_recommended: _test_supported,'
552         ' _test_feature_dynamic_refresh: _test_not_supported</dd>'
553       '<dt style="style_dt;">_test_description</dt><dd>TestPolicyDesc</dd>'
554       '<dt style="style_dt;">_test_example_value</dt>'
555         '<dd>0x00000000 (Windows), false (Linux), &lt;false /&gt; (Mac)</dd>'
556       '</dl></root>')
557
558   def testAddPolicyNote(self):
559     # TODO(jkummerow): The functionality tested by this test is currently not
560     # used for anything and will probably soon be removed.
561     # Test if nodes are correctly added to policies.
562     policy = {
563       'problem_href': 'http://www.example.com/5'
564     }
565     self.writer.messages['doc_note'] = {'text': '...$6...'}
566     self.writer._AddPolicyNote(self.doc_root, policy)
567     self.assertEquals(
568         self.doc_root.toxml(),
569         '<root><div style="style_div.note;">...'
570         '<a href="http://www.example.com/5">http://www.example.com/5</a>'
571         '...</div></root>')
572
573   def testAddPolicyRow(self):
574     # Test if policies are correctly added to the summary table.
575     policy = {
576       'name': 'PolicyName',
577       'caption': 'PolicyCaption',
578       'type': 'string',
579     }
580     self.writer._indent_level = 3
581     self.writer._AddPolicyRow(self.doc_root, policy)
582     self.assertEquals(
583       self.doc_root.toxml(),
584       '<root><tr style="style_tr;">'
585       '<td style="style_td;style_td.left;padding-left: 49px;">'
586         '<a href="#PolicyName">PolicyName</a>'
587       '</td>'
588       '<td style="style_td;style_td.right;">PolicyCaption</td>'
589       '</tr></root>')
590     self.setUp()
591     policy = {
592       'name': 'PolicyName',
593       'caption': 'PolicyCaption',
594       'type': 'group',
595     }
596     self.writer._indent_level = 2
597     self.writer._AddPolicyRow(self.doc_root, policy)
598     self.assertEquals(
599       self.doc_root.toxml(),
600       '<root><tr style="style_tr;">'
601       '<td colspan="2" style="style_td;style_td.left;padding-left: 35px;">'
602         '<a href="#PolicyName">PolicyCaption</a>'
603       '</td>'
604       '</tr></root>')
605
606   def testAddPolicySection(self):
607     # Test if policy details are correctly added to the document.
608     policy = {
609       'name': 'PolicyName',
610       'caption': 'PolicyCaption',
611       'desc': 'PolicyDesc',
612       'type': 'string',
613       'supported_on': [{
614         'product': 'chrome',
615         'platforms': ['win', 'mac'],
616         'since_version': '7',
617         'until_version': '',
618       }],
619       'features': {'dynamic_refresh': False},
620       'example_value': 'False'
621     }
622     self.writer.messages['doc_since_version'] = {'text': '..$6..'}
623     self.writer._AddPolicySection(self.doc_root, policy)
624     self.assertEquals(
625       self.doc_root.toxml(),
626       '<root>'
627         '<div style="margin-left: 0px">'
628           '<h3><a name="PolicyName"/>PolicyName</h3>'
629           '<span>PolicyCaption</span>'
630           '<dl>'
631             '<dt style="style_dt;">_test_data_type</dt>'
632             '<dd>String (REG_SZ)</dd>'
633             '<dt style="style_dt;">_test_win_reg_loc</dt>'
634             '<dd style="style_.monospace;">MockKey\\PolicyName</dd>'
635             '<dt style="style_dt;">_test_mac_linux_pref_name</dt>'
636             '<dd style="style_.monospace;">PolicyName</dd>'
637             '<dt style="style_dt;">_test_supported_on</dt>'
638             '<dd>'
639               '<ul style="style_ul;">'
640                 '<li>Chrome (Windows, Mac) ..7..</li>'
641               '</ul>'
642             '</dd>'
643             '<dt style="style_dt;">_test_supported_features</dt>'
644             '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>'
645             '<dt style="style_dt;">_test_description</dt>'
646             '<dd>PolicyDesc</dd>'
647             '<dt style="style_dt;">_test_example_value</dt>'
648             '<dd>&quot;False&quot;</dd>'
649           '</dl>'
650           '<a href="#top">_test_back_to_top</a>'
651         '</div>'
652       '</root>')
653     # Test for groups.
654     self.setUp()
655     policy['type'] = 'group'
656     self.writer._AddPolicySection(self.doc_root, policy)
657     self.assertEquals(
658       self.doc_root.toxml(),
659       '<root>'
660         '<div style="margin-left: 0px">'
661           '<h2><a name="PolicyName"/>PolicyCaption</h2>'
662           '<div style="style_div.group_desc;">PolicyDesc</div>'
663           '<a href="#top">_test_back_to_top</a>'
664         '</div>'
665       '</root>')
666
667   def testAddPolicySectionForWindowsOnly(self):
668     policy = {
669       'name': 'PolicyName',
670       'caption': 'PolicyCaption',
671       'desc': 'PolicyDesc',
672       'type': 'int',
673       'supported_on': [{
674         'product': 'chrome',
675         'platforms': ['win'],
676         'since_version': '33',
677         'until_version': '',
678       }],
679       'features': {'dynamic_refresh': False},
680       'example_value': 123
681     }
682     self.writer.messages['doc_since_version'] = {'text': '..$6..'}
683     self.writer._AddPolicySection(self.doc_root, policy)
684     self.assertEquals(
685       self.doc_root.toxml(),
686       '<root>'
687         '<div style="margin-left: 0px">'
688           '<h3><a name="PolicyName"/>PolicyName</h3>'
689           '<span>PolicyCaption</span>'
690           '<dl>'
691             '<dt style="style_dt;">_test_data_type</dt>'
692             '<dd>Integer (REG_DWORD)</dd>'
693             '<dt style="style_dt;">_test_win_reg_loc</dt>'
694             '<dd style="style_.monospace;">MockKey\\PolicyName</dd>'
695             '<dt style="style_dt;">_test_supported_on</dt>'
696             '<dd>'
697               '<ul style="style_ul;">'
698                 '<li>Chrome (Windows) ..33..</li>'
699               '</ul>'
700             '</dd>'
701             '<dt style="style_dt;">_test_supported_features</dt>'
702             '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>'
703             '<dt style="style_dt;">_test_description</dt>'
704             '<dd>PolicyDesc</dd>'
705             '<dt style="style_dt;">_test_example_value</dt>'
706             '<dd>0x0000007b (Windows)</dd>'
707           '</dl>'
708           '<a href="#top">_test_back_to_top</a>'
709         '</div>'
710       '</root>')
711
712   def testAddPolicySectionForMacOnly(self):
713     policy = {
714       'name': 'PolicyName',
715       'caption': 'PolicyCaption',
716       'desc': 'PolicyDesc',
717       'type': 'int',
718       'supported_on': [{
719         'product': 'chrome',
720         'platforms': ['mac'],
721         'since_version': '33',
722         'until_version': '',
723       }],
724       'features': {'dynamic_refresh': False},
725       'example_value': 123
726     }
727     self.writer.messages['doc_since_version'] = {'text': '..$6..'}
728     self.writer._AddPolicySection(self.doc_root, policy)
729     self.assertEquals(
730       self.doc_root.toxml(),
731       '<root>'
732         '<div style="margin-left: 0px">'
733           '<h3><a name="PolicyName"/>PolicyName</h3>'
734           '<span>PolicyCaption</span>'
735           '<dl>'
736             '<dt style="style_dt;">_test_data_type</dt>'
737             '<dd>Integer</dd>'
738             '<dt style="style_dt;">_test_mac_linux_pref_name</dt>'
739             '<dd style="style_.monospace;">PolicyName</dd>'
740             '<dt style="style_dt;">_test_supported_on</dt>'
741             '<dd>'
742               '<ul style="style_ul;">'
743                 '<li>Chrome (Mac) ..33..</li>'
744               '</ul>'
745             '</dd>'
746             '<dt style="style_dt;">_test_supported_features</dt>'
747             '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>'
748             '<dt style="style_dt;">_test_description</dt>'
749             '<dd>PolicyDesc</dd>'
750             '<dt style="style_dt;">_test_example_value</dt>'
751             '<dd>123 (Mac)</dd>'
752           '</dl>'
753           '<a href="#top">_test_back_to_top</a>'
754         '</div>'
755       '</root>')
756
757   def testAddPolicySectionForLinuxOnly(self):
758     policy = {
759       'name': 'PolicyName',
760       'caption': 'PolicyCaption',
761       'desc': 'PolicyDesc',
762       'type': 'int',
763       'supported_on': [{
764         'product': 'chrome',
765         'platforms': ['linux'],
766         'since_version': '33',
767         'until_version': '',
768       }],
769       'features': {'dynamic_refresh': False},
770       'example_value': 123
771     }
772     self.writer.messages['doc_since_version'] = {'text': '..$6..'}
773     self.writer._AddPolicySection(self.doc_root, policy)
774     self.assertEquals(
775       self.doc_root.toxml(),
776       '<root>'
777         '<div style="margin-left: 0px">'
778           '<h3><a name="PolicyName"/>PolicyName</h3>'
779           '<span>PolicyCaption</span>'
780           '<dl>'
781             '<dt style="style_dt;">_test_data_type</dt>'
782             '<dd>Integer</dd>'
783             '<dt style="style_dt;">_test_mac_linux_pref_name</dt>'
784             '<dd style="style_.monospace;">PolicyName</dd>'
785             '<dt style="style_dt;">_test_supported_on</dt>'
786             '<dd>'
787               '<ul style="style_ul;">'
788                 '<li>Chrome (Linux) ..33..</li>'
789               '</ul>'
790             '</dd>'
791             '<dt style="style_dt;">_test_supported_features</dt>'
792             '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>'
793             '<dt style="style_dt;">_test_description</dt>'
794             '<dd>PolicyDesc</dd>'
795             '<dt style="style_dt;">_test_example_value</dt>'
796             '<dd>123 (Linux)</dd>'
797           '</dl>'
798           '<a href="#top">_test_back_to_top</a>'
799         '</div>'
800       '</root>')
801
802   def testAddDictionaryExample(self):
803     policy = {
804       'name': 'PolicyName',
805       'caption': 'PolicyCaption',
806       'desc': 'PolicyDesc',
807       'type': 'dict',
808       'supported_on': [{
809         'product': 'chrome',
810         'platforms': ['win', 'mac', 'linux'],
811         'since_version': '7',
812         'until_version': '',
813       }],
814       'features': {'dynamic_refresh': False},
815       'example_value': {
816         "ProxyMode": "direct",
817         "List": ["1", "2", "3"],
818         "True": True,
819         "False": False,
820         "Integer": 123,
821         "DictList": [ {
822             "A": 1,
823             "B": 2,
824           }, {
825             "C": 3,
826             "D": 4,
827           },
828         ],
829       },
830     }
831     self.writer._AddDictionaryExample(self.doc_root, policy)
832     value = json.dumps(policy['example_value']).replace('"', '&quot;')
833     self.assertEquals(
834       self.doc_root.toxml(),
835       '<root>'
836         '<dl style="style_dd dl;">'
837           '<dt>Windows:</dt>'
838           '<dd style="style_.monospace;style_.pre;">MockKey\PolicyName = '
839               + value +
840           '</dd>'
841           '<dt>Linux:</dt>'
842           '<dd style="style_.monospace;">PolicyName: ' + value + '</dd>'
843           '<dt>Mac:</dt>'
844           '<dd style="style_.monospace;style_.pre;">'
845             '&lt;key&gt;PolicyName&lt;/key&gt;\n'
846             '&lt;dict&gt;\n'
847             '  &lt;key&gt;DictList&lt;/key&gt;\n'
848             '  &lt;array&gt;\n'
849             '    &lt;dict&gt;\n'
850             '      &lt;key&gt;A&lt;/key&gt;\n'
851             '      &lt;integer&gt;1&lt;/integer&gt;\n'
852             '      &lt;key&gt;B&lt;/key&gt;\n'
853             '      &lt;integer&gt;2&lt;/integer&gt;\n'
854             '    &lt;/dict&gt;\n'
855             '    &lt;dict&gt;\n'
856             '      &lt;key&gt;C&lt;/key&gt;\n'
857             '      &lt;integer&gt;3&lt;/integer&gt;\n'
858             '      &lt;key&gt;D&lt;/key&gt;\n'
859             '      &lt;integer&gt;4&lt;/integer&gt;\n'
860             '    &lt;/dict&gt;\n'
861             '  &lt;/array&gt;\n'
862             '  &lt;key&gt;False&lt;/key&gt;\n'
863             '  &lt;false/&gt;\n'
864             '  &lt;key&gt;Integer&lt;/key&gt;\n'
865             '  &lt;integer&gt;123&lt;/integer&gt;\n'
866             '  &lt;key&gt;List&lt;/key&gt;\n'
867             '  &lt;array&gt;\n'
868             '    &lt;string&gt;1&lt;/string&gt;\n'
869             '    &lt;string&gt;2&lt;/string&gt;\n'
870             '    &lt;string&gt;3&lt;/string&gt;\n'
871             '  &lt;/array&gt;\n'
872             '  &lt;key&gt;ProxyMode&lt;/key&gt;\n'
873             '  &lt;string&gt;direct&lt;/string&gt;\n'
874             '  &lt;key&gt;True&lt;/key&gt;\n'
875             '  &lt;true/&gt;\n'
876             '&lt;/dict&gt;'
877           '</dd>'
878         '</dl>'
879       '</root>')
880
881
882 if __name__ == '__main__':
883   unittest.main()