blob: bd52c262867df9ff499b1ca962f93dc3ae1410c1 [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 Zhanga98e3662018-02-07 20:28:35 +000011#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -080012#include <string>
13#include <utility>
14#include <vector>
15
thestigbcd3e532016-11-21 13:37:28 -080016#include "core/fdrm/crypto/fx_crypt.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"
Henrique Nakashimadb269572018-01-16 19:02:15 +000022#include "testing/image_diff/image_diff_png.h"
Wei Li091f7a02015-11-09 12:09:55 -080023#include "testing/test_support.h"
24#include "testing/utils/path_service.h"
Artem Strygin0e60b9e2017-09-28 18:46:03 +030025#include "third_party/base/ptr_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070026
27#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080028#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050029#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070030#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080031
Tom Sepez96d13342015-01-16 14:59:26 -080032namespace {
thestigc08cd7a2016-06-27 09:47:59 -070033
Jane Liu28fb7ba2017-08-02 21:45:57 -040034int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
35 const int format = FPDFBitmap_GetFormat(bitmap);
36 switch (format) {
37 case FPDFBitmap_Gray:
38 return 1;
39 case FPDFBitmap_BGR:
40 return 3;
41 case FPDFBitmap_BGRx:
42 case FPDFBitmap_BGRA:
43 return 4;
44 default:
45 ASSERT(false);
46 return 0;
47 }
48}
49
thestigbcd3e532016-11-21 13:37:28 -080050} // namespace
51
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052EmbedderTest::EmbedderTest()
Lei Zhang0729be22018-02-05 21:13:51 +000053 : default_delegate_(pdfium::MakeUnique<EmbedderTest::Delegate>()),
54 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -040055 FPDF_FILEWRITE::version = 1;
56 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080057}
Tom Sepez96d13342015-01-16 14:59:26 -080058
Dan Sinclair5553d8b2018-01-03 09:44:28 -050059EmbedderTest::~EmbedderTest() {}
Tom Sepezf288bb12015-11-20 12:12:46 -080060
61void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070062 FPDF_LIBRARY_CONFIG config;
63 config.version = 2;
64 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070065 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070066 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070067 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080068
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
70 memset(info, 0, sizeof(UNSUPPORT_INFO));
71 info->version = 1;
72 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
73 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +000074
Lei Zhang0729be22018-02-05 21:13:51 +000075 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076}
Tom Sepez96d13342015-01-16 14:59:26 -080077
78void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -080079 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -070080 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -080081 FPDFDOC_ExitFormFillEnvironment(form_handle_);
82 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -080083 }
Tom Sepezc46d0002015-11-30 15:46:36 -080084
Tom Sepez96d13342015-01-16 14:59:26 -080085 FPDFAvail_Destroy(avail_);
86 FPDF_DestroyLibrary();
Lei Zhangd27acae2015-05-15 15:36:02 -070087 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -080088}
89
Tom Sepezd483eb42016-01-06 10:03:59 -080090bool EmbedderTest::CreateEmptyDocument() {
91 document_ = FPDF_CreateNewDocument();
92 if (!document_)
93 return false;
94
Nicolas Pena3ff54002017-07-05 11:55:35 -040095 form_handle_ = SetupFormFillEnvironment(document_);
Tom Sepezd483eb42016-01-06 10:03:59 -080096 return true;
97}
98
Lei Zhang208eecf2017-12-20 19:40:50 +000099bool EmbedderTest::OpenDocument(const std::string& filename) {
100 return OpenDocumentWithOptions(filename, nullptr, false);
101}
102
103bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
104 return OpenDocumentWithOptions(filename, nullptr, true);
105}
106
107bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
108 const char* password) {
109 return OpenDocumentWithOptions(filename, password, false);
110}
111
112bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
113 const char* password,
114 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800115 std::string file_path;
116 if (!PathService::GetTestFilePath(filename, &file_path))
117 return false;
118 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400119 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800120 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800121
thestig29ce9232016-06-22 07:03:23 -0700122 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800123 loader_ = new TestLoader(file_contents_.get(), file_length_);
Lei Zhang0729be22018-02-05 21:13:51 +0000124
125 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800126 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700127 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800128 file_access_.m_Param = loader_;
Lei Zhang0729be22018-02-05 21:13:51 +0000129
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300130 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
131 return OpenDocumentHelper(password, must_linearize, fake_file_access_.get(),
132 &document_, &avail_, &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400133}
Tom Sepez96d13342015-01-16 14:59:26 -0800134
Nicolas Pena56fc9722017-07-13 16:31:34 -0400135bool EmbedderTest::OpenDocumentHelper(const char* password,
136 bool must_linearize,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300137 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400138 FPDF_DOCUMENT* document,
139 FPDF_AVAIL* avail,
140 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300141 network_simulator->AddSegment(0, 1024);
142 network_simulator->SetRequestedDataAvailable();
143 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
144 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400145 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300146 int32_t nRet = PDF_DATA_NOTAVAIL;
147 while (nRet == PDF_DATA_NOTAVAIL) {
148 network_simulator->SetRequestedDataAvailable();
149 nRet =
150 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
151 }
152 if (nRet == PDF_DATA_ERROR)
153 return false;
154
Nicolas Pena56fc9722017-07-13 16:31:34 -0400155 *document = FPDFAvail_GetDocument(*avail, password);
156 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800157 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400158
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300159 nRet = PDF_DATA_NOTAVAIL;
160 while (nRet == PDF_DATA_NOTAVAIL) {
161 network_simulator->SetRequestedDataAvailable();
162 nRet =
163 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
164 }
165 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800166 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400167
168 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800169 for (int i = 0; i < page_count; ++i) {
170 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300171 while (nRet == PDF_DATA_NOTAVAIL) {
172 network_simulator->SetRequestedDataAvailable();
173 nRet = FPDFAvail_IsPageAvail(*avail, i,
174 network_simulator->GetDownloadHints());
175 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400176
177 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800178 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800179 }
180 } else {
Nicolas Pena56fc9722017-07-13 16:31:34 -0400181 if (must_linearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800182 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300183 network_simulator->SetWholeFileAvailable();
184 *document =
185 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400186 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800187 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800188 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400189 *form_handle = SetupFormFillEnvironment(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800190#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400191 int doc_type = FPDF_GetFormType(*document);
192 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
193 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800194#endif // PDF_ENABLE_XFA
Nicolas Pena56fc9722017-07-13 16:31:34 -0400195 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800196 return true;
197}
Tom Sepez96d13342015-01-16 14:59:26 -0800198
Nicolas Pena3ff54002017-07-05 11:55:35 -0400199FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(FPDF_DOCUMENT doc) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800200 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400201 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200202 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800203 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400204 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800205
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800206 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
207 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800208#ifdef PDF_ENABLE_XFA
209 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800210#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800211 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800212#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700213 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
214 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700215 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800216 formfillinfo->m_pJsPlatform = platform;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400217 FPDF_FORMHANDLE form_handle =
218 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000219 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
220 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400221 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
222 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800223}
224
Tom Sepezda8189e2015-01-30 14:41:50 -0800225void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400226 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800227 FORM_DoDocumentJSAction(form_handle_);
228 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800229}
230
231int EmbedderTest::GetFirstPageNum() {
232 int first_page = FPDFAvail_GetFirstPageNum(document_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300233 (void)FPDFAvail_IsPageAvail(avail_, first_page,
234 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800235 return first_page;
236}
237
238int EmbedderTest::GetPageCount() {
239 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400240 for (int i = 0; i < page_count; ++i)
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300241 (void)FPDFAvail_IsPageAvail(avail_, i,
242 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800243 return page_count;
244}
245
Tom Sepezda8189e2015-01-30 14:41:50 -0800246FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400247 ASSERT(form_handle_);
weili0dadcc62016-08-23 21:10:57 -0700248 // First check whether it is loaded already.
249 auto it = page_map_.find(page_number);
250 if (it != page_map_.end())
251 return it->second;
252
Tom Sepez96d13342015-01-16 14:59:26 -0800253 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400254 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800255 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400256
Tom Sepezda8189e2015-01-30 14:41:50 -0800257 FORM_OnAfterLoadPage(page, form_handle_);
258 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700259 // Cache the page.
260 page_map_[page_number] = page;
dsinclaircb92dc72016-09-07 09:02:48 -0700261 page_reverse_map_[page] = page_number;
Tom Sepez396e8722015-09-09 10:16:08 -0700262 return page;
263}
264
Tom Sepezda8189e2015-01-30 14:41:50 -0800265void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400266 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800267 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
268 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800269 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700270
271 auto it = page_reverse_map_.find(page);
272 if (it == page_reverse_map_.end())
273 return;
274
275 page_map_.erase(it->second);
276 page_reverse_map_.erase(it);
Tom Sepez96d13342015-01-16 14:59:26 -0800277}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800278
Lei Zhanga98e3662018-02-07 20:28:35 +0000279FPDF_BITMAP EmbedderTest::RenderPageDeprecated(FPDF_PAGE page) {
280 return RenderPageWithFlagsDeprecated(page, form_handle_, 0);
281}
282
283std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderLoadedPage(
284 FPDF_PAGE page) {
285 return RenderLoadedPageWithFlags(page, 0);
286}
287
288std::unique_ptr<void, FPDFBitmapDeleter>
289EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page, int flags) {
290 return RenderPageWithFlags(page, form_handle_, flags);
291}
292
293std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderSavedPage(
294 FPDF_PAGE page) {
295 return RenderSavedPageWithFlags(page, 0);
296}
297
298std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderSavedPageWithFlags(
299 FPDF_PAGE page,
300 int flags) {
301 return RenderPageWithFlags(page, saved_form_handle_, flags);
302}
303
304// static
305FPDF_BITMAP EmbedderTest::RenderPageWithFlagsDeprecated(FPDF_PAGE page,
306 FPDF_FORMHANDLE handle,
307 int flags) {
308 return RenderPageWithFlags(page, handle, flags).release();
309}
310
311// static
312std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderPageWithFlags(
313 FPDF_PAGE page,
314 FPDF_FORMHANDLE handle,
315 int flags) {
316 int width = static_cast<int>(FPDF_GetPageWidth(page));
317 int height = static_cast<int>(FPDF_GetPageHeight(page));
318 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
319 std::unique_ptr<void, FPDFBitmapDeleter> bitmap(
320 FPDFBitmap_Create(width, height, alpha));
321 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
322 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
323 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
324 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
325 return bitmap;
326}
327
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400328FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300329 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000330 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300331 saved_file_access_.m_GetBlock = GetBlockFromString;
Lei Zhang0729be22018-02-05 21:13:51 +0000332 saved_file_access_.m_Param = &data_string_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400333
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300334 saved_fake_file_access_ =
335 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
336
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400337 EXPECT_TRUE(OpenDocumentHelper(password, false, saved_fake_file_access_.get(),
Lei Zhang0729be22018-02-05 21:13:51 +0000338 &saved_document_, &saved_avail_,
339 &saved_form_handle_));
340 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400341}
342
343void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000344 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400345
Lei Zhang0729be22018-02-05 21:13:51 +0000346 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
347 FPDF_CloseDocument(saved_document_);
348 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400349
Lei Zhang0729be22018-02-05 21:13:51 +0000350 saved_form_handle_ = nullptr;
351 saved_document_ = nullptr;
352 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400353}
354
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000355FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang0729be22018-02-05 21:13:51 +0000356 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400357
Lei Zhang0729be22018-02-05 21:13:51 +0000358 EXPECT_LT(page_number, FPDF_GetPageCount(saved_document_));
359 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400360
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000361 ASSERT(page);
362 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400363}
364
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000365void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
366 ASSERT(page);
367 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400368}
369
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000370void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
371 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400372 int height,
373 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000374 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000375 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400376
Lei Zhanga98e3662018-02-07 20:28:35 +0000377 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
378 RenderSavedPageWithFlags(page, FPDF_ANNOT);
379 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400380}
381
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400382void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
383 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000384 FPDF_PAGE page = LoadSavedPage(0);
385 VerifySavedRendering(page, width, height, md5);
386 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400387 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400388}
389
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300390void EmbedderTest::SetWholeFileAvailable() {
391 ASSERT(fake_file_access_);
392 fake_file_access_->SetWholeFileAvailable();
393}
394
weili0dadcc62016-08-23 21:10:57 -0700395FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700396 FPDF_DOCUMENT document,
397 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700398 EmbedderTest* test = static_cast<EmbedderTest*>(info);
399 auto it = test->page_map_.find(page_index);
400 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700401}
402
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800403// static
404void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
405 int type) {
406 EmbedderTest* test = static_cast<EmbedderTest*>(info);
407 test->delegate_->UnsupportedHandler(type);
408}
409
410// static
411int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
412 FPDF_WIDESTRING message,
413 FPDF_WIDESTRING title,
414 int type,
415 int icon) {
416 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
417 return test->delegate_->Alert(message, title, type, icon);
418}
419
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700420// static
421int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700422 int msecs,
423 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700424 EmbedderTest* test = static_cast<EmbedderTest*>(info);
425 return test->delegate_->SetTimer(msecs, fn);
426}
427
428// static
429void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
430 EmbedderTest* test = static_cast<EmbedderTest*>(info);
431 return test->delegate_->KillTimer(id);
432}
433
Tom Sepez396e8722015-09-09 10:16:08 -0700434// static
435FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
436 FPDF_DOCUMENT document,
437 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700438 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
439 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700440}
441
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000442// static
443std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400444 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000445 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
446 FPDFBitmap_GetWidth(bitmap) *
447 GetBitmapBytesPerPixel(bitmap) *
448 FPDFBitmap_GetHeight(bitmap),
449 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400450 return CryptToBase16(digest);
451}
452
Henrique Nakashimadb269572018-01-16 19:02:15 +0000453#ifndef NDEBUG
454// static
455void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
456 const std::string& filename) {
457 const int stride = FPDFBitmap_GetStride(bitmap);
458 const int width = FPDFBitmap_GetWidth(bitmap);
459 const int height = FPDFBitmap_GetHeight(bitmap);
460 const auto* buffer =
461 static_cast<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap));
462
463 std::vector<unsigned char> png_encoding;
464 bool encoded = image_diff_png::EncodeBGRAPNG(buffer, width, height, stride,
465 false, &png_encoding);
466
467 ASSERT_TRUE(encoded);
468 ASSERT_LT(filename.size(), 256u);
469
470 std::ofstream png_file;
Henrique Nakashima6d6a2432018-01-17 16:52:46 +0000471 png_file.open(filename, std::ios_base::out | std::ios_base::binary);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000472 png_file.write(reinterpret_cast<char*>(&png_encoding.front()),
473 png_encoding.size());
474 ASSERT_TRUE(png_file.good());
475 png_file.close();
476}
477#endif
478
thestigbcd3e532016-11-21 13:37:28 -0800479// static
480void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
481 int expected_width,
482 int expected_height,
483 const char* expected_md5sum) {
484 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
485 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400486
487 // The expected stride is calculated using the same formula as in
488 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
489 const int expected_stride =
490 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800491 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
492
493 if (!expected_md5sum)
494 return;
495
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000496 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800497}
498
Nicolas Pena3ff54002017-07-05 11:55:35 -0400499// static
500int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
501 const void* data,
502 unsigned long size) {
503 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Lei Zhang0729be22018-02-05 21:13:51 +0000504 pThis->data_string_.append(static_cast<const char*>(data), size);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400505 return 1;
506}
507
508// static
509int EmbedderTest::GetBlockFromString(void* param,
510 unsigned long pos,
511 unsigned char* buf,
512 unsigned long size) {
513 std::string* new_file = static_cast<std::string*>(param);
514 if (!new_file || pos + size < pos)
515 return 0;
516
517 unsigned long file_size = new_file->size();
518 if (pos + size > file_size)
519 return 0;
520
521 memcpy(buf, new_file->data() + pos, size);
522 return 1;
523}