blob: ebd8c64b32758878cefacb6bfcd52611981f4cd4 [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
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 Sepezd831dc72015-10-19 16:04:22 -070017#include "test_support.h"
Tom Sepeza310e002015-02-27 13:03:07 -080018#include "testing/gmock/include/gmock/gmock.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070019
20#ifdef PDF_ENABLE_V8
Tom Sepez6efc0ad2015-06-02 17:11:18 -070021#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080022#include "v8/include/v8-platform.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070023#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080024
Tom Sepez96d13342015-01-16 14:59:26 -080025namespace {
Tom Sepez1b1bb492015-01-22 17:36:32 -080026const char* g_exe_path_ = nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080027} // namespace
28
Tom Sepezcf22eb82015-05-12 17:28:08 -070029FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
Tom Sepez96d13342015-01-16 14:59:26 -080030 return true;
31}
32
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {}
Tom Sepez96d13342015-01-16 14:59:26 -080034
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -070036 : default_delegate_(new EmbedderTest::Delegate()),
37 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080038 form_handle_(nullptr),
39 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -070040 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080041 loader_(nullptr),
42 file_length_(0),
43 file_contents_(nullptr) {
44 memset(&hints_, 0, sizeof(hints_));
45 memset(&file_access_, 0, sizeof(file_access_));
46 memset(&file_avail_, 0, sizeof(file_avail_));
Tom Sepeza72e8e22015-10-07 10:17:53 -070047 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080048}
49
50EmbedderTest::~EmbedderTest() {
Tom Sepez4cb0fa72015-02-25 16:08:18 -080051}
52
Tom Sepez96d13342015-01-16 14:59:26 -080053void EmbedderTest::SetUp() {
Tom Sepez452b4f32015-10-13 09:27:27 -070054#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080055#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepezd831dc72015-10-19 16:04:22 -070056 InitializeV8ForPDFium(g_exe_path_, std::string(), &natives_, &snapshot_,
57 &platform_);
58#else
59 InitializeV8ForPDFium(&platform_);
Tom Sepez96d13342015-01-16 14:59:26 -080060#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -070061#endif // FPDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080062
Tom Sepeza72e8e22015-10-07 10:17:53 -070063 FPDF_LIBRARY_CONFIG config;
64 config.version = 2;
65 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070066 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070067 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070068 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080069
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
71 memset(info, 0, sizeof(UNSUPPORT_INFO));
72 info->version = 1;
73 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
74 FSDK_SetUnSpObjProcessHandler(info);
75}
Tom Sepez96d13342015-01-16 14:59:26 -080076
77void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -080078 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -070079 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -070080
81 // Note: The shut down order here is the reverse of the non-XFA branch
82 // order. Need to work out if this is required, and if it is, the lifetimes
83 // of objects owned by |doc| that |form| reference.
Tom Sepezda8189e2015-01-30 14:41:50 -080084 FPDF_CloseDocument(document_);
Lei Zhangd27acae2015-05-15 15:36:02 -070085 FPDFDOC_ExitFormFillEnvironment(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -080086 }
Tom Sepez96d13342015-01-16 14:59:26 -080087 FPDFAvail_Destroy(avail_);
88 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -070089
90#ifdef PDF_ENABLE_V8
Tom Sepez6efc0ad2015-06-02 17:11:18 -070091 v8::V8::ShutdownPlatform();
92 delete platform_;
Tom Sepez452b4f32015-10-13 09:27:27 -070093#endif // PDF_ENABLE_V8
94
Lei Zhangd27acae2015-05-15 15:36:02 -070095 delete loader_;
96 free(file_contents_);
Tom Sepez96d13342015-01-16 14:59:26 -080097}
98
99bool EmbedderTest::OpenDocument(const std::string& filename) {
100 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400101 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800102 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800103
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
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400118 if (!FPDFAvail_IsLinearized(avail_))
Lei Zhangd27acae2015-05-15 15:36:02 -0700119 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400120 else
Lei Zhangd27acae2015-05-15 15:36:02 -0700121 document_ = FPDFAvail_GetDocument(avail_, nullptr);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400122
123 if (!document_)
Tom Sepez96d13342015-01-16 14:59:26 -0800124 return false;
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400125
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 }
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 (void)FPDF_GetDocPermissions(document_);
133 (void)FPDFAvail_IsFormAvail(avail_, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800134
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800135 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
136 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200137 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800138 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800139
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800140 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
141 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800142#ifdef PDF_ENABLE_XFA
143 formfillinfo->version = 2;
144#else
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800145 formfillinfo->version = 1;
Lei Zhangcd396952015-11-04 20:26:50 -0800146#endif
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700147 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
148 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700149 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800150 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800151
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800152 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800153 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
154 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
155
156 return true;
Tom Sepez96d13342015-01-16 14:59:26 -0800157}
158
Tom Sepezda8189e2015-01-30 14:41:50 -0800159void EmbedderTest::DoOpenActions() {
160 FORM_DoDocumentJSAction(form_handle_);
161 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800162}
163
164int EmbedderTest::GetFirstPageNum() {
165 int first_page = FPDFAvail_GetFirstPageNum(document_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800167 return first_page;
168}
169
170int EmbedderTest::GetPageCount() {
171 int page_count = FPDF_GetPageCount(document_);
172 for (int i = 0; i < page_count; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173 (void)FPDFAvail_IsPageAvail(avail_, i, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800174 }
175 return page_count;
176}
177
Tom Sepezda8189e2015-01-30 14:41:50 -0800178FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez96d13342015-01-16 14:59:26 -0800179 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
180 if (!page) {
181 return nullptr;
182 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800183 FORM_OnAfterLoadPage(page, form_handle_);
184 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
Tom Sepez96d13342015-01-16 14:59:26 -0800185 return page;
186}
187
Tom Sepez396e8722015-09-09 10:16:08 -0700188FPDF_PAGE EmbedderTest::LoadAndCachePage(int page_number) {
189 FPDF_PAGE page = delegate_->GetPage(form_handle_, document_, page_number);
190 if (!page) {
191 return nullptr;
192 }
193 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
194 return page;
195}
196
Tom Sepezda8189e2015-01-30 14:41:50 -0800197FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800198 int width = static_cast<int>(FPDF_GetPageWidth(page));
199 int height = static_cast<int>(FPDF_GetPageHeight(page));
200 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
201 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
202 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800203 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800204 return bitmap;
205}
206
Tom Sepezda8189e2015-01-30 14:41:50 -0800207void EmbedderTest::UnloadPage(FPDF_PAGE page) {
208 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
209 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800210 FPDF_ClosePage(page);
211}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800212
Tom Sepez396e8722015-09-09 10:16:08 -0700213FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMHANDLE form_handle,
214 FPDF_DOCUMENT document,
215 int page_index) {
216 auto it = m_pageMap.find(page_index);
217 if (it != m_pageMap.end()) {
218 return it->second;
219 }
220 FPDF_PAGE page = FPDF_LoadPage(document, page_index);
221 if (!page) {
222 return nullptr;
223 }
224 m_pageMap[page_index] = page;
225 FORM_OnAfterLoadPage(page, form_handle);
226 return page;
227}
228
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800229// static
230void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
231 int type) {
232 EmbedderTest* test = static_cast<EmbedderTest*>(info);
233 test->delegate_->UnsupportedHandler(type);
234}
235
236// static
237int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
238 FPDF_WIDESTRING message,
239 FPDF_WIDESTRING title,
240 int type,
241 int icon) {
242 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
243 return test->delegate_->Alert(message, title, type, icon);
244}
245
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700246// static
247int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 int msecs,
249 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700250 EmbedderTest* test = static_cast<EmbedderTest*>(info);
251 return test->delegate_->SetTimer(msecs, fn);
252}
253
254// static
255void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
256 EmbedderTest* test = static_cast<EmbedderTest*>(info);
257 return test->delegate_->KillTimer(id);
258}
259
Tom Sepez396e8722015-09-09 10:16:08 -0700260// static
261FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
262 FPDF_DOCUMENT document,
263 int page_index) {
264 EmbedderTest* test = static_cast<EmbedderTest*>(info);
Tom Sepez436977e2015-10-02 09:16:40 -0700265 return test->delegate_->GetPage(test->form_handle(), document, page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700266}
267
Tom Sepez1b1bb492015-01-22 17:36:32 -0800268// Can't use gtest-provided main since we need to stash the path to the
269// executable in order to find the external V8 binary data files.
270int main(int argc, char** argv) {
271 g_exe_path_ = argv[0];
272 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800273 testing::InitGoogleMock(&argc, argv);
Tom Sepez1b1bb492015-01-22 17:36:32 -0800274 return RUN_ALL_TESTS();
275}