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