blob: 606472bc1d95d825ee889c70b303179faf090b45 [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
5#ifndef TESTING_EMBEDDER_TEST_H_
6#define TESTING_EMBEDDER_TEST_H_
7
Tom Sepez396e8722015-09-09 10:16:08 -07008#include <map>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08009#include <memory>
Tom Sepez96d13342015-01-16 14:59:26 -080010#include <string>
11
Lei Zhangb4e7f302015-11-06 15:52:32 -080012#include "public/fpdf_dataavail.h"
13#include "public/fpdf_ext.h"
14#include "public/fpdf_formfill.h"
Nicolas Pena3ff54002017-07-05 11:55:35 -040015#include "public/fpdf_save.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080016#include "public/fpdfview.h"
Artem Strygin0e60b9e2017-09-28 18:46:03 +030017#include "testing/fake_file_access.h"
Tom Sepez96d13342015-01-16 14:59:26 -080018#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez0aa35312016-01-06 10:16:32 -080019#include "testing/test_support.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070020
21#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080022#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070023#endif // PDF_ENABLE_v8
Tom Sepez96d13342015-01-16 14:59:26 -080024
25class TestLoader;
26
27// This class is used to load a PDF document, and then run programatic
28// API tests against it.
Tom Sepez4cb0fa72015-02-25 16:08:18 -080029class EmbedderTest : public ::testing::Test,
30 public UNSUPPORT_INFO,
31 public IPDF_JSPLATFORM,
Nicolas Pena3ff54002017-07-05 11:55:35 -040032 public FPDF_FORMFILLINFO,
33 public FPDF_FILEWRITE {
Tom Sepez96d13342015-01-16 14:59:26 -080034 public:
Tom Sepez4cb0fa72015-02-25 16:08:18 -080035 class Delegate {
36 public:
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 virtual ~Delegate() {}
Tom Sepez96d13342015-01-16 14:59:26 -080038
Tom Sepez4cb0fa72015-02-25 16:08:18 -080039 // Equivalent to UNSUPPORT_INFO::FSDK_UnSupport_Handler().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040 virtual void UnsupportedHandler(int type) {}
Tom Sepez4cb0fa72015-02-25 16:08:18 -080041
42 // Equivalent to IPDF_JSPLATFORM::app_alert().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 virtual int Alert(FPDF_WIDESTRING message,
44 FPDF_WIDESTRING title,
45 int type,
46 int icon) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -080047 return 0;
48 }
Tom Sepez6efc0ad2015-06-02 17:11:18 -070049
50 // Equivalent to FPDF_FORMFILLINFO::FFI_SetTimer().
51 virtual int SetTimer(int msecs, TimerCallback fn) { return 0; }
52
53 // Equivalent to FPDF_FORMFILLINFO::FFI_KillTimer().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 virtual void KillTimer(int id) {}
Tom Sepez396e8722015-09-09 10:16:08 -070055
56 // Equivalent to FPDF_FORMFILLINFO::FFI_GetPage().
weili0dadcc62016-08-23 21:10:57 -070057 virtual FPDF_PAGE GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -070058 FPDF_DOCUMENT document,
59 int page_index);
Tom Sepez4cb0fa72015-02-25 16:08:18 -080060 };
61
62 EmbedderTest();
63 virtual ~EmbedderTest();
Tom Sepez96d13342015-01-16 14:59:26 -080064
65 void SetUp() override;
66 void TearDown() override;
67
Tom Sepez452b4f32015-10-13 09:27:27 -070068#ifdef PDF_ENABLE_V8
Tom Sepeza72e8e22015-10-07 10:17:53 -070069 // Call before SetUp to pass shared isolate, otherwise PDFium creates one.
Tom Sepez452b4f32015-10-13 09:27:27 -070070 void SetExternalIsolate(void* isolate) {
71 external_isolate_ = static_cast<v8::Isolate*>(isolate);
72 }
73#endif // PDF_ENABLE_V8
Tom Sepeza72e8e22015-10-07 10:17:53 -070074
Tom Sepez4cb0fa72015-02-25 16:08:18 -080075 void SetDelegate(Delegate* delegate) {
Tom Sepeza72e8e22015-10-07 10:17:53 -070076 delegate_ = delegate ? delegate : default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080077 }
78
Tom Sepez96d13342015-01-16 14:59:26 -080079 FPDF_DOCUMENT document() { return document_; }
Tom Sepezda8189e2015-01-30 14:41:50 -080080 FPDF_FORMHANDLE form_handle() { return form_handle_; }
Tom Sepez96d13342015-01-16 14:59:26 -080081
Tom Sepezd483eb42016-01-06 10:03:59 -080082 // Create an empty document, and its form fill environment. Returns true
83 // on success or false on failure.
84 virtual bool CreateEmptyDocument();
85
Tom Sepezda8189e2015-01-30 14:41:50 -080086 // Open the document specified by |filename|, and create its form fill
87 // environment, or return false on failure.
Wei Li091f7a02015-11-09 12:09:55 -080088 // The filename is relative to the test data directory where we store all the
89 // test files.
Jun Fangdf7f3662015-11-10 18:29:18 +080090 virtual bool OpenDocument(const std::string& filename,
thestig27ddf162016-05-23 15:06:59 -070091 const char* password = nullptr,
Jun Fangdf7f3662015-11-10 18:29:18 +080092 bool must_linearize = false);
Tom Sepez96d13342015-01-16 14:59:26 -080093
Tom Sepez96d13342015-01-16 14:59:26 -080094 // Perform JavaScript actions that are to run at document open time.
Tom Sepezda8189e2015-01-30 14:41:50 -080095 virtual void DoOpenActions();
Tom Sepez96d13342015-01-16 14:59:26 -080096
97 // Determine the page numbers present in the document.
98 virtual int GetFirstPageNum();
99 virtual int GetPageCount();
100
101 // Load a specific page of the open document.
Tom Sepezda8189e2015-01-30 14:41:50 -0800102 virtual FPDF_PAGE LoadPage(int page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800103
104 // Convert a loaded page into a bitmap.
Tom Sepezda8189e2015-01-30 14:41:50 -0800105 virtual FPDF_BITMAP RenderPage(FPDF_PAGE page);
Tom Sepez96d13342015-01-16 14:59:26 -0800106
Jane Liuc533f4b2017-06-19 11:13:00 -0400107 // Convert a loaded page into a bitmap with page rendering flags specified.
108 // See public/fpdfview.h for a list of page rendering flags.
Jane Liubaa7ff42017-06-29 19:18:23 -0400109 virtual FPDF_BITMAP RenderPageWithFlags(FPDF_PAGE page,
110 FPDF_FORMHANDLE handle,
111 int flags);
Jane Liuc533f4b2017-06-19 11:13:00 -0400112
Tom Sepez96d13342015-01-16 14:59:26 -0800113 // Relese the resources obtained from LoadPage(). Further use of |page|
114 // is prohibited after this call is made.
Tom Sepezda8189e2015-01-30 14:41:50 -0800115 virtual void UnloadPage(FPDF_PAGE page);
Tom Sepez96d13342015-01-16 14:59:26 -0800116
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400117 protected:
Nicolas Pena56fc9722017-07-13 16:31:34 -0400118 bool OpenDocumentHelper(const char* password,
119 bool must_linearize,
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300120 FakeFileAccess* network_simulator,
Nicolas Pena56fc9722017-07-13 16:31:34 -0400121 FPDF_DOCUMENT* document,
122 FPDF_AVAIL* avail,
123 FPDF_FORMHANDLE* form_handle);
124
Nicolas Pena3ff54002017-07-05 11:55:35 -0400125 FPDF_FORMHANDLE SetupFormFillEnvironment(FPDF_DOCUMENT doc);
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400126
Dan Sinclair957480c2017-06-13 15:21:14 -0400127 // Return the hash of |bitmap|.
128 static std::string HashBitmap(FPDF_BITMAP bitmap,
129 int expected_width,
130 int expected_height);
131
thestigbcd3e532016-11-21 13:37:28 -0800132 // Check |bitmap| to make sure it has the right dimensions and content.
133 static void CompareBitmap(FPDF_BITMAP bitmap,
134 int expected_width,
135 int expected_height,
136 const char* expected_md5sum);
137
Nicolas Pena3ff54002017-07-05 11:55:35 -0400138 void ClearString() { m_String.clear(); }
139 const std::string& GetString() const { return m_String; }
140
141 static int GetBlockFromString(void* param,
142 unsigned long pos,
143 unsigned char* buf,
144 unsigned long size);
145
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400146 FPDF_DOCUMENT OpenSavedDocument(const char* password = nullptr);
147 void CloseSavedDocument();
148 FPDF_PAGE LoadSavedPage();
149 void CloseSavedPage();
150 void VerifySavedRendering(int width, int height, const char* md5);
151 void VerifySavedDocument(int width, int height, const char* md5);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400152
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300153 void SetWholeFileAvailable();
154
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800155 Delegate* delegate_;
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800156 std::unique_ptr<Delegate> default_delegate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800157 FPDF_DOCUMENT document_;
Tom Sepezda8189e2015-01-30 14:41:50 -0800158 FPDF_FORMHANDLE form_handle_;
Tom Sepez96d13342015-01-16 14:59:26 -0800159 FPDF_AVAIL avail_;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300160 FPDF_FILEACCESS file_access_; // must outlive avail_.
Tom Sepez452b4f32015-10-13 09:27:27 -0700161#ifdef PDF_ENABLE_V8
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700162 v8::Platform* platform_;
Tom Sepez452b4f32015-10-13 09:27:27 -0700163#endif // PDF_ENABLE_V8
164 void* external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800165 TestLoader* loader_;
166 size_t file_length_;
Tom Sepez0aa35312016-01-06 10:16:32 -0800167 std::unique_ptr<char, pdfium::FreeDeleter> file_contents_;
weili0dadcc62016-08-23 21:10:57 -0700168 std::map<int, FPDF_PAGE> page_map_;
dsinclaircb92dc72016-09-07 09:02:48 -0700169 std::map<FPDF_PAGE, int> page_reverse_map_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400170 FPDF_DOCUMENT m_SavedDocument;
171 FPDF_PAGE m_SavedPage;
172 FPDF_FORMHANDLE m_SavedForm;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400173 FPDF_AVAIL m_SavedAvail;
Artem Strygin0e60b9e2017-09-28 18:46:03 +0300174 FPDF_FILEACCESS saved_file_access_; // must outlive m_SavedAvail.
175 std::unique_ptr<FakeFileAccess> fake_file_access_; // must outlive avail_.
176 std::unique_ptr<FakeFileAccess>
177 saved_fake_file_access_; // must outlive m_SavedAvail.
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800178
179 private:
180 static void UnsupportedHandlerTrampoline(UNSUPPORT_INFO*, int type);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 static int AlertTrampoline(IPDF_JSPLATFORM* plaform,
182 FPDF_WIDESTRING message,
183 FPDF_WIDESTRING title,
184 int type,
185 int icon);
186 static int SetTimerTrampoline(FPDF_FORMFILLINFO* info,
187 int msecs,
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700188 TimerCallback fn);
189 static void KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id);
Tom Sepez396e8722015-09-09 10:16:08 -0700190 static FPDF_PAGE GetPageTrampoline(FPDF_FORMFILLINFO* info,
191 FPDF_DOCUMENT document,
192 int page_index);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400193 static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
194 const void* data,
195 unsigned long size);
196
197 std::string m_String;
Tom Sepez96d13342015-01-16 14:59:26 -0800198};
199
200#endif // TESTING_EMBEDDER_TEST_H_