summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2016-01-07 12:14:49 -0800
committerTom Sepez <tsepez@chromium.org>2016-01-07 12:14:49 -0800
commit5dc13d7858f9caf58b0e12f38823300a739302e2 (patch)
treef883ae2b246af537f8851b49b6f1a5eefae61800
parenta462162a76b3f487e76d8cc1b1f917f0937ea040 (diff)
downloadpdfium-5dc13d7858f9caf58b0e12f38823300a739302e2.tar.xz
Disentangle fpdfsave_embeddertest's FPDF_FILEWRITE, use gmock matchers.
R=thestig@chromium.org Review URL: https://codereview.chromium.org/1561303002 .
-rw-r--r--fpdfsdk/src/fpdfedit_embeddertest.cpp43
-rw-r--r--fpdfsdk/src/fpdfsave_embeddertest.cpp62
-rw-r--r--testing/test_support.cpp18
-rw-r--r--testing/test_support.h16
4 files changed, 97 insertions, 42 deletions
diff --git a/fpdfsdk/src/fpdfedit_embeddertest.cpp b/fpdfsdk/src/fpdfedit_embeddertest.cpp
index 9db948cd1f..1cd28cff45 100644
--- a/fpdfsdk/src/fpdfedit_embeddertest.cpp
+++ b/fpdfsdk/src/fpdfedit_embeddertest.cpp
@@ -5,14 +5,55 @@
#include "public/fpdf_edit.h"
#include "public/fpdfview.h"
#include "testing/embedder_test.h"
+#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/test_support.h"
-class FPDFEditEmbeddertest : public EmbedderTest {};
+class FPDFEditEmbeddertest : public EmbedderTest, public TestSaver {};
TEST_F(FPDFEditEmbeddertest, EmptyCreation) {
EXPECT_TRUE(CreateEmptyDocument());
FPDF_PAGE page = FPDFPage_New(document(), 1, 640.0, 480.0);
EXPECT_NE(nullptr, page);
EXPECT_TRUE(FPDFPage_GenerateContent(page));
+ EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
+ EXPECT_THAT(GetString(),
+ testing::MatchesRegex(
+ "%PDF-1.7\r\n"
+ "%\xA1\xB3\xC5\xD7\r\n"
+ "1 0 obj\r\n"
+ "<</Type/Catalog/Pages 2 0 R >>\r\n"
+ "endobj\r\n"
+ "2 0 obj\r\n"
+ "<</Type/Pages/Count 1/Kids\\[ 4 0 R \\]>>\r\n"
+ "endobj\r\n"
+ "3 0 obj\r\n"
+ "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n"
+ "endobj\r\n"
+ "4 0 obj\r\n"
+ "<</Type/Page/Parent 2 0 R /MediaBox\\[ 0 0 640 "
+ "480\\]/Rotate 0/Resources<<>>/Contents 5 0 R >>\r\n"
+ "endobj\r\n"
+ "5 0 obj\r\n"
+ "<</Length 8/Filter/FlateDecode>>stream\r\n"
+ "x\x9C\x3\0\0\0\0\x1\r\n"
+ "endstream\r\n"
+ "endobj\r\n"
+ "xref\r\n"
+ "0 6\r\n"
+ "0000000000 65535 f\r\n"
+ "0000000017 00000 n\r\n"
+ "0000000066 00000 n\r\n"
+ "0000000122 00000 n\r\n"
+ "0000000192 00000 n\r\n"
+ "0000000301 00000 n\r\n"
+ "trailer\r\n"
+ "<<\r\n"
+ "/Root 1 0 R\r\n"
+ "/Info 3 0 R\r\n"
+ "/Size 6/ID\\[<.*><.*>\\]>>\r\n"
+ "startxref\r\n"
+ "379\r\n"
+ "%%EOF\r\n"));
FPDFPage_Delete(document(), 1);
}
diff --git a/fpdfsdk/src/fpdfsave_embeddertest.cpp b/fpdfsdk/src/fpdfsave_embeddertest.cpp
index bee452beaf..1c93f4f0e1 100644
--- a/fpdfsdk/src/fpdfsave_embeddertest.cpp
+++ b/fpdfsdk/src/fpdfsave_embeddertest.cpp
@@ -9,64 +9,44 @@
#include "public/fpdfview.h"
#include "testing/embedder_test.h"
#include "testing/fx_string_testhelpers.h"
+#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/test_support.h"
-class FPDFSaveEmbedderTest : public EmbedderTest, public FPDF_FILEWRITE {
- public:
- FPDFSaveEmbedderTest() {
- FPDF_FILEWRITE::version = 1;
- FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
- }
- bool SaveDocumentToString() {
- m_String.clear();
- return FPDF_SaveAsCopy(document(), this, 0);
- }
- bool SaveDocumentWithVersionToString(int version) {
- m_String.clear();
- return FPDF_SaveWithVersion(document(), this, 0, version);
- }
- const std::string& GetString() const { return m_String; }
-
- private:
- static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
- const void* data,
- unsigned long size) {
- FPDFSaveEmbedderTest* pThis =
- static_cast<FPDFSaveEmbedderTest*>(pFileWrite);
- pThis->m_String.append(static_cast<const char*>(data), size);
- return 1;
- }
-
- std::string m_String;
-};
+class FPDFSaveEmbedderTest : public EmbedderTest, public TestSaver {};
TEST_F(FPDFSaveEmbedderTest, SaveSimpleDoc) {
EXPECT_TRUE(OpenDocument("hello_world.pdf"));
- EXPECT_TRUE(SaveDocumentToString());
- EXPECT_EQ("%PDF-1.7\r\n", GetString().substr(0, 10));
+ EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
+ EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
EXPECT_EQ(843, GetString().length());
}
TEST_F(FPDFSaveEmbedderTest, SaveSimpleDocWithVersion) {
EXPECT_TRUE(OpenDocument("hello_world.pdf"));
- EXPECT_TRUE(SaveDocumentWithVersionToString(14));
- EXPECT_EQ("%PDF-1.4\r\n", GetString().substr(0, 10));
+ EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 14));
+ EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.4\r\n"));
EXPECT_EQ(843, GetString().length());
}
TEST_F(FPDFSaveEmbedderTest, SaveSimpleDocWithBadVersion) {
EXPECT_TRUE(OpenDocument("hello_world.pdf"));
- EXPECT_TRUE(SaveDocumentWithVersionToString(-1));
- EXPECT_EQ("%PDF-1.7\r\n", GetString().substr(0, 10));
- EXPECT_TRUE(SaveDocumentWithVersionToString(0));
- EXPECT_EQ("%PDF-1.7\r\n", GetString().substr(0, 10));
- EXPECT_TRUE(SaveDocumentWithVersionToString(18));
- EXPECT_EQ("%PDF-1.7\r\n", GetString().substr(0, 10));
+ EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, -1));
+ EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
+
+ ClearString();
+ EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 0));
+ EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
+
+ ClearString();
+ EXPECT_TRUE(FPDF_SaveWithVersion(document(), this, 0, 18));
+ EXPECT_THAT(GetString(), testing::StartsWith("%PDF-1.7\r\n"));
}
TEST_F(FPDFSaveEmbedderTest, BUG_342) {
EXPECT_TRUE(OpenDocument("hello_world.pdf"));
- EXPECT_TRUE(SaveDocumentToString());
- EXPECT_EQ(std::string::npos, GetString().find("0000000000 65536 f\r\n"));
- EXPECT_NE(std::string::npos, GetString().find("0000000000 65535 f\r\n"));
+ EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
+ EXPECT_THAT(GetString(), testing::HasSubstr("0000000000 65535 f\r\n"));
+ EXPECT_THAT(GetString(),
+ testing::Not(testing::HasSubstr("0000000000 65536 f\r\n")));
}
diff --git a/testing/test_support.cpp b/testing/test_support.cpp
index 96e01c5b01..da5ea7e9ee 100644
--- a/testing/test_support.cpp
+++ b/testing/test_support.cpp
@@ -172,3 +172,21 @@ int TestLoader::GetBlock(void* param,
memcpy(pBuf, pLoader->m_pBuf + pos, size);
return 1;
}
+
+TestSaver::TestSaver() {
+ FPDF_FILEWRITE::version = 1;
+ FPDF_FILEWRITE::WriteBlock = WriteBlockCallback;
+}
+
+void TestSaver::ClearString() {
+ m_String.clear();
+}
+
+// static
+int TestSaver::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
+ const void* data,
+ unsigned long size) {
+ TestSaver* pThis = static_cast<TestSaver*>(pFileWrite);
+ pThis->m_String.append(static_cast<const char*>(data), size);
+ return 1;
+}
diff --git a/testing/test_support.h b/testing/test_support.h
index 945704194d..f59d5440e7 100644
--- a/testing/test_support.h
+++ b/testing/test_support.h
@@ -10,6 +10,7 @@
#include <string>
#include "public/fpdfview.h"
+#include "public/fpdf_save.h"
#ifdef PDF_ENABLE_V8
#include "v8/include/v8.h"
@@ -62,4 +63,19 @@ class TestLoader {
const size_t m_Len;
};
+class TestSaver : public FPDF_FILEWRITE {
+ public:
+ TestSaver();
+
+ void ClearString();
+ const std::string& GetString() const { return m_String; }
+
+ private:
+ static int WriteBlockCallback(FPDF_FILEWRITE* pFileWrite,
+ const void* data,
+ unsigned long size);
+
+ std::string m_String;
+};
+
#endif // TESTING_EMBEDDER_TEST_SUPPORT_H_