blob: 9dd001f817d1243b64ce0a77e1f8bfee678247e7 [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
9#include <list>
Lei Zhang9f72c452018-02-08 21:49:54 +000010#include <map>
Lei Zhanga98e3662018-02-07 20:28:35 +000011#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -080012#include <string>
13#include <utility>
Lei Zhangd69e0652019-04-13 00:39:35 +000014#include <vector>
Tom Sepez96d13342015-01-16 14:59:26 -080015
Lei Zhangd145e4b2018-10-12 18:54:31 +000016#include "core/fdrm/fx_crypt.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000017#include "public/cpp/fpdf_scopers.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080018#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080019#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080020#include "public/fpdf_text.h"
21#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080022#include "testing/gmock/include/gmock/gmock.h"
Lei Zhangd50bdff2019-02-05 19:42:33 +000023#include "testing/test_loader.h"
Henrique Nakashimaf956bad2018-08-16 16:41:42 +000024#include "testing/utils/bitmap_saver.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000025#include "testing/utils/file_util.h"
Lei Zhang4c64e962019-02-05 19:24:12 +000026#include "testing/utils/hash.h"
Wei Li091f7a02015-11-09 12:09:55 -080027#include "testing/utils/path_service.h"
Lei Zhang75c81712018-02-08 17:22:39 +000028#include "third_party/base/logging.h"
Lei Zhang75c81712018-02-08 17:22:39 +000029#include "third_party/base/stl_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070030
31#ifdef PDF_ENABLE_V8
Tom Sepezaf33f512020-06-12 01:00:10 +000032#include "testing/v8_initializer.h"
Lei Zhang8241df72015-11-06 14:38:48 -080033#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050034#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070035#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080036
Tom Sepez96d13342015-01-16 14:59:26 -080037namespace {
thestigc08cd7a2016-06-27 09:47:59 -070038
Tom Sepezaf33f512020-06-12 01:00:10 +000039EmbedderTestEnvironment* g_environment = nullptr;
40
Jane Liu28fb7ba2017-08-02 21:45:57 -040041int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
Tom Sepez6dc12402020-01-07 21:02:17 +000042 return EmbedderTest::BytesPerPixelForFormat(FPDFBitmap_GetFormat(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -040043}
44
Lei Zhang614f1a02019-05-30 00:03:53 +000045#if defined(OS_WIN)
46int CALLBACK GetRecordProc(HDC hdc,
47 HANDLETABLE* handle_table,
48 const ENHMETARECORD* record,
49 int objects_count,
50 LPARAM param) {
51 auto& records = *reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param);
52 records.push_back(record);
53 return 1;
54}
55#endif // defined(OS_WIN)
56
thestigbcd3e532016-11-21 13:37:28 -080057} // namespace
58
Tom Sepezaf33f512020-06-12 01:00:10 +000059EmbedderTestEnvironment::EmbedderTestEnvironment(const char* exe_name)
60#ifdef PDF_ENABLE_V8
61 : exe_path_(exe_name)
62#endif
63{
64 ASSERT(!g_environment);
65 g_environment = this;
66}
67
68EmbedderTestEnvironment::~EmbedderTestEnvironment() {
69 ASSERT(g_environment);
70 g_environment = nullptr;
71
72#ifdef PDF_ENABLE_V8
73#ifdef V8_USE_EXTERNAL_STARTUP_DATA
74 if (v8_snapshot_)
75 free(const_cast<char*>(v8_snapshot_->data));
76#endif // V8_USE_EXTERNAL_STARTUP_DATA
77#endif // PDF_ENABLE_V8
78}
79
80// static
81EmbedderTestEnvironment* EmbedderTestEnvironment::GetInstance() {
82 return g_environment;
83}
84
85void EmbedderTestEnvironment::SetUp() {
86#ifdef PDF_ENABLE_V8
87#ifdef V8_USE_EXTERNAL_STARTUP_DATA
88 if (v8_snapshot_) {
89 platform_ =
90 InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(), nullptr);
91 } else {
92 v8_snapshot_ = std::make_unique<v8::StartupData>();
93 platform_ = InitializeV8ForPDFiumWithStartupData(exe_path_, std::string(),
94 v8_snapshot_.get());
95 }
96#else
97 platform_ = InitializeV8ForPDFium(exe_path_);
98#endif // V8_USE_EXTERNAL_STARTUP_DATA
99#endif // FPDF_ENABLE_V8
100}
101
102void EmbedderTestEnvironment::TearDown() {
103#ifdef PDF_ENABLE_V8
104 v8::V8::ShutdownPlatform();
105#endif // PDF_ENABLE_V8
106}
107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108EmbedderTest::EmbedderTest()
Tom Sepez7df04832020-05-18 22:09:31 +0000109 : default_delegate_(std::make_unique<EmbedderTest::Delegate>()),
Lei Zhang0729be22018-02-05 21:13:51 +0000110 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400111 FPDF_FILEWRITE::version = 1;
112 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -0800113}
Tom Sepez96d13342015-01-16 14:59:26 -0800114
Lei Zhangea208512019-12-18 00:42:11 +0000115EmbedderTest::~EmbedderTest() = default;
Tom Sepezf288bb12015-11-20 12:12:46 -0800116
117void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -0700118 FPDF_LIBRARY_CONFIG config;
119 config.version = 2;
120 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700121 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -0700122 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700123 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -0800124
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
126 memset(info, 0, sizeof(UNSUPPORT_INFO));
127 info->version = 1;
128 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
129 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000130
Lei Zhang0729be22018-02-05 21:13:51 +0000131 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132}
Tom Sepez96d13342015-01-16 14:59:26 -0800133
134void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +0000135 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
136 // possible. This can fail when an ASSERT test fails in a test case.
137 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +0000138 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang75c81712018-02-08 17:22:39 +0000139
Lei Zhang44005192020-04-17 01:35:43 +0000140 if (document_)
Tom Sepez38f725f2019-12-04 00:26:34 +0000141 CloseDocument();
Tom Sepezc46d0002015-11-30 15:46:36 -0800142
Tom Sepez96d13342015-01-16 14:59:26 -0800143 FPDFAvail_Destroy(avail_);
144 FPDF_DestroyLibrary();
Lei Zhangb3be4a12019-02-05 22:11:07 +0000145 loader_.reset();
Tom Sepez96d13342015-01-16 14:59:26 -0800146}
147
Lei Zhang378ec542018-10-18 19:15:47 +0000148#ifdef PDF_ENABLE_V8
149void EmbedderTest::SetExternalIsolate(void* isolate) {
150 external_isolate_ = static_cast<v8::Isolate*>(isolate);
151}
152#endif // PDF_ENABLE_V8
153
Tom Sepezd483eb42016-01-06 10:03:59 -0800154bool EmbedderTest::CreateEmptyDocument() {
155 document_ = FPDF_CreateNewDocument();
156 if (!document_)
157 return false;
158
Tom Sepez0784c732018-04-23 18:02:57 +0000159 form_handle_ =
160 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -0800161 return true;
162}
163
Lei Zhang208eecf2017-12-20 19:40:50 +0000164bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000165 return OpenDocumentWithOptions(filename, nullptr,
166 LinearizeOption::kDefaultLinearize,
167 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000168}
169
170bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000171 return OpenDocumentWithOptions(filename, nullptr,
172 LinearizeOption::kMustLinearize,
173 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000174}
175
176bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
177 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000178 return OpenDocumentWithOptions(filename, password,
179 LinearizeOption::kDefaultLinearize,
180 JavaScriptOption::kEnableJavaScript);
181}
182
183bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
184 return OpenDocumentWithOptions(filename, nullptr,
185 LinearizeOption::kDefaultLinearize,
186 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000187}
188
189bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
190 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000191 LinearizeOption linearize_option,
192 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800193 std::string file_path;
194 if (!PathService::GetTestFilePath(filename, &file_path))
195 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000196
Wei Li091f7a02015-11-09 12:09:55 -0800197 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400198 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800199 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800200
thestig29ce9232016-06-22 07:03:23 -0700201 EXPECT_TRUE(!loader_);
Tom Sepez7df04832020-05-18 22:09:31 +0000202 loader_ = std::make_unique<TestLoader>(
Lei Zhangb3be4a12019-02-05 22:11:07 +0000203 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000204
205 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800206 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700207 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000208 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000209
Tom Sepez7df04832020-05-18 22:09:31 +0000210 fake_file_access_ = std::make_unique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000211 return OpenDocumentHelper(password, linearize_option, javascript_option,
212 fake_file_access_.get(), &document_, &avail_,
213 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400214}
Tom Sepez96d13342015-01-16 14:59:26 -0800215
Nicolas Pena56fc9722017-07-13 16:31:34 -0400216bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000217 LinearizeOption linearize_option,
218 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300219 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400220 FPDF_DOCUMENT* document,
221 FPDF_AVAIL* avail,
222 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300223 network_simulator->AddSegment(0, 1024);
224 network_simulator->SetRequestedDataAvailable();
225 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
226 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400227 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300228 int32_t nRet = PDF_DATA_NOTAVAIL;
229 while (nRet == PDF_DATA_NOTAVAIL) {
230 network_simulator->SetRequestedDataAvailable();
231 nRet =
232 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
233 }
234 if (nRet == PDF_DATA_ERROR)
235 return false;
236
Nicolas Pena56fc9722017-07-13 16:31:34 -0400237 *document = FPDFAvail_GetDocument(*avail, password);
238 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800239 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400240
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300241 nRet = PDF_DATA_NOTAVAIL;
242 while (nRet == PDF_DATA_NOTAVAIL) {
243 network_simulator->SetRequestedDataAvailable();
244 nRet =
245 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
246 }
247 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800248 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400249
250 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800251 for (int i = 0; i < page_count; ++i) {
252 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300253 while (nRet == PDF_DATA_NOTAVAIL) {
254 network_simulator->SetRequestedDataAvailable();
255 nRet = FPDFAvail_IsPageAvail(*avail, i,
256 network_simulator->GetDownloadHints());
257 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400258 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800259 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800260 }
261 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000262 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800263 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300264 network_simulator->SetWholeFileAvailable();
265 *document =
266 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400267 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800268 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800269 }
Tom Sepez0784c732018-04-23 18:02:57 +0000270 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
271
Ryan Harrison854d71c2017-10-18 12:28:14 -0400272 int doc_type = FPDF_GetFormType(*document);
273 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
274 FPDF_LoadXFA(*document);
Tom Sepez0784c732018-04-23 18:02:57 +0000275
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000276 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800277 return true;
278}
Tom Sepez96d13342015-01-16 14:59:26 -0800279
Tom Sepez38f725f2019-12-04 00:26:34 +0000280void EmbedderTest::CloseDocument() {
Lei Zhang44005192020-04-17 01:35:43 +0000281 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepez38f725f2019-12-04 00:26:34 +0000282 FPDFDOC_ExitFormFillEnvironment(form_handle_);
283 form_handle_ = nullptr;
284
285 FPDF_CloseDocument(document_);
286 document_ = nullptr;
287}
288
Tom Sepez0784c732018-04-23 18:02:57 +0000289FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
290 FPDF_DOCUMENT doc,
291 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800292 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400293 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200294 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800295 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400296 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800297
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800298 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
299 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Aayush Dhirc63913e2020-02-12 09:45:34 +0000300 formfillinfo->version = form_fill_info_version_;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700301 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
302 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700303 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000304 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000305 formfillinfo->FFI_DoGoToAction = DoGoToActionTrampoline;
Aayush Dhirc63913e2020-02-12 09:45:34 +0000306 formfillinfo->FFI_OnFocusChange = OnFocusChangeTrampoline;
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000307 formfillinfo->FFI_DoURIActionWithKeyboardModifier =
308 DoURIActionWithKeyboardModifierTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000309
Tom Sepez0784c732018-04-23 18:02:57 +0000310 if (javascript_option == JavaScriptOption::kEnableJavaScript)
311 formfillinfo->m_pJsPlatform = platform;
312
Nicolas Pena3ff54002017-07-05 11:55:35 -0400313 FPDF_FORMHANDLE form_handle =
314 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Tom Sepez92f92222020-01-07 22:16:07 +0000315 SetInitialFormFieldHighlight(form_handle);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400316 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800317}
318
Tom Sepezda8189e2015-01-30 14:41:50 -0800319void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400320 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800321 FORM_DoDocumentJSAction(form_handle_);
322 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800323}
324
325int EmbedderTest::GetFirstPageNum() {
326 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000327 (void)FPDFAvail_IsPageAvail(avail_, first_page,
328 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800329 return first_page;
330}
331
332int EmbedderTest::GetPageCount() {
333 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400334 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000335 (void)FPDFAvail_IsPageAvail(avail_, i,
336 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800337 return page_count;
338}
339
Tom Sepezda8189e2015-01-30 14:41:50 -0800340FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000341 return LoadPageCommon(page_number, true);
342}
343
344FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
345 return LoadPageCommon(page_number, false);
346}
347
348FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400349 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000350 ASSERT(page_number >= 0);
Lei Zhangf245ae62020-05-15 21:49:46 +0000351 ASSERT(!pdfium::Contains(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700352
Tom Sepez96d13342015-01-16 14:59:26 -0800353 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400354 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800355 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400356
Tom Sepez507d0192018-11-07 16:37:51 +0000357 if (do_events) {
358 FORM_OnAfterLoadPage(page, form_handle_);
359 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
360 }
weili0dadcc62016-08-23 21:10:57 -0700361 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700362 return page;
363}
364
Tom Sepezda8189e2015-01-30 14:41:50 -0800365void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000366 UnloadPageCommon(page, true);
367}
Lei Zhang75c81712018-02-08 17:22:39 +0000368
Tom Sepez507d0192018-11-07 16:37:51 +0000369void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
370 UnloadPageCommon(page, false);
371}
372
373void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
374 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000375 int page_number = GetPageNumberForLoadedPage(page);
376 if (page_number < 0) {
377 NOTREACHED();
378 return;
379 }
Tom Sepez507d0192018-11-07 16:37:51 +0000380 if (do_events) {
381 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
382 FORM_OnBeforeClosePage(page, form_handle_);
383 }
Tom Sepez96d13342015-01-16 14:59:26 -0800384 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000385 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800386}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800387
Tom Sepez92f92222020-01-07 22:16:07 +0000388void EmbedderTest::SetInitialFormFieldHighlight(FPDF_FORMHANDLE form) {
389 FPDF_SetFormFieldHighlightColor(form, FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD);
390 FPDF_SetFormFieldHighlightAlpha(form, 100);
391}
392
Tom Sepeze08d2b12018-04-25 18:49:32 +0000393ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000394 return RenderLoadedPageWithFlags(page, 0);
395}
396
Tom Sepeze08d2b12018-04-25 18:49:32 +0000397ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
398 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000399 if (GetPageNumberForLoadedPage(page) < 0) {
400 NOTREACHED();
401 return nullptr;
402 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000403 return RenderPageWithFlags(page, form_handle_, flags);
404}
405
Tom Sepeze08d2b12018-04-25 18:49:32 +0000406ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000407 return RenderSavedPageWithFlags(page, 0);
408}
409
Tom Sepeze08d2b12018-04-25 18:49:32 +0000410ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
411 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000412 if (GetPageNumberForSavedPage(page) < 0) {
413 NOTREACHED();
414 return nullptr;
415 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000416 return RenderPageWithFlags(page, saved_form_handle_, flags);
417}
418
419// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000420ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
421 FPDF_FORMHANDLE handle,
422 int flags) {
Lei Zhangdfa075b2019-12-05 21:52:43 +0000423 int width = static_cast<int>(FPDF_GetPageWidthF(page));
424 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhanga98e3662018-02-07 20:28:35 +0000425 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000426 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000427 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
428 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
429 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
430 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
431 return bitmap;
432}
433
Lei Zhang30ff2532019-01-31 21:37:55 +0000434// static
435ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
436 return RenderPageWithFlags(page, nullptr, 0);
437}
438
Lei Zhangd69e0652019-04-13 00:39:35 +0000439#if defined(OS_WIN)
440// static
441std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
442 int flags) {
443 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
444
Lei Zhangdfa075b2019-12-05 21:52:43 +0000445 int width = static_cast<int>(FPDF_GetPageWidthF(page));
446 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhangd69e0652019-04-13 00:39:35 +0000447 HRGN rgn = CreateRectRgn(0, 0, width, height);
448 SelectClipRgn(dc, rgn);
449 DeleteObject(rgn);
450
451 SelectObject(dc, GetStockObject(NULL_PEN));
452 SelectObject(dc, GetStockObject(WHITE_BRUSH));
453 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
454 Rectangle(dc, 0, 0, width + 1, height + 1);
455
456 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
457
458 HENHMETAFILE emf = CloseEnhMetaFile(dc);
459 size_t size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
460 std::vector<uint8_t> buffer(size_in_bytes);
461 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
462 DeleteEnhMetaFile(emf);
463 return buffer;
464}
Lei Zhang614f1a02019-05-30 00:03:53 +0000465
466// static
467std::string EmbedderTest::GetPostScriptFromEmf(
Tom Sepez87b9e3a2019-11-20 23:45:43 +0000468 pdfium::span<const uint8_t> emf_data) {
Lei Zhang614f1a02019-05-30 00:03:53 +0000469 // This comes from Emf::InitFromData() in Chromium.
470 HENHMETAFILE emf = SetEnhMetaFileBits(emf_data.size(), emf_data.data());
471 if (!emf)
472 return std::string();
473
474 // This comes from Emf::Enumerator::Enumerator() in Chromium.
475 std::vector<const ENHMETARECORD*> records;
476 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
477 DeleteEnhMetaFile(emf);
478 return std::string();
479 }
480
481 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
482 std::string ps_data;
483 for (const auto* record : records) {
484 if (record->iType != EMR_GDICOMMENT)
485 continue;
486
487 // PostScript data is encapsulated inside EMF comment records.
488 // The first two bytes of the comment indicate the string length. The rest
489 // is the actual string data.
490 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
491 const char* data = reinterpret_cast<const char*>(comment->Data);
492 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
493 data += 2;
494 ps_data.append(data, size);
495 }
496 DeleteEnhMetaFile(emf);
497 return ps_data;
498}
Lei Zhangd69e0652019-04-13 00:39:35 +0000499#endif // defined(OS_WIN)
500
Lei Zhang0b494052019-01-31 21:41:15 +0000501FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
502 return OpenSavedDocumentWithPassword(nullptr);
503}
504
Tom Sepez6dc12402020-01-07 21:02:17 +0000505// static
506int EmbedderTest::BytesPerPixelForFormat(int format) {
507 switch (format) {
508 case FPDFBitmap_Gray:
509 return 1;
510 case FPDFBitmap_BGR:
511 return 3;
512 case FPDFBitmap_BGRx:
513 case FPDFBitmap_BGRA:
514 return 4;
515 default:
516 NOTREACHED();
517 return 0;
518 }
519}
520
Lei Zhang0b494052019-01-31 21:41:15 +0000521FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
522 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300523 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000524 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300525 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000526 // Copy data to prevent clearing it before saved document close.
527 saved_document_file_data_ = data_string_;
528 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400529
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300530 saved_fake_file_access_ =
Tom Sepez7df04832020-05-18 22:09:31 +0000531 std::make_unique<FakeFileAccess>(&saved_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300532
Tom Sepez0784c732018-04-23 18:02:57 +0000533 EXPECT_TRUE(OpenDocumentHelper(
534 password, LinearizeOption::kDefaultLinearize,
535 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
536 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000537 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400538}
539
540void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000541 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400542
Lei Zhang0729be22018-02-05 21:13:51 +0000543 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
544 FPDF_CloseDocument(saved_document_);
545 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400546
Lei Zhang0729be22018-02-05 21:13:51 +0000547 saved_form_handle_ = nullptr;
548 saved_document_ = nullptr;
549 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400550}
551
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000552FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000553 ASSERT(saved_form_handle_);
554 ASSERT(page_number >= 0);
Lei Zhangf245ae62020-05-15 21:49:46 +0000555 ASSERT(!pdfium::Contains(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400556
Lei Zhang0729be22018-02-05 21:13:51 +0000557 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000558 if (!page)
559 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400560
Lei Zhang9f72c452018-02-08 21:49:54 +0000561 FORM_OnAfterLoadPage(page, saved_form_handle_);
562 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
563 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000564 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400565}
566
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000567void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000568 ASSERT(saved_form_handle_);
569
570 int page_number = GetPageNumberForSavedPage(page);
571 if (page_number < 0) {
572 NOTREACHED();
573 return;
574 }
575
576 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
577 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000578 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000579
580 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400581}
582
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000583void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
584 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400585 int height,
586 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000587 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000588 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400589
Tom Sepeze08d2b12018-04-25 18:49:32 +0000590 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000591 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400592}
593
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400594void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000595 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000596 FPDF_PAGE page = LoadSavedPage(0);
597 VerifySavedRendering(page, width, height, md5);
598 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400599 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400600}
601
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300602void EmbedderTest::SetWholeFileAvailable() {
603 ASSERT(fake_file_access_);
604 fake_file_access_->SetWholeFileAvailable();
605}
606
weili0dadcc62016-08-23 21:10:57 -0700607FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700608 FPDF_DOCUMENT document,
609 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700610 EmbedderTest* test = static_cast<EmbedderTest*>(info);
611 auto it = test->page_map_.find(page_index);
612 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700613}
614
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800615// static
616void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
617 int type) {
618 EmbedderTest* test = static_cast<EmbedderTest*>(info);
619 test->delegate_->UnsupportedHandler(type);
620}
621
622// static
623int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
624 FPDF_WIDESTRING message,
625 FPDF_WIDESTRING title,
626 int type,
627 int icon) {
628 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
629 return test->delegate_->Alert(message, title, type, icon);
630}
631
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700632// static
633int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634 int msecs,
635 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700636 EmbedderTest* test = static_cast<EmbedderTest*>(info);
637 return test->delegate_->SetTimer(msecs, fn);
638}
639
640// static
641void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
642 EmbedderTest* test = static_cast<EmbedderTest*>(info);
643 return test->delegate_->KillTimer(id);
644}
645
Tom Sepez396e8722015-09-09 10:16:08 -0700646// static
647FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
648 FPDF_DOCUMENT document,
649 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700650 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
651 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700652}
653
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000654// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000655void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
656 FPDF_BYTESTRING uri) {
657 EmbedderTest* test = static_cast<EmbedderTest*>(info);
658 return test->delegate_->DoURIAction(uri);
659}
660
661// static
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000662void EmbedderTest::DoGoToActionTrampoline(FPDF_FORMFILLINFO* info,
663 int page_index,
664 int zoom_mode,
665 float* pos_array,
666 int array_size) {
667 EmbedderTest* test = static_cast<EmbedderTest*>(info);
668 return test->delegate_->DoGoToAction(info, page_index, zoom_mode, pos_array,
669 array_size);
670}
671
672// static
Aayush Dhirc63913e2020-02-12 09:45:34 +0000673void EmbedderTest::OnFocusChangeTrampoline(FPDF_FORMFILLINFO* info,
674 FPDF_ANNOTATION annot,
675 int page_index) {
676 EmbedderTest* test = static_cast<EmbedderTest*>(info);
677 return test->delegate_->OnFocusChange(info, annot, page_index);
678}
679
680// static
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000681void EmbedderTest::DoURIActionWithKeyboardModifierTrampoline(
682 FPDF_FORMFILLINFO* info,
683 FPDF_BYTESTRING uri,
684 int modifiers) {
685 EmbedderTest* test = static_cast<EmbedderTest*>(info);
686 return test->delegate_->DoURIActionWithKeyboardModifier(info, uri, modifiers);
687}
688
689// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000690std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Lei Zhangbf980142019-12-20 01:05:42 +0000691 int stride = FPDFBitmap_GetStride(bitmap);
692 int usable_bytes_per_row =
693 GetBitmapBytesPerPixel(bitmap) * FPDFBitmap_GetWidth(bitmap);
694 int height = FPDFBitmap_GetHeight(bitmap);
695 auto span = pdfium::make_span(
696 static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)), stride * height);
697
698 CRYPT_md5_context context = CRYPT_MD5Start();
699 for (int i = 0; i < height; ++i)
700 CRYPT_MD5Update(&context, span.subspan(i * stride, usable_bytes_per_row));
Dan Sinclair957480c2017-06-13 15:21:14 -0400701 uint8_t digest[16];
Lei Zhangbf980142019-12-20 01:05:42 +0000702 CRYPT_MD5Finish(&context, digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400703 return CryptToBase16(digest);
704}
705
Henrique Nakashimadb269572018-01-16 19:02:15 +0000706#ifndef NDEBUG
707// static
708void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
709 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000710 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000711}
712#endif
713
thestigbcd3e532016-11-21 13:37:28 -0800714// static
715void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
716 int expected_width,
717 int expected_height,
718 const char* expected_md5sum) {
719 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
720 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400721
722 // The expected stride is calculated using the same formula as in
723 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
724 const int expected_stride =
725 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800726 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
727
728 if (!expected_md5sum)
729 return;
730
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000731 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800732}
733
Nicolas Pena3ff54002017-07-05 11:55:35 -0400734// static
735int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
736 const void* data,
737 unsigned long size) {
738 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000739
Lei Zhang0729be22018-02-05 21:13:51 +0000740 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000741
742 if (pThis->filestream_.is_open())
743 pThis->filestream_.write(static_cast<const char*>(data), size);
744
Nicolas Pena3ff54002017-07-05 11:55:35 -0400745 return 1;
746}
747
748// static
749int EmbedderTest::GetBlockFromString(void* param,
750 unsigned long pos,
751 unsigned char* buf,
752 unsigned long size) {
753 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000754 if (!new_file || pos + size < pos) {
755 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400756 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000757 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400758
Lei Zhangcef91f12019-01-08 21:32:51 +0000759 if (pos + size > new_file->size()) {
760 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400761 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000762 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400763
764 memcpy(buf, new_file->data() + pos, size);
765 return 1;
766}
Lei Zhang75c81712018-02-08 17:22:39 +0000767
Lei Zhang9f72c452018-02-08 21:49:54 +0000768// static
769int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
770 FPDF_PAGE page) {
771 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000772 if (it.second == page) {
773 int page_number = it.first;
774 ASSERT(page_number >= 0);
775 return page_number;
776 }
777 }
778 return -1;
779}
Lei Zhang9f72c452018-02-08 21:49:54 +0000780
781int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
782 return GetPageNumberForPage(page_map_, page);
783}
784
785int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
786 return GetPageNumberForPage(saved_page_map_, page);
787}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000788
Lei Zhang81a799e2019-12-16 17:37:13 +0000789#ifndef NDEBUG
790void EmbedderTest::OpenPDFFileForWrite(const std::string& filename) {
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000791 filestream_.open(filename, std::ios_base::binary);
792}
793
794void EmbedderTest::ClosePDFFileForWrite() {
795 filestream_.close();
796}
Lei Zhang81a799e2019-12-16 17:37:13 +0000797#endif