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