Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / document_renderer_test.py
1 #!/usr/bin/env python
2 # Copyright 2013 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 import unittest
7
8 from document_renderer import DocumentRenderer
9 from server_instance import ServerInstance
10 from test_file_system import TestFileSystem
11 from test_data.canned_data import CANNED_TEST_FILE_SYSTEM_DATA
12
13
14 class DocumentRendererUnittest(unittest.TestCase):
15   def setUp(self):
16     self._renderer = ServerInstance.ForTest(
17         TestFileSystem(CANNED_TEST_FILE_SYSTEM_DATA)).document_renderer
18
19   def testNothingToSubstitute(self):
20     document = 'hello world'
21     path = 'some/path/to/document.html'
22
23     text, warnings = self._renderer.Render(document, path)
24     self.assertEqual(document, text)
25     self.assertEqual([], warnings)
26
27     text, warnings = self._renderer.Render(document, path, render_title=True)
28     self.assertEqual(document, text)
29     self.assertEqual(['Expected a title'], warnings)
30
31   def testTitles(self):
32     document = '<h1>title</h1> then $(title) then another $(title)'
33     path = 'some/path/to/document.html'
34
35     text, warnings = self._renderer.Render(document, path)
36     self.assertEqual(document, text)
37     self.assertEqual(['Found unexpected title "title"'], warnings)
38
39     text, warnings = self._renderer.Render(document, path, render_title=True)
40     self.assertEqual('<h1>title</h1> then title then another $(title)', text)
41     self.assertEqual([], warnings)
42
43   def testTocs(self):
44     document = ('here is a toc $(table_of_contents) '
45                 'and another $(table_of_contents)')
46     expected_document = ('here is a toc <table-of-contents> and another '
47                          '$(table_of_contents)')
48     path = 'some/path/to/document.html'
49
50     text, warnings = self._renderer.Render(document, path)
51     self.assertEqual(expected_document, text)
52     self.assertEqual([], warnings)
53
54     text, warnings = self._renderer.Render(document, path, render_title=True)
55     self.assertEqual(expected_document, text)
56     self.assertEqual(['Expected a title'], warnings)
57
58   def testRefs(self):
59     # The references in this and subsequent tests won't actually be resolved
60     document = 'A ref $(ref:baz.baz_e1) here, $(ref:foo.foo_t3 ref title) there'
61     expected_document = ('A ref <a href=#type-baz_e1>baz.baz_e1</a> '
62                          'here, <a href=#type-foo_t3>ref title</a> '
63                          'there')
64     path = 'some/path/to/document.html'
65
66     text, warnings = self._renderer.Render(document, path)
67     self.assertEqual(expected_document, text)
68     self.assertEqual([], warnings)
69
70     text, warnings = self._renderer.Render(document, path, render_title=True)
71     self.assertEqual(expected_document, text)
72     self.assertEqual(['Expected a title'], warnings)
73
74   def testTitleAndToc(self):
75     document = '<h1>title</h1> $(title) and $(table_of_contents)'
76     path = 'some/path/to/document.html'
77
78     text, warnings = self._renderer.Render(document, path)
79     self.assertEqual('<h1>title</h1> $(title) and <table-of-contents>', text)
80     self.assertEqual(['Found unexpected title "title"'], warnings)
81
82     text, warnings = self._renderer.Render(document, path, render_title=True)
83     self.assertEqual('<h1>title</h1> title and <table-of-contents>', text)
84     self.assertEqual([], warnings)
85
86   def testRefInTitle(self):
87     document = '<h1>$(ref:baz.baz_e1 title)</h1> A $(title) was here'
88     expected_document_no_title = ('<h1><a href=#type-baz_e1>'
89                                   'title</a></h1> A $(title) was here')
90
91     expected_document = ('<h1><a href=#type-baz_e1>title</a></h1>'
92                          ' A title was here')
93     path = 'some/path/to/document.html'
94
95     text, warnings = self._renderer.Render(document, path)
96     self.assertEqual(expected_document_no_title, text)
97     self.assertEqual([('Found unexpected title "title"')], warnings)
98
99     text, warnings = self._renderer.Render(document, path, render_title=True)
100     self.assertEqual(expected_document, text)
101     self.assertEqual([], warnings)
102
103   def testInvalidRef(self):
104     # There needs to be more than 100 characters between the invalid ref
105     # and the next ref
106     document = ('An invalid $(ref:foo.foo_t3 a title with some long '
107                 'text containing a valid reference pointing to '
108                 '$(ref:baz.baz_e1) here')
109     expected_document = ('An invalid $(ref:foo.foo_t3 a title with some long '
110                          'text containing a valid reference pointing to <a'
111                          ' href=#type-baz_e1>baz.baz_e1</a> here')
112     path = 'some/path/to/document_api.html'
113
114     text, warnings = self._renderer.Render(document, path)
115     self.assertEqual(expected_document, text)
116     self.assertEqual([], warnings)
117
118     text, warnings = self._renderer.Render(document, path, render_title=True)
119     self.assertEqual(expected_document, text)
120     self.assertEqual(['Expected a title'], warnings)
121
122
123 if __name__ == '__main__':
124   unittest.main()