blob: 794acf336e6f9afa8e2a811f7a32ae07b9df2d01 [file] [log] [blame]
Lei Zhang1ac47eb2015-12-21 11:04:44 -08001// Copyright 2015 PDFium Authors. All rights reserved.
Tom Sepez96d13342015-01-16 14:59:26 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Wei Li091f7a02015-11-09 12:09:55 -08005#include "testing/embedder_test.h"
Tom Sepez96d13342015-01-16 14:59:26 -08006
Lei Zhanga98e3662018-02-07 20:28:35 +00007#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -08008#include <string>
9#include <utility>
Lei Zhangd69e0652019-04-13 00:39:35 +000010#include <vector>
Tom Sepez96d13342015-01-16 14:59:26 -080011
Lei Zhangd145e4b2018-10-12 18:54:31 +000012#include "core/fdrm/fx_crypt.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000013#include "public/cpp/fpdf_scopers.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080014#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080015#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080016#include "public/fpdf_text.h"
17#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080018#include "testing/gmock/include/gmock/gmock.h"
Lei Zhangd50bdff2019-02-05 19:42:33 +000019#include "testing/test_loader.h"
Henrique Nakashimaf956bad2018-08-16 16:41:42 +000020#include "testing/utils/bitmap_saver.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000021#include "testing/utils/file_util.h"
Lei Zhang4c64e962019-02-05 19:24:12 +000022#include "testing/utils/hash.h"
Wei Li091f7a02015-11-09 12:09:55 -080023#include "testing/utils/path_service.h"
Tom Sepez25f33d02021-01-29 01:58:51 +000024#include "third_party/base/check.h"
Lei Zhang87e3d7a2021-06-17 19:40:28 +000025#include "third_party/base/containers/contains.h"
Lei Zhang30632072020-10-02 01:57:54 +000026#include "third_party/base/notreached.h"
Tom Sepezea03a7b2022-02-24 00:30:14 +000027#include "third_party/base/numerics/safe_conversions.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070028
29#ifdef PDF_ENABLE_V8
Tom Sepezeaf9d212020-07-22 16:24:00 +000030#include "testing/v8_test_environment.h"
Lei Zhang8241df72015-11-06 14:38:48 -080031#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050032#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070033#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080034
Tom Sepez96d13342015-01-16 14:59:26 -080035namespace {
thestigc08cd7a2016-06-27 09:47:59 -070036
Jane Liu28fb7ba2017-08-02 21:45:57 -040037int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
Tom Sepez6dc12402020-01-07 21:02:17 +000038 return EmbedderTest::BytesPerPixelForFormat(FPDFBitmap_GetFormat(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -040039}
40
Lei Zhangf997abe2022-01-12 19:14:23 +000041#if BUILDFLAG(IS_WIN)
Lei Zhang614f1a02019-05-30 00:03:53 +000042int CALLBACK GetRecordProc(HDC hdc,
43 HANDLETABLE* handle_table,
44 const ENHMETARECORD* record,
45 int objects_count,
46 LPARAM param) {
47 auto& records = *reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param);
48 records.push_back(record);
49 return 1;
50}
Lei Zhangf997abe2022-01-12 19:14:23 +000051#endif // BUILDFLAG(IS_WIN)
Lei Zhang614f1a02019-05-30 00:03:53 +000052
thestigbcd3e532016-11-21 13:37:28 -080053} // namespace
54
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055EmbedderTest::EmbedderTest()
Tom Sepez7df04832020-05-18 22:09:31 +000056 : default_delegate_(std::make_unique<EmbedderTest::Delegate>()),
Lei Zhang0729be22018-02-05 21:13:51 +000057 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -040058 FPDF_FILEWRITE::version = 1;
59 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080060}
Tom Sepez96d13342015-01-16 14:59:26 -080061
Lei Zhangea208512019-12-18 00:42:11 +000062EmbedderTest::~EmbedderTest() = default;
Tom Sepezf288bb12015-11-20 12:12:46 -080063
64void EmbedderTest::SetUp() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
66 memset(info, 0, sizeof(UNSUPPORT_INFO));
67 info->version = 1;
68 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
69 FSDK_SetUnSpObjProcessHandler(info);
70}
Tom Sepez96d13342015-01-16 14:59:26 -080071
72void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +000073 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
Tom Sepez25f33d02021-01-29 01:58:51 +000074 // possible. This can fail when an DCHECK test fails in a test case.
Lei Zhang75c81712018-02-08 17:22:39 +000075 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +000076 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang38e2b0e2021-11-05 00:28:05 +000077 if (document())
Tom Sepez38f725f2019-12-04 00:26:34 +000078 CloseDocument();
Tom Sepez96d13342015-01-16 14:59:26 -080079}
80
Lei Zhangb8b4a042021-11-05 00:04:25 +000081void EmbedderTest::CreateEmptyDocument() {
Lei Zhang935fb862021-11-05 00:15:05 +000082 CreateEmptyDocumentWithoutFormFillEnvironment();
Lei Zhang38e2b0e2021-11-05 00:28:05 +000083 form_handle_.reset(SetupFormFillEnvironment(
84 document(), JavaScriptOption::kEnableJavaScript));
Tom Sepezd483eb42016-01-06 10:03:59 -080085}
86
Lei Zhang935fb862021-11-05 00:15:05 +000087void EmbedderTest::CreateEmptyDocumentWithoutFormFillEnvironment() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +000088 document_.reset(FPDF_CreateNewDocument());
Lei Zhang935fb862021-11-05 00:15:05 +000089 DCHECK(document_);
90}
91
Lei Zhang208eecf2017-12-20 19:40:50 +000092bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +000093 return OpenDocumentWithOptions(filename, nullptr,
94 LinearizeOption::kDefaultLinearize,
95 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +000096}
97
98bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +000099 return OpenDocumentWithOptions(filename, nullptr,
100 LinearizeOption::kMustLinearize,
101 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000102}
103
104bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
105 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000106 return OpenDocumentWithOptions(filename, password,
107 LinearizeOption::kDefaultLinearize,
108 JavaScriptOption::kEnableJavaScript);
109}
110
111bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
112 return OpenDocumentWithOptions(filename, nullptr,
113 LinearizeOption::kDefaultLinearize,
114 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000115}
116
117bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
118 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000119 LinearizeOption linearize_option,
120 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800121 std::string file_path;
122 if (!PathService::GetTestFilePath(filename, &file_path))
123 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000124
Wei Li091f7a02015-11-09 12:09:55 -0800125 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400126 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800127 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800128
thestig29ce9232016-06-22 07:03:23 -0700129 EXPECT_TRUE(!loader_);
Tom Sepez7df04832020-05-18 22:09:31 +0000130 loader_ = std::make_unique<TestLoader>(
Lei Zhangb3be4a12019-02-05 22:11:07 +0000131 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000132
133 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800134 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700135 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000136 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000137
Tom Sepez7df04832020-05-18 22:09:31 +0000138 fake_file_access_ = std::make_unique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000139 return OpenDocumentHelper(password, linearize_option, javascript_option,
140 fake_file_access_.get(), &document_, &avail_,
141 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400142}
Tom Sepez96d13342015-01-16 14:59:26 -0800143
Nicolas Pena56fc9722017-07-13 16:31:34 -0400144bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000145 LinearizeOption linearize_option,
146 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300147 FakeFileAccess* network_simulator,
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000148 ScopedFPDFDocument* document,
Lei Zhang52d21ac2021-11-04 22:40:15 +0000149 ScopedFPDFAvail* avail,
Lei Zhangdba065e2021-11-04 23:09:15 +0000150 ScopedFPDFFormHandle* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300151 network_simulator->AddSegment(0, 1024);
152 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000153 avail->reset(FPDFAvail_Create(network_simulator->GetFileAvail(),
154 network_simulator->GetFileAccess()));
155 FPDF_AVAIL avail_ptr = avail->get();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000156 FPDF_DOCUMENT document_ptr = nullptr;
Lei Zhang52d21ac2021-11-04 22:40:15 +0000157 if (FPDFAvail_IsLinearized(avail_ptr) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300158 int32_t nRet = PDF_DATA_NOTAVAIL;
159 while (nRet == PDF_DATA_NOTAVAIL) {
160 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000161 nRet = FPDFAvail_IsDocAvail(avail_ptr,
162 network_simulator->GetDownloadHints());
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300163 }
164 if (nRet == PDF_DATA_ERROR)
165 return false;
166
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000167 document->reset(FPDFAvail_GetDocument(avail_ptr, password));
168 document_ptr = document->get();
169 if (!document_ptr)
Jun Fangdf7f3662015-11-10 18:29:18 +0800170 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400171
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300172 nRet = PDF_DATA_NOTAVAIL;
173 while (nRet == PDF_DATA_NOTAVAIL) {
174 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000175 nRet = FPDFAvail_IsFormAvail(avail_ptr,
176 network_simulator->GetDownloadHints());
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300177 }
178 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800179 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400180
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000181 int page_count = FPDF_GetPageCount(document_ptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800182 for (int i = 0; i < page_count; ++i) {
183 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300184 while (nRet == PDF_DATA_NOTAVAIL) {
185 network_simulator->SetRequestedDataAvailable();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000186 nRet = FPDFAvail_IsPageAvail(avail_ptr, i,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300187 network_simulator->GetDownloadHints());
188 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400189 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800190 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800191 }
192 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000193 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800194 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300195 network_simulator->SetWholeFileAvailable();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000196 document->reset(
197 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password));
198 document_ptr = document->get();
199 if (!document_ptr)
Jun Fangdf7f3662015-11-10 18:29:18 +0800200 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800201 }
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000202 form_handle->reset(SetupFormFillEnvironment(document_ptr, javascript_option));
Tom Sepez0784c732018-04-23 18:02:57 +0000203
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000204 int doc_type = FPDF_GetFormType(document_ptr);
Ryan Harrison854d71c2017-10-18 12:28:14 -0400205 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000206 FPDF_LoadXFA(document_ptr);
Tom Sepez0784c732018-04-23 18:02:57 +0000207
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000208 (void)FPDF_GetDocPermissions(document_ptr);
Tom Sepezd483eb42016-01-06 10:03:59 -0800209 return true;
210}
Tom Sepez96d13342015-01-16 14:59:26 -0800211
Tom Sepez38f725f2019-12-04 00:26:34 +0000212void EmbedderTest::CloseDocument() {
Lei Zhangdba065e2021-11-04 23:09:15 +0000213 FORM_DoDocumentAAction(form_handle(), FPDFDOC_AACTION_WC);
214 form_handle_.reset();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000215 document_.reset();
Lei Zhang55b1f462021-11-05 01:44:46 +0000216 avail_.reset();
217 fake_file_access_.reset();
218 memset(&file_access_, 0, sizeof(file_access_));
219 loader_.reset();
220 file_contents_.reset();
Tom Sepez38f725f2019-12-04 00:26:34 +0000221}
222
Tom Sepez0784c732018-04-23 18:02:57 +0000223FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
224 FPDF_DOCUMENT doc,
225 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800226 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400227 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Tom Sepez72f520c2020-08-24 23:43:46 +0000228 platform->version = 3;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800229 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800230
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800231 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
232 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Aayush Dhirc63913e2020-02-12 09:45:34 +0000233 formfillinfo->version = form_fill_info_version_;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700234 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
235 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700236 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000237 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000238 formfillinfo->FFI_DoGoToAction = DoGoToActionTrampoline;
Aayush Dhirc63913e2020-02-12 09:45:34 +0000239 formfillinfo->FFI_OnFocusChange = OnFocusChangeTrampoline;
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000240 formfillinfo->FFI_DoURIActionWithKeyboardModifier =
241 DoURIActionWithKeyboardModifierTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000242
Tom Sepez0784c732018-04-23 18:02:57 +0000243 if (javascript_option == JavaScriptOption::kEnableJavaScript)
244 formfillinfo->m_pJsPlatform = platform;
245
Nicolas Pena3ff54002017-07-05 11:55:35 -0400246 FPDF_FORMHANDLE form_handle =
247 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Tom Sepez92f92222020-01-07 22:16:07 +0000248 SetInitialFormFieldHighlight(form_handle);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400249 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800250}
251
Tom Sepezda8189e2015-01-30 14:41:50 -0800252void EmbedderTest::DoOpenActions() {
Lei Zhangdba065e2021-11-04 23:09:15 +0000253 DCHECK(form_handle());
254 FORM_DoDocumentJSAction(form_handle());
255 FORM_DoDocumentOpenAction(form_handle());
Tom Sepez96d13342015-01-16 14:59:26 -0800256}
257
258int EmbedderTest::GetFirstPageNum() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000259 int first_page = FPDFAvail_GetFirstPageNum(document());
Lei Zhang52d21ac2021-11-04 22:40:15 +0000260 (void)FPDFAvail_IsPageAvail(avail(), first_page,
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000261 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800262 return first_page;
263}
264
265int EmbedderTest::GetPageCount() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000266 int page_count = FPDF_GetPageCount(document());
Nicolas Pena3ff54002017-07-05 11:55:35 -0400267 for (int i = 0; i < page_count; ++i)
Lei Zhang52d21ac2021-11-04 22:40:15 +0000268 (void)FPDFAvail_IsPageAvail(avail(), i,
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000269 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800270 return page_count;
271}
272
Tom Sepezda8189e2015-01-30 14:41:50 -0800273FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000274 return LoadPageCommon(page_number, true);
275}
276
277FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
278 return LoadPageCommon(page_number, false);
279}
280
281FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000282 DCHECK(form_handle());
Tom Sepez25f33d02021-01-29 01:58:51 +0000283 DCHECK(page_number >= 0);
284 DCHECK(!pdfium::Contains(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700285
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000286 FPDF_PAGE page = FPDF_LoadPage(document(), page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400287 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800288 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400289
Tom Sepez507d0192018-11-07 16:37:51 +0000290 if (do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000291 FORM_OnAfterLoadPage(page, form_handle());
292 FORM_DoPageAAction(page, form_handle(), FPDFPAGE_AACTION_OPEN);
Tom Sepez507d0192018-11-07 16:37:51 +0000293 }
weili0dadcc62016-08-23 21:10:57 -0700294 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700295 return page;
296}
297
Tom Sepezda8189e2015-01-30 14:41:50 -0800298void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000299 UnloadPageCommon(page, true);
300}
Lei Zhang75c81712018-02-08 17:22:39 +0000301
Tom Sepez507d0192018-11-07 16:37:51 +0000302void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
303 UnloadPageCommon(page, false);
304}
305
306void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000307 DCHECK(form_handle());
Lei Zhang75c81712018-02-08 17:22:39 +0000308 int page_number = GetPageNumberForLoadedPage(page);
309 if (page_number < 0) {
310 NOTREACHED();
311 return;
312 }
Tom Sepez507d0192018-11-07 16:37:51 +0000313 if (do_events) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000314 FORM_DoPageAAction(page, form_handle(), FPDFPAGE_AACTION_CLOSE);
315 FORM_OnBeforeClosePage(page, form_handle());
Tom Sepez507d0192018-11-07 16:37:51 +0000316 }
Tom Sepez96d13342015-01-16 14:59:26 -0800317 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000318 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800319}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800320
Tom Sepez92f92222020-01-07 22:16:07 +0000321void EmbedderTest::SetInitialFormFieldHighlight(FPDF_FORMHANDLE form) {
322 FPDF_SetFormFieldHighlightColor(form, FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD);
323 FPDF_SetFormFieldHighlightAlpha(form, 100);
324}
325
Tom Sepeze08d2b12018-04-25 18:49:32 +0000326ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000327 return RenderLoadedPageWithFlags(page, 0);
328}
329
Tom Sepeze08d2b12018-04-25 18:49:32 +0000330ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
331 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000332 if (GetPageNumberForLoadedPage(page) < 0) {
333 NOTREACHED();
334 return nullptr;
335 }
Lei Zhangdba065e2021-11-04 23:09:15 +0000336 return RenderPageWithFlags(page, form_handle(), flags);
Lei Zhanga98e3662018-02-07 20:28:35 +0000337}
338
Tom Sepeze08d2b12018-04-25 18:49:32 +0000339ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000340 return RenderSavedPageWithFlags(page, 0);
341}
342
Tom Sepeze08d2b12018-04-25 18:49:32 +0000343ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
344 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000345 if (GetPageNumberForSavedPage(page) < 0) {
346 NOTREACHED();
347 return nullptr;
348 }
Lei Zhangdba065e2021-11-04 23:09:15 +0000349 return RenderPageWithFlags(page, saved_form_handle(), flags);
Lei Zhanga98e3662018-02-07 20:28:35 +0000350}
351
352// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000353ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
354 FPDF_FORMHANDLE handle,
355 int flags) {
Lei Zhangdfa075b2019-12-05 21:52:43 +0000356 int width = static_cast<int>(FPDF_GetPageWidthF(page));
357 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhanga98e3662018-02-07 20:28:35 +0000358 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000359 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000360 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
361 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
362 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
363 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
364 return bitmap;
365}
366
Lei Zhang30ff2532019-01-31 21:37:55 +0000367// static
368ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
369 return RenderPageWithFlags(page, nullptr, 0);
370}
371
Lei Zhangf997abe2022-01-12 19:14:23 +0000372#if BUILDFLAG(IS_WIN)
Lei Zhangd69e0652019-04-13 00:39:35 +0000373// static
374std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
375 int flags) {
376 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
377
Lei Zhangdfa075b2019-12-05 21:52:43 +0000378 int width = static_cast<int>(FPDF_GetPageWidthF(page));
379 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhangd69e0652019-04-13 00:39:35 +0000380 HRGN rgn = CreateRectRgn(0, 0, width, height);
381 SelectClipRgn(dc, rgn);
382 DeleteObject(rgn);
383
384 SelectObject(dc, GetStockObject(NULL_PEN));
385 SelectObject(dc, GetStockObject(WHITE_BRUSH));
386 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
387 Rectangle(dc, 0, 0, width + 1, height + 1);
388
389 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
390
391 HENHMETAFILE emf = CloseEnhMetaFile(dc);
Tom Sepezea03a7b2022-02-24 00:30:14 +0000392 UINT size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
Lei Zhangd69e0652019-04-13 00:39:35 +0000393 std::vector<uint8_t> buffer(size_in_bytes);
394 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
395 DeleteEnhMetaFile(emf);
396 return buffer;
397}
Lei Zhang614f1a02019-05-30 00:03:53 +0000398
399// static
400std::string EmbedderTest::GetPostScriptFromEmf(
Tom Sepez87b9e3a2019-11-20 23:45:43 +0000401 pdfium::span<const uint8_t> emf_data) {
Lei Zhang614f1a02019-05-30 00:03:53 +0000402 // This comes from Emf::InitFromData() in Chromium.
Tom Sepezea03a7b2022-02-24 00:30:14 +0000403 HENHMETAFILE emf = SetEnhMetaFileBits(
404 pdfium::base::checked_cast<UINT>(emf_data.size()), emf_data.data());
Lei Zhang614f1a02019-05-30 00:03:53 +0000405 if (!emf)
406 return std::string();
407
408 // This comes from Emf::Enumerator::Enumerator() in Chromium.
409 std::vector<const ENHMETARECORD*> records;
410 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
411 DeleteEnhMetaFile(emf);
412 return std::string();
413 }
414
415 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
416 std::string ps_data;
417 for (const auto* record : records) {
418 if (record->iType != EMR_GDICOMMENT)
419 continue;
420
421 // PostScript data is encapsulated inside EMF comment records.
422 // The first two bytes of the comment indicate the string length. The rest
423 // is the actual string data.
424 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
425 const char* data = reinterpret_cast<const char*>(comment->Data);
426 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
427 data += 2;
428 ps_data.append(data, size);
429 }
430 DeleteEnhMetaFile(emf);
431 return ps_data;
432}
Lei Zhangf997abe2022-01-12 19:14:23 +0000433#endif // BUILDFLAG(IS_WIN)
Lei Zhangd69e0652019-04-13 00:39:35 +0000434
Lei Zhang0b494052019-01-31 21:41:15 +0000435FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
436 return OpenSavedDocumentWithPassword(nullptr);
437}
438
Tom Sepez6dc12402020-01-07 21:02:17 +0000439// static
440int EmbedderTest::BytesPerPixelForFormat(int format) {
441 switch (format) {
442 case FPDFBitmap_Gray:
443 return 1;
444 case FPDFBitmap_BGR:
445 return 3;
446 case FPDFBitmap_BGRx:
447 case FPDFBitmap_BGRA:
448 return 4;
449 default:
450 NOTREACHED();
451 return 0;
452 }
453}
454
Lei Zhang0b494052019-01-31 21:41:15 +0000455FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
456 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300457 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Tom Sepezea03a7b2022-02-24 00:30:14 +0000458 saved_file_access_.m_FileLen =
459 pdfium::base::checked_cast<unsigned long>(data_string_.size());
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300460 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000461 // Copy data to prevent clearing it before saved document close.
462 saved_document_file_data_ = data_string_;
463 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400464
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300465 saved_fake_file_access_ =
Tom Sepez7df04832020-05-18 22:09:31 +0000466 std::make_unique<FakeFileAccess>(&saved_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300467
Tom Sepez0784c732018-04-23 18:02:57 +0000468 EXPECT_TRUE(OpenDocumentHelper(
469 password, LinearizeOption::kDefaultLinearize,
470 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
471 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000472 return saved_document();
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400473}
474
475void EmbedderTest::CloseSavedDocument() {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000476 DCHECK(saved_document());
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400477
Lei Zhangdba065e2021-11-04 23:09:15 +0000478 saved_form_handle_.reset();
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000479 saved_document_.reset();
Lei Zhang52d21ac2021-11-04 22:40:15 +0000480 saved_avail_.reset();
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400481}
482
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000483FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000484 DCHECK(saved_form_handle());
Tom Sepez25f33d02021-01-29 01:58:51 +0000485 DCHECK(page_number >= 0);
486 DCHECK(!pdfium::Contains(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400487
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000488 FPDF_PAGE page = FPDF_LoadPage(saved_document(), page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000489 if (!page)
490 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400491
Lei Zhangdba065e2021-11-04 23:09:15 +0000492 FORM_OnAfterLoadPage(page, saved_form_handle());
493 FORM_DoPageAAction(page, saved_form_handle(), FPDFPAGE_AACTION_OPEN);
Lei Zhang9f72c452018-02-08 21:49:54 +0000494 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000495 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400496}
497
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000498void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhangdba065e2021-11-04 23:09:15 +0000499 DCHECK(saved_form_handle());
Lei Zhang9f72c452018-02-08 21:49:54 +0000500
501 int page_number = GetPageNumberForSavedPage(page);
502 if (page_number < 0) {
503 NOTREACHED();
504 return;
505 }
506
Lei Zhangdba065e2021-11-04 23:09:15 +0000507 FORM_DoPageAAction(page, saved_form_handle(), FPDFPAGE_AACTION_CLOSE);
508 FORM_OnBeforeClosePage(page, saved_form_handle());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000509 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000510
511 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400512}
513
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000514void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
515 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400516 int height,
517 const char* md5) {
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000518 DCHECK(saved_document());
Tom Sepez25f33d02021-01-29 01:58:51 +0000519 DCHECK(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400520
Tom Sepeze08d2b12018-04-25 18:49:32 +0000521 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000522 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400523}
524
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400525void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000526 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000527 FPDF_PAGE page = LoadSavedPage(0);
528 VerifySavedRendering(page, width, height, md5);
529 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400530 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400531}
532
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300533void EmbedderTest::SetWholeFileAvailable() {
Tom Sepez25f33d02021-01-29 01:58:51 +0000534 DCHECK(fake_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300535 fake_file_access_->SetWholeFileAvailable();
536}
537
Lei Zhang38e2b0e2021-11-05 00:28:05 +0000538void EmbedderTest::SetDocumentFromAvail() {
539 document_.reset(FPDFAvail_GetDocument(avail(), nullptr));
540}
541
Lei Zhang52d21ac2021-11-04 22:40:15 +0000542void EmbedderTest::CreateAvail(FX_FILEAVAIL* file_avail,
543 FPDF_FILEACCESS* file) {
544 avail_.reset(FPDFAvail_Create(file_avail, file));
545}
546
weili0dadcc62016-08-23 21:10:57 -0700547FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700548 FPDF_DOCUMENT document,
549 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700550 EmbedderTest* test = static_cast<EmbedderTest*>(info);
551 auto it = test->page_map_.find(page_index);
552 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700553}
554
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800555// static
556void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
557 int type) {
558 EmbedderTest* test = static_cast<EmbedderTest*>(info);
559 test->delegate_->UnsupportedHandler(type);
560}
561
562// static
563int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
564 FPDF_WIDESTRING message,
565 FPDF_WIDESTRING title,
566 int type,
567 int icon) {
568 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
569 return test->delegate_->Alert(message, title, type, icon);
570}
571
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700572// static
573int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700574 int msecs,
575 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700576 EmbedderTest* test = static_cast<EmbedderTest*>(info);
577 return test->delegate_->SetTimer(msecs, fn);
578}
579
580// static
581void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
582 EmbedderTest* test = static_cast<EmbedderTest*>(info);
583 return test->delegate_->KillTimer(id);
584}
585
Tom Sepez396e8722015-09-09 10:16:08 -0700586// static
587FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
588 FPDF_DOCUMENT document,
589 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700590 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
591 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700592}
593
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000594// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000595void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
596 FPDF_BYTESTRING uri) {
597 EmbedderTest* test = static_cast<EmbedderTest*>(info);
598 return test->delegate_->DoURIAction(uri);
599}
600
601// static
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000602void EmbedderTest::DoGoToActionTrampoline(FPDF_FORMFILLINFO* info,
603 int page_index,
604 int zoom_mode,
605 float* pos_array,
606 int array_size) {
607 EmbedderTest* test = static_cast<EmbedderTest*>(info);
608 return test->delegate_->DoGoToAction(info, page_index, zoom_mode, pos_array,
609 array_size);
610}
611
612// static
Aayush Dhirc63913e2020-02-12 09:45:34 +0000613void EmbedderTest::OnFocusChangeTrampoline(FPDF_FORMFILLINFO* info,
614 FPDF_ANNOTATION annot,
615 int page_index) {
616 EmbedderTest* test = static_cast<EmbedderTest*>(info);
617 return test->delegate_->OnFocusChange(info, annot, page_index);
618}
619
620// static
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000621void EmbedderTest::DoURIActionWithKeyboardModifierTrampoline(
622 FPDF_FORMFILLINFO* info,
623 FPDF_BYTESTRING uri,
624 int modifiers) {
625 EmbedderTest* test = static_cast<EmbedderTest*>(info);
626 return test->delegate_->DoURIActionWithKeyboardModifier(info, uri, modifiers);
627}
628
629// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000630std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Lei Zhangbf980142019-12-20 01:05:42 +0000631 int stride = FPDFBitmap_GetStride(bitmap);
632 int usable_bytes_per_row =
633 GetBitmapBytesPerPixel(bitmap) * FPDFBitmap_GetWidth(bitmap);
634 int height = FPDFBitmap_GetHeight(bitmap);
635 auto span = pdfium::make_span(
636 static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)), stride * height);
637
638 CRYPT_md5_context context = CRYPT_MD5Start();
639 for (int i = 0; i < height; ++i)
640 CRYPT_MD5Update(&context, span.subspan(i * stride, usable_bytes_per_row));
Dan Sinclair957480c2017-06-13 15:21:14 -0400641 uint8_t digest[16];
Lei Zhangbf980142019-12-20 01:05:42 +0000642 CRYPT_MD5Finish(&context, digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400643 return CryptToBase16(digest);
644}
645
Henrique Nakashimadb269572018-01-16 19:02:15 +0000646#ifndef NDEBUG
647// static
648void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
649 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000650 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000651}
652#endif
653
thestigbcd3e532016-11-21 13:37:28 -0800654// static
655void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
656 int expected_width,
657 int expected_height,
658 const char* expected_md5sum) {
659 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
660 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400661
662 // The expected stride is calculated using the same formula as in
663 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
664 const int expected_stride =
665 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800666 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
667
668 if (!expected_md5sum)
669 return;
670
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000671 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800672}
673
Nicolas Pena3ff54002017-07-05 11:55:35 -0400674// static
675int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
676 const void* data,
677 unsigned long size) {
678 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000679
Lei Zhang0729be22018-02-05 21:13:51 +0000680 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000681
682 if (pThis->filestream_.is_open())
683 pThis->filestream_.write(static_cast<const char*>(data), size);
684
Nicolas Pena3ff54002017-07-05 11:55:35 -0400685 return 1;
686}
687
688// static
689int EmbedderTest::GetBlockFromString(void* param,
690 unsigned long pos,
691 unsigned char* buf,
692 unsigned long size) {
693 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000694 if (!new_file || pos + size < pos) {
695 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400696 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000697 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400698
Lei Zhangcef91f12019-01-08 21:32:51 +0000699 if (pos + size > new_file->size()) {
700 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400701 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000702 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400703
704 memcpy(buf, new_file->data() + pos, size);
705 return 1;
706}
Lei Zhang75c81712018-02-08 17:22:39 +0000707
Lei Zhang9f72c452018-02-08 21:49:54 +0000708// static
709int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
710 FPDF_PAGE page) {
711 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000712 if (it.second == page) {
713 int page_number = it.first;
Tom Sepez25f33d02021-01-29 01:58:51 +0000714 DCHECK(page_number >= 0);
Lei Zhang75c81712018-02-08 17:22:39 +0000715 return page_number;
716 }
717 }
718 return -1;
719}
Lei Zhang9f72c452018-02-08 21:49:54 +0000720
721int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
722 return GetPageNumberForPage(page_map_, page);
723}
724
725int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
726 return GetPageNumberForPage(saved_page_map_, page);
727}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000728
Lei Zhang81a799e2019-12-16 17:37:13 +0000729#ifndef NDEBUG
730void EmbedderTest::OpenPDFFileForWrite(const std::string& filename) {
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000731 filestream_.open(filename, std::ios_base::binary);
732}
733
734void EmbedderTest::ClosePDFFileForWrite() {
735 filestream_.close();
736}
Lei Zhang81a799e2019-12-16 17:37:13 +0000737#endif