From 3ff54008a163eea3158a7c107595daf79ccc7d44 Mon Sep 17 00:00:00 2001 From: Nicolas Pena Date: Wed, 5 Jul 2017 11:55:35 -0400 Subject: Allow EmbedderTest to test saving MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Commit-Queue: Nicolás Peña --- fpdfsdk/fpdf_dataavail_embeddertest.cpp | 8 +-- fpdfsdk/fpdf_structtree_embeddertest.cpp | 2 +- fpdfsdk/fpdfannot_embeddertest.cpp | 47 +++++------------ fpdfsdk/fpdfedit_embeddertest.cpp | 67 ++++++------------------ fpdfsdk/fpdfformfill.cpp | 1 - fpdfsdk/fpdfformfill_embeddertest.cpp | 21 ++------ fpdfsdk/fpdfppo_embeddertest.cpp | 1 + fpdfsdk/fpdfsave_embeddertest.cpp | 2 +- testing/embedder_test.cpp | 89 ++++++++++++++++++++++++++------ testing/embedder_test.h | 26 +++++++++- testing/test_support.cpp | 35 ------------- testing/test_support.h | 22 -------- 12 files changed, 135 insertions(+), 186 deletions(-) diff --git a/fpdfsdk/fpdf_dataavail_embeddertest.cpp b/fpdfsdk/fpdf_dataavail_embeddertest.cpp index c226a31b8e..8f37984c9b 100644 --- a/fpdfsdk/fpdf_dataavail_embeddertest.cpp +++ b/fpdfsdk/fpdf_dataavail_embeddertest.cpp @@ -194,9 +194,9 @@ TEST_F(FPDFDataAvailEmbeddertest, LoadUsingHintTables) { // No new data available, to prevent load "Pages" node. loader.set_is_new_data_available(false); - FPDF_PAGE page = LoadPage(1); + FPDF_PAGE page = FPDF_LoadPage(document(), 1); EXPECT_TRUE(page); - UnloadPage(page); + FPDF_ClosePage(page); } TEST_F(FPDFDataAvailEmbeddertest, @@ -234,7 +234,7 @@ TEST_F(FPDFDataAvailEmbeddertest, // Prevent loading data, while page loading. loader.set_is_new_data_available(false); - FPDF_PAGE page = LoadPage(first_page_num); + FPDF_PAGE page = FPDF_LoadPage(document(), first_page_num); EXPECT_TRUE(page); - UnloadPage(page); + FPDF_ClosePage(page); } diff --git a/fpdfsdk/fpdf_structtree_embeddertest.cpp b/fpdfsdk/fpdf_structtree_embeddertest.cpp index 3110988d82..43a4d0159f 100644 --- a/fpdfsdk/fpdf_structtree_embeddertest.cpp +++ b/fpdfsdk/fpdf_structtree_embeddertest.cpp @@ -7,7 +7,7 @@ #include "testing/embedder_test.h" #include "testing/test_support.h" -class FPDFStructTreeEmbeddertest : public EmbedderTest, public TestSaver {}; +class FPDFStructTreeEmbeddertest : public EmbedderTest {}; TEST_F(FPDFStructTreeEmbeddertest, GetAltText) { ASSERT_TRUE(OpenDocument("tagged_alt_text.pdf")); diff --git a/fpdfsdk/fpdfannot_embeddertest.cpp b/fpdfsdk/fpdfannot_embeddertest.cpp index c08460e745..6c56c06396 100644 --- a/fpdfsdk/fpdfannot_embeddertest.cpp +++ b/fpdfsdk/fpdfannot_embeddertest.cpp @@ -13,7 +13,7 @@ #include "testing/embedder_test.h" #include "testing/gtest/include/gtest/gtest.h" -class FPDFAnnotEmbeddertest : public EmbedderTest, public TestSaver {}; +class FPDFAnnotEmbeddertest : public EmbedderTest {}; TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) { // Open a file with one annotation and load its first page. @@ -25,7 +25,7 @@ TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) { // normal appearance defined, only its rollover appearance. In this case, its // normal appearance should be generated, allowing the highlight annotation to // still display. - FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); + FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle(), FPDF_ANNOT); CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); FPDFBitmap_Destroy(bitmap); @@ -279,23 +279,15 @@ TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) { FPDF_ClosePage(page); // Open the saved document. - std::string new_file = GetString(); - FPDF_FILEACCESS file_access; - memset(&file_access, 0, sizeof(file_access)); - file_access.m_FileLen = new_file.size(); - file_access.m_GetBlock = GetBlockFromString; - file_access.m_Param = &new_file; - FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); - ASSERT_TRUE(new_doc); - FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); - ASSERT_TRUE(new_page); + const char md5[] = "184b67b322edaee27994b3232544b8b3"; + TestSaved(612, 792, md5); // Check that the saved document has 2 annotations on the first page - EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page)); + EXPECT_EQ(2, FPDFPage_GetAnnotCount(m_SavedPage)); // Check that the second annotation is an underline annotation and verify // its quadpoints. - FPDF_ANNOTATION new_annot = FPDFPage_GetAnnot(new_page, 1); + FPDF_ANNOTATION new_annot = FPDFPage_GetAnnot(m_SavedPage, 1); ASSERT_TRUE(new_annot); EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot)); FS_QUADPOINTSF new_quadpoints = FPDFAnnot_GetAttachmentPoints(new_annot); @@ -305,8 +297,7 @@ TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) { EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f); FPDFPage_CloseAnnot(new_annot); - FPDF_ClosePage(new_page); - FPDF_CloseDocument(new_doc); + CloseSaved(); } TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) { @@ -433,6 +424,7 @@ TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) { EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); FPDF_ClosePage(page); + // TODO(npm): TestSaved changes annot rect dimensions by 1?? // Open the saved document. std::string new_file = GetString(); FPDF_FILEACCESS file_access; @@ -545,20 +537,11 @@ TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) { FPDF_ClosePage(page); // Open the saved document. - std::string new_file = GetString(); - FPDF_FILEACCESS file_access; - memset(&file_access, 0, sizeof(file_access)); - file_access.m_FileLen = new_file.size(); - file_access.m_GetBlock = GetBlockFromString; - file_access.m_Param = &new_file; - FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); - ASSERT_TRUE(new_doc); - FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); - ASSERT_TRUE(new_page); + TestSaved(595, 842, md5_3); // Check that the saved document has a correct count of annotations and paths. - EXPECT_EQ(3, FPDFPage_GetAnnotCount(new_page)); - annot = FPDFPage_GetAnnot(new_page, 2); + EXPECT_EQ(3, FPDFPage_GetAnnotCount(m_SavedPage)); + annot = FPDFPage_GetAnnot(m_SavedPage, 2); ASSERT_TRUE(annot); EXPECT_EQ(1, FPDFAnnot_GetPathObjectCount(annot)); @@ -569,12 +552,6 @@ TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) { EXPECT_EQ(rect.right, new_rect.right); EXPECT_EQ(rect.top, new_rect.top); - // Check that the saved page renders correctly. - bitmap = RenderPageWithFlags(new_page, nullptr, FPDF_ANNOT); - CompareBitmap(bitmap, 595, 842, md5_3); - FPDFBitmap_Destroy(bitmap); - FPDFPage_CloseAnnot(annot); - FPDF_ClosePage(new_page); - FPDF_CloseDocument(new_doc); + CloseSaved(); } diff --git a/fpdfsdk/fpdfedit_embeddertest.cpp b/fpdfsdk/fpdfedit_embeddertest.cpp index c527f08168..a461f37440 100644 --- a/fpdfsdk/fpdfedit_embeddertest.cpp +++ b/fpdfsdk/fpdfedit_embeddertest.cpp @@ -23,7 +23,7 @@ #include "testing/gtest/include/gtest/gtest.h" #include "testing/test_support.h" -class FPDFEditEmbeddertest : public EmbedderTest, public TestSaver { +class FPDFEditEmbeddertest : public EmbedderTest { protected: FPDF_DOCUMENT CreateNewDocument() { document_ = FPDF_CreateNewDocument(); @@ -120,26 +120,6 @@ class FPDFEditEmbeddertest : public EmbedderTest, public TestSaver { } CPDF_Document* cpdf_doc() { return cpdf_doc_; } - void TestSaved(int width, int height, const char* md5) { - std::string new_file = GetString(); - // Read |new_file| in, and verify its rendered bitmap. - FPDF_FILEACCESS file_access; - memset(&file_access, 0, sizeof(file_access)); - file_access.m_FileLen = new_file.size(); - file_access.m_GetBlock = GetBlockFromString; - file_access.m_Param = &new_file; - - FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); - EXPECT_EQ(1, FPDF_GetPageCount(document_)); - FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); - EXPECT_NE(nullptr, new_page); - FPDF_BITMAP new_bitmap = RenderPage(new_page); - CompareBitmap(new_bitmap, width, height, md5); - FPDFBitmap_Destroy(new_bitmap); - FPDF_ClosePage(new_page); - FPDF_CloseDocument(new_doc); - } - private: CPDF_Document* cpdf_doc_; }; @@ -235,7 +215,7 @@ TEST_F(FPDFEditEmbeddertest, RasterizePDF) { // Get the generated content. Make sure it is at least as big as the original // PDF. EXPECT_GT(GetString().size(), 923U); - TestSaved(612, 792, kAllBlackMd5sum); + TestAndCloseSaved(612, 792, kAllBlackMd5sum); } TEST_F(FPDFEditEmbeddertest, AddPaths) { @@ -315,7 +295,7 @@ TEST_F(FPDFEditEmbeddertest, AddPaths) { FPDF_ClosePage(page); // Render the saved result - TestSaved(612, 792, last_md5); + TestAndCloseSaved(612, 792, last_md5); } TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) { @@ -376,50 +356,35 @@ TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) { // Now save the result, closing the page and document EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); - FPDF_ClosePage(page); + UnloadPage(page); - // Render the saved result. Not calling TestSaved because we don't want to - // close the page and document yet. - std::string new_file = GetString(); - FPDF_FILEACCESS file_access; - memset(&file_access, 0, sizeof(file_access)); - file_access.m_FileLen = new_file.size(); - file_access.m_GetBlock = GetBlockFromString; - file_access.m_Param = &new_file; - FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); - ASSERT_NE(nullptr, new_doc); - EXPECT_EQ(1, FPDF_GetPageCount(new_doc)); - FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); - ASSERT_NE(nullptr, new_page); - FPDF_BITMAP new_bitmap = RenderPage(new_page); - CompareBitmap(new_bitmap, 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073"); - FPDFBitmap_Destroy(new_bitmap); + // Render the saved result without closing the page and document + TestSaved(612, 792, "ad04e5bd0f471a9a564fb034bd0fb073"); ClearString(); // Add another opaque rectangle on top of the existing content FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50); EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255)); EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0)); - FPDFPage_InsertObject(new_page, green_rect); + FPDFPage_InsertObject(m_SavedPage, green_rect); // Add another transparent rectangle on top of existing content FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50); EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100)); EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0)); - FPDFPage_InsertObject(new_page, green_rect2); - new_bitmap = RenderPage(new_page); + FPDFPage_InsertObject(m_SavedPage, green_rect2); + FPDF_BITMAP new_bitmap = RenderPageWithFlags(m_SavedPage, m_SavedForm, 0); const char last_md5[] = "4b5b00f824620f8c9b8801ebb98e1cdd"; CompareBitmap(new_bitmap, 612, 792, last_md5); FPDFBitmap_Destroy(new_bitmap); - EXPECT_TRUE(FPDFPage_GenerateContent(new_page)); + EXPECT_TRUE(FPDFPage_GenerateContent(m_SavedPage)); // Now save the result, closing the page and document - EXPECT_TRUE(FPDF_SaveAsCopy(new_doc, this, 0)); - FPDF_ClosePage(new_page); - FPDF_CloseDocument(new_doc); + EXPECT_TRUE(FPDF_SaveAsCopy(m_SavedDocument, this, 0)); + CloseSaved(); // Render the saved result - TestSaved(612, 792, last_md5); + TestAndCloseSaved(612, 792, last_md5); } TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) { @@ -852,7 +817,7 @@ TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) { EXPECT_TRUE(FPDFPage_GenerateContent(page)); EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); FPDF_ClosePage(page); - TestSaved(612, 792, md5_2); + TestAndCloseSaved(612, 792, md5_2); } TEST_F(FPDFEditEmbeddertest, TransformAnnot) { @@ -926,7 +891,7 @@ TEST_F(FPDFEditEmbeddertest, AddCIDFontText) { EXPECT_TRUE(FPDFPage_GenerateContent(page)); EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); FPDF_ClosePage(page); - TestSaved(612, 792, md5); + TestAndCloseSaved(612, 792, md5); } #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ @@ -958,5 +923,5 @@ TEST_F(FPDFEditEmbeddertest, SaveAndRender) { EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); UnloadPage(page); } - TestSaved(612, 792, md5); + TestAndCloseSaved(612, 792, md5); } diff --git a/fpdfsdk/fpdfformfill.cpp b/fpdfsdk/fpdfformfill.cpp index 12feb791be..6f63f0e2fa 100644 --- a/fpdfsdk/fpdfformfill.cpp +++ b/fpdfsdk/fpdfformfill.cpp @@ -278,7 +278,6 @@ FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle) { if (pFormFillEnv->GetXFAContext()) pFormFillEnv->GetXFAContext()->SetFormFillEnv(nullptr); #endif // PDF_ENABLE_XFA - delete pFormFillEnv; } diff --git a/fpdfsdk/fpdfformfill_embeddertest.cpp b/fpdfsdk/fpdfformfill_embeddertest.cpp index 7779ed25a7..83e560a60c 100644 --- a/fpdfsdk/fpdfformfill_embeddertest.cpp +++ b/fpdfsdk/fpdfformfill_embeddertest.cpp @@ -20,7 +20,7 @@ using testing::_; using testing::Return; -class FPDFFormFillEmbeddertest : public EmbedderTest, public TestSaver { +class FPDFFormFillEmbeddertest : public EmbedderTest { protected: void TypeTextIntoTextfield(FPDF_PAGE page, int num_chars, @@ -366,26 +366,11 @@ TEST_F(FPDFFormFillEmbeddertest, FormText) { EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); - // Close everything + // Close page UnloadPage(page); - FPDFDOC_ExitFormFillEnvironment(form_handle_); - FPDF_CloseDocument(document_); } // Check saved document - std::string new_file = GetString(); - FPDF_FILEACCESS file_access; - memset(&file_access, 0, sizeof(file_access)); - file_access.m_FileLen = new_file.size(); - file_access.m_GetBlock = GetBlockFromString; - file_access.m_Param = &new_file; - document_ = FPDF_LoadCustomDocument(&file_access, nullptr); - SetupFormFillEnvironment(); - EXPECT_EQ(1, FPDF_GetPageCount(document_)); - std::unique_ptr new_page(FPDF_LoadPage(document_, 0)); - ASSERT_TRUE(new_page.get()); - std::unique_ptr new_bitmap( - RenderPage(new_page.get())); - CompareBitmap(new_bitmap.get(), 300, 300, md5_3); + TestAndCloseSaved(300, 300, md5_3); } TEST_F(FPDFFormFillEmbeddertest, GetSelectedTextEmptyAndBasicKeyboard) { diff --git a/fpdfsdk/fpdfppo_embeddertest.cpp b/fpdfsdk/fpdfppo_embeddertest.cpp index 7e6ff33fc3..b6b9167f10 100644 --- a/fpdfsdk/fpdfppo_embeddertest.cpp +++ b/fpdfsdk/fpdfppo_embeddertest.cpp @@ -6,6 +6,7 @@ #include "core/fxcrt/fx_basic.h" #include "public/fpdf_edit.h" #include "public/fpdf_ppo.h" +#include "public/fpdf_save.h" #include "public/fpdfview.h" #include "testing/embedder_test.h" #include "testing/gtest/include/gtest/gtest.h" diff --git a/fpdfsdk/fpdfsave_embeddertest.cpp b/fpdfsdk/fpdfsave_embeddertest.cpp index e5d05c98df..260fe0c2a6 100644 --- a/fpdfsdk/fpdfsave_embeddertest.cpp +++ b/fpdfsdk/fpdfsave_embeddertest.cpp @@ -15,7 +15,7 @@ #include "testing/gtest/include/gtest/gtest.h" #include "testing/test_support.h" -class FPDFSaveEmbedderTest : public EmbedderTest, public TestSaver {}; +class FPDFSaveEmbedderTest : public EmbedderTest {}; TEST_F(FPDFSaveEmbedderTest, SaveSimpleDoc) { EXPECT_TRUE(OpenDocument("hello_world.pdf")); 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(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(pFileWrite); + pThis->m_String.append(static_cast(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(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 default_delegate_; FPDF_DOCUMENT document_; @@ -142,6 +156,9 @@ class EmbedderTest : public ::testing::Test, std::unique_ptr file_contents_; std::map page_map_; std::map 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(pFileWrite); - pThis->m_String.append(static_cast(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(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 #include -#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_ -- cgit v1.2.3