blob: c57126f32095d73651e871e086f60a81dfcff59c [file] [log] [blame]
Tom Sepez96d13342015-01-16 14:59:26 -08001// Copyright (c) 2015 PDFium Authors. All rights reserved.
2// 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"
15#include "public/fpdf_text.h"
16#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080017#include "testing/gmock/include/gmock/gmock.h"
Wei Li091f7a02015-11-09 12:09:55 -080018#include "testing/test_support.h"
19#include "testing/utils/path_service.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070020
21#ifdef PDF_ENABLE_V8
Tom Sepez6efc0ad2015-06-02 17:11:18 -070022#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080023#include "v8/include/v8-platform.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070024#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080025
Tom Sepez96d13342015-01-16 14:59:26 -080026namespace {
Tom Sepez1b1bb492015-01-22 17:36:32 -080027const char* g_exe_path_ = nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080028} // namespace
29
Tom Sepezcf22eb82015-05-12 17:28:08 -070030FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
Tom Sepez96d13342015-01-16 14:59:26 -080031 return true;
32}
33
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {}
Tom Sepez96d13342015-01-16 14:59:26 -080035
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -070037 : default_delegate_(new EmbedderTest::Delegate()),
38 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080039 form_handle_(nullptr),
40 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -070041 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080042 loader_(nullptr),
43 file_length_(0),
44 file_contents_(nullptr) {
45 memset(&hints_, 0, sizeof(hints_));
46 memset(&file_access_, 0, sizeof(file_access_));
47 memset(&file_avail_, 0, sizeof(file_avail_));
Tom Sepeza72e8e22015-10-07 10:17:53 -070048 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080049}
50
51EmbedderTest::~EmbedderTest() {
Tom Sepez4cb0fa72015-02-25 16:08:18 -080052}
53
Tom Sepez96d13342015-01-16 14:59:26 -080054void EmbedderTest::SetUp() {
Tom Sepez452b4f32015-10-13 09:27:27 -070055#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080056#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepezd831dc72015-10-19 16:04:22 -070057 InitializeV8ForPDFium(g_exe_path_, std::string(), &natives_, &snapshot_,
58 &platform_);
59#else
60 InitializeV8ForPDFium(&platform_);
Tom Sepez96d13342015-01-16 14:59:26 -080061#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -070062#endif // FPDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080063
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);
Lei Zhangba026912015-07-16 10:06:11 -070081
82 // 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 Sepezda8189e2015-01-30 14:41:50 -080087 }
Tom Sepez96d13342015-01-16 14:59:26 -080088 FPDFAvail_Destroy(avail_);
89 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -070090
91#ifdef PDF_ENABLE_V8
Tom Sepez6efc0ad2015-06-02 17:11:18 -070092 v8::V8::ShutdownPlatform();
93 delete platform_;
Tom Sepez452b4f32015-10-13 09:27:27 -070094#endif // PDF_ENABLE_V8
95
Lei Zhangd27acae2015-05-15 15:36:02 -070096 delete loader_;
97 free(file_contents_);
Tom Sepez96d13342015-01-16 14:59:26 -080098}
99
Jun Fangdf7f3662015-11-10 18:29:18 +0800100bool EmbedderTest::OpenDocument(const std::string& filename,
101 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800102 std::string file_path;
103 if (!PathService::GetTestFilePath(filename, &file_path))
104 return false;
105 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400106 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800107 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800108
109 loader_ = new TestLoader(file_contents_, file_length_);
110 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700111 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800112 file_access_.m_Param = loader_;
113
114 file_avail_.version = 1;
115 file_avail_.IsDataAvail = Is_Data_Avail;
116
117 hints_.version = 1;
118 hints_.AddSegment = Add_Segment;
119
120 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
Tom Sepez96d13342015-01-16 14:59:26 -0800121
Jun Fangdf7f3662015-11-10 18:29:18 +0800122 if (FPDFAvail_IsLinearized(avail_) == PDF_LINEARIZED) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700123 document_ = FPDFAvail_GetDocument(avail_, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800124 if (!document_) {
125 return false;
126 }
127 int32_t nRet = PDF_DATA_NOTAVAIL;
128 while (nRet == PDF_DATA_NOTAVAIL) {
129 nRet = FPDFAvail_IsDocAvail(avail_, &hints_);
130 }
131 if (nRet == PDF_DATA_ERROR) {
132 return false;
133 }
134 nRet = FPDFAvail_IsFormAvail(avail_, &hints_);
135 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
136 return false;
137 }
138 int page_count = FPDF_GetPageCount(document_);
139 for (int i = 0; i < page_count; ++i) {
140 nRet = PDF_DATA_NOTAVAIL;
141 while (nRet == PDF_DATA_NOTAVAIL) {
142 nRet = FPDFAvail_IsPageAvail(avail_, i, &hints_);
143 }
144 if (nRet == PDF_DATA_ERROR) {
145 return false;
146 }
147 }
148 } else {
149 if (must_linearize) {
150 return false;
151 }
152 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
153 if (!document_) {
154 return false;
155 }
156 }
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400157
JUN FANG827a1722015-03-05 13:39:21 -0800158 int docType = DOCTYPE_PDF;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 if (FPDF_HasXFAField(document_, &docType)) {
JUN FANG827a1722015-03-05 13:39:21 -0800160 if (docType != DOCTYPE_PDF)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 (void)FPDF_LoadXFA(document_);
JUN FANG827a1722015-03-05 13:39:21 -0800162 }
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400163
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 (void)FPDF_GetDocPermissions(document_);
Tom Sepez96d13342015-01-16 14:59:26 -0800165
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800166 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
167 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200168 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800169 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800170
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800171 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
172 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800173#ifdef PDF_ENABLE_XFA
174 formfillinfo->version = 2;
175#else
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800176 formfillinfo->version = 1;
Lei Zhangcd396952015-11-04 20:26:50 -0800177#endif
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700178 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
179 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700180 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800181 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800182
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800183 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800184 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
185 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
186
187 return true;
Tom Sepez96d13342015-01-16 14:59:26 -0800188}
189
Tom Sepezda8189e2015-01-30 14:41:50 -0800190void EmbedderTest::DoOpenActions() {
191 FORM_DoDocumentJSAction(form_handle_);
192 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800193}
194
195int EmbedderTest::GetFirstPageNum() {
196 int first_page = FPDFAvail_GetFirstPageNum(document_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800198 return first_page;
199}
200
201int EmbedderTest::GetPageCount() {
202 int page_count = FPDF_GetPageCount(document_);
203 for (int i = 0; i < page_count; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 (void)FPDFAvail_IsPageAvail(avail_, i, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800205 }
206 return page_count;
207}
208
Tom Sepezda8189e2015-01-30 14:41:50 -0800209FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez96d13342015-01-16 14:59:26 -0800210 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
211 if (!page) {
212 return nullptr;
213 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800214 FORM_OnAfterLoadPage(page, form_handle_);
215 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
Tom Sepez96d13342015-01-16 14:59:26 -0800216 return page;
217}
218
Tom Sepez396e8722015-09-09 10:16:08 -0700219FPDF_PAGE EmbedderTest::LoadAndCachePage(int page_number) {
220 FPDF_PAGE page = delegate_->GetPage(form_handle_, document_, page_number);
221 if (!page) {
222 return nullptr;
223 }
224 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
225 return page;
226}
227
Tom Sepezda8189e2015-01-30 14:41:50 -0800228FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800229 int width = static_cast<int>(FPDF_GetPageWidth(page));
230 int height = static_cast<int>(FPDF_GetPageHeight(page));
231 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
232 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
233 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800234 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800235 return bitmap;
236}
237
Tom Sepezda8189e2015-01-30 14:41:50 -0800238void EmbedderTest::UnloadPage(FPDF_PAGE page) {
239 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
240 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800241 FPDF_ClosePage(page);
242}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800243
Tom Sepez396e8722015-09-09 10:16:08 -0700244FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMHANDLE form_handle,
245 FPDF_DOCUMENT document,
246 int page_index) {
247 auto it = m_pageMap.find(page_index);
248 if (it != m_pageMap.end()) {
249 return it->second;
250 }
251 FPDF_PAGE page = FPDF_LoadPage(document, page_index);
252 if (!page) {
253 return nullptr;
254 }
255 m_pageMap[page_index] = page;
256 FORM_OnAfterLoadPage(page, form_handle);
257 return page;
258}
259
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800260// static
261void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
262 int type) {
263 EmbedderTest* test = static_cast<EmbedderTest*>(info);
264 test->delegate_->UnsupportedHandler(type);
265}
266
267// static
268int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
269 FPDF_WIDESTRING message,
270 FPDF_WIDESTRING title,
271 int type,
272 int icon) {
273 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
274 return test->delegate_->Alert(message, title, type, icon);
275}
276
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700277// static
278int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 int msecs,
280 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700281 EmbedderTest* test = static_cast<EmbedderTest*>(info);
282 return test->delegate_->SetTimer(msecs, fn);
283}
284
285// static
286void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
287 EmbedderTest* test = static_cast<EmbedderTest*>(info);
288 return test->delegate_->KillTimer(id);
289}
290
Tom Sepez396e8722015-09-09 10:16:08 -0700291// static
292FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
293 FPDF_DOCUMENT document,
294 int page_index) {
295 EmbedderTest* test = static_cast<EmbedderTest*>(info);
Tom Sepez436977e2015-10-02 09:16:40 -0700296 return test->delegate_->GetPage(test->form_handle(), document, page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700297}
298
Tom Sepez1b1bb492015-01-22 17:36:32 -0800299// Can't use gtest-provided main since we need to stash the path to the
300// executable in order to find the external V8 binary data files.
301int main(int argc, char** argv) {
302 g_exe_path_ = argv[0];
303 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800304 testing::InitGoogleMock(&argc, argv);
Tom Sepez1b1bb492015-01-22 17:36:32 -0800305 return RUN_ALL_TESTS();
306}