tesseract  5.0.0-alpha-619-ge9db
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 <limits.h>
20 #include <time.h>
21 #include <fstream>
22 #include <iostream>
23 #include <locale>
24 #include <memory> // std::unique_ptr
25 #include <string>
26 #include <tesseract/baseapi.h>
27 #include "gmock/gmock.h"
28 #include "include_gunit.h"
29 #include "leptonica/allheaders.h"
30 #include <tesseract/ocrclass.h>
31 
32 namespace {
33 
34 class QuickTest : public testing::Test {
35  protected:
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_);
42  }
43  time_t start_time_;
44 };
45 
46 class ClassicMockProgressSink {
47  public:
48  MOCK_METHOD1(classicProgress, bool(int));
49  MOCK_METHOD1(cancel, bool(int));
50 
51  ETEXT_DESC monitor;
52 
53  ClassicMockProgressSink() {
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  return ((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  monitor.progress_callback2 = [](ETEXT_DESC* ths, int, int, int,
75  int) -> bool {
76  return ((NewMockProgressSink*)ths->cancel_this)->progress(ths->progress);
77  };
78  }
79 };
80 
81 void ClassicProgressTester(const char* imgname, const char* tessdatadir,
82  const char* lang) {
83  using ::testing::_;
84  using ::testing::AllOf;
85  using ::testing::AtLeast;
86  using ::testing::DoAll;
87  using ::testing::Gt;
88  using ::testing::Le;
89  using ::testing::Return;
90  using ::testing::SaveArg;
91 
92  std::unique_ptr<tesseract::TessBaseAPI> api(new tesseract::TessBaseAPI());
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.";
97  api->SetImage(image);
98 
99  ClassicMockProgressSink progressSink;
100 
101  int currentProgress = -1;
102  EXPECT_CALL(progressSink,
103  classicProgress(AllOf(Gt<int&>(currentProgress), Le(100))))
104  .Times(AtLeast(5))
105  .WillRepeatedly(DoAll(SaveArg<0>(&currentProgress), Return(false)));
106  EXPECT_CALL(progressSink, cancel(_))
107  .Times(AtLeast(5))
108  .WillRepeatedly(Return(false));
109 
110  EXPECT_EQ(api->Recognize(&progressSink.monitor), false);
111  EXPECT_GE(currentProgress, 50) << "The reported progress did not reach 50%";
112 
113  api->End();
114  pixDestroy(&image);
115 }
116 
117 void NewProgressTester(const char* imgname, const char* tessdatadir,
118  const char* lang) {
119  using ::testing::_;
120  using ::testing::AllOf;
121  using ::testing::AtLeast;
122  using ::testing::DoAll;
123  using ::testing::Gt;
124  using ::testing::Le;
125  using ::testing::Return;
126  using ::testing::SaveArg;
127 
128  std::unique_ptr<tesseract::TessBaseAPI> api(new tesseract::TessBaseAPI());
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);
134 
135  NewMockProgressSink progressSink;
136 
137  int currentProgress = -1;
138  EXPECT_CALL(progressSink, classicProgress(_)).Times(0);
139  EXPECT_CALL(progressSink, progress(AllOf(Gt<int&>(currentProgress), Le(100))))
140  .Times(AtLeast(5))
141  .WillRepeatedly(DoAll(SaveArg<0>(&currentProgress), Return(false)));
142  EXPECT_CALL(progressSink, cancel(_))
143  .Times(AtLeast(5))
144  .WillRepeatedly(Return(false));
145 
146  EXPECT_EQ(api->Recognize(&progressSink.monitor), false);
147  EXPECT_GE(currentProgress, 50) << "The reported progress did not reach 50%";
148 
149  api->End();
150  pixDestroy(&image);
151 }
152 
153 TEST(QuickTest, ClassicProgressReporting) {
154  ClassicProgressTester(TESTING_DIR "/phototest.tif", TESSDATA_DIR "_fast",
155  "eng");
156 }
157 
158 TEST(QuickTest, NewProgressReporting) {
159  NewProgressTester(TESTING_DIR "/phototest.tif", TESSDATA_DIR "_fast", "eng");
160 }
161 
162 } // namespace
ETEXT_DESC::progress_callback2
PROGRESS_FUNC2 progress_callback2
called whenever progress increases
Definition: ocrclass.h:108
include_gunit.h
ETEXT_DESC
Definition: ocrclass.h:95
ETEXT_DESC::cancel_this
void * cancel_this
monitor-aware progress callback
Definition: ocrclass.h:109
ETEXT_DESC::progress
int16_t progress
chars in this buffer(0)
Definition: ocrclass.h:98
baseapi.h
tesstrain_utils.int
int
Definition: tesstrain_utils.py:154
tesseract::TessBaseAPI
Definition: baseapi.h:98
ETEXT_DESC::progress_callback
PROGRESS_FUNC progress_callback
returns true to cancel
Definition: ocrclass.h:106
ocrclass.h
ETEXT_DESC::cancel
CANCEL_FUNC cancel
for errcode use
Definition: ocrclass.h:105