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.
10 from extensions_paths
import JSON_TEMPLATES
11 from mock_file_system
import MockFileSystem
12 from server_instance
import ServerInstance
13 from servlet
import Request
14 from sidenav_data_source
import SidenavDataSource
, _AddLevels
, _AddAnnotations
15 from test_file_system
import TestFileSystem
16 from test_util
import CaptureLogging
19 class SamplesDataSourceTest(unittest
.TestCase
):
20 def testAddLevels(self
):
25 'items': [{ 'title': 'X1' }]
35 'items': [{ 'level': 3, 'title': 'X1' }]
39 _AddLevels(sidenav_json
, 1)
40 self
.assertEqual(expected
, sidenav_json
)
42 def testAddAnnotations(self
):
43 item1
= { 'href': '/H1.html' }
44 item2_1
= { 'href': '/H2_1.html' }
45 item2_2
= { 'href': '/H2_2.html' }
46 item2
= { 'href': '/H2.html', 'items': [item2_1
, item2_2
] }
48 expected
= [ item1
, item2
]
50 sidenav_json
= copy
.deepcopy(expected
)
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) }
58 item2_2
['related'] = True
60 self
.assertTrue(_AddAnnotations(sidenav_json
, item2_1
['href']))
61 self
.assertEqual(expected
, sidenav_json
)
63 def testWithDifferentBasePath(self
):
64 file_system
= TestFileSystem({
65 'chrome_sidenav.json': json
.dumps([
66 { 'href': '/H1.html' },
67 { 'href': '/H2.html' },
68 { 'href': '/base/path/H2.html' },
69 { 'href': 'https://qualified/X1.html' },
77 }, relative_to
=JSON_TEMPLATES
)
80 {'href': '/base/path/H1.html', 'level': 2, 'related': True},
81 {'href': '/base/path/H2.html', 'level': 2, 'selected': True, 'related': True},
82 {'href': '/base/path/base/path/H2.html', 'level': 2, 'related': True},
83 {'href': 'https://qualified/X1.html', 'level': 2, 'related': True},
85 {'href': '/base/path/H4.html', 'level': 3}
87 'href': '/base/path/H3.html', 'level': 2, 'related': True}
90 server_instance
= ServerInstance
.ForTest(file_system
,
91 base_path
='/base/path/')
92 sidenav_data_source
= SidenavDataSource(server_instance
,
93 Request
.ForTest('/H2.html'))
95 log_output
= CaptureLogging(
96 lambda: self
.assertEqual(expected
, sidenav_data_source
.get('chrome')))
97 self
.assertEqual(2, len(log_output
))
99 def testSidenavDataSource(self
):
100 file_system
= MockFileSystem(TestFileSystem({
101 'chrome_sidenav.json': json
.dumps([{
109 }, relative_to
=JSON_TEMPLATES
))
113 'child_selected': True,
122 'parent': { 'href': '/H1.html', 'title': 'H1'}
126 sidenav_data_source
= SidenavDataSource(
127 ServerInstance
.ForTest(file_system
), Request
.ForTest('/H2.html'))
128 self
.assertTrue(*file_system
.CheckAndReset())
130 log_output
= CaptureLogging(
131 lambda: self
.assertEqual(expected
, sidenav_data_source
.get('chrome')))
133 self
.assertEqual(1, len(log_output
))
135 log_output
[0].msg
.startswith('Paths in sidenav must be qualified.'))
137 # Test that only a single file is read when creating the sidenav, so that
138 # we can be confident in the compiled_file_system.SingleFile annotation.
139 self
.assertTrue(*file_system
.CheckAndReset(
140 read_count
=1, stat_count
=1, read_resolve_count
=1))
142 def testRefresh(self
):
143 file_system
= TestFileSystem({
144 'chrome_sidenav.json': '[{ "title": "H1" }]'
145 }, relative_to
=JSON_TEMPLATES
)
147 # Ensure Refresh doesn't rely on request.
148 sidenav_data_source
= SidenavDataSource(
149 ServerInstance
.ForTest(file_system
), request
=None)
150 sidenav_data_source
.Refresh().Get()
152 # If Refresh fails, chrome_sidenav.json will not be cached, and the
153 # cache_data access will fail.
154 # TODO(jshumway): Make a non hack version of this check.
155 sidenav_data_source
._cache
._file
_object
_store
.Get(
156 '%schrome_sidenav.json' % JSON_TEMPLATES
).Get().cache_data
159 if __name__
== '__main__':