blob: de7251383baa3ddb05a1c4fab99030e9b6644e3a [file] [log] [blame]
Lei Zhang1ac47eb2015-12-21 11:04:44 -08001// Copyright 2015 PDFium Authors. All rights reserved.
Tom Sepez96d13342015-01-16 14:59:26 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Wei Li091f7a02015-11-09 12:09:55 -08005#include "testing/embedder_test.h"
Tom Sepez96d13342015-01-16 14:59:26 -08006
7#include <limits.h>
Tom Sepez96d13342015-01-16 14:59:26 -08008
9#include <list>
10#include <string>
11#include <utility>
12#include <vector>
13
thestigbcd3e532016-11-21 13:37:28 -080014#include "core/fdrm/crypto/fx_crypt.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080015#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080016#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080017#include "public/fpdf_text.h"
18#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080019#include "testing/gmock/include/gmock/gmock.h"
Wei Li091f7a02015-11-09 12:09:55 -080020#include "testing/test_support.h"
21#include "testing/utils/path_service.h"
Artem Strygin0e60b9e2017-09-28 18:46:03 +030022#include "third_party/base/ptr_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070023
24#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080025#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050026#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070027#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080028
Tom Sepez96d13342015-01-16 14:59:26 -080029namespace {
thestigc08cd7a2016-06-27 09:47:59 -070030
Jane Liu28fb7ba2017-08-02 21:45:57 -040031int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
32 const int format = FPDFBitmap_GetFormat(bitmap);
33 switch (format) {
34 case FPDFBitmap_Gray:
35 return 1;
36 case FPDFBitmap_BGR:
37 return 3;
38 case FPDFBitmap_BGRx:
39 case FPDFBitmap_BGRA:
40 return 4;
41 default:
42 ASSERT(false);
43 return 0;
44 }
45}
46
thestigbcd3e532016-11-21 13:37:28 -080047} // namespace
48
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -070050 : default_delegate_(new EmbedderTest::Delegate()),
51 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080052 form_handle_(nullptr),
53 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -070054 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080055 loader_(nullptr),
56 file_length_(0),
57 file_contents_(nullptr) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -080058 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepeza72e8e22015-10-07 10:17:53 -070059 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080060
Nicolas Pena3ff54002017-07-05 11:55:35 -040061 FPDF_FILEWRITE::version = 1;
62 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080063}
Tom Sepez96d13342015-01-16 14:59:26 -080064
Dan Sinclair5553d8b2018-01-03 09:44:28 -050065EmbedderTest::~EmbedderTest() {}
Tom Sepezf288bb12015-11-20 12:12:46 -080066
67void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070068 FPDF_LIBRARY_CONFIG config;
69 config.version = 2;
70 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070071 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070072 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070073 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080074
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
76 memset(info, 0, sizeof(UNSUPPORT_INFO));
77 info->version = 1;
78 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
79 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +000080
81 m_SavedDocument = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082}
Tom Sepez96d13342015-01-16 14:59:26 -080083
84void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -080085 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -070086 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -080087 FPDFDOC_ExitFormFillEnvironment(form_handle_);
88 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -080089 }
Tom Sepezc46d0002015-11-30 15:46:36 -080090
Tom Sepez96d13342015-01-16 14:59:26 -080091 FPDFAvail_Destroy(avail_);
92 FPDF_DestroyLibrary();
Lei Zhangd27acae2015-05-15 15:36:02 -070093 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -080094}
95
Tom Sepezd483eb42016-01-06 10:03:59 -080096bool EmbedderTest::CreateEmptyDocument() {
97 document_ = FPDF_CreateNewDocument();
98 if (!document_)
99 return false;
100
Nicolas Pena3ff54002017-07-05 11:55:35 -0400101 form_handle_ = SetupFormFillEnvironment(document_);
Tom Sepezd483eb42016-01-06 10:03:59 -0800102 return true;
103}
104
Lei Zhang208eecf2017-12-20 19:40:50 +0000105bool EmbedderTest::OpenDocument(const std::string& filename) {
106 return OpenDocumentWithOptions(filename, nullptr, false);
107}
108
109bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
110 return OpenDocumentWithOptions(filename, nullptr, true);
111}
112
113bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
114 const char* password) {
115 return OpenDocumentWithOptions(filename, password, false);
116}
117
118bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
119 const char* password,
120 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800121 std::string file_path;
122 if (!PathService::GetTestFilePath(filename, &file_path))
123 return false;
124 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400125 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800126 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800127
thestig29ce9232016-06-22 07:03:23 -0700128 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800129 loader_ = new TestLoader(file_contents_.get(), file_length_);
Tom Sepez96d13342015-01-16 14:59:26 -0800130 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700131 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800132 file_access_.m_Param = loader_;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300133 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
134 return OpenDocumentHelper(password, must_linearize, fake_file_access_.get(),
135 &document_, &avail_, &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400136}
Tom Sepez96d13342015-01-16 14:59:26 -0800137
Nicolas Pena56fc9722017-07-13 16:31:34 -0400138bool EmbedderTest::OpenDocumentHelper(const char* password,
139 bool must_linearize,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300140 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400141 FPDF_DOCUMENT* document,
142 FPDF_AVAIL* avail,
143 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300144 network_simulator->AddSegment(0, 1024);
145 network_simulator->SetRequestedDataAvailable();
146 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
147 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400148 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300149 int32_t nRet = PDF_DATA_NOTAVAIL;
150 while (nRet == PDF_DATA_NOTAVAIL) {
151 network_simulator->SetRequestedDataAvailable();
152 nRet =
153 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
154 }
155 if (nRet == PDF_DATA_ERROR)
156 return false;
157
Nicolas Pena56fc9722017-07-13 16:31:34 -0400158 *document = FPDFAvail_GetDocument(*avail, password);
159 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800160 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400161
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300162 nRet = PDF_DATA_NOTAVAIL;
163 while (nRet == PDF_DATA_NOTAVAIL) {
164 network_simulator->SetRequestedDataAvailable();
165 nRet =
166 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
167 }
168 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800169 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400170
171 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800172 for (int i = 0; i < page_count; ++i) {
173 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300174 while (nRet == PDF_DATA_NOTAVAIL) {
175 network_simulator->SetRequestedDataAvailable();
176 nRet = FPDFAvail_IsPageAvail(*avail, i,
177 network_simulator->GetDownloadHints());
178 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400179
180 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800181 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800182 }
183 } else {
Nicolas Pena56fc9722017-07-13 16:31:34 -0400184 if (must_linearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800185 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300186 network_simulator->SetWholeFileAvailable();
187 *document =
188 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400189 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800190 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800191 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400192 *form_handle = SetupFormFillEnvironment(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800193#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400194 int doc_type = FPDF_GetFormType(*document);
195 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
196 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800197#endif // PDF_ENABLE_XFA
Nicolas Pena56fc9722017-07-13 16:31:34 -0400198 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800199 return true;
200}
Tom Sepez96d13342015-01-16 14:59:26 -0800201
Nicolas Pena3ff54002017-07-05 11:55:35 -0400202FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(FPDF_DOCUMENT doc) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800203 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400204 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200205 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800206 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400207 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800208
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800209 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
210 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800211#ifdef PDF_ENABLE_XFA
212 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800213#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800214 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800215#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700216 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
217 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700218 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800219 formfillinfo->m_pJsPlatform = platform;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400220 FPDF_FORMHANDLE form_handle =
221 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000222 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
223 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400224 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
225 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800226}
227
Tom Sepezda8189e2015-01-30 14:41:50 -0800228void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400229 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800230 FORM_DoDocumentJSAction(form_handle_);
231 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800232}
233
234int EmbedderTest::GetFirstPageNum() {
235 int first_page = FPDFAvail_GetFirstPageNum(document_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300236 (void)FPDFAvail_IsPageAvail(avail_, first_page,
237 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800238 return first_page;
239}
240
241int EmbedderTest::GetPageCount() {
242 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400243 for (int i = 0; i < page_count; ++i)
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300244 (void)FPDFAvail_IsPageAvail(avail_, i,
245 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800246 return page_count;
247}
248
Tom Sepezda8189e2015-01-30 14:41:50 -0800249FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400250 ASSERT(form_handle_);
weili0dadcc62016-08-23 21:10:57 -0700251 // First check whether it is loaded already.
252 auto it = page_map_.find(page_number);
253 if (it != page_map_.end())
254 return it->second;
255
Tom Sepez96d13342015-01-16 14:59:26 -0800256 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400257 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800258 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400259
Tom Sepezda8189e2015-01-30 14:41:50 -0800260 FORM_OnAfterLoadPage(page, form_handle_);
261 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700262 // Cache the page.
263 page_map_[page_number] = page;
dsinclaircb92dc72016-09-07 09:02:48 -0700264 page_reverse_map_[page] = page_number;
Tom Sepez396e8722015-09-09 10:16:08 -0700265 return page;
266}
267
Tom Sepezda8189e2015-01-30 14:41:50 -0800268FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400269 return RenderPageWithFlags(page, form_handle_, 0);
Jane Liuc533f4b2017-06-19 11:13:00 -0400270}
271
Jane Liubaa7ff42017-06-29 19:18:23 -0400272FPDF_BITMAP EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
273 FPDF_FORMHANDLE handle,
274 int flags) {
Tom Sepez96d13342015-01-16 14:59:26 -0800275 int width = static_cast<int>(FPDF_GetPageWidth(page));
276 int height = static_cast<int>(FPDF_GetPageHeight(page));
Lei Zhang453d96b2015-12-31 13:13:10 -0800277 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
278 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
279 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
280 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
Jane Liuc533f4b2017-06-19 11:13:00 -0400281 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, flags);
Jane Liubaa7ff42017-06-29 19:18:23 -0400282 FPDF_FFLDraw(handle, bitmap, page, 0, 0, width, height, 0, flags);
Tom Sepez96d13342015-01-16 14:59:26 -0800283 return bitmap;
284}
285
Tom Sepezda8189e2015-01-30 14:41:50 -0800286void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400287 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800288 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
289 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800290 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700291
292 auto it = page_reverse_map_.find(page);
293 if (it == page_reverse_map_.end())
294 return;
295
296 page_map_.erase(it->second);
297 page_reverse_map_.erase(it);
Tom Sepez96d13342015-01-16 14:59:26 -0800298}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800299
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400300FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300301 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
302 saved_file_access_.m_FileLen = m_String.size();
303 saved_file_access_.m_GetBlock = GetBlockFromString;
304 saved_file_access_.m_Param = &m_String;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400305
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300306 saved_fake_file_access_ =
307 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
308
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400309 EXPECT_TRUE(OpenDocumentHelper(password, false, saved_fake_file_access_.get(),
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300310 &m_SavedDocument, &m_SavedAvail,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400311 &m_SavedForm));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400312 return m_SavedDocument;
313}
314
315void EmbedderTest::CloseSavedDocument() {
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400316 ASSERT(m_SavedDocument);
317
318 FPDFDOC_ExitFormFillEnvironment(m_SavedForm);
319 FPDF_CloseDocument(m_SavedDocument);
320 FPDFAvail_Destroy(m_SavedAvail);
321
322 m_SavedForm = nullptr;
323 m_SavedDocument = nullptr;
324 m_SavedAvail = nullptr;
325}
326
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000327FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400328 ASSERT(m_SavedDocument);
329
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000330 EXPECT_LT(page_number, FPDF_GetPageCount(m_SavedDocument));
331 FPDF_PAGE page = FPDF_LoadPage(m_SavedDocument, page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400332
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000333 ASSERT(page);
334 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400335}
336
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000337FPDF_BITMAP EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
338 return RenderPageWithFlags(page, m_SavedForm, 0);
339}
340
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000341void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
342 ASSERT(page);
343 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400344}
345
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000346void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
347 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400348 int height,
349 const char* md5) {
350 ASSERT(m_SavedDocument);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000351 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400352
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000353 FPDF_BITMAP new_bitmap = RenderPageWithFlags(page, m_SavedForm, FPDF_ANNOT);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400354 CompareBitmap(new_bitmap, width, height, md5);
355 FPDFBitmap_Destroy(new_bitmap);
356}
357
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400358void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
359 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000360 FPDF_PAGE page = LoadSavedPage(0);
361 VerifySavedRendering(page, width, height, md5);
362 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400363 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400364}
365
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300366void EmbedderTest::SetWholeFileAvailable() {
367 ASSERT(fake_file_access_);
368 fake_file_access_->SetWholeFileAvailable();
369}
370
weili0dadcc62016-08-23 21:10:57 -0700371FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700372 FPDF_DOCUMENT document,
373 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700374 EmbedderTest* test = static_cast<EmbedderTest*>(info);
375 auto it = test->page_map_.find(page_index);
376 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700377}
378
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800379// static
380void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
381 int type) {
382 EmbedderTest* test = static_cast<EmbedderTest*>(info);
383 test->delegate_->UnsupportedHandler(type);
384}
385
386// static
387int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
388 FPDF_WIDESTRING message,
389 FPDF_WIDESTRING title,
390 int type,
391 int icon) {
392 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
393 return test->delegate_->Alert(message, title, type, icon);
394}
395
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700396// static
397int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 int msecs,
399 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700400 EmbedderTest* test = static_cast<EmbedderTest*>(info);
401 return test->delegate_->SetTimer(msecs, fn);
402}
403
404// static
405void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
406 EmbedderTest* test = static_cast<EmbedderTest*>(info);
407 return test->delegate_->KillTimer(id);
408}
409
Tom Sepez396e8722015-09-09 10:16:08 -0700410// static
411FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
412 FPDF_DOCUMENT document,
413 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700414 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
415 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700416}
417
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000418// static
419std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400420 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000421 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
422 FPDFBitmap_GetWidth(bitmap) *
423 GetBitmapBytesPerPixel(bitmap) *
424 FPDFBitmap_GetHeight(bitmap),
425 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400426 return CryptToBase16(digest);
427}
428
thestigbcd3e532016-11-21 13:37:28 -0800429// static
430void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
431 int expected_width,
432 int expected_height,
433 const char* expected_md5sum) {
434 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
435 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400436
437 // The expected stride is calculated using the same formula as in
438 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
439 const int expected_stride =
440 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800441 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
442
443 if (!expected_md5sum)
444 return;
445
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000446 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800447}
448
Nicolas Pena3ff54002017-07-05 11:55:35 -0400449// static
450int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
451 const void* data,
452 unsigned long size) {
453 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
454 pThis->m_String.append(static_cast<const char*>(data), size);
455 return 1;
456}
457
458// static
459int EmbedderTest::GetBlockFromString(void* param,
460 unsigned long pos,
461 unsigned char* buf,
462 unsigned long size) {
463 std::string* new_file = static_cast<std::string*>(param);
464 if (!new_file || pos + size < pos)
465 return 0;
466
467 unsigned long file_size = new_file->size();
468 if (pos + size > file_size)
469 return 0;
470
471 memcpy(buf, new_file->data() + pos, size);
472 return 1;
473}