blob: 4c29c06da5ba567e7a8d1ecc5ef8020963ca4ba4 [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
31const char* g_exe_path = nullptr;
32
33#ifdef PDF_ENABLE_V8
34#ifdef V8_USE_EXTERNAL_STARTUP_DATA
35v8::StartupData* g_v8_natives = nullptr;
36v8::StartupData* g_v8_snapshot = nullptr;
37#endif // V8_USE_EXTERNAL_STARTUP_DATA
38#endif // PDF_ENABLE_V8
39
Jane Liu28fb7ba2017-08-02 21:45:57 -040040int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
41 const int format = FPDFBitmap_GetFormat(bitmap);
42 switch (format) {
43 case FPDFBitmap_Gray:
44 return 1;
45 case FPDFBitmap_BGR:
46 return 3;
47 case FPDFBitmap_BGRx:
48 case FPDFBitmap_BGRA:
49 return 4;
50 default:
51 ASSERT(false);
52 return 0;
53 }
54}
55
thestigbcd3e532016-11-21 13:37:28 -080056} // namespace
57
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -070059 : default_delegate_(new EmbedderTest::Delegate()),
60 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080061 form_handle_(nullptr),
62 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -070063 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080064 loader_(nullptr),
65 file_length_(0),
66 file_contents_(nullptr) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -080067 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepeza72e8e22015-10-07 10:17:53 -070068 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080069
Tom Sepez452b4f32015-10-13 09:27:27 -070070#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080071#ifdef V8_USE_EXTERNAL_STARTUP_DATA
thestigc08cd7a2016-06-27 09:47:59 -070072 if (g_v8_natives && g_v8_snapshot) {
73 InitializeV8ForPDFium(g_exe_path, std::string(), nullptr, nullptr,
74 &platform_);
75 } else {
76 g_v8_natives = new v8::StartupData;
77 g_v8_snapshot = new v8::StartupData;
78 InitializeV8ForPDFium(g_exe_path, std::string(), g_v8_natives,
79 g_v8_snapshot, &platform_);
80 }
Tom Sepezd831dc72015-10-19 16:04:22 -070081#else
thestigc08cd7a2016-06-27 09:47:59 -070082 InitializeV8ForPDFium(g_exe_path, &platform_);
Tom Sepez96d13342015-01-16 14:59:26 -080083#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -070084#endif // FPDF_ENABLE_V8
Nicolas Pena3ff54002017-07-05 11:55:35 -040085 FPDF_FILEWRITE::version = 1;
86 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080087}
Tom Sepez96d13342015-01-16 14:59:26 -080088
jochen38a1f0a2016-05-31 12:07:40 -070089EmbedderTest::~EmbedderTest() {
90#ifdef PDF_ENABLE_V8
91 v8::V8::ShutdownPlatform();
92 delete platform_;
93#endif // PDF_ENABLE_V8
94}
Tom Sepezf288bb12015-11-20 12:12:46 -080095
96void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070097 FPDF_LIBRARY_CONFIG config;
98 config.version = 2;
99 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700100 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -0700101 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700102 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -0800103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
105 memset(info, 0, sizeof(UNSUPPORT_INFO));
106 info->version = 1;
107 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
108 FSDK_SetUnSpObjProcessHandler(info);
109}
Tom Sepez96d13342015-01-16 14:59:26 -0800110
111void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800112 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700113 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -0800114 FPDFDOC_ExitFormFillEnvironment(form_handle_);
115 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800116 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800117
Tom Sepez96d13342015-01-16 14:59:26 -0800118 FPDFAvail_Destroy(avail_);
119 FPDF_DestroyLibrary();
Lei Zhangd27acae2015-05-15 15:36:02 -0700120 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -0800121}
122
Tom Sepezd483eb42016-01-06 10:03:59 -0800123bool EmbedderTest::CreateEmptyDocument() {
124 document_ = FPDF_CreateNewDocument();
125 if (!document_)
126 return false;
127
Nicolas Pena3ff54002017-07-05 11:55:35 -0400128 form_handle_ = SetupFormFillEnvironment(document_);
Tom Sepezd483eb42016-01-06 10:03:59 -0800129 return true;
130}
131
Jun Fangdf7f3662015-11-10 18:29:18 +0800132bool EmbedderTest::OpenDocument(const std::string& filename,
thestig27ddf162016-05-23 15:06:59 -0700133 const char* password,
Jun Fangdf7f3662015-11-10 18:29:18 +0800134 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800135 std::string file_path;
136 if (!PathService::GetTestFilePath(filename, &file_path))
137 return false;
138 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400139 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800140 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800141
thestig29ce9232016-06-22 07:03:23 -0700142 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800143 loader_ = new TestLoader(file_contents_.get(), file_length_);
Tom Sepez96d13342015-01-16 14:59:26 -0800144 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700145 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800146 file_access_.m_Param = loader_;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300147 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
148 return OpenDocumentHelper(password, must_linearize, fake_file_access_.get(),
149 &document_, &avail_, &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400150}
Tom Sepez96d13342015-01-16 14:59:26 -0800151
Nicolas Pena56fc9722017-07-13 16:31:34 -0400152bool EmbedderTest::OpenDocumentHelper(const char* password,
153 bool must_linearize,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300154 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400155 FPDF_DOCUMENT* document,
156 FPDF_AVAIL* avail,
157 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300158 network_simulator->AddSegment(0, 1024);
159 network_simulator->SetRequestedDataAvailable();
160 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
161 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400162 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300163 int32_t nRet = PDF_DATA_NOTAVAIL;
164 while (nRet == PDF_DATA_NOTAVAIL) {
165 network_simulator->SetRequestedDataAvailable();
166 nRet =
167 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
168 }
169 if (nRet == PDF_DATA_ERROR)
170 return false;
171
Nicolas Pena56fc9722017-07-13 16:31:34 -0400172 *document = FPDFAvail_GetDocument(*avail, password);
173 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800174 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400175
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300176 nRet = PDF_DATA_NOTAVAIL;
177 while (nRet == PDF_DATA_NOTAVAIL) {
178 network_simulator->SetRequestedDataAvailable();
179 nRet =
180 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
181 }
182 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800183 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400184
185 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800186 for (int i = 0; i < page_count; ++i) {
187 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300188 while (nRet == PDF_DATA_NOTAVAIL) {
189 network_simulator->SetRequestedDataAvailable();
190 nRet = FPDFAvail_IsPageAvail(*avail, i,
191 network_simulator->GetDownloadHints());
192 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400193
194 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800195 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800196 }
197 } else {
Nicolas Pena56fc9722017-07-13 16:31:34 -0400198 if (must_linearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800199 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300200 network_simulator->SetWholeFileAvailable();
201 *document =
202 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400203 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800204 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800205 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400206 *form_handle = SetupFormFillEnvironment(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800207#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400208 int doc_type = FPDF_GetFormType(*document);
209 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
210 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800211#endif // PDF_ENABLE_XFA
Nicolas Pena56fc9722017-07-13 16:31:34 -0400212 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800213 return true;
214}
Tom Sepez96d13342015-01-16 14:59:26 -0800215
Nicolas Pena3ff54002017-07-05 11:55:35 -0400216FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(FPDF_DOCUMENT doc) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800217 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400218 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200219 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800220 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400221 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800222
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800223 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
224 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800225#ifdef PDF_ENABLE_XFA
226 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800227#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800228 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800229#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700230 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
231 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700232 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800233 formfillinfo->m_pJsPlatform = platform;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400234 FPDF_FORMHANDLE form_handle =
235 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
236 FPDF_SetFormFieldHighlightColor(form_handle, 0, 0xFFE4DD);
237 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
238 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800239}
240
Tom Sepezda8189e2015-01-30 14:41:50 -0800241void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400242 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800243 FORM_DoDocumentJSAction(form_handle_);
244 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800245}
246
247int EmbedderTest::GetFirstPageNum() {
248 int first_page = FPDFAvail_GetFirstPageNum(document_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300249 (void)FPDFAvail_IsPageAvail(avail_, first_page,
250 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800251 return first_page;
252}
253
254int EmbedderTest::GetPageCount() {
255 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400256 for (int i = 0; i < page_count; ++i)
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300257 (void)FPDFAvail_IsPageAvail(avail_, i,
258 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800259 return page_count;
260}
261
Tom Sepezda8189e2015-01-30 14:41:50 -0800262FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400263 ASSERT(form_handle_);
weili0dadcc62016-08-23 21:10:57 -0700264 // First check whether it is loaded already.
265 auto it = page_map_.find(page_number);
266 if (it != page_map_.end())
267 return it->second;
268
Tom Sepez96d13342015-01-16 14:59:26 -0800269 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400270 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800271 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400272
Tom Sepezda8189e2015-01-30 14:41:50 -0800273 FORM_OnAfterLoadPage(page, form_handle_);
274 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700275 // Cache the page.
276 page_map_[page_number] = page;
dsinclaircb92dc72016-09-07 09:02:48 -0700277 page_reverse_map_[page] = page_number;
Tom Sepez396e8722015-09-09 10:16:08 -0700278 return page;
279}
280
Tom Sepezda8189e2015-01-30 14:41:50 -0800281FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400282 return RenderPageWithFlags(page, form_handle_, 0);
Jane Liuc533f4b2017-06-19 11:13:00 -0400283}
284
Jane Liubaa7ff42017-06-29 19:18:23 -0400285FPDF_BITMAP EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
286 FPDF_FORMHANDLE handle,
287 int flags) {
Tom Sepez96d13342015-01-16 14:59:26 -0800288 int width = static_cast<int>(FPDF_GetPageWidth(page));
289 int height = static_cast<int>(FPDF_GetPageHeight(page));
Lei Zhang453d96b2015-12-31 13:13:10 -0800290 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
291 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
292 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
293 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
Jane Liuc533f4b2017-06-19 11:13:00 -0400294 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, flags);
Jane Liubaa7ff42017-06-29 19:18:23 -0400295 FPDF_FFLDraw(handle, bitmap, page, 0, 0, width, height, 0, flags);
Tom Sepez96d13342015-01-16 14:59:26 -0800296 return bitmap;
297}
298
Tom Sepezda8189e2015-01-30 14:41:50 -0800299void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400300 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800301 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
302 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800303 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700304
305 auto it = page_reverse_map_.find(page);
306 if (it == page_reverse_map_.end())
307 return;
308
309 page_map_.erase(it->second);
310 page_reverse_map_.erase(it);
Tom Sepez96d13342015-01-16 14:59:26 -0800311}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800312
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400313FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300314 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
315 saved_file_access_.m_FileLen = m_String.size();
316 saved_file_access_.m_GetBlock = GetBlockFromString;
317 saved_file_access_.m_Param = &m_String;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400318
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300319 saved_fake_file_access_ =
320 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
321
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400322 EXPECT_TRUE(OpenDocumentHelper(password, false, saved_fake_file_access_.get(),
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300323 &m_SavedDocument, &m_SavedAvail,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400324 &m_SavedForm));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400325 return m_SavedDocument;
326}
327
328void EmbedderTest::CloseSavedDocument() {
329 ASSERT(!m_SavedPage);
330 ASSERT(m_SavedDocument);
331
332 FPDFDOC_ExitFormFillEnvironment(m_SavedForm);
333 FPDF_CloseDocument(m_SavedDocument);
334 FPDFAvail_Destroy(m_SavedAvail);
335
336 m_SavedForm = nullptr;
337 m_SavedDocument = nullptr;
338 m_SavedAvail = nullptr;
339}
340
341FPDF_PAGE EmbedderTest::LoadSavedPage() {
342 ASSERT(m_SavedDocument);
343
Nicolas Pena3ff54002017-07-05 11:55:35 -0400344 EXPECT_EQ(1, FPDF_GetPageCount(m_SavedDocument));
345 m_SavedPage = FPDF_LoadPage(m_SavedDocument, 0);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400346
347 ASSERT(m_SavedPage);
348 return m_SavedPage;
349}
350
351void EmbedderTest::CloseSavedPage() {
352 ASSERT(m_SavedPage);
353 FPDF_ClosePage(m_SavedPage);
354 m_SavedPage = nullptr;
355}
356
357void EmbedderTest::VerifySavedRendering(int width,
358 int height,
359 const char* md5) {
360 ASSERT(m_SavedDocument);
361 ASSERT(m_SavedPage);
362
Nicolas Pena3ff54002017-07-05 11:55:35 -0400363 FPDF_BITMAP new_bitmap =
364 RenderPageWithFlags(m_SavedPage, m_SavedForm, FPDF_ANNOT);
365 CompareBitmap(new_bitmap, width, height, md5);
366 FPDFBitmap_Destroy(new_bitmap);
367}
368
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400369void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
370 OpenSavedDocument();
371 LoadSavedPage();
372 VerifySavedRendering(width, height, md5);
373 CloseSavedPage();
374 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400375}
376
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300377void EmbedderTest::SetWholeFileAvailable() {
378 ASSERT(fake_file_access_);
379 fake_file_access_->SetWholeFileAvailable();
380}
381
weili0dadcc62016-08-23 21:10:57 -0700382FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700383 FPDF_DOCUMENT document,
384 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700385 EmbedderTest* test = static_cast<EmbedderTest*>(info);
386 auto it = test->page_map_.find(page_index);
387 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700388}
389
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800390// static
391void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
392 int type) {
393 EmbedderTest* test = static_cast<EmbedderTest*>(info);
394 test->delegate_->UnsupportedHandler(type);
395}
396
397// static
398int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
399 FPDF_WIDESTRING message,
400 FPDF_WIDESTRING title,
401 int type,
402 int icon) {
403 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
404 return test->delegate_->Alert(message, title, type, icon);
405}
406
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700407// static
408int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 int msecs,
410 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700411 EmbedderTest* test = static_cast<EmbedderTest*>(info);
412 return test->delegate_->SetTimer(msecs, fn);
413}
414
415// static
416void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
417 EmbedderTest* test = static_cast<EmbedderTest*>(info);
418 return test->delegate_->KillTimer(id);
419}
420
Tom Sepez396e8722015-09-09 10:16:08 -0700421// static
422FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
423 FPDF_DOCUMENT document,
424 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700425 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
426 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700427}
428
Dan Sinclair957480c2017-06-13 15:21:14 -0400429std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap,
430 int expected_width,
431 int expected_height) {
432 uint8_t digest[16];
Jane Liu28fb7ba2017-08-02 21:45:57 -0400433 CRYPT_MD5Generate(
434 static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
435 expected_width * GetBitmapBytesPerPixel(bitmap) * expected_height,
436 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400437 return CryptToBase16(digest);
438}
439
thestigbcd3e532016-11-21 13:37:28 -0800440// static
441void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
442 int expected_width,
443 int expected_height,
444 const char* expected_md5sum) {
445 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
446 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400447
448 // The expected stride is calculated using the same formula as in
449 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
450 const int expected_stride =
451 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800452 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
453
454 if (!expected_md5sum)
455 return;
456
Dan Sinclair957480c2017-06-13 15:21:14 -0400457 EXPECT_EQ(expected_md5sum,
458 HashBitmap(bitmap, expected_width, expected_height));
thestigbcd3e532016-11-21 13:37:28 -0800459}
460
Nicolas Pena3ff54002017-07-05 11:55:35 -0400461// static
462int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
463 const void* data,
464 unsigned long size) {
465 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
466 pThis->m_String.append(static_cast<const char*>(data), size);
467 return 1;
468}
469
470// static
471int EmbedderTest::GetBlockFromString(void* param,
472 unsigned long pos,
473 unsigned char* buf,
474 unsigned long size) {
475 std::string* new_file = static_cast<std::string*>(param);
476 if (!new_file || pos + size < pos)
477 return 0;
478
479 unsigned long file_size = new_file->size();
480 if (pos + size > file_size)
481 return 0;
482
483 memcpy(buf, new_file->data() + pos, size);
484 return 1;
485}
486
Tom Sepez1b1bb492015-01-22 17:36:32 -0800487// Can't use gtest-provided main since we need to stash the path to the
488// executable in order to find the external V8 binary data files.
489int main(int argc, char** argv) {
thestigc08cd7a2016-06-27 09:47:59 -0700490 g_exe_path = argv[0];
Tom Sepez1b1bb492015-01-22 17:36:32 -0800491 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800492 testing::InitGoogleMock(&argc, argv);
thestigc08cd7a2016-06-27 09:47:59 -0700493 int ret_val = RUN_ALL_TESTS();
494
495#ifdef PDF_ENABLE_V8
496#ifdef V8_USE_EXTERNAL_STARTUP_DATA
497 if (g_v8_natives)
498 free(const_cast<char*>(g_v8_natives->data));
499 if (g_v8_snapshot)
500 free(const_cast<char*>(g_v8_snapshot->data));
501#endif // V8_USE_EXTERNAL_STARTUP_DATA
502#endif // PDF_ENABLE_V8
503
504 return ret_val;
Tom Sepez1b1bb492015-01-22 17:36:32 -0800505}