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 "public/fpdf_doc.h" | |
6 | |
7 #include <memory> | |
8 #include <vector> | |
9 | |
10 #include "core/include/fpdfapi/cpdf_document.h" | |
11 #include "core/include/fpdfapi/cpdf_name.h" | |
12 #include "core/include/fpdfapi/cpdf_number.h" | |
13 #include "core/include/fpdfapi/cpdf_reference.h" | |
14 #include "core/include/fpdfapi/cpdf_string.h" | |
15 #include "core/include/fpdfapi/fpdf_module.h" | |
16 #include "core/include/fpdfdoc/fpdf_doc.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "testing/test_support.h" | |
19 | |
20 #ifdef PDF_ENABLE_XFA | |
21 #include "fpdfsdk/include/fpdfxfa/fpdfxfa_app.h" | |
22 #include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h" | |
23 #endif // PDF_ENABLE_XFA | |
24 | |
25 class CPDF_TestDocument : public CPDF_Document { | |
26 public: | |
27 void SetRoot(CPDF_Dictionary* root) { m_pRootDict = root; } | |
28 CPDF_IndirectObjectHolder* GetHolder() { return this; } | |
29 }; | |
30 | |
31 #ifdef PDF_ENABLE_XFA | |
32 class CPDF_TestXFADocument : public CPDFXFA_Document { | |
33 public: | |
34 CPDF_TestXFADocument() | |
35 : CPDFXFA_Document(new CPDF_TestDocument(), CPDFXFA_App::GetInstance()) {} | |
36 | |
37 void SetRoot(CPDF_Dictionary* root) { | |
38 reinterpret_cast<CPDF_TestDocument*>(GetPDFDoc())->SetRoot(root); | |
39 } | |
40 | |
41 CPDF_IndirectObjectHolder* GetHolder() { return GetPDFDoc(); } | |
42 }; | |
43 using CPDF_TestPdfDocument = CPDF_TestXFADocument; | |
44 #else // PDF_ENABLE_XFA | |
45 using CPDF_TestPdfDocument = CPDF_TestDocument; | |
46 #endif // PDF_ENABLE_XFA | |
47 | |
48 class PDFDocTest : public testing::Test { | |
49 public: | |
50 struct DictObjInfo { | |
51 FX_DWORD num; | |
52 CPDF_Dictionary* obj; | |
53 }; | |
54 | |
55 void SetUp() override { | |
56 // We don't need page module or render module, but | |
57 // initialize them to keep the code sane. | |
58 CPDF_ModuleMgr::Create(); | |
59 CPDF_ModuleMgr* module_mgr = CPDF_ModuleMgr::Get(); | |
60 module_mgr->InitPageModule(); | |
61 module_mgr->InitRenderModule(); | |
62 | |
63 m_pDoc.reset(new CPDF_TestPdfDocument()); | |
64 m_pIndirectObjs = m_pDoc->GetHolder(); | |
65 // Setup the root directory. | |
66 m_pRootObj.reset(new CPDF_Dictionary()); | |
67 m_pDoc->SetRoot(m_pRootObj.get()); | |
68 } | |
69 | |
70 std::vector<DictObjInfo> CreateDictObjs(int num) { | |
71 std::vector<DictObjInfo> info; | |
72 for (int i = 0; i < num; ++i) { | |
73 // Objects created will be released by the document. | |
74 CPDF_Dictionary* obj(new CPDF_Dictionary()); | |
75 m_pIndirectObjs->AddIndirectObject(obj); | |
76 info.push_back({obj->GetObjNum(), obj}); | |
77 } | |
78 return info; | |
79 } | |
80 | |
81 protected: | |
82 std::unique_ptr<CPDF_TestPdfDocument> m_pDoc; | |
83 CPDF_IndirectObjectHolder* m_pIndirectObjs; | |
84 std::unique_ptr<CPDF_Dictionary, ReleaseDeleter<CPDF_Dictionary>> m_pRootObj; | |
85 }; | |
86 | |
87 TEST_F(PDFDocTest, FindBookmark) { | |
88 { | |
89 // No bookmark information. | |
90 std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = | |
91 GetFPDFWideString(L""); | |
92 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); | |
93 | |
94 title = GetFPDFWideString(L"Preface"); | |
95 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); | |
96 } | |
97 { | |
98 // Empty bookmark tree. | |
99 m_pRootObj->SetAt("Outlines", new CPDF_Dictionary()); | |
100 std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = | |
101 GetFPDFWideString(L""); | |
102 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); | |
103 | |
104 title = GetFPDFWideString(L"Preface"); | |
105 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); | |
106 } | |
107 { | |
108 // Check on a regular bookmark tree. | |
109 auto bookmarks = CreateDictObjs(3); | |
110 | |
111 bookmarks[1].obj->SetAt("Title", new CPDF_String(L"Chapter 1")); | |
112 bookmarks[1].obj->SetAt( | |
113 "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); | |
114 bookmarks[1].obj->SetAt( | |
115 "Next", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num)); | |
116 | |
117 bookmarks[2].obj->SetAt("Title", new CPDF_String(L"Chapter 2")); | |
118 bookmarks[2].obj->SetAt( | |
119 "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); | |
120 bookmarks[2].obj->SetAt( | |
121 "Prev", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); | |
122 | |
123 bookmarks[0].obj->SetAt("Type", new CPDF_Name("Outlines")); | |
124 bookmarks[0].obj->SetAt("Count", new CPDF_Number(2)); | |
125 bookmarks[0].obj->SetAt( | |
126 "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); | |
127 bookmarks[0].obj->SetAt( | |
128 "Last", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num)); | |
129 | |
130 m_pRootObj->SetAt("Outlines", | |
131 new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); | |
132 | |
133 // Title with no match. | |
134 std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = | |
135 GetFPDFWideString(L"Chapter 3"); | |
136 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); | |
137 | |
138 // Title with partial match only. | |
139 title = GetFPDFWideString(L"Chapter"); | |
140 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); | |
141 | |
142 // Title with a match. | |
143 title = GetFPDFWideString(L"Chapter 2"); | |
144 EXPECT_EQ(bookmarks[2].obj, FPDFBookmark_Find(m_pDoc.get(), title.get())); | |
145 | |
146 // Title match is case insensitive. | |
147 title = GetFPDFWideString(L"cHaPter 2"); | |
148 EXPECT_EQ(bookmarks[2].obj, FPDFBookmark_Find(m_pDoc.get(), title.get())); | |
149 } | |
150 { | |
151 // Circular bookmarks in depth. | |
152 auto bookmarks = CreateDictObjs(3); | |
153 | |
154 bookmarks[1].obj->SetAt("Title", new CPDF_String(L"Chapter 1")); | |
155 bookmarks[1].obj->SetAt( | |
156 "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); | |
157 bookmarks[1].obj->SetAt( | |
158 "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num)); | |
159 | |
160 bookmarks[2].obj->SetAt("Title", new CPDF_String(L"Chapter 2")); | |
161 bookmarks[2].obj->SetAt( | |
162 "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); | |
163 bookmarks[2].obj->SetAt( | |
164 "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); | |
165 | |
166 bookmarks[0].obj->SetAt("Type", new CPDF_Name("Outlines")); | |
167 bookmarks[0].obj->SetAt("Count", new CPDF_Number(2)); | |
168 bookmarks[0].obj->SetAt( | |
169 "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); | |
170 bookmarks[0].obj->SetAt( | |
171 "Last", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num)); | |
172 | |
173 m_pRootObj->SetAt("Outlines", | |
174 new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); | |
175 | |
176 // Title with no match. | |
177 std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = | |
178 GetFPDFWideString(L"Chapter 3"); | |
179 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); | |
180 | |
181 // Title with a match. | |
182 title = GetFPDFWideString(L"Chapter 2"); | |
183 EXPECT_EQ(bookmarks[2].obj, FPDFBookmark_Find(m_pDoc.get(), title.get())); | |
184 } | |
185 { | |
186 // Circular bookmarks in breadth. | |
187 auto bookmarks = CreateDictObjs(4); | |
188 | |
189 bookmarks[1].obj->SetAt("Title", new CPDF_String(L"Chapter 1")); | |
190 bookmarks[1].obj->SetAt( | |
191 "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); | |
192 bookmarks[1].obj->SetAt( | |
193 "Next", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num)); | |
194 | |
195 bookmarks[2].obj->SetAt("Title", new CPDF_String(L"Chapter 2")); | |
196 bookmarks[2].obj->SetAt( | |
197 "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); | |
198 bookmarks[2].obj->SetAt( | |
199 "Next", new CPDF_Reference(m_pIndirectObjs, bookmarks[3].num)); | |
200 | |
201 bookmarks[3].obj->SetAt("Title", new CPDF_String(L"Chapter 3")); | |
202 bookmarks[3].obj->SetAt( | |
203 "Parent", new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); | |
204 bookmarks[3].obj->SetAt( | |
205 "Next", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); | |
206 | |
207 bookmarks[0].obj->SetAt("Type", new CPDF_Name("Outlines")); | |
208 bookmarks[0].obj->SetAt("Count", new CPDF_Number(2)); | |
209 bookmarks[0].obj->SetAt( | |
210 "First", new CPDF_Reference(m_pIndirectObjs, bookmarks[1].num)); | |
211 bookmarks[0].obj->SetAt( | |
212 "Last", new CPDF_Reference(m_pIndirectObjs, bookmarks[2].num)); | |
213 | |
214 m_pRootObj->SetAt("Outlines", | |
215 new CPDF_Reference(m_pIndirectObjs, bookmarks[0].num)); | |
216 | |
217 // Title with no match. | |
218 std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = | |
219 GetFPDFWideString(L"Chapter 8"); | |
220 EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); | |
221 | |
222 // Title with a match. | |
223 title = GetFPDFWideString(L"Chapter 3"); | |
224 EXPECT_EQ(bookmarks[3].obj, FPDFBookmark_Find(m_pDoc.get(), title.get())); | |
225 } | |
226 } | |
OLD | NEW |