blob: e4ac4ad2c7fd28c691b2256766c684718c8a39c7 [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
Henrique Nakashimadb269572018-01-16 19:02:15 +00009#include <fstream>
Tom Sepez96d13342015-01-16 14:59:26 -080010#include <list>
Lei Zhang9f72c452018-02-08 21:49:54 +000011#include <map>
Lei Zhanga98e3662018-02-07 20:28:35 +000012#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -080013#include <string>
14#include <utility>
15#include <vector>
16
thestigbcd3e532016-11-21 13:37:28 -080017#include "core/fdrm/crypto/fx_crypt.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000018#include "public/cpp/fpdf_scopers.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080019#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080020#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080021#include "public/fpdf_text.h"
22#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080023#include "testing/gmock/include/gmock/gmock.h"
Henrique Nakashimadb269572018-01-16 19:02:15 +000024#include "testing/image_diff/image_diff_png.h"
Wei Li091f7a02015-11-09 12:09:55 -080025#include "testing/test_support.h"
26#include "testing/utils/path_service.h"
Lei Zhang75c81712018-02-08 17:22:39 +000027#include "third_party/base/logging.h"
Artem Strygin0e60b9e2017-09-28 18:46:03 +030028#include "third_party/base/ptr_util.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) {
39 const int format = FPDFBitmap_GetFormat(bitmap);
40 switch (format) {
41 case FPDFBitmap_Gray:
42 return 1;
43 case FPDFBitmap_BGR:
44 return 3;
45 case FPDFBitmap_BGRx:
46 case FPDFBitmap_BGRA:
47 return 4;
48 default:
49 ASSERT(false);
50 return 0;
51 }
52}
53
thestigbcd3e532016-11-21 13:37:28 -080054} // namespace
55
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056EmbedderTest::EmbedderTest()
Lei Zhang0729be22018-02-05 21:13:51 +000057 : default_delegate_(pdfium::MakeUnique<EmbedderTest::Delegate>()),
58 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
Dan Sinclair5553d8b2018-01-03 09:44:28 -050063EmbedderTest::~EmbedderTest() {}
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
Tom Sepezda8189e2015-01-30 14:41:50 -080088 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -070089 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -080090 FPDFDOC_ExitFormFillEnvironment(form_handle_);
91 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -080092 }
Tom Sepezc46d0002015-11-30 15:46:36 -080093
Tom Sepez96d13342015-01-16 14:59:26 -080094 FPDFAvail_Destroy(avail_);
95 FPDF_DestroyLibrary();
Lei Zhangd27acae2015-05-15 15:36:02 -070096 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -080097}
98
Tom Sepezd483eb42016-01-06 10:03:59 -080099bool EmbedderTest::CreateEmptyDocument() {
100 document_ = FPDF_CreateNewDocument();
101 if (!document_)
102 return false;
103
Tom Sepez0784c732018-04-23 18:02:57 +0000104 form_handle_ =
105 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -0800106 return true;
107}
108
Lei Zhang208eecf2017-12-20 19:40:50 +0000109bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000110 return OpenDocumentWithOptions(filename, nullptr,
111 LinearizeOption::kDefaultLinearize,
112 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000113}
114
115bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000116 return OpenDocumentWithOptions(filename, nullptr,
117 LinearizeOption::kMustLinearize,
118 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000119}
120
121bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
122 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000123 return OpenDocumentWithOptions(filename, password,
124 LinearizeOption::kDefaultLinearize,
125 JavaScriptOption::kEnableJavaScript);
126}
127
128bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
129 return OpenDocumentWithOptions(filename, nullptr,
130 LinearizeOption::kDefaultLinearize,
131 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000132}
133
134bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
135 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000136 LinearizeOption linearize_option,
137 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800138 std::string file_path;
139 if (!PathService::GetTestFilePath(filename, &file_path))
140 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000141
Wei Li091f7a02015-11-09 12:09:55 -0800142 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400143 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800144 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800145
thestig29ce9232016-06-22 07:03:23 -0700146 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800147 loader_ = new TestLoader(file_contents_.get(), file_length_);
Lei Zhang0729be22018-02-05 21:13:51 +0000148
149 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800150 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700151 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800152 file_access_.m_Param = loader_;
Lei Zhang0729be22018-02-05 21:13:51 +0000153
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300154 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000155 return OpenDocumentHelper(password, linearize_option, javascript_option,
156 fake_file_access_.get(), &document_, &avail_,
157 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400158}
Tom Sepez96d13342015-01-16 14:59:26 -0800159
Nicolas Pena56fc9722017-07-13 16:31:34 -0400160bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000161 LinearizeOption linearize_option,
162 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300163 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400164 FPDF_DOCUMENT* document,
165 FPDF_AVAIL* avail,
166 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300167 network_simulator->AddSegment(0, 1024);
168 network_simulator->SetRequestedDataAvailable();
169 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
170 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400171 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300172 int32_t nRet = PDF_DATA_NOTAVAIL;
173 while (nRet == PDF_DATA_NOTAVAIL) {
174 network_simulator->SetRequestedDataAvailable();
175 nRet =
176 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
177 }
178 if (nRet == PDF_DATA_ERROR)
179 return false;
180
Nicolas Pena56fc9722017-07-13 16:31:34 -0400181 *document = FPDFAvail_GetDocument(*avail, password);
182 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800183 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400184
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300185 nRet = PDF_DATA_NOTAVAIL;
186 while (nRet == PDF_DATA_NOTAVAIL) {
187 network_simulator->SetRequestedDataAvailable();
188 nRet =
189 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
190 }
191 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800192 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400193
194 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800195 for (int i = 0; i < page_count; ++i) {
196 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300197 while (nRet == PDF_DATA_NOTAVAIL) {
198 network_simulator->SetRequestedDataAvailable();
199 nRet = FPDFAvail_IsPageAvail(*avail, i,
200 network_simulator->GetDownloadHints());
201 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400202
203 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800204 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800205 }
206 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000207 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800208 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300209 network_simulator->SetWholeFileAvailable();
210 *document =
211 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400212 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800213 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800214 }
Tom Sepez0784c732018-04-23 18:02:57 +0000215 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
216
Tom Sepezc46d0002015-11-30 15:46:36 -0800217#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400218 int doc_type = FPDF_GetFormType(*document);
219 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
220 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800221#endif // PDF_ENABLE_XFA
Tom Sepez0784c732018-04-23 18:02:57 +0000222
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000223 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800224 return true;
225}
Tom Sepez96d13342015-01-16 14:59:26 -0800226
Tom Sepez0784c732018-04-23 18:02:57 +0000227FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
228 FPDF_DOCUMENT doc,
229 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800230 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400231 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200232 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800233 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400234 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800235
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800236 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
237 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800238#ifdef PDF_ENABLE_XFA
239 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800240#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800241 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800242#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700243 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
244 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700245 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez0784c732018-04-23 18:02:57 +0000246 if (javascript_option == JavaScriptOption::kEnableJavaScript)
247 formfillinfo->m_pJsPlatform = platform;
248
Nicolas Pena3ff54002017-07-05 11:55:35 -0400249 FPDF_FORMHANDLE form_handle =
250 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000251 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
252 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400253 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
254 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800255}
256
Tom Sepezda8189e2015-01-30 14:41:50 -0800257void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400258 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800259 FORM_DoDocumentJSAction(form_handle_);
260 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800261}
262
263int EmbedderTest::GetFirstPageNum() {
264 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000265 (void)FPDFAvail_IsPageAvail(avail_, first_page,
266 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800267 return first_page;
268}
269
270int EmbedderTest::GetPageCount() {
271 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400272 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000273 (void)FPDFAvail_IsPageAvail(avail_, i,
274 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800275 return page_count;
276}
277
Tom Sepezda8189e2015-01-30 14:41:50 -0800278FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400279 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000280 ASSERT(page_number >= 0);
281 ASSERT(!pdfium::ContainsKey(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 Sepezda8189e2015-01-30 14:41:50 -0800287 FORM_OnAfterLoadPage(page, form_handle_);
288 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
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) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400294 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000295
296 int page_number = GetPageNumberForLoadedPage(page);
297 if (page_number < 0) {
298 NOTREACHED();
299 return;
300 }
301
Tom Sepezda8189e2015-01-30 14:41:50 -0800302 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
303 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800304 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700305
Lei Zhang75c81712018-02-08 17:22:39 +0000306 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800307}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800308
Tom Sepeze08d2b12018-04-25 18:49:32 +0000309ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000310 return RenderLoadedPageWithFlags(page, 0);
311}
312
Tom Sepeze08d2b12018-04-25 18:49:32 +0000313ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
314 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000315 if (GetPageNumberForLoadedPage(page) < 0) {
316 NOTREACHED();
317 return nullptr;
318 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000319 return RenderPageWithFlags(page, form_handle_, flags);
320}
321
Tom Sepeze08d2b12018-04-25 18:49:32 +0000322ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000323 return RenderSavedPageWithFlags(page, 0);
324}
325
Tom Sepeze08d2b12018-04-25 18:49:32 +0000326ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
327 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000328 if (GetPageNumberForSavedPage(page) < 0) {
329 NOTREACHED();
330 return nullptr;
331 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000332 return RenderPageWithFlags(page, saved_form_handle_, flags);
333}
334
335// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000336ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
337 FPDF_FORMHANDLE handle,
338 int flags) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000339 int width = static_cast<int>(FPDF_GetPageWidth(page));
340 int height = static_cast<int>(FPDF_GetPageHeight(page));
341 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000342 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000343 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
344 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
345 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
346 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
347 return bitmap;
348}
349
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400350FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300351 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000352 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300353 saved_file_access_.m_GetBlock = GetBlockFromString;
Henrique Nakashimadd1083b2018-07-06 17:28:46 +0000354 saved_file_access_.m_Param = &data_string_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400355
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300356 saved_fake_file_access_ =
357 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
358
Tom Sepez0784c732018-04-23 18:02:57 +0000359 EXPECT_TRUE(OpenDocumentHelper(
360 password, LinearizeOption::kDefaultLinearize,
361 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
362 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000363 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400364}
365
366void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000367 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400368
Lei Zhang0729be22018-02-05 21:13:51 +0000369 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
370 FPDF_CloseDocument(saved_document_);
371 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400372
Lei Zhang0729be22018-02-05 21:13:51 +0000373 saved_form_handle_ = nullptr;
374 saved_document_ = nullptr;
375 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400376}
377
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000378FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000379 ASSERT(saved_form_handle_);
380 ASSERT(page_number >= 0);
381 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400382
Lei Zhang0729be22018-02-05 21:13:51 +0000383 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000384 if (!page)
385 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400386
Lei Zhang9f72c452018-02-08 21:49:54 +0000387 FORM_OnAfterLoadPage(page, saved_form_handle_);
388 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
389 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000390 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400391}
392
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000393void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000394 ASSERT(saved_form_handle_);
395
396 int page_number = GetPageNumberForSavedPage(page);
397 if (page_number < 0) {
398 NOTREACHED();
399 return;
400 }
401
402 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
403 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000404 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000405
406 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400407}
408
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000409void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
410 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400411 int height,
412 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000413 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000414 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400415
Tom Sepeze08d2b12018-04-25 18:49:32 +0000416 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000417 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400418}
419
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400420void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
421 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000422 FPDF_PAGE page = LoadSavedPage(0);
423 VerifySavedRendering(page, width, height, md5);
424 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400425 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400426}
427
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300428void EmbedderTest::SetWholeFileAvailable() {
429 ASSERT(fake_file_access_);
430 fake_file_access_->SetWholeFileAvailable();
431}
432
weili0dadcc62016-08-23 21:10:57 -0700433FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700434 FPDF_DOCUMENT document,
435 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700436 EmbedderTest* test = static_cast<EmbedderTest*>(info);
437 auto it = test->page_map_.find(page_index);
438 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700439}
440
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800441// static
442void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
443 int type) {
444 EmbedderTest* test = static_cast<EmbedderTest*>(info);
445 test->delegate_->UnsupportedHandler(type);
446}
447
448// static
449int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
450 FPDF_WIDESTRING message,
451 FPDF_WIDESTRING title,
452 int type,
453 int icon) {
454 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
455 return test->delegate_->Alert(message, title, type, icon);
456}
457
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700458// static
459int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 int msecs,
461 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700462 EmbedderTest* test = static_cast<EmbedderTest*>(info);
463 return test->delegate_->SetTimer(msecs, fn);
464}
465
466// static
467void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
468 EmbedderTest* test = static_cast<EmbedderTest*>(info);
469 return test->delegate_->KillTimer(id);
470}
471
Tom Sepez396e8722015-09-09 10:16:08 -0700472// static
473FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
474 FPDF_DOCUMENT document,
475 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700476 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
477 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700478}
479
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000480// static
481std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400482 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000483 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
484 FPDFBitmap_GetWidth(bitmap) *
485 GetBitmapBytesPerPixel(bitmap) *
486 FPDFBitmap_GetHeight(bitmap),
487 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400488 return CryptToBase16(digest);
489}
490
Henrique Nakashimadb269572018-01-16 19:02:15 +0000491#ifndef NDEBUG
492// static
493void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
494 const std::string& filename) {
495 const int stride = FPDFBitmap_GetStride(bitmap);
496 const int width = FPDFBitmap_GetWidth(bitmap);
497 const int height = FPDFBitmap_GetHeight(bitmap);
498 const auto* buffer =
499 static_cast<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap));
500
501 std::vector<unsigned char> png_encoding;
Lei Zhang9a7c2392018-03-02 23:54:26 +0000502 bool encoded;
503 if (FPDFBitmap_GetFormat(bitmap) == FPDFBitmap_Gray) {
504 encoded = image_diff_png::EncodeGrayPNG(buffer, width, height, stride,
505 &png_encoding);
506 } else {
507 encoded = image_diff_png::EncodeBGRAPNG(buffer, width, height, stride,
508 /*discard_transparency=*/false,
509 &png_encoding);
510 }
Henrique Nakashimadb269572018-01-16 19:02:15 +0000511
512 ASSERT_TRUE(encoded);
513 ASSERT_LT(filename.size(), 256u);
514
515 std::ofstream png_file;
Henrique Nakashima6d6a2432018-01-17 16:52:46 +0000516 png_file.open(filename, std::ios_base::out | std::ios_base::binary);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000517 png_file.write(reinterpret_cast<char*>(&png_encoding.front()),
518 png_encoding.size());
519 ASSERT_TRUE(png_file.good());
520 png_file.close();
521}
522#endif
523
thestigbcd3e532016-11-21 13:37:28 -0800524// static
525void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
526 int expected_width,
527 int expected_height,
528 const char* expected_md5sum) {
529 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
530 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400531
532 // The expected stride is calculated using the same formula as in
533 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
534 const int expected_stride =
535 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800536 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
537
538 if (!expected_md5sum)
539 return;
540
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000541 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800542}
543
Nicolas Pena3ff54002017-07-05 11:55:35 -0400544// static
545int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
546 const void* data,
547 unsigned long size) {
548 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000549
Lei Zhang0729be22018-02-05 21:13:51 +0000550 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000551
552 if (pThis->filestream_.is_open())
553 pThis->filestream_.write(static_cast<const char*>(data), size);
554
Nicolas Pena3ff54002017-07-05 11:55:35 -0400555 return 1;
556}
557
558// static
559int EmbedderTest::GetBlockFromString(void* param,
560 unsigned long pos,
561 unsigned char* buf,
562 unsigned long size) {
563 std::string* new_file = static_cast<std::string*>(param);
564 if (!new_file || pos + size < pos)
565 return 0;
566
567 unsigned long file_size = new_file->size();
568 if (pos + size > file_size)
569 return 0;
570
571 memcpy(buf, new_file->data() + pos, size);
572 return 1;
573}
Lei Zhang75c81712018-02-08 17:22:39 +0000574
Lei Zhang9f72c452018-02-08 21:49:54 +0000575// static
576int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
577 FPDF_PAGE page) {
578 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000579 if (it.second == page) {
580 int page_number = it.first;
581 ASSERT(page_number >= 0);
582 return page_number;
583 }
584 }
585 return -1;
586}
Lei Zhang9f72c452018-02-08 21:49:54 +0000587
588int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
589 return GetPageNumberForPage(page_map_, page);
590}
591
592int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
593 return GetPageNumberForPage(saved_page_map_, page);
594}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000595
596void EmbedderTest::OpenPDFFileForWrite(const char* filename) {
597 filestream_.open(filename, std::ios_base::binary);
598}
599
600void EmbedderTest::ClosePDFFileForWrite() {
601 filestream_.close();
602}