コード例 #1
0
    def test_structs(self):
        file_name = 'a.mojom'
        graph = mojom_files_mojom.MojomFileGraph()
        mojom_file = mojom_files_mojom.MojomFile(file_name='a.mojom',
                                                 module_namespace='foo.bar')
        graph.files = {mojom_file.file_name: mojom_file}

        mojom_struct = mojom_types_mojom.MojomStruct(
            decl_data=mojom_types_mojom.DeclarationData(
                short_name='FirstStruct'))
        mojom_struct.fields = [
            mojom_types_mojom.StructField(
                decl_data=mojom_types_mojom.DeclarationData(
                    short_name='field01', declared_ordinal=5),
                type=mojom_types_mojom.Type(
                    simple_type=mojom_types_mojom.SimpleType.BOOL)),
            mojom_types_mojom.StructField(
                decl_data=mojom_types_mojom.DeclarationData(
                    short_name='field02'),
                type=mojom_types_mojom.Type(
                    simple_type=mojom_types_mojom.SimpleType.DOUBLE),
                default_value=mojom_types_mojom.DefaultFieldValue(
                    value=mojom_types_mojom.Value(
                        literal_value=mojom_types_mojom.LiteralValue(
                            double_value=15)))),
        ]
        mojom_struct.decl_data.source_file_info = mojom_types_mojom.SourceFileInfo(
            file_name=mojom_file.file_name)

        struct = module.Struct()
        translator = mojom_translator.FileTranslator(graph, file_name)
        translator.StructFromMojom(
            struct,
            mojom_types_mojom.UserDefinedType(struct_type=mojom_struct))

        self.assertEquals('FirstStruct', struct.name)
        self.assertEquals(translator._module, struct.module)

        self.assertEquals(len(mojom_struct.fields), len(struct.fields))
        for gold, f in zip(mojom_struct.fields, struct.fields):
            self.assertEquals(f.name, gold.decl_data.short_name)

        self.assertEquals(module.BOOL, struct.fields[0].kind)
        self.assertEquals(5, struct.fields[0].ordinal)

        self.assertEquals(module.DOUBLE, struct.fields[1].kind)
        self.assertEquals(None, struct.fields[1].ordinal)
        self.assertEquals('15.0', struct.fields[1].default)
コード例 #2
0
  def test_user_defined_type_type(self):
    graph = mojom_files_mojom.MojomFileGraph()
    mojom_struct = mojom_types_mojom.MojomStruct(
        decl_data=mojom_types_mojom.DeclarationData(short_name='FirstStruct'))
    type_key = 'some opaque string'
    mojom_struct.fields = [
        # Make sure recursive structs are correctly handled.
        mojom_types_mojom.StructField(
          decl_data=mojom_types_mojom.DeclarationData(short_name='field00'),
          type=mojom_types_mojom.Type(
            type_reference=mojom_types_mojom.TypeReference(type_key=type_key)))
        ]
    add_version_info(mojom_struct, 1)
    graph.resolved_types = {
        type_key: mojom_types_mojom.UserDefinedType(struct_type=mojom_struct)}

    mojom_type = mojom_types_mojom.Type()
    mojom_type.type_reference = mojom_types_mojom.TypeReference(
        type_key=type_key)

    t = mojom_translator.FileTranslator(graph, None)
    result = t.KindFromMojom(mojom_type)
    self.assertTrue(module.IsStructKind(result))
    self.assertEquals(mojom_struct.decl_data.short_name, result.name)
    self.assertEquals(result, result.fields[0].kind)
    self.assertEquals(type_key, result.type_key)

    # Make sure we create only one module object per type.
    result2 = t.KindFromMojom(mojom_type)
    self.assertIs(result, result2)

    # Nullable type reference
    mojom_type.type_reference.nullable = True
    nullable_result = t.KindFromMojom(mojom_type)
    self.assertTrue(module.IsNullableKind(nullable_result))
コード例 #3
0
    def test_method(self):
        file_name = 'a.mojom'
        mojom_method = mojom_types_mojom.MojomMethod(
            ordinal=10,
            decl_data=mojom_types_mojom.DeclarationData(
                short_name='AMethod',
                source_file_info=mojom_types_mojom.SourceFileInfo(
                    file_name=file_name)))

        param1 = mojom_types_mojom.StructField(
            decl_data=mojom_types_mojom.DeclarationData(short_name='a_param'),
            type=mojom_types_mojom.Type(
                simple_type=mojom_types_mojom.SimpleType.UINT32))
        param2 = mojom_types_mojom.StructField(
            decl_data=mojom_types_mojom.DeclarationData(short_name='b_param'),
            type=mojom_types_mojom.Type(
                simple_type=mojom_types_mojom.SimpleType.UINT64))
        mojom_method.parameters = mojom_types_mojom.MojomStruct(
            fields=[param1, param2])

        interface = module.Interface()
        graph = mojom_files_mojom.MojomFileGraph()
        translator = mojom_translator.FileTranslator(graph, file_name)
        method = translator.MethodFromMojom(mojom_method, interface)

        self.assertEquals(mojom_method.decl_data.short_name, method.name)
        self.assertEquals(interface, method.interface)
        self.assertEquals(mojom_method.ordinal, method.ordinal)
        self.assertIsNone(method.response_parameters)
        self.assertEquals(len(mojom_method.parameters.fields),
                          len(method.parameters))
        self.assertEquals(param1.decl_data.short_name,
                          method.parameters[0].name)
        self.assertEquals(param2.decl_data.short_name,
                          method.parameters[1].name)

        # Add empty return params.
        mojom_method.response_params = mojom_types_mojom.MojomStruct(fields=[])
        method = translator.MethodFromMojom(mojom_method, interface)
        self.assertEquals([], method.response_parameters)

        # Add non-empty return params.
        mojom_method.response_params.fields = [param1]
        method = translator.MethodFromMojom(mojom_method, interface)
        self.assertEquals(param1.decl_data.short_name,
                          method.response_parameters[0].name)
コード例 #4
0
    def test_parameter(self):
        # Parameters are encoded as fields in a struct.
        mojom_param = mojom_types_mojom.StructField(
            decl_data=mojom_types_mojom.DeclarationData(short_name='param0',
                                                        declared_ordinal=5),
            type=mojom_types_mojom.Type(
                simple_type=mojom_types_mojom.SimpleType.UINT64),
            default_value=mojom_types_mojom.Value(
                literal_value=mojom_types_mojom.LiteralValue(uint64_value=20)))

        graph = mojom_files_mojom.MojomFileGraph()
        translator = mojom_translator.FileTranslator(graph, '')
        param = translator.ParamFromMojom(mojom_param)

        self.assertEquals(mojom_param.decl_data.short_name, param.name)
        self.assertEquals(module.UINT64, param.kind)
        self.assertEquals(mojom_param.decl_data.declared_ordinal,
                          param.ordinal)
コード例 #5
0
    def test_method(self):
        file_name = 'a.mojom'
        mojom_method = mojom_types_mojom.MojomMethod(
            ordinal=10,
            min_version=6,
            decl_data=mojom_types_mojom.DeclarationData(
                short_name='AMethod',
                source_file_info=mojom_types_mojom.SourceFileInfo(
                    file_name=file_name)))

        param1 = mojom_types_mojom.StructField(
            decl_data=mojom_types_mojom.DeclarationData(short_name='a_param'),
            type=mojom_types_mojom.Type(
                simple_type=mojom_types_mojom.SimpleType.UINT32),
            offset=21,
            bit=6,
            min_version=11)
        param2 = mojom_types_mojom.StructField(
            decl_data=mojom_types_mojom.DeclarationData(short_name='b_param'),
            type=mojom_types_mojom.Type(
                simple_type=mojom_types_mojom.SimpleType.UINT64),
            offset=22,
            bit=7,
            min_version=12)
        mojom_method.parameters = mojom_types_mojom.MojomStruct(
            fields=[param1, param2],
            version_info=build_version_info(2),
            decl_data=build_decl_data('Not used'))

        interface = module.Interface('MyInterface')
        graph = mojom_files_mojom.MojomFileGraph()
        translator = mojom_translator.FileTranslator(graph, file_name)
        method = translator.MethodFromMojom(mojom_method, interface)

        self.assertEquals(mojom_method.decl_data.short_name, method.name)
        self.assertEquals(interface, method.interface)
        self.assertEquals(mojom_method.ordinal, method.ordinal)
        self.assertEquals(mojom_method.min_version, method.min_version)
        self.assertIsNone(method.response_parameters)
        self.assertEquals(len(mojom_method.parameters.fields),
                          len(method.parameters))
        self.assertEquals(param1.decl_data.short_name,
                          method.parameters[0].name)
        self.assertEquals(param2.decl_data.short_name,
                          method.parameters[1].name)
        self.assertEquals('MyInterface_AMethod_Params',
                          method.param_struct.name)
        self.assertEquals(len(mojom_method.parameters.fields),
                          len(method.param_struct.fields))
        for i in xrange(0, len(mojom_method.parameters.fields)):
            gold = mojom_method.parameters.fields[i]
            f = method.param_struct.fields_in_ordinal_order[i]
            self.assertEquals(gold.decl_data.short_name, f.name)
            self.assertEquals(gold.offset, f.computed_offset)
            self.assertEquals(gold.bit, f.computed_bit)
            self.assertEquals(gold.min_version, f.computed_min_version)

        # Add empty return params.
        mojom_method.response_params = mojom_types_mojom.MojomStruct(fields=[])
        add_version_info(mojom_method.response_params, 0)
        add_decl_data(mojom_method.response_params, 'AMethod_Response')
        method = translator.MethodFromMojom(mojom_method, interface)
        self.assertEquals([], method.response_parameters)

        # Add non-empty return params.
        mojom_method.response_params.fields = [param1]
        method = translator.MethodFromMojom(mojom_method, interface)
        self.assertEquals(param1.decl_data.short_name,
                          method.response_parameters[0].name)
コード例 #6
0
    def test_structs(self):
        file_name = 'a.mojom'
        graph = mojom_files_mojom.MojomFileGraph()
        mojom_file = mojom_files_mojom.MojomFile(file_name='a.mojom',
                                                 module_namespace='foo.bar')
        graph.files = {mojom_file.file_name: mojom_file}

        mojom_struct = mojom_types_mojom.MojomStruct(
            decl_data=mojom_types_mojom.DeclarationData(
                short_name='FirstStruct'))
        mojom_struct.fields = [
            mojom_types_mojom.StructField(
                decl_data=mojom_types_mojom.DeclarationData(
                    short_name='field03', declaration_order=2),
                type=mojom_types_mojom.Type(
                    simple_type=mojom_types_mojom.SimpleType.BOOL),
                offset=21,
                bit=6,
                min_version=11),
            mojom_types_mojom.StructField(
                decl_data=mojom_types_mojom.DeclarationData(
                    short_name='field01',
                    declared_ordinal=1,
                    declaration_order=0),
                type=mojom_types_mojom.Type(
                    simple_type=mojom_types_mojom.SimpleType.BOOL),
                offset=17,
                bit=1,
                min_version=4),
            mojom_types_mojom.StructField(
                decl_data=mojom_types_mojom.DeclarationData(
                    short_name='field02', declaration_order=1),
                type=mojom_types_mojom.Type(
                    simple_type=mojom_types_mojom.SimpleType.DOUBLE),
                offset=0,
                bit=0,
                min_version=0,
                default_value=mojom_types_mojom.DefaultFieldValue(
                    value=mojom_types_mojom.Value(
                        literal_value=mojom_types_mojom.LiteralValue(
                            double_value=15)))),
        ]
        mojom_struct.version_info = [
            mojom_types_mojom.StructVersion(version_number=0,
                                            num_bytes=67,
                                            num_fields=1),
            mojom_types_mojom.StructVersion(version_number=1,
                                            num_bytes=76,
                                            num_fields=3),
        ]
        # mojom_fields_declaration_order lists, in declaration order, the indices
        # of the fields in mojom_types_mojom.StructField.
        mojom_fields_declaration_order = [1, 2, 0]
        mojom_struct.decl_data.source_file_info = mojom_types_mojom.SourceFileInfo(
            file_name=mojom_file.file_name)

        struct = module.Struct()
        translator = mojom_translator.FileTranslator(graph, file_name)
        translator.StructFromMojom(
            struct,
            mojom_types_mojom.UserDefinedType(struct_type=mojom_struct))

        self.assertEquals('FirstStruct', struct.name)
        self.assertEquals(translator._module, struct.module)

        self.assertEquals(len(mojom_struct.fields), len(struct.fields))
        for index, gold_index in enumerate(mojom_fields_declaration_order):
            gold = mojom_struct.fields[gold_index]
            f = struct.fields[index]
            self.assertEquals(f.name, gold.decl_data.short_name)
            if gold.decl_data.declared_ordinal >= 0:
                self.assertEquals(gold.decl_data.declared_ordinal, f.ordinal)
            else:
                self.assertEquals(None, f.ordinal)
            self.assertEquals(gold_index, f.computed_ordinal)
            self.assertEquals(gold.offset, f.computed_offset)
            self.assertEquals(gold.bit, f.computed_bit)
            self.assertEquals(gold.min_version, f.computed_min_version)
            self.assertEquals(struct.fields_in_ordinal_order[index].name,
                              mojom_struct.fields[index].decl_data.short_name)

        self.assertEquals(2, len(struct.versions))
        for i in xrange(0, 2):
            self.assertEquals(mojom_struct.version_info[i].version_number,
                              struct.versions[i].version)
            self.assertEquals(mojom_struct.version_info[i].num_bytes,
                              struct.versions[i].num_bytes)
            self.assertEquals(mojom_struct.version_info[i].num_fields,
                              struct.versions[i].num_fields)

        self.assertEquals(module.BOOL, struct.fields[0].kind)
        self.assertEquals(module.DOUBLE, struct.fields[1].kind)

        self.assertEquals('15.0', struct.fields[1].default)