Index: fpdfsdk/src/fpdfsave_embeddertest.cpp |
diff --git a/fpdfsdk/src/fpdfsave_embeddertest.cpp b/fpdfsdk/src/fpdfsave_embeddertest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c3e04f172bccd88400472a5bb647a602c81722ec |
--- /dev/null |
+++ b/fpdfsdk/src/fpdfsave_embeddertest.cpp |
@@ -0,0 +1,153 @@ |
+// Copyright 2015 PDFium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <memory> |
+#include <vector> |
+ |
+#include "public/fpdf_doc.h" |
+#include "public/fpdf_edit.h" |
+#include "public/fpdf_flatten.h" |
+#include "public/fpdf_ppo.h" |
+#include "public/fpdf_save.h" |
+#include "testing/embedder_test.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "testing/test_support.h" |
+ |
+namespace { |
+ |
+class MemBufferFileWrite : public FPDF_FILEWRITE { |
+ public: |
+ MemBufferFileWrite() { |
+ version = 1; |
+ WriteBlock = &WriteBlockImpl; |
+ } |
+ |
+ ~MemBufferFileWrite() {} |
+ |
+ const char* buffer() const { return buffer_.data(); } |
+ size_t size() const { return buffer_.size(); } |
+ |
+ private: |
+ int DoWriteBlock(const void* data, unsigned long size) { |
+ void* dest; |
+ if (buffer_.empty()) { |
+ buffer_.resize(size); |
+ dest = buffer_.data(); |
+ } else { |
+ buffer_.resize(buffer_.size() + size); |
Wei Li
2016/01/04 19:01:13
You may use these when buffer_ is empty as well.
|
+ dest = buffer_.data() + buffer_.size() - size; |
+ } |
+ memcpy(dest, data, size); |
+ return 1; |
+ } |
+ |
+ static int WriteBlockImpl(FPDF_FILEWRITE* this_file_write, |
+ const void* data, |
+ unsigned long size) { |
+ MemBufferFileWrite* mem_buffer_file_write = |
+ static_cast<MemBufferFileWrite*>(this_file_write); |
+ return mem_buffer_file_write->DoWriteBlock(data, size); |
+ } |
+ |
+ std::vector<char> buffer_; |
Tom Sepez
2016/01/04 20:40:22
meh, might be happier if this were a string, so th
|
+}; |
+ |
+} // namespace |
+ |
+class FPDFDocSaveTest : public EmbedderTest { |
+ protected: |
+ FPDFDocSaveTest() |
+ : new_document_(nullptr), new_avail_(nullptr), new_form_handle_(nullptr) { |
+ memset(&new_hints_, 0, sizeof(new_hints_)); |
+ memset(&new_file_access_, 0, sizeof(new_file_access_)); |
+ memset(&new_file_avail_, 0, sizeof(new_file_avail_)); |
+ } |
+ |
+ ~FPDFDocSaveTest() {} |
+ |
+ void TearDown() override { |
+ FORM_DoDocumentAAction(new_form_handle_, FPDFDOC_AACTION_WC); |
+ FPDFDOC_ExitFormFillEnvironment(new_form_handle_); |
+ FPDF_CloseDocument(new_document_); |
+ FPDFAvail_Destroy(new_avail_); |
+ EmbedderTest::TearDown(); |
+ } |
+ |
+ FPDF_DOCUMENT new_document() { return new_document_; } |
+ |
+ bool LoadDocumentInBuffer(const MemBufferFileWrite& output_writer) { |
+ new_loader_.reset( |
+ new TestLoader(output_writer.buffer(), output_writer.size())); |
+ EmbedderTest::LoadDocumentData load_data; |
+ load_data.document = &new_document_; |
+ load_data.file_access = &new_file_access_; |
+ load_data.file_avail = &new_file_avail_; |
+ load_data.avail = &new_avail_; |
+ load_data.hints = &new_hints_; |
+ load_data.form_handle = &new_form_handle_; |
+ return LoadDocument(output_writer.buffer(), output_writer.size(), false, |
+ new_loader_.get(), nullptr, &load_data); |
+ } |
+ |
+ void LoadAndCheckPages(FPDF_DOCUMENT doc, int page_count) { |
+ for (int i = 0; i < page_count; ++i) { |
+ FPDF_PAGE page = FPDF_LoadPage(doc, 0); |
+ ASSERT_TRUE(page); |
+ FPDF_ClosePage(page); |
+ } |
+ } |
+ |
+ private: |
+ std::unique_ptr<TestLoader> new_loader_; |
+ FPDF_DOCUMENT new_document_; |
+ FPDF_FILEACCESS new_file_access_; |
+ FX_FILEAVAIL new_file_avail_; |
+ FPDF_AVAIL new_avail_; |
+ FX_DOWNLOADHINTS new_hints_; |
+ FPDF_FORMHANDLE new_form_handle_; |
+}; |
+ |
+TEST_F(FPDFDocSaveTest, SaveAsCopy) { |
+ EXPECT_TRUE(OpenDocument("weblinks.pdf")); |
+ int page_count = FPDF_GetPageCount(document()); |
+ EXPECT_EQ(1, page_count); |
+ LoadAndCheckPages(document(), page_count); |
+ |
+ MemBufferFileWrite output_writer; |
+ ASSERT_TRUE(FPDF_SaveAsCopy(document(), &output_writer, 0)); |
+ |
+ ASSERT_TRUE(LoadDocumentInBuffer(output_writer)); |
+ int new_page_count = FPDF_GetPageCount(new_document()); |
+ EXPECT_EQ(page_count, new_page_count); |
+ LoadAndCheckPages(new_document(), new_page_count); |
+} |
+ |
+TEST_F(FPDFDocSaveTest, SaveAsCopyFlatten) { |
+ EXPECT_TRUE(OpenDocument("weblinks.pdf")); |
+ int page_count = FPDF_GetPageCount(document()); |
+ EXPECT_EQ(1, page_count); |
+ LoadAndCheckPages(document(), page_count); |
+ |
+ FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument(); |
+ ASSERT_TRUE(output_doc); |
+ EXPECT_FALSE(FPDF_CopyViewerPreferences(output_doc, document())); |
+ ASSERT_TRUE(FPDF_ImportPages(output_doc, document(), nullptr, 0)); |
+ int output_page_count = FPDF_GetPageCount(output_doc); |
+ EXPECT_EQ(page_count, output_page_count); |
+ for (int i = 0; i < output_page_count; ++i) { |
+ FPDF_PAGE page = FPDF_LoadPage(output_doc, 0); |
+ ASSERT_TRUE(page); |
+ ASSERT_NE(FLATTEN_FAIL, FPDFPage_Flatten(page, FLAT_PRINT)); |
+ FPDF_ClosePage(page); |
+ } |
+ |
+ MemBufferFileWrite output_writer; |
+ ASSERT_TRUE(FPDF_SaveAsCopy(output_doc, &output_writer, 0)); |
+ FPDF_CloseDocument(output_doc); |
+ |
+ ASSERT_TRUE(LoadDocumentInBuffer(output_writer)); |
+ int new_page_count = FPDF_GetPageCount(new_document()); |
+ EXPECT_EQ(page_count, new_page_count); |
+ LoadAndCheckPages(new_document(), new_page_count); |
+} |