blob: 033313dbe24a0519350ef3650536cc042b67e727 [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 {
Tom Sepez1b1bb492015-01-22 17:36:32 -080028const char* g_exe_path_ = nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080029} // namespace
30
Tom Sepezcf22eb82015-05-12 17:28:08 -070031FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
Tom Sepez96d13342015-01-16 14:59:26 -080032 return true;
33}
34
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {}
Tom Sepez96d13342015-01-16 14:59:26 -080036
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -070038 : default_delegate_(new EmbedderTest::Delegate()),
39 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080040 form_handle_(nullptr),
41 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -070042 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080043 loader_(nullptr),
44 file_length_(0),
45 file_contents_(nullptr) {
46 memset(&hints_, 0, sizeof(hints_));
47 memset(&file_access_, 0, sizeof(file_access_));
48 memset(&file_avail_, 0, sizeof(file_avail_));
Tom Sepeza72e8e22015-10-07 10:17:53 -070049 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080050
Tom Sepez452b4f32015-10-13 09:27:27 -070051#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080052#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepezd831dc72015-10-19 16:04:22 -070053 InitializeV8ForPDFium(g_exe_path_, std::string(), &natives_, &snapshot_,
54 &platform_);
55#else
56 InitializeV8ForPDFium(&platform_);
Tom Sepez96d13342015-01-16 14:59:26 -080057#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -070058#endif // FPDF_ENABLE_V8
Tom Sepezf288bb12015-11-20 12:12:46 -080059}
Tom Sepez96d13342015-01-16 14:59:26 -080060
Tom Sepezf288bb12015-11-20 12:12:46 -080061EmbedderTest::~EmbedderTest() {}
62
63void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070064 FPDF_LIBRARY_CONFIG config;
65 config.version = 2;
66 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070067 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070068 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070069 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080070
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
72 memset(info, 0, sizeof(UNSUPPORT_INFO));
73 info->version = 1;
74 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
75 FSDK_SetUnSpObjProcessHandler(info);
76}
Tom Sepez96d13342015-01-16 14:59:26 -080077
78void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -080079 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -070080 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -080081#ifdef PDF_ENABLE_XFA
Lei Zhangba026912015-07-16 10:06:11 -070082 // Note: The shut down order here is the reverse of the non-XFA branch
83 // order. Need to work out if this is required, and if it is, the lifetimes
84 // of objects owned by |doc| that |form| reference.
Tom Sepezda8189e2015-01-30 14:41:50 -080085 FPDF_CloseDocument(document_);
Lei Zhangd27acae2015-05-15 15:36:02 -070086 FPDFDOC_ExitFormFillEnvironment(form_handle_);
Tom Sepezc46d0002015-11-30 15:46:36 -080087#else // PDF_ENABLE_XFA
88 FPDFDOC_ExitFormFillEnvironment(form_handle_);
89 FPDF_CloseDocument(document_);
90#endif // PDF_ENABLE_XFA
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();
Tom Sepez452b4f32015-10-13 09:27:27 -070095
96#ifdef PDF_ENABLE_V8
Tom Sepez6efc0ad2015-06-02 17:11:18 -070097 v8::V8::ShutdownPlatform();
98 delete platform_;
Tom Sepez452b4f32015-10-13 09:27:27 -070099#endif // PDF_ENABLE_V8
100
Lei Zhangd27acae2015-05-15 15:36:02 -0700101 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -0800102}
103
Tom Sepezd483eb42016-01-06 10:03:59 -0800104bool EmbedderTest::CreateEmptyDocument() {
105 document_ = FPDF_CreateNewDocument();
106 if (!document_)
107 return false;
108
109 SetupFormFillEnvironment();
110 return true;
111}
112
Jun Fangdf7f3662015-11-10 18:29:18 +0800113bool EmbedderTest::OpenDocument(const std::string& filename,
thestig27ddf162016-05-23 15:06:59 -0700114 const char* password,
Jun Fangdf7f3662015-11-10 18:29:18 +0800115 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800116 std::string file_path;
117 if (!PathService::GetTestFilePath(filename, &file_path))
118 return false;
119 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400120 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800121 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800122
Tom Sepez0aa35312016-01-06 10:16:32 -0800123 loader_ = new TestLoader(file_contents_.get(), file_length_);
Tom Sepez96d13342015-01-16 14:59:26 -0800124 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700125 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800126 file_access_.m_Param = loader_;
127
128 file_avail_.version = 1;
129 file_avail_.IsDataAvail = Is_Data_Avail;
130
131 hints_.version = 1;
132 hints_.AddSegment = Add_Segment;
133
134 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
Tom Sepez96d13342015-01-16 14:59:26 -0800135
Jun Fangdf7f3662015-11-10 18:29:18 +0800136 if (FPDFAvail_IsLinearized(avail_) == PDF_LINEARIZED) {
thestig27ddf162016-05-23 15:06:59 -0700137 document_ = FPDFAvail_GetDocument(avail_, password);
Jun Fangdf7f3662015-11-10 18:29:18 +0800138 if (!document_) {
139 return false;
140 }
141 int32_t nRet = PDF_DATA_NOTAVAIL;
142 while (nRet == PDF_DATA_NOTAVAIL) {
143 nRet = FPDFAvail_IsDocAvail(avail_, &hints_);
144 }
145 if (nRet == PDF_DATA_ERROR) {
146 return false;
147 }
148 nRet = FPDFAvail_IsFormAvail(avail_, &hints_);
149 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
150 return false;
151 }
152 int page_count = FPDF_GetPageCount(document_);
153 for (int i = 0; i < page_count; ++i) {
154 nRet = PDF_DATA_NOTAVAIL;
155 while (nRet == PDF_DATA_NOTAVAIL) {
156 nRet = FPDFAvail_IsPageAvail(avail_, i, &hints_);
157 }
158 if (nRet == PDF_DATA_ERROR) {
159 return false;
160 }
161 }
162 } else {
163 if (must_linearize) {
164 return false;
165 }
166 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
167 if (!document_) {
168 return false;
169 }
170 }
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400171
Tom Sepezc46d0002015-11-30 15:46:36 -0800172#ifdef PDF_ENABLE_XFA
JUN FANG827a1722015-03-05 13:39:21 -0800173 int docType = DOCTYPE_PDF;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 if (FPDF_HasXFAField(document_, &docType)) {
JUN FANG827a1722015-03-05 13:39:21 -0800175 if (docType != DOCTYPE_PDF)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 (void)FPDF_LoadXFA(document_);
JUN FANG827a1722015-03-05 13:39:21 -0800177 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800178#endif // PDF_ENABLE_XFA
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400179
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 (void)FPDF_GetDocPermissions(document_);
Tom Sepezd483eb42016-01-06 10:03:59 -0800181 SetupFormFillEnvironment();
182 return true;
183}
Tom Sepez96d13342015-01-16 14:59:26 -0800184
Tom Sepezd483eb42016-01-06 10:03:59 -0800185void EmbedderTest::SetupFormFillEnvironment() {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800186 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
187 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200188 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800189 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800190
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800191 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
192 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800193#ifdef PDF_ENABLE_XFA
194 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800195#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800196 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800197#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700198 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
199 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700200 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800201 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800202
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800203 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800204 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
205 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
Tom Sepez96d13342015-01-16 14:59:26 -0800206}
207
Tom Sepezda8189e2015-01-30 14:41:50 -0800208void EmbedderTest::DoOpenActions() {
209 FORM_DoDocumentJSAction(form_handle_);
210 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800211}
212
213int EmbedderTest::GetFirstPageNum() {
214 int first_page = FPDFAvail_GetFirstPageNum(document_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800216 return first_page;
217}
218
219int EmbedderTest::GetPageCount() {
220 int page_count = FPDF_GetPageCount(document_);
221 for (int i = 0; i < page_count; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 (void)FPDFAvail_IsPageAvail(avail_, i, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800223 }
224 return page_count;
225}
226
Tom Sepezda8189e2015-01-30 14:41:50 -0800227FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez96d13342015-01-16 14:59:26 -0800228 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
229 if (!page) {
230 return nullptr;
231 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800232 FORM_OnAfterLoadPage(page, form_handle_);
233 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
Tom Sepez96d13342015-01-16 14:59:26 -0800234 return page;
235}
236
Tom Sepez396e8722015-09-09 10:16:08 -0700237FPDF_PAGE EmbedderTest::LoadAndCachePage(int page_number) {
238 FPDF_PAGE page = delegate_->GetPage(form_handle_, document_, page_number);
239 if (!page) {
240 return nullptr;
241 }
242 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
243 return page;
244}
245
Tom Sepezda8189e2015-01-30 14:41:50 -0800246FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800247 int width = static_cast<int>(FPDF_GetPageWidth(page));
248 int height = static_cast<int>(FPDF_GetPageHeight(page));
Lei Zhang453d96b2015-12-31 13:13:10 -0800249 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
250 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
251 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
252 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
Tom Sepez96d13342015-01-16 14:59:26 -0800253 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800254 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800255 return bitmap;
256}
257
Tom Sepezda8189e2015-01-30 14:41:50 -0800258void EmbedderTest::UnloadPage(FPDF_PAGE page) {
259 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
260 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800261 FPDF_ClosePage(page);
262}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800263
Tom Sepez396e8722015-09-09 10:16:08 -0700264FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMHANDLE form_handle,
265 FPDF_DOCUMENT document,
266 int page_index) {
267 auto it = m_pageMap.find(page_index);
268 if (it != m_pageMap.end()) {
269 return it->second;
270 }
271 FPDF_PAGE page = FPDF_LoadPage(document, page_index);
272 if (!page) {
273 return nullptr;
274 }
275 m_pageMap[page_index] = page;
276 FORM_OnAfterLoadPage(page, form_handle);
277 return page;
278}
279
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800280// static
281void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
282 int type) {
283 EmbedderTest* test = static_cast<EmbedderTest*>(info);
284 test->delegate_->UnsupportedHandler(type);
285}
286
287// static
288int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
289 FPDF_WIDESTRING message,
290 FPDF_WIDESTRING title,
291 int type,
292 int icon) {
293 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
294 return test->delegate_->Alert(message, title, type, icon);
295}
296
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700297// static
298int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 int msecs,
300 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700301 EmbedderTest* test = static_cast<EmbedderTest*>(info);
302 return test->delegate_->SetTimer(msecs, fn);
303}
304
305// static
306void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
307 EmbedderTest* test = static_cast<EmbedderTest*>(info);
308 return test->delegate_->KillTimer(id);
309}
310
Tom Sepez396e8722015-09-09 10:16:08 -0700311// static
312FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
313 FPDF_DOCUMENT document,
314 int page_index) {
315 EmbedderTest* test = static_cast<EmbedderTest*>(info);
Tom Sepez436977e2015-10-02 09:16:40 -0700316 return test->delegate_->GetPage(test->form_handle(), document, page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700317}
318
Tom Sepez1b1bb492015-01-22 17:36:32 -0800319// Can't use gtest-provided main since we need to stash the path to the
320// executable in order to find the external V8 binary data files.
321int main(int argc, char** argv) {
322 g_exe_path_ = argv[0];
323 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800324 testing::InitGoogleMock(&argc, argv);
Tom Sepez1b1bb492015-01-22 17:36:32 -0800325 return RUN_ALL_TESTS();
326}