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 import api_schema_graph
10 from availability_finder
import AvailabilityFinder
, AvailabilityInfo
11 from branch_utility
import BranchUtility
, ChannelInfo
12 from compiled_file_system
import CompiledFileSystem
13 from fake_host_file_system_provider
import FakeHostFileSystemProvider
14 from fake_url_fetcher
import FakeUrlFetcher
15 from host_file_system_iterator
import HostFileSystemIterator
16 from mock_function
import MockFunction
17 from object_store_creator
import ObjectStoreCreator
18 from platform_util
import GetPlatforms
19 from test_data
.canned_data
import (CANNED_API_FILE_SYSTEM_DATA
, CANNED_BRANCHES
)
20 from test_data
.object_level_availability
.tabs
import TABS_SCHEMA_BRANCHES
21 from test_util
import Server2Path
22 from schema_processor
import SchemaProcessorFactoryForTest
25 TABS_UNMODIFIED_VERSIONS
= (16, 20, 23, 24)
27 class AvailabilityFinderTest(unittest
.TestCase
):
29 def _create_availability_finder(self
,
33 test_object_store
= ObjectStoreCreator
.ForTest()
34 return AvailabilityFinder(
36 CompiledFileSystem
.Factory(test_object_store
),
38 host_fs_creator
.GetMaster(),
41 SchemaProcessorFactoryForTest())
44 self
._branch
_utility
= BranchUtility(
45 os
.path
.join('branch_utility', 'first.json'),
46 os
.path
.join('branch_utility', 'second.json'),
47 FakeUrlFetcher(Server2Path('test_data')),
48 ObjectStoreCreator
.ForTest())
49 self
._api
_fs
_creator
= FakeHostFileSystemProvider(
50 CANNED_API_FILE_SYSTEM_DATA
)
51 self
._node
_fs
_creator
= FakeHostFileSystemProvider(TABS_SCHEMA_BRANCHES
)
52 self
._api
_fs
_iterator
= HostFileSystemIterator(self
._api
_fs
_creator
,
54 self
._node
_fs
_iterator
= HostFileSystemIterator(self
._node
_fs
_creator
,
57 # Imitate the actual SVN file system by incrementing the stats for paths
58 # where an API schema has changed.
59 last_stat
= type('last_stat', (object,), {'val': 0})
61 def stat_paths(file_system
, channel_info
):
62 if channel_info
.version
not in TABS_UNMODIFIED_VERSIONS
:
64 # HACK: |file_system| is a MockFileSystem backed by a TestFileSystem.
65 # Increment the TestFileSystem stat count.
66 file_system
._file
_system
.IncrementStat(by
=last_stat
.val
)
67 # Continue looping. The iterator will stop after 'master' automatically.
70 # Use the HostFileSystemIterator created above to change global stat values
71 # for the TestFileSystems that it creates.
72 self
._node
_fs
_iterator
.Ascending(
73 # The earliest version represented with the tabs' test data is 13.
74 self
._branch
_utility
.GetStableChannelInfo(13),
77 def testGraphOptimization(self
):
78 for platform
in GetPlatforms():
79 # Keep track of how many times the APISchemaGraph constructor is called.
80 original_constructor
= api_schema_graph
.APISchemaGraph
81 mock_constructor
= MockFunction(original_constructor
)
82 api_schema_graph
.APISchemaGraph
= mock_constructor
84 node_avail_finder
= self
._create
_availability
_finder
(
85 self
._node
_fs
_creator
, self
._node
_fs
_iterator
, platform
)
87 # The test data includes an extra branch where the API does not exist.
88 num_versions
= len(TABS_SCHEMA_BRANCHES
) - 1
89 # We expect an APISchemaGraph to be created only when an API schema file
90 # has different stat data from the previous version's schema file.
91 num_graphs_created
= num_versions
- len(TABS_UNMODIFIED_VERSIONS
)
93 # Run the logic for object-level availability for an API.
94 node_avail_finder
.GetAPINodeAvailability('tabs')
96 self
.assertTrue(*api_schema_graph
.APISchemaGraph
.CheckAndReset(
99 # Ensure that the APISchemaGraph constructor is reset to be the original
101 api_schema_graph
.APISchemaGraph
= original_constructor
103 def testGetAPIAvailability(self
):
104 # Key: Using 'channel' (i.e. 'beta') to represent an availability listing
105 # for an API in a _features.json file, and using |channel| (i.e. |dev|) to
106 # represent the development channel, or phase of development, where an API's
107 # availability is being checked.
109 def assertGet(ch_info
, api
, only_on
=None, scheduled
=None):
110 for platform
in GetPlatforms():
111 get_availability
= self
._create
_availability
_finder
(
112 self
._api
_fs
_creator
,
113 self
._api
_fs
_iterator
,
114 platform
if only_on
is None else only_on
).GetAPIAvailability
115 self
.assertEqual(AvailabilityInfo(ch_info
, scheduled
=scheduled
),
116 get_availability(api
))
118 # Testing APIs with predetermined availability.
119 assertGet(ChannelInfo('master', 'master', 'master'), 'jsonMasterAPI')
120 assertGet(ChannelInfo('dev', CANNED_BRANCHES
[31], 31), 'jsonDevAPI')
121 assertGet(ChannelInfo('beta', CANNED_BRANCHES
[30], 30), 'jsonBetaAPI')
122 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[20], 20), 'jsonStableAPI')
124 # Testing a whitelisted API.
125 assertGet(ChannelInfo('beta', CANNED_BRANCHES
[30], 30),
126 'declarativeWebRequest')
128 # Testing APIs found only by checking file system existence.
129 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[23], 23), 'windows')
130 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[18], 18), 'tabs')
131 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[18], 18), 'input.ime')
133 # Testing API channel existence for _api_features.json.
134 # Listed as 'dev' on |beta|, 'dev' on |dev|.
135 assertGet(ChannelInfo('dev', CANNED_BRANCHES
[31], 31), 'systemInfo.stuff')
136 # Listed as 'stable' on |beta|.
137 assertGet(ChannelInfo('beta', CANNED_BRANCHES
[30], 30),
141 # Testing API channel existence for _manifest_features.json.
142 # Listed as 'master' on all channels.
143 assertGet(ChannelInfo('master', 'master', 'master'), 'sync')
144 # No records of API until |master|.
145 assertGet(ChannelInfo('master', 'master', 'master'), 'history')
146 # Listed as 'dev' on |dev|.
147 assertGet(ChannelInfo('dev', CANNED_BRANCHES
[31], 31), 'storage')
148 # Stable in _manifest_features and into pre-18 versions.
149 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[8], 8), 'pageAction')
151 # Testing API channel existence for _permission_features.json.
152 # Listed as 'beta' on |master|.
153 assertGet(ChannelInfo('master', 'master', 'master'), 'falseBetaAPI')
154 # Listed as 'master' on |master|.
155 assertGet(ChannelInfo('master', 'master', 'master'), 'masterAPI')
156 # Listed as 'master' on all development channels.
157 assertGet(ChannelInfo('master', 'master', 'master'), 'declarativeContent')
158 # Listed as 'dev' on all development channels.
159 assertGet(ChannelInfo('dev', CANNED_BRANCHES
[31], 31), 'bluetooth')
160 # Listed as 'dev' on |dev|.
161 assertGet(ChannelInfo('dev', CANNED_BRANCHES
[31], 31), 'cookies')
162 # Treated as 'stable' APIs.
163 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[24], 24), 'alarms')
164 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[21], 21), 'bookmarks')
166 # Testing older API existence using extension_api.json.
167 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[6], 6), 'menus')
168 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[5], 5), 'idle')
170 # Switches between _features.json files across branches.
171 # Listed as 'master' on all channels, in _api, _permission, or _manifest.
172 assertGet(ChannelInfo('master', 'master', 'master'), 'contextMenus')
173 # Moves between _permission and _manifest as file system is traversed.
174 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[23], 23),
175 'systemInfo.display')
176 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[17], 17), 'webRequest')
179 # Listed as 'dev' on |beta| and 'beta' on |dev|.
180 assertGet(ChannelInfo('dev', CANNED_BRANCHES
[31], 31), 'notifications')
181 # Listed as 'beta' on |stable|, 'dev' on |beta|...until |stable| on master.
182 assertGet(ChannelInfo('master', 'master', 'master'), 'events')
184 # Check for differing availability across apps|extensions
185 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[26], 26),
187 only_on
='extensions')
188 assertGet(ChannelInfo('stable', CANNED_BRANCHES
[25], 25),
192 def testGetAPINodeAvailability(self
):
193 def assertEquals(found
, channel_info
, actual
, scheduled
=None):
194 lookup_result
= api_schema_graph
.LookupResult
195 if channel_info
is None:
196 self
.assertEquals(lookup_result(found
, None), actual
)
198 self
.assertEquals(lookup_result(found
, AvailabilityInfo(channel_info
,
199 scheduled
=scheduled
)), actual
)
201 for platform
in GetPlatforms():
202 # Allow the LookupResult constructions below to take just one line.
203 avail_finder
= self
._create
_availability
_finder
(
204 self
._node
_fs
_creator
,
205 self
._node
_fs
_iterator
,
207 tabs_graph
= avail_finder
.GetAPINodeAvailability('tabs')
208 fake_tabs_graph
= avail_finder
.GetAPINodeAvailability('fakeTabs')
210 # Test an API node with predetermined availability.
211 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(27),
212 tabs_graph
.Lookup('tabs', 'properties', 'fakeTabsProperty4'))
214 assertEquals(True, self
._branch
_utility
.GetChannelInfo('master'),
215 tabs_graph
.Lookup('tabs', 'properties', 'fakeTabsProperty3'))
216 assertEquals(True, self
._branch
_utility
.GetChannelInfo('dev'),
217 tabs_graph
.Lookup('tabs', 'events', 'onActivated', 'parameters',
218 'activeInfo', 'properties', 'windowId'), scheduled
=31)
219 assertEquals(True, self
._branch
_utility
.GetChannelInfo('dev'),
220 tabs_graph
.Lookup('tabs', 'events', 'onUpdated', 'parameters', 'tab'),
222 assertEquals(True, self
._branch
_utility
.GetChannelInfo('beta'),
223 tabs_graph
.Lookup('tabs', 'events', 'onActivated'), scheduled
=30)
224 assertEquals(True, self
._branch
_utility
.GetChannelInfo('beta'),
225 tabs_graph
.Lookup('tabs', 'functions', 'get', 'parameters', 'tabId'),
227 assertEquals(True, self
._branch
_utility
.GetChannelInfo('stable'),
228 tabs_graph
.Lookup('tabs', 'types', 'InjectDetails', 'properties',
230 assertEquals(True, self
._branch
_utility
.GetChannelInfo('stable'),
231 tabs_graph
.Lookup('tabs', 'types', 'InjectDetails', 'properties',
233 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(25),
234 tabs_graph
.Lookup('tabs', 'types', 'InjectDetails'))
237 assertEquals(True, self
._branch
_utility
.GetChannelInfo('master'),
238 tabs_graph
.Lookup('tabs', 'types', 'InlinedType'))
240 # Test implicitly inlined type.
241 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(25),
242 fake_tabs_graph
.Lookup('fakeTabs', 'types',
243 'WasImplicitlyInlinedType'))
245 # Test a node that was restricted to dev channel when it was introduced.
246 assertEquals(True, self
._branch
_utility
.GetChannelInfo('beta'),
247 tabs_graph
.Lookup('tabs', 'functions', 'restrictedFunc'),
250 # Test an explicitly scheduled node.
251 assertEquals(True, self
._branch
_utility
.GetChannelInfo('dev'),
252 tabs_graph
.Lookup('tabs', 'functions', 'scheduledFunc'),
255 # Nothing new in version 24 or 23.
257 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(22),
258 tabs_graph
.Lookup('tabs', 'types', 'Tab', 'properties', 'windowId'))
259 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(21),
260 tabs_graph
.Lookup('tabs', 'types', 'Tab', 'properties', 'selected'))
262 # Nothing new in version 20.
264 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(19),
265 tabs_graph
.Lookup('tabs', 'functions', 'getCurrent'))
266 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(18),
267 tabs_graph
.Lookup('tabs', 'types', 'Tab', 'properties', 'index'))
268 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(17),
269 tabs_graph
.Lookup('tabs', 'events', 'onUpdated', 'parameters',
272 # Nothing new in version 16.
274 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(15),
275 tabs_graph
.Lookup('tabs', 'properties', 'fakeTabsProperty2'))
277 # Everything else is available at the API's release, version 14 here.
278 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(14),
279 tabs_graph
.Lookup('tabs', 'types', 'Tab'))
280 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(14),
281 tabs_graph
.Lookup('tabs', 'types', 'Tab', 'properties', 'url'))
282 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(14),
283 tabs_graph
.Lookup('tabs', 'properties', 'fakeTabsProperty1'))
284 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(14),
285 tabs_graph
.Lookup('tabs', 'functions', 'get', 'parameters',
287 assertEquals(True, self
._branch
_utility
.GetStableChannelInfo(14),
288 tabs_graph
.Lookup('tabs', 'events', 'onUpdated'))
290 # Test things that aren't available.
291 assertEquals(False, None, tabs_graph
.Lookup('tabs', 'types',
293 assertEquals(False, None, tabs_graph
.Lookup('tabs', 'functions', 'get',
294 'parameters', 'callback', 'parameters', 'tab', 'id'))
295 assertEquals(False, None, tabs_graph
.Lookup('functions'))
296 assertEquals(False, None, tabs_graph
.Lookup('events', 'onActivated',
297 'parameters', 'activeInfo', 'tabId'))
300 if __name__
== '__main__':