blob: 918727027fe1a12bfdbe761531e789a506fe180f [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"
14#include "testing/fx_string_testhelpers.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"
Tom Sepez0aec19b2016-01-07 12:22:44 -080017#include "testing/test_support.h"
Tom Sepez9eb811f2016-01-05 14:48:31 -080018
Tom Sepez0aec19b2016-01-07 12:22:44 -080019class FPDFSaveEmbedderTest : public EmbedderTest, public TestSaver {};
Tom Sepez9eb811f2016-01-05 14:48:31 -080020
21TEST_F(FPDFSaveEmbedderTest, SaveSimpleDoc) {
22 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepez0aec19b2016-01-07 12:22:44 -080023 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
24 EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
Wei Li05d53f02016-03-29 16:42:53 -070025 EXPECT_EQ(843u, GetString().length());
Tom Sepez9eb811f2016-01-05 14:48:31 -080026}
27
28TEST_F(FPDFSaveEmbedderTest, SaveSimpleDocWithVersion) {
29 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepez0aec19b2016-01-07 12:22:44 -080030 EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 14));
31 EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.4\r\n"));
Wei Li05d53f02016-03-29 16:42:53 -070032 EXPECT_EQ(843u, GetString().length());
Tom Sepez9eb811f2016-01-05 14:48:31 -080033}
34
35TEST_F(FPDFSaveEmbedderTest, SaveSimpleDocWithBadVersion) {
36 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepez0aec19b2016-01-07 12:22:44 -080037 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"));
Tom Sepez9eb811f2016-01-05 14:48:31 -080047}
48
tsepez6e1d6032016-11-11 17:55:40 -080049TEST_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
Tom Sepez9eb811f2016-01-05 14:48:31 -080064TEST_F(FPDFSaveEmbedderTest, BUG_342) {
65 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
Tom Sepez0aec19b2016-01-07 12:22:44 -080066 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")));
Tom Sepez9eb811f2016-01-05 14:48:31 -080070}