Home | History | Annotate | Download | only in fpdfsdk
      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 "public/fpdf_save.h"
      6 
      7 #include <string>
      8 
      9 #include "core/fxcrt/fx_string.h"
     10 #include "public/fpdf_edit.h"
     11 #include "public/fpdf_ppo.h"
     12 #include "public/fpdfview.h"
     13 #include "testing/embedder_test.h"
     14 #include "testing/gmock/include/gmock/gmock-matchers.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 #include "testing/test_support.h"
     17 
     18 class FPDFSaveEmbedderTest : public EmbedderTest {};
     19 
     20 TEST_F(FPDFSaveEmbedderTest, SaveSimpleDoc) {
     21   EXPECT_TRUE(OpenDocument("hello_world.pdf"));
     22   EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
     23   EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
     24   EXPECT_EQ(805u, GetString().length());
     25 }
     26 
     27 TEST_F(FPDFSaveEmbedderTest, SaveSimpleDocWithVersion) {
     28   EXPECT_TRUE(OpenDocument("hello_world.pdf"));
     29   EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 14));
     30   EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.4\r\n"));
     31   EXPECT_EQ(805u, GetString().length());
     32 }
     33 TEST_F(FPDFSaveEmbedderTest, SaveSimpleDocWithBadVersion) {
     34   EXPECT_TRUE(OpenDocument("hello_world.pdf"));
     35   EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, -1));
     36   EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
     37 
     38   ClearString();
     39   EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 0));
     40   EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
     41 
     42   ClearString();
     43   EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 18));
     44   EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
     45 }
     46 
     47 TEST_F(FPDFSaveEmbedderTest, SaveCopiedDoc) {
     48   EXPECT_TRUE(OpenDocument("hello_world.pdf"));
     49 
     50   FPDF_PAGE page = LoadPage(0);
     51   EXPECT_TRUE(page);
     52 
     53   FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
     54   EXPECT_TRUE(output_doc);
     55   EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "1", 0));
     56   EXPECT_TRUE(FPDF_SaveAsCopy(output_doc, this, 0));
     57   FPDF_CloseDocument(output_doc);
     58 
     59   UnloadPage(page);
     60 }
     61 
     62 TEST_F(FPDFSaveEmbedderTest, SaveLinearizedDoc) {
     63   const int kPageCount = 3;
     64   std::string original_md5[kPageCount];
     65 
     66   EXPECT_TRUE(OpenDocument("linearized.pdf"));
     67   for (int i = 0; i < kPageCount; ++i) {
     68     FPDF_PAGE page = LoadPage(i);
     69     FPDF_BITMAP bitmap = RenderPage(page);
     70     EXPECT_EQ(612, FPDFBitmap_GetWidth(bitmap));
     71     EXPECT_EQ(792, FPDFBitmap_GetHeight(bitmap));
     72     original_md5[i] = HashBitmap(bitmap);
     73     FPDFBitmap_Destroy(bitmap);
     74     UnloadPage(page);
     75   }
     76 
     77   EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
     78   EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.6\r\n"));
     79   EXPECT_THAT(GetString(), testing::HasSubstr("/Root "));
     80   EXPECT_THAT(GetString(), testing::HasSubstr("/Info "));
     81   EXPECT_EQ(8219u, GetString().length());
     82 
     83   // Make sure new document renders the same as the old one.
     84   EXPECT_TRUE(OpenSavedDocument());
     85   for (int i = 0; i < kPageCount; ++i) {
     86     FPDF_PAGE page = LoadSavedPage(i);
     87     FPDF_BITMAP bitmap = RenderSavedPage(page);
     88     EXPECT_EQ(original_md5[i], HashBitmap(bitmap));
     89     FPDFBitmap_Destroy(bitmap);
     90     CloseSavedPage(page);
     91   }
     92   CloseSavedDocument();
     93 }
     94 
     95 TEST_F(FPDFSaveEmbedderTest, BUG_342) {
     96   EXPECT_TRUE(OpenDocument("hello_world.pdf"));
     97   EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
     98   EXPECT_THAT(GetString(), testing::HasSubstr("0000000000 65535 f\r\n"));
     99   EXPECT_THAT(GetString(),
    100               testing::Not(testing::HasSubstr("0000000000 65536 f\r\n")));
    101 }
    102