Tom Sepez | 9eb811f | 2016-01-05 14:48:31 -0800 | [diff] [blame] | 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 | |
tsepez | 6e1d603 | 2016-11-11 17:55:40 -0800 | [diff] [blame] | 5 | #include "public/fpdf_save.h" |
| 6 | |
Tom Sepez | 9eb811f | 2016-01-05 14:48:31 -0800 | [diff] [blame] | 7 | #include <string.h> |
| 8 | |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 9 | #include "core/fxcrt/fx_string.h" |
tsepez | 6e1d603 | 2016-11-11 17:55:40 -0800 | [diff] [blame] | 10 | #include "public/fpdf_edit.h" |
| 11 | #include "public/fpdf_ppo.h" |
Tom Sepez | 9eb811f | 2016-01-05 14:48:31 -0800 | [diff] [blame] | 12 | #include "public/fpdfview.h" |
| 13 | #include "testing/embedder_test.h" |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 14 | #include "testing/gmock/include/gmock/gmock-matchers.h" |
Tom Sepez | 9eb811f | 2016-01-05 14:48:31 -0800 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 16 | #include "testing/test_support.h" |
Tom Sepez | 9eb811f | 2016-01-05 14:48:31 -0800 | [diff] [blame] | 17 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 18 | class FPDFSaveEmbedderTest : public EmbedderTest {}; |
Tom Sepez | 9eb811f | 2016-01-05 14:48:31 -0800 | [diff] [blame] | 19 | |
| 20 | TEST_F(FPDFSaveEmbedderTest, SaveSimpleDoc) { |
| 21 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 22 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 23 | EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n")); |
Artem Strygin | d24b97e | 2017-08-09 18:50:59 +0300 | [diff] [blame] | 24 | EXPECT_EQ(805u, GetString().length()); |
Tom Sepez | 9eb811f | 2016-01-05 14:48:31 -0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | TEST_F(FPDFSaveEmbedderTest, SaveSimpleDocWithVersion) { |
| 28 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 29 | EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 14)); |
| 30 | EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.4\r\n")); |
Artem Strygin | d24b97e | 2017-08-09 18:50:59 +0300 | [diff] [blame] | 31 | EXPECT_EQ(805u, GetString().length()); |
Tom Sepez | 9eb811f | 2016-01-05 14:48:31 -0800 | [diff] [blame] | 32 | } |
Tom Sepez | 9eb811f | 2016-01-05 14:48:31 -0800 | [diff] [blame] | 33 | TEST_F(FPDFSaveEmbedderTest, SaveSimpleDocWithBadVersion) { |
| 34 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 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")); |
Tom Sepez | 9eb811f | 2016-01-05 14:48:31 -0800 | [diff] [blame] | 45 | } |
| 46 | |
tsepez | 6e1d603 | 2016-11-11 17:55:40 -0800 | [diff] [blame] | 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 | |
Tom Sepez | 9eb811f | 2016-01-05 14:48:31 -0800 | [diff] [blame] | 62 | TEST_F(FPDFSaveEmbedderTest, BUG_342) { |
| 63 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 64 | 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 Sepez | 9eb811f | 2016-01-05 14:48:31 -0800 | [diff] [blame] | 68 | } |