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 "../core/include/fxcrt/fx_system.h"
18 #include "../public/fpdf_text.h"
19 #include "../public/fpdfview.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 FPDF_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 FPDF_CloseDocument(document_);
217 FPDFDOC_ExitFormFillEnvironment(form_handle_);
219 FPDFAvail_Destroy(avail_);
220 FPDF_DestroyLibrary();
222 free(file_contents_);
225 bool EmbedderTest::OpenDocument(const std::string& filename) {
226 file_contents_ = GetFileContents(filename.c_str(), &file_length_);
227 if (!file_contents_) {
231 loader_ = new TestLoader(file_contents_, file_length_);
232 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
233 file_access_.m_GetBlock = Get_Block;
234 file_access_.m_Param = loader_;
236 file_avail_.version = 1;
237 file_avail_.IsDataAvail = Is_Data_Avail;
240 hints_.AddSegment = Add_Segment;
242 avail_ = FPDFAvail_Create(&file_avail_, &file_access_);
243 (void) FPDFAvail_IsDocAvail(avail_, &hints_);
245 if (!FPDFAvail_IsLinearized(avail_)) {
246 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr);
248 document_ = FPDFAvail_GetDocument(avail_, nullptr);
251 (void) FPDF_GetDocPermissions(document_);
252 (void) FPDFAvail_IsFormAvail(avail_, &hints_);
254 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
255 memset(platform, 0, sizeof(IPDF_JSPLATFORM));
256 platform->version = 1;
257 platform->app_alert = AlertTrampoline;
259 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
260 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
261 formfillinfo->version = 1;
262 formfillinfo->m_pJsPlatform = platform;
264 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo);
265 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD);
266 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100);
271 void EmbedderTest::DoOpenActions() {
272 FORM_DoDocumentJSAction(form_handle_);
273 FORM_DoDocumentOpenAction(form_handle_);
276 int EmbedderTest::GetFirstPageNum() {
277 int first_page = FPDFAvail_GetFirstPageNum(document_);
278 (void) FPDFAvail_IsPageAvail(avail_, first_page, &hints_);
282 int EmbedderTest::GetPageCount() {
283 int page_count = FPDF_GetPageCount(document_);
284 for (int i = 0; i < page_count; ++i) {
285 (void) FPDFAvail_IsPageAvail(avail_, i, &hints_);
290 FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
291 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
295 FORM_OnAfterLoadPage(page, form_handle_);
296 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
300 FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
301 int width = static_cast<int>(FPDF_GetPageWidth(page));
302 int height = static_cast<int>(FPDF_GetPageHeight(page));
303 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
304 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
305 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
306 FPDF_FFLDraw(form_handle_, bitmap, page, 0, 0, width, height, 0, 0);
310 void EmbedderTest::UnloadPage(FPDF_PAGE page) {
311 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
312 FORM_OnBeforeClosePage(page, form_handle_);
313 FPDF_ClosePage(page);
317 void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
319 EmbedderTest* test = static_cast<EmbedderTest*>(info);
320 test->delegate_->UnsupportedHandler(type);
324 int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
325 FPDF_WIDESTRING message,
326 FPDF_WIDESTRING title,
329 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
330 return test->delegate_->Alert(message, title, type, icon);
333 // Can't use gtest-provided main since we need to stash the path to the
334 // executable in order to find the external V8 binary data files.
335 int main(int argc, char** argv) {
336 g_exe_path_ = argv[0];
337 testing::InitGoogleTest(&argc, argv);
338 testing::InitGoogleMock(&argc, argv);
339 return RUN_ALL_TESTS();