blob: c1d19b2cfcddc32f1f68368f33aab6c088a01597 [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
5#include "embedder_test.h"
6
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
Tom Sepez1ed8a212015-05-11 15:25:39 -070014#include "../public/fpdf_text.h"
15#include "../public/fpdfview.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070016#include "test_support.h"
Tom Sepeza310e002015-02-27 13:03:07 -080017#include "testing/gmock/include/gmock/gmock.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070018
19#ifdef PDF_ENABLE_V8
Tom Sepez2785fb42015-03-03 09:49:29 -080020#include "v8/include/libplatform/libplatform.h"
Tom Sepez6efc0ad2015-06-02 17:11:18 -070021#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070022#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080023
Tom Sepez96d13342015-01-16 14:59:26 -080024namespace {
Tom Sepez1b1bb492015-01-22 17:36:32 -080025const char* g_exe_path_ = nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080026} // namespace
27
Tom Sepezcf22eb82015-05-12 17:28:08 -070028FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
Tom Sepez96d13342015-01-16 14:59:26 -080029 return true;
30}
31
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {}
Tom Sepez96d13342015-01-16 14:59:26 -080033
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -070035 : default_delegate_(new EmbedderTest::Delegate()),
36 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080037 form_handle_(nullptr),
38 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -070039 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080040 loader_(nullptr),
41 file_length_(0),
42 file_contents_(nullptr) {
43 memset(&hints_, 0, sizeof(hints_));
44 memset(&file_access_, 0, sizeof(file_access_));
45 memset(&file_avail_, 0, sizeof(file_avail_));
Tom Sepeza72e8e22015-10-07 10:17:53 -070046 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080047}
48
49EmbedderTest::~EmbedderTest() {
Tom Sepez4cb0fa72015-02-25 16:08:18 -080050}
51
Tom Sepez96d13342015-01-16 14:59:26 -080052void EmbedderTest::SetUp() {
Tom Sepez452b4f32015-10-13 09:27:27 -070053#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080054#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepezd831dc72015-10-19 16:04:22 -070055 InitializeV8ForPDFium(g_exe_path_, std::string(), &natives_, &snapshot_,
56 &platform_);
57#else
58 InitializeV8ForPDFium(&platform_);
Tom Sepez96d13342015-01-16 14:59:26 -080059#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -070060#endif // FPDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080061
Tom Sepeza72e8e22015-10-07 10:17:53 -070062 FPDF_LIBRARY_CONFIG config;
63 config.version = 2;
64 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070065 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070066 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070067 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080068
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
70 memset(info, 0, sizeof(UNSUPPORT_INFO));
71 info->version = 1;
72 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
73 FSDK_SetUnSpObjProcessHandler(info);
74}
Tom Sepez96d13342015-01-16 14:59:26 -080075
76void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -080077 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -070078 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -070079
80 // Note: The shut down order here is the reverse of the non-XFA branch
81 // order. Need to work out if this is required, and if it is, the lifetimes
82 // of objects owned by |doc| that |form| reference.
Tom Sepezda8189e2015-01-30 14:41:50 -080083 FPDF_CloseDocument(document_);
Lei Zhangd27acae2015-05-15 15:36:02 -070084 FPDFDOC_ExitFormFillEnvironment(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -080085 }
Tom Sepez96d13342015-01-16 14:59:26 -080086 FPDFAvail_Destroy(avail_);
87 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -070088
89#ifdef PDF_ENABLE_V8
Tom Sepez6efc0ad2015-06-02 17:11:18 -070090 v8::V8::ShutdownPlatform();
91 delete platform_;
Tom Sepez452b4f32015-10-13 09:27:27 -070092#endif // PDF_ENABLE_V8
93
Lei Zhangd27acae2015-05-15 15:36:02 -070094 delete loader_;
95 free(file_contents_);
Tom Sepez96d13342015-01-16 14:59:26 -080096}
97
98bool EmbedderTest::OpenDocument(const std::string& filename) {
99 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
100 if (!file_contents_) {
101 return false;
102 }
103
104 loader_ = new TestLoader(file_contents_, file_length_);
105 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700106 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800107 file_access_.m_Param = loader_;
108
109 file_avail_.version = 1;
110 file_avail_.IsDataAvail = Is_Data_Avail;
111
112 hints_.version = 1;
113 hints_.AddSegment = Add_Segment;
114
115 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 (void)FPDFAvail_IsDocAvail(avail_, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800117
118 if (!FPDFAvail_IsLinearized(avail_)) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700119 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
Tom Sepez96d13342015-01-16 14:59:26 -0800120 } else {
Lei Zhangd27acae2015-05-15 15:36:02 -0700121 document_ = FPDFAvail_GetDocument(avail_, nullptr);
Tom Sepez96d13342015-01-16 14:59:26 -0800122 }
123 if (!document_) {
124 return false;
125 }
JUN FANG827a1722015-03-05 13:39:21 -0800126 int docType = DOCTYPE_PDF;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 if (FPDF_HasXFAField(document_, &docType)) {
JUN FANG827a1722015-03-05 13:39:21 -0800128 if (docType != DOCTYPE_PDF)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 (void)FPDF_LoadXFA(document_);
JUN FANG827a1722015-03-05 13:39:21 -0800130 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 (void)FPDF_GetDocPermissions(document_);
132 (void)FPDFAvail_IsFormAvail(avail_, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800133
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800134 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
135 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200136 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800137 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800138
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800139 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
140 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
141 formfillinfo->version = 1;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700142 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
143 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700144 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800145 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800146
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800147 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800148 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
149 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
150
151 return true;
Tom Sepez96d13342015-01-16 14:59:26 -0800152}
153
Tom Sepezda8189e2015-01-30 14:41:50 -0800154void EmbedderTest::DoOpenActions() {
155 FORM_DoDocumentJSAction(form_handle_);
156 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800157}
158
159int EmbedderTest::GetFirstPageNum() {
160 int first_page = FPDFAvail_GetFirstPageNum(document_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800162 return first_page;
163}
164
165int EmbedderTest::GetPageCount() {
166 int page_count = FPDF_GetPageCount(document_);
167 for (int i = 0; i < page_count; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 (void)FPDFAvail_IsPageAvail(avail_, i, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800169 }
170 return page_count;
171}
172
Tom Sepezda8189e2015-01-30 14:41:50 -0800173FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez96d13342015-01-16 14:59:26 -0800174 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
175 if (!page) {
176 return nullptr;
177 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800178 FORM_OnAfterLoadPage(page, form_handle_);
179 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
Tom Sepez96d13342015-01-16 14:59:26 -0800180 return page;
181}
182
Tom Sepez396e8722015-09-09 10:16:08 -0700183FPDF_PAGE EmbedderTest::LoadAndCachePage(int page_number) {
184 FPDF_PAGE page = delegate_->GetPage(form_handle_, document_, page_number);
185 if (!page) {
186 return nullptr;
187 }
188 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
189 return page;
190}
191
Tom Sepezda8189e2015-01-30 14:41:50 -0800192FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800193 int width = static_cast<int>(FPDF_GetPageWidth(page));
194 int height = static_cast<int>(FPDF_GetPageHeight(page));
195 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
196 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
197 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800198 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800199 return bitmap;
200}
201
Tom Sepezda8189e2015-01-30 14:41:50 -0800202void EmbedderTest::UnloadPage(FPDF_PAGE page) {
203 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
204 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800205 FPDF_ClosePage(page);
206}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800207
Tom Sepez396e8722015-09-09 10:16:08 -0700208FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMHANDLE form_handle,
209 FPDF_DOCUMENT document,
210 int page_index) {
211 auto it = m_pageMap.find(page_index);
212 if (it != m_pageMap.end()) {
213 return it->second;
214 }
215 FPDF_PAGE page = FPDF_LoadPage(document, page_index);
216 if (!page) {
217 return nullptr;
218 }
219 m_pageMap[page_index] = page;
220 FORM_OnAfterLoadPage(page, form_handle);
221 return page;
222}
223
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800224// static
225void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
226 int type) {
227 EmbedderTest* test = static_cast<EmbedderTest*>(info);
228 test->delegate_->UnsupportedHandler(type);
229}
230
231// static
232int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
233 FPDF_WIDESTRING message,
234 FPDF_WIDESTRING title,
235 int type,
236 int icon) {
237 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
238 return test->delegate_->Alert(message, title, type, icon);
239}
240
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700241// static
242int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 int msecs,
244 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700245 EmbedderTest* test = static_cast<EmbedderTest*>(info);
246 return test->delegate_->SetTimer(msecs, fn);
247}
248
249// static
250void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
251 EmbedderTest* test = static_cast<EmbedderTest*>(info);
252 return test->delegate_->KillTimer(id);
253}
254
Tom Sepez396e8722015-09-09 10:16:08 -0700255// static
256FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
257 FPDF_DOCUMENT document,
258 int page_index) {
259 EmbedderTest* test = static_cast<EmbedderTest*>(info);
Tom Sepez436977e2015-10-02 09:16:40 -0700260 return test->delegate_->GetPage(test->form_handle(), document, page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700261}
262
Tom Sepez1b1bb492015-01-22 17:36:32 -0800263// Can't use gtest-provided main since we need to stash the path to the
264// executable in order to find the external V8 binary data files.
265int main(int argc, char** argv) {
266 g_exe_path_ = argv[0];
267 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800268 testing::InitGoogleMock(&argc, argv);
Tom Sepez1b1bb492015-01-22 17:36:32 -0800269 return RUN_ALL_TESTS();
270}