Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_DocumentTest { | |
| 40 public: | |
| 41 CPDF_Document* SetUpTest() { | |
| 42 CPDF_ModuleMgr* module_mgr = CPDF_ModuleMgr::Get(); | |
| 43 module_mgr->InitPageModule(); | |
| 44 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.
| |
| 45 | |
| 46 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.
| |
| 47 for (size_t i = 0; i < 7; i++) | |
| 48 pages[i] = CreateNumberedPage(i); | |
| 49 CPDF_Array* zeroToTwo = new CPDF_Array(); | |
| 50 zeroToTwo->AddReference(testDoc, testDoc->AddIndirectObject(pages[0])); | |
| 51 zeroToTwo->AddReference(testDoc, testDoc->AddIndirectObject(pages[1])); | |
| 52 zeroToTwo->AddReference(testDoc, testDoc->AddIndirectObject(pages[2])); | |
| 53 CPDF_Dictionary* branch1 = CreatePageTreeNode(zeroToTwo, testDoc, 3); | |
| 54 | |
| 55 CPDF_Array* zeroToThree = new CPDF_Array(); | |
| 56 zeroToThree->AddReference(testDoc, branch1->GetObjNum()); | |
| 57 zeroToThree->AddReference(testDoc, testDoc->AddIndirectObject(pages[3])); | |
| 58 CPDF_Dictionary* branch2 = CreatePageTreeNode(zeroToThree, testDoc, 4); | |
| 59 | |
| 60 CPDF_Array* fourFive = new CPDF_Array(); | |
| 61 fourFive->AddReference(testDoc, testDoc->AddIndirectObject(pages[4])); | |
| 62 fourFive->AddReference(testDoc, testDoc->AddIndirectObject(pages[5])); | |
| 63 CPDF_Dictionary* branch3 = CreatePageTreeNode(fourFive, testDoc, 2); | |
| 64 | |
| 65 CPDF_Array* justSix = new CPDF_Array(); | |
| 66 justSix->AddReference(testDoc, testDoc->AddIndirectObject(pages[6])); | |
| 67 CPDF_Dictionary* branch4 = CreatePageTreeNode(justSix, testDoc, 1); | |
| 68 | |
| 69 CPDF_Array* allPages = new CPDF_Array(); | |
| 70 allPages->AddReference(testDoc, branch2->GetObjNum()); | |
| 71 allPages->AddReference(testDoc, branch3->GetObjNum()); | |
| 72 allPages->AddReference(testDoc, branch4->GetObjNum()); | |
| 73 CPDF_Dictionary* pagesDict = CreatePageTreeNode(allPages, testDoc, 7); | |
| 74 | |
| 75 CPDF_Dictionary* root = new CPDF_Dictionary(); | |
| 76 root->SetReferenceFor("Pages", testDoc, | |
| 77 testDoc->AddIndirectObject(pagesDict)); | |
| 78 testDoc->m_pRootDict = root; | |
| 79 testDoc->m_PageList.SetSize(7); | |
| 80 return testDoc; | |
| 81 } | |
| 82 }; | |
| 83 | |
| 84 TEST(cpdf_document, GetPages) { | |
| 85 CPDF_DocumentTest test; | |
| 86 CPDF_Document* document = test.SetUpTest(); | |
| 87 for (int i = 0; i < 7; i++) { | |
| 88 CPDF_Dictionary* page = document->GetPage(i); | |
| 89 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
| |
| 90 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
| |
| 91 EXPECT_EQ(i, page->GetIntegerFor("PageNumbering")); | |
| 92 } | |
| 93 CPDF_Dictionary* page = document->GetPage(7); | |
| 94 EXPECT_FALSE(page); | |
| 95 } | |
| 96 | |
| 97 TEST(cpdf_document, GetPagesReverseOrder) { | |
| 98 CPDF_DocumentTest test; | |
| 99 CPDF_Document* document = test.SetUpTest(); | |
| 100 for (int i = 6; i >= 0; i--) { | |
| 101 CPDF_Dictionary* page = document->GetPage(i); | |
| 102 ASSERT_TRUE(page); | |
| 103 ASSERT_TRUE(page->GetObjectFor("PageNumbering")); | |
| 104 EXPECT_EQ(i, page->GetIntegerFor("PageNumbering")); | |
| 105 } | |
| 106 CPDF_Dictionary* page = document->GetPage(7); | |
| 107 EXPECT_FALSE(page); | |
| 108 } | |
| OLD | NEW |