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