| OLD | NEW |
| (Empty) |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import unittest | |
| 6 import module | |
| 7 | |
| 8 try: | |
| 9 import mojom_translator | |
| 10 from generated import mojom_files_mojom | |
| 11 from generated import mojom_types_mojom | |
| 12 bindings_imported = True | |
| 13 except ImportError: | |
| 14 bindings_imported = False | |
| 15 | |
| 16 | |
| 17 @unittest.skipUnless(bindings_imported, 'Could not import python bindings.') | |
| 18 class TranslateFileGraph(unittest.TestCase): | |
| 19 | |
| 20 def test_basics(self): | |
| 21 g = mojom_files_mojom.MojomFileGraph() | |
| 22 | |
| 23 # File names need to be set so the file can be translated at all. | |
| 24 g.files = { | |
| 25 'a.mojom': mojom_files_mojom.MojomFile( | |
| 26 file_name='a.mojom', | |
| 27 specified_file_name='', | |
| 28 imports=[]), | |
| 29 'b.mojom': mojom_files_mojom.MojomFile( | |
| 30 file_name='b.mojom', | |
| 31 specified_file_name='', | |
| 32 imports=[]), | |
| 33 'root/c.mojom': mojom_files_mojom.MojomFile( | |
| 34 file_name='root/c.mojom', | |
| 35 specified_file_name='', | |
| 36 imports=[]), | |
| 37 } | |
| 38 | |
| 39 modules = mojom_translator.TranslateFileGraph(g) | |
| 40 self.assertEquals(len(modules), len(g.files)) | |
| 41 | |
| 42 | |
| 43 @unittest.skipUnless(bindings_imported, 'Could not import python bindings.') | |
| 44 class TestTranslateFile(unittest.TestCase): | |
| 45 | |
| 46 def test_basics(self): | |
| 47 graph = mojom_files_mojom.MojomFileGraph( | |
| 48 resolved_types={}) | |
| 49 | |
| 50 file_name = 'root/f.mojom' | |
| 51 imported_file_name = 'other/a.mojom' | |
| 52 second_level_imported_file_name = 'something/other.mojom' | |
| 53 mojom_file = mojom_files_mojom.MojomFile( | |
| 54 file_name=file_name, | |
| 55 specified_file_name='specified_file_name', | |
| 56 module_namespace='somens', | |
| 57 imports=[imported_file_name]) | |
| 58 imported_file = mojom_files_mojom.MojomFile( | |
| 59 file_name=imported_file_name, | |
| 60 specified_file_name='', | |
| 61 module_namespace='somens', | |
| 62 imports=[second_level_imported_file_name]) | |
| 63 second_level_imported_file = mojom_files_mojom.MojomFile( | |
| 64 file_name=second_level_imported_file_name, | |
| 65 specified_file_name='', | |
| 66 module_namespace='somens') | |
| 67 graph.files = { | |
| 68 file_name: mojom_file, | |
| 69 imported_file_name: imported_file, | |
| 70 second_level_imported_file_name: second_level_imported_file | |
| 71 } | |
| 72 | |
| 73 mojom_interface = mojom_types_mojom.MojomInterface( | |
| 74 methods={}, | |
| 75 decl_data=mojom_types_mojom.DeclarationData( | |
| 76 short_name='AnInterface', | |
| 77 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 78 file_name=file_name))) | |
| 79 graph.resolved_types['interface_key'] = mojom_types_mojom.UserDefinedType( | |
| 80 interface_type=mojom_interface) | |
| 81 | |
| 82 mojom_struct = mojom_types_mojom.MojomStruct( | |
| 83 fields=[], | |
| 84 decl_data=mojom_types_mojom.DeclarationData( | |
| 85 short_name='AStruct', | |
| 86 full_identifier='foo.AStruct', | |
| 87 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 88 file_name=file_name))) | |
| 89 add_version_info(mojom_struct, 0) | |
| 90 graph.resolved_types['struct_key'] = mojom_types_mojom.UserDefinedType( | |
| 91 struct_type=mojom_struct) | |
| 92 | |
| 93 mojom_union = mojom_types_mojom.MojomUnion( | |
| 94 fields=[], | |
| 95 decl_data=mojom_types_mojom.DeclarationData( | |
| 96 short_name='AUnion', | |
| 97 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 98 file_name=file_name))) | |
| 99 graph.resolved_types['union_key'] = mojom_types_mojom.UserDefinedType( | |
| 100 union_type=mojom_union) | |
| 101 | |
| 102 mojom_enum = mojom_types_mojom.MojomEnum( | |
| 103 values=[], | |
| 104 decl_data=mojom_types_mojom.DeclarationData( | |
| 105 short_name='AnEnum', | |
| 106 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 107 file_name=file_name))) | |
| 108 graph.resolved_types['enum_key'] = mojom_types_mojom.UserDefinedType( | |
| 109 enum_type=mojom_enum) | |
| 110 | |
| 111 mojom_const = mojom_types_mojom.DeclaredConstant( | |
| 112 decl_data=mojom_types_mojom.DeclarationData(short_name='AConst'), | |
| 113 type=mojom_types_mojom.Type( | |
| 114 simple_type=mojom_types_mojom.SimpleType.INT64), | |
| 115 value=mojom_types_mojom.Value( | |
| 116 literal_value=mojom_types_mojom.LiteralValue( | |
| 117 int64_value=30))) | |
| 118 graph.resolved_constants = {'constant_key': mojom_const} | |
| 119 | |
| 120 mojom_file.declared_mojom_objects = mojom_files_mojom.KeysByType( | |
| 121 interfaces=['interface_key'], | |
| 122 structs=['struct_key'], | |
| 123 unions=['union_key'], | |
| 124 top_level_enums=['enum_key'], | |
| 125 top_level_constants=['constant_key'] | |
| 126 ) | |
| 127 | |
| 128 mod = mojom_translator.FileTranslator(graph, file_name).Translate() | |
| 129 | |
| 130 self.assertEquals('f.mojom', mod.name) | |
| 131 self.assertEquals(mojom_file.specified_file_name, mod.specified_name) | |
| 132 self.assertEquals(mojom_file.file_name, mod.path) | |
| 133 self.assertEquals(mojom_file.module_namespace, mod.namespace) | |
| 134 | |
| 135 self.assertEquals(1, len(mod.imports)) | |
| 136 self.assertEquals('a.mojom', mod.imports[0]['module_name']) | |
| 137 self.assertEquals(imported_file.module_namespace, | |
| 138 mod.imports[0]['namespace']) | |
| 139 self.assertEquals(imported_file.file_name, mod.imports[0]['module'].path) | |
| 140 | |
| 141 self.assertEquals(2, len(mod.transitive_imports)) | |
| 142 transitive_imports_paths = [imp['module'].path | |
| 143 for imp in mod.transitive_imports] | |
| 144 self.assertIn(imported_file_name, transitive_imports_paths) | |
| 145 self.assertIn(second_level_imported_file_name, transitive_imports_paths) | |
| 146 | |
| 147 self.assertEquals('AnInterface', mod.interfaces[0].name) | |
| 148 # Interfaces should be assigned their name as their spec. | |
| 149 self.assertEquals('AnInterface', mod.interfaces[0].spec) | |
| 150 self.assertEquals(mojom_struct.decl_data.short_name, mod.structs[0].name) | |
| 151 # The struct was given a full_identifier so its spec should be that. | |
| 152 self.assertEquals(mojom_struct.decl_data.full_identifier, | |
| 153 mod.structs[0].spec) | |
| 154 self.assertEquals(mojom_union.decl_data.short_name, mod.unions[0].name) | |
| 155 # The union was given a short name but not a full_identifier so its spec | |
| 156 # should be the short name. | |
| 157 self.assertEquals(mojom_union.decl_data.short_name, | |
| 158 mod.unions[0].spec) | |
| 159 self.assertEquals(mojom_enum.decl_data.short_name, mod.enums[0].name) | |
| 160 self.assertEquals(mojom_const.decl_data.short_name, mod.constants[0].name) | |
| 161 | |
| 162 imported_mod = mojom_translator.FileTranslator( | |
| 163 graph, imported_file_name).Translate() | |
| 164 self.assertFalse(imported_mod.specified_name) | |
| 165 | |
| 166 def test_no_imports(self): | |
| 167 graph = mojom_files_mojom.MojomFileGraph( | |
| 168 resolved_types={}) | |
| 169 file_name = 'root/f.mojom' | |
| 170 mojom_file = mojom_files_mojom.MojomFile( | |
| 171 file_name=file_name, | |
| 172 specified_file_name='', | |
| 173 module_namespace='somens') | |
| 174 graph.files = { file_name: mojom_file } | |
| 175 | |
| 176 # Should not throw exceptions despite imports not being set on the file. | |
| 177 mod = mojom_translator.FileTranslator(graph, file_name).Translate() | |
| 178 | |
| 179 self.assertEquals([], mod.imports) | |
| 180 | |
| 181 @unittest.skipUnless(bindings_imported, 'Could not import python bindings.') | |
| 182 class TestUserDefinedFromTypeRef(unittest.TestCase): | |
| 183 | |
| 184 def do_interface_test(self, nullable, interface_request): | |
| 185 # Build a MojomInterface | |
| 186 file_name = 'a.mojom' | |
| 187 mojom_interface = mojom_types_mojom.MojomInterface( | |
| 188 decl_data=mojom_types_mojom.DeclarationData( | |
| 189 short_name='AnInterface', | |
| 190 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 191 file_name=file_name))) | |
| 192 mojom_interface.methods={} | |
| 193 | |
| 194 # Register the MojomInterface in a MojomFileGraph | |
| 195 graph = mojom_files_mojom.MojomFileGraph() | |
| 196 type_key = 'some_type_key' | |
| 197 graph.resolved_types = { | |
| 198 type_key: mojom_types_mojom.UserDefinedType( | |
| 199 interface_type=mojom_interface)} | |
| 200 | |
| 201 # Build a reference to the interface. | |
| 202 type_ref = mojom_types_mojom.Type( | |
| 203 type_reference=mojom_types_mojom.TypeReference( | |
| 204 type_key=type_key, | |
| 205 nullable=nullable, | |
| 206 is_interface_request=interface_request)) | |
| 207 | |
| 208 # Construct a translator | |
| 209 translator = mojom_translator.FileTranslator(graph, file_name) | |
| 210 | |
| 211 # Translate the MojomInterface referenced by type_ref. | |
| 212 interface = translator.UserDefinedFromTypeRef(type_ref) | |
| 213 | |
| 214 # Check the translation | |
| 215 if interface_request: | |
| 216 self.assertEquals('AnInterface', interface.kind.name) | |
| 217 self.assertEquals('some_type_key', interface.kind.type_key) | |
| 218 else: | |
| 219 self.assertEquals('AnInterface', interface.name) | |
| 220 self.assertEquals('some_type_key', interface.type_key) | |
| 221 self.assertEquals(nullable, interface.is_nullable) | |
| 222 self.assertEquals(interface_request, isinstance(interface, | |
| 223 module.InterfaceRequest)) | |
| 224 | |
| 225 def test_interfaces(self): | |
| 226 self.do_interface_test(False, False) | |
| 227 self.do_interface_test(False, True) | |
| 228 self.do_interface_test(True, False) | |
| 229 self.do_interface_test(True, True) | |
| 230 | |
| 231 @unittest.skipUnless(bindings_imported, 'Could not import python bindings.') | |
| 232 class TestUserDefinedTypeFromMojom(unittest.TestCase): | |
| 233 | |
| 234 def test_structs(self): | |
| 235 file_name = 'a.mojom' | |
| 236 graph = mojom_files_mojom.MojomFileGraph() | |
| 237 mojom_file = mojom_files_mojom.MojomFile( | |
| 238 file_name='a.mojom', | |
| 239 module_namespace='foo.bar') | |
| 240 graph.files = {mojom_file.file_name: mojom_file} | |
| 241 | |
| 242 mojom_struct = mojom_types_mojom.MojomStruct( | |
| 243 decl_data=mojom_types_mojom.DeclarationData(short_name='FirstStruct')) | |
| 244 mojom_struct.fields = [ | |
| 245 mojom_types_mojom.StructField( | |
| 246 decl_data=mojom_types_mojom.DeclarationData( | |
| 247 short_name='field03', | |
| 248 declaration_order=2), | |
| 249 type=mojom_types_mojom.Type( | |
| 250 simple_type=mojom_types_mojom.SimpleType.BOOL), | |
| 251 offset=21, | |
| 252 bit=6, | |
| 253 min_version=11), | |
| 254 mojom_types_mojom.StructField( | |
| 255 decl_data=mojom_types_mojom.DeclarationData( | |
| 256 short_name='field01', | |
| 257 declared_ordinal=1, | |
| 258 declaration_order=0), | |
| 259 type=mojom_types_mojom.Type( | |
| 260 simple_type=mojom_types_mojom.SimpleType.BOOL), | |
| 261 offset=17, | |
| 262 bit=1, | |
| 263 min_version=4), | |
| 264 mojom_types_mojom.StructField( | |
| 265 decl_data=mojom_types_mojom.DeclarationData( | |
| 266 short_name='field02', | |
| 267 declaration_order=1), | |
| 268 type=mojom_types_mojom.Type( | |
| 269 simple_type=mojom_types_mojom.SimpleType.DOUBLE), | |
| 270 offset=0, | |
| 271 bit=0, | |
| 272 min_version=0, | |
| 273 default_value=mojom_types_mojom.DefaultFieldValue( | |
| 274 value=mojom_types_mojom.Value( | |
| 275 literal_value=mojom_types_mojom.LiteralValue(double_value=15)))), | |
| 276 ] | |
| 277 mojom_struct.version_info=[ | |
| 278 mojom_types_mojom.StructVersion( | |
| 279 version_number=0, num_bytes=67, num_fields=1), | |
| 280 mojom_types_mojom.StructVersion( | |
| 281 version_number=1, num_bytes=76, num_fields=3), | |
| 282 ] | |
| 283 # mojom_fields_declaration_order lists, in declaration order, the indices | |
| 284 # of the fields in mojom_types_mojom.StructField. | |
| 285 mojom_fields_declaration_order = [1, 2, 0] | |
| 286 mojom_struct.decl_data.source_file_info = mojom_types_mojom.SourceFileInfo( | |
| 287 file_name=mojom_file.file_name) | |
| 288 | |
| 289 struct = module.Struct() | |
| 290 translator = mojom_translator.FileTranslator(graph, file_name) | |
| 291 translator.StructFromMojom( | |
| 292 struct, mojom_types_mojom.UserDefinedType(struct_type=mojom_struct)) | |
| 293 | |
| 294 self.assertEquals('FirstStruct', struct.name) | |
| 295 self.assertEquals(translator._module, struct.module) | |
| 296 | |
| 297 self.assertEquals(len(mojom_struct.fields), len(struct.fields)) | |
| 298 for index, gold_index in enumerate(mojom_fields_declaration_order): | |
| 299 gold = mojom_struct.fields[gold_index] | |
| 300 f = struct.fields[index] | |
| 301 self.assertEquals(f.name, gold.decl_data.short_name) | |
| 302 if gold.decl_data.declared_ordinal >= 0: | |
| 303 self.assertEquals(gold.decl_data.declared_ordinal, f.ordinal) | |
| 304 else: | |
| 305 self.assertEquals(None, f.ordinal) | |
| 306 self.assertEquals(gold_index, f.computed_ordinal) | |
| 307 self.assertEquals(gold.offset, f.computed_offset) | |
| 308 self.assertEquals(gold.bit, f.computed_bit) | |
| 309 self.assertEquals(gold.min_version, f.computed_min_version) | |
| 310 self.assertEquals(struct.fields_in_ordinal_order[index].name, | |
| 311 mojom_struct.fields[index].decl_data.short_name) | |
| 312 | |
| 313 self.assertEquals(2, len(struct.versions)) | |
| 314 for i in xrange(0, 2): | |
| 315 self.assertEquals(mojom_struct.version_info[i].version_number, | |
| 316 struct.versions[i].version) | |
| 317 self.assertEquals(mojom_struct.version_info[i].num_bytes, | |
| 318 struct.versions[i].num_bytes) | |
| 319 self.assertEquals(mojom_struct.version_info[i].num_fields, | |
| 320 struct.versions[i].num_fields) | |
| 321 | |
| 322 self.assertEquals(module.BOOL, struct.fields[0].kind) | |
| 323 self.assertEquals(module.DOUBLE, struct.fields[1].kind) | |
| 324 | |
| 325 self.assertEquals('15.0', struct.fields[1].default) | |
| 326 | |
| 327 def test_constant(self): | |
| 328 file_name = 'a.mojom' | |
| 329 graph = mojom_files_mojom.MojomFileGraph() | |
| 330 | |
| 331 mojom_const = mojom_types_mojom.DeclaredConstant() | |
| 332 mojom_const.decl_data = mojom_types_mojom.DeclarationData( | |
| 333 short_name='foo', container_type_key='struct_key') | |
| 334 mojom_const.type = mojom_types_mojom.Type( | |
| 335 simple_type=mojom_types_mojom.SimpleType.INT64) | |
| 336 mojom_const.value = mojom_types_mojom.Value() | |
| 337 mojom_const.value.literal_value = mojom_types_mojom.LiteralValue( | |
| 338 int64_value=20) | |
| 339 | |
| 340 mojom_struct = mojom_types_mojom.MojomStruct( | |
| 341 fields=[], | |
| 342 decl_data=mojom_types_mojom.DeclarationData( | |
| 343 short_name='AStruct', | |
| 344 source_file_info =mojom_types_mojom.SourceFileInfo( | |
| 345 file_name=file_name))) | |
| 346 add_version_info(mojom_struct, 0) | |
| 347 graph.resolved_types = {'struct_key': mojom_types_mojom.UserDefinedType( | |
| 348 struct_type=mojom_struct)} | |
| 349 | |
| 350 const = module.Constant() | |
| 351 translator = mojom_translator.FileTranslator(graph, file_name) | |
| 352 translator.ConstantFromMojom(const, mojom_const) | |
| 353 | |
| 354 self.assertEquals(mojom_const.decl_data.short_name, const.name) | |
| 355 self.assertEquals(module.INT64, const.kind) | |
| 356 self.assertEquals('20', const.value) | |
| 357 self.assertEquals(translator.UserDefinedFromTypeKey('struct_key'), | |
| 358 const.parent_kind) | |
| 359 | |
| 360 def test_enum(self): | |
| 361 file_name = 'a.mojom' | |
| 362 mojom_enum = mojom_types_mojom.MojomEnum() | |
| 363 mojom_enum.decl_data = mojom_types_mojom.DeclarationData( | |
| 364 short_name='AnEnum', | |
| 365 source_file_info=mojom_types_mojom.SourceFileInfo(file_name=file_name)) | |
| 366 value1 = mojom_types_mojom.EnumValue( | |
| 367 decl_data=mojom_types_mojom.DeclarationData(short_name='val1'), | |
| 368 initializer_value=mojom_types_mojom.Value( | |
| 369 literal_value=mojom_types_mojom.LiteralValue(uint64_value=20)), | |
| 370 int_value=20) | |
| 371 value2 = mojom_types_mojom.EnumValue( | |
| 372 decl_data=mojom_types_mojom.DeclarationData(short_name='val2'), | |
| 373 int_value=70) | |
| 374 mojom_enum.values = [value1, value2] | |
| 375 | |
| 376 | |
| 377 graph = mojom_files_mojom.MojomFileGraph() | |
| 378 enum = module.Enum() | |
| 379 translator = mojom_translator.FileTranslator(graph, file_name) | |
| 380 translator.EnumFromMojom( | |
| 381 enum, mojom_types_mojom.UserDefinedType(enum_type=mojom_enum)) | |
| 382 | |
| 383 self.assertEquals(translator._module, enum.module) | |
| 384 self.assertEquals(mojom_enum.decl_data.short_name, enum.name) | |
| 385 self.assertEquals(len(mojom_enum.values), len(enum.fields)) | |
| 386 | |
| 387 self.assertEquals(value1.decl_data.short_name, enum.fields[0].name) | |
| 388 self.assertEquals(value2.decl_data.short_name, enum.fields[1].name) | |
| 389 | |
| 390 self.assertEquals('20', enum.fields[0].value) | |
| 391 self.assertIsNone(enum.fields[1].value) | |
| 392 | |
| 393 self.assertEquals(value1.int_value, | |
| 394 enum.fields[0].numeric_value) | |
| 395 self.assertEquals(value2.int_value, | |
| 396 enum.fields[1].numeric_value) | |
| 397 | |
| 398 def test_child_enum(self): | |
| 399 file_name = 'a.mojom' | |
| 400 mojom_enum = mojom_types_mojom.MojomEnum() | |
| 401 mojom_enum.decl_data = mojom_types_mojom.DeclarationData( | |
| 402 short_name='AnEnum', | |
| 403 source_file_info=mojom_types_mojom.SourceFileInfo(file_name=file_name), | |
| 404 container_type_key='struct_key') | |
| 405 mojom_enum.values = [] | |
| 406 | |
| 407 graph = mojom_files_mojom.MojomFileGraph() | |
| 408 mojom_struct = mojom_types_mojom.MojomStruct( | |
| 409 fields=[], | |
| 410 decl_data=mojom_types_mojom.DeclarationData( | |
| 411 short_name='AStruct', | |
| 412 source_file_info =mojom_types_mojom.SourceFileInfo( | |
| 413 file_name=file_name))) | |
| 414 add_version_info(mojom_struct, 0) | |
| 415 graph.resolved_types = {'struct_key': mojom_types_mojom.UserDefinedType( | |
| 416 struct_type=mojom_struct)} | |
| 417 | |
| 418 enum = module.Enum() | |
| 419 translator = mojom_translator.FileTranslator(graph, file_name) | |
| 420 translator.EnumFromMojom( | |
| 421 enum, mojom_types_mojom.UserDefinedType(enum_type=mojom_enum)) | |
| 422 | |
| 423 self.assertEquals(mojom_enum.decl_data.short_name, enum.name) | |
| 424 self.assertEquals(len(mojom_enum.values), len(enum.fields)) | |
| 425 | |
| 426 def test_unions(self): | |
| 427 file_name = 'a.mojom' | |
| 428 mojom_union = mojom_types_mojom.MojomUnion() | |
| 429 mojom_union.decl_data = mojom_types_mojom.DeclarationData( | |
| 430 short_name='AUnion', | |
| 431 source_file_info=mojom_types_mojom.SourceFileInfo(file_name=file_name)) | |
| 432 | |
| 433 field1 = mojom_types_mojom.UnionField( | |
| 434 decl_data=mojom_types_mojom.DeclarationData(short_name='field1', | |
| 435 declaration_order=0, declared_ordinal=7), | |
| 436 type=mojom_types_mojom.Type( | |
| 437 simple_type=mojom_types_mojom.SimpleType.BOOL), | |
| 438 tag=7) | |
| 439 field2 = mojom_types_mojom.UnionField( | |
| 440 decl_data=mojom_types_mojom.DeclarationData( | |
| 441 short_name='field2', declaration_order=1), | |
| 442 type=mojom_types_mojom.Type( | |
| 443 simple_type=mojom_types_mojom.SimpleType.DOUBLE), | |
| 444 tag=8) | |
| 445 field3 = mojom_types_mojom.UnionField( | |
| 446 decl_data=mojom_types_mojom.DeclarationData(short_name='field3', | |
| 447 declaration_order=2, declared_ordinal=0), | |
| 448 type=mojom_types_mojom.Type( | |
| 449 simple_type=mojom_types_mojom.SimpleType.INT32), | |
| 450 tag=0) | |
| 451 | |
| 452 mojom_union.fields = [field3, field1, field2] | |
| 453 # mojom_fields_declaration_order lists, in declaration order, the indices | |
| 454 # of the fields in mojom_union.fields | |
| 455 mojom_fields_declaration_order = [1, 2, 0] | |
| 456 | |
| 457 graph = mojom_files_mojom.MojomFileGraph() | |
| 458 union = module.Union() | |
| 459 translator = mojom_translator.FileTranslator(graph, file_name) | |
| 460 translator.UnionFromMojom( | |
| 461 union, mojom_types_mojom.UserDefinedType(union_type=mojom_union)) | |
| 462 | |
| 463 self.assertEquals(translator._module, union.module) | |
| 464 self.assertEquals('AUnion', union.name) | |
| 465 self.assertEquals(len(mojom_union.fields), len(union.fields)) | |
| 466 | |
| 467 for index, gold_index in enumerate(mojom_fields_declaration_order): | |
| 468 gold = mojom_union.fields[gold_index] | |
| 469 f = union.fields[index] | |
| 470 self.assertEquals(gold.decl_data.short_name, f.name) | |
| 471 if gold.decl_data.declared_ordinal >= 0: | |
| 472 self.assertEquals(gold.decl_data.declared_ordinal, f.declared_tag) | |
| 473 else: | |
| 474 self.assertEquals(None, f.declared_tag) | |
| 475 self.assertEquals(gold.tag, f.ordinal) | |
| 476 | |
| 477 self.assertEquals(module.BOOL, union.fields[0].kind) | |
| 478 self.assertEquals(module.DOUBLE, union.fields[1].kind) | |
| 479 self.assertEquals(module.INT32, union.fields[2].kind) | |
| 480 | |
| 481 def literal_value(self, x): | |
| 482 """Creates a typed literal value containing the value |x|. | |
| 483 | |
| 484 Args: | |
| 485 x: A string, int, float or bool value. | |
| 486 | |
| 487 Returns: | |
| 488 {mojom_types.LiteralValue} with an appropriately typed value. | |
| 489 """ | |
| 490 if isinstance(x, str): | |
| 491 return mojom_types_mojom.LiteralValue(string_value=x) | |
| 492 elif isinstance(x, int): | |
| 493 return mojom_types_mojom.LiteralValue(int64_value=x) | |
| 494 elif isinstance(x, float): | |
| 495 return mojom_types_mojom.LiteralValue(double_value=x) | |
| 496 elif isinstance(x, bool): | |
| 497 return mojom_types_mojom.LiteralValue(bool_value=x) | |
| 498 raise Exception("unexpected type(x)=%s" % type(x)) | |
| 499 | |
| 500 | |
| 501 def test_attributes(self): | |
| 502 mojom_enum = mojom_types_mojom.MojomEnum() | |
| 503 mojom_enum.decl_data = mojom_types_mojom.DeclarationData() | |
| 504 gold = { | |
| 505 'foo': 'bar', | |
| 506 'other': 'thing', | |
| 507 'hello': 'world', | |
| 508 'min_version': 2, | |
| 509 'pi': 3.14159, | |
| 510 'is_happy': True | |
| 511 } | |
| 512 mojom_enum.decl_data.attributes = [] | |
| 513 for key, value in gold.iteritems(): | |
| 514 mojom_enum.decl_data.attributes.append( | |
| 515 mojom_types_mojom.Attribute(key=key, value=self.literal_value(value))) | |
| 516 | |
| 517 graph = mojom_files_mojom.MojomFileGraph() | |
| 518 attributes = mojom_translator.FileTranslator( | |
| 519 graph, None).AttributesFromMojom(mojom_enum) | |
| 520 | |
| 521 self.assertEquals(gold, attributes) | |
| 522 | |
| 523 def test_attributes_none(self): | |
| 524 mojom_enum = mojom_types_mojom.MojomEnum() | |
| 525 mojom_enum.decl_data = mojom_types_mojom.DeclarationData() | |
| 526 graph = mojom_files_mojom.MojomFileGraph() | |
| 527 attributes = mojom_translator.FileTranslator( | |
| 528 graph, None).AttributesFromMojom(mojom_enum) | |
| 529 self.assertFalse(attributes) | |
| 530 | |
| 531 def test_imported_struct(self): | |
| 532 graph = mojom_files_mojom.MojomFileGraph() | |
| 533 | |
| 534 graph.files = { | |
| 535 'a.mojom': mojom_files_mojom.MojomFile( | |
| 536 file_name='a.mojom', | |
| 537 specified_file_name='', | |
| 538 module_namespace='namespace', | |
| 539 imports=['root/c.mojom']), | |
| 540 'root/c.mojom': mojom_files_mojom.MojomFile( | |
| 541 file_name='root/c.mojom', | |
| 542 specified_file_name='', | |
| 543 module_namespace='otherns', | |
| 544 imports=[]), | |
| 545 } | |
| 546 | |
| 547 mojom_struct = mojom_types_mojom.MojomStruct() | |
| 548 mojom_struct.decl_data = mojom_types_mojom.DeclarationData( | |
| 549 short_name='AStruct', | |
| 550 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 551 file_name='root/c.mojom')) | |
| 552 mojom_struct.fields = [] | |
| 553 add_version_info(mojom_struct, 0) | |
| 554 | |
| 555 type_key = 'some_type_key' | |
| 556 graph.resolved_types = { | |
| 557 type_key: mojom_types_mojom.UserDefinedType(struct_type=mojom_struct)} | |
| 558 struct = module.Struct() | |
| 559 | |
| 560 # Translate should create the imports. | |
| 561 translator = mojom_translator.FileTranslator(graph, 'a.mojom') | |
| 562 translator.Translate() | |
| 563 | |
| 564 struct = translator.UserDefinedFromTypeRef( | |
| 565 mojom_types_mojom.Type( | |
| 566 type_reference=mojom_types_mojom.TypeReference( | |
| 567 type_key=type_key))) | |
| 568 | |
| 569 self.assertEquals( | |
| 570 translator._transitive_imports['root/c.mojom']['module'], struct.module) | |
| 571 self.assertEquals( | |
| 572 translator._transitive_imports['root/c.mojom'], struct.imported_from) | |
| 573 | |
| 574 def test_interface(self): | |
| 575 self.do_interface_test(True) | |
| 576 self.do_interface_test(False) | |
| 577 | |
| 578 def do_interface_test(self, specify_service_name): | |
| 579 file_name = 'a.mojom' | |
| 580 mojom_interface = mojom_types_mojom.MojomInterface( | |
| 581 current_version=47, | |
| 582 decl_data=mojom_types_mojom.DeclarationData( | |
| 583 short_name='AnInterface', | |
| 584 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 585 file_name=file_name))) | |
| 586 if specify_service_name: | |
| 587 mojom_interface.service_name = 'test::TheInterface' | |
| 588 mojom_interface.decl_data.attributes = [mojom_types_mojom.Attribute( | |
| 589 key='ServiceName', value=mojom_types_mojom.LiteralValue( | |
| 590 string_value='test::TheInterface'))] | |
| 591 else: | |
| 592 mojom_interface.service_name = None | |
| 593 mojom_method10 = mojom_types_mojom.MojomMethod( | |
| 594 ordinal=10, | |
| 595 decl_data=mojom_types_mojom.DeclarationData( | |
| 596 short_name='AMethod10', | |
| 597 declaration_order=1, | |
| 598 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 599 file_name=file_name)), | |
| 600 parameters=mojom_types_mojom.MojomStruct(fields=[], | |
| 601 version_info=build_version_info(0), | |
| 602 decl_data=build_decl_data('AMethod10_Request'))) | |
| 603 mojom_method0 = mojom_types_mojom.MojomMethod( | |
| 604 ordinal=0, | |
| 605 decl_data=mojom_types_mojom.DeclarationData( | |
| 606 short_name='AMethod0', | |
| 607 declaration_order=1, | |
| 608 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 609 file_name=file_name)), | |
| 610 parameters=mojom_types_mojom.MojomStruct(fields=[], | |
| 611 version_info=build_version_info(0), | |
| 612 decl_data=build_decl_data('AMethod0_Request'))) | |
| 613 mojom_method7 = mojom_types_mojom.MojomMethod( | |
| 614 ordinal=7, | |
| 615 decl_data=mojom_types_mojom.DeclarationData( | |
| 616 short_name='AMethod7', | |
| 617 declaration_order=0, | |
| 618 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 619 file_name=file_name)), | |
| 620 parameters=mojom_types_mojom.MojomStruct(fields=[], | |
| 621 version_info=build_version_info(0), | |
| 622 decl_data=build_decl_data('AMethod7_Request'))) | |
| 623 mojom_interface.methods = {10: mojom_method10, 0: mojom_method0, | |
| 624 7: mojom_method7} | |
| 625 | |
| 626 interface = module.Interface() | |
| 627 graph = mojom_files_mojom.MojomFileGraph() | |
| 628 translator = mojom_translator.FileTranslator(graph, file_name) | |
| 629 translator.InterfaceFromMojom(interface, mojom_types_mojom.UserDefinedType( | |
| 630 interface_type=mojom_interface)) | |
| 631 | |
| 632 | |
| 633 self.assertEquals(translator._module, interface.module) | |
| 634 self.assertEquals('AnInterface', interface.name) | |
| 635 self.assertEquals(mojom_interface.current_version, interface.version) | |
| 636 # The methods should be ordered by declaration_order. | |
| 637 self.assertEquals(7, interface.methods[0].ordinal) | |
| 638 self.assertEquals(0, interface.methods[1].ordinal) | |
| 639 self.assertEquals(10, interface.methods[2].ordinal) | |
| 640 if specify_service_name: | |
| 641 self.assertEquals('test::TheInterface', interface.service_name) | |
| 642 else: | |
| 643 self.assertEquals(None, interface.service_name) | |
| 644 | |
| 645 # TODO(azani): Add the contained declarations. | |
| 646 | |
| 647 def test_method(self): | |
| 648 file_name = 'a.mojom' | |
| 649 mojom_method = mojom_types_mojom.MojomMethod( | |
| 650 ordinal=10, | |
| 651 min_version=6, | |
| 652 decl_data=mojom_types_mojom.DeclarationData( | |
| 653 short_name='AMethod', | |
| 654 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 655 file_name=file_name))) | |
| 656 | |
| 657 param1 = mojom_types_mojom.StructField( | |
| 658 decl_data=mojom_types_mojom.DeclarationData(short_name='a_param'), | |
| 659 type=mojom_types_mojom.Type( | |
| 660 simple_type=mojom_types_mojom.SimpleType.UINT32), | |
| 661 offset=21, | |
| 662 bit=6, | |
| 663 min_version=11) | |
| 664 param2 = mojom_types_mojom.StructField( | |
| 665 decl_data=mojom_types_mojom.DeclarationData(short_name='b_param'), | |
| 666 type=mojom_types_mojom.Type( | |
| 667 simple_type=mojom_types_mojom.SimpleType.UINT64), | |
| 668 offset=22, | |
| 669 bit=7, | |
| 670 min_version=12) | |
| 671 mojom_method.parameters = mojom_types_mojom.MojomStruct( | |
| 672 fields=[param1, param2], | |
| 673 version_info=build_version_info(2), | |
| 674 decl_data=build_decl_data('Not used')) | |
| 675 | |
| 676 interface = module.Interface('MyInterface') | |
| 677 graph = mojom_files_mojom.MojomFileGraph() | |
| 678 translator = mojom_translator.FileTranslator(graph, file_name) | |
| 679 method = translator.MethodFromMojom(mojom_method, interface) | |
| 680 | |
| 681 self.assertEquals(mojom_method.decl_data.short_name, method.name) | |
| 682 self.assertEquals(interface, method.interface) | |
| 683 self.assertEquals(mojom_method.ordinal, method.ordinal) | |
| 684 self.assertEquals(mojom_method.min_version, method.min_version) | |
| 685 self.assertIsNone(method.response_parameters) | |
| 686 self.assertEquals( | |
| 687 len(mojom_method.parameters.fields), len(method.parameters)) | |
| 688 self.assertEquals(param1.decl_data.short_name, method.parameters[0].name) | |
| 689 self.assertEquals(param2.decl_data.short_name, method.parameters[1].name) | |
| 690 self.assertEquals('MyInterface_AMethod_Params', method.param_struct.name) | |
| 691 self.assertEquals(len(mojom_method.parameters.fields), | |
| 692 len(method.param_struct.fields)) | |
| 693 for i in xrange(0, len(mojom_method.parameters.fields)): | |
| 694 gold = mojom_method.parameters.fields[i] | |
| 695 f = method.param_struct.fields_in_ordinal_order[i] | |
| 696 self.assertEquals(gold.decl_data.short_name, f.name) | |
| 697 self.assertEquals(gold.offset, f.computed_offset) | |
| 698 self.assertEquals(gold.bit, f.computed_bit) | |
| 699 self.assertEquals(gold.min_version, f.computed_min_version) | |
| 700 | |
| 701 # Add empty return params. | |
| 702 mojom_method.response_params = mojom_types_mojom.MojomStruct(fields=[]) | |
| 703 add_version_info(mojom_method.response_params, 0) | |
| 704 add_decl_data(mojom_method.response_params, 'AMethod_Response') | |
| 705 method = translator.MethodFromMojom(mojom_method, interface) | |
| 706 self.assertEquals([], method.response_parameters) | |
| 707 | |
| 708 # Add non-empty return params. | |
| 709 mojom_method.response_params.fields = [param1] | |
| 710 method = translator.MethodFromMojom(mojom_method, interface) | |
| 711 self.assertEquals( | |
| 712 param1.decl_data.short_name, method.response_parameters[0].name) | |
| 713 | |
| 714 def test_parameter(self): | |
| 715 # Parameters are encoded as fields in a struct. | |
| 716 mojom_param = mojom_types_mojom.StructField( | |
| 717 decl_data=mojom_types_mojom.DeclarationData( | |
| 718 short_name='param0', | |
| 719 declared_ordinal=5), | |
| 720 type=mojom_types_mojom.Type( | |
| 721 simple_type=mojom_types_mojom.SimpleType.UINT64), | |
| 722 default_value=mojom_types_mojom.Value( | |
| 723 literal_value=mojom_types_mojom.LiteralValue(uint64_value=20))) | |
| 724 | |
| 725 graph = mojom_files_mojom.MojomFileGraph() | |
| 726 translator = mojom_translator.FileTranslator(graph, '') | |
| 727 param = translator.ParamFromMojom(mojom_param) | |
| 728 | |
| 729 self.assertEquals(mojom_param.decl_data.short_name, param.name) | |
| 730 self.assertEquals(module.UINT64, param.kind) | |
| 731 self.assertEquals(mojom_param.decl_data.declared_ordinal, param.ordinal) | |
| 732 | |
| 733 def test_contained_declarations(self): | |
| 734 graph = mojom_files_mojom.MojomFileGraph() | |
| 735 file_name = 'root/f.mojom' | |
| 736 | |
| 737 mojom_enum = mojom_types_mojom.MojomEnum( | |
| 738 values=[], | |
| 739 decl_data=mojom_types_mojom.DeclarationData( | |
| 740 short_name='AnEnum', | |
| 741 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 742 file_name=file_name), | |
| 743 container_type_key='parent_key')) | |
| 744 graph.resolved_types = { | |
| 745 'enum_key': mojom_types_mojom.UserDefinedType(enum_type=mojom_enum)} | |
| 746 | |
| 747 mojom_const = mojom_types_mojom.DeclaredConstant( | |
| 748 decl_data=mojom_types_mojom.DeclarationData( | |
| 749 short_name='AConst', | |
| 750 container_type_key='parent_key'), | |
| 751 type=mojom_types_mojom.Type( | |
| 752 simple_type=mojom_types_mojom.SimpleType.INT64), | |
| 753 value=mojom_types_mojom.Value( | |
| 754 literal_value=mojom_types_mojom.LiteralValue( | |
| 755 int64_value=30))) | |
| 756 graph.resolved_constants = {'constant_key': mojom_const} | |
| 757 | |
| 758 contained_declarations = mojom_types_mojom.ContainedDeclarations( | |
| 759 enums=['enum_key'], constants=['constant_key']) | |
| 760 | |
| 761 translator = mojom_translator.FileTranslator(graph, file_name) | |
| 762 struct = module.Struct(name='parent') | |
| 763 translator._type_cache['parent_key'] = struct | |
| 764 translator.PopulateContainedDeclarationsFromMojom( | |
| 765 struct, contained_declarations) | |
| 766 | |
| 767 self.assertEquals( | |
| 768 mojom_enum.decl_data.short_name, struct.enums[0].name) | |
| 769 self.assertEquals(struct, struct.enums[0].parent_kind) | |
| 770 self.assertEquals( | |
| 771 mojom_const.decl_data.short_name, struct.constants[0].name) | |
| 772 self.assertEquals(struct, struct.constants[0].parent_kind) | |
| 773 | |
| 774 | |
| 775 @unittest.skipUnless(bindings_imported, 'Could not import python bindings.') | |
| 776 class TestValueFromMojom(unittest.TestCase): | |
| 777 | |
| 778 def test_literal_value(self): | |
| 779 mojom_int64 = mojom_types_mojom.Value() | |
| 780 mojom_int64.literal_value = mojom_types_mojom.LiteralValue(int64_value=20) | |
| 781 mojom_bool = mojom_types_mojom.Value() | |
| 782 mojom_bool.literal_value = mojom_types_mojom.LiteralValue(bool_value=True) | |
| 783 mojom_double = mojom_types_mojom.Value() | |
| 784 mojom_double.literal_value = mojom_types_mojom.LiteralValue( | |
| 785 double_value=1234.012345678901) | |
| 786 | |
| 787 graph = mojom_files_mojom.MojomFileGraph() | |
| 788 int64_const = mojom_translator.FileTranslator(graph, None).ValueFromMojom( | |
| 789 mojom_int64) | |
| 790 bool_const = mojom_translator.FileTranslator(graph, None).ValueFromMojom( | |
| 791 mojom_bool) | |
| 792 double_const = mojom_translator.FileTranslator(graph, None).ValueFromMojom( | |
| 793 mojom_double) | |
| 794 | |
| 795 self.assertEquals('20', int64_const) | |
| 796 self.assertEquals('true', bool_const) | |
| 797 self.assertEquals('1234.012345678901', double_const) | |
| 798 | |
| 799 def test_builtin_const(self): | |
| 800 mojom = mojom_types_mojom.Value() | |
| 801 | |
| 802 graph = mojom_files_mojom.MojomFileGraph() | |
| 803 | |
| 804 gold = [ | |
| 805 (mojom_types_mojom.BuiltinConstantValue.DOUBLE_INFINITY, | |
| 806 'double.INFINITY'), | |
| 807 (mojom_types_mojom.BuiltinConstantValue.DOUBLE_NEGATIVE_INFINITY, | |
| 808 'double.NEGATIVE_INFINITY'), | |
| 809 (mojom_types_mojom.BuiltinConstantValue.DOUBLE_NAN, | |
| 810 'double.NAN'), | |
| 811 (mojom_types_mojom.BuiltinConstantValue.FLOAT_INFINITY, | |
| 812 'float.INFINITY'), | |
| 813 (mojom_types_mojom.BuiltinConstantValue.FLOAT_NEGATIVE_INFINITY, | |
| 814 'float.NEGATIVE_INFINITY'), | |
| 815 (mojom_types_mojom.BuiltinConstantValue.FLOAT_NAN, 'float.NAN'), | |
| 816 ] | |
| 817 | |
| 818 for mojom_builtin, string in gold: | |
| 819 mojom.builtin_value = mojom_builtin | |
| 820 const = mojom_translator.FileTranslator(graph, None).ValueFromMojom(mojom) | |
| 821 self.assertIsInstance(const, module.BuiltinValue) | |
| 822 self.assertEquals(string, const.value) | |
| 823 | |
| 824 def test_enum_value(self): | |
| 825 file_name = 'a.mojom' | |
| 826 mojom_enum = mojom_types_mojom.MojomEnum() | |
| 827 mojom_enum.decl_data = mojom_types_mojom.DeclarationData( | |
| 828 short_name='AnEnum', | |
| 829 source_file_info=mojom_types_mojom.SourceFileInfo(file_name=file_name)) | |
| 830 value1 = mojom_types_mojom.EnumValue( | |
| 831 decl_data=mojom_types_mojom.DeclarationData( | |
| 832 short_name='val1', | |
| 833 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 834 file_name=file_name)), | |
| 835 initializer_value=mojom_types_mojom.Value( | |
| 836 literal_value=mojom_types_mojom.LiteralValue(uint64_value=20)), | |
| 837 int_value=20) | |
| 838 value2 = mojom_types_mojom.EnumValue( | |
| 839 decl_data=mojom_types_mojom.DeclarationData(short_name='val2'), | |
| 840 int_value=70) | |
| 841 mojom_enum.values = [value1, value2] | |
| 842 | |
| 843 graph = mojom_files_mojom.MojomFileGraph() | |
| 844 graph.resolved_types = { | |
| 845 'enum_key': mojom_types_mojom.UserDefinedType(enum_type=mojom_enum)} | |
| 846 | |
| 847 mojom = mojom_types_mojom.Value( | |
| 848 enum_value_reference=mojom_types_mojom.EnumValueReference( | |
| 849 identifier='SOMEID', | |
| 850 enum_type_key='enum_key', | |
| 851 enum_value_index=0)) | |
| 852 | |
| 853 translator = mojom_translator.FileTranslator(graph, file_name) | |
| 854 enum_value = translator.ValueFromMojom(mojom) | |
| 855 enum = translator.UserDefinedFromTypeKey('enum_key') | |
| 856 | |
| 857 self.assertIs(enum, enum_value.enum) | |
| 858 self.assertIs(value1.decl_data.short_name, enum_value.name) | |
| 859 | |
| 860 def test_constant_value(self): | |
| 861 file_name = 'a.mojom' | |
| 862 mojom_const = mojom_types_mojom.DeclaredConstant( | |
| 863 decl_data=mojom_types_mojom.DeclarationData( | |
| 864 short_name='AConst', | |
| 865 source_file_info=mojom_types_mojom.SourceFileInfo( | |
| 866 file_name=file_name)), | |
| 867 type=mojom_types_mojom.Type( | |
| 868 simple_type=mojom_types_mojom.SimpleType.INT64), | |
| 869 value=mojom_types_mojom.Value( | |
| 870 literal_value=mojom_types_mojom.LiteralValue( | |
| 871 int64_value=30))) | |
| 872 | |
| 873 graph = mojom_files_mojom.MojomFileGraph() | |
| 874 graph.resolved_constants = {'constant_key': mojom_const} | |
| 875 | |
| 876 mojom = mojom_types_mojom.Value( | |
| 877 constant_reference=mojom_types_mojom.ConstantReference( | |
| 878 identifier='SOMEID', | |
| 879 constant_key='constant_key')) | |
| 880 | |
| 881 translator = mojom_translator.FileTranslator(graph, file_name) | |
| 882 const_value = translator.ValueFromMojom(mojom) | |
| 883 self.assertIs( | |
| 884 translator.ConstantFromKey('constant_key'), const_value.constant) | |
| 885 self.assertIs(mojom_const.decl_data.short_name, const_value.name) | |
| 886 | |
| 887 | |
| 888 @unittest.skipUnless(bindings_imported, 'Could not import python bindings.') | |
| 889 class TestKindFromMojom(unittest.TestCase): | |
| 890 | |
| 891 def test_simple_type(self): | |
| 892 simple_types = [ | |
| 893 (mojom_types_mojom.SimpleType.BOOL, module.BOOL), | |
| 894 (mojom_types_mojom.SimpleType.INT8, module.INT8), | |
| 895 (mojom_types_mojom.SimpleType.INT16, module.INT16), | |
| 896 (mojom_types_mojom.SimpleType.INT32, module.INT32), | |
| 897 (mojom_types_mojom.SimpleType.INT64, module.INT64), | |
| 898 (mojom_types_mojom.SimpleType.UINT8, module.UINT8), | |
| 899 (mojom_types_mojom.SimpleType.UINT16, module.UINT16), | |
| 900 (mojom_types_mojom.SimpleType.UINT32, module.UINT32), | |
| 901 (mojom_types_mojom.SimpleType.UINT64, module.UINT64), | |
| 902 (mojom_types_mojom.SimpleType.FLOAT, module.FLOAT), | |
| 903 (mojom_types_mojom.SimpleType.DOUBLE, module.DOUBLE), | |
| 904 ] | |
| 905 | |
| 906 g = mojom_files_mojom.MojomFileGraph() | |
| 907 t = mojom_translator.FileTranslator(g, None) | |
| 908 for mojom, golden in simple_types: | |
| 909 self.assertEquals( | |
| 910 golden, t.KindFromMojom(mojom_types_mojom.Type(simple_type=mojom))) | |
| 911 | |
| 912 def test_handle_type(self): | |
| 913 handle_types = [ | |
| 914 (mojom_types_mojom.HandleType.Kind.UNSPECIFIED, False, | |
| 915 module.HANDLE), | |
| 916 (mojom_types_mojom.HandleType.Kind.MESSAGE_PIPE, False, | |
| 917 module.MSGPIPE), | |
| 918 (mojom_types_mojom.HandleType.Kind.DATA_PIPE_CONSUMER, False, | |
| 919 module.DCPIPE), | |
| 920 (mojom_types_mojom.HandleType.Kind.DATA_PIPE_PRODUCER, False, | |
| 921 module.DPPIPE), | |
| 922 (mojom_types_mojom.HandleType.Kind.SHARED_BUFFER, False, | |
| 923 module.SHAREDBUFFER), | |
| 924 (mojom_types_mojom.HandleType.Kind.UNSPECIFIED, True, | |
| 925 module.NULLABLE_HANDLE), | |
| 926 (mojom_types_mojom.HandleType.Kind.MESSAGE_PIPE, True, | |
| 927 module.NULLABLE_MSGPIPE), | |
| 928 (mojom_types_mojom.HandleType.Kind.DATA_PIPE_CONSUMER, True, | |
| 929 module.NULLABLE_DCPIPE), | |
| 930 (mojom_types_mojom.HandleType.Kind.DATA_PIPE_PRODUCER, True, | |
| 931 module.NULLABLE_DPPIPE), | |
| 932 (mojom_types_mojom.HandleType.Kind.SHARED_BUFFER, True, | |
| 933 module.NULLABLE_SHAREDBUFFER), | |
| 934 ] | |
| 935 g = mojom_files_mojom.MojomFileGraph() | |
| 936 t = mojom_translator.FileTranslator(g, None) | |
| 937 for mojom, nullable, golden in handle_types: | |
| 938 h = mojom_types_mojom.Type() | |
| 939 h.handle_type = mojom_types_mojom.HandleType( | |
| 940 kind=mojom, nullable=nullable) | |
| 941 self.assertEquals(golden, t.KindFromMojom(h)) | |
| 942 | |
| 943 def test_string_type(self): | |
| 944 g = mojom_files_mojom.MojomFileGraph() | |
| 945 t = mojom_translator.FileTranslator(g, None) | |
| 946 | |
| 947 s = mojom_types_mojom.Type(string_type=mojom_types_mojom.StringType()) | |
| 948 self.assertEquals(module.STRING, t.KindFromMojom(s)) | |
| 949 | |
| 950 s.string_type.nullable = True | |
| 951 self.assertEquals(module.NULLABLE_STRING, t.KindFromMojom(s)) | |
| 952 | |
| 953 def test_array_type(self): | |
| 954 array_types = [ | |
| 955 (False, False, -1), | |
| 956 (False, False, 10), | |
| 957 (True, False, -1), | |
| 958 (True, True, -1), | |
| 959 (False, True, -1), | |
| 960 (False, True, 10), | |
| 961 ] | |
| 962 g = mojom_files_mojom.MojomFileGraph() | |
| 963 t = mojom_translator.FileTranslator(g, None) | |
| 964 | |
| 965 for array_nullable, element_nullable, size in array_types: | |
| 966 a = mojom_types_mojom.Type() | |
| 967 a.array_type = mojom_types_mojom.ArrayType( | |
| 968 nullable=array_nullable, | |
| 969 fixed_length=size) | |
| 970 a.array_type.element_type = mojom_types_mojom.Type( | |
| 971 string_type=mojom_types_mojom.StringType(nullable=element_nullable)) | |
| 972 | |
| 973 result = t.KindFromMojom(a) | |
| 974 self.assertTrue(module.IsArrayKind(result)) | |
| 975 self.assertTrue(module.IsStringKind(result.kind)) | |
| 976 self.assertEquals(array_nullable, module.IsNullableKind(result)) | |
| 977 self.assertEquals(element_nullable, module.IsNullableKind(result.kind)) | |
| 978 | |
| 979 if size < 0: | |
| 980 self.assertIsNone(result.length) | |
| 981 else: | |
| 982 self.assertEquals(size, result.length) | |
| 983 | |
| 984 def test_map_type(self): | |
| 985 map_types = [ | |
| 986 (False, False), | |
| 987 (True, False), | |
| 988 (False, True), | |
| 989 (True, True), | |
| 990 ] | |
| 991 g = mojom_files_mojom.MojomFileGraph() | |
| 992 t = mojom_translator.FileTranslator(g, None) | |
| 993 | |
| 994 for map_nullable, value_nullable in map_types: | |
| 995 m = mojom_types_mojom.Type() | |
| 996 m.map_type = mojom_types_mojom.MapType( | |
| 997 nullable=map_nullable) | |
| 998 m.map_type.key_type = mojom_types_mojom.Type( | |
| 999 string_type=mojom_types_mojom.StringType()) | |
| 1000 m.map_type.value_type = mojom_types_mojom.Type( | |
| 1001 handle_type=mojom_types_mojom.HandleType( | |
| 1002 kind=mojom_types_mojom.HandleType.Kind.SHARED_BUFFER, | |
| 1003 nullable=value_nullable)) | |
| 1004 | |
| 1005 result = t.KindFromMojom(m) | |
| 1006 self.assertTrue(module.IsMapKind(result)) | |
| 1007 self.assertTrue(module.IsStringKind(result.key_kind)) | |
| 1008 self.assertTrue(module.IsSharedBufferKind(result.value_kind)) | |
| 1009 self.assertEquals(map_nullable, module.IsNullableKind(result)) | |
| 1010 self.assertEquals(value_nullable, | |
| 1011 module.IsNullableKind(result.value_kind)) | |
| 1012 | |
| 1013 def test_user_defined_type_type(self): | |
| 1014 graph = mojom_files_mojom.MojomFileGraph() | |
| 1015 mojom_struct = mojom_types_mojom.MojomStruct( | |
| 1016 decl_data=mojom_types_mojom.DeclarationData(short_name='FirstStruct')) | |
| 1017 type_key = 'some opaque string' | |
| 1018 mojom_struct.fields = [ | |
| 1019 # Make sure recursive structs are correctly handled. | |
| 1020 mojom_types_mojom.StructField( | |
| 1021 decl_data=mojom_types_mojom.DeclarationData(short_name='field00'), | |
| 1022 type=mojom_types_mojom.Type( | |
| 1023 type_reference=mojom_types_mojom.TypeReference(type_key=type_key))) | |
| 1024 ] | |
| 1025 add_version_info(mojom_struct, 1) | |
| 1026 graph.resolved_types = { | |
| 1027 type_key: mojom_types_mojom.UserDefinedType(struct_type=mojom_struct)} | |
| 1028 | |
| 1029 mojom_type = mojom_types_mojom.Type() | |
| 1030 mojom_type.type_reference = mojom_types_mojom.TypeReference( | |
| 1031 type_key=type_key) | |
| 1032 | |
| 1033 t = mojom_translator.FileTranslator(graph, None) | |
| 1034 result = t.KindFromMojom(mojom_type) | |
| 1035 self.assertTrue(module.IsStructKind(result)) | |
| 1036 self.assertEquals(mojom_struct.decl_data.short_name, result.name) | |
| 1037 self.assertEquals(result, result.fields[0].kind) | |
| 1038 self.assertEquals(type_key, result.type_key) | |
| 1039 | |
| 1040 # Make sure we create only one module object per type. | |
| 1041 result2 = t.KindFromMojom(mojom_type) | |
| 1042 self.assertIs(result, result2) | |
| 1043 | |
| 1044 # Nullable type reference | |
| 1045 mojom_type.type_reference.nullable = True | |
| 1046 nullable_result = t.KindFromMojom(mojom_type) | |
| 1047 self.assertTrue(module.IsNullableKind(nullable_result)) | |
| 1048 | |
| 1049 if __name__ == '__main__': | |
| 1050 unittest.main() | |
| 1051 | |
| 1052 def build_decl_data(short_name): | |
| 1053 """Builds and returns a DeclarationData with the given short_name. | |
| 1054 | |
| 1055 Args: | |
| 1056 short_name: {str} short_name to use | |
| 1057 | |
| 1058 Returns: | |
| 1059 {mojom_types_mojom.DeclarationData} With the given short_name | |
| 1060 """ | |
| 1061 return mojom_types_mojom.DeclarationData(short_name=short_name) | |
| 1062 | |
| 1063 def add_decl_data(element, short_name): | |
| 1064 """Builds a DeclarationData with the given short_name and adds it | |
| 1065 as the |decl_data| attribute of |element|. | |
| 1066 | |
| 1067 Args: | |
| 1068 element: {any} The Python object to which a |decl_data| attribute will be | |
| 1069 added. | |
| 1070 short_name: {str} short_name to use | |
| 1071 """ | |
| 1072 element.decl_data=build_decl_data(short_name) | |
| 1073 | |
| 1074 def build_version_info(num_fields): | |
| 1075 """Builds and returns a list containing a single StructVersion with | |
| 1076 version_number=0, num_bytes=0, and the given value for num_fields. | |
| 1077 | |
| 1078 Args: | |
| 1079 num_fields: {int} The value of num_fields to use. | |
| 1080 Returns: | |
| 1081 {[mojom_types_mojom.StructVersion]} Containing a single element with | |
| 1082 the given value for num_fields. | |
| 1083 """ | |
| 1084 return [mojom_types_mojom.StructVersion( | |
| 1085 version_number=0, num_bytes=0,num_fields=num_fields)] | |
| 1086 | |
| 1087 def add_version_info(mojom_struct, num_fields): | |
| 1088 """Builds a list containing a single StructVersion with | |
| 1089 version_number=0, num_bytes=0, and the given value for num_fields. Adds this | |
| 1090 as the |version_info| attribute of |mojom_struct|. | |
| 1091 | |
| 1092 Args: | |
| 1093 mojom_struct: {any} The Python object to which a |version_info| attribute | |
| 1094 will be added. | |
| 1095 num_fields: {int} The value of num_fields to use. | |
| 1096 """ | |
| 1097 mojom_struct.version_info=build_version_info(num_fields) | |
| OLD | NEW |