blob: 8111945541d78890e6f371ba309b95ba376b12c0 [file] [log] [blame]
Lei Zhang1ac47eb2015-12-21 11:04:44 -08001// Copyright 2015 PDFium Authors. All rights reserved.
Tom Sepez96d13342015-01-16 14:59:26 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Wei Li091f7a02015-11-09 12:09:55 -08005#include "testing/embedder_test.h"
Tom Sepez96d13342015-01-16 14:59:26 -08006
Lei Zhanga98e3662018-02-07 20:28:35 +00007#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -08008#include <string>
9#include <utility>
Lei Zhangd69e0652019-04-13 00:39:35 +000010#include <vector>
Tom Sepez96d13342015-01-16 14:59:26 -080011
Lei Zhangd145e4b2018-10-12 18:54:31 +000012#include "core/fdrm/fx_crypt.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000013#include "public/cpp/fpdf_scopers.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080014#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080015#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080016#include "public/fpdf_text.h"
17#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080018#include "testing/gmock/include/gmock/gmock.h"
Lei Zhangd50bdff2019-02-05 19:42:33 +000019#include "testing/test_loader.h"
Henrique Nakashimaf956bad2018-08-16 16:41:42 +000020#include "testing/utils/bitmap_saver.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000021#include "testing/utils/file_util.h"
Lei Zhang4c64e962019-02-05 19:24:12 +000022#include "testing/utils/hash.h"
Wei Li091f7a02015-11-09 12:09:55 -080023#include "testing/utils/path_service.h"
Tom Sepez25f33d02021-01-29 01:58:51 +000024#include "third_party/base/check.h"
Lei Zhang87e3d7a2021-06-17 19:40:28 +000025#include "third_party/base/containers/contains.h"
Lei Zhang30632072020-10-02 01:57:54 +000026#include "third_party/base/notreached.h"
Tom Sepezea03a7b2022-02-24 00:30:14 +000027#include "third_party/base/numerics/safe_conversions.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070028
29#ifdef PDF_ENABLE_V8
Tom Sepezeaf9d212020-07-22 16:24:00 +000030#include "testing/v8_test_environment.h"
Lei Zhang8241df72015-11-06 14:38:48 -080031#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050032#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070033#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080034
Tom Sepez96d13342015-01-16 14:59:26 -080035namespace {
thestigc08cd7a2016-06-27 09:47:59 -070036
Jane Liu28fb7ba2017-08-02 21:45:57 -040037int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
Tom Sepez6dc12402020-01-07 21:02:17 +000038 return EmbedderTest::BytesPerPixelForFormat(FPDFBitmap_GetFormat(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -040039}
40
Lei Zhangf997abe2022-01-12 19:14:23 +000041#if BUILDFLAG(IS_WIN)
Lei Zhang614f1a02019-05-30 00:03:53 +000042int CALLBACK GetRecordProc(HDC hdc,
43 HANDLETABLE* handle_table,
44 const ENHMETARECORD* record,
45 int objects_count,
46 LPARAM param) {
47 auto& records = *reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param);
48 records.push_back(record);
49 return 1;
50}
Lei Zhangf997abe2022-01-12 19:14:23 +000051#endif // BUILDFLAG(IS_WIN)
Lei Zhang614f1a02019-05-30 00:03:53 +000052
Tom Sepezb2ce4d32022-05-06 17:09:33 +000053// These "jump" into the delegate to do actual testing.
54void UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info, int type) {
55 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
56 delegate->UnsupportedHandler(type);
57}
58
59int AlertTrampoline(IPDF_JSPLATFORM* platform,
60 FPDF_WIDESTRING message,
61 FPDF_WIDESTRING title,
62 int type,
63 int icon) {
64 auto* delegate = static_cast<EmbedderTest*>(platform)->GetDelegate();
65 return delegate->Alert(message, title, type, icon);
66}
67
68int SetTimerTrampoline(FPDF_FORMFILLINFO* info, int msecs, TimerCallback fn) {
69 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
70 return delegate->SetTimer(msecs, fn);
71}
72
73void KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
74 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
75 return delegate->KillTimer(id);
76}
77
78FPDF_PAGE GetPageTrampoline(FPDF_FORMFILLINFO* info,
79 FPDF_DOCUMENT document,
80 int page_index) {
81 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
82 return delegate->GetPage(info, document, page_index);
83}
84
85void DoURIActionTrampoline(FPDF_FORMFILLINFO* info, FPDF_BYTESTRING uri) {
86 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
87 return delegate->DoURIAction(uri);
88}
89
90void DoGoToActionTrampoline(FPDF_FORMFILLINFO* info,
91 int page_index,
92 int zoom_mode,
93 float* pos_array,
94 int array_size) {
95 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
96 return delegate->DoGoToAction(info, page_index, zoom_mode, pos_array,
97 array_size);
98}
99
100void OnFocusChangeTrampoline(FPDF_FORMFILLINFO* info,
101 FPDF_ANNOTATION annot,
102 int page_index) {
103 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
104 return delegate->OnFocusChange(info, annot, page_index);
105}
106
107void DoURIActionWithKeyboardModifierTrampoline(FPDF_FORMFILLINFO* info,
108 FPDF_BYTESTRING uri,
109 int modifiers) {
110 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
111 return delegate->DoURIActionWithKeyboardModifier(info, uri, modifiers);
112}
113
114// These do nothing (but must return a reasonable default value).
115void InvalidateStub(FPDF_FORMFILLINFO* pThis,
116 FPDF_PAGE page,
117 double left,
118 double top,
119 double right,
120 double bottom) {}
121
122void OutputSelectedRectStub(FPDF_FORMFILLINFO* pThis,
123 FPDF_PAGE page,
124 double left,
125 double top,
126 double right,
127 double bottom) {}
128
129void SetCursorStub(FPDF_FORMFILLINFO* pThis, int nCursorType) {}
130
131FPDF_SYSTEMTIME GetLocalTimeStub(FPDF_FORMFILLINFO* pThis) {
132 return {122, 11, 6, 28, 12, 59, 59, 500};
133}
134
135void OnChangeStub(FPDF_FORMFILLINFO* pThis) {}
136
137FPDF_PAGE GetCurrentPageStub(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document) {
138 return GetPageTrampoline(pThis, document, 0);
139}
140
141int GetRotationStub(FPDF_FORMFILLINFO* pThis, FPDF_PAGE page) {
142 return 0;
143}
144
145void ExecuteNamedActionStub(FPDF_FORMFILLINFO* pThis, FPDF_BYTESTRING name) {}
146
147void SetTextFieldFocusStub(FPDF_FORMFILLINFO* pThis,
148 FPDF_WIDESTRING value,
149 FPDF_DWORD valueLen,
150 FPDF_BOOL is_focus) {}
151
152#ifdef PDF_ENABLE_XFA
153void DisplayCaretStub(FPDF_FORMFILLINFO* pThis,
154 FPDF_PAGE page,
155 FPDF_BOOL bVisible,
156 double left,
157 double top,
158 double right,
159 double bottom) {}
160
161int GetCurrentPageIndexStub(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document) {
162 return 0;
163}
164
165void SetCurrentPageStub(FPDF_FORMFILLINFO* pThis,
166 FPDF_DOCUMENT document,
167 int iCurPage) {}
168
169void GotoURLStub(FPDF_FORMFILLINFO* pThis,
170 FPDF_DOCUMENT document,
171 FPDF_WIDESTRING wsURL) {}
172
173void GetPageViewRectStub(FPDF_FORMFILLINFO* pThis,
174 FPDF_PAGE page,
175 double* left,
176 double* top,
177 double* right,
178 double* bottom) {
179 *left = 0.0;
180 *top = 0.0;
181 *right = 512.0;
182 *bottom = 512.0;
183}
184
185void PageEventStub(FPDF_FORMFILLINFO* pThis,
186 int page_count,
187 FPDF_DWORD event_type) {}
188
189FPDF_BOOL PopupMenuStub(FPDF_FORMFILLINFO* pThis,
190 FPDF_PAGE page,
191 FPDF_WIDGET hWidget,
192 int menuFlag,
193 float x,
194 float y) {
195 return true;
196}
197
198FPDF_FILEHANDLER* OpenFileStub(FPDF_FORMFILLINFO* pThis,
199 int fileFlag,
200 FPDF_WIDESTRING wsURL,
201 const char* mode) {
202 return nullptr;
203}
204
205void EmailToStub(FPDF_FORMFILLINFO* pThis,
206 FPDF_FILEHANDLER* fileHandler,
207 FPDF_WIDESTRING pTo,
208 FPDF_WIDESTRING pSubject,
209 FPDF_WIDESTRING pCC,
210 FPDF_WIDESTRING pBcc,
211 FPDF_WIDESTRING pMsg) {}
212
213void UploadToStub(FPDF_FORMFILLINFO* pThis,
214 FPDF_FILEHANDLER* fileHandler,
215 int fileFlag,
216 FPDF_WIDESTRING uploadTo) {}
217
218int GetPlatformStub(FPDF_FORMFILLINFO* pThis, void* platform, int length) {
219 return 0;
220}
221
222int GetLanguageStub(FPDF_FORMFILLINFO* pThis, void* language, int length) {
223 return 0;
224}
225
226FPDF_FILEHANDLER* DownloadFromURLStub(FPDF_FORMFILLINFO* pThis,
227 FPDF_WIDESTRING URL) {
228 return nullptr;
229}
230
231FPDF_BOOL PostRequestURLStub(FPDF_FORMFILLINFO* pThis,
232 FPDF_WIDESTRING wsURL,
233 FPDF_WIDESTRING wsData,
234 FPDF_WIDESTRING wsContentType,
235 FPDF_WIDESTRING wsEncode,
236 FPDF_WIDESTRING wsHeader,
237 FPDF_BSTR* response) {
238 return true;
239}
240
241FPDF_BOOL PutRequestURLStub(FPDF_FORMFILLINFO* pThis,
242 FPDF_WIDESTRING wsURL,
243 FPDF_WIDESTRING wsData,
244 FPDF_WIDESTRING wsEncode) {
245 return true;
246}
247#endif // PDF_ENABLE_XFA
248
thestigbcd3e532016-11-21 13:37:28 -0800249} // namespace
250
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251EmbedderTest::EmbedderTest()
Tom Sepez7df04832020-05-18 22:09:31 +0000252 : default_delegate_(std::make_unique<EmbedderTest::Delegate>()),
Lei Zhang0729be22018-02-05 21:13:51 +0000253 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400254 FPDF_FILEWRITE::version = 1;
255 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -0800256}
Tom Sepez96d13342015-01-16 14:59:26 -0800257
Lei Zhangea208512019-12-18 00:42:11 +0000258EmbedderTest::~EmbedderTest() = default;
Tom Sepezf288bb12015-11-20 12:12:46 -0800259
260void EmbedderTest::SetUp() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
262 memset(info, 0, sizeof(UNSUPPORT_INFO));
263 info->version = 1;
264 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
265 FSDK_SetUnSpObjProcessHandler(info);
266}
Tom Sepez96d13342015-01-16 14:59:26 -0800267
268void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +0000269 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
Tom Sepez25f33d02021-01-29 01:58:51 +0000270 // possible. This can fail when an DCHECK test fails in a test case.
Lei Zhang75c81712018-02-08 17:22:39 +0000271 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +0000272 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000273 if (document())
Tom Sepez38f725f2019-12-04 00:26:34 +0000274 CloseDocument();
Tom Sepez96d13342015-01-16 14:59:26 -0800275}
276
Lei Zhangb8b4a042021-11-05 00:04:25 +0000277void EmbedderTest::CreateEmptyDocument() {
Lei Zhang935fb862021-11-05 00:15:05 +0000278 CreateEmptyDocumentWithoutFormFillEnvironment();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000279 form_handle_.reset(SetupFormFillEnvironment(
280 document(), JavaScriptOption::kEnableJavaScript));
Tom Sepezd483eb42016-01-06 10:03:59 -0800281}
282
Lei Zhang935fb862021-11-05 00:15:05 +0000283void EmbedderTest::CreateEmptyDocumentWithoutFormFillEnvironment() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000284 document_.reset(FPDF_CreateNewDocument());
Lei Zhang935fb862021-11-05 00:15:05 +0000285 DCHECK(document_);
286}
287
Lei Zhang208eecf2017-12-20 19:40:50 +0000288bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000289 return OpenDocumentWithOptions(filename, nullptr,
290 LinearizeOption::kDefaultLinearize,
291 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000292}
293
294bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000295 return OpenDocumentWithOptions(filename, nullptr,
296 LinearizeOption::kMustLinearize,
297 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000298}
299
300bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
301 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000302 return OpenDocumentWithOptions(filename, password,
303 LinearizeOption::kDefaultLinearize,
304 JavaScriptOption::kEnableJavaScript);
305}
306
307bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
308 return OpenDocumentWithOptions(filename, nullptr,
309 LinearizeOption::kDefaultLinearize,
310 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000311}
312
313bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
314 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000315 LinearizeOption linearize_option,
316 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800317 std::string file_path;
318 if (!PathService::GetTestFilePath(filename, &file_path))
319 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000320
Wei Li091f7a02015-11-09 12:09:55 -0800321 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400322 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800323 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800324
thestig29ce9232016-06-22 07:03:23 -0700325 EXPECT_TRUE(!loader_);
Tom Sepez7df04832020-05-18 22:09:31 +0000326 loader_ = std::make_unique<TestLoader>(
Lei Zhangb3be4a12019-02-05 22:11:07 +0000327 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000328
329 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800330 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700331 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000332 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000333
Tom Sepez7df04832020-05-18 22:09:31 +0000334 fake_file_access_ = std::make_unique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000335 return OpenDocumentHelper(password, linearize_option, javascript_option,
336 fake_file_access_.get(), &document_, &avail_,
337 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400338}
Tom Sepez96d13342015-01-16 14:59:26 -0800339
Nicolas Pena56fc9722017-07-13 16:31:34 -0400340bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000341 LinearizeOption linearize_option,
342 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300343 FakeFileAccess* network_simulator,
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000344 ScopedFPDFDocument* document,
Lei Zhang52d21ac2021-11-04 22:40:15 +0000345 ScopedFPDFAvail* avail,
Lei Zhangdba065e2021-11-04 23:09:15 +0000346 ScopedFPDFFormHandle* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300347 network_simulator->AddSegment(0, 1024);
348 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000349 avail->reset(FPDFAvail_Create(network_simulator->GetFileAvail(),
350 network_simulator->GetFileAccess()));
351 FPDF_AVAIL avail_ptr = avail->get();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000352 FPDF_DOCUMENT document_ptr = nullptr;
Lei Zhang52d21ac2021-11-04 22:40:15 +0000353 if (FPDFAvail_IsLinearized(avail_ptr) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300354 int32_t nRet = PDF_DATA_NOTAVAIL;
355 while (nRet == PDF_DATA_NOTAVAIL) {
356 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000357 nRet = FPDFAvail_IsDocAvail(avail_ptr,
358 network_simulator->GetDownloadHints());
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300359 }
360 if (nRet == PDF_DATA_ERROR)
361 return false;
362
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000363 document->reset(FPDFAvail_GetDocument(avail_ptr, password));
364 document_ptr = document->get();
365 if (!document_ptr)
Jun Fangdf7f3662015-11-10 18:29:18 +0800366 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400367
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300368 nRet = PDF_DATA_NOTAVAIL;
369 while (nRet == PDF_DATA_NOTAVAIL) {
370 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000371 nRet = FPDFAvail_IsFormAvail(avail_ptr,
372 network_simulator->GetDownloadHints());
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300373 }
374 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800375 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400376
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000377 int page_count = FPDF_GetPageCount(document_ptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800378 for (int i = 0; i < page_count; ++i) {
379 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300380 while (nRet == PDF_DATA_NOTAVAIL) {
381 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000382 nRet = FPDFAvail_IsPageAvail(avail_ptr, i,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300383 network_simulator->GetDownloadHints());
384 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400385 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800386 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800387 }
388 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000389 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800390 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300391 network_simulator->SetWholeFileAvailable();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000392 document->reset(
393 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password));
394 document_ptr = document->get();
395 if (!document_ptr)
Jun Fangdf7f3662015-11-10 18:29:18 +0800396 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800397 }
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000398 form_handle->reset(SetupFormFillEnvironment(document_ptr, javascript_option));
Tom Sepez0784c732018-04-23 18:02:57 +0000399
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000400 int doc_type = FPDF_GetFormType(document_ptr);
Ryan Harrison854d71c2017-10-18 12:28:14 -0400401 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000402 FPDF_LoadXFA(document_ptr);
Tom Sepez0784c732018-04-23 18:02:57 +0000403
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000404 (void)FPDF_GetDocPermissions(document_ptr);
Tom Sepezd483eb42016-01-06 10:03:59 -0800405 return true;
406}
Tom Sepez96d13342015-01-16 14:59:26 -0800407
Tom Sepez38f725f2019-12-04 00:26:34 +0000408void EmbedderTest::CloseDocument() {
Lei Zhangdba065e2021-11-04 23:09:15 +0000409 FORM_DoDocumentAAction(form_handle(), FPDFDOC_AACTION_WC);
410 form_handle_.reset();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000411 document_.reset();
Lei Zhang55b1f462021-11-05 01:44:46 +0000412 avail_.reset();
413 fake_file_access_.reset();
414 memset(&file_access_, 0, sizeof(file_access_));
415 loader_.reset();
416 file_contents_.reset();
Tom Sepez38f725f2019-12-04 00:26:34 +0000417}
418
Tom Sepez0784c732018-04-23 18:02:57 +0000419FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
420 FPDF_DOCUMENT doc,
421 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800422 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400423 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Tom Sepez72f520c2020-08-24 23:43:46 +0000424 platform->version = 3;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800425 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800426
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800427 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
428 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Aayush Dhirc63913e2020-02-12 09:45:34 +0000429 formfillinfo->version = form_fill_info_version_;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000430 formfillinfo->FFI_Invalidate = InvalidateStub;
431 formfillinfo->FFI_OutputSelectedRect = OutputSelectedRectStub;
432 formfillinfo->FFI_SetCursor = SetCursorStub;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700433 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
434 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000435 formfillinfo->FFI_GetLocalTime = GetLocalTimeStub;
436 formfillinfo->FFI_OnChange = OnChangeStub;
Tom Sepez396e8722015-09-09 10:16:08 -0700437 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000438 formfillinfo->FFI_GetCurrentPage = GetCurrentPageStub;
439 formfillinfo->FFI_GetRotation = GetRotationStub;
440 formfillinfo->FFI_ExecuteNamedAction = ExecuteNamedActionStub;
441 formfillinfo->FFI_SetTextFieldFocus = SetTextFieldFocusStub;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000442 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000443 formfillinfo->FFI_DoGoToAction = DoGoToActionTrampoline;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000444#ifdef PDF_ENABLE_XFA
445 formfillinfo->FFI_DisplayCaret = DisplayCaretStub;
446 formfillinfo->FFI_GetCurrentPageIndex = GetCurrentPageIndexStub;
447 formfillinfo->FFI_SetCurrentPage = SetCurrentPageStub;
448 formfillinfo->FFI_GotoURL = GotoURLStub;
449 formfillinfo->FFI_GetPageViewRect = GetPageViewRectStub;
450 formfillinfo->FFI_PageEvent = PageEventStub;
451 formfillinfo->FFI_PopupMenu = PopupMenuStub;
452 formfillinfo->FFI_OpenFile = OpenFileStub;
453 formfillinfo->FFI_EmailTo = EmailToStub;
454 formfillinfo->FFI_UploadTo = UploadToStub;
455 formfillinfo->FFI_GetPlatform = GetPlatformStub;
456 formfillinfo->FFI_GetLanguage = GetLanguageStub;
457 formfillinfo->FFI_DownloadFromURL = DownloadFromURLStub;
458 formfillinfo->FFI_PostRequestURL = PostRequestURLStub;
459 formfillinfo->FFI_PutRequestURL = PutRequestURLStub;
460#endif // PDF_ENABLE_XFA
Aayush Dhirc63913e2020-02-12 09:45:34 +0000461 formfillinfo->FFI_OnFocusChange = OnFocusChangeTrampoline;
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000462 formfillinfo->FFI_DoURIActionWithKeyboardModifier =
463 DoURIActionWithKeyboardModifierTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000464
Tom Sepez0784c732018-04-23 18:02:57 +0000465 if (javascript_option == JavaScriptOption::kEnableJavaScript)
466 formfillinfo->m_pJsPlatform = platform;
467
Nicolas Pena3ff54002017-07-05 11:55:35 -0400468 FPDF_FORMHANDLE form_handle =
469 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Tom Sepez92f92222020-01-07 22:16:07 +0000470 SetInitialFormFieldHighlight(form_handle);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400471 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800472}
473
Tom Sepezda8189e2015-01-30 14:41:50 -0800474void EmbedderTest::DoOpenActions() {
Lei Zhangdba065e2021-11-04 23:09:15 +0000475 DCHECK(form_handle());
476 FORM_DoDocumentJSAction(form_handle());
477 FORM_DoDocumentOpenAction(form_handle());
Tom Sepez96d13342015-01-16 14:59:26 -0800478}
479
480int EmbedderTest::GetFirstPageNum() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000481 int first_page = FPDFAvail_GetFirstPageNum(document());
Lei Zhang52d21ac2021-11-04 22:40:15 +0000482 (void)FPDFAvail_IsPageAvail(avail(), first_page,
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000483 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800484 return first_page;
485}
486
487int EmbedderTest::GetPageCount() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000488 int page_count = FPDF_GetPageCount(document());
Nicolas Pena3ff54002017-07-05 11:55:35 -0400489 for (int i = 0; i < page_count; ++i)
Lei Zhang52d21ac2021-11-04 22:40:15 +0000490 (void)FPDFAvail_IsPageAvail(avail(), i,
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000491 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800492 return page_count;
493}
494
Tom Sepezda8189e2015-01-30 14:41:50 -0800495FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000496 return LoadPageCommon(page_number, true);
497}
498
499FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
500 return LoadPageCommon(page_number, false);
501}
502
503FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000504 DCHECK(form_handle());
Tom Sepez25f33d02021-01-29 01:58:51 +0000505 DCHECK(page_number >= 0);
506 DCHECK(!pdfium::Contains(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700507
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000508 FPDF_PAGE page = FPDF_LoadPage(document(), page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400509 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800510 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400511
Tom Sepez507d0192018-11-07 16:37:51 +0000512 if (do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000513 FORM_OnAfterLoadPage(page, form_handle());
514 FORM_DoPageAAction(page, form_handle(), FPDFPAGE_AACTION_OPEN);
Tom Sepez507d0192018-11-07 16:37:51 +0000515 }
weili0dadcc62016-08-23 21:10:57 -0700516 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700517 return page;
518}
519
Tom Sepezda8189e2015-01-30 14:41:50 -0800520void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000521 UnloadPageCommon(page, true);
522}
Lei Zhang75c81712018-02-08 17:22:39 +0000523
Tom Sepez507d0192018-11-07 16:37:51 +0000524void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
525 UnloadPageCommon(page, false);
526}
527
528void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000529 DCHECK(form_handle());
Lei Zhang75c81712018-02-08 17:22:39 +0000530 int page_number = GetPageNumberForLoadedPage(page);
531 if (page_number < 0) {
532 NOTREACHED();
533 return;
534 }
Tom Sepez507d0192018-11-07 16:37:51 +0000535 if (do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000536 FORM_DoPageAAction(page, form_handle(), FPDFPAGE_AACTION_CLOSE);
537 FORM_OnBeforeClosePage(page, form_handle());
Tom Sepez507d0192018-11-07 16:37:51 +0000538 }
Tom Sepez96d13342015-01-16 14:59:26 -0800539 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000540 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800541}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800542
Tom Sepez92f92222020-01-07 22:16:07 +0000543void EmbedderTest::SetInitialFormFieldHighlight(FPDF_FORMHANDLE form) {
544 FPDF_SetFormFieldHighlightColor(form, FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD);
545 FPDF_SetFormFieldHighlightAlpha(form, 100);
546}
547
Tom Sepeze08d2b12018-04-25 18:49:32 +0000548ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000549 return RenderLoadedPageWithFlags(page, 0);
550}
551
Tom Sepeze08d2b12018-04-25 18:49:32 +0000552ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
553 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000554 if (GetPageNumberForLoadedPage(page) < 0) {
555 NOTREACHED();
556 return nullptr;
557 }
Lei Zhangdba065e2021-11-04 23:09:15 +0000558 return RenderPageWithFlags(page, form_handle(), flags);
Lei Zhanga98e3662018-02-07 20:28:35 +0000559}
560
Tom Sepeze08d2b12018-04-25 18:49:32 +0000561ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000562 return RenderSavedPageWithFlags(page, 0);
563}
564
Tom Sepeze08d2b12018-04-25 18:49:32 +0000565ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
566 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000567 if (GetPageNumberForSavedPage(page) < 0) {
568 NOTREACHED();
569 return nullptr;
570 }
Lei Zhangdba065e2021-11-04 23:09:15 +0000571 return RenderPageWithFlags(page, saved_form_handle(), flags);
Lei Zhanga98e3662018-02-07 20:28:35 +0000572}
573
574// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000575ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
576 FPDF_FORMHANDLE handle,
577 int flags) {
Lei Zhangdfa075b2019-12-05 21:52:43 +0000578 int width = static_cast<int>(FPDF_GetPageWidthF(page));
579 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhanga98e3662018-02-07 20:28:35 +0000580 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000581 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000582 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
583 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
584 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
585 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
586 return bitmap;
587}
588
Lei Zhang30ff2532019-01-31 21:37:55 +0000589// static
590ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
591 return RenderPageWithFlags(page, nullptr, 0);
592}
593
Lei Zhangf997abe2022-01-12 19:14:23 +0000594#if BUILDFLAG(IS_WIN)
Lei Zhangd69e0652019-04-13 00:39:35 +0000595// static
596std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
597 int flags) {
598 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
599
Lei Zhangdfa075b2019-12-05 21:52:43 +0000600 int width = static_cast<int>(FPDF_GetPageWidthF(page));
601 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhangd69e0652019-04-13 00:39:35 +0000602 HRGN rgn = CreateRectRgn(0, 0, width, height);
603 SelectClipRgn(dc, rgn);
604 DeleteObject(rgn);
605
606 SelectObject(dc, GetStockObject(NULL_PEN));
607 SelectObject(dc, GetStockObject(WHITE_BRUSH));
608 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
609 Rectangle(dc, 0, 0, width + 1, height + 1);
610
611 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
612
613 HENHMETAFILE emf = CloseEnhMetaFile(dc);
Tom Sepezea03a7b2022-02-24 00:30:14 +0000614 UINT size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
Lei Zhangd69e0652019-04-13 00:39:35 +0000615 std::vector<uint8_t> buffer(size_in_bytes);
616 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
617 DeleteEnhMetaFile(emf);
618 return buffer;
619}
Lei Zhang614f1a02019-05-30 00:03:53 +0000620
621// static
622std::string EmbedderTest::GetPostScriptFromEmf(
Tom Sepez87b9e3a2019-11-20 23:45:43 +0000623 pdfium::span<const uint8_t> emf_data) {
Lei Zhang614f1a02019-05-30 00:03:53 +0000624 // This comes from Emf::InitFromData() in Chromium.
Tom Sepezea03a7b2022-02-24 00:30:14 +0000625 HENHMETAFILE emf = SetEnhMetaFileBits(
626 pdfium::base::checked_cast<UINT>(emf_data.size()), emf_data.data());
Lei Zhang614f1a02019-05-30 00:03:53 +0000627 if (!emf)
628 return std::string();
629
630 // This comes from Emf::Enumerator::Enumerator() in Chromium.
631 std::vector<const ENHMETARECORD*> records;
632 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
633 DeleteEnhMetaFile(emf);
634 return std::string();
635 }
636
637 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
638 std::string ps_data;
639 for (const auto* record : records) {
640 if (record->iType != EMR_GDICOMMENT)
641 continue;
642
643 // PostScript data is encapsulated inside EMF comment records.
644 // The first two bytes of the comment indicate the string length. The rest
645 // is the actual string data.
646 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
647 const char* data = reinterpret_cast<const char*>(comment->Data);
648 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
649 data += 2;
650 ps_data.append(data, size);
651 }
652 DeleteEnhMetaFile(emf);
653 return ps_data;
654}
Lei Zhangf997abe2022-01-12 19:14:23 +0000655#endif // BUILDFLAG(IS_WIN)
Lei Zhangd69e0652019-04-13 00:39:35 +0000656
Lei Zhang0b494052019-01-31 21:41:15 +0000657FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
658 return OpenSavedDocumentWithPassword(nullptr);
659}
660
Tom Sepez6dc12402020-01-07 21:02:17 +0000661// static
662int EmbedderTest::BytesPerPixelForFormat(int format) {
663 switch (format) {
664 case FPDFBitmap_Gray:
665 return 1;
666 case FPDFBitmap_BGR:
667 return 3;
668 case FPDFBitmap_BGRx:
669 case FPDFBitmap_BGRA:
670 return 4;
671 default:
672 NOTREACHED();
673 return 0;
674 }
675}
676
Lei Zhang0b494052019-01-31 21:41:15 +0000677FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
678 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300679 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Tom Sepezea03a7b2022-02-24 00:30:14 +0000680 saved_file_access_.m_FileLen =
681 pdfium::base::checked_cast<unsigned long>(data_string_.size());
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300682 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000683 // Copy data to prevent clearing it before saved document close.
684 saved_document_file_data_ = data_string_;
685 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400686
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300687 saved_fake_file_access_ =
Tom Sepez7df04832020-05-18 22:09:31 +0000688 std::make_unique<FakeFileAccess>(&saved_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300689
Tom Sepez0784c732018-04-23 18:02:57 +0000690 EXPECT_TRUE(OpenDocumentHelper(
691 password, LinearizeOption::kDefaultLinearize,
692 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
693 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000694 return saved_document();
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400695}
696
697void EmbedderTest::CloseSavedDocument() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000698 DCHECK(saved_document());
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400699
Lei Zhangdba065e2021-11-04 23:09:15 +0000700 saved_form_handle_.reset();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000701 saved_document_.reset();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000702 saved_avail_.reset();
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400703}
704
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000705FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000706 DCHECK(saved_form_handle());
Tom Sepez25f33d02021-01-29 01:58:51 +0000707 DCHECK(page_number >= 0);
708 DCHECK(!pdfium::Contains(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400709
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000710 FPDF_PAGE page = FPDF_LoadPage(saved_document(), page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000711 if (!page)
712 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400713
Lei Zhangdba065e2021-11-04 23:09:15 +0000714 FORM_OnAfterLoadPage(page, saved_form_handle());
715 FORM_DoPageAAction(page, saved_form_handle(), FPDFPAGE_AACTION_OPEN);
Lei Zhang9f72c452018-02-08 21:49:54 +0000716 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000717 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400718}
719
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000720void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000721 DCHECK(saved_form_handle());
Lei Zhang9f72c452018-02-08 21:49:54 +0000722
723 int page_number = GetPageNumberForSavedPage(page);
724 if (page_number < 0) {
725 NOTREACHED();
726 return;
727 }
728
Lei Zhangdba065e2021-11-04 23:09:15 +0000729 FORM_DoPageAAction(page, saved_form_handle(), FPDFPAGE_AACTION_CLOSE);
730 FORM_OnBeforeClosePage(page, saved_form_handle());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000731 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000732
733 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400734}
735
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000736void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
737 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400738 int height,
739 const char* md5) {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000740 DCHECK(saved_document());
Tom Sepez25f33d02021-01-29 01:58:51 +0000741 DCHECK(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400742
Tom Sepeze08d2b12018-04-25 18:49:32 +0000743 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000744 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400745}
746
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400747void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000748 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000749 FPDF_PAGE page = LoadSavedPage(0);
750 VerifySavedRendering(page, width, height, md5);
751 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400752 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400753}
754
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300755void EmbedderTest::SetWholeFileAvailable() {
Tom Sepez25f33d02021-01-29 01:58:51 +0000756 DCHECK(fake_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300757 fake_file_access_->SetWholeFileAvailable();
758}
759
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000760void EmbedderTest::SetDocumentFromAvail() {
761 document_.reset(FPDFAvail_GetDocument(avail(), nullptr));
762}
763
Lei Zhang52d21ac2021-11-04 22:40:15 +0000764void EmbedderTest::CreateAvail(FX_FILEAVAIL* file_avail,
765 FPDF_FILEACCESS* file) {
766 avail_.reset(FPDFAvail_Create(file_avail, file));
767}
768
weili0dadcc62016-08-23 21:10:57 -0700769FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700770 FPDF_DOCUMENT document,
771 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700772 EmbedderTest* test = static_cast<EmbedderTest*>(info);
773 auto it = test->page_map_.find(page_index);
774 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700775}
776
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800777// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000778std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Lei Zhangbf980142019-12-20 01:05:42 +0000779 int stride = FPDFBitmap_GetStride(bitmap);
780 int usable_bytes_per_row =
781 GetBitmapBytesPerPixel(bitmap) * FPDFBitmap_GetWidth(bitmap);
782 int height = FPDFBitmap_GetHeight(bitmap);
783 auto span = pdfium::make_span(
784 static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)), stride * height);
785
786 CRYPT_md5_context context = CRYPT_MD5Start();
787 for (int i = 0; i < height; ++i)
788 CRYPT_MD5Update(&context, span.subspan(i * stride, usable_bytes_per_row));
Dan Sinclair957480c2017-06-13 15:21:14 -0400789 uint8_t digest[16];
Lei Zhangbf980142019-12-20 01:05:42 +0000790 CRYPT_MD5Finish(&context, digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400791 return CryptToBase16(digest);
792}
793
Henrique Nakashimadb269572018-01-16 19:02:15 +0000794#ifndef NDEBUG
795// static
796void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
797 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000798 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000799}
800#endif
801
thestigbcd3e532016-11-21 13:37:28 -0800802// static
803void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
804 int expected_width,
805 int expected_height,
806 const char* expected_md5sum) {
807 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
808 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400809
810 // The expected stride is calculated using the same formula as in
811 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
812 const int expected_stride =
813 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800814 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
815
816 if (!expected_md5sum)
817 return;
818
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000819 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800820}
821
Nicolas Pena3ff54002017-07-05 11:55:35 -0400822// static
823int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
824 const void* data,
825 unsigned long size) {
826 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000827
Lei Zhang0729be22018-02-05 21:13:51 +0000828 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000829
830 if (pThis->filestream_.is_open())
831 pThis->filestream_.write(static_cast<const char*>(data), size);
832
Nicolas Pena3ff54002017-07-05 11:55:35 -0400833 return 1;
834}
835
836// static
837int EmbedderTest::GetBlockFromString(void* param,
838 unsigned long pos,
839 unsigned char* buf,
840 unsigned long size) {
841 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000842 if (!new_file || pos + size < pos) {
843 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400844 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000845 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400846
Lei Zhangcef91f12019-01-08 21:32:51 +0000847 if (pos + size > new_file->size()) {
848 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400849 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000850 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400851
852 memcpy(buf, new_file->data() + pos, size);
853 return 1;
854}
Lei Zhang75c81712018-02-08 17:22:39 +0000855
Lei Zhang9f72c452018-02-08 21:49:54 +0000856// static
857int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
858 FPDF_PAGE page) {
859 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000860 if (it.second == page) {
861 int page_number = it.first;
Tom Sepez25f33d02021-01-29 01:58:51 +0000862 DCHECK(page_number >= 0);
Lei Zhang75c81712018-02-08 17:22:39 +0000863 return page_number;
864 }
865 }
866 return -1;
867}
Lei Zhang9f72c452018-02-08 21:49:54 +0000868
869int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
870 return GetPageNumberForPage(page_map_, page);
871}
872
873int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
874 return GetPageNumberForPage(saved_page_map_, page);
875}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000876
Lei Zhang81a799e2019-12-16 17:37:13 +0000877#ifndef NDEBUG
878void EmbedderTest::OpenPDFFileForWrite(const std::string& filename) {
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000879 filestream_.open(filename, std::ios_base::binary);
880}
881
882void EmbedderTest::ClosePDFFileForWrite() {
883 filestream_.close();
884}
Lei Zhang81a799e2019-12-16 17:37:13 +0000885#endif