blob: e9e94caa1322cbf8009c6dfdc3747dbfacdc4590 [file] [log] [blame]
Tom Sepez9eb811f2016-01-05 14:48:31 -08001// 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
Lei Zhanga98e3662018-02-07 20:28:35 +00005#include <memory>
Henrique Nakashima9fa50362017-11-10 22:40:44 +00006#include <string>
Tom Sepez9eb811f2016-01-05 14:48:31 -08007
dsinclaira52ab742016-09-29 13:59:29 -07008#include "core/fxcrt/fx_string.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +00009#include "public/cpp/fpdf_scopers.h"
tsepez6e1d6032016-11-11 17:55:40 -080010#include "public/fpdf_edit.h"
11#include "public/fpdf_ppo.h"
Lei Zhanga98e3662018-02-07 20:28:35 +000012#include "public/fpdf_save.h"
Tom Sepez9eb811f2016-01-05 14:48:31 -080013#include "public/fpdfview.h"
14#include "testing/embedder_test.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080015#include "testing/gmock/include/gmock/gmock-matchers.h"
Tom Sepez9eb811f2016-01-05 14:48:31 -080016#include "testing/gtest/include/gtest/gtest.h"
17
Nicolas Pena3ff54002017-07-05 11:55:35 -040018class FPDFSaveEmbedderTest : public EmbedderTest {};
Tom Sepez9eb811f2016-01-05 14:48:31 -080019
20TEST_F(FPDFSaveEmbedderTest, SaveSimpleDoc) {
21 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepez0aec19b2016-01-07 12:22:44 -080022 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
23 EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
Artem Strygind24b97e2017-08-09 18:50:59 +030024 EXPECT_EQ(805u, GetString().length());
Tom Sepez9eb811f2016-01-05 14:48:31 -080025}
26
27TEST_F(FPDFSaveEmbedderTest, SaveSimpleDocWithVersion) {
28 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepez0aec19b2016-01-07 12:22:44 -080029 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 14));
30 EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.4\r\n"));
Artem Strygind24b97e2017-08-09 18:50:59 +030031 EXPECT_EQ(805u, GetString().length());
Tom Sepez9eb811f2016-01-05 14:48:31 -080032}
Tom Sepez9eb811f2016-01-05 14:48:31 -080033TEST_F(FPDFSaveEmbedderTest, SaveSimpleDocWithBadVersion) {
34 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepez0aec19b2016-01-07 12:22:44 -080035 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"));
Tom Sepez9eb811f2016-01-05 14:48:31 -080045}
46
tsepez6e1d6032016-11-11 17:55:40 -080047TEST_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
Henrique Nakashima9fa50362017-11-10 22:40:44 +000062TEST_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);
Lei Zhang6183a6e2018-02-07 23:00:07 +000069 ASSERT_TRUE(page);
Tom Sepeze08d2b12018-04-25 18:49:32 +000070 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Lei Zhang6183a6e2018-02-07 23:00:07 +000071 EXPECT_EQ(612, FPDFBitmap_GetWidth(bitmap.get()));
72 EXPECT_EQ(792, FPDFBitmap_GetHeight(bitmap.get()));
73 original_md5[i] = HashBitmap(bitmap.get());
Henrique Nakashima9fa50362017-11-10 22:40:44 +000074 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.
Lei Zhang0b494052019-01-31 21:41:15 +000084 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima9fa50362017-11-10 22:40:44 +000085 for (int i = 0; i < kPageCount; ++i) {
86 FPDF_PAGE page = LoadSavedPage(i);
Lei Zhanga98e3662018-02-07 20:28:35 +000087 ASSERT_TRUE(page);
Tom Sepeze08d2b12018-04-25 18:49:32 +000088 ScopedFPDFBitmap bitmap = RenderSavedPage(page);
Lei Zhanga98e3662018-02-07 20:28:35 +000089 EXPECT_EQ(original_md5[i], HashBitmap(bitmap.get()));
Henrique Nakashima9fa50362017-11-10 22:40:44 +000090 CloseSavedPage(page);
91 }
92 CloseSavedDocument();
93}
94
Tom Sepez9eb811f2016-01-05 14:48:31 -080095TEST_F(FPDFSaveEmbedderTest, BUG_342) {
96 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepez0aec19b2016-01-07 12:22:44 -080097 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")));
Tom Sepez9eb811f2016-01-05 14:48:31 -0800101}
Lei Zhangc1600692018-11-14 23:12:58 +0000102
103TEST_F(FPDFSaveEmbedderTest, BUG_905142) {
104 EXPECT_TRUE(OpenDocument("bug_905142.pdf"));
105 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
106 EXPECT_THAT(GetString(), testing::HasSubstr("/Length 0"));
107}