blob: 8afd4b34ccdcf30ea1f03306c85ccefbfb218457 [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
Lei Zhang378ec542018-10-18 19:15:47 +000097#ifdef PDF_ENABLE_V8
98void EmbedderTest::SetExternalIsolate(void* isolate) {
99 external_isolate_ = static_cast<v8::Isolate*>(isolate);
100}
101#endif // PDF_ENABLE_V8
102
Tom Sepezd483eb42016-01-06 10:03:59 -0800103bool EmbedderTest::CreateEmptyDocument() {
104 document_ = FPDF_CreateNewDocument();
105 if (!document_)
106 return false;
107
Tom Sepez0784c732018-04-23 18:02:57 +0000108 form_handle_ =
109 SetupFormFillEnvironment(document_, JavaScriptOption::kEnableJavaScript);
Tom Sepezd483eb42016-01-06 10:03:59 -0800110 return true;
111}
112
Lei Zhang208eecf2017-12-20 19:40:50 +0000113bool EmbedderTest::OpenDocument(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000114 return OpenDocumentWithOptions(filename, nullptr,
115 LinearizeOption::kDefaultLinearize,
116 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000117}
118
119bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
Tom Sepez0784c732018-04-23 18:02:57 +0000120 return OpenDocumentWithOptions(filename, nullptr,
121 LinearizeOption::kMustLinearize,
122 JavaScriptOption::kEnableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000123}
124
125bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
126 const char* password) {
Tom Sepez0784c732018-04-23 18:02:57 +0000127 return OpenDocumentWithOptions(filename, password,
128 LinearizeOption::kDefaultLinearize,
129 JavaScriptOption::kEnableJavaScript);
130}
131
132bool EmbedderTest::OpenDocumentWithoutJavaScript(const std::string& filename) {
133 return OpenDocumentWithOptions(filename, nullptr,
134 LinearizeOption::kDefaultLinearize,
135 JavaScriptOption::kDisableJavaScript);
Lei Zhang208eecf2017-12-20 19:40:50 +0000136}
137
138bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
139 const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000140 LinearizeOption linearize_option,
141 JavaScriptOption javascript_option) {
Wei Li091f7a02015-11-09 12:09:55 -0800142 std::string file_path;
143 if (!PathService::GetTestFilePath(filename, &file_path))
144 return false;
Tom Sepez0784c732018-04-23 18:02:57 +0000145
Wei Li091f7a02015-11-09 12:09:55 -0800146 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400147 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800148 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800149
thestig29ce9232016-06-22 07:03:23 -0700150 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800151 loader_ = new TestLoader(file_contents_.get(), file_length_);
Lei Zhang0729be22018-02-05 21:13:51 +0000152
153 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepez96d13342015-01-16 14:59:26 -0800154 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700155 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800156 file_access_.m_Param = loader_;
Lei Zhang0729be22018-02-05 21:13:51 +0000157
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300158 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
Tom Sepez0784c732018-04-23 18:02:57 +0000159 return OpenDocumentHelper(password, linearize_option, javascript_option,
160 fake_file_access_.get(), &document_, &avail_,
161 &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400162}
Tom Sepez96d13342015-01-16 14:59:26 -0800163
Nicolas Pena56fc9722017-07-13 16:31:34 -0400164bool EmbedderTest::OpenDocumentHelper(const char* password,
Tom Sepez0784c732018-04-23 18:02:57 +0000165 LinearizeOption linearize_option,
166 JavaScriptOption javascript_option,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300167 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400168 FPDF_DOCUMENT* document,
169 FPDF_AVAIL* avail,
170 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300171 network_simulator->AddSegment(0, 1024);
172 network_simulator->SetRequestedDataAvailable();
173 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
174 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400175 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300176 int32_t nRet = PDF_DATA_NOTAVAIL;
177 while (nRet == PDF_DATA_NOTAVAIL) {
178 network_simulator->SetRequestedDataAvailable();
179 nRet =
180 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
181 }
182 if (nRet == PDF_DATA_ERROR)
183 return false;
184
Nicolas Pena56fc9722017-07-13 16:31:34 -0400185 *document = FPDFAvail_GetDocument(*avail, password);
186 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800187 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400188
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300189 nRet = PDF_DATA_NOTAVAIL;
190 while (nRet == PDF_DATA_NOTAVAIL) {
191 network_simulator->SetRequestedDataAvailable();
192 nRet =
193 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
194 }
195 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800196 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400197
198 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800199 for (int i = 0; i < page_count; ++i) {
200 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300201 while (nRet == PDF_DATA_NOTAVAIL) {
202 network_simulator->SetRequestedDataAvailable();
203 nRet = FPDFAvail_IsPageAvail(*avail, i,
204 network_simulator->GetDownloadHints());
205 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400206
207 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800208 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800209 }
210 } else {
Tom Sepez0784c732018-04-23 18:02:57 +0000211 if (linearize_option == LinearizeOption::kMustLinearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800212 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300213 network_simulator->SetWholeFileAvailable();
214 *document =
215 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400216 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800217 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800218 }
Tom Sepez0784c732018-04-23 18:02:57 +0000219 *form_handle = SetupFormFillEnvironment(*document, javascript_option);
220
Tom Sepezc46d0002015-11-30 15:46:36 -0800221#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400222 int doc_type = FPDF_GetFormType(*document);
223 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND)
224 FPDF_LoadXFA(*document);
Tom Sepezc46d0002015-11-30 15:46:36 -0800225#endif // PDF_ENABLE_XFA
Tom Sepez0784c732018-04-23 18:02:57 +0000226
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000227 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800228 return true;
229}
Tom Sepez96d13342015-01-16 14:59:26 -0800230
Tom Sepez0784c732018-04-23 18:02:57 +0000231FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(
232 FPDF_DOCUMENT doc,
233 JavaScriptOption javascript_option) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800234 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400235 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200236 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800237 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400238 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800239
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800240 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
241 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800242#ifdef PDF_ENABLE_XFA
243 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800244#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800245 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800246#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700247 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
248 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700249 formfillinfo->FFI_GetPage = GetPageTrampoline;
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000250 formfillinfo->FFI_DoURIAction = DoURIActionTrampoline;
251
Tom Sepez0784c732018-04-23 18:02:57 +0000252 if (javascript_option == JavaScriptOption::kEnableJavaScript)
253 formfillinfo->m_pJsPlatform = platform;
254
Nicolas Pena3ff54002017-07-05 11:55:35 -0400255 FPDF_FORMHANDLE form_handle =
256 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000257 FPDF_SetFormFieldHighlightColor(form_handle, FPDF_FORMFIELD_UNKNOWN,
258 0xFFE4DD);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400259 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
260 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800261}
262
Tom Sepezda8189e2015-01-30 14:41:50 -0800263void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400264 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800265 FORM_DoDocumentJSAction(form_handle_);
266 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800267}
268
269int EmbedderTest::GetFirstPageNum() {
270 int first_page = FPDFAvail_GetFirstPageNum(document_);
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000271 (void)FPDFAvail_IsPageAvail(avail_, first_page,
272 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800273 return first_page;
274}
275
276int EmbedderTest::GetPageCount() {
277 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400278 for (int i = 0; i < page_count; ++i)
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000279 (void)FPDFAvail_IsPageAvail(avail_, i,
280 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800281 return page_count;
282}
283
Tom Sepezda8189e2015-01-30 14:41:50 -0800284FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Tom Sepez507d0192018-11-07 16:37:51 +0000285 return LoadPageCommon(page_number, true);
286}
287
288FPDF_PAGE EmbedderTest::LoadPageNoEvents(int page_number) {
289 return LoadPageCommon(page_number, false);
290}
291
292FPDF_PAGE EmbedderTest::LoadPageCommon(int page_number, bool do_events) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400293 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000294 ASSERT(page_number >= 0);
295 ASSERT(!pdfium::ContainsKey(page_map_, page_number));
weili0dadcc62016-08-23 21:10:57 -0700296
Tom Sepez96d13342015-01-16 14:59:26 -0800297 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400298 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800299 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400300
Tom Sepez507d0192018-11-07 16:37:51 +0000301 if (do_events) {
302 FORM_OnAfterLoadPage(page, form_handle_);
303 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
304 }
weili0dadcc62016-08-23 21:10:57 -0700305 page_map_[page_number] = page;
Tom Sepez396e8722015-09-09 10:16:08 -0700306 return page;
307}
308
Tom Sepezda8189e2015-01-30 14:41:50 -0800309void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Tom Sepez507d0192018-11-07 16:37:51 +0000310 UnloadPageCommon(page, true);
311}
Lei Zhang75c81712018-02-08 17:22:39 +0000312
Tom Sepez507d0192018-11-07 16:37:51 +0000313void EmbedderTest::UnloadPageNoEvents(FPDF_PAGE page) {
314 UnloadPageCommon(page, false);
315}
316
317void EmbedderTest::UnloadPageCommon(FPDF_PAGE page, bool do_events) {
318 ASSERT(form_handle_);
Lei Zhang75c81712018-02-08 17:22:39 +0000319 int page_number = GetPageNumberForLoadedPage(page);
320 if (page_number < 0) {
321 NOTREACHED();
322 return;
323 }
Tom Sepez507d0192018-11-07 16:37:51 +0000324 if (do_events) {
325 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
326 FORM_OnBeforeClosePage(page, form_handle_);
327 }
Tom Sepez96d13342015-01-16 14:59:26 -0800328 FPDF_ClosePage(page);
Lei Zhang75c81712018-02-08 17:22:39 +0000329 page_map_.erase(page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800330}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800331
Tom Sepeze08d2b12018-04-25 18:49:32 +0000332ScopedFPDFBitmap EmbedderTest::RenderLoadedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000333 return RenderLoadedPageWithFlags(page, 0);
334}
335
Tom Sepeze08d2b12018-04-25 18:49:32 +0000336ScopedFPDFBitmap EmbedderTest::RenderLoadedPageWithFlags(FPDF_PAGE page,
337 int flags) {
Lei Zhang75c81712018-02-08 17:22:39 +0000338 if (GetPageNumberForLoadedPage(page) < 0) {
339 NOTREACHED();
340 return nullptr;
341 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000342 return RenderPageWithFlags(page, form_handle_, flags);
343}
344
Tom Sepeze08d2b12018-04-25 18:49:32 +0000345ScopedFPDFBitmap EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000346 return RenderSavedPageWithFlags(page, 0);
347}
348
Tom Sepeze08d2b12018-04-25 18:49:32 +0000349ScopedFPDFBitmap EmbedderTest::RenderSavedPageWithFlags(FPDF_PAGE page,
350 int flags) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000351 if (GetPageNumberForSavedPage(page) < 0) {
352 NOTREACHED();
353 return nullptr;
354 }
Lei Zhanga98e3662018-02-07 20:28:35 +0000355 return RenderPageWithFlags(page, saved_form_handle_, flags);
356}
357
358// static
Tom Sepeze08d2b12018-04-25 18:49:32 +0000359ScopedFPDFBitmap EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
360 FPDF_FORMHANDLE handle,
361 int flags) {
Lei Zhanga98e3662018-02-07 20:28:35 +0000362 int width = static_cast<int>(FPDF_GetPageWidth(page));
363 int height = static_cast<int>(FPDF_GetPageHeight(page));
364 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000365 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Lei Zhanga98e3662018-02-07 20:28:35 +0000366 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
367 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
368 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
369 FPDF_FFLDraw(handle, bitmap.get(), page, 0, 0, width, height, 0, flags);
370 return bitmap;
371}
372
Lei Zhang30ff2532019-01-31 21:37:55 +0000373// static
374ScopedFPDFBitmap EmbedderTest::RenderPage(FPDF_PAGE page) {
375 return RenderPageWithFlags(page, nullptr, 0);
376}
377
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400378FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300379 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
Lei Zhang0729be22018-02-05 21:13:51 +0000380 saved_file_access_.m_FileLen = data_string_.size();
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300381 saved_file_access_.m_GetBlock = GetBlockFromString;
Artem Strygin68d04f22018-07-12 09:18:19 +0000382 // Copy data to prevent clearing it before saved document close.
383 saved_document_file_data_ = data_string_;
384 saved_file_access_.m_Param = &saved_document_file_data_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400385
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300386 saved_fake_file_access_ =
387 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
388
Tom Sepez0784c732018-04-23 18:02:57 +0000389 EXPECT_TRUE(OpenDocumentHelper(
390 password, LinearizeOption::kDefaultLinearize,
391 JavaScriptOption::kEnableJavaScript, saved_fake_file_access_.get(),
392 &saved_document_, &saved_avail_, &saved_form_handle_));
Lei Zhang0729be22018-02-05 21:13:51 +0000393 return saved_document_;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400394}
395
396void EmbedderTest::CloseSavedDocument() {
Lei Zhang0729be22018-02-05 21:13:51 +0000397 ASSERT(saved_document_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400398
Lei Zhang0729be22018-02-05 21:13:51 +0000399 FPDFDOC_ExitFormFillEnvironment(saved_form_handle_);
400 FPDF_CloseDocument(saved_document_);
401 FPDFAvail_Destroy(saved_avail_);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400402
Lei Zhang0729be22018-02-05 21:13:51 +0000403 saved_form_handle_ = nullptr;
404 saved_document_ = nullptr;
405 saved_avail_ = nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400406}
407
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000408FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000409 ASSERT(saved_form_handle_);
410 ASSERT(page_number >= 0);
411 ASSERT(!pdfium::ContainsKey(saved_page_map_, page_number));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400412
Lei Zhang0729be22018-02-05 21:13:51 +0000413 FPDF_PAGE page = FPDF_LoadPage(saved_document_, page_number);
Lei Zhang9f72c452018-02-08 21:49:54 +0000414 if (!page)
415 return nullptr;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400416
Lei Zhang9f72c452018-02-08 21:49:54 +0000417 FORM_OnAfterLoadPage(page, saved_form_handle_);
418 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_OPEN);
419 saved_page_map_[page_number] = page;
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000420 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400421}
422
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000423void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
Lei Zhang9f72c452018-02-08 21:49:54 +0000424 ASSERT(saved_form_handle_);
425
426 int page_number = GetPageNumberForSavedPage(page);
427 if (page_number < 0) {
428 NOTREACHED();
429 return;
430 }
431
432 FORM_DoPageAAction(page, saved_form_handle_, FPDFPAGE_AACTION_CLOSE);
433 FORM_OnBeforeClosePage(page, saved_form_handle_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000434 FPDF_ClosePage(page);
Lei Zhang9f72c452018-02-08 21:49:54 +0000435
436 saved_page_map_.erase(page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400437}
438
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000439void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
440 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400441 int height,
442 const char* md5) {
Lei Zhang0729be22018-02-05 21:13:51 +0000443 ASSERT(saved_document_);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000444 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400445
Tom Sepeze08d2b12018-04-25 18:49:32 +0000446 ScopedFPDFBitmap bitmap = RenderSavedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000447 CompareBitmap(bitmap.get(), width, height, md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400448}
449
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400450void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
Tom Sepezb9c3e272018-08-14 18:22:06 +0000451 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000452 FPDF_PAGE page = LoadSavedPage(0);
453 VerifySavedRendering(page, width, height, md5);
454 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400455 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400456}
457
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300458void EmbedderTest::SetWholeFileAvailable() {
459 ASSERT(fake_file_access_);
460 fake_file_access_->SetWholeFileAvailable();
461}
462
weili0dadcc62016-08-23 21:10:57 -0700463FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700464 FPDF_DOCUMENT document,
465 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700466 EmbedderTest* test = static_cast<EmbedderTest*>(info);
467 auto it = test->page_map_.find(page_index);
468 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700469}
470
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800471// static
472void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
473 int type) {
474 EmbedderTest* test = static_cast<EmbedderTest*>(info);
475 test->delegate_->UnsupportedHandler(type);
476}
477
478// static
479int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
480 FPDF_WIDESTRING message,
481 FPDF_WIDESTRING title,
482 int type,
483 int icon) {
484 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
485 return test->delegate_->Alert(message, title, type, icon);
486}
487
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700488// static
489int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 int msecs,
491 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700492 EmbedderTest* test = static_cast<EmbedderTest*>(info);
493 return test->delegate_->SetTimer(msecs, fn);
494}
495
496// static
497void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
498 EmbedderTest* test = static_cast<EmbedderTest*>(info);
499 return test->delegate_->KillTimer(id);
500}
501
Tom Sepez396e8722015-09-09 10:16:08 -0700502// static
503FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
504 FPDF_DOCUMENT document,
505 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700506 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
507 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700508}
509
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000510// static
Henrique Nakashima9ef93d02018-10-03 18:41:03 +0000511void EmbedderTest::DoURIActionTrampoline(FPDF_FORMFILLINFO* info,
512 FPDF_BYTESTRING uri) {
513 EmbedderTest* test = static_cast<EmbedderTest*>(info);
514 return test->delegate_->DoURIAction(uri);
515}
516
517// static
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000518std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400519 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000520 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
521 FPDFBitmap_GetWidth(bitmap) *
522 GetBitmapBytesPerPixel(bitmap) *
523 FPDFBitmap_GetHeight(bitmap),
524 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400525 return CryptToBase16(digest);
526}
527
Henrique Nakashimadb269572018-01-16 19:02:15 +0000528#ifndef NDEBUG
529// static
530void EmbedderTest::WriteBitmapToPng(FPDF_BITMAP bitmap,
531 const std::string& filename) {
Henrique Nakashimaf956bad2018-08-16 16:41:42 +0000532 BitmapSaver::WriteBitmapToPng(bitmap, filename);
Henrique Nakashimadb269572018-01-16 19:02:15 +0000533}
534#endif
535
thestigbcd3e532016-11-21 13:37:28 -0800536// static
537void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
538 int expected_width,
539 int expected_height,
540 const char* expected_md5sum) {
541 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
542 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400543
544 // The expected stride is calculated using the same formula as in
545 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
546 const int expected_stride =
547 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800548 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
549
550 if (!expected_md5sum)
551 return;
552
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000553 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800554}
555
Nicolas Pena3ff54002017-07-05 11:55:35 -0400556// static
557int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
558 const void* data,
559 unsigned long size) {
560 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000561
Lei Zhang0729be22018-02-05 21:13:51 +0000562 pThis->data_string_.append(static_cast<const char*>(data), size);
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000563
564 if (pThis->filestream_.is_open())
565 pThis->filestream_.write(static_cast<const char*>(data), size);
566
Nicolas Pena3ff54002017-07-05 11:55:35 -0400567 return 1;
568}
569
570// static
571int EmbedderTest::GetBlockFromString(void* param,
572 unsigned long pos,
573 unsigned char* buf,
574 unsigned long size) {
575 std::string* new_file = static_cast<std::string*>(param);
Lei Zhangcef91f12019-01-08 21:32:51 +0000576 if (!new_file || pos + size < pos) {
577 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400578 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000579 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400580
Lei Zhangcef91f12019-01-08 21:32:51 +0000581 if (pos + size > new_file->size()) {
582 NOTREACHED();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400583 return 0;
Lei Zhangcef91f12019-01-08 21:32:51 +0000584 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400585
586 memcpy(buf, new_file->data() + pos, size);
587 return 1;
588}
Lei Zhang75c81712018-02-08 17:22:39 +0000589
Lei Zhang9f72c452018-02-08 21:49:54 +0000590// static
591int EmbedderTest::GetPageNumberForPage(const PageNumberToHandleMap& page_map,
592 FPDF_PAGE page) {
593 for (const auto& it : page_map) {
Lei Zhang75c81712018-02-08 17:22:39 +0000594 if (it.second == page) {
595 int page_number = it.first;
596 ASSERT(page_number >= 0);
597 return page_number;
598 }
599 }
600 return -1;
601}
Lei Zhang9f72c452018-02-08 21:49:54 +0000602
603int EmbedderTest::GetPageNumberForLoadedPage(FPDF_PAGE page) const {
604 return GetPageNumberForPage(page_map_, page);
605}
606
607int EmbedderTest::GetPageNumberForSavedPage(FPDF_PAGE page) const {
608 return GetPageNumberForPage(saved_page_map_, page);
609}
Henrique Nakashima7c2e8a32018-06-06 19:17:34 +0000610
611void EmbedderTest::OpenPDFFileForWrite(const char* filename) {
612 filestream_.open(filename, std::ios_base::binary);
613}
614
615void EmbedderTest::ClosePDFFileForWrite() {
616 filestream_.close();
617}