Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 PDFium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <memory> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "public/fpdf_doc.h" | |
| 9 #include "public/fpdf_edit.h" | |
| 10 #include "public/fpdf_flatten.h" | |
| 11 #include "public/fpdf_ppo.h" | |
| 12 #include "public/fpdf_save.h" | |
| 13 #include "testing/embedder_test.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 #include "testing/test_support.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 class MemBufferFileWrite : public FPDF_FILEWRITE { | |
| 20 public: | |
| 21 MemBufferFileWrite() { | |
| 22 version = 1; | |
| 23 WriteBlock = &WriteBlockImpl; | |
| 24 } | |
| 25 | |
| 26 ~MemBufferFileWrite() {} | |
| 27 | |
| 28 const char* buffer() const { return buffer_.data(); } | |
| 29 size_t size() const { return buffer_.size(); } | |
| 30 | |
| 31 private: | |
| 32 int DoWriteBlock(const void* data, unsigned long size) { | |
| 33 void* dest; | |
| 34 if (buffer_.empty()) { | |
| 35 buffer_.resize(size); | |
| 36 dest = buffer_.data(); | |
| 37 } else { | |
| 38 buffer_.resize(buffer_.size() + size); | |
|
Wei Li
2016/01/04 19:01:13
You may use these when buffer_ is empty as well.
| |
| 39 dest = buffer_.data() + buffer_.size() - size; | |
| 40 } | |
| 41 memcpy(dest, data, size); | |
| 42 return 1; | |
| 43 } | |
| 44 | |
| 45 static int WriteBlockImpl(FPDF_FILEWRITE* this_file_write, | |
| 46 const void* data, | |
| 47 unsigned long size) { | |
| 48 MemBufferFileWrite* mem_buffer_file_write = | |
| 49 static_cast<MemBufferFileWrite*>(this_file_write); | |
| 50 return mem_buffer_file_write->DoWriteBlock(data, size); | |
| 51 } | |
| 52 | |
| 53 std::vector<char> buffer_; | |
|
Tom Sepez
2016/01/04 20:40:22
meh, might be happier if this were a string, so th
| |
| 54 }; | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 class FPDFDocSaveTest : public EmbedderTest { | |
| 59 protected: | |
| 60 FPDFDocSaveTest() | |
| 61 : new_document_(nullptr), new_avail_(nullptr), new_form_handle_(nullptr) { | |
| 62 memset(&new_hints_, 0, sizeof(new_hints_)); | |
| 63 memset(&new_file_access_, 0, sizeof(new_file_access_)); | |
| 64 memset(&new_file_avail_, 0, sizeof(new_file_avail_)); | |
| 65 } | |
| 66 | |
| 67 ~FPDFDocSaveTest() {} | |
| 68 | |
| 69 void TearDown() override { | |
| 70 FORM_DoDocumentAAction(new_form_handle_, FPDFDOC_AACTION_WC); | |
| 71 FPDFDOC_ExitFormFillEnvironment(new_form_handle_); | |
| 72 FPDF_CloseDocument(new_document_); | |
| 73 FPDFAvail_Destroy(new_avail_); | |
| 74 EmbedderTest::TearDown(); | |
| 75 } | |
| 76 | |
| 77 FPDF_DOCUMENT new_document() { return new_document_; } | |
| 78 | |
| 79 bool LoadDocumentInBuffer(const MemBufferFileWrite& output_writer) { | |
| 80 new_loader_.reset( | |
| 81 new TestLoader(output_writer.buffer(), output_writer.size())); | |
| 82 EmbedderTest::LoadDocumentData load_data; | |
| 83 load_data.document = &new_document_; | |
| 84 load_data.file_access = &new_file_access_; | |
| 85 load_data.file_avail = &new_file_avail_; | |
| 86 load_data.avail = &new_avail_; | |
| 87 load_data.hints = &new_hints_; | |
| 88 load_data.form_handle = &new_form_handle_; | |
| 89 return LoadDocument(output_writer.buffer(), output_writer.size(), false, | |
| 90 new_loader_.get(), nullptr, &load_data); | |
| 91 } | |
| 92 | |
| 93 void LoadAndCheckPages(FPDF_DOCUMENT doc, int page_count) { | |
| 94 for (int i = 0; i < page_count; ++i) { | |
| 95 FPDF_PAGE page = FPDF_LoadPage(doc, 0); | |
| 96 ASSERT_TRUE(page); | |
| 97 FPDF_ClosePage(page); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 private: | |
| 102 std::unique_ptr<TestLoader> new_loader_; | |
| 103 FPDF_DOCUMENT new_document_; | |
| 104 FPDF_FILEACCESS new_file_access_; | |
| 105 FX_FILEAVAIL new_file_avail_; | |
| 106 FPDF_AVAIL new_avail_; | |
| 107 FX_DOWNLOADHINTS new_hints_; | |
| 108 FPDF_FORMHANDLE new_form_handle_; | |
| 109 }; | |
| 110 | |
| 111 TEST_F(FPDFDocSaveTest, SaveAsCopy) { | |
| 112 EXPECT_TRUE(OpenDocument("weblinks.pdf")); | |
| 113 int page_count = FPDF_GetPageCount(document()); | |
| 114 EXPECT_EQ(1, page_count); | |
| 115 LoadAndCheckPages(document(), page_count); | |
| 116 | |
| 117 MemBufferFileWrite output_writer; | |
| 118 ASSERT_TRUE(FPDF_SaveAsCopy(document(), &output_writer, 0)); | |
| 119 | |
| 120 ASSERT_TRUE(LoadDocumentInBuffer(output_writer)); | |
| 121 int new_page_count = FPDF_GetPageCount(new_document()); | |
| 122 EXPECT_EQ(page_count, new_page_count); | |
| 123 LoadAndCheckPages(new_document(), new_page_count); | |
| 124 } | |
| 125 | |
| 126 TEST_F(FPDFDocSaveTest, SaveAsCopyFlatten) { | |
| 127 EXPECT_TRUE(OpenDocument("weblinks.pdf")); | |
| 128 int page_count = FPDF_GetPageCount(document()); | |
| 129 EXPECT_EQ(1, page_count); | |
| 130 LoadAndCheckPages(document(), page_count); | |
| 131 | |
| 132 FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument(); | |
| 133 ASSERT_TRUE(output_doc); | |
| 134 EXPECT_FALSE(FPDF_CopyViewerPreferences(output_doc, document())); | |
| 135 ASSERT_TRUE(FPDF_ImportPages(output_doc, document(), nullptr, 0)); | |
| 136 int output_page_count = FPDF_GetPageCount(output_doc); | |
| 137 EXPECT_EQ(page_count, output_page_count); | |
| 138 for (int i = 0; i < output_page_count; ++i) { | |
| 139 FPDF_PAGE page = FPDF_LoadPage(output_doc, 0); | |
| 140 ASSERT_TRUE(page); | |
| 141 ASSERT_NE(FLATTEN_FAIL, FPDFPage_Flatten(page, FLAT_PRINT)); | |
| 142 FPDF_ClosePage(page); | |
| 143 } | |
| 144 | |
| 145 MemBufferFileWrite output_writer; | |
| 146 ASSERT_TRUE(FPDF_SaveAsCopy(output_doc, &output_writer, 0)); | |
| 147 FPDF_CloseDocument(output_doc); | |
| 148 | |
| 149 ASSERT_TRUE(LoadDocumentInBuffer(output_writer)); | |
| 150 int new_page_count = FPDF_GetPageCount(new_document()); | |
| 151 EXPECT_EQ(page_count, new_page_count); | |
| 152 LoadAndCheckPages(new_document(), new_page_count); | |
| 153 } | |
| OLD | NEW |