blob: 215009ceac29388f6dab771633b358643470efb0 [file] [log] [blame]
Lei Zhang1ac47eb2015-12-21 11:04:44 -08001// Copyright 2015 PDFium Authors. All rights reserved.
Tom Sepez96d13342015-01-16 14:59:26 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Wei Li091f7a02015-11-09 12:09:55 -08005#include "testing/embedder_test.h"
Tom Sepez96d13342015-01-16 14:59:26 -08006
7#include <limits.h>
Tom Sepez96d13342015-01-16 14:59:26 -08008
9#include <list>
Lei Zhang9f72c452018-02-08 21:49:54 +000010#include <map>
Lei Zhanga98e3662018-02-07 20:28:35 +000011#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -080012#include <string>
13#include <utility>
Tom Sepez96d13342015-01-16 14:59:26 -080014
thestigbcd3e532016-11-21 13:37:28 -080015#include "core/fdrm/crypto/fx_crypt.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000016#include "public/cpp/fpdf_scopers.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080017#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080018#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080019#include "public/fpdf_text.h"
20#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080021#include "testing/gmock/include/gmock/gmock.h"
Wei Li091f7a02015-11-09 12:09:55 -080022#include "testing/test_support.h"
Henrique Nakashimaf956bad2018-08-16 16:41:42 +000023#include "testing/utils/bitmap_saver.h"
Wei Li091f7a02015-11-09 12:09:55 -080024#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());
Lei Zhang9f72c452018-02-08 21:49:54 +000084 EXPECT_EQ(0U, saved_page_map_.size());
Lei Zhang75c81712018-02-08 17:22:39 +000085
Tom Sepezda8189e2015-01-30 14:41:50 -080086 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -070087 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -080088 FPDFDOC_ExitFormFillEnvironment(form_handle_);
89 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -080090 }
Tom Sepezc46d0002015-11-30 15:46:36 -080091
Tom Sepez96d13342015-01-16 14:59:26 -080092 FPDFAvail_Destroy(avail_);
93 FPDF_DestroyLibrary();
Lei Zhangd27acae2015-05-15 15:36:02 -070094 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -080095}
96
Tom Sepezd483eb42016-01-06 10:03:59 -080097bool EmbedderTest::CreateEmptyDocument() {
98 document_ = FPDF_CreateNewDocument();
99 if (!document_)
100 return false;
101
Tom Sepez0784c732018-04-23 18:02:57 +0000102 form_handle_ =
103 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
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) {
Tom Sepez0784c732018-04-23 18:02:57 +0000108 return OpenDocumentWithOptions(filename, nullptr,
109 LinearizeOption::kDefaultLinearize,
110 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000111}
112
113bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000114 return OpenDocumentWithOptions(filename, nullptr,
115 LinearizeOption::kMustLinearize,
116 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000117}
118
119bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
120 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000121 return OpenDocumentWithOptions(filename, password,
122 LinearizeOption::kDefaultLinearize,
123 JavaScriptOption::kEnableJavaScript);
124}
125
126bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
127 return OpenDocumentWithOptions(filename, nullptr,
128 LinearizeOption::kDefaultLinearize,
129 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000130}
131
132bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
133 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000134 LinearizeOption linearize_option,
135 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800136 std::string file_path;
137 if (!PathService::GetTestFilePath(filename, &file_path))
138 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000139
Wei Li091f7a02015-11-09 12:09:55 -0800140 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400141 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800142 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800143
thestig29ce9232016-06-22 07:03:23 -0700144 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800145 loader_ = new TestLoader(file_contents_.get(), file_length_);
Lei Zhang0729be22018-02-05 21:13:51 +0000146
147 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800148 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700149 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800150 file_access_.m_Param = loader_;
Lei Zhang0729be22018-02-05 21:13:51 +0000151
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300152 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000153 return OpenDocumentHelper(password, linearize_option, javascript_option,
154 fake_file_access_.get(), &document_, &avail_,
155 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400156}
Tom Sepez96d13342015-01-16 14:59:26 -0800157
Nicolas Pena56fc9722017-07-13 16:31:34 -0400158bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000159 LinearizeOption linearize_option,
160 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300161 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400162 FPDF_DOCUMENT* document,
163 FPDF_AVAIL* avail,
164 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300165 network_simulator->AddSegment(0, 1024);
166 network_simulator->SetRequestedDataAvailable();
167 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
168 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400169 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300170 int32_t nRet = PDF_DATA_NOTAVAIL;
171 while (nRet == PDF_DATA_NOTAVAIL) {
172 network_simulator->SetRequestedDataAvailable();
173 nRet =
174 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
175 }
176 if (nRet == PDF_DATA_ERROR)
177 return false;
178
Nicolas Pena56fc9722017-07-13 16:31:34 -0400179 *document = FPDFAvail_GetDocument(*avail, password);
180 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800181 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400182
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300183 nRet = PDF_DATA_NOTAVAIL;
184 while (nRet == PDF_DATA_NOTAVAIL) {
185 network_simulator->SetRequestedDataAvailable();
186 nRet =
187 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
188 }
189 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800190 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400191
192 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800193 for (int i = 0; i < page_count; ++i) {
194 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300195 while (nRet == PDF_DATA_NOTAVAIL) {
196 network_simulator->SetRequestedDataAvailable();
197 nRet = FPDFAvail_IsPageAvail(*avail, i,
198 network_simulator->GetDownloadHints());
199 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400200
201 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800202 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800203 }
204 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000205 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800206 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300207 network_simulator->SetWholeFileAvailable();
208 *document =
209 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400210 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800211 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800212 }
Tom Sepez0784c732018-04-23 18:02:57 +0000213 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
214
Tom Sepezc46d0002015-11-30 15:46:36 -0800215#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400216 int doc_type = FPDF_GetFormType(*document);
217 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
218 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800219#endif // PDF_ENABLE_XFA
Tom Sepez0784c732018-04-23 18:02:57 +0000220
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000221 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800222 return true;
223}
Tom Sepez96d13342015-01-16 14:59:26 -0800224
Tom Sepez0784c732018-04-23 18:02:57 +0000225FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
226 FPDF_DOCUMENT doc,
227 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800228 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400229 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200230 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800231 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400232 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800233
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800234 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
235 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800236#ifdef PDF_ENABLE_XFA
237 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800238#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800239 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800240#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700241 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
242 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700243 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez0784c732018-04-23 18:02:57 +0000244 if (javascript_option == JavaScriptOption::kEnableJavaScript)
245 formfillinfo->m_pJsPlatform = platform;
246
Nicolas Pena3ff54002017-07-05 11:55:35 -0400247 FPDF_FORMHANDLE form_handle =
248 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000249 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
250 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400251 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
252 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800253}
254
Tom Sepezda8189e2015-01-30 14:41:50 -0800255void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400256 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800257 FORM_DoDocumentJSAction(form_handle_);
258 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800259}
260
261int EmbedderTest::GetFirstPageNum() {
262 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000263 (void)FPDFAvail_IsPageAvail(avail_, first_page,
264 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800265 return first_page;
266}
267
268int EmbedderTest::GetPageCount() {
269 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400270 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000271 (void)FPDFAvail_IsPageAvail(avail_, i,
272 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800273 return page_count;
274}
275
Tom Sepezda8189e2015-01-30 14:41:50 -0800276FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400277 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000278 ASSERT(page_number >= 0);
279 ASSERT(!pdfium::ContainsKey(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700280
Tom Sepez96d13342015-01-16 14:59:26 -0800281 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400282 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800283 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400284
Tom Sepezda8189e2015-01-30 14:41:50 -0800285 FORM_OnAfterLoadPage(page, form_handle_);
286 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700287 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700288 return page;
289}
290
Tom Sepezda8189e2015-01-30 14:41:50 -0800291void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400292 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000293
294 int page_number = GetPageNumberForLoadedPage(page);
295 if (page_number < 0) {
296 NOTREACHED();
297 return;
298 }
299
Tom Sepezda8189e2015-01-30 14:41:50 -0800300 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
301 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800302 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700303
Lei Zhang75c81712018-02-08 17:22:39 +0000304 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800305}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800306
Tom Sepeze08d2b12018-04-25 18:49:32 +0000307ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000308 return RenderLoadedPageWithFlags(page, 0);
309}
310
Tom Sepeze08d2b12018-04-25 18:49:32 +0000311ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
312 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000313 if (GetPageNumberForLoadedPage(page) < 0) {
314 NOTREACHED();
315 return nullptr;
316 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000317 return RenderPageWithFlags(page, form_handle_, flags);
318}
319
Tom Sepeze08d2b12018-04-25 18:49:32 +0000320ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000321 return RenderSavedPageWithFlags(page, 0);
322}
323
Tom Sepeze08d2b12018-04-25 18:49:32 +0000324ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
325 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000326 if (GetPageNumberForSavedPage(page) < 0) {
327 NOTREACHED();
328 return nullptr;
329 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000330 return RenderPageWithFlags(page, saved_form_handle_, flags);
331}
332
333// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000334ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
335 FPDF_FORMHANDLE handle,
336 int flags) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000337 int width = static_cast<int>(FPDF_GetPageWidth(page));
338 int height = static_cast<int>(FPDF_GetPageHeight(page));
339 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000340 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000341 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
342 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
343 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
344 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
345 return bitmap;
346}
347
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400348FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300349 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000350 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300351 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000352 // Copy data to prevent clearing it before saved document close.
353 saved_document_file_data_ = data_string_;
354 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400355
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300356 saved_fake_file_access_ =
357 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
358
Tom Sepez0784c732018-04-23 18:02:57 +0000359 EXPECT_TRUE(OpenDocumentHelper(
360 password, LinearizeOption::kDefaultLinearize,
361 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
362 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000363 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400364}
365
366void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000367 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400368
Lei Zhang0729be22018-02-05 21:13:51 +0000369 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
370 FPDF_CloseDocument(saved_document_);
371 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400372
Lei Zhang0729be22018-02-05 21:13:51 +0000373 saved_form_handle_ = nullptr;
374 saved_document_ = nullptr;
375 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400376}
377
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000378FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000379 ASSERT(saved_form_handle_);
380 ASSERT(page_number >= 0);
381 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400382
Lei Zhang0729be22018-02-05 21:13:51 +0000383 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000384 if (!page)
385 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400386
Lei Zhang9f72c452018-02-08 21:49:54 +0000387 FORM_OnAfterLoadPage(page, saved_form_handle_);
388 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
389 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000390 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400391}
392
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000393void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000394 ASSERT(saved_form_handle_);
395
396 int page_number = GetPageNumberForSavedPage(page);
397 if (page_number < 0) {
398 NOTREACHED();
399 return;
400 }
401
402 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
403 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000404 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000405
406 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400407}
408
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000409void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
410 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400411 int height,
412 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000413 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000414 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400415
Tom Sepeze08d2b12018-04-25 18:49:32 +0000416 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000417 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400418}
419
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400420void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Tom Sepezb9c3e272018-08-14 18:22:06 +0000421 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000422 FPDF_PAGE page = LoadSavedPage(0);
423 VerifySavedRendering(page, width, height, md5);
424 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400425 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400426}
427
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300428void EmbedderTest::SetWholeFileAvailable() {
429 ASSERT(fake_file_access_);
430 fake_file_access_->SetWholeFileAvailable();
431}
432
weili0dadcc62016-08-23 21:10:57 -0700433FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700434 FPDF_DOCUMENT document,
435 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700436 EmbedderTest* test = static_cast<EmbedderTest*>(info);
437 auto it = test->page_map_.find(page_index);
438 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700439}
440
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800441// static
442void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
443 int type) {
444 EmbedderTest* test = static_cast<EmbedderTest*>(info);
445 test->delegate_->UnsupportedHandler(type);
446}
447
448// static
449int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
450 FPDF_WIDESTRING message,
451 FPDF_WIDESTRING title,
452 int type,
453 int icon) {
454 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
455 return test->delegate_->Alert(message, title, type, icon);
456}
457
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700458// static
459int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 int msecs,
461 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700462 EmbedderTest* test = static_cast<EmbedderTest*>(info);
463 return test->delegate_->SetTimer(msecs, fn);
464}
465
466// static
467void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
468 EmbedderTest* test = static_cast<EmbedderTest*>(info);
469 return test->delegate_->KillTimer(id);
470}
471
Tom Sepez396e8722015-09-09 10:16:08 -0700472// static
473FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
474 FPDF_DOCUMENT document,
475 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700476 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
477 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700478}
479
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000480// static
481std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400482 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000483 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
484 FPDFBitmap_GetWidth(bitmap) *
485 GetBitmapBytesPerPixel(bitmap) *
486 FPDFBitmap_GetHeight(bitmap),
487 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400488 return CryptToBase16(digest);
489}
490
Henrique Nakashimadb269572018-01-16 19:02:15 +0000491#ifndef NDEBUG
492// static
493void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
494 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000495 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000496}
497#endif
498
thestigbcd3e532016-11-21 13:37:28 -0800499// static
500void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
501 int expected_width,
502 int expected_height,
503 const char* expected_md5sum) {
504 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
505 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400506
507 // The expected stride is calculated using the same formula as in
508 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
509 const int expected_stride =
510 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800511 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
512
513 if (!expected_md5sum)
514 return;
515
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000516 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800517}
518
Nicolas Pena3ff54002017-07-05 11:55:35 -0400519// static
520int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
521 const void* data,
522 unsigned long size) {
523 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000524
Lei Zhang0729be22018-02-05 21:13:51 +0000525 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000526
527 if (pThis->filestream_.is_open())
528 pThis->filestream_.write(static_cast<const char*>(data), size);
529
Nicolas Pena3ff54002017-07-05 11:55:35 -0400530 return 1;
531}
532
533// static
534int EmbedderTest::GetBlockFromString(void* param,
535 unsigned long pos,
536 unsigned char* buf,
537 unsigned long size) {
538 std::string* new_file = static_cast<std::string*>(param);
539 if (!new_file || pos + size < pos)
540 return 0;
541
542 unsigned long file_size = new_file->size();
543 if (pos + size > file_size)
544 return 0;
545
546 memcpy(buf, new_file->data() + pos, size);
547 return 1;
548}
Lei Zhang75c81712018-02-08 17:22:39 +0000549
Lei Zhang9f72c452018-02-08 21:49:54 +0000550// static
551int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
552 FPDF_PAGE page) {
553 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000554 if (it.second == page) {
555 int page_number = it.first;
556 ASSERT(page_number >= 0);
557 return page_number;
558 }
559 }
560 return -1;
561}
Lei Zhang9f72c452018-02-08 21:49:54 +0000562
563int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
564 return GetPageNumberForPage(page_map_, page);
565}
566
567int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
568 return GetPageNumberForPage(saved_page_map_, page);
569}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000570
571void EmbedderTest::OpenPDFFileForWrite(const char* filename) {
572 filestream_.open(filename, std::ios_base::binary);
573}
574
575void EmbedderTest::ClosePDFFileForWrite() {
576 filestream_.close();
577}