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