blob: f8265a4241efb435a2d02597e0da2fbfb04cd4f2 [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
Tom Sepez689ec4d2022-05-06 18:36:03 +00007#include <algorithm>
Lei Zhanga98e3662018-02-07 20:28:35 +00008#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -08009#include <string>
10#include <utility>
Lei Zhangd69e0652019-04-13 00:39:35 +000011#include <vector>
Tom Sepez96d13342015-01-16 14:59:26 -080012
Lei Zhangd145e4b2018-10-12 18:54:31 +000013#include "core/fdrm/fx_crypt.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000014#include "public/cpp/fpdf_scopers.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080015#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080016#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080017#include "public/fpdf_text.h"
18#include "public/fpdfview.h"
Tom Sepez37999602022-05-20 16:44:20 +000019#include "testing/embedder_test_environment.h"
Tom Sepeza310e002015-02-27 13:03:07 -080020#include "testing/gmock/include/gmock/gmock.h"
Lei Zhangd50bdff2019-02-05 19:42:33 +000021#include "testing/test_loader.h"
Henrique Nakashimaf956bad2018-08-16 16:41:42 +000022#include "testing/utils/bitmap_saver.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000023#include "testing/utils/file_util.h"
Lei Zhang4c64e962019-02-05 19:24:12 +000024#include "testing/utils/hash.h"
Wei Li091f7a02015-11-09 12:09:55 -080025#include "testing/utils/path_service.h"
Tom Sepez25f33d02021-01-29 01:58:51 +000026#include "third_party/base/check.h"
Lei Zhang87e3d7a2021-06-17 19:40:28 +000027#include "third_party/base/containers/contains.h"
Lei Zhang30632072020-10-02 01:57:54 +000028#include "third_party/base/notreached.h"
Tom Sepezea03a7b2022-02-24 00:30:14 +000029#include "third_party/base/numerics/safe_conversions.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070030
Tom Sepez96d13342015-01-16 14:59:26 -080031namespace {
thestigc08cd7a2016-06-27 09:47:59 -070032
Jane Liu28fb7ba2017-08-02 21:45:57 -040033int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
Tom Sepez6dc12402020-01-07 21:02:17 +000034 return EmbedderTest::BytesPerPixelForFormat(FPDFBitmap_GetFormat(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -040035}
36
Lei Zhangf997abe2022-01-12 19:14:23 +000037#if BUILDFLAG(IS_WIN)
Lei Zhang614f1a02019-05-30 00:03:53 +000038int CALLBACK GetRecordProc(HDC hdc,
39 HANDLETABLE* handle_table,
40 const ENHMETARECORD* record,
41 int objects_count,
42 LPARAM param) {
43 auto& records = *reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param);
44 records.push_back(record);
45 return 1;
46}
Lei Zhangf997abe2022-01-12 19:14:23 +000047#endif // BUILDFLAG(IS_WIN)
Lei Zhang614f1a02019-05-30 00:03:53 +000048
Tom Sepezb2ce4d32022-05-06 17:09:33 +000049// These "jump" into the delegate to do actual testing.
50void UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info, int type) {
51 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
52 delegate->UnsupportedHandler(type);
53}
54
55int AlertTrampoline(IPDF_JSPLATFORM* platform,
56 FPDF_WIDESTRING message,
57 FPDF_WIDESTRING title,
58 int type,
59 int icon) {
60 auto* delegate = static_cast<EmbedderTest*>(platform)->GetDelegate();
61 return delegate->Alert(message, title, type, icon);
62}
63
64int SetTimerTrampoline(FPDF_FORMFILLINFO* info, int msecs, TimerCallback fn) {
65 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
66 return delegate->SetTimer(msecs, fn);
67}
68
69void KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
70 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
71 return delegate->KillTimer(id);
72}
73
74FPDF_PAGE GetPageTrampoline(FPDF_FORMFILLINFO* info,
75 FPDF_DOCUMENT document,
76 int page_index) {
77 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
78 return delegate->GetPage(info, document, page_index);
79}
80
81void DoURIActionTrampoline(FPDF_FORMFILLINFO* info, FPDF_BYTESTRING uri) {
82 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
83 return delegate->DoURIAction(uri);
84}
85
86void DoGoToActionTrampoline(FPDF_FORMFILLINFO* info,
87 int page_index,
88 int zoom_mode,
89 float* pos_array,
90 int array_size) {
91 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
92 return delegate->DoGoToAction(info, page_index, zoom_mode, pos_array,
93 array_size);
94}
95
96void OnFocusChangeTrampoline(FPDF_FORMFILLINFO* info,
97 FPDF_ANNOTATION annot,
98 int page_index) {
99 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
100 return delegate->OnFocusChange(info, annot, page_index);
101}
102
103void DoURIActionWithKeyboardModifierTrampoline(FPDF_FORMFILLINFO* info,
104 FPDF_BYTESTRING uri,
105 int modifiers) {
106 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
107 return delegate->DoURIActionWithKeyboardModifier(info, uri, modifiers);
108}
109
110// These do nothing (but must return a reasonable default value).
111void InvalidateStub(FPDF_FORMFILLINFO* pThis,
112 FPDF_PAGE page,
113 double left,
114 double top,
115 double right,
116 double bottom) {}
117
118void OutputSelectedRectStub(FPDF_FORMFILLINFO* pThis,
119 FPDF_PAGE page,
120 double left,
121 double top,
122 double right,
123 double bottom) {}
124
125void SetCursorStub(FPDF_FORMFILLINFO* pThis, int nCursorType) {}
126
127FPDF_SYSTEMTIME GetLocalTimeStub(FPDF_FORMFILLINFO* pThis) {
128 return {122, 11, 6, 28, 12, 59, 59, 500};
129}
130
131void OnChangeStub(FPDF_FORMFILLINFO* pThis) {}
132
133FPDF_PAGE GetCurrentPageStub(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document) {
134 return GetPageTrampoline(pThis, document, 0);
135}
136
137int GetRotationStub(FPDF_FORMFILLINFO* pThis, FPDF_PAGE page) {
138 return 0;
139}
140
141void ExecuteNamedActionStub(FPDF_FORMFILLINFO* pThis, FPDF_BYTESTRING name) {}
142
143void SetTextFieldFocusStub(FPDF_FORMFILLINFO* pThis,
144 FPDF_WIDESTRING value,
145 FPDF_DWORD valueLen,
146 FPDF_BOOL is_focus) {}
147
148#ifdef PDF_ENABLE_XFA
149void DisplayCaretStub(FPDF_FORMFILLINFO* pThis,
150 FPDF_PAGE page,
151 FPDF_BOOL bVisible,
152 double left,
153 double top,
154 double right,
155 double bottom) {}
156
157int GetCurrentPageIndexStub(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document) {
158 return 0;
159}
160
161void SetCurrentPageStub(FPDF_FORMFILLINFO* pThis,
162 FPDF_DOCUMENT document,
163 int iCurPage) {}
164
165void GotoURLStub(FPDF_FORMFILLINFO* pThis,
166 FPDF_DOCUMENT document,
167 FPDF_WIDESTRING wsURL) {}
168
169void GetPageViewRectStub(FPDF_FORMFILLINFO* pThis,
170 FPDF_PAGE page,
171 double* left,
172 double* top,
173 double* right,
174 double* bottom) {
175 *left = 0.0;
176 *top = 0.0;
177 *right = 512.0;
178 *bottom = 512.0;
179}
180
181void PageEventStub(FPDF_FORMFILLINFO* pThis,
182 int page_count,
183 FPDF_DWORD event_type) {}
184
185FPDF_BOOL PopupMenuStub(FPDF_FORMFILLINFO* pThis,
186 FPDF_PAGE page,
187 FPDF_WIDGET hWidget,
188 int menuFlag,
189 float x,
190 float y) {
191 return true;
192}
193
194FPDF_FILEHANDLER* OpenFileStub(FPDF_FORMFILLINFO* pThis,
195 int fileFlag,
196 FPDF_WIDESTRING wsURL,
197 const char* mode) {
198 return nullptr;
199}
200
201void EmailToStub(FPDF_FORMFILLINFO* pThis,
202 FPDF_FILEHANDLER* fileHandler,
203 FPDF_WIDESTRING pTo,
204 FPDF_WIDESTRING pSubject,
205 FPDF_WIDESTRING pCC,
206 FPDF_WIDESTRING pBcc,
207 FPDF_WIDESTRING pMsg) {}
208
209void UploadToStub(FPDF_FORMFILLINFO* pThis,
210 FPDF_FILEHANDLER* fileHandler,
211 int fileFlag,
212 FPDF_WIDESTRING uploadTo) {}
213
214int GetPlatformStub(FPDF_FORMFILLINFO* pThis, void* platform, int length) {
215 return 0;
216}
217
218int GetLanguageStub(FPDF_FORMFILLINFO* pThis, void* language, int length) {
219 return 0;
220}
221
222FPDF_FILEHANDLER* DownloadFromURLStub(FPDF_FORMFILLINFO* pThis,
223 FPDF_WIDESTRING URL) {
Tom Sepez689ec4d2022-05-06 18:36:03 +0000224 static const char kString[] = "<body>secrets</body>";
225 static FPDF_FILEHANDLER kFakeFileHandler = {
226 nullptr,
227 [](void*) -> void {},
228 [](void*) -> FPDF_DWORD { return sizeof(kString); },
229 [](void*, FPDF_DWORD off, void* buffer, FPDF_DWORD size) -> FPDF_RESULT {
230 memcpy(buffer, kString, std::min<size_t>(size, sizeof(kString)));
231 return 0;
232 },
233 [](void*, FPDF_DWORD, const void*, FPDF_DWORD) -> FPDF_RESULT {
234 return -1;
235 },
236 [](void*) -> FPDF_RESULT { return 0; },
237 [](void*, FPDF_DWORD) -> FPDF_RESULT { return 0; }};
238 return &kFakeFileHandler;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000239}
240
241FPDF_BOOL PostRequestURLStub(FPDF_FORMFILLINFO* pThis,
242 FPDF_WIDESTRING wsURL,
243 FPDF_WIDESTRING wsData,
244 FPDF_WIDESTRING wsContentType,
245 FPDF_WIDESTRING wsEncode,
246 FPDF_WIDESTRING wsHeader,
247 FPDF_BSTR* response) {
Tom Sepez15d326e2022-05-06 18:13:44 +0000248 const char kString[] = "p\0o\0s\0t\0e\0d\0";
249 FPDF_BStr_Set(response, kString, sizeof(kString) - 1);
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000250 return true;
251}
252
253FPDF_BOOL PutRequestURLStub(FPDF_FORMFILLINFO* pThis,
254 FPDF_WIDESTRING wsURL,
255 FPDF_WIDESTRING wsData,
256 FPDF_WIDESTRING wsEncode) {
257 return true;
258}
259#endif // PDF_ENABLE_XFA
260
thestigbcd3e532016-11-21 13:37:28 -0800261} // namespace
262
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263EmbedderTest::EmbedderTest()
Tom Sepez7df04832020-05-18 22:09:31 +0000264 : default_delegate_(std::make_unique<EmbedderTest::Delegate>()),
Lei Zhang0729be22018-02-05 21:13:51 +0000265 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400266 FPDF_FILEWRITE::version = 1;
267 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -0800268}
Tom Sepez96d13342015-01-16 14:59:26 -0800269
Lei Zhangea208512019-12-18 00:42:11 +0000270EmbedderTest::~EmbedderTest() = default;
Tom Sepezf288bb12015-11-20 12:12:46 -0800271
272void EmbedderTest::SetUp() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
274 memset(info, 0, sizeof(UNSUPPORT_INFO));
275 info->version = 1;
276 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
277 FSDK_SetUnSpObjProcessHandler(info);
278}
Tom Sepez96d13342015-01-16 14:59:26 -0800279
280void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +0000281 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
Tom Sepez25f33d02021-01-29 01:58:51 +0000282 // possible. This can fail when an DCHECK test fails in a test case.
Lei Zhang75c81712018-02-08 17:22:39 +0000283 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +0000284 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000285 if (document())
Tom Sepez38f725f2019-12-04 00:26:34 +0000286 CloseDocument();
Tom Sepez96d13342015-01-16 14:59:26 -0800287}
288
Lei Zhangb8b4a042021-11-05 00:04:25 +0000289void EmbedderTest::CreateEmptyDocument() {
Lei Zhang935fb862021-11-05 00:15:05 +0000290 CreateEmptyDocumentWithoutFormFillEnvironment();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000291 form_handle_.reset(SetupFormFillEnvironment(
292 document(), JavaScriptOption::kEnableJavaScript));
Tom Sepezd483eb42016-01-06 10:03:59 -0800293}
294
Lei Zhang935fb862021-11-05 00:15:05 +0000295void EmbedderTest::CreateEmptyDocumentWithoutFormFillEnvironment() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000296 document_.reset(FPDF_CreateNewDocument());
Lei Zhang935fb862021-11-05 00:15:05 +0000297 DCHECK(document_);
298}
299
Lei Zhang208eecf2017-12-20 19:40:50 +0000300bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000301 return OpenDocumentWithOptions(filename, nullptr,
302 LinearizeOption::kDefaultLinearize,
303 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000304}
305
306bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000307 return OpenDocumentWithOptions(filename, nullptr,
308 LinearizeOption::kMustLinearize,
309 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000310}
311
312bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
313 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000314 return OpenDocumentWithOptions(filename, password,
315 LinearizeOption::kDefaultLinearize,
316 JavaScriptOption::kEnableJavaScript);
317}
318
319bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
320 return OpenDocumentWithOptions(filename, nullptr,
321 LinearizeOption::kDefaultLinearize,
322 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000323}
324
325bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
326 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000327 LinearizeOption linearize_option,
328 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800329 std::string file_path;
330 if (!PathService::GetTestFilePath(filename, &file_path))
331 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000332
Wei Li091f7a02015-11-09 12:09:55 -0800333 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400334 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800335 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800336
thestig29ce9232016-06-22 07:03:23 -0700337 EXPECT_TRUE(!loader_);
Tom Sepez7df04832020-05-18 22:09:31 +0000338 loader_ = std::make_unique<TestLoader>(
Lei Zhangb3be4a12019-02-05 22:11:07 +0000339 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000340
341 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800342 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700343 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000344 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000345
Tom Sepez7df04832020-05-18 22:09:31 +0000346 fake_file_access_ = std::make_unique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000347 return OpenDocumentHelper(password, linearize_option, javascript_option,
348 fake_file_access_.get(), &document_, &avail_,
349 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400350}
Tom Sepez96d13342015-01-16 14:59:26 -0800351
Nicolas Pena56fc9722017-07-13 16:31:34 -0400352bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000353 LinearizeOption linearize_option,
354 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300355 FakeFileAccess* network_simulator,
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000356 ScopedFPDFDocument* document,
Lei Zhang52d21ac2021-11-04 22:40:15 +0000357 ScopedFPDFAvail* avail,
Lei Zhangdba065e2021-11-04 23:09:15 +0000358 ScopedFPDFFormHandle* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300359 network_simulator->AddSegment(0, 1024);
360 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000361 avail->reset(FPDFAvail_Create(network_simulator->GetFileAvail(),
362 network_simulator->GetFileAccess()));
363 FPDF_AVAIL avail_ptr = avail->get();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000364 FPDF_DOCUMENT document_ptr = nullptr;
Lei Zhang52d21ac2021-11-04 22:40:15 +0000365 if (FPDFAvail_IsLinearized(avail_ptr) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300366 int32_t nRet = PDF_DATA_NOTAVAIL;
367 while (nRet == PDF_DATA_NOTAVAIL) {
368 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000369 nRet = FPDFAvail_IsDocAvail(avail_ptr,
370 network_simulator->GetDownloadHints());
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300371 }
372 if (nRet == PDF_DATA_ERROR)
373 return false;
374
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000375 document->reset(FPDFAvail_GetDocument(avail_ptr, password));
376 document_ptr = document->get();
377 if (!document_ptr)
Jun Fangdf7f3662015-11-10 18:29:18 +0800378 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400379
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300380 nRet = PDF_DATA_NOTAVAIL;
381 while (nRet == PDF_DATA_NOTAVAIL) {
382 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000383 nRet = FPDFAvail_IsFormAvail(avail_ptr,
384 network_simulator->GetDownloadHints());
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300385 }
386 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800387 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400388
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000389 int page_count = FPDF_GetPageCount(document_ptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800390 for (int i = 0; i < page_count; ++i) {
391 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300392 while (nRet == PDF_DATA_NOTAVAIL) {
393 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000394 nRet = FPDFAvail_IsPageAvail(avail_ptr, i,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300395 network_simulator->GetDownloadHints());
396 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400397 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800398 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800399 }
400 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000401 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800402 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300403 network_simulator->SetWholeFileAvailable();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000404 document->reset(
405 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password));
406 document_ptr = document->get();
407 if (!document_ptr)
Jun Fangdf7f3662015-11-10 18:29:18 +0800408 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800409 }
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000410 form_handle->reset(SetupFormFillEnvironment(document_ptr, javascript_option));
Tom Sepez0784c732018-04-23 18:02:57 +0000411
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000412 int doc_type = FPDF_GetFormType(document_ptr);
Ryan Harrison854d71c2017-10-18 12:28:14 -0400413 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000414 FPDF_LoadXFA(document_ptr);
Tom Sepez0784c732018-04-23 18:02:57 +0000415
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000416 (void)FPDF_GetDocPermissions(document_ptr);
Tom Sepezd483eb42016-01-06 10:03:59 -0800417 return true;
418}
Tom Sepez96d13342015-01-16 14:59:26 -0800419
Tom Sepez38f725f2019-12-04 00:26:34 +0000420void EmbedderTest::CloseDocument() {
Lei Zhangdba065e2021-11-04 23:09:15 +0000421 FORM_DoDocumentAAction(form_handle(), FPDFDOC_AACTION_WC);
422 form_handle_.reset();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000423 document_.reset();
Lei Zhang55b1f462021-11-05 01:44:46 +0000424 avail_.reset();
425 fake_file_access_.reset();
426 memset(&file_access_, 0, sizeof(file_access_));
427 loader_.reset();
428 file_contents_.reset();
Tom Sepez38f725f2019-12-04 00:26:34 +0000429}
430
Tom Sepez0784c732018-04-23 18:02:57 +0000431FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
432 FPDF_DOCUMENT doc,
433 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800434 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400435 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Tom Sepez72f520c2020-08-24 23:43:46 +0000436 platform->version = 3;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800437 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800438
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800439 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
440 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Aayush Dhirc63913e2020-02-12 09:45:34 +0000441 formfillinfo->version = form_fill_info_version_;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000442 formfillinfo->FFI_Invalidate = InvalidateStub;
443 formfillinfo->FFI_OutputSelectedRect = OutputSelectedRectStub;
444 formfillinfo->FFI_SetCursor = SetCursorStub;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700445 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
446 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000447 formfillinfo->FFI_GetLocalTime = GetLocalTimeStub;
448 formfillinfo->FFI_OnChange = OnChangeStub;
Tom Sepez396e8722015-09-09 10:16:08 -0700449 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000450 formfillinfo->FFI_GetCurrentPage = GetCurrentPageStub;
451 formfillinfo->FFI_GetRotation = GetRotationStub;
452 formfillinfo->FFI_ExecuteNamedAction = ExecuteNamedActionStub;
453 formfillinfo->FFI_SetTextFieldFocus = SetTextFieldFocusStub;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000454 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000455 formfillinfo->FFI_DoGoToAction = DoGoToActionTrampoline;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000456#ifdef PDF_ENABLE_XFA
457 formfillinfo->FFI_DisplayCaret = DisplayCaretStub;
458 formfillinfo->FFI_GetCurrentPageIndex = GetCurrentPageIndexStub;
459 formfillinfo->FFI_SetCurrentPage = SetCurrentPageStub;
460 formfillinfo->FFI_GotoURL = GotoURLStub;
461 formfillinfo->FFI_GetPageViewRect = GetPageViewRectStub;
462 formfillinfo->FFI_PageEvent = PageEventStub;
463 formfillinfo->FFI_PopupMenu = PopupMenuStub;
464 formfillinfo->FFI_OpenFile = OpenFileStub;
465 formfillinfo->FFI_EmailTo = EmailToStub;
466 formfillinfo->FFI_UploadTo = UploadToStub;
467 formfillinfo->FFI_GetPlatform = GetPlatformStub;
468 formfillinfo->FFI_GetLanguage = GetLanguageStub;
469 formfillinfo->FFI_DownloadFromURL = DownloadFromURLStub;
470 formfillinfo->FFI_PostRequestURL = PostRequestURLStub;
471 formfillinfo->FFI_PutRequestURL = PutRequestURLStub;
472#endif // PDF_ENABLE_XFA
Aayush Dhirc63913e2020-02-12 09:45:34 +0000473 formfillinfo->FFI_OnFocusChange = OnFocusChangeTrampoline;
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000474 formfillinfo->FFI_DoURIActionWithKeyboardModifier =
475 DoURIActionWithKeyboardModifierTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000476
Tom Sepez0784c732018-04-23 18:02:57 +0000477 if (javascript_option == JavaScriptOption::kEnableJavaScript)
478 formfillinfo->m_pJsPlatform = platform;
479
Nicolas Pena3ff54002017-07-05 11:55:35 -0400480 FPDF_FORMHANDLE form_handle =
481 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Tom Sepez92f92222020-01-07 22:16:07 +0000482 SetInitialFormFieldHighlight(form_handle);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400483 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800484}
485
Tom Sepezda8189e2015-01-30 14:41:50 -0800486void EmbedderTest::DoOpenActions() {
Lei Zhangdba065e2021-11-04 23:09:15 +0000487 DCHECK(form_handle());
488 FORM_DoDocumentJSAction(form_handle());
489 FORM_DoDocumentOpenAction(form_handle());
Tom Sepez96d13342015-01-16 14:59:26 -0800490}
491
492int EmbedderTest::GetFirstPageNum() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000493 int first_page = FPDFAvail_GetFirstPageNum(document());
Lei Zhang52d21ac2021-11-04 22:40:15 +0000494 (void)FPDFAvail_IsPageAvail(avail(), first_page,
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000495 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800496 return first_page;
497}
498
499int EmbedderTest::GetPageCount() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000500 int page_count = FPDF_GetPageCount(document());
Nicolas Pena3ff54002017-07-05 11:55:35 -0400501 for (int i = 0; i < page_count; ++i)
Lei Zhang52d21ac2021-11-04 22:40:15 +0000502 (void)FPDFAvail_IsPageAvail(avail(), i,
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000503 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800504 return page_count;
505}
506
Tom Sepezda8189e2015-01-30 14:41:50 -0800507FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000508 return LoadPageCommon(page_number, true);
509}
510
511FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
512 return LoadPageCommon(page_number, false);
513}
514
515FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000516 DCHECK(form_handle());
Tom Sepez25f33d02021-01-29 01:58:51 +0000517 DCHECK(page_number >= 0);
518 DCHECK(!pdfium::Contains(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700519
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000520 FPDF_PAGE page = FPDF_LoadPage(document(), page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400521 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800522 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400523
Tom Sepez507d0192018-11-07 16:37:51 +0000524 if (do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000525 FORM_OnAfterLoadPage(page, form_handle());
526 FORM_DoPageAAction(page, form_handle(), FPDFPAGE_AACTION_OPEN);
Tom Sepez507d0192018-11-07 16:37:51 +0000527 }
weili0dadcc62016-08-23 21:10:57 -0700528 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700529 return page;
530}
531
Tom Sepezda8189e2015-01-30 14:41:50 -0800532void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000533 UnloadPageCommon(page, true);
534}
Lei Zhang75c81712018-02-08 17:22:39 +0000535
Tom Sepez507d0192018-11-07 16:37:51 +0000536void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
537 UnloadPageCommon(page, false);
538}
539
540void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000541 DCHECK(form_handle());
Lei Zhang75c81712018-02-08 17:22:39 +0000542 int page_number = GetPageNumberForLoadedPage(page);
543 if (page_number < 0) {
544 NOTREACHED();
545 return;
546 }
Tom Sepez507d0192018-11-07 16:37:51 +0000547 if (do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000548 FORM_DoPageAAction(page, form_handle(), FPDFPAGE_AACTION_CLOSE);
549 FORM_OnBeforeClosePage(page, form_handle());
Tom Sepez507d0192018-11-07 16:37:51 +0000550 }
Tom Sepez96d13342015-01-16 14:59:26 -0800551 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000552 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800553}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800554
Tom Sepez92f92222020-01-07 22:16:07 +0000555void EmbedderTest::SetInitialFormFieldHighlight(FPDF_FORMHANDLE form) {
556 FPDF_SetFormFieldHighlightColor(form, FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD);
557 FPDF_SetFormFieldHighlightAlpha(form, 100);
558}
559
Tom Sepeze08d2b12018-04-25 18:49:32 +0000560ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000561 return RenderLoadedPageWithFlags(page, 0);
562}
563
Tom Sepeze08d2b12018-04-25 18:49:32 +0000564ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
565 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000566 if (GetPageNumberForLoadedPage(page) < 0) {
567 NOTREACHED();
568 return nullptr;
569 }
Lei Zhangdba065e2021-11-04 23:09:15 +0000570 return RenderPageWithFlags(page, form_handle(), flags);
Lei Zhanga98e3662018-02-07 20:28:35 +0000571}
572
Tom Sepeze08d2b12018-04-25 18:49:32 +0000573ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000574 return RenderSavedPageWithFlags(page, 0);
575}
576
Tom Sepeze08d2b12018-04-25 18:49:32 +0000577ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
578 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000579 if (GetPageNumberForSavedPage(page) < 0) {
580 NOTREACHED();
581 return nullptr;
582 }
Lei Zhangdba065e2021-11-04 23:09:15 +0000583 return RenderPageWithFlags(page, saved_form_handle(), flags);
Lei Zhanga98e3662018-02-07 20:28:35 +0000584}
585
586// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000587ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
588 FPDF_FORMHANDLE handle,
589 int flags) {
Lei Zhangdfa075b2019-12-05 21:52:43 +0000590 int width = static_cast<int>(FPDF_GetPageWidthF(page));
591 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhanga98e3662018-02-07 20:28:35 +0000592 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000593 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000594 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
595 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
596 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
597 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
598 return bitmap;
599}
600
Lei Zhang30ff2532019-01-31 21:37:55 +0000601// static
602ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
603 return RenderPageWithFlags(page, nullptr, 0);
604}
605
Lei Zhangf997abe2022-01-12 19:14:23 +0000606#if BUILDFLAG(IS_WIN)
Lei Zhangd69e0652019-04-13 00:39:35 +0000607// static
608std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
609 int flags) {
610 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
611
Lei Zhangdfa075b2019-12-05 21:52:43 +0000612 int width = static_cast<int>(FPDF_GetPageWidthF(page));
613 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhangd69e0652019-04-13 00:39:35 +0000614 HRGN rgn = CreateRectRgn(0, 0, width, height);
615 SelectClipRgn(dc, rgn);
616 DeleteObject(rgn);
617
618 SelectObject(dc, GetStockObject(NULL_PEN));
619 SelectObject(dc, GetStockObject(WHITE_BRUSH));
620 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
621 Rectangle(dc, 0, 0, width + 1, height + 1);
622
623 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
624
625 HENHMETAFILE emf = CloseEnhMetaFile(dc);
Tom Sepezea03a7b2022-02-24 00:30:14 +0000626 UINT size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
Lei Zhangd69e0652019-04-13 00:39:35 +0000627 std::vector<uint8_t> buffer(size_in_bytes);
628 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
629 DeleteEnhMetaFile(emf);
630 return buffer;
631}
Lei Zhang614f1a02019-05-30 00:03:53 +0000632
633// static
634std::string EmbedderTest::GetPostScriptFromEmf(
Tom Sepez87b9e3a2019-11-20 23:45:43 +0000635 pdfium::span<const uint8_t> emf_data) {
Lei Zhang614f1a02019-05-30 00:03:53 +0000636 // This comes from Emf::InitFromData() in Chromium.
Tom Sepezea03a7b2022-02-24 00:30:14 +0000637 HENHMETAFILE emf = SetEnhMetaFileBits(
638 pdfium::base::checked_cast<UINT>(emf_data.size()), emf_data.data());
Lei Zhang614f1a02019-05-30 00:03:53 +0000639 if (!emf)
640 return std::string();
641
642 // This comes from Emf::Enumerator::Enumerator() in Chromium.
643 std::vector<const ENHMETARECORD*> records;
644 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
645 DeleteEnhMetaFile(emf);
646 return std::string();
647 }
648
649 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
650 std::string ps_data;
651 for (const auto* record : records) {
652 if (record->iType != EMR_GDICOMMENT)
653 continue;
654
655 // PostScript data is encapsulated inside EMF comment records.
656 // The first two bytes of the comment indicate the string length. The rest
657 // is the actual string data.
658 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
659 const char* data = reinterpret_cast<const char*>(comment->Data);
660 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
661 data += 2;
662 ps_data.append(data, size);
663 }
664 DeleteEnhMetaFile(emf);
665 return ps_data;
666}
Lei Zhangf997abe2022-01-12 19:14:23 +0000667#endif // BUILDFLAG(IS_WIN)
Lei Zhangd69e0652019-04-13 00:39:35 +0000668
Lei Zhang0b494052019-01-31 21:41:15 +0000669FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
670 return OpenSavedDocumentWithPassword(nullptr);
671}
672
Tom Sepez6dc12402020-01-07 21:02:17 +0000673// static
674int EmbedderTest::BytesPerPixelForFormat(int format) {
675 switch (format) {
676 case FPDFBitmap_Gray:
677 return 1;
678 case FPDFBitmap_BGR:
679 return 3;
680 case FPDFBitmap_BGRx:
681 case FPDFBitmap_BGRA:
682 return 4;
683 default:
684 NOTREACHED();
685 return 0;
686 }
687}
688
Lei Zhang0b494052019-01-31 21:41:15 +0000689FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
690 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300691 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Tom Sepezea03a7b2022-02-24 00:30:14 +0000692 saved_file_access_.m_FileLen =
693 pdfium::base::checked_cast<unsigned long>(data_string_.size());
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300694 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000695 // Copy data to prevent clearing it before saved document close.
696 saved_document_file_data_ = data_string_;
697 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400698
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300699 saved_fake_file_access_ =
Tom Sepez7df04832020-05-18 22:09:31 +0000700 std::make_unique<FakeFileAccess>(&saved_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300701
Tom Sepez0784c732018-04-23 18:02:57 +0000702 EXPECT_TRUE(OpenDocumentHelper(
703 password, LinearizeOption::kDefaultLinearize,
704 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
705 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000706 return saved_document();
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400707}
708
709void EmbedderTest::CloseSavedDocument() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000710 DCHECK(saved_document());
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400711
Lei Zhangdba065e2021-11-04 23:09:15 +0000712 saved_form_handle_.reset();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000713 saved_document_.reset();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000714 saved_avail_.reset();
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400715}
716
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000717FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000718 DCHECK(saved_form_handle());
Tom Sepez25f33d02021-01-29 01:58:51 +0000719 DCHECK(page_number >= 0);
720 DCHECK(!pdfium::Contains(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400721
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000722 FPDF_PAGE page = FPDF_LoadPage(saved_document(), page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000723 if (!page)
724 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400725
Lei Zhangdba065e2021-11-04 23:09:15 +0000726 FORM_OnAfterLoadPage(page, saved_form_handle());
727 FORM_DoPageAAction(page, saved_form_handle(), FPDFPAGE_AACTION_OPEN);
Lei Zhang9f72c452018-02-08 21:49:54 +0000728 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000729 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400730}
731
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000732void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000733 DCHECK(saved_form_handle());
Lei Zhang9f72c452018-02-08 21:49:54 +0000734
735 int page_number = GetPageNumberForSavedPage(page);
736 if (page_number < 0) {
737 NOTREACHED();
738 return;
739 }
740
Lei Zhangdba065e2021-11-04 23:09:15 +0000741 FORM_DoPageAAction(page, saved_form_handle(), FPDFPAGE_AACTION_CLOSE);
742 FORM_OnBeforeClosePage(page, saved_form_handle());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000743 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000744
745 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400746}
747
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000748void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
749 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400750 int height,
751 const char* md5) {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000752 DCHECK(saved_document());
Tom Sepez25f33d02021-01-29 01:58:51 +0000753 DCHECK(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400754
Tom Sepeze08d2b12018-04-25 18:49:32 +0000755 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000756 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400757}
758
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400759void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000760 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000761 FPDF_PAGE page = LoadSavedPage(0);
762 VerifySavedRendering(page, width, height, md5);
763 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400764 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400765}
766
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300767void EmbedderTest::SetWholeFileAvailable() {
Tom Sepez25f33d02021-01-29 01:58:51 +0000768 DCHECK(fake_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300769 fake_file_access_->SetWholeFileAvailable();
770}
771
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000772void EmbedderTest::SetDocumentFromAvail() {
773 document_.reset(FPDFAvail_GetDocument(avail(), nullptr));
774}
775
Lei Zhang52d21ac2021-11-04 22:40:15 +0000776void EmbedderTest::CreateAvail(FX_FILEAVAIL* file_avail,
777 FPDF_FILEACCESS* file) {
778 avail_.reset(FPDFAvail_Create(file_avail, file));
779}
780
weili0dadcc62016-08-23 21:10:57 -0700781FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700782 FPDF_DOCUMENT document,
783 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700784 EmbedderTest* test = static_cast<EmbedderTest*>(info);
785 auto it = test->page_map_.find(page_index);
786 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700787}
788
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800789// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000790std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Lei Zhangbf980142019-12-20 01:05:42 +0000791 int stride = FPDFBitmap_GetStride(bitmap);
792 int usable_bytes_per_row =
793 GetBitmapBytesPerPixel(bitmap) * FPDFBitmap_GetWidth(bitmap);
794 int height = FPDFBitmap_GetHeight(bitmap);
795 auto span = pdfium::make_span(
796 static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)), stride * height);
797
798 CRYPT_md5_context context = CRYPT_MD5Start();
799 for (int i = 0; i < height; ++i)
800 CRYPT_MD5Update(&context, span.subspan(i * stride, usable_bytes_per_row));
Dan Sinclair957480c2017-06-13 15:21:14 -0400801 uint8_t digest[16];
Lei Zhangbf980142019-12-20 01:05:42 +0000802 CRYPT_MD5Finish(&context, digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400803 return CryptToBase16(digest);
804}
805
Henrique Nakashimadb269572018-01-16 19:02:15 +0000806// static
807void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
808 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000809 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000810}
Henrique Nakashimadb269572018-01-16 19:02:15 +0000811
thestigbcd3e532016-11-21 13:37:28 -0800812// static
813void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
814 int expected_width,
815 int expected_height,
816 const char* expected_md5sum) {
817 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
818 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400819
820 // The expected stride is calculated using the same formula as in
821 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
822 const int expected_stride =
823 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800824 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
825
826 if (!expected_md5sum)
827 return;
828
Tom Sepez37999602022-05-20 16:44:20 +0000829 std::string actual_md5sum = HashBitmap(bitmap);
830 EXPECT_EQ(expected_md5sum, actual_md5sum);
831 if (EmbedderTestEnvironment::GetInstance()->write_pngs()) {
832 WriteBitmapToPng(bitmap, actual_md5sum + ".png");
833 }
thestigbcd3e532016-11-21 13:37:28 -0800834}
835
Nicolas Pena3ff54002017-07-05 11:55:35 -0400836// static
837int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
838 const void* data,
839 unsigned long size) {
840 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000841
Lei Zhang0729be22018-02-05 21:13:51 +0000842 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000843
844 if (pThis->filestream_.is_open())
845 pThis->filestream_.write(static_cast<const char*>(data), size);
846
Nicolas Pena3ff54002017-07-05 11:55:35 -0400847 return 1;
848}
849
850// static
851int EmbedderTest::GetBlockFromString(void* param,
852 unsigned long pos,
853 unsigned char* buf,
854 unsigned long size) {
855 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000856 if (!new_file || pos + size < pos) {
857 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400858 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000859 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400860
Lei Zhangcef91f12019-01-08 21:32:51 +0000861 if (pos + size > new_file->size()) {
862 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400863 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000864 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400865
866 memcpy(buf, new_file->data() + pos, size);
867 return 1;
868}
Lei Zhang75c81712018-02-08 17:22:39 +0000869
Lei Zhang9f72c452018-02-08 21:49:54 +0000870// static
871int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
872 FPDF_PAGE page) {
873 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000874 if (it.second == page) {
875 int page_number = it.first;
Tom Sepez25f33d02021-01-29 01:58:51 +0000876 DCHECK(page_number >= 0);
Lei Zhang75c81712018-02-08 17:22:39 +0000877 return page_number;
878 }
879 }
880 return -1;
881}
Lei Zhang9f72c452018-02-08 21:49:54 +0000882
883int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
884 return GetPageNumberForPage(page_map_, page);
885}
886
887int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
888 return GetPageNumberForPage(saved_page_map_, page);
889}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000890
Lei Zhang81a799e2019-12-16 17:37:13 +0000891#ifndef NDEBUG
892void EmbedderTest::OpenPDFFileForWrite(const std::string& filename) {
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000893 filestream_.open(filename, std::ios_base::binary);
894}
895
896void EmbedderTest::ClosePDFFileForWrite() {
897 filestream_.close();
898}
Lei Zhang81a799e2019-12-16 17:37:13 +0000899#endif