blob: b434353bd9f9b14047a7575e061ad5dd18523cb1 [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>
11#include <string>
12#include <utility>
13#include <vector>
14
thestigbcd3e532016-11-21 13:37:28 -080015#include "core/fdrm/crypto/fx_crypt.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080016#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080017#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080018#include "public/fpdf_text.h"
19#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080020#include "testing/gmock/include/gmock/gmock.h"
Henrique Nakashimadb269572018-01-16 19:02:15 +000021#include "testing/image_diff/image_diff_png.h"
Wei Li091f7a02015-11-09 12:09:55 -080022#include "testing/test_support.h"
23#include "testing/utils/path_service.h"
Artem Strygin0e60b9e2017-09-28 18:46:03 +030024#include "third_party/base/ptr_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070025
26#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080027#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050028#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070029#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080030
Tom Sepez96d13342015-01-16 14:59:26 -080031namespace {
thestigc08cd7a2016-06-27 09:47:59 -070032
Jane Liu28fb7ba2017-08-02 21:45:57 -040033int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
34 const int format = FPDFBitmap_GetFormat(bitmap);
35 switch (format) {
36 case FPDFBitmap_Gray:
37 return 1;
38 case FPDFBitmap_BGR:
39 return 3;
40 case FPDFBitmap_BGRx:
41 case FPDFBitmap_BGRA:
42 return 4;
43 default:
44 ASSERT(false);
45 return 0;
46 }
47}
48
thestigbcd3e532016-11-21 13:37:28 -080049} // namespace
50
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -070052 : default_delegate_(new EmbedderTest::Delegate()),
53 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080054 form_handle_(nullptr),
55 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -070056 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080057 loader_(nullptr),
58 file_length_(0),
59 file_contents_(nullptr) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -080060 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepeza72e8e22015-10-07 10:17:53 -070061 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080062
Nicolas Pena3ff54002017-07-05 11:55:35 -040063 FPDF_FILEWRITE::version = 1;
64 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080065}
Tom Sepez96d13342015-01-16 14:59:26 -080066
Dan Sinclair5553d8b2018-01-03 09:44:28 -050067EmbedderTest::~EmbedderTest() {}
Tom Sepezf288bb12015-11-20 12:12:46 -080068
69void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070070 FPDF_LIBRARY_CONFIG config;
71 config.version = 2;
72 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070073 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070074 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070075 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080076
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
78 memset(info, 0, sizeof(UNSUPPORT_INFO));
79 info->version = 1;
80 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
81 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +000082
83 m_SavedDocument = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084}
Tom Sepez96d13342015-01-16 14:59:26 -080085
86void EmbedderTest::TearDown() {
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
Nicolas Pena3ff54002017-07-05 11:55:35 -0400103 form_handle_ = SetupFormFillEnvironment(document_);
Tom Sepezd483eb42016-01-06 10:03:59 -0800104 return true;
105}
106
Lei Zhang208eecf2017-12-20 19:40:50 +0000107bool EmbedderTest::OpenDocument(const std::string& filename) {
108 return OpenDocumentWithOptions(filename, nullptr, false);
109}
110
111bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
112 return OpenDocumentWithOptions(filename, nullptr, true);
113}
114
115bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
116 const char* password) {
117 return OpenDocumentWithOptions(filename, password, false);
118}
119
120bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
121 const char* password,
122 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800123 std::string file_path;
124 if (!PathService::GetTestFilePath(filename, &file_path))
125 return false;
126 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400127 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800128 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800129
thestig29ce9232016-06-22 07:03:23 -0700130 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800131 loader_ = new TestLoader(file_contents_.get(), file_length_);
Tom Sepez96d13342015-01-16 14:59:26 -0800132 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700133 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800134 file_access_.m_Param = loader_;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300135 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
136 return OpenDocumentHelper(password, must_linearize, fake_file_access_.get(),
137 &document_, &avail_, &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400138}
Tom Sepez96d13342015-01-16 14:59:26 -0800139
Nicolas Pena56fc9722017-07-13 16:31:34 -0400140bool EmbedderTest::OpenDocumentHelper(const char* password,
141 bool must_linearize,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300142 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400143 FPDF_DOCUMENT* document,
144 FPDF_AVAIL* avail,
145 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300146 network_simulator->AddSegment(0, 1024);
147 network_simulator->SetRequestedDataAvailable();
148 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
149 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400150 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300151 int32_t nRet = PDF_DATA_NOTAVAIL;
152 while (nRet == PDF_DATA_NOTAVAIL) {
153 network_simulator->SetRequestedDataAvailable();
154 nRet =
155 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
156 }
157 if (nRet == PDF_DATA_ERROR)
158 return false;
159
Nicolas Pena56fc9722017-07-13 16:31:34 -0400160 *document = FPDFAvail_GetDocument(*avail, password);
161 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800162 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400163
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300164 nRet = PDF_DATA_NOTAVAIL;
165 while (nRet == PDF_DATA_NOTAVAIL) {
166 network_simulator->SetRequestedDataAvailable();
167 nRet =
168 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
169 }
170 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800171 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400172
173 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800174 for (int i = 0; i < page_count; ++i) {
175 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300176 while (nRet == PDF_DATA_NOTAVAIL) {
177 network_simulator->SetRequestedDataAvailable();
178 nRet = FPDFAvail_IsPageAvail(*avail, i,
179 network_simulator->GetDownloadHints());
180 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400181
182 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800183 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800184 }
185 } else {
Nicolas Pena56fc9722017-07-13 16:31:34 -0400186 if (must_linearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800187 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300188 network_simulator->SetWholeFileAvailable();
189 *document =
190 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400191 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800192 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800193 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400194 *form_handle = SetupFormFillEnvironment(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800195#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400196 int doc_type = FPDF_GetFormType(*document);
197 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
198 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800199#endif // PDF_ENABLE_XFA
Nicolas Pena56fc9722017-07-13 16:31:34 -0400200 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800201 return true;
202}
Tom Sepez96d13342015-01-16 14:59:26 -0800203
Nicolas Pena3ff54002017-07-05 11:55:35 -0400204FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(FPDF_DOCUMENT doc) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800205 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400206 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200207 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800208 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400209 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800210
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800211 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
212 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800213#ifdef PDF_ENABLE_XFA
214 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800215#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800216 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800217#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700218 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
219 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700220 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800221 formfillinfo->m_pJsPlatform = platform;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400222 FPDF_FORMHANDLE form_handle =
223 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000224 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
225 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400226 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
227 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800228}
229
Tom Sepezda8189e2015-01-30 14:41:50 -0800230void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400231 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800232 FORM_DoDocumentJSAction(form_handle_);
233 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800234}
235
236int EmbedderTest::GetFirstPageNum() {
237 int first_page = FPDFAvail_GetFirstPageNum(document_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300238 (void)FPDFAvail_IsPageAvail(avail_, first_page,
239 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800240 return first_page;
241}
242
243int EmbedderTest::GetPageCount() {
244 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400245 for (int i = 0; i < page_count; ++i)
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300246 (void)FPDFAvail_IsPageAvail(avail_, i,
247 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800248 return page_count;
249}
250
Tom Sepezda8189e2015-01-30 14:41:50 -0800251FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400252 ASSERT(form_handle_);
weili0dadcc62016-08-23 21:10:57 -0700253 // First check whether it is loaded already.
254 auto it = page_map_.find(page_number);
255 if (it != page_map_.end())
256 return it->second;
257
Tom Sepez96d13342015-01-16 14:59:26 -0800258 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400259 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800260 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400261
Tom Sepezda8189e2015-01-30 14:41:50 -0800262 FORM_OnAfterLoadPage(page, form_handle_);
263 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700264 // Cache the page.
265 page_map_[page_number] = page;
dsinclaircb92dc72016-09-07 09:02:48 -0700266 page_reverse_map_[page] = page_number;
Tom Sepez396e8722015-09-09 10:16:08 -0700267 return page;
268}
269
Tom Sepezda8189e2015-01-30 14:41:50 -0800270FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400271 return RenderPageWithFlags(page, form_handle_, 0);
Jane Liuc533f4b2017-06-19 11:13:00 -0400272}
273
Jane Liubaa7ff42017-06-29 19:18:23 -0400274FPDF_BITMAP EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
275 FPDF_FORMHANDLE handle,
276 int flags) {
Tom Sepez96d13342015-01-16 14:59:26 -0800277 int width = static_cast<int>(FPDF_GetPageWidth(page));
278 int height = static_cast<int>(FPDF_GetPageHeight(page));
Lei Zhang453d96b2015-12-31 13:13:10 -0800279 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
280 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
281 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
282 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
Jane Liuc533f4b2017-06-19 11:13:00 -0400283 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, flags);
Jane Liubaa7ff42017-06-29 19:18:23 -0400284 FPDF_FFLDraw(handle, bitmap, page, 0, 0, width, height, 0, flags);
Tom Sepez96d13342015-01-16 14:59:26 -0800285 return bitmap;
286}
287
Tom Sepezda8189e2015-01-30 14:41:50 -0800288void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400289 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800290 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
291 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800292 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700293
294 auto it = page_reverse_map_.find(page);
295 if (it == page_reverse_map_.end())
296 return;
297
298 page_map_.erase(it->second);
299 page_reverse_map_.erase(it);
Tom Sepez96d13342015-01-16 14:59:26 -0800300}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800301
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400302FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300303 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
304 saved_file_access_.m_FileLen = m_String.size();
305 saved_file_access_.m_GetBlock = GetBlockFromString;
306 saved_file_access_.m_Param = &m_String;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400307
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300308 saved_fake_file_access_ =
309 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
310
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400311 EXPECT_TRUE(OpenDocumentHelper(password, false, saved_fake_file_access_.get(),
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300312 &m_SavedDocument, &m_SavedAvail,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400313 &m_SavedForm));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400314 return m_SavedDocument;
315}
316
317void EmbedderTest::CloseSavedDocument() {
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400318 ASSERT(m_SavedDocument);
319
320 FPDFDOC_ExitFormFillEnvironment(m_SavedForm);
321 FPDF_CloseDocument(m_SavedDocument);
322 FPDFAvail_Destroy(m_SavedAvail);
323
324 m_SavedForm = nullptr;
325 m_SavedDocument = nullptr;
326 m_SavedAvail = nullptr;
327}
328
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000329FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400330 ASSERT(m_SavedDocument);
331
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000332 EXPECT_LT(page_number, FPDF_GetPageCount(m_SavedDocument));
333 FPDF_PAGE page = FPDF_LoadPage(m_SavedDocument, page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400334
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000335 ASSERT(page);
336 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400337}
338
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000339FPDF_BITMAP EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
340 return RenderPageWithFlags(page, m_SavedForm, 0);
341}
342
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000343void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
344 ASSERT(page);
345 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400346}
347
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000348void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
349 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400350 int height,
351 const char* md5) {
352 ASSERT(m_SavedDocument);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000353 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400354
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000355 FPDF_BITMAP new_bitmap = RenderPageWithFlags(page, m_SavedForm, FPDF_ANNOT);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400356 CompareBitmap(new_bitmap, width, height, md5);
357 FPDFBitmap_Destroy(new_bitmap);
358}
359
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400360void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
361 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000362 FPDF_PAGE page = LoadSavedPage(0);
363 VerifySavedRendering(page, width, height, md5);
364 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400365 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400366}
367
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300368void EmbedderTest::SetWholeFileAvailable() {
369 ASSERT(fake_file_access_);
370 fake_file_access_->SetWholeFileAvailable();
371}
372
weili0dadcc62016-08-23 21:10:57 -0700373FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700374 FPDF_DOCUMENT document,
375 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700376 EmbedderTest* test = static_cast<EmbedderTest*>(info);
377 auto it = test->page_map_.find(page_index);
378 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700379}
380
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800381// static
382void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
383 int type) {
384 EmbedderTest* test = static_cast<EmbedderTest*>(info);
385 test->delegate_->UnsupportedHandler(type);
386}
387
388// static
389int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
390 FPDF_WIDESTRING message,
391 FPDF_WIDESTRING title,
392 int type,
393 int icon) {
394 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
395 return test->delegate_->Alert(message, title, type, icon);
396}
397
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700398// static
399int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 int msecs,
401 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700402 EmbedderTest* test = static_cast<EmbedderTest*>(info);
403 return test->delegate_->SetTimer(msecs, fn);
404}
405
406// static
407void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
408 EmbedderTest* test = static_cast<EmbedderTest*>(info);
409 return test->delegate_->KillTimer(id);
410}
411
Tom Sepez396e8722015-09-09 10:16:08 -0700412// static
413FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
414 FPDF_DOCUMENT document,
415 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700416 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
417 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700418}
419
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000420// static
421std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400422 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000423 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
424 FPDFBitmap_GetWidth(bitmap) *
425 GetBitmapBytesPerPixel(bitmap) *
426 FPDFBitmap_GetHeight(bitmap),
427 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400428 return CryptToBase16(digest);
429}
430
Henrique Nakashimadb269572018-01-16 19:02:15 +0000431#ifndef NDEBUG
432// static
433void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
434 const std::string& filename) {
435 const int stride = FPDFBitmap_GetStride(bitmap);
436 const int width = FPDFBitmap_GetWidth(bitmap);
437 const int height = FPDFBitmap_GetHeight(bitmap);
438 const auto* buffer =
439 static_cast<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap));
440
441 std::vector<unsigned char> png_encoding;
442 bool encoded = image_diff_png::EncodeBGRAPNG(buffer, width, height, stride,
443 false, &png_encoding);
444
445 ASSERT_TRUE(encoded);
446 ASSERT_LT(filename.size(), 256u);
447
448 std::ofstream png_file;
Henrique Nakashima6d6a2432018-01-17 16:52:46 +0000449 png_file.open(filename, std::ios_base::out | std::ios_base::binary);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000450 png_file.write(reinterpret_cast<char*>(&png_encoding.front()),
451 png_encoding.size());
452 ASSERT_TRUE(png_file.good());
453 png_file.close();
454}
455#endif
456
thestigbcd3e532016-11-21 13:37:28 -0800457// static
458void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
459 int expected_width,
460 int expected_height,
461 const char* expected_md5sum) {
462 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
463 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400464
465 // The expected stride is calculated using the same formula as in
466 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
467 const int expected_stride =
468 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800469 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
470
471 if (!expected_md5sum)
472 return;
473
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000474 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800475}
476
Nicolas Pena3ff54002017-07-05 11:55:35 -0400477// static
478int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
479 const void* data,
480 unsigned long size) {
481 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
482 pThis->m_String.append(static_cast<const char*>(data), size);
483 return 1;
484}
485
486// static
487int EmbedderTest::GetBlockFromString(void* param,
488 unsigned long pos,
489 unsigned char* buf,
490 unsigned long size) {
491 std::string* new_file = static_cast<std::string*>(param);
492 if (!new_file || pos + size < pos)
493 return 0;
494
495 unsigned long file_size = new_file->size();
496 if (pos + size > file_size)
497 return 0;
498
499 memcpy(buf, new_file->data() + pos, size);
500 return 1;
501}