blob: f29eb1bf1a4b3edbc4caefa367d6fe4db4a58078 [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 Sepeza310e002015-02-27 13:03:07 -080019#include "testing/gmock/include/gmock/gmock.h"
Lei Zhangd50bdff2019-02-05 19:42:33 +000020#include "testing/test_loader.h"
Henrique Nakashimaf956bad2018-08-16 16:41:42 +000021#include "testing/utils/bitmap_saver.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000022#include "testing/utils/file_util.h"
Lei Zhang4c64e962019-02-05 19:24:12 +000023#include "testing/utils/hash.h"
Wei Li091f7a02015-11-09 12:09:55 -080024#include "testing/utils/path_service.h"
Tom Sepez25f33d02021-01-29 01:58:51 +000025#include "third_party/base/check.h"
Lei Zhang87e3d7a2021-06-17 19:40:28 +000026#include "third_party/base/containers/contains.h"
Lei Zhang30632072020-10-02 01:57:54 +000027#include "third_party/base/notreached.h"
Tom Sepezea03a7b2022-02-24 00:30:14 +000028#include "third_party/base/numerics/safe_conversions.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070029
30#ifdef PDF_ENABLE_V8
Tom Sepezeaf9d212020-07-22 16:24:00 +000031#include "testing/v8_test_environment.h"
Lei Zhang8241df72015-11-06 14:38:48 -080032#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050033#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070034#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080035
Tom Sepez96d13342015-01-16 14:59:26 -080036namespace {
thestigc08cd7a2016-06-27 09:47:59 -070037
Jane Liu28fb7ba2017-08-02 21:45:57 -040038int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
Tom Sepez6dc12402020-01-07 21:02:17 +000039 return EmbedderTest::BytesPerPixelForFormat(FPDFBitmap_GetFormat(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -040040}
41
Lei Zhangf997abe2022-01-12 19:14:23 +000042#if BUILDFLAG(IS_WIN)
Lei Zhang614f1a02019-05-30 00:03:53 +000043int CALLBACK GetRecordProc(HDC hdc,
44 HANDLETABLE* handle_table,
45 const ENHMETARECORD* record,
46 int objects_count,
47 LPARAM param) {
48 auto& records = *reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param);
49 records.push_back(record);
50 return 1;
51}
Lei Zhangf997abe2022-01-12 19:14:23 +000052#endif // BUILDFLAG(IS_WIN)
Lei Zhang614f1a02019-05-30 00:03:53 +000053
Tom Sepezb2ce4d32022-05-06 17:09:33 +000054// These "jump" into the delegate to do actual testing.
55void UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info, int type) {
56 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
57 delegate->UnsupportedHandler(type);
58}
59
60int AlertTrampoline(IPDF_JSPLATFORM* platform,
61 FPDF_WIDESTRING message,
62 FPDF_WIDESTRING title,
63 int type,
64 int icon) {
65 auto* delegate = static_cast<EmbedderTest*>(platform)->GetDelegate();
66 return delegate->Alert(message, title, type, icon);
67}
68
69int SetTimerTrampoline(FPDF_FORMFILLINFO* info, int msecs, TimerCallback fn) {
70 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
71 return delegate->SetTimer(msecs, fn);
72}
73
74void KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
75 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
76 return delegate->KillTimer(id);
77}
78
79FPDF_PAGE GetPageTrampoline(FPDF_FORMFILLINFO* info,
80 FPDF_DOCUMENT document,
81 int page_index) {
82 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
83 return delegate->GetPage(info, document, page_index);
84}
85
86void DoURIActionTrampoline(FPDF_FORMFILLINFO* info, FPDF_BYTESTRING uri) {
87 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
88 return delegate->DoURIAction(uri);
89}
90
91void DoGoToActionTrampoline(FPDF_FORMFILLINFO* info,
92 int page_index,
93 int zoom_mode,
94 float* pos_array,
95 int array_size) {
96 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
97 return delegate->DoGoToAction(info, page_index, zoom_mode, pos_array,
98 array_size);
99}
100
101void OnFocusChangeTrampoline(FPDF_FORMFILLINFO* info,
102 FPDF_ANNOTATION annot,
103 int page_index) {
104 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
105 return delegate->OnFocusChange(info, annot, page_index);
106}
107
108void DoURIActionWithKeyboardModifierTrampoline(FPDF_FORMFILLINFO* info,
109 FPDF_BYTESTRING uri,
110 int modifiers) {
111 auto* delegate = static_cast<EmbedderTest*>(info)->GetDelegate();
112 return delegate->DoURIActionWithKeyboardModifier(info, uri, modifiers);
113}
114
115// These do nothing (but must return a reasonable default value).
116void InvalidateStub(FPDF_FORMFILLINFO* pThis,
117 FPDF_PAGE page,
118 double left,
119 double top,
120 double right,
121 double bottom) {}
122
123void OutputSelectedRectStub(FPDF_FORMFILLINFO* pThis,
124 FPDF_PAGE page,
125 double left,
126 double top,
127 double right,
128 double bottom) {}
129
130void SetCursorStub(FPDF_FORMFILLINFO* pThis, int nCursorType) {}
131
132FPDF_SYSTEMTIME GetLocalTimeStub(FPDF_FORMFILLINFO* pThis) {
133 return {122, 11, 6, 28, 12, 59, 59, 500};
134}
135
136void OnChangeStub(FPDF_FORMFILLINFO* pThis) {}
137
138FPDF_PAGE GetCurrentPageStub(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document) {
139 return GetPageTrampoline(pThis, document, 0);
140}
141
142int GetRotationStub(FPDF_FORMFILLINFO* pThis, FPDF_PAGE page) {
143 return 0;
144}
145
146void ExecuteNamedActionStub(FPDF_FORMFILLINFO* pThis, FPDF_BYTESTRING name) {}
147
148void SetTextFieldFocusStub(FPDF_FORMFILLINFO* pThis,
149 FPDF_WIDESTRING value,
150 FPDF_DWORD valueLen,
151 FPDF_BOOL is_focus) {}
152
153#ifdef PDF_ENABLE_XFA
154void DisplayCaretStub(FPDF_FORMFILLINFO* pThis,
155 FPDF_PAGE page,
156 FPDF_BOOL bVisible,
157 double left,
158 double top,
159 double right,
160 double bottom) {}
161
162int GetCurrentPageIndexStub(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document) {
163 return 0;
164}
165
166void SetCurrentPageStub(FPDF_FORMFILLINFO* pThis,
167 FPDF_DOCUMENT document,
168 int iCurPage) {}
169
170void GotoURLStub(FPDF_FORMFILLINFO* pThis,
171 FPDF_DOCUMENT document,
172 FPDF_WIDESTRING wsURL) {}
173
174void GetPageViewRectStub(FPDF_FORMFILLINFO* pThis,
175 FPDF_PAGE page,
176 double* left,
177 double* top,
178 double* right,
179 double* bottom) {
180 *left = 0.0;
181 *top = 0.0;
182 *right = 512.0;
183 *bottom = 512.0;
184}
185
186void PageEventStub(FPDF_FORMFILLINFO* pThis,
187 int page_count,
188 FPDF_DWORD event_type) {}
189
190FPDF_BOOL PopupMenuStub(FPDF_FORMFILLINFO* pThis,
191 FPDF_PAGE page,
192 FPDF_WIDGET hWidget,
193 int menuFlag,
194 float x,
195 float y) {
196 return true;
197}
198
199FPDF_FILEHANDLER* OpenFileStub(FPDF_FORMFILLINFO* pThis,
200 int fileFlag,
201 FPDF_WIDESTRING wsURL,
202 const char* mode) {
203 return nullptr;
204}
205
206void EmailToStub(FPDF_FORMFILLINFO* pThis,
207 FPDF_FILEHANDLER* fileHandler,
208 FPDF_WIDESTRING pTo,
209 FPDF_WIDESTRING pSubject,
210 FPDF_WIDESTRING pCC,
211 FPDF_WIDESTRING pBcc,
212 FPDF_WIDESTRING pMsg) {}
213
214void UploadToStub(FPDF_FORMFILLINFO* pThis,
215 FPDF_FILEHANDLER* fileHandler,
216 int fileFlag,
217 FPDF_WIDESTRING uploadTo) {}
218
219int GetPlatformStub(FPDF_FORMFILLINFO* pThis, void* platform, int length) {
220 return 0;
221}
222
223int GetLanguageStub(FPDF_FORMFILLINFO* pThis, void* language, int length) {
224 return 0;
225}
226
227FPDF_FILEHANDLER* DownloadFromURLStub(FPDF_FORMFILLINFO* pThis,
228 FPDF_WIDESTRING URL) {
Tom Sepez689ec4d2022-05-06 18:36:03 +0000229 static const char kString[] = "<body>secrets</body>";
230 static FPDF_FILEHANDLER kFakeFileHandler = {
231 nullptr,
232 [](void*) -> void {},
233 [](void*) -> FPDF_DWORD { return sizeof(kString); },
234 [](void*, FPDF_DWORD off, void* buffer, FPDF_DWORD size) -> FPDF_RESULT {
235 memcpy(buffer, kString, std::min<size_t>(size, sizeof(kString)));
236 return 0;
237 },
238 [](void*, FPDF_DWORD, const void*, FPDF_DWORD) -> FPDF_RESULT {
239 return -1;
240 },
241 [](void*) -> FPDF_RESULT { return 0; },
242 [](void*, FPDF_DWORD) -> FPDF_RESULT { return 0; }};
243 return &kFakeFileHandler;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000244}
245
246FPDF_BOOL PostRequestURLStub(FPDF_FORMFILLINFO* pThis,
247 FPDF_WIDESTRING wsURL,
248 FPDF_WIDESTRING wsData,
249 FPDF_WIDESTRING wsContentType,
250 FPDF_WIDESTRING wsEncode,
251 FPDF_WIDESTRING wsHeader,
252 FPDF_BSTR* response) {
Tom Sepez15d326e2022-05-06 18:13:44 +0000253 const char kString[] = "p\0o\0s\0t\0e\0d\0";
254 FPDF_BStr_Set(response, kString, sizeof(kString) - 1);
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000255 return true;
256}
257
258FPDF_BOOL PutRequestURLStub(FPDF_FORMFILLINFO* pThis,
259 FPDF_WIDESTRING wsURL,
260 FPDF_WIDESTRING wsData,
261 FPDF_WIDESTRING wsEncode) {
262 return true;
263}
264#endif // PDF_ENABLE_XFA
265
thestigbcd3e532016-11-21 13:37:28 -0800266} // namespace
267
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268EmbedderTest::EmbedderTest()
Tom Sepez7df04832020-05-18 22:09:31 +0000269 : default_delegate_(std::make_unique<EmbedderTest::Delegate>()),
Lei Zhang0729be22018-02-05 21:13:51 +0000270 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400271 FPDF_FILEWRITE::version = 1;
272 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -0800273}
Tom Sepez96d13342015-01-16 14:59:26 -0800274
Lei Zhangea208512019-12-18 00:42:11 +0000275EmbedderTest::~EmbedderTest() = default;
Tom Sepezf288bb12015-11-20 12:12:46 -0800276
277void EmbedderTest::SetUp() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
279 memset(info, 0, sizeof(UNSUPPORT_INFO));
280 info->version = 1;
281 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
282 FSDK_SetUnSpObjProcessHandler(info);
283}
Tom Sepez96d13342015-01-16 14:59:26 -0800284
285void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +0000286 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
Tom Sepez25f33d02021-01-29 01:58:51 +0000287 // possible. This can fail when an DCHECK test fails in a test case.
Lei Zhang75c81712018-02-08 17:22:39 +0000288 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +0000289 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000290 if (document())
Tom Sepez38f725f2019-12-04 00:26:34 +0000291 CloseDocument();
Tom Sepez96d13342015-01-16 14:59:26 -0800292}
293
Lei Zhangb8b4a042021-11-05 00:04:25 +0000294void EmbedderTest::CreateEmptyDocument() {
Lei Zhang935fb862021-11-05 00:15:05 +0000295 CreateEmptyDocumentWithoutFormFillEnvironment();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000296 form_handle_.reset(SetupFormFillEnvironment(
297 document(), JavaScriptOption::kEnableJavaScript));
Tom Sepezd483eb42016-01-06 10:03:59 -0800298}
299
Lei Zhang935fb862021-11-05 00:15:05 +0000300void EmbedderTest::CreateEmptyDocumentWithoutFormFillEnvironment() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000301 document_.reset(FPDF_CreateNewDocument());
Lei Zhang935fb862021-11-05 00:15:05 +0000302 DCHECK(document_);
303}
304
Lei Zhang208eecf2017-12-20 19:40:50 +0000305bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000306 return OpenDocumentWithOptions(filename, nullptr,
307 LinearizeOption::kDefaultLinearize,
308 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000309}
310
311bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000312 return OpenDocumentWithOptions(filename, nullptr,
313 LinearizeOption::kMustLinearize,
314 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000315}
316
317bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
318 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000319 return OpenDocumentWithOptions(filename, password,
320 LinearizeOption::kDefaultLinearize,
321 JavaScriptOption::kEnableJavaScript);
322}
323
324bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
325 return OpenDocumentWithOptions(filename, nullptr,
326 LinearizeOption::kDefaultLinearize,
327 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000328}
329
330bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
331 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000332 LinearizeOption linearize_option,
333 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800334 std::string file_path;
335 if (!PathService::GetTestFilePath(filename, &file_path))
336 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000337
Wei Li091f7a02015-11-09 12:09:55 -0800338 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400339 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800340 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800341
thestig29ce9232016-06-22 07:03:23 -0700342 EXPECT_TRUE(!loader_);
Tom Sepez7df04832020-05-18 22:09:31 +0000343 loader_ = std::make_unique<TestLoader>(
Lei Zhangb3be4a12019-02-05 22:11:07 +0000344 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000345
346 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800347 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700348 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000349 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000350
Tom Sepez7df04832020-05-18 22:09:31 +0000351 fake_file_access_ = std::make_unique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000352 return OpenDocumentHelper(password, linearize_option, javascript_option,
353 fake_file_access_.get(), &document_, &avail_,
354 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400355}
Tom Sepez96d13342015-01-16 14:59:26 -0800356
Nicolas Pena56fc9722017-07-13 16:31:34 -0400357bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000358 LinearizeOption linearize_option,
359 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300360 FakeFileAccess* network_simulator,
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000361 ScopedFPDFDocument* document,
Lei Zhang52d21ac2021-11-04 22:40:15 +0000362 ScopedFPDFAvail* avail,
Lei Zhangdba065e2021-11-04 23:09:15 +0000363 ScopedFPDFFormHandle* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300364 network_simulator->AddSegment(0, 1024);
365 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000366 avail->reset(FPDFAvail_Create(network_simulator->GetFileAvail(),
367 network_simulator->GetFileAccess()));
368 FPDF_AVAIL avail_ptr = avail->get();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000369 FPDF_DOCUMENT document_ptr = nullptr;
Lei Zhang52d21ac2021-11-04 22:40:15 +0000370 if (FPDFAvail_IsLinearized(avail_ptr) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300371 int32_t nRet = PDF_DATA_NOTAVAIL;
372 while (nRet == PDF_DATA_NOTAVAIL) {
373 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000374 nRet = FPDFAvail_IsDocAvail(avail_ptr,
375 network_simulator->GetDownloadHints());
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300376 }
377 if (nRet == PDF_DATA_ERROR)
378 return false;
379
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000380 document->reset(FPDFAvail_GetDocument(avail_ptr, password));
381 document_ptr = document->get();
382 if (!document_ptr)
Jun Fangdf7f3662015-11-10 18:29:18 +0800383 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400384
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300385 nRet = PDF_DATA_NOTAVAIL;
386 while (nRet == PDF_DATA_NOTAVAIL) {
387 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000388 nRet = FPDFAvail_IsFormAvail(avail_ptr,
389 network_simulator->GetDownloadHints());
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300390 }
391 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800392 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400393
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000394 int page_count = FPDF_GetPageCount(document_ptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800395 for (int i = 0; i < page_count; ++i) {
396 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300397 while (nRet == PDF_DATA_NOTAVAIL) {
398 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000399 nRet = FPDFAvail_IsPageAvail(avail_ptr, i,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300400 network_simulator->GetDownloadHints());
401 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400402 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800403 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800404 }
405 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000406 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800407 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300408 network_simulator->SetWholeFileAvailable();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000409 document->reset(
410 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password));
411 document_ptr = document->get();
412 if (!document_ptr)
Jun Fangdf7f3662015-11-10 18:29:18 +0800413 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800414 }
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000415 form_handle->reset(SetupFormFillEnvironment(document_ptr, javascript_option));
Tom Sepez0784c732018-04-23 18:02:57 +0000416
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000417 int doc_type = FPDF_GetFormType(document_ptr);
Ryan Harrison854d71c2017-10-18 12:28:14 -0400418 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000419 FPDF_LoadXFA(document_ptr);
Tom Sepez0784c732018-04-23 18:02:57 +0000420
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000421 (void)FPDF_GetDocPermissions(document_ptr);
Tom Sepezd483eb42016-01-06 10:03:59 -0800422 return true;
423}
Tom Sepez96d13342015-01-16 14:59:26 -0800424
Tom Sepez38f725f2019-12-04 00:26:34 +0000425void EmbedderTest::CloseDocument() {
Lei Zhangdba065e2021-11-04 23:09:15 +0000426 FORM_DoDocumentAAction(form_handle(), FPDFDOC_AACTION_WC);
427 form_handle_.reset();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000428 document_.reset();
Lei Zhang55b1f462021-11-05 01:44:46 +0000429 avail_.reset();
430 fake_file_access_.reset();
431 memset(&file_access_, 0, sizeof(file_access_));
432 loader_.reset();
433 file_contents_.reset();
Tom Sepez38f725f2019-12-04 00:26:34 +0000434}
435
Tom Sepez0784c732018-04-23 18:02:57 +0000436FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
437 FPDF_DOCUMENT doc,
438 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800439 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400440 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Tom Sepez72f520c2020-08-24 23:43:46 +0000441 platform->version = 3;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800442 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800443
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800444 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
445 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Aayush Dhirc63913e2020-02-12 09:45:34 +0000446 formfillinfo->version = form_fill_info_version_;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000447 formfillinfo->FFI_Invalidate = InvalidateStub;
448 formfillinfo->FFI_OutputSelectedRect = OutputSelectedRectStub;
449 formfillinfo->FFI_SetCursor = SetCursorStub;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700450 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
451 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000452 formfillinfo->FFI_GetLocalTime = GetLocalTimeStub;
453 formfillinfo->FFI_OnChange = OnChangeStub;
Tom Sepez396e8722015-09-09 10:16:08 -0700454 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000455 formfillinfo->FFI_GetCurrentPage = GetCurrentPageStub;
456 formfillinfo->FFI_GetRotation = GetRotationStub;
457 formfillinfo->FFI_ExecuteNamedAction = ExecuteNamedActionStub;
458 formfillinfo->FFI_SetTextFieldFocus = SetTextFieldFocusStub;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000459 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000460 formfillinfo->FFI_DoGoToAction = DoGoToActionTrampoline;
Tom Sepezb2ce4d32022-05-06 17:09:33 +0000461#ifdef PDF_ENABLE_XFA
462 formfillinfo->FFI_DisplayCaret = DisplayCaretStub;
463 formfillinfo->FFI_GetCurrentPageIndex = GetCurrentPageIndexStub;
464 formfillinfo->FFI_SetCurrentPage = SetCurrentPageStub;
465 formfillinfo->FFI_GotoURL = GotoURLStub;
466 formfillinfo->FFI_GetPageViewRect = GetPageViewRectStub;
467 formfillinfo->FFI_PageEvent = PageEventStub;
468 formfillinfo->FFI_PopupMenu = PopupMenuStub;
469 formfillinfo->FFI_OpenFile = OpenFileStub;
470 formfillinfo->FFI_EmailTo = EmailToStub;
471 formfillinfo->FFI_UploadTo = UploadToStub;
472 formfillinfo->FFI_GetPlatform = GetPlatformStub;
473 formfillinfo->FFI_GetLanguage = GetLanguageStub;
474 formfillinfo->FFI_DownloadFromURL = DownloadFromURLStub;
475 formfillinfo->FFI_PostRequestURL = PostRequestURLStub;
476 formfillinfo->FFI_PutRequestURL = PutRequestURLStub;
477#endif // PDF_ENABLE_XFA
Aayush Dhirc63913e2020-02-12 09:45:34 +0000478 formfillinfo->FFI_OnFocusChange = OnFocusChangeTrampoline;
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000479 formfillinfo->FFI_DoURIActionWithKeyboardModifier =
480 DoURIActionWithKeyboardModifierTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000481
Tom Sepez0784c732018-04-23 18:02:57 +0000482 if (javascript_option == JavaScriptOption::kEnableJavaScript)
483 formfillinfo->m_pJsPlatform = platform;
484
Nicolas Pena3ff54002017-07-05 11:55:35 -0400485 FPDF_FORMHANDLE form_handle =
486 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Tom Sepez92f92222020-01-07 22:16:07 +0000487 SetInitialFormFieldHighlight(form_handle);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400488 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800489}
490
Tom Sepezda8189e2015-01-30 14:41:50 -0800491void EmbedderTest::DoOpenActions() {
Lei Zhangdba065e2021-11-04 23:09:15 +0000492 DCHECK(form_handle());
493 FORM_DoDocumentJSAction(form_handle());
494 FORM_DoDocumentOpenAction(form_handle());
Tom Sepez96d13342015-01-16 14:59:26 -0800495}
496
497int EmbedderTest::GetFirstPageNum() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000498 int first_page = FPDFAvail_GetFirstPageNum(document());
Lei Zhang52d21ac2021-11-04 22:40:15 +0000499 (void)FPDFAvail_IsPageAvail(avail(), first_page,
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000500 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800501 return first_page;
502}
503
504int EmbedderTest::GetPageCount() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000505 int page_count = FPDF_GetPageCount(document());
Nicolas Pena3ff54002017-07-05 11:55:35 -0400506 for (int i = 0; i < page_count; ++i)
Lei Zhang52d21ac2021-11-04 22:40:15 +0000507 (void)FPDFAvail_IsPageAvail(avail(), i,
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000508 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800509 return page_count;
510}
511
Tom Sepezda8189e2015-01-30 14:41:50 -0800512FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000513 return LoadPageCommon(page_number, true);
514}
515
516FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
517 return LoadPageCommon(page_number, false);
518}
519
520FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000521 DCHECK(form_handle());
Tom Sepez25f33d02021-01-29 01:58:51 +0000522 DCHECK(page_number >= 0);
523 DCHECK(!pdfium::Contains(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700524
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000525 FPDF_PAGE page = FPDF_LoadPage(document(), page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400526 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800527 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400528
Tom Sepez507d0192018-11-07 16:37:51 +0000529 if (do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000530 FORM_OnAfterLoadPage(page, form_handle());
531 FORM_DoPageAAction(page, form_handle(), FPDFPAGE_AACTION_OPEN);
Tom Sepez507d0192018-11-07 16:37:51 +0000532 }
weili0dadcc62016-08-23 21:10:57 -0700533 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700534 return page;
535}
536
Tom Sepezda8189e2015-01-30 14:41:50 -0800537void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000538 UnloadPageCommon(page, true);
539}
Lei Zhang75c81712018-02-08 17:22:39 +0000540
Tom Sepez507d0192018-11-07 16:37:51 +0000541void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
542 UnloadPageCommon(page, false);
543}
544
545void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000546 DCHECK(form_handle());
Lei Zhang75c81712018-02-08 17:22:39 +0000547 int page_number = GetPageNumberForLoadedPage(page);
548 if (page_number < 0) {
549 NOTREACHED();
550 return;
551 }
Tom Sepez507d0192018-11-07 16:37:51 +0000552 if (do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000553 FORM_DoPageAAction(page, form_handle(), FPDFPAGE_AACTION_CLOSE);
554 FORM_OnBeforeClosePage(page, form_handle());
Tom Sepez507d0192018-11-07 16:37:51 +0000555 }
Tom Sepez96d13342015-01-16 14:59:26 -0800556 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000557 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800558}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800559
Tom Sepez92f92222020-01-07 22:16:07 +0000560void EmbedderTest::SetInitialFormFieldHighlight(FPDF_FORMHANDLE form) {
561 FPDF_SetFormFieldHighlightColor(form, FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD);
562 FPDF_SetFormFieldHighlightAlpha(form, 100);
563}
564
Tom Sepeze08d2b12018-04-25 18:49:32 +0000565ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000566 return RenderLoadedPageWithFlags(page, 0);
567}
568
Tom Sepeze08d2b12018-04-25 18:49:32 +0000569ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
570 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000571 if (GetPageNumberForLoadedPage(page) < 0) {
572 NOTREACHED();
573 return nullptr;
574 }
Lei Zhangdba065e2021-11-04 23:09:15 +0000575 return RenderPageWithFlags(page, form_handle(), flags);
Lei Zhanga98e3662018-02-07 20:28:35 +0000576}
577
Tom Sepeze08d2b12018-04-25 18:49:32 +0000578ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000579 return RenderSavedPageWithFlags(page, 0);
580}
581
Tom Sepeze08d2b12018-04-25 18:49:32 +0000582ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
583 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000584 if (GetPageNumberForSavedPage(page) < 0) {
585 NOTREACHED();
586 return nullptr;
587 }
Lei Zhangdba065e2021-11-04 23:09:15 +0000588 return RenderPageWithFlags(page, saved_form_handle(), flags);
Lei Zhanga98e3662018-02-07 20:28:35 +0000589}
590
591// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000592ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
593 FPDF_FORMHANDLE handle,
594 int flags) {
Lei Zhangdfa075b2019-12-05 21:52:43 +0000595 int width = static_cast<int>(FPDF_GetPageWidthF(page));
596 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhanga98e3662018-02-07 20:28:35 +0000597 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000598 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000599 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
600 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
601 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
602 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
603 return bitmap;
604}
605
Lei Zhang30ff2532019-01-31 21:37:55 +0000606// static
607ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
608 return RenderPageWithFlags(page, nullptr, 0);
609}
610
Lei Zhangf997abe2022-01-12 19:14:23 +0000611#if BUILDFLAG(IS_WIN)
Lei Zhangd69e0652019-04-13 00:39:35 +0000612// static
613std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
614 int flags) {
615 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
616
Lei Zhangdfa075b2019-12-05 21:52:43 +0000617 int width = static_cast<int>(FPDF_GetPageWidthF(page));
618 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhangd69e0652019-04-13 00:39:35 +0000619 HRGN rgn = CreateRectRgn(0, 0, width, height);
620 SelectClipRgn(dc, rgn);
621 DeleteObject(rgn);
622
623 SelectObject(dc, GetStockObject(NULL_PEN));
624 SelectObject(dc, GetStockObject(WHITE_BRUSH));
625 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
626 Rectangle(dc, 0, 0, width + 1, height + 1);
627
628 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
629
630 HENHMETAFILE emf = CloseEnhMetaFile(dc);
Tom Sepezea03a7b2022-02-24 00:30:14 +0000631 UINT size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
Lei Zhangd69e0652019-04-13 00:39:35 +0000632 std::vector<uint8_t> buffer(size_in_bytes);
633 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
634 DeleteEnhMetaFile(emf);
635 return buffer;
636}
Lei Zhang614f1a02019-05-30 00:03:53 +0000637
638// static
639std::string EmbedderTest::GetPostScriptFromEmf(
Tom Sepez87b9e3a2019-11-20 23:45:43 +0000640 pdfium::span<const uint8_t> emf_data) {
Lei Zhang614f1a02019-05-30 00:03:53 +0000641 // This comes from Emf::InitFromData() in Chromium.
Tom Sepezea03a7b2022-02-24 00:30:14 +0000642 HENHMETAFILE emf = SetEnhMetaFileBits(
643 pdfium::base::checked_cast<UINT>(emf_data.size()), emf_data.data());
Lei Zhang614f1a02019-05-30 00:03:53 +0000644 if (!emf)
645 return std::string();
646
647 // This comes from Emf::Enumerator::Enumerator() in Chromium.
648 std::vector<const ENHMETARECORD*> records;
649 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
650 DeleteEnhMetaFile(emf);
651 return std::string();
652 }
653
654 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
655 std::string ps_data;
656 for (const auto* record : records) {
657 if (record->iType != EMR_GDICOMMENT)
658 continue;
659
660 // PostScript data is encapsulated inside EMF comment records.
661 // The first two bytes of the comment indicate the string length. The rest
662 // is the actual string data.
663 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
664 const char* data = reinterpret_cast<const char*>(comment->Data);
665 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
666 data += 2;
667 ps_data.append(data, size);
668 }
669 DeleteEnhMetaFile(emf);
670 return ps_data;
671}
Lei Zhangf997abe2022-01-12 19:14:23 +0000672#endif // BUILDFLAG(IS_WIN)
Lei Zhangd69e0652019-04-13 00:39:35 +0000673
Lei Zhang0b494052019-01-31 21:41:15 +0000674FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
675 return OpenSavedDocumentWithPassword(nullptr);
676}
677
Tom Sepez6dc12402020-01-07 21:02:17 +0000678// static
679int EmbedderTest::BytesPerPixelForFormat(int format) {
680 switch (format) {
681 case FPDFBitmap_Gray:
682 return 1;
683 case FPDFBitmap_BGR:
684 return 3;
685 case FPDFBitmap_BGRx:
686 case FPDFBitmap_BGRA:
687 return 4;
688 default:
689 NOTREACHED();
690 return 0;
691 }
692}
693
Lei Zhang0b494052019-01-31 21:41:15 +0000694FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
695 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300696 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Tom Sepezea03a7b2022-02-24 00:30:14 +0000697 saved_file_access_.m_FileLen =
698 pdfium::base::checked_cast<unsigned long>(data_string_.size());
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300699 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000700 // Copy data to prevent clearing it before saved document close.
701 saved_document_file_data_ = data_string_;
702 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400703
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300704 saved_fake_file_access_ =
Tom Sepez7df04832020-05-18 22:09:31 +0000705 std::make_unique<FakeFileAccess>(&saved_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300706
Tom Sepez0784c732018-04-23 18:02:57 +0000707 EXPECT_TRUE(OpenDocumentHelper(
708 password, LinearizeOption::kDefaultLinearize,
709 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
710 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000711 return saved_document();
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400712}
713
714void EmbedderTest::CloseSavedDocument() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000715 DCHECK(saved_document());
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400716
Lei Zhangdba065e2021-11-04 23:09:15 +0000717 saved_form_handle_.reset();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000718 saved_document_.reset();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000719 saved_avail_.reset();
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400720}
721
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000722FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000723 DCHECK(saved_form_handle());
Tom Sepez25f33d02021-01-29 01:58:51 +0000724 DCHECK(page_number >= 0);
725 DCHECK(!pdfium::Contains(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400726
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000727 FPDF_PAGE page = FPDF_LoadPage(saved_document(), page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000728 if (!page)
729 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400730
Lei Zhangdba065e2021-11-04 23:09:15 +0000731 FORM_OnAfterLoadPage(page, saved_form_handle());
732 FORM_DoPageAAction(page, saved_form_handle(), FPDFPAGE_AACTION_OPEN);
Lei Zhang9f72c452018-02-08 21:49:54 +0000733 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000734 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400735}
736
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000737void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000738 DCHECK(saved_form_handle());
Lei Zhang9f72c452018-02-08 21:49:54 +0000739
740 int page_number = GetPageNumberForSavedPage(page);
741 if (page_number < 0) {
742 NOTREACHED();
743 return;
744 }
745
Lei Zhangdba065e2021-11-04 23:09:15 +0000746 FORM_DoPageAAction(page, saved_form_handle(), FPDFPAGE_AACTION_CLOSE);
747 FORM_OnBeforeClosePage(page, saved_form_handle());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000748 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000749
750 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400751}
752
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000753void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
754 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400755 int height,
756 const char* md5) {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000757 DCHECK(saved_document());
Tom Sepez25f33d02021-01-29 01:58:51 +0000758 DCHECK(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400759
Tom Sepeze08d2b12018-04-25 18:49:32 +0000760 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000761 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400762}
763
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400764void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000765 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000766 FPDF_PAGE page = LoadSavedPage(0);
767 VerifySavedRendering(page, width, height, md5);
768 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400769 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400770}
771
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300772void EmbedderTest::SetWholeFileAvailable() {
Tom Sepez25f33d02021-01-29 01:58:51 +0000773 DCHECK(fake_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300774 fake_file_access_->SetWholeFileAvailable();
775}
776
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000777void EmbedderTest::SetDocumentFromAvail() {
778 document_.reset(FPDFAvail_GetDocument(avail(), nullptr));
779}
780
Lei Zhang52d21ac2021-11-04 22:40:15 +0000781void EmbedderTest::CreateAvail(FX_FILEAVAIL* file_avail,
782 FPDF_FILEACCESS* file) {
783 avail_.reset(FPDFAvail_Create(file_avail, file));
784}
785
weili0dadcc62016-08-23 21:10:57 -0700786FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700787 FPDF_DOCUMENT document,
788 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700789 EmbedderTest* test = static_cast<EmbedderTest*>(info);
790 auto it = test->page_map_.find(page_index);
791 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700792}
793
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800794// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000795std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Lei Zhangbf980142019-12-20 01:05:42 +0000796 int stride = FPDFBitmap_GetStride(bitmap);
797 int usable_bytes_per_row =
798 GetBitmapBytesPerPixel(bitmap) * FPDFBitmap_GetWidth(bitmap);
799 int height = FPDFBitmap_GetHeight(bitmap);
800 auto span = pdfium::make_span(
801 static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)), stride * height);
802
803 CRYPT_md5_context context = CRYPT_MD5Start();
804 for (int i = 0; i < height; ++i)
805 CRYPT_MD5Update(&context, span.subspan(i * stride, usable_bytes_per_row));
Dan Sinclair957480c2017-06-13 15:21:14 -0400806 uint8_t digest[16];
Lei Zhangbf980142019-12-20 01:05:42 +0000807 CRYPT_MD5Finish(&context, digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400808 return CryptToBase16(digest);
809}
810
Henrique Nakashimadb269572018-01-16 19:02:15 +0000811#ifndef NDEBUG
812// static
813void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
814 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000815 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000816}
817#endif
818
thestigbcd3e532016-11-21 13:37:28 -0800819// static
820void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
821 int expected_width,
822 int expected_height,
823 const char* expected_md5sum) {
824 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
825 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400826
827 // The expected stride is calculated using the same formula as in
828 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
829 const int expected_stride =
830 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800831 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
832
833 if (!expected_md5sum)
834 return;
835
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000836 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800837}
838
Nicolas Pena3ff54002017-07-05 11:55:35 -0400839// static
840int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
841 const void* data,
842 unsigned long size) {
843 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000844
Lei Zhang0729be22018-02-05 21:13:51 +0000845 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000846
847 if (pThis->filestream_.is_open())
848 pThis->filestream_.write(static_cast<const char*>(data), size);
849
Nicolas Pena3ff54002017-07-05 11:55:35 -0400850 return 1;
851}
852
853// static
854int EmbedderTest::GetBlockFromString(void* param,
855 unsigned long pos,
856 unsigned char* buf,
857 unsigned long size) {
858 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000859 if (!new_file || pos + size < pos) {
860 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400861 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000862 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400863
Lei Zhangcef91f12019-01-08 21:32:51 +0000864 if (pos + size > new_file->size()) {
865 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400866 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000867 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400868
869 memcpy(buf, new_file->data() + pos, size);
870 return 1;
871}
Lei Zhang75c81712018-02-08 17:22:39 +0000872
Lei Zhang9f72c452018-02-08 21:49:54 +0000873// static
874int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
875 FPDF_PAGE page) {
876 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000877 if (it.second == page) {
878 int page_number = it.first;
Tom Sepez25f33d02021-01-29 01:58:51 +0000879 DCHECK(page_number >= 0);
Lei Zhang75c81712018-02-08 17:22:39 +0000880 return page_number;
881 }
882 }
883 return -1;
884}
Lei Zhang9f72c452018-02-08 21:49:54 +0000885
886int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
887 return GetPageNumberForPage(page_map_, page);
888}
889
890int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
891 return GetPageNumberForPage(saved_page_map_, page);
892}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000893
Lei Zhang81a799e2019-12-16 17:37:13 +0000894#ifndef NDEBUG
895void EmbedderTest::OpenPDFFileForWrite(const std::string& filename) {
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000896 filestream_.open(filename, std::ios_base::binary);
897}
898
899void EmbedderTest::ClosePDFFileForWrite() {
900 filestream_.close();
901}
Lei Zhang81a799e2019-12-16 17:37:13 +0000902#endif