blob: b1207fba8267fe977b2234be9d9700b58cbda58b [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
Lei Zhangb4e7f302015-11-06 15:52:32 -080014#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080015#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080016#include "public/fpdf_text.h"
17#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080018#include "testing/gmock/include/gmock/gmock.h"
Wei Li091f7a02015-11-09 12:09:55 -080019#include "testing/test_support.h"
20#include "testing/utils/path_service.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070021
22#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080023#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050024#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070025#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080026
Tom Sepez96d13342015-01-16 14:59:26 -080027namespace {
thestigc08cd7a2016-06-27 09:47:59 -070028
29const char* g_exe_path = nullptr;
30
31#ifdef PDF_ENABLE_V8
32#ifdef V8_USE_EXTERNAL_STARTUP_DATA
33v8::StartupData* g_v8_natives = nullptr;
34v8::StartupData* g_v8_snapshot = nullptr;
35#endif // V8_USE_EXTERNAL_STARTUP_DATA
36#endif // PDF_ENABLE_V8
37
Tom Sepez96d13342015-01-16 14:59:26 -080038} // namespace
39
Tom Sepezcf22eb82015-05-12 17:28:08 -070040FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
Tom Sepez96d13342015-01-16 14:59:26 -080041 return true;
42}
43
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {}
Tom Sepez96d13342015-01-16 14:59:26 -080045
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -070047 : default_delegate_(new EmbedderTest::Delegate()),
48 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080049 form_handle_(nullptr),
50 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -070051 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080052 loader_(nullptr),
53 file_length_(0),
54 file_contents_(nullptr) {
55 memset(&hints_, 0, sizeof(hints_));
56 memset(&file_access_, 0, sizeof(file_access_));
57 memset(&file_avail_, 0, sizeof(file_avail_));
Tom Sepeza72e8e22015-10-07 10:17:53 -070058 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080059
Tom Sepez452b4f32015-10-13 09:27:27 -070060#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080061#ifdef V8_USE_EXTERNAL_STARTUP_DATA
thestigc08cd7a2016-06-27 09:47:59 -070062 if (g_v8_natives && g_v8_snapshot) {
63 InitializeV8ForPDFium(g_exe_path, std::string(), nullptr, nullptr,
64 &platform_);
65 } else {
66 g_v8_natives = new v8::StartupData;
67 g_v8_snapshot = new v8::StartupData;
68 InitializeV8ForPDFium(g_exe_path, std::string(), g_v8_natives,
69 g_v8_snapshot, &platform_);
70 }
Tom Sepezd831dc72015-10-19 16:04:22 -070071#else
thestigc08cd7a2016-06-27 09:47:59 -070072 InitializeV8ForPDFium(g_exe_path, &platform_);
Tom Sepez96d13342015-01-16 14:59:26 -080073#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -070074#endif // FPDF_ENABLE_V8
Tom Sepezf288bb12015-11-20 12:12:46 -080075}
Tom Sepez96d13342015-01-16 14:59:26 -080076
jochen38a1f0a2016-05-31 12:07:40 -070077EmbedderTest::~EmbedderTest() {
78#ifdef PDF_ENABLE_V8
79 v8::V8::ShutdownPlatform();
80 delete platform_;
81#endif // PDF_ENABLE_V8
82}
Tom Sepezf288bb12015-11-20 12:12:46 -080083
84void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070085 FPDF_LIBRARY_CONFIG config;
86 config.version = 2;
87 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070088 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070089 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070090 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080091
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
93 memset(info, 0, sizeof(UNSUPPORT_INFO));
94 info->version = 1;
95 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
96 FSDK_SetUnSpObjProcessHandler(info);
97}
Tom Sepez96d13342015-01-16 14:59:26 -080098
99void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800100 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700101 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -0800102#ifdef PDF_ENABLE_XFA
Lei Zhangba026912015-07-16 10:06:11 -0700103 // Note: The shut down order here is the reverse of the non-XFA branch
104 // order. Need to work out if this is required, and if it is, the lifetimes
105 // of objects owned by |doc| that |form| reference.
Tom Sepezda8189e2015-01-30 14:41:50 -0800106 FPDF_CloseDocument(document_);
Lei Zhangd27acae2015-05-15 15:36:02 -0700107 FPDFDOC_ExitFormFillEnvironment(form_handle_);
Tom Sepezc46d0002015-11-30 15:46:36 -0800108#else // PDF_ENABLE_XFA
109 FPDFDOC_ExitFormFillEnvironment(form_handle_);
110 FPDF_CloseDocument(document_);
111#endif // PDF_ENABLE_XFA
Tom Sepezda8189e2015-01-30 14:41:50 -0800112 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800113
Tom Sepez96d13342015-01-16 14:59:26 -0800114 FPDFAvail_Destroy(avail_);
115 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700116
Lei Zhangd27acae2015-05-15 15:36:02 -0700117 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -0800118}
119
Tom Sepezd483eb42016-01-06 10:03:59 -0800120bool EmbedderTest::CreateEmptyDocument() {
121 document_ = FPDF_CreateNewDocument();
122 if (!document_)
123 return false;
124
125 SetupFormFillEnvironment();
126 return true;
127}
128
Jun Fangdf7f3662015-11-10 18:29:18 +0800129bool EmbedderTest::OpenDocument(const std::string& filename,
thestig27ddf162016-05-23 15:06:59 -0700130 const char* password,
Jun Fangdf7f3662015-11-10 18:29:18 +0800131 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800132 std::string file_path;
133 if (!PathService::GetTestFilePath(filename, &file_path))
134 return false;
135 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400136 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800137 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800138
thestig29ce9232016-06-22 07:03:23 -0700139 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800140 loader_ = new TestLoader(file_contents_.get(), file_length_);
Tom Sepez96d13342015-01-16 14:59:26 -0800141 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700142 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800143 file_access_.m_Param = loader_;
144
145 file_avail_.version = 1;
146 file_avail_.IsDataAvail = Is_Data_Avail;
147
148 hints_.version = 1;
149 hints_.AddSegment = Add_Segment;
150
151 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
Tom Sepez96d13342015-01-16 14:59:26 -0800152
Jun Fangdf7f3662015-11-10 18:29:18 +0800153 if (FPDFAvail_IsLinearized(avail_) == PDF_LINEARIZED) {
thestig27ddf162016-05-23 15:06:59 -0700154 document_ = FPDFAvail_GetDocument(avail_, password);
Jun Fangdf7f3662015-11-10 18:29:18 +0800155 if (!document_) {
156 return false;
157 }
158 int32_t nRet = PDF_DATA_NOTAVAIL;
159 while (nRet == PDF_DATA_NOTAVAIL) {
160 nRet = FPDFAvail_IsDocAvail(avail_, &hints_);
161 }
162 if (nRet == PDF_DATA_ERROR) {
163 return false;
164 }
165 nRet = FPDFAvail_IsFormAvail(avail_, &hints_);
166 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
167 return false;
168 }
169 int page_count = FPDF_GetPageCount(document_);
170 for (int i = 0; i < page_count; ++i) {
171 nRet = PDF_DATA_NOTAVAIL;
172 while (nRet == PDF_DATA_NOTAVAIL) {
173 nRet = FPDFAvail_IsPageAvail(avail_, i, &hints_);
174 }
175 if (nRet == PDF_DATA_ERROR) {
176 return false;
177 }
178 }
179 } else {
180 if (must_linearize) {
181 return false;
182 }
183 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
184 if (!document_) {
185 return false;
186 }
187 }
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400188
Tom Sepezc46d0002015-11-30 15:46:36 -0800189#ifdef PDF_ENABLE_XFA
JUN FANG827a1722015-03-05 13:39:21 -0800190 int docType = DOCTYPE_PDF;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 if (FPDF_HasXFAField(document_, &docType)) {
JUN FANG827a1722015-03-05 13:39:21 -0800192 if (docType != DOCTYPE_PDF)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 (void)FPDF_LoadXFA(document_);
JUN FANG827a1722015-03-05 13:39:21 -0800194 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800195#endif // PDF_ENABLE_XFA
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400196
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 (void)FPDF_GetDocPermissions(document_);
Tom Sepezd483eb42016-01-06 10:03:59 -0800198 SetupFormFillEnvironment();
199 return true;
200}
Tom Sepez96d13342015-01-16 14:59:26 -0800201
Tom Sepezd483eb42016-01-06 10:03:59 -0800202void EmbedderTest::SetupFormFillEnvironment() {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800203 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
204 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;
Tom Sepez96d13342015-01-16 14:59:26 -0800207
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800208 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
209 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800210#ifdef PDF_ENABLE_XFA
211 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800212#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800213 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800214#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700215 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
216 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700217 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800218 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800219
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800220 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800221 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
222 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
Tom Sepez96d13342015-01-16 14:59:26 -0800223}
224
Tom Sepezda8189e2015-01-30 14:41:50 -0800225void EmbedderTest::DoOpenActions() {
226 FORM_DoDocumentJSAction(form_handle_);
227 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800228}
229
230int EmbedderTest::GetFirstPageNum() {
231 int first_page = FPDFAvail_GetFirstPageNum(document_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800233 return first_page;
234}
235
236int EmbedderTest::GetPageCount() {
237 int page_count = FPDF_GetPageCount(document_);
238 for (int i = 0; i < page_count; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239 (void)FPDFAvail_IsPageAvail(avail_, i, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800240 }
241 return page_count;
242}
243
Tom Sepezda8189e2015-01-30 14:41:50 -0800244FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez96d13342015-01-16 14:59:26 -0800245 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
246 if (!page) {
247 return nullptr;
248 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800249 FORM_OnAfterLoadPage(page, form_handle_);
250 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
Tom Sepez96d13342015-01-16 14:59:26 -0800251 return page;
252}
253
Tom Sepez396e8722015-09-09 10:16:08 -0700254FPDF_PAGE EmbedderTest::LoadAndCachePage(int page_number) {
255 FPDF_PAGE page = delegate_->GetPage(form_handle_, document_, page_number);
256 if (!page) {
257 return nullptr;
258 }
259 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
260 return page;
261}
262
Tom Sepezda8189e2015-01-30 14:41:50 -0800263FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800264 int width = static_cast<int>(FPDF_GetPageWidth(page));
265 int height = static_cast<int>(FPDF_GetPageHeight(page));
Lei Zhang453d96b2015-12-31 13:13:10 -0800266 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
267 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
268 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
269 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
Tom Sepez96d13342015-01-16 14:59:26 -0800270 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800271 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800272 return bitmap;
273}
274
Tom Sepezda8189e2015-01-30 14:41:50 -0800275void EmbedderTest::UnloadPage(FPDF_PAGE page) {
276 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
277 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800278 FPDF_ClosePage(page);
279}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800280
Tom Sepez396e8722015-09-09 10:16:08 -0700281FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMHANDLE form_handle,
282 FPDF_DOCUMENT document,
283 int page_index) {
284 auto it = m_pageMap.find(page_index);
285 if (it != m_pageMap.end()) {
286 return it->second;
287 }
288 FPDF_PAGE page = FPDF_LoadPage(document, page_index);
289 if (!page) {
290 return nullptr;
291 }
292 m_pageMap[page_index] = page;
293 FORM_OnAfterLoadPage(page, form_handle);
294 return page;
295}
296
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800297// static
298void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
299 int type) {
300 EmbedderTest* test = static_cast<EmbedderTest*>(info);
301 test->delegate_->UnsupportedHandler(type);
302}
303
304// static
305int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
306 FPDF_WIDESTRING message,
307 FPDF_WIDESTRING title,
308 int type,
309 int icon) {
310 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
311 return test->delegate_->Alert(message, title, type, icon);
312}
313
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700314// static
315int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 int msecs,
317 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700318 EmbedderTest* test = static_cast<EmbedderTest*>(info);
319 return test->delegate_->SetTimer(msecs, fn);
320}
321
322// static
323void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
324 EmbedderTest* test = static_cast<EmbedderTest*>(info);
325 return test->delegate_->KillTimer(id);
326}
327
Tom Sepez396e8722015-09-09 10:16:08 -0700328// static
329FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
330 FPDF_DOCUMENT document,
331 int page_index) {
332 EmbedderTest* test = static_cast<EmbedderTest*>(info);
Tom Sepez436977e2015-10-02 09:16:40 -0700333 return test->delegate_->GetPage(test->form_handle(), document, page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700334}
335
Tom Sepez1b1bb492015-01-22 17:36:32 -0800336// Can't use gtest-provided main since we need to stash the path to the
337// executable in order to find the external V8 binary data files.
338int main(int argc, char** argv) {
thestigc08cd7a2016-06-27 09:47:59 -0700339 g_exe_path = argv[0];
Tom Sepez1b1bb492015-01-22 17:36:32 -0800340 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800341 testing::InitGoogleMock(&argc, argv);
thestigc08cd7a2016-06-27 09:47:59 -0700342 int ret_val = RUN_ALL_TESTS();
343
344#ifdef PDF_ENABLE_V8
345#ifdef V8_USE_EXTERNAL_STARTUP_DATA
346 if (g_v8_natives)
347 free(const_cast<char*>(g_v8_natives->data));
348 if (g_v8_snapshot)
349 free(const_cast<char*>(g_v8_snapshot->data));
350#endif // V8_USE_EXTERNAL_STARTUP_DATA
351#endif // PDF_ENABLE_V8
352
353 return ret_val;
Tom Sepez1b1bb492015-01-22 17:36:32 -0800354}