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 #include "flatbuffers/reflection.h"
20 
21 namespace libtextclassifier3 {
22 
TestEntityDataSchema()23 std::string TestEntityDataSchema() {
24   // Create fake entity data schema meta data.
25   // Cannot use object oriented API here as that is not available for the
26   // reflection schema.
27   flatbuffers::FlatBufferBuilder schema_builder;
28   std::vector<flatbuffers::Offset<reflection::Field>> fields = {
29       reflection::CreateField(
30           schema_builder,
31           /*name=*/schema_builder.CreateString("greeting"),
32           /*type=*/
33           reflection::CreateType(schema_builder,
34                                  /*base_type=*/reflection::String),
35           /*id=*/0,
36           /*offset=*/4),
37       reflection::CreateField(
38           schema_builder,
39           /*name=*/schema_builder.CreateString("location"),
40           /*type=*/
41           reflection::CreateType(schema_builder,
42                                  /*base_type=*/reflection::String),
43           /*id=*/1,
44           /*offset=*/6),
45       reflection::CreateField(
46           schema_builder,
47           /*name=*/schema_builder.CreateString("person"),
48           /*type=*/
49           reflection::CreateType(schema_builder,
50                                  /*base_type=*/reflection::String),
51           /*id=*/2,
52           /*offset=*/8)};
53   std::vector<flatbuffers::Offset<reflection::Enum>> enums;
54   std::vector<flatbuffers::Offset<reflection::Object>> objects = {
55       reflection::CreateObject(
56           schema_builder,
57           /*name=*/schema_builder.CreateString("EntityData"),
58           /*fields=*/
59           schema_builder.CreateVectorOfSortedTables(&fields))};
60   schema_builder.Finish(reflection::CreateSchema(
61       schema_builder, schema_builder.CreateVectorOfSortedTables(&objects),
62       schema_builder.CreateVectorOfSortedTables(&enums),
63       /*(unused) file_ident=*/0,
64       /*(unused) file_ext=*/0,
65       /*root_table*/ objects[0]));
66 
67   return std::string(
68       reinterpret_cast<const char*>(schema_builder.GetBufferPointer()),
69       schema_builder.GetSize());
70 }
71 
SetTestEntityDataSchema(ActionsModelT * test_model)72 void SetTestEntityDataSchema(ActionsModelT* test_model) {
73   const std::string serialized_schema = TestEntityDataSchema();
74 
75   test_model->actions_entity_data_schema.assign(
76       serialized_schema.data(),
77       serialized_schema.data() + serialized_schema.size());
78 }
79 
80 }  // namespace libtextclassifier3
81