diff options
author | Nicolas Pena <npm@chromium.org> | 2017-07-05 11:55:35 -0400 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-07-05 17:08:57 +0000 |
commit | 3ff54008a163eea3158a7c107595daf79ccc7d44 (patch) | |
tree | f460b4f50863e65922799970208bf93bd845a7d8 /testing | |
parent | fb00ec2af390ccfe8f5e81f49b9113e044f5e5ad (diff) | |
download | pdfium-3ff54008a163eea3158a7c107595daf79ccc7d44.tar.xz |
Allow EmbedderTest to test saving
This CL adds helper methods for EmbedderTest to save. This means that
TestSaver is no longer needed and all the methods it uses are moved to
the EmbedderTest.
Change-Id: I740f29699bfd8c6b570cb1fd85e7aab9bc1d3f6f
Reviewed-on: https://pdfium-review.googlesource.com/7171
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Nicolás Peña <npm@chromium.org>
Diffstat (limited to 'testing')
-rw-r--r-- | testing/embedder_test.cpp | 89 | ||||
-rw-r--r-- | testing/embedder_test.h | 26 | ||||
-rw-r--r-- | testing/test_support.cpp | 35 | ||||
-rw-r--r-- | testing/test_support.h | 22 |
4 files changed, 97 insertions, 75 deletions
diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp index 55e93a3d32..80d4a66381 100644 --- a/testing/embedder_test.cpp +++ b/testing/embedder_test.cpp @@ -73,6 +73,8 @@ EmbedderTest::EmbedderTest() InitializeV8ForPDFium(g_exe_path, &platform_); #endif // V8_USE_EXTERNAL_STARTUP_DATA #endif // FPDF_ENABLE_V8 + FPDF_FILEWRITE::version = 1; + FPDF_FILEWRITE::WriteBlock = WriteBlockCallback; } EmbedderTest::~EmbedderTest() { @@ -106,7 +108,6 @@ void EmbedderTest::TearDown() { FPDFAvail_Destroy(avail_); FPDF_DestroyLibrary(); - delete loader_; } @@ -115,7 +116,7 @@ bool EmbedderTest::CreateEmptyDocument() { if (!document_) return false; - SetupFormFillEnvironment(); + form_handle_ = SetupFormFillEnvironment(document_); return true; } @@ -178,9 +179,7 @@ bool EmbedderTest::OpenDocument(const std::string& filename, return false; } } - - SetupFormFillEnvironment(); - + form_handle_ = SetupFormFillEnvironment(document_); #ifdef PDF_ENABLE_XFA int docType = DOCTYPE_PDF; if (FPDF_HasXFAField(document_, &docType)) { @@ -188,14 +187,13 @@ bool EmbedderTest::OpenDocument(const std::string& filename, (void)FPDF_LoadXFA(document_); } #endif // PDF_ENABLE_XFA - (void)FPDF_GetDocPermissions(document_); return true; } -void EmbedderTest::SetupFormFillEnvironment() { +FPDF_FORMHANDLE EmbedderTest::SetupFormFillEnvironment(FPDF_DOCUMENT doc) { IPDF_JSPLATFORM* platform = static_cast<IPDF_JSPLATFORM*>(this); - memset(platform, 0, sizeof(IPDF_JSPLATFORM)); + memset(platform, '\0', sizeof(IPDF_JSPLATFORM)); platform->version = 2; platform->app_alert = AlertTrampoline; platform->m_isolate = external_isolate_; @@ -211,13 +209,15 @@ void EmbedderTest::SetupFormFillEnvironment() { formfillinfo->FFI_KillTimer = KillTimerTrampoline; formfillinfo->FFI_GetPage = GetPageTrampoline; formfillinfo->m_pJsPlatform = platform; - - form_handle_ = FPDFDOC_InitFormFillEnvironment(document_, formfillinfo); - FPDF_SetFormFieldHighlightColor(form_handle_, 0, 0xFFE4DD); - FPDF_SetFormFieldHighlightAlpha(form_handle_, 100); + FPDF_FORMHANDLE form_handle = + FPDFDOC_InitFormFillEnvironment(doc, formfillinfo); + FPDF_SetFormFieldHighlightColor(form_handle, 0, 0xFFE4DD); + FPDF_SetFormFieldHighlightAlpha(form_handle, 100); + return form_handle; } void EmbedderTest::DoOpenActions() { + ASSERT(form_handle_); FORM_DoDocumentJSAction(form_handle_); FORM_DoDocumentOpenAction(form_handle_); } @@ -230,22 +230,22 @@ int EmbedderTest::GetFirstPageNum() { int EmbedderTest::GetPageCount() { int page_count = FPDF_GetPageCount(document_); - for (int i = 0; i < page_count; ++i) { + for (int i = 0; i < page_count; ++i) (void)FPDFAvail_IsPageAvail(avail_, i, &hints_); - } return page_count; } FPDF_PAGE EmbedderTest::LoadPage(int page_number) { + ASSERT(form_handle_); // First check whether it is loaded already. auto it = page_map_.find(page_number); if (it != page_map_.end()) return it->second; FPDF_PAGE page = FPDF_LoadPage(document_, page_number); - if (!page) { + if (!page) return nullptr; - } + FORM_OnAfterLoadPage(page, form_handle_); FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_OPEN); // Cache the page. @@ -273,6 +273,7 @@ FPDF_BITMAP EmbedderTest::RenderPageWithFlags(FPDF_PAGE page, } void EmbedderTest::UnloadPage(FPDF_PAGE page) { + ASSERT(form_handle_); FORM_DoPageAAction(page, form_handle_, FPDFPAGE_AACTION_CLOSE); FORM_OnBeforeClosePage(page, form_handle_); FPDF_ClosePage(page); @@ -285,6 +286,36 @@ void EmbedderTest::UnloadPage(FPDF_PAGE page) { page_reverse_map_.erase(it); } +void EmbedderTest::TestSaved(int width, int height, const char* md5) { + FPDF_FILEACCESS file_access; + memset(&file_access, 0, sizeof(file_access)); + file_access.m_FileLen = m_String.size(); + file_access.m_GetBlock = GetBlockFromString; + file_access.m_Param = &m_String; + + m_SavedDocument = FPDF_LoadCustomDocument(&file_access, nullptr); + m_SavedForm = SetupFormFillEnvironment(m_SavedDocument); + ASSERT_TRUE(m_SavedDocument); + EXPECT_EQ(1, FPDF_GetPageCount(m_SavedDocument)); + m_SavedPage = FPDF_LoadPage(m_SavedDocument, 0); + ASSERT_TRUE(m_SavedPage); + FPDF_BITMAP new_bitmap = + RenderPageWithFlags(m_SavedPage, m_SavedForm, FPDF_ANNOT); + CompareBitmap(new_bitmap, width, height, md5); + FPDFBitmap_Destroy(new_bitmap); +} + +void EmbedderTest::CloseSaved() { + FPDF_ClosePage(m_SavedPage); + FPDFDOC_ExitFormFillEnvironment(m_SavedForm); + FPDF_CloseDocument(m_SavedDocument); +} + +void EmbedderTest::TestAndCloseSaved(int width, int height, const char* md5) { + TestSaved(width, height, md5); + CloseSaved(); +} + FPDF_PAGE EmbedderTest::Delegate::GetPage(FPDF_FORMFILLINFO* info, FPDF_DOCUMENT document, int page_index) { @@ -358,6 +389,32 @@ void EmbedderTest::CompareBitmap(FPDF_BITMAP bitmap, HashBitmap(bitmap, expected_width, expected_height)); } +// static +int EmbedderTest::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite, + const void* data, + unsigned long size) { + EmbedderTest* pThis = static_cast<EmbedderTest*>(pFileWrite); + pThis->m_String.append(static_cast<const char*>(data), size); + return 1; +} + +// static +int EmbedderTest::GetBlockFromString(void* param, + unsigned long pos, + unsigned char* buf, + unsigned long size) { + std::string* new_file = static_cast<std::string*>(param); + if (!new_file || pos + size < pos) + return 0; + + unsigned long file_size = new_file->size(); + if (pos + size > file_size) + return 0; + + memcpy(buf, new_file->data() + pos, size); + return 1; +} + // Can't use gtest-provided main since we need to stash the path to the // executable in order to find the external V8 binary data files. int main(int argc, char** argv) { diff --git a/testing/embedder_test.h b/testing/embedder_test.h index ba44673004..d2fd984a10 100644 --- a/testing/embedder_test.h +++ b/testing/embedder_test.h @@ -12,6 +12,7 @@ #include "public/fpdf_dataavail.h" #include "public/fpdf_ext.h" #include "public/fpdf_formfill.h" +#include "public/fpdf_save.h" #include "public/fpdfview.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/test_support.h" @@ -27,7 +28,8 @@ class TestLoader; class EmbedderTest : public ::testing::Test, public UNSUPPORT_INFO, public IPDF_JSPLATFORM, - public FPDF_FORMFILLINFO { + public FPDF_FORMFILLINFO, + public FPDF_FILEWRITE { public: class Delegate { public: @@ -112,7 +114,7 @@ class EmbedderTest : public ::testing::Test, virtual void UnloadPage(FPDF_PAGE page); protected: - void SetupFormFillEnvironment(); + FPDF_FORMHANDLE SetupFormFillEnvironment(FPDF_DOCUMENT doc); // Return the hash of |bitmap|. static std::string HashBitmap(FPDF_BITMAP bitmap, @@ -125,6 +127,18 @@ class EmbedderTest : public ::testing::Test, int expected_height, const char* expected_md5sum); + void ClearString() { m_String.clear(); } + const std::string& GetString() const { return m_String; } + + static int GetBlockFromString(void* param, + unsigned long pos, + unsigned char* buf, + unsigned long size); + + void TestSaved(int width, int height, const char* md5); + void CloseSaved(); + void TestAndCloseSaved(int width, int height, const char* md5); + Delegate* delegate_; std::unique_ptr<Delegate> default_delegate_; FPDF_DOCUMENT document_; @@ -142,6 +156,9 @@ class EmbedderTest : public ::testing::Test, std::unique_ptr<char, pdfium::FreeDeleter> file_contents_; std::map<int, FPDF_PAGE> page_map_; std::map<FPDF_PAGE, int> page_reverse_map_; + FPDF_DOCUMENT m_SavedDocument; + FPDF_PAGE m_SavedPage; + FPDF_FORMHANDLE m_SavedForm; private: static void UnsupportedHandlerTrampoline(UNSUPPORT_INFO*, int type); @@ -157,6 +174,11 @@ class EmbedderTest : public ::testing::Test, static FPDF_PAGE GetPageTrampoline(FPDF_FORMFILLINFO* info, FPDF_DOCUMENT document, int page_index); + static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite, + const void* data, + unsigned long size); + + std::string m_String; }; #endif // TESTING_EMBEDDER_TEST_H_ diff --git a/testing/test_support.cpp b/testing/test_support.cpp index 681a7baca3..1ad8543bae 100644 --- a/testing/test_support.cpp +++ b/testing/test_support.cpp @@ -211,38 +211,3 @@ int TestLoader::GetBlock(void* param, memcpy(pBuf, pLoader->m_pBuf + pos, size); return 1; } - -TestSaver::TestSaver() { - FPDF_FILEWRITE::version = 1; - FPDF_FILEWRITE::WriteBlock = WriteBlockCallback; -} - -void TestSaver::ClearString() { - m_String.clear(); -} - -// static -int TestSaver::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite, - const void* data, - unsigned long size) { - TestSaver* pThis = static_cast<TestSaver*>(pFileWrite); - pThis->m_String.append(static_cast<const char*>(data), size); - return 1; -} - -// static -int TestSaver::GetBlockFromString(void* param, - unsigned long pos, - unsigned char* buf, - unsigned long size) { - std::string* new_file = static_cast<std::string*>(param); - if (!new_file || pos + size < pos) - return 0; - - unsigned long file_size = new_file->size(); - if (pos + size > file_size) - return 0; - - memcpy(buf, new_file->data() + pos, size); - return 1; -} diff --git a/testing/test_support.h b/testing/test_support.h index 146582251f..7676e5783b 100644 --- a/testing/test_support.h +++ b/testing/test_support.h @@ -11,7 +11,6 @@ #include <string> #include <vector> -#include "public/fpdf_save.h" #include "public/fpdfview.h" namespace pdfium { @@ -107,25 +106,4 @@ class TestLoader { const size_t m_Len; }; -class TestSaver : public FPDF_FILEWRITE { - public: - TestSaver(); - - void ClearString(); - const std::string& GetString() const { return m_String; } - - protected: - static int GetBlockFromString(void* param, - unsigned long pos, - unsigned char* buf, - unsigned long size); - - private: - static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite, - const void* data, - unsigned long size); - - std::string m_String; -}; - #endif // TESTING_TEST_SUPPORT_H_ |