- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / sidenav_data_source_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 json
7 import unittest
8
9 from compiled_file_system import CompiledFileSystem
10 from mock_file_system import MockFileSystem
11 from object_store_creator import ObjectStoreCreator
12 from server_instance import ServerInstance
13 from servlet import Request
14 from sidenav_data_source import SidenavDataSource, _AddLevels, _AddSelected
15 from test_file_system import TestFileSystem
16 from test_util import CaptureLogging
17
18
19 class SamplesDataSourceTest(unittest.TestCase):
20   def testAddLevels(self):
21     sidenav_json = [{
22       'title': 'H2',
23       'items': [{
24         'title': 'H3',
25         'items': [{ 'title': 'X1' }]
26       }]
27     }]
28
29     expected = [{
30       'level': 1,
31       'title': 'H2',
32       'items': [{
33         'level': 2,
34         'title': 'H3',
35         'items': [{ 'level': 3, 'title': 'X1' }]
36       }]
37     }]
38
39     _AddLevels(sidenav_json, 1)
40     self.assertEqual(expected, sidenav_json)
41
42   def testAddSelected(self):
43     sidenav_json = [
44       { 'href': '/AH2.html' },
45       {
46         'href': '/H2.html',
47         'items': [{
48           'href': '/H3.html'
49         }]
50       }
51     ]
52
53     expected = [
54       { 'href': '/AH2.html' },
55       {
56         'child_selected': True,
57         'href': '/H2.html',
58         'items': [{
59           'href': '/H3.html',
60           'selected': True
61         }]
62       }
63     ]
64
65     _AddSelected(sidenav_json, '/H3.html')
66     self.assertEqual(expected, sidenav_json)
67
68   def testWithDifferentBasePath(self):
69     file_system = TestFileSystem({
70       'apps_sidenav.json': json.dumps([
71         { 'href': '/H1.html' },
72         { 'href': '/H2.html' },
73         { 'href': '/base/path/H2.html' },
74         { 'href': 'https://qualified/X1.html' },
75         {
76           'href': 'H3.html',
77           'items': [{
78             'href': 'H4.html'
79           }]
80         },
81       ])
82     }, relative_to='docs/templates/json')
83
84     expected = [
85       {'href': '/base/path/H1.html', 'level': 2},
86       {'href': '/base/path/H2.html', 'level': 2, 'selected': True},
87       {'href': '/base/path/base/path/H2.html', 'level': 2},
88       {'href': 'https://qualified/X1.html', 'level': 2},
89       {'items': [
90         {'href': '/base/path/H4.html', 'level': 3}
91       ],
92       'href': '/base/path/H3.html', 'level': 2}
93     ]
94
95     server_instance = ServerInstance.ForTest(file_system,
96                                              base_path='/base/path/')
97     sidenav_data_source = SidenavDataSource(server_instance,
98                                             Request.ForTest('/H2.html'))
99
100     log_output = CaptureLogging(
101         lambda: self.assertEqual(expected, sidenav_data_source.get('apps')))
102     self.assertEqual(2, len(log_output))
103
104   def testSidenavDataSource(self):
105     file_system = MockFileSystem(TestFileSystem({
106       'apps_sidenav.json': json.dumps([{
107         'title': 'H1',
108         'href': 'H1.html',
109         'items': [{
110           'title': 'H2',
111           'href': '/H2.html'
112         }]
113       }])
114     }, relative_to='docs/templates/json'))
115
116     expected = [{
117       'level': 2,
118       'child_selected': True,
119       'title': 'H1',
120       'href': '/H1.html',
121       'items': [{
122         'level': 3,
123         'selected': True,
124         'title': 'H2',
125         'href': '/H2.html'
126       }]
127     }]
128
129     sidenav_data_source = SidenavDataSource(
130         ServerInstance.ForTest(file_system), Request.ForTest('/H2.html'))
131     self.assertTrue(*file_system.CheckAndReset())
132
133     log_output = CaptureLogging(
134         lambda: self.assertEqual(expected, sidenav_data_source.get('apps')))
135
136     self.assertEqual(1, len(log_output))
137     self.assertTrue(
138         log_output[0].msg.startswith('Paths in sidenav must be qualified.'))
139
140     # Test that only a single file is read when creating the sidenav, so that
141     # we can be confident in the compiled_file_system.SingleFile annotation.
142     self.assertTrue(*file_system.CheckAndReset(
143         read_count=1, stat_count=1, read_resolve_count=1))
144
145   def testCron(self):
146     file_system = TestFileSystem({
147       'apps_sidenav.json': '[{ "title": "H1" }]' ,
148       'extensions_sidenav.json': '[{ "title": "H2" }]'
149     }, relative_to='docs/templates/json')
150
151     # Ensure Cron doesn't rely on request.
152     sidenav_data_source = SidenavDataSource(
153         ServerInstance.ForTest(file_system), request=None)
154     sidenav_data_source.Cron()
155
156     # If Cron fails, apps_sidenav.json will not be cached, and the _cache_data
157     # access will fail.
158     # TODO(jshumway): Make a non hack version of this check.
159     sidenav_data_source._cache._file_object_store.Get(
160         'docs/templates/json/apps_sidenav.json').Get()._cache_data
161
162
163 if __name__ == '__main__':
164   unittest.main()