Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / manifest_data_source_test.py
blob9b279625ce734b90aca7c1f6a9a2a31b32806be5
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 from copy import deepcopy
7 import json
8 import unittest
10 from future import Future
11 import manifest_data_source
12 from object_store_creator import ObjectStoreCreator
15 convert_and_annotate_docs = {
16 'name': {
17 'example': "My {{platform}}",
18 'name': 'name',
19 'level': 'required'
21 'doc2': {
22 'level': 'required',
23 'name': 'doc2'
25 'doc1': {
26 'level': 'required',
27 'name': 'doc1',
28 'children': {
29 'sub1': {
30 'annotations': ['not so important'],
31 'level': 'optional',
32 'name': 'sub1'
34 'sub2': {
35 'level': 'required',
36 'name': 'sub2'
40 'doc3': {
41 'level': 'only_one',
42 'name': 'doc3'
44 'doc4': {
45 'level': 'recommended',
46 'name': 'doc4'
48 'doc5': {
49 'level': 'only_one',
50 'name': 'doc5'
52 'doc6': {
53 'level': 'optional',
54 'name': 'doc6'
59 class ManifestDataSourceTest(unittest.TestCase):
60 def testListifyAndSortDocs(self):
61 expected_docs = [
63 'level': 'required',
64 'name': 'doc1',
65 'children': [
67 'level': 'required',
68 'name': 'sub2'
71 'annotations': ['not so important'],
72 'level': 'optional',
73 'name': 'sub1'
78 'level': 'required',
79 'name': 'doc2'
82 'level': 'required',
83 'example': '"My App"',
84 'has_example': True,
85 'name': 'name'
88 'level': 'recommended',
89 'name': 'doc4'
92 'level': 'only_one',
93 'name': 'doc3'
96 'level': 'only_one',
97 'name': 'doc5'
100 'level': 'optional',
101 'name': 'doc6'
105 self.assertEqual(expected_docs, manifest_data_source._ListifyAndSortDocs(
106 deepcopy(convert_and_annotate_docs), 'App'))
108 def testAnnotate(self):
109 expected_docs = [
111 'level': 'required',
112 'name': 'doc1',
113 'children': [
115 'level': 'required',
116 'name': 'sub2'
119 'annotations': ['Optional', 'not so important'],
120 'level': 'optional',
121 'name': 'sub1',
122 'is_last': True
127 'level': 'required',
128 'name': 'doc2'
131 'name': 'name',
132 'level': 'required',
133 'example': '"My App"',
134 'has_example': True
137 'annotations': ['Recommended'],
138 'level': 'recommended',
139 'name': 'doc4'
142 'annotations': ['Pick one (or none)'],
143 'level': 'only_one',
144 'name': 'doc3'
147 'level': 'only_one',
148 'name': 'doc5'
151 'annotations': ['Optional'],
152 'level': 'optional',
153 'name': 'doc6',
154 'is_last': True
158 annotated = manifest_data_source._ListifyAndSortDocs(
159 deepcopy(convert_and_annotate_docs), 'App')
160 manifest_data_source._AddLevelAnnotations(annotated)
161 self.assertEqual(expected_docs, annotated)
163 def testExpandedExamples(self):
164 docs = {
165 'doc1': {
166 'name': 'doc1',
167 'example': {
168 'big': {
169 'nested': {
170 'json_example': ['with', 'more', 'json']
177 expected_docs = [
179 'name': 'doc1',
180 'children': [
182 'name': 'big',
183 'children': [
185 'name': 'nested',
186 'children': [
188 'name': 'json_example',
189 'example': json.dumps(['with', 'more', 'json']),
190 'has_example': True
200 self.assertEqual(
201 expected_docs, manifest_data_source._ListifyAndSortDocs(docs, 'apps'))
203 def testNonExpandedExamples(self):
204 docs = {
205 'doc1': {
206 'name': 'doc1',
207 'example': {}
209 'doc2': {
210 'name': 'doc2',
211 'example': []
213 'doc3': {
214 'name': 'doc3',
215 'example': [{}]
219 expected_docs = [
221 'name': 'doc1',
222 'has_example': True,
223 'example': '{...}'
226 'name': 'doc2',
227 'has_example': True,
228 'example': '[...]'
231 'name': 'doc3',
232 'has_example': True,
233 'example': '[{...}]'
236 self.assertEqual(
237 expected_docs, manifest_data_source._ListifyAndSortDocs(docs, 'apps'))
239 def testManifestDataSource(self):
240 manifest_features = {
241 'doc1': {
242 'name': 'doc1',
243 'platforms': ['apps', 'extensions'],
244 'example': {},
245 'level': 'required'
247 'doc1.sub1': {
248 'name': 'doc1.sub1',
249 'platforms': ['apps'],
250 'annotations': ['important!'],
251 'level': 'recommended'
253 'doc2': {
254 'name': 'doc2',
255 'platforms': ['extensions']
259 expected_app = [
261 'example': '{...}',
262 'has_example': True,
263 'level': 'required',
264 'name': 'doc1',
265 'platforms': ['apps', 'extensions'],
266 'children': [
268 'annotations': [
269 'Recommended',
270 'important!'
272 'level': 'recommended',
273 'name': 'sub1',
274 'platforms': ['apps'],
275 'is_last': True
278 'is_last': True
282 class FakeFeaturesBundle(object):
283 def GetManifestFeatures(self):
284 return Future(value=manifest_features)
286 class FakeServerInstance(object):
287 def __init__(self):
288 self.features_bundle = FakeFeaturesBundle()
289 self.object_store_creator = ObjectStoreCreator.ForTest()
291 mds = manifest_data_source.ManifestDataSource(FakeServerInstance(), None)
292 self.assertEqual(expected_app, mds.get('apps'))
294 if __name__ == '__main__':
295 unittest.main()