1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "actions/test_utils.h"
18
19 namespace libtextclassifier3 {
20
TestEntityDataSchema()21 std::string TestEntityDataSchema() {
22 // Create fake entity data schema meta data.
23 // Cannot use object oriented API here as that is not available for the
24 // reflection schema.
25 flatbuffers::FlatBufferBuilder schema_builder;
26 std::vector<flatbuffers::Offset<reflection::Field>> fields = {
27 reflection::CreateField(
28 schema_builder,
29 /*name=*/schema_builder.CreateString("greeting"),
30 /*type=*/
31 reflection::CreateType(schema_builder,
32 /*base_type=*/reflection::String),
33 /*id=*/0,
34 /*offset=*/4),
35 reflection::CreateField(
36 schema_builder,
37 /*name=*/schema_builder.CreateString("location"),
38 /*type=*/
39 reflection::CreateType(schema_builder,
40 /*base_type=*/reflection::String),
41 /*id=*/1,
42 /*offset=*/6),
43 reflection::CreateField(
44 schema_builder,
45 /*name=*/schema_builder.CreateString("person"),
46 /*type=*/
47 reflection::CreateType(schema_builder,
48 /*base_type=*/reflection::String),
49 /*id=*/2,
50 /*offset=*/8)};
51 std::vector<flatbuffers::Offset<reflection::Enum>> enums;
52 std::vector<flatbuffers::Offset<reflection::Object>> objects = {
53 reflection::CreateObject(
54 schema_builder,
55 /*name=*/schema_builder.CreateString("EntityData"),
56 /*fields=*/
57 schema_builder.CreateVectorOfSortedTables(&fields))};
58 schema_builder.Finish(reflection::CreateSchema(
59 schema_builder, schema_builder.CreateVectorOfSortedTables(&objects),
60 schema_builder.CreateVectorOfSortedTables(&enums),
61 /*(unused) file_ident=*/0,
62 /*(unused) file_ext=*/0,
63 /*root_table*/ objects[0]));
64
65 return std::string(
66 reinterpret_cast<const char*>(schema_builder.GetBufferPointer()),
67 schema_builder.GetSize());
68 }
69
SetTestEntityDataSchema(ActionsModelT * test_model)70 void SetTestEntityDataSchema(ActionsModelT* test_model) {
71 const std::string serialized_schema = TestEntityDataSchema();
72
73 test_model->actions_entity_data_schema.assign(
74 serialized_schema.data(),
75 serialized_schema.data() + serialized_schema.size());
76 }
77
78 } // namespace libtextclassifier3
79