-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathocr_service_impl_test.cc
197 lines (172 loc) · 7.27 KB
/
ocr_service_impl_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <fcntl.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <base/bind.h>
#include <base/files/file_path.h>
#include <base/files/scoped_file.h>
#include <base/files/scoped_temp_dir.h>
#include <base/message_loop/message_pump_type.h>
#include <base/posix/eintr_wrapper.h>
#include <base/task/single_thread_task_executor.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <mojo/core/embedder/embedder.h>
#include <mojo/public/cpp/system/handle.h>
#include <mojo/public/cpp/bindings/pending_receiver.h>
#include <mojo/public/cpp/system/platform_handle.h>
#include <mojo/public/cpp/bindings/remote.h>
#include "ocr/mojo/ocr_service.mojom.h"
#include "ocr/ocr_service_impl.h"
namespace ocr {
namespace {
namespace mojo_ipc = chromeos::ocr::mojom;
// The relative path of the input test image.
constexpr char kTestImageRelativePath[] = "./test_images/phototest.tif";
// The name of the output pdf file.
constexpr char kOutputPdfFilename[] = "phototest.pdf";
mojo::ScopedHandle GetInputFileHandle(const std::string& input_filename) {
base::ScopedFD input_fd(HANDLE_EINTR(
open(input_filename.c_str(), O_RDONLY | O_NOFOLLOW | O_NOCTTY)));
return mojo::WrapPlatformHandle(mojo::PlatformHandle(std::move(input_fd)));
}
mojo::ScopedHandle GetOutputFileHandle(const std::string& output_filename) {
base::ScopedFD out_fd(
HANDLE_EINTR(open(output_filename.c_str(), O_CREAT | O_WRONLY, 0644)));
return mojo::WrapPlatformHandle(mojo::PlatformHandle(std::move(out_fd)));
}
} // namespace
class OcrServiceImplTest : public testing::Test {
protected:
OcrServiceImplTest() { mojo::core::Init(); }
void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
OcrServiceImpl* ocr_service() { return &ocr_service_impl_; }
const base::FilePath& temp_dir_path() const { return temp_dir_.GetPath(); }
private:
OcrServiceImpl ocr_service_impl_;
base::SingleThreadTaskExecutor executor_{base::MessagePumpType::IO};
base::ScopedTempDir temp_dir_;
};
// Tests OCR on a valid input image and with valid parameters.
TEST_F(OcrServiceImplTest, GenerateSearchablePdfFromImageSuccess) {
mojo::Remote<mojo_ipc::OpticalCharacterRecognitionService> remote;
EXPECT_FALSE(remote);
ocr_service()->AddReceiver(remote.BindNewPipeAndPassReceiver(),
false /* should_quit */);
ASSERT_TRUE(remote);
// Construct request.
const std::string input_image_filename =
base::FilePath(kTestImageRelativePath).value();
mojo::ScopedHandle input_fd_handle = GetInputFileHandle(input_image_filename);
const std::string output_filename =
temp_dir_path().Append(kOutputPdfFilename).value();
mojo::ScopedHandle output_fd_handle = GetOutputFileHandle(output_filename);
mojo_ipc::OcrConfigPtr ocr_config = mojo_ipc::OcrConfig::New();
mojo_ipc::PdfRendererConfigPtr pdf_renderer_config =
mojo_ipc::PdfRendererConfig::New();
// Perform OCR.
bool ocr_callback_done = false;
// Ensure that remote is bound.
remote->GenerateSearchablePdfFromImage(
std::move(input_fd_handle), std::move(output_fd_handle),
std::move(ocr_config), std::move(pdf_renderer_config),
base::BindOnce(
[](bool* ocr_callback_done,
const mojo_ipc::OpticalCharacterRecognitionServiceResponsePtr
response) {
EXPECT_EQ(response->result, mojo_ipc::OcrResultEnum::SUCCESS);
*ocr_callback_done = true;
},
&ocr_callback_done));
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(ocr_callback_done);
}
TEST_F(OcrServiceImplTest, OcrFailToLoadLanguage) {
// Construct request.
const std::string input_image_filename =
base::FilePath(kTestImageRelativePath).value();
mojo::ScopedHandle input_fd_handle = GetInputFileHandle(input_image_filename);
const std::string output_filename =
temp_dir_path().Append(kOutputPdfFilename).value();
mojo::ScopedHandle output_fd_handle = GetOutputFileHandle(output_filename);
mojo_ipc::OcrConfigPtr ocr_config = mojo_ipc::OcrConfig::New();
ocr_config->language = "deu";
mojo_ipc::PdfRendererConfigPtr pdf_renderer_config =
mojo_ipc::PdfRendererConfig::New();
// Perform OCR.
bool ocr_callback_done = false;
// Ensure that remote is bound.
ocr_service()->GenerateSearchablePdfFromImage(
std::move(input_fd_handle), std::move(output_fd_handle),
std::move(ocr_config), std::move(pdf_renderer_config),
base::BindOnce(
[](bool* ocr_callback_done,
const mojo_ipc::OpticalCharacterRecognitionServiceResponsePtr
response) {
EXPECT_EQ(response->result,
mojo_ipc::OcrResultEnum::LANGUAGE_NOT_SUPPORTED_ERROR);
*ocr_callback_done = true;
},
&ocr_callback_done));
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(ocr_callback_done);
}
TEST_F(OcrServiceImplTest, OcrInvalidInputImageFileHandle) {
// Construct request.
mojo::ScopedHandle input_fd_handle = mojo::ScopedHandle();
const std::string output_filename =
temp_dir_path().Append(kOutputPdfFilename).value();
mojo::ScopedHandle output_fd_handle = GetOutputFileHandle(output_filename);
mojo_ipc::OcrConfigPtr ocr_config = mojo_ipc::OcrConfig::New();
mojo_ipc::PdfRendererConfigPtr pdf_renderer_config =
mojo_ipc::PdfRendererConfig::New();
// Perform OCR.
bool ocr_callback_done = false;
// Ensure that remote is bound.
ocr_service()->GenerateSearchablePdfFromImage(
std::move(input_fd_handle), std::move(output_fd_handle),
std::move(ocr_config), std::move(pdf_renderer_config),
base::BindOnce(
[](bool* ocr_callback_done,
const mojo_ipc::OpticalCharacterRecognitionServiceResponsePtr
response) {
EXPECT_EQ(response->result,
mojo_ipc::OcrResultEnum::INPUT_FILE_ERROR);
*ocr_callback_done = true;
},
&ocr_callback_done));
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(ocr_callback_done);
}
TEST_F(OcrServiceImplTest, OcrInvalidOutputPdfFileHandle) {
// Construct request.
const std::string input_image_filename =
base::FilePath(kTestImageRelativePath).value();
mojo::ScopedHandle input_fd_handle = GetInputFileHandle(input_image_filename);
mojo::ScopedHandle output_fd_handle = mojo::ScopedHandle();
mojo_ipc::OcrConfigPtr ocr_config = mojo_ipc::OcrConfig::New();
mojo_ipc::PdfRendererConfigPtr pdf_renderer_config =
mojo_ipc::PdfRendererConfig::New();
// Perform OCR.
bool ocr_callback_done = false;
// Ensure that remote is bound.
ocr_service()->GenerateSearchablePdfFromImage(
std::move(input_fd_handle), std::move(output_fd_handle),
std::move(ocr_config), std::move(pdf_renderer_config),
base::BindOnce(
[](bool* ocr_callback_done,
const mojo_ipc::OpticalCharacterRecognitionServiceResponsePtr
response) {
EXPECT_EQ(response->result,
mojo_ipc::OcrResultEnum::OUTPUT_FILE_ERROR);
*ocr_callback_done = true;
},
&ocr_callback_done));
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(ocr_callback_done);
}
} // namespace ocr