blob: 6232027470a29d75f282c27b0b3111f2a3d0638b [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 Zhang430b5322020-07-06 22:23:36 +0000545#else
546 static constexpr char kExpectedHash[] = "354002e1c4386d38fdde29ef8d61074a";
Hui Yingst57daee82020-10-09 05:47:20 +0000547#endif
Tom Sepezef43c262018-11-07 16:41:32 +0000548 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang430b5322020-07-06 22:23:36 +0000549 CompareBitmap(bitmap.get(), 612, 792, kExpectedHash);
Tom Sepezef43c262018-11-07 16:41:32 +0000550 }
Tom Sepez507d0192018-11-07 16:37:51 +0000551 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400552}
Jane Liu20eafda2017-06-07 10:33:24 -0400553
Lei Zhangab41f252018-12-23 03:10:50 +0000554TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400555 // Open a file with one annotation and load its first page.
556 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000557 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400558 ASSERT_TRUE(page);
559
560 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400561 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400562
563 UnloadPage(page);
564}
565
Lei Zhangab41f252018-12-23 03:10:50 +0000566TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400567 // Open a file with no annotation and load its first page.
568 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000569 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400570 ASSERT_TRUE(page);
571 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
572
Lei Zhanga21d5932018-02-05 18:28:38 +0000573 {
574 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000575 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000576 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400577
Lei Zhanga21d5932018-02-05 18:28:38 +0000578 // Check that there is now 1 annotations on this page.
579 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400580
Lei Zhanga21d5932018-02-05 18:28:38 +0000581 // Check that the subtype of the annotation is correct.
582 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
583 }
Jane Liue10509a2017-06-20 16:47:41 -0400584
Lei Zhanga21d5932018-02-05 18:28:38 +0000585 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000586 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000587 ASSERT_TRUE(annot);
588 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400589
Lei Zhanga21d5932018-02-05 18:28:38 +0000590 // Set the color of the annotation.
591 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
592 102, 153, 204));
593 // Check that the color has been set correctly.
594 unsigned int R;
595 unsigned int G;
596 unsigned int B;
597 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000598 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000599 &G, &B, &A));
600 EXPECT_EQ(51u, R);
601 EXPECT_EQ(102u, G);
602 EXPECT_EQ(153u, B);
603 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400604
Lei Zhanga21d5932018-02-05 18:28:38 +0000605 // Change the color of the annotation.
606 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
607 153, 102, 51));
608 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000609 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000610 &G, &B, &A));
611 EXPECT_EQ(204u, R);
612 EXPECT_EQ(153u, G);
613 EXPECT_EQ(102u, B);
614 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400615
Lei Zhanga21d5932018-02-05 18:28:38 +0000616 // Set the annotation rectangle.
617 FS_RECTF rect;
618 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
619 EXPECT_EQ(0.f, rect.left);
620 EXPECT_EQ(0.f, rect.right);
621 rect.left = 35;
622 rect.bottom = 150;
623 rect.right = 53;
624 rect.top = 165;
625 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
626 // Check that the annotation rectangle has been set correctly.
627 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
628 EXPECT_EQ(35.f, rect.left);
629 EXPECT_EQ(150.f, rect.bottom);
630 EXPECT_EQ(53.f, rect.right);
631 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400632
Lei Zhanga21d5932018-02-05 18:28:38 +0000633 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000634 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000635 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000636 ASSERT_TRUE(FPDFAnnot_SetStringValue(
637 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000638 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000639 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000640 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000641 ASSERT_EQ(74u, length_bytes);
642 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
643 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
644 pdfium::annotation::kContents,
645 buf.data(), length_bytes));
646 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000647 }
Jane Liu20eafda2017-06-07 10:33:24 -0400648 UnloadPage(page);
649}
650
Lei Zhang81395aa2021-04-16 00:40:46 +0000651TEST_F(FPDFAnnotEmbedderTest, AddAndSaveLinkAnnotation) {
652 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
653 FPDF_PAGE page = LoadPage(0);
654 ASSERT_TRUE(page);
655 {
656 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
657 CompareBitmap(bitmap.get(), 200, 200, pdfium::kHelloWorldChecksum);
658 }
659 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
660
661 constexpr char kUri[] = "https://pdfium.org/";
662
663 {
664 // Add a link annotation to the page and set its URI.
665 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_LINK));
666 ASSERT_TRUE(annot);
667 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
668 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
669 EXPECT_TRUE(FPDFAnnot_SetURI(annot.get(), kUri));
670 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
671
672 // Negative tests:
673 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, nullptr));
674 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
675 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), nullptr));
676 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
677 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, kUri));
678 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
679
680 // Position the link on top of "Hello, world!" without a border.
681 const FS_RECTF kRect = {19.0f, 48.0f, 85.0f, 60.0f};
682 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &kRect));
683 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/0.0f,
684 /*vertical_radius=*/0.0f,
685 /*border_width=*/0.0f));
686
687 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
688 kUri);
689 }
690
691 {
692 // Add an ink annotation to the page. Trying to add a link to it fails.
693 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
694 ASSERT_TRUE(annot);
695 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
696 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
697 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), kUri));
698 }
699
700 // Remove the ink annotation added above for negative testing.
701 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
702 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
703
704 // Save the document, closing the page.
705 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
706 UnloadPage(page);
707
708 // Reopen the document and make sure it still renders the same. Since the link
709 // does not have a border, it does not affect the rendering.
710 ASSERT_TRUE(OpenSavedDocument());
711 page = LoadSavedPage(0);
712 ASSERT_TRUE(page);
713 VerifySavedRendering(page, 200, 200, pdfium::kHelloWorldChecksum);
714 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
715
716 {
717 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
718 ASSERT_TRUE(annot);
719 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
720 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
721 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
722 kUri);
723 }
724
725 CloseSavedPage(page);
726 CloseSavedDocument();
727}
728
Hui Yingstb3490322020-07-22 00:53:29 +0000729TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400730 // Open a file with one annotation and load its first page.
731 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000732 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400733 ASSERT_TRUE(page);
734
735 // Check that there is a total of one annotation on its first page, and verify
736 // its quadpoints.
737 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400738 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000739 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000740 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000741 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000742 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000743 EXPECT_EQ(115.802643f, quadpoints.x1);
744 EXPECT_EQ(718.913940f, quadpoints.y1);
745 EXPECT_EQ(157.211182f, quadpoints.x4);
746 EXPECT_EQ(706.264465f, quadpoints.y4);
747 }
Jane Liu20eafda2017-06-07 10:33:24 -0400748
749 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000750 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000751 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000752 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
753 ASSERT_TRUE(annot);
754 quadpoints.x1 = 140.802643f;
755 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000756 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000757 }
Jane Liu20eafda2017-06-07 10:33:24 -0400758
Lei Zhangec618142021-04-13 17:36:46 +0000759 // Save the document and close the page.
Jane Liu20eafda2017-06-07 10:33:24 -0400760 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000761 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400762
763 // Open the saved document.
Hui Yingste1215fc2022-03-26 22:29:08 +0000764#if defined(_SKIA_SUPPORT_)
Hui Yingst57daee82020-10-09 05:47:20 +0000765 static const char kChecksum[] = "899387ae792390cd0d83cf7e2bbebfb5";
Hui Yingste1215fc2022-03-26 22:29:08 +0000766#elif defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst57daee82020-10-09 05:47:20 +0000767 static const char kChecksum[] = "e40e235ee35f47ff28dda009aaaf36df";
Hui Yingstb3490322020-07-22 00:53:29 +0000768#else
769 static const char kChecksum[] = "dba153419f67b7c0c0e3d22d3e8910d5";
770#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400771
Lei Zhang0b494052019-01-31 21:41:15 +0000772 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000773 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +0000774 ASSERT_TRUE(page);
Hui Yingstb3490322020-07-22 00:53:29 +0000775 VerifySavedRendering(page, 612, 792, kChecksum);
Jane Liu20eafda2017-06-07 10:33:24 -0400776
777 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000778 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400779
Lei Zhanga21d5932018-02-05 18:28:38 +0000780 {
781 // Check that the second annotation is an underline annotation and verify
782 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000783 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000784 ASSERT_TRUE(new_annot);
785 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
786 FS_QUADPOINTSF new_quadpoints;
787 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000788 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000789 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
790 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
791 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
792 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
793 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400794
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000795 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400796 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400797}
Jane Liu06462752017-06-27 16:41:14 -0400798
Lei Zhangab41f252018-12-23 03:10:50 +0000799TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000800 // Open a file with four annotations and load its first page.
801 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
802 FPDF_PAGE page = LoadPage(0);
803 ASSERT_TRUE(page);
804 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
805
806 // Retrieve the highlight annotation.
807 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
808 ASSERT_TRUE(annot);
809 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
810
811 FS_QUADPOINTSF quadpoints;
812 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
813
814 {
815 // Verify the current one set of quadpoints.
816 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
817
818 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
819 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
820 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
821 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
822 }
823
824 {
825 // Update the quadpoints.
826 FS_QUADPOINTSF new_quadpoints = quadpoints;
827 new_quadpoints.y1 -= 20.f;
828 new_quadpoints.y2 -= 20.f;
829 new_quadpoints.y3 -= 20.f;
830 new_quadpoints.y4 -= 20.f;
831 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
832
833 // Verify added quadpoint set
834 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
835 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
836 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
837 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
838 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
839 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
840 }
841
842 {
843 // Append a new set of quadpoints.
844 FS_QUADPOINTSF new_quadpoints = quadpoints;
845 new_quadpoints.y1 += 20.f;
846 new_quadpoints.y2 += 20.f;
847 new_quadpoints.y3 += 20.f;
848 new_quadpoints.y4 += 20.f;
849 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
850
851 // Verify added quadpoint set
852 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
853 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
854 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
855 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
856 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
857 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
858 }
859
860 {
861 // Setting and getting quadpoints at out-of-bound index should fail
862 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
863 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
864 }
865
866 FPDFPage_CloseAnnot(annot);
867
868 // Retrieve the square annotation
869 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
870
871 {
872 // Check that attempting to set its quadpoints would fail
873 ASSERT_TRUE(squareAnnot);
874 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
875 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
876 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
877 }
878
879 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000880 UnloadPage(page);
881}
882
Hui Yingstb64cd122020-07-27 16:01:02 +0000883// TODO(crbug.com/pdfium/1569): Fix this issue and enable the test for Skia.
884#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +0000885#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
886#else
887#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
888#endif
889TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Hui Yingstb64cd122020-07-27 16:01:02 +0000890#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingste1215fc2022-03-26 22:29:08 +0000891 static const char kMd5Original[] = "127c2d3b4452555e3317827b0dbbb6a0";
Lei Zhang4f556b82019-04-08 16:32:41 +0000892 static const char kMd5ModifiedHighlight[] =
Hui Yingste1215fc2022-03-26 22:29:08 +0000893 "6ffe732be6f80540b60921c4803b590a";
894 static const char kMd5ModifiedSquare[] = "9ecbeea7f54abea298b53ce79d301f4a";
Hui Yingstb64cd122020-07-27 16:01:02 +0000895#else
Hui Yingste1215fc2022-03-26 22:29:08 +0000896#if BUILDFLAG(IS_APPLE)
Hui Yingstb64cd122020-07-27 16:01:02 +0000897 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
898 static const char kMd5ModifiedHighlight[] =
899 "e64bf648f6e9354d1f3eedb47a2c9498";
900 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Jane Liub370e5a2017-08-16 13:24:58 -0400901#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000902 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
903 static const char kMd5ModifiedHighlight[] =
904 "66f3caef3a7d488a4fa1ad37fc06310e";
905 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Hui Yingste1215fc2022-03-26 22:29:08 +0000906#endif // BUILDFLAG(IS_APPLE)
Hui Yingstb64cd122020-07-27 16:01:02 +0000907#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liub370e5a2017-08-16 13:24:58 -0400908
Jane Liu06462752017-06-27 16:41:14 -0400909 // Open a file with four annotations and load its first page.
910 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000911 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400912 ASSERT_TRUE(page);
913 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
914
Jane Liub370e5a2017-08-16 13:24:58 -0400915 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000916 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000917 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000918 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000919 }
Jane Liub370e5a2017-08-16 13:24:58 -0400920
Jane Liu0c6b07d2017-08-15 10:50:22 -0400921 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400922 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000923
924 // Retrieve the highlight annotation which has its AP stream already defined.
925 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000926 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000927 ASSERT_TRUE(annot);
928 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
929
930 // Check that color cannot be set when an AP stream is defined already.
931 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
932 102, 153, 204));
933
934 // Verify its attachment points.
935 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000936 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000937 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
938 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
939 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
940 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
941
942 // Check that updating the attachment points would succeed.
943 quadpoints.x1 -= 50.f;
944 quadpoints.x2 -= 50.f;
945 quadpoints.x3 -= 50.f;
946 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000947 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000948 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000949 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000950 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
951 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
952 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
953 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
954
955 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000956 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000957 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000958 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000959 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000960
961 // Verify its annotation rectangle.
962 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
963 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
964 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
965 EXPECT_NEAR(136.325f, rect.right, 0.001f);
966 EXPECT_NEAR(721.292f, rect.top, 0.001f);
967
968 // Check that updating the rectangle would succeed.
969 rect.left -= 60.f;
970 rect.right -= 60.f;
971 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
972 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
973 EXPECT_EQ(rect.right, new_rect.right);
974 }
Jane Liu06462752017-06-27 16:41:14 -0400975
Jane Liub370e5a2017-08-16 13:24:58 -0400976 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000977 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000978 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000979 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000980 }
Jane Liub370e5a2017-08-16 13:24:58 -0400981
Lei Zhanga21d5932018-02-05 18:28:38 +0000982 {
983 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000984 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000985 ASSERT_TRUE(annot);
986 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400987
Lei Zhanga21d5932018-02-05 18:28:38 +0000988 // Check that updating the rectangle would succeed.
989 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
990 rect.left += 70.f;
991 rect.right += 70.f;
992 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
993 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
994 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400995
Lei Zhanga21d5932018-02-05 18:28:38 +0000996 // Check that updating the rectangle changes the square annotation's
997 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000998 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000999 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +00001000 }
Jane Liub370e5a2017-08-16 13:24:58 -04001001
Jane Liu06462752017-06-27 16:41:14 -04001002 UnloadPage(page);
1003}
Jane Liu8ce58f52017-06-29 13:40:22 -04001004
Lei Zhangab41f252018-12-23 03:10:50 +00001005TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +00001006 // Open a file with multiline markup annotations.
1007 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
1008 FPDF_PAGE page = LoadPage(0);
1009 ASSERT_TRUE(page);
1010 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001011 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +00001012 ASSERT_TRUE(annot);
1013
1014 // This is a three line annotation.
1015 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
1016 }
1017 UnloadPage(page);
1018
1019 // null annotation should return 0
1020 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
1021}
1022
Lei Zhangab41f252018-12-23 03:10:50 +00001023TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -04001024 // Open a file with 3 annotations on its first page.
1025 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +00001026 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -04001027 ASSERT_TRUE(page);
1028 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1029
Jane Liu0c6b07d2017-08-15 10:50:22 -04001030 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -04001031
Lei Zhanga21d5932018-02-05 18:28:38 +00001032 // Check that the annotations have the expected rectangle coordinates.
1033 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001034 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001035 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1036 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1037 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001038
Lei Zhanga21d5932018-02-05 18:28:38 +00001039 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001040 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001041 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1042 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
1043 }
1044
1045 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001046 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001047 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1048 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1049 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001050
1051 // Check that nothing happens when attempting to remove an annotation with an
1052 // out-of-bound index.
1053 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
1054 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
1055 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1056
1057 // Remove the second annotation.
1058 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
1059 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1060 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
1061
Lei Zhangec618142021-04-13 17:36:46 +00001062 // Save the document and close the page.
Jane Liu8ce58f52017-06-29 13:40:22 -04001063 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +00001064 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -04001065
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001066 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -04001067 // Open the saved document.
1068 std::string new_file = GetString();
1069 FPDF_FILEACCESS file_access;
1070 memset(&file_access, 0, sizeof(file_access));
1071 file_access.m_FileLen = new_file.size();
1072 file_access.m_GetBlock = GetBlockFromString;
1073 file_access.m_Param = &new_file;
1074 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
1075 ASSERT_TRUE(new_doc);
1076 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
1077 ASSERT_TRUE(new_page);
1078
1079 // Check that the saved document has 2 annotations on the first page.
1080 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
1081
Lei Zhanga21d5932018-02-05 18:28:38 +00001082 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1083 // by verifying their rectangle coordinates.
1084 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001085 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001086 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1087 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1088 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001089
Lei Zhanga21d5932018-02-05 18:28:38 +00001090 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001091 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001092 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1093 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1094 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001095 FPDF_ClosePage(new_page);
1096 FPDF_CloseDocument(new_doc);
1097}
Jane Liu8ce58f52017-06-29 13:40:22 -04001098
Hui Yingst4a21df02020-05-13 03:15:44 +00001099TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001100#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001101 static const char kMd5ModifiedPath[] = "b820e4ae359db95cbac9823937c6da1a";
1102 static const char kMd5TwoPaths[] = "c53837b7bb6a9a21a846aa786526aa56";
1103 static const char kMd5NewAnnot[] = "4f0f4217156e4251036f369184a48967";
Lei Zhang42d30c22022-01-12 19:24:43 +00001104#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001105 static const char kMd5ModifiedPath[] = "e31421f86c61d4e9cda138f15f561ca3";
1106 static const char kMd5TwoPaths[] = "58d932492f9d485d6a4bc0ba76c04557";
1107 static const char kMd5NewAnnot[] = "61f9ad13f2fd235753db198cf9704773";
Jane Liubaa7ff42017-06-29 19:18:23 -04001108#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001109 static const char kMd5ModifiedPath[] = "980e7636d864f7f7d323a31ad4e8fa04";
1110 static const char kMd5TwoPaths[] = "4c779c394b6790f8cf80305b566b663b";
1111 static const char kMd5NewAnnot[] = "97effd68dcf86273f68d126d6b45152e";
Jane Liubaa7ff42017-06-29 19:18:23 -04001112#endif
1113
1114 // Open a file with two annotations and load its first page.
1115 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001116 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001117 ASSERT_TRUE(page);
1118 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1119
1120 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001121 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001122 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001123 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001124 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001125
Lei Zhanga21d5932018-02-05 18:28:38 +00001126 {
1127 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001128 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001129 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001130
Lei Zhanga21d5932018-02-05 18:28:38 +00001131 // Check that this annotation has one path object and retrieve it.
1132 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001133 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001134 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1135 EXPECT_FALSE(path);
1136 path = FPDFAnnot_GetObject(annot.get(), 0);
1137 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1138 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001139
Lei Zhanga21d5932018-02-05 18:28:38 +00001140 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001141 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001142 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001143
Lei Zhanga21d5932018-02-05 18:28:38 +00001144 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001145 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001146 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001147 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001148 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001149
Lei Zhanga21d5932018-02-05 18:28:38 +00001150 // Add a second path object to the same annotation.
1151 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1152 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001153 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1154 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001155 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1156 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1157 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001158
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001159 // The object is in the annontation, not in the page, so the page object
1160 // array should not change.
1161 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1162
Lei Zhanga21d5932018-02-05 18:28:38 +00001163 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001164 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001165 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001166 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001167 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001168
Lei Zhanga21d5932018-02-05 18:28:38 +00001169 // Delete the newly added path object.
1170 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1171 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001172 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001173 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001174
1175 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001176 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001177 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001178 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001179 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001180
Jane Liubaa7ff42017-06-29 19:18:23 -04001181 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001182
Lei Zhanga21d5932018-02-05 18:28:38 +00001183 {
1184 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001185 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001186 ASSERT_TRUE(annot);
1187 rect.left = 200.f;
1188 rect.bottom = 400.f;
1189 rect.right = 500.f;
1190 rect.top = 600.f;
1191 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001192
Lei Zhanga21d5932018-02-05 18:28:38 +00001193 // Add a new path to the annotation.
1194 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1195 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1196 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1197 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1198 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001199 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1200 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001201 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1202 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1203 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1204
1205 // Check that the annotation's bounding box came from its rectangle.
1206 FS_RECTF new_rect;
1207 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1208 EXPECT_EQ(rect.left, new_rect.left);
1209 EXPECT_EQ(rect.bottom, new_rect.bottom);
1210 EXPECT_EQ(rect.right, new_rect.right);
1211 EXPECT_EQ(rect.top, new_rect.top);
1212 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001213
Lei Zhangec618142021-04-13 17:36:46 +00001214 // Save the document and close the page.
Jane Liubaa7ff42017-06-29 19:18:23 -04001215 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001216 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001217
1218 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001219 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001220 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001221 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001222 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001223
Jane Liu36567742017-07-06 11:13:35 -04001224 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001225 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001226
Lei Zhanga21d5932018-02-05 18:28:38 +00001227 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001228 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001229 ASSERT_TRUE(annot);
1230 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001231
Lei Zhanga21d5932018-02-05 18:28:38 +00001232 // Check that the new annotation's rectangle is as defined.
1233 FS_RECTF new_rect;
1234 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1235 EXPECT_EQ(rect.left, new_rect.left);
1236 EXPECT_EQ(rect.bottom, new_rect.bottom);
1237 EXPECT_EQ(rect.right, new_rect.right);
1238 EXPECT_EQ(rect.top, new_rect.top);
1239 }
1240
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001241 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001242 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001243}
Jane Liub137e752017-07-05 15:04:33 -04001244
Lei Zhangab41f252018-12-23 03:10:50 +00001245TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001246 // Open a file with an annotation and load its first page.
1247 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001248 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001249 ASSERT_TRUE(page);
1250
1251 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001252 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001253 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001254 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1255 }
Jane Liub137e752017-07-05 15:04:33 -04001256
Lei Zhanga21d5932018-02-05 18:28:38 +00001257 {
1258 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001259 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001260 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001261
Lei Zhanga21d5932018-02-05 18:28:38 +00001262 // Check that the original flag values are as expected.
1263 int flags = FPDFAnnot_GetFlags(annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00001264 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_INVISIBLE);
Lei Zhanga21d5932018-02-05 18:28:38 +00001265 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1266 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001267 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOZOOM);
1268 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOROTATE);
1269 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOVIEW);
1270 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_READONLY);
1271 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_LOCKED);
1272 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_TOGGLENOVIEW);
Jane Liub137e752017-07-05 15:04:33 -04001273
Lei Zhanga21d5932018-02-05 18:28:38 +00001274 // Set the HIDDEN flag.
1275 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1276 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1277 flags = FPDFAnnot_GetFlags(annot.get());
1278 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1279 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001280
Lei Zhanga21d5932018-02-05 18:28:38 +00001281 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001282 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001283 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001284 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001285 }
Jane Liub137e752017-07-05 15:04:33 -04001286
Lei Zhanga21d5932018-02-05 18:28:38 +00001287 // Unset the HIDDEN flag.
1288 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1289 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1290 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1291 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1292 flags = FPDFAnnot_GetFlags(annot.get());
1293 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1294 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001295
Lei Zhanga21d5932018-02-05 18:28:38 +00001296 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001297 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001298 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001299 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1300 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001301 }
Jane Liub137e752017-07-05 15:04:33 -04001302
Jane Liub137e752017-07-05 15:04:33 -04001303 UnloadPage(page);
1304}
Jane Liu36567742017-07-06 11:13:35 -04001305
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001306// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1307#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001308#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1309#else
1310#define MAYBE_AddAndModifyImage AddAndModifyImage
1311#endif
1312TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001313#if defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001314 static const char kMd5NewImage[] = "beb7db3647706d7fe4689f92073847aa";
1315 static const char kMd5ModifiedImage[] = "baa9b065469268e215ef958fe6987d6b";
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001316#else
Lei Zhang42d30c22022-01-12 19:24:43 +00001317#if BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001318 static const char kMd5NewImage[] = "c6fcbceb2f079bef10458ac60db3a10c";
1319 static const char kMd5ModifiedImage[] = "8068eb568e5c1c5fbe84e98f7a980ac3";
Jane Liu36567742017-07-06 11:13:35 -04001320#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001321 static const char kMd5NewImage[] = "62c2706511cb50e32e7caeb82b1d3d49";
1322 static const char kMd5ModifiedImage[] = "83093ce9fac746db69fbd2fb394434ac";
Jane Liu36567742017-07-06 11:13:35 -04001323#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001324#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001325
1326 // Open a file with two annotations and load its first page.
1327 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001328 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001329 ASSERT_TRUE(page);
1330 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1331
1332 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001333 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001334 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001335 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001336 }
Jane Liu36567742017-07-06 11:13:35 -04001337
Jane Liu36567742017-07-06 11:13:35 -04001338 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001339 FPDF_BITMAP image_bitmap;
1340
1341 {
1342 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001343 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001344 ASSERT_TRUE(annot);
1345 FS_RECTF rect;
1346 rect.left = 200.f;
1347 rect.bottom = 600.f;
1348 rect.right = 400.f;
1349 rect.top = 800.f;
1350 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1351
1352 // Add a solid-color translucent image object to the new annotation.
1353 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1354 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1355 0xeeeecccc);
1356 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1357 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1358 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1359 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
Lei Zhangc8601bf2021-06-29 23:19:27 +00001360 static constexpr FS_MATRIX kBitmapScaleMatrix{kBitmapSize, 0, 0,
1361 kBitmapSize, 0, 0};
1362 ASSERT_TRUE(FPDFPageObj_SetMatrix(image_object, &kBitmapScaleMatrix));
Lei Zhanga21d5932018-02-05 18:28:38 +00001363 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1364 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1365 }
Jane Liu36567742017-07-06 11:13:35 -04001366
1367 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001368 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001369 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001370 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001371 }
Jane Liu36567742017-07-06 11:13:35 -04001372
Lei Zhanga21d5932018-02-05 18:28:38 +00001373 {
1374 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001375 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001376 ASSERT_TRUE(annot);
1377 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1378 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1379 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001380
Lei Zhanga21d5932018-02-05 18:28:38 +00001381 // Modify the image in the new annotation.
1382 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1383 0xff000000);
1384 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1385 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1386 }
Jane Liu36567742017-07-06 11:13:35 -04001387
Lei Zhangec618142021-04-13 17:36:46 +00001388 // Save the document and close the page.
Jane Liu36567742017-07-06 11:13:35 -04001389 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001390 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001391 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001392
1393 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001394 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001395}
1396
Hui Yingst6cd754f2020-05-14 04:05:25 +00001397TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001398#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001399 static const char kMd5NewText[] = "310d9de5f17fb288fb243f5dbaf2b6dc";
1400 static const char kMd5ModifiedText[] = "22be42c136c3bf5a8ecea3dd83770a02";
Lei Zhang42d30c22022-01-12 19:24:43 +00001401#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001402 static const char kMd5NewText[] = "57a0fb3fba33e17de26bcde4c40b9a75";
1403 static const char kMd5ModifiedText[] = "072574999f2e3f36774ee0b5bc94d4dd";
Jane Liu36567742017-07-06 11:13:35 -04001404#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001405 static const char kMd5NewText[] = "1c4198c38f890c208c5cbaad57be4dc6";
1406 static const char kMd5ModifiedText[] = "cfa78d01406865f41f486bd34a8b9f7b";
Jane Liu36567742017-07-06 11:13:35 -04001407#endif
1408
1409 // Open a file with two annotations and load its first page.
1410 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001411 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001412 ASSERT_TRUE(page);
1413 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1414
1415 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001416 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001417 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001418 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001419 }
Jane Liu36567742017-07-06 11:13:35 -04001420
Lei Zhanga21d5932018-02-05 18:28:38 +00001421 {
1422 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001423 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001424 ASSERT_TRUE(annot);
1425 FS_RECTF rect;
1426 rect.left = 200.f;
1427 rect.bottom = 550.f;
1428 rect.right = 450.f;
1429 rect.top = 650.f;
1430 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001431
Lei Zhanga21d5932018-02-05 18:28:38 +00001432 // Add a translucent text object to the new annotation.
1433 FPDF_PAGEOBJECT text_object =
1434 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1435 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001436 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001437 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1438 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001439 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001440 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1441 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1442 }
Jane Liu36567742017-07-06 11:13:35 -04001443
1444 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001445 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001446 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001447 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001448 }
Jane Liu36567742017-07-06 11:13:35 -04001449
Lei Zhanga21d5932018-02-05 18:28:38 +00001450 {
1451 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001452 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001453 ASSERT_TRUE(annot);
1454 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1455 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1456 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001457
Lei Zhanga21d5932018-02-05 18:28:38 +00001458 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001459 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001460 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1461 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1462 }
Jane Liu36567742017-07-06 11:13:35 -04001463
1464 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001465 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001466 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001467 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001468 }
Jane Liu36567742017-07-06 11:13:35 -04001469
1470 // Remove the new annotation, and check that the page renders as before.
1471 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001472 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001473 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001474 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001475 }
Jane Liu36567742017-07-06 11:13:35 -04001476
1477 UnloadPage(page);
1478}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001479
Hui Yingst9b6b1542020-07-27 16:11:12 +00001480TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001481 // Open a file with four annotations and load its first page.
1482 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001483 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001484 ASSERT_TRUE(page);
1485
Lei Zhang4f556b82019-04-08 16:32:41 +00001486 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001487
Lei Zhanga21d5932018-02-05 18:28:38 +00001488 {
1489 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001490 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001491 ASSERT_TRUE(annot);
1492
1493 // Check that a non-existent key does not exist.
1494 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1495
1496 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001497 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001498 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001499 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1500 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1501 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001502
1503 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001504 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001505 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001506 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001507 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001508 ASSERT_EQ(66u, length_bytes);
1509 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1510 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1511 length_bytes));
1512 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1513 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001514
1515 // Check that the string value of the modified date is correct.
1516 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001517 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1518 nullptr, 0);
1519 ASSERT_EQ(44u, length_bytes);
1520 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001521 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001522 buf.data(), length_bytes));
1523 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001524
1525 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001526 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001527 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1528 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001529 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001530
Lei Zhangec618142021-04-13 17:36:46 +00001531 // Save the document and close the page.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001532 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001533 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001534
Hui Yingst9b6b1542020-07-27 16:11:12 +00001535#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001536 static const char kMd5[] = "aa0e47d966c60d59102a2466542e0e46";
Lei Zhang42d30c22022-01-12 19:24:43 +00001537#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001538 static const char kMd5[] = "cd90315b250dfe08265ce0ac335c5f76";
Hui Yingst9b6b1542020-07-27 16:11:12 +00001539#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001540 static const char kMd5[] = "c4fb6911f2a87f490be196f8898de738";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001541#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001542
1543 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001544 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001545 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001546 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001547 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001548 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001549 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001550
Lei Zhanga21d5932018-02-05 18:28:38 +00001551 // Check that the string value of the modified date is the newly-set value.
1552 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001553 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001554 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001555 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001556 ASSERT_EQ(44u, length_bytes);
1557 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001558 EXPECT_EQ(44u,
1559 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001560 buf.data(), length_bytes));
1561 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001562 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001563
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001564 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001565 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001566}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001567
rycsmith3e785602019-03-05 21:48:36 +00001568TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001569 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001570 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1571 FPDF_PAGE page = LoadPage(0);
1572 ASSERT_TRUE(page);
1573 {
1574 // First two annotations do not have "MaxLen" attribute.
1575 for (int i = 0; i < 2; i++) {
1576 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1577 ASSERT_TRUE(annot);
1578
1579 // Verify that no "MaxLen" key present.
1580 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1581
1582 float value;
1583 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1584 }
1585
1586 // Annotation in index 2 has "MaxLen" of 10.
1587 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1588 ASSERT_TRUE(annot);
1589
1590 // Verify that "MaxLen" key present.
1591 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1592
1593 float value;
1594 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1595 EXPECT_FLOAT_EQ(10.0f, value);
1596
1597 // Check bad inputs.
1598 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1599 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1600 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1601 // Ask for key that exists but is not a number.
1602 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1603 }
1604
1605 UnloadPage(page);
1606}
1607
Lei Zhangab41f252018-12-23 03:10:50 +00001608TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001609 // Open a file with four annotations and load its first page.
1610 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001611 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001612 ASSERT_TRUE(page);
1613
Lei Zhanga21d5932018-02-05 18:28:38 +00001614 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001615 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1616 static constexpr size_t kExpectNormalAPLength = 73970;
1617
Lei Zhanga21d5932018-02-05 18:28:38 +00001618 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001619 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001620 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001621
Lei Zhanga21d5932018-02-05 18:28:38 +00001622 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001623 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001624 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001625 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001626
Lei Zhanga21d5932018-02-05 18:28:38 +00001627 // Check that the string value of an AP is not returned if the buffer is too
1628 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001629 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1630 // Write in the buffer to verify it's not overwritten.
1631 memcpy(buf.data(), "abcdefgh", 8);
1632 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001633 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001634 buf.data(), normal_length_bytes - 1));
1635 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001636
Lei Zhanga21d5932018-02-05 18:28:38 +00001637 // Check that the string value of an AP is returned through a buffer that is
1638 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001639 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001640 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001641 buf.data(), normal_length_bytes));
1642 EXPECT_EQ(kMd5NormalAP,
Lei Zhang0bd11f72022-05-11 00:10:54 +00001643 GenerateMD5Base16({reinterpret_cast<uint8_t*>(buf.data()),
1644 normal_length_bytes}));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001645
Lei Zhanga21d5932018-02-05 18:28:38 +00001646 // Check that the string value of an AP is returned through a buffer that is
1647 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001648 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1649 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001650 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001651 buf.data(), normal_length_bytes + 2));
1652 EXPECT_EQ(kMd5NormalAP,
Lei Zhang0bd11f72022-05-11 00:10:54 +00001653 GenerateMD5Base16({reinterpret_cast<uint8_t*>(buf.data()),
1654 normal_length_bytes}));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001655
Lei Zhanga21d5932018-02-05 18:28:38 +00001656 // Check that getting an AP for a mode that does not have an AP returns an
1657 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001658 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001659 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001660 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001661
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001662 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001663 EXPECT_EQ(2u,
1664 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001665 buf.data(), 1000));
1666 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001667
Lei Zhanga21d5932018-02-05 18:28:38 +00001668 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001669 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001670 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001671 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001672 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001673 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001674 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001675
Lei Zhanga21d5932018-02-05 18:28:38 +00001676 // Set the AP correctly now.
1677 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001678 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001679
Lei Zhanga21d5932018-02-05 18:28:38 +00001680 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001681 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001682 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001683 ASSERT_EQ(24u, rollover_length_bytes);
1684 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001685 EXPECT_EQ(24u,
1686 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001687 buf.data(), rollover_length_bytes));
1688 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001689
Lei Zhanga21d5932018-02-05 18:28:38 +00001690 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001691 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1692 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001693 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001694 buf.data(), normal_length_bytes));
1695 EXPECT_EQ(kMd5NormalAP,
Lei Zhang0bd11f72022-05-11 00:10:54 +00001696 GenerateMD5Base16({reinterpret_cast<uint8_t*>(buf.data()),
1697 normal_length_bytes}));
Lei Zhanga21d5932018-02-05 18:28:38 +00001698 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001699
1700 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001701 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001702 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001703
Lei Zhang0b494052019-01-31 21:41:15 +00001704 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001705 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001706 ASSERT_TRUE(page);
Lei Zhanga21d5932018-02-05 18:28:38 +00001707 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001708 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001709
Lei Zhanga21d5932018-02-05 18:28:38 +00001710 // Check that the new annotation value is equal to the value we set before
1711 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001712 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001713 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001714 ASSERT_EQ(24u, rollover_length_bytes);
1715 std::vector<FPDF_WCHAR> buf =
1716 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001717 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1718 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001719 buf.data(), rollover_length_bytes));
1720 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001721 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001722
1723 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001724 CloseSavedPage(page);
1725 CloseSavedDocument();
1726}
1727
Lei Zhangab41f252018-12-23 03:10:50 +00001728TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001729 // Open a file with four annotations and load its first page.
1730 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001731 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001732 ASSERT_TRUE(page);
1733
Lei Zhanga21d5932018-02-05 18:28:38 +00001734 {
1735 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001736 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001737 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001738
Lei Zhanga21d5932018-02-05 18:28:38 +00001739 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001740 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001741 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001742 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001743 EXPECT_EQ(73970u,
1744 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1745 nullptr, 0));
1746 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1747 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001748
Lei Zhanga21d5932018-02-05 18:28:38 +00001749 // Check that setting the Down AP to null removes the Down entry but keeps
1750 // Normal intact.
1751 EXPECT_TRUE(
1752 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1753 EXPECT_EQ(73970u,
1754 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1755 nullptr, 0));
1756 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1757 nullptr, 0));
1758 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001759
Lei Zhang75c81712018-02-08 17:22:39 +00001760 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001761}
1762
Lei Zhangab41f252018-12-23 03:10:50 +00001763TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001764 // Open a file with four annotations and load its first page.
1765 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001766 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001767 ASSERT_TRUE(page);
1768
Lei Zhanga21d5932018-02-05 18:28:38 +00001769 {
1770 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001771 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001772 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001773
Lei Zhanga21d5932018-02-05 18:28:38 +00001774 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001775 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001776 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001777 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001778 EXPECT_EQ(73970u,
1779 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1780 nullptr, 0));
1781 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1782 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001783
Lei Zhanga21d5932018-02-05 18:28:38 +00001784 // Check that setting the Normal AP to null removes the whole AP dictionary.
1785 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1786 nullptr));
1787 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1788 nullptr, 0));
1789 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1790 nullptr, 0));
1791 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001792
Lei Zhang75c81712018-02-08 17:22:39 +00001793 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001794}
1795
Lei Zhangab41f252018-12-23 03:10:50 +00001796TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001797 // Open a file with annotations and load its first page.
1798 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001799 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001800 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001801 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001802
Lei Zhanga21d5932018-02-05 18:28:38 +00001803 {
1804 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001805 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001806 ASSERT_TRUE(annot);
1807 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1808 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001809 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001810 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1811 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1812 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001813
Lei Zhanga21d5932018-02-05 18:28:38 +00001814 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001815 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001816 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1817 ASSERT_TRUE(popup);
1818 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1819 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1820 FS_RECTF rect;
1821 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1822 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1823 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001824
Lei Zhanga21d5932018-02-05 18:28:38 +00001825 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1826 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001827 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001828 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1829 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001830
Lei Zhanga21d5932018-02-05 18:28:38 +00001831 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1832 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001833 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001834 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001835 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1836 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001837 }
Jane Liu300bb272017-08-21 14:37:53 -04001838
Jane Liu300bb272017-08-21 14:37:53 -04001839 UnloadPage(page);
1840}
1841
Lei Zhangab41f252018-12-23 03:10:50 +00001842TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001843 // Open file with form text fields.
1844 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001845 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001846 ASSERT_TRUE(page);
1847
Lei Zhanga21d5932018-02-05 18:28:38 +00001848 {
1849 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001850 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001851 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001852
Lei Zhanga21d5932018-02-05 18:28:38 +00001853 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001854 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001855 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001856 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1857 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1858 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001859 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001860 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001861
Lei Zhanga21d5932018-02-05 18:28:38 +00001862 {
1863 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001864 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001865 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001866
Lei Zhanga21d5932018-02-05 18:28:38 +00001867 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001868 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001869 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001870 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1871 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1872 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001873 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1874 }
1875
1876 {
1877 // Retrieve the fourth annotation: user-editable password text field.
1878 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1879 ASSERT_TRUE(annot);
1880
1881 // Check that the flag values are as expected.
1882 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1883 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001884 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1885 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1886 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001887 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001888 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001889
1890 UnloadPage(page);
1891}
1892
Lei Zhangab41f252018-12-23 03:10:50 +00001893TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001894 // Open file with form text fields.
1895 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001896 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001897 ASSERT_TRUE(page);
1898
Lei Zhanga21d5932018-02-05 18:28:38 +00001899 {
1900 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001901 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001902 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001903
Lei Zhanga21d5932018-02-05 18:28:38 +00001904 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001905 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001906 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001907 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1908 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001909 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1910 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001911 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001912 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001913
Lei Zhanga21d5932018-02-05 18:28:38 +00001914 {
1915 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001916 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001917 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001918
Lei Zhanga21d5932018-02-05 18:28:38 +00001919 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001920 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001921 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001922 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1923 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001924 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1925 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001926 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001927 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001928
Lei Zhanga21d5932018-02-05 18:28:38 +00001929 {
1930 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001931 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001932 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001933
Lei Zhanga21d5932018-02-05 18:28:38 +00001934 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001935 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001936 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001937 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1938 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001939 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1940 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001941 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001942 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001943
1944 UnloadPage(page);
1945}
Diana Gage40870db2017-07-19 18:16:03 -07001946
Lei Zhangab41f252018-12-23 03:10:50 +00001947TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001948 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001949 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001950 FPDF_PAGE page = LoadPage(0);
1951 ASSERT_TRUE(page);
1952
1953 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001954 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1955 EXPECT_FALSE(
1956 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001957
Lei Zhang8da98232019-12-11 23:29:33 +00001958 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001959 {
1960 // Verify there is an annotation.
1961 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001962 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001963 EXPECT_TRUE(annot);
1964 }
1965
1966 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001967 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1968 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1969 EXPECT_FALSE(
1970 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001971
1972 UnloadPage(page);
1973}
1974
Lei Zhangab41f252018-12-23 03:10:50 +00001975TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001976 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001977 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001978 FPDF_PAGE page = LoadPage(0);
1979 ASSERT_TRUE(page);
1980
Lei Zhanga21d5932018-02-05 18:28:38 +00001981 {
1982 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001983 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001984 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001985 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001986 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001987
Lei Zhanga21d5932018-02-05 18:28:38 +00001988 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001989 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00001990 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1991 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001992 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1993 }
Diana Gage40870db2017-07-19 18:16:03 -07001994
Lei Zhanga21d5932018-02-05 18:28:38 +00001995 {
1996 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001997 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001998 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001999 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002000 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002001
Lei Zhanga21d5932018-02-05 18:28:38 +00002002 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002003 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002004 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002005 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2006 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002007 }
Diana Gage40870db2017-07-19 18:16:03 -07002008
2009 UnloadPage(page);
2010}
2011
Lei Zhangab41f252018-12-23 03:10:50 +00002012TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07002013 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002014 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002015 FPDF_PAGE page = LoadPage(0);
2016 ASSERT_TRUE(page);
2017
Lei Zhanga21d5932018-02-05 18:28:38 +00002018 {
2019 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002020 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002021 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002022 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002023 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002024
Lei Zhanga21d5932018-02-05 18:28:38 +00002025 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002026 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002027 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002028 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2029 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002030 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2031 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002032 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002033 }
Diana Gage40870db2017-07-19 18:16:03 -07002034
Lei Zhanga21d5932018-02-05 18:28:38 +00002035 {
2036 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002037 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002038 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002039 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002040 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002041
Lei Zhanga21d5932018-02-05 18:28:38 +00002042 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002043 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002044 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002045 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2046 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002047 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2048 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002049 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002050 }
Diana Gage40870db2017-07-19 18:16:03 -07002051
Lei Zhanga21d5932018-02-05 18:28:38 +00002052 {
2053 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002054 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002055 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002056 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002057 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002058
Lei Zhanga21d5932018-02-05 18:28:38 +00002059 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002060 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002061 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002062 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2063 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002064 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2065 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002066 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002067 }
Diana Gage40870db2017-07-19 18:16:03 -07002068
2069 UnloadPage(page);
2070}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002071
Hui Yingst603dcd82020-11-09 22:36:46 +00002072TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
Lei Zhang03e5e682019-09-16 19:45:55 +00002073#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst603dcd82020-11-09 22:36:46 +00002074 static const char kExpectedBitmap[] = "a1ea1ceebb26922fae576cb79ce63af0";
Lei Zhang03e5e682019-09-16 19:45:55 +00002075#else
Lei Zhang992e7e22019-02-04 19:20:58 +00002076 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
Hui Yingst603dcd82020-11-09 22:36:46 +00002077#endif
Tom Andersond4fe5f72021-12-03 20:52:52 +00002078 static constexpr size_t kExpectedSize = 1590;
Lei Zhang992e7e22019-02-04 19:20:58 +00002079
2080 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2081
2082 FPDF_PAGE page = LoadPage(0);
2083 ASSERT_TRUE(page);
2084
2085 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2086 EXPECT_EQ(kExpectedSize, GetString().size());
2087 ClearString();
2088
2089 for (size_t i = 0; i < 10; ++i) {
2090 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2091 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2092
2093 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2094 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2095 // equal, not bigger.
2096 EXPECT_LT(kExpectedSize, GetString().size());
2097 ClearString();
2098 }
2099
2100 UnloadPage(page);
2101}
2102
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002103TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2104 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2105 FPDF_PAGE page = LoadPage(0);
2106 ASSERT_TRUE(page);
2107 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2108
2109 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002110 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002111 static const size_t kBufSize = 12;
2112 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002113
2114 {
2115 // Add a text annotation to the page.
2116 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2117 ASSERT_TRUE(annot);
2118 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2119 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2120
2121 // Make sure there is no test key, add set a value there, and read it back.
2122 std::fill(buf.begin(), buf.end(), 'x');
2123 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002124 kBufSize));
2125 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002126
Lei Zhangf0f67682019-04-08 17:03:21 +00002127 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002128 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2129
2130 std::fill(buf.begin(), buf.end(), 'x');
2131 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002132 kBufSize));
2133 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002134 }
2135
Lei Zhang05ec64c2019-01-09 03:00:06 +00002136 {
2137 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2138 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002139 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002140 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002141 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2142 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2143 // Also do the same test for its appearance string.
2144 std::fill(buf.begin(), buf.end(), 'x');
2145 ASSERT_EQ(2u,
2146 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002147 buf.data(), kBufSize));
2148 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002149
Lei Zhangf0f67682019-04-08 17:03:21 +00002150 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002151 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2152 text.get()));
2153
2154 std::fill(buf.begin(), buf.end(), 'x');
2155 ASSERT_EQ(6u,
2156 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002157 buf.data(), kBufSize));
2158 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002159 }
2160
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002161 UnloadPage(page);
2162
2163 {
2164 // Save a copy, open the copy, and check the annotation again.
2165 // Note that it renders the rotation.
2166 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002167 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002168 FPDF_PAGE saved_page = LoadSavedPage(0);
2169 ASSERT_TRUE(saved_page);
2170
Lei Zhang05ec64c2019-01-09 03:00:06 +00002171 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002172 {
2173 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2174 ASSERT_TRUE(annot);
2175 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2176
2177 std::fill(buf.begin(), buf.end(), 'x');
2178 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002179 kBufSize));
2180 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002181 }
2182
Lei Zhang05ec64c2019-01-09 03:00:06 +00002183 {
2184 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2185 ASSERT_TRUE(annot);
2186 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2187 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2188
2189 std::fill(buf.begin(), buf.end(), 'x');
2190 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002191 kBufSize));
2192 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002193 }
2194
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002195 CloseSavedPage(saved_page);
2196 CloseSavedDocument();
2197 }
2198}
rycsmithcb752f32019-02-21 18:40:53 +00002199
2200TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2201 // Open a file with combobox widget annotations and load its first page.
2202 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2203 FPDF_PAGE page = LoadPage(0);
2204 ASSERT_TRUE(page);
2205
2206 {
2207 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2208 ASSERT_TRUE(annot);
2209
2210 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2211
2212 annot.reset(FPDFPage_GetAnnot(page, 1));
2213 ASSERT_TRUE(annot);
2214
2215 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002216
2217 // Check bad form handle / annot.
2218 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2219 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2220 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002221 }
2222
2223 UnloadPage(page);
2224}
2225
2226TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2227 // Open a file with listbox widget annotations and load its first page.
2228 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2229 FPDF_PAGE page = LoadPage(0);
2230 ASSERT_TRUE(page);
2231
2232 {
2233 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2234 ASSERT_TRUE(annot);
2235
2236 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2237
2238 annot.reset(FPDFPage_GetAnnot(page, 1));
2239 ASSERT_TRUE(annot);
2240
2241 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2242 }
2243
2244 UnloadPage(page);
2245}
2246
2247TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2248 // Open a file with ink annotations and load its first page.
2249 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2250 FPDF_PAGE page = LoadPage(0);
2251 ASSERT_TRUE(page);
2252
2253 {
2254 // annotations do not have "Opt" array and will return -1
2255 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2256 ASSERT_TRUE(annot);
2257
2258 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2259
2260 annot.reset(FPDFPage_GetAnnot(page, 1));
2261 ASSERT_TRUE(annot);
2262
2263 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2264 }
2265
2266 UnloadPage(page);
2267}
2268
2269TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2270 // Open a file with combobox widget annotations and load its first page.
2271 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2272 FPDF_PAGE page = LoadPage(0);
2273 ASSERT_TRUE(page);
2274
2275 {
2276 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2277 ASSERT_TRUE(annot);
2278
2279 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002280 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002281 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002282 ASSERT_EQ(8u, length_bytes);
2283 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002284 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002285 buf.data(), length_bytes));
2286 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002287
2288 annot.reset(FPDFPage_GetAnnot(page, 1));
2289 ASSERT_TRUE(annot);
2290
2291 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002292 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002293 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002294 ASSERT_EQ(12u, length_bytes);
2295 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002296 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002297 buf.data(), length_bytes));
2298 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002299
2300 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002301 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002302 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002303 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002304 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002305 buf.data(), length_bytes));
2306 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002307
Lei Zhange7033c82019-02-26 19:30:49 +00002308 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002309 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2310 nullptr, 0));
2311 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2312 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002313
2314 // Check bad form handle / annot.
2315 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2316 EXPECT_EQ(0u,
2317 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2318 EXPECT_EQ(0u,
2319 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002320 }
2321
2322 UnloadPage(page);
2323}
2324
2325TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2326 // Open a file with listbox widget annotations and load its first page.
2327 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2328 FPDF_PAGE page = LoadPage(0);
2329 ASSERT_TRUE(page);
2330
2331 {
2332 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2333 ASSERT_TRUE(annot);
2334
2335 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002336 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002337 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002338 ASSERT_EQ(8u, length_bytes);
2339 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002340 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002341 buf.data(), length_bytes));
2342 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002343
2344 annot.reset(FPDFPage_GetAnnot(page, 1));
2345 ASSERT_TRUE(annot);
2346
2347 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002348 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002349 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002350 ASSERT_EQ(12u, length_bytes);
2351 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002352 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002353 buf.data(), length_bytes));
2354 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002355
2356 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002357 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002358 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002359 ASSERT_EQ(18u, length_bytes);
2360 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002361 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002362 buf.data(), length_bytes));
2363 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002364
2365 // indices out of range
2366 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2367 nullptr, 0));
2368 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2369 nullptr, 0));
2370 }
2371
2372 UnloadPage(page);
2373}
2374
2375TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2376 // Open a file with ink annotations and load its first page.
2377 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2378 FPDF_PAGE page = LoadPage(0);
2379 ASSERT_TRUE(page);
2380
2381 {
2382 // annotations do not have "Opt" array and will return 0
2383 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2384 ASSERT_TRUE(annot);
2385
2386 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2387 nullptr, 0));
2388
2389 annot.reset(FPDFPage_GetAnnot(page, 1));
2390 ASSERT_TRUE(annot);
2391
2392 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2393 nullptr, 0));
2394 }
2395
2396 UnloadPage(page);
2397}
Ryan Smith09c23b12019-04-25 18:09:06 +00002398
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002399TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2400 // Open a file with combobox widget annotations and load its first page.
2401 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2402 FPDF_PAGE page = LoadPage(0);
2403 ASSERT_TRUE(page);
2404
2405 {
2406 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2407 ASSERT_TRUE(annot);
2408
2409 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2410 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2411 ASSERT_EQ(3, count);
2412 for (int i = 0; i < count; i++) {
2413 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2414 }
2415
2416 annot.reset(FPDFPage_GetAnnot(page, 1));
2417 ASSERT_TRUE(annot);
2418
2419 // Checks for Combobox with Values (/V) object which is just a string.
2420 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2421 ASSERT_EQ(26, count);
2422 for (int i = 0; i < count; i++) {
2423 EXPECT_EQ(i == 1,
2424 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2425 }
2426
2427 // Checks for index outside bound i.e. (index >= CountOption()).
2428 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2429 /*index=*/26));
2430 // Checks for negetive index.
2431 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2432 /*index=*/-1));
2433
2434 // Checks for bad form handle/annot.
2435 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2436 EXPECT_FALSE(
2437 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2438 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2439 }
2440
2441 UnloadPage(page);
2442}
2443
2444TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2445 // Open a file with listbox widget annotations and load its first page.
2446 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2447 FPDF_PAGE page = LoadPage(0);
2448 ASSERT_TRUE(page);
2449
2450 {
2451 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2452 ASSERT_TRUE(annot);
2453
2454 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2455 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2456 ASSERT_EQ(3, count);
2457 for (int i = 0; i < count; i++) {
2458 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2459 }
2460
2461 annot.reset(FPDFPage_GetAnnot(page, 1));
2462 ASSERT_TRUE(annot);
2463
2464 // Checks for Listbox with Values (/V) object which is just a string.
2465 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2466 ASSERT_EQ(26, count);
2467 for (int i = 0; i < count; i++) {
2468 EXPECT_EQ(i == 1,
2469 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2470 }
2471
2472 annot.reset(FPDFPage_GetAnnot(page, 3));
2473 ASSERT_TRUE(annot);
2474
2475 // Checks for Listbox with only Selected indices (/I) object which is an
2476 // array with multiple objects.
2477 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2478 ASSERT_EQ(5, count);
2479 for (int i = 0; i < count; i++) {
2480 bool expected = (i == 1 || i == 3);
2481 EXPECT_EQ(expected,
2482 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2483 }
2484
2485 annot.reset(FPDFPage_GetAnnot(page, 4));
2486 ASSERT_TRUE(annot);
2487
2488 // Checks for Listbox with Values (/V) object which is an array with
2489 // multiple objects.
2490 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2491 ASSERT_EQ(5, count);
2492 for (int i = 0; i < count; i++) {
2493 bool expected = (i == 2 || i == 4);
2494 EXPECT_EQ(expected,
2495 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2496 }
2497
2498 annot.reset(FPDFPage_GetAnnot(page, 5));
2499 ASSERT_TRUE(annot);
2500
2501 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2502 // objects conflict with different lengths.
2503 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2504 ASSERT_EQ(5, count);
2505 for (int i = 0; i < count; i++) {
2506 bool expected = (i == 0 || i == 2);
2507 EXPECT_EQ(expected,
2508 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2509 }
2510 }
2511
2512 UnloadPage(page);
2513}
2514
2515TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2516 // Open a file with multiple form field annotations and load its first page.
2517 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2518 FPDF_PAGE page = LoadPage(0);
2519 ASSERT_TRUE(page);
2520
2521 {
2522 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2523 ASSERT_TRUE(annot);
2524
2525 // Checks for link annotation.
2526 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2527 /*index=*/0));
2528
2529 annot.reset(FPDFPage_GetAnnot(page, 3));
2530 ASSERT_TRUE(annot);
2531
2532 // Checks for text field annotation.
2533 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2534 /*index=*/0));
2535 }
2536
2537 UnloadPage(page);
2538}
2539
Ryan Smith09c23b12019-04-25 18:09:06 +00002540TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2541 // Open a file with combobox annotations and load its first page.
2542 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2543 FPDF_PAGE page = LoadPage(0);
2544 ASSERT_TRUE(page);
2545
2546 {
2547 // All 3 widgets have Tf font size 12.
2548 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2549 ASSERT_TRUE(annot);
2550
2551 float value;
2552 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2553 EXPECT_EQ(12.0, value);
2554
2555 annot.reset(FPDFPage_GetAnnot(page, 1));
2556 ASSERT_TRUE(annot);
2557
2558 float value_two;
2559 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2560 EXPECT_EQ(12.0, value_two);
2561
2562 annot.reset(FPDFPage_GetAnnot(page, 2));
2563 ASSERT_TRUE(annot);
2564
2565 float value_three;
2566 ASSERT_TRUE(
2567 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2568 EXPECT_EQ(12.0, value_three);
2569 }
2570
2571 UnloadPage(page);
2572}
2573
2574TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2575 // Open a file with textfield annotations and load its first page.
2576 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2577 FPDF_PAGE page = LoadPage(0);
2578 ASSERT_TRUE(page);
2579
2580 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002581 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002582 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2583 ASSERT_TRUE(annot);
2584
2585 float value;
2586 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2587 EXPECT_EQ(12.0, value);
2588
2589 annot.reset(FPDFPage_GetAnnot(page, 1));
2590 ASSERT_TRUE(annot);
2591
2592 float value_two;
2593 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2594 EXPECT_EQ(12.0, value_two);
2595
2596 annot.reset(FPDFPage_GetAnnot(page, 2));
2597 ASSERT_TRUE(annot);
2598
2599 float value_three;
2600 ASSERT_TRUE(
2601 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2602 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002603
2604 float value_four;
2605 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2606 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002607 }
2608
2609 UnloadPage(page);
2610}
2611
2612TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2613 // Open a file with ink annotations and load its first page.
2614 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2615 FPDF_PAGE page = LoadPage(0);
2616 ASSERT_TRUE(page);
2617
2618 {
2619 // Annotations that do not have variable text and will return -1.
2620 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2621 ASSERT_TRUE(annot);
2622
2623 float value;
2624 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2625
2626 annot.reset(FPDFPage_GetAnnot(page, 1));
2627 ASSERT_TRUE(annot);
2628
2629 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2630 }
2631
2632 UnloadPage(page);
2633}
2634
2635TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2636 // Open a file with combobox annotations and load its first page.
2637 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2638 FPDF_PAGE page = LoadPage(0);
2639 ASSERT_TRUE(page);
2640
2641 {
2642 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2643 ASSERT_TRUE(annot);
2644
2645 // Check bad form handle / annot.
2646 float value;
2647 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2648 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2649 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2650 }
2651
2652 UnloadPage(page);
2653}
2654
2655TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2656 // Open a file with textfield annotations and load its first page.
2657 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2658 FPDF_PAGE page = LoadPage(0);
2659 ASSERT_TRUE(page);
2660
2661 {
2662 // Obtain the first annotation, a text field with negative font size, -12.
2663 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2664 ASSERT_TRUE(annot);
2665
2666 float value;
2667 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2668 EXPECT_EQ(-12.0, value);
2669 }
2670
2671 UnloadPage(page);
2672}
Ryan Smith23fdf892019-07-17 21:51:26 +00002673
2674TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2675 // Open a file with checkbox and radiobuttons widget annotations and load its
2676 // first page.
2677 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2678 FPDF_PAGE page = LoadPage(0);
2679 ASSERT_TRUE(page);
2680
2681 {
2682 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2683 ASSERT_TRUE(annot);
2684 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2685 }
2686
2687 UnloadPage(page);
2688}
2689
2690TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2691 // Open a file with checkbox and radiobutton widget annotations and load its
2692 // first page.
2693 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2694 FPDF_PAGE page = LoadPage(0);
2695 ASSERT_TRUE(page);
2696
2697 {
2698 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2699 ASSERT_TRUE(annot);
2700 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2701 }
2702
2703 UnloadPage(page);
2704}
2705
2706TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2707 // Open a file with checkbox and radiobutton widget annotations and load its
2708 // first page.
2709 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2710 FPDF_PAGE page = LoadPage(0);
2711 ASSERT_TRUE(page);
2712
2713 {
2714 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2715 ASSERT_TRUE(annot);
2716 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2717
2718 annot.reset(FPDFPage_GetAnnot(page, 6));
2719 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2720
2721 annot.reset(FPDFPage_GetAnnot(page, 7));
2722 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2723 }
2724
2725 UnloadPage(page);
2726}
2727
2728TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2729 // Open a file with checkbox and radiobutton widget annotations and load its
2730 // first page.
2731 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2732 FPDF_PAGE page = LoadPage(0);
2733 ASSERT_TRUE(page);
2734
2735 {
2736 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2737 ASSERT_TRUE(annot);
2738 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2739
2740 annot.reset(FPDFPage_GetAnnot(page, 3));
2741 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2742
2743 annot.reset(FPDFPage_GetAnnot(page, 4));
2744 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2745 }
2746
2747 UnloadPage(page);
2748}
2749
2750TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2751 // Open a file with checkbox and radiobuttons widget annotations and load its
2752 // first page.
2753 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2754 FPDF_PAGE page = LoadPage(0);
2755 ASSERT_TRUE(page);
2756
2757 {
2758 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2759 ASSERT_TRUE(annot);
2760 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2761 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2762 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2763 }
2764
2765 UnloadPage(page);
2766}
2767
2768TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2769 // Open a file with text widget annotations and load its first page.
2770 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2771 FPDF_PAGE page = LoadPage(0);
2772 ASSERT_TRUE(page);
2773
2774 {
2775 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2776 ASSERT_TRUE(annot);
2777 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2778 }
2779
2780 UnloadPage(page);
2781}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002782
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002783TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2784 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002785 FPDF_PAGE page = LoadPage(0);
2786 ASSERT_TRUE(page);
2787
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002788 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2789
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002790 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002791 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002792 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002793 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002794 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002795
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002796 constexpr int kExpectedAnnotTypes[] = {-1,
2797 FPDF_FORMFIELD_COMBOBOX,
2798 FPDF_FORMFIELD_LISTBOX,
2799 FPDF_FORMFIELD_TEXTFIELD,
2800 FPDF_FORMFIELD_CHECKBOX,
2801 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002802
Lei Zhang3111d402022-04-18 22:24:07 +00002803 for (size_t i = 0; i < std::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002804 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002805 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002806 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002807 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2808 }
2809 UnloadPage(page);
2810}
2811
2812TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2813 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2814 FPDF_PAGE page = LoadPage(0);
2815 ASSERT_TRUE(page);
2816
2817 {
2818 EXPECT_EQ(0u,
2819 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2820
2821 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2822 ASSERT_TRUE(annot);
2823
2824 EXPECT_EQ(0u,
2825 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2826
2827 unsigned long length_bytes =
2828 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2829 ASSERT_EQ(2u, length_bytes);
2830 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2831 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2832 buf.data(), length_bytes));
2833 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2834 }
2835 {
2836 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2837 ASSERT_TRUE(annot);
2838
2839 unsigned long length_bytes =
2840 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2841 ASSERT_EQ(18u, length_bytes);
2842 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2843 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2844 buf.data(), length_bytes));
2845 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2846 }
2847 UnloadPage(page);
2848}
2849
2850TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2851 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2852 FPDF_PAGE page = LoadPage(0);
2853 ASSERT_TRUE(page);
2854
2855 {
2856 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2857 ASSERT_TRUE(annot);
2858
2859 unsigned long length_bytes =
2860 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2861 ASSERT_EQ(2u, length_bytes);
2862 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2863 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2864 buf.data(), length_bytes));
2865 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2866 }
2867 {
2868 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2869 ASSERT_TRUE(annot);
2870
2871 unsigned long length_bytes =
2872 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2873 ASSERT_EQ(14u, length_bytes);
2874 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2875 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2876 buf.data(), length_bytes));
2877 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2878 }
2879 UnloadPage(page);
2880}
2881
2882TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2883 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2884 FPDF_PAGE page = LoadPage(0);
2885 ASSERT_TRUE(page);
2886
2887 {
2888 EXPECT_EQ(0u,
2889 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2890
2891 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2892 ASSERT_TRUE(annot);
2893
2894 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2895
2896 unsigned long length_bytes =
2897 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2898 ASSERT_EQ(18u, length_bytes);
2899 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2900 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2901 buf.data(), length_bytes));
2902 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2903 }
2904 UnloadPage(page);
2905}
2906
2907TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2908 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2909 FPDF_PAGE page = LoadPage(0);
2910 ASSERT_TRUE(page);
2911
2912 {
2913 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2914 ASSERT_TRUE(annot);
2915
2916 unsigned long length_bytes =
2917 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2918 ASSERT_EQ(30u, length_bytes);
2919 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2920 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2921 buf.data(), length_bytes));
2922 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2923 }
2924 UnloadPage(page);
2925}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002926
Lei Zhang9b444002020-04-17 17:35:23 +00002927TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2928 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002929 FPDF_PAGE page = LoadPage(0);
2930 ASSERT_TRUE(page);
2931
Lei Zhang9b444002020-04-17 17:35:23 +00002932 // Verify widgets are by default focusable.
2933 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2934 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002935
Lei Zhang9b444002020-04-17 17:35:23 +00002936 // Expected annot subtypes for page 0 of annots.pdf.
2937 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2938 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2939 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2940 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2941 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002942
Lei Zhang9b444002020-04-17 17:35:23 +00002943 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002944 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002945 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2946 kExpectedAnnotSubtypes,
2947 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002948
Lei Zhang9b444002020-04-17 17:35:23 +00002949 // Make no annotation type focusable using the preferred method.
2950 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2951 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002952
Lei Zhang9b444002020-04-17 17:35:23 +00002953 // Restore the focusable type count back to 1, then set it back to 0 using a
2954 // different method.
2955 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2956 ASSERT_TRUE(
2957 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2958 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002959
Lei Zhang9b444002020-04-17 17:35:23 +00002960 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002961 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002962
Lei Zhang9b444002020-04-17 17:35:23 +00002963 // Now make links focusable.
2964 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2965 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002966
Lei Zhang9b444002020-04-17 17:35:23 +00002967 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002968 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002969 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2970 kExpectedAnnotSubtypes,
2971 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002972
Lei Zhang9b444002020-04-17 17:35:23 +00002973 // Test invalid parameters.
2974 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang3111d402022-04-18 22:24:07 +00002975 std::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002976 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang3111d402022-04-18 22:24:07 +00002977 std::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002978 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2979
Lei Zhang9b444002020-04-17 17:35:23 +00002980 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2981 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2982 subtypes.size()));
2983 EXPECT_FALSE(
2984 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2985 EXPECT_FALSE(
2986 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002987
2988 UnloadPage(page);
2989}
Lei Zhang671aece2020-04-14 19:02:26 +00002990
Hui Yingstea3816d2020-07-28 22:35:11 +00002991TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00002992 ASSERT_TRUE(OpenDocument("annots.pdf"));
2993 FPDF_PAGE page = LoadPage(0);
2994 ASSERT_TRUE(page);
2995
2996 {
Hui Yingstea3816d2020-07-28 22:35:11 +00002997#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00002998 static const char kMd5sum[] = "b4c8f1dab175508810c476d078ebc5a6";
Lei Zhang42d30c22022-01-12 19:24:43 +00002999#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003000 static const char kMd5sum[] = "108a46c517c4eaace9982ee83e8e3296";
Lei Zhang671aece2020-04-14 19:02:26 +00003001#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003002 static const char kMd5sum[] = "5550d8dcb4d1af1f50e8b4bcaef2ee60";
Lei Zhang671aece2020-04-14 19:02:26 +00003003#endif
3004 // Check the initial rendering.
3005 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3006 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3007 }
3008
3009 // Make links and highlights focusable.
3010 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
3011 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang3111d402022-04-18 22:24:07 +00003012 constexpr int kSubTypesCount = std::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00003013 ASSERT_TRUE(
3014 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
3015 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3016 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
3017 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
3018 subtypes.size()));
3019 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
3020 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
3021
3022 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003023#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003024 static const char kMd5sum[] = "9173db3a892bc1697eef5cdaed19eda6";
Lei Zhang42d30c22022-01-12 19:24:43 +00003025#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003026 static const char kMd5sum[] = "eb3869335e7a219e1b5f25c1c6037b97";
Lei Zhang671aece2020-04-14 19:02:26 +00003027#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003028 static const char kMd5sum[] = "805fe7bb751ac4ed2b82bb66efe6db40";
Lei Zhang671aece2020-04-14 19:02:26 +00003029#endif
3030 // Focus the first link and check the rendering.
3031 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3032 ASSERT_TRUE(annot);
3033 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3034 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3035 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3036 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3037 }
3038
3039 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003040#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003041 static const char kMd5sum[] = "174dbdb218c2b14011c9c1db67fe41c3";
Lei Zhang42d30c22022-01-12 19:24:43 +00003042#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003043 static const char kMd5sum[] = "d20b1978da2362d3942ea0fc6d230997";
Lei Zhang671aece2020-04-14 19:02:26 +00003044#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003045 static const char kMd5sum[] = "c5c5dcb462af3ef5f43b298ec048feef";
Lei Zhang671aece2020-04-14 19:02:26 +00003046#endif
3047 // Focus the first highlight and check the rendering.
3048 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3049 ASSERT_TRUE(annot);
3050 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3051 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3052 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3053 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3054 }
3055
3056 UnloadPage(page);
3057}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003058
3059TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
3060 ASSERT_TRUE(OpenDocument("annots.pdf"));
3061 FPDF_PAGE page = LoadPage(0);
3062 ASSERT_TRUE(page);
3063 {
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003064 constexpr char kExpectedResult[] =
3065 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3066 "fpdf_text.h";
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003067
Lei Zhang306874b2021-04-16 00:33:36 +00003068 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3069 ASSERT_TRUE(annot);
3070 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3071 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()),
3072 kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003073 }
3074
3075 {
3076 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3077 ASSERT_TRUE(annot);
3078 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3079 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3080 }
3081
3082 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3083
3084 UnloadPage(page);
3085}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003086
3087TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3088 // Open a file with radio button widget annotations and load its first page.
3089 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3090 FPDF_PAGE page = LoadPage(0);
3091 ASSERT_TRUE(page);
3092
3093 {
3094 // Checks for bad annot.
3095 EXPECT_EQ(-1,
3096 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3097
3098 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3099 ASSERT_TRUE(annot);
3100
3101 // Checks for bad form handle.
3102 EXPECT_EQ(-1,
3103 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3104
3105 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3106 }
3107
3108 UnloadPage(page);
3109}
3110
3111TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3112 // Open a file with checkbox widget annotations and load its first page.
3113 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3114 FPDF_PAGE page = LoadPage(0);
3115 ASSERT_TRUE(page);
3116
3117 {
3118 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3119 ASSERT_TRUE(annot);
3120 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3121 }
3122
3123 UnloadPage(page);
3124}
3125
3126TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3127 // Open a file with ink annotations and load its first page.
3128 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3129 FPDF_PAGE page = LoadPage(0);
3130 ASSERT_TRUE(page);
3131
3132 {
3133 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3134 ASSERT_TRUE(annot);
3135 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3136 }
3137
3138 UnloadPage(page);
3139}
3140
3141TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3142 // Open a file with radio button widget annotations and load its first page.
3143 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3144 FPDF_PAGE page = LoadPage(0);
3145 ASSERT_TRUE(page);
3146
3147 {
3148 // Checks for bad annot.
3149 EXPECT_EQ(-1,
3150 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3151
3152 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3153 ASSERT_TRUE(annot);
3154
3155 // Checks for bad form handle.
3156 EXPECT_EQ(-1,
3157 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3158
3159 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3160 }
3161
3162 UnloadPage(page);
3163}
3164
3165TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3166 // Open a file with checkbox widget annotations and load its first page.
3167 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3168 FPDF_PAGE page = LoadPage(0);
3169 ASSERT_TRUE(page);
3170
3171 {
3172 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3173 ASSERT_TRUE(annot);
3174 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3175 }
3176
3177 UnloadPage(page);
3178}
3179
3180TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3181 // Open a file with ink annotations and load its first page.
3182 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3183 FPDF_PAGE page = LoadPage(0);
3184 ASSERT_TRUE(page);
3185
3186 {
3187 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3188 ASSERT_TRUE(annot);
3189 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3190 }
3191
3192 UnloadPage(page);
3193}
3194
3195TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3196 // Open a file with radio button widget annotations and load its first page.
3197 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3198 FPDF_PAGE page = LoadPage(0);
3199 ASSERT_TRUE(page);
3200
3201 {
3202 // Checks for bad annot.
3203 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3204 form_handle(), /*annot=*/nullptr,
3205 /*buffer=*/nullptr, /*buflen=*/0));
3206
3207 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3208 ASSERT_TRUE(annot);
3209
3210 // Checks for bad form handle.
3211 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3212 /*hHandle=*/nullptr, annot.get(),
3213 /*buffer=*/nullptr, /*buflen=*/0));
3214
3215 unsigned long length_bytes =
3216 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3217 /*buffer=*/nullptr, /*buflen=*/0);
3218 ASSERT_EQ(14u, length_bytes);
3219 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3220 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3221 buf.data(), length_bytes));
3222 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3223 }
3224
3225 UnloadPage(page);
3226}
3227
3228TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3229 // Open a file with checkbox widget annotations and load its first page.
3230 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3231 FPDF_PAGE page = LoadPage(0);
3232 ASSERT_TRUE(page);
3233
3234 {
3235 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3236 ASSERT_TRUE(annot);
3237
3238 unsigned long length_bytes =
3239 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3240 /*buffer=*/nullptr, /*buflen=*/0);
3241 ASSERT_EQ(8u, length_bytes);
3242 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3243 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3244 buf.data(), length_bytes));
3245 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3246 }
3247
3248 UnloadPage(page);
3249}
3250
3251TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3252 // Open a file with ink annotations and load its first page.
3253 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3254 FPDF_PAGE page = LoadPage(0);
3255 ASSERT_TRUE(page);
3256
3257 {
3258 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3259 ASSERT_TRUE(annot);
3260 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3261 /*buffer=*/nullptr,
3262 /*buflen=*/0));
3263 }
3264
3265 UnloadPage(page);
3266}
Miklos Vajnad1a24fb2020-10-26 18:07:13 +00003267
3268TEST_F(FPDFAnnotEmbedderTest, Redactannotation) {
3269 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3270 FPDF_PAGE page = LoadPage(0);
3271 ASSERT_TRUE(page);
3272 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3273
3274 {
3275 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3276 ASSERT_TRUE(annot);
3277 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3278 }
3279
3280 UnloadPage(page);
3281}
Miklos Vajna09ecef62020-11-10 21:50:38 +00003282
3283TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) {
3284 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3285 FPDF_PAGE page = LoadPage(0);
3286 ASSERT_TRUE(page);
3287 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3288
3289 {
3290 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3291 ASSERT_TRUE(annot);
3292
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003293 // FPDFAnnot_GetVertices() positive testing.
Miklos Vajna09ecef62020-11-10 21:50:38 +00003294 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3295 const size_t kExpectedSize = 3;
3296 ASSERT_EQ(kExpectedSize, size);
3297 std::vector<FS_POINTF> vertices_buffer(size);
3298 EXPECT_EQ(size,
3299 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3300 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3301 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3302 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3303 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3304 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3305 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3306
3307 // FPDFAnnot_GetVertices() negative testing.
3308 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3309
3310 // vertices_buffer is not overwritten if it is too small.
3311 vertices_buffer.resize(1);
3312 vertices_buffer[0].x = 42;
3313 vertices_buffer[0].y = 43;
3314 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3315 vertices_buffer.size());
3316 EXPECT_EQ(kExpectedSize, size);
3317 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3318 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3319 }
3320
3321 {
3322 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3323 ASSERT_TRUE(annot);
3324
3325 // This has an odd number of elements in the vertices array, ignore the last
3326 // element.
3327 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3328 const size_t kExpectedSize = 3;
3329 ASSERT_EQ(kExpectedSize, size);
3330 std::vector<FS_POINTF> vertices_buffer(size);
3331 EXPECT_EQ(size,
3332 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3333 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3334 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3335 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3336 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3337 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3338 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3339 }
3340
3341 {
3342 // Wrong annotation type.
3343 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3344 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3345 }
3346
3347 UnloadPage(page);
3348}
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003349
3350TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) {
3351 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3352 FPDF_PAGE page = LoadPage(0);
3353 ASSERT_TRUE(page);
3354 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3355
3356 {
3357 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3358 ASSERT_TRUE(annot);
3359
3360 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3361 // testing.
3362 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3363 const size_t kExpectedSize = 1;
3364 ASSERT_EQ(kExpectedSize, size);
3365 const unsigned long kPathIndex = 0;
3366 unsigned long path_size =
3367 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3368 const size_t kExpectedPathSize = 3;
3369 ASSERT_EQ(kExpectedPathSize, path_size);
3370 std::vector<FS_POINTF> path_buffer(path_size);
3371 EXPECT_EQ(path_size,
3372 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3373 path_buffer.data(), path_size));
3374 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3375 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3376 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3377 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3378 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3379 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3380
3381 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3382 // testing.
3383 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3384 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3385
3386 // out of bounds path_index.
3387 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3388
3389 // path_buffer is not overwritten if it is too small.
3390 path_buffer.resize(1);
3391 path_buffer[0].x = 42;
3392 path_buffer[0].y = 43;
3393 path_size = FPDFAnnot_GetInkListPath(
3394 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3395 EXPECT_EQ(kExpectedSize, size);
3396 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3397 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3398 }
3399
3400 {
3401 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3402 ASSERT_TRUE(annot);
3403
3404 // This has an odd number of elements in the path array, ignore the last
3405 // element.
3406 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3407 const size_t kExpectedSize = 1;
3408 ASSERT_EQ(kExpectedSize, size);
3409 const unsigned long kPathIndex = 0;
3410 unsigned long path_size =
3411 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3412 const size_t kExpectedPathSize = 3;
3413 ASSERT_EQ(kExpectedPathSize, path_size);
3414 std::vector<FS_POINTF> path_buffer(path_size);
3415 EXPECT_EQ(path_size,
3416 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3417 path_buffer.data(), path_size));
3418 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3419 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3420 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3421 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3422 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3423 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3424 }
3425
3426 {
3427 // Wrong annotation type.
3428 ScopedFPDFAnnotation polygon_annot(
3429 FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON));
3430 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3431 const unsigned long kPathIndex = 0;
3432 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3433 nullptr, 0));
3434 }
3435
3436 UnloadPage(page);
3437}
Miklos Vajna30f45a62020-12-04 19:12:31 +00003438
3439TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) {
3440 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3441 FPDF_PAGE page = LoadPage(0);
3442 ASSERT_TRUE(page);
3443 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3444
3445 {
3446 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3447 ASSERT_TRUE(annot);
3448
3449 // FPDFAnnot_GetVertices() positive testing.
3450 FS_POINTF start;
3451 FS_POINTF end;
3452 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3453 EXPECT_FLOAT_EQ(159.0f, start.x);
3454 EXPECT_FLOAT_EQ(296.0f, start.y);
3455 EXPECT_FLOAT_EQ(472.0f, end.x);
3456 EXPECT_FLOAT_EQ(243.42f, end.y);
3457
3458 // FPDFAnnot_GetVertices() negative testing.
3459 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3460 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3461 }
3462
3463 {
3464 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3465 ASSERT_TRUE(annot);
3466
3467 // Too few elements in the line array.
3468 FS_POINTF start;
3469 FS_POINTF end;
3470 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3471 }
3472
3473 {
3474 // Wrong annotation type.
3475 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3476 FS_POINTF start;
3477 FS_POINTF end;
3478 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3479 }
3480
3481 UnloadPage(page);
3482}
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003483
3484TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) {
3485 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3486 FPDF_PAGE page = LoadPage(0);
3487 ASSERT_TRUE(page);
3488 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3489
3490 {
3491 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3492 ASSERT_TRUE(annot);
3493
3494 // FPDFAnnot_GetBorder() positive testing.
3495 float horizontal_radius;
3496 float vertical_radius;
3497 float border_width;
3498 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3499 &vertical_radius, &border_width));
3500 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3501 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3502 EXPECT_FLOAT_EQ(2.0f, border_width);
3503
3504 // FPDFAnnot_GetBorder() negative testing.
3505 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3506 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3507 }
3508
3509 {
3510 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3511 ASSERT_TRUE(annot);
3512
3513 // Too few elements in the border array.
3514 float horizontal_radius;
3515 float vertical_radius;
3516 float border_width;
3517 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3518 &vertical_radius, &border_width));
Lei Zhang21ea6652021-04-15 23:24:46 +00003519
3520 // FPDFAnnot_SetBorder() positive testing.
3521 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3522 /*vertical_radius=*/3.5f,
3523 /*border_width=*/4.0f));
3524
3525 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3526 &vertical_radius, &border_width));
3527 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3528 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3529 EXPECT_FLOAT_EQ(4.0f, border_width);
3530
3531 // FPDFAnnot_SetBorder() negative testing.
3532 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3533 /*vertical_radius=*/2.5f,
3534 /*border_width=*/3.0f));
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003535 }
3536
3537 UnloadPage(page);
3538}
Lei Zhang24de1492021-04-19 18:06:43 +00003539
3540// Due to https://crbug.com/pdfium/570, the AnnotationBorder test above cannot
3541// actually render the line annotations inside line_annot.pdf. For now, use a
3542// square annotation in annots.pdf for testing.
3543TEST_F(FPDFAnnotEmbedderTest, AnnotationBorderRendering) {
3544 ASSERT_TRUE(OpenDocument("annots.pdf"));
3545 FPDF_PAGE page = LoadPage(1);
3546 ASSERT_TRUE(page);
3547 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
3548
3549#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003550 constexpr char kOriginalChecksum[] = "4f35703e89202bcc8419ca2df739bb4e";
3551 constexpr char kModifiedChecksum[] = "cee0a1b41f33d487af8fb70c4c82e3c9";
Lei Zhang42d30c22022-01-12 19:24:43 +00003552#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003553 constexpr char kOriginalChecksum[] = "522a4a6b6c7eab5bf95ded1f21ea372e";
3554 constexpr char kModifiedChecksum[] = "6844019e07b83cc01723415f58218d06";
Lei Zhang24de1492021-04-19 18:06:43 +00003555#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003556 constexpr char kOriginalChecksum[] = "12127303aecd80c6288460f7c0d79f3f";
3557 constexpr char kModifiedChecksum[] = "73d06ff4c665fe85029acef30240dcca";
Lei Zhang24de1492021-04-19 18:06:43 +00003558#endif
3559
3560 {
3561 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
3562 ASSERT_TRUE(annot);
3563 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
3564
3565 {
3566 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3567 CompareBitmap(bitmap.get(), 612, 792, kOriginalChecksum);
3568 }
3569
3570 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3571 /*vertical_radius=*/3.5f,
3572 /*border_width=*/4.0f));
3573
3574 {
3575 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3576 CompareBitmap(bitmap.get(), 612, 792, kModifiedChecksum);
3577 }
3578 }
3579
3580 // Save the document and close the page.
3581 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
3582 UnloadPage(page);
3583
3584 ASSERT_TRUE(OpenSavedDocument());
3585 page = LoadSavedPage(1);
3586 ASSERT_TRUE(page);
3587 VerifySavedRendering(page, 612, 792, kModifiedChecksum);
3588
3589 CloseSavedPage(page);
3590 CloseSavedDocument();
3591}