blob: 31c1c6409795ab3533df32b7a7c8550bb23be4f7 [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 Zhang532886d2021-06-17 19:10:08 +000031#include "third_party/base/cxx17_backports.h"
Lei Zhang9b444002020-04-17 17:35:23 +000032#include "third_party/base/span.h"
33
Hui Yingst30bfcc52020-07-27 23:54:40 +000034using pdfium::kAnnotationStampWithApChecksum;
35
Lei Zhang9b444002020-04-17 17:35:23 +000036namespace {
37
Tom Sepez204ab052020-06-12 21:33:48 +000038const wchar_t kStreamData[] =
39 L"/GS gs 0.0 0.0 0.0 RG 4 w 211.8 747.6 m 211.8 744.8 "
40 L"212.6 743.0 214.2 740.8 "
41 L"c 215.4 739.0 216.8 737.1 218.9 736.1 c 220.8 735.1 221.4 733.0 "
42 L"223.7 732.4 c 232.6 729.9 242.0 730.8 251.2 730.8 c 257.5 730.8 "
43 L"263.0 732.9 269.0 734.4 c S";
44
Lei Zhang9b444002020-04-17 17:35:23 +000045void VerifyFocusableAnnotSubtypes(
46 FPDF_FORMHANDLE form_handle,
47 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) {
48 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
49 FPDFAnnot_GetFocusableSubtypesCount(form_handle));
50
51 std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes(
52 expected_subtypes.size());
53 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(
54 form_handle, actual_subtypes.data(), actual_subtypes.size()));
55 for (size_t i = 0; i < expected_subtypes.size(); ++i)
56 ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]);
57}
58
59void SetAndVerifyFocusableAnnotSubtypes(
60 FPDF_FORMHANDLE form_handle,
61 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) {
62 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(),
63 subtypes.size()));
64 VerifyFocusableAnnotSubtypes(form_handle, subtypes);
65}
66
67void VerifyAnnotationSubtypesAndFocusability(
68 FPDF_FORMHANDLE form_handle,
69 FPDF_PAGE page,
70 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes,
71 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) {
72 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
73 FPDFPage_GetAnnotCount(page));
74 for (size_t i = 0; i < expected_subtypes.size(); ++i) {
75 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
76 ASSERT_TRUE(annot);
77 EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get()));
78
Lei Zhangf245ae62020-05-15 21:49:46 +000079 bool expected_focusable =
80 pdfium::Contains(expected_focusable_subtypes, expected_subtypes[i]);
Lei Zhang9b444002020-04-17 17:35:23 +000081 EXPECT_EQ(expected_focusable,
82 FORM_SetFocusedAnnot(form_handle, annot.get()));
83
84 // Kill the focus so the next test starts in an unfocused state.
85 FORM_ForceToKillFocus(form_handle);
86 }
87}
88
Lei Zhang306874b2021-04-16 00:33:36 +000089void VerifyUriActionInLink(FPDF_DOCUMENT doc,
90 FPDF_LINK link,
91 const std::string& expected_uri) {
92 ASSERT_TRUE(link);
93
94 FPDF_ACTION action = FPDFLink_GetAction(link);
95 ASSERT_TRUE(action);
96 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
97 FPDFAction_GetType(action));
98
99 unsigned long bufsize = FPDFAction_GetURIPath(doc, action, nullptr, 0);
100 ASSERT_EQ(expected_uri.size() + 1, bufsize);
101
102 std::vector<char> buffer(bufsize);
103 EXPECT_EQ(bufsize,
104 FPDFAction_GetURIPath(doc, action, buffer.data(), bufsize));
105 EXPECT_STREQ(expected_uri.c_str(), buffer.data());
106}
107
Lei Zhang9b444002020-04-17 17:35:23 +0000108} // namespace
Jane Liu4fd9a472017-06-01 18:56:09 -0400109
Lei Zhangab41f252018-12-23 03:10:50 +0000110class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -0400111
Tom Sepez204ab052020-06-12 21:33:48 +0000112TEST_F(FPDFAnnotEmbedderTest, SetAP) {
113 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
114 ASSERT_TRUE(doc);
115 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
116 ASSERT_TRUE(page);
117 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
118 ASSERT_TRUE(ap_stream);
119
120 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
121 ASSERT_TRUE(annot);
122
123 // Negative case: FPDFAnnot_SetAP() should fail if bounding rect is not yet
124 // set on the annotation.
125 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
126 ap_stream.get()));
127
128 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
129 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
130
131 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
132 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/255));
133
134 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
135 ap_stream.get()));
136
137 // Verify that appearance stream is created as form XObject
138 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
139 ASSERT_TRUE(context);
140 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
141 ASSERT_TRUE(annot_dict);
142 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
143 ASSERT_TRUE(ap_dict);
144 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
145 ASSERT_TRUE(stream_dict);
146 // Check for non-existence of resources dictionary in case of opaque color
147 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
148 ASSERT_FALSE(resources_dict);
149 ByteString type = stream_dict->GetStringFor(pdfium::annotation::kType);
150 EXPECT_EQ("XObject", type);
151 ByteString sub_type = stream_dict->GetStringFor(pdfium::annotation::kSubtype);
152 EXPECT_EQ("Form", sub_type);
153
154 // Check that the appearance stream is same as we just set.
155 const uint32_t kStreamDataSize =
156 pdfium::size(kStreamData) * sizeof(FPDF_WCHAR);
157 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
158 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
159 ASSERT_EQ(kStreamDataSize, normal_length_bytes);
160 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
161 EXPECT_EQ(kStreamDataSize,
162 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
163 buf.data(), normal_length_bytes));
164 EXPECT_EQ(kStreamData, GetPlatformWString(buf.data()));
165}
166
167TEST_F(FPDFAnnotEmbedderTest, SetAPWithOpacity) {
168 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
169 ASSERT_TRUE(doc);
170 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
171 ASSERT_TRUE(page);
172 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
173 ASSERT_TRUE(ap_stream);
174
175 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
176 ASSERT_TRUE(annot);
177
178 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
179 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/102));
180
181 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
182 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
183
184 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
185 ap_stream.get()));
186
187 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
188 ASSERT_TRUE(context);
189 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
190 ASSERT_TRUE(annot_dict);
191 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
192 ASSERT_TRUE(ap_dict);
193 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
194 ASSERT_TRUE(stream_dict);
195 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
196 ASSERT_TRUE(stream_dict);
197 CPDF_Dictionary* extGState_dict = resources_dict->GetDictFor("ExtGState");
198 ASSERT_TRUE(extGState_dict);
199 CPDF_Dictionary* gs_dict = extGState_dict->GetDictFor("GS");
200 ASSERT_TRUE(gs_dict);
201 ByteString type = gs_dict->GetStringFor(pdfium::annotation::kType);
202 EXPECT_EQ("ExtGState", type);
203 float opacity = gs_dict->GetNumberFor("CA");
204 // Opacity value of 102 is represented as 0.4f (=104/255) in /CA entry.
205 EXPECT_FLOAT_EQ(0.4f, opacity);
206 ByteString blend_mode = gs_dict->GetStringFor("BM");
207 EXPECT_EQ("Normal", blend_mode);
208 bool alpha_source_flag = gs_dict->GetBooleanFor("AIS", true);
209 EXPECT_FALSE(alpha_source_flag);
210}
211
212TEST_F(FPDFAnnotEmbedderTest, InkListAPIValidations) {
213 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
214 ASSERT_TRUE(doc);
215 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
216 ASSERT_TRUE(page);
217
218 // Create a new ink annotation.
219 ScopedFPDFAnnotation ink_annot(
220 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
221 ASSERT_TRUE(ink_annot);
222 CPDF_AnnotContext* context =
223 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
224 ASSERT_TRUE(context);
225 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
226 ASSERT_TRUE(annot_dict);
227
228 static constexpr FS_POINTF kFirstInkStroke[] = {
229 {80.0f, 90.0f}, {81.0f, 91.0f}, {82.0f, 92.0f},
230 {83.0f, 93.0f}, {84.0f, 94.0f}, {85.0f, 95.0f}};
231 static constexpr size_t kFirstStrokePointCount =
232 pdfium::size(kFirstInkStroke);
233
234 static constexpr FS_POINTF kSecondInkStroke[] = {
235 {70.0f, 90.0f}, {71.0f, 91.0f}, {72.0f, 92.0f}};
236 static constexpr size_t kSecondStrokePointCount =
237 pdfium::size(kSecondInkStroke);
238
239 static constexpr FS_POINTF kThirdInkStroke[] = {{60.0f, 90.0f},
240 {61.0f, 91.0f},
241 {62.0f, 92.0f},
242 {63.0f, 93.0f},
243 {64.0f, 94.0f}};
244 static constexpr size_t kThirdStrokePointCount =
245 pdfium::size(kThirdInkStroke);
246
247 // Negative test: |annot| is passed as nullptr.
248 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(nullptr, kFirstInkStroke,
249 kFirstStrokePointCount));
250
251 // Negative test: |annot| is not ink annotation.
252 // Create a new highlight annotation.
253 ScopedFPDFAnnotation highlight_annot(
254 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
255 ASSERT_TRUE(highlight_annot);
256 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(highlight_annot.get(), kFirstInkStroke,
257 kFirstStrokePointCount));
258
259 // Negative test: passing |point_count| as 0.
260 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke, 0));
261
262 // Negative test: passing |points| array as nullptr.
263 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), nullptr,
264 kFirstStrokePointCount));
265
266 // Negative test: passing |point_count| more than ULONG_MAX/2.
267 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
268 ULONG_MAX / 2 + 1));
269
270 // InkStroke should get added to ink annotation. Also inklist should get
271 // created.
272 EXPECT_EQ(0, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke,
273 kFirstStrokePointCount));
274
275 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
276 ASSERT_TRUE(inklist);
277 EXPECT_EQ(1u, inklist->size());
278 EXPECT_EQ(kFirstStrokePointCount * 2, inklist->GetArrayAt(0)->size());
279
280 // Adding another inkStroke to ink annotation with all valid paremeters.
281 // InkList already exists in ink_annot.
282 EXPECT_EQ(1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
283 kSecondStrokePointCount));
284 EXPECT_EQ(2u, inklist->size());
285 EXPECT_EQ(kSecondStrokePointCount * 2, inklist->GetArrayAt(1)->size());
286
287 // Adding one more InkStroke to the ink annotation. |point_count| passed is
288 // less than the data available in |buffer|.
289 EXPECT_EQ(2, FPDFAnnot_AddInkStroke(ink_annot.get(), kThirdInkStroke,
290 kThirdStrokePointCount - 1));
291 EXPECT_EQ(3u, inklist->size());
292 EXPECT_EQ((kThirdStrokePointCount - 1) * 2, inklist->GetArrayAt(2)->size());
293}
294
295TEST_F(FPDFAnnotEmbedderTest, RemoveInkList) {
296 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
297 ASSERT_TRUE(doc);
298 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
299 ASSERT_TRUE(page);
300
301 // Negative test: |annot| is passed as nullptr.
302 EXPECT_FALSE(FPDFAnnot_RemoveInkList(nullptr));
303
304 // Negative test: |annot| is not ink annotation.
305 // Create a new highlight annotation.
306 ScopedFPDFAnnotation highlight_annot(
307 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
308 ASSERT_TRUE(highlight_annot);
309 EXPECT_FALSE(FPDFAnnot_RemoveInkList(highlight_annot.get()));
310
311 // Create a new ink annotation.
312 ScopedFPDFAnnotation ink_annot(
313 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
314 ASSERT_TRUE(ink_annot);
315 CPDF_AnnotContext* context =
316 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
317 ASSERT_TRUE(context);
318 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
319 ASSERT_TRUE(annot_dict);
320
321 static constexpr FS_POINTF kInkStroke[] = {{80.0f, 90.0f}, {81.0f, 91.0f},
322 {82.0f, 92.0f}, {83.0f, 93.0f},
323 {84.0f, 94.0f}, {85.0f, 95.0f}};
324 static constexpr size_t kPointCount = pdfium::size(kInkStroke);
325
326 // InkStroke should get added to ink annotation. Also inklist should get
327 // created.
328 EXPECT_EQ(0,
329 FPDFAnnot_AddInkStroke(ink_annot.get(), kInkStroke, kPointCount));
330
331 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
332 ASSERT_TRUE(inklist);
333 ASSERT_EQ(1u, inklist->size());
334 EXPECT_EQ(kPointCount * 2, inklist->GetArrayAt(0)->size());
335
336 // Remove inklist.
337 EXPECT_TRUE(FPDFAnnot_RemoveInkList(ink_annot.get()));
338 EXPECT_FALSE(annot_dict->KeyExist("InkList"));
339}
340
Lei Zhangab41f252018-12-23 03:10:50 +0000341TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +0000342 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
343 FPDF_PAGE page = LoadPage(0);
344 ASSERT_TRUE(page);
345
346 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
347
348 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
349 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
350 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
351 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
352 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
353
354 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
355
356 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
357
358 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
359 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
360 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
361
362 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
363
Lei Zhang4f556b82019-04-08 16:32:41 +0000364 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +0000365 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +0000366 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
367
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000368 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +0000369 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
370 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
371 EXPECT_EQ(0u,
372 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
373
374 UnloadPage(page);
375}
376
Lei Zhang3d9a0972019-03-04 19:34:09 +0000377TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
378 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
379 FPDF_PAGE page = LoadPage(0);
380 ASSERT_TRUE(page);
381
382 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +0000383 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +0000384
385 UnloadPage(page);
386}
387
Lei Zhangab41f252018-12-23 03:10:50 +0000388TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -0400389 // Open a file with one annotation and load its first page.
390 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000391 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -0400392 ASSERT_TRUE(page);
393
394 // This annotation has a malformed appearance stream, which does not have its
395 // normal appearance defined, only its rollover appearance. In this case, its
396 // normal appearance should be generated, allowing the highlight annotation to
397 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000398 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000399 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -0400400
401 UnloadPage(page);
402}
403
Hui Yingstd5b6f632020-07-22 01:31:59 +0000404TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang03e5e682019-09-16 19:45:55 +0000405#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstd5b6f632020-07-22 01:31:59 +0000406 static const char kChecksum[] = "ec1f4ccbd0aecfdea6d53893387a0101";
Lei Zhang03e5e682019-09-16 19:45:55 +0000407#else
Hui Yingstd5b6f632020-07-22 01:31:59 +0000408 static const char kChecksum[] = "76512832d88017668d9acc7aacd13dae";
Lei Zhang03e5e682019-09-16 19:45:55 +0000409#endif
Henrique Nakashima5098b252018-03-26 21:46:00 +0000410 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000411 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
412 FPDF_PAGE page = LoadPage(0);
413 ASSERT_TRUE(page);
414
Tom Sepeze08d2b12018-04-25 18:49:32 +0000415 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstd5b6f632020-07-22 01:31:59 +0000416 CompareBitmap(bitmap.get(), 595, 842, kChecksum);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000417
418 UnloadPage(page);
419}
420
Lei Zhangab41f252018-12-23 03:10:50 +0000421TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400422 // Open a file with one annotation and load its first page.
423 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000424 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400425 ASSERT_TRUE(page);
426
427 // Check that there is a total of 1 annotation on its first page.
428 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
429
430 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000431 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000432 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000433 ASSERT_TRUE(annot);
434 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400435
Lei Zhanga21d5932018-02-05 18:28:38 +0000436 // Check that the annotation color is yellow.
437 unsigned int R;
438 unsigned int G;
439 unsigned int B;
440 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000441 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000442 &G, &B, &A));
443 EXPECT_EQ(255u, R);
444 EXPECT_EQ(255u, G);
445 EXPECT_EQ(0u, B);
446 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400447
Lei Zhanga21d5932018-02-05 18:28:38 +0000448 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000449 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000450 EXPECT_EQ(FPDF_OBJECT_STRING,
451 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000452 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000453 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000454 ASSERT_EQ(28u, length_bytes);
455 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000456 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000457 length_bytes));
458 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400459
Lei Zhanga21d5932018-02-05 18:28:38 +0000460 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000461 EXPECT_EQ(
462 FPDF_OBJECT_STRING,
463 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000464 length_bytes = FPDFAnnot_GetStringValue(
465 annot.get(), pdfium::annotation::kContents, nullptr, 0);
466 ASSERT_EQ(2690u, length_bytes);
467 buf = GetFPDFWideStringBuffer(length_bytes);
468 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
469 pdfium::annotation::kContents,
470 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000471 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000472 L"This is a note for that highlight annotation. Very long highlight "
473 "annotation. Long long long Long 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 longLong long longLong long longLong long longLong long "
487 "longLong long longLong long longLong long longLong long longLong long "
488 "longLong long longLong long longLong long longLong long longLong long "
489 "longLong long longLong long longLong long longLong long longLong long "
490 "longLong long longLong long longLong long longLong long longLong long "
491 "longLong long long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000492 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400493
Lei Zhanga21d5932018-02-05 18:28:38 +0000494 // Check that the quadpoints are correct.
495 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000496 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000497 EXPECT_EQ(115.802643f, quadpoints.x1);
498 EXPECT_EQ(718.913940f, quadpoints.y1);
499 EXPECT_EQ(157.211182f, quadpoints.x4);
500 EXPECT_EQ(706.264465f, quadpoints.y4);
501 }
Tom Sepez507d0192018-11-07 16:37:51 +0000502 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400503}
504
Hui Yingst9b6b1542020-07-27 16:11:12 +0000505TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400506 // Open a file with three annotations and load its first page.
507 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000508 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400509 ASSERT_TRUE(page);
510
511 // Check that there is a total of 3 annotation on its first page.
512 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
513
Lei Zhanga21d5932018-02-05 18:28:38 +0000514 {
515 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000516 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000517 ASSERT_TRUE(annot);
518 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400519
Lei Zhanga21d5932018-02-05 18:28:38 +0000520 // Check that the annotation color is blue with opacity.
521 unsigned int R;
522 unsigned int G;
523 unsigned int B;
524 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000525 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000526 &G, &B, &A));
527 EXPECT_EQ(0u, R);
528 EXPECT_EQ(0u, G);
529 EXPECT_EQ(255u, B);
530 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400531
Lei Zhanga21d5932018-02-05 18:28:38 +0000532 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000533 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
534 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400535
Lei Zhang4f556b82019-04-08 16:32:41 +0000536 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000537 // Note that upon rendering, the rectangle coordinates will be adjusted.
538 FS_RECTF rect;
539 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
540 EXPECT_EQ(351.820404f, rect.left);
541 EXPECT_EQ(583.830688f, rect.bottom);
542 EXPECT_EQ(475.336090f, rect.right);
543 EXPECT_EQ(681.535034f, rect.top);
544 }
Tom Sepezef43c262018-11-07 16:41:32 +0000545 {
Lei Zhang42d30c22022-01-12 19:24:43 +0000546#if defined(_SKIA_SUPPORT_) && BUILDFLAG(IS_APPLE)
Hui Yingst57daee82020-10-09 05:47:20 +0000547 static constexpr char kExpectedHash[] = "fad91b9c968fe8019a774f5e2419b8fc";
Lei Zhang42d30c22022-01-12 19:24:43 +0000548#elif defined(_SKIA_SUPPORT_PATHS_) && BUILDFLAG(IS_APPLE)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000549 static constexpr char kExpectedHash[] = "acddfe688a117ead56af7b249a2cf8a1";
Hui Yingst57daee82020-10-09 05:47:20 +0000550#elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000551 static constexpr char kExpectedHash[] = "1fb0dd8dd5f0b9bb8d076e48eb59296d";
Lei Zhangf997abe2022-01-12 19:14:23 +0000552#elif BUILDFLAG(IS_WIN)
Lei Zhang430b5322020-07-06 22:23:36 +0000553 static constexpr char kExpectedHash[] = "49d0a81c636531a337429325273d0508";
554#else
555 static constexpr char kExpectedHash[] = "354002e1c4386d38fdde29ef8d61074a";
Hui Yingst57daee82020-10-09 05:47:20 +0000556#endif
Tom Sepezef43c262018-11-07 16:41:32 +0000557 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang430b5322020-07-06 22:23:36 +0000558 CompareBitmap(bitmap.get(), 612, 792, kExpectedHash);
Tom Sepezef43c262018-11-07 16:41:32 +0000559 }
Tom Sepez507d0192018-11-07 16:37:51 +0000560 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400561}
Jane Liu20eafda2017-06-07 10:33:24 -0400562
Lei Zhangab41f252018-12-23 03:10:50 +0000563TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400564 // Open a file with one annotation and load its first page.
565 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000566 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400567 ASSERT_TRUE(page);
568
569 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400570 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400571
572 UnloadPage(page);
573}
574
Lei Zhangab41f252018-12-23 03:10:50 +0000575TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400576 // Open a file with no annotation and load its first page.
577 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000578 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400579 ASSERT_TRUE(page);
580 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
581
Lei Zhanga21d5932018-02-05 18:28:38 +0000582 {
583 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000584 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000585 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400586
Lei Zhanga21d5932018-02-05 18:28:38 +0000587 // Check that there is now 1 annotations on this page.
588 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400589
Lei Zhanga21d5932018-02-05 18:28:38 +0000590 // Check that the subtype of the annotation is correct.
591 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
592 }
Jane Liue10509a2017-06-20 16:47:41 -0400593
Lei Zhanga21d5932018-02-05 18:28:38 +0000594 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000595 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000596 ASSERT_TRUE(annot);
597 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400598
Lei Zhanga21d5932018-02-05 18:28:38 +0000599 // Set the color of the annotation.
600 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
601 102, 153, 204));
602 // Check that the color has been set correctly.
603 unsigned int R;
604 unsigned int G;
605 unsigned int B;
606 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000607 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000608 &G, &B, &A));
609 EXPECT_EQ(51u, R);
610 EXPECT_EQ(102u, G);
611 EXPECT_EQ(153u, B);
612 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400613
Lei Zhanga21d5932018-02-05 18:28:38 +0000614 // Change the color of the annotation.
615 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
616 153, 102, 51));
617 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000618 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000619 &G, &B, &A));
620 EXPECT_EQ(204u, R);
621 EXPECT_EQ(153u, G);
622 EXPECT_EQ(102u, B);
623 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400624
Lei Zhanga21d5932018-02-05 18:28:38 +0000625 // Set the annotation rectangle.
626 FS_RECTF rect;
627 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
628 EXPECT_EQ(0.f, rect.left);
629 EXPECT_EQ(0.f, rect.right);
630 rect.left = 35;
631 rect.bottom = 150;
632 rect.right = 53;
633 rect.top = 165;
634 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
635 // Check that the annotation rectangle has been set correctly.
636 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
637 EXPECT_EQ(35.f, rect.left);
638 EXPECT_EQ(150.f, rect.bottom);
639 EXPECT_EQ(53.f, rect.right);
640 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400641
Lei Zhanga21d5932018-02-05 18:28:38 +0000642 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000643 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000644 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000645 ASSERT_TRUE(FPDFAnnot_SetStringValue(
646 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000647 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000648 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000649 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000650 ASSERT_EQ(74u, length_bytes);
651 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
652 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
653 pdfium::annotation::kContents,
654 buf.data(), length_bytes));
655 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000656 }
Jane Liu20eafda2017-06-07 10:33:24 -0400657 UnloadPage(page);
658}
659
Lei Zhang81395aa2021-04-16 00:40:46 +0000660TEST_F(FPDFAnnotEmbedderTest, AddAndSaveLinkAnnotation) {
661 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
662 FPDF_PAGE page = LoadPage(0);
663 ASSERT_TRUE(page);
664 {
665 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
666 CompareBitmap(bitmap.get(), 200, 200, pdfium::kHelloWorldChecksum);
667 }
668 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
669
670 constexpr char kUri[] = "https://pdfium.org/";
671
672 {
673 // Add a link annotation to the page and set its URI.
674 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_LINK));
675 ASSERT_TRUE(annot);
676 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
677 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
678 EXPECT_TRUE(FPDFAnnot_SetURI(annot.get(), kUri));
679 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
680
681 // Negative tests:
682 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, nullptr));
683 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
684 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), nullptr));
685 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
686 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, kUri));
687 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
688
689 // Position the link on top of "Hello, world!" without a border.
690 const FS_RECTF kRect = {19.0f, 48.0f, 85.0f, 60.0f};
691 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &kRect));
692 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/0.0f,
693 /*vertical_radius=*/0.0f,
694 /*border_width=*/0.0f));
695
696 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
697 kUri);
698 }
699
700 {
701 // Add an ink annotation to the page. Trying to add a link to it fails.
702 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
703 ASSERT_TRUE(annot);
704 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
705 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
706 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), kUri));
707 }
708
709 // Remove the ink annotation added above for negative testing.
710 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
711 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
712
713 // Save the document, closing the page.
714 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
715 UnloadPage(page);
716
717 // Reopen the document and make sure it still renders the same. Since the link
718 // does not have a border, it does not affect the rendering.
719 ASSERT_TRUE(OpenSavedDocument());
720 page = LoadSavedPage(0);
721 ASSERT_TRUE(page);
722 VerifySavedRendering(page, 200, 200, pdfium::kHelloWorldChecksum);
723 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
724
725 {
726 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
727 ASSERT_TRUE(annot);
728 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
729 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
730 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
731 kUri);
732 }
733
734 CloseSavedPage(page);
735 CloseSavedDocument();
736}
737
Hui Yingstb3490322020-07-22 00:53:29 +0000738TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400739 // Open a file with one annotation and load its first page.
740 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000741 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400742 ASSERT_TRUE(page);
743
744 // Check that there is a total of one annotation on its first page, and verify
745 // its quadpoints.
746 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400747 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000748 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000749 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000750 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000751 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000752 EXPECT_EQ(115.802643f, quadpoints.x1);
753 EXPECT_EQ(718.913940f, quadpoints.y1);
754 EXPECT_EQ(157.211182f, quadpoints.x4);
755 EXPECT_EQ(706.264465f, quadpoints.y4);
756 }
Jane Liu20eafda2017-06-07 10:33:24 -0400757
758 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000759 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000760 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000761 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
762 ASSERT_TRUE(annot);
763 quadpoints.x1 = 140.802643f;
764 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000765 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000766 }
Jane Liu20eafda2017-06-07 10:33:24 -0400767
Lei Zhangec618142021-04-13 17:36:46 +0000768 // Save the document and close the page.
Jane Liu20eafda2017-06-07 10:33:24 -0400769 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000770 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400771
772 // Open the saved document.
Lei Zhang42d30c22022-01-12 19:24:43 +0000773#if defined(_SKIA_SUPPORT_) && BUILDFLAG(IS_APPLE)
Hui Yingst57daee82020-10-09 05:47:20 +0000774 static const char kChecksum[] = "899387ae792390cd0d83cf7e2bbebfb5";
Lei Zhang42d30c22022-01-12 19:24:43 +0000775#elif defined(_SKIA_SUPPORT_PATHS_) && BUILDFLAG(IS_APPLE)
Hui Yingst57daee82020-10-09 05:47:20 +0000776 static const char kChecksum[] = "e40e235ee35f47ff28dda009aaaf36df";
777#elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstb3490322020-07-22 00:53:29 +0000778 static const char kChecksum[] = "798fa41303381c9ba6d99092f5cd4d2b";
779#else
780 static const char kChecksum[] = "dba153419f67b7c0c0e3d22d3e8910d5";
781#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400782
Lei Zhang0b494052019-01-31 21:41:15 +0000783 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000784 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +0000785 ASSERT_TRUE(page);
Hui Yingstb3490322020-07-22 00:53:29 +0000786 VerifySavedRendering(page, 612, 792, kChecksum);
Jane Liu20eafda2017-06-07 10:33:24 -0400787
788 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000789 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400790
Lei Zhanga21d5932018-02-05 18:28:38 +0000791 {
792 // Check that the second annotation is an underline annotation and verify
793 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000794 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000795 ASSERT_TRUE(new_annot);
796 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
797 FS_QUADPOINTSF new_quadpoints;
798 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000799 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000800 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
801 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
802 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
803 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
804 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400805
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000806 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400807 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400808}
Jane Liu06462752017-06-27 16:41:14 -0400809
Lei Zhangab41f252018-12-23 03:10:50 +0000810TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000811 // Open a file with four annotations and load its first page.
812 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
813 FPDF_PAGE page = LoadPage(0);
814 ASSERT_TRUE(page);
815 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
816
817 // Retrieve the highlight annotation.
818 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
819 ASSERT_TRUE(annot);
820 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
821
822 FS_QUADPOINTSF quadpoints;
823 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
824
825 {
826 // Verify the current one set of quadpoints.
827 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
828
829 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
830 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
831 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
832 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
833 }
834
835 {
836 // Update the quadpoints.
837 FS_QUADPOINTSF new_quadpoints = quadpoints;
838 new_quadpoints.y1 -= 20.f;
839 new_quadpoints.y2 -= 20.f;
840 new_quadpoints.y3 -= 20.f;
841 new_quadpoints.y4 -= 20.f;
842 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
843
844 // Verify added quadpoint set
845 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
846 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
847 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
848 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
849 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
850 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
851 }
852
853 {
854 // Append a new set of quadpoints.
855 FS_QUADPOINTSF new_quadpoints = quadpoints;
856 new_quadpoints.y1 += 20.f;
857 new_quadpoints.y2 += 20.f;
858 new_quadpoints.y3 += 20.f;
859 new_quadpoints.y4 += 20.f;
860 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
861
862 // Verify added quadpoint set
863 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
864 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
865 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
866 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
867 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
868 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
869 }
870
871 {
872 // Setting and getting quadpoints at out-of-bound index should fail
873 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
874 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
875 }
876
877 FPDFPage_CloseAnnot(annot);
878
879 // Retrieve the square annotation
880 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
881
882 {
883 // Check that attempting to set its quadpoints would fail
884 ASSERT_TRUE(squareAnnot);
885 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
886 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
887 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
888 }
889
890 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000891 UnloadPage(page);
892}
893
Hui Yingstb64cd122020-07-27 16:01:02 +0000894// TODO(crbug.com/pdfium/1569): Fix this issue and enable the test for Skia.
895#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +0000896#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
897#else
898#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
899#endif
900TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Hui Yingstb64cd122020-07-27 16:01:02 +0000901#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
902 static const char kMd5Original[] = "00e70eb543c2a6e8f8aafb4ee951d9bf";
Lei Zhang4f556b82019-04-08 16:32:41 +0000903 static const char kMd5ModifiedHighlight[] =
Hui Yingstb64cd122020-07-27 16:01:02 +0000904 "7638c4a8fe4aabbf8704e198f49b3198";
905 static const char kMd5ModifiedSquare[] = "54f507af6af63de877b9cafdab1bbdaa";
906#else
Lei Zhangf997abe2022-01-12 19:14:23 +0000907#if BUILDFLAG(IS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000908 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
909 static const char kMd5ModifiedHighlight[] =
910 "66f3caef3a7d488a4fa1ad37fc06310e";
911 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Lei Zhang42d30c22022-01-12 19:24:43 +0000912#elif BUILDFLAG(IS_APPLE)
Hui Yingstb64cd122020-07-27 16:01:02 +0000913 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
914 static const char kMd5ModifiedHighlight[] =
915 "e64bf648f6e9354d1f3eedb47a2c9498";
916 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Jane Liub370e5a2017-08-16 13:24:58 -0400917#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000918 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
919 static const char kMd5ModifiedHighlight[] =
920 "66f3caef3a7d488a4fa1ad37fc06310e";
921 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400922#endif
Hui Yingstb64cd122020-07-27 16:01:02 +0000923#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liub370e5a2017-08-16 13:24:58 -0400924
Jane Liu06462752017-06-27 16:41:14 -0400925 // Open a file with four annotations and load its first page.
926 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000927 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400928 ASSERT_TRUE(page);
929 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
930
Jane Liub370e5a2017-08-16 13:24:58 -0400931 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000932 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000933 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000934 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000935 }
Jane Liub370e5a2017-08-16 13:24:58 -0400936
Jane Liu0c6b07d2017-08-15 10:50:22 -0400937 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400938 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000939
940 // Retrieve the highlight annotation which has its AP stream already defined.
941 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000942 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000943 ASSERT_TRUE(annot);
944 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
945
946 // Check that color cannot be set when an AP stream is defined already.
947 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
948 102, 153, 204));
949
950 // Verify its attachment points.
951 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000952 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000953 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
954 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
955 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
956 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
957
958 // Check that updating the attachment points would succeed.
959 quadpoints.x1 -= 50.f;
960 quadpoints.x2 -= 50.f;
961 quadpoints.x3 -= 50.f;
962 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000963 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000964 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000965 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000966 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
967 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
968 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
969 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
970
971 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000972 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000973 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000974 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000975 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000976
977 // Verify its annotation rectangle.
978 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
979 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
980 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
981 EXPECT_NEAR(136.325f, rect.right, 0.001f);
982 EXPECT_NEAR(721.292f, rect.top, 0.001f);
983
984 // Check that updating the rectangle would succeed.
985 rect.left -= 60.f;
986 rect.right -= 60.f;
987 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
988 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
989 EXPECT_EQ(rect.right, new_rect.right);
990 }
Jane Liu06462752017-06-27 16:41:14 -0400991
Jane Liub370e5a2017-08-16 13:24:58 -0400992 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000993 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000994 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000995 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000996 }
Jane Liub370e5a2017-08-16 13:24:58 -0400997
Lei Zhanga21d5932018-02-05 18:28:38 +0000998 {
999 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001000 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001001 ASSERT_TRUE(annot);
1002 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -04001003
Lei Zhanga21d5932018-02-05 18:28:38 +00001004 // Check that updating the rectangle would succeed.
1005 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1006 rect.left += 70.f;
1007 rect.right += 70.f;
1008 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1009 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1010 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -04001011
Lei Zhanga21d5932018-02-05 18:28:38 +00001012 // Check that updating the rectangle changes the square annotation's
1013 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001014 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001015 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +00001016 }
Jane Liub370e5a2017-08-16 13:24:58 -04001017
Jane Liu06462752017-06-27 16:41:14 -04001018 UnloadPage(page);
1019}
Jane Liu8ce58f52017-06-29 13:40:22 -04001020
Lei Zhangab41f252018-12-23 03:10:50 +00001021TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +00001022 // Open a file with multiline markup annotations.
1023 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
1024 FPDF_PAGE page = LoadPage(0);
1025 ASSERT_TRUE(page);
1026 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001027 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +00001028 ASSERT_TRUE(annot);
1029
1030 // This is a three line annotation.
1031 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
1032 }
1033 UnloadPage(page);
1034
1035 // null annotation should return 0
1036 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
1037}
1038
Lei Zhangab41f252018-12-23 03:10:50 +00001039TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -04001040 // Open a file with 3 annotations on its first page.
1041 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +00001042 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -04001043 ASSERT_TRUE(page);
1044 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1045
Jane Liu0c6b07d2017-08-15 10:50:22 -04001046 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -04001047
Lei Zhanga21d5932018-02-05 18:28:38 +00001048 // Check that the annotations have the expected rectangle coordinates.
1049 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001050 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001051 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1052 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1053 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001054
Lei Zhanga21d5932018-02-05 18:28:38 +00001055 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001056 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001057 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1058 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
1059 }
1060
1061 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001062 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001063 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1064 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1065 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001066
1067 // Check that nothing happens when attempting to remove an annotation with an
1068 // out-of-bound index.
1069 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
1070 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
1071 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1072
1073 // Remove the second annotation.
1074 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
1075 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1076 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
1077
Lei Zhangec618142021-04-13 17:36:46 +00001078 // Save the document and close the page.
Jane Liu8ce58f52017-06-29 13:40:22 -04001079 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +00001080 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -04001081
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001082 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -04001083 // Open the saved document.
1084 std::string new_file = GetString();
1085 FPDF_FILEACCESS file_access;
1086 memset(&file_access, 0, sizeof(file_access));
1087 file_access.m_FileLen = new_file.size();
1088 file_access.m_GetBlock = GetBlockFromString;
1089 file_access.m_Param = &new_file;
1090 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
1091 ASSERT_TRUE(new_doc);
1092 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
1093 ASSERT_TRUE(new_page);
1094
1095 // Check that the saved document has 2 annotations on the first page.
1096 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
1097
Lei Zhanga21d5932018-02-05 18:28:38 +00001098 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1099 // by verifying their rectangle coordinates.
1100 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001101 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001102 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1103 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1104 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001105
Lei Zhanga21d5932018-02-05 18:28:38 +00001106 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001107 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001108 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1109 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1110 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001111 FPDF_ClosePage(new_page);
1112 FPDF_CloseDocument(new_doc);
1113}
Jane Liu8ce58f52017-06-29 13:40:22 -04001114
Hui Yingst4a21df02020-05-13 03:15:44 +00001115TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001116#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001117 static const char kMd5ModifiedPath[] = "b820e4ae359db95cbac9823937c6da1a";
1118 static const char kMd5TwoPaths[] = "c53837b7bb6a9a21a846aa786526aa56";
1119 static const char kMd5NewAnnot[] = "4f0f4217156e4251036f369184a48967";
Lei Zhang42d30c22022-01-12 19:24:43 +00001120#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001121 static const char kMd5ModifiedPath[] = "e31421f86c61d4e9cda138f15f561ca3";
1122 static const char kMd5TwoPaths[] = "58d932492f9d485d6a4bc0ba76c04557";
1123 static const char kMd5NewAnnot[] = "61f9ad13f2fd235753db198cf9704773";
Jane Liubaa7ff42017-06-29 19:18:23 -04001124#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001125 static const char kMd5ModifiedPath[] = "980e7636d864f7f7d323a31ad4e8fa04";
1126 static const char kMd5TwoPaths[] = "4c779c394b6790f8cf80305b566b663b";
1127 static const char kMd5NewAnnot[] = "97effd68dcf86273f68d126d6b45152e";
Jane Liubaa7ff42017-06-29 19:18:23 -04001128#endif
1129
1130 // Open a file with two annotations and load its first page.
1131 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001132 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001133 ASSERT_TRUE(page);
1134 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1135
1136 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001137 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001138 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001139 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001140 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001141
Lei Zhanga21d5932018-02-05 18:28:38 +00001142 {
1143 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001144 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001145 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001146
Lei Zhanga21d5932018-02-05 18:28:38 +00001147 // Check that this annotation has one path object and retrieve it.
1148 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001149 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001150 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1151 EXPECT_FALSE(path);
1152 path = FPDFAnnot_GetObject(annot.get(), 0);
1153 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1154 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001155
Lei Zhanga21d5932018-02-05 18:28:38 +00001156 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001157 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001158 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001159
Lei Zhanga21d5932018-02-05 18:28:38 +00001160 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001161 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001162 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001163 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001164 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001165
Lei Zhanga21d5932018-02-05 18:28:38 +00001166 // Add a second path object to the same annotation.
1167 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1168 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001169 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1170 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001171 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1172 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1173 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001174
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001175 // The object is in the annontation, not in the page, so the page object
1176 // array should not change.
1177 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1178
Lei Zhanga21d5932018-02-05 18:28:38 +00001179 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001180 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001181 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001182 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001183 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001184
Lei Zhanga21d5932018-02-05 18:28:38 +00001185 // Delete the newly added path object.
1186 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1187 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001188 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001189 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001190
1191 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001192 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001193 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001194 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001195 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001196
Jane Liubaa7ff42017-06-29 19:18:23 -04001197 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001198
Lei Zhanga21d5932018-02-05 18:28:38 +00001199 {
1200 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001201 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001202 ASSERT_TRUE(annot);
1203 rect.left = 200.f;
1204 rect.bottom = 400.f;
1205 rect.right = 500.f;
1206 rect.top = 600.f;
1207 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001208
Lei Zhanga21d5932018-02-05 18:28:38 +00001209 // Add a new path to the annotation.
1210 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1211 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1212 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1213 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1214 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001215 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1216 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001217 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1218 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1219 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1220
1221 // Check that the annotation's bounding box came from its rectangle.
1222 FS_RECTF new_rect;
1223 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1224 EXPECT_EQ(rect.left, new_rect.left);
1225 EXPECT_EQ(rect.bottom, new_rect.bottom);
1226 EXPECT_EQ(rect.right, new_rect.right);
1227 EXPECT_EQ(rect.top, new_rect.top);
1228 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001229
Lei Zhangec618142021-04-13 17:36:46 +00001230 // Save the document and close the page.
Jane Liubaa7ff42017-06-29 19:18:23 -04001231 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001232 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001233
1234 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001235 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001236 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001237 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001238 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001239
Jane Liu36567742017-07-06 11:13:35 -04001240 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001241 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001242
Lei Zhanga21d5932018-02-05 18:28:38 +00001243 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001244 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001245 ASSERT_TRUE(annot);
1246 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001247
Lei Zhanga21d5932018-02-05 18:28:38 +00001248 // Check that the new annotation's rectangle is as defined.
1249 FS_RECTF new_rect;
1250 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1251 EXPECT_EQ(rect.left, new_rect.left);
1252 EXPECT_EQ(rect.bottom, new_rect.bottom);
1253 EXPECT_EQ(rect.right, new_rect.right);
1254 EXPECT_EQ(rect.top, new_rect.top);
1255 }
1256
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001257 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001258 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001259}
Jane Liub137e752017-07-05 15:04:33 -04001260
Lei Zhangab41f252018-12-23 03:10:50 +00001261TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001262 // Open a file with an annotation and load its first page.
1263 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001264 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001265 ASSERT_TRUE(page);
1266
1267 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001268 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001269 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001270 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1271 }
Jane Liub137e752017-07-05 15:04:33 -04001272
Lei Zhanga21d5932018-02-05 18:28:38 +00001273 {
1274 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001275 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001276 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001277
Lei Zhanga21d5932018-02-05 18:28:38 +00001278 // Check that the original flag values are as expected.
1279 int flags = FPDFAnnot_GetFlags(annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00001280 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_INVISIBLE);
Lei Zhanga21d5932018-02-05 18:28:38 +00001281 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1282 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001283 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOZOOM);
1284 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOROTATE);
1285 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOVIEW);
1286 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_READONLY);
1287 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_LOCKED);
1288 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_TOGGLENOVIEW);
Jane Liub137e752017-07-05 15:04:33 -04001289
Lei Zhanga21d5932018-02-05 18:28:38 +00001290 // Set the HIDDEN flag.
1291 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1292 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1293 flags = FPDFAnnot_GetFlags(annot.get());
1294 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1295 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001296
Lei Zhanga21d5932018-02-05 18:28:38 +00001297 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001298 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001299 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001300 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001301 }
Jane Liub137e752017-07-05 15:04:33 -04001302
Lei Zhanga21d5932018-02-05 18:28:38 +00001303 // Unset the HIDDEN flag.
1304 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1305 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1306 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1307 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1308 flags = FPDFAnnot_GetFlags(annot.get());
1309 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1310 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001311
Lei Zhanga21d5932018-02-05 18:28:38 +00001312 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001313 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001314 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001315 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1316 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001317 }
Jane Liub137e752017-07-05 15:04:33 -04001318
Jane Liub137e752017-07-05 15:04:33 -04001319 UnloadPage(page);
1320}
Jane Liu36567742017-07-06 11:13:35 -04001321
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001322// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1323#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001324#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1325#else
1326#define MAYBE_AddAndModifyImage AddAndModifyImage
1327#endif
1328TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001329#if defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001330 static const char kMd5NewImage[] = "beb7db3647706d7fe4689f92073847aa";
1331 static const char kMd5ModifiedImage[] = "baa9b065469268e215ef958fe6987d6b";
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001332#else
Lei Zhang42d30c22022-01-12 19:24:43 +00001333#if BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001334 static const char kMd5NewImage[] = "c6fcbceb2f079bef10458ac60db3a10c";
1335 static const char kMd5ModifiedImage[] = "8068eb568e5c1c5fbe84e98f7a980ac3";
Jane Liu36567742017-07-06 11:13:35 -04001336#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001337 static const char kMd5NewImage[] = "62c2706511cb50e32e7caeb82b1d3d49";
1338 static const char kMd5ModifiedImage[] = "83093ce9fac746db69fbd2fb394434ac";
Jane Liu36567742017-07-06 11:13:35 -04001339#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001340#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001341
1342 // Open a file with two annotations and load its first page.
1343 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001344 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001345 ASSERT_TRUE(page);
1346 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1347
1348 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001349 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001350 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001351 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001352 }
Jane Liu36567742017-07-06 11:13:35 -04001353
Jane Liu36567742017-07-06 11:13:35 -04001354 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001355 FPDF_BITMAP image_bitmap;
1356
1357 {
1358 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001359 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001360 ASSERT_TRUE(annot);
1361 FS_RECTF rect;
1362 rect.left = 200.f;
1363 rect.bottom = 600.f;
1364 rect.right = 400.f;
1365 rect.top = 800.f;
1366 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1367
1368 // Add a solid-color translucent image object to the new annotation.
1369 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1370 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1371 0xeeeecccc);
1372 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1373 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1374 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1375 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
Lei Zhangc8601bf2021-06-29 23:19:27 +00001376 static constexpr FS_MATRIX kBitmapScaleMatrix{kBitmapSize, 0, 0,
1377 kBitmapSize, 0, 0};
1378 ASSERT_TRUE(FPDFPageObj_SetMatrix(image_object, &kBitmapScaleMatrix));
Lei Zhanga21d5932018-02-05 18:28:38 +00001379 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1380 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1381 }
Jane Liu36567742017-07-06 11:13:35 -04001382
1383 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001384 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001385 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001386 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001387 }
Jane Liu36567742017-07-06 11:13:35 -04001388
Lei Zhanga21d5932018-02-05 18:28:38 +00001389 {
1390 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001391 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001392 ASSERT_TRUE(annot);
1393 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1394 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1395 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001396
Lei Zhanga21d5932018-02-05 18:28:38 +00001397 // Modify the image in the new annotation.
1398 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1399 0xff000000);
1400 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1401 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1402 }
Jane Liu36567742017-07-06 11:13:35 -04001403
Lei Zhangec618142021-04-13 17:36:46 +00001404 // Save the document and close the page.
Jane Liu36567742017-07-06 11:13:35 -04001405 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001406 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001407 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001408
1409 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001410 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001411}
1412
Hui Yingst6cd754f2020-05-14 04:05:25 +00001413TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001414#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001415 static const char kMd5NewText[] = "310d9de5f17fb288fb243f5dbaf2b6dc";
1416 static const char kMd5ModifiedText[] = "22be42c136c3bf5a8ecea3dd83770a02";
Lei Zhang42d30c22022-01-12 19:24:43 +00001417#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001418 static const char kMd5NewText[] = "57a0fb3fba33e17de26bcde4c40b9a75";
1419 static const char kMd5ModifiedText[] = "072574999f2e3f36774ee0b5bc94d4dd";
Jane Liu36567742017-07-06 11:13:35 -04001420#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001421 static const char kMd5NewText[] = "1c4198c38f890c208c5cbaad57be4dc6";
1422 static const char kMd5ModifiedText[] = "cfa78d01406865f41f486bd34a8b9f7b";
Jane Liu36567742017-07-06 11:13:35 -04001423#endif
1424
1425 // Open a file with two annotations and load its first page.
1426 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001427 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001428 ASSERT_TRUE(page);
1429 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1430
1431 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001432 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001433 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001434 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001435 }
Jane Liu36567742017-07-06 11:13:35 -04001436
Lei Zhanga21d5932018-02-05 18:28:38 +00001437 {
1438 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001439 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001440 ASSERT_TRUE(annot);
1441 FS_RECTF rect;
1442 rect.left = 200.f;
1443 rect.bottom = 550.f;
1444 rect.right = 450.f;
1445 rect.top = 650.f;
1446 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001447
Lei Zhanga21d5932018-02-05 18:28:38 +00001448 // Add a translucent text object to the new annotation.
1449 FPDF_PAGEOBJECT text_object =
1450 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1451 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001452 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001453 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1454 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001455 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001456 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1457 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1458 }
Jane Liu36567742017-07-06 11:13:35 -04001459
1460 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001461 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001462 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001463 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001464 }
Jane Liu36567742017-07-06 11:13:35 -04001465
Lei Zhanga21d5932018-02-05 18:28:38 +00001466 {
1467 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001468 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001469 ASSERT_TRUE(annot);
1470 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1471 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1472 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001473
Lei Zhanga21d5932018-02-05 18:28:38 +00001474 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001475 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001476 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1477 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1478 }
Jane Liu36567742017-07-06 11:13:35 -04001479
1480 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001481 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001482 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001483 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001484 }
Jane Liu36567742017-07-06 11:13:35 -04001485
1486 // Remove the new annotation, and check that the page renders as before.
1487 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001488 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001489 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001490 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001491 }
Jane Liu36567742017-07-06 11:13:35 -04001492
1493 UnloadPage(page);
1494}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001495
Hui Yingst9b6b1542020-07-27 16:11:12 +00001496TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001497 // Open a file with four annotations and load its first page.
1498 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001499 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001500 ASSERT_TRUE(page);
1501
Lei Zhang4f556b82019-04-08 16:32:41 +00001502 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001503
Lei Zhanga21d5932018-02-05 18:28:38 +00001504 {
1505 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001506 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001507 ASSERT_TRUE(annot);
1508
1509 // Check that a non-existent key does not exist.
1510 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1511
1512 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001513 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001514 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001515 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1516 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1517 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001518
1519 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001520 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001521 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001522 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001523 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001524 ASSERT_EQ(66u, length_bytes);
1525 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1526 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1527 length_bytes));
1528 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1529 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001530
1531 // Check that the string value of the modified date is correct.
1532 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001533 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1534 nullptr, 0);
1535 ASSERT_EQ(44u, length_bytes);
1536 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001537 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001538 buf.data(), length_bytes));
1539 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001540
1541 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001542 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001543 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1544 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001545 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001546
Lei Zhangec618142021-04-13 17:36:46 +00001547 // Save the document and close the page.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001548 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001549 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001550
Hui Yingst9b6b1542020-07-27 16:11:12 +00001551#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001552 static const char kMd5[] = "aa0e47d966c60d59102a2466542e0e46";
Lei Zhang42d30c22022-01-12 19:24:43 +00001553#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001554 static const char kMd5[] = "cd90315b250dfe08265ce0ac335c5f76";
Hui Yingst9b6b1542020-07-27 16:11:12 +00001555#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001556 static const char kMd5[] = "c4fb6911f2a87f490be196f8898de738";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001557#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001558
1559 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001560 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001561 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001562 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001563 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001564 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001565 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001566
Lei Zhanga21d5932018-02-05 18:28:38 +00001567 // Check that the string value of the modified date is the newly-set value.
1568 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001569 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001570 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001571 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001572 ASSERT_EQ(44u, length_bytes);
1573 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001574 EXPECT_EQ(44u,
1575 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001576 buf.data(), length_bytes));
1577 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001578 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001579
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001580 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001581 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001582}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001583
rycsmith3e785602019-03-05 21:48:36 +00001584TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001585 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001586 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1587 FPDF_PAGE page = LoadPage(0);
1588 ASSERT_TRUE(page);
1589 {
1590 // First two annotations do not have "MaxLen" attribute.
1591 for (int i = 0; i < 2; i++) {
1592 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1593 ASSERT_TRUE(annot);
1594
1595 // Verify that no "MaxLen" key present.
1596 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1597
1598 float value;
1599 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1600 }
1601
1602 // Annotation in index 2 has "MaxLen" of 10.
1603 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1604 ASSERT_TRUE(annot);
1605
1606 // Verify that "MaxLen" key present.
1607 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1608
1609 float value;
1610 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1611 EXPECT_FLOAT_EQ(10.0f, value);
1612
1613 // Check bad inputs.
1614 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1615 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1616 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1617 // Ask for key that exists but is not a number.
1618 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1619 }
1620
1621 UnloadPage(page);
1622}
1623
Lei Zhangab41f252018-12-23 03:10:50 +00001624TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001625 // Open a file with four annotations and load its first page.
1626 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001627 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001628 ASSERT_TRUE(page);
1629
Lei Zhanga21d5932018-02-05 18:28:38 +00001630 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001631 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1632 static constexpr size_t kExpectNormalAPLength = 73970;
1633
Lei Zhanga21d5932018-02-05 18:28:38 +00001634 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001635 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001636 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001637
Lei Zhanga21d5932018-02-05 18:28:38 +00001638 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001639 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001640 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001641 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001642
Lei Zhanga21d5932018-02-05 18:28:38 +00001643 // Check that the string value of an AP is not returned if the buffer is too
1644 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001645 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1646 // Write in the buffer to verify it's not overwritten.
1647 memcpy(buf.data(), "abcdefgh", 8);
1648 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001649 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001650 buf.data(), normal_length_bytes - 1));
1651 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001652
Lei Zhanga21d5932018-02-05 18:28:38 +00001653 // Check that the string value of an AP is returned through a buffer that is
1654 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001655 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001656 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001657 buf.data(), normal_length_bytes));
1658 EXPECT_EQ(kMd5NormalAP,
1659 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1660 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001661
Lei Zhanga21d5932018-02-05 18:28:38 +00001662 // Check that the string value of an AP is returned through a buffer that is
1663 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001664 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1665 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001666 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001667 buf.data(), normal_length_bytes + 2));
1668 EXPECT_EQ(kMd5NormalAP,
1669 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1670 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001671
Lei Zhanga21d5932018-02-05 18:28:38 +00001672 // Check that getting an AP for a mode that does not have an AP returns an
1673 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001674 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001675 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001676 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001677
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001678 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001679 EXPECT_EQ(2u,
1680 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001681 buf.data(), 1000));
1682 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001683
Lei Zhanga21d5932018-02-05 18:28:38 +00001684 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001685 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001686 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001687 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001688 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001689 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001690 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001691
Lei Zhanga21d5932018-02-05 18:28:38 +00001692 // Set the AP correctly now.
1693 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001694 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001695
Lei Zhanga21d5932018-02-05 18:28:38 +00001696 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001697 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001698 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001699 ASSERT_EQ(24u, rollover_length_bytes);
1700 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001701 EXPECT_EQ(24u,
1702 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001703 buf.data(), rollover_length_bytes));
1704 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001705
Lei Zhanga21d5932018-02-05 18:28:38 +00001706 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001707 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1708 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001709 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001710 buf.data(), normal_length_bytes));
1711 EXPECT_EQ(kMd5NormalAP,
1712 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1713 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001714 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001715
1716 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001717 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001718 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001719
Lei Zhang0b494052019-01-31 21:41:15 +00001720 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001721 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001722 ASSERT_TRUE(page);
Lei Zhanga21d5932018-02-05 18:28:38 +00001723 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001724 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001725
Lei Zhanga21d5932018-02-05 18:28:38 +00001726 // Check that the new annotation value is equal to the value we set before
1727 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001728 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001729 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001730 ASSERT_EQ(24u, rollover_length_bytes);
1731 std::vector<FPDF_WCHAR> buf =
1732 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001733 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1734 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001735 buf.data(), rollover_length_bytes));
1736 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001737 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001738
1739 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001740 CloseSavedPage(page);
1741 CloseSavedDocument();
1742}
1743
Lei Zhangab41f252018-12-23 03:10:50 +00001744TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001745 // Open a file with four annotations and load its first page.
1746 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001747 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001748 ASSERT_TRUE(page);
1749
Lei Zhanga21d5932018-02-05 18:28:38 +00001750 {
1751 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001752 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001753 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001754
Lei Zhanga21d5932018-02-05 18:28:38 +00001755 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001756 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001757 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001758 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001759 EXPECT_EQ(73970u,
1760 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1761 nullptr, 0));
1762 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1763 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001764
Lei Zhanga21d5932018-02-05 18:28:38 +00001765 // Check that setting the Down AP to null removes the Down entry but keeps
1766 // Normal intact.
1767 EXPECT_TRUE(
1768 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1769 EXPECT_EQ(73970u,
1770 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1771 nullptr, 0));
1772 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1773 nullptr, 0));
1774 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001775
Lei Zhang75c81712018-02-08 17:22:39 +00001776 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001777}
1778
Lei Zhangab41f252018-12-23 03:10:50 +00001779TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001780 // Open a file with four annotations and load its first page.
1781 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001782 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001783 ASSERT_TRUE(page);
1784
Lei Zhanga21d5932018-02-05 18:28:38 +00001785 {
1786 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001787 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001788 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001789
Lei Zhanga21d5932018-02-05 18:28:38 +00001790 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001791 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001792 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001793 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001794 EXPECT_EQ(73970u,
1795 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1796 nullptr, 0));
1797 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1798 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001799
Lei Zhanga21d5932018-02-05 18:28:38 +00001800 // Check that setting the Normal AP to null removes the whole AP dictionary.
1801 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1802 nullptr));
1803 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1804 nullptr, 0));
1805 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1806 nullptr, 0));
1807 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001808
Lei Zhang75c81712018-02-08 17:22:39 +00001809 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001810}
1811
Lei Zhangab41f252018-12-23 03:10:50 +00001812TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001813 // Open a file with annotations and load its first page.
1814 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001815 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001816 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001817 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001818
Lei Zhanga21d5932018-02-05 18:28:38 +00001819 {
1820 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001821 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001822 ASSERT_TRUE(annot);
1823 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1824 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001825 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001826 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1827 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1828 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001829
Lei Zhanga21d5932018-02-05 18:28:38 +00001830 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001831 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001832 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1833 ASSERT_TRUE(popup);
1834 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1835 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1836 FS_RECTF rect;
1837 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1838 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1839 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001840
Lei Zhanga21d5932018-02-05 18:28:38 +00001841 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1842 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001843 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001844 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1845 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001846
Lei Zhanga21d5932018-02-05 18:28:38 +00001847 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1848 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001849 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001850 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001851 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1852 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001853 }
Jane Liu300bb272017-08-21 14:37:53 -04001854
Jane Liu300bb272017-08-21 14:37:53 -04001855 UnloadPage(page);
1856}
1857
Lei Zhangab41f252018-12-23 03:10:50 +00001858TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001859 // Open file with form text fields.
1860 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001861 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001862 ASSERT_TRUE(page);
1863
Lei Zhanga21d5932018-02-05 18:28:38 +00001864 {
1865 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001866 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001867 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001868
Lei Zhanga21d5932018-02-05 18:28:38 +00001869 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001870 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001871 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001872 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1873 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1874 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001875 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001876 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001877
Lei Zhanga21d5932018-02-05 18:28:38 +00001878 {
1879 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001880 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001881 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001882
Lei Zhanga21d5932018-02-05 18:28:38 +00001883 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001884 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001885 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001886 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1887 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1888 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001889 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1890 }
1891
1892 {
1893 // Retrieve the fourth annotation: user-editable password text field.
1894 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1895 ASSERT_TRUE(annot);
1896
1897 // Check that the flag values are as expected.
1898 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1899 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001900 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1901 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1902 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001903 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001904 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001905
1906 UnloadPage(page);
1907}
1908
Lei Zhangab41f252018-12-23 03:10:50 +00001909TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001910 // Open file with form text fields.
1911 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001912 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001913 ASSERT_TRUE(page);
1914
Lei Zhanga21d5932018-02-05 18:28:38 +00001915 {
1916 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001917 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001918 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001919
Lei Zhanga21d5932018-02-05 18:28:38 +00001920 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001921 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001922 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001923 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1924 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001925 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1926 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001927 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001928 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001929
Lei Zhanga21d5932018-02-05 18:28:38 +00001930 {
1931 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001932 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001933 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001934
Lei Zhanga21d5932018-02-05 18:28:38 +00001935 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001936 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001937 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001938 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1939 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001940 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1941 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001942 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001943 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001944
Lei Zhanga21d5932018-02-05 18:28:38 +00001945 {
1946 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001947 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001948 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001949
Lei Zhanga21d5932018-02-05 18:28:38 +00001950 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001951 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001952 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001953 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1954 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001955 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1956 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001957 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001958 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001959
1960 UnloadPage(page);
1961}
Diana Gage40870db2017-07-19 18:16:03 -07001962
Lei Zhangab41f252018-12-23 03:10:50 +00001963TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001964 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001965 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001966 FPDF_PAGE page = LoadPage(0);
1967 ASSERT_TRUE(page);
1968
1969 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001970 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1971 EXPECT_FALSE(
1972 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001973
Lei Zhang8da98232019-12-11 23:29:33 +00001974 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001975 {
1976 // Verify there is an annotation.
1977 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001978 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001979 EXPECT_TRUE(annot);
1980 }
1981
1982 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001983 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1984 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1985 EXPECT_FALSE(
1986 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001987
1988 UnloadPage(page);
1989}
1990
Lei Zhangab41f252018-12-23 03:10:50 +00001991TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001992 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001993 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001994 FPDF_PAGE page = LoadPage(0);
1995 ASSERT_TRUE(page);
1996
Lei Zhanga21d5932018-02-05 18:28:38 +00001997 {
1998 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001999 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002000 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002001 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002002 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002003
Lei Zhanga21d5932018-02-05 18:28:38 +00002004 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002005 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00002006 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2007 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002008 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2009 }
Diana Gage40870db2017-07-19 18:16:03 -07002010
Lei Zhanga21d5932018-02-05 18:28:38 +00002011 {
2012 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002013 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002014 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002015 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002016 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002017
Lei Zhanga21d5932018-02-05 18:28:38 +00002018 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002019 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002020 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002021 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2022 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002023 }
Diana Gage40870db2017-07-19 18:16:03 -07002024
2025 UnloadPage(page);
2026}
2027
Lei Zhangab41f252018-12-23 03:10:50 +00002028TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07002029 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002030 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002031 FPDF_PAGE page = LoadPage(0);
2032 ASSERT_TRUE(page);
2033
Lei Zhanga21d5932018-02-05 18:28:38 +00002034 {
2035 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002036 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002037 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002038 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002039 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002040
Lei Zhanga21d5932018-02-05 18:28:38 +00002041 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002042 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002043 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002044 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2045 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002046 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2047 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002048 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002049 }
Diana Gage40870db2017-07-19 18:16:03 -07002050
Lei Zhanga21d5932018-02-05 18:28:38 +00002051 {
2052 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002053 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002054 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002055 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002056 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002057
Lei Zhanga21d5932018-02-05 18:28:38 +00002058 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002059 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002060 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002061 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2062 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002063 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2064 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002065 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002066 }
Diana Gage40870db2017-07-19 18:16:03 -07002067
Lei Zhanga21d5932018-02-05 18:28:38 +00002068 {
2069 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002070 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002071 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002072 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002073 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002074
Lei Zhanga21d5932018-02-05 18:28:38 +00002075 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002076 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002077 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002078 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2079 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002080 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2081 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002082 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002083 }
Diana Gage40870db2017-07-19 18:16:03 -07002084
2085 UnloadPage(page);
2086}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002087
Hui Yingst603dcd82020-11-09 22:36:46 +00002088TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
Lei Zhang03e5e682019-09-16 19:45:55 +00002089#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst603dcd82020-11-09 22:36:46 +00002090 static const char kExpectedBitmap[] = "a1ea1ceebb26922fae576cb79ce63af0";
Lei Zhang03e5e682019-09-16 19:45:55 +00002091#else
Lei Zhang992e7e22019-02-04 19:20:58 +00002092 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
Hui Yingst603dcd82020-11-09 22:36:46 +00002093#endif
Tom Andersond4fe5f72021-12-03 20:52:52 +00002094 static constexpr size_t kExpectedSize = 1590;
Lei Zhang992e7e22019-02-04 19:20:58 +00002095
2096 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2097
2098 FPDF_PAGE page = LoadPage(0);
2099 ASSERT_TRUE(page);
2100
2101 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2102 EXPECT_EQ(kExpectedSize, GetString().size());
2103 ClearString();
2104
2105 for (size_t i = 0; i < 10; ++i) {
2106 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2107 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2108
2109 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2110 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2111 // equal, not bigger.
2112 EXPECT_LT(kExpectedSize, GetString().size());
2113 ClearString();
2114 }
2115
2116 UnloadPage(page);
2117}
2118
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002119TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2120 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2121 FPDF_PAGE page = LoadPage(0);
2122 ASSERT_TRUE(page);
2123 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2124
2125 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002126 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002127 static const size_t kBufSize = 12;
2128 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002129
2130 {
2131 // Add a text annotation to the page.
2132 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2133 ASSERT_TRUE(annot);
2134 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2135 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2136
2137 // Make sure there is no test key, add set a value there, and read it back.
2138 std::fill(buf.begin(), buf.end(), 'x');
2139 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002140 kBufSize));
2141 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002142
Lei Zhangf0f67682019-04-08 17:03:21 +00002143 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002144 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2145
2146 std::fill(buf.begin(), buf.end(), 'x');
2147 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002148 kBufSize));
2149 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002150 }
2151
Lei Zhang05ec64c2019-01-09 03:00:06 +00002152 {
2153 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2154 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002155 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002156 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002157 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2158 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2159 // Also do the same test for its appearance string.
2160 std::fill(buf.begin(), buf.end(), 'x');
2161 ASSERT_EQ(2u,
2162 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002163 buf.data(), kBufSize));
2164 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002165
Lei Zhangf0f67682019-04-08 17:03:21 +00002166 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002167 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2168 text.get()));
2169
2170 std::fill(buf.begin(), buf.end(), 'x');
2171 ASSERT_EQ(6u,
2172 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002173 buf.data(), kBufSize));
2174 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002175 }
2176
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002177 UnloadPage(page);
2178
2179 {
2180 // Save a copy, open the copy, and check the annotation again.
2181 // Note that it renders the rotation.
2182 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002183 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002184 FPDF_PAGE saved_page = LoadSavedPage(0);
2185 ASSERT_TRUE(saved_page);
2186
Lei Zhang05ec64c2019-01-09 03:00:06 +00002187 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002188 {
2189 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2190 ASSERT_TRUE(annot);
2191 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2192
2193 std::fill(buf.begin(), buf.end(), 'x');
2194 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002195 kBufSize));
2196 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002197 }
2198
Lei Zhang05ec64c2019-01-09 03:00:06 +00002199 {
2200 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2201 ASSERT_TRUE(annot);
2202 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2203 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2204
2205 std::fill(buf.begin(), buf.end(), 'x');
2206 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002207 kBufSize));
2208 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002209 }
2210
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002211 CloseSavedPage(saved_page);
2212 CloseSavedDocument();
2213 }
2214}
rycsmithcb752f32019-02-21 18:40:53 +00002215
2216TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2217 // Open a file with combobox widget annotations and load its first page.
2218 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2219 FPDF_PAGE page = LoadPage(0);
2220 ASSERT_TRUE(page);
2221
2222 {
2223 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2224 ASSERT_TRUE(annot);
2225
2226 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2227
2228 annot.reset(FPDFPage_GetAnnot(page, 1));
2229 ASSERT_TRUE(annot);
2230
2231 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002232
2233 // Check bad form handle / annot.
2234 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2235 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2236 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002237 }
2238
2239 UnloadPage(page);
2240}
2241
2242TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2243 // Open a file with listbox widget annotations and load its first page.
2244 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2245 FPDF_PAGE page = LoadPage(0);
2246 ASSERT_TRUE(page);
2247
2248 {
2249 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2250 ASSERT_TRUE(annot);
2251
2252 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2253
2254 annot.reset(FPDFPage_GetAnnot(page, 1));
2255 ASSERT_TRUE(annot);
2256
2257 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2258 }
2259
2260 UnloadPage(page);
2261}
2262
2263TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2264 // Open a file with ink annotations and load its first page.
2265 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2266 FPDF_PAGE page = LoadPage(0);
2267 ASSERT_TRUE(page);
2268
2269 {
2270 // annotations do not have "Opt" array and will return -1
2271 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2272 ASSERT_TRUE(annot);
2273
2274 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2275
2276 annot.reset(FPDFPage_GetAnnot(page, 1));
2277 ASSERT_TRUE(annot);
2278
2279 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2280 }
2281
2282 UnloadPage(page);
2283}
2284
2285TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2286 // Open a file with combobox widget annotations and load its first page.
2287 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2288 FPDF_PAGE page = LoadPage(0);
2289 ASSERT_TRUE(page);
2290
2291 {
2292 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2293 ASSERT_TRUE(annot);
2294
2295 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002296 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002297 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002298 ASSERT_EQ(8u, length_bytes);
2299 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002300 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002301 buf.data(), length_bytes));
2302 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002303
2304 annot.reset(FPDFPage_GetAnnot(page, 1));
2305 ASSERT_TRUE(annot);
2306
2307 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002308 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002309 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002310 ASSERT_EQ(12u, length_bytes);
2311 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002312 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002313 buf.data(), length_bytes));
2314 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002315
2316 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002317 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002318 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002319 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002320 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002321 buf.data(), length_bytes));
2322 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002323
Lei Zhange7033c82019-02-26 19:30:49 +00002324 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002325 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2326 nullptr, 0));
2327 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2328 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002329
2330 // Check bad form handle / annot.
2331 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2332 EXPECT_EQ(0u,
2333 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2334 EXPECT_EQ(0u,
2335 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002336 }
2337
2338 UnloadPage(page);
2339}
2340
2341TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2342 // Open a file with listbox widget annotations and load its first page.
2343 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2344 FPDF_PAGE page = LoadPage(0);
2345 ASSERT_TRUE(page);
2346
2347 {
2348 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2349 ASSERT_TRUE(annot);
2350
2351 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002352 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002353 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002354 ASSERT_EQ(8u, length_bytes);
2355 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002356 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002357 buf.data(), length_bytes));
2358 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002359
2360 annot.reset(FPDFPage_GetAnnot(page, 1));
2361 ASSERT_TRUE(annot);
2362
2363 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002364 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002365 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002366 ASSERT_EQ(12u, length_bytes);
2367 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002368 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002369 buf.data(), length_bytes));
2370 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002371
2372 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002373 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002374 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002375 ASSERT_EQ(18u, length_bytes);
2376 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002377 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002378 buf.data(), length_bytes));
2379 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002380
2381 // indices out of range
2382 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2383 nullptr, 0));
2384 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2385 nullptr, 0));
2386 }
2387
2388 UnloadPage(page);
2389}
2390
2391TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2392 // Open a file with ink annotations and load its first page.
2393 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2394 FPDF_PAGE page = LoadPage(0);
2395 ASSERT_TRUE(page);
2396
2397 {
2398 // annotations do not have "Opt" array and will return 0
2399 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2400 ASSERT_TRUE(annot);
2401
2402 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2403 nullptr, 0));
2404
2405 annot.reset(FPDFPage_GetAnnot(page, 1));
2406 ASSERT_TRUE(annot);
2407
2408 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2409 nullptr, 0));
2410 }
2411
2412 UnloadPage(page);
2413}
Ryan Smith09c23b12019-04-25 18:09:06 +00002414
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002415TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2416 // Open a file with combobox widget annotations and load its first page.
2417 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2418 FPDF_PAGE page = LoadPage(0);
2419 ASSERT_TRUE(page);
2420
2421 {
2422 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2423 ASSERT_TRUE(annot);
2424
2425 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2426 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2427 ASSERT_EQ(3, count);
2428 for (int i = 0; i < count; i++) {
2429 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2430 }
2431
2432 annot.reset(FPDFPage_GetAnnot(page, 1));
2433 ASSERT_TRUE(annot);
2434
2435 // Checks for Combobox with Values (/V) object which is just a string.
2436 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2437 ASSERT_EQ(26, count);
2438 for (int i = 0; i < count; i++) {
2439 EXPECT_EQ(i == 1,
2440 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2441 }
2442
2443 // Checks for index outside bound i.e. (index >= CountOption()).
2444 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2445 /*index=*/26));
2446 // Checks for negetive index.
2447 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2448 /*index=*/-1));
2449
2450 // Checks for bad form handle/annot.
2451 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2452 EXPECT_FALSE(
2453 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2454 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2455 }
2456
2457 UnloadPage(page);
2458}
2459
2460TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2461 // Open a file with listbox widget annotations and load its first page.
2462 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2463 FPDF_PAGE page = LoadPage(0);
2464 ASSERT_TRUE(page);
2465
2466 {
2467 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2468 ASSERT_TRUE(annot);
2469
2470 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2471 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2472 ASSERT_EQ(3, count);
2473 for (int i = 0; i < count; i++) {
2474 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2475 }
2476
2477 annot.reset(FPDFPage_GetAnnot(page, 1));
2478 ASSERT_TRUE(annot);
2479
2480 // Checks for Listbox with Values (/V) object which is just a string.
2481 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2482 ASSERT_EQ(26, count);
2483 for (int i = 0; i < count; i++) {
2484 EXPECT_EQ(i == 1,
2485 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2486 }
2487
2488 annot.reset(FPDFPage_GetAnnot(page, 3));
2489 ASSERT_TRUE(annot);
2490
2491 // Checks for Listbox with only Selected indices (/I) object which is an
2492 // array with 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 == 1 || i == 3);
2497 EXPECT_EQ(expected,
2498 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2499 }
2500
2501 annot.reset(FPDFPage_GetAnnot(page, 4));
2502 ASSERT_TRUE(annot);
2503
2504 // Checks for Listbox with Values (/V) object which is an array with
2505 // multiple objects.
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 == 2 || i == 4);
2510 EXPECT_EQ(expected,
2511 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2512 }
2513
2514 annot.reset(FPDFPage_GetAnnot(page, 5));
2515 ASSERT_TRUE(annot);
2516
2517 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2518 // objects conflict with different lengths.
2519 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2520 ASSERT_EQ(5, count);
2521 for (int i = 0; i < count; i++) {
2522 bool expected = (i == 0 || i == 2);
2523 EXPECT_EQ(expected,
2524 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2525 }
2526 }
2527
2528 UnloadPage(page);
2529}
2530
2531TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2532 // Open a file with multiple form field annotations and load its first page.
2533 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2534 FPDF_PAGE page = LoadPage(0);
2535 ASSERT_TRUE(page);
2536
2537 {
2538 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2539 ASSERT_TRUE(annot);
2540
2541 // Checks for link annotation.
2542 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2543 /*index=*/0));
2544
2545 annot.reset(FPDFPage_GetAnnot(page, 3));
2546 ASSERT_TRUE(annot);
2547
2548 // Checks for text field annotation.
2549 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2550 /*index=*/0));
2551 }
2552
2553 UnloadPage(page);
2554}
2555
Ryan Smith09c23b12019-04-25 18:09:06 +00002556TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2557 // Open a file with combobox annotations and load its first page.
2558 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2559 FPDF_PAGE page = LoadPage(0);
2560 ASSERT_TRUE(page);
2561
2562 {
2563 // All 3 widgets have Tf font size 12.
2564 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2565 ASSERT_TRUE(annot);
2566
2567 float value;
2568 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2569 EXPECT_EQ(12.0, value);
2570
2571 annot.reset(FPDFPage_GetAnnot(page, 1));
2572 ASSERT_TRUE(annot);
2573
2574 float value_two;
2575 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2576 EXPECT_EQ(12.0, value_two);
2577
2578 annot.reset(FPDFPage_GetAnnot(page, 2));
2579 ASSERT_TRUE(annot);
2580
2581 float value_three;
2582 ASSERT_TRUE(
2583 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2584 EXPECT_EQ(12.0, value_three);
2585 }
2586
2587 UnloadPage(page);
2588}
2589
2590TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2591 // Open a file with textfield annotations and load its first page.
2592 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2593 FPDF_PAGE page = LoadPage(0);
2594 ASSERT_TRUE(page);
2595
2596 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002597 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002598 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2599 ASSERT_TRUE(annot);
2600
2601 float value;
2602 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2603 EXPECT_EQ(12.0, value);
2604
2605 annot.reset(FPDFPage_GetAnnot(page, 1));
2606 ASSERT_TRUE(annot);
2607
2608 float value_two;
2609 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2610 EXPECT_EQ(12.0, value_two);
2611
2612 annot.reset(FPDFPage_GetAnnot(page, 2));
2613 ASSERT_TRUE(annot);
2614
2615 float value_three;
2616 ASSERT_TRUE(
2617 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2618 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002619
2620 float value_four;
2621 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2622 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002623 }
2624
2625 UnloadPage(page);
2626}
2627
2628TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2629 // Open a file with ink annotations and load its first page.
2630 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2631 FPDF_PAGE page = LoadPage(0);
2632 ASSERT_TRUE(page);
2633
2634 {
2635 // Annotations that do not have variable text and will return -1.
2636 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2637 ASSERT_TRUE(annot);
2638
2639 float value;
2640 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2641
2642 annot.reset(FPDFPage_GetAnnot(page, 1));
2643 ASSERT_TRUE(annot);
2644
2645 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2646 }
2647
2648 UnloadPage(page);
2649}
2650
2651TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2652 // Open a file with combobox annotations and load its first page.
2653 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2654 FPDF_PAGE page = LoadPage(0);
2655 ASSERT_TRUE(page);
2656
2657 {
2658 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2659 ASSERT_TRUE(annot);
2660
2661 // Check bad form handle / annot.
2662 float value;
2663 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2664 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2665 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2666 }
2667
2668 UnloadPage(page);
2669}
2670
2671TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2672 // Open a file with textfield annotations and load its first page.
2673 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2674 FPDF_PAGE page = LoadPage(0);
2675 ASSERT_TRUE(page);
2676
2677 {
2678 // Obtain the first annotation, a text field with negative font size, -12.
2679 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2680 ASSERT_TRUE(annot);
2681
2682 float value;
2683 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2684 EXPECT_EQ(-12.0, value);
2685 }
2686
2687 UnloadPage(page);
2688}
Ryan Smith23fdf892019-07-17 21:51:26 +00002689
2690TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2691 // Open a file with checkbox and radiobuttons widget annotations and load its
2692 // first page.
2693 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2694 FPDF_PAGE page = LoadPage(0);
2695 ASSERT_TRUE(page);
2696
2697 {
2698 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2699 ASSERT_TRUE(annot);
2700 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2701 }
2702
2703 UnloadPage(page);
2704}
2705
2706TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2707 // Open a file with checkbox and radiobutton widget annotations and load its
2708 // first page.
2709 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2710 FPDF_PAGE page = LoadPage(0);
2711 ASSERT_TRUE(page);
2712
2713 {
2714 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2715 ASSERT_TRUE(annot);
2716 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2717 }
2718
2719 UnloadPage(page);
2720}
2721
2722TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2723 // Open a file with checkbox and radiobutton widget annotations and load its
2724 // first page.
2725 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2726 FPDF_PAGE page = LoadPage(0);
2727 ASSERT_TRUE(page);
2728
2729 {
2730 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2731 ASSERT_TRUE(annot);
2732 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2733
2734 annot.reset(FPDFPage_GetAnnot(page, 6));
2735 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2736
2737 annot.reset(FPDFPage_GetAnnot(page, 7));
2738 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2739 }
2740
2741 UnloadPage(page);
2742}
2743
2744TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2745 // Open a file with checkbox and radiobutton widget annotations and load its
2746 // first page.
2747 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2748 FPDF_PAGE page = LoadPage(0);
2749 ASSERT_TRUE(page);
2750
2751 {
2752 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2753 ASSERT_TRUE(annot);
2754 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2755
2756 annot.reset(FPDFPage_GetAnnot(page, 3));
2757 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2758
2759 annot.reset(FPDFPage_GetAnnot(page, 4));
2760 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2761 }
2762
2763 UnloadPage(page);
2764}
2765
2766TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2767 // Open a file with checkbox and radiobuttons widget annotations and load its
2768 // first page.
2769 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2770 FPDF_PAGE page = LoadPage(0);
2771 ASSERT_TRUE(page);
2772
2773 {
2774 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2775 ASSERT_TRUE(annot);
2776 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2777 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2778 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2779 }
2780
2781 UnloadPage(page);
2782}
2783
2784TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2785 // Open a file with text widget annotations and load its first page.
2786 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2787 FPDF_PAGE page = LoadPage(0);
2788 ASSERT_TRUE(page);
2789
2790 {
2791 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2792 ASSERT_TRUE(annot);
2793 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2794 }
2795
2796 UnloadPage(page);
2797}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002798
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002799TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2800 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002801 FPDF_PAGE page = LoadPage(0);
2802 ASSERT_TRUE(page);
2803
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002804 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2805
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002806 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002807 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002808 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002809 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002810 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002811
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002812 constexpr int kExpectedAnnotTypes[] = {-1,
2813 FPDF_FORMFIELD_COMBOBOX,
2814 FPDF_FORMFIELD_LISTBOX,
2815 FPDF_FORMFIELD_TEXTFIELD,
2816 FPDF_FORMFIELD_CHECKBOX,
2817 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002818
Lei Zhang4501a502020-05-18 16:52:59 +00002819 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002820 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002821 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002822 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002823 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2824 }
2825 UnloadPage(page);
2826}
2827
2828TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2829 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2830 FPDF_PAGE page = LoadPage(0);
2831 ASSERT_TRUE(page);
2832
2833 {
2834 EXPECT_EQ(0u,
2835 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2836
2837 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2838 ASSERT_TRUE(annot);
2839
2840 EXPECT_EQ(0u,
2841 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2842
2843 unsigned long length_bytes =
2844 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2845 ASSERT_EQ(2u, length_bytes);
2846 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2847 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2848 buf.data(), length_bytes));
2849 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2850 }
2851 {
2852 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2853 ASSERT_TRUE(annot);
2854
2855 unsigned long length_bytes =
2856 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2857 ASSERT_EQ(18u, length_bytes);
2858 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2859 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2860 buf.data(), length_bytes));
2861 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2862 }
2863 UnloadPage(page);
2864}
2865
2866TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2867 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2868 FPDF_PAGE page = LoadPage(0);
2869 ASSERT_TRUE(page);
2870
2871 {
2872 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2873 ASSERT_TRUE(annot);
2874
2875 unsigned long length_bytes =
2876 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2877 ASSERT_EQ(2u, length_bytes);
2878 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2879 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2880 buf.data(), length_bytes));
2881 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2882 }
2883 {
2884 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2885 ASSERT_TRUE(annot);
2886
2887 unsigned long length_bytes =
2888 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2889 ASSERT_EQ(14u, length_bytes);
2890 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2891 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2892 buf.data(), length_bytes));
2893 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2894 }
2895 UnloadPage(page);
2896}
2897
2898TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2899 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2900 FPDF_PAGE page = LoadPage(0);
2901 ASSERT_TRUE(page);
2902
2903 {
2904 EXPECT_EQ(0u,
2905 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2906
2907 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2908 ASSERT_TRUE(annot);
2909
2910 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2911
2912 unsigned long length_bytes =
2913 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2914 ASSERT_EQ(18u, length_bytes);
2915 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2916 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2917 buf.data(), length_bytes));
2918 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2919 }
2920 UnloadPage(page);
2921}
2922
2923TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2924 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2925 FPDF_PAGE page = LoadPage(0);
2926 ASSERT_TRUE(page);
2927
2928 {
2929 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2930 ASSERT_TRUE(annot);
2931
2932 unsigned long length_bytes =
2933 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2934 ASSERT_EQ(30u, length_bytes);
2935 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2936 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2937 buf.data(), length_bytes));
2938 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2939 }
2940 UnloadPage(page);
2941}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002942
Lei Zhang9b444002020-04-17 17:35:23 +00002943TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2944 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002945 FPDF_PAGE page = LoadPage(0);
2946 ASSERT_TRUE(page);
2947
Lei Zhang9b444002020-04-17 17:35:23 +00002948 // Verify widgets are by default focusable.
2949 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2950 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002951
Lei Zhang9b444002020-04-17 17:35:23 +00002952 // Expected annot subtypes for page 0 of annots.pdf.
2953 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2954 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2955 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2956 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2957 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002958
Lei Zhang9b444002020-04-17 17:35:23 +00002959 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002960 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002961 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2962 kExpectedAnnotSubtypes,
2963 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002964
Lei Zhang9b444002020-04-17 17:35:23 +00002965 // Make no annotation type focusable using the preferred method.
2966 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2967 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002968
Lei Zhang9b444002020-04-17 17:35:23 +00002969 // Restore the focusable type count back to 1, then set it back to 0 using a
2970 // different method.
2971 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2972 ASSERT_TRUE(
2973 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2974 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002975
Lei Zhang9b444002020-04-17 17:35:23 +00002976 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002977 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002978
Lei Zhang9b444002020-04-17 17:35:23 +00002979 // Now make links focusable.
2980 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2981 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002982
Lei Zhang9b444002020-04-17 17:35:23 +00002983 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002984 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002985 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2986 kExpectedAnnotSubtypes,
2987 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002988
Lei Zhang9b444002020-04-17 17:35:23 +00002989 // Test invalid parameters.
2990 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00002991 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002992 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00002993 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002994 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2995
Lei Zhang9b444002020-04-17 17:35:23 +00002996 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2997 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2998 subtypes.size()));
2999 EXPECT_FALSE(
3000 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
3001 EXPECT_FALSE(
3002 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003003
3004 UnloadPage(page);
3005}
Lei Zhang671aece2020-04-14 19:02:26 +00003006
Hui Yingstea3816d2020-07-28 22:35:11 +00003007TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00003008 ASSERT_TRUE(OpenDocument("annots.pdf"));
3009 FPDF_PAGE page = LoadPage(0);
3010 ASSERT_TRUE(page);
3011
3012 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003013#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003014 static const char kMd5sum[] = "b4c8f1dab175508810c476d078ebc5a6";
Lei Zhang42d30c22022-01-12 19:24:43 +00003015#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003016 static const char kMd5sum[] = "108a46c517c4eaace9982ee83e8e3296";
Lei Zhang671aece2020-04-14 19:02:26 +00003017#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003018 static const char kMd5sum[] = "5550d8dcb4d1af1f50e8b4bcaef2ee60";
Lei Zhang671aece2020-04-14 19:02:26 +00003019#endif
3020 // Check the initial rendering.
3021 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3022 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3023 }
3024
3025 // Make links and highlights focusable.
3026 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
3027 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00003028 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00003029 ASSERT_TRUE(
3030 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
3031 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3032 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
3033 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
3034 subtypes.size()));
3035 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
3036 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
3037
3038 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003039#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003040 static const char kMd5sum[] = "9173db3a892bc1697eef5cdaed19eda6";
Lei Zhang42d30c22022-01-12 19:24:43 +00003041#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003042 static const char kMd5sum[] = "eb3869335e7a219e1b5f25c1c6037b97";
Lei Zhang671aece2020-04-14 19:02:26 +00003043#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003044 static const char kMd5sum[] = "805fe7bb751ac4ed2b82bb66efe6db40";
Lei Zhang671aece2020-04-14 19:02:26 +00003045#endif
3046 // Focus the first link and check the rendering.
3047 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3048 ASSERT_TRUE(annot);
3049 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3050 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3051 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3052 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3053 }
3054
3055 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003056#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003057 static const char kMd5sum[] = "174dbdb218c2b14011c9c1db67fe41c3";
Lei Zhang42d30c22022-01-12 19:24:43 +00003058#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003059 static const char kMd5sum[] = "d20b1978da2362d3942ea0fc6d230997";
Lei Zhang671aece2020-04-14 19:02:26 +00003060#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003061 static const char kMd5sum[] = "c5c5dcb462af3ef5f43b298ec048feef";
Lei Zhang671aece2020-04-14 19:02:26 +00003062#endif
3063 // Focus the first highlight and check the rendering.
3064 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3065 ASSERT_TRUE(annot);
3066 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3067 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3068 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3069 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3070 }
3071
3072 UnloadPage(page);
3073}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003074
3075TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
3076 ASSERT_TRUE(OpenDocument("annots.pdf"));
3077 FPDF_PAGE page = LoadPage(0);
3078 ASSERT_TRUE(page);
3079 {
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003080 constexpr char kExpectedResult[] =
3081 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3082 "fpdf_text.h";
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003083
Lei Zhang306874b2021-04-16 00:33:36 +00003084 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3085 ASSERT_TRUE(annot);
3086 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3087 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()),
3088 kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003089 }
3090
3091 {
3092 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3093 ASSERT_TRUE(annot);
3094 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3095 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3096 }
3097
3098 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3099
3100 UnloadPage(page);
3101}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003102
3103TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3104 // Open a file with radio button widget annotations and load its first page.
3105 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3106 FPDF_PAGE page = LoadPage(0);
3107 ASSERT_TRUE(page);
3108
3109 {
3110 // Checks for bad annot.
3111 EXPECT_EQ(-1,
3112 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3113
3114 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3115 ASSERT_TRUE(annot);
3116
3117 // Checks for bad form handle.
3118 EXPECT_EQ(-1,
3119 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3120
3121 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3122 }
3123
3124 UnloadPage(page);
3125}
3126
3127TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3128 // Open a file with checkbox widget annotations and load its first page.
3129 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3130 FPDF_PAGE page = LoadPage(0);
3131 ASSERT_TRUE(page);
3132
3133 {
3134 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3135 ASSERT_TRUE(annot);
3136 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3137 }
3138
3139 UnloadPage(page);
3140}
3141
3142TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3143 // Open a file with ink annotations and load its first page.
3144 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3145 FPDF_PAGE page = LoadPage(0);
3146 ASSERT_TRUE(page);
3147
3148 {
3149 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3150 ASSERT_TRUE(annot);
3151 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3152 }
3153
3154 UnloadPage(page);
3155}
3156
3157TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3158 // Open a file with radio button widget annotations and load its first page.
3159 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3160 FPDF_PAGE page = LoadPage(0);
3161 ASSERT_TRUE(page);
3162
3163 {
3164 // Checks for bad annot.
3165 EXPECT_EQ(-1,
3166 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3167
3168 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3169 ASSERT_TRUE(annot);
3170
3171 // Checks for bad form handle.
3172 EXPECT_EQ(-1,
3173 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3174
3175 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3176 }
3177
3178 UnloadPage(page);
3179}
3180
3181TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3182 // Open a file with checkbox widget annotations and load its first page.
3183 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3184 FPDF_PAGE page = LoadPage(0);
3185 ASSERT_TRUE(page);
3186
3187 {
3188 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3189 ASSERT_TRUE(annot);
3190 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3191 }
3192
3193 UnloadPage(page);
3194}
3195
3196TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3197 // Open a file with ink annotations and load its first page.
3198 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3199 FPDF_PAGE page = LoadPage(0);
3200 ASSERT_TRUE(page);
3201
3202 {
3203 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3204 ASSERT_TRUE(annot);
3205 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3206 }
3207
3208 UnloadPage(page);
3209}
3210
3211TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3212 // Open a file with radio button widget annotations and load its first page.
3213 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3214 FPDF_PAGE page = LoadPage(0);
3215 ASSERT_TRUE(page);
3216
3217 {
3218 // Checks for bad annot.
3219 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3220 form_handle(), /*annot=*/nullptr,
3221 /*buffer=*/nullptr, /*buflen=*/0));
3222
3223 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3224 ASSERT_TRUE(annot);
3225
3226 // Checks for bad form handle.
3227 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3228 /*hHandle=*/nullptr, annot.get(),
3229 /*buffer=*/nullptr, /*buflen=*/0));
3230
3231 unsigned long length_bytes =
3232 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3233 /*buffer=*/nullptr, /*buflen=*/0);
3234 ASSERT_EQ(14u, length_bytes);
3235 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3236 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3237 buf.data(), length_bytes));
3238 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3239 }
3240
3241 UnloadPage(page);
3242}
3243
3244TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3245 // Open a file with checkbox widget annotations and load its first page.
3246 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3247 FPDF_PAGE page = LoadPage(0);
3248 ASSERT_TRUE(page);
3249
3250 {
3251 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3252 ASSERT_TRUE(annot);
3253
3254 unsigned long length_bytes =
3255 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3256 /*buffer=*/nullptr, /*buflen=*/0);
3257 ASSERT_EQ(8u, length_bytes);
3258 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3259 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3260 buf.data(), length_bytes));
3261 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3262 }
3263
3264 UnloadPage(page);
3265}
3266
3267TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3268 // Open a file with ink annotations and load its first page.
3269 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3270 FPDF_PAGE page = LoadPage(0);
3271 ASSERT_TRUE(page);
3272
3273 {
3274 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3275 ASSERT_TRUE(annot);
3276 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3277 /*buffer=*/nullptr,
3278 /*buflen=*/0));
3279 }
3280
3281 UnloadPage(page);
3282}
Miklos Vajnad1a24fb2020-10-26 18:07:13 +00003283
3284TEST_F(FPDFAnnotEmbedderTest, Redactannotation) {
3285 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3286 FPDF_PAGE page = LoadPage(0);
3287 ASSERT_TRUE(page);
3288 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3289
3290 {
3291 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3292 ASSERT_TRUE(annot);
3293 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3294 }
3295
3296 UnloadPage(page);
3297}
Miklos Vajna09ecef62020-11-10 21:50:38 +00003298
3299TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) {
3300 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3301 FPDF_PAGE page = LoadPage(0);
3302 ASSERT_TRUE(page);
3303 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3304
3305 {
3306 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3307 ASSERT_TRUE(annot);
3308
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003309 // FPDFAnnot_GetVertices() positive testing.
Miklos Vajna09ecef62020-11-10 21:50:38 +00003310 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3311 const size_t kExpectedSize = 3;
3312 ASSERT_EQ(kExpectedSize, size);
3313 std::vector<FS_POINTF> vertices_buffer(size);
3314 EXPECT_EQ(size,
3315 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3316 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3317 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3318 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3319 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3320 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3321 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3322
3323 // FPDFAnnot_GetVertices() negative testing.
3324 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3325
3326 // vertices_buffer is not overwritten if it is too small.
3327 vertices_buffer.resize(1);
3328 vertices_buffer[0].x = 42;
3329 vertices_buffer[0].y = 43;
3330 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3331 vertices_buffer.size());
3332 EXPECT_EQ(kExpectedSize, size);
3333 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3334 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3335 }
3336
3337 {
3338 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3339 ASSERT_TRUE(annot);
3340
3341 // This has an odd number of elements in the vertices array, ignore the last
3342 // element.
3343 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3344 const size_t kExpectedSize = 3;
3345 ASSERT_EQ(kExpectedSize, size);
3346 std::vector<FS_POINTF> vertices_buffer(size);
3347 EXPECT_EQ(size,
3348 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3349 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3350 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3351 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3352 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3353 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3354 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3355 }
3356
3357 {
3358 // Wrong annotation type.
3359 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3360 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3361 }
3362
3363 UnloadPage(page);
3364}
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003365
3366TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) {
3367 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3368 FPDF_PAGE page = LoadPage(0);
3369 ASSERT_TRUE(page);
3370 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3371
3372 {
3373 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3374 ASSERT_TRUE(annot);
3375
3376 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3377 // testing.
3378 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3379 const size_t kExpectedSize = 1;
3380 ASSERT_EQ(kExpectedSize, size);
3381 const unsigned long kPathIndex = 0;
3382 unsigned long path_size =
3383 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3384 const size_t kExpectedPathSize = 3;
3385 ASSERT_EQ(kExpectedPathSize, path_size);
3386 std::vector<FS_POINTF> path_buffer(path_size);
3387 EXPECT_EQ(path_size,
3388 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3389 path_buffer.data(), path_size));
3390 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3391 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3392 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3393 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3394 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3395 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3396
3397 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3398 // testing.
3399 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3400 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3401
3402 // out of bounds path_index.
3403 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3404
3405 // path_buffer is not overwritten if it is too small.
3406 path_buffer.resize(1);
3407 path_buffer[0].x = 42;
3408 path_buffer[0].y = 43;
3409 path_size = FPDFAnnot_GetInkListPath(
3410 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3411 EXPECT_EQ(kExpectedSize, size);
3412 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3413 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3414 }
3415
3416 {
3417 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3418 ASSERT_TRUE(annot);
3419
3420 // This has an odd number of elements in the path array, ignore the last
3421 // element.
3422 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3423 const size_t kExpectedSize = 1;
3424 ASSERT_EQ(kExpectedSize, size);
3425 const unsigned long kPathIndex = 0;
3426 unsigned long path_size =
3427 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3428 const size_t kExpectedPathSize = 3;
3429 ASSERT_EQ(kExpectedPathSize, path_size);
3430 std::vector<FS_POINTF> path_buffer(path_size);
3431 EXPECT_EQ(path_size,
3432 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3433 path_buffer.data(), path_size));
3434 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3435 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3436 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3437 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3438 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3439 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3440 }
3441
3442 {
3443 // Wrong annotation type.
3444 ScopedFPDFAnnotation polygon_annot(
3445 FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON));
3446 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3447 const unsigned long kPathIndex = 0;
3448 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3449 nullptr, 0));
3450 }
3451
3452 UnloadPage(page);
3453}
Miklos Vajna30f45a62020-12-04 19:12:31 +00003454
3455TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) {
3456 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3457 FPDF_PAGE page = LoadPage(0);
3458 ASSERT_TRUE(page);
3459 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3460
3461 {
3462 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3463 ASSERT_TRUE(annot);
3464
3465 // FPDFAnnot_GetVertices() positive testing.
3466 FS_POINTF start;
3467 FS_POINTF end;
3468 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3469 EXPECT_FLOAT_EQ(159.0f, start.x);
3470 EXPECT_FLOAT_EQ(296.0f, start.y);
3471 EXPECT_FLOAT_EQ(472.0f, end.x);
3472 EXPECT_FLOAT_EQ(243.42f, end.y);
3473
3474 // FPDFAnnot_GetVertices() negative testing.
3475 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3476 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3477 }
3478
3479 {
3480 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3481 ASSERT_TRUE(annot);
3482
3483 // Too few elements in the line array.
3484 FS_POINTF start;
3485 FS_POINTF end;
3486 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3487 }
3488
3489 {
3490 // Wrong annotation type.
3491 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3492 FS_POINTF start;
3493 FS_POINTF end;
3494 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3495 }
3496
3497 UnloadPage(page);
3498}
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003499
3500TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) {
3501 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3502 FPDF_PAGE page = LoadPage(0);
3503 ASSERT_TRUE(page);
3504 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3505
3506 {
3507 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3508 ASSERT_TRUE(annot);
3509
3510 // FPDFAnnot_GetBorder() positive testing.
3511 float horizontal_radius;
3512 float vertical_radius;
3513 float border_width;
3514 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3515 &vertical_radius, &border_width));
3516 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3517 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3518 EXPECT_FLOAT_EQ(2.0f, border_width);
3519
3520 // FPDFAnnot_GetBorder() negative testing.
3521 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3522 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3523 }
3524
3525 {
3526 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3527 ASSERT_TRUE(annot);
3528
3529 // Too few elements in the border array.
3530 float horizontal_radius;
3531 float vertical_radius;
3532 float border_width;
3533 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3534 &vertical_radius, &border_width));
Lei Zhang21ea6652021-04-15 23:24:46 +00003535
3536 // FPDFAnnot_SetBorder() positive testing.
3537 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3538 /*vertical_radius=*/3.5f,
3539 /*border_width=*/4.0f));
3540
3541 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3542 &vertical_radius, &border_width));
3543 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3544 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3545 EXPECT_FLOAT_EQ(4.0f, border_width);
3546
3547 // FPDFAnnot_SetBorder() negative testing.
3548 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3549 /*vertical_radius=*/2.5f,
3550 /*border_width=*/3.0f));
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003551 }
3552
3553 UnloadPage(page);
3554}
Lei Zhang24de1492021-04-19 18:06:43 +00003555
3556// Due to https://crbug.com/pdfium/570, the AnnotationBorder test above cannot
3557// actually render the line annotations inside line_annot.pdf. For now, use a
3558// square annotation in annots.pdf for testing.
3559TEST_F(FPDFAnnotEmbedderTest, AnnotationBorderRendering) {
3560 ASSERT_TRUE(OpenDocument("annots.pdf"));
3561 FPDF_PAGE page = LoadPage(1);
3562 ASSERT_TRUE(page);
3563 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
3564
3565#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003566 constexpr char kOriginalChecksum[] = "4f35703e89202bcc8419ca2df739bb4e";
3567 constexpr char kModifiedChecksum[] = "cee0a1b41f33d487af8fb70c4c82e3c9";
Lei Zhang42d30c22022-01-12 19:24:43 +00003568#elif BUILDFLAG(IS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003569 constexpr char kOriginalChecksum[] = "522a4a6b6c7eab5bf95ded1f21ea372e";
3570 constexpr char kModifiedChecksum[] = "6844019e07b83cc01723415f58218d06";
Lei Zhang24de1492021-04-19 18:06:43 +00003571#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003572 constexpr char kOriginalChecksum[] = "12127303aecd80c6288460f7c0d79f3f";
3573 constexpr char kModifiedChecksum[] = "73d06ff4c665fe85029acef30240dcca";
Lei Zhang24de1492021-04-19 18:06:43 +00003574#endif
3575
3576 {
3577 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
3578 ASSERT_TRUE(annot);
3579 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
3580
3581 {
3582 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3583 CompareBitmap(bitmap.get(), 612, 792, kOriginalChecksum);
3584 }
3585
3586 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3587 /*vertical_radius=*/3.5f,
3588 /*border_width=*/4.0f));
3589
3590 {
3591 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3592 CompareBitmap(bitmap.get(), 612, 792, kModifiedChecksum);
3593 }
3594 }
3595
3596 // Save the document and close the page.
3597 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
3598 UnloadPage(page);
3599
3600 ASSERT_TRUE(OpenSavedDocument());
3601 page = LoadSavedPage(1);
3602 ASSERT_TRUE(page);
3603 VerifySavedRendering(page, 612, 792, kModifiedChecksum);
3604
3605 CloseSavedPage(page);
3606 CloseSavedDocument();
3607}