blob: e49f289948a5c808732cbe0d9c63c23509d8bffc [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 +0000285FPDF_BITMAP EmbedderTest::RenderPageDeprecated(FPDF_PAGE page) {
286 return RenderPageWithFlagsDeprecated(page, form_handle_, 0);
287}
288
289std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderLoadedPage(
290 FPDF_PAGE page) {
291 return RenderLoadedPageWithFlags(page, 0);
292}
293
294std::unique_ptr<void, FPDFBitmapDeleter>
295EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page, int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000296 if (GetPageNumberForLoadedPage(page) < 0) {
297 NOTREACHED();
298 return nullptr;
299 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000300 return RenderPageWithFlags(page, form_handle_, flags);
301}
302
303std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderSavedPage(
304 FPDF_PAGE page) {
305 return RenderSavedPageWithFlags(page, 0);
306}
307
308std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderSavedPageWithFlags(
309 FPDF_PAGE page,
310 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000311 if (GetPageNumberForSavedPage(page) < 0) {
312 NOTREACHED();
313 return nullptr;
314 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000315 return RenderPageWithFlags(page, saved_form_handle_, flags);
316}
317
318// static
319FPDF_BITMAP EmbedderTest::RenderPageWithFlagsDeprecated(FPDF_PAGE page,
320 FPDF_FORMHANDLE handle,
321 int flags) {
322 return RenderPageWithFlags(page, handle, flags).release();
323}
324
325// static
326std::unique_ptr<void, FPDFBitmapDeleter> EmbedderTest::RenderPageWithFlags(
327 FPDF_PAGE page,
328 FPDF_FORMHANDLE handle,
329 int flags) {
330 int width = static_cast<int>(FPDF_GetPageWidth(page));
331 int height = static_cast<int>(FPDF_GetPageHeight(page));
332 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
333 std::unique_ptr<void, FPDFBitmapDeleter> bitmap(
334 FPDFBitmap_Create(width, height, alpha));
335 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
336 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
337 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
338 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
339 return bitmap;
340}
341
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400342FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300343 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000344 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300345 saved_file_access_.m_GetBlock = GetBlockFromString;
Lei Zhang0729be22018-02-05 21:13:51 +0000346 saved_file_access_.m_Param = &data_string_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400347
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300348 saved_fake_file_access_ =
349 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
350
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400351 EXPECT_TRUE(OpenDocumentHelper(password, false, saved_fake_file_access_.get(),
Lei Zhang0729be22018-02-05 21:13:51 +0000352 &saved_document_, &saved_avail_,
353 &saved_form_handle_));
354 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400355}
356
357void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000358 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400359
Lei Zhang0729be22018-02-05 21:13:51 +0000360 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
361 FPDF_CloseDocument(saved_document_);
362 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400363
Lei Zhang0729be22018-02-05 21:13:51 +0000364 saved_form_handle_ = nullptr;
365 saved_document_ = nullptr;
366 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400367}
368
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000369FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000370 ASSERT(saved_form_handle_);
371 ASSERT(page_number >= 0);
372 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400373
Lei Zhang0729be22018-02-05 21:13:51 +0000374 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000375 if (!page)
376 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400377
Lei Zhang9f72c452018-02-08 21:49:54 +0000378 FORM_OnAfterLoadPage(page, saved_form_handle_);
379 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
380 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000381 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400382}
383
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000384void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000385 ASSERT(saved_form_handle_);
386
387 int page_number = GetPageNumberForSavedPage(page);
388 if (page_number < 0) {
389 NOTREACHED();
390 return;
391 }
392
393 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
394 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000395 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000396
397 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400398}
399
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000400void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
401 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400402 int height,
403 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000404 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000405 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400406
Lei Zhanga98e3662018-02-07 20:28:35 +0000407 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
408 RenderSavedPageWithFlags(page, FPDF_ANNOT);
409 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400410}
411
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400412void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
413 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000414 FPDF_PAGE page = LoadSavedPage(0);
415 VerifySavedRendering(page, width, height, md5);
416 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400417 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400418}
419
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300420void EmbedderTest::SetWholeFileAvailable() {
421 ASSERT(fake_file_access_);
422 fake_file_access_->SetWholeFileAvailable();
423}
424
weili0dadcc62016-08-23 21:10:57 -0700425FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700426 FPDF_DOCUMENT document,
427 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700428 EmbedderTest* test = static_cast<EmbedderTest*>(info);
429 auto it = test->page_map_.find(page_index);
430 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700431}
432
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800433// static
434void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
435 int type) {
436 EmbedderTest* test = static_cast<EmbedderTest*>(info);
437 test->delegate_->UnsupportedHandler(type);
438}
439
440// static
441int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
442 FPDF_WIDESTRING message,
443 FPDF_WIDESTRING title,
444 int type,
445 int icon) {
446 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
447 return test->delegate_->Alert(message, title, type, icon);
448}
449
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700450// static
451int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 int msecs,
453 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700454 EmbedderTest* test = static_cast<EmbedderTest*>(info);
455 return test->delegate_->SetTimer(msecs, fn);
456}
457
458// static
459void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
460 EmbedderTest* test = static_cast<EmbedderTest*>(info);
461 return test->delegate_->KillTimer(id);
462}
463
Tom Sepez396e8722015-09-09 10:16:08 -0700464// static
465FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
466 FPDF_DOCUMENT document,
467 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700468 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
469 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700470}
471
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000472// static
473std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400474 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000475 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
476 FPDFBitmap_GetWidth(bitmap) *
477 GetBitmapBytesPerPixel(bitmap) *
478 FPDFBitmap_GetHeight(bitmap),
479 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400480 return CryptToBase16(digest);
481}
482
Henrique Nakashimadb269572018-01-16 19:02:15 +0000483#ifndef NDEBUG
484// static
485void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
486 const std::string& filename) {
487 const int stride = FPDFBitmap_GetStride(bitmap);
488 const int width = FPDFBitmap_GetWidth(bitmap);
489 const int height = FPDFBitmap_GetHeight(bitmap);
490 const auto* buffer =
491 static_cast<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap));
492
493 std::vector<unsigned char> png_encoding;
494 bool encoded = image_diff_png::EncodeBGRAPNG(buffer, width, height, stride,
495 false, &png_encoding);
496
497 ASSERT_TRUE(encoded);
498 ASSERT_LT(filename.size(), 256u);
499
500 std::ofstream png_file;
Henrique Nakashima6d6a2432018-01-17 16:52:46 +0000501 png_file.open(filename, std::ios_base::out | std::ios_base::binary);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000502 png_file.write(reinterpret_cast<char*>(&png_encoding.front()),
503 png_encoding.size());
504 ASSERT_TRUE(png_file.good());
505 png_file.close();
506}
507#endif
508
thestigbcd3e532016-11-21 13:37:28 -0800509// static
510void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
511 int expected_width,
512 int expected_height,
513 const char* expected_md5sum) {
514 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
515 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400516
517 // The expected stride is calculated using the same formula as in
518 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
519 const int expected_stride =
520 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800521 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
522
523 if (!expected_md5sum)
524 return;
525
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000526 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800527}
528
Nicolas Pena3ff54002017-07-05 11:55:35 -0400529// static
530int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
531 const void* data,
532 unsigned long size) {
533 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Lei Zhang0729be22018-02-05 21:13:51 +0000534 pThis->data_string_.append(static_cast<const char*>(data), size);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400535 return 1;
536}
537
538// static
539int EmbedderTest::GetBlockFromString(void* param,
540 unsigned long pos,
541 unsigned char* buf,
542 unsigned long size) {
543 std::string* new_file = static_cast<std::string*>(param);
544 if (!new_file || pos + size < pos)
545 return 0;
546
547 unsigned long file_size = new_file->size();
548 if (pos + size > file_size)
549 return 0;
550
551 memcpy(buf, new_file->data() + pos, size);
552 return 1;
553}
Lei Zhang75c81712018-02-08 17:22:39 +0000554
Lei Zhang9f72c452018-02-08 21:49:54 +0000555// static
556int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
557 FPDF_PAGE page) {
558 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000559 if (it.second == page) {
560 int page_number = it.first;
561 ASSERT(page_number >= 0);
562 return page_number;
563 }
564 }
565 return -1;
566}
Lei Zhang9f72c452018-02-08 21:49:54 +0000567
568int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
569 return GetPageNumberForPage(page_map_, page);
570}
571
572int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
573 return GetPageNumberForPage(saved_page_map_, page);
574}