Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 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 | |
Dan Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 5 | #include <memory> |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 6 | #include <string> |
| 7 | |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 8 | #include "core/fxcrt/fx_string.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 9 | #include "public/fpdf_doc.h" |
dsinclair | 1f5f2ff | 2016-04-25 14:14:56 -0700 | [diff] [blame] | 10 | #include "public/fpdf_edit.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 11 | #include "public/fpdfview.h" |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 12 | #include "testing/embedder_test.h" |
| 13 | #include "testing/fx_string_testhelpers.h" |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
Dan Sinclair | 61046b9 | 2016-02-18 14:48:48 -0500 | [diff] [blame] | 15 | #include "testing/test_support.h" |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 16 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 17 | class FPDFDocEmbeddertest : public EmbedderTest {}; |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 18 | |
| 19 | TEST_F(FPDFDocEmbeddertest, DestGetPageIndex) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 20 | EXPECT_TRUE(OpenDocument("named_dests.pdf")); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 21 | |
| 22 | // NULL FPDF_DEST case. |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 23 | EXPECT_EQ(0U, FPDFDest_GetPageIndex(document(), nullptr)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 24 | |
| 25 | // Page number directly in item from Dests NameTree. |
| 26 | FPDF_DEST dest = FPDF_GetNamedDestByName(document(), "First"); |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 27 | EXPECT_TRUE(dest); |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 28 | EXPECT_EQ(1U, FPDFDest_GetPageIndex(document(), dest)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 29 | |
| 30 | // Page number via object reference in item from Dests NameTree. |
| 31 | dest = FPDF_GetNamedDestByName(document(), "Next"); |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 32 | EXPECT_TRUE(dest); |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 33 | EXPECT_EQ(1U, FPDFDest_GetPageIndex(document(), dest)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 34 | |
| 35 | // Page number directly in item from Dests dictionary. |
| 36 | dest = FPDF_GetNamedDestByName(document(), "FirstAlternate"); |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 37 | EXPECT_TRUE(dest); |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 38 | EXPECT_EQ(11U, FPDFDest_GetPageIndex(document(), dest)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 39 | |
| 40 | // Invalid object reference in item from Dests NameTree. |
| 41 | dest = FPDF_GetNamedDestByName(document(), "LastAlternate"); |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 42 | EXPECT_TRUE(dest); |
Lei Zhang | b9c3197 | 2015-08-11 14:09:35 -0700 | [diff] [blame] | 43 | EXPECT_EQ(0U, FPDFDest_GetPageIndex(document(), dest)); |
Tom Sepez | c8f6ab6 | 2015-01-22 11:20:06 -0800 | [diff] [blame] | 44 | } |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 45 | |
dsinclair | c59fa88 | 2016-11-08 06:55:40 -0800 | [diff] [blame] | 46 | TEST_F(FPDFDocEmbeddertest, DestGetLocationInPage) { |
| 47 | EXPECT_TRUE(OpenDocument("named_dests.pdf")); |
| 48 | |
| 49 | // NULL FPDF_DEST case. |
| 50 | EXPECT_EQ(0U, FPDFDest_GetPageIndex(document(), nullptr)); |
| 51 | |
| 52 | FPDF_DEST dest = FPDF_GetNamedDestByName(document(), "First"); |
| 53 | EXPECT_TRUE(dest); |
| 54 | |
| 55 | FPDF_BOOL hasX; |
| 56 | FPDF_BOOL hasY; |
| 57 | FPDF_BOOL hasZoom; |
| 58 | FS_FLOAT x; |
| 59 | FS_FLOAT y; |
| 60 | FS_FLOAT zoom; |
| 61 | EXPECT_TRUE( |
| 62 | FPDFDest_GetLocationInPage(dest, &hasX, &hasY, &hasZoom, &x, &y, &zoom)); |
| 63 | EXPECT_TRUE(hasX); |
| 64 | EXPECT_TRUE(hasY); |
| 65 | EXPECT_TRUE(hasZoom); |
| 66 | EXPECT_EQ(0, x); |
| 67 | EXPECT_EQ(0, y); |
| 68 | EXPECT_EQ(1, zoom); |
| 69 | } |
| 70 | |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 71 | TEST_F(FPDFDocEmbeddertest, ActionGetFilePath) { |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 72 | EXPECT_TRUE(OpenDocument("launch_action.pdf")); |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 73 | |
| 74 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 75 | ASSERT_TRUE(page); |
| 76 | |
| 77 | // The target action is nearly the size of the whole page. |
| 78 | FPDF_LINK link = FPDFLink_GetLinkAtPoint(page, 100, 100); |
| 79 | ASSERT_TRUE(link); |
| 80 | |
| 81 | FPDF_ACTION action = FPDFLink_GetAction(link); |
| 82 | ASSERT_TRUE(action); |
| 83 | |
| 84 | const char kExpectedResult[] = "test.pdf"; |
| 85 | const unsigned long kExpectedLength = sizeof(kExpectedResult); |
| 86 | unsigned long bufsize = FPDFAction_GetFilePath(action, nullptr, 0); |
| 87 | ASSERT_EQ(kExpectedLength, bufsize); |
| 88 | |
| 89 | char buf[kExpectedLength]; |
| 90 | EXPECT_EQ(bufsize, FPDFAction_GetFilePath(action, buf, bufsize)); |
| 91 | EXPECT_EQ(std::string(kExpectedResult), std::string(buf)); |
| 92 | |
| 93 | FPDF_ClosePage(page); |
| 94 | } |
Tom Sepez | 758ae14 | 2015-10-14 09:26:32 -0700 | [diff] [blame] | 95 | |
| 96 | TEST_F(FPDFDocEmbeddertest, NoBookmarks) { |
| 97 | // Open a file with no bookmarks. |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 98 | EXPECT_TRUE(OpenDocument("named_dests.pdf")); |
Tom Sepez | 758ae14 | 2015-10-14 09:26:32 -0700 | [diff] [blame] | 99 | |
| 100 | // The non-existent top-level bookmark has no title. |
| 101 | unsigned short buf[128]; |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 102 | EXPECT_EQ(0u, FPDFBookmark_GetTitle(nullptr, buf, sizeof(buf))); |
Tom Sepez | 758ae14 | 2015-10-14 09:26:32 -0700 | [diff] [blame] | 103 | |
| 104 | // The non-existent top-level bookmark has no children. |
| 105 | EXPECT_EQ(nullptr, FPDFBookmark_GetFirstChild(document(), nullptr)); |
| 106 | } |
| 107 | |
| 108 | TEST_F(FPDFDocEmbeddertest, Bookmarks) { |
| 109 | // Open a file with two bookmarks. |
Wei Li | 091f7a0 | 2015-11-09 12:09:55 -0800 | [diff] [blame] | 110 | EXPECT_TRUE(OpenDocument("bookmarks.pdf")); |
Tom Sepez | 758ae14 | 2015-10-14 09:26:32 -0700 | [diff] [blame] | 111 | |
| 112 | // The existent top-level bookmark has no title. |
| 113 | unsigned short buf[128]; |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 114 | EXPECT_EQ(0u, FPDFBookmark_GetTitle(nullptr, buf, sizeof(buf))); |
Tom Sepez | 758ae14 | 2015-10-14 09:26:32 -0700 | [diff] [blame] | 115 | |
| 116 | FPDF_BOOKMARK child = FPDFBookmark_GetFirstChild(document(), nullptr); |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 117 | EXPECT_TRUE(child); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 118 | EXPECT_EQ(34u, FPDFBookmark_GetTitle(child, buf, sizeof(buf))); |
Tom Sepez | 758ae14 | 2015-10-14 09:26:32 -0700 | [diff] [blame] | 119 | EXPECT_EQ(CFX_WideString(L"A Good Beginning"), |
| 120 | CFX_WideString::FromUTF16LE(buf, 16)); |
| 121 | |
| 122 | EXPECT_EQ(nullptr, FPDFBookmark_GetFirstChild(document(), child)); |
| 123 | |
| 124 | FPDF_BOOKMARK sibling = FPDFBookmark_GetNextSibling(document(), child); |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 125 | EXPECT_TRUE(sibling); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 126 | EXPECT_EQ(28u, FPDFBookmark_GetTitle(sibling, buf, sizeof(buf))); |
Tom Sepez | 758ae14 | 2015-10-14 09:26:32 -0700 | [diff] [blame] | 127 | EXPECT_EQ(CFX_WideString(L"A Good Ending"), |
| 128 | CFX_WideString::FromUTF16LE(buf, 13)); |
| 129 | |
| 130 | EXPECT_EQ(nullptr, FPDFBookmark_GetNextSibling(document(), sibling)); |
| 131 | } |
Tom Sepez | 0861c16 | 2016-01-04 16:35:52 -0800 | [diff] [blame] | 132 | |
| 133 | TEST_F(FPDFDocEmbeddertest, FindBookmarks) { |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 134 | // Open a file with two bookmarks. |
Tom Sepez | 0861c16 | 2016-01-04 16:35:52 -0800 | [diff] [blame] | 135 | EXPECT_TRUE(OpenDocument("bookmarks.pdf")); |
| 136 | |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 137 | // Find the first one, based on its known title. |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 138 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = |
| 139 | GetFPDFWideString(L"A Good Beginning"); |
| 140 | FPDF_BOOKMARK child = FPDFBookmark_Find(document(), title.get()); |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 141 | EXPECT_TRUE(child); |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 142 | |
| 143 | // Check that the string matches. |
| 144 | unsigned short buf[128]; |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame] | 145 | EXPECT_EQ(34u, FPDFBookmark_GetTitle(child, buf, sizeof(buf))); |
Tom Sepez | 0861c16 | 2016-01-04 16:35:52 -0800 | [diff] [blame] | 146 | EXPECT_EQ(CFX_WideString(L"A Good Beginning"), |
| 147 | CFX_WideString::FromUTF16LE(buf, 16)); |
| 148 | |
Tom Sepez | 8ab45ea | 2016-01-05 10:17:30 -0800 | [diff] [blame] | 149 | // Check that it is them same as the one returned by GetFirstChild. |
| 150 | EXPECT_EQ(child, FPDFBookmark_GetFirstChild(document(), nullptr)); |
Tom Sepez | 0861c16 | 2016-01-04 16:35:52 -0800 | [diff] [blame] | 151 | |
| 152 | // Try to find one using a non-existent title. |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 153 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> bad_title = |
| 154 | GetFPDFWideString(L"A BAD Beginning"); |
| 155 | EXPECT_EQ(nullptr, FPDFBookmark_Find(document(), bad_title.get())); |
Tom Sepez | 0861c16 | 2016-01-04 16:35:52 -0800 | [diff] [blame] | 156 | } |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 157 | |
| 158 | // Check circular bookmarks will not cause infinite loop. |
| 159 | TEST_F(FPDFDocEmbeddertest, FindBookmarks_bug420) { |
| 160 | // Open a file with circular bookmarks. |
| 161 | EXPECT_TRUE(OpenDocument("bookmarks_circular.pdf")); |
| 162 | |
| 163 | // Try to find a title. |
| 164 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> title = |
| 165 | GetFPDFWideString(L"anything"); |
| 166 | EXPECT_EQ(nullptr, FPDFBookmark_Find(document(), title.get())); |
| 167 | } |
dsinclair | 1f5f2ff | 2016-04-25 14:14:56 -0700 | [diff] [blame] | 168 | |
| 169 | TEST_F(FPDFDocEmbeddertest, DeletePage) { |
| 170 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 171 | EXPECT_EQ(1, FPDF_GetPageCount(document())); |
| 172 | FPDFPage_Delete(document(), 0); |
| 173 | EXPECT_EQ(0, FPDF_GetPageCount(document())); |
| 174 | } |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 175 | |
| 176 | TEST_F(FPDFDocEmbeddertest, NoPageLabels) { |
| 177 | EXPECT_TRUE(OpenDocument("about_blank.pdf")); |
| 178 | EXPECT_EQ(1, FPDF_GetPageCount(document())); |
| 179 | |
dsinclair | 6bdb56c | 2016-12-06 09:53:27 -0800 | [diff] [blame^] | 180 | ASSERT_EQ(0u, FPDF_GetPageLabel(document(), 0, nullptr, 0)); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | TEST_F(FPDFDocEmbeddertest, GetPageLabels) { |
| 184 | EXPECT_TRUE(OpenDocument("page_labels.pdf")); |
| 185 | EXPECT_EQ(7, FPDF_GetPageCount(document())); |
| 186 | |
| 187 | unsigned short buf[128]; |
dsinclair | 6bdb56c | 2016-12-06 09:53:27 -0800 | [diff] [blame^] | 188 | EXPECT_EQ(0u, FPDF_GetPageLabel(document(), -2, buf, sizeof(buf))); |
| 189 | EXPECT_EQ(0u, FPDF_GetPageLabel(document(), -1, buf, sizeof(buf))); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 190 | |
| 191 | const FX_WCHAR kExpectedPageLabel0[] = L"i"; |
dsinclair | 6bdb56c | 2016-12-06 09:53:27 -0800 | [diff] [blame^] | 192 | ASSERT_EQ(4u, FPDF_GetPageLabel(document(), 0, buf, sizeof(buf))); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 193 | EXPECT_EQ(CFX_WideString(kExpectedPageLabel0), |
| 194 | CFX_WideString::FromUTF16LE(buf, FXSYS_len(kExpectedPageLabel0))); |
| 195 | |
| 196 | const FX_WCHAR kExpectedPageLabel1[] = L"ii"; |
dsinclair | 6bdb56c | 2016-12-06 09:53:27 -0800 | [diff] [blame^] | 197 | ASSERT_EQ(6u, FPDF_GetPageLabel(document(), 1, buf, sizeof(buf))); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 198 | EXPECT_EQ(CFX_WideString(kExpectedPageLabel1), |
| 199 | CFX_WideString::FromUTF16LE(buf, FXSYS_len(kExpectedPageLabel1))); |
| 200 | |
| 201 | const FX_WCHAR kExpectedPageLabel2[] = L"1"; |
dsinclair | 6bdb56c | 2016-12-06 09:53:27 -0800 | [diff] [blame^] | 202 | ASSERT_EQ(4u, FPDF_GetPageLabel(document(), 2, buf, sizeof(buf))); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 203 | EXPECT_EQ(CFX_WideString(kExpectedPageLabel2), |
| 204 | CFX_WideString::FromUTF16LE(buf, FXSYS_len(kExpectedPageLabel2))); |
| 205 | |
| 206 | const FX_WCHAR kExpectedPageLabel3[] = L"2"; |
dsinclair | 6bdb56c | 2016-12-06 09:53:27 -0800 | [diff] [blame^] | 207 | ASSERT_EQ(4u, FPDF_GetPageLabel(document(), 3, buf, sizeof(buf))); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 208 | EXPECT_EQ(CFX_WideString(kExpectedPageLabel3), |
| 209 | CFX_WideString::FromUTF16LE(buf, FXSYS_len(kExpectedPageLabel3))); |
| 210 | |
| 211 | const FX_WCHAR kExpectedPageLabel4[] = L"zzA"; |
dsinclair | 6bdb56c | 2016-12-06 09:53:27 -0800 | [diff] [blame^] | 212 | ASSERT_EQ(8u, FPDF_GetPageLabel(document(), 4, buf, sizeof(buf))); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 213 | EXPECT_EQ(CFX_WideString(kExpectedPageLabel4), |
| 214 | CFX_WideString::FromUTF16LE(buf, FXSYS_len(kExpectedPageLabel4))); |
| 215 | |
| 216 | const FX_WCHAR kExpectedPageLabel5[] = L"zzB"; |
dsinclair | 6bdb56c | 2016-12-06 09:53:27 -0800 | [diff] [blame^] | 217 | ASSERT_EQ(8u, FPDF_GetPageLabel(document(), 5, buf, sizeof(buf))); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 218 | EXPECT_EQ(CFX_WideString(kExpectedPageLabel5), |
| 219 | CFX_WideString::FromUTF16LE(buf, FXSYS_len(kExpectedPageLabel5))); |
| 220 | |
| 221 | const FX_WCHAR kExpectedPageLabel6[] = L""; |
dsinclair | 6bdb56c | 2016-12-06 09:53:27 -0800 | [diff] [blame^] | 222 | ASSERT_EQ(2u, FPDF_GetPageLabel(document(), 6, buf, sizeof(buf))); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 223 | EXPECT_EQ(CFX_WideString(kExpectedPageLabel6), |
| 224 | CFX_WideString::FromUTF16LE(buf, FXSYS_len(kExpectedPageLabel6))); |
| 225 | |
dsinclair | 6bdb56c | 2016-12-06 09:53:27 -0800 | [diff] [blame^] | 226 | ASSERT_EQ(0u, FPDF_GetPageLabel(document(), 7, buf, sizeof(buf))); |
| 227 | ASSERT_EQ(0u, FPDF_GetPageLabel(document(), 8, buf, sizeof(buf))); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 228 | } |