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

Side by Side Diff: core/fpdfapi/parser/cpdf_document_unittest.cpp

Issue 2435783006: Add CPDF_Document::GetPage() unittests (Closed)
Patch Set: Use unique_ptr 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 unified diff | Download patch
« no previous file with comments | « core/fpdfapi/parser/cpdf_document.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
5 #include "core/fpdfapi/parser/cpdf_document.h"
6
7 #include <memory>
8
9 #include "core/fpdfapi/cpdf_modulemgr.h"
10 #include "core/fpdfapi/parser/cpdf_array.h"
11 #include "core/fpdfapi/parser/cpdf_dictionary.h"
12 #include "core/fpdfapi/parser/cpdf_parser.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 CPDF_Dictionary* CreatePageTreeNode(CPDF_Array* kids,
18 CPDF_Document* pDoc,
19 int count) {
20 CPDF_Dictionary* pageNode = new CPDF_Dictionary();
21 pageNode->SetStringFor("Type", "Pages");
22 pageNode->SetReferenceFor("Kids", pDoc, pDoc->AddIndirectObject(kids));
23 pageNode->SetIntegerFor("Count", count);
24 uint32_t pageNodeRef = pDoc->AddIndirectObject(pageNode);
25 for (size_t i = 0; i < kids->GetCount(); i++)
26 kids->GetDictAt(i)->SetReferenceFor("Parent", pDoc, pageNodeRef);
27 return pageNode;
28 }
29
30 CPDF_Dictionary* CreateNumberedPage(size_t number) {
31 CPDF_Dictionary* page = new CPDF_Dictionary();
32 page->SetStringFor("Type", "Page");
33 page->SetIntegerFor("PageNumbering", number);
34 return page;
35 }
36
37 } // namespace
38
39 class CPDF_TestDocumentForPages : public CPDF_Document {
40 public:
41 CPDF_TestDocumentForPages() : CPDF_Document(nullptr) {
42 CPDF_ModuleMgr* module_mgr = CPDF_ModuleMgr::Get();
43 module_mgr->InitPageModule();
44 // Set up test
45 CPDF_Array* zeroToTwo = new CPDF_Array();
46 zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(0)));
47 zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(1)));
48 zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(2)));
49 CPDF_Dictionary* branch1 = CreatePageTreeNode(zeroToTwo, this, 3);
50
51 CPDF_Array* zeroToThree = new CPDF_Array();
52 zeroToThree->AddReference(this, branch1->GetObjNum());
53 zeroToThree->AddReference(this, AddIndirectObject(CreateNumberedPage(3)));
54 CPDF_Dictionary* branch2 = CreatePageTreeNode(zeroToThree, this, 4);
55
56 CPDF_Array* fourFive = new CPDF_Array();
57 fourFive->AddReference(this, AddIndirectObject(CreateNumberedPage(4)));
58 fourFive->AddReference(this, AddIndirectObject(CreateNumberedPage(5)));
59 CPDF_Dictionary* branch3 = CreatePageTreeNode(fourFive, this, 2);
60
61 CPDF_Array* justSix = new CPDF_Array();
62 justSix->AddReference(this, AddIndirectObject(CreateNumberedPage(6)));
63 CPDF_Dictionary* branch4 = CreatePageTreeNode(justSix, this, 1);
64
65 CPDF_Array* allPages = new CPDF_Array();
66 allPages->AddReference(this, branch2->GetObjNum());
67 allPages->AddReference(this, branch3->GetObjNum());
68 allPages->AddReference(this, branch4->GetObjNum());
69 CPDF_Dictionary* pagesDict = CreatePageTreeNode(allPages, this, 7);
70
71 CPDF_Dictionary* root = new CPDF_Dictionary();
72 root->SetReferenceFor("Pages", this, AddIndirectObject(pagesDict));
73 m_pRootDict = root;
74 m_PageList.SetSize(7);
75 }
76 };
77
78 TEST(cpdf_document, GetPages) {
79 std::unique_ptr<CPDF_TestDocumentForPages> document =
80 pdfium::MakeUnique<CPDF_TestDocumentForPages>();
81 for (int i = 0; i < 7; i++) {
82 CPDF_Dictionary* page = document->GetPage(i);
83 ASSERT_TRUE(page);
84 ASSERT_TRUE(page->GetObjectFor("PageNumbering"));
85 EXPECT_EQ(i, page->GetIntegerFor("PageNumbering"));
86 }
87 CPDF_Dictionary* page = document->GetPage(7);
88 EXPECT_FALSE(page);
89 }
90
91 TEST(cpdf_document, GetPagesReverseOrder) {
92 std::unique_ptr<CPDF_TestDocumentForPages> document =
93 pdfium::MakeUnique<CPDF_TestDocumentForPages>();
94 for (int i = 6; i >= 0; i--) {
95 CPDF_Dictionary* page = document->GetPage(i);
96 ASSERT_TRUE(page);
97 ASSERT_TRUE(page->GetObjectFor("PageNumbering"));
98 EXPECT_EQ(i, page->GetIntegerFor("PageNumbering"));
99 }
100 CPDF_Dictionary* page = document->GetPage(7);
101 EXPECT_FALSE(page);
102 }
OLDNEW
« no previous file with comments | « core/fpdfapi/parser/cpdf_document.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698