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.
9 from api_models
import APIModels
10 from compiled_file_system
import CompiledFileSystem
11 from extensions_paths
import (API_PATHS
, CHROME_API
, CHROME_EXTENSIONS
,
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
26 'inspected_window.json': ReadFile(
27 CHROME_API
, 'devtools', 'inspected_window.json'),
29 '_api_features.json': json
.dumps({
32 'app.runtime': {'noparent': True},
33 'app.runtime.foo': {},
34 'declarativeWebRequest': {},
35 'devtools.inspectedWindow': {},
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'),
49 'manifest.json': '{}',
50 'permissions.json': '{}',
57 class APIModelsTest(unittest
.TestCase
):
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
,
67 self
._api
_models
= APIModels(features_bundle
,
69 self
._mock
_file
_system
,
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'.
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
+
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
+
125 self
.assertRaises(FileNotFoundError
,
126 self
._api
_models
.GetModel('storage').Get
)
127 self
.assertRaises(FileNotFoundError
,
128 self
._api
_models
.GetModel(CHROME_API
+
130 self
.assertRaises(FileNotFoundError
,
131 self
._api
_models
.GetModel(CHROME_API
+
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.
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))
156 self
.assertTrue(*self
._mock
_file
_system
.CheckAndReset())
159 if __name__
== '__main__':