Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / tools / json_schema_compiler / idl_schema_test.py
1 #!/usr/bin/env python
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.
5
6 import idl_schema
7 import unittest
8
9 def getFunction(schema, name):
10   for item in schema['functions']:
11     if item['name'] == name:
12       return item
13   raise KeyError('Missing function %s' % name)
14
15
16 def getParams(schema, name):
17   function = getFunction(schema, name)
18   return function['parameters']
19
20
21 def getReturns(schema, name):
22   function = getFunction(schema, name)
23   return function['returns']
24
25
26 def getType(schema, id):
27   for item in schema['types']:
28     if item['id'] == id:
29       return item
30
31
32 class IdlSchemaTest(unittest.TestCase):
33   def setUp(self):
34     loaded = idl_schema.Load('test/idl_basics.idl')
35     self.assertEquals(1, len(loaded))
36     self.assertEquals('idl_basics', loaded[0]['namespace'])
37     self.idl_basics = loaded[0]
38
39   def testSimpleCallbacks(self):
40     schema = self.idl_basics
41     expected = [{'type':'function', 'name':'cb', 'parameters':[]}]
42     self.assertEquals(expected, getParams(schema, 'function4'))
43
44     expected = [{'type':'function', 'name':'cb',
45                  'parameters':[{'name':'x', 'type':'integer'}]}]
46     self.assertEquals(expected, getParams(schema, 'function5'))
47
48     expected = [{'type':'function', 'name':'cb',
49                  'parameters':[{'name':'arg', '$ref':'MyType1'}]}]
50     self.assertEquals(expected, getParams(schema, 'function6'))
51
52   def testCallbackWithArrayArgument(self):
53     schema = self.idl_basics
54     expected = [{'type':'function', 'name':'cb',
55                  'parameters':[{'name':'arg', 'type':'array',
56                                 'items':{'$ref':'MyType2'}}]}]
57     self.assertEquals(expected, getParams(schema, 'function12'))
58
59
60   def testArrayOfCallbacks(self):
61     schema = idl_schema.Load('test/idl_callback_arrays.idl')[0]
62     expected = [{'type':'array', 'name':'callbacks',
63                  'items':{'type':'function', 'name':'MyCallback',
64                           'parameters':[{'type':'integer', 'name':'x'}]}}]
65     self.assertEquals(expected, getParams(schema, 'whatever'))
66
67   def testLegalValues(self):
68     self.assertEquals({
69         'x': {'name': 'x', 'type': 'integer', 'enum': [1,2],
70               'description': 'This comment tests "double-quotes".'},
71         'y': {'name': 'y', 'type': 'string'},
72         'z': {'name': 'z', 'type': 'string'},
73         'a': {'name': 'a', 'type': 'string'},
74         'b': {'name': 'b', 'type': 'string'},
75         'c': {'name': 'c', 'type': 'string'}},
76       getType(self.idl_basics, 'MyType1')['properties'])
77
78   def testMemberOrdering(self):
79     self.assertEquals(
80         ['x', 'y', 'z', 'a', 'b', 'c'],
81         getType(self.idl_basics, 'MyType1')['properties'].keys())
82
83   def testEnum(self):
84     schema = self.idl_basics
85     expected = {'enum': [{'name': 'name1', 'description': 'comment1'},
86                          {'name': 'name2'}],
87                 'description': 'Enum description',
88                 'type': 'string', 'id': 'EnumType'}
89     self.assertEquals(expected, getType(schema, expected['id']))
90
91     expected = [{'name':'type', '$ref':'EnumType'},
92                 {'type':'function', 'name':'cb',
93                   'parameters':[{'name':'type', '$ref':'EnumType'}]}]
94     self.assertEquals(expected, getParams(schema, 'function13'))
95
96     expected = [{'items': {'$ref': 'EnumType'}, 'name': 'types',
97                  'type': 'array'}]
98     self.assertEquals(expected, getParams(schema, 'function14'))
99
100   def testNoCompile(self):
101     schema = self.idl_basics
102     func = getFunction(schema, 'function15')
103     self.assertTrue(func is not None)
104     self.assertTrue(func['nocompile'])
105
106   def testNoDocOnEnum(self):
107     schema = self.idl_basics
108     enum_with_nodoc = getType(schema, 'EnumTypeWithNoDoc')
109     self.assertTrue(enum_with_nodoc is not None)
110     self.assertTrue(enum_with_nodoc['nodoc'])
111
112   def testInternalNamespace(self):
113     idl_basics  = self.idl_basics
114     self.assertEquals('idl_basics', idl_basics['namespace'])
115     self.assertTrue(idl_basics['internal'])
116     self.assertFalse(idl_basics['nodoc'])
117
118   def testReturnTypes(self):
119     schema = self.idl_basics
120     self.assertEquals({'name': 'function19', 'type': 'integer'},
121                       getReturns(schema, 'function19'))
122     self.assertEquals({'name': 'function20', '$ref': 'MyType1',
123                        'optional': True},
124                       getReturns(schema, 'function20'))
125     self.assertEquals({'name': 'function21', 'type': 'array',
126                        'items': {'$ref': 'MyType1'}},
127                       getReturns(schema, 'function21'))
128     self.assertEquals({'name': 'function22', '$ref': 'EnumType',
129                        'optional': True},
130                       getReturns(schema, 'function22'))
131     self.assertEquals({'name': 'function23', 'type': 'array',
132                        'items': {'$ref': 'EnumType'}},
133                       getReturns(schema, 'function23'))
134
135   def testChromeOSPlatformsNamespace(self):
136     schema = idl_schema.Load('test/idl_namespace_chromeos.idl')[0]
137     self.assertEquals('idl_namespace_chromeos', schema['namespace'])
138     expected = ['chromeos']
139     self.assertEquals(expected, schema['platforms'])
140
141   def testAllPlatformsNamespace(self):
142     schema = idl_schema.Load('test/idl_namespace_all_platforms.idl')[0]
143     self.assertEquals('idl_namespace_all_platforms', schema['namespace'])
144     expected = ['chromeos', 'chromeos_touch', 'linux', 'mac', 'win']
145     self.assertEquals(expected, schema['platforms'])
146
147   def testNonSpecificPlatformsNamespace(self):
148     schema = idl_schema.Load('test/idl_namespace_non_specific_platforms.idl')[0]
149     self.assertEquals('idl_namespace_non_specific_platforms',
150                       schema['namespace'])
151     expected = None
152     self.assertEquals(expected, schema['platforms'])
153
154   def testSpecificImplementNamespace(self):
155     schema = idl_schema.Load('test/idl_namespace_specific_implement.idl')[0]
156     self.assertEquals('idl_namespace_specific_implement',
157                       schema['namespace'])
158     expected = 'idl_namespace_specific_implement.idl'
159     self.assertEquals(expected, schema['compiler_options']['implemented_in'])
160
161   def testSpecificImplementOnChromeOSNamespace(self):
162     schema = idl_schema.Load(
163         'test/idl_namespace_specific_implement_chromeos.idl')[0]
164     self.assertEquals('idl_namespace_specific_implement_chromeos',
165                       schema['namespace'])
166     expected_implemented_path = 'idl_namespace_specific_implement_chromeos.idl'
167     expected_platform = ['chromeos']
168     self.assertEquals(expected_implemented_path,
169                       schema['compiler_options']['implemented_in'])
170     self.assertEquals(expected_platform, schema['platforms'])
171
172   def testCallbackComment(self):
173     schema = self.idl_basics
174     self.assertEquals('A comment on a callback.',
175                       getParams(schema, 'function16')[0]['description'])
176     self.assertEquals(
177         'A parameter.',
178         getParams(schema, 'function16')[0]['parameters'][0]['description'])
179     self.assertEquals(
180         'Just a parameter comment, with no comment on the callback.',
181         getParams(schema, 'function17')[0]['parameters'][0]['description'])
182     self.assertEquals(
183         'Override callback comment.',
184         getParams(schema, 'function18')[0]['description'])
185
186   def testFunctionComment(self):
187     schema = self.idl_basics
188     func = getFunction(schema, 'function3')
189     self.assertEquals(('This comment should appear in the documentation, '
190                        'despite occupying multiple lines.'),
191                       func['description'])
192     self.assertEquals(
193         [{'description': ('So should this comment about the argument. '
194                           '<em>HTML</em> is fine too.'),
195           'name': 'arg',
196           '$ref': 'MyType1'}],
197         func['parameters'])
198     func = getFunction(schema, 'function4')
199     self.assertEquals(('This tests if "double-quotes" are escaped correctly.'
200                        '<br/><br/> It also tests a comment with two newlines.'),
201                       func['description'])
202
203   def testReservedWords(self):
204     schema = idl_schema.Load('test/idl_reserved_words.idl')[0]
205
206     foo_type = getType(schema, 'Foo')
207     self.assertEquals([{'name': 'float'}, {'name': 'DOMString'}],
208                       foo_type['enum'])
209
210     enum_type = getType(schema, 'enum')
211     self.assertEquals([{'name': 'callback'}, {'name': 'namespace'}],
212                       enum_type['enum'])
213
214     dictionary = getType(schema, 'dictionary')
215     self.assertEquals('integer', dictionary['properties']['long']['type'])
216
217     mytype = getType(schema, 'MyType')
218     self.assertEquals('string', mytype['properties']['interface']['type'])
219
220     params = getParams(schema, 'static')
221     self.assertEquals('Foo', params[0]['$ref'])
222     self.assertEquals('enum', params[1]['$ref'])
223
224
225 if __name__ == '__main__':
226   unittest.main()