tesseract v5.3.3.20231005
progress_test.cc
Go to the documentation of this file.
1
2// 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
36namespace tesseract {
37
38class QuickTest : public testing::Test {
39protected:
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
52public:
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
75public:
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
85void ClassicProgressTester(const char *imgname, const char *tessdatadir, const char *lang) {
86 using ::testing::_;
87 using ::testing::AllOf;
90 using ::testing::Gt;
91 using ::testing::Le;
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
116void NewProgressTester(const char *imgname, const char *tessdatadir, const char *lang) {
117 using ::testing::_;
118 using ::testing::AllOf;
121 using ::testing::Gt;
122 using ::testing::Le;
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
148TEST(QuickTest, ClassicProgressReporting) {
149 ClassicProgressTester(TESTING_DIR "/phototest.tif", TESSDATA_DIR "_fast", "eng");
150}
151
152TEST(QuickTest, NewProgressReporting) {
153 NewProgressTester(TESTING_DIR "/phototest.tif", TESSDATA_DIR "_fast", "eng");
154}
155
156} // namespace tesseract
#define EXPECT_CALL(obj, call)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2043
#define ASSERT_FALSE(condition)
Definition: gtest.h:1994
#define EXPECT_GE(val1, val2)
Definition: gtest.h:2051
#define EXPECT_TRUE(condition)
Definition: gtest.h:1982
#define ASSERT_TRUE(condition)
Definition: gtest.h:1990
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)
internal::SaveArgAction< k, Ptr > SaveArg(Ptr pointer)
GTEST_API_ Cardinality AtLeast(int n)
internal::DoAllAction< typename std::decay< Action >::type... > DoAll(Action &&... action)
::std::string PrintToString(const T &value)
internal::ReturnAction< R > Return(R value)
void * cancel_this
monitor-aware progress callback
Definition: ocrclass.h:116
PROGRESS_FUNC progress_callback
returns true to cancel
Definition: ocrclass.h:113
PROGRESS_FUNC2 progress_callback2
called whenever progress increases
Definition: ocrclass.h:115
int16_t progress
chars in this buffer(0)
Definition: ocrclass.h:105
CANCEL_FUNC cancel
for errcode use
Definition: ocrclass.h:112
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))