blob: c39539b3fb606d731e01b43658e9fd6aa423daf9 [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
Henrique Nakashimadb269572018-01-16 19:02:15 +00009#include <fstream>
Tom Sepez96d13342015-01-16 14:59:26 -080010#include <list>
Lei Zhang9f72c452018-02-08 21:49:54 +000011#include <map>
Lei Zhanga98e3662018-02-07 20:28:35 +000012#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -080013#include <string>
14#include <utility>
15#include <vector>
16
thestigbcd3e532016-11-21 13:37:28 -080017#include "core/fdrm/crypto/fx_crypt.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080018#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080019#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080020#include "public/fpdf_text.h"
21#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080022#include "testing/gmock/include/gmock/gmock.h"
Henrique Nakashimadb269572018-01-16 19:02:15 +000023#include "testing/image_diff/image_diff_png.h"
Wei Li091f7a02015-11-09 12:09:55 -080024#include "testing/test_support.h"
25#include "testing/utils/path_service.h"
Lei Zhang75c81712018-02-08 17:22:39 +000026#include "third_party/base/logging.h"
Artem Strygin0e60b9e2017-09-28 18:46:03 +030027#include "third_party/base/ptr_util.h"
Lei Zhang75c81712018-02-08 17:22:39 +000028#include "third_party/base/stl_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070029
30#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080031#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050032#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070033#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080034
Tom Sepez96d13342015-01-16 14:59:26 -080035namespace {
thestigc08cd7a2016-06-27 09:47:59 -070036
Jane Liu28fb7ba2017-08-02 21:45:57 -040037int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
38 const int format = FPDFBitmap_GetFormat(bitmap);
39 switch (format) {
40 case FPDFBitmap_Gray:
41 return 1;
42 case FPDFBitmap_BGR:
43 return 3;
44 case FPDFBitmap_BGRx:
45 case FPDFBitmap_BGRA:
46 return 4;
47 default:
48 ASSERT(false);
49 return 0;
50 }
51}
52
thestigbcd3e532016-11-21 13:37:28 -080053} // namespace
54
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055EmbedderTest::EmbedderTest()
Lei Zhang0729be22018-02-05 21:13:51 +000056 : default_delegate_(pdfium::MakeUnique<EmbedderTest::Delegate>()),
57 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -040058 FPDF_FILEWRITE::version = 1;
59 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080060}
Tom Sepez96d13342015-01-16 14:59:26 -080061
Dan Sinclair5553d8b2018-01-03 09:44:28 -050062EmbedderTest::~EmbedderTest() {}
Tom Sepezf288bb12015-11-20 12:12:46 -080063
64void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070065 FPDF_LIBRARY_CONFIG config;
66 config.version = 2;
67 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070068 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070069 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070070 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080071
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
73 memset(info, 0, sizeof(UNSUPPORT_INFO));
74 info->version = 1;
75 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
76 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +000077
Lei Zhang0729be22018-02-05 21:13:51 +000078 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079}
Tom Sepez96d13342015-01-16 14:59:26 -080080
81void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +000082 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
83 // possible. This can fail when an ASSERT test fails in a test case.
84 EXPECT_EQ(0U, page_map_.size());
Lei Zhang9f72c452018-02-08 21:49:54 +000085 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang75c81712018-02-08 17:22:39 +000086
Tom Sepezda8189e2015-01-30 14:41:50 -080087 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -070088 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -080089 FPDFDOC_ExitFormFillEnvironment(form_handle_);
90 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -080091 }
Tom Sepezc46d0002015-11-30 15:46:36 -080092
Tom Sepez96d13342015-01-16 14:59:26 -080093 FPDFAvail_Destroy(avail_);
94 FPDF_DestroyLibrary();
Lei Zhangd27acae2015-05-15 15:36:02 -070095 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -080096}
97
Tom Sepezd483eb42016-01-06 10:03:59 -080098bool EmbedderTest::CreateEmptyDocument() {
99 document_ = FPDF_CreateNewDocument();
100 if (!document_)
101 return false;
102
Nicolas Pena3ff54002017-07-05 11:55:35 -0400103 form_handle_ = SetupFormFillEnvironment(document_);
Tom Sepezd483eb42016-01-06 10:03:59 -0800104 return true;
105}
106
Lei Zhang208eecf2017-12-20 19:40:50 +0000107bool EmbedderTest::OpenDocument(const std::string& filename) {
108 return OpenDocumentWithOptions(filename, nullptr, false);
109}
110
111bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
112 return OpenDocumentWithOptions(filename, nullptr, true);
113}
114
115bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
116 const char* password) {
117 return OpenDocumentWithOptions(filename, password, false);
118}
119
120bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
121 const char* password,
122 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800123 std::string file_path;
124 if (!PathService::GetTestFilePath(filename, &file_path))
125 return false;
126 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400127 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800128 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800129
thestig29ce9232016-06-22 07:03:23 -0700130 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800131 loader_ = new TestLoader(file_contents_.get(), file_length_);
Lei Zhang0729be22018-02-05 21:13:51 +0000132
133 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800134 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700135 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800136 file_access_.m_Param = loader_;
Lei Zhang0729be22018-02-05 21:13:51 +0000137
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300138 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
139 return OpenDocumentHelper(password, must_linearize, fake_file_access_.get(),
140 &document_, &avail_, &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400141}
Tom Sepez96d13342015-01-16 14:59:26 -0800142
Nicolas Pena56fc9722017-07-13 16:31:34 -0400143bool EmbedderTest::OpenDocumentHelper(const char* password,
144 bool must_linearize,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300145 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400146 FPDF_DOCUMENT* document,
147 FPDF_AVAIL* avail,
148 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300149 network_simulator->AddSegment(0, 1024);
150 network_simulator->SetRequestedDataAvailable();
151 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
152 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400153 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300154 int32_t nRet = PDF_DATA_NOTAVAIL;
155 while (nRet == PDF_DATA_NOTAVAIL) {
156 network_simulator->SetRequestedDataAvailable();
157 nRet =
158 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
159 }
160 if (nRet == PDF_DATA_ERROR)
161 return false;
162
Nicolas Pena56fc9722017-07-13 16:31:34 -0400163 *document = FPDFAvail_GetDocument(*avail, password);
164 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800165 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400166
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300167 nRet = PDF_DATA_NOTAVAIL;
168 while (nRet == PDF_DATA_NOTAVAIL) {
169 network_simulator->SetRequestedDataAvailable();
170 nRet =
171 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
172 }
173 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800174 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400175
176 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800177 for (int i = 0; i < page_count; ++i) {
178 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300179 while (nRet == PDF_DATA_NOTAVAIL) {
180 network_simulator->SetRequestedDataAvailable();
181 nRet = FPDFAvail_IsPageAvail(*avail, i,
182 network_simulator->GetDownloadHints());
183 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400184
185 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800186 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800187 }
188 } else {
Nicolas Pena56fc9722017-07-13 16:31:34 -0400189 if (must_linearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800190 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300191 network_simulator->SetWholeFileAvailable();
192 *document =
193 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400194 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800195 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800196 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400197 *form_handle = SetupFormFillEnvironment(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800198#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400199 int doc_type = FPDF_GetFormType(*document);
200 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
201 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800202#endif // PDF_ENABLE_XFA
Nicolas Pena56fc9722017-07-13 16:31:34 -0400203 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800204 return true;
205}
Tom Sepez96d13342015-01-16 14:59:26 -0800206
Nicolas Pena3ff54002017-07-05 11:55:35 -0400207FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(FPDF_DOCUMENT doc) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800208 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400209 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200210 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800211 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400212 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800213
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800214 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
215 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800216#ifdef PDF_ENABLE_XFA
217 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800218#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800219 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800220#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700221 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
222 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700223 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800224 formfillinfo->m_pJsPlatform = platform;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400225 FPDF_FORMHANDLE form_handle =
226 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000227 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
228 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400229 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
230 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800231}
232
Tom Sepezda8189e2015-01-30 14:41:50 -0800233void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400234 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800235 FORM_DoDocumentJSAction(form_handle_);
236 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800237}
238
239int EmbedderTest::GetFirstPageNum() {
240 int first_page = FPDFAvail_GetFirstPageNum(document_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300241 (void)FPDFAvail_IsPageAvail(avail_, first_page,
242 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800243 return first_page;
244}
245
246int EmbedderTest::GetPageCount() {
247 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400248 for (int i = 0; i < page_count; ++i)
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300249 (void)FPDFAvail_IsPageAvail(avail_, i,
250 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800251 return page_count;
252}
253
Tom Sepezda8189e2015-01-30 14:41:50 -0800254FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400255 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000256 ASSERT(page_number >= 0);
257 ASSERT(!pdfium::ContainsKey(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700258
Tom Sepez96d13342015-01-16 14:59:26 -0800259 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400260 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800261 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400262
Tom Sepezda8189e2015-01-30 14:41:50 -0800263 FORM_OnAfterLoadPage(page, form_handle_);
264 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700265 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700266 return page;
267}
268
Tom Sepezda8189e2015-01-30 14:41:50 -0800269void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400270 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000271
272 int page_number = GetPageNumberForLoadedPage(page);
273 if (page_number < 0) {
274 NOTREACHED();
275 return;
276 }
277
Tom Sepezda8189e2015-01-30 14:41:50 -0800278 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
279 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800280 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700281
Lei Zhang75c81712018-02-08 17:22:39 +0000282 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800283}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800284
Lei Zhanga98e3662018-02-07 20:28:35 +0000285std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderLoadedPage(
286 FPDF_PAGE page) {
287 return RenderLoadedPageWithFlags(page, 0);
288}
289
290std::unique_ptr<void, FPDFBitmapDeleter>
291EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page, int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000292 if (GetPageNumberForLoadedPage(page) < 0) {
293 NOTREACHED();
294 return nullptr;
295 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000296 return RenderPageWithFlags(page, form_handle_, flags);
297}
298
299std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderSavedPage(
300 FPDF_PAGE page) {
301 return RenderSavedPageWithFlags(page, 0);
302}
303
304std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderSavedPageWithFlags(
305 FPDF_PAGE page,
306 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000307 if (GetPageNumberForSavedPage(page) < 0) {
308 NOTREACHED();
309 return nullptr;
310 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000311 return RenderPageWithFlags(page, saved_form_handle_, flags);
312}
313
314// static
Lei Zhanga98e3662018-02-07 20:28:35 +0000315std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderPageWithFlags(
316 FPDF_PAGE page,
317 FPDF_FORMHANDLE handle,
318 int flags) {
319 int width = static_cast<int>(FPDF_GetPageWidth(page));
320 int height = static_cast<int>(FPDF_GetPageHeight(page));
321 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
322 std::unique_ptr<void, FPDFBitmapDeleter> bitmap(
323 FPDFBitmap_Create(width, height, alpha));
324 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
325 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
326 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
327 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
328 return bitmap;
329}
330
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400331FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300332 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000333 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300334 saved_file_access_.m_GetBlock = GetBlockFromString;
Lei Zhang0729be22018-02-05 21:13:51 +0000335 saved_file_access_.m_Param = &data_string_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400336
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300337 saved_fake_file_access_ =
338 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
339
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400340 EXPECT_TRUE(OpenDocumentHelper(password, false, saved_fake_file_access_.get(),
Lei Zhang0729be22018-02-05 21:13:51 +0000341 &saved_document_, &saved_avail_,
342 &saved_form_handle_));
343 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400344}
345
346void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000347 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400348
Lei Zhang0729be22018-02-05 21:13:51 +0000349 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
350 FPDF_CloseDocument(saved_document_);
351 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400352
Lei Zhang0729be22018-02-05 21:13:51 +0000353 saved_form_handle_ = nullptr;
354 saved_document_ = nullptr;
355 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400356}
357
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000358FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000359 ASSERT(saved_form_handle_);
360 ASSERT(page_number >= 0);
361 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400362
Lei Zhang0729be22018-02-05 21:13:51 +0000363 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000364 if (!page)
365 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400366
Lei Zhang9f72c452018-02-08 21:49:54 +0000367 FORM_OnAfterLoadPage(page, saved_form_handle_);
368 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
369 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000370 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400371}
372
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000373void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000374 ASSERT(saved_form_handle_);
375
376 int page_number = GetPageNumberForSavedPage(page);
377 if (page_number < 0) {
378 NOTREACHED();
379 return;
380 }
381
382 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
383 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000384 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000385
386 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400387}
388
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000389void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
390 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400391 int height,
392 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000393 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000394 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400395
Lei Zhanga98e3662018-02-07 20:28:35 +0000396 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
397 RenderSavedPageWithFlags(page, FPDF_ANNOT);
398 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400399}
400
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400401void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
402 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000403 FPDF_PAGE page = LoadSavedPage(0);
404 VerifySavedRendering(page, width, height, md5);
405 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400406 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400407}
408
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300409void EmbedderTest::SetWholeFileAvailable() {
410 ASSERT(fake_file_access_);
411 fake_file_access_->SetWholeFileAvailable();
412}
413
weili0dadcc62016-08-23 21:10:57 -0700414FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700415 FPDF_DOCUMENT document,
416 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700417 EmbedderTest* test = static_cast<EmbedderTest*>(info);
418 auto it = test->page_map_.find(page_index);
419 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700420}
421
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800422// static
423void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
424 int type) {
425 EmbedderTest* test = static_cast<EmbedderTest*>(info);
426 test->delegate_->UnsupportedHandler(type);
427}
428
429// static
430int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
431 FPDF_WIDESTRING message,
432 FPDF_WIDESTRING title,
433 int type,
434 int icon) {
435 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
436 return test->delegate_->Alert(message, title, type, icon);
437}
438
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700439// static
440int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700441 int msecs,
442 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700443 EmbedderTest* test = static_cast<EmbedderTest*>(info);
444 return test->delegate_->SetTimer(msecs, fn);
445}
446
447// static
448void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
449 EmbedderTest* test = static_cast<EmbedderTest*>(info);
450 return test->delegate_->KillTimer(id);
451}
452
Tom Sepez396e8722015-09-09 10:16:08 -0700453// static
454FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
455 FPDF_DOCUMENT document,
456 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700457 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
458 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700459}
460
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000461// static
462std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400463 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000464 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
465 FPDFBitmap_GetWidth(bitmap) *
466 GetBitmapBytesPerPixel(bitmap) *
467 FPDFBitmap_GetHeight(bitmap),
468 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400469 return CryptToBase16(digest);
470}
471
Henrique Nakashimadb269572018-01-16 19:02:15 +0000472#ifndef NDEBUG
473// static
474void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
475 const std::string& filename) {
476 const int stride = FPDFBitmap_GetStride(bitmap);
477 const int width = FPDFBitmap_GetWidth(bitmap);
478 const int height = FPDFBitmap_GetHeight(bitmap);
479 const auto* buffer =
480 static_cast<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap));
481
482 std::vector<unsigned char> png_encoding;
Lei Zhang9a7c2392018-03-02 23:54:26 +0000483 bool encoded;
484 if (FPDFBitmap_GetFormat(bitmap) == FPDFBitmap_Gray) {
485 encoded = image_diff_png::EncodeGrayPNG(buffer, width, height, stride,
486 &png_encoding);
487 } else {
488 encoded = image_diff_png::EncodeBGRAPNG(buffer, width, height, stride,
489 /*discard_transparency=*/false,
490 &png_encoding);
491 }
Henrique Nakashimadb269572018-01-16 19:02:15 +0000492
493 ASSERT_TRUE(encoded);
494 ASSERT_LT(filename.size(), 256u);
495
496 std::ofstream png_file;
Henrique Nakashima6d6a2432018-01-17 16:52:46 +0000497 png_file.open(filename, std::ios_base::out | std::ios_base::binary);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000498 png_file.write(reinterpret_cast<char*>(&png_encoding.front()),
499 png_encoding.size());
500 ASSERT_TRUE(png_file.good());
501 png_file.close();
502}
503#endif
504
thestigbcd3e532016-11-21 13:37:28 -0800505// static
506void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
507 int expected_width,
508 int expected_height,
509 const char* expected_md5sum) {
510 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
511 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400512
513 // The expected stride is calculated using the same formula as in
514 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
515 const int expected_stride =
516 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800517 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
518
519 if (!expected_md5sum)
520 return;
521
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000522 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800523}
524
Nicolas Pena3ff54002017-07-05 11:55:35 -0400525// static
526int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
527 const void* data,
528 unsigned long size) {
529 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Lei Zhang0729be22018-02-05 21:13:51 +0000530 pThis->data_string_.append(static_cast<const char*>(data), size);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400531 return 1;
532}
533
534// static
535int EmbedderTest::GetBlockFromString(void* param,
536 unsigned long pos,
537 unsigned char* buf,
538 unsigned long size) {
539 std::string* new_file = static_cast<std::string*>(param);
540 if (!new_file || pos + size < pos)
541 return 0;
542
543 unsigned long file_size = new_file->size();
544 if (pos + size > file_size)
545 return 0;
546
547 memcpy(buf, new_file->data() + pos, size);
548 return 1;
549}
Lei Zhang75c81712018-02-08 17:22:39 +0000550
Lei Zhang9f72c452018-02-08 21:49:54 +0000551// static
552int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
553 FPDF_PAGE page) {
554 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000555 if (it.second == page) {
556 int page_number = it.first;
557 ASSERT(page_number >= 0);
558 return page_number;
559 }
560 }
561 return -1;
562}
Lei Zhang9f72c452018-02-08 21:49:54 +0000563
564int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
565 return GetPageNumberForPage(page_map_, page);
566}
567
568int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
569 return GetPageNumberForPage(saved_page_map_, page);
570}