1 // 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.
5 #include "embedder_test.h"
17 #include "../public/fpdf_text.h"
18 #include "../public/fpdfview.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "v8/include/libplatform/libplatform.h"
21 #include "v8/include/v8.h"
24 #define snprintf _snprintf
25 #define PATH_SEPARATOR '\\'
27 #define PATH_SEPARATOR '/'
32 const char* g_exe_path_ = nullptr;
34 // Reads the entire contents of a file into a newly malloc'd buffer.
35 static char* GetFileContents(const char* filename, size_t* retlen) {
36 FILE* file = fopen(filename, "rb");
38 fprintf(stderr, "Failed to open: %s\n", filename);
41 (void)fseek(file, 0, SEEK_END);
42 size_t file_length = ftell(file);
46 (void)fseek(file, 0, SEEK_SET);
47 char* buffer = (char*)malloc(file_length);
51 size_t bytes_read = fread(buffer, 1, file_length, file);
53 if (bytes_read != file_length) {
54 fprintf(stderr, "Failed to read: %s\n", filename);
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.
65 static std::string GetFullPathForSnapshotFile(const std::string& exe_path,
66 const std::string& filename) {
68 if (!exe_path.empty()) {
69 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
70 if (last_separator != std::string::npos) {
71 result = exe_path.substr(0, last_separator + 1);
78 // Reads an extenal V8 data file from the |options|-indicated location,
79 // returing true on success and false on error.
80 static bool GetExternalData(const std::string& exe_path,
81 const std::string& filename,
82 v8::StartupData* result_data) {
83 std::string full_path = GetFullPathForSnapshotFile(exe_path, filename);
84 size_t data_length = 0;
85 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
89 result_data->data = const_cast<const char*>(data_buffer);
90 result_data->raw_size = data_length;
93 #endif // V8_USE_EXTERNAL_STARTUP_DATA
99 TestLoader(const char* pBuf, size_t len);
105 TestLoader::TestLoader(const char* pBuf, size_t len)
106 : m_pBuf(pBuf), m_Len(len) {}
108 int Get_Block(void* param,
111 unsigned long size) {
112 TestLoader* pLoader = (TestLoader*)param;
113 if (pos + size < pos || pos + size > pLoader->m_Len)
115 memcpy(pBuf, pLoader->m_pBuf + pos, size);
119 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
123 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {}
125 EmbedderTest::EmbedderTest()
126 : document_(nullptr),
127 form_handle_(nullptr),
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_));
135 default_delegate_ = new EmbedderTest::Delegate();
136 delegate_ = default_delegate_;
139 EmbedderTest::~EmbedderTest() {
140 delete default_delegate_;
143 void EmbedderTest::SetUp() {
144 v8::V8::InitializeICU();
146 platform_ = v8::platform::CreateDefaultPlatform();
147 v8::V8::InitializePlatform(platform_);
148 v8::V8::Initialize();
150 // 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)));
155 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
156 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_);
160 #endif // V8_USE_EXTERNAL_STARTUP_DATA
164 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
165 memset(info, 0, sizeof(UNSUPPORT_INFO));
167 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
168 FSDK_SetUnSpObjProcessHandler(info);
171 void EmbedderTest::TearDown() {
173 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
174 FPDFDOC_ExitFormFillEnvironment(form_handle_);
175 FPDF_CloseDocument(document_);
177 FPDFAvail_Destroy(avail_);
178 FPDF_DestroyLibrary();
179 v8::V8::ShutdownPlatform();
182 free(file_contents_);
185 bool EmbedderTest::OpenDocument(const std::string& filename) {
186 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
187 if (!file_contents_) {
191 loader_ = new TestLoader(file_contents_, file_length_);
192 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
193 file_access_.m_GetBlock = Get_Block;
194 file_access_.m_Param = loader_;
196 file_avail_.version = 1;
197 file_avail_.IsDataAvail = Is_Data_Avail;
200 hints_.AddSegment = Add_Segment;
202 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
203 (void)FPDFAvail_IsDocAvail(avail_, &hints_);
205 if (!FPDFAvail_IsLinearized(avail_)) {
206 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
208 document_ = FPDFAvail_GetDocument(avail_, nullptr);
211 (void)FPDF_GetDocPermissions(document_);
212 (void)FPDFAvail_IsFormAvail(avail_, &hints_);
214 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
215 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
216 platform->version = 2;
217 platform->app_alert = AlertTrampoline;
219 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
220 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
221 formfillinfo->version = 1;
222 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
223 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
224 formfillinfo->FFI_GetPage = GetPageTrampoline;
225 formfillinfo->m_pJsPlatform = platform;
227 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
228 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
229 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
234 void EmbedderTest::DoOpenActions() {
235 FORM_DoDocumentJSAction(form_handle_);
236 FORM_DoDocumentOpenAction(form_handle_);
239 int EmbedderTest::GetFirstPageNum() {
240 int first_page = FPDFAvail_GetFirstPageNum(document_);
241 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
245 int EmbedderTest::GetPageCount() {
246 int page_count = FPDF_GetPageCount(document_);
247 for (int i = 0; i < page_count; ++i) {
248 (void)FPDFAvail_IsPageAvail(avail_, i, &hints_);
253 FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
254 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
258 FORM_OnAfterLoadPage(page, form_handle_);
259 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
263 FPDF_PAGE EmbedderTest::LoadAndCachePage(int page_number) {
264 FPDF_PAGE page = delegate_->GetPage(form_handle_, document_, page_number);
268 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
272 FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
273 int width = static_cast<int>(FPDF_GetPageWidth(page));
274 int height = static_cast<int>(FPDF_GetPageHeight(page));
275 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
276 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
277 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
278 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
282 void EmbedderTest::UnloadPage(FPDF_PAGE page) {
283 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
284 FORM_OnBeforeClosePage(page, form_handle_);
285 FPDF_ClosePage(page);
288 FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMHANDLE form_handle,
289 FPDF_DOCUMENT document,
291 auto it = m_pageMap.find(page_index);
292 if (it != m_pageMap.end()) {
295 FPDF_PAGE page = FPDF_LoadPage(document, page_index);
299 m_pageMap[page_index] = page;
300 FORM_OnAfterLoadPage(page, form_handle);
305 void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
307 EmbedderTest* test = static_cast<EmbedderTest*>(info);
308 test->delegate_->UnsupportedHandler(type);
312 int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
313 FPDF_WIDESTRING message,
314 FPDF_WIDESTRING title,
317 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
318 return test->delegate_->Alert(message, title, type, icon);
322 int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
325 EmbedderTest* test = static_cast<EmbedderTest*>(info);
326 return test->delegate_->SetTimer(msecs, fn);
330 void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
331 EmbedderTest* test = static_cast<EmbedderTest*>(info);
332 return test->delegate_->KillTimer(id);
336 FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
337 FPDF_DOCUMENT document,
339 EmbedderTest* test = static_cast<EmbedderTest*>(info);
340 return test->delegate_->GetPage(test->form_handle(), document, page_index);
343 // Can't use gtest-provided main since we need to stash the path to the
344 // executable in order to find the external V8 binary data files.
345 int main(int argc, char** argv) {
346 g_exe_path_ = argv[0];
347 testing::InitGoogleTest(&argc, argv);
348 testing::InitGoogleMock(&argc, argv);
349 return RUN_ALL_TESTS();