blob: 260fe0c2a6995a3fb22b8e75ae6d451e05845250 [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
tsepez6e1d6032016-11-11 17:55:40 -08005#include "public/fpdf_save.h"
6
Tom Sepez9eb811f2016-01-05 14:48:31 -08007#include <string.h>
8
dsinclaira52ab742016-09-29 13:59:29 -07009#include "core/fxcrt/fx_string.h"
tsepez6e1d6032016-11-11 17:55:40 -080010#include "public/fpdf_edit.h"
11#include "public/fpdf_ppo.h"
Tom Sepez9eb811f2016-01-05 14:48:31 -080012#include "public/fpdfview.h"
13#include "testing/embedder_test.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080014#include "testing/gmock/include/gmock/gmock-matchers.h"
Tom Sepez9eb811f2016-01-05 14:48:31 -080015#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080016#include "testing/test_support.h"
Tom Sepez9eb811f2016-01-05 14:48:31 -080017
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"));
Wei Li05d53f02016-03-29 16:42:53 -070024 EXPECT_EQ(843u, 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"));
Wei Li05d53f02016-03-29 16:42:53 -070031 EXPECT_EQ(843u, 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
Tom Sepez9eb811f2016-01-05 14:48:31 -080062TEST_F(FPDFSaveEmbedderTest, BUG_342) {
63 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepez0aec19b2016-01-07 12:22:44 -080064 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
65 EXPECT_THAT(GetString(), testing::HasSubstr("0000000000 65535 f\r\n"));
66 EXPECT_THAT(GetString(),
67 testing::Not(testing::HasSubstr("0000000000 65536 f\r\n")));
Tom Sepez9eb811f2016-01-05 14:48:31 -080068}