blob: bcdd41ac342c87bb984b7a8241672d4cd284f34f [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"
Lei Zhang4c64e962019-02-05 19:24:12 +000024#include "testing/utils/hash.h"
Wei Li091f7a02015-11-09 12:09:55 -080025#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
Lei Zhang378ec542018-10-18 19:15:47 +000098#ifdef PDF_ENABLE_V8
99void EmbedderTest::SetExternalIsolate(void* isolate) {
100 external_isolate_ = static_cast<v8::Isolate*>(isolate);
101}
102#endif // PDF_ENABLE_V8
103
Tom Sepezd483eb42016-01-06 10:03:59 -0800104bool EmbedderTest::CreateEmptyDocument() {
105 document_ = FPDF_CreateNewDocument();
106 if (!document_)
107 return false;
108
Tom Sepez0784c732018-04-23 18:02:57 +0000109 form_handle_ =
110 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -0800111 return true;
112}
113
Lei Zhang208eecf2017-12-20 19:40:50 +0000114bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000115 return OpenDocumentWithOptions(filename, nullptr,
116 LinearizeOption::kDefaultLinearize,
117 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000118}
119
120bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000121 return OpenDocumentWithOptions(filename, nullptr,
122 LinearizeOption::kMustLinearize,
123 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000124}
125
126bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
127 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000128 return OpenDocumentWithOptions(filename, password,
129 LinearizeOption::kDefaultLinearize,
130 JavaScriptOption::kEnableJavaScript);
131}
132
133bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
134 return OpenDocumentWithOptions(filename, nullptr,
135 LinearizeOption::kDefaultLinearize,
136 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000137}
138
139bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
140 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000141 LinearizeOption linearize_option,
142 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800143 std::string file_path;
144 if (!PathService::GetTestFilePath(filename, &file_path))
145 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000146
Wei Li091f7a02015-11-09 12:09:55 -0800147 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400148 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800149 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800150
thestig29ce9232016-06-22 07:03:23 -0700151 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800152 loader_ = new TestLoader(file_contents_.get(), file_length_);
Lei Zhang0729be22018-02-05 21:13:51 +0000153
154 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800155 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700156 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800157 file_access_.m_Param = loader_;
Lei Zhang0729be22018-02-05 21:13:51 +0000158
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300159 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000160 return OpenDocumentHelper(password, linearize_option, javascript_option,
161 fake_file_access_.get(), &document_, &avail_,
162 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400163}
Tom Sepez96d13342015-01-16 14:59:26 -0800164
Nicolas Pena56fc9722017-07-13 16:31:34 -0400165bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000166 LinearizeOption linearize_option,
167 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300168 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400169 FPDF_DOCUMENT* document,
170 FPDF_AVAIL* avail,
171 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300172 network_simulator->AddSegment(0, 1024);
173 network_simulator->SetRequestedDataAvailable();
174 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
175 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400176 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300177 int32_t nRet = PDF_DATA_NOTAVAIL;
178 while (nRet == PDF_DATA_NOTAVAIL) {
179 network_simulator->SetRequestedDataAvailable();
180 nRet =
181 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
182 }
183 if (nRet == PDF_DATA_ERROR)
184 return false;
185
Nicolas Pena56fc9722017-07-13 16:31:34 -0400186 *document = FPDFAvail_GetDocument(*avail, password);
187 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800188 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400189
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300190 nRet = PDF_DATA_NOTAVAIL;
191 while (nRet == PDF_DATA_NOTAVAIL) {
192 network_simulator->SetRequestedDataAvailable();
193 nRet =
194 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
195 }
196 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800197 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400198
199 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800200 for (int i = 0; i < page_count; ++i) {
201 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300202 while (nRet == PDF_DATA_NOTAVAIL) {
203 network_simulator->SetRequestedDataAvailable();
204 nRet = FPDFAvail_IsPageAvail(*avail, i,
205 network_simulator->GetDownloadHints());
206 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400207
208 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800209 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800210 }
211 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000212 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800213 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300214 network_simulator->SetWholeFileAvailable();
215 *document =
216 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400217 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800218 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800219 }
Tom Sepez0784c732018-04-23 18:02:57 +0000220 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
221
Tom Sepezc46d0002015-11-30 15:46:36 -0800222#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400223 int doc_type = FPDF_GetFormType(*document);
224 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
225 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800226#endif // PDF_ENABLE_XFA
Tom Sepez0784c732018-04-23 18:02:57 +0000227
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000228 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800229 return true;
230}
Tom Sepez96d13342015-01-16 14:59:26 -0800231
Tom Sepez0784c732018-04-23 18:02:57 +0000232FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
233 FPDF_DOCUMENT doc,
234 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800235 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400236 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200237 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800238 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400239 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800240
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800241 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
242 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800243#ifdef PDF_ENABLE_XFA
244 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800245#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800246 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800247#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700248 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
249 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700250 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000251 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
252
Tom Sepez0784c732018-04-23 18:02:57 +0000253 if (javascript_option == JavaScriptOption::kEnableJavaScript)
254 formfillinfo->m_pJsPlatform = platform;
255
Nicolas Pena3ff54002017-07-05 11:55:35 -0400256 FPDF_FORMHANDLE form_handle =
257 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000258 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
259 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400260 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
261 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800262}
263
Tom Sepezda8189e2015-01-30 14:41:50 -0800264void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400265 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800266 FORM_DoDocumentJSAction(form_handle_);
267 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800268}
269
270int EmbedderTest::GetFirstPageNum() {
271 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000272 (void)FPDFAvail_IsPageAvail(avail_, first_page,
273 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800274 return first_page;
275}
276
277int EmbedderTest::GetPageCount() {
278 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400279 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000280 (void)FPDFAvail_IsPageAvail(avail_, i,
281 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800282 return page_count;
283}
284
Tom Sepezda8189e2015-01-30 14:41:50 -0800285FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000286 return LoadPageCommon(page_number, true);
287}
288
289FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
290 return LoadPageCommon(page_number, false);
291}
292
293FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400294 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000295 ASSERT(page_number >= 0);
296 ASSERT(!pdfium::ContainsKey(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700297
Tom Sepez96d13342015-01-16 14:59:26 -0800298 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400299 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800300 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400301
Tom Sepez507d0192018-11-07 16:37:51 +0000302 if (do_events) {
303 FORM_OnAfterLoadPage(page, form_handle_);
304 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
305 }
weili0dadcc62016-08-23 21:10:57 -0700306 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700307 return page;
308}
309
Tom Sepezda8189e2015-01-30 14:41:50 -0800310void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000311 UnloadPageCommon(page, true);
312}
Lei Zhang75c81712018-02-08 17:22:39 +0000313
Tom Sepez507d0192018-11-07 16:37:51 +0000314void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
315 UnloadPageCommon(page, false);
316}
317
318void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
319 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000320 int page_number = GetPageNumberForLoadedPage(page);
321 if (page_number < 0) {
322 NOTREACHED();
323 return;
324 }
Tom Sepez507d0192018-11-07 16:37:51 +0000325 if (do_events) {
326 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
327 FORM_OnBeforeClosePage(page, form_handle_);
328 }
Tom Sepez96d13342015-01-16 14:59:26 -0800329 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000330 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800331}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800332
Tom Sepeze08d2b12018-04-25 18:49:32 +0000333ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000334 return RenderLoadedPageWithFlags(page, 0);
335}
336
Tom Sepeze08d2b12018-04-25 18:49:32 +0000337ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
338 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000339 if (GetPageNumberForLoadedPage(page) < 0) {
340 NOTREACHED();
341 return nullptr;
342 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000343 return RenderPageWithFlags(page, form_handle_, flags);
344}
345
Tom Sepeze08d2b12018-04-25 18:49:32 +0000346ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000347 return RenderSavedPageWithFlags(page, 0);
348}
349
Tom Sepeze08d2b12018-04-25 18:49:32 +0000350ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
351 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000352 if (GetPageNumberForSavedPage(page) < 0) {
353 NOTREACHED();
354 return nullptr;
355 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000356 return RenderPageWithFlags(page, saved_form_handle_, flags);
357}
358
359// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000360ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
361 FPDF_FORMHANDLE handle,
362 int flags) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000363 int width = static_cast<int>(FPDF_GetPageWidth(page));
364 int height = static_cast<int>(FPDF_GetPageHeight(page));
365 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000366 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000367 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
368 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
369 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
370 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
371 return bitmap;
372}
373
Lei Zhang30ff2532019-01-31 21:37:55 +0000374// static
375ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
376 return RenderPageWithFlags(page, nullptr, 0);
377}
378
Lei Zhang0b494052019-01-31 21:41:15 +0000379FPDF_DOCUMENT EmbedderTest::OpenSavedDocument() {
380 return OpenSavedDocumentWithPassword(nullptr);
381}
382
383FPDF_DOCUMENT EmbedderTest::OpenSavedDocumentWithPassword(
384 const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300385 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000386 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300387 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000388 // Copy data to prevent clearing it before saved document close.
389 saved_document_file_data_ = data_string_;
390 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400391
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300392 saved_fake_file_access_ =
393 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
394
Tom Sepez0784c732018-04-23 18:02:57 +0000395 EXPECT_TRUE(OpenDocumentHelper(
396 password, LinearizeOption::kDefaultLinearize,
397 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
398 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000399 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400400}
401
402void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000403 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400404
Lei Zhang0729be22018-02-05 21:13:51 +0000405 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
406 FPDF_CloseDocument(saved_document_);
407 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400408
Lei Zhang0729be22018-02-05 21:13:51 +0000409 saved_form_handle_ = nullptr;
410 saved_document_ = nullptr;
411 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400412}
413
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000414FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000415 ASSERT(saved_form_handle_);
416 ASSERT(page_number >= 0);
417 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400418
Lei Zhang0729be22018-02-05 21:13:51 +0000419 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000420 if (!page)
421 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400422
Lei Zhang9f72c452018-02-08 21:49:54 +0000423 FORM_OnAfterLoadPage(page, saved_form_handle_);
424 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
425 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000426 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400427}
428
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000429void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000430 ASSERT(saved_form_handle_);
431
432 int page_number = GetPageNumberForSavedPage(page);
433 if (page_number < 0) {
434 NOTREACHED();
435 return;
436 }
437
438 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
439 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000440 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000441
442 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400443}
444
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000445void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
446 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400447 int height,
448 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000449 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000450 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400451
Tom Sepeze08d2b12018-04-25 18:49:32 +0000452 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000453 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400454}
455
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400456void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Lei Zhang0b494052019-01-31 21:41:15 +0000457 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000458 FPDF_PAGE page = LoadSavedPage(0);
459 VerifySavedRendering(page, width, height, md5);
460 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400461 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400462}
463
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300464void EmbedderTest::SetWholeFileAvailable() {
465 ASSERT(fake_file_access_);
466 fake_file_access_->SetWholeFileAvailable();
467}
468
weili0dadcc62016-08-23 21:10:57 -0700469FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700470 FPDF_DOCUMENT document,
471 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700472 EmbedderTest* test = static_cast<EmbedderTest*>(info);
473 auto it = test->page_map_.find(page_index);
474 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700475}
476
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800477// static
478void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
479 int type) {
480 EmbedderTest* test = static_cast<EmbedderTest*>(info);
481 test->delegate_->UnsupportedHandler(type);
482}
483
484// static
485int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
486 FPDF_WIDESTRING message,
487 FPDF_WIDESTRING title,
488 int type,
489 int icon) {
490 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
491 return test->delegate_->Alert(message, title, type, icon);
492}
493
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700494// static
495int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 int msecs,
497 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700498 EmbedderTest* test = static_cast<EmbedderTest*>(info);
499 return test->delegate_->SetTimer(msecs, fn);
500}
501
502// static
503void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
504 EmbedderTest* test = static_cast<EmbedderTest*>(info);
505 return test->delegate_->KillTimer(id);
506}
507
Tom Sepez396e8722015-09-09 10:16:08 -0700508// static
509FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
510 FPDF_DOCUMENT document,
511 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700512 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
513 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700514}
515
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000516// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000517void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
518 FPDF_BYTESTRING uri) {
519 EmbedderTest* test = static_cast<EmbedderTest*>(info);
520 return test->delegate_->DoURIAction(uri);
521}
522
523// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000524std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400525 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000526 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
527 FPDFBitmap_GetWidth(bitmap) *
528 GetBitmapBytesPerPixel(bitmap) *
529 FPDFBitmap_GetHeight(bitmap),
530 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400531 return CryptToBase16(digest);
532}
533
Henrique Nakashimadb269572018-01-16 19:02:15 +0000534#ifndef NDEBUG
535// static
536void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
537 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000538 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000539}
540#endif
541
thestigbcd3e532016-11-21 13:37:28 -0800542// static
543void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
544 int expected_width,
545 int expected_height,
546 const char* expected_md5sum) {
547 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
548 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400549
550 // The expected stride is calculated using the same formula as in
551 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
552 const int expected_stride =
553 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800554 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
555
556 if (!expected_md5sum)
557 return;
558
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000559 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800560}
561
Nicolas Pena3ff54002017-07-05 11:55:35 -0400562// static
563int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
564 const void* data,
565 unsigned long size) {
566 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000567
Lei Zhang0729be22018-02-05 21:13:51 +0000568 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000569
570 if (pThis->filestream_.is_open())
571 pThis->filestream_.write(static_cast<const char*>(data), size);
572
Nicolas Pena3ff54002017-07-05 11:55:35 -0400573 return 1;
574}
575
576// static
577int EmbedderTest::GetBlockFromString(void* param,
578 unsigned long pos,
579 unsigned char* buf,
580 unsigned long size) {
581 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000582 if (!new_file || pos + size < pos) {
583 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400584 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000585 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400586
Lei Zhangcef91f12019-01-08 21:32:51 +0000587 if (pos + size > new_file->size()) {
588 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400589 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000590 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400591
592 memcpy(buf, new_file->data() + pos, size);
593 return 1;
594}
Lei Zhang75c81712018-02-08 17:22:39 +0000595
Lei Zhang9f72c452018-02-08 21:49:54 +0000596// static
597int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
598 FPDF_PAGE page) {
599 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000600 if (it.second == page) {
601 int page_number = it.first;
602 ASSERT(page_number >= 0);
603 return page_number;
604 }
605 }
606 return -1;
607}
Lei Zhang9f72c452018-02-08 21:49:54 +0000608
609int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
610 return GetPageNumberForPage(page_map_, page);
611}
612
613int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
614 return GetPageNumberForPage(saved_page_map_, page);
615}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000616
617void EmbedderTest::OpenPDFFileForWrite(const char* filename) {
618 filestream_.open(filename, std::ios_base::binary);
619}
620
621void EmbedderTest::ClosePDFFileForWrite() {
622 filestream_.close();
623}