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 | |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 5 | #include <memory> |
| 6 | #include <string> |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 9 | #include "core/fxcrt/fx_system.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 10 | #include "public/fpdf_annot.h" |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 11 | #include "public/fpdf_edit.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 12 | #include "public/fpdfview.h" |
| 13 | #include "testing/embedder_test.h" |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 16 | class FPDFAnnotEmbeddertest : public EmbedderTest {}; |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 17 | |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 18 | TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) { |
| 19 | // Open a file with one annotation and load its first page. |
| 20 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
| 21 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 22 | ASSERT_TRUE(page); |
| 23 | |
| 24 | // This annotation has a malformed appearance stream, which does not have its |
| 25 | // normal appearance defined, only its rollover appearance. In this case, its |
| 26 | // normal appearance should be generated, allowing the highlight annotation to |
| 27 | // still display. |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 28 | FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle(), FPDF_ANNOT); |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 29 | CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 30 | FPDFBitmap_Destroy(bitmap); |
| 31 | |
| 32 | UnloadPage(page); |
| 33 | } |
| 34 | |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 35 | TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) { |
| 36 | // Open a file with one annotation and load its first page. |
| 37 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
| 38 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 39 | ASSERT_TRUE(page); |
| 40 | |
| 41 | // Check that there is a total of 1 annotation on its first page. |
| 42 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 43 | |
| 44 | // Check that the annotation is of type "highlight". |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 45 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 46 | ASSERT_TRUE(annot); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 47 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot)); |
| 48 | |
| 49 | // Check that the annotation color is yellow. |
| 50 | unsigned int R; |
| 51 | unsigned int G; |
| 52 | unsigned int B; |
| 53 | unsigned int A; |
| 54 | EXPECT_TRUE( |
| 55 | FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)); |
| 56 | EXPECT_EQ(255u, R); |
| 57 | EXPECT_EQ(255u, G); |
| 58 | EXPECT_EQ(0u, B); |
| 59 | EXPECT_EQ(255u, A); |
| 60 | |
| 61 | // Check that the author is correct. |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 62 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> author_key = |
| 63 | GetFPDFWideString(L"T"); |
| 64 | EXPECT_EQ(FPDF_OBJECT_STRING, |
| 65 | FPDFAnnot_GetValueType(annot, author_key.get())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 66 | unsigned long len = |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 67 | FPDFAnnot_GetStringValue(annot, author_key.get(), nullptr, 0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 68 | std::vector<char> buf(len); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 69 | EXPECT_EQ(28u, |
| 70 | FPDFAnnot_GetStringValue(annot, author_key.get(), buf.data(), len)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 71 | EXPECT_STREQ(L"Jae Hyun Park", |
| 72 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 73 | .c_str()); |
| 74 | |
| 75 | // Check that the content is correct. |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 76 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> contents_key = |
| 77 | GetFPDFWideString(L"Contents"); |
| 78 | EXPECT_EQ(FPDF_OBJECT_STRING, |
| 79 | FPDFAnnot_GetValueType(annot, contents_key.get())); |
| 80 | len = FPDFAnnot_GetStringValue(annot, contents_key.get(), nullptr, 0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 81 | buf.clear(); |
| 82 | buf.resize(len); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 83 | EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot, contents_key.get(), |
| 84 | buf.data(), len)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 85 | const wchar_t contents[] = |
| 86 | L"This is a note for that highlight annotation. Very long highlight " |
| 87 | "annotation. Long long long Long long longLong long longLong long " |
| 88 | "longLong long longLong long longLong long longLong long longLong long " |
| 89 | "longLong long longLong long longLong long longLong long longLong long " |
| 90 | "longLong long longLong long longLong long longLong long longLong long " |
| 91 | "longLong long longLong long longLong long longLong long longLong long " |
| 92 | "longLong long longLong long longLong long longLong long longLong long " |
| 93 | "longLong long longLong long longLong long longLong long longLong long " |
| 94 | "longLong long longLong long longLong long longLong long longLong long " |
| 95 | "longLong long longLong long longLong long longLong long longLong long " |
| 96 | "longLong long longLong long longLong long longLong long longLong long " |
| 97 | "longLong long longLong long longLong long longLong long longLong long " |
| 98 | "longLong long longLong long longLong long longLong long longLong long " |
| 99 | "longLong long longLong long longLong long longLong long longLong long " |
| 100 | "longLong long longLong long longLong 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 long. END"; |
| 106 | EXPECT_STREQ(contents, |
| 107 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 108 | .c_str()); |
| 109 | |
| 110 | // Check that the quadpoints are correct. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 111 | FS_QUADPOINTSF quadpoints; |
| 112 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 113 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 114 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 115 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 116 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 117 | |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 118 | FPDFPage_CloseAnnot(annot); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 119 | UnloadPage(page); |
| 120 | } |
| 121 | |
| 122 | TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) { |
| 123 | // Open a file with three annotations and load its first page. |
| 124 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 125 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 126 | ASSERT_TRUE(page); |
| 127 | |
| 128 | // Check that there is a total of 3 annotation on its first page. |
| 129 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 130 | |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 131 | // Check that the third annotation is of type "ink". |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 132 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 2); |
| 133 | ASSERT_TRUE(annot); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 134 | EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot)); |
| 135 | |
| 136 | // Check that the annotation color is blue with opacity. |
| 137 | unsigned int R; |
| 138 | unsigned int G; |
| 139 | unsigned int B; |
| 140 | unsigned int A; |
| 141 | EXPECT_TRUE( |
| 142 | FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)); |
| 143 | EXPECT_EQ(0u, R); |
| 144 | EXPECT_EQ(0u, G); |
| 145 | EXPECT_EQ(255u, B); |
| 146 | EXPECT_EQ(76u, A); |
| 147 | |
| 148 | // Check that there is no content. |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 149 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> contents_key = |
| 150 | GetFPDFWideString(L"Contents"); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 151 | EXPECT_EQ(2u, |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 152 | FPDFAnnot_GetStringValue(annot, contents_key.get(), nullptr, 0)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 153 | |
| 154 | // Check that the rectange coordinates are correct. |
| 155 | // Note that upon rendering, the rectangle coordinates will be adjusted. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 156 | FS_RECTF rect; |
| 157 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 158 | EXPECT_EQ(351.820404f, rect.left); |
| 159 | EXPECT_EQ(583.830688f, rect.bottom); |
| 160 | EXPECT_EQ(475.336090f, rect.right); |
| 161 | EXPECT_EQ(681.535034f, rect.top); |
| 162 | |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 163 | FPDFPage_CloseAnnot(annot); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 164 | UnloadPage(page); |
| 165 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 166 | |
| 167 | TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) { |
| 168 | // Open a file with one annotation and load its first page. |
| 169 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
| 170 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 171 | ASSERT_TRUE(page); |
| 172 | |
| 173 | // Add an annotation with an illegal subtype. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 174 | ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 175 | |
| 176 | UnloadPage(page); |
| 177 | } |
| 178 | |
Jane Liu | d321ef9 | 2017-06-14 09:56:22 -0400 | [diff] [blame] | 179 | TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 180 | // Open a file with no annotation and load its first page. |
| 181 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 182 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 183 | ASSERT_TRUE(page); |
| 184 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 185 | |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 186 | // Add a text annotation to the page. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 187 | FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT); |
| 188 | ASSERT_TRUE(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 189 | |
| 190 | // Check that there is now 1 annotations on this page. |
| 191 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 192 | |
| 193 | // Check that the subtype of the annotation is correct. |
| 194 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot)); |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 195 | FPDFPage_CloseAnnot(annot); |
| 196 | |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 197 | annot = FPDFPage_GetAnnot(page, 0); |
| 198 | ASSERT_TRUE(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 199 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot)); |
| 200 | |
| 201 | // Set the color of the annotation. |
| 202 | ASSERT_TRUE( |
| 203 | FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204)); |
| 204 | // Check that the color has been set correctly. |
| 205 | unsigned int R; |
| 206 | unsigned int G; |
| 207 | unsigned int B; |
| 208 | unsigned int A; |
| 209 | EXPECT_TRUE( |
| 210 | FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)); |
| 211 | EXPECT_EQ(51u, R); |
| 212 | EXPECT_EQ(102u, G); |
| 213 | EXPECT_EQ(153u, B); |
| 214 | EXPECT_EQ(204u, A); |
| 215 | |
| 216 | // Change the color of the annotation. |
| 217 | ASSERT_TRUE( |
| 218 | FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 204, 153, 102, 51)); |
| 219 | // Check that the color has been set correctly. |
| 220 | EXPECT_TRUE( |
| 221 | FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)); |
| 222 | EXPECT_EQ(204u, R); |
| 223 | EXPECT_EQ(153u, G); |
| 224 | EXPECT_EQ(102u, B); |
| 225 | EXPECT_EQ(51u, A); |
| 226 | |
| 227 | // Set the annotation rectangle. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 228 | FS_RECTF rect; |
| 229 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 230 | EXPECT_EQ(0.f, rect.left); |
| 231 | EXPECT_EQ(0.f, rect.right); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 232 | rect.left = 35; |
| 233 | rect.bottom = 150; |
| 234 | rect.right = 53; |
| 235 | rect.top = 165; |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 236 | ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 237 | // Check that the annotation rectangle has been set correctly. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 238 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 239 | EXPECT_EQ(35.f, rect.left); |
| 240 | EXPECT_EQ(150.f, rect.bottom); |
| 241 | EXPECT_EQ(53.f, rect.right); |
| 242 | EXPECT_EQ(165.f, rect.top); |
| 243 | |
| 244 | // Set the content of the annotation. |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 245 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> contents_key = |
| 246 | GetFPDFWideString(L"Contents"); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 247 | const wchar_t contents[] = L"Hello! This is a customized content."; |
| 248 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 249 | GetFPDFWideString(contents); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 250 | ASSERT_TRUE(FPDFAnnot_SetStringValue(annot, contents_key.get(), text.get())); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 251 | // Check that the content has been set correctly. |
| 252 | unsigned long len = |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 253 | FPDFAnnot_GetStringValue(annot, contents_key.get(), nullptr, 0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 254 | std::vector<char> buf(len); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 255 | EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot, contents_key.get(), buf.data(), |
| 256 | len)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 257 | EXPECT_STREQ(contents, |
| 258 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 259 | .c_str()); |
| 260 | |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 261 | FPDFPage_CloseAnnot(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 262 | UnloadPage(page); |
| 263 | } |
| 264 | |
| 265 | TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) { |
| 266 | // Open a file with one annotation and load its first page. |
| 267 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
| 268 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 269 | ASSERT_TRUE(page); |
| 270 | |
| 271 | // Check that there is a total of one annotation on its first page, and verify |
| 272 | // its quadpoints. |
| 273 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 274 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 275 | ASSERT_TRUE(annot); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 276 | FS_QUADPOINTSF quadpoints; |
| 277 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 278 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 279 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 280 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 281 | EXPECT_EQ(706.264465f, quadpoints.y4); |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 282 | FPDFPage_CloseAnnot(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 283 | |
| 284 | // Add an underline annotation to the page and set its quadpoints. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 285 | annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE); |
| 286 | ASSERT_TRUE(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 287 | quadpoints.x1 = 140.802643f; |
| 288 | quadpoints.x3 = 140.802643f; |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 289 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, &quadpoints)); |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 290 | FPDFPage_CloseAnnot(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 291 | |
| 292 | // Save the document, closing the page and document. |
| 293 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 294 | FPDF_ClosePage(page); |
| 295 | |
| 296 | // Open the saved document. |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 297 | const char md5[] = "184b67b322edaee27994b3232544b8b3"; |
| 298 | TestSaved(612, 792, md5); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 299 | |
| 300 | // Check that the saved document has 2 annotations on the first page |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 301 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(m_SavedPage)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 302 | |
| 303 | // Check that the second annotation is an underline annotation and verify |
| 304 | // its quadpoints. |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 305 | FPDF_ANNOTATION new_annot = FPDFPage_GetAnnot(m_SavedPage, 1); |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 306 | ASSERT_TRUE(new_annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 307 | EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 308 | FS_QUADPOINTSF new_quadpoints; |
| 309 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(new_annot, &new_quadpoints)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 310 | EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f); |
| 311 | EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f); |
| 312 | EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f); |
| 313 | EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f); |
| 314 | |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 315 | FPDFPage_CloseAnnot(new_annot); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 316 | CloseSaved(); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 317 | } |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 318 | |
| 319 | TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) { |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 320 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 321 | const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941"; |
| 322 | const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2"; |
| 323 | const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56"; |
| 324 | #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 325 | const char md5_original[] = "ade6762a70e85605546ce067e7d2148f"; |
| 326 | const char md5_modified_highlight[] = "fb8440ed1a070b53ed5598ce7451cfad"; |
| 327 | const char md5_modified_square[] = "7925f6726b343393f258e8b4e93dd65d"; |
| 328 | #else |
| 329 | const char md5_original[] = "ade6762a70e85605546ce067e7d2148f"; |
| 330 | const char md5_modified_highlight[] = "fb8440ed1a070b53ed5598ce7451cfad"; |
| 331 | const char md5_modified_square[] = "7925f6726b343393f258e8b4e93dd65d"; |
| 332 | #endif |
| 333 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 334 | // Open a file with four annotations and load its first page. |
| 335 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
| 336 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 337 | ASSERT_TRUE(page); |
| 338 | EXPECT_EQ(4, FPDFPage_GetAnnotCount(page)); |
| 339 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 340 | // Check that the original file renders correctly. |
| 341 | FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 342 | CompareBitmap(bitmap, 612, 792, md5_original); |
| 343 | FPDFBitmap_Destroy(bitmap); |
| 344 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 345 | // Retrieve the highlight annotation which has its AP stream already defined. |
| 346 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 347 | ASSERT_TRUE(annot); |
| 348 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot)); |
| 349 | |
| 350 | // Check that color cannot be set when an AP stream is defined already. |
| 351 | EXPECT_FALSE( |
| 352 | FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204)); |
| 353 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 354 | // Verify its attachment points. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 355 | FS_QUADPOINTSF quadpoints; |
| 356 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints)); |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 357 | EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f); |
| 358 | EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f); |
| 359 | EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f); |
| 360 | EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 361 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 362 | // Check that updating the attachment points would succeed. |
| 363 | quadpoints.x1 -= 50.f; |
| 364 | quadpoints.x2 -= 50.f; |
| 365 | quadpoints.x3 -= 50.f; |
| 366 | quadpoints.x4 -= 50.f; |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 367 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, &quadpoints)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 368 | FS_QUADPOINTSF new_quadpoints; |
| 369 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &new_quadpoints)); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 370 | EXPECT_EQ(quadpoints.x1, new_quadpoints.x1); |
| 371 | EXPECT_EQ(quadpoints.y1, new_quadpoints.y1); |
| 372 | EXPECT_EQ(quadpoints.x4, new_quadpoints.x4); |
| 373 | EXPECT_EQ(quadpoints.y4, new_quadpoints.y4); |
| 374 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 375 | // Check that updating quadpoints does not change the annotation's position. |
| 376 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 377 | CompareBitmap(bitmap, 612, 792, md5_original); |
| 378 | FPDFBitmap_Destroy(bitmap); |
| 379 | |
| 380 | // Verify its annotation rectangle. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 381 | FS_RECTF rect; |
| 382 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 383 | EXPECT_NEAR(67.7299f, rect.left, 0.001f); |
| 384 | EXPECT_NEAR(704.296f, rect.bottom, 0.001f); |
| 385 | EXPECT_NEAR(136.325f, rect.right, 0.001f); |
| 386 | EXPECT_NEAR(721.292f, rect.top, 0.001f); |
| 387 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 388 | // Check that updating the rectangle would succeed. |
| 389 | rect.left -= 60.f; |
| 390 | rect.right -= 60.f; |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 391 | ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 392 | FS_RECTF new_rect; |
| 393 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect)); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 394 | EXPECT_EQ(rect.right, new_rect.right); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 395 | FPDFPage_CloseAnnot(annot); |
| 396 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 397 | // Check that updating the rectangle changes the annotation's position. |
| 398 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 399 | CompareBitmap(bitmap, 612, 792, md5_modified_highlight); |
| 400 | FPDFBitmap_Destroy(bitmap); |
| 401 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 402 | // Retrieve the square annotation which has its AP stream already defined. |
| 403 | annot = FPDFPage_GetAnnot(page, 2); |
| 404 | ASSERT_TRUE(annot); |
| 405 | EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot)); |
| 406 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 407 | // Check that updating the rectangle would succeed. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 408 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 409 | rect.left += 70.f; |
| 410 | rect.right += 70.f; |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 411 | ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 412 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect)); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 413 | EXPECT_EQ(rect.right, new_rect.right); |
| 414 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 415 | // Check that updating the rectangle changes the square annotation's position. |
| 416 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 417 | CompareBitmap(bitmap, 612, 792, md5_modified_square); |
| 418 | FPDFBitmap_Destroy(bitmap); |
| 419 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 420 | FPDFPage_CloseAnnot(annot); |
| 421 | UnloadPage(page); |
| 422 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 423 | |
| 424 | TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) { |
| 425 | // Open a file with 3 annotations on its first page. |
| 426 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 427 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 428 | ASSERT_TRUE(page); |
| 429 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 430 | |
| 431 | // Check that the annotations have the expected rectangle coordinates. |
| 432 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 433 | FS_RECTF rect; |
| 434 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 435 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 436 | FPDFPage_CloseAnnot(annot); |
| 437 | |
| 438 | annot = FPDFPage_GetAnnot(page, 1); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 439 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 440 | EXPECT_NEAR(149.8127f, rect.left, 0.001f); |
| 441 | FPDFPage_CloseAnnot(annot); |
| 442 | |
| 443 | annot = FPDFPage_GetAnnot(page, 2); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 444 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 445 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 446 | FPDFPage_CloseAnnot(annot); |
| 447 | |
| 448 | // Check that nothing happens when attempting to remove an annotation with an |
| 449 | // out-of-bound index. |
| 450 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4)); |
| 451 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1)); |
| 452 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 453 | |
| 454 | // Remove the second annotation. |
| 455 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1)); |
| 456 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 457 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 2)); |
| 458 | |
| 459 | // Save the document, closing the page and document. |
| 460 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 461 | FPDF_ClosePage(page); |
| 462 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 463 | // TODO(npm): TestSaved changes annot rect dimensions by 1?? |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 464 | // Open the saved document. |
| 465 | std::string new_file = GetString(); |
| 466 | FPDF_FILEACCESS file_access; |
| 467 | memset(&file_access, 0, sizeof(file_access)); |
| 468 | file_access.m_FileLen = new_file.size(); |
| 469 | file_access.m_GetBlock = GetBlockFromString; |
| 470 | file_access.m_Param = &new_file; |
| 471 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 472 | ASSERT_TRUE(new_doc); |
| 473 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 474 | ASSERT_TRUE(new_page); |
| 475 | |
| 476 | // Check that the saved document has 2 annotations on the first page. |
| 477 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page)); |
| 478 | |
| 479 | // Check that the remaining 2 annotations are the original 1st and 3rd ones by |
| 480 | // verifying their rectangle coordinates. |
| 481 | annot = FPDFPage_GetAnnot(new_page, 0); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 482 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 483 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 484 | FPDFPage_CloseAnnot(annot); |
| 485 | |
| 486 | annot = FPDFPage_GetAnnot(new_page, 1); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 487 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 488 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 489 | FPDFPage_CloseAnnot(annot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 490 | FPDF_ClosePage(new_page); |
| 491 | FPDF_CloseDocument(new_doc); |
| 492 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 493 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 494 | TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) { |
| 495 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 496 | const char md5_original[] = "c35408717759562d1f8bf33d317483d2"; |
| 497 | const char md5_modified_path[] = "cf3cea74bd46497520ff6c4d1ea228c8"; |
| 498 | const char md5_two_paths[] = "e8994452fc4385337bae5522354e10ff"; |
| 499 | const char md5_new_annot[] = "ee5372b31fede117fc83b9384598aa25"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 500 | #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 501 | const char md5_original[] = "4f64add0190ede63f7bb9eb1e2e83edb"; |
| 502 | const char md5_modified_path[] = "681f0d0738dded0722e146f6c219bfac"; |
| 503 | const char md5_two_paths[] = "67c7e90fc3b64e20f6b69a1744f7f4f0"; |
| 504 | const char md5_new_annot[] = "262187984451bae2fe826067d68623ff"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 505 | #else |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 506 | const char md5_original[] = "02e1c6adff8fee4aeabd91c2c2e4be43"; |
| 507 | const char md5_modified_path[] = "87a78cbacd8509b961a67be56b5665a2"; |
| 508 | const char md5_two_paths[] = "76e985c18b73ceacf409f77f978176d4"; |
| 509 | const char md5_new_annot[] = "c95de7a9a1f61faca03d953961a319b9"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 510 | #endif |
| 511 | |
| 512 | // Open a file with two annotations and load its first page. |
| 513 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
| 514 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 515 | ASSERT_TRUE(page); |
| 516 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 517 | |
| 518 | // Check that the page renders correctly. |
| 519 | FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 520 | CompareBitmap(bitmap, 595, 842, md5_original); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 521 | FPDFBitmap_Destroy(bitmap); |
| 522 | |
| 523 | // Retrieve the stamp annotation which has its AP stream already defined. |
| 524 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 525 | ASSERT_TRUE(annot); |
| 526 | |
| 527 | // Check that this annotation has one path object and retrieve it. |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 528 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot)); |
| 529 | FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot, 1); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 530 | EXPECT_FALSE(path); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 531 | path = FPDFAnnot_GetObject(annot, 0); |
| 532 | EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 533 | EXPECT_TRUE(path); |
| 534 | |
| 535 | // Modify the color of the path object. |
| 536 | EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 537 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot, path)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 538 | |
| 539 | // Check that the page with the modified annotation renders correctly. |
| 540 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 541 | CompareBitmap(bitmap, 595, 842, md5_modified_path); |
| 542 | FPDFBitmap_Destroy(bitmap); |
| 543 | |
| 544 | // Add a second path object to the same annotation. |
| 545 | FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84); |
| 546 | EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88)); |
| 547 | EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100)); |
| 548 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14)); |
| 549 | EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1)); |
| 550 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot, dot)); |
| 551 | EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot)); |
| 552 | |
| 553 | // Check that the page with an annotation with two paths renders correctly. |
| 554 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 555 | CompareBitmap(bitmap, 595, 842, md5_two_paths); |
| 556 | FPDFBitmap_Destroy(bitmap); |
| 557 | |
| 558 | // Delete the newly added path object. |
| 559 | EXPECT_TRUE(FPDFAnnot_RemoveObject(annot, 1)); |
| 560 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot)); |
| 561 | FPDFPage_CloseAnnot(annot); |
| 562 | |
| 563 | // Check that the page renders the same as before. |
| 564 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 565 | CompareBitmap(bitmap, 595, 842, md5_modified_path); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 566 | FPDFBitmap_Destroy(bitmap); |
| 567 | |
| 568 | // Create another stamp annotation and set its annotation rectangle. |
| 569 | annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP); |
| 570 | ASSERT_TRUE(annot); |
| 571 | FS_RECTF rect; |
| 572 | rect.left = 200.f; |
| 573 | rect.bottom = 400.f; |
| 574 | rect.right = 500.f; |
| 575 | rect.top = 600.f; |
| 576 | EXPECT_TRUE(FPDFAnnot_SetRect(annot, &rect)); |
| 577 | |
| 578 | // Add a new path to the annotation. |
| 579 | FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500); |
| 580 | EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400)); |
| 581 | EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600)); |
| 582 | EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550)); |
| 583 | EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450)); |
| 584 | EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180)); |
| 585 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f)); |
| 586 | EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 587 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot, check)); |
| 588 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 589 | |
| 590 | // Check that the annotation's bounding box came from its rectangle. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 591 | FS_RECTF new_rect; |
| 592 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 593 | EXPECT_EQ(rect.left, new_rect.left); |
| 594 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 595 | EXPECT_EQ(rect.right, new_rect.right); |
| 596 | EXPECT_EQ(rect.top, new_rect.top); |
| 597 | |
| 598 | // Save the document, closing the page and document. |
| 599 | FPDFPage_CloseAnnot(annot); |
| 600 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 601 | FPDF_ClosePage(page); |
| 602 | |
| 603 | // Open the saved document. |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 604 | TestSaved(595, 842, md5_new_annot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 605 | |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 606 | // Check that the document has a correct count of annotations and objects. |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 607 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(m_SavedPage)); |
| 608 | annot = FPDFPage_GetAnnot(m_SavedPage, 2); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 609 | ASSERT_TRUE(annot); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 610 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 611 | |
| 612 | // Check that the new annotation's rectangle is as defined. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 613 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 614 | EXPECT_EQ(rect.left, new_rect.left); |
| 615 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 616 | EXPECT_EQ(rect.right, new_rect.right); |
| 617 | EXPECT_EQ(rect.top, new_rect.top); |
| 618 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 619 | FPDFPage_CloseAnnot(annot); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 620 | CloseSaved(); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 621 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 622 | |
| 623 | TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) { |
| 624 | // Open a file with an annotation and load its first page. |
| 625 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
| 626 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 627 | ASSERT_TRUE(page); |
| 628 | |
| 629 | // Check that the page renders correctly. |
| 630 | FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 631 | CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 632 | FPDFBitmap_Destroy(bitmap); |
| 633 | |
| 634 | // Retrieve the annotation. |
| 635 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 636 | ASSERT_TRUE(annot); |
| 637 | |
| 638 | // Check that the original flag values are as expected. |
| 639 | int flags = FPDFAnnot_GetFlags(annot); |
| 640 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 641 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
| 642 | |
| 643 | // Set the HIDDEN flag. |
| 644 | flags |= FPDF_ANNOT_FLAG_HIDDEN; |
| 645 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot, flags)); |
| 646 | flags = FPDFAnnot_GetFlags(annot); |
| 647 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 648 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
| 649 | |
| 650 | // Check that the page renders correctly without rendering the annotation. |
| 651 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 652 | CompareBitmap(bitmap, 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3"); |
| 653 | FPDFBitmap_Destroy(bitmap); |
| 654 | |
| 655 | // Unset the HIDDEN flag. |
| 656 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot, FPDF_ANNOT_FLAG_NONE)); |
| 657 | EXPECT_FALSE(FPDFAnnot_GetFlags(annot)); |
| 658 | flags &= ~FPDF_ANNOT_FLAG_HIDDEN; |
| 659 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot, flags)); |
| 660 | flags = FPDFAnnot_GetFlags(annot); |
| 661 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 662 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
| 663 | |
| 664 | // Check that the page renders correctly as before. |
| 665 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 666 | CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 667 | FPDFBitmap_Destroy(bitmap); |
| 668 | |
| 669 | FPDFPage_CloseAnnot(annot); |
| 670 | UnloadPage(page); |
| 671 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 672 | |
| 673 | TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) { |
| 674 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 675 | const char md5_original[] = "c35408717759562d1f8bf33d317483d2"; |
| 676 | const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596"; |
| 677 | const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 678 | #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 679 | const char md5_original[] = "4f64add0190ede63f7bb9eb1e2e83edb"; |
| 680 | const char md5_new_image[] = "6fb176c20996cc554d0210d8c8b6138f"; |
| 681 | const char md5_modified_image[] = "546959714dfb0dcd7e7b00259e8d178c"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 682 | #else |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 683 | const char md5_original[] = "02e1c6adff8fee4aeabd91c2c2e4be43"; |
| 684 | const char md5_new_image[] = "e7658232abd8977cdc3367dd02aee04a"; |
| 685 | const char md5_modified_image[] = "f393432b9a9b452ea69022f46c8b3f75"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 686 | #endif |
| 687 | |
| 688 | // Open a file with two annotations and load its first page. |
| 689 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
| 690 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 691 | ASSERT_TRUE(page); |
| 692 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 693 | |
| 694 | // Check that the page renders correctly. |
| 695 | FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 696 | CompareBitmap(bitmap, 595, 842, md5_original); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 697 | FPDFBitmap_Destroy(bitmap); |
| 698 | |
| 699 | // Create a stamp annotation and set its annotation rectangle. |
| 700 | FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP); |
| 701 | ASSERT_TRUE(annot); |
| 702 | FS_RECTF rect; |
| 703 | rect.left = 200.f; |
| 704 | rect.bottom = 600.f; |
| 705 | rect.right = 400.f; |
| 706 | rect.top = 800.f; |
| 707 | EXPECT_TRUE(FPDFAnnot_SetRect(annot, &rect)); |
| 708 | |
| 709 | // Add a solid-color translucent image object to the new annotation. |
| 710 | constexpr int kBitmapSize = 200; |
| 711 | FPDF_BITMAP image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1); |
| 712 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, 0xeeeecccc); |
| 713 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap)); |
| 714 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap)); |
| 715 | FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document()); |
| 716 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 717 | ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0, |
| 718 | kBitmapSize, 0, 0)); |
| 719 | FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600); |
| 720 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot, image_object)); |
| 721 | FPDFPage_CloseAnnot(annot); |
| 722 | |
| 723 | // Check that the page renders correctly with the new image object. |
| 724 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 725 | CompareBitmap(bitmap, 595, 842, md5_new_image); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 726 | FPDFBitmap_Destroy(bitmap); |
| 727 | |
| 728 | // Retrieve the newly added stamp annotation and its image object. |
| 729 | annot = FPDFPage_GetAnnot(page, 2); |
| 730 | ASSERT_TRUE(annot); |
| 731 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot)); |
| 732 | image_object = FPDFAnnot_GetObject(annot, 0); |
| 733 | EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object)); |
| 734 | |
| 735 | // Modify the image in the new annotation. |
| 736 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, 0xff000000); |
| 737 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 738 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot, image_object)); |
| 739 | FPDFPage_CloseAnnot(annot); |
| 740 | |
| 741 | // Save the document, closing the page and document. |
| 742 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 743 | FPDF_ClosePage(page); |
| 744 | |
| 745 | // Test that the saved document renders the modified image object correctly. |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 746 | TestSaved(595, 842, md5_modified_image); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 747 | |
| 748 | FPDFBitmap_Destroy(image_bitmap); |
| 749 | CloseSaved(); |
| 750 | } |
| 751 | |
| 752 | TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) { |
| 753 | #if _FXM_PLATFORM_ == _FXM_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_text[] = "e5680ed048c2cfd9a1d27212cdf41286"; |
| 756 | const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 757 | #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 758 | const char md5_original[] = "4f64add0190ede63f7bb9eb1e2e83edb"; |
| 759 | const char md5_new_text[] = "998abae4962f8f41e094e7612d8339fc"; |
| 760 | const char md5_modified_text[] = "e89b82ca4589b8f0b45fff42ca3a96a4"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 761 | #else |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 762 | const char md5_original[] = "02e1c6adff8fee4aeabd91c2c2e4be43"; |
| 763 | const char md5_new_text[] = "3fbbaec4d846ccf2be89e09daae0273d"; |
| 764 | const char md5_modified_text[] = "2ad0acaf2d8990bcdf48e1d12e6c44ad"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 765 | #endif |
| 766 | |
| 767 | // Open a file with two annotations and load its first page. |
| 768 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
| 769 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 770 | ASSERT_TRUE(page); |
| 771 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 772 | |
| 773 | // Check that the page renders correctly. |
| 774 | FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 775 | CompareBitmap(bitmap, 595, 842, md5_original); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 776 | FPDFBitmap_Destroy(bitmap); |
| 777 | |
| 778 | // Create a stamp annotation and set its annotation rectangle. |
| 779 | FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP); |
| 780 | ASSERT_TRUE(annot); |
| 781 | FS_RECTF rect; |
| 782 | rect.left = 200.f; |
| 783 | rect.bottom = 550.f; |
| 784 | rect.right = 450.f; |
| 785 | rect.top = 650.f; |
| 786 | EXPECT_TRUE(FPDFAnnot_SetRect(annot, &rect)); |
| 787 | |
| 788 | // Add a translucent text object to the new annotation. |
| 789 | FPDF_PAGEOBJECT text_object = |
| 790 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 791 | EXPECT_TRUE(text_object); |
| 792 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 793 | GetFPDFWideString(L"I'm a translucent text laying on other text."); |
| 794 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 795 | EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150)); |
| 796 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600); |
| 797 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot, text_object)); |
| 798 | FPDFPage_CloseAnnot(annot); |
| 799 | |
| 800 | // Check that the page renders correctly with the new text object. |
| 801 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 802 | CompareBitmap(bitmap, 595, 842, md5_new_text); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 803 | FPDFBitmap_Destroy(bitmap); |
| 804 | |
| 805 | // Retrieve the newly added stamp annotation and its text object. |
| 806 | annot = FPDFPage_GetAnnot(page, 2); |
| 807 | ASSERT_TRUE(annot); |
| 808 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot)); |
| 809 | text_object = FPDFAnnot_GetObject(annot, 0); |
| 810 | EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object)); |
| 811 | |
| 812 | // Modify the text in the new annotation. |
| 813 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text = |
| 814 | GetFPDFWideString(L"New text!"); |
| 815 | EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get())); |
| 816 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot, text_object)); |
| 817 | FPDFPage_CloseAnnot(annot); |
| 818 | |
| 819 | // Check that the page renders correctly with the modified text object. |
| 820 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 821 | CompareBitmap(bitmap, 595, 842, md5_modified_text); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 822 | FPDFBitmap_Destroy(bitmap); |
| 823 | |
| 824 | // Remove the new annotation, and check that the page renders as before. |
| 825 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2)); |
| 826 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 827 | CompareBitmap(bitmap, 595, 842, md5_original); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 828 | FPDFBitmap_Destroy(bitmap); |
| 829 | |
| 830 | UnloadPage(page); |
| 831 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 832 | |
| 833 | TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) { |
| 834 | // Open a file with four annotations and load its first page. |
| 835 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
| 836 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 837 | ASSERT_TRUE(page); |
| 838 | |
| 839 | // Retrieve the first annotation. |
| 840 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 841 | ASSERT_TRUE(annot); |
| 842 | |
| 843 | // Check that a non-existent key does not exist. |
| 844 | EXPECT_FALSE(FPDFAnnot_HasKey(annot, GetFPDFWideString(L"none").get())); |
| 845 | |
| 846 | // Check that the string value of a non-string dictionary entry is empty. |
| 847 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> ap_key = |
| 848 | GetFPDFWideString(L"AP"); |
| 849 | EXPECT_TRUE(FPDFAnnot_HasKey(annot, ap_key.get())); |
| 850 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, ap_key.get())); |
| 851 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot, ap_key.get(), nullptr, 0)); |
| 852 | |
| 853 | // Check that the string value of the hash is correct. |
| 854 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> hash_key = |
| 855 | GetFPDFWideString(L"AAPL:Hash"); |
| 856 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot, hash_key.get())); |
| 857 | unsigned long len = |
| 858 | FPDFAnnot_GetStringValue(annot, hash_key.get(), nullptr, 0); |
| 859 | std::vector<char> buf(len); |
| 860 | EXPECT_EQ(66u, |
| 861 | FPDFAnnot_GetStringValue(annot, hash_key.get(), buf.data(), len)); |
| 862 | EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad", |
| 863 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 864 | .c_str()); |
| 865 | |
| 866 | // Check that the string value of the modified date is correct. |
| 867 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> date_key = |
| 868 | GetFPDFWideString(L"M"); |
| 869 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot, hash_key.get())); |
| 870 | len = FPDFAnnot_GetStringValue(annot, date_key.get(), nullptr, 0); |
| 871 | buf.clear(); |
| 872 | buf.resize(len); |
| 873 | EXPECT_EQ(44u, |
| 874 | FPDFAnnot_GetStringValue(annot, date_key.get(), buf.data(), len)); |
| 875 | EXPECT_STREQ(L"D:201706071721Z00'00'", |
| 876 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 877 | .c_str()); |
| 878 | |
| 879 | // Update the date entry for the annotation. |
| 880 | const wchar_t new_date[] = L"D:201706282359Z00'00'"; |
| 881 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 882 | GetFPDFWideString(new_date); |
| 883 | EXPECT_TRUE(FPDFAnnot_SetStringValue(annot, date_key.get(), text.get())); |
| 884 | |
| 885 | // Save the document, closing the page and document. |
| 886 | FPDFPage_CloseAnnot(annot); |
| 887 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 888 | FPDF_ClosePage(page); |
| 889 | |
| 890 | // Open the saved annotation. |
| 891 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Artem Strygin | d24b97e | 2017-08-09 18:50:59 +0300 | [diff] [blame] | 892 | const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 893 | #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
Artem Strygin | d24b97e | 2017-08-09 18:50:59 +0300 | [diff] [blame] | 894 | const char md5[] = "0e3710ea6476f5bcba2cd39eb42d54e2"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 895 | #else |
Artem Strygin | d24b97e | 2017-08-09 18:50:59 +0300 | [diff] [blame] | 896 | const char md5[] = "831a3c465a56d2e0c89aef7bdf15306a"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 897 | #endif |
| 898 | TestSaved(595, 842, md5); |
| 899 | FPDF_ANNOTATION new_annot = FPDFPage_GetAnnot(m_SavedPage, 0); |
| 900 | |
| 901 | // Check that the string value of the modified date is the newly-set value. |
| 902 | EXPECT_EQ(FPDF_OBJECT_STRING, |
| 903 | FPDFAnnot_GetValueType(new_annot, date_key.get())); |
| 904 | len = FPDFAnnot_GetStringValue(new_annot, date_key.get(), nullptr, 0); |
| 905 | buf.clear(); |
| 906 | buf.resize(len); |
| 907 | EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot, date_key.get(), buf.data(), |
| 908 | len)); |
| 909 | EXPECT_STREQ(new_date, |
| 910 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 911 | .c_str()); |
| 912 | |
| 913 | FPDFPage_CloseAnnot(new_annot); |
| 914 | CloseSaved(); |
| 915 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 916 | |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 917 | TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) { |
| 918 | // Open a file with annotations and load its first page. |
| 919 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
| 920 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 921 | ASSERT_TRUE(page); |
| 922 | |
| 923 | // Retrieve the highlight annotation which has its popup defined. |
| 924 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 925 | ASSERT_TRUE(annot); |
| 926 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot)); |
| 927 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> popup_key = |
| 928 | GetFPDFWideString(L"Popup"); |
| 929 | ASSERT_TRUE(FPDFAnnot_HasKey(annot, popup_key.get())); |
| 930 | ASSERT_EQ(FPDF_OBJECT_REFERENCE, |
| 931 | FPDFAnnot_GetValueType(annot, popup_key.get())); |
| 932 | |
| 933 | // Retrieve and verify the popup of the highlight annotation. |
| 934 | FPDF_ANNOTATION popup = FPDFAnnot_GetLinkedAnnot(annot, popup_key.get()); |
| 935 | ASSERT_TRUE(popup); |
| 936 | EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup)); |
| 937 | FS_RECTF rect; |
| 938 | ASSERT_TRUE(FPDFAnnot_GetRect(popup, &rect)); |
| 939 | EXPECT_NEAR(612.0f, rect.left, 0.001f); |
| 940 | EXPECT_NEAR(578.792, rect.bottom, 0.001f); |
| 941 | |
| 942 | // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail, since |
| 943 | // "IRT" is not a key in |annot|'s dictionary. |
| 944 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> irt_key = |
| 945 | GetFPDFWideString(L"IRT"); |
| 946 | ASSERT_FALSE(FPDFAnnot_HasKey(annot, irt_key.get())); |
| 947 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot, irt_key.get())); |
| 948 | |
| 949 | // Attempting to retrieve |annot|'s parent dictionary as an annotation would |
| 950 | // fail, since its parent is not an annotation. |
| 951 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> p_key = |
| 952 | GetFPDFWideString(L"P"); |
| 953 | ASSERT_TRUE(FPDFAnnot_HasKey(annot, p_key.get())); |
| 954 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, p_key.get())); |
| 955 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot, p_key.get())); |
| 956 | |
| 957 | FPDFPage_CloseAnnot(popup); |
| 958 | FPDFPage_CloseAnnot(annot); |
| 959 | UnloadPage(page); |
| 960 | } |
| 961 | |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 962 | TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) { |
| 963 | // Open file with form text fields. |
| 964 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 965 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 966 | ASSERT_TRUE(page); |
| 967 | |
| 968 | // Retrieve the first annotation: user-editable text field. |
| 969 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 970 | ASSERT_TRUE(annot); |
| 971 | |
| 972 | // Check that the flag values are as expected. |
| 973 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 974 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 975 | FPDFPage_CloseAnnot(annot); |
| 976 | |
| 977 | // Retrieve the second annotation: read-only text field. |
| 978 | annot = FPDFPage_GetAnnot(page, 1); |
| 979 | ASSERT_TRUE(annot); |
| 980 | |
| 981 | // Check that the flag values are as expected. |
| 982 | flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 983 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 984 | FPDFPage_CloseAnnot(annot); |
| 985 | |
| 986 | UnloadPage(page); |
| 987 | } |
| 988 | |
| 989 | TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) { |
| 990 | // Open file with form text fields. |
| 991 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 992 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 993 | ASSERT_TRUE(page); |
| 994 | |
| 995 | // Retrieve the first annotation: user-editable combobox. |
| 996 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 997 | ASSERT_TRUE(annot); |
| 998 | |
| 999 | // Check that the flag values are as expected. |
| 1000 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1001 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1002 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1003 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1004 | FPDFPage_CloseAnnot(annot); |
| 1005 | |
| 1006 | // Retrieve the second annotation: regular combobox. |
| 1007 | annot = FPDFPage_GetAnnot(page, 1); |
| 1008 | ASSERT_TRUE(annot); |
| 1009 | |
| 1010 | // Check that the flag values are as expected. |
| 1011 | flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1012 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1013 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1014 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1015 | FPDFPage_CloseAnnot(annot); |
| 1016 | |
| 1017 | // Retrieve the third annotation: read-only combobox. |
| 1018 | annot = FPDFPage_GetAnnot(page, 2); |
| 1019 | ASSERT_TRUE(annot); |
| 1020 | |
| 1021 | // Check that the flag values are as expected. |
| 1022 | flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1023 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1024 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1025 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1026 | FPDFPage_CloseAnnot(annot); |
| 1027 | |
| 1028 | UnloadPage(page); |
| 1029 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1030 | |
| 1031 | TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) { |
| 1032 | // Open file with form text fields. |
| 1033 | EXPECT_TRUE(OpenDocument("text_form.pdf")); |
| 1034 | FPDF_PAGE page = LoadPage(0); |
| 1035 | ASSERT_TRUE(page); |
| 1036 | |
| 1037 | // Attempt to get an annotation where no annotation exists on page. |
| 1038 | FPDF_ANNOTATION annot = |
| 1039 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0); |
| 1040 | EXPECT_FALSE(annot); |
| 1041 | |
| 1042 | UnloadPage(page); |
| 1043 | } |
| 1044 | |
| 1045 | TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) { |
| 1046 | // Open file with form text fields. |
| 1047 | EXPECT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 1048 | FPDF_PAGE page = LoadPage(0); |
| 1049 | ASSERT_TRUE(page); |
| 1050 | |
| 1051 | // Retrieve user-editable text field annotation. |
| 1052 | FPDF_ANNOTATION annot = |
| 1053 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118); |
| 1054 | ASSERT_TRUE(annot); |
| 1055 | |
| 1056 | // Check that interactive form annotation flag values are as expected. |
| 1057 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1058 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1059 | FPDFPage_CloseAnnot(annot); |
| 1060 | |
| 1061 | // Retrieve read-only text field annotation. |
| 1062 | annot = FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202); |
| 1063 | ASSERT_TRUE(annot); |
| 1064 | |
| 1065 | // Check that interactive form annotation flag values are as expected. |
| 1066 | flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1067 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1068 | FPDFPage_CloseAnnot(annot); |
| 1069 | |
| 1070 | UnloadPage(page); |
| 1071 | } |
| 1072 | |
| 1073 | TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) { |
| 1074 | // Open file with form comboboxes. |
| 1075 | EXPECT_TRUE(OpenDocument("combobox_form.pdf")); |
| 1076 | FPDF_PAGE page = LoadPage(0); |
| 1077 | ASSERT_TRUE(page); |
| 1078 | |
| 1079 | // Retrieve user-editable combobox annotation. |
| 1080 | FPDF_ANNOTATION annot = |
| 1081 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 63); |
| 1082 | ASSERT_TRUE(annot); |
| 1083 | |
| 1084 | // Check that interactive form annotation flag values are as expected. |
| 1085 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1086 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1087 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1088 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1089 | FPDFPage_CloseAnnot(annot); |
| 1090 | |
| 1091 | // Retrieve regular combobox annotation. |
| 1092 | annot = FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 113); |
| 1093 | ASSERT_TRUE(annot); |
| 1094 | |
| 1095 | // Check that interactive form annotation flag values are as expected. |
| 1096 | flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1097 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1098 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1099 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1100 | FPDFPage_CloseAnnot(annot); |
| 1101 | |
| 1102 | // Retrieve read-only combobox annotation. |
| 1103 | annot = FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 213); |
| 1104 | ASSERT_TRUE(annot); |
| 1105 | |
| 1106 | // Check that interactive form annotation flag values are as expected. |
| 1107 | flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1108 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1109 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1110 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1111 | FPDFPage_CloseAnnot(annot); |
| 1112 | |
| 1113 | UnloadPage(page); |
| 1114 | } |