blob: 32c761e050b676870865d8b051f3b18a07e050d7 [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"
Lei Zhang75c81712018-02-08 17:22:39 +000025#include "third_party/base/logging.h"
Artem Strygin0e60b9e2017-09-28 18:46:03 +030026#include "third_party/base/ptr_util.h"
Lei Zhang75c81712018-02-08 17:22:39 +000027#include "third_party/base/stl_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070028
29#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080030#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050031#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070032#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080033
Tom Sepez96d13342015-01-16 14:59:26 -080034namespace {
thestigc08cd7a2016-06-27 09:47:59 -070035
Jane Liu28fb7ba2017-08-02 21:45:57 -040036int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
37 const int format = FPDFBitmap_GetFormat(bitmap);
38 switch (format) {
39 case FPDFBitmap_Gray:
40 return 1;
41 case FPDFBitmap_BGR:
42 return 3;
43 case FPDFBitmap_BGRx:
44 case FPDFBitmap_BGRA:
45 return 4;
46 default:
47 ASSERT(false);
48 return 0;
49 }
50}
51
thestigbcd3e532016-11-21 13:37:28 -080052} // namespace
53
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054EmbedderTest::EmbedderTest()
Lei Zhang0729be22018-02-05 21:13:51 +000055 : default_delegate_(pdfium::MakeUnique<EmbedderTest::Delegate>()),
56 delegate_(default_delegate_.get()) {
Nicolas Pena3ff54002017-07-05 11:55:35 -040057 FPDF_FILEWRITE::version = 1;
58 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080059}
Tom Sepez96d13342015-01-16 14:59:26 -080060
Dan Sinclair5553d8b2018-01-03 09:44:28 -050061EmbedderTest::~EmbedderTest() {}
Tom Sepezf288bb12015-11-20 12:12:46 -080062
63void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070064 FPDF_LIBRARY_CONFIG config;
65 config.version = 2;
66 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -070067 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -070068 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -070069 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -080070
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
72 memset(info, 0, sizeof(UNSUPPORT_INFO));
73 info->version = 1;
74 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
75 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +000076
Lei Zhang0729be22018-02-05 21:13:51 +000077 saved_document_ = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078}
Tom Sepez96d13342015-01-16 14:59:26 -080079
80void EmbedderTest::TearDown() {
Lei Zhang75c81712018-02-08 17:22:39 +000081 // Use an EXPECT_EQ() here and continue to let TearDown() finish as cleanly as
82 // possible. This can fail when an ASSERT test fails in a test case.
83 EXPECT_EQ(0U, page_map_.size());
84
Tom Sepezda8189e2015-01-30 14:41:50 -080085 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -070086 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -080087 FPDFDOC_ExitFormFillEnvironment(form_handle_);
88 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -080089 }
Tom Sepezc46d0002015-11-30 15:46:36 -080090
Tom Sepez96d13342015-01-16 14:59:26 -080091 FPDFAvail_Destroy(avail_);
92 FPDF_DestroyLibrary();
Lei Zhangd27acae2015-05-15 15:36:02 -070093 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -080094}
95
Tom Sepezd483eb42016-01-06 10:03:59 -080096bool EmbedderTest::CreateEmptyDocument() {
97 document_ = FPDF_CreateNewDocument();
98 if (!document_)
99 return false;
100
Nicolas Pena3ff54002017-07-05 11:55:35 -0400101 form_handle_ = SetupFormFillEnvironment(document_);
Tom Sepezd483eb42016-01-06 10:03:59 -0800102 return true;
103}
104
Lei Zhang208eecf2017-12-20 19:40:50 +0000105bool EmbedderTest::OpenDocument(const std::string& filename) {
106 return OpenDocumentWithOptions(filename, nullptr, false);
107}
108
109bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
110 return OpenDocumentWithOptions(filename, nullptr, true);
111}
112
113bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
114 const char* password) {
115 return OpenDocumentWithOptions(filename, password, false);
116}
117
118bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
119 const char* password,
120 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800121 std::string file_path;
122 if (!PathService::GetTestFilePath(filename, &file_path))
123 return false;
124 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400125 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800126 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800127
thestig29ce9232016-06-22 07:03:23 -0700128 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800129 loader_ = new TestLoader(file_contents_.get(), file_length_);
Lei Zhang0729be22018-02-05 21:13:51 +0000130
131 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800132 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700133 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800134 file_access_.m_Param = loader_;
Lei Zhang0729be22018-02-05 21:13:51 +0000135
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300136 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
137 return OpenDocumentHelper(password, must_linearize, fake_file_access_.get(),
138 &document_, &avail_, &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400139}
Tom Sepez96d13342015-01-16 14:59:26 -0800140
Nicolas Pena56fc9722017-07-13 16:31:34 -0400141bool EmbedderTest::OpenDocumentHelper(const char* password,
142 bool must_linearize,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300143 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400144 FPDF_DOCUMENT* document,
145 FPDF_AVAIL* avail,
146 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300147 network_simulator->AddSegment(0, 1024);
148 network_simulator->SetRequestedDataAvailable();
149 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
150 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400151 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300152 int32_t nRet = PDF_DATA_NOTAVAIL;
153 while (nRet == PDF_DATA_NOTAVAIL) {
154 network_simulator->SetRequestedDataAvailable();
155 nRet =
156 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
157 }
158 if (nRet == PDF_DATA_ERROR)
159 return false;
160
Nicolas Pena56fc9722017-07-13 16:31:34 -0400161 *document = FPDFAvail_GetDocument(*avail, password);
162 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800163 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400164
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300165 nRet = PDF_DATA_NOTAVAIL;
166 while (nRet == PDF_DATA_NOTAVAIL) {
167 network_simulator->SetRequestedDataAvailable();
168 nRet =
169 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
170 }
171 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800172 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400173
174 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800175 for (int i = 0; i < page_count; ++i) {
176 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300177 while (nRet == PDF_DATA_NOTAVAIL) {
178 network_simulator->SetRequestedDataAvailable();
179 nRet = FPDFAvail_IsPageAvail(*avail, i,
180 network_simulator->GetDownloadHints());
181 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400182
183 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800184 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800185 }
186 } else {
Nicolas Pena56fc9722017-07-13 16:31:34 -0400187 if (must_linearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800188 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300189 network_simulator->SetWholeFileAvailable();
190 *document =
191 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400192 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800193 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800194 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400195 *form_handle = SetupFormFillEnvironment(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800196#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400197 int doc_type = FPDF_GetFormType(*document);
198 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
199 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800200#endif // PDF_ENABLE_XFA
Nicolas Pena56fc9722017-07-13 16:31:34 -0400201 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800202 return true;
203}
Tom Sepez96d13342015-01-16 14:59:26 -0800204
Nicolas Pena3ff54002017-07-05 11:55:35 -0400205FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(FPDF_DOCUMENT doc) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800206 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400207 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200208 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800209 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400210 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800211
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800212 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
213 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800214#ifdef PDF_ENABLE_XFA
215 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800216#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800217 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800218#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700219 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
220 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700221 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800222 formfillinfo->m_pJsPlatform = platform;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400223 FPDF_FORMHANDLE form_handle =
224 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000225 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
226 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400227 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
228 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800229}
230
Tom Sepezda8189e2015-01-30 14:41:50 -0800231void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400232 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800233 FORM_DoDocumentJSAction(form_handle_);
234 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800235}
236
237int EmbedderTest::GetFirstPageNum() {
238 int first_page = FPDFAvail_GetFirstPageNum(document_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300239 (void)FPDFAvail_IsPageAvail(avail_, first_page,
240 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800241 return first_page;
242}
243
244int EmbedderTest::GetPageCount() {
245 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400246 for (int i = 0; i < page_count; ++i)
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300247 (void)FPDFAvail_IsPageAvail(avail_, i,
248 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800249 return page_count;
250}
251
Tom Sepezda8189e2015-01-30 14:41:50 -0800252FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400253 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000254 ASSERT(page_number >= 0);
255 ASSERT(!pdfium::ContainsKey(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700256
Tom Sepez96d13342015-01-16 14:59:26 -0800257 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400258 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800259 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400260
Tom Sepezda8189e2015-01-30 14:41:50 -0800261 FORM_OnAfterLoadPage(page, form_handle_);
262 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700263 // Cache the page.
264 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700265 return page;
266}
267
Tom Sepezda8189e2015-01-30 14:41:50 -0800268void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400269 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000270
271 int page_number = GetPageNumberForLoadedPage(page);
272 if (page_number < 0) {
273 NOTREACHED();
274 return;
275 }
276
Tom Sepezda8189e2015-01-30 14:41:50 -0800277 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
278 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800279 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700280
Lei Zhang75c81712018-02-08 17:22:39 +0000281 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800282}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800283
Lei Zhanga98e3662018-02-07 20:28:35 +0000284FPDF_BITMAP EmbedderTest::RenderPageDeprecated(FPDF_PAGE page) {
285 return RenderPageWithFlagsDeprecated(page, form_handle_, 0);
286}
287
288std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderLoadedPage(
289 FPDF_PAGE page) {
290 return RenderLoadedPageWithFlags(page, 0);
291}
292
293std::unique_ptr<void, FPDFBitmapDeleter>
294EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page, int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000295 if (GetPageNumberForLoadedPage(page) < 0) {
296 NOTREACHED();
297 return nullptr;
298 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000299 return RenderPageWithFlags(page, form_handle_, flags);
300}
301
302std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderSavedPage(
303 FPDF_PAGE page) {
304 return RenderSavedPageWithFlags(page, 0);
305}
306
307std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderSavedPageWithFlags(
308 FPDF_PAGE page,
309 int flags) {
310 return RenderPageWithFlags(page, saved_form_handle_, flags);
311}
312
313// static
314FPDF_BITMAP EmbedderTest::RenderPageWithFlagsDeprecated(FPDF_PAGE page,
315 FPDF_FORMHANDLE handle,
316 int flags) {
317 return RenderPageWithFlags(page, handle, flags).release();
318}
319
320// static
321std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderPageWithFlags(
322 FPDF_PAGE page,
323 FPDF_FORMHANDLE handle,
324 int flags) {
325 int width = static_cast<int>(FPDF_GetPageWidth(page));
326 int height = static_cast<int>(FPDF_GetPageHeight(page));
327 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
328 std::unique_ptr<void, FPDFBitmapDeleter> bitmap(
329 FPDFBitmap_Create(width, height, alpha));
330 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
331 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
332 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
333 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
334 return bitmap;
335}
336
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400337FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300338 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000339 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300340 saved_file_access_.m_GetBlock = GetBlockFromString;
Lei Zhang0729be22018-02-05 21:13:51 +0000341 saved_file_access_.m_Param = &data_string_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400342
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300343 saved_fake_file_access_ =
344 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
345
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400346 EXPECT_TRUE(OpenDocumentHelper(password, false, saved_fake_file_access_.get(),
Lei Zhang0729be22018-02-05 21:13:51 +0000347 &saved_document_, &saved_avail_,
348 &saved_form_handle_));
349 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400350}
351
352void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000353 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400354
Lei Zhang0729be22018-02-05 21:13:51 +0000355 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
356 FPDF_CloseDocument(saved_document_);
357 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400358
Lei Zhang0729be22018-02-05 21:13:51 +0000359 saved_form_handle_ = nullptr;
360 saved_document_ = nullptr;
361 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400362}
363
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000364FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang0729be22018-02-05 21:13:51 +0000365 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400366
Lei Zhang0729be22018-02-05 21:13:51 +0000367 EXPECT_LT(page_number, FPDF_GetPageCount(saved_document_));
368 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400369
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000370 ASSERT(page);
371 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400372}
373
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000374void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
375 ASSERT(page);
376 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400377}
378
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000379void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
380 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400381 int height,
382 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000383 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000384 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400385
Lei Zhanga98e3662018-02-07 20:28:35 +0000386 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
387 RenderSavedPageWithFlags(page, FPDF_ANNOT);
388 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400389}
390
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400391void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
392 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000393 FPDF_PAGE page = LoadSavedPage(0);
394 VerifySavedRendering(page, width, height, md5);
395 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400396 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400397}
398
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300399void EmbedderTest::SetWholeFileAvailable() {
400 ASSERT(fake_file_access_);
401 fake_file_access_->SetWholeFileAvailable();
402}
403
weili0dadcc62016-08-23 21:10:57 -0700404FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700405 FPDF_DOCUMENT document,
406 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700407 EmbedderTest* test = static_cast<EmbedderTest*>(info);
408 auto it = test->page_map_.find(page_index);
409 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700410}
411
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800412// static
413void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
414 int type) {
415 EmbedderTest* test = static_cast<EmbedderTest*>(info);
416 test->delegate_->UnsupportedHandler(type);
417}
418
419// static
420int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
421 FPDF_WIDESTRING message,
422 FPDF_WIDESTRING title,
423 int type,
424 int icon) {
425 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
426 return test->delegate_->Alert(message, title, type, icon);
427}
428
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700429// static
430int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 int msecs,
432 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700433 EmbedderTest* test = static_cast<EmbedderTest*>(info);
434 return test->delegate_->SetTimer(msecs, fn);
435}
436
437// static
438void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
439 EmbedderTest* test = static_cast<EmbedderTest*>(info);
440 return test->delegate_->KillTimer(id);
441}
442
Tom Sepez396e8722015-09-09 10:16:08 -0700443// static
444FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
445 FPDF_DOCUMENT document,
446 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700447 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
448 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700449}
450
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000451// static
452std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400453 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000454 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
455 FPDFBitmap_GetWidth(bitmap) *
456 GetBitmapBytesPerPixel(bitmap) *
457 FPDFBitmap_GetHeight(bitmap),
458 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400459 return CryptToBase16(digest);
460}
461
Henrique Nakashimadb269572018-01-16 19:02:15 +0000462#ifndef NDEBUG
463// static
464void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
465 const std::string& filename) {
466 const int stride = FPDFBitmap_GetStride(bitmap);
467 const int width = FPDFBitmap_GetWidth(bitmap);
468 const int height = FPDFBitmap_GetHeight(bitmap);
469 const auto* buffer =
470 static_cast<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap));
471
472 std::vector<unsigned char> png_encoding;
473 bool encoded = image_diff_png::EncodeBGRAPNG(buffer, width, height, stride,
474 false, &png_encoding);
475
476 ASSERT_TRUE(encoded);
477 ASSERT_LT(filename.size(), 256u);
478
479 std::ofstream png_file;
Henrique Nakashima6d6a2432018-01-17 16:52:46 +0000480 png_file.open(filename, std::ios_base::out | std::ios_base::binary);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000481 png_file.write(reinterpret_cast<char*>(&png_encoding.front()),
482 png_encoding.size());
483 ASSERT_TRUE(png_file.good());
484 png_file.close();
485}
486#endif
487
thestigbcd3e532016-11-21 13:37:28 -0800488// static
489void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
490 int expected_width,
491 int expected_height,
492 const char* expected_md5sum) {
493 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
494 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400495
496 // The expected stride is calculated using the same formula as in
497 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
498 const int expected_stride =
499 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800500 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
501
502 if (!expected_md5sum)
503 return;
504
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000505 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800506}
507
Nicolas Pena3ff54002017-07-05 11:55:35 -0400508// static
509int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
510 const void* data,
511 unsigned long size) {
512 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Lei Zhang0729be22018-02-05 21:13:51 +0000513 pThis->data_string_.append(static_cast<const char*>(data), size);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400514 return 1;
515}
516
517// static
518int EmbedderTest::GetBlockFromString(void* param,
519 unsigned long pos,
520 unsigned char* buf,
521 unsigned long size) {
522 std::string* new_file = static_cast<std::string*>(param);
523 if (!new_file || pos + size < pos)
524 return 0;
525
526 unsigned long file_size = new_file->size();
527 if (pos + size > file_size)
528 return 0;
529
530 memcpy(buf, new_file->data() + pos, size);
531 return 1;
532}
Lei Zhang75c81712018-02-08 17:22:39 +0000533
534int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
535 for (const auto& it : page_map_) {
536 if (it.second == page) {
537 int page_number = it.first;
538 ASSERT(page_number >= 0);
539 return page_number;
540 }
541 }
542 return -1;
543}