blob: 1a3c4c6e5300b8054b389989d6b26cb2fc093898 [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>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include <list>
13#include <string>
14#include <utility>
15#include <vector>
16
Tom Sepez1ed8a212015-05-11 15:25:39 -070017#include "../public/fpdf_text.h"
18#include "../public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080019#include "testing/gmock/include/gmock/gmock.h"
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 Sepez96d13342015-01-16 14:59:26 -080022
23#ifdef _WIN32
24#define snprintf _snprintf
25#define PATH_SEPARATOR '\\'
26#else
27#define PATH_SEPARATOR '/'
28#endif
29
30namespace {
31
Tom Sepez1b1bb492015-01-22 17:36:32 -080032const char* g_exe_path_ = nullptr;
33
Tom Sepez96d13342015-01-16 14:59:26 -080034// Reads the entire contents of a file into a newly malloc'd buffer.
35static char* GetFileContents(const char* filename, size_t* retlen) {
36 FILE* file = fopen(filename, "rb");
37 if (!file) {
38 fprintf(stderr, "Failed to open: %s\n", filename);
Lei Zhangd27acae2015-05-15 15:36:02 -070039 return nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080040 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 (void)fseek(file, 0, SEEK_END);
Tom Sepez96d13342015-01-16 14:59:26 -080042 size_t file_length = ftell(file);
43 if (!file_length) {
Lei Zhangd27acae2015-05-15 15:36:02 -070044 return nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080045 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 (void)fseek(file, 0, SEEK_SET);
47 char* buffer = (char*)malloc(file_length);
Tom Sepez96d13342015-01-16 14:59:26 -080048 if (!buffer) {
Lei Zhangd27acae2015-05-15 15:36:02 -070049 return nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080050 }
51 size_t bytes_read = fread(buffer, 1, file_length, file);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 (void)fclose(file);
Tom Sepez96d13342015-01-16 14:59:26 -080053 if (bytes_read != file_length) {
54 fprintf(stderr, "Failed to read: %s\n", filename);
55 free(buffer);
Lei Zhangd27acae2015-05-15 15:36:02 -070056 return nullptr;
Tom Sepez96d13342015-01-16 14:59:26 -080057 }
58 *retlen = bytes_read;
59 return buffer;
60}
61
62#ifdef V8_USE_EXTERNAL_STARTUP_DATA
63// Returns the full path for an external V8 data file based on either
64// the currect exectuable path or an explicit override.
Tom Sepez1b1bb492015-01-22 17:36:32 -080065static std::string GetFullPathForSnapshotFile(const std::string& exe_path,
Tom Sepez96d13342015-01-16 14:59:26 -080066 const std::string& filename) {
67 std::string result;
Tom Sepez1b1bb492015-01-22 17:36:32 -080068 if (!exe_path.empty()) {
69 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 if (last_separator != std::string::npos) {
Tom Sepez1b1bb492015-01-22 17:36:32 -080071 result = exe_path.substr(0, last_separator + 1);
Tom Sepez96d13342015-01-16 14:59:26 -080072 }
73 }
74 result += filename;
75 return result;
76}
77
78// Reads an extenal V8 data file from the |options|-indicated location,
79// returing true on success and false on error.
Tom Sepez1b1bb492015-01-22 17:36:32 -080080static bool GetExternalData(const std::string& exe_path,
81 const std::string& filename,
Tom Sepez96d13342015-01-16 14:59:26 -080082 v8::StartupData* result_data) {
Tom Sepez1b1bb492015-01-22 17:36:32 -080083 std::string full_path = GetFullPathForSnapshotFile(exe_path, filename);
Tom Sepez96d13342015-01-16 14:59:26 -080084 size_t data_length = 0;
85 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
86 if (!data_buffer) {
87 return false;
88 }
89 result_data->data = const_cast<const char*>(data_buffer);
90 result_data->raw_size = data_length;
91 return true;
92}
93#endif // V8_USE_EXTERNAL_STARTUP_DATA
94
95} // namespace
96
Tom Sepez96d13342015-01-16 14:59:26 -080097class TestLoader {
98 public:
99 TestLoader(const char* pBuf, size_t len);
100
101 const char* m_pBuf;
102 size_t m_Len;
103};
104
105TestLoader::TestLoader(const char* pBuf, size_t len)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 : m_pBuf(pBuf), m_Len(len) {}
Tom Sepez96d13342015-01-16 14:59:26 -0800107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108int Get_Block(void* param,
109 unsigned long pos,
110 unsigned char* pBuf,
Tom Sepez96d13342015-01-16 14:59:26 -0800111 unsigned long size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 TestLoader* pLoader = (TestLoader*)param;
113 if (pos + size < pos || pos + size > pLoader->m_Len)
114 return 0;
Tom Sepez96d13342015-01-16 14:59:26 -0800115 memcpy(pBuf, pLoader->m_pBuf + pos, size);
116 return 1;
117}
118
Tom Sepezcf22eb82015-05-12 17:28:08 -0700119FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
Tom Sepez96d13342015-01-16 14:59:26 -0800120 return true;
121}
122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {}
Tom Sepez96d13342015-01-16 14:59:26 -0800124
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -0700126 : default_delegate_(new EmbedderTest::Delegate()),
127 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800128 form_handle_(nullptr),
129 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -0700130 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800131 loader_(nullptr),
132 file_length_(0),
133 file_contents_(nullptr) {
134 memset(&hints_, 0, sizeof(hints_));
135 memset(&file_access_, 0, sizeof(file_access_));
136 memset(&file_avail_, 0, sizeof(file_avail_));
Tom Sepeza72e8e22015-10-07 10:17:53 -0700137 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800138}
139
140EmbedderTest::~EmbedderTest() {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800141}
142
Tom Sepez96d13342015-01-16 14:59:26 -0800143void EmbedderTest::SetUp() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 v8::V8::InitializeICU();
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700145
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 platform_ = v8::platform::CreateDefaultPlatform();
147 v8::V8::InitializePlatform(platform_);
148 v8::V8::Initialize();
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 // By enabling predictable mode, V8 won't post any background tasks.
151 const char predictable_flag[] = "--predictable";
152 v8::V8::SetFlagsFromString(predictable_flag,
153 static_cast<int>(strlen(predictable_flag)));
Tom Sepez96d13342015-01-16 14:59:26 -0800154
155#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_));
157 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_));
158 v8::V8::SetNativesDataBlob(&natives_);
159 v8::V8::SetSnapshotDataBlob(&snapshot_);
Tom Sepez96d13342015-01-16 14:59:26 -0800160#endif // V8_USE_EXTERNAL_STARTUP_DATA
161
Tom Sepeza72e8e22015-10-07 10:17:53 -0700162 FPDF_LIBRARY_CONFIG config;
163 config.version = 2;
164 config.m_pUserFontPaths = nullptr;
165 config.m_pIsolate = external_isolate_;
166 config.m_v8EmbedderSlot = 0;
167 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -0800168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
170 memset(info, 0, sizeof(UNSUPPORT_INFO));
171 info->version = 1;
172 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
173 FSDK_SetUnSpObjProcessHandler(info);
174}
Tom Sepez96d13342015-01-16 14:59:26 -0800175
176void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800177 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700178 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700179
180 // Note: The shut down order here is the reverse of the non-XFA branch
181 // order. Need to work out if this is required, and if it is, the lifetimes
182 // of objects owned by |doc| that |form| reference.
Tom Sepezda8189e2015-01-30 14:41:50 -0800183 FPDF_CloseDocument(document_);
Lei Zhangd27acae2015-05-15 15:36:02 -0700184 FPDFDOC_ExitFormFillEnvironment(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800185 }
Tom Sepez96d13342015-01-16 14:59:26 -0800186 FPDFAvail_Destroy(avail_);
187 FPDF_DestroyLibrary();
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700188 v8::V8::ShutdownPlatform();
189 delete platform_;
Lei Zhangd27acae2015-05-15 15:36:02 -0700190 delete loader_;
191 free(file_contents_);
Tom Sepez96d13342015-01-16 14:59:26 -0800192}
193
194bool EmbedderTest::OpenDocument(const std::string& filename) {
195 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
196 if (!file_contents_) {
197 return false;
198 }
199
200 loader_ = new TestLoader(file_contents_, file_length_);
201 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
202 file_access_.m_GetBlock = Get_Block;
203 file_access_.m_Param = loader_;
204
205 file_avail_.version = 1;
206 file_avail_.IsDataAvail = Is_Data_Avail;
207
208 hints_.version = 1;
209 hints_.AddSegment = Add_Segment;
210
211 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 (void)FPDFAvail_IsDocAvail(avail_, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800213
214 if (!FPDFAvail_IsLinearized(avail_)) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700215 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
Tom Sepez96d13342015-01-16 14:59:26 -0800216 } else {
Lei Zhangd27acae2015-05-15 15:36:02 -0700217 document_ = FPDFAvail_GetDocument(avail_, nullptr);
Tom Sepez96d13342015-01-16 14:59:26 -0800218 }
219 if (!document_) {
220 return false;
221 }
JUN FANG827a1722015-03-05 13:39:21 -0800222 int docType = DOCTYPE_PDF;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 if (FPDF_HasXFAField(document_, &docType)) {
JUN FANG827a1722015-03-05 13:39:21 -0800224 if (docType != DOCTYPE_PDF)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 (void)FPDF_LoadXFA(document_);
JUN FANG827a1722015-03-05 13:39:21 -0800226 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 (void)FPDF_GetDocPermissions(document_);
228 (void)FPDFAvail_IsFormAvail(avail_, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800229
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800230 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
231 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200232 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800233 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800234
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800235 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
236 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
237 formfillinfo->version = 1;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700238 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
239 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700240 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800241 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800242
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800243 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800244 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
245 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
246
247 return true;
Tom Sepez96d13342015-01-16 14:59:26 -0800248}
249
Tom Sepezda8189e2015-01-30 14:41:50 -0800250void EmbedderTest::DoOpenActions() {
251 FORM_DoDocumentJSAction(form_handle_);
252 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800253}
254
255int EmbedderTest::GetFirstPageNum() {
256 int first_page = FPDFAvail_GetFirstPageNum(document_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800258 return first_page;
259}
260
261int EmbedderTest::GetPageCount() {
262 int page_count = FPDF_GetPageCount(document_);
263 for (int i = 0; i < page_count; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 (void)FPDFAvail_IsPageAvail(avail_, i, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800265 }
266 return page_count;
267}
268
Tom Sepezda8189e2015-01-30 14:41:50 -0800269FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez96d13342015-01-16 14:59:26 -0800270 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
271 if (!page) {
272 return nullptr;
273 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800274 FORM_OnAfterLoadPage(page, form_handle_);
275 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
Tom Sepez96d13342015-01-16 14:59:26 -0800276 return page;
277}
278
Tom Sepez396e8722015-09-09 10:16:08 -0700279FPDF_PAGE EmbedderTest::LoadAndCachePage(int page_number) {
280 FPDF_PAGE page = delegate_->GetPage(form_handle_, document_, page_number);
281 if (!page) {
282 return nullptr;
283 }
284 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
285 return page;
286}
287
Tom Sepezda8189e2015-01-30 14:41:50 -0800288FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800289 int width = static_cast<int>(FPDF_GetPageWidth(page));
290 int height = static_cast<int>(FPDF_GetPageHeight(page));
291 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
292 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
293 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800294 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800295 return bitmap;
296}
297
Tom Sepezda8189e2015-01-30 14:41:50 -0800298void EmbedderTest::UnloadPage(FPDF_PAGE page) {
299 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
300 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800301 FPDF_ClosePage(page);
302}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800303
Tom Sepez396e8722015-09-09 10:16:08 -0700304FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMHANDLE form_handle,
305 FPDF_DOCUMENT document,
306 int page_index) {
307 auto it = m_pageMap.find(page_index);
308 if (it != m_pageMap.end()) {
309 return it->second;
310 }
311 FPDF_PAGE page = FPDF_LoadPage(document, page_index);
312 if (!page) {
313 return nullptr;
314 }
315 m_pageMap[page_index] = page;
316 FORM_OnAfterLoadPage(page, form_handle);
317 return page;
318}
319
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800320// static
321void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
322 int type) {
323 EmbedderTest* test = static_cast<EmbedderTest*>(info);
324 test->delegate_->UnsupportedHandler(type);
325}
326
327// static
328int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
329 FPDF_WIDESTRING message,
330 FPDF_WIDESTRING title,
331 int type,
332 int icon) {
333 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
334 return test->delegate_->Alert(message, title, type, icon);
335}
336
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700337// static
338int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339 int msecs,
340 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700341 EmbedderTest* test = static_cast<EmbedderTest*>(info);
342 return test->delegate_->SetTimer(msecs, fn);
343}
344
345// static
346void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
347 EmbedderTest* test = static_cast<EmbedderTest*>(info);
348 return test->delegate_->KillTimer(id);
349}
350
Tom Sepez396e8722015-09-09 10:16:08 -0700351// static
352FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
353 FPDF_DOCUMENT document,
354 int page_index) {
355 EmbedderTest* test = static_cast<EmbedderTest*>(info);
Tom Sepez436977e2015-10-02 09:16:40 -0700356 return test->delegate_->GetPage(test->form_handle(), document, page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700357}
358
Tom Sepez1b1bb492015-01-22 17:36:32 -0800359// Can't use gtest-provided main since we need to stash the path to the
360// executable in order to find the external V8 binary data files.
361int main(int argc, char** argv) {
362 g_exe_path_ = argv[0];
363 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800364 testing::InitGoogleMock(&argc, argv);
Tom Sepez1b1bb492015-01-22 17:36:32 -0800365 return RUN_ALL_TESTS();
366}