blob: f910d6dd76f51b75e597b9194e66aa823b5560f7 [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 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 Zhang614f1a02019-05-30 00:03:53 +000041#if defined(OS_WIN)
42int 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}
51#endif // defined(OS_WIN)
52
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);
Lei Zhang0729be22018-02-05 21:13:51 +000070 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071}
Tom Sepez96d13342015-01-16 14:59:26 -080072
73void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +000074 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
75 // possible. This can fail when an ASSERT test fails in a test case.
76 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +000077 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang44005192020-04-17 01:35:43 +000078 if (document_)
Tom Sepez38f725f2019-12-04 00:26:34 +000079 CloseDocument();
Tom Sepezc46d0002015-11-30 15:46:36 -080080
Tom Sepez96d13342015-01-16 14:59:26 -080081 FPDFAvail_Destroy(avail_);
Lei Zhangb3be4a12019-02-05 22:11:07 +000082 loader_.reset();
Tom Sepez96d13342015-01-16 14:59:26 -080083}
84
Tom Sepezd483eb42016-01-06 10:03:59 -080085bool EmbedderTest::CreateEmptyDocument() {
86 document_ = FPDF_CreateNewDocument();
87 if (!document_)
88 return false;
89
Tom Sepez0784c732018-04-23 18:02:57 +000090 form_handle_ =
91 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -080092 return true;
93}
94
Lei Zhang208eecf2017-12-20 19:40:50 +000095bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +000096 return OpenDocumentWithOptions(filename, nullptr,
97 LinearizeOption::kDefaultLinearize,
98 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +000099}
100
101bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000102 return OpenDocumentWithOptions(filename, nullptr,
103 LinearizeOption::kMustLinearize,
104 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000105}
106
107bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
108 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000109 return OpenDocumentWithOptions(filename, password,
110 LinearizeOption::kDefaultLinearize,
111 JavaScriptOption::kEnableJavaScript);
112}
113
114bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
115 return OpenDocumentWithOptions(filename, nullptr,
116 LinearizeOption::kDefaultLinearize,
117 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000118}
119
120bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
121 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000122 LinearizeOption linearize_option,
123 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800124 std::string file_path;
125 if (!PathService::GetTestFilePath(filename, &file_path))
126 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000127
Wei Li091f7a02015-11-09 12:09:55 -0800128 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400129 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800130 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800131
thestig29ce9232016-06-22 07:03:23 -0700132 EXPECT_TRUE(!loader_);
Tom Sepez7df04832020-05-18 22:09:31 +0000133 loader_ = std::make_unique<TestLoader>(
Lei Zhangb3be4a12019-02-05 22:11:07 +0000134 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000135
136 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800137 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700138 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000139 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000140
Tom Sepez7df04832020-05-18 22:09:31 +0000141 fake_file_access_ = std::make_unique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000142 return OpenDocumentHelper(password, linearize_option, javascript_option,
143 fake_file_access_.get(), &document_, &avail_,
144 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400145}
Tom Sepez96d13342015-01-16 14:59:26 -0800146
Nicolas Pena56fc9722017-07-13 16:31:34 -0400147bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000148 LinearizeOption linearize_option,
149 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300150 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400151 FPDF_DOCUMENT* document,
152 FPDF_AVAIL* avail,
153 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300154 network_simulator->AddSegment(0, 1024);
155 network_simulator->SetRequestedDataAvailable();
156 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
157 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400158 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300159 int32_t nRet = PDF_DATA_NOTAVAIL;
160 while (nRet == PDF_DATA_NOTAVAIL) {
161 network_simulator->SetRequestedDataAvailable();
162 nRet =
163 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
164 }
165 if (nRet == PDF_DATA_ERROR)
166 return false;
167
Nicolas Pena56fc9722017-07-13 16:31:34 -0400168 *document = FPDFAvail_GetDocument(*avail, password);
169 if (!*document)
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();
175 nRet =
176 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
177 }
178 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800179 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400180
181 int page_count = FPDF_GetPageCount(*document);
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();
186 nRet = FPDFAvail_IsPageAvail(*avail, i,
187 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();
196 *document =
197 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400198 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800199 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800200 }
Tom Sepez0784c732018-04-23 18:02:57 +0000201 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
202
Ryan Harrison854d71c2017-10-18 12:28:14 -0400203 int doc_type = FPDF_GetFormType(*document);
204 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
205 FPDF_LoadXFA(*document);
Tom Sepez0784c732018-04-23 18:02:57 +0000206
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000207 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800208 return true;
209}
Tom Sepez96d13342015-01-16 14:59:26 -0800210
Tom Sepez38f725f2019-12-04 00:26:34 +0000211void EmbedderTest::CloseDocument() {
Lei Zhang44005192020-04-17 01:35:43 +0000212 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepez38f725f2019-12-04 00:26:34 +0000213 FPDFDOC_ExitFormFillEnvironment(form_handle_);
214 form_handle_ = nullptr;
215
216 FPDF_CloseDocument(document_);
217 document_ = nullptr;
218}
219
Tom Sepez0784c732018-04-23 18:02:57 +0000220FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
221 FPDF_DOCUMENT doc,
222 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800223 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400224 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Tom Sepez72f520c2020-08-24 23:43:46 +0000225 platform->version = 3;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800226 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800227
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800228 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
229 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Aayush Dhirc63913e2020-02-12 09:45:34 +0000230 formfillinfo->version = form_fill_info_version_;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700231 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
232 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700233 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000234 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000235 formfillinfo->FFI_DoGoToAction = DoGoToActionTrampoline;
Aayush Dhirc63913e2020-02-12 09:45:34 +0000236 formfillinfo->FFI_OnFocusChange = OnFocusChangeTrampoline;
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000237 formfillinfo->FFI_DoURIActionWithKeyboardModifier =
238 DoURIActionWithKeyboardModifierTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000239
Tom Sepez0784c732018-04-23 18:02:57 +0000240 if (javascript_option == JavaScriptOption::kEnableJavaScript)
241 formfillinfo->m_pJsPlatform = platform;
242
Nicolas Pena3ff54002017-07-05 11:55:35 -0400243 FPDF_FORMHANDLE form_handle =
244 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Tom Sepez92f92222020-01-07 22:16:07 +0000245 SetInitialFormFieldHighlight(form_handle);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400246 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800247}
248
Tom Sepezda8189e2015-01-30 14:41:50 -0800249void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400250 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800251 FORM_DoDocumentJSAction(form_handle_);
252 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800253}
254
255int EmbedderTest::GetFirstPageNum() {
256 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000257 (void)FPDFAvail_IsPageAvail(avail_, first_page,
258 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800259 return first_page;
260}
261
262int EmbedderTest::GetPageCount() {
263 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400264 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000265 (void)FPDFAvail_IsPageAvail(avail_, i,
266 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800267 return page_count;
268}
269
Tom Sepezda8189e2015-01-30 14:41:50 -0800270FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000271 return LoadPageCommon(page_number, true);
272}
273
274FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
275 return LoadPageCommon(page_number, false);
276}
277
278FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400279 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000280 ASSERT(page_number >= 0);
Lei Zhangf245ae62020-05-15 21:49:46 +0000281 ASSERT(!pdfium::Contains(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700282
Tom Sepez96d13342015-01-16 14:59:26 -0800283 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400284 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800285 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400286
Tom Sepez507d0192018-11-07 16:37:51 +0000287 if (do_events) {
288 FORM_OnAfterLoadPage(page, form_handle_);
289 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
290 }
weili0dadcc62016-08-23 21:10:57 -0700291 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700292 return page;
293}
294
Tom Sepezda8189e2015-01-30 14:41:50 -0800295void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000296 UnloadPageCommon(page, true);
297}
Lei Zhang75c81712018-02-08 17:22:39 +0000298
Tom Sepez507d0192018-11-07 16:37:51 +0000299void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
300 UnloadPageCommon(page, false);
301}
302
303void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
304 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000305 int page_number = GetPageNumberForLoadedPage(page);
306 if (page_number < 0) {
307 NOTREACHED();
308 return;
309 }
Tom Sepez507d0192018-11-07 16:37:51 +0000310 if (do_events) {
311 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
312 FORM_OnBeforeClosePage(page, form_handle_);
313 }
Tom Sepez96d13342015-01-16 14:59:26 -0800314 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000315 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800316}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800317
Tom Sepez92f92222020-01-07 22:16:07 +0000318void EmbedderTest::SetInitialFormFieldHighlight(FPDF_FORMHANDLE form) {
319 FPDF_SetFormFieldHighlightColor(form, FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD);
320 FPDF_SetFormFieldHighlightAlpha(form, 100);
321}
322
Tom Sepeze08d2b12018-04-25 18:49:32 +0000323ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000324 return RenderLoadedPageWithFlags(page, 0);
325}
326
Tom Sepeze08d2b12018-04-25 18:49:32 +0000327ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
328 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000329 if (GetPageNumberForLoadedPage(page) < 0) {
330 NOTREACHED();
331 return nullptr;
332 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000333 return RenderPageWithFlags(page, form_handle_, flags);
334}
335
Tom Sepeze08d2b12018-04-25 18:49:32 +0000336ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000337 return RenderSavedPageWithFlags(page, 0);
338}
339
Tom Sepeze08d2b12018-04-25 18:49:32 +0000340ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
341 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000342 if (GetPageNumberForSavedPage(page) < 0) {
343 NOTREACHED();
344 return nullptr;
345 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000346 return RenderPageWithFlags(page, saved_form_handle_, flags);
347}
348
349// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000350ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
351 FPDF_FORMHANDLE handle,
352 int flags) {
Lei Zhangdfa075b2019-12-05 21:52:43 +0000353 int width = static_cast<int>(FPDF_GetPageWidthF(page));
354 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhanga98e3662018-02-07 20:28:35 +0000355 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000356 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000357 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
358 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
359 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
360 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
361 return bitmap;
362}
363
Lei Zhang30ff2532019-01-31 21:37:55 +0000364// static
365ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
366 return RenderPageWithFlags(page, nullptr, 0);
367}
368
Lei Zhangd69e0652019-04-13 00:39:35 +0000369#if defined(OS_WIN)
370// static
371std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
372 int flags) {
373 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
374
Lei Zhangdfa075b2019-12-05 21:52:43 +0000375 int width = static_cast<int>(FPDF_GetPageWidthF(page));
376 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhangd69e0652019-04-13 00:39:35 +0000377 HRGN rgn = CreateRectRgn(0, 0, width, height);
378 SelectClipRgn(dc, rgn);
379 DeleteObject(rgn);
380
381 SelectObject(dc, GetStockObject(NULL_PEN));
382 SelectObject(dc, GetStockObject(WHITE_BRUSH));
383 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
384 Rectangle(dc, 0, 0, width + 1, height + 1);
385
386 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
387
388 HENHMETAFILE emf = CloseEnhMetaFile(dc);
389 size_t size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
390 std::vector<uint8_t> buffer(size_in_bytes);
391 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
392 DeleteEnhMetaFile(emf);
393 return buffer;
394}
Lei Zhang614f1a02019-05-30 00:03:53 +0000395
396// static
397std::string EmbedderTest::GetPostScriptFromEmf(
Tom Sepez87b9e3a2019-11-20 23:45:43 +0000398 pdfium::span<const uint8_t> emf_data) {
Lei Zhang614f1a02019-05-30 00:03:53 +0000399 // This comes from Emf::InitFromData() in Chromium.
400 HENHMETAFILE emf = SetEnhMetaFileBits(emf_data.size(), emf_data.data());
401 if (!emf)
402 return std::string();
403
404 // This comes from Emf::Enumerator::Enumerator() in Chromium.
405 std::vector<const ENHMETARECORD*> records;
406 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
407 DeleteEnhMetaFile(emf);
408 return std::string();
409 }
410
411 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
412 std::string ps_data;
413 for (const auto* record : records) {
414 if (record->iType != EMR_GDICOMMENT)
415 continue;
416
417 // PostScript data is encapsulated inside EMF comment records.
418 // The first two bytes of the comment indicate the string length. The rest
419 // is the actual string data.
420 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
421 const char* data = reinterpret_cast<const char*>(comment->Data);
422 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
423 data += 2;
424 ps_data.append(data, size);
425 }
426 DeleteEnhMetaFile(emf);
427 return ps_data;
428}
Lei Zhangd69e0652019-04-13 00:39:35 +0000429#endif // defined(OS_WIN)
430
Lei Zhang0b494052019-01-31 21:41:15 +0000431FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
432 return OpenSavedDocumentWithPassword(nullptr);
433}
434
Tom Sepez6dc12402020-01-07 21:02:17 +0000435// static
436int EmbedderTest::BytesPerPixelForFormat(int format) {
437 switch (format) {
438 case FPDFBitmap_Gray:
439 return 1;
440 case FPDFBitmap_BGR:
441 return 3;
442 case FPDFBitmap_BGRx:
443 case FPDFBitmap_BGRA:
444 return 4;
445 default:
446 NOTREACHED();
447 return 0;
448 }
449}
450
Lei Zhang0b494052019-01-31 21:41:15 +0000451FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
452 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300453 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000454 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300455 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000456 // Copy data to prevent clearing it before saved document close.
457 saved_document_file_data_ = data_string_;
458 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400459
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300460 saved_fake_file_access_ =
Tom Sepez7df04832020-05-18 22:09:31 +0000461 std::make_unique<FakeFileAccess>(&saved_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300462
Tom Sepez0784c732018-04-23 18:02:57 +0000463 EXPECT_TRUE(OpenDocumentHelper(
464 password, LinearizeOption::kDefaultLinearize,
465 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
466 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000467 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400468}
469
470void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000471 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400472
Lei Zhang0729be22018-02-05 21:13:51 +0000473 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
474 FPDF_CloseDocument(saved_document_);
475 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400476
Lei Zhang0729be22018-02-05 21:13:51 +0000477 saved_form_handle_ = nullptr;
478 saved_document_ = nullptr;
479 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400480}
481
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000482FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000483 ASSERT(saved_form_handle_);
484 ASSERT(page_number >= 0);
Lei Zhangf245ae62020-05-15 21:49:46 +0000485 ASSERT(!pdfium::Contains(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400486
Lei Zhang0729be22018-02-05 21:13:51 +0000487 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000488 if (!page)
489 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400490
Lei Zhang9f72c452018-02-08 21:49:54 +0000491 FORM_OnAfterLoadPage(page, saved_form_handle_);
492 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
493 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000494 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400495}
496
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000497void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000498 ASSERT(saved_form_handle_);
499
500 int page_number = GetPageNumberForSavedPage(page);
501 if (page_number < 0) {
502 NOTREACHED();
503 return;
504 }
505
506 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
507 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000508 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000509
510 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400511}
512
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000513void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
514 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400515 int height,
516 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000517 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000518 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400519
Tom Sepeze08d2b12018-04-25 18:49:32 +0000520 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000521 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400522}
523
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400524void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000525 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000526 FPDF_PAGE page = LoadSavedPage(0);
527 VerifySavedRendering(page, width, height, md5);
528 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400529 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400530}
531
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300532void EmbedderTest::SetWholeFileAvailable() {
533 ASSERT(fake_file_access_);
534 fake_file_access_->SetWholeFileAvailable();
535}
536
weili0dadcc62016-08-23 21:10:57 -0700537FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700538 FPDF_DOCUMENT document,
539 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700540 EmbedderTest* test = static_cast<EmbedderTest*>(info);
541 auto it = test->page_map_.find(page_index);
542 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700543}
544
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800545// static
546void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
547 int type) {
548 EmbedderTest* test = static_cast<EmbedderTest*>(info);
549 test->delegate_->UnsupportedHandler(type);
550}
551
552// static
553int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
554 FPDF_WIDESTRING message,
555 FPDF_WIDESTRING title,
556 int type,
557 int icon) {
558 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
559 return test->delegate_->Alert(message, title, type, icon);
560}
561
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700562// static
563int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564 int msecs,
565 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700566 EmbedderTest* test = static_cast<EmbedderTest*>(info);
567 return test->delegate_->SetTimer(msecs, fn);
568}
569
570// static
571void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
572 EmbedderTest* test = static_cast<EmbedderTest*>(info);
573 return test->delegate_->KillTimer(id);
574}
575
Tom Sepez396e8722015-09-09 10:16:08 -0700576// static
577FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
578 FPDF_DOCUMENT document,
579 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700580 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
581 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700582}
583
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000584// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000585void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
586 FPDF_BYTESTRING uri) {
587 EmbedderTest* test = static_cast<EmbedderTest*>(info);
588 return test->delegate_->DoURIAction(uri);
589}
590
591// static
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000592void EmbedderTest::DoGoToActionTrampoline(FPDF_FORMFILLINFO* info,
593 int page_index,
594 int zoom_mode,
595 float* pos_array,
596 int array_size) {
597 EmbedderTest* test = static_cast<EmbedderTest*>(info);
598 return test->delegate_->DoGoToAction(info, page_index, zoom_mode, pos_array,
599 array_size);
600}
601
602// static
Aayush Dhirc63913e2020-02-12 09:45:34 +0000603void EmbedderTest::OnFocusChangeTrampoline(FPDF_FORMFILLINFO* info,
604 FPDF_ANNOTATION annot,
605 int page_index) {
606 EmbedderTest* test = static_cast<EmbedderTest*>(info);
607 return test->delegate_->OnFocusChange(info, annot, page_index);
608}
609
610// static
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000611void EmbedderTest::DoURIActionWithKeyboardModifierTrampoline(
612 FPDF_FORMFILLINFO* info,
613 FPDF_BYTESTRING uri,
614 int modifiers) {
615 EmbedderTest* test = static_cast<EmbedderTest*>(info);
616 return test->delegate_->DoURIActionWithKeyboardModifier(info, uri, modifiers);
617}
618
619// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000620std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Lei Zhangbf980142019-12-20 01:05:42 +0000621 int stride = FPDFBitmap_GetStride(bitmap);
622 int usable_bytes_per_row =
623 GetBitmapBytesPerPixel(bitmap) * FPDFBitmap_GetWidth(bitmap);
624 int height = FPDFBitmap_GetHeight(bitmap);
625 auto span = pdfium::make_span(
626 static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)), stride * height);
627
628 CRYPT_md5_context context = CRYPT_MD5Start();
629 for (int i = 0; i < height; ++i)
630 CRYPT_MD5Update(&context, span.subspan(i * stride, usable_bytes_per_row));
Dan Sinclair957480c2017-06-13 15:21:14 -0400631 uint8_t digest[16];
Lei Zhangbf980142019-12-20 01:05:42 +0000632 CRYPT_MD5Finish(&context, digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400633 return CryptToBase16(digest);
634}
635
Henrique Nakashimadb269572018-01-16 19:02:15 +0000636#ifndef NDEBUG
637// static
638void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
639 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000640 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000641}
642#endif
643
thestigbcd3e532016-11-21 13:37:28 -0800644// static
645void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
646 int expected_width,
647 int expected_height,
648 const char* expected_md5sum) {
649 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
650 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400651
652 // The expected stride is calculated using the same formula as in
653 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
654 const int expected_stride =
655 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800656 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
657
658 if (!expected_md5sum)
659 return;
660
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000661 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800662}
663
Nicolas Pena3ff54002017-07-05 11:55:35 -0400664// static
665int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
666 const void* data,
667 unsigned long size) {
668 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000669
Lei Zhang0729be22018-02-05 21:13:51 +0000670 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000671
672 if (pThis->filestream_.is_open())
673 pThis->filestream_.write(static_cast<const char*>(data), size);
674
Nicolas Pena3ff54002017-07-05 11:55:35 -0400675 return 1;
676}
677
678// static
679int EmbedderTest::GetBlockFromString(void* param,
680 unsigned long pos,
681 unsigned char* buf,
682 unsigned long size) {
683 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000684 if (!new_file || pos + size < pos) {
685 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400686 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000687 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400688
Lei Zhangcef91f12019-01-08 21:32:51 +0000689 if (pos + size > new_file->size()) {
690 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400691 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000692 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400693
694 memcpy(buf, new_file->data() + pos, size);
695 return 1;
696}
Lei Zhang75c81712018-02-08 17:22:39 +0000697
Lei Zhang9f72c452018-02-08 21:49:54 +0000698// static
699int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
700 FPDF_PAGE page) {
701 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000702 if (it.second == page) {
703 int page_number = it.first;
704 ASSERT(page_number >= 0);
705 return page_number;
706 }
707 }
708 return -1;
709}
Lei Zhang9f72c452018-02-08 21:49:54 +0000710
711int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
712 return GetPageNumberForPage(page_map_, page);
713}
714
715int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
716 return GetPageNumberForPage(saved_page_map_, page);
717}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000718
Lei Zhang81a799e2019-12-16 17:37:13 +0000719#ifndef NDEBUG
720void EmbedderTest::OpenPDFFileForWrite(const std::string& filename) {
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000721 filestream_.open(filename, std::ios_base::binary);
722}
723
724void EmbedderTest::ClosePDFFileForWrite() {
725 filestream_.close();
726}
Lei Zhang81a799e2019-12-16 17:37:13 +0000727#endif