Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / availability_finder_test.py
1 #!/usr/bin/env python
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.
5 import os
6 import sys
7 import unittest
8
9 import api_schema_graph
10 from availability_finder import AvailabilityFinder
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
21
22
23 TABS_UNMODIFIED_VERSIONS = (16, 20, 23, 24)
24
25 class AvailabilityFinderTest(unittest.TestCase):
26
27   def setUp(self):
28     self._branch_utility = BranchUtility(
29         os.path.join('branch_utility', 'first.json'),
30         os.path.join('branch_utility', 'second.json'),
31         FakeUrlFetcher(os.path.join(sys.path[0], 'test_data')),
32         ObjectStoreCreator.ForTest())
33     api_fs_creator = FakeHostFileSystemProvider(CANNED_API_FILE_SYSTEM_DATA)
34     self._node_fs_creator = FakeHostFileSystemProvider(TABS_SCHEMA_BRANCHES)
35
36     def create_availability_finder(host_fs_creator):
37       test_object_store = ObjectStoreCreator.ForTest()
38       return AvailabilityFinder(
39           self._branch_utility,
40           CompiledFileSystem.Factory(test_object_store),
41           HostFileSystemIterator(host_fs_creator,
42                                  self._branch_utility),
43           host_fs_creator.GetTrunk(),
44           test_object_store)
45
46     self._avail_finder = create_availability_finder(api_fs_creator)
47     self._node_avail_finder = create_availability_finder(self._node_fs_creator)
48
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})
52
53     def stat_paths(file_system, channel_info):
54       if channel_info.version not in TABS_UNMODIFIED_VERSIONS:
55         last_stat.val += 1
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.
60       return True
61
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),
67         stat_paths)
68
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
74
75     try:
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)
81
82       # Run the logic for object-level availability for an API.
83       self._node_avail_finder.GetApiNodeAvailability('tabs')
84
85       self.assertTrue(*api_schema_graph.APISchemaGraph.CheckAndReset(
86           num_graphs_created))
87     finally:
88       # Ensure that the APISchemaGraph constructor is reset to be the original
89       # constructor.
90       api_schema_graph.APISchemaGraph = original_constructor
91
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.
97
98     # Testing APIs with predetermined availability.
99     self.assertEqual(
100         ChannelInfo('trunk', 'trunk', 'trunk'),
101         self._avail_finder.GetApiAvailability('jsonTrunkAPI'))
102     self.assertEqual(
103         ChannelInfo('dev', CANNED_BRANCHES[28], 28),
104         self._avail_finder.GetApiAvailability('jsonDevAPI'))
105     self.assertEqual(
106         ChannelInfo('beta', CANNED_BRANCHES[27], 27),
107         self._avail_finder.GetApiAvailability('jsonBetaAPI'))
108     self.assertEqual(
109         ChannelInfo('stable', CANNED_BRANCHES[20], 20),
110         self._avail_finder.GetApiAvailability('jsonStableAPI'))
111
112     # Testing a whitelisted API.
113     self.assertEquals(
114         ChannelInfo('beta', CANNED_BRANCHES[27], 27),
115         self._avail_finder.GetApiAvailability('declarativeWebRequest'))
116
117     # Testing APIs found only by checking file system existence.
118     self.assertEquals(
119         ChannelInfo('stable', CANNED_BRANCHES[23], 23),
120         self._avail_finder.GetApiAvailability('windows'))
121     self.assertEquals(
122         ChannelInfo('stable', CANNED_BRANCHES[18], 18),
123         self._avail_finder.GetApiAvailability('tabs'))
124     self.assertEquals(
125         ChannelInfo('stable', CANNED_BRANCHES[18], 18),
126         self._avail_finder.GetApiAvailability('input.ime'))
127
128     # Testing API channel existence for _api_features.json.
129     # Listed as 'dev' on |beta|, 'dev' on |dev|.
130     self.assertEquals(
131         ChannelInfo('dev', CANNED_BRANCHES[28], 28),
132         self._avail_finder.GetApiAvailability('systemInfo.stuff'))
133     # Listed as 'stable' on |beta|.
134     self.assertEquals(
135         ChannelInfo('beta', CANNED_BRANCHES[27], 27),
136         self._avail_finder.GetApiAvailability('systemInfo.cpu'))
137
138     # Testing API channel existence for _manifest_features.json.
139     # Listed as 'trunk' on all channels.
140     self.assertEquals(
141         ChannelInfo('trunk', 'trunk', 'trunk'),
142         self._avail_finder.GetApiAvailability('sync'))
143     # No records of API until |trunk|.
144     self.assertEquals(
145         ChannelInfo('trunk', 'trunk', 'trunk'),
146         self._avail_finder.GetApiAvailability('history'))
147     # Listed as 'dev' on |dev|.
148     self.assertEquals(
149         ChannelInfo('dev', CANNED_BRANCHES[28], 28),
150         self._avail_finder.GetApiAvailability('storage'))
151     # Stable in _manifest_features and into pre-18 versions.
152     self.assertEquals(
153         ChannelInfo('stable', CANNED_BRANCHES[8], 8),
154         self._avail_finder.GetApiAvailability('pageAction'))
155
156     # Testing API channel existence for _permission_features.json.
157     # Listed as 'beta' on |trunk|.
158     self.assertEquals(
159         ChannelInfo('trunk', 'trunk', 'trunk'),
160         self._avail_finder.GetApiAvailability('falseBetaAPI'))
161     # Listed as 'trunk' on |trunk|.
162     self.assertEquals(
163         ChannelInfo('trunk', 'trunk', 'trunk'),
164         self._avail_finder.GetApiAvailability('trunkAPI'))
165     # Listed as 'trunk' on all development channels.
166     self.assertEquals(
167         ChannelInfo('trunk', 'trunk', 'trunk'),
168         self._avail_finder.GetApiAvailability('declarativeContent'))
169     # Listed as 'dev' on all development channels.
170     self.assertEquals(
171         ChannelInfo('dev', CANNED_BRANCHES[28], 28),
172         self._avail_finder.GetApiAvailability('bluetooth'))
173     # Listed as 'dev' on |dev|.
174     self.assertEquals(
175         ChannelInfo('dev', CANNED_BRANCHES[28], 28),
176         self._avail_finder.GetApiAvailability('cookies'))
177     # Treated as 'stable' APIs.
178     self.assertEquals(
179         ChannelInfo('stable', CANNED_BRANCHES[24], 24),
180         self._avail_finder.GetApiAvailability('alarms'))
181     self.assertEquals(
182         ChannelInfo('stable', CANNED_BRANCHES[21], 21),
183         self._avail_finder.GetApiAvailability('bookmarks'))
184
185     # Testing older API existence using extension_api.json.
186     self.assertEquals(
187         ChannelInfo('stable', CANNED_BRANCHES[6], 6),
188         self._avail_finder.GetApiAvailability('menus'))
189     self.assertEquals(
190         ChannelInfo('stable', CANNED_BRANCHES[5], 5),
191         self._avail_finder.GetApiAvailability('idle'))
192
193     # Switches between _features.json files across branches.
194     # Listed as 'trunk' on all channels, in _api, _permission, or _manifest.
195     self.assertEquals(
196         ChannelInfo('trunk', 'trunk', 'trunk'),
197         self._avail_finder.GetApiAvailability('contextMenus'))
198     # Moves between _permission and _manifest as file system is traversed.
199     self.assertEquals(
200         ChannelInfo('stable', CANNED_BRANCHES[23], 23),
201         self._avail_finder.GetApiAvailability('systemInfo.display'))
202     self.assertEquals(
203         ChannelInfo('stable', CANNED_BRANCHES[17], 17),
204         self._avail_finder.GetApiAvailability('webRequest'))
205
206     # Mid-upgrade cases:
207     # Listed as 'dev' on |beta| and 'beta' on |dev|.
208     self.assertEquals(
209         ChannelInfo('dev', CANNED_BRANCHES[28], 28),
210         self._avail_finder.GetApiAvailability('notifications'))
211     # Listed as 'beta' on |stable|, 'dev' on |beta| ... until |stable| on trunk.
212     self.assertEquals(
213         ChannelInfo('trunk', 'trunk', 'trunk'),
214         self._avail_finder.GetApiAvailability('events'))
215
216   def testGetApiNodeAvailability(self):
217     # Allow the LookupResult constructions below to take just one line.
218     lookup_result = api_schema_graph.LookupResult
219     availability_graph = self._node_avail_finder.GetApiNodeAvailability('tabs')
220
221     self.assertEquals(
222         lookup_result(True, self._branch_utility.GetChannelInfo('trunk')),
223         availability_graph.Lookup('tabs', 'properties',
224                                   'fakeTabsProperty3'))
225     self.assertEquals(
226         lookup_result(True, self._branch_utility.GetChannelInfo('dev')),
227         availability_graph.Lookup('tabs', 'events', 'onActivated',
228                                   'parameters', 'activeInfo', 'properties',
229                                   'windowId'))
230     self.assertEquals(
231         lookup_result(True, self._branch_utility.GetChannelInfo('dev')),
232         availability_graph.Lookup('tabs', 'events', 'onUpdated', 'parameters',
233                                   'tab'))
234     self.assertEquals(
235         lookup_result(True, self._branch_utility.GetChannelInfo('beta')),
236         availability_graph.Lookup('tabs', 'events','onActivated'))
237     self.assertEquals(
238         lookup_result(True, self._branch_utility.GetChannelInfo('beta')),
239         availability_graph.Lookup('tabs', 'functions', 'get', 'parameters',
240                                   'tabId'))
241     self.assertEquals(
242         lookup_result(True, self._branch_utility.GetChannelInfo('stable')),
243         availability_graph.Lookup('tabs', 'types', 'InjectDetails',
244                                   'properties', 'code'))
245     self.assertEquals(
246         lookup_result(True, self._branch_utility.GetChannelInfo('stable')),
247         availability_graph.Lookup('tabs', 'types', 'InjectDetails',
248                                   'properties', 'file'))
249     self.assertEquals(
250         lookup_result(True, self._branch_utility.GetStableChannelInfo(25)),
251         availability_graph.Lookup('tabs', 'types', 'InjectDetails'))
252
253     # Nothing new in version 24 or 23.
254
255     self.assertEquals(
256         lookup_result(True, self._branch_utility.GetStableChannelInfo(22)),
257         availability_graph.Lookup('tabs', 'types', 'Tab', 'properties',
258                                   'windowId'))
259     self.assertEquals(
260         lookup_result(True, self._branch_utility.GetStableChannelInfo(21)),
261         availability_graph.Lookup('tabs', 'types', 'Tab', 'properties',
262                                   'selected'))
263
264     # Nothing new in version 20.
265
266     self.assertEquals(
267         lookup_result(True, self._branch_utility.GetStableChannelInfo(19)),
268         availability_graph.Lookup('tabs', 'functions', 'getCurrent'))
269     self.assertEquals(
270         lookup_result(True, self._branch_utility.GetStableChannelInfo(18)),
271         availability_graph.Lookup('tabs', 'types', 'Tab', 'properties',
272                                   'index'))
273     self.assertEquals(
274         lookup_result(True, self._branch_utility.GetStableChannelInfo(17)),
275         availability_graph.Lookup('tabs', 'events', 'onUpdated', 'parameters',
276                                   'changeInfo'))
277
278     # Nothing new in version 16.
279
280     self.assertEquals(
281         lookup_result(True, self._branch_utility.GetStableChannelInfo(15)),
282         availability_graph.Lookup('tabs', 'properties',
283                                   'fakeTabsProperty2'))
284
285     # Everything else is available at the API's release, version 14 here.
286     self.assertEquals(
287         lookup_result(True, self._branch_utility.GetStableChannelInfo(14)),
288         availability_graph.Lookup('tabs', 'types', 'Tab'))
289     self.assertEquals(
290         lookup_result(True, self._branch_utility.GetStableChannelInfo(14)),
291         availability_graph.Lookup('tabs', 'types', 'Tab',
292                                   'properties', 'url'))
293     self.assertEquals(
294         lookup_result(True, self._branch_utility.GetStableChannelInfo(14)),
295         availability_graph.Lookup('tabs', 'properties',
296                                   'fakeTabsProperty1'))
297     self.assertEquals(
298         lookup_result(True, self._branch_utility.GetStableChannelInfo(14)),
299         availability_graph.Lookup('tabs', 'functions', 'get', 'parameters',
300                                   'callback'))
301     self.assertEquals(
302         lookup_result(True, self._branch_utility.GetStableChannelInfo(14)),
303         availability_graph.Lookup('tabs', 'events', 'onUpdated'))
304
305     # Test things that aren't available.
306     self.assertEqual(lookup_result(False, None),
307                      availability_graph.Lookup('tabs', 'types',
308                                                'UpdateInfo'))
309     self.assertEqual(lookup_result(False, None),
310                      availability_graph.Lookup('tabs', 'functions', 'get',
311                                                'parameters', 'callback',
312                                                'parameters', 'tab', 'id'))
313     self.assertEqual(lookup_result(False, None),
314                      availability_graph.Lookup('functions'))
315     self.assertEqual(lookup_result(False, None),
316                      availability_graph.Lookup('events', 'onActivated',
317                                                'parameters', 'activeInfo',
318                                                'tabId'))
319
320
321 if __name__ == '__main__':
322   unittest.main()