2018-06-02 06:47:34 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
2019-01-14 22:38:04 +08:00
|
|
|
// File: progress_test.cc
|
2018-06-02 06:47:34 +08:00
|
|
|
// Description: Progress reporting API Test for Tesseract.
|
|
|
|
// Author: Jaroslaw Kubik
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// expects clone of tessdata_fast repo in ../../tessdata_fast
|
|
|
|
|
2020-12-29 01:36:04 +08:00
|
|
|
#include "include_gunit.h"
|
|
|
|
|
|
|
|
#include <tesseract/baseapi.h>
|
|
|
|
#include <tesseract/ocrclass.h>
|
2021-04-01 03:39:43 +08:00
|
|
|
#include "image.h"
|
2020-12-29 01:36:04 +08:00
|
|
|
|
2021-02-13 17:17:20 +08:00
|
|
|
#include <allheaders.h>
|
2020-12-29 01:36:04 +08:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
|
2018-09-29 15:19:13 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <locale>
|
2021-03-13 05:06:34 +08:00
|
|
|
#include <memory> // std::unique_ptr
|
2018-09-29 15:19:13 +08:00
|
|
|
#include <string>
|
2020-12-29 01:36:04 +08:00
|
|
|
|
|
|
|
#include <time.h>
|
2018-06-02 06:47:34 +08:00
|
|
|
|
2020-12-27 17:41:48 +08:00
|
|
|
namespace tesseract {
|
2018-06-02 06:47:34 +08:00
|
|
|
|
|
|
|
class QuickTest : public testing::Test {
|
2021-03-13 05:06:34 +08:00
|
|
|
protected:
|
2021-03-22 15:26:05 +08:00
|
|
|
void SetUp() override {
|
2021-03-13 05:06:34 +08:00
|
|
|
start_time_ = time(nullptr);
|
|
|
|
}
|
2021-03-22 15:26:05 +08:00
|
|
|
void TearDown() override {
|
2018-06-02 06:47:34 +08:00
|
|
|
const time_t end_time = time(nullptr);
|
2018-09-29 15:19:13 +08:00
|
|
|
EXPECT_TRUE(end_time - start_time_ <= 25)
|
2021-03-13 05:06:34 +08:00
|
|
|
<< "The test took too long - " << ::testing::PrintToString(end_time - start_time_);
|
2018-06-02 06:47:34 +08:00
|
|
|
}
|
|
|
|
time_t start_time_;
|
2018-09-29 15:19:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ClassicMockProgressSink {
|
2021-03-13 05:06:34 +08:00
|
|
|
public:
|
2018-09-29 15:19:13 +08:00
|
|
|
MOCK_METHOD1(classicProgress, bool(int));
|
|
|
|
MOCK_METHOD1(cancel, bool(int));
|
|
|
|
|
|
|
|
ETEXT_DESC monitor;
|
|
|
|
|
|
|
|
ClassicMockProgressSink() {
|
|
|
|
monitor.progress_callback = [](int progress, int, int, int, int) -> bool {
|
|
|
|
return instance->classicProgress(progress);
|
|
|
|
};
|
2021-03-13 05:06:34 +08:00
|
|
|
monitor.cancel = [](void *ths, int words) -> bool {
|
|
|
|
return ((ClassicMockProgressSink *)ths)->cancel(words);
|
2018-09-29 15:19:13 +08:00
|
|
|
};
|
|
|
|
monitor.cancel_this = this;
|
|
|
|
instance = this;
|
2018-06-02 06:47:34 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
static ClassicMockProgressSink *instance;
|
2018-09-29 15:19:13 +08:00
|
|
|
};
|
2018-06-02 06:47:34 +08:00
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
ClassicMockProgressSink *ClassicMockProgressSink::instance = nullptr;
|
2018-09-29 15:19:13 +08:00
|
|
|
|
|
|
|
class NewMockProgressSink : public ClassicMockProgressSink {
|
2021-03-13 05:06:34 +08:00
|
|
|
public:
|
2018-09-29 15:19:13 +08:00
|
|
|
MOCK_METHOD1(progress, bool(int));
|
2018-06-02 06:47:34 +08:00
|
|
|
|
2018-09-29 15:19:13 +08:00
|
|
|
NewMockProgressSink() {
|
2021-03-13 05:06:34 +08:00
|
|
|
monitor.progress_callback2 = [](ETEXT_DESC *ths, int, int, int, int) -> bool {
|
|
|
|
return ((NewMockProgressSink *)ths->cancel_this)->progress(ths->progress);
|
2018-09-29 15:19:13 +08:00
|
|
|
};
|
2018-06-02 06:47:34 +08:00
|
|
|
}
|
2018-09-29 15:19:13 +08:00
|
|
|
};
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
void ClassicProgressTester(const char *imgname, const char *tessdatadir, const char *lang) {
|
2018-09-29 15:19:13 +08:00
|
|
|
using ::testing::_;
|
|
|
|
using ::testing::AllOf;
|
|
|
|
using ::testing::AtLeast;
|
|
|
|
using ::testing::DoAll;
|
|
|
|
using ::testing::Gt;
|
|
|
|
using ::testing::Le;
|
|
|
|
using ::testing::Return;
|
|
|
|
using ::testing::SaveArg;
|
|
|
|
|
2021-03-15 04:06:19 +08:00
|
|
|
auto api = std::make_unique<tesseract::TessBaseAPI>();
|
2021-03-13 05:06:34 +08:00
|
|
|
ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
|
2021-04-01 03:39:43 +08:00
|
|
|
Image image = pixRead(imgname);
|
2018-09-29 15:19:13 +08:00
|
|
|
ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
|
|
|
|
api->SetImage(image);
|
|
|
|
|
|
|
|
ClassicMockProgressSink progressSink;
|
|
|
|
|
|
|
|
int currentProgress = -1;
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_CALL(progressSink, classicProgress(AllOf(Gt<int &>(currentProgress), Le(100))))
|
2018-09-29 15:19:13 +08:00
|
|
|
.Times(AtLeast(5))
|
|
|
|
.WillRepeatedly(DoAll(SaveArg<0>(¤tProgress), Return(false)));
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_CALL(progressSink, cancel(_)).Times(AtLeast(5)).WillRepeatedly(Return(false));
|
2018-09-29 15:19:13 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(api->Recognize(&progressSink.monitor), false);
|
|
|
|
EXPECT_GE(currentProgress, 50) << "The reported progress did not reach 50%";
|
|
|
|
|
|
|
|
api->End();
|
2021-04-01 03:39:43 +08:00
|
|
|
image.destroy();
|
2018-09-29 15:19:13 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
void NewProgressTester(const char *imgname, const char *tessdatadir, const char *lang) {
|
2018-09-29 15:19:13 +08:00
|
|
|
using ::testing::_;
|
|
|
|
using ::testing::AllOf;
|
|
|
|
using ::testing::AtLeast;
|
|
|
|
using ::testing::DoAll;
|
|
|
|
using ::testing::Gt;
|
|
|
|
using ::testing::Le;
|
|
|
|
using ::testing::Return;
|
|
|
|
using ::testing::SaveArg;
|
|
|
|
|
2021-03-15 04:06:19 +08:00
|
|
|
auto api = std::make_unique<tesseract::TessBaseAPI>();
|
2021-03-13 05:06:34 +08:00
|
|
|
ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
|
2021-04-01 03:39:43 +08:00
|
|
|
Image image = pixRead(imgname);
|
2018-09-29 15:19:13 +08:00
|
|
|
ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
|
|
|
|
api->SetImage(image);
|
|
|
|
|
|
|
|
NewMockProgressSink progressSink;
|
|
|
|
|
|
|
|
int currentProgress = -1;
|
|
|
|
EXPECT_CALL(progressSink, classicProgress(_)).Times(0);
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_CALL(progressSink, progress(AllOf(Gt<int &>(currentProgress), Le(100))))
|
2018-09-29 15:19:13 +08:00
|
|
|
.Times(AtLeast(5))
|
|
|
|
.WillRepeatedly(DoAll(SaveArg<0>(¤tProgress), Return(false)));
|
2021-03-13 05:06:34 +08:00
|
|
|
EXPECT_CALL(progressSink, cancel(_)).Times(AtLeast(5)).WillRepeatedly(Return(false));
|
2018-09-29 15:19:13 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(api->Recognize(&progressSink.monitor), false);
|
|
|
|
EXPECT_GE(currentProgress, 50) << "The reported progress did not reach 50%";
|
|
|
|
|
|
|
|
api->End();
|
2021-04-01 03:39:43 +08:00
|
|
|
image.destroy();
|
2018-09-29 15:19:13 +08:00
|
|
|
}
|
|
|
|
|
2018-11-13 15:24:50 +08:00
|
|
|
TEST(QuickTest, ClassicProgressReporting) {
|
2021-03-13 05:06:34 +08:00
|
|
|
ClassicProgressTester(TESTING_DIR "/phototest.tif", TESSDATA_DIR "_fast", "eng");
|
2018-09-29 15:19:13 +08:00
|
|
|
}
|
|
|
|
|
2018-11-13 15:24:50 +08:00
|
|
|
TEST(QuickTest, NewProgressReporting) {
|
2018-09-29 15:19:13 +08:00
|
|
|
NewProgressTester(TESTING_DIR "/phototest.tif", TESSDATA_DIR "_fast", "eng");
|
|
|
|
}
|
2018-06-02 06:47:34 +08:00
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
} // namespace tesseract
|