# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-import json
-import os
-import sys
import unittest
-from api_data_source import (_JSCModel,
- _FormatValue,
- _GetEventByNameFromEvents)
-from availability_finder import AvailabilityInfo
-from branch_utility import ChannelInfo
+from api_data_source import APIDataSource
from extensions_paths import CHROME_EXTENSIONS
-from fake_host_file_system_provider import FakeHostFileSystemProvider
-from features_bundle import FeaturesBundle
-from file_system import FileNotFoundError
-from future import Future
-from object_store_creator import ObjectStoreCreator
+from servlet import Request
from server_instance import ServerInstance
-from test_data.canned_data import (CANNED_API_FILE_SYSTEM_DATA, CANNED_BRANCHES)
from test_data.api_data_source.canned_trunk_fs import CANNED_TRUNK_FS_DATA
from test_file_system import TestFileSystem
-from test_util import Server2Path
-from third_party.json_schema_compiler.memoize import memoize
-
-
-def _MakeLink(href, text):
- return '<a href="%s">%s</a>' % (href, text)
-
-
-def _GetType(dict_, name):
- for type_ in dict_['types']:
- if type_['name'] == name:
- return type_
-
-
-class _FakeAvailabilityFinder(object):
-
- def GetAPIAvailability(self, version):
- return AvailabilityInfo(ChannelInfo('stable', '396', 5))
-
-
-class _FakeScheduledAvailabilityFinder(object):
-
- def GetAPIAvailability(self, version):
- return AvailabilityInfo(ChannelInfo('beta', '1453', 27), scheduled=28)
-
-
-class _FakeTemplateCache(object):
-
- def GetFromFile(self, key):
- return Future(value='handlebar %s' % key)
class APIDataSourceTest(unittest.TestCase):
-
def setUp(self):
- self._base_path = Server2Path('test_data', 'test_json')
-
- server_instance = ServerInstance.ForTest(
+ self.server_instance = ServerInstance.ForTest(
TestFileSystem(CANNED_TRUNK_FS_DATA, relative_to=CHROME_EXTENSIONS))
- file_system = server_instance.host_file_system_provider.GetTrunk()
- self._json_cache = server_instance.compiled_fs_factory.ForJson(file_system)
- self._features_bundle = FeaturesBundle(file_system,
- server_instance.compiled_fs_factory,
- server_instance.object_store_creator)
- self._api_models = server_instance.api_models
-
- # Used for testGetAPIAvailability() so that valid-ish data is processed.
- server_instance = ServerInstance.ForTest(
- file_system_provider=FakeHostFileSystemProvider(
- CANNED_API_FILE_SYSTEM_DATA))
- self._avail_api_models = server_instance.api_models
- self._avail_json_cache = server_instance.compiled_fs_factory.ForJson(
- server_instance.host_file_system_provider.GetTrunk())
- self._avail_finder = server_instance.availability_finder
-
- def _ReadLocalFile(self, filename):
- with open(os.path.join(self._base_path, filename), 'r') as f:
- return f.read()
-
- def _LoadJSON(self, filename):
- return json.loads(self._ReadLocalFile(filename))
-
- def testCreateId(self):
- dict_ = _JSCModel(self._api_models.GetModel('tester').Get(),
- _FakeAvailabilityFinder(),
- self._json_cache,
- _FakeTemplateCache(),
- self._features_bundle,
- None).ToDict()
- self.assertEquals('type-TypeA', dict_['types'][0]['id'])
- self.assertEquals('property-TypeA-b',
- dict_['types'][0]['properties'][0]['id'])
- self.assertEquals('method-get', dict_['functions'][0]['id'])
- self.assertEquals('event-EventA', dict_['events'][0]['id'])
-
- # TODO(kalman): re-enable this when we have a rebase option.
- def DISABLED_testToDict(self):
- expected_json = self._LoadJSON('expected_tester.json')
- dict_ = _JSCModel(self._api_models.GetModel('tester').Get(),
- _FakeAvailabilityFinder(),
- self._json_cache,
- _FakeTemplateCache(),
- self._features_bundle,
- None).ToDict()
- self.assertEquals(expected_json, dict_)
- def testFormatValue(self):
- self.assertEquals('1,234,567', _FormatValue(1234567))
- self.assertEquals('67', _FormatValue(67))
- self.assertEquals('234,567', _FormatValue(234567))
-
- def testGetAPIAvailability(self):
- api_availabilities = {
- 'bluetooth': AvailabilityInfo(
- ChannelInfo('dev', CANNED_BRANCHES[28], 28)),
- 'contextMenus': AvailabilityInfo(
- ChannelInfo('trunk', CANNED_BRANCHES['trunk'], 'trunk')),
- 'jsonStableAPI': AvailabilityInfo(
- ChannelInfo('stable', CANNED_BRANCHES[20], 20)),
- 'idle': AvailabilityInfo(
- ChannelInfo('stable', CANNED_BRANCHES[5], 5)),
- 'input.ime': AvailabilityInfo(
- ChannelInfo('stable', CANNED_BRANCHES[18], 18)),
- 'tabs': AvailabilityInfo(
- ChannelInfo('stable', CANNED_BRANCHES[18], 18))
- }
- for api_name, availability in api_availabilities.iteritems():
- model = _JSCModel(self._avail_api_models.GetModel(api_name).Get(),
- self._avail_finder,
- self._avail_json_cache,
- _FakeTemplateCache(),
- self._features_bundle,
- None)
- self.assertEquals(availability, model._GetAPIAvailability())
-
- def testGetIntroList(self):
- model = _JSCModel(self._api_models.GetModel('tester').Get(),
- _FakeAvailabilityFinder(),
- self._json_cache,
- _FakeTemplateCache(),
- self._features_bundle,
- None)
- expected_list = [
- { 'title': 'Description',
- 'content': [
- { 'text': 'a test api' }
- ]
- },
- { 'title': 'Availability',
- 'content': [
- { 'partial': 'handlebar chrome/common/extensions/docs/' +
- 'templates/private/intro_tables/stable_message.html',
- 'version': 5,
- 'scheduled': None
- }
- ]
- },
- { 'title': 'Permissions',
- 'content': [
- { 'class': 'override',
- 'text': '"tester"'
+ def testGet(self):
+ api_ds = APIDataSource(self.server_instance, Request.ForTest('/'))
+ jsc_view = api_ds.get('extensions').get('tester')
+ funcs_arr = [{
+ 'availability': None,
+ 'callback': {
+ 'name': 'callback',
+ 'optional': False,
+ 'parameters': [{
+ 'array': {
+ 'availability': None,
+ 'description': None,
+ 'events': [],
+ 'functions': [],
+ 'id': 'type-results-resultsType',
+ 'is_object': False,
+ 'link': {
+ 'name': 'TypeA',
+ 'ref': 'tester.TypeA',
+ 'text': 'TypeA'
+ },
+ 'name': 'resultsType',
+ 'properties': []
},
- { 'text': 'is an API for testing things.' }
- ]
+ 'availability': None,
+ 'description': None,
+ 'functions': [],
+ 'id': 'property-callback-results',
+ 'is_object': False,
+ 'last': True,
+ 'name': 'results',
+ 'optional': None,
+ 'parameters': [],
+ 'parentName': 'callback',
+ 'properties': [],
+ 'returns': None
+ }],
+ 'simple_type': {
+ 'simple_type': 'function'
+ }
},
- { 'title': 'Manifest',
- 'content': [
- { 'class': 'code',
- 'text': '"tester": {...}'
- }
- ]
+ 'description': 'Gets stuff.',
+ 'id': 'method-get',
+ 'name': 'get',
+ 'parameters': [{
+ 'availability': None,
+ 'choices': [{
+ 'availability': None,
+ 'description': None,
+ 'events': [],
+ 'functions': [],
+ 'id': 'type-a-string',
+ 'is_object': False,
+ 'name': 'string',
+ 'properties': [],
+ 'simple_type': 'string'
+ },
+ {
+ 'array': {
+ 'availability': None,
+ 'description': None,
+ 'events': [],
+ 'functions': [],
+ 'id': 'type-strings-stringsType',
+ 'is_object': False,
+ 'name': 'stringsType',
+ 'properties': [],
+ 'simple_type': 'string'
+ },
+ 'availability': None,
+ 'description': None,
+ 'events': [],
+ 'functions': [],
+ 'id': 'type-a-strings',
+ 'is_object': False,
+ 'last': True,
+ 'name': 'strings',
+ 'properties': []
+ }],
+ 'description': 'a param',
+ 'functions': [],
+ 'id': 'property-get-a',
+ 'is_object': False,
+ 'name': 'a',
+ 'optional': None,
+ 'parameters': [],
+ 'parentName': 'get',
+ 'properties': [],
+ 'returns': None
},
- { 'title': 'Learn More',
- 'content': [
- { 'link': 'https://tester.test.com/welcome.html',
- 'text': 'Welcome!'
+ {
+ 'asFunction': {
+ 'name': 'callback',
+ 'optional': False,
+ 'parameters': [{
+ 'array': {
+ 'availability': None,
+ 'description': None,
+ 'events': [],
+ 'functions': [],
+ 'id': 'type-results-resultsType',
+ 'is_object': False,
+ 'link': {
+ 'name': 'TypeA',
+ 'ref': 'tester.TypeA',
+ 'text': 'TypeA'
+ },
+ 'name': 'resultsType',
+ 'properties': []
+ },
+ 'availability': None,
+ 'description': None,
+ 'functions': [],
+ 'id': 'property-callback-results',
+ 'is_object': False,
+ 'last': True,
+ 'name': 'results',
+ 'optional': None,
+ 'parameters': [],
+ 'parentName': 'callback',
+ 'properties': [],
+ 'returns': None
+ }],
+ 'simple_type': {
+ 'simple_type': 'function'
}
- ]
- }
- ]
- self.assertEquals(model._GetIntroTableList(), expected_list)
-
- # Tests the same data with a scheduled availability.
- model = _JSCModel(self._api_models.GetModel('tester').Get(),
- _FakeScheduledAvailabilityFinder(),
- self._json_cache,
- _FakeTemplateCache(),
- self._features_bundle,
- None)
- expected_list[1] = {
- 'title': 'Availability',
- 'content': [
- { 'partial': 'handlebar chrome/common/extensions/docs/' +
- 'templates/private/intro_tables/beta_message.html',
- 'version': 27,
- 'scheduled': 28
- }
- ]
- }
- self.assertEquals(model._GetIntroTableList(), expected_list)
-
- def testGetEventByNameFromEvents(self):
- events = {}
- # Missing 'types' completely.
- self.assertRaises(AssertionError, _GetEventByNameFromEvents, events)
-
- events['types'] = []
- # No type 'Event' defined.
- self.assertRaises(AssertionError, _GetEventByNameFromEvents, events)
-
- events['types'].append({ 'name': 'Event',
- 'functions': []})
- add_rules = { "name": "addRules" }
- events['types'][0]['functions'].append(add_rules)
- self.assertEqual(add_rules,
- _GetEventByNameFromEvents(events)['addRules'])
-
- events['types'][0]['functions'].append(add_rules)
- # Duplicates are an error.
- self.assertRaises(AssertionError, _GetEventByNameFromEvents, events)
-
- def _FakeLoadAddRulesSchema(self):
- events = self._LoadJSON('add_rules_def_test.json')
- return _GetEventByNameFromEvents(events)
-
- def testAddRules(self):
- dict_ = _JSCModel(self._api_models.GetModel('add_rules_tester').Get(),
- _FakeAvailabilityFinder(),
- self._json_cache,
- _FakeTemplateCache(),
- self._features_bundle,
- self._FakeLoadAddRulesSchema).ToDict()
-
- # Check that the first event has the addRulesFunction defined.
- self.assertEquals('add_rules_tester', dict_['name'])
- self.assertEquals('rules', dict_['events'][0]['name'])
- self.assertEquals('notable_name_to_check_for',
- dict_['events'][0]['byName']['addRules'][
- 'parameters'][0]['name'])
-
- # Check that the second event has addListener defined.
- self.assertEquals('noRules', dict_['events'][1]['name'])
- self.assertEquals('add_rules_tester', dict_['name'])
- self.assertEquals('noRules', dict_['events'][1]['name'])
- self.assertEquals('callback',
- dict_['events'][0]['byName']['addListener'][
- 'parameters'][0]['name'])
+ },
+ 'description': None,
+ 'id': 'property-get-callback',
+ 'isCallback': True,
+ 'last': True,
+ 'name': 'callback',
+ 'optional': False,
+ 'parentName': 'get',
+ 'simple_type': 'function'
+ }],
+ 'returns': None
+ }]
+ self.assertEquals(funcs_arr, jsc_view['functions'])
+ types_arr = [{
+ 'availability': None,
+ 'description': 'A cool thing.',
+ 'events': [],
+ 'functions': [],
+ 'id': 'type-TypeA',
+ 'is_object': True,
+ 'name': 'TypeA',
+ 'properties': [{
+ 'array': {
+ 'availability': None,
+ 'description': None,
+ 'events': [],
+ 'functions': [],
+ 'id': 'type-b-bType',
+ 'is_object': False,
+ 'link': {
+ 'name': 'TypeA',
+ 'ref': 'tester.TypeA',
+ 'text': 'TypeA'
+ },
+ 'name': 'bType',
+ 'properties': []
+ },
+ 'availability': None,
+ 'description': 'List of TypeA.',
+ 'functions': [],
+ 'id': 'property-TypeA-b',
+ 'is_object': False,
+ 'name': 'b',
+ 'optional': True,
+ 'parameters': [],
+ 'parentName': 'TypeA',
+ 'properties': [],
+ 'returns': None
+ }],
+ 'simple_type': 'object'
+ }]
+ self.assertEquals(types_arr, jsc_view['types'])
if __name__ == '__main__':
unittest.main()