Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 1 | // Copyright 2017 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 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 5 | #include <algorithm> |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 6 | #include <cwchar> |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <string> |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 11 | #include "constants/annotation_common.h" |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 12 | #include "core/fxcrt/fx_system.h" |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 13 | #include "public/cpp/fpdf_scopers.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 14 | #include "public/fpdf_annot.h" |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 15 | #include "public/fpdf_edit.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 16 | #include "public/fpdfview.h" |
| 17 | #include "testing/embedder_test.h" |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 18 | #include "testing/fx_string_testhelpers.h" |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 19 | #include "testing/gmock/include/gmock/gmock-matchers.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 20 | #include "testing/gtest/include/gtest/gtest.h" |
| 21 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 22 | class FPDFAnnotEmbedderTest : public EmbedderTest {}; |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 23 | |
Lei Zhang | 4afee17 | 2018-01-10 20:57:15 +0000 | [diff] [blame] | 24 | std::wstring BufferToWString(const std::vector<char>& buf) { |
| 25 | return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data())); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Lei Zhang | 4afee17 | 2018-01-10 20:57:15 +0000 | [diff] [blame] | 28 | std::string BufferToString(const std::vector<char>& buf) { |
| 29 | return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data())); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 32 | TEST_F(FPDFAnnotEmbedderTest, BadParams) { |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 33 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 34 | FPDF_PAGE page = LoadPage(0); |
| 35 | ASSERT_TRUE(page); |
| 36 | |
| 37 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr)); |
| 38 | |
| 39 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0)); |
| 40 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1)); |
| 41 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1)); |
| 42 | EXPECT_FALSE(FPDFPage_GetAnnot(page, -1)); |
| 43 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 1)); |
| 44 | |
| 45 | EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr)); |
| 46 | |
| 47 | EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr)); |
| 48 | |
| 49 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0)); |
| 50 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1)); |
| 51 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1)); |
| 52 | |
| 53 | EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo")); |
| 54 | |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 55 | static const wchar_t kContents[] = L"Bar"; |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame^] | 56 | ScopedFPDFWideString text = GetFPDFWideString(kContents); |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 57 | EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get())); |
| 58 | |
| 59 | char buffer[128]; |
| 60 | EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0)); |
| 61 | EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0)); |
| 62 | EXPECT_EQ(0u, |
| 63 | FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer))); |
| 64 | |
| 65 | UnloadPage(page); |
| 66 | } |
| 67 | |
Lei Zhang | 3d9a097 | 2019-03-04 19:34:09 +0000 | [diff] [blame] | 68 | TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) { |
| 69 | ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf")); |
| 70 | FPDF_PAGE page = LoadPage(0); |
| 71 | ASSERT_TRUE(page); |
| 72 | |
| 73 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Lei Zhang | 98dc8c0 | 2019-03-04 19:40:30 +0000 | [diff] [blame] | 74 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | 3d9a097 | 2019-03-04 19:34:09 +0000 | [diff] [blame] | 75 | |
| 76 | UnloadPage(page); |
| 77 | } |
| 78 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 79 | TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) { |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 80 | // Open a file with one annotation and load its first page. |
| 81 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 82 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 83 | ASSERT_TRUE(page); |
| 84 | |
| 85 | // This annotation has a malformed appearance stream, which does not have its |
| 86 | // normal appearance defined, only its rollover appearance. In this case, its |
| 87 | // normal appearance should be generated, allowing the highlight annotation to |
| 88 | // still display. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 89 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 90 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 91 | |
| 92 | UnloadPage(page); |
| 93 | } |
| 94 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 95 | TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) { |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 96 | static const char kMd5[] = "76512832d88017668d9acc7aacd13dae"; |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 97 | // Open a file with multiline markup annotations. |
Ralf Sippl | b3a5240 | 2018-03-19 23:30:28 +0000 | [diff] [blame] | 98 | ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf")); |
| 99 | FPDF_PAGE page = LoadPage(0); |
| 100 | ASSERT_TRUE(page); |
| 101 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 102 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 103 | CompareBitmap(bitmap.get(), 595, 842, kMd5); |
Ralf Sippl | b3a5240 | 2018-03-19 23:30:28 +0000 | [diff] [blame] | 104 | |
| 105 | UnloadPage(page); |
| 106 | } |
| 107 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 108 | TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) { |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 109 | // Open a file with one annotation and load its first page. |
| 110 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 111 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 112 | ASSERT_TRUE(page); |
| 113 | |
| 114 | // Check that there is a total of 1 annotation on its first page. |
| 115 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 116 | |
| 117 | // Check that the annotation is of type "highlight". |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 118 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 119 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 120 | ASSERT_TRUE(annot); |
| 121 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 122 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 123 | // Check that the annotation color is yellow. |
| 124 | unsigned int R; |
| 125 | unsigned int G; |
| 126 | unsigned int B; |
| 127 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 128 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 129 | &G, &B, &A)); |
| 130 | EXPECT_EQ(255u, R); |
| 131 | EXPECT_EQ(255u, G); |
| 132 | EXPECT_EQ(0u, B); |
| 133 | EXPECT_EQ(255u, A); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 134 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 135 | // Check that the author is correct. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 136 | static const char kAuthorKey[] = "T"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 137 | EXPECT_EQ(FPDF_OBJECT_STRING, |
| 138 | FPDFAnnot_GetValueType(annot.get(), kAuthorKey)); |
| 139 | unsigned long len = |
| 140 | FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0); |
| 141 | std::vector<char> buf(len); |
| 142 | EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(), |
| 143 | len)); |
| 144 | EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str()); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 145 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 146 | // Check that the content is correct. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 147 | EXPECT_EQ( |
| 148 | FPDF_OBJECT_STRING, |
| 149 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents)); |
| 150 | len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kContents, |
| 151 | nullptr, 0); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 152 | buf.clear(); |
| 153 | buf.resize(len); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 154 | EXPECT_EQ(2690u, |
| 155 | FPDFAnnot_GetStringValue( |
| 156 | annot.get(), pdfium::annotation::kContents, buf.data(), len)); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 157 | static const wchar_t kContents[] = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 158 | L"This is a note for that highlight annotation. Very long highlight " |
| 159 | "annotation. Long long long Long long longLong long longLong long " |
| 160 | "longLong long longLong long longLong long longLong long longLong long " |
| 161 | "longLong long longLong long longLong long longLong long longLong long " |
| 162 | "longLong long longLong long longLong long longLong long longLong long " |
| 163 | "longLong long longLong long longLong long longLong long longLong long " |
| 164 | "longLong long longLong long longLong long longLong long longLong long " |
| 165 | "longLong long longLong long longLong long longLong long longLong long " |
| 166 | "longLong long longLong long longLong long longLong long longLong long " |
| 167 | "longLong long longLong long longLong long longLong long longLong long " |
| 168 | "longLong long longLong long longLong long longLong long longLong long " |
| 169 | "longLong long longLong long longLong long longLong long longLong long " |
| 170 | "longLong long longLong long longLong long longLong long longLong long " |
| 171 | "longLong long longLong long longLong long longLong long longLong long " |
| 172 | "longLong long longLong long longLong long longLong long longLong long " |
| 173 | "longLong long longLong long longLong long longLong long longLong long " |
| 174 | "longLong long longLong long longLong long longLong long longLong long " |
| 175 | "longLong long longLong long longLong long longLong long longLong long " |
| 176 | "longLong long longLong long longLong long longLong long longLong long " |
| 177 | "longLong long long. END"; |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 178 | EXPECT_STREQ(kContents, BufferToWString(buf).c_str()); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 179 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 180 | // Check that the quadpoints are correct. |
| 181 | FS_QUADPOINTSF quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 182 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 183 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 184 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 185 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 186 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 187 | } |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 188 | UnloadPageNoEvents(page); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 191 | TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) { |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 192 | // Open a file with three annotations and load its first page. |
| 193 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 194 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 195 | ASSERT_TRUE(page); |
| 196 | |
| 197 | // Check that there is a total of 3 annotation on its first page. |
| 198 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 199 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 200 | { |
| 201 | // Check that the third annotation is of type "ink". |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 202 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 203 | ASSERT_TRUE(annot); |
| 204 | EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 205 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 206 | // Check that the annotation color is blue with opacity. |
| 207 | unsigned int R; |
| 208 | unsigned int G; |
| 209 | unsigned int B; |
| 210 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 211 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 212 | &G, &B, &A)); |
| 213 | EXPECT_EQ(0u, R); |
| 214 | EXPECT_EQ(0u, G); |
| 215 | EXPECT_EQ(255u, B); |
| 216 | EXPECT_EQ(76u, A); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 217 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 218 | // Check that there is no content. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 219 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue( |
| 220 | annot.get(), pdfium::annotation::kContents, nullptr, 0)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 221 | |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 222 | // Check that the rectangle coordinates are correct. |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 223 | // Note that upon rendering, the rectangle coordinates will be adjusted. |
| 224 | FS_RECTF rect; |
| 225 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 226 | EXPECT_EQ(351.820404f, rect.left); |
| 227 | EXPECT_EQ(583.830688f, rect.bottom); |
| 228 | EXPECT_EQ(475.336090f, rect.right); |
| 229 | EXPECT_EQ(681.535034f, rect.top); |
| 230 | } |
Tom Sepez | ef43c26 | 2018-11-07 16:41:32 +0000 | [diff] [blame] | 231 | { |
| 232 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 233 | CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a"); |
| 234 | } |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 235 | UnloadPageNoEvents(page); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 236 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 237 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 238 | TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 239 | // Open a file with one annotation and load its first page. |
| 240 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 241 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 242 | ASSERT_TRUE(page); |
| 243 | |
| 244 | // Add an annotation with an illegal subtype. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 245 | ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 246 | |
| 247 | UnloadPage(page); |
| 248 | } |
| 249 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 250 | TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 251 | // Open a file with no annotation and load its first page. |
| 252 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 253 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 254 | ASSERT_TRUE(page); |
| 255 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 256 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 257 | { |
| 258 | // Add a text annotation to the page. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 259 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 260 | ASSERT_TRUE(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 261 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 262 | // Check that there is now 1 annotations on this page. |
| 263 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 264 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 265 | // Check that the subtype of the annotation is correct. |
| 266 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 267 | } |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 268 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 269 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 270 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 271 | ASSERT_TRUE(annot); |
| 272 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 273 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 274 | // Set the color of the annotation. |
| 275 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51, |
| 276 | 102, 153, 204)); |
| 277 | // Check that the color has been set correctly. |
| 278 | unsigned int R; |
| 279 | unsigned int G; |
| 280 | unsigned int B; |
| 281 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 282 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 283 | &G, &B, &A)); |
| 284 | EXPECT_EQ(51u, R); |
| 285 | EXPECT_EQ(102u, G); |
| 286 | EXPECT_EQ(153u, B); |
| 287 | EXPECT_EQ(204u, A); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 288 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 289 | // Change the color of the annotation. |
| 290 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204, |
| 291 | 153, 102, 51)); |
| 292 | // Check that the color has been set correctly. |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 293 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 294 | &G, &B, &A)); |
| 295 | EXPECT_EQ(204u, R); |
| 296 | EXPECT_EQ(153u, G); |
| 297 | EXPECT_EQ(102u, B); |
| 298 | EXPECT_EQ(51u, A); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 299 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 300 | // Set the annotation rectangle. |
| 301 | FS_RECTF rect; |
| 302 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 303 | EXPECT_EQ(0.f, rect.left); |
| 304 | EXPECT_EQ(0.f, rect.right); |
| 305 | rect.left = 35; |
| 306 | rect.bottom = 150; |
| 307 | rect.right = 53; |
| 308 | rect.top = 165; |
| 309 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 310 | // Check that the annotation rectangle has been set correctly. |
| 311 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 312 | EXPECT_EQ(35.f, rect.left); |
| 313 | EXPECT_EQ(150.f, rect.bottom); |
| 314 | EXPECT_EQ(53.f, rect.right); |
| 315 | EXPECT_EQ(165.f, rect.top); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 316 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 317 | // Set the content of the annotation. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 318 | static const wchar_t kContents[] = L"Hello! This is a customized content."; |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame^] | 319 | ScopedFPDFWideString text = GetFPDFWideString(kContents); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 320 | ASSERT_TRUE(FPDFAnnot_SetStringValue( |
| 321 | annot.get(), pdfium::annotation::kContents, text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 322 | // Check that the content has been set correctly. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 323 | unsigned long len = FPDFAnnot_GetStringValue( |
| 324 | annot.get(), pdfium::annotation::kContents, nullptr, 0); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 325 | std::vector<char> buf(len); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 326 | EXPECT_EQ(74u, |
| 327 | FPDFAnnot_GetStringValue( |
| 328 | annot.get(), pdfium::annotation::kContents, buf.data(), len)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 329 | EXPECT_STREQ(kContents, BufferToWString(buf).c_str()); |
| 330 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 331 | UnloadPage(page); |
| 332 | } |
| 333 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 334 | TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 335 | // Open a file with one annotation and load its first page. |
| 336 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 337 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 338 | ASSERT_TRUE(page); |
| 339 | |
| 340 | // Check that there is a total of one annotation on its first page, and verify |
| 341 | // its quadpoints. |
| 342 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 343 | FS_QUADPOINTSF quadpoints; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 344 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 345 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 346 | ASSERT_TRUE(annot); |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 347 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 348 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 349 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 350 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 351 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 352 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 353 | |
| 354 | // Add an underline annotation to the page and set its quadpoints. |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 355 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 356 | ScopedFPDFAnnotation annot( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 357 | FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE)); |
| 358 | ASSERT_TRUE(annot); |
| 359 | quadpoints.x1 = 140.802643f; |
| 360 | quadpoints.x3 = 140.802643f; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 361 | ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 362 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 363 | |
| 364 | // Save the document, closing the page and document. |
| 365 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 366 | UnloadPage(page); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 367 | |
| 368 | // Open the saved document. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 369 | static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5"; |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 370 | |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 371 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 372 | page = LoadSavedPage(0); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 373 | VerifySavedRendering(page, 612, 792, kMd5); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 374 | |
| 375 | // Check that the saved document has 2 annotations on the first page |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 376 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 377 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 378 | { |
| 379 | // Check that the second annotation is an underline annotation and verify |
| 380 | // its quadpoints. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 381 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 382 | ASSERT_TRUE(new_annot); |
| 383 | EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get())); |
| 384 | FS_QUADPOINTSF new_quadpoints; |
| 385 | ASSERT_TRUE( |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 386 | FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 387 | EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f); |
| 388 | EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f); |
| 389 | EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f); |
| 390 | EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f); |
| 391 | } |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 392 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 393 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 394 | CloseSavedDocument(); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 395 | } |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 396 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 397 | TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) { |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 398 | // Open a file with four annotations and load its first page. |
| 399 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
| 400 | FPDF_PAGE page = LoadPage(0); |
| 401 | ASSERT_TRUE(page); |
| 402 | EXPECT_EQ(4, FPDFPage_GetAnnotCount(page)); |
| 403 | |
| 404 | // Retrieve the highlight annotation. |
| 405 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 406 | ASSERT_TRUE(annot); |
| 407 | ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot)); |
| 408 | |
| 409 | FS_QUADPOINTSF quadpoints; |
| 410 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints)); |
| 411 | |
| 412 | { |
| 413 | // Verify the current one set of quadpoints. |
| 414 | ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 415 | |
| 416 | EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f); |
| 417 | EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f); |
| 418 | EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f); |
| 419 | EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f); |
| 420 | } |
| 421 | |
| 422 | { |
| 423 | // Update the quadpoints. |
| 424 | FS_QUADPOINTSF new_quadpoints = quadpoints; |
| 425 | new_quadpoints.y1 -= 20.f; |
| 426 | new_quadpoints.y2 -= 20.f; |
| 427 | new_quadpoints.y3 -= 20.f; |
| 428 | new_quadpoints.y4 -= 20.f; |
| 429 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints)); |
| 430 | |
| 431 | // Verify added quadpoint set |
| 432 | ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 433 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints)); |
| 434 | EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f); |
| 435 | EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f); |
| 436 | EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f); |
| 437 | EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f); |
| 438 | } |
| 439 | |
| 440 | { |
| 441 | // Append a new set of quadpoints. |
| 442 | FS_QUADPOINTSF new_quadpoints = quadpoints; |
| 443 | new_quadpoints.y1 += 20.f; |
| 444 | new_quadpoints.y2 += 20.f; |
| 445 | new_quadpoints.y3 += 20.f; |
| 446 | new_quadpoints.y4 += 20.f; |
| 447 | ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints)); |
| 448 | |
| 449 | // Verify added quadpoint set |
| 450 | ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 451 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints)); |
| 452 | EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f); |
| 453 | EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f); |
| 454 | EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f); |
| 455 | EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f); |
| 456 | } |
| 457 | |
| 458 | { |
| 459 | // Setting and getting quadpoints at out-of-bound index should fail |
| 460 | EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints)); |
| 461 | EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints)); |
| 462 | } |
| 463 | |
| 464 | FPDFPage_CloseAnnot(annot); |
| 465 | |
| 466 | // Retrieve the square annotation |
| 467 | FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2); |
| 468 | |
| 469 | { |
| 470 | // Check that attempting to set its quadpoints would fail |
| 471 | ASSERT_TRUE(squareAnnot); |
| 472 | EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot)); |
| 473 | EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot)); |
| 474 | EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints)); |
| 475 | } |
| 476 | |
| 477 | FPDFPage_CloseAnnot(squareAnnot); |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 478 | UnloadPage(page); |
| 479 | } |
| 480 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 481 | TEST_F(FPDFAnnotEmbedderTest, ModifyRectQuadpointsWithAP) { |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 482 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 483 | static const char kMd5Original[] = "63af8432fab95a67cdebb7cd0e514941"; |
| 484 | static const char kMd5ModifiedHighlight[] = |
| 485 | "aec26075011349dec9bace891856b5f2"; |
| 486 | static const char kMd5ModifiedSquare[] = "057f57a32be95975775e5ec513fdcb56"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 487 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 488 | static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5"; |
| 489 | static const char kMd5ModifiedHighlight[] = |
| 490 | "66f3caef3a7d488a4fa1ad37fc06310e"; |
| 491 | static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563"; |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 492 | #else |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 493 | static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5"; |
| 494 | static const char kMd5ModifiedHighlight[] = |
| 495 | "66f3caef3a7d488a4fa1ad37fc06310e"; |
| 496 | static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563"; |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 497 | #endif |
| 498 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 499 | // Open a file with four annotations and load its first page. |
| 500 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 501 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 502 | ASSERT_TRUE(page); |
| 503 | EXPECT_EQ(4, FPDFPage_GetAnnotCount(page)); |
| 504 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 505 | // Check that the original file renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 506 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 507 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 508 | CompareBitmap(bitmap.get(), 612, 792, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 509 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 510 | |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 511 | FS_RECTF rect; |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 512 | FS_RECTF new_rect; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 513 | |
| 514 | // Retrieve the highlight annotation which has its AP stream already defined. |
| 515 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 516 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 517 | ASSERT_TRUE(annot); |
| 518 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 519 | |
| 520 | // Check that color cannot be set when an AP stream is defined already. |
| 521 | EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51, |
| 522 | 102, 153, 204)); |
| 523 | |
| 524 | // Verify its attachment points. |
| 525 | FS_QUADPOINTSF quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 526 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 527 | EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f); |
| 528 | EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f); |
| 529 | EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f); |
| 530 | EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f); |
| 531 | |
| 532 | // Check that updating the attachment points would succeed. |
| 533 | quadpoints.x1 -= 50.f; |
| 534 | quadpoints.x2 -= 50.f; |
| 535 | quadpoints.x3 -= 50.f; |
| 536 | quadpoints.x4 -= 50.f; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 537 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 538 | FS_QUADPOINTSF new_quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 539 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 540 | EXPECT_EQ(quadpoints.x1, new_quadpoints.x1); |
| 541 | EXPECT_EQ(quadpoints.y1, new_quadpoints.y1); |
| 542 | EXPECT_EQ(quadpoints.x4, new_quadpoints.x4); |
| 543 | EXPECT_EQ(quadpoints.y4, new_quadpoints.y4); |
| 544 | |
| 545 | // Check that updating quadpoints does not change the annotation's position. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 546 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 547 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 548 | CompareBitmap(bitmap.get(), 612, 792, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 549 | } |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 550 | |
| 551 | // Verify its annotation rectangle. |
| 552 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 553 | EXPECT_NEAR(67.7299f, rect.left, 0.001f); |
| 554 | EXPECT_NEAR(704.296f, rect.bottom, 0.001f); |
| 555 | EXPECT_NEAR(136.325f, rect.right, 0.001f); |
| 556 | EXPECT_NEAR(721.292f, rect.top, 0.001f); |
| 557 | |
| 558 | // Check that updating the rectangle would succeed. |
| 559 | rect.left -= 60.f; |
| 560 | rect.right -= 60.f; |
| 561 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 562 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 563 | EXPECT_EQ(rect.right, new_rect.right); |
| 564 | } |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 565 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 566 | // Check that updating the rectangle changes the annotation's position. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 567 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 568 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 569 | CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 570 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 571 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 572 | { |
| 573 | // Retrieve the square annotation which has its AP stream already defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 574 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 575 | ASSERT_TRUE(annot); |
| 576 | EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 577 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 578 | // Check that updating the rectangle would succeed. |
| 579 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 580 | rect.left += 70.f; |
| 581 | rect.right += 70.f; |
| 582 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 583 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 584 | EXPECT_EQ(rect.right, new_rect.right); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 585 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 586 | // Check that updating the rectangle changes the square annotation's |
| 587 | // position. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 588 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 589 | CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 590 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 591 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 592 | UnloadPage(page); |
| 593 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 594 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 595 | TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) { |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 596 | // Open a file with multiline markup annotations. |
| 597 | ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf")); |
| 598 | FPDF_PAGE page = LoadPage(0); |
| 599 | ASSERT_TRUE(page); |
| 600 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 601 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 602 | ASSERT_TRUE(annot); |
| 603 | |
| 604 | // This is a three line annotation. |
| 605 | EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get())); |
| 606 | } |
| 607 | UnloadPage(page); |
| 608 | |
| 609 | // null annotation should return 0 |
| 610 | EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr)); |
| 611 | } |
| 612 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 613 | TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) { |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 614 | // Open a file with 3 annotations on its first page. |
| 615 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 616 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 617 | ASSERT_TRUE(page); |
| 618 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 619 | |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 620 | FS_RECTF rect; |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 621 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 622 | // Check that the annotations have the expected rectangle coordinates. |
| 623 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 624 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 625 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 626 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 627 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 628 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 629 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 630 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 631 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 632 | EXPECT_NEAR(149.8127f, rect.left, 0.001f); |
| 633 | } |
| 634 | |
| 635 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 636 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 637 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 638 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 639 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 640 | |
| 641 | // Check that nothing happens when attempting to remove an annotation with an |
| 642 | // out-of-bound index. |
| 643 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4)); |
| 644 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1)); |
| 645 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 646 | |
| 647 | // Remove the second annotation. |
| 648 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1)); |
| 649 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 650 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 2)); |
| 651 | |
| 652 | // Save the document, closing the page and document. |
| 653 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 654 | UnloadPageNoEvents(page); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 655 | |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 656 | // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1?? |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 657 | // Open the saved document. |
| 658 | std::string new_file = GetString(); |
| 659 | FPDF_FILEACCESS file_access; |
| 660 | memset(&file_access, 0, sizeof(file_access)); |
| 661 | file_access.m_FileLen = new_file.size(); |
| 662 | file_access.m_GetBlock = GetBlockFromString; |
| 663 | file_access.m_Param = &new_file; |
| 664 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 665 | ASSERT_TRUE(new_doc); |
| 666 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 667 | ASSERT_TRUE(new_page); |
| 668 | |
| 669 | // Check that the saved document has 2 annotations on the first page. |
| 670 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page)); |
| 671 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 672 | // Check that the remaining 2 annotations are the original 1st and 3rd ones |
| 673 | // by verifying their rectangle coordinates. |
| 674 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 675 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 676 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 677 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 678 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 679 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 680 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 681 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 682 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 683 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 684 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 685 | FPDF_ClosePage(new_page); |
| 686 | FPDF_CloseDocument(new_doc); |
| 687 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 688 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 689 | TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) { |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 690 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 691 | static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2"; |
| 692 | static const char kMd5ModifiedPath[] = "9059723a045e17478753d2f0eb33bc03"; |
| 693 | static const char kMd5TwoPaths[] = "7eed0cfba780f1d4dd8068f717d3a6bf"; |
| 694 | static const char kMd5NewAnnot[] = "1de8212d43b7066a6df042095c2aca61"; |
Lei Zhang | 5e2c51c | 2018-07-27 22:33:34 +0000 | [diff] [blame] | 695 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 696 | static const char kMd5Original[] = "6f3cc2dd37479ce7cc072bfb0c63c275"; |
| 697 | static const char kMd5ModifiedPath[] = "c0c2eb2aba73ad15b3240e342fbe0d72"; |
| 698 | static const char kMd5TwoPaths[] = "2306bf04915fe001b5f4726843d184c8"; |
| 699 | static const char kMd5NewAnnot[] = "64a319f145768cb09944d2109efe394e"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 700 | #else |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 701 | static const char kMd5Original[] = "964f89bbe8911e540a465cf1a64b7f7e"; |
| 702 | static const char kMd5ModifiedPath[] = "9a38048fb3ac1b2c9a4b34139caa993c"; |
| 703 | static const char kMd5TwoPaths[] = "ece3d4df54b3395d6a2bf7a29b17239c"; |
| 704 | static const char kMd5NewAnnot[] = "4299493de84c249f42f220f30f2bbb67"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 705 | #endif |
| 706 | |
| 707 | // Open a file with two annotations and load its first page. |
| 708 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 709 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 710 | ASSERT_TRUE(page); |
| 711 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 712 | |
| 713 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 714 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 715 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 716 | CompareBitmap(bitmap.get(), 595, 842, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 717 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 718 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 719 | { |
| 720 | // Retrieve the stamp annotation which has its AP stream already defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 721 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 722 | ASSERT_TRUE(annot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 723 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 724 | // Check that this annotation has one path object and retrieve it. |
| 725 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 726 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 727 | FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1); |
| 728 | EXPECT_FALSE(path); |
| 729 | path = FPDFAnnot_GetObject(annot.get(), 0); |
| 730 | EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path)); |
| 731 | EXPECT_TRUE(path); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 732 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 733 | // Modify the color of the path object. |
| 734 | EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255)); |
| 735 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 736 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 737 | // Check that the page with the modified annotation renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 738 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 739 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 740 | CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 741 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 742 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 743 | // Add a second path object to the same annotation. |
| 744 | FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84); |
| 745 | EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88)); |
| 746 | EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100)); |
| 747 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14)); |
| 748 | EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1)); |
| 749 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot)); |
| 750 | EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get())); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 751 | |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 752 | // The object is in the annontation, not in the page, so the page object |
| 753 | // array should not change. |
| 754 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
| 755 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 756 | // Check that the page with an annotation with two paths renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 757 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 758 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 759 | CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 760 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 761 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 762 | // Delete the newly added path object. |
| 763 | EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1)); |
| 764 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 765 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 766 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 767 | |
| 768 | // Check that the page renders the same as before. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 769 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 770 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 771 | CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 772 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 773 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 774 | FS_RECTF rect; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 775 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 776 | { |
| 777 | // Create another stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 778 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 779 | ASSERT_TRUE(annot); |
| 780 | rect.left = 200.f; |
| 781 | rect.bottom = 400.f; |
| 782 | rect.right = 500.f; |
| 783 | rect.top = 600.f; |
| 784 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 785 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 786 | // Add a new path to the annotation. |
| 787 | FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500); |
| 788 | EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400)); |
| 789 | EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600)); |
| 790 | EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550)); |
| 791 | EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450)); |
| 792 | EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180)); |
| 793 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f)); |
| 794 | EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
| 795 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check)); |
| 796 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 797 | |
| 798 | // Check that the annotation's bounding box came from its rectangle. |
| 799 | FS_RECTF new_rect; |
| 800 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 801 | EXPECT_EQ(rect.left, new_rect.left); |
| 802 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 803 | EXPECT_EQ(rect.right, new_rect.right); |
| 804 | EXPECT_EQ(rect.top, new_rect.top); |
| 805 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 806 | |
| 807 | // Save the document, closing the page and document. |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 808 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 809 | UnloadPage(page); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 810 | |
| 811 | // Open the saved document. |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 812 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 813 | page = LoadSavedPage(0); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 814 | VerifySavedRendering(page, 595, 842, kMd5NewAnnot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 815 | |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 816 | // Check that the document has a correct count of annotations and objects. |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 817 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 818 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 819 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 820 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 821 | ASSERT_TRUE(annot); |
| 822 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 823 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 824 | // Check that the new annotation's rectangle is as defined. |
| 825 | FS_RECTF new_rect; |
| 826 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 827 | EXPECT_EQ(rect.left, new_rect.left); |
| 828 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 829 | EXPECT_EQ(rect.right, new_rect.right); |
| 830 | EXPECT_EQ(rect.top, new_rect.top); |
| 831 | } |
| 832 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 833 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 834 | CloseSavedDocument(); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 835 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 836 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 837 | TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) { |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 838 | // Open a file with an annotation and load its first page. |
| 839 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 840 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 841 | ASSERT_TRUE(page); |
| 842 | |
| 843 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 844 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 845 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 846 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 847 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 848 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 849 | { |
| 850 | // Retrieve the annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 851 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 852 | ASSERT_TRUE(annot); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 853 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 854 | // Check that the original flag values are as expected. |
| 855 | int flags = FPDFAnnot_GetFlags(annot.get()); |
| 856 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 857 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 858 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 859 | // Set the HIDDEN flag. |
| 860 | flags |= FPDF_ANNOT_FLAG_HIDDEN; |
| 861 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags)); |
| 862 | flags = FPDFAnnot_GetFlags(annot.get()); |
| 863 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 864 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 865 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 866 | // Check that the page renders correctly without rendering the annotation. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 867 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 868 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 869 | CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3"); |
| 870 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 871 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 872 | // Unset the HIDDEN flag. |
| 873 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE)); |
| 874 | EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get())); |
| 875 | flags &= ~FPDF_ANNOT_FLAG_HIDDEN; |
| 876 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags)); |
| 877 | flags = FPDFAnnot_GetFlags(annot.get()); |
| 878 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 879 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 880 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 881 | // Check that the page renders correctly as before. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 882 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 883 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 884 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 885 | } |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 886 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 887 | |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 888 | UnloadPage(page); |
| 889 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 890 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 891 | TEST_F(FPDFAnnotEmbedderTest, AddAndModifyImage) { |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 892 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 893 | static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2"; |
| 894 | static const char kMd5NewImage[] = "ff012f5697436dfcaec25b32d1333596"; |
| 895 | static const char kMd5ModifiedImage[] = "86cf8cb2755a7a2046a543e66d9c1e61"; |
Lei Zhang | 5e2c51c | 2018-07-27 22:33:34 +0000 | [diff] [blame] | 896 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 897 | static const char kMd5Original[] = "6f3cc2dd37479ce7cc072bfb0c63c275"; |
| 898 | static const char kMd5NewImage[] = "d19c6fcfd9a170802fcfb9adfa13557e"; |
| 899 | static const char kMd5ModifiedImage[] = "1273cf2363570a50d1aa0c95b1318197"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 900 | #else |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 901 | static const char kMd5Original[] = "964f89bbe8911e540a465cf1a64b7f7e"; |
| 902 | static const char kMd5NewImage[] = "9ea8732dc9d579f68853f16892856208"; |
| 903 | static const char kMd5ModifiedImage[] = "74239d2a8c55c9de1dbb9cd8781895aa"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 904 | #endif |
| 905 | |
| 906 | // Open a file with two annotations and load its first page. |
| 907 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 908 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 909 | ASSERT_TRUE(page); |
| 910 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 911 | |
| 912 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 913 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 914 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 915 | CompareBitmap(bitmap.get(), 595, 842, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 916 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 917 | |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 918 | constexpr int kBitmapSize = 200; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 919 | FPDF_BITMAP image_bitmap; |
| 920 | |
| 921 | { |
| 922 | // Create a stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 923 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 924 | ASSERT_TRUE(annot); |
| 925 | FS_RECTF rect; |
| 926 | rect.left = 200.f; |
| 927 | rect.bottom = 600.f; |
| 928 | rect.right = 400.f; |
| 929 | rect.top = 800.f; |
| 930 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 931 | |
| 932 | // Add a solid-color translucent image object to the new annotation. |
| 933 | image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1); |
| 934 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, |
| 935 | 0xeeeecccc); |
| 936 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap)); |
| 937 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap)); |
| 938 | FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document()); |
| 939 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 940 | ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0, |
| 941 | kBitmapSize, 0, 0)); |
| 942 | FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600); |
| 943 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object)); |
| 944 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 945 | |
| 946 | // Check that the page renders correctly with the new image object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 947 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 948 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 949 | CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 950 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 951 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 952 | { |
| 953 | // Retrieve the newly added stamp annotation and its image object. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 954 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 955 | ASSERT_TRUE(annot); |
| 956 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 957 | FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0); |
| 958 | EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 959 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 960 | // Modify the image in the new annotation. |
| 961 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, |
| 962 | 0xff000000); |
| 963 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 964 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object)); |
| 965 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 966 | |
| 967 | // Save the document, closing the page and document. |
| 968 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 969 | UnloadPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 970 | FPDFBitmap_Destroy(image_bitmap); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 971 | |
| 972 | // Test that the saved document renders the modified image object correctly. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 973 | VerifySavedDocument(595, 842, kMd5ModifiedImage); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 974 | } |
| 975 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 976 | TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) { |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 977 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 978 | static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2"; |
| 979 | static const char kMd5NewText[] = "60031c1b0330cf1e1575f7d46687d429"; |
| 980 | static const char kMd5ModifiedText[] = "79f5cfb0b07caaf936f65f6a7a57ce77"; |
Lei Zhang | 5e2c51c | 2018-07-27 22:33:34 +0000 | [diff] [blame] | 981 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 982 | static const char kMd5Original[] = "6f3cc2dd37479ce7cc072bfb0c63c275"; |
| 983 | static const char kMd5NewText[] = "87d55e09f9096de7e6552f5ae79afd3b"; |
| 984 | static const char kMd5ModifiedText[] = "26e94fbd3af4b1e65479327507600114"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 985 | #else |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 986 | static const char kMd5Original[] = "964f89bbe8911e540a465cf1a64b7f7e"; |
| 987 | static const char kMd5NewText[] = "30f3f5b989612ca03827d95f184f0979"; |
| 988 | static const char kMd5ModifiedText[] = "076c8f24a09ddc0e49f7e758edead6f0"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 989 | #endif |
| 990 | |
| 991 | // Open a file with two annotations and load its first page. |
| 992 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 993 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 994 | ASSERT_TRUE(page); |
| 995 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 996 | |
| 997 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 998 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 999 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1000 | CompareBitmap(bitmap.get(), 595, 842, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1001 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1002 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1003 | { |
| 1004 | // Create a stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1005 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1006 | ASSERT_TRUE(annot); |
| 1007 | FS_RECTF rect; |
| 1008 | rect.left = 200.f; |
| 1009 | rect.bottom = 550.f; |
| 1010 | rect.right = 450.f; |
| 1011 | rect.top = 650.f; |
| 1012 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1013 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1014 | // Add a translucent text object to the new annotation. |
| 1015 | FPDF_PAGEOBJECT text_object = |
| 1016 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 1017 | EXPECT_TRUE(text_object); |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame^] | 1018 | ScopedFPDFWideString text = |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1019 | GetFPDFWideString(L"I'm a translucent text laying on other text."); |
| 1020 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 1021 | EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150)); |
| 1022 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600); |
| 1023 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object)); |
| 1024 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1025 | |
| 1026 | // Check that the page renders correctly with the new text object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1027 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1028 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1029 | CompareBitmap(bitmap.get(), 595, 842, kMd5NewText); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1030 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1031 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1032 | { |
| 1033 | // Retrieve the newly added stamp annotation and its text object. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1034 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1035 | ASSERT_TRUE(annot); |
| 1036 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 1037 | FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0); |
| 1038 | EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1039 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1040 | // Modify the text in the new annotation. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame^] | 1041 | ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!"); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1042 | EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get())); |
| 1043 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object)); |
| 1044 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1045 | |
| 1046 | // Check that the page renders correctly with the modified text object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1047 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1048 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1049 | CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1050 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1051 | |
| 1052 | // Remove the new annotation, and check that the page renders as before. |
| 1053 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2)); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1054 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1055 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1056 | CompareBitmap(bitmap.get(), 595, 842, kMd5Original); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1057 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1058 | |
| 1059 | UnloadPage(page); |
| 1060 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1061 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1062 | TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) { |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1063 | // Open a file with four annotations and load its first page. |
| 1064 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1065 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1066 | ASSERT_TRUE(page); |
| 1067 | |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1068 | static const wchar_t kNewDate[] = L"D:201706282359Z00'00'"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1069 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1070 | { |
| 1071 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1072 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1073 | ASSERT_TRUE(annot); |
| 1074 | |
| 1075 | // Check that a non-existent key does not exist. |
| 1076 | EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none")); |
| 1077 | |
| 1078 | // Check that the string value of a non-string dictionary entry is empty. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1079 | EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1080 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1081 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP)); |
| 1082 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP, |
| 1083 | nullptr, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1084 | |
| 1085 | // Check that the string value of the hash is correct. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1086 | static const char kHashKey[] = "AAPL:Hash"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1087 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey)); |
| 1088 | unsigned long len = |
| 1089 | FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0); |
| 1090 | std::vector<char> buf(len); |
| 1091 | EXPECT_EQ(66u, |
| 1092 | FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len)); |
| 1093 | EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad", |
| 1094 | BufferToWString(buf).c_str()); |
| 1095 | |
| 1096 | // Check that the string value of the modified date is correct. |
| 1097 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey)); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1098 | len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, nullptr, |
| 1099 | 0); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1100 | buf.clear(); |
| 1101 | buf.resize(len); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1102 | EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, |
| 1103 | buf.data(), len)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1104 | EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str()); |
| 1105 | |
| 1106 | // Update the date entry for the annotation. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame^] | 1107 | ScopedFPDFWideString text = GetFPDFWideString(kNewDate); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1108 | EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM, |
| 1109 | text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1110 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1111 | |
| 1112 | // Save the document, closing the page and document. |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1113 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1114 | UnloadPage(page); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1115 | |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1116 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1117 | static const char kMd5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c"; |
Lei Zhang | 5e2c51c | 2018-07-27 22:33:34 +0000 | [diff] [blame] | 1118 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1119 | static const char kMd5[] = "9ee141f698c3fcb56c050dffd6c82624"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1120 | #else |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1121 | static const char kMd5[] = "c96ee1f316d7f5a1b154de9f9d467f01"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1122 | #endif |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 1123 | |
| 1124 | // Open the saved annotation. |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1125 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1126 | page = LoadSavedPage(0); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1127 | VerifySavedRendering(page, 595, 842, kMd5); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1128 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1129 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0)); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1130 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1131 | // Check that the string value of the modified date is the newly-set value. |
| 1132 | EXPECT_EQ(FPDF_OBJECT_STRING, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1133 | FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM)); |
| 1134 | unsigned long len = FPDFAnnot_GetStringValue( |
| 1135 | new_annot.get(), pdfium::annotation::kM, nullptr, 0); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1136 | std::vector<char> buf(len); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1137 | EXPECT_EQ(44u, |
| 1138 | FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM, |
| 1139 | buf.data(), len)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1140 | EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str()); |
| 1141 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1142 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1143 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1144 | CloseSavedDocument(); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1145 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1146 | |
rycsmith | 3e78560 | 2019-03-05 21:48:36 +0000 | [diff] [blame] | 1147 | TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) { |
| 1148 | // Open a file with three text annotations and load its first page. |
| 1149 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 1150 | FPDF_PAGE page = LoadPage(0); |
| 1151 | ASSERT_TRUE(page); |
| 1152 | { |
| 1153 | // First two annotations do not have "MaxLen" attribute. |
| 1154 | for (int i = 0; i < 2; i++) { |
| 1155 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i)); |
| 1156 | ASSERT_TRUE(annot); |
| 1157 | |
| 1158 | // Verify that no "MaxLen" key present. |
| 1159 | EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen")); |
| 1160 | |
| 1161 | float value; |
| 1162 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value)); |
| 1163 | } |
| 1164 | |
| 1165 | // Annotation in index 2 has "MaxLen" of 10. |
| 1166 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
| 1167 | ASSERT_TRUE(annot); |
| 1168 | |
| 1169 | // Verify that "MaxLen" key present. |
| 1170 | EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen")); |
| 1171 | |
| 1172 | float value; |
| 1173 | EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value)); |
| 1174 | EXPECT_FLOAT_EQ(10.0f, value); |
| 1175 | |
| 1176 | // Check bad inputs. |
| 1177 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value)); |
| 1178 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value)); |
| 1179 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr)); |
| 1180 | // Ask for key that exists but is not a number. |
| 1181 | EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value)); |
| 1182 | } |
| 1183 | |
| 1184 | UnloadPage(page); |
| 1185 | } |
| 1186 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1187 | TEST_F(FPDFAnnotEmbedderTest, GetSetAP) { |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1188 | // Open a file with four annotations and load its first page. |
| 1189 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1190 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1191 | ASSERT_TRUE(page); |
| 1192 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1193 | { |
| 1194 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1195 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1196 | ASSERT_TRUE(annot); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1197 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1198 | // Check that the string value of an AP returns the expected length. |
| 1199 | unsigned long normal_len = FPDFAnnot_GetAP( |
| 1200 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0); |
| 1201 | EXPECT_EQ(73970u, normal_len); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1202 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1203 | // Check that the string value of an AP is not returned if the buffer is too |
| 1204 | // small. The result buffer should be overwritten with an empty string. |
| 1205 | std::vector<char> buf(normal_len - 1); |
| 1206 | // Write L"z" in the buffer to verify it's not overwritten. |
| 1207 | wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z"); |
| 1208 | EXPECT_EQ(73970u, |
| 1209 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1210 | buf.data(), buf.size())); |
| 1211 | std::string ap = BufferToString(buf); |
| 1212 | EXPECT_STREQ("z", ap.c_str()); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1213 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1214 | // Check that the string value of an AP is returned through a buffer that is |
| 1215 | // the right size. |
| 1216 | buf.clear(); |
| 1217 | buf.resize(normal_len); |
| 1218 | EXPECT_EQ(73970u, |
| 1219 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1220 | buf.data(), buf.size())); |
| 1221 | ap = BufferToString(buf); |
| 1222 | EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J")); |
| 1223 | EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q")); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1224 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1225 | // Check that the string value of an AP is returned through a buffer that is |
| 1226 | // larger than necessary. |
| 1227 | buf.clear(); |
| 1228 | buf.resize(normal_len + 1); |
| 1229 | EXPECT_EQ(73970u, |
| 1230 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1231 | buf.data(), buf.size())); |
| 1232 | ap = BufferToString(buf); |
| 1233 | EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J")); |
| 1234 | EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q")); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1235 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1236 | // Check that getting an AP for a mode that does not have an AP returns an |
| 1237 | // empty string. |
| 1238 | unsigned long rollover_len = FPDFAnnot_GetAP( |
| 1239 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
| 1240 | EXPECT_EQ(2u, rollover_len); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1241 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1242 | buf.clear(); |
| 1243 | buf.resize(1000); |
| 1244 | EXPECT_EQ(2u, |
| 1245 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1246 | buf.data(), buf.size())); |
| 1247 | EXPECT_STREQ("", BufferToString(buf).c_str()); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1248 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1249 | // Check that setting the AP for an invalid appearance mode fails. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame^] | 1250 | ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap"); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1251 | EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1252 | EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1253 | ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1254 | EXPECT_FALSE(FPDFAnnot_SetAP( |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1255 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1256 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1257 | // Set the AP correctly now. |
| 1258 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1259 | ap_text.get())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1260 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1261 | // Check that the new annotation value is equal to the value we just set. |
| 1262 | rollover_len = FPDFAnnot_GetAP( |
| 1263 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
| 1264 | EXPECT_EQ(24u, rollover_len); |
| 1265 | buf.clear(); |
| 1266 | buf.resize(rollover_len); |
| 1267 | EXPECT_EQ(24u, |
| 1268 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1269 | buf.data(), buf.size())); |
| 1270 | EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str()); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1271 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1272 | // Check that the Normal AP was not touched when the Rollover AP was set. |
| 1273 | buf.clear(); |
| 1274 | buf.resize(normal_len); |
| 1275 | EXPECT_EQ(73970u, |
| 1276 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1277 | buf.data(), buf.size())); |
| 1278 | ap = BufferToString(buf); |
| 1279 | EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J")); |
| 1280 | EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q")); |
| 1281 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1282 | |
| 1283 | // Save the modified document, then reopen it. |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1284 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1285 | UnloadPage(page); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1286 | |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1287 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1288 | page = LoadSavedPage(0); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1289 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1290 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1291 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1292 | // Check that the new annotation value is equal to the value we set before |
| 1293 | // saving. |
| 1294 | unsigned long rollover_len = FPDFAnnot_GetAP( |
| 1295 | new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
| 1296 | EXPECT_EQ(24u, rollover_len); |
| 1297 | std::vector<char> buf(rollover_len); |
| 1298 | EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(), |
| 1299 | FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1300 | buf.data(), buf.size())); |
| 1301 | EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str()); |
| 1302 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1303 | |
| 1304 | // Close saved document. |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1305 | CloseSavedPage(page); |
| 1306 | CloseSavedDocument(); |
| 1307 | } |
| 1308 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1309 | TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) { |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1310 | // Open a file with four annotations and load its first page. |
| 1311 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1312 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1313 | ASSERT_TRUE(page); |
| 1314 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1315 | { |
| 1316 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1317 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1318 | ASSERT_TRUE(annot); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1319 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1320 | // Set Down AP. Normal AP is already set. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame^] | 1321 | ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap"); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1322 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1323 | ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1324 | EXPECT_EQ(73970u, |
| 1325 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1326 | nullptr, 0)); |
| 1327 | EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1328 | nullptr, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1329 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1330 | // Check that setting the Down AP to null removes the Down entry but keeps |
| 1331 | // Normal intact. |
| 1332 | EXPECT_TRUE( |
| 1333 | FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr)); |
| 1334 | EXPECT_EQ(73970u, |
| 1335 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1336 | nullptr, 0)); |
| 1337 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1338 | nullptr, 0)); |
| 1339 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1340 | |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1341 | UnloadPage(page); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1344 | TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) { |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1345 | // Open a file with four annotations and load its first page. |
| 1346 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1347 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1348 | ASSERT_TRUE(page); |
| 1349 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1350 | { |
| 1351 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1352 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1353 | ASSERT_TRUE(annot); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1354 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1355 | // Set Down AP. Normal AP is already set. |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame^] | 1356 | ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap"); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1357 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1358 | ap_text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1359 | EXPECT_EQ(73970u, |
| 1360 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1361 | nullptr, 0)); |
| 1362 | EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1363 | nullptr, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1364 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1365 | // Check that setting the Normal AP to null removes the whole AP dictionary. |
| 1366 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1367 | nullptr)); |
| 1368 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1369 | nullptr, 0)); |
| 1370 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1371 | nullptr, 0)); |
| 1372 | } |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1373 | |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1374 | UnloadPage(page); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1377 | TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) { |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1378 | // Open a file with annotations and load its first page. |
| 1379 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1380 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1381 | ASSERT_TRUE(page); |
Jane Liu | d1ed1ce | 2017-08-24 12:31:10 -0400 | [diff] [blame] | 1382 | EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1383 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1384 | { |
| 1385 | // Retrieve the highlight annotation which has its popup defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1386 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1387 | ASSERT_TRUE(annot); |
| 1388 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 1389 | EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get())); |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1390 | static const char kPopupKey[] = "Popup"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1391 | ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey)); |
| 1392 | ASSERT_EQ(FPDF_OBJECT_REFERENCE, |
| 1393 | FPDFAnnot_GetValueType(annot.get(), kPopupKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1394 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1395 | // Retrieve and verify the popup of the highlight annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1396 | ScopedFPDFAnnotation popup( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1397 | FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey)); |
| 1398 | ASSERT_TRUE(popup); |
| 1399 | EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get())); |
| 1400 | EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get())); |
| 1401 | FS_RECTF rect; |
| 1402 | ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect)); |
| 1403 | EXPECT_NEAR(612.0f, rect.left, 0.001f); |
| 1404 | EXPECT_NEAR(578.792, rect.bottom, 0.001f); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1405 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1406 | // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail, |
| 1407 | // since "IRT" is not a key in |annot|'s dictionary. |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1408 | static const char kIRTKey[] = "IRT"; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1409 | ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey)); |
| 1410 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1411 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1412 | // Attempting to retrieve |annot|'s parent dictionary as an annotation |
| 1413 | // would fail, since its parent is not an annotation. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1414 | ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1415 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1416 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP)); |
| 1417 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1418 | } |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1419 | |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1420 | UnloadPage(page); |
| 1421 | } |
| 1422 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1423 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) { |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1424 | // Open file with form text fields. |
| 1425 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1426 | FPDF_PAGE page = LoadPage(0); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1427 | ASSERT_TRUE(page); |
| 1428 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1429 | { |
| 1430 | // Retrieve the first annotation: user-editable text field. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1431 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1432 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1433 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1434 | // Check that the flag values are as expected. |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 1435 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1436 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1437 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1438 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1439 | { |
| 1440 | // Retrieve the second annotation: read-only text field. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1441 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1442 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1443 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1444 | // Check that the flag values are as expected. |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 1445 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1446 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1447 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1448 | |
| 1449 | UnloadPage(page); |
| 1450 | } |
| 1451 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1452 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) { |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1453 | // Open file with form text fields. |
| 1454 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1455 | FPDF_PAGE page = LoadPage(0); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1456 | ASSERT_TRUE(page); |
| 1457 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1458 | { |
| 1459 | // Retrieve the first annotation: user-editable combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1460 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1461 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1462 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1463 | // Check that the flag values are as expected. |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 1464 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1465 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1466 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1467 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1468 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1469 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1470 | { |
| 1471 | // Retrieve the second annotation: regular combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1472 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1473 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1474 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1475 | // Check that the flag values are as expected. |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 1476 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1477 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1478 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1479 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1480 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1481 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1482 | { |
| 1483 | // Retrieve the third annotation: read-only combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1484 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1485 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1486 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1487 | // Check that the flag values are as expected. |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 1488 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1489 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1490 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1491 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1492 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1493 | |
| 1494 | UnloadPage(page); |
| 1495 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1496 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1497 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1498 | // Open file with form text fields. |
| 1499 | EXPECT_TRUE(OpenDocument("text_form.pdf")); |
| 1500 | FPDF_PAGE page = LoadPage(0); |
| 1501 | ASSERT_TRUE(page); |
| 1502 | |
| 1503 | // Attempt to get an annotation where no annotation exists on page. |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 1504 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0)); |
| 1505 | |
| 1506 | { |
| 1507 | // Verify there is an annotation. |
| 1508 | ScopedFPDFAnnotation annot( |
| 1509 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120)); |
| 1510 | EXPECT_TRUE(annot); |
| 1511 | } |
| 1512 | |
| 1513 | // Try other bad inputs at a valid location. |
| 1514 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120)); |
| 1515 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120)); |
| 1516 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120)); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1517 | |
| 1518 | UnloadPage(page); |
| 1519 | } |
| 1520 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1521 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1522 | // Open file with form text fields. |
| 1523 | EXPECT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 1524 | FPDF_PAGE page = LoadPage(0); |
| 1525 | ASSERT_TRUE(page); |
| 1526 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1527 | { |
| 1528 | // Retrieve user-editable text field annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1529 | ScopedFPDFAnnotation annot( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1530 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118)); |
| 1531 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1532 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1533 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 1534 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1535 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1536 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1537 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1538 | { |
| 1539 | // Retrieve read-only text field annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1540 | ScopedFPDFAnnotation annot( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1541 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202)); |
| 1542 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1543 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1544 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 1545 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1546 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1547 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1548 | |
| 1549 | UnloadPage(page); |
| 1550 | } |
| 1551 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1552 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1553 | // Open file with form comboboxes. |
| 1554 | EXPECT_TRUE(OpenDocument("combobox_form.pdf")); |
| 1555 | FPDF_PAGE page = LoadPage(0); |
| 1556 | ASSERT_TRUE(page); |
| 1557 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1558 | { |
| 1559 | // Retrieve user-editable combobox annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1560 | ScopedFPDFAnnotation annot( |
Henrique Nakashima | 2083092 | 2018-03-19 21:21:46 +0000 | [diff] [blame] | 1561 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1562 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1563 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1564 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 1565 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1566 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1567 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1568 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1569 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1570 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1571 | { |
| 1572 | // Retrieve regular combobox annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1573 | ScopedFPDFAnnotation annot( |
Henrique Nakashima | 2083092 | 2018-03-19 21:21:46 +0000 | [diff] [blame] | 1574 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1575 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1576 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1577 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 1578 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1579 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1580 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1581 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1582 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1583 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1584 | { |
| 1585 | // Retrieve read-only combobox annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1586 | ScopedFPDFAnnotation annot( |
Henrique Nakashima | 2083092 | 2018-03-19 21:21:46 +0000 | [diff] [blame] | 1587 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1588 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1589 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1590 | // Check that interactive form annotation flag values are as expected. |
Lei Zhang | e6fcdfa | 2019-02-14 04:07:09 +0000 | [diff] [blame] | 1591 | int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get()); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1592 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1593 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1594 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1595 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1596 | |
| 1597 | UnloadPage(page); |
| 1598 | } |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1599 | |
Lei Zhang | 992e7e2 | 2019-02-04 19:20:58 +0000 | [diff] [blame] | 1600 | TEST_F(FPDFAnnotEmbedderTest, BUG_1206) { |
| 1601 | static constexpr size_t kExpectedSize = 1609; |
| 1602 | static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc"; |
| 1603 | |
| 1604 | ASSERT_TRUE(OpenDocument("bug_1206.pdf")); |
| 1605 | |
| 1606 | FPDF_PAGE page = LoadPage(0); |
| 1607 | ASSERT_TRUE(page); |
| 1608 | |
| 1609 | ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1610 | EXPECT_EQ(kExpectedSize, GetString().size()); |
| 1611 | ClearString(); |
| 1612 | |
| 1613 | for (size_t i = 0; i < 10; ++i) { |
| 1614 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 1615 | CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap); |
| 1616 | |
| 1617 | ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1618 | // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be |
| 1619 | // equal, not bigger. |
| 1620 | EXPECT_LT(kExpectedSize, GetString().size()); |
| 1621 | ClearString(); |
| 1622 | } |
| 1623 | |
| 1624 | UnloadPage(page); |
| 1625 | } |
| 1626 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1627 | TEST_F(FPDFAnnotEmbedderTest, BUG_1212) { |
| 1628 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 1629 | FPDF_PAGE page = LoadPage(0); |
| 1630 | ASSERT_TRUE(page); |
| 1631 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 1632 | |
| 1633 | static const char kTestKey[] = "test"; |
Lei Zhang | 4f556b8 | 2019-04-08 16:32:41 +0000 | [diff] [blame] | 1634 | static const wchar_t kData[] = L"\xf6\xe4"; |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1635 | std::vector<char> buf(12); |
| 1636 | |
| 1637 | { |
| 1638 | // Add a text annotation to the page. |
| 1639 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT)); |
| 1640 | ASSERT_TRUE(annot); |
| 1641 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 1642 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 1643 | |
| 1644 | // Make sure there is no test key, add set a value there, and read it back. |
| 1645 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1646 | ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
| 1647 | buf.size())); |
| 1648 | EXPECT_STREQ(L"", BufferToWString(buf).c_str()); |
| 1649 | |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame^] | 1650 | ScopedFPDFWideString text = GetFPDFWideString(kData); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1651 | EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get())); |
| 1652 | |
| 1653 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1654 | ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
| 1655 | buf.size())); |
| 1656 | EXPECT_STREQ(kData, BufferToWString(buf).c_str()); |
| 1657 | } |
| 1658 | |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1659 | { |
| 1660 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
| 1661 | ASSERT_TRUE(annot); |
| 1662 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 1663 | EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get())); |
| 1664 | // Also do the same test for its appearance string. |
| 1665 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1666 | ASSERT_EQ(2u, |
| 1667 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1668 | buf.data(), buf.size())); |
| 1669 | EXPECT_STREQ(L"", BufferToWString(buf).c_str()); |
| 1670 | |
Lei Zhang | f0f6768 | 2019-04-08 17:03:21 +0000 | [diff] [blame^] | 1671 | ScopedFPDFWideString text = GetFPDFWideString(kData); |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1672 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1673 | text.get())); |
| 1674 | |
| 1675 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1676 | ASSERT_EQ(6u, |
| 1677 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1678 | buf.data(), buf.size())); |
| 1679 | EXPECT_STREQ(kData, BufferToWString(buf).c_str()); |
| 1680 | } |
| 1681 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1682 | UnloadPage(page); |
| 1683 | |
| 1684 | { |
| 1685 | // Save a copy, open the copy, and check the annotation again. |
| 1686 | // Note that it renders the rotation. |
| 1687 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1688 | ASSERT_TRUE(OpenSavedDocument()); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1689 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 1690 | ASSERT_TRUE(saved_page); |
| 1691 | |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1692 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page)); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1693 | { |
| 1694 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0)); |
| 1695 | ASSERT_TRUE(annot); |
| 1696 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 1697 | |
| 1698 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1699 | ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
| 1700 | buf.size())); |
| 1701 | EXPECT_STREQ(kData, BufferToWString(buf).c_str()); |
| 1702 | } |
| 1703 | |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1704 | { |
| 1705 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0)); |
| 1706 | ASSERT_TRUE(annot); |
| 1707 | // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason. |
| 1708 | // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 1709 | |
| 1710 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1711 | ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
| 1712 | buf.size())); |
| 1713 | EXPECT_STREQ(kData, BufferToWString(buf).c_str()); |
| 1714 | } |
| 1715 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1716 | CloseSavedPage(saved_page); |
| 1717 | CloseSavedDocument(); |
| 1718 | } |
| 1719 | } |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1720 | |
| 1721 | TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) { |
| 1722 | // Open a file with combobox widget annotations and load its first page. |
| 1723 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 1724 | FPDF_PAGE page = LoadPage(0); |
| 1725 | ASSERT_TRUE(page); |
| 1726 | |
| 1727 | { |
| 1728 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 1729 | ASSERT_TRUE(annot); |
| 1730 | |
| 1731 | EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 1732 | |
| 1733 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 1734 | ASSERT_TRUE(annot); |
| 1735 | |
| 1736 | EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
Lei Zhang | e7033c8 | 2019-02-26 19:30:49 +0000 | [diff] [blame] | 1737 | |
| 1738 | // Check bad form handle / annot. |
| 1739 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr)); |
| 1740 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr)); |
| 1741 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get())); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | UnloadPage(page); |
| 1745 | } |
| 1746 | |
| 1747 | TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) { |
| 1748 | // Open a file with listbox widget annotations and load its first page. |
| 1749 | ASSERT_TRUE(OpenDocument("listbox_form.pdf")); |
| 1750 | FPDF_PAGE page = LoadPage(0); |
| 1751 | ASSERT_TRUE(page); |
| 1752 | |
| 1753 | { |
| 1754 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 1755 | ASSERT_TRUE(annot); |
| 1756 | |
| 1757 | EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 1758 | |
| 1759 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 1760 | ASSERT_TRUE(annot); |
| 1761 | |
| 1762 | EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 1763 | } |
| 1764 | |
| 1765 | UnloadPage(page); |
| 1766 | } |
| 1767 | |
| 1768 | TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) { |
| 1769 | // Open a file with ink annotations and load its first page. |
| 1770 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 1771 | FPDF_PAGE page = LoadPage(0); |
| 1772 | ASSERT_TRUE(page); |
| 1773 | |
| 1774 | { |
| 1775 | // annotations do not have "Opt" array and will return -1 |
| 1776 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 1777 | ASSERT_TRUE(annot); |
| 1778 | |
| 1779 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 1780 | |
| 1781 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 1782 | ASSERT_TRUE(annot); |
| 1783 | |
| 1784 | EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get())); |
| 1785 | } |
| 1786 | |
| 1787 | UnloadPage(page); |
| 1788 | } |
| 1789 | |
| 1790 | TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) { |
| 1791 | // Open a file with combobox widget annotations and load its first page. |
| 1792 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 1793 | FPDF_PAGE page = LoadPage(0); |
| 1794 | ASSERT_TRUE(page); |
| 1795 | |
| 1796 | { |
| 1797 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 1798 | ASSERT_TRUE(annot); |
| 1799 | |
| 1800 | int index = 0; |
| 1801 | unsigned long len = |
| 1802 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
| 1803 | std::vector<char> buf(len); |
| 1804 | EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
| 1805 | buf.data(), len)); |
| 1806 | EXPECT_STREQ(L"Foo", BufferToWString(buf).c_str()); |
| 1807 | |
| 1808 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 1809 | ASSERT_TRUE(annot); |
| 1810 | |
| 1811 | index = 0; |
| 1812 | len = |
| 1813 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
| 1814 | buf.clear(); |
| 1815 | buf.resize(len); |
| 1816 | EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
| 1817 | buf.data(), len)); |
| 1818 | EXPECT_STREQ(L"Apple", BufferToWString(buf).c_str()); |
| 1819 | |
| 1820 | index = 25; |
| 1821 | len = |
| 1822 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
| 1823 | buf.clear(); |
| 1824 | buf.resize(len); |
| 1825 | EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
| 1826 | buf.data(), len)); |
| 1827 | EXPECT_STREQ(L"Zucchini", BufferToWString(buf).c_str()); |
| 1828 | |
Lei Zhang | e7033c8 | 2019-02-26 19:30:49 +0000 | [diff] [blame] | 1829 | // Indices out of range |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1830 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1, |
| 1831 | nullptr, 0)); |
| 1832 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26, |
| 1833 | nullptr, 0)); |
Lei Zhang | e7033c8 | 2019-02-26 19:30:49 +0000 | [diff] [blame] | 1834 | |
| 1835 | // Check bad form handle / annot. |
| 1836 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0)); |
| 1837 | EXPECT_EQ(0u, |
| 1838 | FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0)); |
| 1839 | EXPECT_EQ(0u, |
| 1840 | FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0)); |
rycsmith | cb752f3 | 2019-02-21 18:40:53 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | UnloadPage(page); |
| 1844 | } |
| 1845 | |
| 1846 | TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) { |
| 1847 | // Open a file with listbox widget annotations and load its first page. |
| 1848 | ASSERT_TRUE(OpenDocument("listbox_form.pdf")); |
| 1849 | FPDF_PAGE page = LoadPage(0); |
| 1850 | ASSERT_TRUE(page); |
| 1851 | |
| 1852 | { |
| 1853 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 1854 | ASSERT_TRUE(annot); |
| 1855 | |
| 1856 | int index = 0; |
| 1857 | unsigned long len = |
| 1858 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
| 1859 | std::vector<char> buf(len); |
| 1860 | EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
| 1861 | buf.data(), len)); |
| 1862 | EXPECT_STREQ(L"Foo", BufferToWString(buf).c_str()); |
| 1863 | |
| 1864 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 1865 | ASSERT_TRUE(annot); |
| 1866 | |
| 1867 | index = 0; |
| 1868 | len = |
| 1869 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
| 1870 | buf.clear(); |
| 1871 | buf.resize(len); |
| 1872 | EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
| 1873 | buf.data(), len)); |
| 1874 | EXPECT_STREQ(L"Apple", BufferToWString(buf).c_str()); |
| 1875 | |
| 1876 | index = 25; |
| 1877 | len = |
| 1878 | FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0); |
| 1879 | buf.clear(); |
| 1880 | buf.resize(len); |
| 1881 | EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, |
| 1882 | buf.data(), len)); |
| 1883 | EXPECT_STREQ(L"Zucchini", BufferToWString(buf).c_str()); |
| 1884 | |
| 1885 | // indices out of range |
| 1886 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1, |
| 1887 | nullptr, 0)); |
| 1888 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26, |
| 1889 | nullptr, 0)); |
| 1890 | } |
| 1891 | |
| 1892 | UnloadPage(page); |
| 1893 | } |
| 1894 | |
| 1895 | TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) { |
| 1896 | // Open a file with ink annotations and load its first page. |
| 1897 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 1898 | FPDF_PAGE page = LoadPage(0); |
| 1899 | ASSERT_TRUE(page); |
| 1900 | |
| 1901 | { |
| 1902 | // annotations do not have "Opt" array and will return 0 |
| 1903 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
| 1904 | ASSERT_TRUE(annot); |
| 1905 | |
| 1906 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0, |
| 1907 | nullptr, 0)); |
| 1908 | |
| 1909 | annot.reset(FPDFPage_GetAnnot(page, 1)); |
| 1910 | ASSERT_TRUE(annot); |
| 1911 | |
| 1912 | EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0, |
| 1913 | nullptr, 0)); |
| 1914 | } |
| 1915 | |
| 1916 | UnloadPage(page); |
| 1917 | } |