OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 <string> | |
6 | |
7 #include "core/include/fxcrt/fx_string.h" | |
8 #include "public/fpdf_doc.h" | |
9 #include "public/fpdfview.h" | |
10 #include "testing/embedder_test.h" | |
11 #include "testing/fx_string_testhelpers.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 #include "testing/test_support.h" | |
14 | |
15 class FPDFDocEmbeddertest : public EmbedderTest {}; | |
16 | |
17 TEST_F(FPDFDocEmbeddertest, DestGetPageIndex) { | |
18 EXPECT_TRUE(OpenDocument("named_dests.pdf")); | |
19 | |
20 // NULL FPDF_DEST case. | |
21 EXPECT_EQ(0U, FPDFDest_GetPageIndex(document(), nullptr)); | |
22 | |
23 // Page number directly in item from Dests NameTree. | |
24 FPDF_DEST dest = FPDF_GetNamedDestByName(document(), "First"); | |
25 EXPECT_NE(nullptr, dest); | |
26 EXPECT_EQ(1U, FPDFDest_GetPageIndex(document(), dest)); | |
27 | |
28 // Page number via object reference in item from Dests NameTree. | |
29 dest = FPDF_GetNamedDestByName(document(), "Next"); | |
30 EXPECT_NE(nullptr, dest); | |
31 EXPECT_EQ(1U, FPDFDest_GetPageIndex(document(), dest)); | |
32 | |
33 // Page number directly in item from Dests dictionary. | |
34 dest = FPDF_GetNamedDestByName(document(), "FirstAlternate"); | |
35 EXPECT_NE(nullptr, dest); | |
36 EXPECT_EQ(11U, FPDFDest_GetPageIndex(document(), dest)); | |
37 | |
38 // Invalid object reference in item from Dests NameTree. | |
39 dest = FPDF_GetNamedDestByName(document(), "LastAlternate"); | |
40 EXPECT_NE(nullptr, dest); | |
41 EXPECT_EQ(0U, FPDFDest_GetPageIndex(document(), dest)); | |
42 } | |
43 | |
44 TEST_F(FPDFDocEmbeddertest, ActionGetFilePath) { | |
45 EXPECT_TRUE(OpenDocument("launch_action.pdf")); | |
46 | |
47 FPDF_PAGE page = FPDF_LoadPage(document(), 0); | |
48 ASSERT_TRUE(page); | |
49 | |
50 // The target action is nearly the size of the whole page. | |
51 FPDF_LINK link = FPDFLink_GetLinkAtPoint(page, 100, 100); | |
52 ASSERT_TRUE(link); | |
53 | |
54 FPDF_ACTION action = FPDFLink_GetAction(link); | |
55 ASSERT_TRUE(action); | |
56 | |
57 const char kExpectedResult[] = "test.pdf"; | |
58 const unsigned long kExpectedLength = sizeof(kExpectedResult); | |
59 unsigned long bufsize = FPDFAction_GetFilePath(action, nullptr, 0); | |
60 ASSERT_EQ(kExpectedLength, bufsize); | |
61 | |
62 char buf[kExpectedLength]; | |
63 EXPECT_EQ(bufsize, FPDFAction_GetFilePath(action, buf, bufsize)); | |
64 EXPECT_EQ(std::string(kExpectedResult), std::string(buf)); | |
65 | |
66 FPDF_ClosePage(page); | |
67 } | |
68 | |
69 TEST_F(FPDFDocEmbeddertest, NoBookmarks) { | |
70 // Open a file with no bookmarks. | |
71 EXPECT_TRUE(OpenDocument("named_dests.pdf")); | |
72 | |
73 // The non-existent top-level bookmark has no title. | |
74 unsigned short buf[128]; | |
75 EXPECT_EQ(0, FPDFBookmark_GetTitle(nullptr, buf, sizeof(buf))); | |
76 | |
77 // The non-existent top-level bookmark has no children. | |
78 EXPECT_EQ(nullptr, FPDFBookmark_GetFirstChild(document(), nullptr)); | |
79 } | |
80 | |
81 TEST_F(FPDFDocEmbeddertest, Bookmarks) { | |
82 // Open a file with two bookmarks. | |
83 EXPECT_TRUE(OpenDocument("bookmarks.pdf")); | |
84 | |
85 // The existent top-level bookmark has no title. | |
86 unsigned short buf[128]; | |
87 EXPECT_EQ(0, FPDFBookmark_GetTitle(nullptr, buf, sizeof(buf))); | |
88 | |
89 FPDF_BOOKMARK child = FPDFBookmark_GetFirstChild(document(), nullptr); | |
90 EXPECT_NE(nullptr, child); | |
91 EXPECT_EQ(34, FPDFBookmark_GetTitle(child, buf, sizeof(buf))); | |
92 EXPECT_EQ(CFX_WideString(L"A Good Beginning"), | |
93 CFX_WideString::FromUTF16LE(buf, 16)); | |
94 | |
95 EXPECT_EQ(nullptr, FPDFBookmark_GetFirstChild(document(), child)); | |
96 | |
97 FPDF_BOOKMARK sibling = FPDFBookmark_GetNextSibling(document(), child); | |
98 EXPECT_NE(nullptr, sibling); | |
99 EXPECT_EQ(28, FPDFBookmark_GetTitle(sibling, buf, sizeof(buf))); | |
100 EXPECT_EQ(CFX_WideString(L"A Good Ending"), | |
101 CFX_WideString::FromUTF16LE(buf, 13)); | |
102 | |
103 EXPECT_EQ(nullptr, FPDFBookmark_GetNextSibling(document(), sibling)); | |
104 } | |
105 | |
106 TEST_F(FPDFDocEmbeddertest, FindBookmarks) { | |
107 // Open a file with two bookmarks. | |
108 EXPECT_TRUE(OpenDocument("bookmarks.pdf")); | |
109 | |
110 // Find the first one, based on its known title. | |
111 std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = | |
112 GetFPDFWideString(L"A Good Beginning"); | |
113 FPDF_BOOKMARK child = FPDFBookmark_Find(document(), title.get()); | |
114 EXPECT_NE(nullptr, child); | |
115 | |
116 // Check that the string matches. | |
117 unsigned short buf[128]; | |
118 EXPECT_EQ(34, FPDFBookmark_GetTitle(child, buf, sizeof(buf))); | |
119 EXPECT_EQ(CFX_WideString(L"A Good Beginning"), | |
120 CFX_WideString::FromUTF16LE(buf, 16)); | |
121 | |
122 // Check that it is them same as the one returned by GetFirstChild. | |
123 EXPECT_EQ(child, FPDFBookmark_GetFirstChild(document(), nullptr)); | |
124 | |
125 // Try to find one using a non-existent title. | |
126 std::unique_ptr<unsigned short, pdfium::FreeDeleter> bad_title = | |
127 GetFPDFWideString(L"A BAD Beginning"); | |
128 EXPECT_EQ(nullptr, FPDFBookmark_Find(document(), bad_title.get())); | |
129 } | |
130 | |
131 // Check circular bookmarks will not cause infinite loop. | |
132 TEST_F(FPDFDocEmbeddertest, FindBookmarks_bug420) { | |
133 // Open a file with circular bookmarks. | |
134 EXPECT_TRUE(OpenDocument("bookmarks_circular.pdf")); | |
135 | |
136 // Try to find a title. | |
137 std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = | |
138 GetFPDFWideString(L"anything"); | |
139 EXPECT_EQ(nullptr, FPDFBookmark_Find(document(), title.get())); | |
140 } | |
OLD | NEW |