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.
7 from operator
import itemgetter
10 from extensions_paths
import EXTENSIONS
11 from permissions_data_source
import PermissionsDataSource
12 from server_instance
import ServerInstance
13 from third_party
.handlebar
import Handlebar
14 from test_file_system
import TestFileSystem
17 _PERMISSION_FEATURES
= {
18 # This will appear for extensions with a description as defined in the
19 # permissions.json file.
22 'platforms': ['extensions'],
24 # This will appear for apps and extensions with an auto-generated description
25 # since the entry appears in _api_features.json.
28 'platforms': ['apps', 'extensions'],
30 # This won't appear for anything since there's no entry in permissions.json
31 # and it's not an API.
33 'name': 'audioCapture',
34 'platforms': ['apps'],
36 # This won't appear for anything because it's private.
37 'commandLinePrivate': {
38 'name': 'commandLinePrivate',
39 'platforms': ['apps', 'extensions']
41 # This will only appear for apps with an auto-generated description because
51 # This will appear for both apps and extensions with a custom description,
54 'name': 'match pattern',
55 'anchor': 'custom-anchor',
56 'partial': 'permissions/host_permissions.html',
57 'platforms': ['apps', 'extensions'],
60 # A custom 'partial' here overrides the default partial.
62 'partial': 'permissions/active_tab.html'
67 _PERMISSIONS_PARTIALS
= {
68 'active_tab.html': 'active tab',
69 'host_permissions.html': 'host permissions',
70 'generic_description.html': 'generic description',
76 'dependencies': ['permission:alarms']
79 'dependencies': ['permission:cookies']
84 class PermissionsDataSourceTest(unittest
.TestCase
):
85 def testCreatePermissionsDataSource(self
):
86 expected_extensions
= [
88 'anchor': 'custom-anchor',
89 'description': 'host permissions',
91 'name': 'match pattern',
92 'platforms': ['apps', 'extensions']
95 'anchor': 'activeTab',
96 'description': 'active tab',
98 'platforms': ['extensions'],
102 'description': 'generic description',
104 'platforms': ['apps', 'extensions'],
110 'anchor': 'custom-anchor',
111 'description': 'host permissions',
112 'literal_name': True,
113 'name': 'match pattern',
114 'platforms': ['apps', 'extensions'],
118 'description': 'generic description',
120 'platforms': ['apps', 'extensions'],
124 'description': 'generic description',
126 'platforms': ['apps'],
130 test_file_system
= TestFileSystem({
132 '_api_features.json': json
.dumps(_API_FEATURES
),
133 '_manifest_features.json': '{}',
134 '_permission_features.json': json
.dumps(_PERMISSION_FEATURES
),
139 'manifest.json': '{}',
140 'permissions.json': json
.dumps(_PERMISSIONS_JSON
),
143 'permissions': _PERMISSIONS_PARTIALS
147 }, relative_to
=EXTENSIONS
)
149 permissions_data_source
= PermissionsDataSource(
150 ServerInstance
.ForTest(test_file_system
), None)
152 actual_extensions
= permissions_data_source
.get('declare_extensions')
153 actual_apps
= permissions_data_source
.get('declare_apps')
155 # Normalise all test data.
156 # - Sort keys. Since the tests don't use OrderedDicts we can't make
157 # assertions about the order, which is unfortunate. Oh well.
158 # - Render all of the Handlerbar instances so that we can use ==.
159 # Handlebars don't implement __eq__, but they probably should.
160 for lst
in (actual_apps
, actual_extensions
,
161 expected_apps
, expected_extensions
):
162 lst
.sort(key
=itemgetter('name'))
164 for key
, value
in mapping
.iteritems():
165 if isinstance(value
, Handlebar
):
166 mapping
[key
] = value
.Render().text
168 self
.assertEqual(expected_extensions
, actual_extensions
)
169 self
.assertEqual(expected_apps
, actual_apps
)
172 if __name__
== '__main__':