blob: 878e50bfdd1daf8c616eebaad03ef34af0d39908 [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"
Tom Sepez96d13342015-01-16 14:59:26 -080017#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez0aa35312016-01-06 10:16:32 -080018#include "testing/test_support.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070019
20#ifdef PDF_ENABLE_V8
Tom Sepez96d13342015-01-16 14:59:26 -080021#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070022#endif // PDF_ENABLE_v8
Tom Sepez96d13342015-01-16 14:59:26 -080023
24class TestLoader;
25
26// This class is used to load a PDF document, and then run programatic
27// API tests against it.
Tom Sepez4cb0fa72015-02-25 16:08:18 -080028class EmbedderTest : public ::testing::Test,
29 public UNSUPPORT_INFO,
30 public IPDF_JSPLATFORM,
Nicolas Pena3ff54002017-07-05 11:55:35 -040031 public FPDF_FORMFILLINFO,
32 public FPDF_FILEWRITE {
Tom Sepez96d13342015-01-16 14:59:26 -080033 public:
Tom Sepez4cb0fa72015-02-25 16:08:18 -080034 class Delegate {
35 public:
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 virtual ~Delegate() {}
Tom Sepez96d13342015-01-16 14:59:26 -080037
Tom Sepez4cb0fa72015-02-25 16:08:18 -080038 // Equivalent to UNSUPPORT_INFO::FSDK_UnSupport_Handler().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 virtual void UnsupportedHandler(int type) {}
Tom Sepez4cb0fa72015-02-25 16:08:18 -080040
41 // Equivalent to IPDF_JSPLATFORM::app_alert().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 virtual int Alert(FPDF_WIDESTRING message,
43 FPDF_WIDESTRING title,
44 int type,
45 int icon) {
Tom Sepez4cb0fa72015-02-25 16:08:18 -080046 return 0;
47 }
Tom Sepez6efc0ad2015-06-02 17:11:18 -070048
49 // Equivalent to FPDF_FORMFILLINFO::FFI_SetTimer().
50 virtual int SetTimer(int msecs, TimerCallback fn) { return 0; }
51
52 // Equivalent to FPDF_FORMFILLINFO::FFI_KillTimer().
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 virtual void KillTimer(int id) {}
Tom Sepez396e8722015-09-09 10:16:08 -070054
55 // Equivalent to FPDF_FORMFILLINFO::FFI_GetPage().
weili0dadcc62016-08-23 21:10:57 -070056 virtual FPDF_PAGE GetPage(FPDF_FORMFILLINFO* info,
Tom Sepez396e8722015-09-09 10:16:08 -070057 FPDF_DOCUMENT document,
58 int page_index);
Tom Sepez4cb0fa72015-02-25 16:08:18 -080059 };
60
61 EmbedderTest();
62 virtual ~EmbedderTest();
Tom Sepez96d13342015-01-16 14:59:26 -080063
64 void SetUp() override;
65 void TearDown() override;
66
Tom Sepez452b4f32015-10-13 09:27:27 -070067#ifdef PDF_ENABLE_V8
Tom Sepeza72e8e22015-10-07 10:17:53 -070068 // Call before SetUp to pass shared isolate, otherwise PDFium creates one.
Tom Sepez452b4f32015-10-13 09:27:27 -070069 void SetExternalIsolate(void* isolate) {
70 external_isolate_ = static_cast<v8::Isolate*>(isolate);
71 }
72#endif // PDF_ENABLE_V8
Tom Sepeza72e8e22015-10-07 10:17:53 -070073
Tom Sepez4cb0fa72015-02-25 16:08:18 -080074 void SetDelegate(Delegate* delegate) {
Tom Sepeza72e8e22015-10-07 10:17:53 -070075 delegate_ = delegate ? delegate : default_delegate_.get();
Tom Sepez4cb0fa72015-02-25 16:08:18 -080076 }
77
Tom Sepez96d13342015-01-16 14:59:26 -080078 FPDF_DOCUMENT document() { return document_; }
Tom Sepezda8189e2015-01-30 14:41:50 -080079 FPDF_FORMHANDLE form_handle() { return form_handle_; }
Tom Sepez96d13342015-01-16 14:59:26 -080080
Tom Sepezd483eb42016-01-06 10:03:59 -080081 // Create an empty document, and its form fill environment. Returns true
82 // on success or false on failure.
83 virtual bool CreateEmptyDocument();
84
Tom Sepezda8189e2015-01-30 14:41:50 -080085 // Open the document specified by |filename|, and create its form fill
86 // environment, or return false on failure.
Wei Li091f7a02015-11-09 12:09:55 -080087 // The filename is relative to the test data directory where we store all the
88 // test files.
Jun Fangdf7f3662015-11-10 18:29:18 +080089 virtual bool OpenDocument(const std::string& filename,
thestig27ddf162016-05-23 15:06:59 -070090 const char* password = nullptr,
Jun Fangdf7f3662015-11-10 18:29:18 +080091 bool must_linearize = false);
Tom Sepez96d13342015-01-16 14:59:26 -080092
Tom Sepez96d13342015-01-16 14:59:26 -080093 // Perform JavaScript actions that are to run at document open time.
Tom Sepezda8189e2015-01-30 14:41:50 -080094 virtual void DoOpenActions();
Tom Sepez96d13342015-01-16 14:59:26 -080095
96 // Determine the page numbers present in the document.
97 virtual int GetFirstPageNum();
98 virtual int GetPageCount();
99
100 // Load a specific page of the open document.
Tom Sepezda8189e2015-01-30 14:41:50 -0800101 virtual FPDF_PAGE LoadPage(int page_number);
Tom Sepez96d13342015-01-16 14:59:26 -0800102
103 // Convert a loaded page into a bitmap.
Tom Sepezda8189e2015-01-30 14:41:50 -0800104 virtual FPDF_BITMAP RenderPage(FPDF_PAGE page);
Tom Sepez96d13342015-01-16 14:59:26 -0800105
Jane Liuc533f4b2017-06-19 11:13:00 -0400106 // Convert a loaded page into a bitmap with page rendering flags specified.
107 // See public/fpdfview.h for a list of page rendering flags.
Jane Liubaa7ff42017-06-29 19:18:23 -0400108 virtual FPDF_BITMAP RenderPageWithFlags(FPDF_PAGE page,
109 FPDF_FORMHANDLE handle,
110 int flags);
Jane Liuc533f4b2017-06-19 11:13:00 -0400111
Tom Sepez96d13342015-01-16 14:59:26 -0800112 // Relese the resources obtained from LoadPage(). Further use of |page|
113 // is prohibited after this call is made.
Tom Sepezda8189e2015-01-30 14:41:50 -0800114 virtual void UnloadPage(FPDF_PAGE page);
Tom Sepez96d13342015-01-16 14:59:26 -0800115
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400116 protected:
Nicolas Pena56fc9722017-07-13 16:31:34 -0400117 bool OpenDocumentHelper(const char* password,
118 bool must_linearize,
119 FX_FILEAVAIL* file_avail,
120 FX_DOWNLOADHINTS* hints,
121 FPDF_FILEACCESS* file_access,
122 FPDF_DOCUMENT* document,
123 FPDF_AVAIL* avail,
124 FPDF_FORMHANDLE* form_handle);
125
Nicolas Pena3ff54002017-07-05 11:55:35 -0400126 FPDF_FORMHANDLE SetupFormFillEnvironment(FPDF_DOCUMENT doc);
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400127
Dan Sinclair957480c2017-06-13 15:21:14 -0400128 // Return the hash of |bitmap|.
129 static std::string HashBitmap(FPDF_BITMAP bitmap,
130 int expected_width,
131 int expected_height);
132
thestigbcd3e532016-11-21 13:37:28 -0800133 // Check |bitmap| to make sure it has the right dimensions and content.
134 static void CompareBitmap(FPDF_BITMAP bitmap,
135 int expected_width,
136 int expected_height,
137 const char* expected_md5sum);
138
Nicolas Pena3ff54002017-07-05 11:55:35 -0400139 void ClearString() { m_String.clear(); }
140 const std::string& GetString() const { return m_String; }
141
142 static int GetBlockFromString(void* param,
143 unsigned long pos,
144 unsigned char* buf,
145 unsigned long size);
146
Nicolas Pena56fc9722017-07-13 16:31:34 -0400147 void TestSaved(int width,
148 int height,
149 const char* md5,
150 const char* password = nullptr);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400151 void CloseSaved();
152 void TestAndCloseSaved(int width, int height, const char* md5);
153
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800154 Delegate* delegate_;
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800155 std::unique_ptr<Delegate> default_delegate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800156 FPDF_DOCUMENT document_;
Tom Sepezda8189e2015-01-30 14:41:50 -0800157 FPDF_FORMHANDLE form_handle_;
Tom Sepez96d13342015-01-16 14:59:26 -0800158 FPDF_AVAIL avail_;
159 FX_DOWNLOADHINTS hints_;
160 FPDF_FILEACCESS file_access_;
161 FX_FILEAVAIL file_avail_;
Tom Sepez452b4f32015-10-13 09:27:27 -0700162#ifdef PDF_ENABLE_V8
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700163 v8::Platform* platform_;
Tom Sepez452b4f32015-10-13 09:27:27 -0700164#endif // PDF_ENABLE_V8
165 void* external_isolate_;
Tom Sepez96d13342015-01-16 14:59:26 -0800166 TestLoader* loader_;
167 size_t file_length_;
Tom Sepez0aa35312016-01-06 10:16:32 -0800168 std::unique_ptr<char, pdfium::FreeDeleter> file_contents_;
weili0dadcc62016-08-23 21:10:57 -0700169 std::map<int, FPDF_PAGE> page_map_;
dsinclaircb92dc72016-09-07 09:02:48 -0700170 std::map<FPDF_PAGE, int> page_reverse_map_;
Nicolas Pena3ff54002017-07-05 11:55:35 -0400171 FPDF_DOCUMENT m_SavedDocument;
172 FPDF_PAGE m_SavedPage;
173 FPDF_FORMHANDLE m_SavedForm;
Nicolas Pena56fc9722017-07-13 16:31:34 -0400174 FPDF_AVAIL m_SavedAvail;
Tom Sepez4cb0fa72015-02-25 16:08:18 -0800175
176 private:
177 static void UnsupportedHandlerTrampoline(UNSUPPORT_INFO*, int type);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 static int AlertTrampoline(IPDF_JSPLATFORM* plaform,
179 FPDF_WIDESTRING message,
180 FPDF_WIDESTRING title,
181 int type,
182 int icon);
183 static int SetTimerTrampoline(FPDF_FORMFILLINFO* info,
184 int msecs,
Tom Sepez6efc0ad2015-06-02 17:11:18 -0700185 TimerCallback fn);
186 static void KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id);
Tom Sepez396e8722015-09-09 10:16:08 -0700187 static FPDF_PAGE GetPageTrampoline(FPDF_FORMFILLINFO* info,
188 FPDF_DOCUMENT document,
189 int page_index);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400190 static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
191 const void* data,
192 unsigned long size);
193
194 std::string m_String;
Tom Sepez96d13342015-01-16 14:59:26 -0800195};
196
197#endif // TESTING_EMBEDDER_TEST_H_