Upstream version 10.39.225.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       })
43     self.writer.messages = {
44       'doc_back_to_top': {'text': '_test_back_to_top'},
45       'doc_complex_policies_on_windows': {'text': '_test_complex_policies_win'},
46       'doc_data_type': {'text': '_test_data_type'},
47       'doc_description': {'text': '_test_description'},
48       'doc_description_column_title': {
49         'text': '_test_description_column_title'
50       },
51       'doc_example_value': {'text': '_test_example_value'},
52       'doc_feature_dynamic_refresh': {'text': '_test_feature_dynamic_refresh'},
53       'doc_feature_can_be_recommended': {'text': '_test_feature_recommended'},
54       'doc_feature_can_be_mandatory': {'text': '_test_feature_mandatory'},
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.example.com</a> for more details.
173 <ul><li>0 = Disable foo</li><li>2 = Solve your problem</li><li>5 = Enable bar</li></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.example.com</a> for more details.
194 <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>''')
195
196   def testAddFeatures(self):
197     # Test if the list of features of a policy is handled correctly.
198     policy = {
199       'features': {
200         'spaceship_docking': False,
201         'dynamic_refresh': True,
202         'can_be_recommended': True,
203       }
204     }
205     self.writer._FEATURE_MAP = {
206       'can_be_recommended': 'Can Be Recommended',
207       'dynamic_refresh': 'Dynamic Refresh',
208       'spaceship_docking': 'Spaceship Docking',
209     }
210     self.writer._AddFeatures(self.doc_root, policy)
211     self.assertEquals(
212         self.doc_root.toxml(),
213         '<root>'
214           'Can Be Recommended: _test_supported, '
215           'Dynamic Refresh: _test_supported, '
216           'Spaceship Docking: _test_not_supported'
217         '</root>')
218
219   def testAddListExample(self):
220     policy = {
221       'name': 'PolicyName',
222       'example_value': ['Foo', 'Bar'],
223       'supported_on': [ { 'platforms': ['win', 'mac', 'linux'] } ]
224     }
225     self.writer._AddListExample(self.doc_root, policy)
226     self.assertEquals(
227       self.doc_root.toxml(),
228       '<root>'
229         '<dl style="style_dd dl;">'
230           '<dt>Windows:</dt>'
231           '<dd style="style_.monospace;style_.pre;">'
232             'MockKey\\PolicyName\\1 = &quot;Foo&quot;\n'
233             'MockKey\\PolicyName\\2 = &quot;Bar&quot;'
234           '</dd>'
235           '<dt>Linux:</dt>'
236           '<dd style="style_.monospace;">'
237             '[&quot;Foo&quot;, &quot;Bar&quot;]'
238           '</dd>'
239           '<dt>Mac:</dt>'
240           '<dd style="style_.monospace;style_.pre;">'
241             '&lt;array&gt;\n'
242             '  &lt;string&gt;Foo&lt;/string&gt;\n'
243             '  &lt;string&gt;Bar&lt;/string&gt;\n'
244             '&lt;/array&gt;'
245           '</dd>'
246         '</dl>'
247       '</root>')
248
249   def testBoolExample(self):
250     # Test representation of boolean example values.
251     policy = {
252       'name': 'PolicyName',
253       'type': 'main',
254       'example_value': True,
255       'supported_on': [ { 'platforms': ['win', 'mac', 'linux'] } ]
256     }
257     e1 = self.writer.AddElement(self.doc_root, 'e1')
258     self.writer._AddExample(e1, policy)
259     self.assertEquals(
260         e1.toxml(),
261         '<e1>0x00000001 (Windows), true (Linux), &lt;true /&gt; (Mac)</e1>')
262
263     policy = {
264       'name': 'PolicyName',
265       'type': 'main',
266       'example_value': False,
267       'supported_on': [ { 'platforms': ['win', 'mac', 'linux'] } ]
268     }
269     e2 = self.writer.AddElement(self.doc_root, 'e2')
270     self.writer._AddExample(e2, policy)
271     self.assertEquals(
272         e2.toxml(),
273         '<e2>0x00000000 (Windows), false (Linux), &lt;false /&gt; (Mac)</e2>')
274
275   def testIntEnumExample(self):
276     # Test representation of 'int-enum' example values.
277     policy = {
278       'name': 'PolicyName',
279       'type': 'int-enum',
280       'example_value': 16,
281       'supported_on': [ { 'platforms': ['win', 'mac', 'linux'] } ]
282     }
283     self.writer._AddExample(self.doc_root, policy)
284     self.assertEquals(
285         self.doc_root.toxml(),
286         '<root>0x00000010 (Windows), 16 (Linux), 16 (Mac)</root>')
287
288   def testStringEnumExample(self):
289     # Test representation of 'string-enum' example values.
290     policy = {
291       'name': 'PolicyName',
292       'type': 'string-enum',
293       'example_value': "wacky"
294     }
295     self.writer._AddExample(self.doc_root, policy)
296     self.assertEquals(
297         self.doc_root.toxml(),
298         '<root>&quot;wacky&quot;</root>')
299
300   def testListExample(self):
301     # Test representation of 'list' example values.
302     policy = {
303       'name': 'PolicyName',
304       'type': 'list',
305       'example_value': ['one', 'two'],
306       'supported_on': [ { 'platforms': ['linux'] } ]
307     }
308     self.writer._AddExample(self.doc_root, policy)
309     self.assertEquals(
310         self.doc_root.toxml(),
311         '<root><dl style="style_dd dl;">'
312         '<dt>Linux:</dt>'
313         '<dd style="style_.monospace;">'
314         '[&quot;one&quot;, &quot;two&quot;]'
315         '</dd></dl></root>')
316
317   def testStringEnumListExample(self):
318     # Test representation of 'string-enum-list' example values.
319     policy = {
320       'name': 'PolicyName',
321       'type': 'string-enum-list',
322       'example_value': ['one', 'two'],
323       'supported_on': [ { 'platforms': ['linux'] } ]
324     }
325     self.writer._AddExample(self.doc_root, policy)
326     self.assertEquals(
327         self.doc_root.toxml(),
328         '<root><dl style="style_dd dl;">'
329         '<dt>Linux:</dt>'
330         '<dd style="style_.monospace;">'
331         '[&quot;one&quot;, &quot;two&quot;]'
332         '</dd></dl></root>')
333
334   def testStringExample(self):
335     # Test representation of 'string' example values.
336     policy = {
337       'name': 'PolicyName',
338       'type': 'string',
339       'example_value': 'awesome-example'
340     }
341     self.writer._AddExample(self.doc_root, policy)
342     self.assertEquals(
343         self.doc_root.toxml(),
344         '<root>&quot;awesome-example&quot;</root>')
345
346   def testIntExample(self):
347     # Test representation of 'int' example values.
348     policy = {
349       'name': 'PolicyName',
350       'type': 'int',
351       'example_value': 26,
352       'supported_on': [ { 'platforms': ['win', 'mac', 'linux'] } ]
353     }
354     self.writer._AddExample(self.doc_root, policy)
355     self.assertEquals(
356         self.doc_root.toxml(),
357         '<root>0x0000001a (Windows), 26 (Linux), 26 (Mac)</root>')
358
359   def testAddPolicyAttribute(self):
360     # Test creating a policy attribute term-definition pair.
361     self.writer._AddPolicyAttribute(
362         self.doc_root, 'bla', 'hello, world', ['key1'])
363     self.assertEquals(
364         self.doc_root.toxml(),
365         '<root>'
366           '<dt style="style_dt;">_test_bla</dt>'
367           '<dd style="style1;">hello, world</dd>'
368         '</root>')
369
370   def testAddPolicyDetails(self):
371     # Test if the definition list (<dl>) of policy details is created correctly.
372     policy = {
373       'type': 'main',
374       'name': 'TestPolicyName',
375       'caption': 'TestPolicyCaption',
376       'desc': 'TestPolicyDesc',
377       'supported_on': [{
378         'product': 'chrome',
379         'platforms': ['win', 'mac', 'linux'],
380         'since_version': '8',
381         'until_version': '',
382       }, {
383         'product': 'chrome',
384         'platforms': ['android'],
385         'since_version': '30',
386         'until_version': '',
387       }, {
388         'product': 'chrome',
389         'platforms': ['ios'],
390         'since_version': '34',
391         'until_version': '',
392       }],
393       'features': {'dynamic_refresh': False},
394       'example_value': False
395     }
396     self.writer.messages['doc_since_version'] = {'text': '...$6...'}
397     self.writer._AddPolicyDetails(self.doc_root, policy)
398     self.assertEquals(
399       self.doc_root.toxml(),
400       '<root><dl>'
401       '<dt style="style_dt;">_test_data_type</dt><dd>Boolean (REG_DWORD)</dd>'
402       '<dt style="style_dt;">_test_win_reg_loc</dt>'
403       '<dd style="style_.monospace;">MockKey\TestPolicyName</dd>'
404       '<dt style="style_dt;">_test_mac_linux_pref_name</dt>'
405         '<dd style="style_.monospace;">TestPolicyName</dd>'
406       '<dt style="style_dt;">_test_supported_on</dt>'
407       '<dd>'
408         '<ul style="style_ul;">'
409           '<li>Chrome (Windows, Mac, Linux) ...8...</li>'
410           '<li>Chrome (Android) ...30...</li>'
411           '<li>Chrome (iOS) ...34...</li>'
412         '</ul>'
413       '</dd>'
414       '<dt style="style_dt;">_test_supported_features</dt>'
415         '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>'
416       '<dt style="style_dt;">_test_description</dt><dd>TestPolicyDesc</dd>'
417       '<dt style="style_dt;">_test_example_value</dt>'
418         '<dd>0x00000000 (Windows), false (Linux), &lt;false /&gt; (Mac)</dd>'
419       '</dl></root>')
420
421   def testAddDictPolicyDetails(self):
422     # Test if the definition list (<dl>) of policy details is created correctly
423     # for 'dict' policies.
424     policy = {
425       'type': 'dict',
426       'name': 'TestPolicyName',
427       'caption': 'TestPolicyCaption',
428       'desc': 'TestPolicyDesc',
429       'supported_on': [{
430         'product': 'chrome',
431         'platforms': ['win', 'mac', 'linux'],
432         'since_version': '8',
433         'until_version': '',
434       }],
435       'features': {'dynamic_refresh': False},
436       'example_value': { 'foo': 123 }
437     }
438     self.writer.messages['doc_since_version'] = {'text': '...$6...'}
439     self.writer._AddPolicyDetails(self.doc_root, policy)
440     self.assertEquals(
441       self.doc_root.toxml(),
442       '<root><dl>'
443       '<dt style="style_dt;">_test_data_type</dt><dd>Dictionary (REG_SZ; _test_complex_policies_win)</dd>'
444       '<dt style="style_dt;">_test_win_reg_loc</dt>'
445       '<dd style="style_.monospace;">MockKey\TestPolicyName</dd>'
446       '<dt style="style_dt;">_test_mac_linux_pref_name</dt>'
447         '<dd style="style_.monospace;">TestPolicyName</dd>'
448       '<dt style="style_dt;">_test_supported_on</dt>'
449       '<dd>'
450         '<ul style="style_ul;">'
451           '<li>Chrome (Windows, Mac, Linux) ...8...</li>'
452         '</ul>'
453       '</dd>'
454       '<dt style="style_dt;">_test_supported_features</dt>'
455         '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>'
456       '<dt style="style_dt;">_test_description</dt><dd>TestPolicyDesc</dd>'
457       '<dt style="style_dt;">_test_example_value</dt>'
458         '<dd>'
459           '<dl style="style_dd dl;">'
460             '<dt>Windows:</dt>'
461             '<dd style="style_.monospace;style_.pre;">MockKey\TestPolicyName = {&quot;foo&quot;: 123}</dd>'
462             '<dt>Linux:</dt>'
463             '<dd style="style_.monospace;">TestPolicyName: {&quot;foo&quot;: 123}</dd>'
464             '<dt>Mac:</dt>'
465             '<dd style="style_.monospace;style_.pre;">'
466               '&lt;key&gt;TestPolicyName&lt;/key&gt;\n'
467               '&lt;dict&gt;\n'
468               '  &lt;key&gt;foo&lt;/key&gt;\n'
469               '  &lt;integer&gt;123&lt;/integer&gt;\n'
470               '&lt;/dict&gt;'
471             '</dd>'
472           '</dl>'
473         '</dd>'
474       '</dl></root>')
475
476   def testAddPolicyDetailsRecommendedOnly(self):
477     policy = {
478       'type': 'main',
479       'name': 'TestPolicyName',
480       'caption': 'TestPolicyCaption',
481       'desc': 'TestPolicyDesc',
482       'supported_on': [{
483         'product': 'chrome',
484         'platforms': ['win', 'mac', 'linux'],
485         'since_version': '8',
486         'until_version': '',
487       }, {
488         'product': 'chrome',
489         'platforms': ['android'],
490         'since_version': '30',
491         'until_version': '',
492       }, {
493         'product': 'chrome',
494         'platforms': ['ios'],
495         'since_version': '34',
496         'until_version': '',
497       }],
498       'features': {
499         'dynamic_refresh': False,
500         'can_be_mandatory': False,
501         'can_be_recommended': True
502       },
503       'example_value': False
504     }
505     self.writer.messages['doc_since_version'] = {'text': '...$6...'}
506     self.writer._AddPolicyDetails(self.doc_root, policy)
507     self.assertEquals(
508       self.doc_root.toxml(),
509       '<root><dl>'
510       '<dt style="style_dt;">_test_data_type</dt><dd>Boolean (REG_DWORD)</dd>'
511       '<dt style="style_dt;">_test_win_reg_loc</dt>'
512       '<dd style="style_.monospace;">MockKeyRec\TestPolicyName</dd>'
513       '<dt style="style_dt;">_test_mac_linux_pref_name</dt>'
514         '<dd style="style_.monospace;">TestPolicyName</dd>'
515       '<dt style="style_dt;">_test_supported_on</dt>'
516       '<dd>'
517         '<ul style="style_ul;">'
518           '<li>Chrome (Windows, Mac, Linux) ...8...</li>'
519           '<li>Chrome (Android) ...30...</li>'
520           '<li>Chrome (iOS) ...34...</li>'
521         '</ul>'
522       '</dd>'
523       '<dt style="style_dt;">_test_supported_features</dt>'
524         '<dd>_test_feature_mandatory: _test_not_supported,'
525         ' _test_feature_recommended: _test_supported,'
526         ' _test_feature_dynamic_refresh: _test_not_supported</dd>'
527       '<dt style="style_dt;">_test_description</dt><dd>TestPolicyDesc</dd>'
528       '<dt style="style_dt;">_test_example_value</dt>'
529         '<dd>0x00000000 (Windows), false (Linux), &lt;false /&gt; (Mac)</dd>'
530       '</dl></root>')
531
532   def testAddPolicyNote(self):
533     # TODO(jkummerow): The functionality tested by this test is currently not
534     # used for anything and will probably soon be removed.
535     # Test if nodes are correctly added to policies.
536     policy = {
537       'problem_href': 'http://www.example.com/5'
538     }
539     self.writer.messages['doc_note'] = {'text': '...$6...'}
540     self.writer._AddPolicyNote(self.doc_root, policy)
541     self.assertEquals(
542         self.doc_root.toxml(),
543         '<root><div style="style_div.note;">...'
544         '<a href="http://www.example.com/5">http://www.example.com/5</a>'
545         '...</div></root>')
546
547   def testAddPolicyRow(self):
548     # Test if policies are correctly added to the summary table.
549     policy = {
550       'name': 'PolicyName',
551       'caption': 'PolicyCaption',
552       'type': 'string',
553     }
554     self.writer._indent_level = 3
555     self.writer._AddPolicyRow(self.doc_root, policy)
556     self.assertEquals(
557       self.doc_root.toxml(),
558       '<root><tr style="style_tr;">'
559       '<td style="style_td;style_td.left;padding-left: 49px;">'
560         '<a href="#PolicyName">PolicyName</a>'
561       '</td>'
562       '<td style="style_td;style_td.right;">PolicyCaption</td>'
563       '</tr></root>')
564     self.setUp()
565     policy = {
566       'name': 'PolicyName',
567       'caption': 'PolicyCaption',
568       'type': 'group',
569     }
570     self.writer._indent_level = 2
571     self.writer._AddPolicyRow(self.doc_root, policy)
572     self.assertEquals(
573       self.doc_root.toxml(),
574       '<root><tr style="style_tr;">'
575       '<td colspan="2" style="style_td;style_td.left;padding-left: 35px;">'
576         '<a href="#PolicyName">PolicyCaption</a>'
577       '</td>'
578       '</tr></root>')
579
580   def testAddPolicySection(self):
581     # Test if policy details are correctly added to the document.
582     policy = {
583       'name': 'PolicyName',
584       'caption': 'PolicyCaption',
585       'desc': 'PolicyDesc',
586       'type': 'string',
587       'supported_on': [{
588         'product': 'chrome',
589         'platforms': ['win', 'mac'],
590         'since_version': '7',
591         'until_version': '',
592       }],
593       'features': {'dynamic_refresh': False},
594       'example_value': 'False'
595     }
596     self.writer.messages['doc_since_version'] = {'text': '..$6..'}
597     self.writer._AddPolicySection(self.doc_root, policy)
598     self.assertEquals(
599       self.doc_root.toxml(),
600       '<root>'
601         '<div style="margin-left: 0px">'
602           '<h3><a name="PolicyName"/>PolicyName</h3>'
603           '<span>PolicyCaption</span>'
604           '<dl>'
605             '<dt style="style_dt;">_test_data_type</dt>'
606             '<dd>String (REG_SZ)</dd>'
607             '<dt style="style_dt;">_test_win_reg_loc</dt>'
608             '<dd style="style_.monospace;">MockKey\\PolicyName</dd>'
609             '<dt style="style_dt;">_test_mac_linux_pref_name</dt>'
610             '<dd style="style_.monospace;">PolicyName</dd>'
611             '<dt style="style_dt;">_test_supported_on</dt>'
612             '<dd>'
613               '<ul style="style_ul;">'
614                 '<li>Chrome (Windows, Mac) ..7..</li>'
615               '</ul>'
616             '</dd>'
617             '<dt style="style_dt;">_test_supported_features</dt>'
618             '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>'
619             '<dt style="style_dt;">_test_description</dt>'
620             '<dd>PolicyDesc</dd>'
621             '<dt style="style_dt;">_test_example_value</dt>'
622             '<dd>&quot;False&quot;</dd>'
623           '</dl>'
624           '<a href="#top">_test_back_to_top</a>'
625         '</div>'
626       '</root>')
627     # Test for groups.
628     self.setUp()
629     policy['type'] = 'group'
630     self.writer._AddPolicySection(self.doc_root, policy)
631     self.assertEquals(
632       self.doc_root.toxml(),
633       '<root>'
634         '<div style="margin-left: 0px">'
635           '<h2><a name="PolicyName"/>PolicyCaption</h2>'
636           '<div style="style_div.group_desc;">PolicyDesc</div>'
637           '<a href="#top">_test_back_to_top</a>'
638         '</div>'
639       '</root>')
640
641   def testAddPolicySectionForWindowsOnly(self):
642     policy = {
643       'name': 'PolicyName',
644       'caption': 'PolicyCaption',
645       'desc': 'PolicyDesc',
646       'type': 'int',
647       'supported_on': [{
648         'product': 'chrome',
649         'platforms': ['win'],
650         'since_version': '33',
651         'until_version': '',
652       }],
653       'features': {'dynamic_refresh': False},
654       'example_value': 123
655     }
656     self.writer.messages['doc_since_version'] = {'text': '..$6..'}
657     self.writer._AddPolicySection(self.doc_root, policy)
658     self.assertEquals(
659       self.doc_root.toxml(),
660       '<root>'
661         '<div style="margin-left: 0px">'
662           '<h3><a name="PolicyName"/>PolicyName</h3>'
663           '<span>PolicyCaption</span>'
664           '<dl>'
665             '<dt style="style_dt;">_test_data_type</dt>'
666             '<dd>Integer (REG_DWORD)</dd>'
667             '<dt style="style_dt;">_test_win_reg_loc</dt>'
668             '<dd style="style_.monospace;">MockKey\\PolicyName</dd>'
669             '<dt style="style_dt;">_test_supported_on</dt>'
670             '<dd>'
671               '<ul style="style_ul;">'
672                 '<li>Chrome (Windows) ..33..</li>'
673               '</ul>'
674             '</dd>'
675             '<dt style="style_dt;">_test_supported_features</dt>'
676             '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>'
677             '<dt style="style_dt;">_test_description</dt>'
678             '<dd>PolicyDesc</dd>'
679             '<dt style="style_dt;">_test_example_value</dt>'
680             '<dd>0x0000007b (Windows)</dd>'
681           '</dl>'
682           '<a href="#top">_test_back_to_top</a>'
683         '</div>'
684       '</root>')
685
686   def testAddPolicySectionForMacOnly(self):
687     policy = {
688       'name': 'PolicyName',
689       'caption': 'PolicyCaption',
690       'desc': 'PolicyDesc',
691       'type': 'int',
692       'supported_on': [{
693         'product': 'chrome',
694         'platforms': ['mac'],
695         'since_version': '33',
696         'until_version': '',
697       }],
698       'features': {'dynamic_refresh': False},
699       'example_value': 123
700     }
701     self.writer.messages['doc_since_version'] = {'text': '..$6..'}
702     self.writer._AddPolicySection(self.doc_root, policy)
703     self.assertEquals(
704       self.doc_root.toxml(),
705       '<root>'
706         '<div style="margin-left: 0px">'
707           '<h3><a name="PolicyName"/>PolicyName</h3>'
708           '<span>PolicyCaption</span>'
709           '<dl>'
710             '<dt style="style_dt;">_test_data_type</dt>'
711             '<dd>Integer</dd>'
712             '<dt style="style_dt;">_test_mac_linux_pref_name</dt>'
713             '<dd style="style_.monospace;">PolicyName</dd>'
714             '<dt style="style_dt;">_test_supported_on</dt>'
715             '<dd>'
716               '<ul style="style_ul;">'
717                 '<li>Chrome (Mac) ..33..</li>'
718               '</ul>'
719             '</dd>'
720             '<dt style="style_dt;">_test_supported_features</dt>'
721             '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>'
722             '<dt style="style_dt;">_test_description</dt>'
723             '<dd>PolicyDesc</dd>'
724             '<dt style="style_dt;">_test_example_value</dt>'
725             '<dd>123 (Mac)</dd>'
726           '</dl>'
727           '<a href="#top">_test_back_to_top</a>'
728         '</div>'
729       '</root>')
730
731   def testAddPolicySectionForLinuxOnly(self):
732     policy = {
733       'name': 'PolicyName',
734       'caption': 'PolicyCaption',
735       'desc': 'PolicyDesc',
736       'type': 'int',
737       'supported_on': [{
738         'product': 'chrome',
739         'platforms': ['linux'],
740         'since_version': '33',
741         'until_version': '',
742       }],
743       'features': {'dynamic_refresh': False},
744       'example_value': 123
745     }
746     self.writer.messages['doc_since_version'] = {'text': '..$6..'}
747     self.writer._AddPolicySection(self.doc_root, policy)
748     self.assertEquals(
749       self.doc_root.toxml(),
750       '<root>'
751         '<div style="margin-left: 0px">'
752           '<h3><a name="PolicyName"/>PolicyName</h3>'
753           '<span>PolicyCaption</span>'
754           '<dl>'
755             '<dt style="style_dt;">_test_data_type</dt>'
756             '<dd>Integer</dd>'
757             '<dt style="style_dt;">_test_mac_linux_pref_name</dt>'
758             '<dd style="style_.monospace;">PolicyName</dd>'
759             '<dt style="style_dt;">_test_supported_on</dt>'
760             '<dd>'
761               '<ul style="style_ul;">'
762                 '<li>Chrome (Linux) ..33..</li>'
763               '</ul>'
764             '</dd>'
765             '<dt style="style_dt;">_test_supported_features</dt>'
766             '<dd>_test_feature_dynamic_refresh: _test_not_supported</dd>'
767             '<dt style="style_dt;">_test_description</dt>'
768             '<dd>PolicyDesc</dd>'
769             '<dt style="style_dt;">_test_example_value</dt>'
770             '<dd>123 (Linux)</dd>'
771           '</dl>'
772           '<a href="#top">_test_back_to_top</a>'
773         '</div>'
774       '</root>')
775
776   def testAddDictionaryExample(self):
777     policy = {
778       'name': 'PolicyName',
779       'caption': 'PolicyCaption',
780       'desc': 'PolicyDesc',
781       'type': 'dict',
782       'supported_on': [{
783         'product': 'chrome',
784         'platforms': ['win', 'mac', 'linux'],
785         'since_version': '7',
786         'until_version': '',
787       }],
788       'features': {'dynamic_refresh': False},
789       'example_value': {
790         "ProxyMode": "direct",
791         "List": ["1", "2", "3"],
792         "True": True,
793         "False": False,
794         "Integer": 123,
795         "DictList": [ {
796             "A": 1,
797             "B": 2,
798           }, {
799             "C": 3,
800             "D": 4,
801           },
802         ],
803       },
804     }
805     self.writer._AddDictionaryExample(self.doc_root, policy)
806     value = json.dumps(policy['example_value']).replace('"', '&quot;')
807     self.assertEquals(
808       self.doc_root.toxml(),
809       '<root>'
810         '<dl style="style_dd dl;">'
811           '<dt>Windows:</dt>'
812           '<dd style="style_.monospace;style_.pre;">MockKey\PolicyName = '
813               + value +
814           '</dd>'
815           '<dt>Linux:</dt>'
816           '<dd style="style_.monospace;">PolicyName: ' + value + '</dd>'
817           '<dt>Mac:</dt>'
818           '<dd style="style_.monospace;style_.pre;">'
819             '&lt;key&gt;PolicyName&lt;/key&gt;\n'
820             '&lt;dict&gt;\n'
821             '  &lt;key&gt;DictList&lt;/key&gt;\n'
822             '  &lt;array&gt;\n'
823             '    &lt;dict&gt;\n'
824             '      &lt;key&gt;A&lt;/key&gt;\n'
825             '      &lt;integer&gt;1&lt;/integer&gt;\n'
826             '      &lt;key&gt;B&lt;/key&gt;\n'
827             '      &lt;integer&gt;2&lt;/integer&gt;\n'
828             '    &lt;/dict&gt;\n'
829             '    &lt;dict&gt;\n'
830             '      &lt;key&gt;C&lt;/key&gt;\n'
831             '      &lt;integer&gt;3&lt;/integer&gt;\n'
832             '      &lt;key&gt;D&lt;/key&gt;\n'
833             '      &lt;integer&gt;4&lt;/integer&gt;\n'
834             '    &lt;/dict&gt;\n'
835             '  &lt;/array&gt;\n'
836             '  &lt;key&gt;False&lt;/key&gt;\n'
837             '  &lt;false/&gt;\n'
838             '  &lt;key&gt;Integer&lt;/key&gt;\n'
839             '  &lt;integer&gt;123&lt;/integer&gt;\n'
840             '  &lt;key&gt;List&lt;/key&gt;\n'
841             '  &lt;array&gt;\n'
842             '    &lt;string&gt;1&lt;/string&gt;\n'
843             '    &lt;string&gt;2&lt;/string&gt;\n'
844             '    &lt;string&gt;3&lt;/string&gt;\n'
845             '  &lt;/array&gt;\n'
846             '  &lt;key&gt;ProxyMode&lt;/key&gt;\n'
847             '  &lt;string&gt;direct&lt;/string&gt;\n'
848             '  &lt;key&gt;True&lt;/key&gt;\n'
849             '  &lt;true/&gt;\n'
850             '&lt;/dict&gt;'
851           '</dd>'
852         '</dl>'
853       '</root>')
854
855
856 if __name__ == '__main__':
857   unittest.main()