blob: ccc98b4dcd8af177cd1aa0f6e3d061d798e11077 [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>
10#include <string>
11#include <utility>
12#include <vector>
13
thestigbcd3e532016-11-21 13:37:28 -080014#include "core/fdrm/crypto/fx_crypt.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080015#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080016#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080017#include "public/fpdf_text.h"
18#include "public/fpdfview.h"
Tom Sepeza310e002015-02-27 13:03:07 -080019#include "testing/gmock/include/gmock/gmock.h"
Wei Li091f7a02015-11-09 12:09:55 -080020#include "testing/test_support.h"
21#include "testing/utils/path_service.h"
Artem Strygin0e60b9e2017-09-28 18:46:03 +030022#include "third_party/base/ptr_util.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070023
24#ifdef PDF_ENABLE_V8
Lei Zhang8241df72015-11-06 14:38:48 -080025#include "v8/include/v8-platform.h"
Dan Sinclair61046b92016-02-18 14:48:48 -050026#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070027#endif // PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080028
Tom Sepez96d13342015-01-16 14:59:26 -080029namespace {
thestigc08cd7a2016-06-27 09:47:59 -070030
31const char* g_exe_path = nullptr;
32
33#ifdef PDF_ENABLE_V8
34#ifdef V8_USE_EXTERNAL_STARTUP_DATA
35v8::StartupData* g_v8_natives = nullptr;
36v8::StartupData* g_v8_snapshot = nullptr;
37#endif // V8_USE_EXTERNAL_STARTUP_DATA
38#endif // PDF_ENABLE_V8
39
Jane Liu28fb7ba2017-08-02 21:45:57 -040040int GetBitmapBytesPerPixel(FPDF_BITMAP bitmap) {
41 const int format = FPDFBitmap_GetFormat(bitmap);
42 switch (format) {
43 case FPDFBitmap_Gray:
44 return 1;
45 case FPDFBitmap_BGR:
46 return 3;
47 case FPDFBitmap_BGRx:
48 case FPDFBitmap_BGRA:
49 return 4;
50 default:
51 ASSERT(false);
52 return 0;
53 }
54}
55
thestigbcd3e532016-11-21 13:37:28 -080056} // namespace
57
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058EmbedderTest::EmbedderTest()
Tom Sepeza72e8e22015-10-07 10:17:53 -070059 : default_delegate_(new EmbedderTest::Delegate()),
60 document_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080061 form_handle_(nullptr),
62 avail_(nullptr),
Tom Sepeza72e8e22015-10-07 10:17:53 -070063 external_isolate_(nullptr),
Tom Sepez4cb0fa72015-02-25 16:08:18 -080064 loader_(nullptr),
65 file_length_(0),
66 file_contents_(nullptr) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -080067 memset(&file_access_, 0, sizeof(file_access_));
Tom Sepeza72e8e22015-10-07 10:17:53 -070068 delegate_ = default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080069
Tom Sepez452b4f32015-10-13 09:27:27 -070070#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080071#ifdef V8_USE_EXTERNAL_STARTUP_DATA
thestigc08cd7a2016-06-27 09:47:59 -070072 if (g_v8_natives && g_v8_snapshot) {
73 InitializeV8ForPDFium(g_exe_path, std::string(), nullptr, nullptr,
74 &platform_);
75 } else {
76 g_v8_natives = new v8::StartupData;
77 g_v8_snapshot = new v8::StartupData;
78 InitializeV8ForPDFium(g_exe_path, std::string(), g_v8_natives,
79 g_v8_snapshot, &platform_);
80 }
Tom Sepezd831dc72015-10-19 16:04:22 -070081#else
thestigc08cd7a2016-06-27 09:47:59 -070082 InitializeV8ForPDFium(g_exe_path, &platform_);
Tom Sepez96d13342015-01-16 14:59:26 -080083#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -070084#endif // FPDF_ENABLE_V8
Nicolas Pena3ff54002017-07-05 11:55:35 -040085 FPDF_FILEWRITE::version = 1;
86 FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
Tom Sepezf288bb12015-11-20 12:12:46 -080087}
Tom Sepez96d13342015-01-16 14:59:26 -080088
jochen38a1f0a2016-05-31 12:07:40 -070089EmbedderTest::~EmbedderTest() {
90#ifdef PDF_ENABLE_V8
91 v8::V8::ShutdownPlatform();
92 delete platform_;
93#endif // PDF_ENABLE_V8
94}
Tom Sepezf288bb12015-11-20 12:12:46 -080095
96void EmbedderTest::SetUp() {
Tom Sepeza72e8e22015-10-07 10:17:53 -070097 FPDF_LIBRARY_CONFIG config;
98 config.version = 2;
99 config.m_pUserFontPaths = nullptr;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700100 config.m_v8EmbedderSlot = 0;
Tom Sepez452b4f32015-10-13 09:27:27 -0700101 config.m_pIsolate = external_isolate_;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700102 FPDF_InitLibraryWithConfig(&config);
Tom Sepez96d13342015-01-16 14:59:26 -0800103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 UNSUPPORT_INFO* info = static_cast<UNSUPPORT_INFO*>(this);
105 memset(info, 0, sizeof(UNSUPPORT_INFO));
106 info->version = 1;
107 info->FSDK_UnSupport_Handler = UnsupportedHandlerTrampoline;
108 FSDK_SetUnSpObjProcessHandler(info);
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000109
110 m_SavedDocument = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111}
Tom Sepez96d13342015-01-16 14:59:26 -0800112
113void EmbedderTest::TearDown() {
Tom Sepezda8189e2015-01-30 14:41:50 -0800114 if (document_) {
Lei Zhangd27acae2015-05-15 15:36:02 -0700115 FORM_DoDocumentAAction(form_handle_, FPDFDOC_AACTION_WC);
Tom Sepezc46d0002015-11-30 15:46:36 -0800116 FPDFDOC_ExitFormFillEnvironment(form_handle_);
117 FPDF_CloseDocument(document_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800118 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800119
Tom Sepez96d13342015-01-16 14:59:26 -0800120 FPDFAvail_Destroy(avail_);
121 FPDF_DestroyLibrary();
Lei Zhangd27acae2015-05-15 15:36:02 -0700122 delete loader_;
Tom Sepez96d13342015-01-16 14:59:26 -0800123}
124
Tom Sepezd483eb42016-01-06 10:03:59 -0800125bool EmbedderTest::CreateEmptyDocument() {
126 document_ = FPDF_CreateNewDocument();
127 if (!document_)
128 return false;
129
Nicolas Pena3ff54002017-07-05 11:55:35 -0400130 form_handle_ = SetupFormFillEnvironment(document_);
Tom Sepezd483eb42016-01-06 10:03:59 -0800131 return true;
132}
133
Lei Zhang208eecf2017-12-20 19:40:50 +0000134bool EmbedderTest::OpenDocument(const std::string& filename) {
135 return OpenDocumentWithOptions(filename, nullptr, false);
136}
137
138bool EmbedderTest::OpenDocumentLinearized(const std::string& filename) {
139 return OpenDocumentWithOptions(filename, nullptr, true);
140}
141
142bool EmbedderTest::OpenDocumentWithPassword(const std::string& filename,
143 const char* password) {
144 return OpenDocumentWithOptions(filename, password, false);
145}
146
147bool EmbedderTest::OpenDocumentWithOptions(const std::string& filename,
148 const char* password,
149 bool must_linearize) {
Wei Li091f7a02015-11-09 12:09:55 -0800150 std::string file_path;
151 if (!PathService::GetTestFilePath(filename, &file_path))
152 return false;
153 file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
Dan Sinclair6be2aab2015-10-28 13:58:49 -0400154 if (!file_contents_)
Tom Sepez96d13342015-01-16 14:59:26 -0800155 return false;
Tom Sepez96d13342015-01-16 14:59:26 -0800156
thestig29ce9232016-06-22 07:03:23 -0700157 EXPECT_TRUE(!loader_);
Tom Sepez0aa35312016-01-06 10:16:32 -0800158 loader_ = new TestLoader(file_contents_.get(), file_length_);
Tom Sepez96d13342015-01-16 14:59:26 -0800159 file_access_.m_FileLen = static_cast<unsigned long>(file_length_);
Tom Sepezd831dc72015-10-19 16:04:22 -0700160 file_access_.m_GetBlock = TestLoader::GetBlock;
Tom Sepez96d13342015-01-16 14:59:26 -0800161 file_access_.m_Param = loader_;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300162 fake_file_access_ = pdfium::MakeUnique<FakeFileAccess>(&file_access_);
163 return OpenDocumentHelper(password, must_linearize, fake_file_access_.get(),
164 &document_, &avail_, &form_handle_);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400165}
Tom Sepez96d13342015-01-16 14:59:26 -0800166
Nicolas Pena56fc9722017-07-13 16:31:34 -0400167bool EmbedderTest::OpenDocumentHelper(const char* password,
168 bool must_linearize,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300169 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400170 FPDF_DOCUMENT* document,
171 FPDF_AVAIL* avail,
172 FPDF_FORMHANDLE* form_handle) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300173 network_simulator->AddSegment(0, 1024);
174 network_simulator->SetRequestedDataAvailable();
175 *avail = FPDFAvail_Create(network_simulator->GetFileAvail(),
176 network_simulator->GetFileAccess());
Nicolas Pena56fc9722017-07-13 16:31:34 -0400177 if (FPDFAvail_IsLinearized(*avail) == PDF_LINEARIZED) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300178 int32_t nRet = PDF_DATA_NOTAVAIL;
179 while (nRet == PDF_DATA_NOTAVAIL) {
180 network_simulator->SetRequestedDataAvailable();
181 nRet =
182 FPDFAvail_IsDocAvail(*avail, network_simulator->GetDownloadHints());
183 }
184 if (nRet == PDF_DATA_ERROR)
185 return false;
186
Nicolas Pena56fc9722017-07-13 16:31:34 -0400187 *document = FPDFAvail_GetDocument(*avail, password);
188 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800189 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400190
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300191 nRet = PDF_DATA_NOTAVAIL;
192 while (nRet == PDF_DATA_NOTAVAIL) {
193 network_simulator->SetRequestedDataAvailable();
194 nRet =
195 FPDFAvail_IsFormAvail(*avail, network_simulator->GetDownloadHints());
196 }
197 if (nRet == PDF_FORM_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800198 return false;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400199
200 int page_count = FPDF_GetPageCount(*document);
Jun Fangdf7f3662015-11-10 18:29:18 +0800201 for (int i = 0; i < page_count; ++i) {
202 nRet = PDF_DATA_NOTAVAIL;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300203 while (nRet == PDF_DATA_NOTAVAIL) {
204 network_simulator->SetRequestedDataAvailable();
205 nRet = FPDFAvail_IsPageAvail(*avail, i,
206 network_simulator->GetDownloadHints());
207 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400208
209 if (nRet == PDF_DATA_ERROR)
Jun Fangdf7f3662015-11-10 18:29:18 +0800210 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800211 }
212 } else {
Nicolas Pena56fc9722017-07-13 16:31:34 -0400213 if (must_linearize)
Jun Fangdf7f3662015-11-10 18:29:18 +0800214 return false;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300215 network_simulator->SetWholeFileAvailable();
216 *document =
217 FPDF_LoadCustomDocument(network_simulator->GetFileAccess(), password);
Nicolas Pena56fc9722017-07-13 16:31:34 -0400218 if (!*document)
Jun Fangdf7f3662015-11-10 18:29:18 +0800219 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800220 }
Nicolas Pena56fc9722017-07-13 16:31:34 -0400221 *form_handle = SetupFormFillEnvironment(*document);
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
Nicolas Pena56fc9722017-07-13 16:31:34 -0400227 (void)FPDF_GetDocPermissions(*document);
Tom Sepezd483eb42016-01-06 10:03:59 -0800228 return true;
229}
Tom Sepez96d13342015-01-16 14:59:26 -0800230
Nicolas Pena3ff54002017-07-05 11:55:35 -0400231FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(FPDF_DOCUMENT doc) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800232 IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400233 memset(platform, '\0', sizeof(IPDF_JSPLATFORM));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200234 platform->version = 2;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800235 platform->app_alert = AlertTrampoline;
Dan Sinclair14aacd52017-05-18 14:11:29 -0400236 platform->m_isolate = external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800237
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800238 FPDF_FORMFILLINFO* formfillinfo = static_cast<FPDF_FORMFILLINFO*>(this);
239 memset(formfillinfo, 0, sizeof(FPDF_FORMFILLINFO));
Lei Zhangcd396952015-11-04 20:26:50 -0800240#ifdef PDF_ENABLE_XFA
241 formfillinfo->version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800242#else // PDF_ENABLE_XFA
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800243 formfillinfo->version = 1;
Tom Sepezc46d0002015-11-30 15:46:36 -0800244#endif // PDF_ENABLE_XFA
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700245 formfillinfo->FFI_SetTimer = SetTimerTrampoline;
246 formfillinfo->FFI_KillTimer = KillTimerTrampoline;
Tom Sepez396e8722015-09-09 10:16:08 -0700247 formfillinfo->FFI_GetPage = GetPageTrampoline;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800248 formfillinfo->m_pJsPlatform = platform;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400249 FPDF_FORMHANDLE form_handle =
250 FPDFDOC_InitFormFillEnvironment(doc, formfillinfo);
251 FPDF_SetFormFieldHighlightColor(form_handle, 0, 0xFFE4DD);
252 FPDF_SetFormFieldHighlightAlpha(form_handle, 100);
253 return form_handle;
Tom Sepez96d13342015-01-16 14:59:26 -0800254}
255
Tom Sepezda8189e2015-01-30 14:41:50 -0800256void EmbedderTest::DoOpenActions() {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400257 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800258 FORM_DoDocumentJSAction(form_handle_);
259 FORM_DoDocumentOpenAction(form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800260}
261
262int EmbedderTest::GetFirstPageNum() {
263 int first_page = FPDFAvail_GetFirstPageNum(document_);
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300264 (void)FPDFAvail_IsPageAvail(avail_, first_page,
265 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800266 return first_page;
267}
268
269int EmbedderTest::GetPageCount() {
270 int page_count = FPDF_GetPageCount(document_);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400271 for (int i = 0; i < page_count; ++i)
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300272 (void)FPDFAvail_IsPageAvail(avail_, i,
273 fake_file_access_->GetDownloadHints());
Tom Sepez96d13342015-01-16 14:59:26 -0800274 return page_count;
275}
276
Tom Sepezda8189e2015-01-30 14:41:50 -0800277FPDF_PAGE EmbedderTest::LoadPage(int page_number) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400278 ASSERT(form_handle_);
weili0dadcc62016-08-23 21:10:57 -0700279 // First check whether it is loaded already.
280 auto it = page_map_.find(page_number);
281 if (it != page_map_.end())
282 return it->second;
283
Tom Sepez96d13342015-01-16 14:59:26 -0800284 FPDF_PAGE page = FPDF_LoadPage(document_, page_number);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400285 if (!page)
Tom Sepez96d13342015-01-16 14:59:26 -0800286 return nullptr;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400287
Tom Sepezda8189e2015-01-30 14:41:50 -0800288 FORM_OnAfterLoadPage(page, form_handle_);
289 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN);
weili0dadcc62016-08-23 21:10:57 -0700290 // Cache the page.
291 page_map_[page_number] = page;
dsinclaircb92dc72016-09-07 09:02:48 -0700292 page_reverse_map_[page] = page_number;
Tom Sepez396e8722015-09-09 10:16:08 -0700293 return page;
294}
295
Tom Sepezda8189e2015-01-30 14:41:50 -0800296FPDF_BITMAP EmbedderTest::RenderPage(FPDF_PAGE page) {
Jane Liubaa7ff42017-06-29 19:18:23 -0400297 return RenderPageWithFlags(page, form_handle_, 0);
Jane Liuc533f4b2017-06-19 11:13:00 -0400298}
299
Jane Liubaa7ff42017-06-29 19:18:23 -0400300FPDF_BITMAP EmbedderTest::RenderPageWithFlags(FPDF_PAGE page,
301 FPDF_FORMHANDLE handle,
302 int flags) {
Tom Sepez96d13342015-01-16 14:59:26 -0800303 int width = static_cast<int>(FPDF_GetPageWidth(page));
304 int height = static_cast<int>(FPDF_GetPageHeight(page));
Lei Zhang453d96b2015-12-31 13:13:10 -0800305 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
306 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
307 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
308 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
Jane Liuc533f4b2017-06-19 11:13:00 -0400309 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, flags);
Jane Liubaa7ff42017-06-29 19:18:23 -0400310 FPDF_FFLDraw(handle, bitmap, page, 0, 0, width, height, 0, flags);
Tom Sepez96d13342015-01-16 14:59:26 -0800311 return bitmap;
312}
313
Tom Sepezda8189e2015-01-30 14:41:50 -0800314void EmbedderTest::UnloadPage(FPDF_PAGE page) {
Nicolas Pena3ff54002017-07-05 11:55:35 -0400315 ASSERT(form_handle_);
Tom Sepezda8189e2015-01-30 14:41:50 -0800316 FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE);
317 FORM_OnBeforeClosePage(page, form_handle_);
Tom Sepez96d13342015-01-16 14:59:26 -0800318 FPDF_ClosePage(page);
dsinclaircb92dc72016-09-07 09:02:48 -0700319
320 auto it = page_reverse_map_.find(page);
321 if (it == page_reverse_map_.end())
322 return;
323
324 page_map_.erase(it->second);
325 page_reverse_map_.erase(it);
Tom Sepez96d13342015-01-16 14:59:26 -0800326}
Tom Sepez1b1bb492015-01-22 17:36:32 -0800327
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400328FPDF_DOCUMENT EmbedderTest::OpenSavedDocument(const char* password) {
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300329 memset(&saved_file_access_, 0, sizeof(saved_file_access_));
330 saved_file_access_.m_FileLen = m_String.size();
331 saved_file_access_.m_GetBlock = GetBlockFromString;
332 saved_file_access_.m_Param = &m_String;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400333
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300334 saved_fake_file_access_ =
335 pdfium::MakeUnique<FakeFileAccess>(&saved_file_access_);
336
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400337 EXPECT_TRUE(OpenDocumentHelper(password, false, saved_fake_file_access_.get(),
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300338 &m_SavedDocument, &m_SavedAvail,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400339 &m_SavedForm));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400340 return m_SavedDocument;
341}
342
343void EmbedderTest::CloseSavedDocument() {
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400344 ASSERT(m_SavedDocument);
345
346 FPDFDOC_ExitFormFillEnvironment(m_SavedForm);
347 FPDF_CloseDocument(m_SavedDocument);
348 FPDFAvail_Destroy(m_SavedAvail);
349
350 m_SavedForm = nullptr;
351 m_SavedDocument = nullptr;
352 m_SavedAvail = nullptr;
353}
354
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000355FPDF_PAGE EmbedderTest::LoadSavedPage(int page_number) {
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400356 ASSERT(m_SavedDocument);
357
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000358 EXPECT_LT(page_number, FPDF_GetPageCount(m_SavedDocument));
359 FPDF_PAGE page = FPDF_LoadPage(m_SavedDocument, page_number);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400360
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000361 ASSERT(page);
362 return page;
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400363}
364
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000365FPDF_BITMAP EmbedderTest::RenderSavedPage(FPDF_PAGE page) {
366 return RenderPageWithFlags(page, m_SavedForm, 0);
367}
368
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000369void EmbedderTest::CloseSavedPage(FPDF_PAGE page) {
370 ASSERT(page);
371 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400372}
373
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000374void EmbedderTest::VerifySavedRendering(FPDF_PAGE page,
375 int width,
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400376 int height,
377 const char* md5) {
378 ASSERT(m_SavedDocument);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000379 ASSERT(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400380
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000381 FPDF_BITMAP new_bitmap = RenderPageWithFlags(page, m_SavedForm, FPDF_ANNOT);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400382 CompareBitmap(new_bitmap, width, height, md5);
383 FPDFBitmap_Destroy(new_bitmap);
384}
385
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400386void EmbedderTest::VerifySavedDocument(int width, int height, const char* md5) {
387 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000388 FPDF_PAGE page = LoadSavedPage(0);
389 VerifySavedRendering(page, width, height, md5);
390 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400391 CloseSavedDocument();
Nicolas Pena3ff54002017-07-05 11:55:35 -0400392}
393
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300394void EmbedderTest::SetWholeFileAvailable() {
395 ASSERT(fake_file_access_);
396 fake_file_access_->SetWholeFileAvailable();
397}
398
weili0dadcc62016-08-23 21:10:57 -0700399FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -0700400 FPDF_DOCUMENT document,
401 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700402 EmbedderTest* test = static_cast<EmbedderTest*>(info);
403 auto it = test->page_map_.find(page_index);
404 return it != test->page_map_.end() ? it->second : nullptr;
Tom Sepez396e8722015-09-09 10:16:08 -0700405}
406
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800407// static
408void EmbedderTest::UnsupportedHandlerTrampoline(UNSUPPORT_INFO* info,
409 int type) {
410 EmbedderTest* test = static_cast<EmbedderTest*>(info);
411 test->delegate_->UnsupportedHandler(type);
412}
413
414// static
415int EmbedderTest::AlertTrampoline(IPDF_JSPLATFORM* platform,
416 FPDF_WIDESTRING message,
417 FPDF_WIDESTRING title,
418 int type,
419 int icon) {
420 EmbedderTest* test = static_cast<EmbedderTest*>(platform);
421 return test->delegate_->Alert(message, title, type, icon);
422}
423
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700424// static
425int EmbedderTest::SetTimerTrampoline(FPDF_FORMFILLINFO* info,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 int msecs,
427 TimerCallback fn) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700428 EmbedderTest* test = static_cast<EmbedderTest*>(info);
429 return test->delegate_->SetTimer(msecs, fn);
430}
431
432// static
433void EmbedderTest::KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id) {
434 EmbedderTest* test = static_cast<EmbedderTest*>(info);
435 return test->delegate_->KillTimer(id);
436}
437
Tom Sepez396e8722015-09-09 10:16:08 -0700438// static
439FPDF_PAGE EmbedderTest::GetPageTrampoline(FPDF_FORMFILLINFO* info,
440 FPDF_DOCUMENT document,
441 int page_index) {
weili0dadcc62016-08-23 21:10:57 -0700442 return static_cast<EmbedderTest*>(info)->delegate_->GetPage(info, document,
443 page_index);
Tom Sepez396e8722015-09-09 10:16:08 -0700444}
445
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000446// static
447std::string EmbedderTest::HashBitmap(FPDF_BITMAP bitmap) {
Dan Sinclair957480c2017-06-13 15:21:14 -0400448 uint8_t digest[16];
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000449 CRYPT_MD5Generate(static_cast<uint8_t*>(FPDFBitmap_GetBuffer(bitmap)),
450 FPDFBitmap_GetWidth(bitmap) *
451 GetBitmapBytesPerPixel(bitmap) *
452 FPDFBitmap_GetHeight(bitmap),
453 digest);
Dan Sinclair957480c2017-06-13 15:21:14 -0400454 return CryptToBase16(digest);
455}
456
thestigbcd3e532016-11-21 13:37:28 -0800457// static
458void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap,
459 int expected_width,
460 int expected_height,
461 const char* expected_md5sum) {
462 ASSERT_EQ(expected_width, FPDFBitmap_GetWidth(bitmap));
463 ASSERT_EQ(expected_height, FPDFBitmap_GetHeight(bitmap));
Jane Liu28fb7ba2017-08-02 21:45:57 -0400464
465 // The expected stride is calculated using the same formula as in
466 // CFX_DIBitmap::CalculatePitchAndSize(), which sets the bitmap stride.
467 const int expected_stride =
468 (expected_width * GetBitmapBytesPerPixel(bitmap) * 8 + 31) / 32 * 4;
thestigbcd3e532016-11-21 13:37:28 -0800469 ASSERT_EQ(expected_stride, FPDFBitmap_GetStride(bitmap));
470
471 if (!expected_md5sum)
472 return;
473
Henrique Nakashima9fa50362017-11-10 22:40:44 +0000474 EXPECT_EQ(expected_md5sum, HashBitmap(bitmap));
thestigbcd3e532016-11-21 13:37:28 -0800475}
476
Nicolas Pena3ff54002017-07-05 11:55:35 -0400477// static
478int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
479 const void* data,
480 unsigned long size) {
481 EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite);
482 pThis->m_String.append(static_cast<const char*>(data), size);
483 return 1;
484}
485
486// static
487int EmbedderTest::GetBlockFromString(void* param,
488 unsigned long pos,
489 unsigned char* buf,
490 unsigned long size) {
491 std::string* new_file = static_cast<std::string*>(param);
492 if (!new_file || pos + size < pos)
493 return 0;
494
495 unsigned long file_size = new_file->size();
496 if (pos + size > file_size)
497 return 0;
498
499 memcpy(buf, new_file->data() + pos, size);
500 return 1;
501}
502
Tom Sepez1b1bb492015-01-22 17:36:32 -0800503// Can't use gtest-provided main since we need to stash the path to the
504// executable in order to find the external V8 binary data files.
505int main(int argc, char** argv) {
thestigc08cd7a2016-06-27 09:47:59 -0700506 g_exe_path = argv[0];
Tom Sepez1b1bb492015-01-22 17:36:32 -0800507 testing::InitGoogleTest(&argc, argv);
Tom Sepeza310e002015-02-27 13:03:07 -0800508 testing::InitGoogleMock(&argc, argv);
thestigc08cd7a2016-06-27 09:47:59 -0700509 int ret_val = RUN_ALL_TESTS();
510
511#ifdef PDF_ENABLE_V8
512#ifdef V8_USE_EXTERNAL_STARTUP_DATA
513 if (g_v8_natives)
514 free(const_cast<char*>(g_v8_natives->data));
515 if (g_v8_snapshot)
516 free(const_cast<char*>(g_v8_snapshot->data));
517#endif // V8_USE_EXTERNAL_STARTUP_DATA
518#endif // PDF_ENABLE_V8
519
520 return ret_val;
Tom Sepez1b1bb492015-01-22 17:36:32 -0800521}