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.h>
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/fx_string_testhelpers.h"
15 #include "testing/gmock/include/gmock/gmock-matchers.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/test_support.h"
18
19 class FPDFSaveEmbedderTest : public EmbedderTest, public TestSaver {};
20
TEST_F(FPDFSaveEmbedderTest,SaveSimpleDoc)21 TEST_F(FPDFSaveEmbedderTest, SaveSimpleDoc) {
22 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
23 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
24 EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
25 EXPECT_EQ(843u, GetString().length());
26 }
27
TEST_F(FPDFSaveEmbedderTest,SaveSimpleDocWithVersion)28 TEST_F(FPDFSaveEmbedderTest, SaveSimpleDocWithVersion) {
29 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
30 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 14));
31 EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.4\r\n"));
32 EXPECT_EQ(843u, GetString().length());
33 }
34
TEST_F(FPDFSaveEmbedderTest,SaveSimpleDocWithBadVersion)35 TEST_F(FPDFSaveEmbedderTest, SaveSimpleDocWithBadVersion) {
36 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
37 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, -1));
38 EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
39
40 ClearString();
41 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 0));
42 EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
43
44 ClearString();
45 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 18));
46 EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
47 }
48
TEST_F(FPDFSaveEmbedderTest,SaveCopiedDoc)49 TEST_F(FPDFSaveEmbedderTest, SaveCopiedDoc) {
50 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
51
52 FPDF_PAGE page = LoadPage(0);
53 EXPECT_TRUE(page);
54
55 FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
56 EXPECT_TRUE(output_doc);
57 EXPECT_TRUE(FPDF_ImportPages(output_doc, document(), "1", 0));
58 EXPECT_TRUE(FPDF_SaveAsCopy(output_doc, this, 0));
59 FPDF_CloseDocument(output_doc);
60
61 UnloadPage(page);
62 }
63
TEST_F(FPDFSaveEmbedderTest,BUG_342)64 TEST_F(FPDFSaveEmbedderTest, BUG_342) {
65 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
66 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
67 EXPECT_THAT(GetString(), testing::HasSubstr("0000000000 65535 f\r\n"));
68 EXPECT_THAT(GetString(),
69 testing::Not(testing::HasSubstr("0000000000 65536 f\r\n")));
70 }
71