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