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"
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
97 class EmbedderTestDefaultDelegate : public EmbedderTest::Delegate {
99 int Alert(FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) override {
100 printf("Form_Alert called.\n");
104 void UnsupportedHandler(int type) {
105 std::string feature = "Unknown";
107 case FPDF_UNSP_DOC_XFAFORM:
110 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
111 feature = "Portfolios_Packages";
113 case FPDF_UNSP_DOC_ATTACHMENT:
114 case FPDF_UNSP_ANNOT_ATTACHMENT:
115 feature = "Attachment";
117 case FPDF_UNSP_DOC_SECURITY:
118 feature = "Rights_Management";
120 case FPDF_UNSP_DOC_SHAREDREVIEW:
121 feature = "Shared_Review";
123 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
124 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
125 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
126 feature = "Shared_Form";
128 case FPDF_UNSP_ANNOT_3DANNOT:
131 case FPDF_UNSP_ANNOT_MOVIE:
134 case FPDF_UNSP_ANNOT_SOUND:
137 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
138 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
141 case FPDF_UNSP_ANNOT_SIG:
142 feature = "Digital_Signature";
145 printf("Unsupported feature: %s.\n", feature.c_str());
151 TestLoader(const char* pBuf, size_t len);
157 TestLoader::TestLoader(const char* pBuf, size_t len)
158 : m_pBuf(pBuf), m_Len(len) {
161 int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
162 unsigned long size) {
163 TestLoader* pLoader = (TestLoader*) param;
164 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
165 memcpy(pBuf, pLoader->m_pBuf + pos, size);
169 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
173 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
176 EmbedderTest::EmbedderTest() :
178 form_handle_(nullptr),
182 file_contents_(nullptr) {
183 memset(&hints_, 0, sizeof(hints_));
184 memset(&file_access_, 0, sizeof(file_access_));
185 memset(&file_avail_, 0, sizeof(file_avail_));
186 default_delegate_ = new EmbedderTestDefaultDelegate();
187 delegate_ = default_delegate_;
190 EmbedderTest::~EmbedderTest() {
191 delete default_delegate_;
194 void EmbedderTest::SetUp() {
195 v8::V8::InitializeICU();
197 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
198 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_));
199 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_));
200 v8::V8::SetNativesDataBlob(&natives_);
201 v8::V8::SetSnapshotDataBlob(&snapshot_);
202 #endif // V8_USE_EXTERNAL_STARTUP_DATA
206 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
207 memset(info, 0, sizeof(UNSUPPORT_INFO));
209 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
210 FSDK_SetUnSpObjProcessHandler(info);
213 void EmbedderTest::TearDown() {
215 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
216 FPDFDOC_ExitFormFillEnvironment(form_handle_);
219 FPDF_CloseDocument(document_);
221 FPDFAvail_Destroy(avail_);
222 FPDF_DestroyLibrary();
226 if (file_contents_) {
227 free(file_contents_);
231 bool EmbedderTest::OpenDocument(const std::string& filename) {
232 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
233 if (!file_contents_) {
237 loader_ = new TestLoader(file_contents_, file_length_);
238 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
239 file_access_.m_GetBlock = Get_Block;
240 file_access_.m_Param = loader_;
242 file_avail_.version = 1;
243 file_avail_.IsDataAvail = Is_Data_Avail;
246 hints_.AddSegment = Add_Segment;
248 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
249 (void) FPDFAvail_IsDocAvail(avail_, &hints_);
251 if (!FPDFAvail_IsLinearized(avail_)) {
252 document_ = FPDF_LoadCustomDocument(&file_access_, NULL);
254 document_ = FPDFAvail_GetDocument(avail_, NULL);
259 (void) FPDF_LoadXFA(document_);
260 (void) FPDF_GetDocPermissions(document_);
261 (void) FPDFAvail_IsFormAvail(avail_, &hints_);
263 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
264 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
265 platform->version = 1;
266 platform->app_alert = AlertTrampoline;
268 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
269 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
270 formfillinfo->version = 1;
271 formfillinfo->m_pJsPlatform = platform;
273 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
274 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
275 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
280 void EmbedderTest::DoOpenActions() {
281 FORM_DoDocumentJSAction(form_handle_);
282 FORM_DoDocumentOpenAction(form_handle_);
285 int EmbedderTest::GetFirstPageNum() {
286 int first_page = FPDFAvail_GetFirstPageNum(document_);
287 (void) FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
291 int EmbedderTest::GetPageCount() {
292 int page_count = FPDF_GetPageCount(document_);
293 for (int i = 0; i < page_count; ++i) {
294 (void) FPDFAvail_IsPageAvail(avail_, i, &hints_);
299 FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
300 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
304 FORM_OnAfterLoadPage(page, form_handle_);
305 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
309 FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
310 int width = static_cast<int>(FPDF_GetPageWidth(page));
311 int height = static_cast<int>(FPDF_GetPageHeight(page));
312 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
313 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
314 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
315 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
319 void EmbedderTest::UnloadPage(FPDF_PAGE page) {
320 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
321 FORM_OnBeforeClosePage(page, form_handle_);
322 FPDF_ClosePage(page);
326 void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
328 EmbedderTest* test = static_cast<EmbedderTest*>(info);
329 test->delegate_->UnsupportedHandler(type);
333 int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
334 FPDF_WIDESTRING message,
335 FPDF_WIDESTRING title,
338 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
339 return test->delegate_->Alert(message, title, type, icon);
342 // Can't use gtest-provided main since we need to stash the path to the
343 // executable in order to find the external V8 binary data files.
344 int main(int argc, char** argv) {
345 g_exe_path_ = argv[0];
346 testing::InitGoogleTest(&argc, argv);
347 testing::InitGoogleMock(&argc, argv);
348 return RUN_ALL_TESTS();