blob: d819d9c2a28d705cc399683e9c17f42795faa0f2 [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 }
41 (void) fseek(file, 0, SEEK_END);
42 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 }
46 (void) fseek(file, 0, SEEK_SET);
47 char* buffer = (char*) malloc(file_length);
48 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);
52 (void) fclose(file);
53 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);
Tom Sepez96d13342015-01-16 14:59:26 -080070 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)
106 : m_pBuf(pBuf), m_Len(len) {
107}
108
109int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
110 unsigned long size) {
111 TestLoader* pLoader = (TestLoader*) param;
112 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
113 memcpy(pBuf, pLoader->m_pBuf + pos, size);
114 return 1;
115}
116
Tom Sepezcf22eb82015-05-12 17:28:08 -0700117FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
Tom Sepez96d13342015-01-16 14:59:26 -0800118 return true;
119}
120
121void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
122}
123
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800124EmbedderTest::EmbedderTest() :
125 document_(nullptr),
126 form_handle_(nullptr),
127 avail_(nullptr),
128 loader_(nullptr),
129 file_length_(0),
130 file_contents_(nullptr) {
131 memset(&hints_, 0, sizeof(hints_));
132 memset(&file_access_, 0, sizeof(file_access_));
133 memset(&file_avail_, 0, sizeof(file_avail_));
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700134 default_delegate_ = new EmbedderTest::Delegate();
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800135 delegate_ = default_delegate_;
136}
137
138EmbedderTest::~EmbedderTest() {
139 delete default_delegate_;
140}
141
Tom Sepez96d13342015-01-16 14:59:26 -0800142void EmbedderTest::SetUp() {
143 v8::V8::InitializeICU();
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700144
145 platform_ = v8::platform::CreateDefaultPlatform();
146 v8::V8::InitializePlatform(platform_);
147 v8::V8::Initialize();
148
149 // By enabling predictable mode, V8 won't post any background tasks.
150 const char predictable_flag[] = "--predictable";
151 v8::V8::SetFlagsFromString(predictable_flag,
152 static_cast<int>(strlen(predictable_flag)));
Tom Sepez96d13342015-01-16 14:59:26 -0800153
154#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez1b1bb492015-01-22 17:36:32 -0800155 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_));
156 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_));
157 v8::V8::SetNativesDataBlob(&natives_);
158 v8::V8::SetSnapshotDataBlob(&snapshot_);
Tom Sepez96d13342015-01-16 14:59:26 -0800159#endif // V8_USE_EXTERNAL_STARTUP_DATA
160
161 FPDF_InitLibrary();
162
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800163 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
164 memset(info, 0, sizeof(UNSUPPORT_INFO));
165 info->version = 1;
166 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
167 FSDK_SetUnSpObjProcessHandler(info);
Tom Sepez96d13342015-01-16 14:59:26 -0800168 }
169
170void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800171 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700172 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700173
174 // Note: The shut down order here is the reverse of the non-XFA branch
175 // order. Need to work out if this is required, and if it is, the lifetimes
176 // of objects owned by |doc| that |form| reference.
Tom Sepezda8189e2015-01-30 14:41:50 -0800177 FPDF_CloseDocument(document_);
Lei Zhangd27acae2015-05-15 15:36:02 -0700178 FPDFDOC_ExitFormFillEnvironment(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800179 }
Tom Sepez96d13342015-01-16 14:59:26 -0800180 FPDFAvail_Destroy(avail_);
181 FPDF_DestroyLibrary();
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700182 v8::V8::ShutdownPlatform();
183 delete platform_;
Lei Zhangd27acae2015-05-15 15:36:02 -0700184 delete loader_;
185 free(file_contents_);
Tom Sepez96d13342015-01-16 14:59:26 -0800186}
187
188bool EmbedderTest::OpenDocument(const std::string& filename) {
189 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
190 if (!file_contents_) {
191 return false;
192 }
193
194 loader_ = new TestLoader(file_contents_, file_length_);
195 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
196 file_access_.m_GetBlock = Get_Block;
197 file_access_.m_Param = loader_;
198
199 file_avail_.version = 1;
200 file_avail_.IsDataAvail = Is_Data_Avail;
201
202 hints_.version = 1;
203 hints_.AddSegment = Add_Segment;
204
205 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
206 (void) FPDFAvail_IsDocAvail(avail_, &hints_);
207
208 if (!FPDFAvail_IsLinearized(avail_)) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700209 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
Tom Sepez96d13342015-01-16 14:59:26 -0800210 } else {
Lei Zhangd27acae2015-05-15 15:36:02 -0700211 document_ = FPDFAvail_GetDocument(avail_, nullptr);
Tom Sepez96d13342015-01-16 14:59:26 -0800212 }
213 if (!document_) {
214 return false;
215 }
JUN FANG827a1722015-03-05 13:39:21 -0800216 int docType = DOCTYPE_PDF;
Tom Sepezcf22eb82015-05-12 17:28:08 -0700217 if (FPDF_HasXFAField(document_, &docType))
JUN FANG827a1722015-03-05 13:39:21 -0800218 {
219 if (docType != DOCTYPE_PDF)
220 (void) FPDF_LoadXFA(document_);
221 }
Tom Sepez96d13342015-01-16 14:59:26 -0800222 (void) FPDF_GetDocPermissions(document_);
223 (void) FPDFAvail_IsFormAvail(avail_, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800224
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800225 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
226 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200227 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800228 platform->app_alert = AlertTrampoline;
Tom Sepez96d13342015-01-16 14:59:26 -0800229
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800230 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
231 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
232 formfillinfo->version = 1;
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700233 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
234 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800235 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800236
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800237 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800238 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
239 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
240
241 return true;
Tom Sepez96d13342015-01-16 14:59:26 -0800242}
243
Tom Sepezda8189e2015-01-30 14:41:50 -0800244void EmbedderTest::DoOpenActions() {
245 FORM_DoDocumentJSAction(form_handle_);
246 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800247}
248
249int EmbedderTest::GetFirstPageNum() {
250 int first_page = FPDFAvail_GetFirstPageNum(document_);
251 (void) FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
252 return first_page;
253}
254
255int EmbedderTest::GetPageCount() {
256 int page_count = FPDF_GetPageCount(document_);
257 for (int i = 0; i < page_count; ++i) {
258 (void) FPDFAvail_IsPageAvail(avail_, i, &hints_);
259 }
260 return page_count;
261}
262
Tom Sepezda8189e2015-01-30 14:41:50 -0800263FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez96d13342015-01-16 14:59:26 -0800264 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
265 if (!page) {
266 return nullptr;
267 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800268 FORM_OnAfterLoadPage(page, form_handle_);
269 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
Tom Sepez96d13342015-01-16 14:59:26 -0800270 return page;
271}
272
Tom Sepezda8189e2015-01-30 14:41:50 -0800273FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800274 int width = static_cast<int>(FPDF_GetPageWidth(page));
275 int height = static_cast<int>(FPDF_GetPageHeight(page));
276 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
277 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
278 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800279 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800280 return bitmap;
281}
282
Tom Sepezda8189e2015-01-30 14:41:50 -0800283void EmbedderTest::UnloadPage(FPDF_PAGE page) {
284 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
285 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800286 FPDF_ClosePage(page);
287}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800288
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800289// static
290void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
291 int type) {
292 EmbedderTest* test = static_cast<EmbedderTest*>(info);
293 test->delegate_->UnsupportedHandler(type);
294}
295
296// static
297int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
298 FPDF_WIDESTRING message,
299 FPDF_WIDESTRING title,
300 int type,
301 int icon) {
302 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
303 return test->delegate_->Alert(message, title, type, icon);
304}
305
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700306// static
307int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
308 int msecs, TimerCallback fn) {
309 EmbedderTest* test = static_cast<EmbedderTest*>(info);
310 return test->delegate_->SetTimer(msecs, fn);
311}
312
313// static
314void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
315 EmbedderTest* test = static_cast<EmbedderTest*>(info);
316 return test->delegate_->KillTimer(id);
317}
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}