progress_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.
  15. ///////////////////////////////////////////////////////////////////////
  16. // expects clone of tessdata_fast repo in ../../tessdata_fast
  17. #include "include_gunit.h"
  18. #include <tesseract/baseapi.h>
  19. #include <tesseract/ocrclass.h>
  20. #include "image.h"
  21. #include <allheaders.h>
  22. #include "gmock/gmock.h"
  23. #include <fstream>
  24. #include <iostream>
  25. #include <locale>
  26. #include <memory> // std::unique_ptr
  27. #include <string>
  28. #include <time.h>
  29. namespace tesseract {
  30. class QuickTest : public testing::Test {
  31. protected:
  32. void SetUp() override {
  33. start_time_ = time(nullptr);
  34. }
  35. void TearDown() override {
  36. const time_t end_time = time(nullptr);
  37. EXPECT_TRUE(end_time - start_time_ <= 25)
  38. << "The test took too long - " << ::testing::PrintToString(end_time - start_time_);
  39. }
  40. time_t start_time_;
  41. };
  42. class ClassicMockProgressSink {
  43. public:
  44. MOCK_METHOD1(classicProgress, bool(int));
  45. MOCK_METHOD1(cancel, bool(int));
  46. ETEXT_DESC monitor;
  47. ClassicMockProgressSink() {
  48. monitor.progress_callback = [](int progress, int, int, int, int) -> bool {
  49. return instance->classicProgress(progress);
  50. };
  51. monitor.cancel = [](void *ths, int words) -> bool {
  52. return ((ClassicMockProgressSink *)ths)->cancel(words);
  53. };
  54. monitor.cancel_this = this;
  55. instance = this;
  56. }
  57. static ClassicMockProgressSink *instance;
  58. };
  59. ClassicMockProgressSink *ClassicMockProgressSink::instance = nullptr;
  60. class NewMockProgressSink : public ClassicMockProgressSink {
  61. public:
  62. MOCK_METHOD1(progress, bool(int));
  63. NewMockProgressSink() {
  64. monitor.progress_callback2 = [](ETEXT_DESC *ths, int, int, int, int) -> bool {
  65. return ((NewMockProgressSink *)ths->cancel_this)->progress(ths->progress);
  66. };
  67. }
  68. };
  69. void ClassicProgressTester(const char *imgname, const char *tessdatadir, const char *lang) {
  70. using ::testing::_;
  71. using ::testing::AllOf;
  72. using ::testing::AtLeast;
  73. using ::testing::DoAll;
  74. using ::testing::Gt;
  75. using ::testing::Le;
  76. using ::testing::Return;
  77. using ::testing::SaveArg;
  78. auto api = std::make_unique<tesseract::TessBaseAPI>();
  79. ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
  80. Image image = pixRead(imgname);
  81. ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
  82. api->SetImage(image);
  83. ClassicMockProgressSink progressSink;
  84. int currentProgress = -1;
  85. EXPECT_CALL(progressSink, classicProgress(AllOf(Gt<int &>(currentProgress), Le(100))))
  86. .Times(AtLeast(5))
  87. .WillRepeatedly(DoAll(SaveArg<0>(&currentProgress), Return(false)));
  88. EXPECT_CALL(progressSink, cancel(_)).Times(AtLeast(5)).WillRepeatedly(Return(false));
  89. EXPECT_EQ(api->Recognize(&progressSink.monitor), false);
  90. EXPECT_GE(currentProgress, 50) << "The reported progress did not reach 50%";
  91. api->End();
  92. image.destroy();
  93. }
  94. void NewProgressTester(const char *imgname, const char *tessdatadir, const char *lang) {
  95. using ::testing::_;
  96. using ::testing::AllOf;
  97. using ::testing::AtLeast;
  98. using ::testing::DoAll;
  99. using ::testing::Gt;
  100. using ::testing::Le;
  101. using ::testing::Return;
  102. using ::testing::SaveArg;
  103. auto api = std::make_unique<tesseract::TessBaseAPI>();
  104. ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
  105. Image image = pixRead(imgname);
  106. ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
  107. api->SetImage(image);
  108. NewMockProgressSink progressSink;
  109. int currentProgress = -1;
  110. EXPECT_CALL(progressSink, classicProgress(_)).Times(0);
  111. EXPECT_CALL(progressSink, progress(AllOf(Gt<int &>(currentProgress), Le(100))))
  112. .Times(AtLeast(5))
  113. .WillRepeatedly(DoAll(SaveArg<0>(&currentProgress), Return(false)));
  114. EXPECT_CALL(progressSink, cancel(_)).Times(AtLeast(5)).WillRepeatedly(Return(false));
  115. EXPECT_EQ(api->Recognize(&progressSink.monitor), false);
  116. EXPECT_GE(currentProgress, 50) << "The reported progress did not reach 50%";
  117. api->End();
  118. image.destroy();
  119. }
  120. TEST(QuickTest, ClassicProgressReporting) {
  121. ClassicProgressTester(TESTING_DIR "/phototest.tif", TESSDATA_DIR "_fast", "eng");
  122. }
  123. TEST(QuickTest, NewProgressReporting) {
  124. NewProgressTester(TESTING_DIR "/phototest.tif", TESSDATA_DIR "_fast", "eng");
  125. }
  126. } // namespace tesseract