fpdfedit_embeddertest.cpp revision 4d3acf4ec42bf6e838f9060103aff98fbf170794
1// Copyright 2016 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 <string> 7 8#include "public/fpdf_edit.h" 9#include "public/fpdfview.h" 10#include "testing/embedder_test.h" 11#include "testing/gmock/include/gmock/gmock-matchers.h" 12#include "testing/gtest/include/gtest/gtest.h" 13#include "testing/test_support.h" 14 15class FPDFEditEmbeddertest : public EmbedderTest, public TestSaver {}; 16 17namespace { 18 19const char kExpectedPDF[] = 20 "%PDF-1.7\r\n" 21 "%\xA1\xB3\xC5\xD7\r\n" 22 "1 0 obj\r\n" 23 "<</Pages 2 0 R /Type/Catalog>>\r\n" 24 "endobj\r\n" 25 "2 0 obj\r\n" 26 "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n" 27 "endobj\r\n" 28 "3 0 obj\r\n" 29 "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n" 30 "endobj\r\n" 31 "4 0 obj\r\n" 32 "<</Contents 5 0 R /MediaBox\\[ 0 0 640 480\\]" 33 "/Parent 2 0 R /Resources<<>>/Rotate 0/Type/Page" 34 ">>\r\n" 35 "endobj\r\n" 36 "5 0 obj\r\n" 37 "<</Filter/FlateDecode/Length 8>>stream\r\n" 38 // Character '_' is matching '\0' (see comment below). 39 "x\x9C\x3____\x1\r\n" 40 "endstream\r\n" 41 "endobj\r\n" 42 "xref\r\n" 43 "0 6\r\n" 44 "0000000000 65535 f\r\n" 45 "0000000017 00000 n\r\n" 46 "0000000066 00000 n\r\n" 47 "0000000122 00000 n\r\n" 48 "0000000192 00000 n\r\n" 49 "0000000301 00000 n\r\n" 50 "trailer\r\n" 51 "<<\r\n" 52 "/Root 1 0 R\r\n" 53 "/Info 3 0 R\r\n" 54 "/Size 6/ID\\[<.*><.*>\\]>>\r\n" 55 "startxref\r\n" 56 "379\r\n" 57 "%%EOF\r\n"; 58 59int GetBlockFromString(void* param, 60 unsigned long pos, 61 unsigned char* buf, 62 unsigned long size) { 63 std::string* new_file = static_cast<std::string*>(param); 64 if (!new_file || pos + size < pos) 65 return 0; 66 67 unsigned long file_size = new_file->size(); 68 if (pos + size > file_size) 69 return 0; 70 71 memcpy(buf, new_file->data() + pos, size); 72 return 1; 73} 74 75} // namespace 76 77TEST_F(FPDFEditEmbeddertest, EmptyCreation) { 78 EXPECT_TRUE(CreateEmptyDocument()); 79 FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0); 80 EXPECT_NE(nullptr, page); 81 EXPECT_TRUE(FPDFPage_GenerateContent(page)); 82 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); 83 84 // The MatchesRegexp doesn't support embedded NUL ('\0') characters. They are 85 // replaced by '_' for the purpose of the test. 86 std::string result = GetString(); 87 std::replace(result.begin(), result.end(), '\0', '_'); 88 EXPECT_THAT(result, testing::MatchesRegex( 89 std::string(kExpectedPDF, sizeof(kExpectedPDF)))); 90 FPDF_ClosePage(page); 91} 92 93// Regression test for https://crbug.com/667012 94TEST_F(FPDFEditEmbeddertest, RasterizePDF) { 95 const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615"; 96 97 // Get the bitmap for the original document/ 98 FPDF_BITMAP orig_bitmap; 99 { 100 EXPECT_TRUE(OpenDocument("black.pdf")); 101 FPDF_PAGE orig_page = LoadPage(0); 102 EXPECT_NE(nullptr, orig_page); 103 orig_bitmap = RenderPage(orig_page); 104 CompareBitmap(orig_bitmap, 612, 792, kAllBlackMd5sum); 105 UnloadPage(orig_page); 106 } 107 108 // Create a new document from |orig_bitmap| and save it. 109 { 110 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); 111 FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792); 112 113 // Add the bitmap to an image object and add the image object to the output 114 // page. 115 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_doc); 116 EXPECT_TRUE(FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap)); 117 EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0)); 118 FPDFPage_InsertObject(temp_page, temp_img); 119 EXPECT_TRUE(FPDFPage_GenerateContent(temp_page)); 120 EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0)); 121 FPDF_ClosePage(temp_page); 122 FPDF_CloseDocument(temp_doc); 123 } 124 FPDFBitmap_Destroy(orig_bitmap); 125 126 // Get the generated content. Make sure it is at least as big as the original 127 // PDF. 128 std::string new_file = GetString(); 129 EXPECT_GT(new_file.size(), 923U); 130 131 // Read |new_file| in, and verify its rendered bitmap. 132 { 133 FPDF_FILEACCESS file_access; 134 memset(&file_access, 0, sizeof(file_access)); 135 file_access.m_FileLen = new_file.size(); 136 file_access.m_GetBlock = GetBlockFromString; 137 file_access.m_Param = &new_file; 138 139 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); 140 EXPECT_EQ(1, FPDF_GetPageCount(document_)); 141 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); 142 EXPECT_NE(nullptr, new_page); 143 int width = static_cast<int>(FPDF_GetPageWidth(new_page)); 144 int height = static_cast<int>(FPDF_GetPageHeight(new_page)); 145 int alpha = FPDFPage_HasTransparency(new_page) ? 1 : 0; 146 FPDF_BITMAP new_bitmap = FPDFBitmap_Create(width, height, alpha); 147 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF; 148 FPDFBitmap_FillRect(new_bitmap, 0, 0, width, height, fill_color); 149 FPDF_RenderPageBitmap(new_bitmap, new_page, 0, 0, width, height, 0, 0); 150 CompareBitmap(new_bitmap, 612, 792, kAllBlackMd5sum); 151 FPDF_ClosePage(new_page); 152 FPDF_CloseDocument(new_doc); 153 FPDFBitmap_Destroy(new_bitmap); 154 } 155} 156