Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / permissions_data_source_test.py
blobe92b16b3af0f0218fe6d7f6fd37286a352cf7032
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.
6 import json
7 from operator import itemgetter
8 import unittest
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.
20 'activeTab': {
21 'name': 'activeTab',
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.
26 'alarms': {
27 'name': 'alarms',
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.
32 'audioCapture': {
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
42 # it's an API.
43 'cookies': {
44 'name': 'cookies',
45 'platforms': ['apps']
50 _PERMISSIONS_JSON = {
51 # This will appear for both apps and extensions with a custom description,
52 # anchor, etc.
53 'host-permissions': {
54 'name': 'match pattern',
55 'anchor': 'custom-anchor',
56 'partial': 'permissions/host_permissions.html',
57 'platforms': ['apps', 'extensions'],
58 'literal_name': True
60 # A custom 'partial' here overrides the default partial.
61 'activeTab': {
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',
74 _API_FEATURES = {
75 'alarms': {
76 'dependencies': ['permission:alarms']
78 'cookies': {
79 'dependencies': ['permission:cookies']
84 class PermissionsDataSourceTest(unittest.TestCase):
85 def testCreatePermissionsDataSource(self):
86 expected_extensions = [
88 'anchor': 'custom-anchor',
89 'description': 'host permissions',
90 'literal_name': True,
91 'name': 'match pattern',
92 'platforms': ['apps', 'extensions']
95 'anchor': 'activeTab',
96 'description': 'active tab',
97 'name': 'activeTab',
98 'platforms': ['extensions'],
101 'anchor': 'alarms',
102 'description': 'generic description',
103 'name': 'alarms',
104 'platforms': ['apps', 'extensions'],
108 expected_apps = [
110 'anchor': 'custom-anchor',
111 'description': 'host permissions',
112 'literal_name': True,
113 'name': 'match pattern',
114 'platforms': ['apps', 'extensions'],
117 'anchor': 'alarms',
118 'description': 'generic description',
119 'name': 'alarms',
120 'platforms': ['apps', 'extensions'],
123 'anchor': 'cookies',
124 'description': 'generic description',
125 'name': 'cookies',
126 'platforms': ['apps'],
130 test_file_system = TestFileSystem({
131 'api': {
132 '_api_features.json': json.dumps(_API_FEATURES),
133 '_manifest_features.json': '{}',
134 '_permission_features.json': json.dumps(_PERMISSION_FEATURES),
136 'docs': {
137 'templates': {
138 'json': {
139 'manifest.json': '{}',
140 'permissions.json': json.dumps(_PERMISSIONS_JSON),
142 'private': {
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'))
163 for mapping in lst:
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__':
173 unittest.main()