tesseract  5.0.0
progress_test.cc
Go to the documentation of this file.
1 // File: progress_test.cc
3 // Description: Progress reporting API Test for Tesseract.
4 // Author: Jaroslaw Kubik
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 
17 // expects clone of tessdata_fast repo in ../../tessdata_fast
18 
19 #include "include_gunit.h"
20 
21 #include <tesseract/baseapi.h>
22 #include <tesseract/ocrclass.h>
23 #include "image.h"
24 
25 #include <allheaders.h>
26 #include "gmock/gmock.h"
27 
28 #include <fstream>
29 #include <iostream>
30 #include <locale>
31 #include <memory> // std::unique_ptr
32 #include <string>
33 
34 #include <time.h>
35 
36 namespace tesseract {
37 
38 class QuickTest : public testing::Test {
39 protected:
40  void SetUp() override {
41  start_time_ = time(nullptr);
42  }
43  void TearDown() override {
44  const time_t end_time = time(nullptr);
45  EXPECT_TRUE(end_time - start_time_ <= 25)
46  << "The test took too long - " << ::testing::PrintToString(end_time - start_time_);
47  }
48  time_t start_time_;
49 };
50 
52 public:
53  MOCK_METHOD1(classicProgress, bool(int));
54  MOCK_METHOD1(cancel, bool(int));
55 
57 
59  monitor.progress_callback = [](int progress, int, int, int, int) -> bool {
60  return instance->classicProgress(progress);
61  };
62  monitor.cancel = [](void *ths, int words) -> bool {
63  return ((ClassicMockProgressSink *)ths)->cancel(words);
64  };
65  monitor.cancel_this = this;
66  instance = this;
67  }
68 
70 };
71 
73 
75 public:
76  MOCK_METHOD1(progress, bool(int));
77 
79  monitor.progress_callback2 = [](ETEXT_DESC *ths, int, int, int, int) -> bool {
80  return ((NewMockProgressSink *)ths->cancel_this)->progress(ths->progress);
81  };
82  }
83 };
84 
85 void ClassicProgressTester(const char *imgname, const char *tessdatadir, const char *lang) {
86  using ::testing::_;
87  using ::testing::AllOf;
88  using ::testing::AtLeast;
89  using ::testing::DoAll;
90  using ::testing::Gt;
91  using ::testing::Le;
92  using ::testing::Return;
93  using ::testing::SaveArg;
94 
95  auto api = std::make_unique<tesseract::TessBaseAPI>();
96  ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
97  Image image = pixRead(imgname);
98  ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
99  api->SetImage(image);
100 
101  ClassicMockProgressSink progressSink;
102 
103  int currentProgress = -1;
104  EXPECT_CALL(progressSink, classicProgress(AllOf(Gt<int &>(currentProgress), Le(100))))
105  .Times(AtLeast(5))
106  .WillRepeatedly(DoAll(SaveArg<0>(&currentProgress), Return(false)));
107  EXPECT_CALL(progressSink, cancel(_)).Times(AtLeast(5)).WillRepeatedly(Return(false));
108 
109  EXPECT_EQ(api->Recognize(&progressSink.monitor), false);
110  EXPECT_GE(currentProgress, 50) << "The reported progress did not reach 50%";
111 
112  api->End();
113  image.destroy();
114 }
115 
116 void NewProgressTester(const char *imgname, const char *tessdatadir, const char *lang) {
117  using ::testing::_;
118  using ::testing::AllOf;
119  using ::testing::AtLeast;
120  using ::testing::DoAll;
121  using ::testing::Gt;
122  using ::testing::Le;
123  using ::testing::Return;
124  using ::testing::SaveArg;
125 
126  auto api = std::make_unique<tesseract::TessBaseAPI>();
127  ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
128  Image image = pixRead(imgname);
129  ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
130  api->SetImage(image);
131 
132  NewMockProgressSink progressSink;
133 
134  int currentProgress = -1;
135  EXPECT_CALL(progressSink, classicProgress(_)).Times(0);
136  EXPECT_CALL(progressSink, progress(AllOf(Gt<int &>(currentProgress), Le(100))))
137  .Times(AtLeast(5))
138  .WillRepeatedly(DoAll(SaveArg<0>(&currentProgress), Return(false)));
139  EXPECT_CALL(progressSink, cancel(_)).Times(AtLeast(5)).WillRepeatedly(Return(false));
140 
141  EXPECT_EQ(api->Recognize(&progressSink.monitor), false);
142  EXPECT_GE(currentProgress, 50) << "The reported progress did not reach 50%";
143 
144  api->End();
145  image.destroy();
146 }
147 
148 TEST(QuickTest, ClassicProgressReporting) {
149  ClassicProgressTester(TESTING_DIR "/phototest.tif", TESSDATA_DIR "_fast", "eng");
150 }
151 
152 TEST(QuickTest, NewProgressReporting) {
153  NewProgressTester(TESTING_DIR "/phototest.tif", TESSDATA_DIR "_fast", "eng");
154 }
155 
156 } // namespace tesseract
void ClassicProgressTester(const char *imgname, const char *tessdatadir, const char *lang)
void NewProgressTester(const char *imgname, const char *tessdatadir, const char *lang)
TEST(TesseractInstanceTest, TestMultipleTessInstances)
void * cancel_this
monitor-aware progress callback
Definition: ocrclass.h:115
PROGRESS_FUNC progress_callback
returns true to cancel
Definition: ocrclass.h:112
PROGRESS_FUNC2 progress_callback2
called whenever progress increases
Definition: ocrclass.h:114
int16_t progress
chars in this buffer(0)
Definition: ocrclass.h:104
CANCEL_FUNC cancel
for errcode use
Definition: ocrclass.h:111
void destroy()
Definition: image.cpp:32
void TearDown() override
void SetUp() override
MOCK_METHOD1(classicProgress, bool(int))
static ClassicMockProgressSink * instance
MOCK_METHOD1(progress, bool(int))