blob: 9d5c5548b61785eb3fe08614cd76f27d42627a46 [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"));
Lei Zhang75c81712018-02-08 17:22:39 +000034 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040035 ASSERT_TRUE(page);
36
37 // This annotation has a malformed appearance stream, which does not have its
38 // normal appearance defined, only its rollover appearance. In this case, its
39 // normal appearance should be generated, allowing the highlight annotation to
40 // still display.
Lei Zhanga98e3662018-02-07 20:28:35 +000041 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
42 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
43 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040044
45 UnloadPage(page);
46}
47
Ralf Sipplb3a52402018-03-19 23:30:28 +000048TEST_F(FPDFAnnotEmbeddertest, RenderMultilineMarkupAnnotWithoutAP) {
49 const char md5_hash[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000050 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000051 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
52 FPDF_PAGE page = LoadPage(0);
53 ASSERT_TRUE(page);
54
55 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
56 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
57 CompareBitmap(bitmap.get(), 595, 842, md5_hash);
58
59 UnloadPage(page);
60}
61
Jane Liu4fd9a472017-06-01 18:56:09 -040062TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
63 // Open a file with one annotation and load its first page.
64 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000065 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
66 // the FORM API calls in LoadPage() makes this test fail. So use
67 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu4fd9a472017-06-01 18:56:09 -040068 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
69 ASSERT_TRUE(page);
70
71 // Check that there is a total of 1 annotation on its first page.
72 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
73
74 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +000075 {
76 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
77 FPDFPage_GetAnnot(page, 0));
78 ASSERT_TRUE(annot);
79 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -040080
Lei Zhanga21d5932018-02-05 18:28:38 +000081 // Check that the annotation color is yellow.
82 unsigned int R;
83 unsigned int G;
84 unsigned int B;
85 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +000086 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +000087 &G, &B, &A));
88 EXPECT_EQ(255u, R);
89 EXPECT_EQ(255u, G);
90 EXPECT_EQ(0u, B);
91 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -040092
Lei Zhanga21d5932018-02-05 18:28:38 +000093 // Check that the author is correct.
94 static constexpr char kAuthorKey[] = "T";
95 EXPECT_EQ(FPDF_OBJECT_STRING,
96 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
97 unsigned long len =
98 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
99 std::vector<char> buf(len);
100 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
101 len));
102 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400103
Lei Zhanga21d5932018-02-05 18:28:38 +0000104 // Check that the content is correct.
105 EXPECT_EQ(FPDF_OBJECT_STRING,
106 FPDFAnnot_GetValueType(annot.get(), kContentsKey));
107 len = FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
108 buf.clear();
109 buf.resize(len);
110 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
111 buf.data(), len));
112 const wchar_t contents[] =
113 L"This is a note for that highlight annotation. Very long highlight "
114 "annotation. Long long long Long long longLong long longLong long "
115 "longLong long longLong long longLong long longLong long longLong long "
116 "longLong long longLong long longLong long longLong long longLong long "
117 "longLong long longLong long longLong long longLong long longLong long "
118 "longLong long longLong long longLong long longLong long longLong long "
119 "longLong long longLong long longLong long longLong long longLong long "
120 "longLong long longLong long longLong long longLong long longLong long "
121 "longLong long longLong long longLong long longLong long longLong long "
122 "longLong long longLong long longLong long longLong long longLong long "
123 "longLong long longLong long longLong long longLong long longLong long "
124 "longLong long longLong long longLong long longLong long longLong long "
125 "longLong long longLong long longLong long longLong long longLong long "
126 "longLong long longLong long longLong long longLong long longLong long "
127 "longLong long longLong long longLong long longLong long longLong long "
128 "longLong long longLong long longLong long longLong long longLong long "
129 "longLong long longLong long longLong long longLong long longLong long "
130 "longLong long longLong long longLong long longLong long longLong long "
131 "longLong long longLong long longLong long longLong long longLong long "
132 "longLong long long. END";
133 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400134
Lei Zhanga21d5932018-02-05 18:28:38 +0000135 // Check that the quadpoints are correct.
136 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000137 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000138 EXPECT_EQ(115.802643f, quadpoints.x1);
139 EXPECT_EQ(718.913940f, quadpoints.y1);
140 EXPECT_EQ(157.211182f, quadpoints.x4);
141 EXPECT_EQ(706.264465f, quadpoints.y4);
142 }
Lei Zhang75c81712018-02-08 17:22:39 +0000143 FPDF_ClosePage(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400144}
145
146TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
147 // Open a file with three annotations and load its first page.
148 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000149 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
150 // the FORM API calls in LoadPage() makes this test fail. So use
151 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu4fd9a472017-06-01 18:56:09 -0400152 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
153 ASSERT_TRUE(page);
154
155 // Check that there is a total of 3 annotation on its first page.
156 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
157
Lei Zhanga21d5932018-02-05 18:28:38 +0000158 {
159 // Check that the third annotation is of type "ink".
160 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
161 FPDFPage_GetAnnot(page, 2));
162 ASSERT_TRUE(annot);
163 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400164
Lei Zhanga21d5932018-02-05 18:28:38 +0000165 // Check that the annotation color is blue with opacity.
166 unsigned int R;
167 unsigned int G;
168 unsigned int B;
169 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000170 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000171 &G, &B, &A));
172 EXPECT_EQ(0u, R);
173 EXPECT_EQ(0u, G);
174 EXPECT_EQ(255u, B);
175 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400176
Lei Zhanga21d5932018-02-05 18:28:38 +0000177 // Check that there is no content.
178 EXPECT_EQ(2u,
179 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400180
Lei Zhanga21d5932018-02-05 18:28:38 +0000181 // Check that the rectange coordinates are correct.
182 // Note that upon rendering, the rectangle coordinates will be adjusted.
183 FS_RECTF rect;
184 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
185 EXPECT_EQ(351.820404f, rect.left);
186 EXPECT_EQ(583.830688f, rect.bottom);
187 EXPECT_EQ(475.336090f, rect.right);
188 EXPECT_EQ(681.535034f, rect.top);
189 }
Lei Zhang75c81712018-02-08 17:22:39 +0000190 FPDF_ClosePage(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400191}
Jane Liu20eafda2017-06-07 10:33:24 -0400192
193TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
194 // Open a file with one annotation and load its first page.
195 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000196 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400197 ASSERT_TRUE(page);
198
199 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400200 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400201
202 UnloadPage(page);
203}
204
Jane Liud321ef92017-06-14 09:56:22 -0400205TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400206 // Open a file with no annotation and load its first page.
207 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000208 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400209 ASSERT_TRUE(page);
210 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
211
Lei Zhanga21d5932018-02-05 18:28:38 +0000212 {
213 // Add a text annotation to the page.
214 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
215 FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
216 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400217
Lei Zhanga21d5932018-02-05 18:28:38 +0000218 // Check that there is now 1 annotations on this page.
219 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400220
Lei Zhanga21d5932018-02-05 18:28:38 +0000221 // Check that the subtype of the annotation is correct.
222 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
223 }
Jane Liue10509a2017-06-20 16:47:41 -0400224
Lei Zhanga21d5932018-02-05 18:28:38 +0000225 {
226 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
227 FPDFPage_GetAnnot(page, 0));
228 ASSERT_TRUE(annot);
229 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400230
Lei Zhanga21d5932018-02-05 18:28:38 +0000231 // Set the color of the annotation.
232 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
233 102, 153, 204));
234 // Check that the color has been set correctly.
235 unsigned int R;
236 unsigned int G;
237 unsigned int B;
238 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000239 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000240 &G, &B, &A));
241 EXPECT_EQ(51u, R);
242 EXPECT_EQ(102u, G);
243 EXPECT_EQ(153u, B);
244 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400245
Lei Zhanga21d5932018-02-05 18:28:38 +0000246 // Change the color of the annotation.
247 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
248 153, 102, 51));
249 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000250 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000251 &G, &B, &A));
252 EXPECT_EQ(204u, R);
253 EXPECT_EQ(153u, G);
254 EXPECT_EQ(102u, B);
255 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400256
Lei Zhanga21d5932018-02-05 18:28:38 +0000257 // Set the annotation rectangle.
258 FS_RECTF rect;
259 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
260 EXPECT_EQ(0.f, rect.left);
261 EXPECT_EQ(0.f, rect.right);
262 rect.left = 35;
263 rect.bottom = 150;
264 rect.right = 53;
265 rect.top = 165;
266 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
267 // Check that the annotation rectangle has been set correctly.
268 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
269 EXPECT_EQ(35.f, rect.left);
270 EXPECT_EQ(150.f, rect.bottom);
271 EXPECT_EQ(53.f, rect.right);
272 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400273
Lei Zhanga21d5932018-02-05 18:28:38 +0000274 // Set the content of the annotation.
275 static constexpr wchar_t kContents[] =
276 L"Hello! This is a customized content.";
277 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
278 GetFPDFWideString(kContents);
279 ASSERT_TRUE(
280 FPDFAnnot_SetStringValue(annot.get(), kContentsKey, text.get()));
281 // Check that the content has been set correctly.
282 unsigned long len =
283 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
284 std::vector<char> buf(len);
285 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
286 buf.data(), len));
287 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
288 }
Jane Liu20eafda2017-06-07 10:33:24 -0400289 UnloadPage(page);
290}
291
292TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
293 // Open a file with one annotation and load its first page.
294 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000295 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400296 ASSERT_TRUE(page);
297
298 // Check that there is a total of one annotation on its first page, and verify
299 // its quadpoints.
300 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400301 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000302 {
303 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
304 FPDFPage_GetAnnot(page, 0));
305 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000306 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000307 EXPECT_EQ(115.802643f, quadpoints.x1);
308 EXPECT_EQ(718.913940f, quadpoints.y1);
309 EXPECT_EQ(157.211182f, quadpoints.x4);
310 EXPECT_EQ(706.264465f, quadpoints.y4);
311 }
Jane Liu20eafda2017-06-07 10:33:24 -0400312
313 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000314 {
315 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
316 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
317 ASSERT_TRUE(annot);
318 quadpoints.x1 = 140.802643f;
319 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000320 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000321 }
Jane Liu20eafda2017-06-07 10:33:24 -0400322
323 // Save the document, closing the page and document.
324 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000325 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400326
327 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000328 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400329
330 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000331 page = LoadSavedPage(0);
332 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400333
334 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000335 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400336
Lei Zhanga21d5932018-02-05 18:28:38 +0000337 {
338 // Check that the second annotation is an underline annotation and verify
339 // its quadpoints.
340 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
341 FPDFPage_GetAnnot(page, 1));
342 ASSERT_TRUE(new_annot);
343 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
344 FS_QUADPOINTSF new_quadpoints;
345 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000346 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000347 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
348 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
349 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
350 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
351 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400352
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000353 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400354 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400355}
Jane Liu06462752017-06-27 16:41:14 -0400356
Ralf Sippl16381792018-04-12 21:20:26 +0000357TEST_F(FPDFAnnotEmbeddertest, GetAndSetQuadPoints) {
358 // Open a file with four annotations and load its first page.
359 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
360 FPDF_PAGE page = LoadPage(0);
361 ASSERT_TRUE(page);
362 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
363
364 // Retrieve the highlight annotation.
365 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
366 ASSERT_TRUE(annot);
367 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
368
369 FS_QUADPOINTSF quadpoints;
370 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
371
372 {
373 // Verify the current one set of quadpoints.
374 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
375
376 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
377 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
378 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
379 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
380 }
381
382 {
383 // Update the quadpoints.
384 FS_QUADPOINTSF new_quadpoints = quadpoints;
385 new_quadpoints.y1 -= 20.f;
386 new_quadpoints.y2 -= 20.f;
387 new_quadpoints.y3 -= 20.f;
388 new_quadpoints.y4 -= 20.f;
389 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
390
391 // Verify added quadpoint set
392 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
393 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
394 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
395 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
396 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
397 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
398 }
399
400 {
401 // Append a new set of quadpoints.
402 FS_QUADPOINTSF new_quadpoints = quadpoints;
403 new_quadpoints.y1 += 20.f;
404 new_quadpoints.y2 += 20.f;
405 new_quadpoints.y3 += 20.f;
406 new_quadpoints.y4 += 20.f;
407 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
408
409 // Verify added quadpoint set
410 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
411 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
412 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
413 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
414 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
415 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
416 }
417
418 {
419 // Setting and getting quadpoints at out-of-bound index should fail
420 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
421 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
422 }
423
424 FPDFPage_CloseAnnot(annot);
425
426 // Retrieve the square annotation
427 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
428
429 {
430 // Check that attempting to set its quadpoints would fail
431 ASSERT_TRUE(squareAnnot);
432 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
433 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
434 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
435 }
436
437 FPDFPage_CloseAnnot(squareAnnot);
438
439 UnloadPage(page);
440}
441
Jane Liu06462752017-06-27 16:41:14 -0400442TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400443#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400444 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
445 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
446 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400447#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000448 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
449 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
450 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400451#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000452 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
453 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
454 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400455#endif
456
Jane Liu06462752017-06-27 16:41:14 -0400457 // Open a file with four annotations and load its first page.
458 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000459 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400460 ASSERT_TRUE(page);
461 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
462
Jane Liub370e5a2017-08-16 13:24:58 -0400463 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000464 {
465 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
466 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
467 CompareBitmap(bitmap.get(), 612, 792, md5_original);
468 }
Jane Liub370e5a2017-08-16 13:24:58 -0400469
Jane Liu0c6b07d2017-08-15 10:50:22 -0400470 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400471 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000472
473 // Retrieve the highlight annotation which has its AP stream already defined.
474 {
475 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
476 FPDFPage_GetAnnot(page, 0));
477 ASSERT_TRUE(annot);
478 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
479
480 // Check that color cannot be set when an AP stream is defined already.
481 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
482 102, 153, 204));
483
484 // Verify its attachment points.
485 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000486 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000487 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
488 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
489 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
490 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
491
492 // Check that updating the attachment points would succeed.
493 quadpoints.x1 -= 50.f;
494 quadpoints.x2 -= 50.f;
495 quadpoints.x3 -= 50.f;
496 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000497 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000498 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000499 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000500 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
501 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
502 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
503 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
504
505 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000506 {
507 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
508 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
509 CompareBitmap(bitmap.get(), 612, 792, md5_original);
510 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000511
512 // Verify its annotation rectangle.
513 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
514 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
515 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
516 EXPECT_NEAR(136.325f, rect.right, 0.001f);
517 EXPECT_NEAR(721.292f, rect.top, 0.001f);
518
519 // Check that updating the rectangle would succeed.
520 rect.left -= 60.f;
521 rect.right -= 60.f;
522 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
523 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
524 EXPECT_EQ(rect.right, new_rect.right);
525 }
Jane Liu06462752017-06-27 16:41:14 -0400526
Jane Liub370e5a2017-08-16 13:24:58 -0400527 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000528 {
529 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
530 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
531 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
532 }
Jane Liub370e5a2017-08-16 13:24:58 -0400533
Lei Zhanga21d5932018-02-05 18:28:38 +0000534 {
535 // Retrieve the square annotation which has its AP stream already defined.
536 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
537 FPDFPage_GetAnnot(page, 2));
538 ASSERT_TRUE(annot);
539 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400540
Lei Zhanga21d5932018-02-05 18:28:38 +0000541 // Check that updating the rectangle would succeed.
542 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
543 rect.left += 70.f;
544 rect.right += 70.f;
545 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
546 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
547 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400548
Lei Zhanga21d5932018-02-05 18:28:38 +0000549 // Check that updating the rectangle changes the square annotation's
550 // position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000551 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
552 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
553 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000554 }
Jane Liub370e5a2017-08-16 13:24:58 -0400555
Jane Liu06462752017-06-27 16:41:14 -0400556 UnloadPage(page);
557}
Jane Liu8ce58f52017-06-29 13:40:22 -0400558
Henrique Nakashima5098b252018-03-26 21:46:00 +0000559TEST_F(FPDFAnnotEmbeddertest, CountAttachmentPoints) {
560 // Open a file with multiline markup annotations.
561 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
562 FPDF_PAGE page = LoadPage(0);
563 ASSERT_TRUE(page);
564 {
565 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
566 FPDFPage_GetAnnot(page, 0));
567 ASSERT_TRUE(annot);
568
569 // This is a three line annotation.
570 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
571 }
572 UnloadPage(page);
573
574 // null annotation should return 0
575 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
576}
577
Jane Liu8ce58f52017-06-29 13:40:22 -0400578TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
579 // Open a file with 3 annotations on its first page.
580 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000581 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
582 // the FORM API calls in LoadPage() makes this test fail. So use
583 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu8ce58f52017-06-29 13:40:22 -0400584 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
585 ASSERT_TRUE(page);
586 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
587
Jane Liu0c6b07d2017-08-15 10:50:22 -0400588 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400589
Lei Zhanga21d5932018-02-05 18:28:38 +0000590 // Check that the annotations have the expected rectangle coordinates.
591 {
592 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
593 FPDFPage_GetAnnot(page, 0));
594 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
595 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
596 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400597
Lei Zhanga21d5932018-02-05 18:28:38 +0000598 {
599 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
600 FPDFPage_GetAnnot(page, 1));
601 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
602 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
603 }
604
605 {
606 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
607 FPDFPage_GetAnnot(page, 2));
608 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
609 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
610 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400611
612 // Check that nothing happens when attempting to remove an annotation with an
613 // out-of-bound index.
614 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
615 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
616 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
617
618 // Remove the second annotation.
619 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
620 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
621 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
622
623 // Save the document, closing the page and document.
624 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
625 FPDF_ClosePage(page);
626
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400627 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400628 // Open the saved document.
629 std::string new_file = GetString();
630 FPDF_FILEACCESS file_access;
631 memset(&file_access, 0, sizeof(file_access));
632 file_access.m_FileLen = new_file.size();
633 file_access.m_GetBlock = GetBlockFromString;
634 file_access.m_Param = &new_file;
635 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
636 ASSERT_TRUE(new_doc);
637 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
638 ASSERT_TRUE(new_page);
639
640 // Check that the saved document has 2 annotations on the first page.
641 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
642
Lei Zhanga21d5932018-02-05 18:28:38 +0000643 // Check that the remaining 2 annotations are the original 1st and 3rd ones
644 // by verifying their rectangle coordinates.
645 {
646 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
647 FPDFPage_GetAnnot(new_page, 0));
648 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
649 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
650 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400651
Lei Zhanga21d5932018-02-05 18:28:38 +0000652 {
653 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
654 FPDFPage_GetAnnot(new_page, 1));
655 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
656 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
657 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400658 FPDF_ClosePage(new_page);
659 FPDF_CloseDocument(new_doc);
660}
Jane Liu8ce58f52017-06-29 13:40:22 -0400661
Jane Liubaa7ff42017-06-29 19:18:23 -0400662TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400663#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400664 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000665 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
666 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
667 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Jane Liubaa7ff42017-06-29 19:18:23 -0400668#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000669 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000670 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
671 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
672 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400673#endif
674
675 // Open a file with two annotations and load its first page.
676 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000677 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400678 ASSERT_TRUE(page);
679 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
680
681 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000682 {
683 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
684 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
685 CompareBitmap(bitmap.get(), 595, 842, md5_original);
686 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400687
Lei Zhanga21d5932018-02-05 18:28:38 +0000688 {
689 // Retrieve the stamp annotation which has its AP stream already defined.
690 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
691 FPDFPage_GetAnnot(page, 0));
692 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400693
Lei Zhanga21d5932018-02-05 18:28:38 +0000694 // Check that this annotation has one path object and retrieve it.
695 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000696 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000697 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
698 EXPECT_FALSE(path);
699 path = FPDFAnnot_GetObject(annot.get(), 0);
700 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
701 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400702
Lei Zhanga21d5932018-02-05 18:28:38 +0000703 // Modify the color of the path object.
704 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
705 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400706
Lei Zhanga21d5932018-02-05 18:28:38 +0000707 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000708 {
709 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
710 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
711 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
712 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400713
Lei Zhanga21d5932018-02-05 18:28:38 +0000714 // Add a second path object to the same annotation.
715 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
716 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
717 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
718 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
719 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
720 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
721 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400722
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000723 // The object is in the annontation, not in the page, so the page object
724 // array should not change.
725 ASSERT_EQ(32, FPDFPage_CountObjects(page));
726
Lei Zhanga21d5932018-02-05 18:28:38 +0000727 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000728 {
729 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
730 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
731 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
732 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400733
Lei Zhanga21d5932018-02-05 18:28:38 +0000734 // Delete the newly added path object.
735 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
736 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000737 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000738 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400739
740 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000741 {
742 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
743 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
744 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
745 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400746
Jane Liubaa7ff42017-06-29 19:18:23 -0400747 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400748
Lei Zhanga21d5932018-02-05 18:28:38 +0000749 {
750 // Create another stamp annotation and set its annotation rectangle.
751 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
752 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
753 ASSERT_TRUE(annot);
754 rect.left = 200.f;
755 rect.bottom = 400.f;
756 rect.right = 500.f;
757 rect.top = 600.f;
758 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400759
Lei Zhanga21d5932018-02-05 18:28:38 +0000760 // Add a new path to the annotation.
761 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
762 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
763 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
764 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
765 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
766 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
767 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
768 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
769 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
770 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
771
772 // Check that the annotation's bounding box came from its rectangle.
773 FS_RECTF new_rect;
774 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
775 EXPECT_EQ(rect.left, new_rect.left);
776 EXPECT_EQ(rect.bottom, new_rect.bottom);
777 EXPECT_EQ(rect.right, new_rect.right);
778 EXPECT_EQ(rect.top, new_rect.top);
779 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400780
781 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400782 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000783 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400784
785 // Open the saved document.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400786 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000787 page = LoadSavedPage(0);
788 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400789
Jane Liu36567742017-07-06 11:13:35 -0400790 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000791 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400792
Lei Zhanga21d5932018-02-05 18:28:38 +0000793 {
794 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
795 FPDFPage_GetAnnot(page, 2));
796 ASSERT_TRUE(annot);
797 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400798
Lei Zhanga21d5932018-02-05 18:28:38 +0000799 // Check that the new annotation's rectangle is as defined.
800 FS_RECTF new_rect;
801 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
802 EXPECT_EQ(rect.left, new_rect.left);
803 EXPECT_EQ(rect.bottom, new_rect.bottom);
804 EXPECT_EQ(rect.right, new_rect.right);
805 EXPECT_EQ(rect.top, new_rect.top);
806 }
807
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000808 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400809 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400810}
Jane Liub137e752017-07-05 15:04:33 -0400811
812TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
813 // Open a file with an annotation and load its first page.
814 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000815 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400816 ASSERT_TRUE(page);
817
818 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000819 {
820 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
821 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
822 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
823 }
Jane Liub137e752017-07-05 15:04:33 -0400824
Lei Zhanga21d5932018-02-05 18:28:38 +0000825 {
826 // Retrieve the annotation.
827 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
828 FPDFPage_GetAnnot(page, 0));
829 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400830
Lei Zhanga21d5932018-02-05 18:28:38 +0000831 // Check that the original flag values are as expected.
832 int flags = FPDFAnnot_GetFlags(annot.get());
833 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
834 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400835
Lei Zhanga21d5932018-02-05 18:28:38 +0000836 // Set the HIDDEN flag.
837 flags |= FPDF_ANNOT_FLAG_HIDDEN;
838 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
839 flags = FPDFAnnot_GetFlags(annot.get());
840 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
841 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400842
Lei Zhanga21d5932018-02-05 18:28:38 +0000843 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000844 {
845 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
846 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
847 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
848 }
Jane Liub137e752017-07-05 15:04:33 -0400849
Lei Zhanga21d5932018-02-05 18:28:38 +0000850 // Unset the HIDDEN flag.
851 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
852 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
853 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
854 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
855 flags = FPDFAnnot_GetFlags(annot.get());
856 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
857 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400858
Lei Zhanga21d5932018-02-05 18:28:38 +0000859 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000860 {
861 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
862 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
863 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
864 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000865 }
Jane Liub137e752017-07-05 15:04:33 -0400866
Jane Liub137e752017-07-05 15:04:33 -0400867 UnloadPage(page);
868}
Jane Liu36567742017-07-06 11:13:35 -0400869
870TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400871#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400872 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
873 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
874 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Jane Liu36567742017-07-06 11:13:35 -0400875#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000876 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
877 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
878 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400879#endif
880
881 // Open a file with two annotations and load its first page.
882 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000883 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400884 ASSERT_TRUE(page);
885 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
886
887 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000888 {
889 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
890 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
891 CompareBitmap(bitmap.get(), 595, 842, md5_original);
892 }
Jane Liu36567742017-07-06 11:13:35 -0400893
Jane Liu36567742017-07-06 11:13:35 -0400894 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000895 FPDF_BITMAP image_bitmap;
896
897 {
898 // Create a stamp annotation and set its annotation rectangle.
899 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
900 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
901 ASSERT_TRUE(annot);
902 FS_RECTF rect;
903 rect.left = 200.f;
904 rect.bottom = 600.f;
905 rect.right = 400.f;
906 rect.top = 800.f;
907 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
908
909 // Add a solid-color translucent image object to the new annotation.
910 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
911 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
912 0xeeeecccc);
913 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
914 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
915 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
916 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
917 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
918 kBitmapSize, 0, 0));
919 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
920 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
921 }
Jane Liu36567742017-07-06 11:13:35 -0400922
923 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000924 {
925 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
926 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
927 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
928 }
Jane Liu36567742017-07-06 11:13:35 -0400929
Lei Zhanga21d5932018-02-05 18:28:38 +0000930 {
931 // Retrieve the newly added stamp annotation and its image object.
932 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
933 FPDFPage_GetAnnot(page, 2));
934 ASSERT_TRUE(annot);
935 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
936 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
937 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400938
Lei Zhanga21d5932018-02-05 18:28:38 +0000939 // Modify the image in the new annotation.
940 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
941 0xff000000);
942 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
943 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
944 }
Jane Liu36567742017-07-06 11:13:35 -0400945
946 // Save the document, closing the page and document.
947 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000948 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400949 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400950
951 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400952 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400953}
954
955TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400956#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400957 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
958 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
959 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Jane Liu36567742017-07-06 11:13:35 -0400960#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000961 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
962 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
963 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400964#endif
965
966 // Open a file with two annotations and load its first page.
967 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000968 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400969 ASSERT_TRUE(page);
970 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
971
972 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000973 {
974 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
975 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
976 CompareBitmap(bitmap.get(), 595, 842, md5_original);
977 }
Jane Liu36567742017-07-06 11:13:35 -0400978
Lei Zhanga21d5932018-02-05 18:28:38 +0000979 {
980 // Create a stamp annotation and set its annotation rectangle.
981 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
982 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
983 ASSERT_TRUE(annot);
984 FS_RECTF rect;
985 rect.left = 200.f;
986 rect.bottom = 550.f;
987 rect.right = 450.f;
988 rect.top = 650.f;
989 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -0400990
Lei Zhanga21d5932018-02-05 18:28:38 +0000991 // Add a translucent text object to the new annotation.
992 FPDF_PAGEOBJECT text_object =
993 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
994 EXPECT_TRUE(text_object);
995 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
996 GetFPDFWideString(L"I'm a translucent text laying on other text.");
997 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
998 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
999 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1000 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1001 }
Jane Liu36567742017-07-06 11:13:35 -04001002
1003 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001004 {
1005 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
1006 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1007 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
1008 }
Jane Liu36567742017-07-06 11:13:35 -04001009
Lei Zhanga21d5932018-02-05 18:28:38 +00001010 {
1011 // Retrieve the newly added stamp annotation and its text object.
1012 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1013 FPDFPage_GetAnnot(page, 2));
1014 ASSERT_TRUE(annot);
1015 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1016 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1017 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001018
Lei Zhanga21d5932018-02-05 18:28:38 +00001019 // Modify the text in the new annotation.
1020 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
1021 GetFPDFWideString(L"New text!");
1022 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1023 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1024 }
Jane Liu36567742017-07-06 11:13:35 -04001025
1026 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001027 {
1028 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
1029 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1030 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
1031 }
Jane Liu36567742017-07-06 11:13:35 -04001032
1033 // Remove the new annotation, and check that the page renders as before.
1034 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001035 {
1036 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
1037 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1038 CompareBitmap(bitmap.get(), 595, 842, md5_original);
1039 }
Jane Liu36567742017-07-06 11:13:35 -04001040
1041 UnloadPage(page);
1042}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001043
1044TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
1045 // Open a file with four annotations and load its first page.
1046 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001047 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001048 ASSERT_TRUE(page);
1049
Lei Zhangdf064df2017-08-31 02:33:27 -07001050 static constexpr char kDateKey[] = "M";
Lei Zhanga21d5932018-02-05 18:28:38 +00001051 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001052
Lei Zhanga21d5932018-02-05 18:28:38 +00001053 {
1054 // Retrieve the first annotation.
1055 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1056 FPDFPage_GetAnnot(page, 0));
1057 ASSERT_TRUE(annot);
1058
1059 // Check that a non-existent key does not exist.
1060 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1061
1062 // Check that the string value of a non-string dictionary entry is empty.
1063 static constexpr char kApKey[] = "AP";
1064 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), kApKey));
1065 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1066 FPDFAnnot_GetValueType(annot.get(), kApKey));
1067 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kApKey, nullptr, 0));
1068
1069 // Check that the string value of the hash is correct.
1070 static constexpr char kHashKey[] = "AAPL:Hash";
1071 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1072 unsigned long len =
1073 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1074 std::vector<char> buf(len);
1075 EXPECT_EQ(66u,
1076 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1077 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1078 BufferToWString(buf).c_str());
1079
1080 // Check that the string value of the modified date is correct.
1081 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1082 len = FPDFAnnot_GetStringValue(annot.get(), kDateKey, nullptr, 0);
1083 buf.clear();
1084 buf.resize(len);
1085 EXPECT_EQ(44u,
1086 FPDFAnnot_GetStringValue(annot.get(), kDateKey, buf.data(), len));
1087 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1088
1089 // Update the date entry for the annotation.
1090 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1091 GetFPDFWideString(kNewDate);
1092 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kDateKey, text.get()));
1093 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001094
1095 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001096 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001097 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001098
Dan Sinclair698aed72017-09-26 16:24:49 -04001099#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +03001100 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001101#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001102 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001103#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001104
1105 // Open the saved annotation.
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001106 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001107 page = LoadSavedPage(0);
1108 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001109 {
1110 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
1111 FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001112
Lei Zhanga21d5932018-02-05 18:28:38 +00001113 // Check that the string value of the modified date is the newly-set value.
1114 EXPECT_EQ(FPDF_OBJECT_STRING,
1115 FPDFAnnot_GetValueType(new_annot.get(), kDateKey));
1116 unsigned long len =
1117 FPDFAnnot_GetStringValue(new_annot.get(), kDateKey, nullptr, 0);
1118 std::vector<char> buf(len);
1119 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot.get(), kDateKey,
1120 buf.data(), len));
1121 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1122 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001123
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001124 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001125 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001126}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001127
Henrique Nakashima5970a472018-01-11 22:40:59 +00001128TEST_F(FPDFAnnotEmbeddertest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001129 // Open a file with four annotations and load its first page.
1130 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001131 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001132 ASSERT_TRUE(page);
1133
Lei Zhanga21d5932018-02-05 18:28:38 +00001134 {
1135 // Retrieve the first annotation.
1136 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1137 FPDFPage_GetAnnot(page, 0));
1138 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001139
Lei Zhanga21d5932018-02-05 18:28:38 +00001140 // Check that the string value of an AP returns the expected length.
1141 unsigned long normal_len = FPDFAnnot_GetAP(
1142 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1143 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001144
Lei Zhanga21d5932018-02-05 18:28:38 +00001145 // Check that the string value of an AP is not returned if the buffer is too
1146 // small. The result buffer should be overwritten with an empty string.
1147 std::vector<char> buf(normal_len - 1);
1148 // Write L"z" in the buffer to verify it's not overwritten.
1149 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1150 EXPECT_EQ(73970u,
1151 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1152 buf.data(), buf.size()));
1153 std::string ap = BufferToString(buf);
1154 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001155
Lei Zhanga21d5932018-02-05 18:28:38 +00001156 // Check that the string value of an AP is returned through a buffer that is
1157 // the right size.
1158 buf.clear();
1159 buf.resize(normal_len);
1160 EXPECT_EQ(73970u,
1161 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1162 buf.data(), buf.size()));
1163 ap = BufferToString(buf);
1164 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1165 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001166
Lei Zhanga21d5932018-02-05 18:28:38 +00001167 // Check that the string value of an AP is returned through a buffer that is
1168 // larger than necessary.
1169 buf.clear();
1170 buf.resize(normal_len + 1);
1171 EXPECT_EQ(73970u,
1172 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1173 buf.data(), buf.size()));
1174 ap = BufferToString(buf);
1175 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1176 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001177
Lei Zhanga21d5932018-02-05 18:28:38 +00001178 // Check that getting an AP for a mode that does not have an AP returns an
1179 // empty string.
1180 unsigned long rollover_len = FPDFAnnot_GetAP(
1181 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1182 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001183
Lei Zhanga21d5932018-02-05 18:28:38 +00001184 buf.clear();
1185 buf.resize(1000);
1186 EXPECT_EQ(2u,
1187 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1188 buf.data(), buf.size()));
1189 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001190
Lei Zhanga21d5932018-02-05 18:28:38 +00001191 // Check that setting the AP for an invalid appearance mode fails.
1192 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1193 GetFPDFWideString(L"new test ap");
1194 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1195 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1196 apText.get()));
1197 EXPECT_FALSE(FPDFAnnot_SetAP(
1198 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001199
Lei Zhanga21d5932018-02-05 18:28:38 +00001200 // Set the AP correctly now.
1201 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1202 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001203
Lei Zhanga21d5932018-02-05 18:28:38 +00001204 // Check that the new annotation value is equal to the value we just set.
1205 rollover_len = FPDFAnnot_GetAP(
1206 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1207 EXPECT_EQ(24u, rollover_len);
1208 buf.clear();
1209 buf.resize(rollover_len);
1210 EXPECT_EQ(24u,
1211 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1212 buf.data(), buf.size()));
1213 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001214
Lei Zhanga21d5932018-02-05 18:28:38 +00001215 // Check that the Normal AP was not touched when the Rollover AP was set.
1216 buf.clear();
1217 buf.resize(normal_len);
1218 EXPECT_EQ(73970u,
1219 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1220 buf.data(), buf.size()));
1221 ap = BufferToString(buf);
1222 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1223 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1224 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001225
1226 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001227 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001228 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001229
1230 OpenSavedDocument();
1231 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001232 {
1233 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
1234 FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001235
Lei Zhanga21d5932018-02-05 18:28:38 +00001236 // Check that the new annotation value is equal to the value we set before
1237 // saving.
1238 unsigned long rollover_len = FPDFAnnot_GetAP(
1239 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1240 EXPECT_EQ(24u, rollover_len);
1241 std::vector<char> buf(rollover_len);
1242 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1243 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1244 buf.data(), buf.size()));
1245 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1246 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001247
1248 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001249 CloseSavedPage(page);
1250 CloseSavedDocument();
1251}
1252
1253TEST_F(FPDFAnnotEmbeddertest, RemoveOptionalAP) {
1254 // Open a file with four annotations and load its first page.
1255 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001256 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001257 ASSERT_TRUE(page);
1258
Lei Zhanga21d5932018-02-05 18:28:38 +00001259 {
1260 // Retrieve the first annotation.
1261 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1262 FPDFPage_GetAnnot(page, 0));
1263 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001264
Lei Zhanga21d5932018-02-05 18:28:38 +00001265 // Set Down AP. Normal AP is already set.
1266 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1267 GetFPDFWideString(L"new test ap");
1268 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1269 apText.get()));
1270 EXPECT_EQ(73970u,
1271 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1272 nullptr, 0));
1273 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1274 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001275
Lei Zhanga21d5932018-02-05 18:28:38 +00001276 // Check that setting the Down AP to null removes the Down entry but keeps
1277 // Normal intact.
1278 EXPECT_TRUE(
1279 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1280 EXPECT_EQ(73970u,
1281 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1282 nullptr, 0));
1283 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1284 nullptr, 0));
1285 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001286
Lei Zhang75c81712018-02-08 17:22:39 +00001287 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001288}
1289
1290TEST_F(FPDFAnnotEmbeddertest, RemoveRequiredAP) {
1291 // Open a file with four annotations and load its first page.
1292 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001293 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001294 ASSERT_TRUE(page);
1295
Lei Zhanga21d5932018-02-05 18:28:38 +00001296 {
1297 // Retrieve the first annotation.
1298 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1299 FPDFPage_GetAnnot(page, 0));
1300 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001301
Lei Zhanga21d5932018-02-05 18:28:38 +00001302 // Set Down AP. Normal AP is already set.
1303 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1304 GetFPDFWideString(L"new test ap");
1305 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1306 apText.get()));
1307 EXPECT_EQ(73970u,
1308 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1309 nullptr, 0));
1310 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1311 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001312
Lei Zhanga21d5932018-02-05 18:28:38 +00001313 // Check that setting the Normal AP to null removes the whole AP dictionary.
1314 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1315 nullptr));
1316 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1317 nullptr, 0));
1318 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1319 nullptr, 0));
1320 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001321
Lei Zhang75c81712018-02-08 17:22:39 +00001322 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001323}
1324
Jane Liu300bb272017-08-21 14:37:53 -04001325TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) {
1326 // Open a file with annotations and load its first page.
1327 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001328 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001329 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001330 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001331
Lei Zhanga21d5932018-02-05 18:28:38 +00001332 {
1333 // Retrieve the highlight annotation which has its popup defined.
1334 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1335 FPDFPage_GetAnnot(page, 0));
1336 ASSERT_TRUE(annot);
1337 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1338 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1339 static constexpr char kPopupKey[] = "Popup";
1340 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1341 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1342 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001343
Lei Zhanga21d5932018-02-05 18:28:38 +00001344 // Retrieve and verify the popup of the highlight annotation.
1345 std::unique_ptr<void, FPDFAnnotationDeleter> popup(
1346 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1347 ASSERT_TRUE(popup);
1348 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1349 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1350 FS_RECTF rect;
1351 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1352 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1353 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001354
Lei Zhanga21d5932018-02-05 18:28:38 +00001355 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1356 // since "IRT" is not a key in |annot|'s dictionary.
1357 static constexpr char kIRTKey[] = "IRT";
1358 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1359 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001360
Lei Zhanga21d5932018-02-05 18:28:38 +00001361 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1362 // would fail, since its parent is not an annotation.
1363 static constexpr char kPKey[] = "P";
1364 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPKey));
1365 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1366 FPDFAnnot_GetValueType(annot.get(), kPKey));
1367 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kPKey));
1368 }
Jane Liu300bb272017-08-21 14:37:53 -04001369
Jane Liu300bb272017-08-21 14:37:53 -04001370 UnloadPage(page);
1371}
1372
Diana Gage7e0c05d2017-07-19 17:33:33 -07001373TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
1374 // Open file with form text fields.
1375 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001376 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001377 ASSERT_TRUE(page);
1378
Lei Zhanga21d5932018-02-05 18:28:38 +00001379 {
1380 // Retrieve the first annotation: user-editable text field.
1381 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1382 FPDFPage_GetAnnot(page, 0));
1383 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001384
Lei Zhanga21d5932018-02-05 18:28:38 +00001385 // Check that the flag values are as expected.
1386 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1387 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1388 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001389
Lei Zhanga21d5932018-02-05 18:28:38 +00001390 {
1391 // Retrieve the second annotation: read-only text field.
1392 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1393 FPDFPage_GetAnnot(page, 1));
1394 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001395
Lei Zhanga21d5932018-02-05 18:28:38 +00001396 // Check that the flag values are as expected.
1397 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1398 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1399 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001400
1401 UnloadPage(page);
1402}
1403
1404TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
1405 // Open file with form text fields.
1406 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001407 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001408 ASSERT_TRUE(page);
1409
Lei Zhanga21d5932018-02-05 18:28:38 +00001410 {
1411 // Retrieve the first annotation: user-editable combobox.
1412 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1413 FPDFPage_GetAnnot(page, 0));
1414 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001415
Lei Zhanga21d5932018-02-05 18:28:38 +00001416 // Check that the flag values are as expected.
1417 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1418 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1419 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1420 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1421 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001422
Lei Zhanga21d5932018-02-05 18:28:38 +00001423 {
1424 // Retrieve the second annotation: regular combobox.
1425 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1426 FPDFPage_GetAnnot(page, 1));
1427 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001428
Lei Zhanga21d5932018-02-05 18:28:38 +00001429 // Check that the flag values are as expected.
1430 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1431 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1432 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1433 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1434 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001435
Lei Zhanga21d5932018-02-05 18:28:38 +00001436 {
1437 // Retrieve the third annotation: read-only combobox.
1438 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1439 FPDFPage_GetAnnot(page, 2));
1440 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001441
Lei Zhanga21d5932018-02-05 18:28:38 +00001442 // Check that the flag values are as expected.
1443 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1444 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1445 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1446 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1447 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001448
1449 UnloadPage(page);
1450}
Diana Gage40870db2017-07-19 18:16:03 -07001451
1452TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
1453 // Open file with form text fields.
1454 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1455 FPDF_PAGE page = LoadPage(0);
1456 ASSERT_TRUE(page);
1457
1458 // Attempt to get an annotation where no annotation exists on page.
1459 FPDF_ANNOTATION annot =
1460 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0);
1461 EXPECT_FALSE(annot);
1462
1463 UnloadPage(page);
1464}
1465
1466TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
1467 // Open file with form text fields.
1468 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1469 FPDF_PAGE page = LoadPage(0);
1470 ASSERT_TRUE(page);
1471
Lei Zhanga21d5932018-02-05 18:28:38 +00001472 {
1473 // Retrieve user-editable text field annotation.
1474 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1475 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1476 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001477
Lei Zhanga21d5932018-02-05 18:28:38 +00001478 // Check that interactive form annotation flag values are as expected.
1479 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1480 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1481 }
Diana Gage40870db2017-07-19 18:16:03 -07001482
Lei Zhanga21d5932018-02-05 18:28:38 +00001483 {
1484 // Retrieve read-only text field annotation.
1485 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1486 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1487 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001488
Lei Zhanga21d5932018-02-05 18:28:38 +00001489 // Check that interactive form annotation flag values are as expected.
1490 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1491 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1492 }
Diana Gage40870db2017-07-19 18:16:03 -07001493
1494 UnloadPage(page);
1495}
1496
1497TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1498 // Open file with form comboboxes.
1499 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1500 FPDF_PAGE page = LoadPage(0);
1501 ASSERT_TRUE(page);
1502
Lei Zhanga21d5932018-02-05 18:28:38 +00001503 {
1504 // Retrieve user-editable combobox annotation.
1505 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001506 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001507 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001508
Lei Zhanga21d5932018-02-05 18:28:38 +00001509 // Check that interactive form annotation flag values are as expected.
1510 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1511 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1512 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1513 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1514 }
Diana Gage40870db2017-07-19 18:16:03 -07001515
Lei Zhanga21d5932018-02-05 18:28:38 +00001516 {
1517 // Retrieve regular combobox annotation.
1518 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001519 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001520 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001521
Lei Zhanga21d5932018-02-05 18:28:38 +00001522 // Check that interactive form annotation flag values are as expected.
1523 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1524 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1525 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1526 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1527 }
Diana Gage40870db2017-07-19 18:16:03 -07001528
Lei Zhanga21d5932018-02-05 18:28:38 +00001529 {
1530 // Retrieve read-only combobox annotation.
1531 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001532 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001533 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001534
Lei Zhanga21d5932018-02-05 18:28:38 +00001535 // Check that interactive form annotation flag values are as expected.
1536 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1537 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1538 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1539 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1540 }
Diana Gage40870db2017-07-19 18:16:03 -07001541
1542 UnloadPage(page);
1543}