Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1013)

Unified Diff: core/fpdfapi/parser/cpdf_document_unittest.cpp

Issue 2435783006: Add CPDF_Document::GetPage() unittests (Closed)
Patch Set: Try fix win Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« core/fpdfapi/parser/cpdf_document.h ('K') | « core/fpdfapi/parser/cpdf_document.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/fpdfapi/parser/cpdf_document_unittest.cpp
diff --git a/core/fpdfapi/parser/cpdf_document_unittest.cpp b/core/fpdfapi/parser/cpdf_document_unittest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..56ff28fbf53213a27630bd22cf3602d053993509
--- /dev/null
+++ b/core/fpdfapi/parser/cpdf_document_unittest.cpp
@@ -0,0 +1,108 @@
+// Copyright 2016 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/fpdfapi/parser/cpdf_document.h"
+
+#include <memory>
+
+#include "core/fpdfapi/cpdf_modulemgr.h"
+#include "core/fpdfapi/parser/cpdf_array.h"
+#include "core/fpdfapi/parser/cpdf_dictionary.h"
+#include "core/fpdfapi/parser/cpdf_parser.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+CPDF_Dictionary* CreatePageTreeNode(CPDF_Array* kids,
+ CPDF_Document* pDoc,
+ int count) {
+ CPDF_Dictionary* pageNode = new CPDF_Dictionary();
+ pageNode->SetStringFor("Type", "Pages");
+ pageNode->SetReferenceFor("Kids", pDoc, pDoc->AddIndirectObject(kids));
+ pageNode->SetIntegerFor("Count", count);
+ uint32_t pageNodeRef = pDoc->AddIndirectObject(pageNode);
+ for (size_t i = 0; i < kids->GetCount(); i++)
+ kids->GetDictAt(i)->SetReferenceFor("Parent", pDoc, pageNodeRef);
+ return pageNode;
+}
+
+CPDF_Dictionary* CreateNumberedPage(size_t number) {
+ CPDF_Dictionary* page = new CPDF_Dictionary();
+ page->SetStringFor("Type", "Page");
+ page->SetIntegerFor("PageNumbering", number);
+ return page;
+}
+
+} // namespace
+
+class CPDF_DocumentTest {
+ public:
+ CPDF_Document* SetUpTest() {
+ CPDF_ModuleMgr* module_mgr = CPDF_ModuleMgr::Get();
+ module_mgr->InitPageModule();
+ CPDF_Document* testDoc = new CPDF_Document(std::unique_ptr<CPDF_Parser>());
Tom Sepez 2016/10/21 21:31:40 Are you sure you don't want to inherit from CPDF_D
npm 2016/10/21 22:12:04 Done.
+
+ CPDF_Dictionary* pages[7];
Tom Sepez 2016/10/21 21:31:40 nit: probably don't need this local, later on just
npm 2016/10/21 22:12:04 Done.
+ for (size_t i = 0; i < 7; i++)
+ pages[i] = CreateNumberedPage(i);
+ CPDF_Array* zeroToTwo = new CPDF_Array();
+ zeroToTwo->AddReference(testDoc, testDoc->AddIndirectObject(pages[0]));
+ zeroToTwo->AddReference(testDoc, testDoc->AddIndirectObject(pages[1]));
+ zeroToTwo->AddReference(testDoc, testDoc->AddIndirectObject(pages[2]));
+ CPDF_Dictionary* branch1 = CreatePageTreeNode(zeroToTwo, testDoc, 3);
+
+ CPDF_Array* zeroToThree = new CPDF_Array();
+ zeroToThree->AddReference(testDoc, branch1->GetObjNum());
+ zeroToThree->AddReference(testDoc, testDoc->AddIndirectObject(pages[3]));
+ CPDF_Dictionary* branch2 = CreatePageTreeNode(zeroToThree, testDoc, 4);
+
+ CPDF_Array* fourFive = new CPDF_Array();
+ fourFive->AddReference(testDoc, testDoc->AddIndirectObject(pages[4]));
+ fourFive->AddReference(testDoc, testDoc->AddIndirectObject(pages[5]));
+ CPDF_Dictionary* branch3 = CreatePageTreeNode(fourFive, testDoc, 2);
+
+ CPDF_Array* justSix = new CPDF_Array();
+ justSix->AddReference(testDoc, testDoc->AddIndirectObject(pages[6]));
+ CPDF_Dictionary* branch4 = CreatePageTreeNode(justSix, testDoc, 1);
+
+ CPDF_Array* allPages = new CPDF_Array();
+ allPages->AddReference(testDoc, branch2->GetObjNum());
+ allPages->AddReference(testDoc, branch3->GetObjNum());
+ allPages->AddReference(testDoc, branch4->GetObjNum());
+ CPDF_Dictionary* pagesDict = CreatePageTreeNode(allPages, testDoc, 7);
+
+ CPDF_Dictionary* root = new CPDF_Dictionary();
+ root->SetReferenceFor("Pages", testDoc,
+ testDoc->AddIndirectObject(pagesDict));
+ testDoc->m_pRootDict = root;
+ testDoc->m_PageList.SetSize(7);
+ return testDoc;
+ }
+};
+
+TEST(cpdf_document, GetPages) {
+ CPDF_DocumentTest test;
+ CPDF_Document* document = test.SetUpTest();
+ for (int i = 0; i < 7; i++) {
+ CPDF_Dictionary* page = document->GetPage(i);
+ ASSERT_TRUE(page);
Tom Sepez 2016/10/21 21:31:40 probably don't need this one, segv on next line et
npm 2016/10/21 21:46:00 I thought we prefer not to crash if possible (ie t
Tom Sepez 2016/10/21 21:48:33 Acknowledged. Let's leave it then
+ ASSERT_TRUE(page->GetObjectFor("PageNumbering"));
Tom Sepez 2016/10/21 21:31:40 probably should be expect_true.
npm 2016/10/21 21:46:00 Ditto: if this is expect, then it will crash on th
+ EXPECT_EQ(i, page->GetIntegerFor("PageNumbering"));
+ }
+ CPDF_Dictionary* page = document->GetPage(7);
+ EXPECT_FALSE(page);
+}
+
+TEST(cpdf_document, GetPagesReverseOrder) {
+ CPDF_DocumentTest test;
+ CPDF_Document* document = test.SetUpTest();
+ for (int i = 6; i >= 0; i--) {
+ CPDF_Dictionary* page = document->GetPage(i);
+ ASSERT_TRUE(page);
+ ASSERT_TRUE(page->GetObjectFor("PageNumbering"));
+ EXPECT_EQ(i, page->GetIntegerFor("PageNumbering"));
+ }
+ CPDF_Dictionary* page = document->GetPage(7);
+ EXPECT_FALSE(page);
+}
« core/fpdfapi/parser/cpdf_document.h ('K') | « core/fpdfapi/parser/cpdf_document.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698