blob: 13a33061f7cf1aa7f5421aaa81a16fba44426f6a [file] [log] [blame]
Lei Zhang1ac47eb2015-12-21 11:04:44 -08001// Copyright 2015 PDFium Authors. All rights reserved.
Tom Sepez96d13342015-01-16 14:59:26 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Wei Li091f7a02015-11-09 12:09:55 -08005#include "testing/embedder_test.h"
Tom Sepez96d13342015-01-16 14:59:26 -08006
7#include <limits.h>
Tom Sepez96d13342015-01-16 14:59:26 -08008
9#include <list>
Lei Zhang9f72c452018-02-08 21:49:54 +000010#include <map>
Lei Zhanga98e3662018-02-07 20:28:35 +000011#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -080012#include <string>
13#include <utility>
Tom Sepez96d13342015-01-16 14:59:26 -080014
Lei Zhangd145e4b2018-10-12 18:54:31 +000015#include "core/fdrm/fx_crypt.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000016#include "public/cpp/fpdf_scopers.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080017#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080018#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080019#include "public/fpdf_text.h"
20#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080021#include "testing/gmock/include/gmock/gmock.h"
Lei Zhangd50bdff2019-02-05 19:42:33 +000022#include "testing/test_loader.h"
Henrique Nakashimaf956bad2018-08-16 16:41:42 +000023#include "testing/utils/bitmap_saver.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000024#include "testing/utils/file_util.h"
Lei Zhang4c64e962019-02-05 19:24:12 +000025#include "testing/utils/hash.h"
Wei Li091f7a02015-11-09 12:09:55 -080026#include "testing/utils/path_service.h"
Lei Zhang75c81712018-02-08 17:22:39 +000027#include "third_party/base/logging.h"
Artem Strygin0e60b9e2017-09-28 18:46:03 +030028#include "third_party/base/ptr_util.h"
Lei Zhang75c81712018-02-08 17:22:39 +000029#include "third_party/base/stl_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070030
31#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080032#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050033#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070034#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080035
Tom Sepez96d13342015-01-16 14:59:26 -080036namespace {
thestigc08cd7a2016-06-27 09:47:59 -070037
Jane Liu28fb7ba2017-08-02 21:45:57 -040038int 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
thestigbcd3e532016-11-21 13:37:28 -080054} // namespace
55
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056EmbedderTest::EmbedderTest()
Lei Zhang0729be22018-02-05 21:13:51 +000057 : default_delegate_(pdfium::MakeUnique<EmbedderTest::Delegate>()),
58 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -040059 FPDF_FILEWRITE::version = 1;
60 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080061}
Tom Sepez96d13342015-01-16 14:59:26 -080062
Dan Sinclair5553d8b2018-01-03 09:44:28 -050063EmbedderTest::~EmbedderTest() {}
Tom Sepezf288bb12015-11-20 12:12:46 -080064
65void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070066 FPDF_LIBRARY_CONFIG config;
67 config.version = 2;
68 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070069 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070070 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070071 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080072
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 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 Nakashima9fa50362017-11-10 22:40:44 +000078
Lei Zhang0729be22018-02-05 21:13:51 +000079 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080}
Tom Sepez96d13342015-01-16 14:59:26 -080081
82void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +000083 // 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 Zhang9f72c452018-02-08 21:49:54 +000086 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang75c81712018-02-08 17:22:39 +000087
Tom Sepezda8189e2015-01-30 14:41:50 -080088 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -070089 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -080090 FPDFDOC_ExitFormFillEnvironment(form_handle_);
91 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -080092 }
Tom Sepezc46d0002015-11-30 15:46:36 -080093
Tom Sepez96d13342015-01-16 14:59:26 -080094 FPDFAvail_Destroy(avail_);
95 FPDF_DestroyLibrary();
Lei Zhangb3be4a12019-02-05 22:11:07 +000096 loader_.reset();
Tom Sepez96d13342015-01-16 14:59:26 -080097}
98
Lei Zhang378ec542018-10-18 19:15:47 +000099#ifdef PDF_ENABLE_V8
100void EmbedderTest::SetExternalIsolate(void* isolate) {
101 external_isolate_ = static_cast<v8::Isolate*>(isolate);
102}
103#endif // PDF_ENABLE_V8
104
Tom Sepezd483eb42016-01-06 10:03:59 -0800105bool EmbedderTest::CreateEmptyDocument() {
106 document_ = FPDF_CreateNewDocument();
107 if (!document_)
108 return false;
109
Tom Sepez0784c732018-04-23 18:02:57 +0000110 form_handle_ =
111 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -0800112 return true;
113}
114
Lei Zhang208eecf2017-12-20 19:40:50 +0000115bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000116 return OpenDocumentWithOptions(filename, nullptr,
117 LinearizeOption::kDefaultLinearize,
118 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000119}
120
121bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000122 return OpenDocumentWithOptions(filename, nullptr,
123 LinearizeOption::kMustLinearize,
124 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000125}
126
127bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
128 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000129 return OpenDocumentWithOptions(filename, password,
130 LinearizeOption::kDefaultLinearize,
131 JavaScriptOption::kEnableJavaScript);
132}
133
134bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
135 return OpenDocumentWithOptions(filename, nullptr,
136 LinearizeOption::kDefaultLinearize,
137 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000138}
139
140bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
141 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000142 LinearizeOption linearize_option,
143 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800144 std::string file_path;
145 if (!PathService::GetTestFilePath(filename, &file_path))
146 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000147
Wei Li091f7a02015-11-09 12:09:55 -0800148 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400149 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800150 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800151
thestig29ce9232016-06-22 07:03:23 -0700152 EXPECT_TRUE(!loader_);
Lei Zhangb3be4a12019-02-05 22:11:07 +0000153 loader_ = pdfium::MakeUnique<TestLoader>(
154 pdfium::make_span(file_contents_.get(), file_length_));
Lei Zhang0729be22018-02-05 21:13:51 +0000155
156 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800157 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700158 file_access_.m_GetBlock = TestLoader::GetBlock;
Lei Zhangb3be4a12019-02-05 22:11:07 +0000159 file_access_.m_Param = loader_.get();
Lei Zhang0729be22018-02-05 21:13:51 +0000160
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300161 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000162 return OpenDocumentHelper(password, linearize_option, javascript_option,
163 fake_file_access_.get(), &document_, &avail_,
164 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400165}
Tom Sepez96d13342015-01-16 14:59:26 -0800166
Nicolas Pena56fc9722017-07-13 16:31:34 -0400167bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000168 LinearizeOption linearize_option,
169 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300170 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400171 FPDF_DOCUMENT* document,
172 FPDF_AVAIL* avail,
173 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300174 network_simulator->AddSegment(0, 1024);
175 network_simulator->SetRequestedDataAvailable();
176 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
177 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400178 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300179 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 Pena56fc9722017-07-13 16:31:34 -0400188 *document = FPDFAvail_GetDocument(*avail, password);
189 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800190 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400191
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300192 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 Fangdf7f3662015-11-10 18:29:18 +0800199 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400200
201 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800202 for (int i = 0; i < page_count; ++i) {
203 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300204 while (nRet == PDF_DATA_NOTAVAIL) {
205 network_simulator->SetRequestedDataAvailable();
206 nRet = FPDFAvail_IsPageAvail(*avail, i,
207 network_simulator->GetDownloadHints());
208 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400209
210 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800211 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800212 }
213 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000214 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800215 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300216 network_simulator->SetWholeFileAvailable();
217 *document =
218 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400219 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800220 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800221 }
Tom Sepez0784c732018-04-23 18:02:57 +0000222 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
223
Tom Sepezc46d0002015-11-30 15:46:36 -0800224#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400225 int doc_type = FPDF_GetFormType(*document);
226 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
227 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800228#endif // PDF_ENABLE_XFA
Tom Sepez0784c732018-04-23 18:02:57 +0000229
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000230 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800231 return true;
232}
Tom Sepez96d13342015-01-16 14:59:26 -0800233
Tom Sepez0784c732018-04-23 18:02:57 +0000234FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
235 FPDF_DOCUMENT doc,
236 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800237 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400238 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200239 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800240 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400241 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800242
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800243 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
244 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800245#ifdef PDF_ENABLE_XFA
246 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800247#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800248 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800249#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700250 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
251 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700252 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000253 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
254
Tom Sepez0784c732018-04-23 18:02:57 +0000255 if (javascript_option == JavaScriptOption::kEnableJavaScript)
256 formfillinfo->m_pJsPlatform = platform;
257
Nicolas Pena3ff54002017-07-05 11:55:35 -0400258 FPDF_FORMHANDLE form_handle =
259 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000260 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
261 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400262 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
263 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800264}
265
Tom Sepezda8189e2015-01-30 14:41:50 -0800266void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400267 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800268 FORM_DoDocumentJSAction(form_handle_);
269 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800270}
271
272int EmbedderTest::GetFirstPageNum() {
273 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000274 (void)FPDFAvail_IsPageAvail(avail_, first_page,
275 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800276 return first_page;
277}
278
279int EmbedderTest::GetPageCount() {
280 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400281 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000282 (void)FPDFAvail_IsPageAvail(avail_, i,
283 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800284 return page_count;
285}
286
Tom Sepezda8189e2015-01-30 14:41:50 -0800287FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000288 return LoadPageCommon(page_number, true);
289}
290
291FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
292 return LoadPageCommon(page_number, false);
293}
294
295FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400296 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000297 ASSERT(page_number >= 0);
298 ASSERT(!pdfium::ContainsKey(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700299
Tom Sepez96d13342015-01-16 14:59:26 -0800300 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400301 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800302 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400303
Tom Sepez507d0192018-11-07 16:37:51 +0000304 if (do_events) {
305 FORM_OnAfterLoadPage(page, form_handle_);
306 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
307 }
weili0dadcc62016-08-23 21:10:57 -0700308 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700309 return page;
310}
311
Tom Sepezda8189e2015-01-30 14:41:50 -0800312void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000313 UnloadPageCommon(page, true);
314}
Lei Zhang75c81712018-02-08 17:22:39 +0000315
Tom Sepez507d0192018-11-07 16:37:51 +0000316void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
317 UnloadPageCommon(page, false);
318}
319
320void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
321 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000322 int page_number = GetPageNumberForLoadedPage(page);
323 if (page_number < 0) {
324 NOTREACHED();
325 return;
326 }
Tom Sepez507d0192018-11-07 16:37:51 +0000327 if (do_events) {
328 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
329 FORM_OnBeforeClosePage(page, form_handle_);
330 }
Tom Sepez96d13342015-01-16 14:59:26 -0800331 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000332 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800333}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800334
Tom Sepeze08d2b12018-04-25 18:49:32 +0000335ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000336 return RenderLoadedPageWithFlags(page, 0);
337}
338
Tom Sepeze08d2b12018-04-25 18:49:32 +0000339ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
340 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000341 if (GetPageNumberForLoadedPage(page) < 0) {
342 NOTREACHED();
343 return nullptr;
344 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000345 return RenderPageWithFlags(page, form_handle_, flags);
346}
347
Tom Sepeze08d2b12018-04-25 18:49:32 +0000348ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000349 return RenderSavedPageWithFlags(page, 0);
350}
351
Tom Sepeze08d2b12018-04-25 18:49:32 +0000352ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
353 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000354 if (GetPageNumberForSavedPage(page) < 0) {
355 NOTREACHED();
356 return nullptr;
357 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000358 return RenderPageWithFlags(page, saved_form_handle_, flags);
359}
360
361// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000362ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
363 FPDF_FORMHANDLE handle,
364 int flags) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000365 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 Sepeze08d2b12018-04-25 18:49:32 +0000368 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000369 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 Zhang30ff2532019-01-31 21:37:55 +0000376// static
377ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
378 return RenderPageWithFlags(page, nullptr, 0);
379}
380
Lei Zhang0b494052019-01-31 21:41:15 +0000381FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
382 return OpenSavedDocumentWithPassword(nullptr);
383}
384
385FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
386 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300387 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000388 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300389 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000390 // 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 Pena3ff54002017-07-05 11:55:35 -0400393
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300394 saved_fake_file_access_ =
395 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
396
Tom Sepez0784c732018-04-23 18:02:57 +0000397 EXPECT_TRUE(OpenDocumentHelper(
398 password, LinearizeOption::kDefaultLinearize,
399 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
400 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000401 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400402}
403
404void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000405 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400406
Lei Zhang0729be22018-02-05 21:13:51 +0000407 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
408 FPDF_CloseDocument(saved_document_);
409 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400410
Lei Zhang0729be22018-02-05 21:13:51 +0000411 saved_form_handle_ = nullptr;
412 saved_document_ = nullptr;
413 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400414}
415
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000416FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000417 ASSERT(saved_form_handle_);
418 ASSERT(page_number >= 0);
419 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400420
Lei Zhang0729be22018-02-05 21:13:51 +0000421 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000422 if (!page)
423 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400424
Lei Zhang9f72c452018-02-08 21:49:54 +0000425 FORM_OnAfterLoadPage(page, saved_form_handle_);
426 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
427 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000428 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400429}
430
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000431void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000432 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 Nakashima8baea3c2017-11-10 20:27:23 +0000442 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000443
444 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400445}
446
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000447void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
448 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400449 int height,
450 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000451 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000452 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400453
Tom Sepeze08d2b12018-04-25 18:49:32 +0000454 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000455 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400456}
457
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400458void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000459 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000460 FPDF_PAGE page = LoadSavedPage(0);
461 VerifySavedRendering(page, width, height, md5);
462 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400463 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400464}
465
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300466void EmbedderTest::SetWholeFileAvailable() {
467 ASSERT(fake_file_access_);
468 fake_file_access_->SetWholeFileAvailable();
469}
470
weili0dadcc62016-08-23 21:10:57 -0700471FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700472 FPDF_DOCUMENT document,
473 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700474 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 Sepez396e8722015-09-09 10:16:08 -0700477}
478
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800479// static
480void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
481 int type) {
482 EmbedderTest* test = static_cast<EmbedderTest*>(info);
483 test->delegate_->UnsupportedHandler(type);
484}
485
486// static
487int 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 Sepez6efc0ad2015-06-02 17:11:18 -0700496// static
497int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 int msecs,
499 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700500 EmbedderTest* test = static_cast<EmbedderTest*>(info);
501 return test->delegate_->SetTimer(msecs, fn);
502}
503
504// static
505void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
506 EmbedderTest* test = static_cast<EmbedderTest*>(info);
507 return test->delegate_->KillTimer(id);
508}
509
Tom Sepez396e8722015-09-09 10:16:08 -0700510// static
511FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
512 FPDF_DOCUMENT document,
513 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700514 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
515 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700516}
517
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000518// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000519void 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 Nakashima9fa50362017-11-10 22:40:44 +0000526std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400527 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000528 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
529 FPDFBitmap_GetWidth(bitmap) *
530 GetBitmapBytesPerPixel(bitmap) *
531 FPDFBitmap_GetHeight(bitmap),
532 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400533 return CryptToBase16(digest);
534}
535
Henrique Nakashimadb269572018-01-16 19:02:15 +0000536#ifndef NDEBUG
537// static
538void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
539 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000540 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000541}
542#endif
543
thestigbcd3e532016-11-21 13:37:28 -0800544// static
545void 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 Liu28fb7ba2017-08-02 21:45:57 -0400551
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;
thestigbcd3e532016-11-21 13:37:28 -0800556 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
557
558 if (!expected_md5sum)
559 return;
560
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000561 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800562}
563
Nicolas Pena3ff54002017-07-05 11:55:35 -0400564// static
565int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
566 const void* data,
567 unsigned long size) {
568 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000569
Lei Zhang0729be22018-02-05 21:13:51 +0000570 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000571
572 if (pThis->filestream_.is_open())
573 pThis->filestream_.write(static_cast<const char*>(data), size);
574
Nicolas Pena3ff54002017-07-05 11:55:35 -0400575 return 1;
576}
577
578// static
579int 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 Zhangcef91f12019-01-08 21:32:51 +0000584 if (!new_file || pos + size < pos) {
585 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400586 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000587 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400588
Lei Zhangcef91f12019-01-08 21:32:51 +0000589 if (pos + size > new_file->size()) {
590 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400591 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000592 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400593
594 memcpy(buf, new_file->data() + pos, size);
595 return 1;
596}
Lei Zhang75c81712018-02-08 17:22:39 +0000597
Lei Zhang9f72c452018-02-08 21:49:54 +0000598// static
599int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
600 FPDF_PAGE page) {
601 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000602 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 Zhang9f72c452018-02-08 21:49:54 +0000610
611int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
612 return GetPageNumberForPage(page_map_, page);
613}
614
615int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
616 return GetPageNumberForPage(saved_page_map_, page);
617}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000618
619void EmbedderTest::OpenPDFFileForWrite(const char* filename) {
620 filestream_.open(filename, std::ios_base::binary);
621}
622
623void EmbedderTest::ClosePDFFileForWrite() {
624 filestream_.close();
625}