Lei Zhang | 1ac47eb | 2015-12-21 11:04:44 -0800 | [diff] [blame] | 1 | // Copyright 2015 PDFium Authors. All rights reserved. |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 5 | #include "testing/embedder_test.h" |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 6 | |
| 7 | #include <limits.h> |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 8 | |
| 9 | #include <list> |
Lei Zhang | 9f72c45 | 2018-02-08 21:49:54 +0000 | [diff] [blame] | 10 | #include <map> |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 11 | #include <memory> |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <utility> |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 14 | |
Lei Zhang | d145e4b | 2018-10-12 18:54:31 +0000 | [diff] [blame] | 15 | #include "core/fdrm/fx_crypt.h" |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 16 | #include "public/cpp/fpdf_scopers.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 17 | #include "public/fpdf_dataavail.h" |
Lei Zhang | 453d96b | 2015-12-31 13:13:10 -0800 | [diff] [blame] | 18 | #include "public/fpdf_edit.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 19 | #include "public/fpdf_text.h" |
| 20 | #include "public/fpdfview.h" |
Tom Sepez | a310e00 | 2015-02-27 13:03:07 -0800 | [diff] [blame] | 21 | #include "testing/gmock/include/gmock/gmock.h" |
Lei Zhang | d50bdff | 2019-02-05 19:42:33 +0000 | [diff] [blame] | 22 | #include "testing/test_loader.h" |
Henrique Nakashima | f956bad | 2018-08-16 16:41:42 +0000 | [diff] [blame] | 23 | #include "testing/utils/bitmap_saver.h" |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 24 | #include "testing/utils/file_util.h" |
Lei Zhang | 4c64e96 | 2019-02-05 19:24:12 +0000 | [diff] [blame] | 25 | #include "testing/utils/hash.h" |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 26 | #include "testing/utils/path_service.h" |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 27 | #include "third_party/base/logging.h" |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 28 | #include "third_party/base/ptr_util.h" |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 29 | #include "third_party/base/stl_util.h" |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 30 | |
| 31 | #ifdef PDF_ENABLE_V8 |
Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 32 | #include "v8/include/v8-platform.h" |
Dan Sinclair | 61046b9 | 2016-02-18 14:48:48 -0500 | [diff] [blame] | 33 | #include "v8/include/v8.h" |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 34 | #endif // PDF_ENABLE_V8 |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 35 | |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 36 | namespace { |
thestig | c08cd7a | 2016-06-27 09:47:59 -0700 | [diff] [blame] | 37 | |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 38 | int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) { |
| 39 | const int format = FPDFBitmap_GetFormat(bitmap); |
| 40 | switch (format) { |
| 41 | case FPDFBitmap_Gray: |
| 42 | return 1; |
| 43 | case FPDFBitmap_BGR: |
| 44 | return 3; |
| 45 | case FPDFBitmap_BGRx: |
| 46 | case FPDFBitmap_BGRA: |
| 47 | return 4; |
| 48 | default: |
| 49 | ASSERT(false); |
| 50 | return 0; |
| 51 | } |
| 52 | } |
| 53 | |
thestig | bcd3e53 | 2016-11-21 13:37:28 -0800 | [diff] [blame] | 54 | } // namespace |
| 55 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 56 | EmbedderTest::EmbedderTest() |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 57 | : default_delegate_(pdfium::MakeUnique<EmbedderTest::Delegate>()), |
| 58 | delegate_(default_delegate_.get()) { |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 59 | FPDF_FILEWRITE::version = 1; |
| 60 | FPDF_FILEWRITE::WriteBlock = WriteBlockCallback; |
Tom Sepez | f288bb1 | 2015-11-20 12:12:46 -0800 | [diff] [blame] | 61 | } |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 62 | |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 63 | EmbedderTest::~EmbedderTest() {} |
Tom Sepez | f288bb1 | 2015-11-20 12:12:46 -0800 | [diff] [blame] | 64 | |
| 65 | void EmbedderTest::SetUp() { |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 66 | FPDF_LIBRARY_CONFIG config; |
| 67 | config.version = 2; |
| 68 | config.m_pUserFontPaths = nullptr; |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 69 | config.m_v8EmbedderSlot = 0; |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 70 | config.m_pIsolate = external_isolate_; |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 71 | FPDF_InitLibraryWithConfig(&config); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 72 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 73 | UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this); |
| 74 | memset(info, 0, sizeof(UNSUPPORT_INFO)); |
| 75 | info->version = 1; |
| 76 | info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline; |
| 77 | FSDK_SetUnSpObjProcessHandler(info); |
Henrique Nakashima | 9fa5036 | 2017-11-10 22:40:44 +0000 | [diff] [blame] | 78 | |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 79 | saved_document_ = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 80 | } |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 81 | |
| 82 | void EmbedderTest::TearDown() { |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 83 | // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as |
| 84 | // possible. This can fail when an ASSERT test fails in a test case. |
| 85 | EXPECT_EQ(0U, page_map_.size()); |
Lei Zhang | 9f72c45 | 2018-02-08 21:49:54 +0000 | [diff] [blame] | 86 | EXPECT_EQ(0U, saved_page_map_.size()); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 87 | |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 88 | if (document_) { |
Lei Zhang | d27acae | 2015-05-15 15:36:02 -0700 | [diff] [blame] | 89 | FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC); |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 90 | FPDFDOC_ExitFormFillEnvironment(form_handle_); |
| 91 | FPDF_CloseDocument(document_); |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 92 | } |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 93 | |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 94 | FPDFAvail_Destroy(avail_); |
| 95 | FPDF_DestroyLibrary(); |
Lei Zhang | b3be4a1 | 2019-02-05 22:11:07 +0000 | [diff] [blame] | 96 | loader_.reset(); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Lei Zhang | 378ec54 | 2018-10-18 19:15:47 +0000 | [diff] [blame] | 99 | #ifdef PDF_ENABLE_V8 |
| 100 | void EmbedderTest::SetExternalIsolate(void* isolate) { |
| 101 | external_isolate_ = static_cast<v8::Isolate*>(isolate); |
| 102 | } |
| 103 | #endif // PDF_ENABLE_V8 |
| 104 | |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 105 | bool EmbedderTest::CreateEmptyDocument() { |
| 106 | document_ = FPDF_CreateNewDocument(); |
| 107 | if (!document_) |
| 108 | return false; |
| 109 | |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 110 | form_handle_ = |
| 111 | SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript); |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 112 | return true; |
| 113 | } |
| 114 | |
Lei Zhang | 208eecf | 2017-12-20 19:40:50 +0000 | [diff] [blame] | 115 | bool EmbedderTest::OpenDocument(const std::string& filename) { |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 116 | return OpenDocumentWithOptions(filename, nullptr, |
| 117 | LinearizeOption::kDefaultLinearize, |
| 118 | JavaScriptOption::kEnableJavaScript); |
Lei Zhang | 208eecf | 2017-12-20 19:40:50 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) { |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 122 | return OpenDocumentWithOptions(filename, nullptr, |
| 123 | LinearizeOption::kMustLinearize, |
| 124 | JavaScriptOption::kEnableJavaScript); |
Lei Zhang | 208eecf | 2017-12-20 19:40:50 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename, |
| 128 | const char* password) { |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 129 | return OpenDocumentWithOptions(filename, password, |
| 130 | LinearizeOption::kDefaultLinearize, |
| 131 | JavaScriptOption::kEnableJavaScript); |
| 132 | } |
| 133 | |
| 134 | bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) { |
| 135 | return OpenDocumentWithOptions(filename, nullptr, |
| 136 | LinearizeOption::kDefaultLinearize, |
| 137 | JavaScriptOption::kDisableJavaScript); |
Lei Zhang | 208eecf | 2017-12-20 19:40:50 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename, |
| 141 | const char* password, |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 142 | LinearizeOption linearize_option, |
| 143 | JavaScriptOption javascript_option) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 144 | std::string file_path; |
| 145 | if (!PathService::GetTestFilePath(filename, &file_path)) |
| 146 | return false; |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 147 | |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 148 | file_contents_ = GetFileContents(file_path.c_str(), &file_length_); |
Dan Sinclair | 6be2aab | 2015-10-28 13:58:49 -0400 | [diff] [blame] | 149 | if (!file_contents_) |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 150 | return false; |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 151 | |
thestig | 29ce923 | 2016-06-22 07:03:23 -0700 | [diff] [blame] | 152 | EXPECT_TRUE(!loader_); |
Lei Zhang | b3be4a1 | 2019-02-05 22:11:07 +0000 | [diff] [blame] | 153 | loader_ = pdfium::MakeUnique<TestLoader>( |
| 154 | pdfium::make_span(file_contents_.get(), file_length_)); |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 155 | |
| 156 | memset(&file_access_, 0, sizeof(file_access_)); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 157 | file_access_.m_FileLen = static_cast<unsigned long>(file_length_); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 158 | file_access_.m_GetBlock = TestLoader::GetBlock; |
Lei Zhang | b3be4a1 | 2019-02-05 22:11:07 +0000 | [diff] [blame] | 159 | file_access_.m_Param = loader_.get(); |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 160 | |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 161 | fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_); |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 162 | return OpenDocumentHelper(password, linearize_option, javascript_option, |
| 163 | fake_file_access_.get(), &document_, &avail_, |
| 164 | &form_handle_); |
Nicolas Pena | 56fc972 | 2017-07-13 16:31:34 -0400 | [diff] [blame] | 165 | } |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 166 | |
Nicolas Pena | 56fc972 | 2017-07-13 16:31:34 -0400 | [diff] [blame] | 167 | bool EmbedderTest::OpenDocumentHelper(const char* password, |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 168 | LinearizeOption linearize_option, |
| 169 | JavaScriptOption javascript_option, |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 170 | FakeFileAccess* network_simulator, |
Nicolas Pena | 56fc972 | 2017-07-13 16:31:34 -0400 | [diff] [blame] | 171 | FPDF_DOCUMENT* document, |
| 172 | FPDF_AVAIL* avail, |
| 173 | FPDF_FORMHANDLE* form_handle) { |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 174 | network_simulator->AddSegment(0, 1024); |
| 175 | network_simulator->SetRequestedDataAvailable(); |
| 176 | *avail = FPDFAvail_Create(network_simulator->GetFileAvail(), |
| 177 | network_simulator->GetFileAccess()); |
Nicolas Pena | 56fc972 | 2017-07-13 16:31:34 -0400 | [diff] [blame] | 178 | if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) { |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 179 | int32_t nRet = PDF_DATA_NOTAVAIL; |
| 180 | while (nRet == PDF_DATA_NOTAVAIL) { |
| 181 | network_simulator->SetRequestedDataAvailable(); |
| 182 | nRet = |
| 183 | FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints()); |
| 184 | } |
| 185 | if (nRet == PDF_DATA_ERROR) |
| 186 | return false; |
| 187 | |
Nicolas Pena | 56fc972 | 2017-07-13 16:31:34 -0400 | [diff] [blame] | 188 | *document = FPDFAvail_GetDocument(*avail, password); |
| 189 | if (!*document) |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 190 | return false; |
Nicolas Pena | 56fc972 | 2017-07-13 16:31:34 -0400 | [diff] [blame] | 191 | |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 192 | nRet = PDF_DATA_NOTAVAIL; |
| 193 | while (nRet == PDF_DATA_NOTAVAIL) { |
| 194 | network_simulator->SetRequestedDataAvailable(); |
| 195 | nRet = |
| 196 | FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints()); |
| 197 | } |
| 198 | if (nRet == PDF_FORM_ERROR) |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 199 | return false; |
Nicolas Pena | 56fc972 | 2017-07-13 16:31:34 -0400 | [diff] [blame] | 200 | |
| 201 | int page_count = FPDF_GetPageCount(*document); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 202 | for (int i = 0; i < page_count; ++i) { |
| 203 | nRet = PDF_DATA_NOTAVAIL; |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 204 | while (nRet == PDF_DATA_NOTAVAIL) { |
| 205 | network_simulator->SetRequestedDataAvailable(); |
| 206 | nRet = FPDFAvail_IsPageAvail(*avail, i, |
| 207 | network_simulator->GetDownloadHints()); |
| 208 | } |
Nicolas Pena | 56fc972 | 2017-07-13 16:31:34 -0400 | [diff] [blame] | 209 | |
| 210 | if (nRet == PDF_DATA_ERROR) |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 211 | return false; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 212 | } |
| 213 | } else { |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 214 | if (linearize_option == LinearizeOption::kMustLinearize) |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 215 | return false; |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 216 | network_simulator->SetWholeFileAvailable(); |
| 217 | *document = |
| 218 | FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password); |
Nicolas Pena | 56fc972 | 2017-07-13 16:31:34 -0400 | [diff] [blame] | 219 | if (!*document) |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 220 | return false; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 221 | } |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 222 | *form_handle = SetupFormFillEnvironment(*document, javascript_option); |
| 223 | |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 224 | #ifdef PDF_ENABLE_XFA |
Ryan Harrison | 854d71c | 2017-10-18 12:28:14 -0400 | [diff] [blame] | 225 | int doc_type = FPDF_GetFormType(*document); |
| 226 | if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND) |
| 227 | FPDF_LoadXFA(*document); |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 228 | #endif // PDF_ENABLE_XFA |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 229 | |
Ryan Harrison | 6cec70a | 2018-06-05 14:06:10 +0000 | [diff] [blame] | 230 | (void)FPDF_GetDocPermissions(*document); |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 231 | return true; |
| 232 | } |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 233 | |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 234 | FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment( |
| 235 | FPDF_DOCUMENT doc, |
| 236 | JavaScriptOption javascript_option) { |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 237 | IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 238 | memset(platform, '\0', sizeof(IPDF_JSPLATFORM)); |
Jochen Eisinger | 06b6002 | 2015-07-30 17:44:35 +0200 | [diff] [blame] | 239 | platform->version = 2; |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 240 | platform->app_alert = AlertTrampoline; |
Dan Sinclair | 14aacd5 | 2017-05-18 14:11:29 -0400 | [diff] [blame] | 241 | platform->m_isolate = external_isolate_; |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 242 | |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 243 | FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this); |
| 244 | memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO)); |
Lei Zhang | cd39695 | 2015-11-04 20:26:50 -0800 | [diff] [blame] | 245 | #ifdef PDF_ENABLE_XFA |
| 246 | formfillinfo->version = 2; |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 247 | #else // PDF_ENABLE_XFA |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 248 | formfillinfo->version = 1; |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 249 | #endif // PDF_ENABLE_XFA |
Tom Sepez | 6efc0ad | 2015-06-02 17:11:18 -0700 | [diff] [blame] | 250 | formfillinfo->FFI_SetTimer = SetTimerTrampoline; |
| 251 | formfillinfo->FFI_KillTimer = KillTimerTrampoline; |
Tom Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 252 | formfillinfo->FFI_GetPage = GetPageTrampoline; |
Henrique Nakashima | 9ef93d0 | 2018-10-03 18:41:03 +0000 | [diff] [blame] | 253 | formfillinfo->FFI_DoURIAction = DoURIActionTrampoline; |
| 254 | |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 255 | if (javascript_option == JavaScriptOption::kEnableJavaScript) |
| 256 | formfillinfo->m_pJsPlatform = platform; |
| 257 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 258 | FPDF_FORMHANDLE form_handle = |
| 259 | FPDFDOC_InitFormFillEnvironment(doc, formfillinfo); |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 260 | FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN, |
| 261 | 0xFFE4DD); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 262 | FPDF_SetFormFieldHighlightAlpha(form_handle, 100); |
| 263 | return form_handle; |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 266 | void EmbedderTest::DoOpenActions() { |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 267 | ASSERT(form_handle_); |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 268 | FORM_DoDocumentJSAction(form_handle_); |
| 269 | FORM_DoDocumentOpenAction(form_handle_); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | int EmbedderTest::GetFirstPageNum() { |
| 273 | int first_page = FPDFAvail_GetFirstPageNum(document_); |
Ryan Harrison | 6cec70a | 2018-06-05 14:06:10 +0000 | [diff] [blame] | 274 | (void)FPDFAvail_IsPageAvail(avail_, first_page, |
| 275 | fake_file_access_->GetDownloadHints()); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 276 | return first_page; |
| 277 | } |
| 278 | |
| 279 | int EmbedderTest::GetPageCount() { |
| 280 | int page_count = FPDF_GetPageCount(document_); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 281 | for (int i = 0; i < page_count; ++i) |
Ryan Harrison | 6cec70a | 2018-06-05 14:06:10 +0000 | [diff] [blame] | 282 | (void)FPDFAvail_IsPageAvail(avail_, i, |
| 283 | fake_file_access_->GetDownloadHints()); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 284 | return page_count; |
| 285 | } |
| 286 | |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 287 | FPDF_PAGE EmbedderTest::LoadPage(int page_number) { |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 288 | return LoadPageCommon(page_number, true); |
| 289 | } |
| 290 | |
| 291 | FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) { |
| 292 | return LoadPageCommon(page_number, false); |
| 293 | } |
| 294 | |
| 295 | FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) { |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 296 | ASSERT(form_handle_); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 297 | ASSERT(page_number >= 0); |
| 298 | ASSERT(!pdfium::ContainsKey(page_map_, page_number)); |
weili | 0dadcc6 | 2016-08-23 21:10:57 -0700 | [diff] [blame] | 299 | |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 300 | FPDF_PAGE page = FPDF_LoadPage(document_, page_number); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 301 | if (!page) |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 302 | return nullptr; |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 303 | |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 304 | if (do_events) { |
| 305 | FORM_OnAfterLoadPage(page, form_handle_); |
| 306 | FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN); |
| 307 | } |
weili | 0dadcc6 | 2016-08-23 21:10:57 -0700 | [diff] [blame] | 308 | page_map_[page_number] = page; |
Tom Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 309 | return page; |
| 310 | } |
| 311 | |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 312 | void EmbedderTest::UnloadPage(FPDF_PAGE page) { |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 313 | UnloadPageCommon(page, true); |
| 314 | } |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 315 | |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 316 | void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) { |
| 317 | UnloadPageCommon(page, false); |
| 318 | } |
| 319 | |
| 320 | void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) { |
| 321 | ASSERT(form_handle_); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 322 | int page_number = GetPageNumberForLoadedPage(page); |
| 323 | if (page_number < 0) { |
| 324 | NOTREACHED(); |
| 325 | return; |
| 326 | } |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 327 | if (do_events) { |
| 328 | FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE); |
| 329 | FORM_OnBeforeClosePage(page, form_handle_); |
| 330 | } |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 331 | FPDF_ClosePage(page); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 332 | page_map_.erase(page_number); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 333 | } |
Tom Sepez | 1b1bb49 | 2015-01-22 17:36:32 -0800 | [diff] [blame] | 334 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 335 | ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) { |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 336 | return RenderLoadedPageWithFlags(page, 0); |
| 337 | } |
| 338 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 339 | ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page, |
| 340 | int flags) { |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 341 | if (GetPageNumberForLoadedPage(page) < 0) { |
| 342 | NOTREACHED(); |
| 343 | return nullptr; |
| 344 | } |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 345 | return RenderPageWithFlags(page, form_handle_, flags); |
| 346 | } |
| 347 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 348 | ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) { |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 349 | return RenderSavedPageWithFlags(page, 0); |
| 350 | } |
| 351 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 352 | ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page, |
| 353 | int flags) { |
Lei Zhang | 9f72c45 | 2018-02-08 21:49:54 +0000 | [diff] [blame] | 354 | if (GetPageNumberForSavedPage(page) < 0) { |
| 355 | NOTREACHED(); |
| 356 | return nullptr; |
| 357 | } |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 358 | return RenderPageWithFlags(page, saved_form_handle_, flags); |
| 359 | } |
| 360 | |
| 361 | // static |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 362 | ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page, |
| 363 | FPDF_FORMHANDLE handle, |
| 364 | int flags) { |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 365 | int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 366 | int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 367 | int alpha = FPDFPage_HasTransparency(page) ? 1 : 0; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 368 | ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha)); |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 369 | FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF; |
| 370 | FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color); |
| 371 | FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags); |
| 372 | FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags); |
| 373 | return bitmap; |
| 374 | } |
| 375 | |
Lei Zhang | 30ff253 | 2019-01-31 21:37:55 +0000 | [diff] [blame] | 376 | // static |
| 377 | ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) { |
| 378 | return RenderPageWithFlags(page, nullptr, 0); |
| 379 | } |
| 380 | |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 381 | FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() { |
| 382 | return OpenSavedDocumentWithPassword(nullptr); |
| 383 | } |
| 384 | |
| 385 | FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword( |
| 386 | const char* password) { |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 387 | memset(&saved_file_access_, 0, sizeof(saved_file_access_)); |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 388 | saved_file_access_.m_FileLen = data_string_.size(); |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 389 | saved_file_access_.m_GetBlock = GetBlockFromString; |
Artem Strygin | 68d04f2 | 2018-07-12 09:18:19 +0000 | [diff] [blame] | 390 | // Copy data to prevent clearing it before saved document close. |
| 391 | saved_document_file_data_ = data_string_; |
| 392 | saved_file_access_.m_Param = &saved_document_file_data_; |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 393 | |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 394 | saved_fake_file_access_ = |
| 395 | pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_); |
| 396 | |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 397 | EXPECT_TRUE(OpenDocumentHelper( |
| 398 | password, LinearizeOption::kDefaultLinearize, |
| 399 | JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(), |
| 400 | &saved_document_, &saved_avail_, &saved_form_handle_)); |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 401 | return saved_document_; |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void EmbedderTest::CloseSavedDocument() { |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 405 | ASSERT(saved_document_); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 406 | |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 407 | FPDFDOC_ExitFormFillEnvironment(saved_form_handle_); |
| 408 | FPDF_CloseDocument(saved_document_); |
| 409 | FPDFAvail_Destroy(saved_avail_); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 410 | |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 411 | saved_form_handle_ = nullptr; |
| 412 | saved_document_ = nullptr; |
| 413 | saved_avail_ = nullptr; |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 414 | } |
| 415 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 416 | FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) { |
Lei Zhang | 9f72c45 | 2018-02-08 21:49:54 +0000 | [diff] [blame] | 417 | ASSERT(saved_form_handle_); |
| 418 | ASSERT(page_number >= 0); |
| 419 | ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number)); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 420 | |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 421 | FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number); |
Lei Zhang | 9f72c45 | 2018-02-08 21:49:54 +0000 | [diff] [blame] | 422 | if (!page) |
| 423 | return nullptr; |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 424 | |
Lei Zhang | 9f72c45 | 2018-02-08 21:49:54 +0000 | [diff] [blame] | 425 | FORM_OnAfterLoadPage(page, saved_form_handle_); |
| 426 | FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN); |
| 427 | saved_page_map_[page_number] = page; |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 428 | return page; |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 429 | } |
| 430 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 431 | void EmbedderTest::CloseSavedPage(FPDF_PAGE page) { |
Lei Zhang | 9f72c45 | 2018-02-08 21:49:54 +0000 | [diff] [blame] | 432 | ASSERT(saved_form_handle_); |
| 433 | |
| 434 | int page_number = GetPageNumberForSavedPage(page); |
| 435 | if (page_number < 0) { |
| 436 | NOTREACHED(); |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE); |
| 441 | FORM_OnBeforeClosePage(page, saved_form_handle_); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 442 | FPDF_ClosePage(page); |
Lei Zhang | 9f72c45 | 2018-02-08 21:49:54 +0000 | [diff] [blame] | 443 | |
| 444 | saved_page_map_.erase(page_number); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 447 | void EmbedderTest::VerifySavedRendering(FPDF_PAGE page, |
| 448 | int width, |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 449 | int height, |
| 450 | const char* md5) { |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 451 | ASSERT(saved_document_); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 452 | ASSERT(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 453 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 454 | ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 455 | CompareBitmap(bitmap.get(), width, height, md5); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 456 | } |
| 457 | |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 458 | void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) { |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 459 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 460 | FPDF_PAGE page = LoadSavedPage(0); |
| 461 | VerifySavedRendering(page, width, height, md5); |
| 462 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 463 | CloseSavedDocument(); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 464 | } |
| 465 | |
Artem Strygin | 0e60b9e | 2017-09-28 18:46:03 +0300 | [diff] [blame] | 466 | void EmbedderTest::SetWholeFileAvailable() { |
| 467 | ASSERT(fake_file_access_); |
| 468 | fake_file_access_->SetWholeFileAvailable(); |
| 469 | } |
| 470 | |
weili | 0dadcc6 | 2016-08-23 21:10:57 -0700 | [diff] [blame] | 471 | FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info, |
Tom Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 472 | FPDF_DOCUMENT document, |
| 473 | int page_index) { |
weili | 0dadcc6 | 2016-08-23 21:10:57 -0700 | [diff] [blame] | 474 | EmbedderTest* test = static_cast<EmbedderTest*>(info); |
| 475 | auto it = test->page_map_.find(page_index); |
| 476 | return it != test->page_map_.end() ? it->second : nullptr; |
Tom Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 479 | // static |
| 480 | void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info, |
| 481 | int type) { |
| 482 | EmbedderTest* test = static_cast<EmbedderTest*>(info); |
| 483 | test->delegate_->UnsupportedHandler(type); |
| 484 | } |
| 485 | |
| 486 | // static |
| 487 | int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform, |
| 488 | FPDF_WIDESTRING message, |
| 489 | FPDF_WIDESTRING title, |
| 490 | int type, |
| 491 | int icon) { |
| 492 | EmbedderTest* test = static_cast<EmbedderTest*>(platform); |
| 493 | return test->delegate_->Alert(message, title, type, icon); |
| 494 | } |
| 495 | |
Tom Sepez | 6efc0ad | 2015-06-02 17:11:18 -0700 | [diff] [blame] | 496 | // static |
| 497 | int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 498 | int msecs, |
| 499 | TimerCallback fn) { |
Tom Sepez | 6efc0ad | 2015-06-02 17:11:18 -0700 | [diff] [blame] | 500 | EmbedderTest* test = static_cast<EmbedderTest*>(info); |
| 501 | return test->delegate_->SetTimer(msecs, fn); |
| 502 | } |
| 503 | |
| 504 | // static |
| 505 | void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) { |
| 506 | EmbedderTest* test = static_cast<EmbedderTest*>(info); |
| 507 | return test->delegate_->KillTimer(id); |
| 508 | } |
| 509 | |
Tom Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 510 | // static |
| 511 | FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info, |
| 512 | FPDF_DOCUMENT document, |
| 513 | int page_index) { |
weili | 0dadcc6 | 2016-08-23 21:10:57 -0700 | [diff] [blame] | 514 | return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document, |
| 515 | page_index); |
Tom Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Henrique Nakashima | 9fa5036 | 2017-11-10 22:40:44 +0000 | [diff] [blame] | 518 | // static |
Henrique Nakashima | 9ef93d0 | 2018-10-03 18:41:03 +0000 | [diff] [blame] | 519 | void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info, |
| 520 | FPDF_BYTESTRING uri) { |
| 521 | EmbedderTest* test = static_cast<EmbedderTest*>(info); |
| 522 | return test->delegate_->DoURIAction(uri); |
| 523 | } |
| 524 | |
| 525 | // static |
Henrique Nakashima | 9fa5036 | 2017-11-10 22:40:44 +0000 | [diff] [blame] | 526 | std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) { |
Dan Sinclair | 957480c | 2017-06-13 15:21:14 -0400 | [diff] [blame] | 527 | uint8_t digest[16]; |
Henrique Nakashima | 9fa5036 | 2017-11-10 22:40:44 +0000 | [diff] [blame] | 528 | CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)), |
| 529 | FPDFBitmap_GetWidth(bitmap) * |
| 530 | GetBitmapBytesPerPixel(bitmap) * |
| 531 | FPDFBitmap_GetHeight(bitmap), |
| 532 | digest); |
Dan Sinclair | 957480c | 2017-06-13 15:21:14 -0400 | [diff] [blame] | 533 | return CryptToBase16(digest); |
| 534 | } |
| 535 | |
Henrique Nakashima | db26957 | 2018-01-16 19:02:15 +0000 | [diff] [blame] | 536 | #ifndef NDEBUG |
| 537 | // static |
| 538 | void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap, |
| 539 | const std::string& filename) { |
Henrique Nakashima | f956bad | 2018-08-16 16:41:42 +0000 | [diff] [blame] | 540 | BitmapSaver::WriteBitmapToPng(bitmap, filename); |
Henrique Nakashima | db26957 | 2018-01-16 19:02:15 +0000 | [diff] [blame] | 541 | } |
| 542 | #endif |
| 543 | |
thestig | bcd3e53 | 2016-11-21 13:37:28 -0800 | [diff] [blame] | 544 | // static |
| 545 | void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap, |
| 546 | int expected_width, |
| 547 | int expected_height, |
| 548 | const char* expected_md5sum) { |
| 549 | ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap)); |
| 550 | ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap)); |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 551 | |
| 552 | // The expected stride is calculated using the same formula as in |
| 553 | // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride. |
| 554 | const int expected_stride = |
| 555 | (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4; |
thestig | bcd3e53 | 2016-11-21 13:37:28 -0800 | [diff] [blame] | 556 | ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap)); |
| 557 | |
| 558 | if (!expected_md5sum) |
| 559 | return; |
| 560 | |
Henrique Nakashima | 9fa5036 | 2017-11-10 22:40:44 +0000 | [diff] [blame] | 561 | EXPECT_EQ(expected_md5sum, HashBitmap(bitmap)); |
thestig | bcd3e53 | 2016-11-21 13:37:28 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 564 | // static |
| 565 | int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite, |
| 566 | const void* data, |
| 567 | unsigned long size) { |
| 568 | EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite); |
Henrique Nakashima | 7c2e8a3 | 2018-06-06 19:17:34 +0000 | [diff] [blame] | 569 | |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 570 | pThis->data_string_.append(static_cast<const char*>(data), size); |
Henrique Nakashima | 7c2e8a3 | 2018-06-06 19:17:34 +0000 | [diff] [blame] | 571 | |
| 572 | if (pThis->filestream_.is_open()) |
| 573 | pThis->filestream_.write(static_cast<const char*>(data), size); |
| 574 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 575 | return 1; |
| 576 | } |
| 577 | |
| 578 | // static |
| 579 | int EmbedderTest::GetBlockFromString(void* param, |
| 580 | unsigned long pos, |
| 581 | unsigned char* buf, |
| 582 | unsigned long size) { |
| 583 | std::string* new_file = static_cast<std::string*>(param); |
Lei Zhang | cef91f1 | 2019-01-08 21:32:51 +0000 | [diff] [blame] | 584 | if (!new_file || pos + size < pos) { |
| 585 | NOTREACHED(); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 586 | return 0; |
Lei Zhang | cef91f1 | 2019-01-08 21:32:51 +0000 | [diff] [blame] | 587 | } |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 588 | |
Lei Zhang | cef91f1 | 2019-01-08 21:32:51 +0000 | [diff] [blame] | 589 | if (pos + size > new_file->size()) { |
| 590 | NOTREACHED(); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 591 | return 0; |
Lei Zhang | cef91f1 | 2019-01-08 21:32:51 +0000 | [diff] [blame] | 592 | } |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 593 | |
| 594 | memcpy(buf, new_file->data() + pos, size); |
| 595 | return 1; |
| 596 | } |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 597 | |
Lei Zhang | 9f72c45 | 2018-02-08 21:49:54 +0000 | [diff] [blame] | 598 | // static |
| 599 | int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map, |
| 600 | FPDF_PAGE page) { |
| 601 | for (const auto& it : page_map) { |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 602 | if (it.second == page) { |
| 603 | int page_number = it.first; |
| 604 | ASSERT(page_number >= 0); |
| 605 | return page_number; |
| 606 | } |
| 607 | } |
| 608 | return -1; |
| 609 | } |
Lei Zhang | 9f72c45 | 2018-02-08 21:49:54 +0000 | [diff] [blame] | 610 | |
| 611 | int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const { |
| 612 | return GetPageNumberForPage(page_map_, page); |
| 613 | } |
| 614 | |
| 615 | int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const { |
| 616 | return GetPageNumberForPage(saved_page_map_, page); |
| 617 | } |
Henrique Nakashima | 7c2e8a3 | 2018-06-06 19:17:34 +0000 | [diff] [blame] | 618 | |
| 619 | void EmbedderTest::OpenPDFFileForWrite(const char* filename) { |
| 620 | filestream_.open(filename, std::ios_base::binary); |
| 621 | } |
| 622 | |
| 623 | void EmbedderTest::ClosePDFFileForWrite() { |
| 624 | filestream_.close(); |
| 625 | } |