blob: c3e71929f17a24e813bfb819080e4b3c0d8d43bc [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;
117 config.version = 2;
118 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 Sepeza72e8e22015-10-07 10:17:53 -0700121 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -0800122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
124 memset(info, 0, sizeof(UNSUPPORT_INFO));
125 info->version = 1;
126 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
127 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000128
Lei Zhang0729be22018-02-05 21:13:51 +0000129 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130}
Tom Sepez96d13342015-01-16 14:59:26 -0800131
132void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +0000133 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
134 // possible. This can fail when an ASSERT test fails in a test case.
135 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +0000136 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang75c81712018-02-08 17:22:39 +0000137
Lei Zhang44005192020-04-17 01:35:43 +0000138 if (document_)
Tom Sepez38f725f2019-12-04 00:26:34 +0000139 CloseDocument();
Tom Sepezc46d0002015-11-30 15:46:36 -0800140
Tom Sepez96d13342015-01-16 14:59:26 -0800141 FPDFAvail_Destroy(avail_);
142 FPDF_DestroyLibrary();
Lei Zhangb3be4a12019-02-05 22:11:07 +0000143 loader_.reset();
Tom Sepez96d13342015-01-16 14:59:26 -0800144}
145
Lei Zhang378ec542018-10-18 19:15:47 +0000146#ifdef PDF_ENABLE_V8
147void EmbedderTest::SetExternalIsolate(void* isolate) {
148 external_isolate_ = static_cast<v8::Isolate*>(isolate);
149}
150#endif // PDF_ENABLE_V8
151
Tom Sepezd483eb42016-01-06 10:03:59 -0800152bool EmbedderTest::CreateEmptyDocument() {
153 document_ = FPDF_CreateNewDocument();
154 if (!document_)
155 return false;
156
Tom Sepez0784c732018-04-23 18:02:57 +0000157 form_handle_ =
158 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -0800159 return true;
160}
161
Lei Zhang208eecf2017-12-20 19:40:50 +0000162bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000163 return OpenDocumentWithOptions(filename, nullptr,
164 LinearizeOption::kDefaultLinearize,
165 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000166}
167
168bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000169 return OpenDocumentWithOptions(filename, nullptr,
170 LinearizeOption::kMustLinearize,
171 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000172}
173
174bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
175 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000176 return OpenDocumentWithOptions(filename, password,
177 LinearizeOption::kDefaultLinearize,
178 JavaScriptOption::kEnableJavaScript);
179}
180
181bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
182 return OpenDocumentWithOptions(filename, nullptr,
183 LinearizeOption::kDefaultLinearize,
184 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000185}
186
187bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
188 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000189 LinearizeOption linearize_option,
190 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800191 std::string file_path;
192 if (!PathService::GetTestFilePath(filename, &file_path))
193 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000194
Wei Li091f7a02015-11-09 12:09:55 -0800195 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400196 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800197 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800198
thestig29ce9232016-06-22 07:03:23 -0700199 EXPECT_TRUE(!loader_);
Tom Sepez7df04832020-05-18 22:09:31 +0000200 loader_ = std::make_unique<TestLoader>(
Lei Zhangb3be4a12019-02-05 22:11:07 +0000201 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000202
203 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800204 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700205 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000206 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000207
Tom Sepez7df04832020-05-18 22:09:31 +0000208 fake_file_access_ = std::make_unique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000209 return OpenDocumentHelper(password, linearize_option, javascript_option,
210 fake_file_access_.get(), &document_, &avail_,
211 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400212}
Tom Sepez96d13342015-01-16 14:59:26 -0800213
Nicolas Pena56fc9722017-07-13 16:31:34 -0400214bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000215 LinearizeOption linearize_option,
216 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300217 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400218 FPDF_DOCUMENT* document,
219 FPDF_AVAIL* avail,
220 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300221 network_simulator->AddSegment(0, 1024);
222 network_simulator->SetRequestedDataAvailable();
223 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
224 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400225 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300226 int32_t nRet = PDF_DATA_NOTAVAIL;
227 while (nRet == PDF_DATA_NOTAVAIL) {
228 network_simulator->SetRequestedDataAvailable();
229 nRet =
230 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
231 }
232 if (nRet == PDF_DATA_ERROR)
233 return false;
234
Nicolas Pena56fc9722017-07-13 16:31:34 -0400235 *document = FPDFAvail_GetDocument(*avail, password);
236 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800237 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400238
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300239 nRet = PDF_DATA_NOTAVAIL;
240 while (nRet == PDF_DATA_NOTAVAIL) {
241 network_simulator->SetRequestedDataAvailable();
242 nRet =
243 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
244 }
245 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800246 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400247
248 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800249 for (int i = 0; i < page_count; ++i) {
250 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300251 while (nRet == PDF_DATA_NOTAVAIL) {
252 network_simulator->SetRequestedDataAvailable();
253 nRet = FPDFAvail_IsPageAvail(*avail, i,
254 network_simulator->GetDownloadHints());
255 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400256 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800257 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800258 }
259 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000260 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800261 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300262 network_simulator->SetWholeFileAvailable();
263 *document =
264 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400265 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800266 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800267 }
Tom Sepez0784c732018-04-23 18:02:57 +0000268 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
269
Ryan Harrison854d71c2017-10-18 12:28:14 -0400270 int doc_type = FPDF_GetFormType(*document);
271 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
272 FPDF_LoadXFA(*document);
Tom Sepez0784c732018-04-23 18:02:57 +0000273
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000274 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800275 return true;
276}
Tom Sepez96d13342015-01-16 14:59:26 -0800277
Tom Sepez38f725f2019-12-04 00:26:34 +0000278void EmbedderTest::CloseDocument() {
Lei Zhang44005192020-04-17 01:35:43 +0000279 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepez38f725f2019-12-04 00:26:34 +0000280 FPDFDOC_ExitFormFillEnvironment(form_handle_);
281 form_handle_ = nullptr;
282
283 FPDF_CloseDocument(document_);
284 document_ = nullptr;
285}
286
Tom Sepez0784c732018-04-23 18:02:57 +0000287FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
288 FPDF_DOCUMENT doc,
289 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800290 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400291 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200292 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800293 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400294 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800295
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800296 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
297 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Aayush Dhirc63913e2020-02-12 09:45:34 +0000298 formfillinfo->version = form_fill_info_version_;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700299 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
300 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700301 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000302 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000303 formfillinfo->FFI_DoGoToAction = DoGoToActionTrampoline;
Aayush Dhirc63913e2020-02-12 09:45:34 +0000304 formfillinfo->FFI_OnFocusChange = OnFocusChangeTrampoline;
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000305 formfillinfo->FFI_DoURIActionWithKeyboardModifier =
306 DoURIActionWithKeyboardModifierTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000307
Tom Sepez0784c732018-04-23 18:02:57 +0000308 if (javascript_option == JavaScriptOption::kEnableJavaScript)
309 formfillinfo->m_pJsPlatform = platform;
310
Nicolas Pena3ff54002017-07-05 11:55:35 -0400311 FPDF_FORMHANDLE form_handle =
312 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Tom Sepez92f92222020-01-07 22:16:07 +0000313 SetInitialFormFieldHighlight(form_handle);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400314 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800315}
316
Tom Sepezda8189e2015-01-30 14:41:50 -0800317void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400318 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800319 FORM_DoDocumentJSAction(form_handle_);
320 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800321}
322
323int EmbedderTest::GetFirstPageNum() {
324 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000325 (void)FPDFAvail_IsPageAvail(avail_, first_page,
326 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800327 return first_page;
328}
329
330int EmbedderTest::GetPageCount() {
331 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400332 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000333 (void)FPDFAvail_IsPageAvail(avail_, i,
334 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800335 return page_count;
336}
337
Tom Sepezda8189e2015-01-30 14:41:50 -0800338FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000339 return LoadPageCommon(page_number, true);
340}
341
342FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
343 return LoadPageCommon(page_number, false);
344}
345
346FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400347 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000348 ASSERT(page_number >= 0);
Lei Zhangf245ae62020-05-15 21:49:46 +0000349 ASSERT(!pdfium::Contains(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700350
Tom Sepez96d13342015-01-16 14:59:26 -0800351 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400352 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800353 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400354
Tom Sepez507d0192018-11-07 16:37:51 +0000355 if (do_events) {
356 FORM_OnAfterLoadPage(page, form_handle_);
357 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
358 }
weili0dadcc62016-08-23 21:10:57 -0700359 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700360 return page;
361}
362
Tom Sepezda8189e2015-01-30 14:41:50 -0800363void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000364 UnloadPageCommon(page, true);
365}
Lei Zhang75c81712018-02-08 17:22:39 +0000366
Tom Sepez507d0192018-11-07 16:37:51 +0000367void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
368 UnloadPageCommon(page, false);
369}
370
371void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
372 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000373 int page_number = GetPageNumberForLoadedPage(page);
374 if (page_number < 0) {
375 NOTREACHED();
376 return;
377 }
Tom Sepez507d0192018-11-07 16:37:51 +0000378 if (do_events) {
379 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
380 FORM_OnBeforeClosePage(page, form_handle_);
381 }
Tom Sepez96d13342015-01-16 14:59:26 -0800382 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000383 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800384}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800385
Tom Sepez92f92222020-01-07 22:16:07 +0000386void EmbedderTest::SetInitialFormFieldHighlight(FPDF_FORMHANDLE form) {
387 FPDF_SetFormFieldHighlightColor(form, FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD);
388 FPDF_SetFormFieldHighlightAlpha(form, 100);
389}
390
Tom Sepeze08d2b12018-04-25 18:49:32 +0000391ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000392 return RenderLoadedPageWithFlags(page, 0);
393}
394
Tom Sepeze08d2b12018-04-25 18:49:32 +0000395ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
396 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000397 if (GetPageNumberForLoadedPage(page) < 0) {
398 NOTREACHED();
399 return nullptr;
400 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000401 return RenderPageWithFlags(page, form_handle_, flags);
402}
403
Tom Sepeze08d2b12018-04-25 18:49:32 +0000404ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000405 return RenderSavedPageWithFlags(page, 0);
406}
407
Tom Sepeze08d2b12018-04-25 18:49:32 +0000408ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
409 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000410 if (GetPageNumberForSavedPage(page) < 0) {
411 NOTREACHED();
412 return nullptr;
413 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000414 return RenderPageWithFlags(page, saved_form_handle_, flags);
415}
416
417// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000418ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
419 FPDF_FORMHANDLE handle,
420 int flags) {
Lei Zhangdfa075b2019-12-05 21:52:43 +0000421 int width = static_cast<int>(FPDF_GetPageWidthF(page));
422 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhanga98e3662018-02-07 20:28:35 +0000423 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000424 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000425 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
426 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
427 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
428 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
429 return bitmap;
430}
431
Lei Zhang30ff2532019-01-31 21:37:55 +0000432// static
433ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
434 return RenderPageWithFlags(page, nullptr, 0);
435}
436
Lei Zhangd69e0652019-04-13 00:39:35 +0000437#if defined(OS_WIN)
438// static
439std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
440 int flags) {
441 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
442
Lei Zhangdfa075b2019-12-05 21:52:43 +0000443 int width = static_cast<int>(FPDF_GetPageWidthF(page));
444 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhangd69e0652019-04-13 00:39:35 +0000445 HRGN rgn = CreateRectRgn(0, 0, width, height);
446 SelectClipRgn(dc, rgn);
447 DeleteObject(rgn);
448
449 SelectObject(dc, GetStockObject(NULL_PEN));
450 SelectObject(dc, GetStockObject(WHITE_BRUSH));
451 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
452 Rectangle(dc, 0, 0, width + 1, height + 1);
453
454 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
455
456 HENHMETAFILE emf = CloseEnhMetaFile(dc);
457 size_t size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
458 std::vector<uint8_t> buffer(size_in_bytes);
459 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
460 DeleteEnhMetaFile(emf);
461 return buffer;
462}
Lei Zhang614f1a02019-05-30 00:03:53 +0000463
464// static
465std::string EmbedderTest::GetPostScriptFromEmf(
Tom Sepez87b9e3a2019-11-20 23:45:43 +0000466 pdfium::span<const uint8_t> emf_data) {
Lei Zhang614f1a02019-05-30 00:03:53 +0000467 // This comes from Emf::InitFromData() in Chromium.
468 HENHMETAFILE emf = SetEnhMetaFileBits(emf_data.size(), emf_data.data());
469 if (!emf)
470 return std::string();
471
472 // This comes from Emf::Enumerator::Enumerator() in Chromium.
473 std::vector<const ENHMETARECORD*> records;
474 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
475 DeleteEnhMetaFile(emf);
476 return std::string();
477 }
478
479 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
480 std::string ps_data;
481 for (const auto* record : records) {
482 if (record->iType != EMR_GDICOMMENT)
483 continue;
484
485 // PostScript data is encapsulated inside EMF comment records.
486 // The first two bytes of the comment indicate the string length. The rest
487 // is the actual string data.
488 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
489 const char* data = reinterpret_cast<const char*>(comment->Data);
490 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
491 data += 2;
492 ps_data.append(data, size);
493 }
494 DeleteEnhMetaFile(emf);
495 return ps_data;
496}
Lei Zhangd69e0652019-04-13 00:39:35 +0000497#endif // defined(OS_WIN)
498
Lei Zhang0b494052019-01-31 21:41:15 +0000499FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
500 return OpenSavedDocumentWithPassword(nullptr);
501}
502
Tom Sepez6dc12402020-01-07 21:02:17 +0000503// static
504int EmbedderTest::BytesPerPixelForFormat(int format) {
505 switch (format) {
506 case FPDFBitmap_Gray:
507 return 1;
508 case FPDFBitmap_BGR:
509 return 3;
510 case FPDFBitmap_BGRx:
511 case FPDFBitmap_BGRA:
512 return 4;
513 default:
514 NOTREACHED();
515 return 0;
516 }
517}
518
Lei Zhang0b494052019-01-31 21:41:15 +0000519FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
520 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300521 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000522 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300523 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000524 // Copy data to prevent clearing it before saved document close.
525 saved_document_file_data_ = data_string_;
526 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400527
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300528 saved_fake_file_access_ =
Tom Sepez7df04832020-05-18 22:09:31 +0000529 std::make_unique<FakeFileAccess>(&saved_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300530
Tom Sepez0784c732018-04-23 18:02:57 +0000531 EXPECT_TRUE(OpenDocumentHelper(
532 password, LinearizeOption::kDefaultLinearize,
533 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
534 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000535 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400536}
537
538void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000539 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400540
Lei Zhang0729be22018-02-05 21:13:51 +0000541 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
542 FPDF_CloseDocument(saved_document_);
543 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400544
Lei Zhang0729be22018-02-05 21:13:51 +0000545 saved_form_handle_ = nullptr;
546 saved_document_ = nullptr;
547 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400548}
549
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000550FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000551 ASSERT(saved_form_handle_);
552 ASSERT(page_number >= 0);
Lei Zhangf245ae62020-05-15 21:49:46 +0000553 ASSERT(!pdfium::Contains(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400554
Lei Zhang0729be22018-02-05 21:13:51 +0000555 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000556 if (!page)
557 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400558
Lei Zhang9f72c452018-02-08 21:49:54 +0000559 FORM_OnAfterLoadPage(page, saved_form_handle_);
560 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
561 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000562 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400563}
564
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000565void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000566 ASSERT(saved_form_handle_);
567
568 int page_number = GetPageNumberForSavedPage(page);
569 if (page_number < 0) {
570 NOTREACHED();
571 return;
572 }
573
574 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
575 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000576 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000577
578 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400579}
580
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000581void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
582 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400583 int height,
584 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000585 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000586 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400587
Tom Sepeze08d2b12018-04-25 18:49:32 +0000588 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000589 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400590}
591
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400592void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000593 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000594 FPDF_PAGE page = LoadSavedPage(0);
595 VerifySavedRendering(page, width, height, md5);
596 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400597 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400598}
599
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300600void EmbedderTest::SetWholeFileAvailable() {
601 ASSERT(fake_file_access_);
602 fake_file_access_->SetWholeFileAvailable();
603}
604
weili0dadcc62016-08-23 21:10:57 -0700605FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700606 FPDF_DOCUMENT document,
607 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700608 EmbedderTest* test = static_cast<EmbedderTest*>(info);
609 auto it = test->page_map_.find(page_index);
610 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700611}
612
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800613// static
614void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
615 int type) {
616 EmbedderTest* test = static_cast<EmbedderTest*>(info);
617 test->delegate_->UnsupportedHandler(type);
618}
619
620// static
621int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
622 FPDF_WIDESTRING message,
623 FPDF_WIDESTRING title,
624 int type,
625 int icon) {
626 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
627 return test->delegate_->Alert(message, title, type, icon);
628}
629
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700630// static
631int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632 int msecs,
633 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700634 EmbedderTest* test = static_cast<EmbedderTest*>(info);
635 return test->delegate_->SetTimer(msecs, fn);
636}
637
638// static
639void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
640 EmbedderTest* test = static_cast<EmbedderTest*>(info);
641 return test->delegate_->KillTimer(id);
642}
643
Tom Sepez396e8722015-09-09 10:16:08 -0700644// static
645FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
646 FPDF_DOCUMENT document,
647 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700648 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
649 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700650}
651
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000652// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000653void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
654 FPDF_BYTESTRING uri) {
655 EmbedderTest* test = static_cast<EmbedderTest*>(info);
656 return test->delegate_->DoURIAction(uri);
657}
658
659// static
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000660void EmbedderTest::DoGoToActionTrampoline(FPDF_FORMFILLINFO* info,
661 int page_index,
662 int zoom_mode,
663 float* pos_array,
664 int array_size) {
665 EmbedderTest* test = static_cast<EmbedderTest*>(info);
666 return test->delegate_->DoGoToAction(info, page_index, zoom_mode, pos_array,
667 array_size);
668}
669
670// static
Aayush Dhirc63913e2020-02-12 09:45:34 +0000671void EmbedderTest::OnFocusChangeTrampoline(FPDF_FORMFILLINFO* info,
672 FPDF_ANNOTATION annot,
673 int page_index) {
674 EmbedderTest* test = static_cast<EmbedderTest*>(info);
675 return test->delegate_->OnFocusChange(info, annot, page_index);
676}
677
678// static
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000679void EmbedderTest::DoURIActionWithKeyboardModifierTrampoline(
680 FPDF_FORMFILLINFO* info,
681 FPDF_BYTESTRING uri,
682 int modifiers) {
683 EmbedderTest* test = static_cast<EmbedderTest*>(info);
684 return test->delegate_->DoURIActionWithKeyboardModifier(info, uri, modifiers);
685}
686
687// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000688std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Lei Zhangbf980142019-12-20 01:05:42 +0000689 int stride = FPDFBitmap_GetStride(bitmap);
690 int usable_bytes_per_row =
691 GetBitmapBytesPerPixel(bitmap) * FPDFBitmap_GetWidth(bitmap);
692 int height = FPDFBitmap_GetHeight(bitmap);
693 auto span = pdfium::make_span(
694 static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)), stride * height);
695
696 CRYPT_md5_context context = CRYPT_MD5Start();
697 for (int i = 0; i < height; ++i)
698 CRYPT_MD5Update(&context, span.subspan(i * stride, usable_bytes_per_row));
Dan Sinclair957480c2017-06-13 15:21:14 -0400699 uint8_t digest[16];
Lei Zhangbf980142019-12-20 01:05:42 +0000700 CRYPT_MD5Finish(&context, digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400701 return CryptToBase16(digest);
702}
703
Henrique Nakashimadb269572018-01-16 19:02:15 +0000704#ifndef NDEBUG
705// static
706void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
707 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000708 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000709}
710#endif
711
thestigbcd3e532016-11-21 13:37:28 -0800712// static
713void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
714 int expected_width,
715 int expected_height,
716 const char* expected_md5sum) {
717 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
718 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400719
720 // The expected stride is calculated using the same formula as in
721 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
722 const int expected_stride =
723 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800724 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
725
726 if (!expected_md5sum)
727 return;
728
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000729 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800730}
731
Nicolas Pena3ff54002017-07-05 11:55:35 -0400732// static
733int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
734 const void* data,
735 unsigned long size) {
736 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000737
Lei Zhang0729be22018-02-05 21:13:51 +0000738 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000739
740 if (pThis->filestream_.is_open())
741 pThis->filestream_.write(static_cast<const char*>(data), size);
742
Nicolas Pena3ff54002017-07-05 11:55:35 -0400743 return 1;
744}
745
746// static
747int EmbedderTest::GetBlockFromString(void* param,
748 unsigned long pos,
749 unsigned char* buf,
750 unsigned long size) {
751 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000752 if (!new_file || pos + size < pos) {
753 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400754 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000755 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400756
Lei Zhangcef91f12019-01-08 21:32:51 +0000757 if (pos + size > new_file->size()) {
758 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400759 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000760 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400761
762 memcpy(buf, new_file->data() + pos, size);
763 return 1;
764}
Lei Zhang75c81712018-02-08 17:22:39 +0000765
Lei Zhang9f72c452018-02-08 21:49:54 +0000766// static
767int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
768 FPDF_PAGE page) {
769 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000770 if (it.second == page) {
771 int page_number = it.first;
772 ASSERT(page_number >= 0);
773 return page_number;
774 }
775 }
776 return -1;
777}
Lei Zhang9f72c452018-02-08 21:49:54 +0000778
779int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
780 return GetPageNumberForPage(page_map_, page);
781}
782
783int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
784 return GetPageNumberForPage(saved_page_map_, page);
785}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000786
Lei Zhang81a799e2019-12-16 17:37:13 +0000787#ifndef NDEBUG
788void EmbedderTest::OpenPDFFileForWrite(const std::string& filename) {
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000789 filestream_.open(filename, std::ios_base::binary);
790}
791
792void EmbedderTest::ClosePDFFileForWrite() {
793 filestream_.close();
794}
Lei Zhang81a799e2019-12-16 17:37:13 +0000795#endif