cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / api_models_test.py
blob4d966325278dc9fdaa0449ffcada5f6396125462
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 import unittest
9 from api_models import APIModels
10 from compiled_file_system import CompiledFileSystem
11 from extensions_paths import (API_PATHS, CHROME_API, CHROME_EXTENSIONS,
12 EXTENSIONS_API)
13 from features_bundle import FeaturesBundle
14 from file_system import FileNotFoundError
15 from mock_file_system import MockFileSystem
16 from object_store_creator import ObjectStoreCreator
17 from test_file_system import TestFileSystem
18 from test_util import ReadFile
19 from future import Future
20 from schema_processor import SchemaProcessorFactoryForTest
23 _TEST_DATA = {
24 'api': {
25 'devtools': {
26 'inspected_window.json': ReadFile(
27 CHROME_API, 'devtools', 'inspected_window.json'),
29 '_api_features.json': json.dumps({
30 'alarms': {},
31 'app': {},
32 'app.runtime': {'noparent': True},
33 'app.runtime.foo': {},
34 'declarativeWebRequest': {},
35 'devtools.inspectedWindow': {},
36 'input': {},
37 'input.ime': {},
38 'storage': {},
39 }),
40 '_manifest_features.json': '{}',
41 '_permission_features.json': '{}',
42 'alarms.idl': ReadFile(EXTENSIONS_API, 'alarms.idl'),
43 'input_ime.json': ReadFile(CHROME_API, 'input_ime.json'),
44 'page_action.json': ReadFile(CHROME_API, 'page_action.json'),
46 'docs': {
47 'templates': {
48 'json': {
49 'manifest.json': '{}',
50 'permissions.json': '{}',
57 class APIModelsTest(unittest.TestCase):
58 def setUp(self):
59 object_store_creator = ObjectStoreCreator.ForTest()
60 compiled_fs_factory = CompiledFileSystem.Factory(object_store_creator)
61 self._mock_file_system = MockFileSystem(
62 TestFileSystem(_TEST_DATA, relative_to=CHROME_EXTENSIONS))
63 features_bundle = FeaturesBundle(self._mock_file_system,
64 compiled_fs_factory,
65 object_store_creator,
66 'extensions')
67 self._api_models = APIModels(features_bundle,
68 compiled_fs_factory,
69 self._mock_file_system,
70 object_store_creator,
71 'extensions',
72 SchemaProcessorFactoryForTest())
74 def testGetNames(self):
75 # Both 'app' and 'app.runtime' appear here because 'app.runtime' has
76 # noparent:true, but 'app.runtime.foo' etc doesn't so it's a sub-feature of
77 # 'app.runtime' not a separate API. 'devtools.inspectedWindow' is an API
78 # because there is no 'devtools'.
79 self.assertEqual(
80 ['alarms', 'app', 'app.runtime', 'declarativeWebRequest',
81 'devtools.inspectedWindow', 'input', 'storage'],
82 sorted(self._api_models.GetNames()))
84 def testGetModel(self):
85 def get_model_name(api_name):
86 return self._api_models.GetModel(api_name).Get().name
87 self.assertEqual('devtools.inspectedWindow',
88 get_model_name('devtools.inspectedWindow'))
89 self.assertEqual('devtools.inspectedWindow',
90 get_model_name('devtools/inspected_window.json'))
91 self.assertEqual('devtools.inspectedWindow',
92 get_model_name(CHROME_API +
93 'devtools/inspected_window.json'))
94 self.assertEqual('alarms', get_model_name('alarms'))
95 self.assertEqual('alarms', get_model_name('alarms.idl'))
96 self.assertEqual('alarms', get_model_name(CHROME_API + 'alarms.idl'))
97 self.assertEqual('input.ime', get_model_name('input.ime'))
98 self.assertEqual('input.ime', get_model_name('input_ime.json'))
99 self.assertEqual('input.ime',
100 get_model_name(CHROME_API + 'input_ime.json'))
101 self.assertEqual('pageAction', get_model_name('pageAction'))
102 self.assertEqual('pageAction', get_model_name('page_action.json'))
103 self.assertEqual('pageAction', get_model_name(CHROME_API +
104 'page_action.json'))
106 def testGetNonexistentModel(self):
107 self.assertRaises(FileNotFoundError,
108 self._api_models.GetModel('declarativeWebRequest').Get)
109 self.assertRaises(FileNotFoundError,
110 self._api_models.GetModel(
111 'declarative_web_request.json').Get)
112 self.assertRaises(FileNotFoundError,
113 self._api_models.GetModel(
114 CHROME_API + 'declarative_web_request.json').Get)
115 self.assertRaises(FileNotFoundError,
116 self._api_models.GetModel('notfound').Get)
117 self.assertRaises(FileNotFoundError,
118 self._api_models.GetModel('notfound.json').Get)
119 self.assertRaises(FileNotFoundError,
120 self._api_models.GetModel(CHROME_API +
121 'notfound.json').Get)
122 self.assertRaises(FileNotFoundError,
123 self._api_models.GetModel(CHROME_API +
124 'alarms.json').Get)
125 self.assertRaises(FileNotFoundError,
126 self._api_models.GetModel('storage').Get)
127 self.assertRaises(FileNotFoundError,
128 self._api_models.GetModel(CHROME_API +
129 'storage.json').Get)
130 self.assertRaises(FileNotFoundError,
131 self._api_models.GetModel(CHROME_API +
132 'storage.idl').Get)
134 def testSingleFile(self):
135 # 2 stats (1 for JSON and 1 for IDL) for each available API path.
136 # 1 read (for IDL file which existed).
137 future = self._api_models.GetModel('alarms')
138 self.assertTrue(*self._mock_file_system.CheckAndReset(
139 read_count=1, stat_count=len(API_PATHS)*2))
141 # 1 read-resolve (for the IDL file).
143 # The important part here and above is that it's only doing a single read;
144 # any more would break the contract that only a single file is accessed -
145 # see the SingleFile annotation in api_models._CreateAPIModel.
146 future.Get()
147 self.assertTrue(*self._mock_file_system.CheckAndReset(
148 read_resolve_count=1))
150 # 2 stats (1 for JSON and 1 for IDL) for each available API path.
151 # No reads (still cached).
152 future = self._api_models.GetModel('alarms')
153 self.assertTrue(*self._mock_file_system.CheckAndReset(
154 stat_count=len(API_PATHS)*2))
155 future.Get()
156 self.assertTrue(*self._mock_file_system.CheckAndReset())
159 if __name__ == '__main__':
160 unittest.main()