2 # Copyright (c) 2012 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 from future
import Future
10 from reference_resolver
import ReferenceResolver
11 from test_object_store
import TestObjectStore
12 from test_util
import Server2Path
13 from third_party
.json_schema_compiler
.model
import Namespace
63 'baz_p1': {'type': 'any'},
64 'baz_p2': {'type': 'any'},
65 'baz_p3': {'type': 'any'}
69 'namespace': 'bar.bon',
101 'name': 'bar_bon_e1',
105 'name': 'bar_bon_e2',
109 'name': 'bar_bon_e3',
114 'bar_bon_p1': {'type': 'any'},
115 'bar_bon_p2': {'type': 'any'},
116 'bar_bon_p3': {'type': 'any'}
179 'bar_p1': {'type': 'any'},
180 'bar_p2': {'type': 'any'},
181 'bar_p3': {'$ref': 'bar_t1'}
236 'foo_p1': {'$ref': 'foo_t3'},
237 'foo_p2': {'type': 'any'},
238 'foo_p3': {'type': 'any'}
244 class _FakePlatformBundle(object):
246 self
.platforms
= ('apps', 'extensions')
248 def GetAPIModels(self
, platform
):
249 if platform
== 'apps':
250 return _FakeAPIModels(_TEST_DATA
)
251 # Only includes some of the data in the 'extensions' APIModels.
252 # ReferenceResolver will have to look at other platforms to resolve 'foo'.
253 return _FakeAPIModels({
254 'bar': _TEST_DATA
['bar'],
255 'bar.bon': _TEST_DATA
['bar.bon'],
256 'baz': _TEST_DATA
['baz']
260 class _FakeAPIModels(object):
261 def __init__(self
, apis
):
265 return self
._apis
.keys()
267 def GetModel(self
, name
):
268 return Future(value
=Namespace(self
._apis
[name
], 'fake/path.json'))
271 class ReferenceResolverTest(unittest
.TestCase
):
273 self
._base
_path
= Server2Path('test_data', 'test_json')
275 def _ReadLocalFile(self
, filename
):
276 with
open(os
.path
.join(self
._base
_path
, filename
), 'r') as f
:
279 def testGetLink(self
):
280 apps_resolver
= ReferenceResolver(
281 _FakePlatformBundle().GetAPIModels('apps'),
282 TestObjectStore('apps/test'))
283 extensions_resolver
= ReferenceResolver(
284 _FakePlatformBundle().GetAPIModels('extensions'),
285 TestObjectStore('extensions/test'))
291 }, apps_resolver
.GetLink('foo', namespace
='baz'))
293 'href': 'foo#type-foo_t1',
294 'text': 'foo.foo_t1',
296 }, apps_resolver
.GetLink('foo.foo_t1', namespace
='baz'))
298 'href': 'baz#event-baz_e1',
301 }, apps_resolver
.GetLink('baz.baz_e1', namespace
='baz'))
303 'href': 'baz#event-baz_e1',
306 }, apps_resolver
.GetLink('baz_e1', namespace
='baz'))
308 'href': 'foo#method-foo_f1',
309 'text': 'foo.foo_f1',
311 }, apps_resolver
.GetLink('foo.foo_f1', namespace
='baz'))
313 'href': 'foo#property-foo_p3',
314 'text': 'foo.foo_p3',
316 }, apps_resolver
.GetLink('foo.foo_p3', namespace
='baz'))
318 'href': 'bar.bon#type-bar_bon_t3',
319 'text': 'bar.bon.bar_bon_t3',
321 }, apps_resolver
.GetLink('bar.bon.bar_bon_t3', namespace
='baz'))
323 'href': 'bar.bon#property-bar_bon_p3',
324 'text': 'bar_bon_p3',
326 }, apps_resolver
.GetLink('bar_bon_p3', namespace
='bar.bon'))
328 'href': 'bar.bon#property-bar_bon_p3',
329 'text': 'bar_bon_p3',
331 }, apps_resolver
.GetLink('bar.bon.bar_bon_p3', namespace
='bar.bon'))
333 'href': 'bar#event-bar_e2',
336 }, apps_resolver
.GetLink('bar.bar_e2', namespace
='bar'))
338 'href': 'bar#type-bon',
341 }, apps_resolver
.GetLink('bar.bon', namespace
='bar'))
343 'href': 'foo#event-foo_t3-foo_t3_e1',
344 'text': 'foo_t3.foo_t3_e1',
346 }, apps_resolver
.GetLink('foo_t3.foo_t3_e1', namespace
='foo'))
348 'href': 'foo#event-foo_t3-foo_t3_e1',
349 'text': 'foo_t3.foo_t3_e1',
351 }, apps_resolver
.GetLink('foo.foo_t3.foo_t3_e1', namespace
='foo'))
353 'href': 'foo#event-foo_t3-foo_t3_e1',
354 'text': 'foo_t3.foo_t3_e1',
356 }, apps_resolver
.GetLink('foo.foo_p1.foo_t3_e1', namespace
='foo'))
358 'href': 'bar#property-bar_t1-bar_t1_p1',
359 'text': 'bar.bar_t1.bar_t1_p1',
361 }, apps_resolver
.GetLink('bar.bar_p3.bar_t1_p1', namespace
='foo'))
362 # Test extensions_resolver.
364 'href': 'bar#property-bar_t1-bar_t1_p1',
365 'text': 'bar.bar_t1.bar_t1_p1',
367 }, extensions_resolver
.GetLink('bar.bar_p3.bar_t1_p1', namespace
='foo'))
369 'href': 'bar#property-bar_t1-bar_t1_p1',
370 'text': 'bar_t1.bar_t1_p1',
372 }, apps_resolver
.GetLink('bar_p3.bar_t1_p1', namespace
='bar'))
375 apps_resolver
.GetLink('bar.bar_p3.bar_t2_p1', namespace
='bar'))
378 apps_resolver
.GetLink('bar.bon.bar_e3', namespace
='bar'))
381 apps_resolver
.GetLink('bar_p3', namespace
='baz.bon'))
384 apps_resolver
.GetLink('falafel.faf', namespace
='a'))
387 apps_resolver
.GetLink('bar_p3', namespace
='foo'))
388 # Exists in apps but not extensions.
391 extensions_resolver
.GetLink('foo.foo_p3', namespace
='baz'))
393 if __name__
== '__main__':