Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / api_schema_graph_test.py
blobe6d65af4c81c4218272ccd19b1c536701d5e6682
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.
6 import unittest
8 from api_schema_graph import APISchemaGraph, LookupResult
11 API_SCHEMA = [{
12 'namespace': 'tabs',
13 'properties': {
14 'lowercase': {
15 'properties': {
16 'one': { 'value': 1 },
17 'two': { 'description': 'just as bad as one' }
20 'TAB_PROPERTY_ONE': { 'value': 'magic' },
21 'TAB_PROPERTY_TWO': {}
23 'types': [
25 'id': 'Tab',
26 'properties': {
27 'id': {},
28 'url': {}
32 'functions': [
34 'name': 'get',
35 'parameters': [ { 'name': 'tab',
36 'type': 'object',
37 'description': 'gets stuff, never complains'
39 { 'name': 'tabId' }
43 'events': [
45 'name': 'onActivated',
46 'parameters': [ {'name': 'activeInfo'} ]
49 'name': 'onUpdated',
50 'parameters': [ {'name': 'updateInfo'} ]
56 class APISchemaGraphTest(unittest.TestCase):
58 def testLookup(self):
59 self._testAPISchema(APISchemaGraph(API_SCHEMA))
61 def testIsEmpty(self):
62 # A few assertions to make sure that Lookup works on empty sets.
63 empty_graph = APISchemaGraph({})
64 self.assertTrue(empty_graph.IsEmpty())
65 self.assertEqual(LookupResult(False, None),
66 empty_graph.Lookup('tabs', 'properties',
67 'TAB_PROPERTY_ONE'))
68 self.assertEqual(LookupResult(False, None),
69 empty_graph.Lookup('tabs', 'functions', 'get',
70 'parameters', 'tab'))
71 self.assertEqual(LookupResult(False, None),
72 empty_graph.Lookup('tabs', 'functions', 'get',
73 'parameters', 'tabId'))
74 self.assertEqual(LookupResult(False, None),
75 empty_graph.Lookup('tabs', 'events', 'onActivated',
76 'parameters', 'activeInfo'))
77 self.assertEqual(LookupResult(False, None),
78 empty_graph.Lookup('tabs', 'events', 'onUpdated',
79 'parameters', 'updateInfo'))
81 def testSubtractEmpty(self):
82 self._testAPISchema(
83 APISchemaGraph(API_SCHEMA).Subtract(APISchemaGraph({})))
85 def _testAPISchema(self, api_schema_graph):
86 self.assertEqual(LookupResult(True, None),
87 api_schema_graph.Lookup('tabs', 'properties',
88 'TAB_PROPERTY_ONE'))
89 self.assertEqual(LookupResult(True, None),
90 api_schema_graph.Lookup('tabs', 'types', 'Tab'))
91 self.assertEqual(LookupResult(True, None),
92 api_schema_graph.Lookup('tabs', 'functions', 'get',
93 'parameters', 'tab'))
94 self.assertEqual(LookupResult(True, None),
95 api_schema_graph.Lookup('tabs', 'functions', 'get',
96 'parameters', 'tabId'))
97 self.assertEqual(LookupResult(True, None),
98 api_schema_graph.Lookup('tabs', 'functions', 'get',
99 'parameters', 'tab', 'type'))
100 self.assertEqual(LookupResult(True, None),
101 api_schema_graph.Lookup('tabs', 'events',
102 'onActivated', 'parameters',
103 'activeInfo'))
104 self.assertEqual(LookupResult(True, None),
105 api_schema_graph.Lookup('tabs', 'events', 'onUpdated',
106 'parameters', 'updateInfo'))
107 self.assertEqual(LookupResult(True, None),
108 api_schema_graph.Lookup('tabs', 'properties',
109 'lowercase', 'properties',
110 'one', 'value'))
111 self.assertEqual(LookupResult(True, None),
112 api_schema_graph.Lookup('tabs', 'properties',
113 'lowercase', 'properties',
114 'two', 'description'))
115 self.assertEqual(LookupResult(False, None),
116 api_schema_graph.Lookup('windows'))
117 self.assertEqual(LookupResult(False, None),
118 api_schema_graph.Lookup('tabs', 'properties',
119 'TAB_PROPERTY_DEUX'))
120 self.assertEqual(LookupResult(False, None),
121 api_schema_graph.Lookup('tabs', 'events', 'onActivated',
122 'parameters', 'callback'))
123 self.assertEqual(LookupResult(False, None),
124 api_schema_graph.Lookup('tabs', 'functions', 'getById',
125 'parameters', 'tab'))
126 self.assertEqual(LookupResult(False, None),
127 api_schema_graph.Lookup('tabs', 'functions', 'get',
128 'parameters', 'type'))
129 self.assertEqual(LookupResult(False, None),
130 api_schema_graph.Lookup('tabs', 'properties',
131 'lowercase', 'properties',
132 'two', 'value'))
134 def testSubtractSelf(self):
135 self.assertTrue(
136 APISchemaGraph(API_SCHEMA).Subtract(APISchemaGraph(API_SCHEMA))
137 .IsEmpty())
140 def testSubtractDisjointSet(self):
141 difference = APISchemaGraph(API_SCHEMA).Subtract(APISchemaGraph({
142 'contextMenus': {
143 'properties': {
144 'CONTEXT_MENU_PROPERTY_ONE': {}
146 'types': {
147 'Menu': {
148 'properties': {
149 'id': {},
150 'width': {}
154 'functions': {
155 'get': {
156 'parameters': {
157 'callback': {}
161 'events': {
162 'onClicked': {
163 'parameters': {
164 'clickInfo': {}
167 'onUpdated': {
168 'parameters': {
169 'updateInfo': {}
175 self.assertEqual(LookupResult(True, None),
176 difference.Lookup('tabs', 'properties',
177 'TAB_PROPERTY_ONE'))
178 self.assertEqual(LookupResult(True, None),
179 difference.Lookup('tabs', 'functions', 'get',
180 'parameters', 'tab'))
181 self.assertEqual(LookupResult(True, None),
182 difference.Lookup('tabs', 'events', 'onUpdated',
183 'parameters', 'updateInfo'))
184 self.assertEqual(LookupResult(True, None),
185 difference.Lookup('tabs', 'functions', 'get',
186 'parameters', 'tabId'))
187 self.assertEqual(LookupResult(False, None),
188 difference.Lookup('contextMenus', 'properties',
189 'CONTEXT_MENU_PROPERTY_ONE'))
190 self.assertEqual(LookupResult(False, None),
191 difference.Lookup('contextMenus', 'types', 'Menu'))
192 self.assertEqual(LookupResult(False, None),
193 difference.Lookup('contextMenus', 'types', 'Menu',
194 'properties', 'id'))
195 self.assertEqual(LookupResult(False, None),
196 difference.Lookup('contextMenus', 'functions'))
197 self.assertEqual(LookupResult(False, None),
198 difference.Lookup('contextMenus', 'events', 'onClicked',
199 'parameters', 'clickInfo'))
200 self.assertEqual(LookupResult(False, None),
201 difference.Lookup('contextMenus', 'events', 'onUpdated',
202 'parameters', 'updateInfo'))
204 def testSubtractSubset(self):
205 difference = APISchemaGraph(API_SCHEMA).Subtract(APISchemaGraph({
206 'tabs': {
207 'properties': {
208 'TAB_PROPERTY_ONE': { 'value': {} }
210 'functions': {
211 'get': {
212 'parameters': {
213 'tab': { 'name': {},
214 'type': {},
215 'description': {}
220 'events': {
221 'onUpdated': {
222 'parameters': {
223 'updateInfo': {
224 'name': {},
225 'properties': {
226 'tabId': {}
234 self.assertEqual(LookupResult(True, None),
235 difference.Lookup('tabs'))
236 self.assertEqual(LookupResult(True, None),
237 difference.Lookup('tabs', 'properties',
238 'TAB_PROPERTY_TWO'))
239 self.assertEqual(LookupResult(True, None),
240 difference.Lookup('tabs', 'properties', 'lowercase',
241 'properties', 'two', 'description'))
242 self.assertEqual(LookupResult(True, None),
243 difference.Lookup('tabs', 'types', 'Tab', 'properties',
244 'url'))
245 self.assertEqual(LookupResult(True, None),
246 difference.Lookup('tabs', 'events', 'onActivated',
247 'parameters', 'activeInfo'))
248 self.assertEqual(LookupResult(False, None),
249 difference.Lookup('tabs', 'events', 'onUpdated',
250 'parameters', 'updateInfo'))
251 self.assertEqual(LookupResult(False, None),
252 difference.Lookup('tabs', 'properties',
253 'TAB_PROPERTY_ONE'))
254 self.assertEqual(LookupResult(False, None),
255 difference.Lookup('tabs', 'properties',
256 'TAB_PROPERTY_ONE', 'value'))
257 self.assertEqual(LookupResult(False, None),
258 difference.Lookup('tabs', 'functions', 'get',
259 'parameters', 'tab'))
261 def testSubtractSuperset(self):
262 difference = APISchemaGraph(API_SCHEMA).Subtract(APISchemaGraph({
263 'tabs': {
264 'namespace': {},
265 'properties': {
266 'lowercase': {
267 'properties': {
268 'one': { 'value': {} },
269 'two': { 'description': {} }
272 'TAB_PROPERTY_ONE': { 'value': {} },
273 'TAB_PROPERTY_TWO': {},
274 'TAB_PROPERTY_THREE': {}
276 'types': {
277 'Tab': {
278 'id': {},
279 'properties': {
280 'id': {},
281 'url': {}
284 'UpdateInfo': {}
286 'functions': {
287 'get': {
288 'name': {},
289 'parameters': {
290 'tab': { 'name': {},
291 'type': {},
292 'description': {}
294 'tabId': { 'name': {} }
297 'getById': {
298 'parameters': {
299 'tabId': {}
303 'events': {
304 'onActivated': {
305 'name': {},
306 'parameters': {
307 'activeInfo': { 'name': {} }
310 'onUpdated': {
311 'name': {},
312 'parameters': {
313 'updateInfo': { 'name': {} }
316 'onClicked': {
317 'name': {},
318 'parameters': {
319 'clickInfo': {}
325 self.assertEqual(LookupResult(False, None),
326 difference.Lookup('tabs'))
327 self.assertEqual(LookupResult(False, None),
328 difference.Lookup('tabs', 'properties',
329 'TAB_PROPERTY_TWO'))
330 self.assertEqual(LookupResult(False, None),
331 difference.Lookup('tabs', 'properties'))
332 self.assertEqual(LookupResult(False, None),
333 difference.Lookup('tabs', 'types', 'Tab', 'properties',
334 'url'))
335 self.assertEqual(LookupResult(False, None),
336 difference.Lookup('tabs', 'types', 'Tab', 'properties',
337 'id'))
338 self.assertEqual(LookupResult(False, None),
339 difference.Lookup('tabs', 'events', 'onUpdated',
340 'parameters', 'updateInfo'))
341 self.assertEqual(LookupResult(False, None),
342 difference.Lookup('tabs', 'properties',
343 'TAB_PROPERTY_ONE'))
344 self.assertEqual(LookupResult(False, None),
345 difference.Lookup('tabs', 'functions', 'get',
346 'parameters', 'tabId'))
347 self.assertEqual(LookupResult(False, None),
348 difference.Lookup('events', 'onUpdated', 'parameters',
349 'updateInfo'))
351 def testUpdate(self):
352 result = APISchemaGraph(API_SCHEMA)
353 to_add = APISchemaGraph({
354 'tabs': {
355 'properties': {
356 'TAB_PROPERTY_THREE': { 'description': 'better than two' },
357 'TAB_PROPERTY_FOUR': { 'value': 4 }
359 'functions': {
360 'get': {
361 'name': {},
362 'parameters': {
363 'tab': {
364 'type': {},
365 'name': {},
366 'description': {},
367 'surprise': {}
371 'getAllInWindow': {
372 'parameters': {
373 'windowId': { 'type': 'object' }
379 result.Update(to_add, lambda _: 'first')
380 # Looking up elements that were originally available in |result|. Because
381 # of this, no |annotation| object should be attached to the LookupResult
382 # object.
383 self.assertEqual(LookupResult(True, None),
384 result.Lookup('tabs'))
385 self.assertEqual(LookupResult(True, None),
386 result.Lookup('tabs', 'functions', 'get',
387 'parameters'))
388 self.assertEqual(LookupResult(True, None),
389 result.Lookup('tabs', 'properties',
390 'TAB_PROPERTY_ONE'))
391 self.assertEqual(LookupResult(True, None),
392 result.Lookup('tabs', 'properties',
393 'TAB_PROPERTY_ONE'))
394 self.assertEqual(LookupResult(True, None),
395 result.Lookup('tabs', 'functions', 'get',
396 'parameters', 'tabId'))
398 # Looking up elements that were just added to |result|.
399 self.assertEqual(LookupResult(True, 'first'),
400 result.Lookup('tabs', 'properties',
401 'TAB_PROPERTY_THREE'))
402 self.assertEqual(LookupResult(True, 'first'),
403 result.Lookup('tabs', 'properties',
404 'TAB_PROPERTY_FOUR'))
405 self.assertEqual(LookupResult(True, 'first'),
406 result.Lookup('tabs', 'functions', 'getAllInWindow',
407 'parameters', 'windowId'))
408 self.assertEqual(LookupResult(True, 'first'),
409 result.Lookup('tabs', 'functions', 'get', 'parameters',
410 'tab', 'surprise'))
412 to_add = APISchemaGraph({
413 'tabs': {
414 'properties': {
415 'TAB_PROPERTY_FIVE': { 'description': 'stayin\' alive' }
417 'functions': {
418 'getAllInWindow': {
419 'parameters': {
420 'callback': { 'type': 'function' }
426 result.Update(to_add, lambda _: 'second')
427 # Looking up the second group of elements added to |result|.
428 self.assertEqual(LookupResult(True, 'first'),
429 result.Lookup('tabs', 'properties',
430 'TAB_PROPERTY_FOUR'))
431 self.assertEqual(LookupResult(True, 'second'),
432 result.Lookup('tabs', 'properties',
433 'TAB_PROPERTY_FIVE'))
434 self.assertEqual(LookupResult(True, 'first'),
435 result.Lookup('tabs', 'functions',
436 'getAllInWindow', 'parameters',
437 'windowId'))
438 self.assertEqual(LookupResult(True, 'second'),
439 result.Lookup('tabs', 'functions',
440 'getAllInWindow', 'parameters',
441 'callback'))
442 self.assertEqual(LookupResult(True, 'first'),
443 result.Lookup('tabs', 'functions',
444 'getAllInWindow'))
447 if __name__ == '__main__':
448 unittest.main()