Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 PDFium Authors. All rights reserved. | 1 // Copyright 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 "testing/embedder_test.h" | 5 #include "testing/embedder_test.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 InitializeV8ForPDFium(g_exe_path_, std::string(), &natives_, &snapshot_, | 52 InitializeV8ForPDFium(g_exe_path_, std::string(), &natives_, &snapshot_, |
| 53 &platform_); | 53 &platform_); |
| 54 #else | 54 #else |
| 55 InitializeV8ForPDFium(&platform_); | 55 InitializeV8ForPDFium(&platform_); |
| 56 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 56 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 57 #endif // FPDF_ENABLE_V8 | 57 #endif // FPDF_ENABLE_V8 |
| 58 } | 58 } |
| 59 | 59 |
| 60 EmbedderTest::~EmbedderTest() {} | 60 EmbedderTest::~EmbedderTest() {} |
| 61 | 61 |
| 62 // static | |
| 63 bool EmbedderTest::LoadDocument(const char* data, | |
| 64 size_t data_size, | |
| 65 bool must_linearize, | |
| 66 TestLoader* loader, | |
| 67 EmbedderTest* embedder_test, | |
| 68 LoadDocumentData* out_data) { | |
| 69 out_data->file_access->m_FileLen = static_cast<unsigned long>(data_size); | |
| 70 out_data->file_access->m_GetBlock = TestLoader::GetBlock; | |
| 71 out_data->file_access->m_Param = loader; | |
| 72 | |
| 73 out_data->file_avail->version = 1; | |
| 74 out_data->file_avail->IsDataAvail = Is_Data_Avail; | |
| 75 | |
| 76 out_data->hints->version = 1; | |
| 77 out_data->hints->AddSegment = Add_Segment; | |
| 78 | |
| 79 *out_data->avail = | |
| 80 FPDFAvail_Create(out_data->file_avail, out_data->file_access); | |
| 81 | |
| 82 if (FPDFAvail_IsLinearized(*out_data->avail) == PDF_LINEARIZED) { | |
| 83 *out_data->document = FPDFAvail_GetDocument(*out_data->avail, nullptr); | |
| 84 if (!*out_data->document) { | |
| 85 return false; | |
| 86 } | |
| 87 int32_t nRet = PDF_DATA_NOTAVAIL; | |
| 88 while (nRet == PDF_DATA_NOTAVAIL) { | |
| 89 nRet = FPDFAvail_IsDocAvail(*out_data->avail, out_data->hints); | |
| 90 } | |
| 91 if (nRet == PDF_DATA_ERROR) { | |
| 92 return false; | |
| 93 } | |
| 94 nRet = FPDFAvail_IsFormAvail(*out_data->avail, out_data->hints); | |
| 95 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) { | |
| 96 return false; | |
| 97 } | |
| 98 int page_count = FPDF_GetPageCount(*out_data->document); | |
| 99 for (int i = 0; i < page_count; ++i) { | |
| 100 nRet = PDF_DATA_NOTAVAIL; | |
| 101 while (nRet == PDF_DATA_NOTAVAIL) { | |
| 102 nRet = FPDFAvail_IsPageAvail(*out_data->avail, i, out_data->hints); | |
|
Wei Li
2016/01/04 19:01:13
I know this is from the original code, but worth c
Lei Zhang
2016/01/07 02:50:58
Maybe. If you can create a test case that does tha
| |
| 103 } | |
| 104 if (nRet == PDF_DATA_ERROR) { | |
| 105 return false; | |
| 106 } | |
| 107 } | |
| 108 } else { | |
| 109 if (must_linearize) { | |
| 110 return false; | |
| 111 } | |
| 112 *out_data->document = | |
| 113 FPDF_LoadCustomDocument(out_data->file_access, nullptr); | |
| 114 if (!*out_data->document) { | |
| 115 return false; | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 (void)FPDF_GetDocPermissions(*out_data->document); | |
| 120 | |
| 121 if (embedder_test) { | |
| 122 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(embedder_test); | |
| 123 memset(platform, 0, sizeof(IPDF_JSPLATFORM)); | |
| 124 platform->version = 2; | |
| 125 platform->app_alert = AlertTrampoline; | |
| 126 | |
| 127 FPDF_FORMFILLINFO* formfillinfo = | |
| 128 static_cast<FPDF_FORMFILLINFO*>(embedder_test); | |
| 129 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO)); | |
| 130 formfillinfo->version = 1; | |
| 131 formfillinfo->FFI_SetTimer = SetTimerTrampoline; | |
| 132 formfillinfo->FFI_KillTimer = KillTimerTrampoline; | |
| 133 formfillinfo->FFI_GetPage = GetPageTrampoline; | |
| 134 formfillinfo->m_pJsPlatform = platform; | |
| 135 | |
| 136 *out_data->form_handle = | |
| 137 FPDFDOC_InitFormFillEnvironment(*out_data->document, formfillinfo); | |
| 138 FPDF_SetFormFieldHighlightColor(*out_data->form_handle, 0, 0xFFE4DD); | |
| 139 FPDF_SetFormFieldHighlightAlpha(*out_data->form_handle, 100); | |
| 140 } else { | |
| 141 *out_data->form_handle = nullptr; | |
| 142 } | |
| 143 | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 62 void EmbedderTest::SetUp() { | 147 void EmbedderTest::SetUp() { |
| 63 FPDF_LIBRARY_CONFIG config; | 148 FPDF_LIBRARY_CONFIG config; |
| 64 config.version = 2; | 149 config.version = 2; |
| 65 config.m_pUserFontPaths = nullptr; | 150 config.m_pUserFontPaths = nullptr; |
| 66 config.m_v8EmbedderSlot = 0; | 151 config.m_v8EmbedderSlot = 0; |
| 67 config.m_pIsolate = external_isolate_; | 152 config.m_pIsolate = external_isolate_; |
| 68 FPDF_InitLibraryWithConfig(&config); | 153 FPDF_InitLibraryWithConfig(&config); |
| 69 | 154 |
| 70 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this); | 155 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this); |
| 71 memset(info, 0, sizeof(UNSUPPORT_INFO)); | 156 memset(info, 0, sizeof(UNSUPPORT_INFO)); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 95 bool EmbedderTest::OpenDocument(const std::string& filename, | 180 bool EmbedderTest::OpenDocument(const std::string& filename, |
| 96 bool must_linearize) { | 181 bool must_linearize) { |
| 97 std::string file_path; | 182 std::string file_path; |
| 98 if (!PathService::GetTestFilePath(filename, &file_path)) | 183 if (!PathService::GetTestFilePath(filename, &file_path)) |
| 99 return false; | 184 return false; |
| 100 file_contents_ = GetFileContents(file_path.c_str(), &file_length_); | 185 file_contents_ = GetFileContents(file_path.c_str(), &file_length_); |
| 101 if (!file_contents_) | 186 if (!file_contents_) |
| 102 return false; | 187 return false; |
| 103 | 188 |
| 104 loader_ = new TestLoader(file_contents_, file_length_); | 189 loader_ = new TestLoader(file_contents_, file_length_); |
| 105 file_access_.m_FileLen = static_cast<unsigned long>(file_length_); | 190 LoadDocumentData load_data; |
| 106 file_access_.m_GetBlock = TestLoader::GetBlock; | 191 load_data.document = &document_; |
| 107 file_access_.m_Param = loader_; | 192 load_data.file_access = &file_access_; |
| 108 | 193 load_data.file_avail = &file_avail_; |
| 109 file_avail_.version = 1; | 194 load_data.avail = &avail_; |
| 110 file_avail_.IsDataAvail = Is_Data_Avail; | 195 load_data.hints = &hints_; |
| 111 | 196 load_data.form_handle = &form_handle_; |
| 112 hints_.version = 1; | 197 return LoadDocument(file_contents_, file_length_, must_linearize, loader_, |
| 113 hints_.AddSegment = Add_Segment; | 198 this, &load_data); |
| 114 | |
| 115 avail_ = FPDFAvail_Create(&file_avail_, &file_access_); | |
| 116 | |
| 117 if (FPDFAvail_IsLinearized(avail_) == PDF_LINEARIZED) { | |
| 118 document_ = FPDFAvail_GetDocument(avail_, nullptr); | |
| 119 if (!document_) { | |
| 120 return false; | |
| 121 } | |
| 122 int32_t nRet = PDF_DATA_NOTAVAIL; | |
| 123 while (nRet == PDF_DATA_NOTAVAIL) { | |
| 124 nRet = FPDFAvail_IsDocAvail(avail_, &hints_); | |
| 125 } | |
| 126 if (nRet == PDF_DATA_ERROR) { | |
| 127 return false; | |
| 128 } | |
| 129 nRet = FPDFAvail_IsFormAvail(avail_, &hints_); | |
| 130 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) { | |
| 131 return false; | |
| 132 } | |
| 133 int page_count = FPDF_GetPageCount(document_); | |
| 134 for (int i = 0; i < page_count; ++i) { | |
| 135 nRet = PDF_DATA_NOTAVAIL; | |
| 136 while (nRet == PDF_DATA_NOTAVAIL) { | |
| 137 nRet = FPDFAvail_IsPageAvail(avail_, i, &hints_); | |
| 138 } | |
| 139 if (nRet == PDF_DATA_ERROR) { | |
| 140 return false; | |
| 141 } | |
| 142 } | |
| 143 } else { | |
| 144 if (must_linearize) { | |
| 145 return false; | |
| 146 } | |
| 147 document_ = FPDF_LoadCustomDocument(&file_access_, nullptr); | |
| 148 if (!document_) { | |
| 149 return false; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 (void)FPDF_GetDocPermissions(document_); | |
| 154 | |
| 155 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this); | |
| 156 memset(platform, 0, sizeof(IPDF_JSPLATFORM)); | |
| 157 platform->version = 2; | |
| 158 platform->app_alert = AlertTrampoline; | |
| 159 | |
| 160 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this); | |
| 161 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO)); | |
| 162 formfillinfo->version = 1; | |
| 163 formfillinfo->FFI_SetTimer = SetTimerTrampoline; | |
| 164 formfillinfo->FFI_KillTimer = KillTimerTrampoline; | |
| 165 formfillinfo->FFI_GetPage = GetPageTrampoline; | |
| 166 formfillinfo->m_pJsPlatform = platform; | |
| 167 | |
| 168 form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo); | |
| 169 FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD); | |
| 170 FPDF_SetFormFieldHighlightAlpha(form_handle_, 100); | |
| 171 | |
| 172 return true; | |
| 173 } | 199 } |
| 174 | 200 |
| 175 void EmbedderTest::DoOpenActions() { | 201 void EmbedderTest::DoOpenActions() { |
| 176 FORM_DoDocumentJSAction(form_handle_); | 202 FORM_DoDocumentJSAction(form_handle_); |
| 177 FORM_DoDocumentOpenAction(form_handle_); | 203 FORM_DoDocumentOpenAction(form_handle_); |
| 178 } | 204 } |
| 179 | 205 |
| 180 int EmbedderTest::GetFirstPageNum() { | 206 int EmbedderTest::GetFirstPageNum() { |
| 181 int first_page = FPDFAvail_GetFirstPageNum(document_); | 207 int first_page = FPDFAvail_GetFirstPageNum(document_); |
| 182 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_); | 208 (void)FPDFAvail_IsPageAvail(avail_, first_page, &hints_); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 } | 308 } |
| 283 | 309 |
| 284 // Can't use gtest-provided main since we need to stash the path to the | 310 // Can't use gtest-provided main since we need to stash the path to the |
| 285 // executable in order to find the external V8 binary data files. | 311 // executable in order to find the external V8 binary data files. |
| 286 int main(int argc, char** argv) { | 312 int main(int argc, char** argv) { |
| 287 g_exe_path_ = argv[0]; | 313 g_exe_path_ = argv[0]; |
| 288 testing::InitGoogleTest(&argc, argv); | 314 testing::InitGoogleTest(&argc, argv); |
| 289 testing::InitGoogleMock(&argc, argv); | 315 testing::InitGoogleMock(&argc, argv); |
| 290 return RUN_ALL_TESTS(); | 316 return RUN_ALL_TESTS(); |
| 291 } | 317 } |
| OLD | NEW |