blob: 7d91b048bcf61baff2642a52d207aa70ad1a0820 [file] [log] [blame]
Lei Zhang1ac47eb2015-12-21 11:04:44 -08001// Copyright 2015 PDFium Authors. All rights reserved.
Tom Sepez96d13342015-01-16 14:59:26 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Wei Li091f7a02015-11-09 12:09:55 -08005#include "testing/embedder_test.h"
Tom Sepez96d13342015-01-16 14:59:26 -08006
Lei Zhanga98e3662018-02-07 20:28:35 +00007#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -08008#include <string>
9#include <utility>
Lei Zhangd69e0652019-04-13 00:39:35 +000010#include <vector>
Tom Sepez96d13342015-01-16 14:59:26 -080011
Lei Zhangd145e4b2018-10-12 18:54:31 +000012#include "core/fdrm/fx_crypt.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000013#include "public/cpp/fpdf_scopers.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080014#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080015#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080016#include "public/fpdf_text.h"
17#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080018#include "testing/gmock/include/gmock/gmock.h"
Lei Zhangd50bdff2019-02-05 19:42:33 +000019#include "testing/test_loader.h"
Henrique Nakashimaf956bad2018-08-16 16:41:42 +000020#include "testing/utils/bitmap_saver.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000021#include "testing/utils/file_util.h"
Lei Zhang4c64e962019-02-05 19:24:12 +000022#include "testing/utils/hash.h"
Wei Li091f7a02015-11-09 12:09:55 -080023#include "testing/utils/path_service.h"
Lei Zhang30632072020-10-02 01:57:54 +000024#include "third_party/base/notreached.h"
Lei Zhang75c81712018-02-08 17:22:39 +000025#include "third_party/base/stl_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070026
27#ifdef PDF_ENABLE_V8
Tom Sepezeaf9d212020-07-22 16:24:00 +000028#include "testing/v8_test_environment.h"
Lei Zhang8241df72015-11-06 14:38:48 -080029#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050030#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070031#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080032
Tom Sepez96d13342015-01-16 14:59:26 -080033namespace {
thestigc08cd7a2016-06-27 09:47:59 -070034
Jane Liu28fb7ba2017-08-02 21:45:57 -040035int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
Tom Sepez6dc12402020-01-07 21:02:17 +000036 return EmbedderTest::BytesPerPixelForFormat(FPDFBitmap_GetFormat(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -040037}
38
Lei Zhang614f1a02019-05-30 00:03:53 +000039#if defined(OS_WIN)
40int CALLBACK GetRecordProc(HDC hdc,
41 HANDLETABLE* handle_table,
42 const ENHMETARECORD* record,
43 int objects_count,
44 LPARAM param) {
45 auto& records = *reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param);
46 records.push_back(record);
47 return 1;
48}
49#endif // defined(OS_WIN)
50
thestigbcd3e532016-11-21 13:37:28 -080051} // namespace
52
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053EmbedderTest::EmbedderTest()
Tom Sepez7df04832020-05-18 22:09:31 +000054 : default_delegate_(std::make_unique<EmbedderTest::Delegate>()),
Lei Zhang0729be22018-02-05 21:13:51 +000055 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -040056 FPDF_FILEWRITE::version = 1;
57 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080058}
Tom Sepez96d13342015-01-16 14:59:26 -080059
Lei Zhangea208512019-12-18 00:42:11 +000060EmbedderTest::~EmbedderTest() = default;
Tom Sepezf288bb12015-11-20 12:12:46 -080061
62void EmbedderTest::SetUp() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
64 memset(info, 0, sizeof(UNSUPPORT_INFO));
65 info->version = 1;
66 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
67 FSDK_SetUnSpObjProcessHandler(info);
Lei Zhang0729be22018-02-05 21:13:51 +000068 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069}
Tom Sepez96d13342015-01-16 14:59:26 -080070
71void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +000072 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
73 // possible. This can fail when an ASSERT test fails in a test case.
74 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +000075 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang44005192020-04-17 01:35:43 +000076 if (document_)
Tom Sepez38f725f2019-12-04 00:26:34 +000077 CloseDocument();
Tom Sepezc46d0002015-11-30 15:46:36 -080078
Tom Sepez96d13342015-01-16 14:59:26 -080079 FPDFAvail_Destroy(avail_);
Lei Zhangb3be4a12019-02-05 22:11:07 +000080 loader_.reset();
Tom Sepez96d13342015-01-16 14:59:26 -080081}
82
Tom Sepezd483eb42016-01-06 10:03:59 -080083bool EmbedderTest::CreateEmptyDocument() {
84 document_ = FPDF_CreateNewDocument();
85 if (!document_)
86 return false;
87
Tom Sepez0784c732018-04-23 18:02:57 +000088 form_handle_ =
89 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -080090 return true;
91}
92
Lei Zhang208eecf2017-12-20 19:40:50 +000093bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +000094 return OpenDocumentWithOptions(filename, nullptr,
95 LinearizeOption::kDefaultLinearize,
96 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +000097}
98
99bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000100 return OpenDocumentWithOptions(filename, nullptr,
101 LinearizeOption::kMustLinearize,
102 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000103}
104
105bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
106 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000107 return OpenDocumentWithOptions(filename, password,
108 LinearizeOption::kDefaultLinearize,
109 JavaScriptOption::kEnableJavaScript);
110}
111
112bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
113 return OpenDocumentWithOptions(filename, nullptr,
114 LinearizeOption::kDefaultLinearize,
115 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000116}
117
118bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
119 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000120 LinearizeOption linearize_option,
121 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800122 std::string file_path;
123 if (!PathService::GetTestFilePath(filename, &file_path))
124 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000125
Wei Li091f7a02015-11-09 12:09:55 -0800126 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400127 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800128 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800129
thestig29ce9232016-06-22 07:03:23 -0700130 EXPECT_TRUE(!loader_);
Tom Sepez7df04832020-05-18 22:09:31 +0000131 loader_ = std::make_unique<TestLoader>(
Lei Zhangb3be4a12019-02-05 22:11:07 +0000132 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000133
134 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800135 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700136 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000137 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000138
Tom Sepez7df04832020-05-18 22:09:31 +0000139 fake_file_access_ = std::make_unique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000140 return OpenDocumentHelper(password, linearize_option, javascript_option,
141 fake_file_access_.get(), &document_, &avail_,
142 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400143}
Tom Sepez96d13342015-01-16 14:59:26 -0800144
Nicolas Pena56fc9722017-07-13 16:31:34 -0400145bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000146 LinearizeOption linearize_option,
147 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300148 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400149 FPDF_DOCUMENT* document,
150 FPDF_AVAIL* avail,
151 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300152 network_simulator->AddSegment(0, 1024);
153 network_simulator->SetRequestedDataAvailable();
154 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
155 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400156 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300157 int32_t nRet = PDF_DATA_NOTAVAIL;
158 while (nRet == PDF_DATA_NOTAVAIL) {
159 network_simulator->SetRequestedDataAvailable();
160 nRet =
161 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
162 }
163 if (nRet == PDF_DATA_ERROR)
164 return false;
165
Nicolas Pena56fc9722017-07-13 16:31:34 -0400166 *document = FPDFAvail_GetDocument(*avail, password);
167 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800168 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400169
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300170 nRet = PDF_DATA_NOTAVAIL;
171 while (nRet == PDF_DATA_NOTAVAIL) {
172 network_simulator->SetRequestedDataAvailable();
173 nRet =
174 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
175 }
176 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800177 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400178
179 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800180 for (int i = 0; i < page_count; ++i) {
181 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300182 while (nRet == PDF_DATA_NOTAVAIL) {
183 network_simulator->SetRequestedDataAvailable();
184 nRet = FPDFAvail_IsPageAvail(*avail, i,
185 network_simulator->GetDownloadHints());
186 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400187 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800188 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800189 }
190 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000191 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800192 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300193 network_simulator->SetWholeFileAvailable();
194 *document =
195 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400196 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800197 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800198 }
Tom Sepez0784c732018-04-23 18:02:57 +0000199 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
200
Ryan Harrison854d71c2017-10-18 12:28:14 -0400201 int doc_type = FPDF_GetFormType(*document);
202 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
203 FPDF_LoadXFA(*document);
Tom Sepez0784c732018-04-23 18:02:57 +0000204
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000205 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800206 return true;
207}
Tom Sepez96d13342015-01-16 14:59:26 -0800208
Tom Sepez38f725f2019-12-04 00:26:34 +0000209void EmbedderTest::CloseDocument() {
Lei Zhang44005192020-04-17 01:35:43 +0000210 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepez38f725f2019-12-04 00:26:34 +0000211 FPDFDOC_ExitFormFillEnvironment(form_handle_);
212 form_handle_ = nullptr;
213
214 FPDF_CloseDocument(document_);
215 document_ = nullptr;
216}
217
Tom Sepez0784c732018-04-23 18:02:57 +0000218FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
219 FPDF_DOCUMENT doc,
220 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800221 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400222 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Tom Sepez72f520c2020-08-24 23:43:46 +0000223 platform->version = 3;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800224 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800225
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800226 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
227 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Aayush Dhirc63913e2020-02-12 09:45:34 +0000228 formfillinfo->version = form_fill_info_version_;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700229 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
230 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700231 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000232 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000233 formfillinfo->FFI_DoGoToAction = DoGoToActionTrampoline;
Aayush Dhirc63913e2020-02-12 09:45:34 +0000234 formfillinfo->FFI_OnFocusChange = OnFocusChangeTrampoline;
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000235 formfillinfo->FFI_DoURIActionWithKeyboardModifier =
236 DoURIActionWithKeyboardModifierTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000237
Tom Sepez0784c732018-04-23 18:02:57 +0000238 if (javascript_option == JavaScriptOption::kEnableJavaScript)
239 formfillinfo->m_pJsPlatform = platform;
240
Nicolas Pena3ff54002017-07-05 11:55:35 -0400241 FPDF_FORMHANDLE form_handle =
242 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Tom Sepez92f92222020-01-07 22:16:07 +0000243 SetInitialFormFieldHighlight(form_handle);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400244 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800245}
246
Tom Sepezda8189e2015-01-30 14:41:50 -0800247void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400248 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800249 FORM_DoDocumentJSAction(form_handle_);
250 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800251}
252
253int EmbedderTest::GetFirstPageNum() {
254 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000255 (void)FPDFAvail_IsPageAvail(avail_, first_page,
256 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800257 return first_page;
258}
259
260int EmbedderTest::GetPageCount() {
261 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400262 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000263 (void)FPDFAvail_IsPageAvail(avail_, i,
264 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800265 return page_count;
266}
267
Tom Sepezda8189e2015-01-30 14:41:50 -0800268FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000269 return LoadPageCommon(page_number, true);
270}
271
272FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
273 return LoadPageCommon(page_number, false);
274}
275
276FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400277 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000278 ASSERT(page_number >= 0);
Lei Zhangf245ae62020-05-15 21:49:46 +0000279 ASSERT(!pdfium::Contains(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700280
Tom Sepez96d13342015-01-16 14:59:26 -0800281 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400282 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800283 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400284
Tom Sepez507d0192018-11-07 16:37:51 +0000285 if (do_events) {
286 FORM_OnAfterLoadPage(page, form_handle_);
287 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
288 }
weili0dadcc62016-08-23 21:10:57 -0700289 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700290 return page;
291}
292
Tom Sepezda8189e2015-01-30 14:41:50 -0800293void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000294 UnloadPageCommon(page, true);
295}
Lei Zhang75c81712018-02-08 17:22:39 +0000296
Tom Sepez507d0192018-11-07 16:37:51 +0000297void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
298 UnloadPageCommon(page, false);
299}
300
301void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
302 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000303 int page_number = GetPageNumberForLoadedPage(page);
304 if (page_number < 0) {
305 NOTREACHED();
306 return;
307 }
Tom Sepez507d0192018-11-07 16:37:51 +0000308 if (do_events) {
309 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
310 FORM_OnBeforeClosePage(page, form_handle_);
311 }
Tom Sepez96d13342015-01-16 14:59:26 -0800312 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000313 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800314}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800315
Tom Sepez92f92222020-01-07 22:16:07 +0000316void EmbedderTest::SetInitialFormFieldHighlight(FPDF_FORMHANDLE form) {
317 FPDF_SetFormFieldHighlightColor(form, FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD);
318 FPDF_SetFormFieldHighlightAlpha(form, 100);
319}
320
Tom Sepeze08d2b12018-04-25 18:49:32 +0000321ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000322 return RenderLoadedPageWithFlags(page, 0);
323}
324
Tom Sepeze08d2b12018-04-25 18:49:32 +0000325ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
326 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000327 if (GetPageNumberForLoadedPage(page) < 0) {
328 NOTREACHED();
329 return nullptr;
330 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000331 return RenderPageWithFlags(page, form_handle_, flags);
332}
333
Tom Sepeze08d2b12018-04-25 18:49:32 +0000334ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000335 return RenderSavedPageWithFlags(page, 0);
336}
337
Tom Sepeze08d2b12018-04-25 18:49:32 +0000338ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
339 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000340 if (GetPageNumberForSavedPage(page) < 0) {
341 NOTREACHED();
342 return nullptr;
343 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000344 return RenderPageWithFlags(page, saved_form_handle_, flags);
345}
346
347// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000348ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
349 FPDF_FORMHANDLE handle,
350 int flags) {
Lei Zhangdfa075b2019-12-05 21:52:43 +0000351 int width = static_cast<int>(FPDF_GetPageWidthF(page));
352 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhanga98e3662018-02-07 20:28:35 +0000353 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000354 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000355 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
356 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
357 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
358 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
359 return bitmap;
360}
361
Lei Zhang30ff2532019-01-31 21:37:55 +0000362// static
363ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
364 return RenderPageWithFlags(page, nullptr, 0);
365}
366
Lei Zhangd69e0652019-04-13 00:39:35 +0000367#if defined(OS_WIN)
368// static
369std::vector<uint8_t> EmbedderTest::RenderPageWithFlagsToEmf(FPDF_PAGE page,
370 int flags) {
371 HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
372
Lei Zhangdfa075b2019-12-05 21:52:43 +0000373 int width = static_cast<int>(FPDF_GetPageWidthF(page));
374 int height = static_cast<int>(FPDF_GetPageHeightF(page));
Lei Zhangd69e0652019-04-13 00:39:35 +0000375 HRGN rgn = CreateRectRgn(0, 0, width, height);
376 SelectClipRgn(dc, rgn);
377 DeleteObject(rgn);
378
379 SelectObject(dc, GetStockObject(NULL_PEN));
380 SelectObject(dc, GetStockObject(WHITE_BRUSH));
381 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
382 Rectangle(dc, 0, 0, width + 1, height + 1);
383
384 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, flags);
385
386 HENHMETAFILE emf = CloseEnhMetaFile(dc);
387 size_t size_in_bytes = GetEnhMetaFileBits(emf, 0, nullptr);
388 std::vector<uint8_t> buffer(size_in_bytes);
389 GetEnhMetaFileBits(emf, size_in_bytes, buffer.data());
390 DeleteEnhMetaFile(emf);
391 return buffer;
392}
Lei Zhang614f1a02019-05-30 00:03:53 +0000393
394// static
395std::string EmbedderTest::GetPostScriptFromEmf(
Tom Sepez87b9e3a2019-11-20 23:45:43 +0000396 pdfium::span<const uint8_t> emf_data) {
Lei Zhang614f1a02019-05-30 00:03:53 +0000397 // This comes from Emf::InitFromData() in Chromium.
398 HENHMETAFILE emf = SetEnhMetaFileBits(emf_data.size(), emf_data.data());
399 if (!emf)
400 return std::string();
401
402 // This comes from Emf::Enumerator::Enumerator() in Chromium.
403 std::vector<const ENHMETARECORD*> records;
404 if (!EnumEnhMetaFile(nullptr, emf, &GetRecordProc, &records, nullptr)) {
405 DeleteEnhMetaFile(emf);
406 return std::string();
407 }
408
409 // This comes from PostScriptMetaFile::SafePlayback() in Chromium.
410 std::string ps_data;
411 for (const auto* record : records) {
412 if (record->iType != EMR_GDICOMMENT)
413 continue;
414
415 // PostScript data is encapsulated inside EMF comment records.
416 // The first two bytes of the comment indicate the string length. The rest
417 // is the actual string data.
418 const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
419 const char* data = reinterpret_cast<const char*>(comment->Data);
420 uint16_t size = *reinterpret_cast<const uint16_t*>(data);
421 data += 2;
422 ps_data.append(data, size);
423 }
424 DeleteEnhMetaFile(emf);
425 return ps_data;
426}
Lei Zhangd69e0652019-04-13 00:39:35 +0000427#endif // defined(OS_WIN)
428
Lei Zhang0b494052019-01-31 21:41:15 +0000429FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
430 return OpenSavedDocumentWithPassword(nullptr);
431}
432
Tom Sepez6dc12402020-01-07 21:02:17 +0000433// static
434int EmbedderTest::BytesPerPixelForFormat(int format) {
435 switch (format) {
436 case FPDFBitmap_Gray:
437 return 1;
438 case FPDFBitmap_BGR:
439 return 3;
440 case FPDFBitmap_BGRx:
441 case FPDFBitmap_BGRA:
442 return 4;
443 default:
444 NOTREACHED();
445 return 0;
446 }
447}
448
Lei Zhang0b494052019-01-31 21:41:15 +0000449FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
450 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300451 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000452 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300453 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000454 // Copy data to prevent clearing it before saved document close.
455 saved_document_file_data_ = data_string_;
456 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400457
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300458 saved_fake_file_access_ =
Tom Sepez7df04832020-05-18 22:09:31 +0000459 std::make_unique<FakeFileAccess>(&saved_file_access_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300460
Tom Sepez0784c732018-04-23 18:02:57 +0000461 EXPECT_TRUE(OpenDocumentHelper(
462 password, LinearizeOption::kDefaultLinearize,
463 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
464 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000465 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400466}
467
468void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000469 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400470
Lei Zhang0729be22018-02-05 21:13:51 +0000471 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
472 FPDF_CloseDocument(saved_document_);
473 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400474
Lei Zhang0729be22018-02-05 21:13:51 +0000475 saved_form_handle_ = nullptr;
476 saved_document_ = nullptr;
477 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400478}
479
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000480FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000481 ASSERT(saved_form_handle_);
482 ASSERT(page_number >= 0);
Lei Zhangf245ae62020-05-15 21:49:46 +0000483 ASSERT(!pdfium::Contains(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400484
Lei Zhang0729be22018-02-05 21:13:51 +0000485 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000486 if (!page)
487 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400488
Lei Zhang9f72c452018-02-08 21:49:54 +0000489 FORM_OnAfterLoadPage(page, saved_form_handle_);
490 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
491 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000492 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400493}
494
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000495void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000496 ASSERT(saved_form_handle_);
497
498 int page_number = GetPageNumberForSavedPage(page);
499 if (page_number < 0) {
500 NOTREACHED();
501 return;
502 }
503
504 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
505 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000506 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000507
508 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400509}
510
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000511void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
512 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400513 int height,
514 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000515 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000516 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400517
Tom Sepeze08d2b12018-04-25 18:49:32 +0000518 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000519 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400520}
521
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400522void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000523 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000524 FPDF_PAGE page = LoadSavedPage(0);
525 VerifySavedRendering(page, width, height, md5);
526 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400527 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400528}
529
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300530void EmbedderTest::SetWholeFileAvailable() {
531 ASSERT(fake_file_access_);
532 fake_file_access_->SetWholeFileAvailable();
533}
534
weili0dadcc62016-08-23 21:10:57 -0700535FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700536 FPDF_DOCUMENT document,
537 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700538 EmbedderTest* test = static_cast<EmbedderTest*>(info);
539 auto it = test->page_map_.find(page_index);
540 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700541}
542
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800543// static
544void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
545 int type) {
546 EmbedderTest* test = static_cast<EmbedderTest*>(info);
547 test->delegate_->UnsupportedHandler(type);
548}
549
550// static
551int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
552 FPDF_WIDESTRING message,
553 FPDF_WIDESTRING title,
554 int type,
555 int icon) {
556 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
557 return test->delegate_->Alert(message, title, type, icon);
558}
559
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700560// static
561int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562 int msecs,
563 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700564 EmbedderTest* test = static_cast<EmbedderTest*>(info);
565 return test->delegate_->SetTimer(msecs, fn);
566}
567
568// static
569void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
570 EmbedderTest* test = static_cast<EmbedderTest*>(info);
571 return test->delegate_->KillTimer(id);
572}
573
Tom Sepez396e8722015-09-09 10:16:08 -0700574// static
575FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
576 FPDF_DOCUMENT document,
577 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700578 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
579 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700580}
581
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000582// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000583void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
584 FPDF_BYTESTRING uri) {
585 EmbedderTest* test = static_cast<EmbedderTest*>(info);
586 return test->delegate_->DoURIAction(uri);
587}
588
589// static
Badhri Ravikumarf5cc1ac2020-04-30 19:00:55 +0000590void EmbedderTest::DoGoToActionTrampoline(FPDF_FORMFILLINFO* info,
591 int page_index,
592 int zoom_mode,
593 float* pos_array,
594 int array_size) {
595 EmbedderTest* test = static_cast<EmbedderTest*>(info);
596 return test->delegate_->DoGoToAction(info, page_index, zoom_mode, pos_array,
597 array_size);
598}
599
600// static
Aayush Dhirc63913e2020-02-12 09:45:34 +0000601void EmbedderTest::OnFocusChangeTrampoline(FPDF_FORMFILLINFO* info,
602 FPDF_ANNOTATION annot,
603 int page_index) {
604 EmbedderTest* test = static_cast<EmbedderTest*>(info);
605 return test->delegate_->OnFocusChange(info, annot, page_index);
606}
607
608// static
Badhri Ravikumare86bbfa2020-04-16 21:42:40 +0000609void EmbedderTest::DoURIActionWithKeyboardModifierTrampoline(
610 FPDF_FORMFILLINFO* info,
611 FPDF_BYTESTRING uri,
612 int modifiers) {
613 EmbedderTest* test = static_cast<EmbedderTest*>(info);
614 return test->delegate_->DoURIActionWithKeyboardModifier(info, uri, modifiers);
615}
616
617// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000618std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Lei Zhangbf980142019-12-20 01:05:42 +0000619 int stride = FPDFBitmap_GetStride(bitmap);
620 int usable_bytes_per_row =
621 GetBitmapBytesPerPixel(bitmap) * FPDFBitmap_GetWidth(bitmap);
622 int height = FPDFBitmap_GetHeight(bitmap);
623 auto span = pdfium::make_span(
624 static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)), stride * height);
625
626 CRYPT_md5_context context = CRYPT_MD5Start();
627 for (int i = 0; i < height; ++i)
628 CRYPT_MD5Update(&context, span.subspan(i * stride, usable_bytes_per_row));
Dan Sinclair957480c2017-06-13 15:21:14 -0400629 uint8_t digest[16];
Lei Zhangbf980142019-12-20 01:05:42 +0000630 CRYPT_MD5Finish(&context, digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400631 return CryptToBase16(digest);
632}
633
Henrique Nakashimadb269572018-01-16 19:02:15 +0000634#ifndef NDEBUG
635// static
636void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
637 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000638 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000639}
640#endif
641
thestigbcd3e532016-11-21 13:37:28 -0800642// static
643void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
644 int expected_width,
645 int expected_height,
646 const char* expected_md5sum) {
647 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
648 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400649
650 // The expected stride is calculated using the same formula as in
651 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
652 const int expected_stride =
653 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800654 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
655
656 if (!expected_md5sum)
657 return;
658
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000659 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800660}
661
Nicolas Pena3ff54002017-07-05 11:55:35 -0400662// static
663int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
664 const void* data,
665 unsigned long size) {
666 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000667
Lei Zhang0729be22018-02-05 21:13:51 +0000668 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000669
670 if (pThis->filestream_.is_open())
671 pThis->filestream_.write(static_cast<const char*>(data), size);
672
Nicolas Pena3ff54002017-07-05 11:55:35 -0400673 return 1;
674}
675
676// static
677int EmbedderTest::GetBlockFromString(void* param,
678 unsigned long pos,
679 unsigned char* buf,
680 unsigned long size) {
681 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000682 if (!new_file || pos + size < pos) {
683 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400684 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000685 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400686
Lei Zhangcef91f12019-01-08 21:32:51 +0000687 if (pos + size > new_file->size()) {
688 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400689 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000690 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400691
692 memcpy(buf, new_file->data() + pos, size);
693 return 1;
694}
Lei Zhang75c81712018-02-08 17:22:39 +0000695
Lei Zhang9f72c452018-02-08 21:49:54 +0000696// static
697int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
698 FPDF_PAGE page) {
699 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000700 if (it.second == page) {
701 int page_number = it.first;
702 ASSERT(page_number >= 0);
703 return page_number;
704 }
705 }
706 return -1;
707}
Lei Zhang9f72c452018-02-08 21:49:54 +0000708
709int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
710 return GetPageNumberForPage(page_map_, page);
711}
712
713int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
714 return GetPageNumberForPage(saved_page_map_, page);
715}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000716
Lei Zhang81a799e2019-12-16 17:37:13 +0000717#ifndef NDEBUG
718void EmbedderTest::OpenPDFFileForWrite(const std::string& filename) {
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000719 filestream_.open(filename, std::ios_base::binary);
720}
721
722void EmbedderTest::ClosePDFFileForWrite() {
723 filestream_.close();
724}
Lei Zhang81a799e2019-12-16 17:37:13 +0000725#endif