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