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

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

Issue 2510223002: Make CPDF_Dictionary use unique pointers. (Closed)
Patch Set: rebase Created 4 years, 1 month 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.cpp ('k') | core/fpdfapi/parser/cpdf_object_unittest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 PDFium Authors. All rights reserved. 1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/fpdfapi/parser/cpdf_document.h" 5 #include "core/fpdfapi/parser/cpdf_document.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility>
8 9
9 #include "core/fpdfapi/cpdf_modulemgr.h" 10 #include "core/fpdfapi/cpdf_modulemgr.h"
10 #include "core/fpdfapi/parser/cpdf_array.h" 11 #include "core/fpdfapi/parser/cpdf_array.h"
12 #include "core/fpdfapi/parser/cpdf_boolean.h"
11 #include "core/fpdfapi/parser/cpdf_dictionary.h" 13 #include "core/fpdfapi/parser/cpdf_dictionary.h"
12 #include "core/fpdfapi/parser/cpdf_linearized_header.h" 14 #include "core/fpdfapi/parser/cpdf_linearized_header.h"
15 #include "core/fpdfapi/parser/cpdf_number.h"
13 #include "core/fpdfapi/parser/cpdf_parser.h" 16 #include "core/fpdfapi/parser/cpdf_parser.h"
14 #include "core/fpdfapi/parser/cpdf_reference.h" 17 #include "core/fpdfapi/parser/cpdf_reference.h"
18 #include "core/fpdfapi/parser/cpdf_string.h"
15 #include "core/fxcrt/fx_memory.h" 19 #include "core/fxcrt/fx_memory.h"
16 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/base/ptr_util.h" 21 #include "third_party/base/ptr_util.h"
18 22
19 namespace { 23 namespace {
20 24
21 CPDF_Dictionary* CreatePageTreeNode(std::unique_ptr<CPDF_Array> kids, 25 CPDF_Dictionary* CreatePageTreeNode(std::unique_ptr<CPDF_Array> kids,
22 CPDF_Document* pDoc, 26 CPDF_Document* pDoc,
23 int count) { 27 int count) {
24 CPDF_Array* pUnowned = pDoc->AddIndirectObject(std::move(kids))->AsArray(); 28 CPDF_Array* pUnowned = pDoc->AddIndirectObject(std::move(kids))->AsArray();
25 CPDF_Dictionary* pageNode = pDoc->NewIndirect<CPDF_Dictionary>(); 29 CPDF_Dictionary* pageNode = pDoc->NewIndirect<CPDF_Dictionary>();
26 pageNode->SetStringFor("Type", "Pages"); 30 pageNode->SetNewFor<CPDF_String>("Type", "Pages", false);
27 pageNode->SetReferenceFor("Kids", pDoc, pUnowned); 31 pageNode->SetNewFor<CPDF_Reference>("Kids", pDoc, pUnowned->GetObjNum());
28 pageNode->SetIntegerFor("Count", count); 32 pageNode->SetNewFor<CPDF_Number>("Count", count);
29 for (size_t i = 0; i < pUnowned->GetCount(); i++) 33 for (size_t i = 0; i < pUnowned->GetCount(); i++) {
30 pUnowned->GetDictAt(i)->SetReferenceFor("Parent", pDoc, pageNode); 34 pUnowned->GetDictAt(i)->SetNewFor<CPDF_Reference>("Parent", pDoc,
35 pageNode->GetObjNum());
36 }
31 return pageNode; 37 return pageNode;
32 } 38 }
33 39
34 std::unique_ptr<CPDF_Dictionary> CreateNumberedPage(size_t number) { 40 std::unique_ptr<CPDF_Dictionary> CreateNumberedPage(size_t number) {
35 auto page = pdfium::MakeUnique<CPDF_Dictionary>(); 41 auto page = pdfium::MakeUnique<CPDF_Dictionary>();
36 page->SetStringFor("Type", "Page"); 42 page->SetNewFor<CPDF_String>("Type", "Page", false);
37 page->SetIntegerFor("PageNumbering", number); 43 page->SetNewFor<CPDF_Number>("PageNumbering", static_cast<int>(number));
38 return page; 44 return page;
39 } 45 }
40 46
41 class CPDF_TestDocumentForPages : public CPDF_Document { 47 class CPDF_TestDocumentForPages : public CPDF_Document {
42 public: 48 public:
43 CPDF_TestDocumentForPages() : CPDF_Document(nullptr) { 49 CPDF_TestDocumentForPages() : CPDF_Document(nullptr) {
44 // Set up test 50 // Set up test
45 auto zeroToTwo = pdfium::MakeUnique<CPDF_Array>(); 51 auto zeroToTwo = pdfium::MakeUnique<CPDF_Array>();
46 zeroToTwo->AddNew<CPDF_Reference>( 52 zeroToTwo->AddNew<CPDF_Reference>(
47 this, AddIndirectObject(CreateNumberedPage(0))->GetObjNum()); 53 this, AddIndirectObject(CreateNumberedPage(0))->GetObjNum());
(...skipping 24 matching lines...) Expand all
72 CPDF_Dictionary* branch4 = CreatePageTreeNode(std::move(justSix), this, 1); 78 CPDF_Dictionary* branch4 = CreatePageTreeNode(std::move(justSix), this, 1);
73 79
74 auto allPages = pdfium::MakeUnique<CPDF_Array>(); 80 auto allPages = pdfium::MakeUnique<CPDF_Array>();
75 allPages->AddNew<CPDF_Reference>(this, branch2->GetObjNum()); 81 allPages->AddNew<CPDF_Reference>(this, branch2->GetObjNum());
76 allPages->AddNew<CPDF_Reference>(this, branch3->GetObjNum()); 82 allPages->AddNew<CPDF_Reference>(this, branch3->GetObjNum());
77 allPages->AddNew<CPDF_Reference>(this, branch4->GetObjNum()); 83 allPages->AddNew<CPDF_Reference>(this, branch4->GetObjNum());
78 CPDF_Dictionary* pagesDict = 84 CPDF_Dictionary* pagesDict =
79 CreatePageTreeNode(std::move(allPages), this, 7); 85 CreatePageTreeNode(std::move(allPages), this, 7);
80 86
81 m_pOwnedRootDict = pdfium::MakeUnique<CPDF_Dictionary>(); 87 m_pOwnedRootDict = pdfium::MakeUnique<CPDF_Dictionary>();
82 m_pOwnedRootDict->SetReferenceFor("Pages", this, pagesDict); 88 m_pOwnedRootDict->SetNewFor<CPDF_Reference>("Pages", this,
89 pagesDict->GetObjNum());
83 m_pRootDict = m_pOwnedRootDict.get(); 90 m_pRootDict = m_pOwnedRootDict.get();
84 m_PageList.SetSize(7); 91 m_PageList.SetSize(7);
85 } 92 }
86 93
87 private: 94 private:
88 std::unique_ptr<CPDF_Dictionary> m_pOwnedRootDict; 95 std::unique_ptr<CPDF_Dictionary> m_pOwnedRootDict;
89 }; 96 };
90 97
91 class CPDF_TestDocumentWithPageWithoutPageNum : public CPDF_Document { 98 class CPDF_TestDocumentWithPageWithoutPageNum : public CPDF_Document {
92 public: 99 public:
93 CPDF_TestDocumentWithPageWithoutPageNum() : CPDF_Document(nullptr) { 100 CPDF_TestDocumentWithPageWithoutPageNum() : CPDF_Document(nullptr) {
94 // Set up test 101 // Set up test
95 auto allPages = pdfium::MakeUnique<CPDF_Array>(); 102 auto allPages = pdfium::MakeUnique<CPDF_Array>();
96 allPages->AddNew<CPDF_Reference>( 103 allPages->AddNew<CPDF_Reference>(
97 this, AddIndirectObject(CreateNumberedPage(0))->GetObjNum()); 104 this, AddIndirectObject(CreateNumberedPage(0))->GetObjNum());
98 allPages->AddNew<CPDF_Reference>( 105 allPages->AddNew<CPDF_Reference>(
99 this, AddIndirectObject(CreateNumberedPage(1))->GetObjNum()); 106 this, AddIndirectObject(CreateNumberedPage(1))->GetObjNum());
100 // Page without pageNum. 107 // Page without pageNum.
101 allPages->Add(CreateNumberedPage(2)); 108 allPages->Add(CreateNumberedPage(2));
102 CPDF_Dictionary* pagesDict = 109 CPDF_Dictionary* pagesDict =
103 CreatePageTreeNode(std::move(allPages), this, 3); 110 CreatePageTreeNode(std::move(allPages), this, 3);
104 m_pOwnedRootDict.reset(new CPDF_Dictionary()); 111 m_pOwnedRootDict.reset(new CPDF_Dictionary());
105 m_pOwnedRootDict->SetReferenceFor("Pages", this, pagesDict->GetObjNum()); 112 m_pOwnedRootDict->SetNewFor<CPDF_Reference>("Pages", this,
113 pagesDict->GetObjNum());
106 m_pRootDict = m_pOwnedRootDict.get(); 114 m_pRootDict = m_pOwnedRootDict.get();
107 m_PageList.SetSize(3); 115 m_PageList.SetSize(3);
108 } 116 }
109 117
110 private: 118 private:
111 std::unique_ptr<CPDF_Dictionary> m_pOwnedRootDict; 119 std::unique_ptr<CPDF_Dictionary> m_pOwnedRootDict;
112 }; 120 };
113 121
114 class TestLinearized : public CPDF_LinearizedHeader { 122 class TestLinearized : public CPDF_LinearizedHeader {
115 public: 123 public:
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 ASSERT_TRUE(page->KeyExist("PageNumbering")); 195 ASSERT_TRUE(page->KeyExist("PageNumbering"));
188 EXPECT_EQ(6, page->GetIntegerFor("PageNumbering")); 196 EXPECT_EQ(6, page->GetIntegerFor("PageNumbering"));
189 } 197 }
190 198
191 TEST_F(cpdf_document_test, UseCachedPageObjNumIfHaveNotPagesDict) { 199 TEST_F(cpdf_document_test, UseCachedPageObjNumIfHaveNotPagesDict) {
192 // ObjNum can be added in CPDF_DataAvail::IsPageAvail, and PagesDict 200 // ObjNum can be added in CPDF_DataAvail::IsPageAvail, and PagesDict
193 // can be not exists in this case. 201 // can be not exists in this case.
194 // (case, when hint table is used to page check in CPDF_DataAvail). 202 // (case, when hint table is used to page check in CPDF_DataAvail).
195 CPDF_Document document(pdfium::MakeUnique<CPDF_Parser>()); 203 CPDF_Document document(pdfium::MakeUnique<CPDF_Parser>());
196 auto dict = pdfium::MakeUnique<CPDF_Dictionary>(); 204 auto dict = pdfium::MakeUnique<CPDF_Dictionary>();
197 dict->SetBooleanFor("Linearized", true); 205 dict->SetNewFor<CPDF_Boolean>("Linearized", true);
198 const int page_count = 100; 206 const int page_count = 100;
199 dict->SetIntegerFor("N", page_count); 207 dict->SetNewFor<CPDF_Number>("N", page_count);
200 TestLinearized linearized(dict.get()); 208 TestLinearized linearized(dict.get());
201 document.LoadLinearizedDoc(&linearized); 209 document.LoadLinearizedDoc(&linearized);
202 ASSERT_EQ(page_count, document.GetPageCount()); 210 ASSERT_EQ(page_count, document.GetPageCount());
203 CPDF_Object* page_stub = document.NewIndirect<CPDF_Dictionary>(); 211 CPDF_Object* page_stub = document.NewIndirect<CPDF_Dictionary>();
204 const uint32_t obj_num = page_stub->GetObjNum(); 212 const uint32_t obj_num = page_stub->GetObjNum();
205 const int test_page_num = 33; 213 const int test_page_num = 33;
206 214
207 EXPECT_FALSE(document.IsPageLoaded(test_page_num)); 215 EXPECT_FALSE(document.IsPageLoaded(test_page_num));
208 EXPECT_EQ(nullptr, document.GetPage(test_page_num)); 216 EXPECT_EQ(nullptr, document.GetPage(test_page_num));
209 217
210 document.SetPageObjNum(test_page_num, obj_num); 218 document.SetPageObjNum(test_page_num, obj_num);
211 EXPECT_TRUE(document.IsPageLoaded(test_page_num)); 219 EXPECT_TRUE(document.IsPageLoaded(test_page_num));
212 EXPECT_EQ(page_stub, document.GetPage(test_page_num)); 220 EXPECT_EQ(page_stub, document.GetPage(test_page_num));
213 } 221 }
OLDNEW
« no previous file with comments | « core/fpdfapi/parser/cpdf_document.cpp ('k') | core/fpdfapi/parser/cpdf_object_unittest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698