• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     AssertIsInitialized();
49   }
50 
51   static std::unique_ptr<TestingAnnotator> FromUnownedBuffer(
52       const char* buffer, int size, const UniLib* unilib = nullptr,
53       const CalendarLib* calendarlib = nullptr) {
54     // Safe to downcast from Annotator* to TestingAnnotator* because the
55     // subclass is not adding any new members.
56     return std::unique_ptr<TestingAnnotator>(
57         reinterpret_cast<TestingAnnotator*>(
58             Annotator::FromUnownedBuffer(buffer, size, unilib, calendarlib)
59                 .release()));
60   }
61 
62   using Annotator::ResolveConflicts;
63 
64  private:
AssertIsInitialized()65   void AssertIsInitialized() { ASSERT_TRUE(IsInitialized()); }
66 };
67 
68 class AnnotatorTest : public ::testing::TestWithParam<const char*> {
69  protected:
AnnotatorTest()70   AnnotatorTest()
71       : unilib_(CreateUniLibForTesting()),
72         calendarlib_(CreateCalendarLibForTesting()) {}
73 
74   std::unique_ptr<UniLib> unilib_;
75   std::unique_ptr<CalendarLib> calendarlib_;
76 };
77 
78 }  // namespace test_internal
79 }  // namespace libtextclassifier3
80 
81 #endif  // LIBTEXTCLASSIFIER_ANNOTATOR_ANNOTATOR_TEST_INCLUDE_H_
82