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 "../fpdfsdk/include/fpdftext.h"
18 #include "../fpdfsdk/include/fpdfview.h"
19 #include "../core/include/fxcrt/fx_system.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "v8/include/v8.h"
22 #include "v8/include/libplatform/libplatform.h"
25 #define snprintf _snprintf
26 #define PATH_SEPARATOR '\\'
28 #define PATH_SEPARATOR '/'
33 const char* g_exe_path_ = nullptr;
35 // Reads the entire contents of a file into a newly malloc'd buffer.
36 static char* GetFileContents(const char* filename, size_t* retlen) {
37 FILE* file = fopen(filename, "rb");
39 fprintf(stderr, "Failed to open: %s\n", filename);
42 (void) fseek(file, 0, SEEK_END);
43 size_t file_length = ftell(file);
47 (void) fseek(file, 0, SEEK_SET);
48 char* buffer = (char*) malloc(file_length);
52 size_t bytes_read = fread(buffer, 1, file_length, file);
54 if (bytes_read != file_length) {
55 fprintf(stderr, "Failed to read: %s\n", filename);
63 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
64 // Returns the full path for an external V8 data file based on either
65 // the currect exectuable path or an explicit override.
66 static std::string GetFullPathForSnapshotFile(const std::string& exe_path,
67 const std::string& filename) {
69 if (!exe_path.empty()) {
70 size_t last_separator = exe_path.rfind(PATH_SEPARATOR);
71 if (last_separator != std::string::npos) {
72 result = exe_path.substr(0, last_separator + 1);
79 // Reads an extenal V8 data file from the |options|-indicated location,
80 // returing true on success and false on error.
81 static bool GetExternalData(const std::string& exe_path,
82 const std::string& filename,
83 v8::StartupData* result_data) {
84 std::string full_path = GetFullPathForSnapshotFile(exe_path, filename);
85 size_t data_length = 0;
86 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
90 result_data->data = const_cast<const char*>(data_buffer);
91 result_data->raw_size = data_length;
94 #endif // V8_USE_EXTERNAL_STARTUP_DATA
98 class EmbedderTestDefaultDelegate : public EmbedderTest::Delegate {
100 int Alert(FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) override {
101 printf("Form_Alert called.\n");
105 void UnsupportedHandler(int type) {
106 std::string feature = "Unknown";
108 case FPDF_UNSP_DOC_XFAFORM:
111 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
112 feature = "Portfolios_Packages";
114 case FPDF_UNSP_DOC_ATTACHMENT:
115 case FPDF_UNSP_ANNOT_ATTACHMENT:
116 feature = "Attachment";
118 case FPDF_UNSP_DOC_SECURITY:
119 feature = "Rights_Management";
121 case FPDF_UNSP_DOC_SHAREDREVIEW:
122 feature = "Shared_Review";
124 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
125 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
126 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
127 feature = "Shared_Form";
129 case FPDF_UNSP_ANNOT_3DANNOT:
132 case FPDF_UNSP_ANNOT_MOVIE:
135 case FPDF_UNSP_ANNOT_SOUND:
138 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
139 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
142 case FPDF_UNSP_ANNOT_SIG:
143 feature = "Digital_Signature";
146 printf("Unsupported feature: %s.\n", feature.c_str());
152 TestLoader(const char* pBuf, size_t len);
158 TestLoader::TestLoader(const char* pBuf, size_t len)
159 : m_pBuf(pBuf), m_Len(len) {
162 int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
163 unsigned long size) {
164 TestLoader* pLoader = (TestLoader*) param;
165 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
166 memcpy(pBuf, pLoader->m_pBuf + pos, size);
170 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
174 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
177 EmbedderTest::EmbedderTest() :
179 form_handle_(nullptr),
183 file_contents_(nullptr) {
184 memset(&hints_, 0, sizeof(hints_));
185 memset(&file_access_, 0, sizeof(file_access_));
186 memset(&file_avail_, 0, sizeof(file_avail_));
187 default_delegate_ = new EmbedderTestDefaultDelegate();
188 delegate_ = default_delegate_;
191 EmbedderTest::~EmbedderTest() {
192 delete default_delegate_;
195 void EmbedderTest::SetUp() {
196 v8::V8::InitializeICU();
197 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
198 v8::V8::InitializePlatform(platform);
199 v8::V8::Initialize();
201 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
202 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_));
203 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_));
204 v8::V8::SetNativesDataBlob(&natives_);
205 v8::V8::SetSnapshotDataBlob(&snapshot_);
206 #endif // V8_USE_EXTERNAL_STARTUP_DATA
210 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
211 memset(info, 0, sizeof(UNSUPPORT_INFO));
213 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
214 FSDK_SetUnSpObjProcessHandler(info);
217 void EmbedderTest::TearDown() {
219 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
220 FPDFDOC_ExitFormFillEnvironment(form_handle_);
223 FPDF_CloseDocument(document_);
225 FPDFAvail_Destroy(avail_);
226 FPDF_DestroyLibrary();
230 if (file_contents_) {
231 free(file_contents_);
233 v8::V8::ShutdownPlatform();
236 bool EmbedderTest::OpenDocument(const std::string& filename) {
237 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
238 if (!file_contents_) {
242 loader_ = new TestLoader(file_contents_, file_length_);
243 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
244 file_access_.m_GetBlock = Get_Block;
245 file_access_.m_Param = loader_;
247 file_avail_.version = 1;
248 file_avail_.IsDataAvail = Is_Data_Avail;
251 hints_.AddSegment = Add_Segment;
253 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
254 (void) FPDFAvail_IsDocAvail(avail_, &hints_);
256 if (!FPDFAvail_IsLinearized(avail_)) {
257 document_ = FPDF_LoadCustomDocument(&file_access_, NULL);
259 document_ = FPDFAvail_GetDocument(avail_, NULL);
264 (void) FPDF_LoadXFA(document_);
265 (void) FPDF_GetDocPermissions(document_);
266 (void) FPDFAvail_IsFormAvail(avail_, &hints_);
268 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
269 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
270 platform->version = 1;
271 platform->app_alert = AlertTrampoline;
273 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
274 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
275 formfillinfo->version = 1;
276 formfillinfo->m_pJsPlatform = platform;
278 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
279 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
280 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
285 void EmbedderTest::DoOpenActions() {
286 FORM_DoDocumentJSAction(form_handle_);
287 FORM_DoDocumentOpenAction(form_handle_);
290 int EmbedderTest::GetFirstPageNum() {
291 int first_page = FPDFAvail_GetFirstPageNum(document_);
292 (void) FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
296 int EmbedderTest::GetPageCount() {
297 int page_count = FPDF_GetPageCount(document_);
298 for (int i = 0; i < page_count; ++i) {
299 (void) FPDFAvail_IsPageAvail(avail_, i, &hints_);
304 FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
305 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
309 FORM_OnAfterLoadPage(page, form_handle_);
310 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
314 FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
315 int width = static_cast<int>(FPDF_GetPageWidth(page));
316 int height = static_cast<int>(FPDF_GetPageHeight(page));
317 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
318 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
319 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
320 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
324 void EmbedderTest::UnloadPage(FPDF_PAGE page) {
325 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
326 FORM_OnBeforeClosePage(page, form_handle_);
327 FPDF_ClosePage(page);
331 void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
333 EmbedderTest* test = static_cast<EmbedderTest*>(info);
334 test->delegate_->UnsupportedHandler(type);
338 int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
339 FPDF_WIDESTRING message,
340 FPDF_WIDESTRING title,
343 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
344 return test->delegate_->Alert(message, title, type, icon);
347 // Can't use gtest-provided main since we need to stash the path to the
348 // executable in order to find the external V8 binary data files.
349 int main(int argc, char** argv) {
350 g_exe_path_ = argv[0];
351 testing::InitGoogleTest(&argc, argv);
352 testing::InitGoogleMock(&argc, argv);
353 return RUN_ALL_TESTS();