blob: baedeb3432c9ce056e050ec81adf67fd172dab11 [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);
222 FPDF_SetFormFieldHighlightColor(form_handle, 0, 0xFFE4DD);
223 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
224 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800225}
226
Tom Sepezda8189e2015-01-30 14:41:50 -0800227void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400228 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800229 FORM_DoDocumentJSAction(form_handle_);
230 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800231}
232
233int EmbedderTest::GetFirstPageNum() {
234 int first_page = FPDFAvail_GetFirstPageNum(document_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300235 (void)FPDFAvail_IsPageAvail(avail_, first_page,
236 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800237 return first_page;
238}
239
240int EmbedderTest::GetPageCount() {
241 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400242 for (int i = 0; i < page_count; ++i)
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300243 (void)FPDFAvail_IsPageAvail(avail_, i,
244 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800245 return page_count;
246}
247
Tom Sepezda8189e2015-01-30 14:41:50 -0800248FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400249 ASSERT(form_handle_);
weili0dadcc62016-08-23 21:10:57 -0700250 // First check whether it is loaded already.
251 auto it = page_map_.find(page_number);
252 if (it != page_map_.end())
253 return it->second;
254
Tom Sepez96d13342015-01-16 14:59:26 -0800255 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400256 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800257 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400258
Tom Sepezda8189e2015-01-30 14:41:50 -0800259 FORM_OnAfterLoadPage(page, form_handle_);
260 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700261 // Cache the page.
262 page_map_[page_number] = page;
dsinclaircb92dc72016-09-07 09:02:48 -0700263 page_reverse_map_[page] = page_number;
Tom Sepez396e8722015-09-09 10:16:08 -0700264 return page;
265}
266
Tom Sepezda8189e2015-01-30 14:41:50 -0800267FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400268 return RenderPageWithFlags(page, form_handle_, 0);
Jane Liuc533f4b2017-06-19 11:13:00 -0400269}
270
Jane Liubaa7ff42017-06-29 19:18:23 -0400271FPDF_BITMAP EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
272 FPDF_FORMHANDLE handle,
273 int flags) {
Tom Sepez96d13342015-01-16 14:59:26 -0800274 int width = static_cast<int>(FPDF_GetPageWidth(page));
275 int height = static_cast<int>(FPDF_GetPageHeight(page));
Lei Zhang453d96b2015-12-31 13:13:10 -0800276 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
277 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
278 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
279 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
Jane Liuc533f4b2017-06-19 11:13:00 -0400280 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, flags);
Jane Liubaa7ff42017-06-29 19:18:23 -0400281 FPDF_FFLDraw(handle, bitmap, page, 0, 0, width, height, 0, flags);
Tom Sepez96d13342015-01-16 14:59:26 -0800282 return bitmap;
283}
284
Tom Sepezda8189e2015-01-30 14:41:50 -0800285void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400286 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800287 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
288 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800289 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700290
291 auto it = page_reverse_map_.find(page);
292 if (it == page_reverse_map_.end())
293 return;
294
295 page_map_.erase(it->second);
296 page_reverse_map_.erase(it);
Tom Sepez96d13342015-01-16 14:59:26 -0800297}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800298
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400299FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300300 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
301 saved_file_access_.m_FileLen = m_String.size();
302 saved_file_access_.m_GetBlock = GetBlockFromString;
303 saved_file_access_.m_Param = &m_String;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400304
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300305 saved_fake_file_access_ =
306 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
307
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400308 EXPECT_TRUE(OpenDocumentHelper(password, false, saved_fake_file_access_.get(),
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300309 &m_SavedDocument, &m_SavedAvail,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400310 &m_SavedForm));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400311 return m_SavedDocument;
312}
313
314void EmbedderTest::CloseSavedDocument() {
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400315 ASSERT(m_SavedDocument);
316
317 FPDFDOC_ExitFormFillEnvironment(m_SavedForm);
318 FPDF_CloseDocument(m_SavedDocument);
319 FPDFAvail_Destroy(m_SavedAvail);
320
321 m_SavedForm = nullptr;
322 m_SavedDocument = nullptr;
323 m_SavedAvail = nullptr;
324}
325
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000326FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400327 ASSERT(m_SavedDocument);
328
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000329 EXPECT_LT(page_number, FPDF_GetPageCount(m_SavedDocument));
330 FPDF_PAGE page = FPDF_LoadPage(m_SavedDocument, page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400331
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000332 ASSERT(page);
333 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400334}
335
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000336FPDF_BITMAP EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
337 return RenderPageWithFlags(page, m_SavedForm, 0);
338}
339
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000340void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
341 ASSERT(page);
342 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400343}
344
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000345void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
346 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400347 int height,
348 const char* md5) {
349 ASSERT(m_SavedDocument);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000350 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400351
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000352 FPDF_BITMAP new_bitmap = RenderPageWithFlags(page, m_SavedForm, FPDF_ANNOT);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400353 CompareBitmap(new_bitmap, width, height, md5);
354 FPDFBitmap_Destroy(new_bitmap);
355}
356
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400357void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
358 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000359 FPDF_PAGE page = LoadSavedPage(0);
360 VerifySavedRendering(page, width, height, md5);
361 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400362 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400363}
364
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300365void EmbedderTest::SetWholeFileAvailable() {
366 ASSERT(fake_file_access_);
367 fake_file_access_->SetWholeFileAvailable();
368}
369
weili0dadcc62016-08-23 21:10:57 -0700370FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700371 FPDF_DOCUMENT document,
372 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700373 EmbedderTest* test = static_cast<EmbedderTest*>(info);
374 auto it = test->page_map_.find(page_index);
375 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700376}
377
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800378// static
379void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
380 int type) {
381 EmbedderTest* test = static_cast<EmbedderTest*>(info);
382 test->delegate_->UnsupportedHandler(type);
383}
384
385// static
386int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
387 FPDF_WIDESTRING message,
388 FPDF_WIDESTRING title,
389 int type,
390 int icon) {
391 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
392 return test->delegate_->Alert(message, title, type, icon);
393}
394
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700395// static
396int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 int msecs,
398 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700399 EmbedderTest* test = static_cast<EmbedderTest*>(info);
400 return test->delegate_->SetTimer(msecs, fn);
401}
402
403// static
404void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
405 EmbedderTest* test = static_cast<EmbedderTest*>(info);
406 return test->delegate_->KillTimer(id);
407}
408
Tom Sepez396e8722015-09-09 10:16:08 -0700409// static
410FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
411 FPDF_DOCUMENT document,
412 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700413 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
414 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700415}
416
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000417// static
418std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400419 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000420 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
421 FPDFBitmap_GetWidth(bitmap) *
422 GetBitmapBytesPerPixel(bitmap) *
423 FPDFBitmap_GetHeight(bitmap),
424 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400425 return CryptToBase16(digest);
426}
427
thestigbcd3e532016-11-21 13:37:28 -0800428// static
429void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
430 int expected_width,
431 int expected_height,
432 const char* expected_md5sum) {
433 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
434 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400435
436 // The expected stride is calculated using the same formula as in
437 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
438 const int expected_stride =
439 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800440 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
441
442 if (!expected_md5sum)
443 return;
444
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000445 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800446}
447
Nicolas Pena3ff54002017-07-05 11:55:35 -0400448// static
449int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
450 const void* data,
451 unsigned long size) {
452 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
453 pThis->m_String.append(static_cast<const char*>(data), size);
454 return 1;
455}
456
457// static
458int EmbedderTest::GetBlockFromString(void* param,
459 unsigned long pos,
460 unsigned char* buf,
461 unsigned long size) {
462 std::string* new_file = static_cast<std::string*>(param);
463 if (!new_file || pos + size < pos)
464 return 0;
465
466 unsigned long file_size = new_file->size();
467 if (pos + size > file_size)
468 return 0;
469
470 memcpy(buf, new_file->data() + pos, size);
471 return 1;
472}