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
, EXTENSIONS
12 from features_bundle
import FeaturesBundle
13 from file_system
import FileNotFoundError
14 from mock_file_system
import MockFileSystem
15 from object_store_creator
import ObjectStoreCreator
16 from test_file_system
import TestFileSystem
17 from test_util
import ReadFile
23 'inspected_window.json': ReadFile(
24 API
, 'devtools', 'inspected_window.json'),
26 '_api_features.json': json
.dumps({
29 'app.runtime': {'noparent': True},
30 'app.runtime.experimental': {},
31 'app.runtime.experimental.foo': {},
32 'declarativeWebRequest': {},
33 'devtools.inspectedWindow': {},
34 'experimental.accessibility': {},
37 '_manifest_features.json': '{}',
38 '_permission_features.json': '{}',
39 'alarms.idl': ReadFile(API
, 'alarms.idl'),
40 'declarative_web_request.json': ReadFile(
41 API
, 'declarative_web_request.json'),
42 'experimental_accessibility.json': ReadFile(
43 API
, 'experimental_accessibility.json'),
44 'page_action.json': ReadFile(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
=EXTENSIONS
))
63 features_bundle
= FeaturesBundle(
64 self
._mock
_file
_system
, compiled_fs_factory
, object_store_creator
)
65 self
._api
_models
= APIModels(
66 features_bundle
, compiled_fs_factory
, self
._mock
_file
_system
)
68 def testGetNames(self
):
69 # Both 'app' and 'app.runtime' appear here because 'app.runtime' has
70 # noparent:true, but 'app.runtime.experimental' etc doesn't so it's a
71 # sub-feature of 'app.runtime' not a separate API.
72 # 'devtools.inspectedWindow' is an API because there is no 'devtools'.
74 ['alarms', 'app', 'app.runtime', 'declarativeWebRequest',
75 'devtools.inspectedWindow', 'experimental.accessibility', 'storage'],
76 sorted(self
._api
_models
.GetNames()))
78 def testGetModel(self
):
79 def get_model_name(api_name
):
80 return self
._api
_models
.GetModel(api_name
).Get().name
81 self
.assertEqual('devtools.inspectedWindow',
82 get_model_name('devtools.inspectedWindow'))
83 self
.assertEqual('devtools.inspectedWindow',
84 get_model_name('devtools/inspected_window.json'))
85 self
.assertEqual('devtools.inspectedWindow',
86 get_model_name('%s/devtools/inspected_window.json' % API
))
87 self
.assertEqual('alarms', get_model_name('alarms'))
88 self
.assertEqual('alarms', get_model_name('alarms.idl'))
89 self
.assertEqual('alarms', get_model_name('%s/alarms.idl' % API
))
90 self
.assertEqual('declarativeWebRequest',
91 get_model_name('declarativeWebRequest'))
92 self
.assertEqual('declarativeWebRequest',
93 get_model_name('declarative_web_request.json'))
94 self
.assertEqual('declarativeWebRequest',
95 get_model_name('%s/declarative_web_request.json' % API
))
96 self
.assertEqual('experimental.accessibility',
97 get_model_name('experimental.accessibility'))
98 self
.assertEqual('experimental.accessibility',
99 get_model_name('experimental_accessibility.json'))
100 self
.assertEqual('experimental.accessibility',
101 get_model_name('%s/experimental_accessibility.json' % API
))
102 self
.assertEqual('pageAction', get_model_name('pageAction'))
103 self
.assertEqual('pageAction', get_model_name('page_action.json'))
104 self
.assertEqual('pageAction', get_model_name('%s/page_action.json' % API
))
106 def testGetNonexistentModel(self
):
107 self
.assertRaises(FileNotFoundError
,
108 self
._api
_models
.GetModel('notfound').Get
)
109 self
.assertRaises(FileNotFoundError
,
110 self
._api
_models
.GetModel('notfound.json').Get
)
111 self
.assertRaises(FileNotFoundError
,
112 self
._api
_models
.GetModel('%s/notfound.json' % API
).Get
)
113 self
.assertRaises(FileNotFoundError
,
114 self
._api
_models
.GetModel('%s/alarms.json' % API
).Get
)
115 self
.assertRaises(FileNotFoundError
,
116 self
._api
_models
.GetModel('storage').Get
)
117 self
.assertRaises(FileNotFoundError
,
118 self
._api
_models
.GetModel('%s/storage.json' % API
).Get
)
119 self
.assertRaises(FileNotFoundError
,
120 self
._api
_models
.GetModel('%s/storage.idl' % API
).Get
)
122 def testSingleFile(self
):
123 # 2 stats (1 for JSON and 1 for IDL), 1 read (for IDL file which existed).
124 future
= self
._api
_models
.GetModel('alarms')
125 self
.assertTrue(*self
._mock
_file
_system
.CheckAndReset(
126 read_count
=1, stat_count
=2))
128 # 1 read-resolve (for the IDL file).
130 # The important part here and above is that it's only doing a single read;
131 # any more would break the contract that only a single file is accessed -
132 # see the SingleFile annotation in api_models._CreateAPIModel.
134 self
.assertTrue(*self
._mock
_file
_system
.CheckAndReset(
135 read_resolve_count
=1))
137 # 2 stats (1 for JSON and 1 for IDL), no reads (still cached).
138 future
= self
._api
_models
.GetModel('alarms')
139 self
.assertTrue(*self
._mock
_file
_system
.CheckAndReset(stat_count
=2))
141 self
.assertTrue(*self
._mock
_file
_system
.CheckAndReset())
144 if __name__
== '__main__':