blob: 0846d8ccb712c251fa95b7a1fba6929145dcd6c3 [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"
Tom Sepez452b4f32015-10-13 09:27:27 -070022
23#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080024#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050025#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070026#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080027
Tom Sepez96d13342015-01-16 14:59:26 -080028namespace {
thestigc08cd7a2016-06-27 09:47:59 -070029
30const char* g_exe_path = nullptr;
31
32#ifdef PDF_ENABLE_V8
33#ifdef V8_USE_EXTERNAL_STARTUP_DATA
34v8::StartupData* g_v8_natives = nullptr;
35v8::StartupData* g_v8_snapshot = nullptr;
36#endif // V8_USE_EXTERNAL_STARTUP_DATA
37#endif // PDF_ENABLE_V8
38
Tom Sepezcf22eb82015-05-12 17:28:08 -070039FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
Tom Sepez96d13342015-01-16 14:59:26 -080040 return true;
41}
42
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {}
Tom Sepez96d13342015-01-16 14:59:26 -080044
thestigbcd3e532016-11-21 13:37:28 -080045} // namespace
46
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -070048 : default_delegate_(new EmbedderTest::Delegate()),
49 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080050 form_handle_(nullptr),
51 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -070052 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080053 loader_(nullptr),
54 file_length_(0),
55 file_contents_(nullptr) {
56 memset(&hints_, 0, sizeof(hints_));
57 memset(&file_access_, 0, sizeof(file_access_));
58 memset(&file_avail_, 0, sizeof(file_avail_));
Tom Sepeza72e8e22015-10-07 10:17:53 -070059 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080060
Tom Sepez452b4f32015-10-13 09:27:27 -070061#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080062#ifdef V8_USE_EXTERNAL_STARTUP_DATA
thestigc08cd7a2016-06-27 09:47:59 -070063 if (g_v8_natives && g_v8_snapshot) {
64 InitializeV8ForPDFium(g_exe_path, std::string(), nullptr, nullptr,
65 &platform_);
66 } else {
67 g_v8_natives = new v8::StartupData;
68 g_v8_snapshot = new v8::StartupData;
69 InitializeV8ForPDFium(g_exe_path, std::string(), g_v8_natives,
70 g_v8_snapshot, &platform_);
71 }
Tom Sepezd831dc72015-10-19 16:04:22 -070072#else
thestigc08cd7a2016-06-27 09:47:59 -070073 InitializeV8ForPDFium(g_exe_path, &platform_);
Tom Sepez96d13342015-01-16 14:59:26 -080074#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -070075#endif // FPDF_ENABLE_V8
Nicolas Pena3ff54002017-07-05 11:55:35 -040076 FPDF_FILEWRITE::version = 1;
77 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080078}
Tom Sepez96d13342015-01-16 14:59:26 -080079
jochen38a1f0a2016-05-31 12:07:40 -070080EmbedderTest::~EmbedderTest() {
81#ifdef PDF_ENABLE_V8
82 v8::V8::ShutdownPlatform();
83 delete platform_;
84#endif // PDF_ENABLE_V8
85}
Tom Sepezf288bb12015-11-20 12:12:46 -080086
87void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070088 FPDF_LIBRARY_CONFIG config;
89 config.version = 2;
90 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070091 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070092 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070093 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080094
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
96 memset(info, 0, sizeof(UNSUPPORT_INFO));
97 info->version = 1;
98 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
99 FSDK_SetUnSpObjProcessHandler(info);
100}
Tom Sepez96d13342015-01-16 14:59:26 -0800101
102void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800103 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700104 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -0800105 FPDFDOC_ExitFormFillEnvironment(form_handle_);
106 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800107 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800108
Tom Sepez96d13342015-01-16 14:59:26 -0800109 FPDFAvail_Destroy(avail_);
110 FPDF_DestroyLibrary();
Lei Zhangd27acae2015-05-15 15:36:02 -0700111 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -0800112}
113
Tom Sepezd483eb42016-01-06 10:03:59 -0800114bool EmbedderTest::CreateEmptyDocument() {
115 document_ = FPDF_CreateNewDocument();
116 if (!document_)
117 return false;
118
Nicolas Pena3ff54002017-07-05 11:55:35 -0400119 form_handle_ = SetupFormFillEnvironment(document_);
Tom Sepezd483eb42016-01-06 10:03:59 -0800120 return true;
121}
122
Jun Fangdf7f3662015-11-10 18:29:18 +0800123bool EmbedderTest::OpenDocument(const std::string& filename,
thestig27ddf162016-05-23 15:06:59 -0700124 const char* password,
Jun Fangdf7f3662015-11-10 18:29:18 +0800125 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800126 std::string file_path;
127 if (!PathService::GetTestFilePath(filename, &file_path))
128 return false;
129 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400130 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800131 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800132
thestig29ce9232016-06-22 07:03:23 -0700133 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800134 loader_ = new TestLoader(file_contents_.get(), file_length_);
Tom Sepez96d13342015-01-16 14:59:26 -0800135 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700136 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800137 file_access_.m_Param = loader_;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400138 return OpenDocumentHelper(password, must_linearize, &file_avail_, &hints_,
139 &file_access_, &document_, &avail_, &form_handle_);
140}
Tom Sepez96d13342015-01-16 14:59:26 -0800141
Nicolas Pena56fc9722017-07-13 16:31:34 -0400142bool EmbedderTest::OpenDocumentHelper(const char* password,
143 bool must_linearize,
144 FX_FILEAVAIL* file_avail,
145 FX_DOWNLOADHINTS* hints,
146 FPDF_FILEACCESS* file_access,
147 FPDF_DOCUMENT* document,
148 FPDF_AVAIL* avail,
149 FPDF_FORMHANDLE* form_handle) {
150 file_avail->version = 1;
151 file_avail->IsDataAvail = Is_Data_Avail;
Tom Sepez96d13342015-01-16 14:59:26 -0800152
Nicolas Pena56fc9722017-07-13 16:31:34 -0400153 hints->version = 1;
154 hints->AddSegment = Add_Segment;
Tom Sepez96d13342015-01-16 14:59:26 -0800155
Nicolas Pena56fc9722017-07-13 16:31:34 -0400156 *avail = FPDFAvail_Create(file_avail, file_access);
Tom Sepez96d13342015-01-16 14:59:26 -0800157
Nicolas Pena56fc9722017-07-13 16:31:34 -0400158 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
159 *document = FPDFAvail_GetDocument(*avail, password);
160 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800161 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400162
Jun Fangdf7f3662015-11-10 18:29:18 +0800163 int32_t nRet = PDF_DATA_NOTAVAIL;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400164 while (nRet == PDF_DATA_NOTAVAIL)
165 nRet = FPDFAvail_IsDocAvail(*avail, hints);
166 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800167 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400168
169 nRet = FPDFAvail_IsFormAvail(*avail, hints);
170 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL)
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;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400176 while (nRet == PDF_DATA_NOTAVAIL)
177 nRet = FPDFAvail_IsPageAvail(*avail, i, hints);
178
179 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800180 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800181 }
182 } else {
Nicolas Pena56fc9722017-07-13 16:31:34 -0400183 if (must_linearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800184 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400185
186 *document = FPDF_LoadCustomDocument(file_access, password);
187 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800188 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800189 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400190 *form_handle = SetupFormFillEnvironment(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800191#ifdef PDF_ENABLE_XFA
JUN FANG827a1722015-03-05 13:39:21 -0800192 int docType = DOCTYPE_PDF;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400193 if (FPDF_HasXFAField(*document, &docType)) {
JUN FANG827a1722015-03-05 13:39:21 -0800194 if (docType != DOCTYPE_PDF)
Nicolas Pena56fc9722017-07-13 16:31:34 -0400195 (void)FPDF_LoadXFA(*document);
JUN FANG827a1722015-03-05 13:39:21 -0800196 }
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_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800236 return first_page;
237}
238
239int EmbedderTest::GetPageCount() {
240 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400241 for (int i = 0; i < page_count; ++i)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 (void)FPDFAvail_IsPageAvail(avail_, i, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800243 return page_count;
244}
245
Tom Sepezda8189e2015-01-30 14:41:50 -0800246FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400247 ASSERT(form_handle_);
weili0dadcc62016-08-23 21:10:57 -0700248 // First check whether it is loaded already.
249 auto it = page_map_.find(page_number);
250 if (it != page_map_.end())
251 return it->second;
252
Tom Sepez96d13342015-01-16 14:59:26 -0800253 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400254 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800255 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400256
Tom Sepezda8189e2015-01-30 14:41:50 -0800257 FORM_OnAfterLoadPage(page, form_handle_);
258 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700259 // Cache the page.
260 page_map_[page_number] = page;
dsinclaircb92dc72016-09-07 09:02:48 -0700261 page_reverse_map_[page] = page_number;
Tom Sepez396e8722015-09-09 10:16:08 -0700262 return page;
263}
264
Tom Sepezda8189e2015-01-30 14:41:50 -0800265FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400266 return RenderPageWithFlags(page, form_handle_, 0);
Jane Liuc533f4b2017-06-19 11:13:00 -0400267}
268
Jane Liubaa7ff42017-06-29 19:18:23 -0400269FPDF_BITMAP EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
270 FPDF_FORMHANDLE handle,
271 int flags) {
Tom Sepez96d13342015-01-16 14:59:26 -0800272 int width = static_cast<int>(FPDF_GetPageWidth(page));
273 int height = static_cast<int>(FPDF_GetPageHeight(page));
Lei Zhang453d96b2015-12-31 13:13:10 -0800274 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
275 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
276 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
277 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
Jane Liuc533f4b2017-06-19 11:13:00 -0400278 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, flags);
Jane Liubaa7ff42017-06-29 19:18:23 -0400279 FPDF_FFLDraw(handle, bitmap, page, 0, 0, width, height, 0, flags);
Tom Sepez96d13342015-01-16 14:59:26 -0800280 return bitmap;
281}
282
Tom Sepezda8189e2015-01-30 14:41:50 -0800283void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400284 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800285 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
286 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800287 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700288
289 auto it = page_reverse_map_.find(page);
290 if (it == page_reverse_map_.end())
291 return;
292
293 page_map_.erase(it->second);
294 page_reverse_map_.erase(it);
Tom Sepez96d13342015-01-16 14:59:26 -0800295}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800296
Nicolas Pena56fc9722017-07-13 16:31:34 -0400297void EmbedderTest::TestSaved(int width,
298 int height,
299 const char* md5,
300 const char* password) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400301 FPDF_FILEACCESS file_access;
302 memset(&file_access, 0, sizeof(file_access));
303 file_access.m_FileLen = m_String.size();
304 file_access.m_GetBlock = GetBlockFromString;
305 file_access.m_Param = &m_String;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400306 FX_FILEAVAIL file_avail;
307 FX_DOWNLOADHINTS hints;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400308
Nicolas Pena56fc9722017-07-13 16:31:34 -0400309 ASSERT_TRUE(OpenDocumentHelper(password, false, &file_avail, &hints,
310 &file_access, &m_SavedDocument, &m_SavedAvail,
311 &m_SavedForm));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400312 EXPECT_EQ(1, FPDF_GetPageCount(m_SavedDocument));
313 m_SavedPage = FPDF_LoadPage(m_SavedDocument, 0);
314 ASSERT_TRUE(m_SavedPage);
315 FPDF_BITMAP new_bitmap =
316 RenderPageWithFlags(m_SavedPage, m_SavedForm, FPDF_ANNOT);
317 CompareBitmap(new_bitmap, width, height, md5);
318 FPDFBitmap_Destroy(new_bitmap);
319}
320
321void EmbedderTest::CloseSaved() {
322 FPDF_ClosePage(m_SavedPage);
323 FPDFDOC_ExitFormFillEnvironment(m_SavedForm);
324 FPDF_CloseDocument(m_SavedDocument);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400325 FPDFAvail_Destroy(m_SavedAvail);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400326}
327
328void EmbedderTest::TestAndCloseSaved(int width, int height, const char* md5) {
329 TestSaved(width, height, md5);
330 CloseSaved();
331}
332
weili0dadcc62016-08-23 21:10:57 -0700333FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700334 FPDF_DOCUMENT document,
335 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700336 EmbedderTest* test = static_cast<EmbedderTest*>(info);
337 auto it = test->page_map_.find(page_index);
338 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700339}
340
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800341// static
342void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
343 int type) {
344 EmbedderTest* test = static_cast<EmbedderTest*>(info);
345 test->delegate_->UnsupportedHandler(type);
346}
347
348// static
349int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
350 FPDF_WIDESTRING message,
351 FPDF_WIDESTRING title,
352 int type,
353 int icon) {
354 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
355 return test->delegate_->Alert(message, title, type, icon);
356}
357
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700358// static
359int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 int msecs,
361 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700362 EmbedderTest* test = static_cast<EmbedderTest*>(info);
363 return test->delegate_->SetTimer(msecs, fn);
364}
365
366// static
367void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
368 EmbedderTest* test = static_cast<EmbedderTest*>(info);
369 return test->delegate_->KillTimer(id);
370}
371
Tom Sepez396e8722015-09-09 10:16:08 -0700372// static
373FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
374 FPDF_DOCUMENT document,
375 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700376 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
377 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700378}
379
Dan Sinclair957480c2017-06-13 15:21:14 -0400380std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap,
381 int expected_width,
382 int expected_height) {
383 uint8_t digest[16];
384 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
385 expected_width * 4 * expected_height, digest);
386 return CryptToBase16(digest);
387}
388
thestigbcd3e532016-11-21 13:37:28 -0800389// static
390void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
391 int expected_width,
392 int expected_height,
393 const char* expected_md5sum) {
394 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
395 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
396 const int expected_stride = expected_width * 4;
397 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
398
399 if (!expected_md5sum)
400 return;
401
Dan Sinclair957480c2017-06-13 15:21:14 -0400402 EXPECT_EQ(expected_md5sum,
403 HashBitmap(bitmap, expected_width, expected_height));
thestigbcd3e532016-11-21 13:37:28 -0800404}
405
Nicolas Pena3ff54002017-07-05 11:55:35 -0400406// static
407int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
408 const void* data,
409 unsigned long size) {
410 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
411 pThis->m_String.append(static_cast<const char*>(data), size);
412 return 1;
413}
414
415// static
416int EmbedderTest::GetBlockFromString(void* param,
417 unsigned long pos,
418 unsigned char* buf,
419 unsigned long size) {
420 std::string* new_file = static_cast<std::string*>(param);
421 if (!new_file || pos + size < pos)
422 return 0;
423
424 unsigned long file_size = new_file->size();
425 if (pos + size > file_size)
426 return 0;
427
428 memcpy(buf, new_file->data() + pos, size);
429 return 1;
430}
431
Tom Sepez1b1bb492015-01-22 17:36:32 -0800432// Can't use gtest-provided main since we need to stash the path to the
433// executable in order to find the external V8 binary data files.
434int main(int argc, char** argv) {
thestigc08cd7a2016-06-27 09:47:59 -0700435 g_exe_path = argv[0];
Tom Sepez1b1bb492015-01-22 17:36:32 -0800436 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800437 testing::InitGoogleMock(&argc, argv);
thestigc08cd7a2016-06-27 09:47:59 -0700438 int ret_val = RUN_ALL_TESTS();
439
440#ifdef PDF_ENABLE_V8
441#ifdef V8_USE_EXTERNAL_STARTUP_DATA
442 if (g_v8_natives)
443 free(const_cast<char*>(g_v8_natives->data));
444 if (g_v8_snapshot)
445 free(const_cast<char*>(g_v8_snapshot->data));
446#endif // V8_USE_EXTERNAL_STARTUP_DATA
447#endif // PDF_ENABLE_V8
448
449 return ret_val;
Tom Sepez1b1bb492015-01-22 17:36:32 -0800450}