blob: ae09cf9ffbee55c8e4b1508a56e8a8d30d4b5b3d [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
7#include <limits.h>
Tom Sepez96d13342015-01-16 14:59:26 -08008
Lei Zhanga98e3662018-02-07 20:28:35 +00009#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -080010#include <string>
11#include <utility>
Lei Zhangd69e0652019-04-13 00:39:35 +000012#include <vector>
Tom Sepez96d13342015-01-16 14:59:26 -080013
Lei Zhangd145e4b2018-10-12 18:54:31 +000014#include "core/fdrm/fx_crypt.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000015#include "public/cpp/fpdf_scopers.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080016#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080017#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080018#include "public/fpdf_text.h"
19#include "public/fpdfview.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"
Lei Zhang75c81712018-02-08 17:22:39 +000026#include "third_party/base/logging.h"
Lei Zhang75c81712018-02-08 17:22:39 +000027#include "third_party/base/stl_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070028
29#ifdef PDF_ENABLE_V8
Tom Sepezaf33f512020-06-12 01:00:10 +000030#include "testing/v8_initializer.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
Tom Sepezaf33f512020-06-12 01:00:10 +000037EmbedderTestEnvironment* g_environment = nullptr;
38
Jane Liu28fb7ba2017-08-02 21:45:57 -040039int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
Tom Sepez6dc12402020-01-07 21:02:17 +000040 return EmbedderTest::BytesPerPixelForFormat(FPDFBitmap_GetFormat(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -040041}
42
Lei Zhang614f1a02019-05-30 00:03:53 +000043#if defined(OS_WIN)
44int CALLBACK GetRecordProc(HDC hdc,
45 HANDLETABLE* handle_table,
46 const ENHMETARECORD* record,
47 int objects_count,
48 LPARAM param) {
49 auto& records = *reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param);
50 records.push_back(record);
51 return 1;
52}
53#endif // defined(OS_WIN)
54
thestigbcd3e532016-11-21 13:37:28 -080055} // namespace
56
Tom Sepezaf33f512020-06-12 01:00:10 +000057EmbedderTestEnvironment::EmbedderTestEnvironment(const char* exe_name)
58#ifdef PDF_ENABLE_V8
59 : exe_path_(exe_name)
60#endif
61{
62 ASSERT(!g_environment);
63 g_environment = this;
64}
65
66EmbedderTestEnvironment::~EmbedderTestEnvironment() {
67 ASSERT(g_environment);
68 g_environment = nullptr;
69
70#ifdef PDF_ENABLE_V8
71#ifdef V8_USE_EXTERNAL_STARTUP_DATA
72 if (v8_snapshot_)
73 free(const_cast<char*>(v8_snapshot_->data));
74#endif // V8_USE_EXTERNAL_STARTUP_DATA
75#endif // PDF_ENABLE_V8
76}
77
78// static
79EmbedderTestEnvironment* EmbedderTestEnvironment::GetInstance() {
80 return g_environment;
81}
82
83void EmbedderTestEnvironment::SetUp() {
84#ifdef PDF_ENABLE_V8
85#ifdef V8_USE_EXTERNAL_STARTUP_DATA
86 if (v8_snapshot_) {
87 platform_ =
88 InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(), nullptr);
89 } else {
90 v8_snapshot_ = std::make_unique<v8::StartupData>();
91 platform_ = InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(),
92 v8_snapshot_.get());
93 }
94#else
95 platform_ = InitializeV8ForPDFium(exe_path_);
96#endif // V8_USE_EXTERNAL_STARTUP_DATA
97#endif // FPDF_ENABLE_V8
98}
99
100void EmbedderTestEnvironment::TearDown() {
101#ifdef PDF_ENABLE_V8
102 v8::V8::ShutdownPlatform();
103#endif // PDF_ENABLE_V8
104}
105
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106EmbedderTest::EmbedderTest()
Tom Sepez7df04832020-05-18 22:09:31 +0000107 : default_delegate_(std::make_unique<EmbedderTest::Delegate>()),
Lei Zhang0729be22018-02-05 21:13:51 +0000108 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400109 FPDF_FILEWRITE::version = 1;
110 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -0800111}
Tom Sepez96d13342015-01-16 14:59:26 -0800112
Lei Zhangea208512019-12-18 00:42:11 +0000113EmbedderTest::~EmbedderTest() = default;
Tom Sepezf288bb12015-11-20 12:12:46 -0800114
115void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -0700116 FPDF_LIBRARY_CONFIG config;
Tom Sepezcba1a932020-06-15 17:46:03 +0000117 config.version = 3;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700118 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700119 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -0700120 config.m_pIsolate = external_isolate_;
Tom Sepezcba1a932020-06-15 17:46:03 +0000121#ifdef PDF_ENABLE_V8
122 config.m_pPlatform = EmbedderTestEnvironment::GetInstance()->platform();
123#else // PDF_ENABLE_V8
124 config.m_pPlatform = nullptr;
125#endif // PDF_ENABLE_V8
126
Tom Sepeza72e8e22015-10-07 10:17:53 -0700127 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -0800128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
130 memset(info, 0, sizeof(UNSUPPORT_INFO));
131 info->version = 1;
132 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
133 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000134
Lei Zhang0729be22018-02-05 21:13:51 +0000135 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136}
Tom Sepez96d13342015-01-16 14:59:26 -0800137
138void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +0000139 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
140 // possible. This can fail when an ASSERT test fails in a test case.
141 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +0000142 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang75c81712018-02-08 17:22:39 +0000143
Lei Zhang44005192020-04-17 01:35:43 +0000144 if (document_)
Tom Sepez38f725f2019-12-04 00:26:34 +0000145 CloseDocument();
Tom Sepezc46d0002015-11-30 15:46:36 -0800146
Tom Sepez96d13342015-01-16 14:59:26 -0800147 FPDFAvail_Destroy(avail_);
148 FPDF_DestroyLibrary();
Lei Zhangb3be4a12019-02-05 22:11:07 +0000149 loader_.reset();
Tom Sepez96d13342015-01-16 14:59:26 -0800150}
151
Lei Zhang378ec542018-10-18 19:15:47 +0000152#ifdef PDF_ENABLE_V8
153void EmbedderTest::SetExternalIsolate(void* isolate) {
154 external_isolate_ = static_cast<v8::Isolate*>(isolate);
155}
156#endif // PDF_ENABLE_V8
157
Tom Sepezd483eb42016-01-06 10:03:59 -0800158bool EmbedderTest::CreateEmptyDocument() {
159 document_ = FPDF_CreateNewDocument();
160 if (!document_)
161 return false;
162
Tom Sepez0784c732018-04-23 18:02:57 +0000163 form_handle_ =
164 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -0800165 return true;
166}
167
Lei Zhang208eecf2017-12-20 19:40:50 +0000168bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000169 return OpenDocumentWithOptions(filename, nullptr,
170 LinearizeOption::kDefaultLinearize,
171 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000172}
173
174bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000175 return OpenDocumentWithOptions(filename, nullptr,
176 LinearizeOption::kMustLinearize,
177 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000178}
179
180bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
181 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000182 return OpenDocumentWithOptions(filename, password,
183 LinearizeOption::kDefaultLinearize,
184 JavaScriptOption::kEnableJavaScript);
185}
186
187bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
188 return OpenDocumentWithOptions(filename, nullptr,
189 LinearizeOption::kDefaultLinearize,
190 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000191}
192
193bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
194 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000195 LinearizeOption linearize_option,
196 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800197 std::string file_path;
198 if (!PathService::GetTestFilePath(filename, &file_path))
199 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000200
Wei Li091f7a02015-11-09 12:09:55 -0800201 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400202 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800203 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800204
thestig29ce9232016-06-22 07:03:23 -0700205 EXPECT_TRUE(!loader_);
Tom Sepez7df04832020-05-18 22:09:31 +0000206 loader_ = std::make_unique<TestLoader>(
Lei Zhangb3be4a12019-02-05 22:11:07 +0000207 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000208
209 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800210 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700211 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000212 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000213
Tom Sepez7df04832020-05-18 22:09:31 +0000214 fake_file_access_ = std::make_unique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000215 return OpenDocumentHelper(password, linearize_option, javascript_option,
216 fake_file_access_.get(), &document_, &avail_,
217 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400218}
Tom Sepez96d13342015-01-16 14:59:26 -0800219
Nicolas Pena56fc9722017-07-13 16:31:34 -0400220bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000221 LinearizeOption linearize_option,
222 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300223 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400224 FPDF_DOCUMENT* document,
225 FPDF_AVAIL* avail,
226 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300227 network_simulator->AddSegment(0, 1024);
228 network_simulator->SetRequestedDataAvailable();
229 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
230 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400231 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300232 int32_t nRet = PDF_DATA_NOTAVAIL;
233 while (nRet == PDF_DATA_NOTAVAIL) {
234 network_simulator->SetRequestedDataAvailable();
235 nRet =
236 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
237 }
238 if (nRet == PDF_DATA_ERROR)
239 return false;
240
Nicolas Pena56fc9722017-07-13 16:31:34 -0400241 *document = FPDFAvail_GetDocument(*avail, password);
242 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800243 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400244
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300245 nRet = PDF_DATA_NOTAVAIL;
246 while (nRet == PDF_DATA_NOTAVAIL) {
247 network_simulator->SetRequestedDataAvailable();
248 nRet =
249 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
250 }
251 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800252 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400253
254 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800255 for (int i = 0; i < page_count; ++i) {
256 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300257 while (nRet == PDF_DATA_NOTAVAIL) {
258 network_simulator->SetRequestedDataAvailable();
259 nRet = FPDFAvail_IsPageAvail(*avail, i,
260 network_simulator->GetDownloadHints());
261 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400262 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800263 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800264 }
265 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000266 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800267 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300268 network_simulator->SetWholeFileAvailable();
269 *document =
270 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400271 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800272 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800273 }
Tom Sepez0784c732018-04-23 18:02:57 +0000274 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
275
Ryan Harrison854d71c2017-10-18 12:28:14 -0400276 int doc_type = FPDF_GetFormType(*document);
277 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
278 FPDF_LoadXFA(*document);
Tom Sepez0784c732018-04-23 18:02:57 +0000279
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000280 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800281 return true;
282}
Tom Sepez96d13342015-01-16 14:59:26 -0800283
Tom Sepez38f725f2019-12-04 00:26:34 +0000284void EmbedderTest::CloseDocument() {
Lei Zhang44005192020-04-17 01:35:43 +0000285 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepez38f725f2019-12-04 00:26:34 +0000286 FPDFDOC_ExitFormFillEnvironment(form_handle_);
287 form_handle_ = nullptr;
288
289 FPDF_CloseDocument(document_);
290 document_ = nullptr;
291}
292
Tom Sepez0784c732018-04-23 18:02:57 +0000293FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
294 FPDF_DOCUMENT doc,
295 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800296 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400297 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200298 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800299 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400300 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800301
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800302 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
303 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Aayush Dhirc63913e2020-02-12 09:45:34 +0000304 formfillinfo->version = form_fill_info_version_;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700305 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
306 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700307 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000308 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000309 formfillinfo->FFI_DoGoToAction = DoGoToActionTrampoline;
Aayush Dhirc63913e2020-02-12 09:45:34 +0000310 formfillinfo->FFI_OnFocusChange = OnFocusChangeTrampoline;
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000311 formfillinfo->FFI_DoURIActionWithKeyboardModifier =
312 DoURIActionWithKeyboardModifierTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000313
Tom Sepez0784c732018-04-23 18:02:57 +0000314 if (javascript_option == JavaScriptOption::kEnableJavaScript)
315 formfillinfo->m_pJsPlatform = platform;
316
Nicolas Pena3ff54002017-07-05 11:55:35 -0400317 FPDF_FORMHANDLE form_handle =
318 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Tom Sepez92f92222020-01-07 22:16:07 +0000319 SetInitialFormFieldHighlight(form_handle);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400320 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800321}
322
Tom Sepezda8189e2015-01-30 14:41:50 -0800323void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400324 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800325 FORM_DoDocumentJSAction(form_handle_);
326 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800327}
328
329int EmbedderTest::GetFirstPageNum() {
330 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000331 (void)FPDFAvail_IsPageAvail(avail_, first_page,
332 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800333 return first_page;
334}
335
336int EmbedderTest::GetPageCount() {
337 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400338 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000339 (void)FPDFAvail_IsPageAvail(avail_, i,
340 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800341 return page_count;
342}
343
Tom Sepezda8189e2015-01-30 14:41:50 -0800344FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000345 return LoadPageCommon(page_number, true);
346}
347
348FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
349 return LoadPageCommon(page_number, false);
350}
351
352FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400353 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000354 ASSERT(page_number >= 0);
Lei Zhangf245ae62020-05-15 21:49:46 +0000355 ASSERT(!pdfium::Contains(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700356
Tom Sepez96d13342015-01-16 14:59:26 -0800357 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400358 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800359 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400360
Tom Sepez507d0192018-11-07 16:37:51 +0000361 if (do_events) {
362 FORM_OnAfterLoadPage(page, form_handle_);
363 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
364 }
weili0dadcc62016-08-23 21:10:57 -0700365 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700366 return page;
367}
368
Tom Sepezda8189e2015-01-30 14:41:50 -0800369void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000370 UnloadPageCommon(page, true);
371}
Lei Zhang75c81712018-02-08 17:22:39 +0000372
Tom Sepez507d0192018-11-07 16:37:51 +0000373void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
374 UnloadPageCommon(page, false);
375}
376
377void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
378 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000379 int page_number = GetPageNumberForLoadedPage(page);
380 if (page_number < 0) {
381 NOTREACHED();
382 return;
383 }
Tom Sepez507d0192018-11-07 16:37:51 +0000384 if (do_events) {
385 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
386 FORM_OnBeforeClosePage(page, form_handle_);
387 }
Tom Sepez96d13342015-01-16 14:59:26 -0800388 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000389 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800390}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800391
Tom Sepez92f92222020-01-07 22:16:07 +0000392void EmbedderTest::SetInitialFormFieldHighlight(FPDF_FORMHANDLE form) {
393 FPDF_SetFormFieldHighlightColor(form, FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD);
394 FPDF_SetFormFieldHighlightAlpha(form, 100);
395}
396
Tom Sepeze08d2b12018-04-25 18:49:32 +0000397ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000398 return RenderLoadedPageWithFlags(page, 0);
399}
400
Tom Sepeze08d2b12018-04-25 18:49:32 +0000401ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
402 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000403 if (GetPageNumberForLoadedPage(page) < 0) {
404 NOTREACHED();
405 return nullptr;
406 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000407 return RenderPageWithFlags(page, form_handle_, flags);
408}
409
Tom Sepeze08d2b12018-04-25 18:49:32 +0000410ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000411 return RenderSavedPageWithFlags(page, 0);
412}
413
Tom Sepeze08d2b12018-04-25 18:49:32 +0000414ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
415 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000416 if (GetPageNumberForSavedPage(page) < 0) {
417 NOTREACHED();
418 return nullptr;
419 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000420 return RenderPageWithFlags(page, saved_form_handle_, flags);
421}
422
423// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000424ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
425 FPDF_FORMHANDLE handle,
426 int flags) {
Lei Zhangdfa075b2019-12-05 21:52:43 +0000427 int width = static_cast<int>(FPDF_GetPageWidthF(page));
428 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhanga98e3662018-02-07 20:28:35 +0000429 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000430 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000431 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
432 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
433 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
434 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
435 return bitmap;
436}
437
Lei Zhang30ff2532019-01-31 21:37:55 +0000438// static
439ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
440 return RenderPageWithFlags(page, nullptr, 0);
441}
442
Lei Zhangd69e0652019-04-13 00:39:35 +0000443#if defined(OS_WIN)
444// static
445std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
446 int flags) {
447 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
448
Lei Zhangdfa075b2019-12-05 21:52:43 +0000449 int width = static_cast<int>(FPDF_GetPageWidthF(page));
450 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhangd69e0652019-04-13 00:39:35 +0000451 HRGN rgn = CreateRectRgn(0, 0, width, height);
452 SelectClipRgn(dc, rgn);
453 DeleteObject(rgn);
454
455 SelectObject(dc, GetStockObject(NULL_PEN));
456 SelectObject(dc, GetStockObject(WHITE_BRUSH));
457 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
458 Rectangle(dc, 0, 0, width + 1, height + 1);
459
460 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
461
462 HENHMETAFILE emf = CloseEnhMetaFile(dc);
463 size_t size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
464 std::vector<uint8_t> buffer(size_in_bytes);
465 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
466 DeleteEnhMetaFile(emf);
467 return buffer;
468}
Lei Zhang614f1a02019-05-30 00:03:53 +0000469
470// static
471std::string EmbedderTest::GetPostScriptFromEmf(
Tom Sepez87b9e3a2019-11-20 23:45:43 +0000472 pdfium::span<const uint8_t> emf_data) {
Lei Zhang614f1a02019-05-30 00:03:53 +0000473 // This comes from Emf::InitFromData() in Chromium.
474 HENHMETAFILE emf = SetEnhMetaFileBits(emf_data.size(), emf_data.data());
475 if (!emf)
476 return std::string();
477
478 // This comes from Emf::Enumerator::Enumerator() in Chromium.
479 std::vector<const ENHMETARECORD*> records;
480 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
481 DeleteEnhMetaFile(emf);
482 return std::string();
483 }
484
485 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
486 std::string ps_data;
487 for (const auto* record : records) {
488 if (record->iType != EMR_GDICOMMENT)
489 continue;
490
491 // PostScript data is encapsulated inside EMF comment records.
492 // The first two bytes of the comment indicate the string length. The rest
493 // is the actual string data.
494 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
495 const char* data = reinterpret_cast<const char*>(comment->Data);
496 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
497 data += 2;
498 ps_data.append(data, size);
499 }
500 DeleteEnhMetaFile(emf);
501 return ps_data;
502}
Lei Zhangd69e0652019-04-13 00:39:35 +0000503#endif // defined(OS_WIN)
504
Lei Zhang0b494052019-01-31 21:41:15 +0000505FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
506 return OpenSavedDocumentWithPassword(nullptr);
507}
508
Tom Sepez6dc12402020-01-07 21:02:17 +0000509// static
510int EmbedderTest::BytesPerPixelForFormat(int format) {
511 switch (format) {
512 case FPDFBitmap_Gray:
513 return 1;
514 case FPDFBitmap_BGR:
515 return 3;
516 case FPDFBitmap_BGRx:
517 case FPDFBitmap_BGRA:
518 return 4;
519 default:
520 NOTREACHED();
521 return 0;
522 }
523}
524
Lei Zhang0b494052019-01-31 21:41:15 +0000525FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
526 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300527 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000528 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300529 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000530 // Copy data to prevent clearing it before saved document close.
531 saved_document_file_data_ = data_string_;
532 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400533
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300534 saved_fake_file_access_ =
Tom Sepez7df04832020-05-18 22:09:31 +0000535 std::make_unique<FakeFileAccess>(&saved_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300536
Tom Sepez0784c732018-04-23 18:02:57 +0000537 EXPECT_TRUE(OpenDocumentHelper(
538 password, LinearizeOption::kDefaultLinearize,
539 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
540 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000541 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400542}
543
544void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000545 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400546
Lei Zhang0729be22018-02-05 21:13:51 +0000547 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
548 FPDF_CloseDocument(saved_document_);
549 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400550
Lei Zhang0729be22018-02-05 21:13:51 +0000551 saved_form_handle_ = nullptr;
552 saved_document_ = nullptr;
553 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400554}
555
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000556FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000557 ASSERT(saved_form_handle_);
558 ASSERT(page_number >= 0);
Lei Zhangf245ae62020-05-15 21:49:46 +0000559 ASSERT(!pdfium::Contains(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400560
Lei Zhang0729be22018-02-05 21:13:51 +0000561 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000562 if (!page)
563 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400564
Lei Zhang9f72c452018-02-08 21:49:54 +0000565 FORM_OnAfterLoadPage(page, saved_form_handle_);
566 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
567 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000568 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400569}
570
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000571void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000572 ASSERT(saved_form_handle_);
573
574 int page_number = GetPageNumberForSavedPage(page);
575 if (page_number < 0) {
576 NOTREACHED();
577 return;
578 }
579
580 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
581 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000582 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000583
584 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400585}
586
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000587void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
588 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400589 int height,
590 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000591 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000592 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400593
Tom Sepeze08d2b12018-04-25 18:49:32 +0000594 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000595 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400596}
597
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400598void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000599 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000600 FPDF_PAGE page = LoadSavedPage(0);
601 VerifySavedRendering(page, width, height, md5);
602 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400603 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400604}
605
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300606void EmbedderTest::SetWholeFileAvailable() {
607 ASSERT(fake_file_access_);
608 fake_file_access_->SetWholeFileAvailable();
609}
610
weili0dadcc62016-08-23 21:10:57 -0700611FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700612 FPDF_DOCUMENT document,
613 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700614 EmbedderTest* test = static_cast<EmbedderTest*>(info);
615 auto it = test->page_map_.find(page_index);
616 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700617}
618
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800619// static
620void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
621 int type) {
622 EmbedderTest* test = static_cast<EmbedderTest*>(info);
623 test->delegate_->UnsupportedHandler(type);
624}
625
626// static
627int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
628 FPDF_WIDESTRING message,
629 FPDF_WIDESTRING title,
630 int type,
631 int icon) {
632 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
633 return test->delegate_->Alert(message, title, type, icon);
634}
635
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700636// static
637int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638 int msecs,
639 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700640 EmbedderTest* test = static_cast<EmbedderTest*>(info);
641 return test->delegate_->SetTimer(msecs, fn);
642}
643
644// static
645void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
646 EmbedderTest* test = static_cast<EmbedderTest*>(info);
647 return test->delegate_->KillTimer(id);
648}
649
Tom Sepez396e8722015-09-09 10:16:08 -0700650// static
651FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
652 FPDF_DOCUMENT document,
653 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700654 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
655 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700656}
657
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000658// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000659void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
660 FPDF_BYTESTRING uri) {
661 EmbedderTest* test = static_cast<EmbedderTest*>(info);
662 return test->delegate_->DoURIAction(uri);
663}
664
665// static
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000666void EmbedderTest::DoGoToActionTrampoline(FPDF_FORMFILLINFO* info,
667 int page_index,
668 int zoom_mode,
669 float* pos_array,
670 int array_size) {
671 EmbedderTest* test = static_cast<EmbedderTest*>(info);
672 return test->delegate_->DoGoToAction(info, page_index, zoom_mode, pos_array,
673 array_size);
674}
675
676// static
Aayush Dhirc63913e2020-02-12 09:45:34 +0000677void EmbedderTest::OnFocusChangeTrampoline(FPDF_FORMFILLINFO* info,
678 FPDF_ANNOTATION annot,
679 int page_index) {
680 EmbedderTest* test = static_cast<EmbedderTest*>(info);
681 return test->delegate_->OnFocusChange(info, annot, page_index);
682}
683
684// static
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000685void EmbedderTest::DoURIActionWithKeyboardModifierTrampoline(
686 FPDF_FORMFILLINFO* info,
687 FPDF_BYTESTRING uri,
688 int modifiers) {
689 EmbedderTest* test = static_cast<EmbedderTest*>(info);
690 return test->delegate_->DoURIActionWithKeyboardModifier(info, uri, modifiers);
691}
692
693// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000694std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Lei Zhangbf980142019-12-20 01:05:42 +0000695 int stride = FPDFBitmap_GetStride(bitmap);
696 int usable_bytes_per_row =
697 GetBitmapBytesPerPixel(bitmap) * FPDFBitmap_GetWidth(bitmap);
698 int height = FPDFBitmap_GetHeight(bitmap);
699 auto span = pdfium::make_span(
700 static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)), stride * height);
701
702 CRYPT_md5_context context = CRYPT_MD5Start();
703 for (int i = 0; i < height; ++i)
704 CRYPT_MD5Update(&context, span.subspan(i * stride, usable_bytes_per_row));
Dan Sinclair957480c2017-06-13 15:21:14 -0400705 uint8_t digest[16];
Lei Zhangbf980142019-12-20 01:05:42 +0000706 CRYPT_MD5Finish(&context, digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400707 return CryptToBase16(digest);
708}
709
Henrique Nakashimadb269572018-01-16 19:02:15 +0000710#ifndef NDEBUG
711// static
712void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
713 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000714 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000715}
716#endif
717
thestigbcd3e532016-11-21 13:37:28 -0800718// static
719void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
720 int expected_width,
721 int expected_height,
722 const char* expected_md5sum) {
723 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
724 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400725
726 // The expected stride is calculated using the same formula as in
727 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
728 const int expected_stride =
729 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800730 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
731
732 if (!expected_md5sum)
733 return;
734
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000735 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800736}
737
Nicolas Pena3ff54002017-07-05 11:55:35 -0400738// static
739int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
740 const void* data,
741 unsigned long size) {
742 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000743
Lei Zhang0729be22018-02-05 21:13:51 +0000744 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000745
746 if (pThis->filestream_.is_open())
747 pThis->filestream_.write(static_cast<const char*>(data), size);
748
Nicolas Pena3ff54002017-07-05 11:55:35 -0400749 return 1;
750}
751
752// static
753int EmbedderTest::GetBlockFromString(void* param,
754 unsigned long pos,
755 unsigned char* buf,
756 unsigned long size) {
757 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000758 if (!new_file || pos + size < pos) {
759 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400760 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000761 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400762
Lei Zhangcef91f12019-01-08 21:32:51 +0000763 if (pos + size > new_file->size()) {
764 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400765 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000766 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400767
768 memcpy(buf, new_file->data() + pos, size);
769 return 1;
770}
Lei Zhang75c81712018-02-08 17:22:39 +0000771
Lei Zhang9f72c452018-02-08 21:49:54 +0000772// static
773int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
774 FPDF_PAGE page) {
775 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000776 if (it.second == page) {
777 int page_number = it.first;
778 ASSERT(page_number >= 0);
779 return page_number;
780 }
781 }
782 return -1;
783}
Lei Zhang9f72c452018-02-08 21:49:54 +0000784
785int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
786 return GetPageNumberForPage(page_map_, page);
787}
788
789int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
790 return GetPageNumberForPage(saved_page_map_, page);
791}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000792
Lei Zhang81a799e2019-12-16 17:37:13 +0000793#ifndef NDEBUG
794void EmbedderTest::OpenPDFFileForWrite(const std::string& filename) {
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000795 filestream_.open(filename, std::ios_base::binary);
796}
797
798void EmbedderTest::ClosePDFFileForWrite() {
799 filestream_.close();
800}
Lei Zhang81a799e2019-12-16 17:37:13 +0000801#endif