blob: 5dc5a66eb644fdf0c180e1bce5d141744ae3dcad [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()
126 : document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800127 form_handle_(nullptr),
128 avail_(nullptr),
129 loader_(nullptr),
130 file_length_(0),
131 file_contents_(nullptr) {
132 memset(&hints_, 0, sizeof(hints_));
133 memset(&file_access_, 0, sizeof(file_access_));
134 memset(&file_avail_, 0, sizeof(file_avail_));
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700135 default_delegate_ = new EmbedderTest::Delegate();
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800136 delegate_ = default_delegate_;
137}
138
139EmbedderTest::~EmbedderTest() {
140 delete default_delegate_;
141}
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
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 FPDF_InitLibrary();
Tom Sepez96d13342015-01-16 14:59:26 -0800163
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
165 memset(info, 0, sizeof(UNSUPPORT_INFO));
166 info->version = 1;
167 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
168 FSDK_SetUnSpObjProcessHandler(info);
169}
Tom Sepez96d13342015-01-16 14:59:26 -0800170
171void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800172 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700173 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700174
175 // Note: The shut down order here is the reverse of the non-XFA branch
176 // order. Need to work out if this is required, and if it is, the lifetimes
177 // of objects owned by |doc| that |form| reference.
Tom Sepezda8189e2015-01-30 14:41:50 -0800178 FPDF_CloseDocument(document_);
Lei Zhangd27acae2015-05-15 15:36:02 -0700179 FPDFDOC_ExitFormFillEnvironment(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800180 }
Tom Sepez96d13342015-01-16 14:59:26 -0800181 FPDFAvail_Destroy(avail_);
182 FPDF_DestroyLibrary();
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700183 v8::V8::ShutdownPlatform();
184 delete platform_;
Lei Zhangd27acae2015-05-15 15:36:02 -0700185 delete loader_;
186 free(file_contents_);
Tom Sepez96d13342015-01-16 14:59:26 -0800187}
188
189bool EmbedderTest::OpenDocument(const std::string& filename) {
190 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
191 if (!file_contents_) {
192 return false;
193 }
194
195 loader_ = new TestLoader(file_contents_, file_length_);
196 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
197 file_access_.m_GetBlock = Get_Block;
198 file_access_.m_Param = loader_;
199
200 file_avail_.version = 1;
201 file_avail_.IsDataAvail = Is_Data_Avail;
202
203 hints_.version = 1;
204 hints_.AddSegment = Add_Segment;
205
206 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 (void)FPDFAvail_IsDocAvail(avail_, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800208
209 if (!FPDFAvail_IsLinearized(avail_)) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700210 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
Tom Sepez96d13342015-01-16 14:59:26 -0800211 } else {
Lei Zhangd27acae2015-05-15 15:36:02 -0700212 document_ = FPDFAvail_GetDocument(avail_, nullptr);
Tom Sepez96d13342015-01-16 14:59:26 -0800213 }
214 if (!document_) {
215 return false;
216 }
JUN FANG827a1722015-03-05 13:39:21 -0800217 int docType = DOCTYPE_PDF;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 if (FPDF_HasXFAField(document_, &docType)) {
JUN FANG827a1722015-03-05 13:39:21 -0800219 if (docType != DOCTYPE_PDF)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 (void)FPDF_LoadXFA(document_);
JUN FANG827a1722015-03-05 13:39:21 -0800221 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 (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 Sepez396e8722015-09-09 10:16:08 -0700235 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800236 formfillinfo->m_pJsPlatform = platform;
Tom Sepez96d13342015-01-16 14:59:26 -0800237
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800238 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
Tom Sepezda8189e2015-01-30 14:41:50 -0800239 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
240 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
241
242 return true;
Tom Sepez96d13342015-01-16 14:59:26 -0800243}
244
Tom Sepezda8189e2015-01-30 14:41:50 -0800245void EmbedderTest::DoOpenActions() {
246 FORM_DoDocumentJSAction(form_handle_);
247 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800248}
249
250int EmbedderTest::GetFirstPageNum() {
251 int first_page = FPDFAvail_GetFirstPageNum(document_);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800253 return first_page;
254}
255
256int EmbedderTest::GetPageCount() {
257 int page_count = FPDF_GetPageCount(document_);
258 for (int i = 0; i < page_count; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 (void)FPDFAvail_IsPageAvail(avail_, i, &hints_);
Tom Sepez96d13342015-01-16 14:59:26 -0800260 }
261 return page_count;
262}
263
Tom Sepezda8189e2015-01-30 14:41:50 -0800264FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez96d13342015-01-16 14:59:26 -0800265 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
266 if (!page) {
267 return nullptr;
268 }
Tom Sepezda8189e2015-01-30 14:41:50 -0800269 FORM_OnAfterLoadPage(page, form_handle_);
270 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
Tom Sepez96d13342015-01-16 14:59:26 -0800271 return page;
272}
273
Tom Sepez396e8722015-09-09 10:16:08 -0700274FPDF_PAGE EmbedderTest::LoadAndCachePage(int page_number) {
275 FPDF_PAGE page = delegate_->GetPage(form_handle_, document_, page_number);
276 if (!page) {
277 return nullptr;
278 }
279 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
280 return page;
281}
282
Tom Sepezda8189e2015-01-30 14:41:50 -0800283FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Tom Sepez96d13342015-01-16 14:59:26 -0800284 int width = static_cast<int>(FPDF_GetPageWidth(page));
285 int height = static_cast<int>(FPDF_GetPageHeight(page));
286 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
287 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
288 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepezda8189e2015-01-30 14:41:50 -0800289 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
Tom Sepez96d13342015-01-16 14:59:26 -0800290 return bitmap;
291}
292
Tom Sepezda8189e2015-01-30 14:41:50 -0800293void EmbedderTest::UnloadPage(FPDF_PAGE page) {
294 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
295 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800296 FPDF_ClosePage(page);
297}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800298
Tom Sepez396e8722015-09-09 10:16:08 -0700299FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMHANDLE form_handle,
300 FPDF_DOCUMENT document,
301 int page_index) {
302 auto it = m_pageMap.find(page_index);
303 if (it != m_pageMap.end()) {
304 return it->second;
305 }
306 FPDF_PAGE page = FPDF_LoadPage(document, page_index);
307 if (!page) {
308 return nullptr;
309 }
310 m_pageMap[page_index] = page;
311 FORM_OnAfterLoadPage(page, form_handle);
312 return page;
313}
314
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800315// static
316void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
317 int type) {
318 EmbedderTest* test = static_cast<EmbedderTest*>(info);
319 test->delegate_->UnsupportedHandler(type);
320}
321
322// static
323int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
324 FPDF_WIDESTRING message,
325 FPDF_WIDESTRING title,
326 int type,
327 int icon) {
328 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
329 return test->delegate_->Alert(message, title, type, icon);
330}
331
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700332// static
333int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 int msecs,
335 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700336 EmbedderTest* test = static_cast<EmbedderTest*>(info);
337 return test->delegate_->SetTimer(msecs, fn);
338}
339
340// static
341void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
342 EmbedderTest* test = static_cast<EmbedderTest*>(info);
343 return test->delegate_->KillTimer(id);
344}
345
Tom Sepez396e8722015-09-09 10:16:08 -0700346// static
347FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
348 FPDF_DOCUMENT document,
349 int page_index) {
350 EmbedderTest* test = static_cast<EmbedderTest*>(info);
Tom Sepez436977e2015-10-02 09:16:40 -0700351 return test->delegate_->GetPage(test->form_handle(), document, page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700352}
353
Tom Sepez1b1bb492015-01-22 17:36:32 -0800354// Can't use gtest-provided main since we need to stash the path to the
355// executable in order to find the external V8 binary data files.
356int main(int argc, char** argv) {
357 g_exe_path_ = argv[0];
358 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800359 testing::InitGoogleMock(&argc, argv);
Tom Sepez1b1bb492015-01-22 17:36:32 -0800360 return RUN_ALL_TESTS();
361}