blob: 10b944d6083911931ef3294f5150b6ce0c880349 [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
Lei Zhangd145e4b2018-10-12 18:54:31 +000015#include "core/fdrm/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;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000244 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
245
Tom Sepez0784c732018-04-23 18:02:57 +0000246 if (javascript_option == JavaScriptOption::kEnableJavaScript)
247 formfillinfo->m_pJsPlatform = platform;
248
Nicolas Pena3ff54002017-07-05 11:55:35 -0400249 FPDF_FORMHANDLE form_handle =
250 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000251 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
252 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400253 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
254 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800255}
256
Tom Sepezda8189e2015-01-30 14:41:50 -0800257void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400258 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800259 FORM_DoDocumentJSAction(form_handle_);
260 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800261}
262
263int EmbedderTest::GetFirstPageNum() {
264 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000265 (void)FPDFAvail_IsPageAvail(avail_, first_page,
266 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800267 return first_page;
268}
269
270int EmbedderTest::GetPageCount() {
271 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400272 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000273 (void)FPDFAvail_IsPageAvail(avail_, i,
274 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800275 return page_count;
276}
277
Tom Sepezda8189e2015-01-30 14:41:50 -0800278FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400279 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000280 ASSERT(page_number >= 0);
281 ASSERT(!pdfium::ContainsKey(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700282
Tom Sepez96d13342015-01-16 14:59:26 -0800283 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400284 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800285 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400286
Tom Sepezda8189e2015-01-30 14:41:50 -0800287 FORM_OnAfterLoadPage(page, form_handle_);
288 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700289 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700290 return page;
291}
292
Tom Sepezda8189e2015-01-30 14:41:50 -0800293void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400294 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000295
296 int page_number = GetPageNumberForLoadedPage(page);
297 if (page_number < 0) {
298 NOTREACHED();
299 return;
300 }
301
Tom Sepezda8189e2015-01-30 14:41:50 -0800302 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
303 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800304 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700305
Lei Zhang75c81712018-02-08 17:22:39 +0000306 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800307}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800308
Tom Sepeze08d2b12018-04-25 18:49:32 +0000309ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000310 return RenderLoadedPageWithFlags(page, 0);
311}
312
Tom Sepeze08d2b12018-04-25 18:49:32 +0000313ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
314 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000315 if (GetPageNumberForLoadedPage(page) < 0) {
316 NOTREACHED();
317 return nullptr;
318 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000319 return RenderPageWithFlags(page, form_handle_, flags);
320}
321
Tom Sepeze08d2b12018-04-25 18:49:32 +0000322ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000323 return RenderSavedPageWithFlags(page, 0);
324}
325
Tom Sepeze08d2b12018-04-25 18:49:32 +0000326ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
327 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000328 if (GetPageNumberForSavedPage(page) < 0) {
329 NOTREACHED();
330 return nullptr;
331 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000332 return RenderPageWithFlags(page, saved_form_handle_, flags);
333}
334
335// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000336ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
337 FPDF_FORMHANDLE handle,
338 int flags) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000339 int width = static_cast<int>(FPDF_GetPageWidth(page));
340 int height = static_cast<int>(FPDF_GetPageHeight(page));
341 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000342 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000343 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
344 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
345 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
346 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
347 return bitmap;
348}
349
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400350FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300351 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000352 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300353 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000354 // Copy data to prevent clearing it before saved document close.
355 saved_document_file_data_ = data_string_;
356 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400357
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300358 saved_fake_file_access_ =
359 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
360
Tom Sepez0784c732018-04-23 18:02:57 +0000361 EXPECT_TRUE(OpenDocumentHelper(
362 password, LinearizeOption::kDefaultLinearize,
363 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
364 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000365 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400366}
367
368void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000369 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400370
Lei Zhang0729be22018-02-05 21:13:51 +0000371 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
372 FPDF_CloseDocument(saved_document_);
373 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400374
Lei Zhang0729be22018-02-05 21:13:51 +0000375 saved_form_handle_ = nullptr;
376 saved_document_ = nullptr;
377 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400378}
379
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000380FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000381 ASSERT(saved_form_handle_);
382 ASSERT(page_number >= 0);
383 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400384
Lei Zhang0729be22018-02-05 21:13:51 +0000385 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000386 if (!page)
387 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400388
Lei Zhang9f72c452018-02-08 21:49:54 +0000389 FORM_OnAfterLoadPage(page, saved_form_handle_);
390 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
391 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000392 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400393}
394
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000395void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000396 ASSERT(saved_form_handle_);
397
398 int page_number = GetPageNumberForSavedPage(page);
399 if (page_number < 0) {
400 NOTREACHED();
401 return;
402 }
403
404 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
405 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000406 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000407
408 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400409}
410
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000411void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
412 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400413 int height,
414 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000415 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000416 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400417
Tom Sepeze08d2b12018-04-25 18:49:32 +0000418 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000419 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400420}
421
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400422void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Tom Sepezb9c3e272018-08-14 18:22:06 +0000423 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000424 FPDF_PAGE page = LoadSavedPage(0);
425 VerifySavedRendering(page, width, height, md5);
426 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400427 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400428}
429
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300430void EmbedderTest::SetWholeFileAvailable() {
431 ASSERT(fake_file_access_);
432 fake_file_access_->SetWholeFileAvailable();
433}
434
weili0dadcc62016-08-23 21:10:57 -0700435FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700436 FPDF_DOCUMENT document,
437 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700438 EmbedderTest* test = static_cast<EmbedderTest*>(info);
439 auto it = test->page_map_.find(page_index);
440 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700441}
442
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800443// static
444void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
445 int type) {
446 EmbedderTest* test = static_cast<EmbedderTest*>(info);
447 test->delegate_->UnsupportedHandler(type);
448}
449
450// static
451int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
452 FPDF_WIDESTRING message,
453 FPDF_WIDESTRING title,
454 int type,
455 int icon) {
456 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
457 return test->delegate_->Alert(message, title, type, icon);
458}
459
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700460// static
461int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462 int msecs,
463 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700464 EmbedderTest* test = static_cast<EmbedderTest*>(info);
465 return test->delegate_->SetTimer(msecs, fn);
466}
467
468// static
469void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
470 EmbedderTest* test = static_cast<EmbedderTest*>(info);
471 return test->delegate_->KillTimer(id);
472}
473
Tom Sepez396e8722015-09-09 10:16:08 -0700474// static
475FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
476 FPDF_DOCUMENT document,
477 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700478 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
479 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700480}
481
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000482// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000483void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
484 FPDF_BYTESTRING uri) {
485 EmbedderTest* test = static_cast<EmbedderTest*>(info);
486 return test->delegate_->DoURIAction(uri);
487}
488
489// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000490std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400491 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000492 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
493 FPDFBitmap_GetWidth(bitmap) *
494 GetBitmapBytesPerPixel(bitmap) *
495 FPDFBitmap_GetHeight(bitmap),
496 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400497 return CryptToBase16(digest);
498}
499
Henrique Nakashimadb269572018-01-16 19:02:15 +0000500#ifndef NDEBUG
501// static
502void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
503 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000504 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000505}
506#endif
507
thestigbcd3e532016-11-21 13:37:28 -0800508// static
509void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
510 int expected_width,
511 int expected_height,
512 const char* expected_md5sum) {
513 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
514 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400515
516 // The expected stride is calculated using the same formula as in
517 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
518 const int expected_stride =
519 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800520 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
521
522 if (!expected_md5sum)
523 return;
524
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000525 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800526}
527
Nicolas Pena3ff54002017-07-05 11:55:35 -0400528// static
529int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
530 const void* data,
531 unsigned long size) {
532 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000533
Lei Zhang0729be22018-02-05 21:13:51 +0000534 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000535
536 if (pThis->filestream_.is_open())
537 pThis->filestream_.write(static_cast<const char*>(data), size);
538
Nicolas Pena3ff54002017-07-05 11:55:35 -0400539 return 1;
540}
541
542// static
543int EmbedderTest::GetBlockFromString(void* param,
544 unsigned long pos,
545 unsigned char* buf,
546 unsigned long size) {
547 std::string* new_file = static_cast<std::string*>(param);
548 if (!new_file || pos + size < pos)
549 return 0;
550
551 unsigned long file_size = new_file->size();
552 if (pos + size > file_size)
553 return 0;
554
555 memcpy(buf, new_file->data() + pos, size);
556 return 1;
557}
Lei Zhang75c81712018-02-08 17:22:39 +0000558
Lei Zhang9f72c452018-02-08 21:49:54 +0000559// static
560int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
561 FPDF_PAGE page) {
562 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000563 if (it.second == page) {
564 int page_number = it.first;
565 ASSERT(page_number >= 0);
566 return page_number;
567 }
568 }
569 return -1;
570}
Lei Zhang9f72c452018-02-08 21:49:54 +0000571
572int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
573 return GetPageNumberForPage(page_map_, page);
574}
575
576int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
577 return GetPageNumberForPage(saved_page_map_, page);
578}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000579
580void EmbedderTest::OpenPDFFileForWrite(const char* filename) {
581 filestream_.open(filename, std::ios_base::binary);
582}
583
584void EmbedderTest::ClosePDFFileForWrite() {
585 filestream_.close();
586}