27 #include "gmock/gmock.h"
29 #include "leptonica/allheaders.h"
34 class QuickTest :
public testing::Test {
36 virtual void SetUp() { start_time_ = time(
nullptr); }
37 virtual void TearDown() {
38 const time_t end_time = time(
nullptr);
39 EXPECT_TRUE(end_time - start_time_ <= 25)
40 <<
"The test took too long - "
41 << ::testing::PrintToString(end_time - start_time_);
46 class ClassicMockProgressSink {
48 MOCK_METHOD1(classicProgress,
bool(
int));
49 MOCK_METHOD1(cancel,
bool(
int));
53 ClassicMockProgressSink() {
55 return instance->classicProgress(progress);
57 monitor.
cancel = [](
void* ths,
int words) ->
bool {
58 return ((ClassicMockProgressSink*)ths)->cancel(words);
64 static ClassicMockProgressSink* instance;
67 ClassicMockProgressSink* ClassicMockProgressSink::instance =
nullptr;
69 class NewMockProgressSink :
public ClassicMockProgressSink {
71 MOCK_METHOD1(progress,
bool(
int));
73 NewMockProgressSink() {
81 void ClassicProgressTester(
const char* imgname,
const char* tessdatadir,
84 using ::testing::AllOf;
85 using ::testing::AtLeast;
86 using ::testing::DoAll;
89 using ::testing::Return;
90 using ::testing::SaveArg;
93 ASSERT_FALSE(api->Init(tessdatadir, lang))
94 <<
"Could not initialize tesseract.";
95 Pix* image = pixRead(imgname);
96 ASSERT_TRUE(image !=
nullptr) <<
"Failed to read test image.";
99 ClassicMockProgressSink progressSink;
101 int currentProgress = -1;
102 EXPECT_CALL(progressSink,
103 classicProgress(AllOf(Gt<int&>(currentProgress), Le(100))))
105 .WillRepeatedly(DoAll(SaveArg<0>(¤tProgress), Return(
false)));
106 EXPECT_CALL(progressSink, cancel(_))
108 .WillRepeatedly(Return(
false));
110 EXPECT_EQ(api->Recognize(&progressSink.monitor),
false);
111 EXPECT_GE(currentProgress, 50) <<
"The reported progress did not reach 50%";
117 void NewProgressTester(
const char* imgname,
const char* tessdatadir,
120 using ::testing::AllOf;
121 using ::testing::AtLeast;
122 using ::testing::DoAll;
125 using ::testing::Return;
126 using ::testing::SaveArg;
129 ASSERT_FALSE(api->Init(tessdatadir, lang))
130 <<
"Could not initialize tesseract.";
131 Pix* image = pixRead(imgname);
132 ASSERT_TRUE(image !=
nullptr) <<
"Failed to read test image.";
133 api->SetImage(image);
135 NewMockProgressSink progressSink;
137 int currentProgress = -1;
138 EXPECT_CALL(progressSink, classicProgress(_)).Times(0);
139 EXPECT_CALL(progressSink, progress(AllOf(Gt<int&>(currentProgress), Le(100))))
141 .WillRepeatedly(DoAll(SaveArg<0>(¤tProgress), Return(
false)));
142 EXPECT_CALL(progressSink, cancel(_))
144 .WillRepeatedly(Return(
false));
146 EXPECT_EQ(api->Recognize(&progressSink.monitor),
false);
147 EXPECT_GE(currentProgress, 50) <<
"The reported progress did not reach 50%";
153 TEST(QuickTest, ClassicProgressReporting) {
154 ClassicProgressTester(TESTING_DIR
"/phototest.tif", TESSDATA_DIR
"_fast",
158 TEST(QuickTest, NewProgressReporting) {
159 NewProgressTester(TESTING_DIR
"/phototest.tif", TESSDATA_DIR
"_fast",
"eng");