blob: ba9bbe0ea9dd95c54c93bf2d2e66a6da0a5b99a2 [file] [log] [blame]
Jane Liu4fd9a472017-06-01 18:56:09 -04001// 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 Nakashimaa74e75d2018-01-10 18:06:55 +00005#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -04006#include <memory>
7#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04008#include <vector>
9
Jane Liubaa7ff42017-06-29 19:18:23 -040010#include "core/fxcrt/fx_system.h"
Lei Zhanga21d5932018-02-05 18:28:38 +000011#include "public/cpp/fpdf_deleters.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040012#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040013#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040014#include "public/fpdfview.h"
15#include "testing/embedder_test.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000016#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040017#include "testing/gtest/include/gtest/gtest.h"
18
Lei Zhangdf064df2017-08-31 02:33:27 -070019static constexpr char kContentsKey[] = "Contents";
20
Nicolas Pena3ff54002017-07-05 11:55:35 -040021class FPDFAnnotEmbeddertest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040022
Lei Zhang4afee172018-01-10 20:57:15 +000023std::wstring BufferToWString(const std::vector<char>& buf) {
24 return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000025}
26
Lei Zhang4afee172018-01-10 20:57:15 +000027std::string BufferToString(const std::vector<char>& buf) {
28 return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000029}
30
Jane Liue17011d2017-06-21 12:18:37 -040031TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) {
32 // Open a file with one annotation and load its first page.
33 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
34 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
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.
Nicolas Pena3ff54002017-07-05 11:55:35 -040041 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle(), FPDF_ANNOT);
Jane Liue17011d2017-06-21 12:18:37 -040042 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
43 FPDFBitmap_Destroy(bitmap);
44
45 UnloadPage(page);
46}
47
Jane Liu4fd9a472017-06-01 18:56:09 -040048TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
49 // Open a file with one annotation and load its first page.
50 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
51 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
52 ASSERT_TRUE(page);
53
54 // Check that there is a total of 1 annotation on its first page.
55 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
56
57 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +000058 {
59 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
60 FPDFPage_GetAnnot(page, 0));
61 ASSERT_TRUE(annot);
62 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -040063
Lei Zhanga21d5932018-02-05 18:28:38 +000064 // Check that the annotation color is yellow.
65 unsigned int R;
66 unsigned int G;
67 unsigned int B;
68 unsigned int A;
69 EXPECT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
70 &G, &B, &A));
71 EXPECT_EQ(255u, R);
72 EXPECT_EQ(255u, G);
73 EXPECT_EQ(0u, B);
74 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -040075
Lei Zhanga21d5932018-02-05 18:28:38 +000076 // Check that the author is correct.
77 static constexpr char kAuthorKey[] = "T";
78 EXPECT_EQ(FPDF_OBJECT_STRING,
79 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
80 unsigned long len =
81 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
82 std::vector<char> buf(len);
83 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
84 len));
85 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -040086
Lei Zhanga21d5932018-02-05 18:28:38 +000087 // Check that the content is correct.
88 EXPECT_EQ(FPDF_OBJECT_STRING,
89 FPDFAnnot_GetValueType(annot.get(), kContentsKey));
90 len = FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
91 buf.clear();
92 buf.resize(len);
93 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
94 buf.data(), len));
95 const wchar_t contents[] =
96 L"This is a note for that highlight annotation. Very long highlight "
97 "annotation. Long long long Long 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 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 long. END";
116 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400117
Lei Zhanga21d5932018-02-05 18:28:38 +0000118 // Check that the quadpoints are correct.
119 FS_QUADPOINTSF quadpoints;
120 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &quadpoints));
121 EXPECT_EQ(115.802643f, quadpoints.x1);
122 EXPECT_EQ(718.913940f, quadpoints.y1);
123 EXPECT_EQ(157.211182f, quadpoints.x4);
124 EXPECT_EQ(706.264465f, quadpoints.y4);
125 }
Jane Liu4fd9a472017-06-01 18:56:09 -0400126 UnloadPage(page);
127}
128
129TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
130 // Open a file with three annotations and load its first page.
131 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
132 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
133 ASSERT_TRUE(page);
134
135 // Check that there is a total of 3 annotation on its first page.
136 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
137
Lei Zhanga21d5932018-02-05 18:28:38 +0000138 {
139 // Check that the third annotation is of type "ink".
140 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
141 FPDFPage_GetAnnot(page, 2));
142 ASSERT_TRUE(annot);
143 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400144
Lei Zhanga21d5932018-02-05 18:28:38 +0000145 // Check that the annotation color is blue with opacity.
146 unsigned int R;
147 unsigned int G;
148 unsigned int B;
149 unsigned int A;
150 EXPECT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
151 &G, &B, &A));
152 EXPECT_EQ(0u, R);
153 EXPECT_EQ(0u, G);
154 EXPECT_EQ(255u, B);
155 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400156
Lei Zhanga21d5932018-02-05 18:28:38 +0000157 // Check that there is no content.
158 EXPECT_EQ(2u,
159 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400160
Lei Zhanga21d5932018-02-05 18:28:38 +0000161 // Check that the rectange coordinates are correct.
162 // Note that upon rendering, the rectangle coordinates will be adjusted.
163 FS_RECTF rect;
164 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
165 EXPECT_EQ(351.820404f, rect.left);
166 EXPECT_EQ(583.830688f, rect.bottom);
167 EXPECT_EQ(475.336090f, rect.right);
168 EXPECT_EQ(681.535034f, rect.top);
169 }
Jane Liu4fd9a472017-06-01 18:56:09 -0400170 UnloadPage(page);
171}
Jane Liu20eafda2017-06-07 10:33:24 -0400172
173TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
174 // Open a file with one annotation and load its first page.
175 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
176 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
177 ASSERT_TRUE(page);
178
179 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400180 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400181
182 UnloadPage(page);
183}
184
Jane Liud321ef92017-06-14 09:56:22 -0400185TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400186 // Open a file with no annotation and load its first page.
187 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
188 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
189 ASSERT_TRUE(page);
190 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
191
Lei Zhanga21d5932018-02-05 18:28:38 +0000192 {
193 // Add a text annotation to the page.
194 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
195 FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
196 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400197
Lei Zhanga21d5932018-02-05 18:28:38 +0000198 // Check that there is now 1 annotations on this page.
199 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400200
Lei Zhanga21d5932018-02-05 18:28:38 +0000201 // Check that the subtype of the annotation is correct.
202 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
203 }
Jane Liue10509a2017-06-20 16:47:41 -0400204
Lei Zhanga21d5932018-02-05 18:28:38 +0000205 {
206 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
207 FPDFPage_GetAnnot(page, 0));
208 ASSERT_TRUE(annot);
209 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400210
Lei Zhanga21d5932018-02-05 18:28:38 +0000211 // Set the color of the annotation.
212 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
213 102, 153, 204));
214 // Check that the color has been set correctly.
215 unsigned int R;
216 unsigned int G;
217 unsigned int B;
218 unsigned int A;
219 EXPECT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
220 &G, &B, &A));
221 EXPECT_EQ(51u, R);
222 EXPECT_EQ(102u, G);
223 EXPECT_EQ(153u, B);
224 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400225
Lei Zhanga21d5932018-02-05 18:28:38 +0000226 // Change the color of the annotation.
227 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
228 153, 102, 51));
229 // Check that the color has been set correctly.
230 EXPECT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
231 &G, &B, &A));
232 EXPECT_EQ(204u, R);
233 EXPECT_EQ(153u, G);
234 EXPECT_EQ(102u, B);
235 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400236
Lei Zhanga21d5932018-02-05 18:28:38 +0000237 // Set the annotation rectangle.
238 FS_RECTF rect;
239 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
240 EXPECT_EQ(0.f, rect.left);
241 EXPECT_EQ(0.f, rect.right);
242 rect.left = 35;
243 rect.bottom = 150;
244 rect.right = 53;
245 rect.top = 165;
246 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
247 // Check that the annotation rectangle has been set correctly.
248 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
249 EXPECT_EQ(35.f, rect.left);
250 EXPECT_EQ(150.f, rect.bottom);
251 EXPECT_EQ(53.f, rect.right);
252 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400253
Lei Zhanga21d5932018-02-05 18:28:38 +0000254 // Set the content of the annotation.
255 static constexpr wchar_t kContents[] =
256 L"Hello! This is a customized content.";
257 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
258 GetFPDFWideString(kContents);
259 ASSERT_TRUE(
260 FPDFAnnot_SetStringValue(annot.get(), kContentsKey, text.get()));
261 // Check that the content has been set correctly.
262 unsigned long len =
263 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
264 std::vector<char> buf(len);
265 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
266 buf.data(), len));
267 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
268 }
Jane Liu20eafda2017-06-07 10:33:24 -0400269 UnloadPage(page);
270}
271
272TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
273 // Open a file with one annotation and load its first page.
274 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
275 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
276 ASSERT_TRUE(page);
277
278 // Check that there is a total of one annotation on its first page, and verify
279 // its quadpoints.
280 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400281 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000282 {
283 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
284 FPDFPage_GetAnnot(page, 0));
285 ASSERT_TRUE(annot);
286 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &quadpoints));
287 EXPECT_EQ(115.802643f, quadpoints.x1);
288 EXPECT_EQ(718.913940f, quadpoints.y1);
289 EXPECT_EQ(157.211182f, quadpoints.x4);
290 EXPECT_EQ(706.264465f, quadpoints.y4);
291 }
Jane Liu20eafda2017-06-07 10:33:24 -0400292
293 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000294 {
295 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
296 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
297 ASSERT_TRUE(annot);
298 quadpoints.x1 = 140.802643f;
299 quadpoints.x3 = 140.802643f;
300 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), &quadpoints));
301 }
Jane Liu20eafda2017-06-07 10:33:24 -0400302
303 // Save the document, closing the page and document.
304 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
305 FPDF_ClosePage(page);
306
307 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000308 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400309
310 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000311 page = LoadSavedPage(0);
312 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400313
314 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000315 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400316
Lei Zhanga21d5932018-02-05 18:28:38 +0000317 {
318 // Check that the second annotation is an underline annotation and verify
319 // its quadpoints.
320 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
321 FPDFPage_GetAnnot(page, 1));
322 ASSERT_TRUE(new_annot);
323 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
324 FS_QUADPOINTSF new_quadpoints;
325 ASSERT_TRUE(
326 FPDFAnnot_GetAttachmentPoints(new_annot.get(), &new_quadpoints));
327 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
328 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
329 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
330 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
331 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400332
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000333 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400334 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400335}
Jane Liu06462752017-06-27 16:41:14 -0400336
337TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400338#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400339 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
340 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
341 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400342#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000343 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
344 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
345 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400346#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000347 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
348 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
349 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400350#endif
351
Jane Liu06462752017-06-27 16:41:14 -0400352 // Open a file with four annotations and load its first page.
353 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
354 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
355 ASSERT_TRUE(page);
356 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
357
Jane Liub370e5a2017-08-16 13:24:58 -0400358 // Check that the original file renders correctly.
359 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
360 CompareBitmap(bitmap, 612, 792, md5_original);
361 FPDFBitmap_Destroy(bitmap);
362
Jane Liu0c6b07d2017-08-15 10:50:22 -0400363 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400364 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000365
366 // Retrieve the highlight annotation which has its AP stream already defined.
367 {
368 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
369 FPDFPage_GetAnnot(page, 0));
370 ASSERT_TRUE(annot);
371 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
372
373 // Check that color cannot be set when an AP stream is defined already.
374 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
375 102, 153, 204));
376
377 // Verify its attachment points.
378 FS_QUADPOINTSF quadpoints;
379 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &quadpoints));
380 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
381 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
382 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
383 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
384
385 // Check that updating the attachment points would succeed.
386 quadpoints.x1 -= 50.f;
387 quadpoints.x2 -= 50.f;
388 quadpoints.x3 -= 50.f;
389 quadpoints.x4 -= 50.f;
390 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), &quadpoints));
391 FS_QUADPOINTSF new_quadpoints;
392 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &new_quadpoints));
393 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
394 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
395 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
396 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
397
398 // Check that updating quadpoints does not change the annotation's position.
399 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
400 CompareBitmap(bitmap, 612, 792, md5_original);
401 FPDFBitmap_Destroy(bitmap);
402
403 // Verify its annotation rectangle.
404 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
405 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
406 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
407 EXPECT_NEAR(136.325f, rect.right, 0.001f);
408 EXPECT_NEAR(721.292f, rect.top, 0.001f);
409
410 // Check that updating the rectangle would succeed.
411 rect.left -= 60.f;
412 rect.right -= 60.f;
413 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
414 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
415 EXPECT_EQ(rect.right, new_rect.right);
416 }
Jane Liu06462752017-06-27 16:41:14 -0400417
Jane Liub370e5a2017-08-16 13:24:58 -0400418 // Check that updating the rectangle changes the annotation's position.
419 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
420 CompareBitmap(bitmap, 612, 792, md5_modified_highlight);
421 FPDFBitmap_Destroy(bitmap);
422
Lei Zhanga21d5932018-02-05 18:28:38 +0000423 {
424 // Retrieve the square annotation which has its AP stream already defined.
425 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
426 FPDFPage_GetAnnot(page, 2));
427 ASSERT_TRUE(annot);
428 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400429
Lei Zhanga21d5932018-02-05 18:28:38 +0000430 // Check that updating the rectangle would succeed.
431 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
432 rect.left += 70.f;
433 rect.right += 70.f;
434 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
435 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
436 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400437
Lei Zhanga21d5932018-02-05 18:28:38 +0000438 // Check that updating the rectangle changes the square annotation's
439 // position.
440 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
441 CompareBitmap(bitmap, 612, 792, md5_modified_square);
442 FPDFBitmap_Destroy(bitmap);
443 }
Jane Liub370e5a2017-08-16 13:24:58 -0400444
Jane Liu06462752017-06-27 16:41:14 -0400445 UnloadPage(page);
446}
Jane Liu8ce58f52017-06-29 13:40:22 -0400447
448TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
449 // Open a file with 3 annotations on its first page.
450 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
451 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
452 ASSERT_TRUE(page);
453 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
454
Jane Liu0c6b07d2017-08-15 10:50:22 -0400455 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400456
Lei Zhanga21d5932018-02-05 18:28:38 +0000457 // Check that the annotations have the expected rectangle coordinates.
458 {
459 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
460 FPDFPage_GetAnnot(page, 0));
461 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
462 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
463 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400464
Lei Zhanga21d5932018-02-05 18:28:38 +0000465 {
466 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
467 FPDFPage_GetAnnot(page, 1));
468 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
469 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
470 }
471
472 {
473 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
474 FPDFPage_GetAnnot(page, 2));
475 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
476 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
477 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400478
479 // Check that nothing happens when attempting to remove an annotation with an
480 // out-of-bound index.
481 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
482 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
483 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
484
485 // Remove the second annotation.
486 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
487 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
488 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
489
490 // Save the document, closing the page and document.
491 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
492 FPDF_ClosePage(page);
493
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400494 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400495 // Open the saved document.
496 std::string new_file = GetString();
497 FPDF_FILEACCESS file_access;
498 memset(&file_access, 0, sizeof(file_access));
499 file_access.m_FileLen = new_file.size();
500 file_access.m_GetBlock = GetBlockFromString;
501 file_access.m_Param = &new_file;
502 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
503 ASSERT_TRUE(new_doc);
504 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
505 ASSERT_TRUE(new_page);
506
507 // Check that the saved document has 2 annotations on the first page.
508 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
509
Lei Zhanga21d5932018-02-05 18:28:38 +0000510 // Check that the remaining 2 annotations are the original 1st and 3rd ones
511 // by verifying their rectangle coordinates.
512 {
513 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
514 FPDFPage_GetAnnot(new_page, 0));
515 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
516 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
517 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400518
Lei Zhanga21d5932018-02-05 18:28:38 +0000519 {
520 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
521 FPDFPage_GetAnnot(new_page, 1));
522 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
523 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
524 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400525 FPDF_ClosePage(new_page);
526 FPDF_CloseDocument(new_doc);
527}
Jane Liu8ce58f52017-06-29 13:40:22 -0400528
Jane Liubaa7ff42017-06-29 19:18:23 -0400529TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400530#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400531 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
532 const char md5_modified_path[] = "cf3cea74bd46497520ff6c4d1ea228c8";
533 const char md5_two_paths[] = "e8994452fc4385337bae5522354e10ff";
534 const char md5_new_annot[] = "ee5372b31fede117fc83b9384598aa25";
Jane Liubaa7ff42017-06-29 19:18:23 -0400535#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000536 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
537 const char md5_modified_path[] = "3f77b88ce6048e08e636c9a03921b2e5";
538 const char md5_two_paths[] = "bffbf5ecd15862b9fe553c795400ff8e";
539 const char md5_new_annot[] = "e020534c7eeea76be537c70d6e359a40";
Jane Liubaa7ff42017-06-29 19:18:23 -0400540#endif
541
542 // Open a file with two annotations and load its first page.
543 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
544 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
545 ASSERT_TRUE(page);
546 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
547
548 // Check that the page renders correctly.
549 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400550 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liubaa7ff42017-06-29 19:18:23 -0400551 FPDFBitmap_Destroy(bitmap);
552
Lei Zhanga21d5932018-02-05 18:28:38 +0000553 {
554 // Retrieve the stamp annotation which has its AP stream already defined.
555 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
556 FPDFPage_GetAnnot(page, 0));
557 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400558
Lei Zhanga21d5932018-02-05 18:28:38 +0000559 // Check that this annotation has one path object and retrieve it.
560 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
561 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
562 EXPECT_FALSE(path);
563 path = FPDFAnnot_GetObject(annot.get(), 0);
564 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
565 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400566
Lei Zhanga21d5932018-02-05 18:28:38 +0000567 // Modify the color of the path object.
568 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
569 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400570
Lei Zhanga21d5932018-02-05 18:28:38 +0000571 // Check that the page with the modified annotation renders correctly.
572 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
573 CompareBitmap(bitmap, 595, 842, md5_modified_path);
574 FPDFBitmap_Destroy(bitmap);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400575
Lei Zhanga21d5932018-02-05 18:28:38 +0000576 // Add a second path object to the same annotation.
577 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
578 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
579 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
580 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
581 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
582 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
583 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400584
Lei Zhanga21d5932018-02-05 18:28:38 +0000585 // Check that the page with an annotation with two paths renders correctly.
586 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
587 CompareBitmap(bitmap, 595, 842, md5_two_paths);
588 FPDFBitmap_Destroy(bitmap);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400589
Lei Zhanga21d5932018-02-05 18:28:38 +0000590 // Delete the newly added path object.
591 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
592 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
593 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400594
595 // Check that the page renders the same as before.
596 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
597 CompareBitmap(bitmap, 595, 842, md5_modified_path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400598 FPDFBitmap_Destroy(bitmap);
599
Jane Liubaa7ff42017-06-29 19:18:23 -0400600 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400601
Lei Zhanga21d5932018-02-05 18:28:38 +0000602 {
603 // Create another stamp annotation and set its annotation rectangle.
604 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
605 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
606 ASSERT_TRUE(annot);
607 rect.left = 200.f;
608 rect.bottom = 400.f;
609 rect.right = 500.f;
610 rect.top = 600.f;
611 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400612
Lei Zhanga21d5932018-02-05 18:28:38 +0000613 // Add a new path to the annotation.
614 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
615 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
616 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
617 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
618 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
619 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
620 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
621 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
622 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
623 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
624
625 // Check that the annotation's bounding box came from its rectangle.
626 FS_RECTF new_rect;
627 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
628 EXPECT_EQ(rect.left, new_rect.left);
629 EXPECT_EQ(rect.bottom, new_rect.bottom);
630 EXPECT_EQ(rect.right, new_rect.right);
631 EXPECT_EQ(rect.top, new_rect.top);
632 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400633
634 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400635 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
636 FPDF_ClosePage(page);
637
638 // Open the saved document.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400639 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000640 page = LoadSavedPage(0);
641 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400642
Jane Liu36567742017-07-06 11:13:35 -0400643 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000644 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400645
Lei Zhanga21d5932018-02-05 18:28:38 +0000646 {
647 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
648 FPDFPage_GetAnnot(page, 2));
649 ASSERT_TRUE(annot);
650 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400651
Lei Zhanga21d5932018-02-05 18:28:38 +0000652 // Check that the new annotation's rectangle is as defined.
653 FS_RECTF new_rect;
654 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
655 EXPECT_EQ(rect.left, new_rect.left);
656 EXPECT_EQ(rect.bottom, new_rect.bottom);
657 EXPECT_EQ(rect.right, new_rect.right);
658 EXPECT_EQ(rect.top, new_rect.top);
659 }
660
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000661 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400662 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400663}
Jane Liub137e752017-07-05 15:04:33 -0400664
665TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
666 // Open a file with an annotation and load its first page.
667 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
668 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
669 ASSERT_TRUE(page);
670
671 // Check that the page renders correctly.
672 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
673 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
674 FPDFBitmap_Destroy(bitmap);
675
Lei Zhanga21d5932018-02-05 18:28:38 +0000676 {
677 // Retrieve the annotation.
678 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
679 FPDFPage_GetAnnot(page, 0));
680 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400681
Lei Zhanga21d5932018-02-05 18:28:38 +0000682 // Check that the original flag values are as expected.
683 int flags = FPDFAnnot_GetFlags(annot.get());
684 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
685 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400686
Lei Zhanga21d5932018-02-05 18:28:38 +0000687 // Set the HIDDEN flag.
688 flags |= FPDF_ANNOT_FLAG_HIDDEN;
689 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
690 flags = FPDFAnnot_GetFlags(annot.get());
691 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
692 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400693
Lei Zhanga21d5932018-02-05 18:28:38 +0000694 // Check that the page renders correctly without rendering the annotation.
695 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
696 CompareBitmap(bitmap, 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
697 FPDFBitmap_Destroy(bitmap);
Jane Liub137e752017-07-05 15:04:33 -0400698
Lei Zhanga21d5932018-02-05 18:28:38 +0000699 // Unset the HIDDEN flag.
700 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
701 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
702 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
703 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
704 flags = FPDFAnnot_GetFlags(annot.get());
705 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
706 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400707
Lei Zhanga21d5932018-02-05 18:28:38 +0000708 // Check that the page renders correctly as before.
709 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
710 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
711 FPDFBitmap_Destroy(bitmap);
712 }
Jane Liub137e752017-07-05 15:04:33 -0400713
Jane Liub137e752017-07-05 15:04:33 -0400714 UnloadPage(page);
715}
Jane Liu36567742017-07-06 11:13:35 -0400716
717TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400718#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400719 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
720 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
721 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Jane Liu36567742017-07-06 11:13:35 -0400722#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000723 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
724 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
725 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400726#endif
727
728 // Open a file with two annotations and load its first page.
729 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
730 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
731 ASSERT_TRUE(page);
732 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
733
734 // Check that the page renders correctly.
735 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400736 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liu36567742017-07-06 11:13:35 -0400737 FPDFBitmap_Destroy(bitmap);
738
Jane Liu36567742017-07-06 11:13:35 -0400739 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000740 FPDF_BITMAP image_bitmap;
741
742 {
743 // Create a stamp annotation and set its annotation rectangle.
744 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
745 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
746 ASSERT_TRUE(annot);
747 FS_RECTF rect;
748 rect.left = 200.f;
749 rect.bottom = 600.f;
750 rect.right = 400.f;
751 rect.top = 800.f;
752 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
753
754 // Add a solid-color translucent image object to the new annotation.
755 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
756 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
757 0xeeeecccc);
758 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
759 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
760 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
761 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
762 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
763 kBitmapSize, 0, 0));
764 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
765 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
766 }
Jane Liu36567742017-07-06 11:13:35 -0400767
768 // Check that the page renders correctly with the new image object.
769 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400770 CompareBitmap(bitmap, 595, 842, md5_new_image);
Jane Liu36567742017-07-06 11:13:35 -0400771 FPDFBitmap_Destroy(bitmap);
772
Lei Zhanga21d5932018-02-05 18:28:38 +0000773 {
774 // Retrieve the newly added stamp annotation and its image object.
775 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
776 FPDFPage_GetAnnot(page, 2));
777 ASSERT_TRUE(annot);
778 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
779 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
780 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400781
Lei Zhanga21d5932018-02-05 18:28:38 +0000782 // Modify the image in the new annotation.
783 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
784 0xff000000);
785 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
786 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
787 }
Jane Liu36567742017-07-06 11:13:35 -0400788
789 // Save the document, closing the page and document.
790 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
791 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400792 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400793
794 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400795 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400796}
797
798TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400799#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400800 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
801 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
802 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Jane Liu36567742017-07-06 11:13:35 -0400803#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000804 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
805 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
806 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400807#endif
808
809 // Open a file with two annotations and load its first page.
810 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
811 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
812 ASSERT_TRUE(page);
813 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
814
815 // Check that the page renders correctly.
816 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400817 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liu36567742017-07-06 11:13:35 -0400818 FPDFBitmap_Destroy(bitmap);
819
Lei Zhanga21d5932018-02-05 18:28:38 +0000820 {
821 // Create a stamp annotation and set its annotation rectangle.
822 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
823 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
824 ASSERT_TRUE(annot);
825 FS_RECTF rect;
826 rect.left = 200.f;
827 rect.bottom = 550.f;
828 rect.right = 450.f;
829 rect.top = 650.f;
830 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -0400831
Lei Zhanga21d5932018-02-05 18:28:38 +0000832 // Add a translucent text object to the new annotation.
833 FPDF_PAGEOBJECT text_object =
834 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
835 EXPECT_TRUE(text_object);
836 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
837 GetFPDFWideString(L"I'm a translucent text laying on other text.");
838 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
839 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
840 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
841 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
842 }
Jane Liu36567742017-07-06 11:13:35 -0400843
844 // Check that the page renders correctly with the new text object.
845 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400846 CompareBitmap(bitmap, 595, 842, md5_new_text);
Jane Liu36567742017-07-06 11:13:35 -0400847 FPDFBitmap_Destroy(bitmap);
848
Lei Zhanga21d5932018-02-05 18:28:38 +0000849 {
850 // Retrieve the newly added stamp annotation and its text object.
851 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
852 FPDFPage_GetAnnot(page, 2));
853 ASSERT_TRUE(annot);
854 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
855 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
856 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -0400857
Lei Zhanga21d5932018-02-05 18:28:38 +0000858 // Modify the text in the new annotation.
859 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
860 GetFPDFWideString(L"New text!");
861 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
862 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
863 }
Jane Liu36567742017-07-06 11:13:35 -0400864
865 // Check that the page renders correctly with the modified text object.
866 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400867 CompareBitmap(bitmap, 595, 842, md5_modified_text);
Jane Liu36567742017-07-06 11:13:35 -0400868 FPDFBitmap_Destroy(bitmap);
869
870 // Remove the new annotation, and check that the page renders as before.
871 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
872 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400873 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liu36567742017-07-06 11:13:35 -0400874 FPDFBitmap_Destroy(bitmap);
875
876 UnloadPage(page);
877}
Jane Liu2e1a32b2017-07-06 12:01:25 -0400878
879TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
880 // Open a file with four annotations and load its first page.
881 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
882 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
883 ASSERT_TRUE(page);
884
Lei Zhangdf064df2017-08-31 02:33:27 -0700885 static constexpr char kDateKey[] = "M";
Lei Zhanga21d5932018-02-05 18:28:38 +0000886 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400887
Lei Zhanga21d5932018-02-05 18:28:38 +0000888 {
889 // Retrieve the first annotation.
890 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
891 FPDFPage_GetAnnot(page, 0));
892 ASSERT_TRUE(annot);
893
894 // Check that a non-existent key does not exist.
895 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
896
897 // Check that the string value of a non-string dictionary entry is empty.
898 static constexpr char kApKey[] = "AP";
899 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), kApKey));
900 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
901 FPDFAnnot_GetValueType(annot.get(), kApKey));
902 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kApKey, nullptr, 0));
903
904 // Check that the string value of the hash is correct.
905 static constexpr char kHashKey[] = "AAPL:Hash";
906 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
907 unsigned long len =
908 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
909 std::vector<char> buf(len);
910 EXPECT_EQ(66u,
911 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
912 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
913 BufferToWString(buf).c_str());
914
915 // Check that the string value of the modified date is correct.
916 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
917 len = FPDFAnnot_GetStringValue(annot.get(), kDateKey, nullptr, 0);
918 buf.clear();
919 buf.resize(len);
920 EXPECT_EQ(44u,
921 FPDFAnnot_GetStringValue(annot.get(), kDateKey, buf.data(), len));
922 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
923
924 // Update the date entry for the annotation.
925 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
926 GetFPDFWideString(kNewDate);
927 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kDateKey, text.get()));
928 }
Jane Liu2e1a32b2017-07-06 12:01:25 -0400929
930 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -0400931 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
932 FPDF_ClosePage(page);
933
934 // Open the saved annotation.
Dan Sinclair698aed72017-09-26 16:24:49 -0400935#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +0300936 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400937#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000938 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400939#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400940 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000941 page = LoadSavedPage(0);
942 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +0000943 {
944 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
945 FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -0400946
Lei Zhanga21d5932018-02-05 18:28:38 +0000947 // Check that the string value of the modified date is the newly-set value.
948 EXPECT_EQ(FPDF_OBJECT_STRING,
949 FPDFAnnot_GetValueType(new_annot.get(), kDateKey));
950 unsigned long len =
951 FPDFAnnot_GetStringValue(new_annot.get(), kDateKey, nullptr, 0);
952 std::vector<char> buf(len);
953 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot.get(), kDateKey,
954 buf.data(), len));
955 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
956 }
Jane Liu2e1a32b2017-07-06 12:01:25 -0400957
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000958 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400959 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -0400960}
Diana Gage7e0c05d2017-07-19 17:33:33 -0700961
Henrique Nakashima5970a472018-01-11 22:40:59 +0000962TEST_F(FPDFAnnotEmbeddertest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000963 // Open a file with four annotations and load its first page.
964 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
965 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
966 ASSERT_TRUE(page);
967
Lei Zhanga21d5932018-02-05 18:28:38 +0000968 {
969 // Retrieve the first annotation.
970 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
971 FPDFPage_GetAnnot(page, 0));
972 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000973
Lei Zhanga21d5932018-02-05 18:28:38 +0000974 // Check that the string value of an AP returns the expected length.
975 unsigned long normal_len = FPDFAnnot_GetAP(
976 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
977 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000978
Lei Zhanga21d5932018-02-05 18:28:38 +0000979 // Check that the string value of an AP is not returned if the buffer is too
980 // small. The result buffer should be overwritten with an empty string.
981 std::vector<char> buf(normal_len - 1);
982 // Write L"z" in the buffer to verify it's not overwritten.
983 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
984 EXPECT_EQ(73970u,
985 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
986 buf.data(), buf.size()));
987 std::string ap = BufferToString(buf);
988 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000989
Lei Zhanga21d5932018-02-05 18:28:38 +0000990 // Check that the string value of an AP is returned through a buffer that is
991 // the right size.
992 buf.clear();
993 buf.resize(normal_len);
994 EXPECT_EQ(73970u,
995 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
996 buf.data(), buf.size()));
997 ap = BufferToString(buf);
998 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
999 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001000
Lei Zhanga21d5932018-02-05 18:28:38 +00001001 // Check that the string value of an AP is returned through a buffer that is
1002 // larger than necessary.
1003 buf.clear();
1004 buf.resize(normal_len + 1);
1005 EXPECT_EQ(73970u,
1006 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1007 buf.data(), buf.size()));
1008 ap = BufferToString(buf);
1009 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1010 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001011
Lei Zhanga21d5932018-02-05 18:28:38 +00001012 // Check that getting an AP for a mode that does not have an AP returns an
1013 // empty string.
1014 unsigned long rollover_len = FPDFAnnot_GetAP(
1015 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1016 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001017
Lei Zhanga21d5932018-02-05 18:28:38 +00001018 buf.clear();
1019 buf.resize(1000);
1020 EXPECT_EQ(2u,
1021 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1022 buf.data(), buf.size()));
1023 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001024
Lei Zhanga21d5932018-02-05 18:28:38 +00001025 // Check that setting the AP for an invalid appearance mode fails.
1026 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1027 GetFPDFWideString(L"new test ap");
1028 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1029 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1030 apText.get()));
1031 EXPECT_FALSE(FPDFAnnot_SetAP(
1032 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001033
Lei Zhanga21d5932018-02-05 18:28:38 +00001034 // Set the AP correctly now.
1035 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1036 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001037
Lei Zhanga21d5932018-02-05 18:28:38 +00001038 // Check that the new annotation value is equal to the value we just set.
1039 rollover_len = FPDFAnnot_GetAP(
1040 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1041 EXPECT_EQ(24u, rollover_len);
1042 buf.clear();
1043 buf.resize(rollover_len);
1044 EXPECT_EQ(24u,
1045 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1046 buf.data(), buf.size()));
1047 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001048
Lei Zhanga21d5932018-02-05 18:28:38 +00001049 // Check that the Normal AP was not touched when the Rollover AP was set.
1050 buf.clear();
1051 buf.resize(normal_len);
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"));
1058 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001059
1060 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001061 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1062 FPDF_ClosePage(page);
1063
1064 OpenSavedDocument();
1065 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001066 {
1067 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
1068 FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001069
Lei Zhanga21d5932018-02-05 18:28:38 +00001070 // Check that the new annotation value is equal to the value we set before
1071 // saving.
1072 unsigned long rollover_len = FPDFAnnot_GetAP(
1073 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1074 EXPECT_EQ(24u, rollover_len);
1075 std::vector<char> buf(rollover_len);
1076 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1077 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1078 buf.data(), buf.size()));
1079 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1080 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001081
1082 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001083 CloseSavedPage(page);
1084 CloseSavedDocument();
1085}
1086
1087TEST_F(FPDFAnnotEmbeddertest, RemoveOptionalAP) {
1088 // Open a file with four annotations and load its first page.
1089 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
1090 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
1091 ASSERT_TRUE(page);
1092
Lei Zhanga21d5932018-02-05 18:28:38 +00001093 {
1094 // Retrieve the first annotation.
1095 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1096 FPDFPage_GetAnnot(page, 0));
1097 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001098
Lei Zhanga21d5932018-02-05 18:28:38 +00001099 // Set Down AP. Normal AP is already set.
1100 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1101 GetFPDFWideString(L"new test ap");
1102 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1103 apText.get()));
1104 EXPECT_EQ(73970u,
1105 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1106 nullptr, 0));
1107 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1108 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001109
Lei Zhanga21d5932018-02-05 18:28:38 +00001110 // Check that setting the Down AP to null removes the Down entry but keeps
1111 // Normal intact.
1112 EXPECT_TRUE(
1113 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1114 EXPECT_EQ(73970u,
1115 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1116 nullptr, 0));
1117 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1118 nullptr, 0));
1119 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001120
Henrique Nakashima5970a472018-01-11 22:40:59 +00001121 FPDF_ClosePage(page);
1122}
1123
1124TEST_F(FPDFAnnotEmbeddertest, RemoveRequiredAP) {
1125 // Open a file with four annotations and load its first page.
1126 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
1127 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
1128 ASSERT_TRUE(page);
1129
Lei Zhanga21d5932018-02-05 18:28:38 +00001130 {
1131 // Retrieve the first annotation.
1132 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1133 FPDFPage_GetAnnot(page, 0));
1134 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001135
Lei Zhanga21d5932018-02-05 18:28:38 +00001136 // Set Down AP. Normal AP is already set.
1137 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1138 GetFPDFWideString(L"new test ap");
1139 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1140 apText.get()));
1141 EXPECT_EQ(73970u,
1142 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1143 nullptr, 0));
1144 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1145 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001146
Lei Zhanga21d5932018-02-05 18:28:38 +00001147 // Check that setting the Normal AP to null removes the whole AP dictionary.
1148 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1149 nullptr));
1150 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1151 nullptr, 0));
1152 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1153 nullptr, 0));
1154 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001155
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001156 FPDF_ClosePage(page);
1157}
1158
Jane Liu300bb272017-08-21 14:37:53 -04001159TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) {
1160 // Open a file with annotations and load its first page.
1161 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
1162 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
1163 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001164 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001165
Lei Zhanga21d5932018-02-05 18:28:38 +00001166 {
1167 // Retrieve the highlight annotation which has its popup defined.
1168 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1169 FPDFPage_GetAnnot(page, 0));
1170 ASSERT_TRUE(annot);
1171 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1172 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1173 static constexpr char kPopupKey[] = "Popup";
1174 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1175 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1176 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001177
Lei Zhanga21d5932018-02-05 18:28:38 +00001178 // Retrieve and verify the popup of the highlight annotation.
1179 std::unique_ptr<void, FPDFAnnotationDeleter> popup(
1180 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1181 ASSERT_TRUE(popup);
1182 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1183 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1184 FS_RECTF rect;
1185 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1186 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1187 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001188
Lei Zhanga21d5932018-02-05 18:28:38 +00001189 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1190 // since "IRT" is not a key in |annot|'s dictionary.
1191 static constexpr char kIRTKey[] = "IRT";
1192 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1193 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001194
Lei Zhanga21d5932018-02-05 18:28:38 +00001195 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1196 // would fail, since its parent is not an annotation.
1197 static constexpr char kPKey[] = "P";
1198 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPKey));
1199 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1200 FPDFAnnot_GetValueType(annot.get(), kPKey));
1201 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kPKey));
1202 }
Jane Liu300bb272017-08-21 14:37:53 -04001203
Jane Liu300bb272017-08-21 14:37:53 -04001204 UnloadPage(page);
1205}
1206
Diana Gage7e0c05d2017-07-19 17:33:33 -07001207TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
1208 // Open file with form text fields.
1209 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1210 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
1211 ASSERT_TRUE(page);
1212
Lei Zhanga21d5932018-02-05 18:28:38 +00001213 {
1214 // Retrieve the first annotation: user-editable text field.
1215 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1216 FPDFPage_GetAnnot(page, 0));
1217 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001218
Lei Zhanga21d5932018-02-05 18:28:38 +00001219 // Check that the flag values are as expected.
1220 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1221 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1222 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001223
Lei Zhanga21d5932018-02-05 18:28:38 +00001224 {
1225 // Retrieve the second annotation: read-only text field.
1226 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1227 FPDFPage_GetAnnot(page, 1));
1228 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001229
Lei Zhanga21d5932018-02-05 18:28:38 +00001230 // Check that the flag values are as expected.
1231 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1232 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1233 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001234
1235 UnloadPage(page);
1236}
1237
1238TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
1239 // Open file with form text fields.
1240 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1241 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
1242 ASSERT_TRUE(page);
1243
Lei Zhanga21d5932018-02-05 18:28:38 +00001244 {
1245 // Retrieve the first annotation: user-editable combobox.
1246 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1247 FPDFPage_GetAnnot(page, 0));
1248 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001249
Lei Zhanga21d5932018-02-05 18:28:38 +00001250 // Check that the flag values are as expected.
1251 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1252 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1253 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1254 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1255 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001256
Lei Zhanga21d5932018-02-05 18:28:38 +00001257 {
1258 // Retrieve the second annotation: regular combobox.
1259 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1260 FPDFPage_GetAnnot(page, 1));
1261 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001262
Lei Zhanga21d5932018-02-05 18:28:38 +00001263 // Check that the flag values are as expected.
1264 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1265 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1266 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1267 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1268 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001269
Lei Zhanga21d5932018-02-05 18:28:38 +00001270 {
1271 // Retrieve the third annotation: read-only combobox.
1272 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1273 FPDFPage_GetAnnot(page, 2));
1274 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001275
Lei Zhanga21d5932018-02-05 18:28:38 +00001276 // Check that the flag values are as expected.
1277 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1278 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1279 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1280 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1281 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001282
1283 UnloadPage(page);
1284}
Diana Gage40870db2017-07-19 18:16:03 -07001285
1286TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
1287 // Open file with form text fields.
1288 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1289 FPDF_PAGE page = LoadPage(0);
1290 ASSERT_TRUE(page);
1291
1292 // Attempt to get an annotation where no annotation exists on page.
1293 FPDF_ANNOTATION annot =
1294 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0);
1295 EXPECT_FALSE(annot);
1296
1297 UnloadPage(page);
1298}
1299
1300TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
1301 // Open file with form text fields.
1302 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1303 FPDF_PAGE page = LoadPage(0);
1304 ASSERT_TRUE(page);
1305
Lei Zhanga21d5932018-02-05 18:28:38 +00001306 {
1307 // Retrieve user-editable text field annotation.
1308 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1309 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1310 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001311
Lei Zhanga21d5932018-02-05 18:28:38 +00001312 // Check that interactive form annotation flag values are as expected.
1313 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1314 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1315 }
Diana Gage40870db2017-07-19 18:16:03 -07001316
Lei Zhanga21d5932018-02-05 18:28:38 +00001317 {
1318 // Retrieve read-only text field annotation.
1319 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1320 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1321 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001322
Lei Zhanga21d5932018-02-05 18:28:38 +00001323 // Check that interactive form annotation flag values are as expected.
1324 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1325 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1326 }
Diana Gage40870db2017-07-19 18:16:03 -07001327
1328 UnloadPage(page);
1329}
1330
1331TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1332 // Open file with form comboboxes.
1333 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1334 FPDF_PAGE page = LoadPage(0);
1335 ASSERT_TRUE(page);
1336
Lei Zhanga21d5932018-02-05 18:28:38 +00001337 {
1338 // Retrieve user-editable combobox annotation.
1339 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1340 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 63));
1341 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001342
Lei Zhanga21d5932018-02-05 18:28:38 +00001343 // Check that interactive form annotation flag values are as expected.
1344 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1345 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1346 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1347 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1348 }
Diana Gage40870db2017-07-19 18:16:03 -07001349
Lei Zhanga21d5932018-02-05 18:28:38 +00001350 {
1351 // Retrieve regular combobox annotation.
1352 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1353 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 113));
1354 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001355
Lei Zhanga21d5932018-02-05 18:28:38 +00001356 // Check that interactive form annotation flag values are as expected.
1357 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1358 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1359 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1360 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1361 }
Diana Gage40870db2017-07-19 18:16:03 -07001362
Lei Zhanga21d5932018-02-05 18:28:38 +00001363 {
1364 // Retrieve read-only combobox annotation.
1365 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1366 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 213));
1367 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001368
Lei Zhanga21d5932018-02-05 18:28:38 +00001369 // Check that interactive form annotation flag values are as expected.
1370 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1371 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1372 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1373 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1374 }
Diana Gage40870db2017-07-19 18:16:03 -07001375
1376 UnloadPage(page);
1377}