blob: 650805e87a5c1f110a11de1feda68651b08244a0 [file] [log] [blame]
Lei Zhang1ac47eb2015-12-21 11:04:44 -08001// Copyright 2015 PDFium Authors. All rights reserved.
Tom Sepez96d13342015-01-16 14:59:26 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Wei Li091f7a02015-11-09 12:09:55 -08005#include "testing/embedder_test.h"
Tom Sepez96d13342015-01-16 14:59:26 -08006
7#include <limits.h>
Tom Sepez96d13342015-01-16 14:59:26 -08008
9#include <list>
Lei Zhang9f72c452018-02-08 21:49:54 +000010#include <map>
Lei Zhanga98e3662018-02-07 20:28:35 +000011#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -080012#include <string>
13#include <utility>
Lei Zhangd69e0652019-04-13 00:39:35 +000014#include <vector>
Tom Sepez96d13342015-01-16 14:59:26 -080015
Lei Zhangd145e4b2018-10-12 18:54:31 +000016#include "core/fdrm/fx_crypt.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000017#include "public/cpp/fpdf_scopers.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080018#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080019#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080020#include "public/fpdf_text.h"
21#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080022#include "testing/gmock/include/gmock/gmock.h"
Lei Zhangd50bdff2019-02-05 19:42:33 +000023#include "testing/test_loader.h"
Henrique Nakashimaf956bad2018-08-16 16:41:42 +000024#include "testing/utils/bitmap_saver.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000025#include "testing/utils/file_util.h"
Lei Zhang4c64e962019-02-05 19:24:12 +000026#include "testing/utils/hash.h"
Wei Li091f7a02015-11-09 12:09:55 -080027#include "testing/utils/path_service.h"
Lei Zhang75c81712018-02-08 17:22:39 +000028#include "third_party/base/logging.h"
Lei Zhang75c81712018-02-08 17:22:39 +000029#include "third_party/base/stl_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070030
31#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080032#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050033#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070034#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080035
Tom Sepez96d13342015-01-16 14:59:26 -080036namespace {
thestigc08cd7a2016-06-27 09:47:59 -070037
Jane Liu28fb7ba2017-08-02 21:45:57 -040038int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
Tom Sepez6dc12402020-01-07 21:02:17 +000039 return EmbedderTest::BytesPerPixelForFormat(FPDFBitmap_GetFormat(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -040040}
41
Lei Zhang614f1a02019-05-30 00:03:53 +000042#if defined(OS_WIN)
43int CALLBACK GetRecordProc(HDC hdc,
44 HANDLETABLE* handle_table,
45 const ENHMETARECORD* record,
46 int objects_count,
47 LPARAM param) {
48 auto& records = *reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param);
49 records.push_back(record);
50 return 1;
51}
52#endif // defined(OS_WIN)
53
thestigbcd3e532016-11-21 13:37:28 -080054} // namespace
55
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056EmbedderTest::EmbedderTest()
Tom Sepez7df04832020-05-18 22:09:31 +000057 : default_delegate_(std::make_unique<EmbedderTest::Delegate>()),
Lei Zhang0729be22018-02-05 21:13:51 +000058 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -040059 FPDF_FILEWRITE::version = 1;
60 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080061}
Tom Sepez96d13342015-01-16 14:59:26 -080062
Lei Zhangea208512019-12-18 00:42:11 +000063EmbedderTest::~EmbedderTest() = default;
Tom Sepezf288bb12015-11-20 12:12:46 -080064
65void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070066 FPDF_LIBRARY_CONFIG config;
67 config.version = 2;
68 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070069 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070070 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070071 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080072
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
74 memset(info, 0, sizeof(UNSUPPORT_INFO));
75 info->version = 1;
76 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
77 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +000078
Lei Zhang0729be22018-02-05 21:13:51 +000079 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080}
Tom Sepez96d13342015-01-16 14:59:26 -080081
82void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +000083 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
84 // possible. This can fail when an ASSERT test fails in a test case.
85 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +000086 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang75c81712018-02-08 17:22:39 +000087
Lei Zhang44005192020-04-17 01:35:43 +000088 if (document_)
Tom Sepez38f725f2019-12-04 00:26:34 +000089 CloseDocument();
Tom Sepezc46d0002015-11-30 15:46:36 -080090
Tom Sepez96d13342015-01-16 14:59:26 -080091 FPDFAvail_Destroy(avail_);
92 FPDF_DestroyLibrary();
Lei Zhangb3be4a12019-02-05 22:11:07 +000093 loader_.reset();
Tom Sepez96d13342015-01-16 14:59:26 -080094}
95
Lei Zhang378ec542018-10-18 19:15:47 +000096#ifdef PDF_ENABLE_V8
97void EmbedderTest::SetExternalIsolate(void* isolate) {
98 external_isolate_ = static_cast<v8::Isolate*>(isolate);
99}
100#endif // PDF_ENABLE_V8
101
Tom Sepezd483eb42016-01-06 10:03:59 -0800102bool EmbedderTest::CreateEmptyDocument() {
103 document_ = FPDF_CreateNewDocument();
104 if (!document_)
105 return false;
106
Tom Sepez0784c732018-04-23 18:02:57 +0000107 form_handle_ =
108 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -0800109 return true;
110}
111
Lei Zhang208eecf2017-12-20 19:40:50 +0000112bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000113 return OpenDocumentWithOptions(filename, nullptr,
114 LinearizeOption::kDefaultLinearize,
115 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000116}
117
118bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000119 return OpenDocumentWithOptions(filename, nullptr,
120 LinearizeOption::kMustLinearize,
121 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000122}
123
124bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
125 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000126 return OpenDocumentWithOptions(filename, password,
127 LinearizeOption::kDefaultLinearize,
128 JavaScriptOption::kEnableJavaScript);
129}
130
131bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
132 return OpenDocumentWithOptions(filename, nullptr,
133 LinearizeOption::kDefaultLinearize,
134 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000135}
136
137bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
138 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000139 LinearizeOption linearize_option,
140 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800141 std::string file_path;
142 if (!PathService::GetTestFilePath(filename, &file_path))
143 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000144
Wei Li091f7a02015-11-09 12:09:55 -0800145 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400146 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800147 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800148
thestig29ce9232016-06-22 07:03:23 -0700149 EXPECT_TRUE(!loader_);
Tom Sepez7df04832020-05-18 22:09:31 +0000150 loader_ = std::make_unique<TestLoader>(
Lei Zhangb3be4a12019-02-05 22:11:07 +0000151 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000152
153 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800154 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700155 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000156 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000157
Tom Sepez7df04832020-05-18 22:09:31 +0000158 fake_file_access_ = std::make_unique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000159 return OpenDocumentHelper(password, linearize_option, javascript_option,
160 fake_file_access_.get(), &document_, &avail_,
161 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400162}
Tom Sepez96d13342015-01-16 14:59:26 -0800163
Nicolas Pena56fc9722017-07-13 16:31:34 -0400164bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000165 LinearizeOption linearize_option,
166 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300167 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400168 FPDF_DOCUMENT* document,
169 FPDF_AVAIL* avail,
170 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300171 network_simulator->AddSegment(0, 1024);
172 network_simulator->SetRequestedDataAvailable();
173 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
174 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400175 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300176 int32_t nRet = PDF_DATA_NOTAVAIL;
177 while (nRet == PDF_DATA_NOTAVAIL) {
178 network_simulator->SetRequestedDataAvailable();
179 nRet =
180 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
181 }
182 if (nRet == PDF_DATA_ERROR)
183 return false;
184
Nicolas Pena56fc9722017-07-13 16:31:34 -0400185 *document = FPDFAvail_GetDocument(*avail, password);
186 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800187 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400188
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300189 nRet = PDF_DATA_NOTAVAIL;
190 while (nRet == PDF_DATA_NOTAVAIL) {
191 network_simulator->SetRequestedDataAvailable();
192 nRet =
193 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
194 }
195 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800196 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400197
198 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800199 for (int i = 0; i < page_count; ++i) {
200 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300201 while (nRet == PDF_DATA_NOTAVAIL) {
202 network_simulator->SetRequestedDataAvailable();
203 nRet = FPDFAvail_IsPageAvail(*avail, i,
204 network_simulator->GetDownloadHints());
205 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400206 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800207 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800208 }
209 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000210 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800211 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300212 network_simulator->SetWholeFileAvailable();
213 *document =
214 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400215 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800216 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800217 }
Tom Sepez0784c732018-04-23 18:02:57 +0000218 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
219
Ryan Harrison854d71c2017-10-18 12:28:14 -0400220 int doc_type = FPDF_GetFormType(*document);
221 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
222 FPDF_LoadXFA(*document);
Tom Sepez0784c732018-04-23 18:02:57 +0000223
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000224 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800225 return true;
226}
Tom Sepez96d13342015-01-16 14:59:26 -0800227
Tom Sepez38f725f2019-12-04 00:26:34 +0000228void EmbedderTest::CloseDocument() {
Lei Zhang44005192020-04-17 01:35:43 +0000229 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepez38f725f2019-12-04 00:26:34 +0000230 FPDFDOC_ExitFormFillEnvironment(form_handle_);
231 form_handle_ = nullptr;
232
233 FPDF_CloseDocument(document_);
234 document_ = nullptr;
235}
236
Tom Sepez0784c732018-04-23 18:02:57 +0000237FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
238 FPDF_DOCUMENT doc,
239 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800240 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400241 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200242 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800243 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400244 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800245
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800246 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
247 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Aayush Dhirc63913e2020-02-12 09:45:34 +0000248 formfillinfo->version = form_fill_info_version_;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700249 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
250 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700251 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000252 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000253 formfillinfo->FFI_DoGoToAction = DoGoToActionTrampoline;
Aayush Dhirc63913e2020-02-12 09:45:34 +0000254 formfillinfo->FFI_OnFocusChange = OnFocusChangeTrampoline;
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000255 formfillinfo->FFI_DoURIActionWithKeyboardModifier =
256 DoURIActionWithKeyboardModifierTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000257
Tom Sepez0784c732018-04-23 18:02:57 +0000258 if (javascript_option == JavaScriptOption::kEnableJavaScript)
259 formfillinfo->m_pJsPlatform = platform;
260
Nicolas Pena3ff54002017-07-05 11:55:35 -0400261 FPDF_FORMHANDLE form_handle =
262 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Tom Sepez92f92222020-01-07 22:16:07 +0000263 SetInitialFormFieldHighlight(form_handle);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400264 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800265}
266
Tom Sepezda8189e2015-01-30 14:41:50 -0800267void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400268 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800269 FORM_DoDocumentJSAction(form_handle_);
270 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800271}
272
273int EmbedderTest::GetFirstPageNum() {
274 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000275 (void)FPDFAvail_IsPageAvail(avail_, first_page,
276 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800277 return first_page;
278}
279
280int EmbedderTest::GetPageCount() {
281 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400282 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000283 (void)FPDFAvail_IsPageAvail(avail_, i,
284 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800285 return page_count;
286}
287
Tom Sepezda8189e2015-01-30 14:41:50 -0800288FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000289 return LoadPageCommon(page_number, true);
290}
291
292FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
293 return LoadPageCommon(page_number, false);
294}
295
296FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400297 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000298 ASSERT(page_number >= 0);
Lei Zhangf245ae62020-05-15 21:49:46 +0000299 ASSERT(!pdfium::Contains(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700300
Tom Sepez96d13342015-01-16 14:59:26 -0800301 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400302 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800303 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400304
Tom Sepez507d0192018-11-07 16:37:51 +0000305 if (do_events) {
306 FORM_OnAfterLoadPage(page, form_handle_);
307 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
308 }
weili0dadcc62016-08-23 21:10:57 -0700309 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700310 return page;
311}
312
Tom Sepezda8189e2015-01-30 14:41:50 -0800313void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000314 UnloadPageCommon(page, true);
315}
Lei Zhang75c81712018-02-08 17:22:39 +0000316
Tom Sepez507d0192018-11-07 16:37:51 +0000317void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
318 UnloadPageCommon(page, false);
319}
320
321void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
322 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000323 int page_number = GetPageNumberForLoadedPage(page);
324 if (page_number < 0) {
325 NOTREACHED();
326 return;
327 }
Tom Sepez507d0192018-11-07 16:37:51 +0000328 if (do_events) {
329 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
330 FORM_OnBeforeClosePage(page, form_handle_);
331 }
Tom Sepez96d13342015-01-16 14:59:26 -0800332 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000333 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800334}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800335
Tom Sepez92f92222020-01-07 22:16:07 +0000336void EmbedderTest::SetInitialFormFieldHighlight(FPDF_FORMHANDLE form) {
337 FPDF_SetFormFieldHighlightColor(form, FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD);
338 FPDF_SetFormFieldHighlightAlpha(form, 100);
339}
340
Tom Sepeze08d2b12018-04-25 18:49:32 +0000341ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000342 return RenderLoadedPageWithFlags(page, 0);
343}
344
Tom Sepeze08d2b12018-04-25 18:49:32 +0000345ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
346 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000347 if (GetPageNumberForLoadedPage(page) < 0) {
348 NOTREACHED();
349 return nullptr;
350 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000351 return RenderPageWithFlags(page, form_handle_, flags);
352}
353
Tom Sepeze08d2b12018-04-25 18:49:32 +0000354ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000355 return RenderSavedPageWithFlags(page, 0);
356}
357
Tom Sepeze08d2b12018-04-25 18:49:32 +0000358ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
359 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000360 if (GetPageNumberForSavedPage(page) < 0) {
361 NOTREACHED();
362 return nullptr;
363 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000364 return RenderPageWithFlags(page, saved_form_handle_, flags);
365}
366
367// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000368ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
369 FPDF_FORMHANDLE handle,
370 int flags) {
Lei Zhangdfa075b2019-12-05 21:52:43 +0000371 int width = static_cast<int>(FPDF_GetPageWidthF(page));
372 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhanga98e3662018-02-07 20:28:35 +0000373 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000374 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000375 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
376 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
377 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
378 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
379 return bitmap;
380}
381
Lei Zhang30ff2532019-01-31 21:37:55 +0000382// static
383ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
384 return RenderPageWithFlags(page, nullptr, 0);
385}
386
Lei Zhangd69e0652019-04-13 00:39:35 +0000387#if defined(OS_WIN)
388// static
389std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
390 int flags) {
391 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
392
Lei Zhangdfa075b2019-12-05 21:52:43 +0000393 int width = static_cast<int>(FPDF_GetPageWidthF(page));
394 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhangd69e0652019-04-13 00:39:35 +0000395 HRGN rgn = CreateRectRgn(0, 0, width, height);
396 SelectClipRgn(dc, rgn);
397 DeleteObject(rgn);
398
399 SelectObject(dc, GetStockObject(NULL_PEN));
400 SelectObject(dc, GetStockObject(WHITE_BRUSH));
401 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
402 Rectangle(dc, 0, 0, width + 1, height + 1);
403
404 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
405
406 HENHMETAFILE emf = CloseEnhMetaFile(dc);
407 size_t size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
408 std::vector<uint8_t> buffer(size_in_bytes);
409 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
410 DeleteEnhMetaFile(emf);
411 return buffer;
412}
Lei Zhang614f1a02019-05-30 00:03:53 +0000413
414// static
415std::string EmbedderTest::GetPostScriptFromEmf(
Tom Sepez87b9e3a2019-11-20 23:45:43 +0000416 pdfium::span<const uint8_t> emf_data) {
Lei Zhang614f1a02019-05-30 00:03:53 +0000417 // This comes from Emf::InitFromData() in Chromium.
418 HENHMETAFILE emf = SetEnhMetaFileBits(emf_data.size(), emf_data.data());
419 if (!emf)
420 return std::string();
421
422 // This comes from Emf::Enumerator::Enumerator() in Chromium.
423 std::vector<const ENHMETARECORD*> records;
424 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
425 DeleteEnhMetaFile(emf);
426 return std::string();
427 }
428
429 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
430 std::string ps_data;
431 for (const auto* record : records) {
432 if (record->iType != EMR_GDICOMMENT)
433 continue;
434
435 // PostScript data is encapsulated inside EMF comment records.
436 // The first two bytes of the comment indicate the string length. The rest
437 // is the actual string data.
438 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
439 const char* data = reinterpret_cast<const char*>(comment->Data);
440 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
441 data += 2;
442 ps_data.append(data, size);
443 }
444 DeleteEnhMetaFile(emf);
445 return ps_data;
446}
Lei Zhangd69e0652019-04-13 00:39:35 +0000447#endif // defined(OS_WIN)
448
Lei Zhang0b494052019-01-31 21:41:15 +0000449FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
450 return OpenSavedDocumentWithPassword(nullptr);
451}
452
Tom Sepez6dc12402020-01-07 21:02:17 +0000453// static
454int EmbedderTest::BytesPerPixelForFormat(int format) {
455 switch (format) {
456 case FPDFBitmap_Gray:
457 return 1;
458 case FPDFBitmap_BGR:
459 return 3;
460 case FPDFBitmap_BGRx:
461 case FPDFBitmap_BGRA:
462 return 4;
463 default:
464 NOTREACHED();
465 return 0;
466 }
467}
468
Lei Zhang0b494052019-01-31 21:41:15 +0000469FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
470 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300471 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000472 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300473 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000474 // Copy data to prevent clearing it before saved document close.
475 saved_document_file_data_ = data_string_;
476 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400477
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300478 saved_fake_file_access_ =
Tom Sepez7df04832020-05-18 22:09:31 +0000479 std::make_unique<FakeFileAccess>(&saved_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300480
Tom Sepez0784c732018-04-23 18:02:57 +0000481 EXPECT_TRUE(OpenDocumentHelper(
482 password, LinearizeOption::kDefaultLinearize,
483 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
484 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000485 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400486}
487
488void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000489 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400490
Lei Zhang0729be22018-02-05 21:13:51 +0000491 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
492 FPDF_CloseDocument(saved_document_);
493 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400494
Lei Zhang0729be22018-02-05 21:13:51 +0000495 saved_form_handle_ = nullptr;
496 saved_document_ = nullptr;
497 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400498}
499
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000500FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000501 ASSERT(saved_form_handle_);
502 ASSERT(page_number >= 0);
Lei Zhangf245ae62020-05-15 21:49:46 +0000503 ASSERT(!pdfium::Contains(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400504
Lei Zhang0729be22018-02-05 21:13:51 +0000505 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000506 if (!page)
507 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400508
Lei Zhang9f72c452018-02-08 21:49:54 +0000509 FORM_OnAfterLoadPage(page, saved_form_handle_);
510 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
511 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000512 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400513}
514
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000515void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000516 ASSERT(saved_form_handle_);
517
518 int page_number = GetPageNumberForSavedPage(page);
519 if (page_number < 0) {
520 NOTREACHED();
521 return;
522 }
523
524 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
525 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000526 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000527
528 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400529}
530
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000531void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
532 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400533 int height,
534 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000535 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000536 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400537
Tom Sepeze08d2b12018-04-25 18:49:32 +0000538 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000539 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400540}
541
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400542void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000543 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000544 FPDF_PAGE page = LoadSavedPage(0);
545 VerifySavedRendering(page, width, height, md5);
546 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400547 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400548}
549
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300550void EmbedderTest::SetWholeFileAvailable() {
551 ASSERT(fake_file_access_);
552 fake_file_access_->SetWholeFileAvailable();
553}
554
weili0dadcc62016-08-23 21:10:57 -0700555FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700556 FPDF_DOCUMENT document,
557 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700558 EmbedderTest* test = static_cast<EmbedderTest*>(info);
559 auto it = test->page_map_.find(page_index);
560 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700561}
562
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800563// static
564void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
565 int type) {
566 EmbedderTest* test = static_cast<EmbedderTest*>(info);
567 test->delegate_->UnsupportedHandler(type);
568}
569
570// static
571int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
572 FPDF_WIDESTRING message,
573 FPDF_WIDESTRING title,
574 int type,
575 int icon) {
576 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
577 return test->delegate_->Alert(message, title, type, icon);
578}
579
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700580// static
581int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582 int msecs,
583 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700584 EmbedderTest* test = static_cast<EmbedderTest*>(info);
585 return test->delegate_->SetTimer(msecs, fn);
586}
587
588// static
589void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
590 EmbedderTest* test = static_cast<EmbedderTest*>(info);
591 return test->delegate_->KillTimer(id);
592}
593
Tom Sepez396e8722015-09-09 10:16:08 -0700594// static
595FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
596 FPDF_DOCUMENT document,
597 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700598 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
599 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700600}
601
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000602// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000603void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
604 FPDF_BYTESTRING uri) {
605 EmbedderTest* test = static_cast<EmbedderTest*>(info);
606 return test->delegate_->DoURIAction(uri);
607}
608
609// static
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000610void EmbedderTest::DoGoToActionTrampoline(FPDF_FORMFILLINFO* info,
611 int page_index,
612 int zoom_mode,
613 float* pos_array,
614 int array_size) {
615 EmbedderTest* test = static_cast<EmbedderTest*>(info);
616 return test->delegate_->DoGoToAction(info, page_index, zoom_mode, pos_array,
617 array_size);
618}
619
620// static
Aayush Dhirc63913e2020-02-12 09:45:34 +0000621void EmbedderTest::OnFocusChangeTrampoline(FPDF_FORMFILLINFO* info,
622 FPDF_ANNOTATION annot,
623 int page_index) {
624 EmbedderTest* test = static_cast<EmbedderTest*>(info);
625 return test->delegate_->OnFocusChange(info, annot, page_index);
626}
627
628// static
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000629void EmbedderTest::DoURIActionWithKeyboardModifierTrampoline(
630 FPDF_FORMFILLINFO* info,
631 FPDF_BYTESTRING uri,
632 int modifiers) {
633 EmbedderTest* test = static_cast<EmbedderTest*>(info);
634 return test->delegate_->DoURIActionWithKeyboardModifier(info, uri, modifiers);
635}
636
637// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000638std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Lei Zhangbf980142019-12-20 01:05:42 +0000639 int stride = FPDFBitmap_GetStride(bitmap);
640 int usable_bytes_per_row =
641 GetBitmapBytesPerPixel(bitmap) * FPDFBitmap_GetWidth(bitmap);
642 int height = FPDFBitmap_GetHeight(bitmap);
643 auto span = pdfium::make_span(
644 static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)), stride * height);
645
646 CRYPT_md5_context context = CRYPT_MD5Start();
647 for (int i = 0; i < height; ++i)
648 CRYPT_MD5Update(&context, span.subspan(i * stride, usable_bytes_per_row));
Dan Sinclair957480c2017-06-13 15:21:14 -0400649 uint8_t digest[16];
Lei Zhangbf980142019-12-20 01:05:42 +0000650 CRYPT_MD5Finish(&context, digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400651 return CryptToBase16(digest);
652}
653
Henrique Nakashimadb269572018-01-16 19:02:15 +0000654#ifndef NDEBUG
655// static
656void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
657 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000658 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000659}
660#endif
661
thestigbcd3e532016-11-21 13:37:28 -0800662// static
663void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
664 int expected_width,
665 int expected_height,
666 const char* expected_md5sum) {
667 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
668 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400669
670 // The expected stride is calculated using the same formula as in
671 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
672 const int expected_stride =
673 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800674 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
675
676 if (!expected_md5sum)
677 return;
678
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000679 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800680}
681
Nicolas Pena3ff54002017-07-05 11:55:35 -0400682// static
683int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
684 const void* data,
685 unsigned long size) {
686 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000687
Lei Zhang0729be22018-02-05 21:13:51 +0000688 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000689
690 if (pThis->filestream_.is_open())
691 pThis->filestream_.write(static_cast<const char*>(data), size);
692
Nicolas Pena3ff54002017-07-05 11:55:35 -0400693 return 1;
694}
695
696// static
697int EmbedderTest::GetBlockFromString(void* param,
698 unsigned long pos,
699 unsigned char* buf,
700 unsigned long size) {
701 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000702 if (!new_file || pos + size < pos) {
703 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400704 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000705 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400706
Lei Zhangcef91f12019-01-08 21:32:51 +0000707 if (pos + size > new_file->size()) {
708 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400709 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000710 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400711
712 memcpy(buf, new_file->data() + pos, size);
713 return 1;
714}
Lei Zhang75c81712018-02-08 17:22:39 +0000715
Lei Zhang9f72c452018-02-08 21:49:54 +0000716// static
717int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
718 FPDF_PAGE page) {
719 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000720 if (it.second == page) {
721 int page_number = it.first;
722 ASSERT(page_number >= 0);
723 return page_number;
724 }
725 }
726 return -1;
727}
Lei Zhang9f72c452018-02-08 21:49:54 +0000728
729int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
730 return GetPageNumberForPage(page_map_, page);
731}
732
733int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
734 return GetPageNumberForPage(saved_page_map_, page);
735}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000736
Lei Zhang81a799e2019-12-16 17:37:13 +0000737#ifndef NDEBUG
738void EmbedderTest::OpenPDFFileForWrite(const std::string& filename) {
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000739 filestream_.open(filename, std::ios_base::binary);
740}
741
742void EmbedderTest::ClosePDFFileForWrite() {
743 filestream_.close();
744}
Lei Zhang81a799e2019-12-16 17:37:13 +0000745#endif