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