blob: 63affb9a553dde299c93b679507a1ea847d7477d [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_)
Hui Yingstbf9eee12022-05-25 21:20:24 +00001101 static const char kMd5ModifiedPath[] = "f671765166acf45d80e833ea3aff8b90";
1102 static const char kMd5TwoPaths[] = "7d2db46e1ae6bcf88d18d334af309551";
1103 static const char kMd5NewAnnot[] = "92bfb06058ff608571a3baf65f7fc05d";
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_)
Hui Yingstbf9eee12022-05-25 21:20:24 +00001314 static const char kMd5NewImage[] = "bf158b64c0373f3f36e347ae83e55cde";
1315 static const char kMd5ModifiedImage[] = "5806fadc1a192bc4bb07511a0711c957";
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) {
Hui Yingstbf9eee12022-05-25 21:20:24 +00001398#if defined(_SKIA_SUPPORT_)
1399 static const char kMd5NewText[] = "63b931799a9ba21c36d9d4f9711f252b";
1400 static const char kMd5ModifiedText[] = "e29ddba6a49d5c9c5cdde7d1693a251c";
1401#elif defined(_SKIA_SUPPORT_PATHS_)
1402 static const char kMd5NewText[] = "e2a563fe60b263342347b84199649899";
1403 static const char kMd5ModifiedText[] = "6052d53a7de28382e305d22edfb93873";
Lei Zhang42d30c22022-01-12 19:24:43 +00001404#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001405 static const char kMd5NewText[] = "57a0fb3fba33e17de26bcde4c40b9a75";
1406 static const char kMd5ModifiedText[] = "072574999f2e3f36774ee0b5bc94d4dd";
Jane Liu36567742017-07-06 11:13:35 -04001407#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001408 static const char kMd5NewText[] = "1c4198c38f890c208c5cbaad57be4dc6";
1409 static const char kMd5ModifiedText[] = "cfa78d01406865f41f486bd34a8b9f7b";
Jane Liu36567742017-07-06 11:13:35 -04001410#endif
1411
1412 // Open a file with two annotations and load its first page.
1413 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001414 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001415 ASSERT_TRUE(page);
1416 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1417
1418 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001419 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001420 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001421 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001422 }
Jane Liu36567742017-07-06 11:13:35 -04001423
Lei Zhanga21d5932018-02-05 18:28:38 +00001424 {
1425 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001426 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001427 ASSERT_TRUE(annot);
1428 FS_RECTF rect;
1429 rect.left = 200.f;
1430 rect.bottom = 550.f;
1431 rect.right = 450.f;
1432 rect.top = 650.f;
1433 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001434
Lei Zhanga21d5932018-02-05 18:28:38 +00001435 // Add a translucent text object to the new annotation.
1436 FPDF_PAGEOBJECT text_object =
1437 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1438 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001439 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001440 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1441 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001442 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001443 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1444 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1445 }
Jane Liu36567742017-07-06 11:13:35 -04001446
1447 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001448 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001449 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001450 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001451 }
Jane Liu36567742017-07-06 11:13:35 -04001452
Lei Zhanga21d5932018-02-05 18:28:38 +00001453 {
1454 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001455 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001456 ASSERT_TRUE(annot);
1457 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1458 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1459 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001460
Lei Zhanga21d5932018-02-05 18:28:38 +00001461 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001462 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001463 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1464 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1465 }
Jane Liu36567742017-07-06 11:13:35 -04001466
1467 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001468 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001469 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001470 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001471 }
Jane Liu36567742017-07-06 11:13:35 -04001472
1473 // Remove the new annotation, and check that the page renders as before.
1474 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001475 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001476 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001477 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001478 }
Jane Liu36567742017-07-06 11:13:35 -04001479
1480 UnloadPage(page);
1481}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001482
Hui Yingst9b6b1542020-07-27 16:11:12 +00001483TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001484 // Open a file with four annotations and load its first page.
1485 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001486 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001487 ASSERT_TRUE(page);
1488
Lei Zhang4f556b82019-04-08 16:32:41 +00001489 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001490
Lei Zhanga21d5932018-02-05 18:28:38 +00001491 {
1492 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001493 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001494 ASSERT_TRUE(annot);
1495
1496 // Check that a non-existent key does not exist.
1497 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1498
1499 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001500 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001501 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001502 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1503 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1504 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001505
1506 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001507 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001508 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001509 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001510 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001511 ASSERT_EQ(66u, length_bytes);
1512 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1513 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1514 length_bytes));
1515 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1516 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001517
1518 // Check that the string value of the modified date is correct.
1519 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001520 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1521 nullptr, 0);
1522 ASSERT_EQ(44u, length_bytes);
1523 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001524 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001525 buf.data(), length_bytes));
1526 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001527
1528 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001529 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001530 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1531 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001532 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001533
Lei Zhangec618142021-04-13 17:36:46 +00001534 // Save the document and close the page.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001535 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001536 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001537
Hui Yingst9b6b1542020-07-27 16:11:12 +00001538#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstbf9eee12022-05-25 21:20:24 +00001539 static const char kMd5[] = "2b9078043cd6130fef4e8542dcda943e";
Lei Zhang42d30c22022-01-12 19:24:43 +00001540#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001541 static const char kMd5[] = "cd90315b250dfe08265ce0ac335c5f76";
Hui Yingst9b6b1542020-07-27 16:11:12 +00001542#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001543 static const char kMd5[] = "c4fb6911f2a87f490be196f8898de738";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001544#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001545
1546 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001547 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001548 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001549 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001550 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001551 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001552 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001553
Lei Zhanga21d5932018-02-05 18:28:38 +00001554 // Check that the string value of the modified date is the newly-set value.
1555 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001556 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001557 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001558 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001559 ASSERT_EQ(44u, length_bytes);
1560 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001561 EXPECT_EQ(44u,
1562 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001563 buf.data(), length_bytes));
1564 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001565 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001566
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001567 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001568 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001569}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001570
rycsmith3e785602019-03-05 21:48:36 +00001571TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001572 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001573 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1574 FPDF_PAGE page = LoadPage(0);
1575 ASSERT_TRUE(page);
1576 {
1577 // First two annotations do not have "MaxLen" attribute.
1578 for (int i = 0; i < 2; i++) {
1579 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1580 ASSERT_TRUE(annot);
1581
1582 // Verify that no "MaxLen" key present.
1583 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1584
1585 float value;
1586 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1587 }
1588
1589 // Annotation in index 2 has "MaxLen" of 10.
1590 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1591 ASSERT_TRUE(annot);
1592
1593 // Verify that "MaxLen" key present.
1594 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1595
1596 float value;
1597 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1598 EXPECT_FLOAT_EQ(10.0f, value);
1599
1600 // Check bad inputs.
1601 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1602 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1603 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1604 // Ask for key that exists but is not a number.
1605 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1606 }
1607
1608 UnloadPage(page);
1609}
1610
Lei Zhangab41f252018-12-23 03:10:50 +00001611TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001612 // Open a file with four annotations and load its first page.
1613 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001614 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001615 ASSERT_TRUE(page);
1616
Lei Zhanga21d5932018-02-05 18:28:38 +00001617 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001618 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1619 static constexpr size_t kExpectNormalAPLength = 73970;
1620
Lei Zhanga21d5932018-02-05 18:28:38 +00001621 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001622 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001623 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001624
Lei Zhanga21d5932018-02-05 18:28:38 +00001625 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001626 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001627 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001628 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001629
Lei Zhanga21d5932018-02-05 18:28:38 +00001630 // Check that the string value of an AP is not returned if the buffer is too
1631 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001632 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1633 // Write in the buffer to verify it's not overwritten.
1634 memcpy(buf.data(), "abcdefgh", 8);
1635 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001636 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001637 buf.data(), normal_length_bytes - 1));
1638 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001639
Lei Zhanga21d5932018-02-05 18:28:38 +00001640 // Check that the string value of an AP is returned through a buffer that is
1641 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001642 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001643 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001644 buf.data(), normal_length_bytes));
1645 EXPECT_EQ(kMd5NormalAP,
Lei Zhang0bd11f72022-05-11 00:10:54 +00001646 GenerateMD5Base16({reinterpret_cast<uint8_t*>(buf.data()),
1647 normal_length_bytes}));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001648
Lei Zhanga21d5932018-02-05 18:28:38 +00001649 // Check that the string value of an AP is returned through a buffer that is
1650 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001651 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1652 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001653 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001654 buf.data(), normal_length_bytes + 2));
1655 EXPECT_EQ(kMd5NormalAP,
Lei Zhang0bd11f72022-05-11 00:10:54 +00001656 GenerateMD5Base16({reinterpret_cast<uint8_t*>(buf.data()),
1657 normal_length_bytes}));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001658
Lei Zhanga21d5932018-02-05 18:28:38 +00001659 // Check that getting an AP for a mode that does not have an AP returns an
1660 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001661 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001662 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001663 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001664
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001665 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001666 EXPECT_EQ(2u,
1667 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001668 buf.data(), 1000));
1669 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001670
Lei Zhanga21d5932018-02-05 18:28:38 +00001671 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001672 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001673 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001674 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001675 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001676 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001677 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001678
Lei Zhanga21d5932018-02-05 18:28:38 +00001679 // Set the AP correctly now.
1680 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001681 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001682
Lei Zhanga21d5932018-02-05 18:28:38 +00001683 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001684 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001685 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001686 ASSERT_EQ(24u, rollover_length_bytes);
1687 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001688 EXPECT_EQ(24u,
1689 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001690 buf.data(), rollover_length_bytes));
1691 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001692
Lei Zhanga21d5932018-02-05 18:28:38 +00001693 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001694 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1695 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001696 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001697 buf.data(), normal_length_bytes));
1698 EXPECT_EQ(kMd5NormalAP,
Lei Zhang0bd11f72022-05-11 00:10:54 +00001699 GenerateMD5Base16({reinterpret_cast<uint8_t*>(buf.data()),
1700 normal_length_bytes}));
Lei Zhanga21d5932018-02-05 18:28:38 +00001701 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001702
1703 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001704 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001705 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001706
Lei Zhang0b494052019-01-31 21:41:15 +00001707 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001708 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001709 ASSERT_TRUE(page);
Lei Zhanga21d5932018-02-05 18:28:38 +00001710 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001711 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001712
Lei Zhanga21d5932018-02-05 18:28:38 +00001713 // Check that the new annotation value is equal to the value we set before
1714 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001715 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001716 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001717 ASSERT_EQ(24u, rollover_length_bytes);
1718 std::vector<FPDF_WCHAR> buf =
1719 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001720 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1721 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001722 buf.data(), rollover_length_bytes));
1723 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001724 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001725
1726 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001727 CloseSavedPage(page);
1728 CloseSavedDocument();
1729}
1730
Lei Zhangab41f252018-12-23 03:10:50 +00001731TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001732 // Open a file with four annotations and load its first page.
1733 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001734 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001735 ASSERT_TRUE(page);
1736
Lei Zhanga21d5932018-02-05 18:28:38 +00001737 {
1738 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001739 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001740 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001741
Lei Zhanga21d5932018-02-05 18:28:38 +00001742 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001743 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001744 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001745 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001746 EXPECT_EQ(73970u,
1747 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1748 nullptr, 0));
1749 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1750 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001751
Lei Zhanga21d5932018-02-05 18:28:38 +00001752 // Check that setting the Down AP to null removes the Down entry but keeps
1753 // Normal intact.
1754 EXPECT_TRUE(
1755 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1756 EXPECT_EQ(73970u,
1757 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1758 nullptr, 0));
1759 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1760 nullptr, 0));
1761 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001762
Lei Zhang75c81712018-02-08 17:22:39 +00001763 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001764}
1765
Lei Zhangab41f252018-12-23 03:10:50 +00001766TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001767 // Open a file with four annotations and load its first page.
1768 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001769 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001770 ASSERT_TRUE(page);
1771
Lei Zhanga21d5932018-02-05 18:28:38 +00001772 {
1773 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001774 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001775 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001776
Lei Zhanga21d5932018-02-05 18:28:38 +00001777 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001778 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001779 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001780 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001781 EXPECT_EQ(73970u,
1782 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1783 nullptr, 0));
1784 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1785 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001786
Lei Zhanga21d5932018-02-05 18:28:38 +00001787 // Check that setting the Normal AP to null removes the whole AP dictionary.
1788 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1789 nullptr));
1790 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1791 nullptr, 0));
1792 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1793 nullptr, 0));
1794 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001795
Lei Zhang75c81712018-02-08 17:22:39 +00001796 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001797}
1798
Lei Zhangab41f252018-12-23 03:10:50 +00001799TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001800 // Open a file with annotations and load its first page.
1801 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001802 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001803 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001804 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001805
Lei Zhanga21d5932018-02-05 18:28:38 +00001806 {
1807 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001808 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001809 ASSERT_TRUE(annot);
1810 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1811 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001812 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001813 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1814 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1815 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001816
Lei Zhanga21d5932018-02-05 18:28:38 +00001817 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001818 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001819 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1820 ASSERT_TRUE(popup);
1821 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1822 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1823 FS_RECTF rect;
1824 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1825 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1826 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001827
Lei Zhanga21d5932018-02-05 18:28:38 +00001828 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1829 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001830 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001831 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1832 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001833
Lei Zhanga21d5932018-02-05 18:28:38 +00001834 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1835 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001836 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001837 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001838 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1839 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001840 }
Jane Liu300bb272017-08-21 14:37:53 -04001841
Jane Liu300bb272017-08-21 14:37:53 -04001842 UnloadPage(page);
1843}
1844
Lei Zhangab41f252018-12-23 03:10:50 +00001845TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001846 // Open file with form text fields.
1847 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001848 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001849 ASSERT_TRUE(page);
1850
Lei Zhanga21d5932018-02-05 18:28:38 +00001851 {
1852 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001853 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001854 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001855
Lei Zhanga21d5932018-02-05 18:28:38 +00001856 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001857 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001858 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001859 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1860 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1861 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001862 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001863 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001864
Lei Zhanga21d5932018-02-05 18:28:38 +00001865 {
1866 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001867 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001868 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001869
Lei Zhanga21d5932018-02-05 18:28:38 +00001870 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001871 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001872 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001873 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1874 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1875 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001876 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1877 }
1878
1879 {
1880 // Retrieve the fourth annotation: user-editable password text field.
1881 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1882 ASSERT_TRUE(annot);
1883
1884 // Check that the flag values are as expected.
1885 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1886 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001887 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1888 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1889 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001890 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001891 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001892
1893 UnloadPage(page);
1894}
1895
Lei Zhangab41f252018-12-23 03:10:50 +00001896TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001897 // Open file with form text fields.
1898 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001899 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001900 ASSERT_TRUE(page);
1901
Lei Zhanga21d5932018-02-05 18:28:38 +00001902 {
1903 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001904 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001905 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001906
Lei Zhanga21d5932018-02-05 18:28:38 +00001907 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001908 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001909 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001910 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1911 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001912 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1913 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001914 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001915 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001916
Lei Zhanga21d5932018-02-05 18:28:38 +00001917 {
1918 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001919 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001920 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001921
Lei Zhanga21d5932018-02-05 18:28:38 +00001922 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001923 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001924 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001925 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1926 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001927 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1928 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001929 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001930 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001931
Lei Zhanga21d5932018-02-05 18:28:38 +00001932 {
1933 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001934 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001935 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001936
Lei Zhanga21d5932018-02-05 18:28:38 +00001937 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001938 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001939 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001940 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1941 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001942 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1943 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001944 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001945 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001946
1947 UnloadPage(page);
1948}
Diana Gage40870db2017-07-19 18:16:03 -07001949
Lei Zhangab41f252018-12-23 03:10:50 +00001950TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001951 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001952 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001953 FPDF_PAGE page = LoadPage(0);
1954 ASSERT_TRUE(page);
1955
1956 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001957 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1958 EXPECT_FALSE(
1959 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001960
Lei Zhang8da98232019-12-11 23:29:33 +00001961 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001962 {
1963 // Verify there is an annotation.
1964 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001965 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001966 EXPECT_TRUE(annot);
1967 }
1968
1969 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001970 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1971 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1972 EXPECT_FALSE(
1973 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001974
1975 UnloadPage(page);
1976}
1977
Lei Zhangab41f252018-12-23 03:10:50 +00001978TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001979 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001980 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001981 FPDF_PAGE page = LoadPage(0);
1982 ASSERT_TRUE(page);
1983
Lei Zhanga21d5932018-02-05 18:28:38 +00001984 {
1985 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001986 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001987 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001988 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001989 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001990
Lei Zhanga21d5932018-02-05 18:28:38 +00001991 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001992 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00001993 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1994 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001995 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1996 }
Diana Gage40870db2017-07-19 18:16:03 -07001997
Lei Zhanga21d5932018-02-05 18:28:38 +00001998 {
1999 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002000 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002001 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002002 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002003 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002004
Lei Zhanga21d5932018-02-05 18:28:38 +00002005 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002006 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002007 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002008 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2009 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002010 }
Diana Gage40870db2017-07-19 18:16:03 -07002011
2012 UnloadPage(page);
2013}
2014
Lei Zhangab41f252018-12-23 03:10:50 +00002015TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07002016 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002017 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002018 FPDF_PAGE page = LoadPage(0);
2019 ASSERT_TRUE(page);
2020
Lei Zhanga21d5932018-02-05 18:28:38 +00002021 {
2022 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002023 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002024 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002025 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002026 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002027
Lei Zhanga21d5932018-02-05 18:28:38 +00002028 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002029 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002030 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002031 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2032 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002033 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2034 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002035 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002036 }
Diana Gage40870db2017-07-19 18:16:03 -07002037
Lei Zhanga21d5932018-02-05 18:28:38 +00002038 {
2039 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002040 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002041 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002042 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002043 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002044
Lei Zhanga21d5932018-02-05 18:28:38 +00002045 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002046 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002047 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002048 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2049 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002050 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2051 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002052 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002053 }
Diana Gage40870db2017-07-19 18:16:03 -07002054
Lei Zhanga21d5932018-02-05 18:28:38 +00002055 {
2056 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002057 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002058 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002059 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002060 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002061
Lei Zhanga21d5932018-02-05 18:28:38 +00002062 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002063 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002064 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002065 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2066 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002067 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2068 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002069 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002070 }
Diana Gage40870db2017-07-19 18:16:03 -07002071
2072 UnloadPage(page);
2073}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002074
Hui Yingst603dcd82020-11-09 22:36:46 +00002075TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
Lei Zhang03e5e682019-09-16 19:45:55 +00002076#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst603dcd82020-11-09 22:36:46 +00002077 static const char kExpectedBitmap[] = "a1ea1ceebb26922fae576cb79ce63af0";
Lei Zhang03e5e682019-09-16 19:45:55 +00002078#else
Lei Zhang992e7e22019-02-04 19:20:58 +00002079 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
Hui Yingst603dcd82020-11-09 22:36:46 +00002080#endif
Tom Andersond4fe5f72021-12-03 20:52:52 +00002081 static constexpr size_t kExpectedSize = 1590;
Lei Zhang992e7e22019-02-04 19:20:58 +00002082
2083 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2084
2085 FPDF_PAGE page = LoadPage(0);
2086 ASSERT_TRUE(page);
2087
2088 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2089 EXPECT_EQ(kExpectedSize, GetString().size());
2090 ClearString();
2091
2092 for (size_t i = 0; i < 10; ++i) {
2093 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2094 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2095
2096 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2097 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2098 // equal, not bigger.
2099 EXPECT_LT(kExpectedSize, GetString().size());
2100 ClearString();
2101 }
2102
2103 UnloadPage(page);
2104}
2105
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002106TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2107 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2108 FPDF_PAGE page = LoadPage(0);
2109 ASSERT_TRUE(page);
2110 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2111
2112 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002113 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002114 static const size_t kBufSize = 12;
2115 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002116
2117 {
2118 // Add a text annotation to the page.
2119 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2120 ASSERT_TRUE(annot);
2121 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2122 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2123
2124 // Make sure there is no test key, add set a value there, and read it back.
2125 std::fill(buf.begin(), buf.end(), 'x');
2126 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002127 kBufSize));
2128 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002129
Lei Zhangf0f67682019-04-08 17:03:21 +00002130 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002131 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2132
2133 std::fill(buf.begin(), buf.end(), 'x');
2134 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002135 kBufSize));
2136 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002137 }
2138
Lei Zhang05ec64c2019-01-09 03:00:06 +00002139 {
2140 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2141 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002142 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002143 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002144 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2145 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2146 // Also do the same test for its appearance string.
2147 std::fill(buf.begin(), buf.end(), 'x');
2148 ASSERT_EQ(2u,
2149 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002150 buf.data(), kBufSize));
2151 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002152
Lei Zhangf0f67682019-04-08 17:03:21 +00002153 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002154 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2155 text.get()));
2156
2157 std::fill(buf.begin(), buf.end(), 'x');
2158 ASSERT_EQ(6u,
2159 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002160 buf.data(), kBufSize));
2161 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002162 }
2163
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002164 UnloadPage(page);
2165
2166 {
2167 // Save a copy, open the copy, and check the annotation again.
2168 // Note that it renders the rotation.
2169 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002170 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002171 FPDF_PAGE saved_page = LoadSavedPage(0);
2172 ASSERT_TRUE(saved_page);
2173
Lei Zhang05ec64c2019-01-09 03:00:06 +00002174 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002175 {
2176 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2177 ASSERT_TRUE(annot);
2178 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2179
2180 std::fill(buf.begin(), buf.end(), 'x');
2181 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002182 kBufSize));
2183 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002184 }
2185
Lei Zhang05ec64c2019-01-09 03:00:06 +00002186 {
2187 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2188 ASSERT_TRUE(annot);
2189 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2190 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2191
2192 std::fill(buf.begin(), buf.end(), 'x');
2193 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002194 kBufSize));
2195 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002196 }
2197
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002198 CloseSavedPage(saved_page);
2199 CloseSavedDocument();
2200 }
2201}
rycsmithcb752f32019-02-21 18:40:53 +00002202
2203TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2204 // Open a file with combobox widget annotations and load its first page.
2205 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2206 FPDF_PAGE page = LoadPage(0);
2207 ASSERT_TRUE(page);
2208
2209 {
2210 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2211 ASSERT_TRUE(annot);
2212
2213 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2214
2215 annot.reset(FPDFPage_GetAnnot(page, 1));
2216 ASSERT_TRUE(annot);
2217
2218 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002219
2220 // Check bad form handle / annot.
2221 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2222 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2223 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002224 }
2225
2226 UnloadPage(page);
2227}
2228
2229TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2230 // Open a file with listbox widget annotations and load its first page.
2231 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2232 FPDF_PAGE page = LoadPage(0);
2233 ASSERT_TRUE(page);
2234
2235 {
2236 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2237 ASSERT_TRUE(annot);
2238
2239 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2240
2241 annot.reset(FPDFPage_GetAnnot(page, 1));
2242 ASSERT_TRUE(annot);
2243
2244 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2245 }
2246
2247 UnloadPage(page);
2248}
2249
2250TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2251 // Open a file with ink annotations and load its first page.
2252 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2253 FPDF_PAGE page = LoadPage(0);
2254 ASSERT_TRUE(page);
2255
2256 {
2257 // annotations do not have "Opt" array and will return -1
2258 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2259 ASSERT_TRUE(annot);
2260
2261 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2262
2263 annot.reset(FPDFPage_GetAnnot(page, 1));
2264 ASSERT_TRUE(annot);
2265
2266 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2267 }
2268
2269 UnloadPage(page);
2270}
2271
2272TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2273 // Open a file with combobox widget annotations and load its first page.
2274 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2275 FPDF_PAGE page = LoadPage(0);
2276 ASSERT_TRUE(page);
2277
2278 {
2279 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2280 ASSERT_TRUE(annot);
2281
2282 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002283 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002284 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002285 ASSERT_EQ(8u, length_bytes);
2286 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002287 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002288 buf.data(), length_bytes));
2289 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002290
2291 annot.reset(FPDFPage_GetAnnot(page, 1));
2292 ASSERT_TRUE(annot);
2293
2294 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002295 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002296 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002297 ASSERT_EQ(12u, length_bytes);
2298 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002299 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002300 buf.data(), length_bytes));
2301 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002302
2303 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002304 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002305 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002306 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002307 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002308 buf.data(), length_bytes));
2309 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002310
Lei Zhange7033c82019-02-26 19:30:49 +00002311 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002312 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2313 nullptr, 0));
2314 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2315 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002316
2317 // Check bad form handle / annot.
2318 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2319 EXPECT_EQ(0u,
2320 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2321 EXPECT_EQ(0u,
2322 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002323 }
2324
2325 UnloadPage(page);
2326}
2327
2328TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2329 // Open a file with listbox widget annotations and load its first page.
2330 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2331 FPDF_PAGE page = LoadPage(0);
2332 ASSERT_TRUE(page);
2333
2334 {
2335 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2336 ASSERT_TRUE(annot);
2337
2338 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002339 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002340 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002341 ASSERT_EQ(8u, length_bytes);
2342 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002343 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002344 buf.data(), length_bytes));
2345 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002346
2347 annot.reset(FPDFPage_GetAnnot(page, 1));
2348 ASSERT_TRUE(annot);
2349
2350 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002351 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002352 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002353 ASSERT_EQ(12u, length_bytes);
2354 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002355 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002356 buf.data(), length_bytes));
2357 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002358
2359 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002360 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002361 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002362 ASSERT_EQ(18u, length_bytes);
2363 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002364 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002365 buf.data(), length_bytes));
2366 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002367
2368 // indices out of range
2369 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2370 nullptr, 0));
2371 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2372 nullptr, 0));
2373 }
2374
2375 UnloadPage(page);
2376}
2377
2378TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2379 // Open a file with ink annotations and load its first page.
2380 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2381 FPDF_PAGE page = LoadPage(0);
2382 ASSERT_TRUE(page);
2383
2384 {
2385 // annotations do not have "Opt" array and will return 0
2386 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2387 ASSERT_TRUE(annot);
2388
2389 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2390 nullptr, 0));
2391
2392 annot.reset(FPDFPage_GetAnnot(page, 1));
2393 ASSERT_TRUE(annot);
2394
2395 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2396 nullptr, 0));
2397 }
2398
2399 UnloadPage(page);
2400}
Ryan Smith09c23b12019-04-25 18:09:06 +00002401
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002402TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2403 // Open a file with combobox widget annotations and load its first page.
2404 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2405 FPDF_PAGE page = LoadPage(0);
2406 ASSERT_TRUE(page);
2407
2408 {
2409 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2410 ASSERT_TRUE(annot);
2411
2412 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2413 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2414 ASSERT_EQ(3, count);
2415 for (int i = 0; i < count; i++) {
2416 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2417 }
2418
2419 annot.reset(FPDFPage_GetAnnot(page, 1));
2420 ASSERT_TRUE(annot);
2421
2422 // Checks for Combobox with Values (/V) object which is just a string.
2423 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2424 ASSERT_EQ(26, count);
2425 for (int i = 0; i < count; i++) {
2426 EXPECT_EQ(i == 1,
2427 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2428 }
2429
2430 // Checks for index outside bound i.e. (index >= CountOption()).
2431 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2432 /*index=*/26));
2433 // Checks for negetive index.
2434 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2435 /*index=*/-1));
2436
2437 // Checks for bad form handle/annot.
2438 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2439 EXPECT_FALSE(
2440 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2441 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2442 }
2443
2444 UnloadPage(page);
2445}
2446
2447TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2448 // Open a file with listbox widget annotations and load its first page.
2449 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2450 FPDF_PAGE page = LoadPage(0);
2451 ASSERT_TRUE(page);
2452
2453 {
2454 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2455 ASSERT_TRUE(annot);
2456
2457 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2458 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2459 ASSERT_EQ(3, count);
2460 for (int i = 0; i < count; i++) {
2461 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2462 }
2463
2464 annot.reset(FPDFPage_GetAnnot(page, 1));
2465 ASSERT_TRUE(annot);
2466
2467 // Checks for Listbox with Values (/V) object which is just a string.
2468 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2469 ASSERT_EQ(26, count);
2470 for (int i = 0; i < count; i++) {
2471 EXPECT_EQ(i == 1,
2472 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2473 }
2474
2475 annot.reset(FPDFPage_GetAnnot(page, 3));
2476 ASSERT_TRUE(annot);
2477
2478 // Checks for Listbox with only Selected indices (/I) object which is an
2479 // array with multiple objects.
2480 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2481 ASSERT_EQ(5, count);
2482 for (int i = 0; i < count; i++) {
2483 bool expected = (i == 1 || i == 3);
2484 EXPECT_EQ(expected,
2485 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2486 }
2487
2488 annot.reset(FPDFPage_GetAnnot(page, 4));
2489 ASSERT_TRUE(annot);
2490
2491 // Checks for Listbox with Values (/V) object which is an array with
2492 // multiple objects.
2493 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2494 ASSERT_EQ(5, count);
2495 for (int i = 0; i < count; i++) {
2496 bool expected = (i == 2 || i == 4);
2497 EXPECT_EQ(expected,
2498 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2499 }
2500
2501 annot.reset(FPDFPage_GetAnnot(page, 5));
2502 ASSERT_TRUE(annot);
2503
2504 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2505 // objects conflict with different lengths.
2506 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2507 ASSERT_EQ(5, count);
2508 for (int i = 0; i < count; i++) {
2509 bool expected = (i == 0 || i == 2);
2510 EXPECT_EQ(expected,
2511 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2512 }
2513 }
2514
2515 UnloadPage(page);
2516}
2517
2518TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2519 // Open a file with multiple form field annotations and load its first page.
2520 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2521 FPDF_PAGE page = LoadPage(0);
2522 ASSERT_TRUE(page);
2523
2524 {
2525 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2526 ASSERT_TRUE(annot);
2527
2528 // Checks for link annotation.
2529 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2530 /*index=*/0));
2531
2532 annot.reset(FPDFPage_GetAnnot(page, 3));
2533 ASSERT_TRUE(annot);
2534
2535 // Checks for text field annotation.
2536 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2537 /*index=*/0));
2538 }
2539
2540 UnloadPage(page);
2541}
2542
Ryan Smith09c23b12019-04-25 18:09:06 +00002543TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2544 // Open a file with combobox annotations and load its first page.
2545 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2546 FPDF_PAGE page = LoadPage(0);
2547 ASSERT_TRUE(page);
2548
2549 {
2550 // All 3 widgets have Tf font size 12.
2551 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2552 ASSERT_TRUE(annot);
2553
2554 float value;
2555 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2556 EXPECT_EQ(12.0, value);
2557
2558 annot.reset(FPDFPage_GetAnnot(page, 1));
2559 ASSERT_TRUE(annot);
2560
2561 float value_two;
2562 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2563 EXPECT_EQ(12.0, value_two);
2564
2565 annot.reset(FPDFPage_GetAnnot(page, 2));
2566 ASSERT_TRUE(annot);
2567
2568 float value_three;
2569 ASSERT_TRUE(
2570 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2571 EXPECT_EQ(12.0, value_three);
2572 }
2573
2574 UnloadPage(page);
2575}
2576
2577TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2578 // Open a file with textfield annotations and load its first page.
2579 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2580 FPDF_PAGE page = LoadPage(0);
2581 ASSERT_TRUE(page);
2582
2583 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002584 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002585 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2586 ASSERT_TRUE(annot);
2587
2588 float value;
2589 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2590 EXPECT_EQ(12.0, value);
2591
2592 annot.reset(FPDFPage_GetAnnot(page, 1));
2593 ASSERT_TRUE(annot);
2594
2595 float value_two;
2596 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2597 EXPECT_EQ(12.0, value_two);
2598
2599 annot.reset(FPDFPage_GetAnnot(page, 2));
2600 ASSERT_TRUE(annot);
2601
2602 float value_three;
2603 ASSERT_TRUE(
2604 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2605 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002606
2607 float value_four;
2608 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2609 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002610 }
2611
2612 UnloadPage(page);
2613}
2614
2615TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2616 // Open a file with ink annotations and load its first page.
2617 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2618 FPDF_PAGE page = LoadPage(0);
2619 ASSERT_TRUE(page);
2620
2621 {
2622 // Annotations that do not have variable text and will return -1.
2623 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2624 ASSERT_TRUE(annot);
2625
2626 float value;
2627 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2628
2629 annot.reset(FPDFPage_GetAnnot(page, 1));
2630 ASSERT_TRUE(annot);
2631
2632 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2633 }
2634
2635 UnloadPage(page);
2636}
2637
2638TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2639 // Open a file with combobox annotations and load its first page.
2640 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2641 FPDF_PAGE page = LoadPage(0);
2642 ASSERT_TRUE(page);
2643
2644 {
2645 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2646 ASSERT_TRUE(annot);
2647
2648 // Check bad form handle / annot.
2649 float value;
2650 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2651 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2652 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2653 }
2654
2655 UnloadPage(page);
2656}
2657
2658TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2659 // Open a file with textfield annotations and load its first page.
2660 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2661 FPDF_PAGE page = LoadPage(0);
2662 ASSERT_TRUE(page);
2663
2664 {
2665 // Obtain the first annotation, a text field with negative font size, -12.
2666 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2667 ASSERT_TRUE(annot);
2668
2669 float value;
2670 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2671 EXPECT_EQ(-12.0, value);
2672 }
2673
2674 UnloadPage(page);
2675}
Ryan Smith23fdf892019-07-17 21:51:26 +00002676
2677TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2678 // Open a file with checkbox and radiobuttons widget annotations and load its
2679 // first page.
2680 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2681 FPDF_PAGE page = LoadPage(0);
2682 ASSERT_TRUE(page);
2683
2684 {
2685 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2686 ASSERT_TRUE(annot);
2687 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2688 }
2689
2690 UnloadPage(page);
2691}
2692
2693TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2694 // Open a file with checkbox and radiobutton widget annotations and load its
2695 // first page.
2696 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2697 FPDF_PAGE page = LoadPage(0);
2698 ASSERT_TRUE(page);
2699
2700 {
2701 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2702 ASSERT_TRUE(annot);
2703 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2704 }
2705
2706 UnloadPage(page);
2707}
2708
2709TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2710 // Open a file with checkbox and radiobutton widget annotations and load its
2711 // first page.
2712 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2713 FPDF_PAGE page = LoadPage(0);
2714 ASSERT_TRUE(page);
2715
2716 {
2717 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2718 ASSERT_TRUE(annot);
2719 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2720
2721 annot.reset(FPDFPage_GetAnnot(page, 6));
2722 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2723
2724 annot.reset(FPDFPage_GetAnnot(page, 7));
2725 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2726 }
2727
2728 UnloadPage(page);
2729}
2730
2731TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2732 // Open a file with checkbox and radiobutton widget annotations and load its
2733 // first page.
2734 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2735 FPDF_PAGE page = LoadPage(0);
2736 ASSERT_TRUE(page);
2737
2738 {
2739 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2740 ASSERT_TRUE(annot);
2741 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2742
2743 annot.reset(FPDFPage_GetAnnot(page, 3));
2744 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2745
2746 annot.reset(FPDFPage_GetAnnot(page, 4));
2747 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2748 }
2749
2750 UnloadPage(page);
2751}
2752
2753TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2754 // Open a file with checkbox and radiobuttons widget annotations and load its
2755 // first page.
2756 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2757 FPDF_PAGE page = LoadPage(0);
2758 ASSERT_TRUE(page);
2759
2760 {
2761 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2762 ASSERT_TRUE(annot);
2763 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2764 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2765 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2766 }
2767
2768 UnloadPage(page);
2769}
2770
2771TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2772 // Open a file with text widget annotations and load its first page.
2773 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2774 FPDF_PAGE page = LoadPage(0);
2775 ASSERT_TRUE(page);
2776
2777 {
2778 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2779 ASSERT_TRUE(annot);
2780 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2781 }
2782
2783 UnloadPage(page);
2784}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002785
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002786TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2787 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002788 FPDF_PAGE page = LoadPage(0);
2789 ASSERT_TRUE(page);
2790
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002791 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2792
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002793 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002794 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002795 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002796 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002797 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002798
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002799 constexpr int kExpectedAnnotTypes[] = {-1,
2800 FPDF_FORMFIELD_COMBOBOX,
2801 FPDF_FORMFIELD_LISTBOX,
2802 FPDF_FORMFIELD_TEXTFIELD,
2803 FPDF_FORMFIELD_CHECKBOX,
2804 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002805
Lei Zhang3111d402022-04-18 22:24:07 +00002806 for (size_t i = 0; i < std::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002807 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002808 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002809 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002810 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2811 }
2812 UnloadPage(page);
2813}
2814
2815TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2816 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2817 FPDF_PAGE page = LoadPage(0);
2818 ASSERT_TRUE(page);
2819
2820 {
2821 EXPECT_EQ(0u,
2822 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2823
2824 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2825 ASSERT_TRUE(annot);
2826
2827 EXPECT_EQ(0u,
2828 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2829
2830 unsigned long length_bytes =
2831 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2832 ASSERT_EQ(2u, length_bytes);
2833 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2834 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2835 buf.data(), length_bytes));
2836 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2837 }
2838 {
2839 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2840 ASSERT_TRUE(annot);
2841
2842 unsigned long length_bytes =
2843 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2844 ASSERT_EQ(18u, length_bytes);
2845 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2846 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2847 buf.data(), length_bytes));
2848 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2849 }
2850 UnloadPage(page);
2851}
2852
2853TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2854 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2855 FPDF_PAGE page = LoadPage(0);
2856 ASSERT_TRUE(page);
2857
2858 {
2859 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2860 ASSERT_TRUE(annot);
2861
2862 unsigned long length_bytes =
2863 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2864 ASSERT_EQ(2u, length_bytes);
2865 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2866 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2867 buf.data(), length_bytes));
2868 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2869 }
2870 {
2871 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2872 ASSERT_TRUE(annot);
2873
2874 unsigned long length_bytes =
2875 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2876 ASSERT_EQ(14u, length_bytes);
2877 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2878 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2879 buf.data(), length_bytes));
2880 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2881 }
2882 UnloadPage(page);
2883}
2884
2885TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2886 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2887 FPDF_PAGE page = LoadPage(0);
2888 ASSERT_TRUE(page);
2889
2890 {
2891 EXPECT_EQ(0u,
2892 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2893
2894 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2895 ASSERT_TRUE(annot);
2896
2897 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2898
2899 unsigned long length_bytes =
2900 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2901 ASSERT_EQ(18u, length_bytes);
2902 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2903 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2904 buf.data(), length_bytes));
2905 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2906 }
2907 UnloadPage(page);
2908}
2909
2910TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2911 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2912 FPDF_PAGE page = LoadPage(0);
2913 ASSERT_TRUE(page);
2914
2915 {
2916 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2917 ASSERT_TRUE(annot);
2918
2919 unsigned long length_bytes =
2920 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2921 ASSERT_EQ(30u, length_bytes);
2922 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2923 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2924 buf.data(), length_bytes));
2925 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2926 }
2927 UnloadPage(page);
2928}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002929
Lei Zhang9b444002020-04-17 17:35:23 +00002930TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2931 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002932 FPDF_PAGE page = LoadPage(0);
2933 ASSERT_TRUE(page);
2934
Lei Zhang9b444002020-04-17 17:35:23 +00002935 // Verify widgets are by default focusable.
2936 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2937 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002938
Lei Zhang9b444002020-04-17 17:35:23 +00002939 // Expected annot subtypes for page 0 of annots.pdf.
2940 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2941 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2942 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2943 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2944 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002945
Lei Zhang9b444002020-04-17 17:35:23 +00002946 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002947 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002948 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2949 kExpectedAnnotSubtypes,
2950 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002951
Lei Zhang9b444002020-04-17 17:35:23 +00002952 // Make no annotation type focusable using the preferred method.
2953 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2954 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002955
Lei Zhang9b444002020-04-17 17:35:23 +00002956 // Restore the focusable type count back to 1, then set it back to 0 using a
2957 // different method.
2958 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2959 ASSERT_TRUE(
2960 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2961 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002962
Lei Zhang9b444002020-04-17 17:35:23 +00002963 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002964 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002965
Lei Zhang9b444002020-04-17 17:35:23 +00002966 // Now make links focusable.
2967 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2968 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002969
Lei Zhang9b444002020-04-17 17:35:23 +00002970 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002971 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002972 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2973 kExpectedAnnotSubtypes,
2974 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002975
Lei Zhang9b444002020-04-17 17:35:23 +00002976 // Test invalid parameters.
2977 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang3111d402022-04-18 22:24:07 +00002978 std::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002979 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang3111d402022-04-18 22:24:07 +00002980 std::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002981 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2982
Lei Zhang9b444002020-04-17 17:35:23 +00002983 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2984 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2985 subtypes.size()));
2986 EXPECT_FALSE(
2987 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2988 EXPECT_FALSE(
2989 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002990
2991 UnloadPage(page);
2992}
Lei Zhang671aece2020-04-14 19:02:26 +00002993
Hui Yingstea3816d2020-07-28 22:35:11 +00002994TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00002995 ASSERT_TRUE(OpenDocument("annots.pdf"));
2996 FPDF_PAGE page = LoadPage(0);
2997 ASSERT_TRUE(page);
2998
2999 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003000#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstbf9eee12022-05-25 21:20:24 +00003001 static const char kMd5sum[] = "7b08d6e8c0423302755c110e17abf7de";
Lei Zhang42d30c22022-01-12 19:24:43 +00003002#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003003 static const char kMd5sum[] = "108a46c517c4eaace9982ee83e8e3296";
Lei Zhang671aece2020-04-14 19:02:26 +00003004#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003005 static const char kMd5sum[] = "5550d8dcb4d1af1f50e8b4bcaef2ee60";
Lei Zhang671aece2020-04-14 19:02:26 +00003006#endif
3007 // Check the initial rendering.
3008 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3009 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3010 }
3011
3012 // Make links and highlights focusable.
3013 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
3014 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang3111d402022-04-18 22:24:07 +00003015 constexpr int kSubTypesCount = std::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00003016 ASSERT_TRUE(
3017 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
3018 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3019 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
3020 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
3021 subtypes.size()));
3022 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
3023 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
3024
3025 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003026#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstbf9eee12022-05-25 21:20:24 +00003027 static const char kMd5sum[] = "371171ea3f000db6354b24a702b0312b";
Lei Zhang42d30c22022-01-12 19:24:43 +00003028#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003029 static const char kMd5sum[] = "eb3869335e7a219e1b5f25c1c6037b97";
Lei Zhang671aece2020-04-14 19:02:26 +00003030#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003031 static const char kMd5sum[] = "805fe7bb751ac4ed2b82bb66efe6db40";
Lei Zhang671aece2020-04-14 19:02:26 +00003032#endif
3033 // Focus the first link and check the rendering.
3034 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3035 ASSERT_TRUE(annot);
3036 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3037 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3038 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3039 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3040 }
3041
3042 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003043#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstbf9eee12022-05-25 21:20:24 +00003044 static const char kMd5sum[] = "4aba010a83b9d91722921fde6bf30cdc";
Lei Zhang42d30c22022-01-12 19:24:43 +00003045#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003046 static const char kMd5sum[] = "d20b1978da2362d3942ea0fc6d230997";
Lei Zhang671aece2020-04-14 19:02:26 +00003047#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003048 static const char kMd5sum[] = "c5c5dcb462af3ef5f43b298ec048feef";
Lei Zhang671aece2020-04-14 19:02:26 +00003049#endif
3050 // Focus the first highlight and check the rendering.
3051 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3052 ASSERT_TRUE(annot);
3053 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3054 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3055 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3056 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3057 }
3058
3059 UnloadPage(page);
3060}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003061
3062TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
3063 ASSERT_TRUE(OpenDocument("annots.pdf"));
3064 FPDF_PAGE page = LoadPage(0);
3065 ASSERT_TRUE(page);
3066 {
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003067 constexpr char kExpectedResult[] =
3068 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3069 "fpdf_text.h";
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003070
Lei Zhang306874b2021-04-16 00:33:36 +00003071 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3072 ASSERT_TRUE(annot);
3073 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3074 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()),
3075 kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003076 }
3077
3078 {
3079 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3080 ASSERT_TRUE(annot);
3081 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3082 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3083 }
3084
3085 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3086
3087 UnloadPage(page);
3088}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003089
3090TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3091 // Open a file with radio button widget annotations and load its first page.
3092 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3093 FPDF_PAGE page = LoadPage(0);
3094 ASSERT_TRUE(page);
3095
3096 {
3097 // Checks for bad annot.
3098 EXPECT_EQ(-1,
3099 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3100
3101 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3102 ASSERT_TRUE(annot);
3103
3104 // Checks for bad form handle.
3105 EXPECT_EQ(-1,
3106 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3107
3108 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3109 }
3110
3111 UnloadPage(page);
3112}
3113
3114TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3115 // Open a file with checkbox widget annotations and load its first page.
3116 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3117 FPDF_PAGE page = LoadPage(0);
3118 ASSERT_TRUE(page);
3119
3120 {
3121 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3122 ASSERT_TRUE(annot);
3123 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3124 }
3125
3126 UnloadPage(page);
3127}
3128
3129TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3130 // Open a file with ink annotations and load its first page.
3131 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3132 FPDF_PAGE page = LoadPage(0);
3133 ASSERT_TRUE(page);
3134
3135 {
3136 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3137 ASSERT_TRUE(annot);
3138 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3139 }
3140
3141 UnloadPage(page);
3142}
3143
3144TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3145 // Open a file with radio button widget annotations and load its first page.
3146 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3147 FPDF_PAGE page = LoadPage(0);
3148 ASSERT_TRUE(page);
3149
3150 {
3151 // Checks for bad annot.
3152 EXPECT_EQ(-1,
3153 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3154
3155 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3156 ASSERT_TRUE(annot);
3157
3158 // Checks for bad form handle.
3159 EXPECT_EQ(-1,
3160 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3161
3162 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3163 }
3164
3165 UnloadPage(page);
3166}
3167
3168TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3169 // Open a file with checkbox widget annotations and load its first page.
3170 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3171 FPDF_PAGE page = LoadPage(0);
3172 ASSERT_TRUE(page);
3173
3174 {
3175 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3176 ASSERT_TRUE(annot);
3177 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3178 }
3179
3180 UnloadPage(page);
3181}
3182
3183TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3184 // Open a file with ink annotations and load its first page.
3185 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3186 FPDF_PAGE page = LoadPage(0);
3187 ASSERT_TRUE(page);
3188
3189 {
3190 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3191 ASSERT_TRUE(annot);
3192 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3193 }
3194
3195 UnloadPage(page);
3196}
3197
3198TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3199 // Open a file with radio button widget annotations and load its first page.
3200 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3201 FPDF_PAGE page = LoadPage(0);
3202 ASSERT_TRUE(page);
3203
3204 {
3205 // Checks for bad annot.
3206 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3207 form_handle(), /*annot=*/nullptr,
3208 /*buffer=*/nullptr, /*buflen=*/0));
3209
3210 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3211 ASSERT_TRUE(annot);
3212
3213 // Checks for bad form handle.
3214 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3215 /*hHandle=*/nullptr, annot.get(),
3216 /*buffer=*/nullptr, /*buflen=*/0));
3217
3218 unsigned long length_bytes =
3219 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3220 /*buffer=*/nullptr, /*buflen=*/0);
3221 ASSERT_EQ(14u, length_bytes);
3222 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3223 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3224 buf.data(), length_bytes));
3225 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3226 }
3227
3228 UnloadPage(page);
3229}
3230
3231TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3232 // Open a file with checkbox widget annotations and load its first page.
3233 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3234 FPDF_PAGE page = LoadPage(0);
3235 ASSERT_TRUE(page);
3236
3237 {
3238 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3239 ASSERT_TRUE(annot);
3240
3241 unsigned long length_bytes =
3242 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3243 /*buffer=*/nullptr, /*buflen=*/0);
3244 ASSERT_EQ(8u, length_bytes);
3245 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3246 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3247 buf.data(), length_bytes));
3248 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3249 }
3250
3251 UnloadPage(page);
3252}
3253
3254TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3255 // Open a file with ink annotations and load its first page.
3256 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3257 FPDF_PAGE page = LoadPage(0);
3258 ASSERT_TRUE(page);
3259
3260 {
3261 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3262 ASSERT_TRUE(annot);
3263 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3264 /*buffer=*/nullptr,
3265 /*buflen=*/0));
3266 }
3267
3268 UnloadPage(page);
3269}
Miklos Vajnad1a24fb2020-10-26 18:07:13 +00003270
3271TEST_F(FPDFAnnotEmbedderTest, Redactannotation) {
3272 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3273 FPDF_PAGE page = LoadPage(0);
3274 ASSERT_TRUE(page);
3275 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3276
3277 {
3278 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3279 ASSERT_TRUE(annot);
3280 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3281 }
3282
3283 UnloadPage(page);
3284}
Miklos Vajna09ecef62020-11-10 21:50:38 +00003285
3286TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) {
3287 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3288 FPDF_PAGE page = LoadPage(0);
3289 ASSERT_TRUE(page);
3290 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3291
3292 {
3293 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3294 ASSERT_TRUE(annot);
3295
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003296 // FPDFAnnot_GetVertices() positive testing.
Miklos Vajna09ecef62020-11-10 21:50:38 +00003297 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3298 const size_t kExpectedSize = 3;
3299 ASSERT_EQ(kExpectedSize, size);
3300 std::vector<FS_POINTF> vertices_buffer(size);
3301 EXPECT_EQ(size,
3302 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3303 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3304 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3305 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3306 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3307 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3308 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3309
3310 // FPDFAnnot_GetVertices() negative testing.
3311 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3312
3313 // vertices_buffer is not overwritten if it is too small.
3314 vertices_buffer.resize(1);
3315 vertices_buffer[0].x = 42;
3316 vertices_buffer[0].y = 43;
3317 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3318 vertices_buffer.size());
3319 EXPECT_EQ(kExpectedSize, size);
3320 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3321 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3322 }
3323
3324 {
3325 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3326 ASSERT_TRUE(annot);
3327
3328 // This has an odd number of elements in the vertices array, ignore the last
3329 // element.
3330 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3331 const size_t kExpectedSize = 3;
3332 ASSERT_EQ(kExpectedSize, size);
3333 std::vector<FS_POINTF> vertices_buffer(size);
3334 EXPECT_EQ(size,
3335 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3336 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3337 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3338 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3339 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3340 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3341 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3342 }
3343
3344 {
3345 // Wrong annotation type.
3346 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3347 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3348 }
3349
3350 UnloadPage(page);
3351}
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003352
3353TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) {
3354 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3355 FPDF_PAGE page = LoadPage(0);
3356 ASSERT_TRUE(page);
3357 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3358
3359 {
3360 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3361 ASSERT_TRUE(annot);
3362
3363 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3364 // testing.
3365 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3366 const size_t kExpectedSize = 1;
3367 ASSERT_EQ(kExpectedSize, size);
3368 const unsigned long kPathIndex = 0;
3369 unsigned long path_size =
3370 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3371 const size_t kExpectedPathSize = 3;
3372 ASSERT_EQ(kExpectedPathSize, path_size);
3373 std::vector<FS_POINTF> path_buffer(path_size);
3374 EXPECT_EQ(path_size,
3375 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3376 path_buffer.data(), path_size));
3377 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3378 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3379 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3380 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3381 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3382 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3383
3384 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3385 // testing.
3386 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3387 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3388
3389 // out of bounds path_index.
3390 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3391
3392 // path_buffer is not overwritten if it is too small.
3393 path_buffer.resize(1);
3394 path_buffer[0].x = 42;
3395 path_buffer[0].y = 43;
3396 path_size = FPDFAnnot_GetInkListPath(
3397 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3398 EXPECT_EQ(kExpectedSize, size);
3399 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3400 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3401 }
3402
3403 {
3404 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3405 ASSERT_TRUE(annot);
3406
3407 // This has an odd number of elements in the path array, ignore the last
3408 // element.
3409 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3410 const size_t kExpectedSize = 1;
3411 ASSERT_EQ(kExpectedSize, size);
3412 const unsigned long kPathIndex = 0;
3413 unsigned long path_size =
3414 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3415 const size_t kExpectedPathSize = 3;
3416 ASSERT_EQ(kExpectedPathSize, path_size);
3417 std::vector<FS_POINTF> path_buffer(path_size);
3418 EXPECT_EQ(path_size,
3419 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3420 path_buffer.data(), path_size));
3421 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3422 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3423 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3424 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3425 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3426 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3427 }
3428
3429 {
3430 // Wrong annotation type.
3431 ScopedFPDFAnnotation polygon_annot(
3432 FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON));
3433 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3434 const unsigned long kPathIndex = 0;
3435 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3436 nullptr, 0));
3437 }
3438
3439 UnloadPage(page);
3440}
Miklos Vajna30f45a62020-12-04 19:12:31 +00003441
3442TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) {
3443 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3444 FPDF_PAGE page = LoadPage(0);
3445 ASSERT_TRUE(page);
3446 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3447
3448 {
3449 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3450 ASSERT_TRUE(annot);
3451
3452 // FPDFAnnot_GetVertices() positive testing.
3453 FS_POINTF start;
3454 FS_POINTF end;
3455 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3456 EXPECT_FLOAT_EQ(159.0f, start.x);
3457 EXPECT_FLOAT_EQ(296.0f, start.y);
3458 EXPECT_FLOAT_EQ(472.0f, end.x);
3459 EXPECT_FLOAT_EQ(243.42f, end.y);
3460
3461 // FPDFAnnot_GetVertices() negative testing.
3462 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3463 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3464 }
3465
3466 {
3467 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3468 ASSERT_TRUE(annot);
3469
3470 // Too few elements in the line array.
3471 FS_POINTF start;
3472 FS_POINTF end;
3473 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3474 }
3475
3476 {
3477 // Wrong annotation type.
3478 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3479 FS_POINTF start;
3480 FS_POINTF end;
3481 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3482 }
3483
3484 UnloadPage(page);
3485}
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003486
3487TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) {
3488 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3489 FPDF_PAGE page = LoadPage(0);
3490 ASSERT_TRUE(page);
3491 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3492
3493 {
3494 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3495 ASSERT_TRUE(annot);
3496
3497 // FPDFAnnot_GetBorder() positive testing.
3498 float horizontal_radius;
3499 float vertical_radius;
3500 float border_width;
3501 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3502 &vertical_radius, &border_width));
3503 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3504 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3505 EXPECT_FLOAT_EQ(2.0f, border_width);
3506
3507 // FPDFAnnot_GetBorder() negative testing.
3508 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3509 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3510 }
3511
3512 {
3513 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3514 ASSERT_TRUE(annot);
3515
3516 // Too few elements in the border array.
3517 float horizontal_radius;
3518 float vertical_radius;
3519 float border_width;
3520 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3521 &vertical_radius, &border_width));
Lei Zhang21ea6652021-04-15 23:24:46 +00003522
3523 // FPDFAnnot_SetBorder() positive testing.
3524 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3525 /*vertical_radius=*/3.5f,
3526 /*border_width=*/4.0f));
3527
3528 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3529 &vertical_radius, &border_width));
3530 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3531 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3532 EXPECT_FLOAT_EQ(4.0f, border_width);
3533
3534 // FPDFAnnot_SetBorder() negative testing.
3535 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3536 /*vertical_radius=*/2.5f,
3537 /*border_width=*/3.0f));
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003538 }
3539
3540 UnloadPage(page);
3541}
Lei Zhang24de1492021-04-19 18:06:43 +00003542
3543// Due to https://crbug.com/pdfium/570, the AnnotationBorder test above cannot
3544// actually render the line annotations inside line_annot.pdf. For now, use a
3545// square annotation in annots.pdf for testing.
3546TEST_F(FPDFAnnotEmbedderTest, AnnotationBorderRendering) {
3547 ASSERT_TRUE(OpenDocument("annots.pdf"));
3548 FPDF_PAGE page = LoadPage(1);
3549 ASSERT_TRUE(page);
3550 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
3551
Hui Yingstbf9eee12022-05-25 21:20:24 +00003552#if BUILDFLAG(IS_APPLE) && !defined(_SKIA_SUPPORT_) && \
3553 !defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003554 constexpr char kOriginalChecksum[] = "522a4a6b6c7eab5bf95ded1f21ea372e";
3555 constexpr char kModifiedChecksum[] = "6844019e07b83cc01723415f58218d06";
Lei Zhang24de1492021-04-19 18:06:43 +00003556#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003557 constexpr char kOriginalChecksum[] = "12127303aecd80c6288460f7c0d79f3f";
3558 constexpr char kModifiedChecksum[] = "73d06ff4c665fe85029acef30240dcca";
Lei Zhang24de1492021-04-19 18:06:43 +00003559#endif
3560
3561 {
3562 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
3563 ASSERT_TRUE(annot);
3564 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
3565
3566 {
3567 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3568 CompareBitmap(bitmap.get(), 612, 792, kOriginalChecksum);
3569 }
3570
3571 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3572 /*vertical_radius=*/3.5f,
3573 /*border_width=*/4.0f));
3574
3575 {
3576 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3577 CompareBitmap(bitmap.get(), 612, 792, kModifiedChecksum);
3578 }
3579 }
3580
3581 // Save the document and close the page.
3582 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
3583 UnloadPage(page);
3584
3585 ASSERT_TRUE(OpenSavedDocument());
3586 page = LoadSavedPage(1);
3587 ASSERT_TRUE(page);
3588 VerifySavedRendering(page, 612, 792, kModifiedChecksum);
3589
3590 CloseSavedPage(page);
3591 CloseSavedDocument();
3592}