Skip to content

Commit e05d333

Browse files
author
Jaroslaw Kubik
committed
Added tests for progress reporting API
The progress reporting API is now tested using googlemock tools.
1 parent 92168c5 commit e05d333

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

unittest/Makefile.am

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ AM_CPPFLAGS += -isystem $(top_srcdir)/googletest/googletest/include \
5252

5353
check_PROGRAMS = \
5454
apiexample_test \
55+
progress_test \
5556
intsimdmatrix_test \
5657
matrix_test \
5758
osd_test \
@@ -66,6 +67,10 @@ apiexample_test_SOURCES = apiexample_test.cc
6667
apiexample_test_LDFLAGS = $(OPENCL_LDFLAGS) $(LEPTONICA_LIBS)
6768
apiexample_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS)
6869

70+
progress_test_SOURCES = progress_test.cc
71+
progress_test_LDFLAGS = $(OPENCL_LDFLAGS) $(LEPTONICA_LIBS)
72+
progress_test_LDADD = $(GTEST_LIBS) $(GMOCK_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS)
73+
6974
intsimdmatrix_test_SOURCES = intsimdmatrix_test.cc
7075
intsimdmatrix_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)
7176

unittest/progress_test.cc

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
///////////////////////////////////////////////////////////////////////
2+
// File: apiexample_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.
15+
///////////////////////////////////////////////////////////////////////
16+
17+
// expects clone of tessdata_fast repo in ../../tessdata_fast
18+
19+
#include "include_gunit.h"
20+
#include "gmock/gmock.h"
21+
#include "baseapi.h"
22+
#include "ocrclass.h"
23+
#include "leptonica/allheaders.h"
24+
#include <iostream>
25+
#include <string>
26+
#include <fstream>
27+
#include <locale>
28+
#include <limits.h>
29+
#include <time.h>
30+
31+
namespace {
32+
33+
class QuickTest : public testing::Test {
34+
protected:
35+
virtual void SetUp() {
36+
start_time_ = time(nullptr);
37+
}
38+
virtual void TearDown() {
39+
const time_t end_time = time(nullptr);
40+
EXPECT_TRUE(end_time - start_time_ <=25) << "The test took too long - " << ::testing::PrintToString(end_time - start_time_);
41+
}
42+
time_t start_time_;
43+
};
44+
45+
class ClassicMockProgressSink {
46+
public:
47+
MOCK_METHOD1(classicProgress, bool( int ) );
48+
MOCK_METHOD1(cancel, bool( int ));
49+
50+
ETEXT_DESC monitor;
51+
52+
ClassicMockProgressSink()
53+
{
54+
monitor.progress_callback = []( int progress, int, int, int, int ) ->bool {
55+
return instance->classicProgress( progress );
56+
};
57+
monitor.cancel = []( void* ths, int words ) -> bool {
58+
((ClassicMockProgressSink*)ths)->cancel( words );
59+
};
60+
monitor.cancel_this = this;
61+
instance = this;
62+
}
63+
64+
static ClassicMockProgressSink* instance;
65+
};
66+
67+
ClassicMockProgressSink* ClassicMockProgressSink::instance = nullptr;
68+
69+
class NewMockProgressSink : public ClassicMockProgressSink {
70+
public:
71+
MOCK_METHOD1(progress, bool( int ) );
72+
73+
NewMockProgressSink()
74+
{
75+
monitor.progress_callback2 = [](ETEXT_DESC* ths, int, int, int, int ) -> bool {
76+
return ((NewMockProgressSink*)ths->cancel_this)->progress( ths->progress );
77+
};
78+
}
79+
};
80+
81+
void ClassicProgressTester(const char* imgname, const char* tessdatadir, const char* lang) {
82+
using ::testing::_;
83+
using ::testing::AllOf;
84+
using ::testing::AtLeast;
85+
using ::testing::DoAll;
86+
using ::testing::Gt;
87+
using ::testing::Le;
88+
using ::testing::Return;
89+
using ::testing::SaveArg;
90+
91+
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
92+
ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
93+
Pix *image = pixRead(imgname);
94+
ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
95+
api->SetImage(image);
96+
97+
ClassicMockProgressSink progressSink;
98+
99+
int currentProgress = -1;
100+
EXPECT_CALL( progressSink, classicProgress(AllOf(Gt(currentProgress),Le(100))) )
101+
.Times(AtLeast(5))
102+
.WillRepeatedly( DoAll(SaveArg<0>(&currentProgress),
103+
Return(false) ));
104+
EXPECT_CALL( progressSink, cancel(_) )
105+
.Times(AtLeast(5))
106+
.WillRepeatedly(Return(false));
107+
108+
EXPECT_EQ( api->Recognize( &progressSink.monitor ), false );
109+
EXPECT_GE( currentProgress, 50 ) << "The reported progress did not reach 50%";
110+
111+
api->End();
112+
pixDestroy(&image);
113+
}
114+
115+
void NewProgressTester(const char* imgname, const char* tessdatadir, const char* lang) {
116+
using ::testing::_;
117+
using ::testing::AllOf;
118+
using ::testing::AtLeast;
119+
using ::testing::DoAll;
120+
using ::testing::Gt;
121+
using ::testing::Le;
122+
using ::testing::Return;
123+
using ::testing::SaveArg;
124+
125+
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
126+
ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
127+
Pix *image = pixRead(imgname);
128+
ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
129+
api->SetImage(image);
130+
131+
NewMockProgressSink progressSink;
132+
133+
int currentProgress = -1;
134+
EXPECT_CALL( progressSink, classicProgress(_) )
135+
.Times(0);
136+
EXPECT_CALL( progressSink, progress(AllOf(Gt(currentProgress),Le(100))) )
137+
.Times(AtLeast(5))
138+
.WillRepeatedly( DoAll(SaveArg<0>(&currentProgress),
139+
Return(false) ));
140+
EXPECT_CALL( progressSink, cancel(_) )
141+
.Times(AtLeast(5))
142+
.WillRepeatedly(Return(false));
143+
144+
EXPECT_EQ( api->Recognize( &progressSink.monitor ), false );
145+
EXPECT_GE( currentProgress, 50 ) << "The reported progress did not reach 50%";
146+
147+
api->End();
148+
pixDestroy(&image);
149+
}
150+
151+
TEST(QuickTest, ClassicProgressReporitng) {
152+
ClassicProgressTester(TESTING_DIR "/phototest.tif",
153+
TESSDATA_DIR "_fast", "eng");
154+
}
155+
156+
TEST(QuickTest, NewProgressReporitng) {
157+
NewProgressTester(TESTING_DIR "/phototest.tif",
158+
TESSDATA_DIR "_fast", "eng");
159+
}
160+
161+
} // namespace

0 commit comments

Comments
 (0)