OLD | NEW |
1 // Copyright (c) 2015 PDFium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "embedder_test.h" | 5 #include "embedder_test.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 const char* g_exe_path_ = nullptr; | 32 const char* g_exe_path_ = nullptr; |
33 | 33 |
34 // Reads the entire contents of a file into a newly malloc'd buffer. | 34 // Reads the entire contents of a file into a newly malloc'd buffer. |
35 static char* GetFileContents(const char* filename, size_t* retlen) { | 35 static char* GetFileContents(const char* filename, size_t* retlen) { |
36 FILE* file = fopen(filename, "rb"); | 36 FILE* file = fopen(filename, "rb"); |
37 if (!file) { | 37 if (!file) { |
38 fprintf(stderr, "Failed to open: %s\n", filename); | 38 fprintf(stderr, "Failed to open: %s\n", filename); |
39 return nullptr; | 39 return nullptr; |
40 } | 40 } |
41 (void) fseek(file, 0, SEEK_END); | 41 (void)fseek(file, 0, SEEK_END); |
42 size_t file_length = ftell(file); | 42 size_t file_length = ftell(file); |
43 if (!file_length) { | 43 if (!file_length) { |
44 return nullptr; | 44 return nullptr; |
45 } | 45 } |
46 (void) fseek(file, 0, SEEK_SET); | 46 (void)fseek(file, 0, SEEK_SET); |
47 char* buffer = (char*) malloc(file_length); | 47 char* buffer = (char*)malloc(file_length); |
48 if (!buffer) { | 48 if (!buffer) { |
49 return nullptr; | 49 return nullptr; |
50 } | 50 } |
51 size_t bytes_read = fread(buffer, 1, file_length, file); | 51 size_t bytes_read = fread(buffer, 1, file_length, file); |
52 (void) fclose(file); | 52 (void)fclose(file); |
53 if (bytes_read != file_length) { | 53 if (bytes_read != file_length) { |
54 fprintf(stderr, "Failed to read: %s\n", filename); | 54 fprintf(stderr, "Failed to read: %s\n", filename); |
55 free(buffer); | 55 free(buffer); |
56 return nullptr; | 56 return nullptr; |
57 } | 57 } |
58 *retlen = bytes_read; | 58 *retlen = bytes_read; |
59 return buffer; | 59 return buffer; |
60 } | 60 } |
61 | 61 |
62 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 62 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
63 // Returns the full path for an external V8 data file based on either | 63 // Returns the full path for an external V8 data file based on either |
64 // the currect exectuable path or an explicit override. | 64 // the currect exectuable path or an explicit override. |
65 static std::string GetFullPathForSnapshotFile(const std::string& exe_path, | 65 static std::string GetFullPathForSnapshotFile(const std::string& exe_path, |
66 const std::string& filename) { | 66 const std::string& filename) { |
67 std::string result; | 67 std::string result; |
68 if (!exe_path.empty()) { | 68 if (!exe_path.empty()) { |
69 size_t last_separator = exe_path.rfind(PATH_SEPARATOR); | 69 size_t last_separator = exe_path.rfind(PATH_SEPARATOR); |
70 if (last_separator != std::string::npos) { | 70 if (last_separator != std::string::npos) { |
71 result = exe_path.substr(0, last_separator + 1); | 71 result = exe_path.substr(0, last_separator + 1); |
72 } | 72 } |
73 } | 73 } |
74 result += filename; | 74 result += filename; |
75 return result; | 75 return result; |
76 } | 76 } |
77 | 77 |
78 // Reads an extenal V8 data file from the |options|-indicated location, | 78 // Reads an extenal V8 data file from the |options|-indicated location, |
79 // returing true on success and false on error. | 79 // returing true on success and false on error. |
80 static bool GetExternalData(const std::string& exe_path, | 80 static bool GetExternalData(const std::string& exe_path, |
(...skipping 15 matching lines...) Expand all Loading... |
96 | 96 |
97 class TestLoader { | 97 class TestLoader { |
98 public: | 98 public: |
99 TestLoader(const char* pBuf, size_t len); | 99 TestLoader(const char* pBuf, size_t len); |
100 | 100 |
101 const char* m_pBuf; | 101 const char* m_pBuf; |
102 size_t m_Len; | 102 size_t m_Len; |
103 }; | 103 }; |
104 | 104 |
105 TestLoader::TestLoader(const char* pBuf, size_t len) | 105 TestLoader::TestLoader(const char* pBuf, size_t len) |
106 : m_pBuf(pBuf), m_Len(len) { | 106 : m_pBuf(pBuf), m_Len(len) {} |
107 } | |
108 | 107 |
109 int Get_Block(void* param, unsigned long pos, unsigned char* pBuf, | 108 int Get_Block(void* param, |
| 109 unsigned long pos, |
| 110 unsigned char* pBuf, |
110 unsigned long size) { | 111 unsigned long size) { |
111 TestLoader* pLoader = (TestLoader*) param; | 112 TestLoader* pLoader = (TestLoader*)param; |
112 if (pos + size < pos || pos + size > pLoader->m_Len) return 0; | 113 if (pos + size < pos || pos + size > pLoader->m_Len) |
| 114 return 0; |
113 memcpy(pBuf, pLoader->m_pBuf + pos, size); | 115 memcpy(pBuf, pLoader->m_pBuf + pos, size); |
114 return 1; | 116 return 1; |
115 } | 117 } |
116 | 118 |
117 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { | 119 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
118 return true; | 120 return true; |
119 } | 121 } |
120 | 122 |
121 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { | 123 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {} |
122 } | |
123 | 124 |
124 EmbedderTest::EmbedderTest() : | 125 EmbedderTest::EmbedderTest() |
125 document_(nullptr), | 126 : document_(nullptr), |
126 form_handle_(nullptr), | 127 form_handle_(nullptr), |
127 avail_(nullptr), | 128 avail_(nullptr), |
128 loader_(nullptr), | 129 loader_(nullptr), |
129 file_length_(0), | 130 file_length_(0), |
130 file_contents_(nullptr) { | 131 file_contents_(nullptr) { |
131 memset(&hints_, 0, sizeof(hints_)); | 132 memset(&hints_, 0, sizeof(hints_)); |
132 memset(&file_access_, 0, sizeof(file_access_)); | 133 memset(&file_access_, 0, sizeof(file_access_)); |
133 memset(&file_avail_, 0, sizeof(file_avail_)); | 134 memset(&file_avail_, 0, sizeof(file_avail_)); |
134 default_delegate_ = new EmbedderTest::Delegate(); | 135 default_delegate_ = new EmbedderTest::Delegate(); |
135 delegate_ = default_delegate_; | 136 delegate_ = default_delegate_; |
136 } | 137 } |
137 | 138 |
138 EmbedderTest::~EmbedderTest() { | 139 EmbedderTest::~EmbedderTest() { |
139 delete default_delegate_; | 140 delete default_delegate_; |
140 } | 141 } |
141 | 142 |
142 void EmbedderTest::SetUp() { | 143 void EmbedderTest::SetUp() { |
143 v8::V8::InitializeICU(); | 144 v8::V8::InitializeICU(); |
144 | 145 |
145 platform_ = v8::platform::CreateDefaultPlatform(); | 146 platform_ = v8::platform::CreateDefaultPlatform(); |
146 v8::V8::InitializePlatform(platform_); | 147 v8::V8::InitializePlatform(platform_); |
147 v8::V8::Initialize(); | 148 v8::V8::Initialize(); |
148 | 149 |
149 // By enabling predictable mode, V8 won't post any background tasks. | 150 // By enabling predictable mode, V8 won't post any background tasks. |
150 const char predictable_flag[] = "--predictable"; | 151 const char predictable_flag[] = "--predictable"; |
151 v8::V8::SetFlagsFromString(predictable_flag, | 152 v8::V8::SetFlagsFromString(predictable_flag, |
152 static_cast<int>(strlen(predictable_flag))); | 153 static_cast<int>(strlen(predictable_flag))); |
153 | 154 |
154 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 155 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
155 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_)); | 156 ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_)); |
156 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_)); | 157 ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_)); |
157 v8::V8::SetNativesDataBlob(&natives_); | 158 v8::V8::SetNativesDataBlob(&natives_); |
158 v8::V8::SetSnapshotDataBlob(&snapshot_); | 159 v8::V8::SetSnapshotDataBlob(&snapshot_); |
159 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 160 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
160 | 161 |
161 FPDF_InitLibrary(); | 162 FPDF_InitLibrary(); |
162 | 163 |
163 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this); | 164 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this); |
164 memset(info, 0, sizeof(UNSUPPORT_INFO)); | 165 memset(info, 0, sizeof(UNSUPPORT_INFO)); |
165 info->version = 1; | 166 info->version = 1; |
166 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline; | 167 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline; |
167 FSDK_SetUnSpObjProcessHandler(info); | 168 FSDK_SetUnSpObjProcessHandler(info); |
168 } | 169 } |
169 | 170 |
170 void EmbedderTest::TearDown() { | 171 void EmbedderTest::TearDown() { |
171 if (document_) { | 172 if (document_) { |
172 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC); | 173 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC); |
173 FPDFDOC_ExitFormFillEnvironment(form_handle_); | 174 FPDFDOC_ExitFormFillEnvironment(form_handle_); |
174 FPDF_CloseDocument(document_); | 175 FPDF_CloseDocument(document_); |
175 } | 176 } |
176 FPDFAvail_Destroy(avail_); | 177 FPDFAvail_Destroy(avail_); |
177 FPDF_DestroyLibrary(); | 178 FPDF_DestroyLibrary(); |
178 v8::V8::ShutdownPlatform(); | 179 v8::V8::ShutdownPlatform(); |
(...skipping 13 matching lines...) Expand all Loading... |
192 file_access_.m_GetBlock = Get_Block; | 193 file_access_.m_GetBlock = Get_Block; |
193 file_access_.m_Param = loader_; | 194 file_access_.m_Param = loader_; |
194 | 195 |
195 file_avail_.version = 1; | 196 file_avail_.version = 1; |
196 file_avail_.IsDataAvail = Is_Data_Avail; | 197 file_avail_.IsDataAvail = Is_Data_Avail; |
197 | 198 |
198 hints_.version = 1; | 199 hints_.version = 1; |
199 hints_.AddSegment = Add_Segment; | 200 hints_.AddSegment = Add_Segment; |
200 | 201 |
201 avail_ = FPDFAvail_Create(&file_avail_, &file_access_); | 202 avail_ = FPDFAvail_Create(&file_avail_, &file_access_); |
202 (void) FPDFAvail_IsDocAvail(avail_, &hints_); | 203 (void)FPDFAvail_IsDocAvail(avail_, &hints_); |
203 | 204 |
204 if (!FPDFAvail_IsLinearized(avail_)) { | 205 if (!FPDFAvail_IsLinearized(avail_)) { |
205 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr); | 206 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr); |
206 } else { | 207 } else { |
207 document_ = FPDFAvail_GetDocument(avail_, nullptr); | 208 document_ = FPDFAvail_GetDocument(avail_, nullptr); |
208 } | 209 } |
209 | 210 |
210 (void) FPDF_GetDocPermissions(document_); | 211 (void)FPDF_GetDocPermissions(document_); |
211 (void) FPDFAvail_IsFormAvail(avail_, &hints_); | 212 (void)FPDFAvail_IsFormAvail(avail_, &hints_); |
212 | 213 |
213 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this); | 214 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this); |
214 memset(platform, 0, sizeof(IPDF_JSPLATFORM)); | 215 memset(platform, 0, sizeof(IPDF_JSPLATFORM)); |
215 platform->version = 2; | 216 platform->version = 2; |
216 platform->app_alert = AlertTrampoline; | 217 platform->app_alert = AlertTrampoline; |
217 | 218 |
218 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this); | 219 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this); |
219 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO)); | 220 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO)); |
220 formfillinfo->version = 1; | 221 formfillinfo->version = 1; |
221 formfillinfo->FFI_SetTimer = SetTimerTrampoline; | 222 formfillinfo->FFI_SetTimer = SetTimerTrampoline; |
222 formfillinfo->FFI_KillTimer = KillTimerTrampoline; | 223 formfillinfo->FFI_KillTimer = KillTimerTrampoline; |
223 formfillinfo->m_pJsPlatform = platform; | 224 formfillinfo->m_pJsPlatform = platform; |
224 | 225 |
225 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo); | 226 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo); |
226 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD); | 227 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD); |
227 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100); | 228 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100); |
228 | 229 |
229 return true; | 230 return true; |
230 } | 231 } |
231 | 232 |
232 void EmbedderTest::DoOpenActions() { | 233 void EmbedderTest::DoOpenActions() { |
233 FORM_DoDocumentJSAction(form_handle_); | 234 FORM_DoDocumentJSAction(form_handle_); |
234 FORM_DoDocumentOpenAction(form_handle_); | 235 FORM_DoDocumentOpenAction(form_handle_); |
235 } | 236 } |
236 | 237 |
237 int EmbedderTest::GetFirstPageNum() { | 238 int EmbedderTest::GetFirstPageNum() { |
238 int first_page = FPDFAvail_GetFirstPageNum(document_); | 239 int first_page = FPDFAvail_GetFirstPageNum(document_); |
239 (void) FPDFAvail_IsPageAvail(avail_, first_page, &hints_); | 240 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_); |
240 return first_page; | 241 return first_page; |
241 } | 242 } |
242 | 243 |
243 int EmbedderTest::GetPageCount() { | 244 int EmbedderTest::GetPageCount() { |
244 int page_count = FPDF_GetPageCount(document_); | 245 int page_count = FPDF_GetPageCount(document_); |
245 for (int i = 0; i < page_count; ++i) { | 246 for (int i = 0; i < page_count; ++i) { |
246 (void) FPDFAvail_IsPageAvail(avail_, i, &hints_); | 247 (void)FPDFAvail_IsPageAvail(avail_, i, &hints_); |
247 } | 248 } |
248 return page_count; | 249 return page_count; |
249 } | 250 } |
250 | 251 |
251 FPDF_PAGE EmbedderTest::LoadPage(int page_number) { | 252 FPDF_PAGE EmbedderTest::LoadPage(int page_number) { |
252 FPDF_PAGE page = FPDF_LoadPage(document_, page_number); | 253 FPDF_PAGE page = FPDF_LoadPage(document_, page_number); |
253 if (!page) { | 254 if (!page) { |
254 return nullptr; | 255 return nullptr; |
255 } | 256 } |
256 FORM_OnAfterLoadPage(page, form_handle_); | 257 FORM_OnAfterLoadPage(page, form_handle_); |
(...skipping 29 matching lines...) Expand all Loading... |
286 FPDF_WIDESTRING message, | 287 FPDF_WIDESTRING message, |
287 FPDF_WIDESTRING title, | 288 FPDF_WIDESTRING title, |
288 int type, | 289 int type, |
289 int icon) { | 290 int icon) { |
290 EmbedderTest* test = static_cast<EmbedderTest*>(platform); | 291 EmbedderTest* test = static_cast<EmbedderTest*>(platform); |
291 return test->delegate_->Alert(message, title, type, icon); | 292 return test->delegate_->Alert(message, title, type, icon); |
292 } | 293 } |
293 | 294 |
294 // static | 295 // static |
295 int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info, | 296 int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info, |
296 int msecs, TimerCallback fn) { | 297 int msecs, |
| 298 TimerCallback fn) { |
297 EmbedderTest* test = static_cast<EmbedderTest*>(info); | 299 EmbedderTest* test = static_cast<EmbedderTest*>(info); |
298 return test->delegate_->SetTimer(msecs, fn); | 300 return test->delegate_->SetTimer(msecs, fn); |
299 } | 301 } |
300 | 302 |
301 // static | 303 // static |
302 void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) { | 304 void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) { |
303 EmbedderTest* test = static_cast<EmbedderTest*>(info); | 305 EmbedderTest* test = static_cast<EmbedderTest*>(info); |
304 return test->delegate_->KillTimer(id); | 306 return test->delegate_->KillTimer(id); |
305 } | 307 } |
306 | 308 |
307 // Can't use gtest-provided main since we need to stash the path to the | 309 // Can't use gtest-provided main since we need to stash the path to the |
308 // executable in order to find the external V8 binary data files. | 310 // executable in order to find the external V8 binary data files. |
309 int main(int argc, char** argv) { | 311 int main(int argc, char** argv) { |
310 g_exe_path_ = argv[0]; | 312 g_exe_path_ = argv[0]; |
311 testing::InitGoogleTest(&argc, argv); | 313 testing::InitGoogleTest(&argc, argv); |
312 testing::InitGoogleMock(&argc, argv); | 314 testing::InitGoogleMock(&argc, argv); |
313 return RUN_ALL_TESTS(); | 315 return RUN_ALL_TESTS(); |
314 } | 316 } |
OLD | NEW |