blob: 1e45eb1d4c26887195bb1586296150dfd713c6db [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
Tom Sepez204ab052020-06-12 21:33:48 +00005#include "public/fpdf_annot.h"
6
7#include <limits.h>
8
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00009#include <algorithm>
Jane Liu20eafda2017-06-07 10:33:24 -040010#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -040011#include <vector>
12
Lei Zhange4cdac52019-04-30 16:45:57 +000013#include "build/build_config.h"
Lei Zhanga5c1daf2019-01-31 21:56:47 +000014#include "constants/annotation_common.h"
Tom Sepez204ab052020-06-12 21:33:48 +000015#include "core/fpdfapi/page/cpdf_annotcontext.h"
Tom Sepez204ab052020-06-12 21:33:48 +000016#include "core/fpdfapi/parser/cpdf_array.h"
17#include "core/fpdfapi/parser/cpdf_dictionary.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040018#include "core/fxcrt/fx_system.h"
Tom Sepez204ab052020-06-12 21:33:48 +000019#include "fpdfsdk/cpdfsdk_helpers.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000020#include "public/cpp/fpdf_scopers.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040021#include "public/fpdf_edit.h"
Mansi Awasthi07bf7e62020-01-24 10:34:17 +000022#include "public/fpdf_formfill.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040023#include "public/fpdfview.h"
24#include "testing/embedder_test.h"
Hui Yingstb4baceb2020-04-28 23:46:10 +000025#include "testing/embedder_test_constants.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000026#include "testing/fx_string_testhelpers.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000027#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040028#include "testing/gtest/include/gtest/gtest.h"
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000029#include "testing/utils/hash.h"
Lei Zhang87e3d7a2021-06-17 19:40:28 +000030#include "third_party/base/containers/contains.h"
Lei Zhang9b444002020-04-17 17:35:23 +000031#include "third_party/base/span.h"
32
Hui Yingst30bfcc52020-07-27 23:54:40 +000033using pdfium::kAnnotationStampWithApChecksum;
34
Lei Zhang9b444002020-04-17 17:35:23 +000035namespace {
36
Tom Sepez204ab052020-06-12 21:33:48 +000037const wchar_t kStreamData[] =
38 L"/GS gs 0.0 0.0 0.0 RG 4 w 211.8 747.6 m 211.8 744.8 "
39 L"212.6 743.0 214.2 740.8 "
40 L"c 215.4 739.0 216.8 737.1 218.9 736.1 c 220.8 735.1 221.4 733.0 "
41 L"223.7 732.4 c 232.6 729.9 242.0 730.8 251.2 730.8 c 257.5 730.8 "
42 L"263.0 732.9 269.0 734.4 c S";
43
Lei Zhang9b444002020-04-17 17:35:23 +000044void VerifyFocusableAnnotSubtypes(
45 FPDF_FORMHANDLE form_handle,
46 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) {
47 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
48 FPDFAnnot_GetFocusableSubtypesCount(form_handle));
49
50 std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes(
51 expected_subtypes.size());
52 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(
53 form_handle, actual_subtypes.data(), actual_subtypes.size()));
54 for (size_t i = 0; i < expected_subtypes.size(); ++i)
55 ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]);
56}
57
58void SetAndVerifyFocusableAnnotSubtypes(
59 FPDF_FORMHANDLE form_handle,
60 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) {
61 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(),
62 subtypes.size()));
63 VerifyFocusableAnnotSubtypes(form_handle, subtypes);
64}
65
66void VerifyAnnotationSubtypesAndFocusability(
67 FPDF_FORMHANDLE form_handle,
68 FPDF_PAGE page,
69 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes,
70 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) {
71 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
72 FPDFPage_GetAnnotCount(page));
73 for (size_t i = 0; i < expected_subtypes.size(); ++i) {
74 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
75 ASSERT_TRUE(annot);
76 EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get()));
77
Lei Zhangf245ae62020-05-15 21:49:46 +000078 bool expected_focusable =
79 pdfium::Contains(expected_focusable_subtypes, expected_subtypes[i]);
Lei Zhang9b444002020-04-17 17:35:23 +000080 EXPECT_EQ(expected_focusable,
81 FORM_SetFocusedAnnot(form_handle, annot.get()));
82
83 // Kill the focus so the next test starts in an unfocused state.
84 FORM_ForceToKillFocus(form_handle);
85 }
86}
87
Lei Zhang306874b2021-04-16 00:33:36 +000088void VerifyUriActionInLink(FPDF_DOCUMENT doc,
89 FPDF_LINK link,
90 const std::string& expected_uri) {
91 ASSERT_TRUE(link);
92
93 FPDF_ACTION action = FPDFLink_GetAction(link);
94 ASSERT_TRUE(action);
95 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
96 FPDFAction_GetType(action));
97
98 unsigned long bufsize = FPDFAction_GetURIPath(doc, action, nullptr, 0);
99 ASSERT_EQ(expected_uri.size() + 1, bufsize);
100
101 std::vector<char> buffer(bufsize);
102 EXPECT_EQ(bufsize,
103 FPDFAction_GetURIPath(doc, action, buffer.data(), bufsize));
104 EXPECT_STREQ(expected_uri.c_str(), buffer.data());
105}
106
Lei Zhang9b444002020-04-17 17:35:23 +0000107} // namespace
Jane Liu4fd9a472017-06-01 18:56:09 -0400108
Lei Zhangab41f252018-12-23 03:10:50 +0000109class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -0400110
Tom Sepez204ab052020-06-12 21:33:48 +0000111TEST_F(FPDFAnnotEmbedderTest, SetAP) {
112 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
113 ASSERT_TRUE(doc);
114 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
115 ASSERT_TRUE(page);
116 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
117 ASSERT_TRUE(ap_stream);
118
119 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
120 ASSERT_TRUE(annot);
121
122 // Negative case: FPDFAnnot_SetAP() should fail if bounding rect is not yet
123 // set on the annotation.
124 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
125 ap_stream.get()));
126
127 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
128 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
129
130 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
131 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/255));
132
133 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
134 ap_stream.get()));
135
136 // Verify that appearance stream is created as form XObject
137 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
138 ASSERT_TRUE(context);
139 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
140 ASSERT_TRUE(annot_dict);
141 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
142 ASSERT_TRUE(ap_dict);
143 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
144 ASSERT_TRUE(stream_dict);
145 // Check for non-existence of resources dictionary in case of opaque color
146 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
147 ASSERT_FALSE(resources_dict);
148 ByteString type = stream_dict->GetStringFor(pdfium::annotation::kType);
149 EXPECT_EQ("XObject", type);
150 ByteString sub_type = stream_dict->GetStringFor(pdfium::annotation::kSubtype);
151 EXPECT_EQ("Form", sub_type);
152
153 // Check that the appearance stream is same as we just set.
Lei Zhang3111d402022-04-18 22:24:07 +0000154 const uint32_t kStreamDataSize = std::size(kStreamData) * sizeof(FPDF_WCHAR);
Tom Sepez204ab052020-06-12 21:33:48 +0000155 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
156 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
157 ASSERT_EQ(kStreamDataSize, normal_length_bytes);
158 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
159 EXPECT_EQ(kStreamDataSize,
160 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
161 buf.data(), normal_length_bytes));
162 EXPECT_EQ(kStreamData, GetPlatformWString(buf.data()));
163}
164
165TEST_F(FPDFAnnotEmbedderTest, SetAPWithOpacity) {
166 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
167 ASSERT_TRUE(doc);
168 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
169 ASSERT_TRUE(page);
170 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
171 ASSERT_TRUE(ap_stream);
172
173 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
174 ASSERT_TRUE(annot);
175
176 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
177 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/102));
178
179 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
180 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
181
182 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
183 ap_stream.get()));
184
185 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
186 ASSERT_TRUE(context);
187 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
188 ASSERT_TRUE(annot_dict);
189 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
190 ASSERT_TRUE(ap_dict);
191 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
192 ASSERT_TRUE(stream_dict);
193 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
194 ASSERT_TRUE(stream_dict);
195 CPDF_Dictionary* extGState_dict = resources_dict->GetDictFor("ExtGState");
196 ASSERT_TRUE(extGState_dict);
197 CPDF_Dictionary* gs_dict = extGState_dict->GetDictFor("GS");
198 ASSERT_TRUE(gs_dict);
199 ByteString type = gs_dict->GetStringFor(pdfium::annotation::kType);
200 EXPECT_EQ("ExtGState", type);
201 float opacity = gs_dict->GetNumberFor("CA");
202 // Opacity value of 102 is represented as 0.4f (=104/255) in /CA entry.
203 EXPECT_FLOAT_EQ(0.4f, opacity);
204 ByteString blend_mode = gs_dict->GetStringFor("BM");
205 EXPECT_EQ("Normal", blend_mode);
206 bool alpha_source_flag = gs_dict->GetBooleanFor("AIS", true);
207 EXPECT_FALSE(alpha_source_flag);
208}
209
210TEST_F(FPDFAnnotEmbedderTest, InkListAPIValidations) {
211 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
212 ASSERT_TRUE(doc);
213 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
214 ASSERT_TRUE(page);
215
216 // Create a new ink annotation.
217 ScopedFPDFAnnotation ink_annot(
218 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
219 ASSERT_TRUE(ink_annot);
220 CPDF_AnnotContext* context =
221 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
222 ASSERT_TRUE(context);
223 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
224 ASSERT_TRUE(annot_dict);
225
226 static constexpr FS_POINTF kFirstInkStroke[] = {
227 {80.0f, 90.0f}, {81.0f, 91.0f}, {82.0f, 92.0f},
228 {83.0f, 93.0f}, {84.0f, 94.0f}, {85.0f, 95.0f}};
Lei Zhang3111d402022-04-18 22:24:07 +0000229 static constexpr size_t kFirstStrokePointCount = std::size(kFirstInkStroke);
Tom Sepez204ab052020-06-12 21:33:48 +0000230
231 static constexpr FS_POINTF kSecondInkStroke[] = {
232 {70.0f, 90.0f}, {71.0f, 91.0f}, {72.0f, 92.0f}};
Lei Zhang3111d402022-04-18 22:24:07 +0000233 static constexpr size_t kSecondStrokePointCount = std::size(kSecondInkStroke);
Tom Sepez204ab052020-06-12 21:33:48 +0000234
235 static constexpr FS_POINTF kThirdInkStroke[] = {{60.0f, 90.0f},
236 {61.0f, 91.0f},
237 {62.0f, 92.0f},
238 {63.0f, 93.0f},
239 {64.0f, 94.0f}};
Lei Zhang3111d402022-04-18 22:24:07 +0000240 static constexpr size_t kThirdStrokePointCount = std::size(kThirdInkStroke);
Tom Sepez204ab052020-06-12 21:33:48 +0000241
242 // Negative test: |annot| is passed as nullptr.
243 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(nullptr, kFirstInkStroke,
244 kFirstStrokePointCount));
245
246 // Negative test: |annot| is not ink annotation.
247 // Create a new highlight annotation.
248 ScopedFPDFAnnotation highlight_annot(
249 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
250 ASSERT_TRUE(highlight_annot);
251 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(highlight_annot.get(), kFirstInkStroke,
252 kFirstStrokePointCount));
253
254 // Negative test: passing |point_count| as 0.
255 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke, 0));
256
257 // Negative test: passing |points| array as nullptr.
258 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), nullptr,
259 kFirstStrokePointCount));
260
261 // Negative test: passing |point_count| more than ULONG_MAX/2.
262 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
263 ULONG_MAX / 2 + 1));
264
265 // InkStroke should get added to ink annotation. Also inklist should get
266 // created.
267 EXPECT_EQ(0, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke,
268 kFirstStrokePointCount));
269
270 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
271 ASSERT_TRUE(inklist);
272 EXPECT_EQ(1u, inklist->size());
273 EXPECT_EQ(kFirstStrokePointCount * 2, inklist->GetArrayAt(0)->size());
274
275 // Adding another inkStroke to ink annotation with all valid paremeters.
276 // InkList already exists in ink_annot.
277 EXPECT_EQ(1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
278 kSecondStrokePointCount));
279 EXPECT_EQ(2u, inklist->size());
280 EXPECT_EQ(kSecondStrokePointCount * 2, inklist->GetArrayAt(1)->size());
281
282 // Adding one more InkStroke to the ink annotation. |point_count| passed is
283 // less than the data available in |buffer|.
284 EXPECT_EQ(2, FPDFAnnot_AddInkStroke(ink_annot.get(), kThirdInkStroke,
285 kThirdStrokePointCount - 1));
286 EXPECT_EQ(3u, inklist->size());
287 EXPECT_EQ((kThirdStrokePointCount - 1) * 2, inklist->GetArrayAt(2)->size());
288}
289
290TEST_F(FPDFAnnotEmbedderTest, RemoveInkList) {
291 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
292 ASSERT_TRUE(doc);
293 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
294 ASSERT_TRUE(page);
295
296 // Negative test: |annot| is passed as nullptr.
297 EXPECT_FALSE(FPDFAnnot_RemoveInkList(nullptr));
298
299 // Negative test: |annot| is not ink annotation.
300 // Create a new highlight annotation.
301 ScopedFPDFAnnotation highlight_annot(
302 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
303 ASSERT_TRUE(highlight_annot);
304 EXPECT_FALSE(FPDFAnnot_RemoveInkList(highlight_annot.get()));
305
306 // Create a new ink annotation.
307 ScopedFPDFAnnotation ink_annot(
308 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
309 ASSERT_TRUE(ink_annot);
310 CPDF_AnnotContext* context =
311 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
312 ASSERT_TRUE(context);
313 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
314 ASSERT_TRUE(annot_dict);
315
316 static constexpr FS_POINTF kInkStroke[] = {{80.0f, 90.0f}, {81.0f, 91.0f},
317 {82.0f, 92.0f}, {83.0f, 93.0f},
318 {84.0f, 94.0f}, {85.0f, 95.0f}};
Lei Zhang3111d402022-04-18 22:24:07 +0000319 static constexpr size_t kPointCount = std::size(kInkStroke);
Tom Sepez204ab052020-06-12 21:33:48 +0000320
321 // InkStroke should get added to ink annotation. Also inklist should get
322 // created.
323 EXPECT_EQ(0,
324 FPDFAnnot_AddInkStroke(ink_annot.get(), kInkStroke, kPointCount));
325
326 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
327 ASSERT_TRUE(inklist);
328 ASSERT_EQ(1u, inklist->size());
329 EXPECT_EQ(kPointCount * 2, inklist->GetArrayAt(0)->size());
330
331 // Remove inklist.
332 EXPECT_TRUE(FPDFAnnot_RemoveInkList(ink_annot.get()));
333 EXPECT_FALSE(annot_dict->KeyExist("InkList"));
334}
335
Lei Zhangab41f252018-12-23 03:10:50 +0000336TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +0000337 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
338 FPDF_PAGE page = LoadPage(0);
339 ASSERT_TRUE(page);
340
341 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
342
343 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
344 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
345 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
346 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
347 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
348
349 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
350
351 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
352
353 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
354 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
355 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
356
357 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
358
Lei Zhang4f556b82019-04-08 16:32:41 +0000359 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +0000360 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +0000361 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
362
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000363 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +0000364 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
365 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
366 EXPECT_EQ(0u,
367 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
368
369 UnloadPage(page);
370}
371
Lei Zhang3d9a0972019-03-04 19:34:09 +0000372TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
373 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
374 FPDF_PAGE page = LoadPage(0);
375 ASSERT_TRUE(page);
376
377 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +0000378 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +0000379
380 UnloadPage(page);
381}
382
Lei Zhangab41f252018-12-23 03:10:50 +0000383TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -0400384 // Open a file with one annotation and load its first page.
385 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000386 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -0400387 ASSERT_TRUE(page);
388
389 // This annotation has a malformed appearance stream, which does not have its
390 // normal appearance defined, only its rollover appearance. In this case, its
391 // normal appearance should be generated, allowing the highlight annotation to
392 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000393 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000394 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -0400395
396 UnloadPage(page);
397}
398
Hui Yingstd5b6f632020-07-22 01:31:59 +0000399TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang03e5e682019-09-16 19:45:55 +0000400#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstd5b6f632020-07-22 01:31:59 +0000401 static const char kChecksum[] = "ec1f4ccbd0aecfdea6d53893387a0101";
Lei Zhang03e5e682019-09-16 19:45:55 +0000402#else
Hui Yingstd5b6f632020-07-22 01:31:59 +0000403 static const char kChecksum[] = "76512832d88017668d9acc7aacd13dae";
Lei Zhang03e5e682019-09-16 19:45:55 +0000404#endif
Henrique Nakashima5098b252018-03-26 21:46:00 +0000405 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000406 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
407 FPDF_PAGE page = LoadPage(0);
408 ASSERT_TRUE(page);
409
Tom Sepeze08d2b12018-04-25 18:49:32 +0000410 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstd5b6f632020-07-22 01:31:59 +0000411 CompareBitmap(bitmap.get(), 595, 842, kChecksum);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000412
413 UnloadPage(page);
414}
415
Lei Zhangab41f252018-12-23 03:10:50 +0000416TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400417 // Open a file with one annotation and load its first page.
418 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000419 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400420 ASSERT_TRUE(page);
421
422 // Check that there is a total of 1 annotation on its first page.
423 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
424
425 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000426 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000427 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000428 ASSERT_TRUE(annot);
429 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400430
Lei Zhanga21d5932018-02-05 18:28:38 +0000431 // Check that the annotation color is yellow.
432 unsigned int R;
433 unsigned int G;
434 unsigned int B;
435 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000436 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000437 &G, &B, &A));
438 EXPECT_EQ(255u, R);
439 EXPECT_EQ(255u, G);
440 EXPECT_EQ(0u, B);
441 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400442
Lei Zhanga21d5932018-02-05 18:28:38 +0000443 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000444 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000445 EXPECT_EQ(FPDF_OBJECT_STRING,
446 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000447 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000448 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000449 ASSERT_EQ(28u, length_bytes);
450 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000451 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000452 length_bytes));
453 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400454
Lei Zhanga21d5932018-02-05 18:28:38 +0000455 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000456 EXPECT_EQ(
457 FPDF_OBJECT_STRING,
458 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000459 length_bytes = FPDFAnnot_GetStringValue(
460 annot.get(), pdfium::annotation::kContents, nullptr, 0);
461 ASSERT_EQ(2690u, length_bytes);
462 buf = GetFPDFWideStringBuffer(length_bytes);
463 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
464 pdfium::annotation::kContents,
465 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000466 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000467 L"This is a note for that highlight annotation. Very long highlight "
468 "annotation. Long long long Long long longLong long longLong long "
469 "longLong long longLong long longLong long longLong long longLong long "
470 "longLong long longLong long longLong long longLong long longLong long "
471 "longLong long longLong long longLong long longLong long longLong long "
472 "longLong long longLong long longLong long longLong long longLong long "
473 "longLong long longLong long longLong long longLong long longLong long "
474 "longLong long longLong long longLong long longLong long longLong long "
475 "longLong long longLong long longLong long longLong long longLong long "
476 "longLong long longLong long longLong long longLong long longLong long "
477 "longLong long longLong long longLong long longLong long longLong long "
478 "longLong long longLong long longLong long longLong long longLong long "
479 "longLong long longLong long longLong long longLong long longLong long "
480 "longLong long longLong long longLong long longLong long longLong long "
481 "longLong long longLong long longLong long longLong long longLong long "
482 "longLong long longLong long longLong long longLong long longLong long "
483 "longLong long longLong long longLong long longLong long longLong long "
484 "longLong long longLong long longLong long longLong long longLong long "
485 "longLong long longLong long longLong long longLong long longLong long "
486 "longLong long long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000487 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400488
Lei Zhanga21d5932018-02-05 18:28:38 +0000489 // Check that the quadpoints are correct.
490 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000491 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000492 EXPECT_EQ(115.802643f, quadpoints.x1);
493 EXPECT_EQ(718.913940f, quadpoints.y1);
494 EXPECT_EQ(157.211182f, quadpoints.x4);
495 EXPECT_EQ(706.264465f, quadpoints.y4);
496 }
Tom Sepez507d0192018-11-07 16:37:51 +0000497 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400498}
499
Hui Yingst9b6b1542020-07-27 16:11:12 +0000500TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400501 // Open a file with three annotations and load its first page.
502 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000503 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400504 ASSERT_TRUE(page);
505
506 // Check that there is a total of 3 annotation on its first page.
507 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
508
Lei Zhanga21d5932018-02-05 18:28:38 +0000509 {
510 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000511 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000512 ASSERT_TRUE(annot);
513 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400514
Lei Zhanga21d5932018-02-05 18:28:38 +0000515 // Check that the annotation color is blue with opacity.
516 unsigned int R;
517 unsigned int G;
518 unsigned int B;
519 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000520 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000521 &G, &B, &A));
522 EXPECT_EQ(0u, R);
523 EXPECT_EQ(0u, G);
524 EXPECT_EQ(255u, B);
525 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400526
Lei Zhanga21d5932018-02-05 18:28:38 +0000527 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000528 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
529 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400530
Lei Zhang4f556b82019-04-08 16:32:41 +0000531 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000532 // Note that upon rendering, the rectangle coordinates will be adjusted.
533 FS_RECTF rect;
534 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
535 EXPECT_EQ(351.820404f, rect.left);
536 EXPECT_EQ(583.830688f, rect.bottom);
537 EXPECT_EQ(475.336090f, rect.right);
538 EXPECT_EQ(681.535034f, rect.top);
539 }
Tom Sepezef43c262018-11-07 16:41:32 +0000540 {
Hui Yingste1215fc2022-03-26 22:29:08 +0000541#if defined(_SKIA_SUPPORT_)
Hui Yingst57daee82020-10-09 05:47:20 +0000542 static constexpr char kExpectedHash[] = "fad91b9c968fe8019a774f5e2419b8fc";
Hui Yingste1215fc2022-03-26 22:29:08 +0000543#elif defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000544 static constexpr char kExpectedHash[] = "acddfe688a117ead56af7b249a2cf8a1";
Lei Zhangf997abe2022-01-12 19:14:23 +0000545#elif BUILDFLAG(IS_WIN)
Lei Zhang430b5322020-07-06 22:23:36 +0000546 static constexpr char kExpectedHash[] = "49d0a81c636531a337429325273d0508";
547#else
548 static constexpr char kExpectedHash[] = "354002e1c4386d38fdde29ef8d61074a";
Hui Yingst57daee82020-10-09 05:47:20 +0000549#endif
Tom Sepezef43c262018-11-07 16:41:32 +0000550 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang430b5322020-07-06 22:23:36 +0000551 CompareBitmap(bitmap.get(), 612, 792, kExpectedHash);
Tom Sepezef43c262018-11-07 16:41:32 +0000552 }
Tom Sepez507d0192018-11-07 16:37:51 +0000553 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400554}
Jane Liu20eafda2017-06-07 10:33:24 -0400555
Lei Zhangab41f252018-12-23 03:10:50 +0000556TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400557 // Open a file with one annotation and load its first page.
558 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000559 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400560 ASSERT_TRUE(page);
561
562 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400563 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400564
565 UnloadPage(page);
566}
567
Lei Zhangab41f252018-12-23 03:10:50 +0000568TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400569 // Open a file with no annotation and load its first page.
570 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000571 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400572 ASSERT_TRUE(page);
573 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
574
Lei Zhanga21d5932018-02-05 18:28:38 +0000575 {
576 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000577 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000578 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400579
Lei Zhanga21d5932018-02-05 18:28:38 +0000580 // Check that there is now 1 annotations on this page.
581 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400582
Lei Zhanga21d5932018-02-05 18:28:38 +0000583 // Check that the subtype of the annotation is correct.
584 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
585 }
Jane Liue10509a2017-06-20 16:47:41 -0400586
Lei Zhanga21d5932018-02-05 18:28:38 +0000587 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000588 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000589 ASSERT_TRUE(annot);
590 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400591
Lei Zhanga21d5932018-02-05 18:28:38 +0000592 // Set the color of the annotation.
593 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
594 102, 153, 204));
595 // Check that the color has been set correctly.
596 unsigned int R;
597 unsigned int G;
598 unsigned int B;
599 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000600 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000601 &G, &B, &A));
602 EXPECT_EQ(51u, R);
603 EXPECT_EQ(102u, G);
604 EXPECT_EQ(153u, B);
605 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400606
Lei Zhanga21d5932018-02-05 18:28:38 +0000607 // Change the color of the annotation.
608 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
609 153, 102, 51));
610 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000611 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000612 &G, &B, &A));
613 EXPECT_EQ(204u, R);
614 EXPECT_EQ(153u, G);
615 EXPECT_EQ(102u, B);
616 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400617
Lei Zhanga21d5932018-02-05 18:28:38 +0000618 // Set the annotation rectangle.
619 FS_RECTF rect;
620 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
621 EXPECT_EQ(0.f, rect.left);
622 EXPECT_EQ(0.f, rect.right);
623 rect.left = 35;
624 rect.bottom = 150;
625 rect.right = 53;
626 rect.top = 165;
627 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
628 // Check that the annotation rectangle has been set correctly.
629 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
630 EXPECT_EQ(35.f, rect.left);
631 EXPECT_EQ(150.f, rect.bottom);
632 EXPECT_EQ(53.f, rect.right);
633 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400634
Lei Zhanga21d5932018-02-05 18:28:38 +0000635 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000636 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000637 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000638 ASSERT_TRUE(FPDFAnnot_SetStringValue(
639 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000640 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000641 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000642 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000643 ASSERT_EQ(74u, length_bytes);
644 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
645 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
646 pdfium::annotation::kContents,
647 buf.data(), length_bytes));
648 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000649 }
Jane Liu20eafda2017-06-07 10:33:24 -0400650 UnloadPage(page);
651}
652
Lei Zhang81395aa2021-04-16 00:40:46 +0000653TEST_F(FPDFAnnotEmbedderTest, AddAndSaveLinkAnnotation) {
654 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
655 FPDF_PAGE page = LoadPage(0);
656 ASSERT_TRUE(page);
657 {
658 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
659 CompareBitmap(bitmap.get(), 200, 200, pdfium::kHelloWorldChecksum);
660 }
661 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
662
663 constexpr char kUri[] = "https://pdfium.org/";
664
665 {
666 // Add a link annotation to the page and set its URI.
667 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_LINK));
668 ASSERT_TRUE(annot);
669 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
670 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
671 EXPECT_TRUE(FPDFAnnot_SetURI(annot.get(), kUri));
672 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
673
674 // Negative tests:
675 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, nullptr));
676 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
677 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), nullptr));
678 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
679 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, kUri));
680 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
681
682 // Position the link on top of "Hello, world!" without a border.
683 const FS_RECTF kRect = {19.0f, 48.0f, 85.0f, 60.0f};
684 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &kRect));
685 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/0.0f,
686 /*vertical_radius=*/0.0f,
687 /*border_width=*/0.0f));
688
689 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
690 kUri);
691 }
692
693 {
694 // Add an ink annotation to the page. Trying to add a link to it fails.
695 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
696 ASSERT_TRUE(annot);
697 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
698 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
699 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), kUri));
700 }
701
702 // Remove the ink annotation added above for negative testing.
703 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
704 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
705
706 // Save the document, closing the page.
707 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
708 UnloadPage(page);
709
710 // Reopen the document and make sure it still renders the same. Since the link
711 // does not have a border, it does not affect the rendering.
712 ASSERT_TRUE(OpenSavedDocument());
713 page = LoadSavedPage(0);
714 ASSERT_TRUE(page);
715 VerifySavedRendering(page, 200, 200, pdfium::kHelloWorldChecksum);
716 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
717
718 {
719 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
720 ASSERT_TRUE(annot);
721 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
722 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
723 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
724 kUri);
725 }
726
727 CloseSavedPage(page);
728 CloseSavedDocument();
729}
730
Hui Yingstb3490322020-07-22 00:53:29 +0000731TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400732 // Open a file with one annotation and load its first page.
733 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000734 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400735 ASSERT_TRUE(page);
736
737 // Check that there is a total of one annotation on its first page, and verify
738 // its quadpoints.
739 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400740 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000741 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000742 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000743 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000744 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000745 EXPECT_EQ(115.802643f, quadpoints.x1);
746 EXPECT_EQ(718.913940f, quadpoints.y1);
747 EXPECT_EQ(157.211182f, quadpoints.x4);
748 EXPECT_EQ(706.264465f, quadpoints.y4);
749 }
Jane Liu20eafda2017-06-07 10:33:24 -0400750
751 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000752 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000753 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000754 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
755 ASSERT_TRUE(annot);
756 quadpoints.x1 = 140.802643f;
757 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000758 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000759 }
Jane Liu20eafda2017-06-07 10:33:24 -0400760
Lei Zhangec618142021-04-13 17:36:46 +0000761 // Save the document and close the page.
Jane Liu20eafda2017-06-07 10:33:24 -0400762 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000763 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400764
765 // Open the saved document.
Hui Yingste1215fc2022-03-26 22:29:08 +0000766#if defined(_SKIA_SUPPORT_)
Hui Yingst57daee82020-10-09 05:47:20 +0000767 static const char kChecksum[] = "899387ae792390cd0d83cf7e2bbebfb5";
Hui Yingste1215fc2022-03-26 22:29:08 +0000768#elif defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst57daee82020-10-09 05:47:20 +0000769 static const char kChecksum[] = "e40e235ee35f47ff28dda009aaaf36df";
Hui Yingstb3490322020-07-22 00:53:29 +0000770#else
771 static const char kChecksum[] = "dba153419f67b7c0c0e3d22d3e8910d5";
772#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400773
Lei Zhang0b494052019-01-31 21:41:15 +0000774 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000775 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +0000776 ASSERT_TRUE(page);
Hui Yingstb3490322020-07-22 00:53:29 +0000777 VerifySavedRendering(page, 612, 792, kChecksum);
Jane Liu20eafda2017-06-07 10:33:24 -0400778
779 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000780 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400781
Lei Zhanga21d5932018-02-05 18:28:38 +0000782 {
783 // Check that the second annotation is an underline annotation and verify
784 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000785 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000786 ASSERT_TRUE(new_annot);
787 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
788 FS_QUADPOINTSF new_quadpoints;
789 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000790 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000791 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
792 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
793 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
794 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
795 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400796
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000797 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400798 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400799}
Jane Liu06462752017-06-27 16:41:14 -0400800
Lei Zhangab41f252018-12-23 03:10:50 +0000801TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000802 // Open a file with four annotations and load its first page.
803 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
804 FPDF_PAGE page = LoadPage(0);
805 ASSERT_TRUE(page);
806 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
807
808 // Retrieve the highlight annotation.
809 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
810 ASSERT_TRUE(annot);
811 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
812
813 FS_QUADPOINTSF quadpoints;
814 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
815
816 {
817 // Verify the current one set of quadpoints.
818 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
819
820 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
821 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
822 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
823 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
824 }
825
826 {
827 // Update the quadpoints.
828 FS_QUADPOINTSF new_quadpoints = quadpoints;
829 new_quadpoints.y1 -= 20.f;
830 new_quadpoints.y2 -= 20.f;
831 new_quadpoints.y3 -= 20.f;
832 new_quadpoints.y4 -= 20.f;
833 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
834
835 // Verify added quadpoint set
836 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
837 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
838 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
839 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
840 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
841 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
842 }
843
844 {
845 // Append a new set of quadpoints.
846 FS_QUADPOINTSF new_quadpoints = quadpoints;
847 new_quadpoints.y1 += 20.f;
848 new_quadpoints.y2 += 20.f;
849 new_quadpoints.y3 += 20.f;
850 new_quadpoints.y4 += 20.f;
851 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
852
853 // Verify added quadpoint set
854 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
855 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
856 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
857 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
858 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
859 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
860 }
861
862 {
863 // Setting and getting quadpoints at out-of-bound index should fail
864 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
865 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
866 }
867
868 FPDFPage_CloseAnnot(annot);
869
870 // Retrieve the square annotation
871 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
872
873 {
874 // Check that attempting to set its quadpoints would fail
875 ASSERT_TRUE(squareAnnot);
876 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
877 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
878 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
879 }
880
881 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000882 UnloadPage(page);
883}
884
Hui Yingstb64cd122020-07-27 16:01:02 +0000885// TODO(crbug.com/pdfium/1569): Fix this issue and enable the test for Skia.
886#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +0000887#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
888#else
889#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
890#endif
891TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Hui Yingstb64cd122020-07-27 16:01:02 +0000892#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingste1215fc2022-03-26 22:29:08 +0000893 static const char kMd5Original[] = "127c2d3b4452555e3317827b0dbbb6a0";
Lei Zhang4f556b82019-04-08 16:32:41 +0000894 static const char kMd5ModifiedHighlight[] =
Hui Yingste1215fc2022-03-26 22:29:08 +0000895 "6ffe732be6f80540b60921c4803b590a";
896 static const char kMd5ModifiedSquare[] = "9ecbeea7f54abea298b53ce79d301f4a";
Hui Yingstb64cd122020-07-27 16:01:02 +0000897#else
Hui Yingste1215fc2022-03-26 22:29:08 +0000898#if BUILDFLAG(IS_APPLE)
Hui Yingstb64cd122020-07-27 16:01:02 +0000899 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
900 static const char kMd5ModifiedHighlight[] =
901 "e64bf648f6e9354d1f3eedb47a2c9498";
902 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Jane Liub370e5a2017-08-16 13:24:58 -0400903#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000904 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
905 static const char kMd5ModifiedHighlight[] =
906 "66f3caef3a7d488a4fa1ad37fc06310e";
907 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Hui Yingste1215fc2022-03-26 22:29:08 +0000908#endif // BUILDFLAG(IS_APPLE)
Hui Yingstb64cd122020-07-27 16:01:02 +0000909#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liub370e5a2017-08-16 13:24:58 -0400910
Jane Liu06462752017-06-27 16:41:14 -0400911 // Open a file with four annotations and load its first page.
912 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000913 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400914 ASSERT_TRUE(page);
915 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
916
Jane Liub370e5a2017-08-16 13:24:58 -0400917 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000918 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000919 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000920 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000921 }
Jane Liub370e5a2017-08-16 13:24:58 -0400922
Jane Liu0c6b07d2017-08-15 10:50:22 -0400923 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400924 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000925
926 // Retrieve the highlight annotation which has its AP stream already defined.
927 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000928 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000929 ASSERT_TRUE(annot);
930 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
931
932 // Check that color cannot be set when an AP stream is defined already.
933 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
934 102, 153, 204));
935
936 // Verify its attachment points.
937 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000938 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000939 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
940 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
941 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
942 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
943
944 // Check that updating the attachment points would succeed.
945 quadpoints.x1 -= 50.f;
946 quadpoints.x2 -= 50.f;
947 quadpoints.x3 -= 50.f;
948 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000949 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000950 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000951 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000952 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
953 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
954 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
955 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
956
957 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000958 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000959 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000960 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000961 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000962
963 // Verify its annotation rectangle.
964 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
965 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
966 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
967 EXPECT_NEAR(136.325f, rect.right, 0.001f);
968 EXPECT_NEAR(721.292f, rect.top, 0.001f);
969
970 // Check that updating the rectangle would succeed.
971 rect.left -= 60.f;
972 rect.right -= 60.f;
973 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
974 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
975 EXPECT_EQ(rect.right, new_rect.right);
976 }
Jane Liu06462752017-06-27 16:41:14 -0400977
Jane Liub370e5a2017-08-16 13:24:58 -0400978 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000979 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000980 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000981 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000982 }
Jane Liub370e5a2017-08-16 13:24:58 -0400983
Lei Zhanga21d5932018-02-05 18:28:38 +0000984 {
985 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000986 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000987 ASSERT_TRUE(annot);
988 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400989
Lei Zhanga21d5932018-02-05 18:28:38 +0000990 // Check that updating the rectangle would succeed.
991 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
992 rect.left += 70.f;
993 rect.right += 70.f;
994 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
995 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
996 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400997
Lei Zhanga21d5932018-02-05 18:28:38 +0000998 // Check that updating the rectangle changes the square annotation's
999 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001000 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001001 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +00001002 }
Jane Liub370e5a2017-08-16 13:24:58 -04001003
Jane Liu06462752017-06-27 16:41:14 -04001004 UnloadPage(page);
1005}
Jane Liu8ce58f52017-06-29 13:40:22 -04001006
Lei Zhangab41f252018-12-23 03:10:50 +00001007TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +00001008 // Open a file with multiline markup annotations.
1009 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
1010 FPDF_PAGE page = LoadPage(0);
1011 ASSERT_TRUE(page);
1012 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001013 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +00001014 ASSERT_TRUE(annot);
1015
1016 // This is a three line annotation.
1017 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
1018 }
1019 UnloadPage(page);
1020
1021 // null annotation should return 0
1022 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
1023}
1024
Lei Zhangab41f252018-12-23 03:10:50 +00001025TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -04001026 // Open a file with 3 annotations on its first page.
1027 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +00001028 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -04001029 ASSERT_TRUE(page);
1030 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1031
Jane Liu0c6b07d2017-08-15 10:50:22 -04001032 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -04001033
Lei Zhanga21d5932018-02-05 18:28:38 +00001034 // Check that the annotations have the expected rectangle coordinates.
1035 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001036 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001037 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1038 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1039 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001040
Lei Zhanga21d5932018-02-05 18:28:38 +00001041 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001042 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001043 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1044 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
1045 }
1046
1047 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001048 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001049 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1050 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1051 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001052
1053 // Check that nothing happens when attempting to remove an annotation with an
1054 // out-of-bound index.
1055 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
1056 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
1057 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1058
1059 // Remove the second annotation.
1060 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
1061 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1062 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
1063
Lei Zhangec618142021-04-13 17:36:46 +00001064 // Save the document and close the page.
Jane Liu8ce58f52017-06-29 13:40:22 -04001065 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +00001066 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -04001067
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001068 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -04001069 // Open the saved document.
1070 std::string new_file = GetString();
1071 FPDF_FILEACCESS file_access;
1072 memset(&file_access, 0, sizeof(file_access));
1073 file_access.m_FileLen = new_file.size();
1074 file_access.m_GetBlock = GetBlockFromString;
1075 file_access.m_Param = &new_file;
1076 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
1077 ASSERT_TRUE(new_doc);
1078 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
1079 ASSERT_TRUE(new_page);
1080
1081 // Check that the saved document has 2 annotations on the first page.
1082 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
1083
Lei Zhanga21d5932018-02-05 18:28:38 +00001084 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1085 // by verifying their rectangle coordinates.
1086 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001087 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001088 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1089 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1090 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001091
Lei Zhanga21d5932018-02-05 18:28:38 +00001092 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001093 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001094 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1095 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1096 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001097 FPDF_ClosePage(new_page);
1098 FPDF_CloseDocument(new_doc);
1099}
Jane Liu8ce58f52017-06-29 13:40:22 -04001100
Hui Yingst4a21df02020-05-13 03:15:44 +00001101TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001102#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001103 static const char kMd5ModifiedPath[] = "b820e4ae359db95cbac9823937c6da1a";
1104 static const char kMd5TwoPaths[] = "c53837b7bb6a9a21a846aa786526aa56";
1105 static const char kMd5NewAnnot[] = "4f0f4217156e4251036f369184a48967";
Lei Zhang42d30c22022-01-12 19:24:43 +00001106#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001107 static const char kMd5ModifiedPath[] = "e31421f86c61d4e9cda138f15f561ca3";
1108 static const char kMd5TwoPaths[] = "58d932492f9d485d6a4bc0ba76c04557";
1109 static const char kMd5NewAnnot[] = "61f9ad13f2fd235753db198cf9704773";
Jane Liubaa7ff42017-06-29 19:18:23 -04001110#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001111 static const char kMd5ModifiedPath[] = "980e7636d864f7f7d323a31ad4e8fa04";
1112 static const char kMd5TwoPaths[] = "4c779c394b6790f8cf80305b566b663b";
1113 static const char kMd5NewAnnot[] = "97effd68dcf86273f68d126d6b45152e";
Jane Liubaa7ff42017-06-29 19:18:23 -04001114#endif
1115
1116 // Open a file with two annotations and load its first page.
1117 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001118 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001119 ASSERT_TRUE(page);
1120 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1121
1122 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001123 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001124 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001125 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001126 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001127
Lei Zhanga21d5932018-02-05 18:28:38 +00001128 {
1129 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001130 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001131 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001132
Lei Zhanga21d5932018-02-05 18:28:38 +00001133 // Check that this annotation has one path object and retrieve it.
1134 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001135 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001136 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1137 EXPECT_FALSE(path);
1138 path = FPDFAnnot_GetObject(annot.get(), 0);
1139 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1140 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001141
Lei Zhanga21d5932018-02-05 18:28:38 +00001142 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001143 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001144 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001145
Lei Zhanga21d5932018-02-05 18:28:38 +00001146 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001147 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001148 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001149 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001150 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001151
Lei Zhanga21d5932018-02-05 18:28:38 +00001152 // Add a second path object to the same annotation.
1153 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1154 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001155 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1156 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001157 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1158 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1159 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001160
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001161 // The object is in the annontation, not in the page, so the page object
1162 // array should not change.
1163 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1164
Lei Zhanga21d5932018-02-05 18:28:38 +00001165 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001166 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001167 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001168 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001169 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001170
Lei Zhanga21d5932018-02-05 18:28:38 +00001171 // Delete the newly added path object.
1172 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1173 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001174 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001175 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001176
1177 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001178 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001179 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001180 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001181 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001182
Jane Liubaa7ff42017-06-29 19:18:23 -04001183 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001184
Lei Zhanga21d5932018-02-05 18:28:38 +00001185 {
1186 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001187 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001188 ASSERT_TRUE(annot);
1189 rect.left = 200.f;
1190 rect.bottom = 400.f;
1191 rect.right = 500.f;
1192 rect.top = 600.f;
1193 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001194
Lei Zhanga21d5932018-02-05 18:28:38 +00001195 // Add a new path to the annotation.
1196 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1197 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1198 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1199 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1200 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001201 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1202 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001203 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1204 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1205 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1206
1207 // Check that the annotation's bounding box came from its rectangle.
1208 FS_RECTF new_rect;
1209 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1210 EXPECT_EQ(rect.left, new_rect.left);
1211 EXPECT_EQ(rect.bottom, new_rect.bottom);
1212 EXPECT_EQ(rect.right, new_rect.right);
1213 EXPECT_EQ(rect.top, new_rect.top);
1214 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001215
Lei Zhangec618142021-04-13 17:36:46 +00001216 // Save the document and close the page.
Jane Liubaa7ff42017-06-29 19:18:23 -04001217 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001218 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001219
1220 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001221 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001222 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001223 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001224 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001225
Jane Liu36567742017-07-06 11:13:35 -04001226 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001227 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001228
Lei Zhanga21d5932018-02-05 18:28:38 +00001229 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001230 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001231 ASSERT_TRUE(annot);
1232 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001233
Lei Zhanga21d5932018-02-05 18:28:38 +00001234 // Check that the new annotation's rectangle is as defined.
1235 FS_RECTF new_rect;
1236 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1237 EXPECT_EQ(rect.left, new_rect.left);
1238 EXPECT_EQ(rect.bottom, new_rect.bottom);
1239 EXPECT_EQ(rect.right, new_rect.right);
1240 EXPECT_EQ(rect.top, new_rect.top);
1241 }
1242
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001243 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001244 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001245}
Jane Liub137e752017-07-05 15:04:33 -04001246
Lei Zhangab41f252018-12-23 03:10:50 +00001247TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001248 // Open a file with an annotation and load its first page.
1249 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001250 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001251 ASSERT_TRUE(page);
1252
1253 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001254 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001255 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001256 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1257 }
Jane Liub137e752017-07-05 15:04:33 -04001258
Lei Zhanga21d5932018-02-05 18:28:38 +00001259 {
1260 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001261 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001262 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001263
Lei Zhanga21d5932018-02-05 18:28:38 +00001264 // Check that the original flag values are as expected.
1265 int flags = FPDFAnnot_GetFlags(annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00001266 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_INVISIBLE);
Lei Zhanga21d5932018-02-05 18:28:38 +00001267 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1268 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001269 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOZOOM);
1270 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOROTATE);
1271 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOVIEW);
1272 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_READONLY);
1273 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_LOCKED);
1274 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_TOGGLENOVIEW);
Jane Liub137e752017-07-05 15:04:33 -04001275
Lei Zhanga21d5932018-02-05 18:28:38 +00001276 // Set the HIDDEN flag.
1277 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1278 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1279 flags = FPDFAnnot_GetFlags(annot.get());
1280 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1281 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001282
Lei Zhanga21d5932018-02-05 18:28:38 +00001283 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001284 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001285 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001286 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001287 }
Jane Liub137e752017-07-05 15:04:33 -04001288
Lei Zhanga21d5932018-02-05 18:28:38 +00001289 // Unset the HIDDEN flag.
1290 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1291 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1292 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1293 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1294 flags = FPDFAnnot_GetFlags(annot.get());
1295 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1296 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001297
Lei Zhanga21d5932018-02-05 18:28:38 +00001298 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001299 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001300 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001301 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1302 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001303 }
Jane Liub137e752017-07-05 15:04:33 -04001304
Jane Liub137e752017-07-05 15:04:33 -04001305 UnloadPage(page);
1306}
Jane Liu36567742017-07-06 11:13:35 -04001307
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001308// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1309#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001310#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1311#else
1312#define MAYBE_AddAndModifyImage AddAndModifyImage
1313#endif
1314TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001315#if defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001316 static const char kMd5NewImage[] = "beb7db3647706d7fe4689f92073847aa";
1317 static const char kMd5ModifiedImage[] = "baa9b065469268e215ef958fe6987d6b";
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001318#else
Lei Zhang42d30c22022-01-12 19:24:43 +00001319#if BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001320 static const char kMd5NewImage[] = "c6fcbceb2f079bef10458ac60db3a10c";
1321 static const char kMd5ModifiedImage[] = "8068eb568e5c1c5fbe84e98f7a980ac3";
Jane Liu36567742017-07-06 11:13:35 -04001322#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001323 static const char kMd5NewImage[] = "62c2706511cb50e32e7caeb82b1d3d49";
1324 static const char kMd5ModifiedImage[] = "83093ce9fac746db69fbd2fb394434ac";
Jane Liu36567742017-07-06 11:13:35 -04001325#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001326#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001327
1328 // Open a file with two annotations and load its first page.
1329 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001330 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001331 ASSERT_TRUE(page);
1332 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1333
1334 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001335 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001336 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001337 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001338 }
Jane Liu36567742017-07-06 11:13:35 -04001339
Jane Liu36567742017-07-06 11:13:35 -04001340 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001341 FPDF_BITMAP image_bitmap;
1342
1343 {
1344 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001345 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001346 ASSERT_TRUE(annot);
1347 FS_RECTF rect;
1348 rect.left = 200.f;
1349 rect.bottom = 600.f;
1350 rect.right = 400.f;
1351 rect.top = 800.f;
1352 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1353
1354 // Add a solid-color translucent image object to the new annotation.
1355 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1356 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1357 0xeeeecccc);
1358 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1359 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1360 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1361 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
Lei Zhangc8601bf2021-06-29 23:19:27 +00001362 static constexpr FS_MATRIX kBitmapScaleMatrix{kBitmapSize, 0, 0,
1363 kBitmapSize, 0, 0};
1364 ASSERT_TRUE(FPDFPageObj_SetMatrix(image_object, &kBitmapScaleMatrix));
Lei Zhanga21d5932018-02-05 18:28:38 +00001365 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1366 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1367 }
Jane Liu36567742017-07-06 11:13:35 -04001368
1369 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001370 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001371 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001372 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001373 }
Jane Liu36567742017-07-06 11:13:35 -04001374
Lei Zhanga21d5932018-02-05 18:28:38 +00001375 {
1376 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001377 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001378 ASSERT_TRUE(annot);
1379 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1380 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1381 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001382
Lei Zhanga21d5932018-02-05 18:28:38 +00001383 // Modify the image in the new annotation.
1384 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1385 0xff000000);
1386 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1387 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1388 }
Jane Liu36567742017-07-06 11:13:35 -04001389
Lei Zhangec618142021-04-13 17:36:46 +00001390 // Save the document and close the page.
Jane Liu36567742017-07-06 11:13:35 -04001391 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001392 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001393 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001394
1395 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001396 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001397}
1398
Hui Yingst6cd754f2020-05-14 04:05:25 +00001399TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001400#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001401 static const char kMd5NewText[] = "310d9de5f17fb288fb243f5dbaf2b6dc";
1402 static const char kMd5ModifiedText[] = "22be42c136c3bf5a8ecea3dd83770a02";
Lei Zhang42d30c22022-01-12 19:24:43 +00001403#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001404 static const char kMd5NewText[] = "57a0fb3fba33e17de26bcde4c40b9a75";
1405 static const char kMd5ModifiedText[] = "072574999f2e3f36774ee0b5bc94d4dd";
Jane Liu36567742017-07-06 11:13:35 -04001406#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001407 static const char kMd5NewText[] = "1c4198c38f890c208c5cbaad57be4dc6";
1408 static const char kMd5ModifiedText[] = "cfa78d01406865f41f486bd34a8b9f7b";
Jane Liu36567742017-07-06 11:13:35 -04001409#endif
1410
1411 // Open a file with two annotations and load its first page.
1412 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001413 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001414 ASSERT_TRUE(page);
1415 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1416
1417 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001418 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001419 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001420 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001421 }
Jane Liu36567742017-07-06 11:13:35 -04001422
Lei Zhanga21d5932018-02-05 18:28:38 +00001423 {
1424 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001425 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001426 ASSERT_TRUE(annot);
1427 FS_RECTF rect;
1428 rect.left = 200.f;
1429 rect.bottom = 550.f;
1430 rect.right = 450.f;
1431 rect.top = 650.f;
1432 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001433
Lei Zhanga21d5932018-02-05 18:28:38 +00001434 // Add a translucent text object to the new annotation.
1435 FPDF_PAGEOBJECT text_object =
1436 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1437 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001438 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001439 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1440 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001441 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001442 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1443 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1444 }
Jane Liu36567742017-07-06 11:13:35 -04001445
1446 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001447 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001448 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001449 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001450 }
Jane Liu36567742017-07-06 11:13:35 -04001451
Lei Zhanga21d5932018-02-05 18:28:38 +00001452 {
1453 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001454 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001455 ASSERT_TRUE(annot);
1456 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1457 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1458 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001459
Lei Zhanga21d5932018-02-05 18:28:38 +00001460 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001461 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001462 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1463 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1464 }
Jane Liu36567742017-07-06 11:13:35 -04001465
1466 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001467 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001468 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001469 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001470 }
Jane Liu36567742017-07-06 11:13:35 -04001471
1472 // Remove the new annotation, and check that the page renders as before.
1473 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001474 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001475 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001476 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001477 }
Jane Liu36567742017-07-06 11:13:35 -04001478
1479 UnloadPage(page);
1480}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001481
Hui Yingst9b6b1542020-07-27 16:11:12 +00001482TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001483 // Open a file with four annotations and load its first page.
1484 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001485 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001486 ASSERT_TRUE(page);
1487
Lei Zhang4f556b82019-04-08 16:32:41 +00001488 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001489
Lei Zhanga21d5932018-02-05 18:28:38 +00001490 {
1491 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001492 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001493 ASSERT_TRUE(annot);
1494
1495 // Check that a non-existent key does not exist.
1496 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1497
1498 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001499 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001500 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001501 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1502 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1503 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001504
1505 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001506 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001507 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001508 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001509 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001510 ASSERT_EQ(66u, length_bytes);
1511 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1512 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1513 length_bytes));
1514 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1515 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001516
1517 // Check that the string value of the modified date is correct.
1518 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001519 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1520 nullptr, 0);
1521 ASSERT_EQ(44u, length_bytes);
1522 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001523 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001524 buf.data(), length_bytes));
1525 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001526
1527 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001528 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001529 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1530 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001531 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001532
Lei Zhangec618142021-04-13 17:36:46 +00001533 // Save the document and close the page.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001534 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001535 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001536
Hui Yingst9b6b1542020-07-27 16:11:12 +00001537#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001538 static const char kMd5[] = "aa0e47d966c60d59102a2466542e0e46";
Lei Zhang42d30c22022-01-12 19:24:43 +00001539#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001540 static const char kMd5[] = "cd90315b250dfe08265ce0ac335c5f76";
Hui Yingst9b6b1542020-07-27 16:11:12 +00001541#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001542 static const char kMd5[] = "c4fb6911f2a87f490be196f8898de738";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001543#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001544
1545 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001546 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001547 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001548 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001549 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001550 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001551 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001552
Lei Zhanga21d5932018-02-05 18:28:38 +00001553 // Check that the string value of the modified date is the newly-set value.
1554 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001555 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001556 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001557 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001558 ASSERT_EQ(44u, length_bytes);
1559 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001560 EXPECT_EQ(44u,
1561 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001562 buf.data(), length_bytes));
1563 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001564 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001565
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001566 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001567 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001568}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001569
rycsmith3e785602019-03-05 21:48:36 +00001570TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001571 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001572 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1573 FPDF_PAGE page = LoadPage(0);
1574 ASSERT_TRUE(page);
1575 {
1576 // First two annotations do not have "MaxLen" attribute.
1577 for (int i = 0; i < 2; i++) {
1578 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1579 ASSERT_TRUE(annot);
1580
1581 // Verify that no "MaxLen" key present.
1582 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1583
1584 float value;
1585 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1586 }
1587
1588 // Annotation in index 2 has "MaxLen" of 10.
1589 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1590 ASSERT_TRUE(annot);
1591
1592 // Verify that "MaxLen" key present.
1593 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1594
1595 float value;
1596 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1597 EXPECT_FLOAT_EQ(10.0f, value);
1598
1599 // Check bad inputs.
1600 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1601 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1602 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1603 // Ask for key that exists but is not a number.
1604 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1605 }
1606
1607 UnloadPage(page);
1608}
1609
Lei Zhangab41f252018-12-23 03:10:50 +00001610TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001611 // Open a file with four annotations and load its first page.
1612 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001613 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001614 ASSERT_TRUE(page);
1615
Lei Zhanga21d5932018-02-05 18:28:38 +00001616 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001617 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1618 static constexpr size_t kExpectNormalAPLength = 73970;
1619
Lei Zhanga21d5932018-02-05 18:28:38 +00001620 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001621 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001622 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001623
Lei Zhanga21d5932018-02-05 18:28:38 +00001624 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001625 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001626 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001627 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001628
Lei Zhanga21d5932018-02-05 18:28:38 +00001629 // Check that the string value of an AP is not returned if the buffer is too
1630 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001631 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1632 // Write in the buffer to verify it's not overwritten.
1633 memcpy(buf.data(), "abcdefgh", 8);
1634 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001635 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001636 buf.data(), normal_length_bytes - 1));
1637 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001638
Lei Zhanga21d5932018-02-05 18:28:38 +00001639 // Check that the string value of an AP is returned through a buffer that is
1640 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001641 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001642 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001643 buf.data(), normal_length_bytes));
1644 EXPECT_EQ(kMd5NormalAP,
1645 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1646 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001647
Lei Zhanga21d5932018-02-05 18:28:38 +00001648 // Check that the string value of an AP is returned through a buffer that is
1649 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001650 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1651 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001652 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001653 buf.data(), normal_length_bytes + 2));
1654 EXPECT_EQ(kMd5NormalAP,
1655 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1656 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001657
Lei Zhanga21d5932018-02-05 18:28:38 +00001658 // Check that getting an AP for a mode that does not have an AP returns an
1659 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001660 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001661 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001662 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001663
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001664 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001665 EXPECT_EQ(2u,
1666 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001667 buf.data(), 1000));
1668 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001669
Lei Zhanga21d5932018-02-05 18:28:38 +00001670 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001671 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001672 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001673 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001674 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001675 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001676 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001677
Lei Zhanga21d5932018-02-05 18:28:38 +00001678 // Set the AP correctly now.
1679 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001680 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001681
Lei Zhanga21d5932018-02-05 18:28:38 +00001682 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001683 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001684 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001685 ASSERT_EQ(24u, rollover_length_bytes);
1686 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001687 EXPECT_EQ(24u,
1688 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001689 buf.data(), rollover_length_bytes));
1690 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001691
Lei Zhanga21d5932018-02-05 18:28:38 +00001692 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001693 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1694 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001695 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001696 buf.data(), normal_length_bytes));
1697 EXPECT_EQ(kMd5NormalAP,
1698 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1699 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001700 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001701
1702 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001703 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001704 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001705
Lei Zhang0b494052019-01-31 21:41:15 +00001706 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001707 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001708 ASSERT_TRUE(page);
Lei Zhanga21d5932018-02-05 18:28:38 +00001709 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001710 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001711
Lei Zhanga21d5932018-02-05 18:28:38 +00001712 // Check that the new annotation value is equal to the value we set before
1713 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001714 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001715 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001716 ASSERT_EQ(24u, rollover_length_bytes);
1717 std::vector<FPDF_WCHAR> buf =
1718 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001719 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1720 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001721 buf.data(), rollover_length_bytes));
1722 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001723 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001724
1725 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001726 CloseSavedPage(page);
1727 CloseSavedDocument();
1728}
1729
Lei Zhangab41f252018-12-23 03:10:50 +00001730TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001731 // Open a file with four annotations and load its first page.
1732 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001733 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001734 ASSERT_TRUE(page);
1735
Lei Zhanga21d5932018-02-05 18:28:38 +00001736 {
1737 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001738 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001739 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001740
Lei Zhanga21d5932018-02-05 18:28:38 +00001741 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001742 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001743 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001744 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001745 EXPECT_EQ(73970u,
1746 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1747 nullptr, 0));
1748 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1749 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001750
Lei Zhanga21d5932018-02-05 18:28:38 +00001751 // Check that setting the Down AP to null removes the Down entry but keeps
1752 // Normal intact.
1753 EXPECT_TRUE(
1754 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1755 EXPECT_EQ(73970u,
1756 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1757 nullptr, 0));
1758 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1759 nullptr, 0));
1760 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001761
Lei Zhang75c81712018-02-08 17:22:39 +00001762 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001763}
1764
Lei Zhangab41f252018-12-23 03:10:50 +00001765TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001766 // Open a file with four annotations and load its first page.
1767 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001768 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001769 ASSERT_TRUE(page);
1770
Lei Zhanga21d5932018-02-05 18:28:38 +00001771 {
1772 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001773 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001774 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001775
Lei Zhanga21d5932018-02-05 18:28:38 +00001776 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001777 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001778 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001779 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001780 EXPECT_EQ(73970u,
1781 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1782 nullptr, 0));
1783 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1784 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001785
Lei Zhanga21d5932018-02-05 18:28:38 +00001786 // Check that setting the Normal AP to null removes the whole AP dictionary.
1787 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1788 nullptr));
1789 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1790 nullptr, 0));
1791 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1792 nullptr, 0));
1793 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001794
Lei Zhang75c81712018-02-08 17:22:39 +00001795 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001796}
1797
Lei Zhangab41f252018-12-23 03:10:50 +00001798TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001799 // Open a file with annotations and load its first page.
1800 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001801 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001802 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001803 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001804
Lei Zhanga21d5932018-02-05 18:28:38 +00001805 {
1806 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001807 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001808 ASSERT_TRUE(annot);
1809 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1810 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001811 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001812 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1813 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1814 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001815
Lei Zhanga21d5932018-02-05 18:28:38 +00001816 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001817 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001818 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1819 ASSERT_TRUE(popup);
1820 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1821 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1822 FS_RECTF rect;
1823 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1824 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1825 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001826
Lei Zhanga21d5932018-02-05 18:28:38 +00001827 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1828 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001829 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001830 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1831 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001832
Lei Zhanga21d5932018-02-05 18:28:38 +00001833 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1834 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001835 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001836 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001837 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1838 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001839 }
Jane Liu300bb272017-08-21 14:37:53 -04001840
Jane Liu300bb272017-08-21 14:37:53 -04001841 UnloadPage(page);
1842}
1843
Lei Zhangab41f252018-12-23 03:10:50 +00001844TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001845 // Open file with form text fields.
1846 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001847 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001848 ASSERT_TRUE(page);
1849
Lei Zhanga21d5932018-02-05 18:28:38 +00001850 {
1851 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001852 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001853 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001854
Lei Zhanga21d5932018-02-05 18:28:38 +00001855 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001856 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001857 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001858 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1859 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1860 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001861 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001862 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001863
Lei Zhanga21d5932018-02-05 18:28:38 +00001864 {
1865 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001866 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001867 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001868
Lei Zhanga21d5932018-02-05 18:28:38 +00001869 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001870 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001871 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001872 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1873 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1874 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001875 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1876 }
1877
1878 {
1879 // Retrieve the fourth annotation: user-editable password text field.
1880 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1881 ASSERT_TRUE(annot);
1882
1883 // Check that the flag values are as expected.
1884 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1885 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001886 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1887 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1888 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001889 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001890 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001891
1892 UnloadPage(page);
1893}
1894
Lei Zhangab41f252018-12-23 03:10:50 +00001895TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001896 // Open file with form text fields.
1897 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001898 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001899 ASSERT_TRUE(page);
1900
Lei Zhanga21d5932018-02-05 18:28:38 +00001901 {
1902 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001903 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001904 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001905
Lei Zhanga21d5932018-02-05 18:28:38 +00001906 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001907 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001908 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001909 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1910 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001911 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1912 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001913 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001914 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001915
Lei Zhanga21d5932018-02-05 18:28:38 +00001916 {
1917 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001918 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001919 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001920
Lei Zhanga21d5932018-02-05 18:28:38 +00001921 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001922 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001923 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001924 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1925 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001926 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1927 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001928 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001929 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001930
Lei Zhanga21d5932018-02-05 18:28:38 +00001931 {
1932 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001933 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001934 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001935
Lei Zhanga21d5932018-02-05 18:28:38 +00001936 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001937 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001938 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001939 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1940 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001941 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1942 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001943 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001944 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001945
1946 UnloadPage(page);
1947}
Diana Gage40870db2017-07-19 18:16:03 -07001948
Lei Zhangab41f252018-12-23 03:10:50 +00001949TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001950 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001951 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001952 FPDF_PAGE page = LoadPage(0);
1953 ASSERT_TRUE(page);
1954
1955 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001956 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1957 EXPECT_FALSE(
1958 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001959
Lei Zhang8da98232019-12-11 23:29:33 +00001960 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001961 {
1962 // Verify there is an annotation.
1963 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001964 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001965 EXPECT_TRUE(annot);
1966 }
1967
1968 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001969 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1970 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1971 EXPECT_FALSE(
1972 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001973
1974 UnloadPage(page);
1975}
1976
Lei Zhangab41f252018-12-23 03:10:50 +00001977TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001978 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001979 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001980 FPDF_PAGE page = LoadPage(0);
1981 ASSERT_TRUE(page);
1982
Lei Zhanga21d5932018-02-05 18:28:38 +00001983 {
1984 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001985 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001986 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001987 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001988 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001989
Lei Zhanga21d5932018-02-05 18:28:38 +00001990 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001991 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00001992 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1993 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001994 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1995 }
Diana Gage40870db2017-07-19 18:16:03 -07001996
Lei Zhanga21d5932018-02-05 18:28:38 +00001997 {
1998 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001999 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002000 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002001 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002002 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002003
Lei Zhanga21d5932018-02-05 18:28:38 +00002004 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002005 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002006 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002007 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2008 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002009 }
Diana Gage40870db2017-07-19 18:16:03 -07002010
2011 UnloadPage(page);
2012}
2013
Lei Zhangab41f252018-12-23 03:10:50 +00002014TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07002015 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002016 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002017 FPDF_PAGE page = LoadPage(0);
2018 ASSERT_TRUE(page);
2019
Lei Zhanga21d5932018-02-05 18:28:38 +00002020 {
2021 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002022 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002023 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002024 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002025 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002026
Lei Zhanga21d5932018-02-05 18:28:38 +00002027 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002028 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002029 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002030 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2031 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002032 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2033 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002034 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002035 }
Diana Gage40870db2017-07-19 18:16:03 -07002036
Lei Zhanga21d5932018-02-05 18:28:38 +00002037 {
2038 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002039 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002040 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002041 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002042 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002043
Lei Zhanga21d5932018-02-05 18:28:38 +00002044 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002045 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002046 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002047 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2048 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002049 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2050 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002051 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002052 }
Diana Gage40870db2017-07-19 18:16:03 -07002053
Lei Zhanga21d5932018-02-05 18:28:38 +00002054 {
2055 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002056 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002057 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002058 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002059 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002060
Lei Zhanga21d5932018-02-05 18:28:38 +00002061 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002062 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002063 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002064 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2065 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002066 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2067 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002068 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002069 }
Diana Gage40870db2017-07-19 18:16:03 -07002070
2071 UnloadPage(page);
2072}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002073
Hui Yingst603dcd82020-11-09 22:36:46 +00002074TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
Lei Zhang03e5e682019-09-16 19:45:55 +00002075#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst603dcd82020-11-09 22:36:46 +00002076 static const char kExpectedBitmap[] = "a1ea1ceebb26922fae576cb79ce63af0";
Lei Zhang03e5e682019-09-16 19:45:55 +00002077#else
Lei Zhang992e7e22019-02-04 19:20:58 +00002078 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
Hui Yingst603dcd82020-11-09 22:36:46 +00002079#endif
Tom Andersond4fe5f72021-12-03 20:52:52 +00002080 static constexpr size_t kExpectedSize = 1590;
Lei Zhang992e7e22019-02-04 19:20:58 +00002081
2082 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2083
2084 FPDF_PAGE page = LoadPage(0);
2085 ASSERT_TRUE(page);
2086
2087 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2088 EXPECT_EQ(kExpectedSize, GetString().size());
2089 ClearString();
2090
2091 for (size_t i = 0; i < 10; ++i) {
2092 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2093 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2094
2095 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2096 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2097 // equal, not bigger.
2098 EXPECT_LT(kExpectedSize, GetString().size());
2099 ClearString();
2100 }
2101
2102 UnloadPage(page);
2103}
2104
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002105TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2106 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2107 FPDF_PAGE page = LoadPage(0);
2108 ASSERT_TRUE(page);
2109 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2110
2111 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002112 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002113 static const size_t kBufSize = 12;
2114 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002115
2116 {
2117 // Add a text annotation to the page.
2118 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2119 ASSERT_TRUE(annot);
2120 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2121 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2122
2123 // Make sure there is no test key, add set a value there, and read it back.
2124 std::fill(buf.begin(), buf.end(), 'x');
2125 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002126 kBufSize));
2127 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002128
Lei Zhangf0f67682019-04-08 17:03:21 +00002129 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002130 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2131
2132 std::fill(buf.begin(), buf.end(), 'x');
2133 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002134 kBufSize));
2135 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002136 }
2137
Lei Zhang05ec64c2019-01-09 03:00:06 +00002138 {
2139 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2140 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002141 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002142 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002143 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2144 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2145 // Also do the same test for its appearance string.
2146 std::fill(buf.begin(), buf.end(), 'x');
2147 ASSERT_EQ(2u,
2148 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002149 buf.data(), kBufSize));
2150 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002151
Lei Zhangf0f67682019-04-08 17:03:21 +00002152 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002153 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2154 text.get()));
2155
2156 std::fill(buf.begin(), buf.end(), 'x');
2157 ASSERT_EQ(6u,
2158 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002159 buf.data(), kBufSize));
2160 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002161 }
2162
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002163 UnloadPage(page);
2164
2165 {
2166 // Save a copy, open the copy, and check the annotation again.
2167 // Note that it renders the rotation.
2168 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002169 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002170 FPDF_PAGE saved_page = LoadSavedPage(0);
2171 ASSERT_TRUE(saved_page);
2172
Lei Zhang05ec64c2019-01-09 03:00:06 +00002173 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002174 {
2175 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2176 ASSERT_TRUE(annot);
2177 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2178
2179 std::fill(buf.begin(), buf.end(), 'x');
2180 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002181 kBufSize));
2182 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002183 }
2184
Lei Zhang05ec64c2019-01-09 03:00:06 +00002185 {
2186 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2187 ASSERT_TRUE(annot);
2188 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2189 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2190
2191 std::fill(buf.begin(), buf.end(), 'x');
2192 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002193 kBufSize));
2194 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002195 }
2196
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002197 CloseSavedPage(saved_page);
2198 CloseSavedDocument();
2199 }
2200}
rycsmithcb752f32019-02-21 18:40:53 +00002201
2202TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2203 // Open a file with combobox widget annotations and load its first page.
2204 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2205 FPDF_PAGE page = LoadPage(0);
2206 ASSERT_TRUE(page);
2207
2208 {
2209 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2210 ASSERT_TRUE(annot);
2211
2212 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2213
2214 annot.reset(FPDFPage_GetAnnot(page, 1));
2215 ASSERT_TRUE(annot);
2216
2217 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002218
2219 // Check bad form handle / annot.
2220 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2221 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2222 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002223 }
2224
2225 UnloadPage(page);
2226}
2227
2228TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2229 // Open a file with listbox widget annotations and load its first page.
2230 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2231 FPDF_PAGE page = LoadPage(0);
2232 ASSERT_TRUE(page);
2233
2234 {
2235 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2236 ASSERT_TRUE(annot);
2237
2238 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2239
2240 annot.reset(FPDFPage_GetAnnot(page, 1));
2241 ASSERT_TRUE(annot);
2242
2243 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2244 }
2245
2246 UnloadPage(page);
2247}
2248
2249TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2250 // Open a file with ink annotations and load its first page.
2251 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2252 FPDF_PAGE page = LoadPage(0);
2253 ASSERT_TRUE(page);
2254
2255 {
2256 // annotations do not have "Opt" array and will return -1
2257 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2258 ASSERT_TRUE(annot);
2259
2260 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2261
2262 annot.reset(FPDFPage_GetAnnot(page, 1));
2263 ASSERT_TRUE(annot);
2264
2265 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2266 }
2267
2268 UnloadPage(page);
2269}
2270
2271TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2272 // Open a file with combobox widget annotations and load its first page.
2273 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2274 FPDF_PAGE page = LoadPage(0);
2275 ASSERT_TRUE(page);
2276
2277 {
2278 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2279 ASSERT_TRUE(annot);
2280
2281 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002282 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002283 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002284 ASSERT_EQ(8u, length_bytes);
2285 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002286 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002287 buf.data(), length_bytes));
2288 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002289
2290 annot.reset(FPDFPage_GetAnnot(page, 1));
2291 ASSERT_TRUE(annot);
2292
2293 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002294 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002295 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002296 ASSERT_EQ(12u, length_bytes);
2297 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002298 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002299 buf.data(), length_bytes));
2300 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002301
2302 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002303 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002304 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002305 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002306 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002307 buf.data(), length_bytes));
2308 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002309
Lei Zhange7033c82019-02-26 19:30:49 +00002310 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002311 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2312 nullptr, 0));
2313 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2314 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002315
2316 // Check bad form handle / annot.
2317 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2318 EXPECT_EQ(0u,
2319 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2320 EXPECT_EQ(0u,
2321 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002322 }
2323
2324 UnloadPage(page);
2325}
2326
2327TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2328 // Open a file with listbox widget annotations and load its first page.
2329 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2330 FPDF_PAGE page = LoadPage(0);
2331 ASSERT_TRUE(page);
2332
2333 {
2334 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2335 ASSERT_TRUE(annot);
2336
2337 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002338 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002339 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002340 ASSERT_EQ(8u, length_bytes);
2341 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002342 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002343 buf.data(), length_bytes));
2344 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002345
2346 annot.reset(FPDFPage_GetAnnot(page, 1));
2347 ASSERT_TRUE(annot);
2348
2349 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002350 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002351 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002352 ASSERT_EQ(12u, length_bytes);
2353 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002354 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002355 buf.data(), length_bytes));
2356 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002357
2358 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002359 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002360 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002361 ASSERT_EQ(18u, length_bytes);
2362 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002363 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002364 buf.data(), length_bytes));
2365 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002366
2367 // indices out of range
2368 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2369 nullptr, 0));
2370 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2371 nullptr, 0));
2372 }
2373
2374 UnloadPage(page);
2375}
2376
2377TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2378 // Open a file with ink annotations and load its first page.
2379 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2380 FPDF_PAGE page = LoadPage(0);
2381 ASSERT_TRUE(page);
2382
2383 {
2384 // annotations do not have "Opt" array and will return 0
2385 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2386 ASSERT_TRUE(annot);
2387
2388 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2389 nullptr, 0));
2390
2391 annot.reset(FPDFPage_GetAnnot(page, 1));
2392 ASSERT_TRUE(annot);
2393
2394 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2395 nullptr, 0));
2396 }
2397
2398 UnloadPage(page);
2399}
Ryan Smith09c23b12019-04-25 18:09:06 +00002400
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002401TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2402 // Open a file with combobox widget annotations and load its first page.
2403 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2404 FPDF_PAGE page = LoadPage(0);
2405 ASSERT_TRUE(page);
2406
2407 {
2408 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2409 ASSERT_TRUE(annot);
2410
2411 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2412 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2413 ASSERT_EQ(3, count);
2414 for (int i = 0; i < count; i++) {
2415 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2416 }
2417
2418 annot.reset(FPDFPage_GetAnnot(page, 1));
2419 ASSERT_TRUE(annot);
2420
2421 // Checks for Combobox with Values (/V) object which is just a string.
2422 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2423 ASSERT_EQ(26, count);
2424 for (int i = 0; i < count; i++) {
2425 EXPECT_EQ(i == 1,
2426 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2427 }
2428
2429 // Checks for index outside bound i.e. (index >= CountOption()).
2430 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2431 /*index=*/26));
2432 // Checks for negetive index.
2433 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2434 /*index=*/-1));
2435
2436 // Checks for bad form handle/annot.
2437 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2438 EXPECT_FALSE(
2439 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2440 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2441 }
2442
2443 UnloadPage(page);
2444}
2445
2446TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2447 // Open a file with listbox widget annotations and load its first page.
2448 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2449 FPDF_PAGE page = LoadPage(0);
2450 ASSERT_TRUE(page);
2451
2452 {
2453 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2454 ASSERT_TRUE(annot);
2455
2456 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2457 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2458 ASSERT_EQ(3, count);
2459 for (int i = 0; i < count; i++) {
2460 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2461 }
2462
2463 annot.reset(FPDFPage_GetAnnot(page, 1));
2464 ASSERT_TRUE(annot);
2465
2466 // Checks for Listbox with Values (/V) object which is just a string.
2467 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2468 ASSERT_EQ(26, count);
2469 for (int i = 0; i < count; i++) {
2470 EXPECT_EQ(i == 1,
2471 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2472 }
2473
2474 annot.reset(FPDFPage_GetAnnot(page, 3));
2475 ASSERT_TRUE(annot);
2476
2477 // Checks for Listbox with only Selected indices (/I) object which is an
2478 // array with multiple objects.
2479 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2480 ASSERT_EQ(5, count);
2481 for (int i = 0; i < count; i++) {
2482 bool expected = (i == 1 || i == 3);
2483 EXPECT_EQ(expected,
2484 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2485 }
2486
2487 annot.reset(FPDFPage_GetAnnot(page, 4));
2488 ASSERT_TRUE(annot);
2489
2490 // Checks for Listbox with Values (/V) object which is an array with
2491 // multiple objects.
2492 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2493 ASSERT_EQ(5, count);
2494 for (int i = 0; i < count; i++) {
2495 bool expected = (i == 2 || i == 4);
2496 EXPECT_EQ(expected,
2497 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2498 }
2499
2500 annot.reset(FPDFPage_GetAnnot(page, 5));
2501 ASSERT_TRUE(annot);
2502
2503 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2504 // objects conflict with different lengths.
2505 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2506 ASSERT_EQ(5, count);
2507 for (int i = 0; i < count; i++) {
2508 bool expected = (i == 0 || i == 2);
2509 EXPECT_EQ(expected,
2510 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2511 }
2512 }
2513
2514 UnloadPage(page);
2515}
2516
2517TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2518 // Open a file with multiple form field annotations and load its first page.
2519 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2520 FPDF_PAGE page = LoadPage(0);
2521 ASSERT_TRUE(page);
2522
2523 {
2524 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2525 ASSERT_TRUE(annot);
2526
2527 // Checks for link annotation.
2528 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2529 /*index=*/0));
2530
2531 annot.reset(FPDFPage_GetAnnot(page, 3));
2532 ASSERT_TRUE(annot);
2533
2534 // Checks for text field annotation.
2535 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2536 /*index=*/0));
2537 }
2538
2539 UnloadPage(page);
2540}
2541
Ryan Smith09c23b12019-04-25 18:09:06 +00002542TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2543 // Open a file with combobox annotations and load its first page.
2544 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2545 FPDF_PAGE page = LoadPage(0);
2546 ASSERT_TRUE(page);
2547
2548 {
2549 // All 3 widgets have Tf font size 12.
2550 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2551 ASSERT_TRUE(annot);
2552
2553 float value;
2554 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2555 EXPECT_EQ(12.0, value);
2556
2557 annot.reset(FPDFPage_GetAnnot(page, 1));
2558 ASSERT_TRUE(annot);
2559
2560 float value_two;
2561 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2562 EXPECT_EQ(12.0, value_two);
2563
2564 annot.reset(FPDFPage_GetAnnot(page, 2));
2565 ASSERT_TRUE(annot);
2566
2567 float value_three;
2568 ASSERT_TRUE(
2569 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2570 EXPECT_EQ(12.0, value_three);
2571 }
2572
2573 UnloadPage(page);
2574}
2575
2576TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2577 // Open a file with textfield annotations and load its first page.
2578 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2579 FPDF_PAGE page = LoadPage(0);
2580 ASSERT_TRUE(page);
2581
2582 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002583 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002584 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2585 ASSERT_TRUE(annot);
2586
2587 float value;
2588 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2589 EXPECT_EQ(12.0, value);
2590
2591 annot.reset(FPDFPage_GetAnnot(page, 1));
2592 ASSERT_TRUE(annot);
2593
2594 float value_two;
2595 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2596 EXPECT_EQ(12.0, value_two);
2597
2598 annot.reset(FPDFPage_GetAnnot(page, 2));
2599 ASSERT_TRUE(annot);
2600
2601 float value_three;
2602 ASSERT_TRUE(
2603 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2604 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002605
2606 float value_four;
2607 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2608 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002609 }
2610
2611 UnloadPage(page);
2612}
2613
2614TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2615 // Open a file with ink annotations and load its first page.
2616 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2617 FPDF_PAGE page = LoadPage(0);
2618 ASSERT_TRUE(page);
2619
2620 {
2621 // Annotations that do not have variable text and will return -1.
2622 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2623 ASSERT_TRUE(annot);
2624
2625 float value;
2626 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2627
2628 annot.reset(FPDFPage_GetAnnot(page, 1));
2629 ASSERT_TRUE(annot);
2630
2631 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2632 }
2633
2634 UnloadPage(page);
2635}
2636
2637TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2638 // Open a file with combobox annotations and load its first page.
2639 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2640 FPDF_PAGE page = LoadPage(0);
2641 ASSERT_TRUE(page);
2642
2643 {
2644 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2645 ASSERT_TRUE(annot);
2646
2647 // Check bad form handle / annot.
2648 float value;
2649 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2650 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2651 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2652 }
2653
2654 UnloadPage(page);
2655}
2656
2657TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2658 // Open a file with textfield annotations and load its first page.
2659 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2660 FPDF_PAGE page = LoadPage(0);
2661 ASSERT_TRUE(page);
2662
2663 {
2664 // Obtain the first annotation, a text field with negative font size, -12.
2665 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2666 ASSERT_TRUE(annot);
2667
2668 float value;
2669 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2670 EXPECT_EQ(-12.0, value);
2671 }
2672
2673 UnloadPage(page);
2674}
Ryan Smith23fdf892019-07-17 21:51:26 +00002675
2676TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2677 // Open a file with checkbox and radiobuttons widget annotations and load its
2678 // first page.
2679 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2680 FPDF_PAGE page = LoadPage(0);
2681 ASSERT_TRUE(page);
2682
2683 {
2684 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2685 ASSERT_TRUE(annot);
2686 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2687 }
2688
2689 UnloadPage(page);
2690}
2691
2692TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2693 // Open a file with checkbox and radiobutton widget annotations and load its
2694 // first page.
2695 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2696 FPDF_PAGE page = LoadPage(0);
2697 ASSERT_TRUE(page);
2698
2699 {
2700 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2701 ASSERT_TRUE(annot);
2702 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2703 }
2704
2705 UnloadPage(page);
2706}
2707
2708TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2709 // Open a file with checkbox and radiobutton widget annotations and load its
2710 // first page.
2711 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2712 FPDF_PAGE page = LoadPage(0);
2713 ASSERT_TRUE(page);
2714
2715 {
2716 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2717 ASSERT_TRUE(annot);
2718 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2719
2720 annot.reset(FPDFPage_GetAnnot(page, 6));
2721 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2722
2723 annot.reset(FPDFPage_GetAnnot(page, 7));
2724 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2725 }
2726
2727 UnloadPage(page);
2728}
2729
2730TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2731 // Open a file with checkbox and radiobutton widget annotations and load its
2732 // first page.
2733 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2734 FPDF_PAGE page = LoadPage(0);
2735 ASSERT_TRUE(page);
2736
2737 {
2738 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2739 ASSERT_TRUE(annot);
2740 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2741
2742 annot.reset(FPDFPage_GetAnnot(page, 3));
2743 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2744
2745 annot.reset(FPDFPage_GetAnnot(page, 4));
2746 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2747 }
2748
2749 UnloadPage(page);
2750}
2751
2752TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2753 // Open a file with checkbox and radiobuttons widget annotations and load its
2754 // first page.
2755 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2756 FPDF_PAGE page = LoadPage(0);
2757 ASSERT_TRUE(page);
2758
2759 {
2760 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2761 ASSERT_TRUE(annot);
2762 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2763 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2764 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2765 }
2766
2767 UnloadPage(page);
2768}
2769
2770TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2771 // Open a file with text widget annotations and load its first page.
2772 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2773 FPDF_PAGE page = LoadPage(0);
2774 ASSERT_TRUE(page);
2775
2776 {
2777 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2778 ASSERT_TRUE(annot);
2779 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2780 }
2781
2782 UnloadPage(page);
2783}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002784
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002785TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2786 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002787 FPDF_PAGE page = LoadPage(0);
2788 ASSERT_TRUE(page);
2789
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002790 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2791
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002792 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002793 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002794 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002795 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002796 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002797
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002798 constexpr int kExpectedAnnotTypes[] = {-1,
2799 FPDF_FORMFIELD_COMBOBOX,
2800 FPDF_FORMFIELD_LISTBOX,
2801 FPDF_FORMFIELD_TEXTFIELD,
2802 FPDF_FORMFIELD_CHECKBOX,
2803 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002804
Lei Zhang3111d402022-04-18 22:24:07 +00002805 for (size_t i = 0; i < std::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002806 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002807 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002808 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002809 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2810 }
2811 UnloadPage(page);
2812}
2813
2814TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2815 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2816 FPDF_PAGE page = LoadPage(0);
2817 ASSERT_TRUE(page);
2818
2819 {
2820 EXPECT_EQ(0u,
2821 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2822
2823 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2824 ASSERT_TRUE(annot);
2825
2826 EXPECT_EQ(0u,
2827 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2828
2829 unsigned long length_bytes =
2830 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2831 ASSERT_EQ(2u, length_bytes);
2832 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2833 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2834 buf.data(), length_bytes));
2835 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2836 }
2837 {
2838 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2839 ASSERT_TRUE(annot);
2840
2841 unsigned long length_bytes =
2842 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2843 ASSERT_EQ(18u, length_bytes);
2844 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2845 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2846 buf.data(), length_bytes));
2847 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2848 }
2849 UnloadPage(page);
2850}
2851
2852TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2853 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2854 FPDF_PAGE page = LoadPage(0);
2855 ASSERT_TRUE(page);
2856
2857 {
2858 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2859 ASSERT_TRUE(annot);
2860
2861 unsigned long length_bytes =
2862 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2863 ASSERT_EQ(2u, length_bytes);
2864 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2865 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2866 buf.data(), length_bytes));
2867 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2868 }
2869 {
2870 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2871 ASSERT_TRUE(annot);
2872
2873 unsigned long length_bytes =
2874 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2875 ASSERT_EQ(14u, length_bytes);
2876 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2877 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2878 buf.data(), length_bytes));
2879 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2880 }
2881 UnloadPage(page);
2882}
2883
2884TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2885 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2886 FPDF_PAGE page = LoadPage(0);
2887 ASSERT_TRUE(page);
2888
2889 {
2890 EXPECT_EQ(0u,
2891 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2892
2893 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2894 ASSERT_TRUE(annot);
2895
2896 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2897
2898 unsigned long length_bytes =
2899 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2900 ASSERT_EQ(18u, length_bytes);
2901 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2902 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2903 buf.data(), length_bytes));
2904 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2905 }
2906 UnloadPage(page);
2907}
2908
2909TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2910 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2911 FPDF_PAGE page = LoadPage(0);
2912 ASSERT_TRUE(page);
2913
2914 {
2915 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2916 ASSERT_TRUE(annot);
2917
2918 unsigned long length_bytes =
2919 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2920 ASSERT_EQ(30u, length_bytes);
2921 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2922 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2923 buf.data(), length_bytes));
2924 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2925 }
2926 UnloadPage(page);
2927}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002928
Lei Zhang9b444002020-04-17 17:35:23 +00002929TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2930 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002931 FPDF_PAGE page = LoadPage(0);
2932 ASSERT_TRUE(page);
2933
Lei Zhang9b444002020-04-17 17:35:23 +00002934 // Verify widgets are by default focusable.
2935 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2936 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002937
Lei Zhang9b444002020-04-17 17:35:23 +00002938 // Expected annot subtypes for page 0 of annots.pdf.
2939 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2940 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2941 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2942 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2943 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002944
Lei Zhang9b444002020-04-17 17:35:23 +00002945 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002946 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002947 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2948 kExpectedAnnotSubtypes,
2949 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002950
Lei Zhang9b444002020-04-17 17:35:23 +00002951 // Make no annotation type focusable using the preferred method.
2952 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2953 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002954
Lei Zhang9b444002020-04-17 17:35:23 +00002955 // Restore the focusable type count back to 1, then set it back to 0 using a
2956 // different method.
2957 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2958 ASSERT_TRUE(
2959 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2960 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002961
Lei Zhang9b444002020-04-17 17:35:23 +00002962 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002963 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002964
Lei Zhang9b444002020-04-17 17:35:23 +00002965 // Now make links focusable.
2966 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2967 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002968
Lei Zhang9b444002020-04-17 17:35:23 +00002969 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002970 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002971 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2972 kExpectedAnnotSubtypes,
2973 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002974
Lei Zhang9b444002020-04-17 17:35:23 +00002975 // Test invalid parameters.
2976 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang3111d402022-04-18 22:24:07 +00002977 std::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002978 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang3111d402022-04-18 22:24:07 +00002979 std::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002980 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2981
Lei Zhang9b444002020-04-17 17:35:23 +00002982 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2983 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2984 subtypes.size()));
2985 EXPECT_FALSE(
2986 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2987 EXPECT_FALSE(
2988 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002989
2990 UnloadPage(page);
2991}
Lei Zhang671aece2020-04-14 19:02:26 +00002992
Hui Yingstea3816d2020-07-28 22:35:11 +00002993TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00002994 ASSERT_TRUE(OpenDocument("annots.pdf"));
2995 FPDF_PAGE page = LoadPage(0);
2996 ASSERT_TRUE(page);
2997
2998 {
Hui Yingstea3816d2020-07-28 22:35:11 +00002999#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003000 static const char kMd5sum[] = "b4c8f1dab175508810c476d078ebc5a6";
Lei Zhang42d30c22022-01-12 19:24:43 +00003001#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003002 static const char kMd5sum[] = "108a46c517c4eaace9982ee83e8e3296";
Lei Zhang671aece2020-04-14 19:02:26 +00003003#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003004 static const char kMd5sum[] = "5550d8dcb4d1af1f50e8b4bcaef2ee60";
Lei Zhang671aece2020-04-14 19:02:26 +00003005#endif
3006 // Check the initial rendering.
3007 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3008 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3009 }
3010
3011 // Make links and highlights focusable.
3012 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
3013 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang3111d402022-04-18 22:24:07 +00003014 constexpr int kSubTypesCount = std::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00003015 ASSERT_TRUE(
3016 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
3017 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3018 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
3019 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
3020 subtypes.size()));
3021 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
3022 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
3023
3024 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003025#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003026 static const char kMd5sum[] = "9173db3a892bc1697eef5cdaed19eda6";
Lei Zhang42d30c22022-01-12 19:24:43 +00003027#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003028 static const char kMd5sum[] = "eb3869335e7a219e1b5f25c1c6037b97";
Lei Zhang671aece2020-04-14 19:02:26 +00003029#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003030 static const char kMd5sum[] = "805fe7bb751ac4ed2b82bb66efe6db40";
Lei Zhang671aece2020-04-14 19:02:26 +00003031#endif
3032 // Focus the first link and check the rendering.
3033 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3034 ASSERT_TRUE(annot);
3035 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3036 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3037 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3038 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3039 }
3040
3041 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003042#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003043 static const char kMd5sum[] = "174dbdb218c2b14011c9c1db67fe41c3";
Lei Zhang42d30c22022-01-12 19:24:43 +00003044#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003045 static const char kMd5sum[] = "d20b1978da2362d3942ea0fc6d230997";
Lei Zhang671aece2020-04-14 19:02:26 +00003046#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003047 static const char kMd5sum[] = "c5c5dcb462af3ef5f43b298ec048feef";
Lei Zhang671aece2020-04-14 19:02:26 +00003048#endif
3049 // Focus the first highlight and check the rendering.
3050 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3051 ASSERT_TRUE(annot);
3052 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3053 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3054 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3055 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3056 }
3057
3058 UnloadPage(page);
3059}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003060
3061TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
3062 ASSERT_TRUE(OpenDocument("annots.pdf"));
3063 FPDF_PAGE page = LoadPage(0);
3064 ASSERT_TRUE(page);
3065 {
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003066 constexpr char kExpectedResult[] =
3067 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3068 "fpdf_text.h";
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003069
Lei Zhang306874b2021-04-16 00:33:36 +00003070 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3071 ASSERT_TRUE(annot);
3072 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3073 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()),
3074 kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003075 }
3076
3077 {
3078 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3079 ASSERT_TRUE(annot);
3080 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3081 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3082 }
3083
3084 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3085
3086 UnloadPage(page);
3087}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003088
3089TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3090 // Open a file with radio button widget annotations and load its first page.
3091 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3092 FPDF_PAGE page = LoadPage(0);
3093 ASSERT_TRUE(page);
3094
3095 {
3096 // Checks for bad annot.
3097 EXPECT_EQ(-1,
3098 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3099
3100 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3101 ASSERT_TRUE(annot);
3102
3103 // Checks for bad form handle.
3104 EXPECT_EQ(-1,
3105 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3106
3107 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3108 }
3109
3110 UnloadPage(page);
3111}
3112
3113TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3114 // Open a file with checkbox widget annotations and load its first page.
3115 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3116 FPDF_PAGE page = LoadPage(0);
3117 ASSERT_TRUE(page);
3118
3119 {
3120 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3121 ASSERT_TRUE(annot);
3122 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3123 }
3124
3125 UnloadPage(page);
3126}
3127
3128TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3129 // Open a file with ink annotations and load its first page.
3130 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3131 FPDF_PAGE page = LoadPage(0);
3132 ASSERT_TRUE(page);
3133
3134 {
3135 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3136 ASSERT_TRUE(annot);
3137 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3138 }
3139
3140 UnloadPage(page);
3141}
3142
3143TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3144 // Open a file with radio button widget annotations and load its first page.
3145 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3146 FPDF_PAGE page = LoadPage(0);
3147 ASSERT_TRUE(page);
3148
3149 {
3150 // Checks for bad annot.
3151 EXPECT_EQ(-1,
3152 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3153
3154 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3155 ASSERT_TRUE(annot);
3156
3157 // Checks for bad form handle.
3158 EXPECT_EQ(-1,
3159 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3160
3161 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3162 }
3163
3164 UnloadPage(page);
3165}
3166
3167TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3168 // Open a file with checkbox widget annotations and load its first page.
3169 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3170 FPDF_PAGE page = LoadPage(0);
3171 ASSERT_TRUE(page);
3172
3173 {
3174 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3175 ASSERT_TRUE(annot);
3176 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3177 }
3178
3179 UnloadPage(page);
3180}
3181
3182TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3183 // Open a file with ink annotations and load its first page.
3184 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3185 FPDF_PAGE page = LoadPage(0);
3186 ASSERT_TRUE(page);
3187
3188 {
3189 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3190 ASSERT_TRUE(annot);
3191 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3192 }
3193
3194 UnloadPage(page);
3195}
3196
3197TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3198 // Open a file with radio button widget annotations and load its first page.
3199 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3200 FPDF_PAGE page = LoadPage(0);
3201 ASSERT_TRUE(page);
3202
3203 {
3204 // Checks for bad annot.
3205 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3206 form_handle(), /*annot=*/nullptr,
3207 /*buffer=*/nullptr, /*buflen=*/0));
3208
3209 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3210 ASSERT_TRUE(annot);
3211
3212 // Checks for bad form handle.
3213 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3214 /*hHandle=*/nullptr, annot.get(),
3215 /*buffer=*/nullptr, /*buflen=*/0));
3216
3217 unsigned long length_bytes =
3218 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3219 /*buffer=*/nullptr, /*buflen=*/0);
3220 ASSERT_EQ(14u, length_bytes);
3221 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3222 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3223 buf.data(), length_bytes));
3224 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3225 }
3226
3227 UnloadPage(page);
3228}
3229
3230TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3231 // Open a file with checkbox widget annotations and load its first page.
3232 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3233 FPDF_PAGE page = LoadPage(0);
3234 ASSERT_TRUE(page);
3235
3236 {
3237 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3238 ASSERT_TRUE(annot);
3239
3240 unsigned long length_bytes =
3241 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3242 /*buffer=*/nullptr, /*buflen=*/0);
3243 ASSERT_EQ(8u, length_bytes);
3244 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3245 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3246 buf.data(), length_bytes));
3247 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3248 }
3249
3250 UnloadPage(page);
3251}
3252
3253TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3254 // Open a file with ink annotations and load its first page.
3255 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3256 FPDF_PAGE page = LoadPage(0);
3257 ASSERT_TRUE(page);
3258
3259 {
3260 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3261 ASSERT_TRUE(annot);
3262 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3263 /*buffer=*/nullptr,
3264 /*buflen=*/0));
3265 }
3266
3267 UnloadPage(page);
3268}
Miklos Vajnad1a24fb2020-10-26 18:07:13 +00003269
3270TEST_F(FPDFAnnotEmbedderTest, Redactannotation) {
3271 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3272 FPDF_PAGE page = LoadPage(0);
3273 ASSERT_TRUE(page);
3274 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3275
3276 {
3277 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3278 ASSERT_TRUE(annot);
3279 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3280 }
3281
3282 UnloadPage(page);
3283}
Miklos Vajna09ecef62020-11-10 21:50:38 +00003284
3285TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) {
3286 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3287 FPDF_PAGE page = LoadPage(0);
3288 ASSERT_TRUE(page);
3289 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3290
3291 {
3292 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3293 ASSERT_TRUE(annot);
3294
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003295 // FPDFAnnot_GetVertices() positive testing.
Miklos Vajna09ecef62020-11-10 21:50:38 +00003296 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3297 const size_t kExpectedSize = 3;
3298 ASSERT_EQ(kExpectedSize, size);
3299 std::vector<FS_POINTF> vertices_buffer(size);
3300 EXPECT_EQ(size,
3301 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3302 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3303 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3304 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3305 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3306 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3307 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3308
3309 // FPDFAnnot_GetVertices() negative testing.
3310 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3311
3312 // vertices_buffer is not overwritten if it is too small.
3313 vertices_buffer.resize(1);
3314 vertices_buffer[0].x = 42;
3315 vertices_buffer[0].y = 43;
3316 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3317 vertices_buffer.size());
3318 EXPECT_EQ(kExpectedSize, size);
3319 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3320 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3321 }
3322
3323 {
3324 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3325 ASSERT_TRUE(annot);
3326
3327 // This has an odd number of elements in the vertices array, ignore the last
3328 // element.
3329 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3330 const size_t kExpectedSize = 3;
3331 ASSERT_EQ(kExpectedSize, size);
3332 std::vector<FS_POINTF> vertices_buffer(size);
3333 EXPECT_EQ(size,
3334 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3335 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3336 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3337 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3338 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3339 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3340 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3341 }
3342
3343 {
3344 // Wrong annotation type.
3345 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3346 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3347 }
3348
3349 UnloadPage(page);
3350}
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003351
3352TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) {
3353 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3354 FPDF_PAGE page = LoadPage(0);
3355 ASSERT_TRUE(page);
3356 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3357
3358 {
3359 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3360 ASSERT_TRUE(annot);
3361
3362 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3363 // testing.
3364 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3365 const size_t kExpectedSize = 1;
3366 ASSERT_EQ(kExpectedSize, size);
3367 const unsigned long kPathIndex = 0;
3368 unsigned long path_size =
3369 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3370 const size_t kExpectedPathSize = 3;
3371 ASSERT_EQ(kExpectedPathSize, path_size);
3372 std::vector<FS_POINTF> path_buffer(path_size);
3373 EXPECT_EQ(path_size,
3374 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3375 path_buffer.data(), path_size));
3376 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3377 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3378 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3379 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3380 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3381 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3382
3383 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3384 // testing.
3385 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3386 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3387
3388 // out of bounds path_index.
3389 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3390
3391 // path_buffer is not overwritten if it is too small.
3392 path_buffer.resize(1);
3393 path_buffer[0].x = 42;
3394 path_buffer[0].y = 43;
3395 path_size = FPDFAnnot_GetInkListPath(
3396 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3397 EXPECT_EQ(kExpectedSize, size);
3398 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3399 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3400 }
3401
3402 {
3403 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3404 ASSERT_TRUE(annot);
3405
3406 // This has an odd number of elements in the path array, ignore the last
3407 // element.
3408 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3409 const size_t kExpectedSize = 1;
3410 ASSERT_EQ(kExpectedSize, size);
3411 const unsigned long kPathIndex = 0;
3412 unsigned long path_size =
3413 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3414 const size_t kExpectedPathSize = 3;
3415 ASSERT_EQ(kExpectedPathSize, path_size);
3416 std::vector<FS_POINTF> path_buffer(path_size);
3417 EXPECT_EQ(path_size,
3418 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3419 path_buffer.data(), path_size));
3420 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3421 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3422 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3423 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3424 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3425 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3426 }
3427
3428 {
3429 // Wrong annotation type.
3430 ScopedFPDFAnnotation polygon_annot(
3431 FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON));
3432 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3433 const unsigned long kPathIndex = 0;
3434 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3435 nullptr, 0));
3436 }
3437
3438 UnloadPage(page);
3439}
Miklos Vajna30f45a62020-12-04 19:12:31 +00003440
3441TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) {
3442 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3443 FPDF_PAGE page = LoadPage(0);
3444 ASSERT_TRUE(page);
3445 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3446
3447 {
3448 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3449 ASSERT_TRUE(annot);
3450
3451 // FPDFAnnot_GetVertices() positive testing.
3452 FS_POINTF start;
3453 FS_POINTF end;
3454 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3455 EXPECT_FLOAT_EQ(159.0f, start.x);
3456 EXPECT_FLOAT_EQ(296.0f, start.y);
3457 EXPECT_FLOAT_EQ(472.0f, end.x);
3458 EXPECT_FLOAT_EQ(243.42f, end.y);
3459
3460 // FPDFAnnot_GetVertices() negative testing.
3461 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3462 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3463 }
3464
3465 {
3466 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3467 ASSERT_TRUE(annot);
3468
3469 // Too few elements in the line array.
3470 FS_POINTF start;
3471 FS_POINTF end;
3472 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3473 }
3474
3475 {
3476 // Wrong annotation type.
3477 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3478 FS_POINTF start;
3479 FS_POINTF end;
3480 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3481 }
3482
3483 UnloadPage(page);
3484}
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003485
3486TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) {
3487 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3488 FPDF_PAGE page = LoadPage(0);
3489 ASSERT_TRUE(page);
3490 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3491
3492 {
3493 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3494 ASSERT_TRUE(annot);
3495
3496 // FPDFAnnot_GetBorder() positive testing.
3497 float horizontal_radius;
3498 float vertical_radius;
3499 float border_width;
3500 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3501 &vertical_radius, &border_width));
3502 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3503 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3504 EXPECT_FLOAT_EQ(2.0f, border_width);
3505
3506 // FPDFAnnot_GetBorder() negative testing.
3507 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3508 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3509 }
3510
3511 {
3512 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3513 ASSERT_TRUE(annot);
3514
3515 // Too few elements in the border array.
3516 float horizontal_radius;
3517 float vertical_radius;
3518 float border_width;
3519 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3520 &vertical_radius, &border_width));
Lei Zhang21ea6652021-04-15 23:24:46 +00003521
3522 // FPDFAnnot_SetBorder() positive testing.
3523 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3524 /*vertical_radius=*/3.5f,
3525 /*border_width=*/4.0f));
3526
3527 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3528 &vertical_radius, &border_width));
3529 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3530 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3531 EXPECT_FLOAT_EQ(4.0f, border_width);
3532
3533 // FPDFAnnot_SetBorder() negative testing.
3534 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3535 /*vertical_radius=*/2.5f,
3536 /*border_width=*/3.0f));
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003537 }
3538
3539 UnloadPage(page);
3540}
Lei Zhang24de1492021-04-19 18:06:43 +00003541
3542// Due to https://crbug.com/pdfium/570, the AnnotationBorder test above cannot
3543// actually render the line annotations inside line_annot.pdf. For now, use a
3544// square annotation in annots.pdf for testing.
3545TEST_F(FPDFAnnotEmbedderTest, AnnotationBorderRendering) {
3546 ASSERT_TRUE(OpenDocument("annots.pdf"));
3547 FPDF_PAGE page = LoadPage(1);
3548 ASSERT_TRUE(page);
3549 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
3550
3551#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003552 constexpr char kOriginalChecksum[] = "4f35703e89202bcc8419ca2df739bb4e";
3553 constexpr char kModifiedChecksum[] = "cee0a1b41f33d487af8fb70c4c82e3c9";
Lei Zhang42d30c22022-01-12 19:24:43 +00003554#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003555 constexpr char kOriginalChecksum[] = "522a4a6b6c7eab5bf95ded1f21ea372e";
3556 constexpr char kModifiedChecksum[] = "6844019e07b83cc01723415f58218d06";
Lei Zhang24de1492021-04-19 18:06:43 +00003557#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003558 constexpr char kOriginalChecksum[] = "12127303aecd80c6288460f7c0d79f3f";
3559 constexpr char kModifiedChecksum[] = "73d06ff4c665fe85029acef30240dcca";
Lei Zhang24de1492021-04-19 18:06:43 +00003560#endif
3561
3562 {
3563 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
3564 ASSERT_TRUE(annot);
3565 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
3566
3567 {
3568 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3569 CompareBitmap(bitmap.get(), 612, 792, kOriginalChecksum);
3570 }
3571
3572 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3573 /*vertical_radius=*/3.5f,
3574 /*border_width=*/4.0f));
3575
3576 {
3577 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3578 CompareBitmap(bitmap.get(), 612, 792, kModifiedChecksum);
3579 }
3580 }
3581
3582 // Save the document and close the page.
3583 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
3584 UnloadPage(page);
3585
3586 ASSERT_TRUE(OpenSavedDocument());
3587 page = LoadSavedPage(1);
3588 ASSERT_TRUE(page);
3589 VerifySavedRendering(page, 612, 792, kModifiedChecksum);
3590
3591 CloseSavedPage(page);
3592 CloseSavedDocument();
3593}