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
24 TABS_UNMODIFIED_VERSIONS = (16, 20, 23, 24)
26 class AvailabilityFinderTest(unittest.TestCase):
28 def _create_availability_finder(self,
32 test_object_store = ObjectStoreCreator.ForTest()
33 return AvailabilityFinder(
35 CompiledFileSystem.Factory(test_object_store),
37 host_fs_creator.GetTrunk(),
42 self._branch_utility = BranchUtility(
43 os.path.join('branch_utility', 'first.json'),
44 os.path.join('branch_utility', 'second.json'),
45 FakeUrlFetcher(Server2Path('test_data')),
46 ObjectStoreCreator.ForTest())
47 self._api_fs_creator = FakeHostFileSystemProvider(
48 CANNED_API_FILE_SYSTEM_DATA)
49 self._node_fs_creator = FakeHostFileSystemProvider(TABS_SCHEMA_BRANCHES)
50 self._api_fs_iterator = HostFileSystemIterator(self._api_fs_creator,
52 self._node_fs_iterator = HostFileSystemIterator(self._node_fs_creator,
55 # Imitate the actual SVN file system by incrementing the stats for paths
56 # where an API schema has changed.
57 last_stat = type('last_stat', (object,), {'val': 0})
59 def stat_paths(file_system, channel_info):
60 if channel_info.version not in TABS_UNMODIFIED_VERSIONS:
62 # HACK: |file_system| is a MockFileSystem backed by a TestFileSystem.
63 # Increment the TestFileSystem stat count.
64 file_system._file_system.IncrementStat(by=last_stat.val)
65 # Continue looping. The iterator will stop after 'trunk' automatically.
68 # Use the HostFileSystemIterator created above to change global stat values
69 # for the TestFileSystems that it creates.
70 self._node_fs_iterator.Ascending(
71 # The earliest version represented with the tabs' test data is 13.
72 self._branch_utility.GetStableChannelInfo(13),
75 def testGraphOptimization(self):
76 for platform in GetPlatforms():
77 # Keep track of how many times the APISchemaGraph constructor is called.
78 original_constructor = api_schema_graph.APISchemaGraph
79 mock_constructor = MockFunction(original_constructor)
80 api_schema_graph.APISchemaGraph = mock_constructor
82 node_avail_finder = self._create_availability_finder(
83 self._node_fs_creator, self._node_fs_iterator, platform)
85 # The test data includes an extra branch where the API does not exist.
86 num_versions = len(TABS_SCHEMA_BRANCHES) - 1
87 # We expect an APISchemaGraph to be created only when an API schema file
88 # has different stat data from the previous version's schema file.
89 num_graphs_created = num_versions - len(TABS_UNMODIFIED_VERSIONS)
91 # Run the logic for object-level availability for an API.
92 node_avail_finder.GetAPINodeAvailability('tabs')
94 self.assertTrue(*api_schema_graph.APISchemaGraph.CheckAndReset(
97 # Ensure that the APISchemaGraph constructor is reset to be the original
99 api_schema_graph.APISchemaGraph = original_constructor
101 def testGetAPIAvailability(self):
102 # Key: Using 'channel' (i.e. 'beta') to represent an availability listing
103 # for an API in a _features.json file, and using |channel| (i.e. |dev|) to
104 # represent the development channel, or phase of development, where an API's
105 # availability is being checked.
107 def assertGet(ch_info, api, only_on=None, scheduled=None):
108 for platform in GetPlatforms():
109 get_availability = self._create_availability_finder(
110 self._api_fs_creator,
111 self._api_fs_iterator,
112 platform if only_on is None else only_on).GetAPIAvailability
113 self.assertEqual(AvailabilityInfo(ch_info, scheduled=scheduled),
114 get_availability(api))
116 # Testing APIs with predetermined availability.
117 assertGet(ChannelInfo('trunk', 'trunk', 'trunk'), 'jsonTrunkAPI')
118 assertGet(ChannelInfo('dev', CANNED_BRANCHES[31], 31), 'jsonDevAPI')
119 assertGet(ChannelInfo('beta', CANNED_BRANCHES[30], 30), 'jsonBetaAPI')
120 assertGet(ChannelInfo('stable', CANNED_BRANCHES[20], 20), 'jsonStableAPI')
122 # Testing a whitelisted API.
123 assertGet(ChannelInfo('beta', CANNED_BRANCHES[30], 30),
124 'declarativeWebRequest')
126 # Testing APIs found only by checking file system existence.
127 assertGet(ChannelInfo('stable', CANNED_BRANCHES[23], 23), 'windows')
128 assertGet(ChannelInfo('stable', CANNED_BRANCHES[18], 18), 'tabs')
129 assertGet(ChannelInfo('stable', CANNED_BRANCHES[18], 18), 'input.ime')
131 # Testing API channel existence for _api_features.json.
132 # Listed as 'dev' on |beta|, 'dev' on |dev|.
133 assertGet(ChannelInfo('dev', CANNED_BRANCHES[31], 31), 'systemInfo.stuff')
134 # Listed as 'stable' on |beta|.
135 assertGet(ChannelInfo('beta', CANNED_BRANCHES[30], 30),
139 # Testing API channel existence for _manifest_features.json.
140 # Listed as 'trunk' on all channels.
141 assertGet(ChannelInfo('trunk', 'trunk', 'trunk'), 'sync')
142 # No records of API until |trunk|.
143 assertGet(ChannelInfo('trunk', 'trunk', 'trunk'), 'history')
144 # Listed as 'dev' on |dev|.
145 assertGet(ChannelInfo('dev', CANNED_BRANCHES[31], 31), 'storage')
146 # Stable in _manifest_features and into pre-18 versions.
147 assertGet(ChannelInfo('stable', CANNED_BRANCHES[8], 8), 'pageAction')
149 # Testing API channel existence for _permission_features.json.
150 # Listed as 'beta' on |trunk|.
151 assertGet(ChannelInfo('trunk', 'trunk', 'trunk'), 'falseBetaAPI')
152 # Listed as 'trunk' on |trunk|.
153 assertGet(ChannelInfo('trunk', 'trunk', 'trunk'), 'trunkAPI')
154 # Listed as 'trunk' on all development channels.
155 assertGet(ChannelInfo('trunk', 'trunk', 'trunk'), 'declarativeContent')
156 # Listed as 'dev' on all development channels.
157 assertGet(ChannelInfo('dev', CANNED_BRANCHES[31], 31), 'bluetooth')
158 # Listed as 'dev' on |dev|.
159 assertGet(ChannelInfo('dev', CANNED_BRANCHES[31], 31), 'cookies')
160 # Treated as 'stable' APIs.
161 assertGet(ChannelInfo('stable', CANNED_BRANCHES[24], 24), 'alarms')
162 assertGet(ChannelInfo('stable', CANNED_BRANCHES[21], 21), 'bookmarks')
164 # Testing older API existence using extension_api.json.
165 assertGet(ChannelInfo('stable', CANNED_BRANCHES[6], 6), 'menus')
166 assertGet(ChannelInfo('stable', CANNED_BRANCHES[5], 5), 'idle')
168 # Switches between _features.json files across branches.
169 # Listed as 'trunk' on all channels, in _api, _permission, or _manifest.
170 assertGet(ChannelInfo('trunk', 'trunk', 'trunk'), 'contextMenus')
171 # Moves between _permission and _manifest as file system is traversed.
172 assertGet(ChannelInfo('stable', CANNED_BRANCHES[23], 23),
173 'systemInfo.display')
174 assertGet(ChannelInfo('stable', CANNED_BRANCHES[17], 17), 'webRequest')
177 # Listed as 'dev' on |beta| and 'beta' on |dev|.
178 assertGet(ChannelInfo('dev', CANNED_BRANCHES[31], 31), 'notifications')
179 # Listed as 'beta' on |stable|, 'dev' on |beta|...until |stable| on trunk.
180 assertGet(ChannelInfo('trunk', 'trunk', 'trunk'), 'events')
182 # Check for differing availability across apps|extensions
183 assertGet(ChannelInfo('stable', CANNED_BRANCHES[26], 26),
185 only_on='extensions')
186 assertGet(ChannelInfo('stable', CANNED_BRANCHES[25], 25),
190 def testGetAPINodeAvailability(self):
191 def assertEquals(found, channel_info, actual, scheduled=None):
192 lookup_result = api_schema_graph.LookupResult
193 if channel_info is None:
194 self.assertEquals(lookup_result(found, None), actual)
196 self.assertEquals(lookup_result(found, AvailabilityInfo(channel_info,
197 scheduled=scheduled)), actual)
199 for platform in GetPlatforms():
200 # Allow the LookupResult constructions below to take just one line.
201 avail_finder = self._create_availability_finder(
202 self._node_fs_creator,
203 self._node_fs_iterator,
205 tabs_graph = avail_finder.GetAPINodeAvailability('tabs')
206 fake_tabs_graph = avail_finder.GetAPINodeAvailability('fakeTabs')
208 assertEquals(True, self._branch_utility.GetChannelInfo('trunk'),
209 tabs_graph.Lookup('tabs', 'properties', 'fakeTabsProperty3'))
210 assertEquals(True, self._branch_utility.GetChannelInfo('dev'),
211 tabs_graph.Lookup('tabs', 'events', 'onActivated', 'parameters',
212 'activeInfo', 'properties', 'windowId'), scheduled=31)
213 assertEquals(True, self._branch_utility.GetChannelInfo('dev'),
214 tabs_graph.Lookup('tabs', 'events', 'onUpdated', 'parameters', 'tab'),
216 assertEquals(True, self._branch_utility.GetChannelInfo('beta'),
217 tabs_graph.Lookup('tabs', 'events', 'onActivated'), scheduled=30)
218 assertEquals(True, self._branch_utility.GetChannelInfo('beta'),
219 tabs_graph.Lookup('tabs', 'functions', 'get', 'parameters', 'tabId'),
221 assertEquals(True, self._branch_utility.GetChannelInfo('stable'),
222 tabs_graph.Lookup('tabs', 'types', 'InjectDetails', 'properties',
224 assertEquals(True, self._branch_utility.GetChannelInfo('stable'),
225 tabs_graph.Lookup('tabs', 'types', 'InjectDetails', 'properties',
227 assertEquals(True, self._branch_utility.GetStableChannelInfo(25),
228 tabs_graph.Lookup('tabs', 'types', 'InjectDetails'))
231 assertEquals(True, self._branch_utility.GetChannelInfo('trunk'),
232 tabs_graph.Lookup('tabs', 'types', 'InlinedType'))
234 # Test implicitly inlined type.
235 assertEquals(True, self._branch_utility.GetStableChannelInfo(25),
236 fake_tabs_graph.Lookup('fakeTabs', 'types',
237 'WasImplicitlyInlinedType'))
239 # Test a node that was restricted to dev channel when it was introduced.
240 assertEquals(True, self._branch_utility.GetChannelInfo('beta'),
241 tabs_graph.Lookup('tabs', 'functions', 'restrictedFunc'),
244 # Test an explicitly scheduled node.
245 assertEquals(True, self._branch_utility.GetChannelInfo('dev'),
246 tabs_graph.Lookup('tabs', 'functions', 'scheduledFunc'),
249 # Nothing new in version 24 or 23.
251 assertEquals(True, self._branch_utility.GetStableChannelInfo(22),
252 tabs_graph.Lookup('tabs', 'types', 'Tab', 'properties', 'windowId'))
253 assertEquals(True, self._branch_utility.GetStableChannelInfo(21),
254 tabs_graph.Lookup('tabs', 'types', 'Tab', 'properties', 'selected'))
256 # Nothing new in version 20.
258 assertEquals(True, self._branch_utility.GetStableChannelInfo(19),
259 tabs_graph.Lookup('tabs', 'functions', 'getCurrent'))
260 assertEquals(True, self._branch_utility.GetStableChannelInfo(18),
261 tabs_graph.Lookup('tabs', 'types', 'Tab', 'properties', 'index'))
262 assertEquals(True, self._branch_utility.GetStableChannelInfo(17),
263 tabs_graph.Lookup('tabs', 'events', 'onUpdated', 'parameters',
266 # Nothing new in version 16.
268 assertEquals(True, self._branch_utility.GetStableChannelInfo(15),
269 tabs_graph.Lookup('tabs', 'properties', 'fakeTabsProperty2'))
271 # Everything else is available at the API's release, version 14 here.
272 assertEquals(True, self._branch_utility.GetStableChannelInfo(14),
273 tabs_graph.Lookup('tabs', 'types', 'Tab'))
274 assertEquals(True, self._branch_utility.GetStableChannelInfo(14),
275 tabs_graph.Lookup('tabs', 'types', 'Tab', 'properties', 'url'))
276 assertEquals(True, self._branch_utility.GetStableChannelInfo(14),
277 tabs_graph.Lookup('tabs', 'properties', 'fakeTabsProperty1'))
278 assertEquals(True, self._branch_utility.GetStableChannelInfo(14),
279 tabs_graph.Lookup('tabs', 'functions', 'get', 'parameters',
281 assertEquals(True, self._branch_utility.GetStableChannelInfo(14),
282 tabs_graph.Lookup('tabs', 'events', 'onUpdated'))
284 # Test things that aren't available.
285 assertEquals(False, None, tabs_graph.Lookup('tabs', 'types',
287 assertEquals(False, None, tabs_graph.Lookup('tabs', 'functions', 'get',
288 'parameters', 'callback', 'parameters', 'tab', 'id'))
289 assertEquals(False, None, tabs_graph.Lookup('functions'))
290 assertEquals(False, None, tabs_graph.Lookup('events', 'onActivated',
291 'parameters', 'activeInfo', 'tabId'))
294 if __name__ == '__main__':