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 #ifndef LIBTEXTCLASSIFIER_ANNOTATOR_ANNOTATOR_TEST_INCLUDE_H_
18 #define LIBTEXTCLASSIFIER_ANNOTATOR_ANNOTATOR_TEST_INCLUDE_H_
19
20 #include <fstream>
21 #include <string>
22
23 #include "annotator/annotator.h"
24 #include "utils/base/logging.h"
25 #include "utils/jvm-test-utils.h"
26 #include "utils/test-data-test-utils.h"
27 #include "utils/testing/annotator.h"
28 #include "utils/utf8/unilib.h"
29 #include "gtest/gtest.h"
30
31 namespace libtextclassifier3 {
32 namespace test_internal {
33
GetModelPath()34 inline std::string GetModelPath() {
35 return GetTestDataPath("annotator/test_data/");
36 }
37
38 class TestingAnnotator : public Annotator {
39 public:
40 TestingAnnotator(
41 const UniLib* unilib, const CalendarLib* calendarlib,
42 const std::function<void(ModelT*)> model_update_fn = [](ModelT* model) {
43 }) {
44 owned_buffer_ = CreateEmptyModel(model_update_fn);
45 ValidateAndInitialize(libtextclassifier3::ViewModel(owned_buffer_.data(),
46 owned_buffer_.size()),
47 unilib, calendarlib);
48 }
49
50 static std::unique_ptr<TestingAnnotator> FromUnownedBuffer(
51 const char* buffer, int size, const UniLib* unilib = nullptr,
52 const CalendarLib* calendarlib = nullptr) {
53 // Safe to downcast from Annotator* to TestingAnnotator* because the
54 // subclass is not adding any new members.
55 return std::unique_ptr<TestingAnnotator>(
56 reinterpret_cast<TestingAnnotator*>(
57 Annotator::FromUnownedBuffer(buffer, size, unilib, calendarlib)
58 .release()));
59 }
60
61 using Annotator::ResolveConflicts;
62 };
63
64 class AnnotatorTest : public ::testing::TestWithParam<const char*> {
65 protected:
AnnotatorTest()66 AnnotatorTest()
67 : unilib_(CreateUniLibForTesting()),
68 calendarlib_(CreateCalendarLibForTesting()) {}
69
70 std::unique_ptr<UniLib> unilib_;
71 std::unique_ptr<CalendarLib> calendarlib_;
72 };
73
74 } // namespace test_internal
75 } // namespace libtextclassifier3
76
77 #endif // LIBTEXTCLASSIFIER_ANNOTATOR_ANNOTATOR_TEST_INCLUDE_H_
78