blob: bb202bff1c87ab9b7665c8c9033c07f1b6a7314d [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);
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000109
110 m_SavedDocument = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111}
Tom Sepez96d13342015-01-16 14:59:26 -0800112
113void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800114 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700115 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -0800116 FPDFDOC_ExitFormFillEnvironment(form_handle_);
117 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800118 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800119
Tom Sepez96d13342015-01-16 14:59:26 -0800120 FPDFAvail_Destroy(avail_);
121 FPDF_DestroyLibrary();
Lei Zhangd27acae2015-05-15 15:36:02 -0700122 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -0800123}
124
Tom Sepezd483eb42016-01-06 10:03:59 -0800125bool EmbedderTest::CreateEmptyDocument() {
126 document_ = FPDF_CreateNewDocument();
127 if (!document_)
128 return false;
129
Nicolas Pena3ff54002017-07-05 11:55:35 -0400130 form_handle_ = SetupFormFillEnvironment(document_);
Tom Sepezd483eb42016-01-06 10:03:59 -0800131 return true;
132}
133
Jun Fangdf7f3662015-11-10 18:29:18 +0800134bool EmbedderTest::OpenDocument(const std::string& filename,
thestig27ddf162016-05-23 15:06:59 -0700135 const char* password,
Jun Fangdf7f3662015-11-10 18:29:18 +0800136 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800137 std::string file_path;
138 if (!PathService::GetTestFilePath(filename, &file_path))
139 return false;
140 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400141 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800142 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800143
thestig29ce9232016-06-22 07:03:23 -0700144 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800145 loader_ = new TestLoader(file_contents_.get(), file_length_);
Tom Sepez96d13342015-01-16 14:59:26 -0800146 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700147 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800148 file_access_.m_Param = loader_;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300149 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
150 return OpenDocumentHelper(password, must_linearize, fake_file_access_.get(),
151 &document_, &avail_, &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400152}
Tom Sepez96d13342015-01-16 14:59:26 -0800153
Nicolas Pena56fc9722017-07-13 16:31:34 -0400154bool EmbedderTest::OpenDocumentHelper(const char* password,
155 bool must_linearize,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300156 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400157 FPDF_DOCUMENT* document,
158 FPDF_AVAIL* avail,
159 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300160 network_simulator->AddSegment(0, 1024);
161 network_simulator->SetRequestedDataAvailable();
162 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
163 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400164 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300165 int32_t nRet = PDF_DATA_NOTAVAIL;
166 while (nRet == PDF_DATA_NOTAVAIL) {
167 network_simulator->SetRequestedDataAvailable();
168 nRet =
169 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
170 }
171 if (nRet == PDF_DATA_ERROR)
172 return false;
173
Nicolas Pena56fc9722017-07-13 16:31:34 -0400174 *document = FPDFAvail_GetDocument(*avail, password);
175 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800176 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400177
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300178 nRet = PDF_DATA_NOTAVAIL;
179 while (nRet == PDF_DATA_NOTAVAIL) {
180 network_simulator->SetRequestedDataAvailable();
181 nRet =
182 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
183 }
184 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800185 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400186
187 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800188 for (int i = 0; i < page_count; ++i) {
189 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300190 while (nRet == PDF_DATA_NOTAVAIL) {
191 network_simulator->SetRequestedDataAvailable();
192 nRet = FPDFAvail_IsPageAvail(*avail, i,
193 network_simulator->GetDownloadHints());
194 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400195
196 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800197 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800198 }
199 } else {
Nicolas Pena56fc9722017-07-13 16:31:34 -0400200 if (must_linearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800201 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300202 network_simulator->SetWholeFileAvailable();
203 *document =
204 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400205 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800206 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800207 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400208 *form_handle = SetupFormFillEnvironment(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800209#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400210 int doc_type = FPDF_GetFormType(*document);
211 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
212 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800213#endif // PDF_ENABLE_XFA
Nicolas Pena56fc9722017-07-13 16:31:34 -0400214 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800215 return true;
216}
Tom Sepez96d13342015-01-16 14:59:26 -0800217
Nicolas Pena3ff54002017-07-05 11:55:35 -0400218FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(FPDF_DOCUMENT doc) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800219 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400220 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200221 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800222 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400223 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800224
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800225 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
226 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800227#ifdef PDF_ENABLE_XFA
228 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800229#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800230 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800231#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700232 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
233 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700234 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800235 formfillinfo->m_pJsPlatform = platform;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400236 FPDF_FORMHANDLE form_handle =
237 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
238 FPDF_SetFormFieldHighlightColor(form_handle, 0, 0xFFE4DD);
239 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
240 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800241}
242
Tom Sepezda8189e2015-01-30 14:41:50 -0800243void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400244 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800245 FORM_DoDocumentJSAction(form_handle_);
246 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800247}
248
249int EmbedderTest::GetFirstPageNum() {
250 int first_page = FPDFAvail_GetFirstPageNum(document_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300251 (void)FPDFAvail_IsPageAvail(avail_, first_page,
252 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800253 return first_page;
254}
255
256int EmbedderTest::GetPageCount() {
257 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400258 for (int i = 0; i < page_count; ++i)
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300259 (void)FPDFAvail_IsPageAvail(avail_, i,
260 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800261 return page_count;
262}
263
Tom Sepezda8189e2015-01-30 14:41:50 -0800264FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400265 ASSERT(form_handle_);
weili0dadcc62016-08-23 21:10:57 -0700266 // First check whether it is loaded already.
267 auto it = page_map_.find(page_number);
268 if (it != page_map_.end())
269 return it->second;
270
Tom Sepez96d13342015-01-16 14:59:26 -0800271 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400272 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800273 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400274
Tom Sepezda8189e2015-01-30 14:41:50 -0800275 FORM_OnAfterLoadPage(page, form_handle_);
276 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700277 // Cache the page.
278 page_map_[page_number] = page;
dsinclaircb92dc72016-09-07 09:02:48 -0700279 page_reverse_map_[page] = page_number;
Tom Sepez396e8722015-09-09 10:16:08 -0700280 return page;
281}
282
Tom Sepezda8189e2015-01-30 14:41:50 -0800283FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400284 return RenderPageWithFlags(page, form_handle_, 0);
Jane Liuc533f4b2017-06-19 11:13:00 -0400285}
286
Jane Liubaa7ff42017-06-29 19:18:23 -0400287FPDF_BITMAP EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
288 FPDF_FORMHANDLE handle,
289 int flags) {
Tom Sepez96d13342015-01-16 14:59:26 -0800290 int width = static_cast<int>(FPDF_GetPageWidth(page));
291 int height = static_cast<int>(FPDF_GetPageHeight(page));
Lei Zhang453d96b2015-12-31 13:13:10 -0800292 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
293 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
294 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
295 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
Jane Liuc533f4b2017-06-19 11:13:00 -0400296 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, flags);
Jane Liubaa7ff42017-06-29 19:18:23 -0400297 FPDF_FFLDraw(handle, bitmap, page, 0, 0, width, height, 0, flags);
Tom Sepez96d13342015-01-16 14:59:26 -0800298 return bitmap;
299}
300
Tom Sepezda8189e2015-01-30 14:41:50 -0800301void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400302 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800303 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
304 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800305 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700306
307 auto it = page_reverse_map_.find(page);
308 if (it == page_reverse_map_.end())
309 return;
310
311 page_map_.erase(it->second);
312 page_reverse_map_.erase(it);
Tom Sepez96d13342015-01-16 14:59:26 -0800313}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800314
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400315FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300316 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
317 saved_file_access_.m_FileLen = m_String.size();
318 saved_file_access_.m_GetBlock = GetBlockFromString;
319 saved_file_access_.m_Param = &m_String;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400320
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300321 saved_fake_file_access_ =
322 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
323
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400324 EXPECT_TRUE(OpenDocumentHelper(password, false, saved_fake_file_access_.get(),
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300325 &m_SavedDocument, &m_SavedAvail,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400326 &m_SavedForm));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400327 return m_SavedDocument;
328}
329
330void EmbedderTest::CloseSavedDocument() {
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400331 ASSERT(m_SavedDocument);
332
333 FPDFDOC_ExitFormFillEnvironment(m_SavedForm);
334 FPDF_CloseDocument(m_SavedDocument);
335 FPDFAvail_Destroy(m_SavedAvail);
336
337 m_SavedForm = nullptr;
338 m_SavedDocument = nullptr;
339 m_SavedAvail = nullptr;
340}
341
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000342FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400343 ASSERT(m_SavedDocument);
344
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000345 EXPECT_LT(page_number, FPDF_GetPageCount(m_SavedDocument));
346 FPDF_PAGE page = FPDF_LoadPage(m_SavedDocument, page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400347
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000348 ASSERT(page);
349 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400350}
351
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000352FPDF_BITMAP EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
353 return RenderPageWithFlags(page, m_SavedForm, 0);
354}
355
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000356void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
357 ASSERT(page);
358 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400359}
360
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000361void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
362 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400363 int height,
364 const char* md5) {
365 ASSERT(m_SavedDocument);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000366 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400367
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000368 FPDF_BITMAP new_bitmap = RenderPageWithFlags(page, m_SavedForm, FPDF_ANNOT);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400369 CompareBitmap(new_bitmap, width, height, md5);
370 FPDFBitmap_Destroy(new_bitmap);
371}
372
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400373void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
374 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000375 FPDF_PAGE page = LoadSavedPage(0);
376 VerifySavedRendering(page, width, height, md5);
377 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400378 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400379}
380
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300381void EmbedderTest::SetWholeFileAvailable() {
382 ASSERT(fake_file_access_);
383 fake_file_access_->SetWholeFileAvailable();
384}
385
weili0dadcc62016-08-23 21:10:57 -0700386FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700387 FPDF_DOCUMENT document,
388 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700389 EmbedderTest* test = static_cast<EmbedderTest*>(info);
390 auto it = test->page_map_.find(page_index);
391 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700392}
393
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800394// static
395void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
396 int type) {
397 EmbedderTest* test = static_cast<EmbedderTest*>(info);
398 test->delegate_->UnsupportedHandler(type);
399}
400
401// static
402int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
403 FPDF_WIDESTRING message,
404 FPDF_WIDESTRING title,
405 int type,
406 int icon) {
407 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
408 return test->delegate_->Alert(message, title, type, icon);
409}
410
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700411// static
412int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413 int msecs,
414 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700415 EmbedderTest* test = static_cast<EmbedderTest*>(info);
416 return test->delegate_->SetTimer(msecs, fn);
417}
418
419// static
420void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
421 EmbedderTest* test = static_cast<EmbedderTest*>(info);
422 return test->delegate_->KillTimer(id);
423}
424
Tom Sepez396e8722015-09-09 10:16:08 -0700425// static
426FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
427 FPDF_DOCUMENT document,
428 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700429 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
430 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700431}
432
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000433// static
434std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400435 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000436 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
437 FPDFBitmap_GetWidth(bitmap) *
438 GetBitmapBytesPerPixel(bitmap) *
439 FPDFBitmap_GetHeight(bitmap),
440 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400441 return CryptToBase16(digest);
442}
443
thestigbcd3e532016-11-21 13:37:28 -0800444// static
445void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
446 int expected_width,
447 int expected_height,
448 const char* expected_md5sum) {
449 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
450 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400451
452 // The expected stride is calculated using the same formula as in
453 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
454 const int expected_stride =
455 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800456 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
457
458 if (!expected_md5sum)
459 return;
460
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000461 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800462}
463
Nicolas Pena3ff54002017-07-05 11:55:35 -0400464// static
465int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
466 const void* data,
467 unsigned long size) {
468 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
469 pThis->m_String.append(static_cast<const char*>(data), size);
470 return 1;
471}
472
473// static
474int EmbedderTest::GetBlockFromString(void* param,
475 unsigned long pos,
476 unsigned char* buf,
477 unsigned long size) {
478 std::string* new_file = static_cast<std::string*>(param);
479 if (!new_file || pos + size < pos)
480 return 0;
481
482 unsigned long file_size = new_file->size();
483 if (pos + size > file_size)
484 return 0;
485
486 memcpy(buf, new_file->data() + pos, size);
487 return 1;
488}
489
Tom Sepez1b1bb492015-01-22 17:36:32 -0800490// Can't use gtest-provided main since we need to stash the path to the
491// executable in order to find the external V8 binary data files.
492int main(int argc, char** argv) {
thestigc08cd7a2016-06-27 09:47:59 -0700493 g_exe_path = argv[0];
Tom Sepez1b1bb492015-01-22 17:36:32 -0800494 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800495 testing::InitGoogleMock(&argc, argv);
thestigc08cd7a2016-06-27 09:47:59 -0700496 int ret_val = RUN_ALL_TESTS();
497
498#ifdef PDF_ENABLE_V8
499#ifdef V8_USE_EXTERNAL_STARTUP_DATA
500 if (g_v8_natives)
501 free(const_cast<char*>(g_v8_natives->data));
502 if (g_v8_snapshot)
503 free(const_cast<char*>(g_v8_snapshot->data));
504#endif // V8_USE_EXTERNAL_STARTUP_DATA
505#endif // PDF_ENABLE_V8
506
507 return ret_val;
Tom Sepez1b1bb492015-01-22 17:36:32 -0800508}