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 test_data
.canned_data
import (CANNED_API_FILE_SYSTEM_DATA
, CANNED_BRANCHES
)
19 from test_data
.object_level_availability
.tabs
import TABS_SCHEMA_BRANCHES
20 from test_util
import Server2Path
23 TABS_UNMODIFIED_VERSIONS
= (16, 20, 23, 24)
25 class AvailabilityFinderTest(unittest
.TestCase
):
28 self
._branch
_utility
= BranchUtility(
29 os
.path
.join('branch_utility', 'first.json'),
30 os
.path
.join('branch_utility', 'second.json'),
31 FakeUrlFetcher(Server2Path('test_data')),
32 ObjectStoreCreator
.ForTest())
33 api_fs_creator
= FakeHostFileSystemProvider(CANNED_API_FILE_SYSTEM_DATA
)
34 self
._node
_fs
_creator
= FakeHostFileSystemProvider(TABS_SCHEMA_BRANCHES
)
36 def create_availability_finder(host_fs_creator
):
37 test_object_store
= ObjectStoreCreator
.ForTest()
38 return AvailabilityFinder(
40 CompiledFileSystem
.Factory(test_object_store
),
41 HostFileSystemIterator(host_fs_creator
,
42 self
._branch
_utility
),
43 host_fs_creator
.GetTrunk(),
46 self
._avail
_finder
= create_availability_finder(api_fs_creator
)
47 self
._node
_avail
_finder
= create_availability_finder(self
._node
_fs
_creator
)
49 # Imitate the actual SVN file system by incrementing the stats for paths
50 # where an API schema has changed.
51 last_stat
= type('last_stat', (object,), {'val': 0})
53 def stat_paths(file_system
, channel_info
):
54 if channel_info
.version
not in TABS_UNMODIFIED_VERSIONS
:
56 # HACK: |file_system| is a MockFileSystem backed by a TestFileSystem.
57 # Increment the TestFileSystem stat count.
58 file_system
._file
_system
.IncrementStat(by
=last_stat
.val
)
59 # Continue looping. The iterator will stop after 'trunk' automatically.
62 # Use the HostFileSystemIterator created above to change global stat values
63 # for the TestFileSystems that it creates.
64 self
._node
_avail
_finder
._file
_system
_iterator
.Ascending(
65 # The earliest version represented with the tabs' test data is 13.
66 self
._branch
_utility
.GetStableChannelInfo(13),
69 def testGraphOptimization(self
):
70 # Keep track of how many times the APISchemaGraph constructor is called.
71 original_constructor
= api_schema_graph
.APISchemaGraph
72 mock_constructor
= MockFunction(original_constructor
)
73 api_schema_graph
.APISchemaGraph
= mock_constructor
76 # The test data includes an extra branch where the API does not exist.
77 num_versions
= len(TABS_SCHEMA_BRANCHES
) - 1
78 # We expect an APISchemaGraph to be created only when an API schema file
79 # has different stat data from the previous version's schema file.
80 num_graphs_created
= num_versions
- len(TABS_UNMODIFIED_VERSIONS
)
82 # Run the logic for object-level availability for an API.
83 self
._node
_avail
_finder
.GetApiNodeAvailability('tabs')
85 self
.assertTrue(*api_schema_graph
.APISchemaGraph
.CheckAndReset(
88 # Ensure that the APISchemaGraph constructor is reset to be the original
90 api_schema_graph
.APISchemaGraph
= original_constructor
92 def testGetApiAvailability(self
):
93 # Key: Using 'channel' (i.e. 'beta') to represent an availability listing
94 # for an API in a _features.json file, and using |channel| (i.e. |dev|) to
95 # represent the development channel, or phase of development, where an API's
96 # availability is being checked.
98 # Testing APIs with predetermined availability.
100 AvailabilityInfo(ChannelInfo('trunk', 'trunk', 'trunk')),
101 self
._avail
_finder
.GetApiAvailability('jsonTrunkAPI'))
103 AvailabilityInfo(ChannelInfo('dev', CANNED_BRANCHES
[28], 28)),
104 self
._avail
_finder
.GetApiAvailability('jsonDevAPI'))
106 AvailabilityInfo(ChannelInfo('beta', CANNED_BRANCHES
[27], 27)),
107 self
._avail
_finder
.GetApiAvailability('jsonBetaAPI'))
109 AvailabilityInfo(ChannelInfo('stable', CANNED_BRANCHES
[20], 20)),
110 self
._avail
_finder
.GetApiAvailability('jsonStableAPI'))
112 # Testing a whitelisted API.
114 AvailabilityInfo(ChannelInfo('beta', CANNED_BRANCHES
[27], 27)),
115 self
._avail
_finder
.GetApiAvailability('declarativeWebRequest'))
117 # Testing APIs found only by checking file system existence.
119 AvailabilityInfo(ChannelInfo('stable', CANNED_BRANCHES
[23], 23)),
120 self
._avail
_finder
.GetApiAvailability('windows'))
122 AvailabilityInfo(ChannelInfo('stable', CANNED_BRANCHES
[18], 18)),
123 self
._avail
_finder
.GetApiAvailability('tabs'))
125 AvailabilityInfo(ChannelInfo('stable', CANNED_BRANCHES
[18], 18)),
126 self
._avail
_finder
.GetApiAvailability('input.ime'))
128 # Testing API channel existence for _api_features.json.
129 # Listed as 'dev' on |beta|, 'dev' on |dev|.
131 AvailabilityInfo(ChannelInfo('dev', CANNED_BRANCHES
[28], 28)),
132 self
._avail
_finder
.GetApiAvailability('systemInfo.stuff'))
133 # Listed as 'stable' on |beta|.
136 ChannelInfo('beta', CANNED_BRANCHES
[27], 27),
138 self
._avail
_finder
.GetApiAvailability('systemInfo.cpu'))
140 # Testing API channel existence for _manifest_features.json.
141 # Listed as 'trunk' on all channels.
143 AvailabilityInfo(ChannelInfo('trunk', 'trunk', 'trunk')),
144 self
._avail
_finder
.GetApiAvailability('sync'))
145 # No records of API until |trunk|.
147 AvailabilityInfo(ChannelInfo('trunk', 'trunk', 'trunk')),
148 self
._avail
_finder
.GetApiAvailability('history'))
149 # Listed as 'dev' on |dev|.
151 AvailabilityInfo(ChannelInfo('dev', CANNED_BRANCHES
[28], 28)),
152 self
._avail
_finder
.GetApiAvailability('storage'))
153 # Stable in _manifest_features and into pre-18 versions.
155 AvailabilityInfo(ChannelInfo('stable', CANNED_BRANCHES
[8], 8)),
156 self
._avail
_finder
.GetApiAvailability('pageAction'))
158 # Testing API channel existence for _permission_features.json.
159 # Listed as 'beta' on |trunk|.
161 AvailabilityInfo(ChannelInfo('trunk', 'trunk', 'trunk')),
162 self
._avail
_finder
.GetApiAvailability('falseBetaAPI'))
163 # Listed as 'trunk' on |trunk|.
165 AvailabilityInfo(ChannelInfo('trunk', 'trunk', 'trunk')),
166 self
._avail
_finder
.GetApiAvailability('trunkAPI'))
167 # Listed as 'trunk' on all development channels.
169 AvailabilityInfo(ChannelInfo('trunk', 'trunk', 'trunk')),
170 self
._avail
_finder
.GetApiAvailability('declarativeContent'))
171 # Listed as 'dev' on all development channels.
173 AvailabilityInfo(ChannelInfo('dev', CANNED_BRANCHES
[28], 28)),
174 self
._avail
_finder
.GetApiAvailability('bluetooth'))
175 # Listed as 'dev' on |dev|.
177 AvailabilityInfo(ChannelInfo('dev', CANNED_BRANCHES
[28], 28)),
178 self
._avail
_finder
.GetApiAvailability('cookies'))
179 # Treated as 'stable' APIs.
181 AvailabilityInfo(ChannelInfo('stable', CANNED_BRANCHES
[24], 24)),
182 self
._avail
_finder
.GetApiAvailability('alarms'))
184 AvailabilityInfo(ChannelInfo('stable', CANNED_BRANCHES
[21], 21)),
185 self
._avail
_finder
.GetApiAvailability('bookmarks'))
187 # Testing older API existence using extension_api.json.
189 AvailabilityInfo(ChannelInfo('stable', CANNED_BRANCHES
[6], 6)),
190 self
._avail
_finder
.GetApiAvailability('menus'))
192 AvailabilityInfo(ChannelInfo('stable', CANNED_BRANCHES
[5], 5)),
193 self
._avail
_finder
.GetApiAvailability('idle'))
195 # Switches between _features.json files across branches.
196 # Listed as 'trunk' on all channels, in _api, _permission, or _manifest.
198 AvailabilityInfo(ChannelInfo('trunk', 'trunk', 'trunk')),
199 self
._avail
_finder
.GetApiAvailability('contextMenus'))
200 # Moves between _permission and _manifest as file system is traversed.
202 AvailabilityInfo(ChannelInfo('stable', CANNED_BRANCHES
[23], 23)),
203 self
._avail
_finder
.GetApiAvailability('systemInfo.display'))
205 AvailabilityInfo(ChannelInfo('stable', CANNED_BRANCHES
[17], 17)),
206 self
._avail
_finder
.GetApiAvailability('webRequest'))
209 # Listed as 'dev' on |beta| and 'beta' on |dev|.
211 AvailabilityInfo(ChannelInfo('dev', CANNED_BRANCHES
[28], 28)),
212 self
._avail
_finder
.GetApiAvailability('notifications'))
213 # Listed as 'beta' on |stable|, 'dev' on |beta| ... until |stable| on trunk.
215 AvailabilityInfo(ChannelInfo('trunk', 'trunk', 'trunk')),
216 self
._avail
_finder
.GetApiAvailability('events'))
218 def testGetApiNodeAvailability(self
):
219 # Allow the LookupResult constructions below to take just one line.
220 lookup_result
= api_schema_graph
.LookupResult
221 availability_graph
= self
._node
_avail
_finder
.GetApiNodeAvailability('tabs')
224 lookup_result(True, self
._branch
_utility
.GetChannelInfo('trunk')),
225 availability_graph
.Lookup('tabs', 'properties',
226 'fakeTabsProperty3'))
228 lookup_result(True, self
._branch
_utility
.GetChannelInfo('dev')),
229 availability_graph
.Lookup('tabs', 'events', 'onActivated',
230 'parameters', 'activeInfo', 'properties',
233 lookup_result(True, self
._branch
_utility
.GetChannelInfo('dev')),
234 availability_graph
.Lookup('tabs', 'events', 'onUpdated', 'parameters',
237 lookup_result(True, self
._branch
_utility
.GetChannelInfo('beta')),
238 availability_graph
.Lookup('tabs', 'events','onActivated'))
240 lookup_result(True, self
._branch
_utility
.GetChannelInfo('beta')),
241 availability_graph
.Lookup('tabs', 'functions', 'get', 'parameters',
244 lookup_result(True, self
._branch
_utility
.GetChannelInfo('stable')),
245 availability_graph
.Lookup('tabs', 'types', 'InjectDetails',
246 'properties', 'code'))
248 lookup_result(True, self
._branch
_utility
.GetChannelInfo('stable')),
249 availability_graph
.Lookup('tabs', 'types', 'InjectDetails',
250 'properties', 'file'))
252 lookup_result(True, self
._branch
_utility
.GetStableChannelInfo(25)),
253 availability_graph
.Lookup('tabs', 'types', 'InjectDetails'))
255 # Nothing new in version 24 or 23.
258 lookup_result(True, self
._branch
_utility
.GetStableChannelInfo(22)),
259 availability_graph
.Lookup('tabs', 'types', 'Tab', 'properties',
262 lookup_result(True, self
._branch
_utility
.GetStableChannelInfo(21)),
263 availability_graph
.Lookup('tabs', 'types', 'Tab', 'properties',
266 # Nothing new in version 20.
269 lookup_result(True, self
._branch
_utility
.GetStableChannelInfo(19)),
270 availability_graph
.Lookup('tabs', 'functions', 'getCurrent'))
272 lookup_result(True, self
._branch
_utility
.GetStableChannelInfo(18)),
273 availability_graph
.Lookup('tabs', 'types', 'Tab', 'properties',
276 lookup_result(True, self
._branch
_utility
.GetStableChannelInfo(17)),
277 availability_graph
.Lookup('tabs', 'events', 'onUpdated', 'parameters',
280 # Nothing new in version 16.
283 lookup_result(True, self
._branch
_utility
.GetStableChannelInfo(15)),
284 availability_graph
.Lookup('tabs', 'properties',
285 'fakeTabsProperty2'))
287 # Everything else is available at the API's release, version 14 here.
289 lookup_result(True, self
._branch
_utility
.GetStableChannelInfo(14)),
290 availability_graph
.Lookup('tabs', 'types', 'Tab'))
292 lookup_result(True, self
._branch
_utility
.GetStableChannelInfo(14)),
293 availability_graph
.Lookup('tabs', 'types', 'Tab',
294 'properties', 'url'))
296 lookup_result(True, self
._branch
_utility
.GetStableChannelInfo(14)),
297 availability_graph
.Lookup('tabs', 'properties',
298 'fakeTabsProperty1'))
300 lookup_result(True, self
._branch
_utility
.GetStableChannelInfo(14)),
301 availability_graph
.Lookup('tabs', 'functions', 'get', 'parameters',
304 lookup_result(True, self
._branch
_utility
.GetStableChannelInfo(14)),
305 availability_graph
.Lookup('tabs', 'events', 'onUpdated'))
307 # Test things that aren't available.
308 self
.assertEqual(lookup_result(False, None),
309 availability_graph
.Lookup('tabs', 'types',
311 self
.assertEqual(lookup_result(False, None),
312 availability_graph
.Lookup('tabs', 'functions', 'get',
313 'parameters', 'callback',
314 'parameters', 'tab', 'id'))
315 self
.assertEqual(lookup_result(False, None),
316 availability_graph
.Lookup('functions'))
317 self
.assertEqual(lookup_result(False, None),
318 availability_graph
.Lookup('events', 'onActivated',
319 'parameters', 'activeInfo',
323 if __name__
== '__main__':