blob: fd53b52fdc21b7e5674eb3c996aa164b0c3cb6e7 [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"
Lei Zhangb4e7f302015-11-06 15:52:32 -080018#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080019#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080020#include "public/fpdf_text.h"
21#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080022#include "testing/gmock/include/gmock/gmock.h"
Henrique Nakashimadb269572018-01-16 19:02:15 +000023#include "testing/image_diff/image_diff_png.h"
Wei Li091f7a02015-11-09 12:09:55 -080024#include "testing/test_support.h"
25#include "testing/utils/path_service.h"
Lei Zhang75c81712018-02-08 17:22:39 +000026#include "third_party/base/logging.h"
Artem Strygin0e60b9e2017-09-28 18:46:03 +030027#include "third_party/base/ptr_util.h"
Lei Zhang75c81712018-02-08 17:22:39 +000028#include "third_party/base/stl_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070029
30#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080031#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050032#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070033#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080034
Tom Sepez96d13342015-01-16 14:59:26 -080035namespace {
thestigc08cd7a2016-06-27 09:47:59 -070036
Jane Liu28fb7ba2017-08-02 21:45:57 -040037int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
38 const int format = FPDFBitmap_GetFormat(bitmap);
39 switch (format) {
40 case FPDFBitmap_Gray:
41 return 1;
42 case FPDFBitmap_BGR:
43 return 3;
44 case FPDFBitmap_BGRx:
45 case FPDFBitmap_BGRA:
46 return 4;
47 default:
48 ASSERT(false);
49 return 0;
50 }
51}
52
thestigbcd3e532016-11-21 13:37:28 -080053} // namespace
54
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055EmbedderTest::EmbedderTest()
Lei Zhang0729be22018-02-05 21:13:51 +000056 : default_delegate_(pdfium::MakeUnique<EmbedderTest::Delegate>()),
57 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -040058 FPDF_FILEWRITE::version = 1;
59 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080060}
Tom Sepez96d13342015-01-16 14:59:26 -080061
Dan Sinclair5553d8b2018-01-03 09:44:28 -050062EmbedderTest::~EmbedderTest() {}
Tom Sepezf288bb12015-11-20 12:12:46 -080063
64void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070065 FPDF_LIBRARY_CONFIG config;
66 config.version = 2;
67 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070068 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070069 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070070 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080071
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
73 memset(info, 0, sizeof(UNSUPPORT_INFO));
74 info->version = 1;
75 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
76 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +000077
Lei Zhang0729be22018-02-05 21:13:51 +000078 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079}
Tom Sepez96d13342015-01-16 14:59:26 -080080
81void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +000082 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
83 // possible. This can fail when an ASSERT test fails in a test case.
84 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +000085 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang75c81712018-02-08 17:22:39 +000086
Tom Sepezda8189e2015-01-30 14:41:50 -080087 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -070088 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -080089 FPDFDOC_ExitFormFillEnvironment(form_handle_);
90 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -080091 }
Tom Sepezc46d0002015-11-30 15:46:36 -080092
Tom Sepez96d13342015-01-16 14:59:26 -080093 FPDFAvail_Destroy(avail_);
94 FPDF_DestroyLibrary();
Lei Zhangd27acae2015-05-15 15:36:02 -070095 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -080096}
97
Tom Sepezd483eb42016-01-06 10:03:59 -080098bool EmbedderTest::CreateEmptyDocument() {
99 document_ = FPDF_CreateNewDocument();
100 if (!document_)
101 return false;
102
Tom Sepez0784c732018-04-23 18:02:57 +0000103 form_handle_ =
104 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -0800105 return true;
106}
107
Lei Zhang208eecf2017-12-20 19:40:50 +0000108bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000109 return OpenDocumentWithOptions(filename, nullptr,
110 LinearizeOption::kDefaultLinearize,
111 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000112}
113
114bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000115 return OpenDocumentWithOptions(filename, nullptr,
116 LinearizeOption::kMustLinearize,
117 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000118}
119
120bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
121 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000122 return OpenDocumentWithOptions(filename, password,
123 LinearizeOption::kDefaultLinearize,
124 JavaScriptOption::kEnableJavaScript);
125}
126
127bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
128 return OpenDocumentWithOptions(filename, nullptr,
129 LinearizeOption::kDefaultLinearize,
130 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000131}
132
133bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
134 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000135 LinearizeOption linearize_option,
136 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800137 std::string file_path;
138 if (!PathService::GetTestFilePath(filename, &file_path))
139 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000140
Wei Li091f7a02015-11-09 12:09:55 -0800141 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400142 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800143 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800144
thestig29ce9232016-06-22 07:03:23 -0700145 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800146 loader_ = new TestLoader(file_contents_.get(), file_length_);
Lei Zhang0729be22018-02-05 21:13:51 +0000147
148 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800149 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700150 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800151 file_access_.m_Param = loader_;
Lei Zhang0729be22018-02-05 21:13:51 +0000152
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300153 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000154 return OpenDocumentHelper(password, linearize_option, javascript_option,
155 fake_file_access_.get(), &document_, &avail_,
156 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400157}
Tom Sepez96d13342015-01-16 14:59:26 -0800158
Nicolas Pena56fc9722017-07-13 16:31:34 -0400159bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000160 LinearizeOption linearize_option,
161 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300162 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400163 FPDF_DOCUMENT* document,
164 FPDF_AVAIL* avail,
165 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300166 network_simulator->AddSegment(0, 1024);
167 network_simulator->SetRequestedDataAvailable();
168 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
169 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400170 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300171 int32_t nRet = PDF_DATA_NOTAVAIL;
172 while (nRet == PDF_DATA_NOTAVAIL) {
173 network_simulator->SetRequestedDataAvailable();
174 nRet =
175 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
176 }
177 if (nRet == PDF_DATA_ERROR)
178 return false;
179
Nicolas Pena56fc9722017-07-13 16:31:34 -0400180 *document = FPDFAvail_GetDocument(*avail, password);
181 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800182 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400183
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300184 nRet = PDF_DATA_NOTAVAIL;
185 while (nRet == PDF_DATA_NOTAVAIL) {
186 network_simulator->SetRequestedDataAvailable();
187 nRet =
188 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
189 }
190 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800191 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400192
193 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800194 for (int i = 0; i < page_count; ++i) {
195 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300196 while (nRet == PDF_DATA_NOTAVAIL) {
197 network_simulator->SetRequestedDataAvailable();
198 nRet = FPDFAvail_IsPageAvail(*avail, i,
199 network_simulator->GetDownloadHints());
200 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400201
202 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800203 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800204 }
205 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000206 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800207 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300208 network_simulator->SetWholeFileAvailable();
209 *document =
210 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400211 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800212 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800213 }
Tom Sepez0784c732018-04-23 18:02:57 +0000214 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
215
Tom Sepezc46d0002015-11-30 15:46:36 -0800216#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400217 int doc_type = FPDF_GetFormType(*document);
218 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
219 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800220#endif // PDF_ENABLE_XFA
Tom Sepez0784c732018-04-23 18:02:57 +0000221
Nicolas Pena56fc9722017-07-13 16:31:34 -0400222 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800223 return true;
224}
Tom Sepez96d13342015-01-16 14:59:26 -0800225
Tom Sepez0784c732018-04-23 18:02:57 +0000226FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
227 FPDF_DOCUMENT doc,
228 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800229 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400230 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200231 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800232 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400233 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800234
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800235 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
236 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800237#ifdef PDF_ENABLE_XFA
238 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800239#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800240 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800241#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700242 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
243 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700244 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez0784c732018-04-23 18:02:57 +0000245 if (javascript_option == JavaScriptOption::kEnableJavaScript)
246 formfillinfo->m_pJsPlatform = platform;
247
Nicolas Pena3ff54002017-07-05 11:55:35 -0400248 FPDF_FORMHANDLE form_handle =
249 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000250 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
251 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400252 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
253 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800254}
255
Tom Sepezda8189e2015-01-30 14:41:50 -0800256void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400257 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800258 FORM_DoDocumentJSAction(form_handle_);
259 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800260}
261
262int EmbedderTest::GetFirstPageNum() {
263 int first_page = FPDFAvail_GetFirstPageNum(document_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300264 (void)FPDFAvail_IsPageAvail(avail_, first_page,
265 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800266 return first_page;
267}
268
269int EmbedderTest::GetPageCount() {
270 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400271 for (int i = 0; i < page_count; ++i)
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300272 (void)FPDFAvail_IsPageAvail(avail_, i,
273 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800274 return page_count;
275}
276
Tom Sepezda8189e2015-01-30 14:41:50 -0800277FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400278 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000279 ASSERT(page_number >= 0);
280 ASSERT(!pdfium::ContainsKey(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700281
Tom Sepez96d13342015-01-16 14:59:26 -0800282 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400283 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800284 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400285
Tom Sepezda8189e2015-01-30 14:41:50 -0800286 FORM_OnAfterLoadPage(page, form_handle_);
287 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700288 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700289 return page;
290}
291
Tom Sepezda8189e2015-01-30 14:41:50 -0800292void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400293 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000294
295 int page_number = GetPageNumberForLoadedPage(page);
296 if (page_number < 0) {
297 NOTREACHED();
298 return;
299 }
300
Tom Sepezda8189e2015-01-30 14:41:50 -0800301 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
302 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800303 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700304
Lei Zhang75c81712018-02-08 17:22:39 +0000305 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800306}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800307
Lei Zhanga98e3662018-02-07 20:28:35 +0000308std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderLoadedPage(
309 FPDF_PAGE page) {
310 return RenderLoadedPageWithFlags(page, 0);
311}
312
313std::unique_ptr<void, FPDFBitmapDeleter>
314EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page, 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
322std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderSavedPage(
323 FPDF_PAGE page) {
324 return RenderSavedPageWithFlags(page, 0);
325}
326
327std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderSavedPageWithFlags(
328 FPDF_PAGE page,
329 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000330 if (GetPageNumberForSavedPage(page) < 0) {
331 NOTREACHED();
332 return nullptr;
333 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000334 return RenderPageWithFlags(page, saved_form_handle_, flags);
335}
336
337// static
Lei Zhanga98e3662018-02-07 20:28:35 +0000338std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderPageWithFlags(
339 FPDF_PAGE page,
340 FPDF_FORMHANDLE handle,
341 int flags) {
342 int width = static_cast<int>(FPDF_GetPageWidth(page));
343 int height = static_cast<int>(FPDF_GetPageHeight(page));
344 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
345 std::unique_ptr<void, FPDFBitmapDeleter> bitmap(
346 FPDFBitmap_Create(width, height, alpha));
347 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
348 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
349 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
350 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
351 return bitmap;
352}
353
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400354FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300355 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000356 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300357 saved_file_access_.m_GetBlock = GetBlockFromString;
Lei Zhang0729be22018-02-05 21:13:51 +0000358 saved_file_access_.m_Param = &data_string_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400359
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300360 saved_fake_file_access_ =
361 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
362
Tom Sepez0784c732018-04-23 18:02:57 +0000363 EXPECT_TRUE(OpenDocumentHelper(
364 password, LinearizeOption::kDefaultLinearize,
365 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
366 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000367 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400368}
369
370void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000371 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400372
Lei Zhang0729be22018-02-05 21:13:51 +0000373 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
374 FPDF_CloseDocument(saved_document_);
375 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400376
Lei Zhang0729be22018-02-05 21:13:51 +0000377 saved_form_handle_ = nullptr;
378 saved_document_ = nullptr;
379 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400380}
381
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000382FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000383 ASSERT(saved_form_handle_);
384 ASSERT(page_number >= 0);
385 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400386
Lei Zhang0729be22018-02-05 21:13:51 +0000387 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000388 if (!page)
389 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400390
Lei Zhang9f72c452018-02-08 21:49:54 +0000391 FORM_OnAfterLoadPage(page, saved_form_handle_);
392 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
393 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000394 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400395}
396
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000397void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000398 ASSERT(saved_form_handle_);
399
400 int page_number = GetPageNumberForSavedPage(page);
401 if (page_number < 0) {
402 NOTREACHED();
403 return;
404 }
405
406 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
407 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000408 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000409
410 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400411}
412
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000413void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
414 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400415 int height,
416 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000417 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000418 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400419
Lei Zhanga98e3662018-02-07 20:28:35 +0000420 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
421 RenderSavedPageWithFlags(page, FPDF_ANNOT);
422 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400423}
424
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400425void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
426 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000427 FPDF_PAGE page = LoadSavedPage(0);
428 VerifySavedRendering(page, width, height, md5);
429 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400430 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400431}
432
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300433void EmbedderTest::SetWholeFileAvailable() {
434 ASSERT(fake_file_access_);
435 fake_file_access_->SetWholeFileAvailable();
436}
437
weili0dadcc62016-08-23 21:10:57 -0700438FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700439 FPDF_DOCUMENT document,
440 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700441 EmbedderTest* test = static_cast<EmbedderTest*>(info);
442 auto it = test->page_map_.find(page_index);
443 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700444}
445
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800446// static
447void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
448 int type) {
449 EmbedderTest* test = static_cast<EmbedderTest*>(info);
450 test->delegate_->UnsupportedHandler(type);
451}
452
453// static
454int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
455 FPDF_WIDESTRING message,
456 FPDF_WIDESTRING title,
457 int type,
458 int icon) {
459 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
460 return test->delegate_->Alert(message, title, type, icon);
461}
462
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700463// static
464int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 int msecs,
466 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700467 EmbedderTest* test = static_cast<EmbedderTest*>(info);
468 return test->delegate_->SetTimer(msecs, fn);
469}
470
471// static
472void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
473 EmbedderTest* test = static_cast<EmbedderTest*>(info);
474 return test->delegate_->KillTimer(id);
475}
476
Tom Sepez396e8722015-09-09 10:16:08 -0700477// static
478FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
479 FPDF_DOCUMENT document,
480 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700481 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
482 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700483}
484
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000485// static
486std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400487 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000488 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
489 FPDFBitmap_GetWidth(bitmap) *
490 GetBitmapBytesPerPixel(bitmap) *
491 FPDFBitmap_GetHeight(bitmap),
492 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400493 return CryptToBase16(digest);
494}
495
Henrique Nakashimadb269572018-01-16 19:02:15 +0000496#ifndef NDEBUG
497// static
498void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
499 const std::string& filename) {
500 const int stride = FPDFBitmap_GetStride(bitmap);
501 const int width = FPDFBitmap_GetWidth(bitmap);
502 const int height = FPDFBitmap_GetHeight(bitmap);
503 const auto* buffer =
504 static_cast<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap));
505
506 std::vector<unsigned char> png_encoding;
Lei Zhang9a7c2392018-03-02 23:54:26 +0000507 bool encoded;
508 if (FPDFBitmap_GetFormat(bitmap) == FPDFBitmap_Gray) {
509 encoded = image_diff_png::EncodeGrayPNG(buffer, width, height, stride,
510 &png_encoding);
511 } else {
512 encoded = image_diff_png::EncodeBGRAPNG(buffer, width, height, stride,
513 /*discard_transparency=*/false,
514 &png_encoding);
515 }
Henrique Nakashimadb269572018-01-16 19:02:15 +0000516
517 ASSERT_TRUE(encoded);
518 ASSERT_LT(filename.size(), 256u);
519
520 std::ofstream png_file;
Henrique Nakashima6d6a2432018-01-17 16:52:46 +0000521 png_file.open(filename, std::ios_base::out | std::ios_base::binary);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000522 png_file.write(reinterpret_cast<char*>(&png_encoding.front()),
523 png_encoding.size());
524 ASSERT_TRUE(png_file.good());
525 png_file.close();
526}
527#endif
528
thestigbcd3e532016-11-21 13:37:28 -0800529// static
530void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
531 int expected_width,
532 int expected_height,
533 const char* expected_md5sum) {
534 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
535 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400536
537 // The expected stride is calculated using the same formula as in
538 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
539 const int expected_stride =
540 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800541 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
542
543 if (!expected_md5sum)
544 return;
545
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000546 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800547}
548
Nicolas Pena3ff54002017-07-05 11:55:35 -0400549// static
550int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
551 const void* data,
552 unsigned long size) {
553 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Lei Zhang0729be22018-02-05 21:13:51 +0000554 pThis->data_string_.append(static_cast<const char*>(data), size);
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}