Lei Zhang | 1ac47eb | 2015-12-21 11:04:44 -0800 | [diff] [blame] | 1 | // Copyright 2015 PDFium Authors. All rights reserved. |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 2 | // 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 Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 8 | #include <map> |
Lei Zhang | aa8bf7e | 2015-12-24 19:13:32 -0800 | [diff] [blame] | 9 | #include <memory> |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 10 | #include <string> |
| 11 | |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 12 | #include "public/fpdf_dataavail.h" |
| 13 | #include "public/fpdf_ext.h" |
| 14 | #include "public/fpdf_formfill.h" |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 15 | #include "public/fpdf_save.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 16 | #include "public/fpdfview.h" |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 18 | #include "testing/test_support.h" |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 19 | |
| 20 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 21 | #include "v8/include/v8.h" |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 22 | #endif // PDF_ENABLE_v8 |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 23 | |
| 24 | class TestLoader; |
| 25 | |
| 26 | // This class is used to load a PDF document, and then run programatic |
| 27 | // API tests against it. |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 28 | class EmbedderTest : public ::testing::Test, |
| 29 | public UNSUPPORT_INFO, |
| 30 | public IPDF_JSPLATFORM, |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 31 | public FPDF_FORMFILLINFO, |
| 32 | public FPDF_FILEWRITE { |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 33 | public: |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 34 | class Delegate { |
| 35 | public: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 36 | virtual ~Delegate() {} |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 37 | |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 38 | // Equivalent to UNSUPPORT_INFO::FSDK_UnSupport_Handler(). |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 39 | virtual void UnsupportedHandler(int type) {} |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 40 | |
| 41 | // Equivalent to IPDF_JSPLATFORM::app_alert(). |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 42 | virtual int Alert(FPDF_WIDESTRING message, |
| 43 | FPDF_WIDESTRING title, |
| 44 | int type, |
| 45 | int icon) { |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 46 | return 0; |
| 47 | } |
Tom Sepez | 6efc0ad | 2015-06-02 17:11:18 -0700 | [diff] [blame] | 48 | |
| 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 Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 53 | virtual void KillTimer(int id) {} |
Tom Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 54 | |
| 55 | // Equivalent to FPDF_FORMFILLINFO::FFI_GetPage(). |
weili | 0dadcc6 | 2016-08-23 21:10:57 -0700 | [diff] [blame] | 56 | virtual FPDF_PAGE GetPage(FPDF_FORMFILLINFO* info, |
Tom Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 57 | FPDF_DOCUMENT document, |
| 58 | int page_index); |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | EmbedderTest(); |
| 62 | virtual ~EmbedderTest(); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 63 | |
| 64 | void SetUp() override; |
| 65 | void TearDown() override; |
| 66 | |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 67 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 68 | // Call before SetUp to pass shared isolate, otherwise PDFium creates one. |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 69 | void SetExternalIsolate(void* isolate) { |
| 70 | external_isolate_ = static_cast<v8::Isolate*>(isolate); |
| 71 | } |
| 72 | #endif // PDF_ENABLE_V8 |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 73 | |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 74 | void SetDelegate(Delegate* delegate) { |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 75 | delegate_ = delegate ? delegate : default_delegate_.get(); |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 78 | FPDF_DOCUMENT document() { return document_; } |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 79 | FPDF_FORMHANDLE form_handle() { return form_handle_; } |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 80 | |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 81 | // Create an empty document, and its form fill environment. Returns true |
| 82 | // on success or false on failure. |
| 83 | virtual bool CreateEmptyDocument(); |
| 84 | |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 85 | // Open the document specified by |filename|, and create its form fill |
| 86 | // environment, or return false on failure. |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 87 | // The filename is relative to the test data directory where we store all the |
| 88 | // test files. |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 89 | virtual bool OpenDocument(const std::string& filename, |
thestig | 27ddf16 | 2016-05-23 15:06:59 -0700 | [diff] [blame] | 90 | const char* password = nullptr, |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 91 | bool must_linearize = false); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 92 | |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 93 | // Perform JavaScript actions that are to run at document open time. |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 94 | virtual void DoOpenActions(); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 95 | |
| 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 Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 101 | virtual FPDF_PAGE LoadPage(int page_number); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 102 | |
| 103 | // Convert a loaded page into a bitmap. |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 104 | virtual FPDF_BITMAP RenderPage(FPDF_PAGE page); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 105 | |
Jane Liu | c533f4b | 2017-06-19 11:13:00 -0400 | [diff] [blame] | 106 | // 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 Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 108 | virtual FPDF_BITMAP RenderPageWithFlags(FPDF_PAGE page, |
| 109 | FPDF_FORMHANDLE handle, |
| 110 | int flags); |
Jane Liu | c533f4b | 2017-06-19 11:13:00 -0400 | [diff] [blame] | 111 | |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 112 | // Relese the resources obtained from LoadPage(). Further use of |page| |
| 113 | // is prohibited after this call is made. |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 114 | virtual void UnloadPage(FPDF_PAGE page); |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 115 | |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 116 | protected: |
Nicolas Pena | 56fc972 | 2017-07-13 16:31:34 -0400 | [diff] [blame] | 117 | 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 Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 126 | FPDF_FORMHANDLE SetupFormFillEnvironment(FPDF_DOCUMENT doc); |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 127 | |
Dan Sinclair | 957480c | 2017-06-13 15:21:14 -0400 | [diff] [blame] | 128 | // Return the hash of |bitmap|. |
| 129 | static std::string HashBitmap(FPDF_BITMAP bitmap, |
| 130 | int expected_width, |
| 131 | int expected_height); |
| 132 | |
thestig | bcd3e53 | 2016-11-21 13:37:28 -0800 | [diff] [blame] | 133 | // 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 Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 139 | 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 Pena | 56fc972 | 2017-07-13 16:31:34 -0400 | [diff] [blame] | 147 | void TestSaved(int width, |
| 148 | int height, |
| 149 | const char* md5, |
| 150 | const char* password = nullptr); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 151 | void CloseSaved(); |
| 152 | void TestAndCloseSaved(int width, int height, const char* md5); |
| 153 | |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 154 | Delegate* delegate_; |
Lei Zhang | aa8bf7e | 2015-12-24 19:13:32 -0800 | [diff] [blame] | 155 | std::unique_ptr<Delegate> default_delegate_; |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 156 | FPDF_DOCUMENT document_; |
Tom Sepez | da8189e | 2015-01-30 14:41:50 -0800 | [diff] [blame] | 157 | FPDF_FORMHANDLE form_handle_; |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 158 | FPDF_AVAIL avail_; |
| 159 | FX_DOWNLOADHINTS hints_; |
| 160 | FPDF_FILEACCESS file_access_; |
| 161 | FX_FILEAVAIL file_avail_; |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 162 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | 6efc0ad | 2015-06-02 17:11:18 -0700 | [diff] [blame] | 163 | v8::Platform* platform_; |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 164 | #endif // PDF_ENABLE_V8 |
| 165 | void* external_isolate_; |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 166 | TestLoader* loader_; |
| 167 | size_t file_length_; |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 168 | std::unique_ptr<char, pdfium::FreeDeleter> file_contents_; |
weili | 0dadcc6 | 2016-08-23 21:10:57 -0700 | [diff] [blame] | 169 | std::map<int, FPDF_PAGE> page_map_; |
dsinclair | cb92dc7 | 2016-09-07 09:02:48 -0700 | [diff] [blame] | 170 | std::map<FPDF_PAGE, int> page_reverse_map_; |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 171 | FPDF_DOCUMENT m_SavedDocument; |
| 172 | FPDF_PAGE m_SavedPage; |
| 173 | FPDF_FORMHANDLE m_SavedForm; |
Nicolas Pena | 56fc972 | 2017-07-13 16:31:34 -0400 | [diff] [blame] | 174 | FPDF_AVAIL m_SavedAvail; |
Tom Sepez | 4cb0fa7 | 2015-02-25 16:08:18 -0800 | [diff] [blame] | 175 | |
| 176 | private: |
| 177 | static void UnsupportedHandlerTrampoline(UNSUPPORT_INFO*, int type); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | 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 Sepez | 6efc0ad | 2015-06-02 17:11:18 -0700 | [diff] [blame] | 185 | TimerCallback fn); |
| 186 | static void KillTimerTrampoline(FPDF_FORMFILLINFO* info, int id); |
Tom Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 187 | static FPDF_PAGE GetPageTrampoline(FPDF_FORMFILLINFO* info, |
| 188 | FPDF_DOCUMENT document, |
| 189 | int page_index); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 190 | static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite, |
| 191 | const void* data, |
| 192 | unsigned long size); |
| 193 | |
| 194 | std::string m_String; |
Tom Sepez | 96d1334 | 2015-01-16 14:59:26 -0800 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | #endif // TESTING_EMBEDDER_TEST_H_ |