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