blob: 3f953d22df7633547fd376aed142978156663ff2 [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;
Artem Strygin68d04f22018-07-12 09:18:19 +0000354 // Copy data to prevent clearing it before saved document close.
355 saved_document_file_data_ = data_string_;
356 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400357
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300358 saved_fake_file_access_ =
359 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
360
Tom Sepez0784c732018-04-23 18:02:57 +0000361 EXPECT_TRUE(OpenDocumentHelper(
362 password, LinearizeOption::kDefaultLinearize,
363 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
364 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000365 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400366}
367
368void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000369 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400370
Lei Zhang0729be22018-02-05 21:13:51 +0000371 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
372 FPDF_CloseDocument(saved_document_);
373 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400374
Lei Zhang0729be22018-02-05 21:13:51 +0000375 saved_form_handle_ = nullptr;
376 saved_document_ = nullptr;
377 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400378}
379
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000380FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000381 ASSERT(saved_form_handle_);
382 ASSERT(page_number >= 0);
383 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400384
Lei Zhang0729be22018-02-05 21:13:51 +0000385 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000386 if (!page)
387 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400388
Lei Zhang9f72c452018-02-08 21:49:54 +0000389 FORM_OnAfterLoadPage(page, saved_form_handle_);
390 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
391 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000392 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400393}
394
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000395void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000396 ASSERT(saved_form_handle_);
397
398 int page_number = GetPageNumberForSavedPage(page);
399 if (page_number < 0) {
400 NOTREACHED();
401 return;
402 }
403
404 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
405 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000406 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000407
408 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400409}
410
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000411void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
412 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400413 int height,
414 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000415 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000416 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400417
Tom Sepeze08d2b12018-04-25 18:49:32 +0000418 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000419 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400420}
421
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400422void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Tom Sepezb9c3e272018-08-14 18:22:06 +0000423 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000424 FPDF_PAGE page = LoadSavedPage(0);
425 VerifySavedRendering(page, width, height, md5);
426 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400427 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400428}
429
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300430void EmbedderTest::SetWholeFileAvailable() {
431 ASSERT(fake_file_access_);
432 fake_file_access_->SetWholeFileAvailable();
433}
434
weili0dadcc62016-08-23 21:10:57 -0700435FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700436 FPDF_DOCUMENT document,
437 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700438 EmbedderTest* test = static_cast<EmbedderTest*>(info);
439 auto it = test->page_map_.find(page_index);
440 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700441}
442
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800443// static
444void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
445 int type) {
446 EmbedderTest* test = static_cast<EmbedderTest*>(info);
447 test->delegate_->UnsupportedHandler(type);
448}
449
450// static
451int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
452 FPDF_WIDESTRING message,
453 FPDF_WIDESTRING title,
454 int type,
455 int icon) {
456 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
457 return test->delegate_->Alert(message, title, type, icon);
458}
459
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700460// static
461int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462 int msecs,
463 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700464 EmbedderTest* test = static_cast<EmbedderTest*>(info);
465 return test->delegate_->SetTimer(msecs, fn);
466}
467
468// static
469void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
470 EmbedderTest* test = static_cast<EmbedderTest*>(info);
471 return test->delegate_->KillTimer(id);
472}
473
Tom Sepez396e8722015-09-09 10:16:08 -0700474// static
475FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
476 FPDF_DOCUMENT document,
477 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700478 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
479 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700480}
481
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000482// static
483std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400484 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000485 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
486 FPDFBitmap_GetWidth(bitmap) *
487 GetBitmapBytesPerPixel(bitmap) *
488 FPDFBitmap_GetHeight(bitmap),
489 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400490 return CryptToBase16(digest);
491}
492
Henrique Nakashimadb269572018-01-16 19:02:15 +0000493#ifndef NDEBUG
494// static
495void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
496 const std::string& filename) {
497 const int stride = FPDFBitmap_GetStride(bitmap);
498 const int width = FPDFBitmap_GetWidth(bitmap);
499 const int height = FPDFBitmap_GetHeight(bitmap);
500 const auto* buffer =
501 static_cast<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap));
502
503 std::vector<unsigned char> png_encoding;
Lei Zhang9a7c2392018-03-02 23:54:26 +0000504 bool encoded;
505 if (FPDFBitmap_GetFormat(bitmap) == FPDFBitmap_Gray) {
506 encoded = image_diff_png::EncodeGrayPNG(buffer, width, height, stride,
507 &png_encoding);
508 } else {
509 encoded = image_diff_png::EncodeBGRAPNG(buffer, width, height, stride,
510 /*discard_transparency=*/false,
511 &png_encoding);
512 }
Henrique Nakashimadb269572018-01-16 19:02:15 +0000513
514 ASSERT_TRUE(encoded);
515 ASSERT_LT(filename.size(), 256u);
516
517 std::ofstream png_file;
Henrique Nakashima6d6a2432018-01-17 16:52:46 +0000518 png_file.open(filename, std::ios_base::out | std::ios_base::binary);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000519 png_file.write(reinterpret_cast<char*>(&png_encoding.front()),
520 png_encoding.size());
521 ASSERT_TRUE(png_file.good());
522 png_file.close();
523}
524#endif
525
thestigbcd3e532016-11-21 13:37:28 -0800526// static
527void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
528 int expected_width,
529 int expected_height,
530 const char* expected_md5sum) {
531 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
532 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400533
534 // The expected stride is calculated using the same formula as in
535 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
536 const int expected_stride =
537 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800538 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
539
540 if (!expected_md5sum)
541 return;
542
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000543 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800544}
545
Nicolas Pena3ff54002017-07-05 11:55:35 -0400546// static
547int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
548 const void* data,
549 unsigned long size) {
550 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000551
Lei Zhang0729be22018-02-05 21:13:51 +0000552 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000553
554 if (pThis->filestream_.is_open())
555 pThis->filestream_.write(static_cast<const char*>(data), size);
556
Nicolas Pena3ff54002017-07-05 11:55:35 -0400557 return 1;
558}
559
560// static
561int EmbedderTest::GetBlockFromString(void* param,
562 unsigned long pos,
563 unsigned char* buf,
564 unsigned long size) {
565 std::string* new_file = static_cast<std::string*>(param);
566 if (!new_file || pos + size < pos)
567 return 0;
568
569 unsigned long file_size = new_file->size();
570 if (pos + size > file_size)
571 return 0;
572
573 memcpy(buf, new_file->data() + pos, size);
574 return 1;
575}
Lei Zhang75c81712018-02-08 17:22:39 +0000576
Lei Zhang9f72c452018-02-08 21:49:54 +0000577// static
578int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
579 FPDF_PAGE page) {
580 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000581 if (it.second == page) {
582 int page_number = it.first;
583 ASSERT(page_number >= 0);
584 return page_number;
585 }
586 }
587 return -1;
588}
Lei Zhang9f72c452018-02-08 21:49:54 +0000589
590int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
591 return GetPageNumberForPage(page_map_, page);
592}
593
594int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
595 return GetPageNumberForPage(saved_page_map_, page);
596}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000597
598void EmbedderTest::OpenPDFFileForWrite(const char* filename) {
599 filestream_.open(filename, std::ios_base::binary);
600}
601
602void EmbedderTest::ClosePDFFileForWrite() {
603 filestream_.close();
604}