blob: 820fdc79f579139e37bd463036f98edbb5c81560 [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"
16#include "core/fpdfapi/page/cpdf_pagemodule.h"
17#include "core/fpdfapi/parser/cpdf_array.h"
18#include "core/fpdfapi/parser/cpdf_dictionary.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040019#include "core/fxcrt/fx_system.h"
Tom Sepez204ab052020-06-12 21:33:48 +000020#include "fpdfsdk/cpdfsdk_helpers.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000021#include "public/cpp/fpdf_scopers.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040022#include "public/fpdf_edit.h"
Mansi Awasthi07bf7e62020-01-24 10:34:17 +000023#include "public/fpdf_formfill.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040024#include "public/fpdfview.h"
25#include "testing/embedder_test.h"
Hui Yingstb4baceb2020-04-28 23:46:10 +000026#include "testing/embedder_test_constants.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000027#include "testing/fx_string_testhelpers.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000028#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040029#include "testing/gtest/include/gtest/gtest.h"
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000030#include "testing/utils/hash.h"
Lei Zhang87e3d7a2021-06-17 19:40:28 +000031#include "third_party/base/containers/contains.h"
Lei Zhang532886d2021-06-17 19:10:08 +000032#include "third_party/base/cxx17_backports.h"
Lei Zhang9b444002020-04-17 17:35:23 +000033#include "third_party/base/span.h"
34
Hui Yingst30bfcc52020-07-27 23:54:40 +000035using pdfium::kAnnotationStampWithApChecksum;
36
Lei Zhang9b444002020-04-17 17:35:23 +000037namespace {
38
Tom Sepez204ab052020-06-12 21:33:48 +000039const wchar_t kStreamData[] =
40 L"/GS gs 0.0 0.0 0.0 RG 4 w 211.8 747.6 m 211.8 744.8 "
41 L"212.6 743.0 214.2 740.8 "
42 L"c 215.4 739.0 216.8 737.1 218.9 736.1 c 220.8 735.1 221.4 733.0 "
43 L"223.7 732.4 c 232.6 729.9 242.0 730.8 251.2 730.8 c 257.5 730.8 "
44 L"263.0 732.9 269.0 734.4 c S";
45
Lei Zhang9b444002020-04-17 17:35:23 +000046void VerifyFocusableAnnotSubtypes(
47 FPDF_FORMHANDLE form_handle,
48 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) {
49 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
50 FPDFAnnot_GetFocusableSubtypesCount(form_handle));
51
52 std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes(
53 expected_subtypes.size());
54 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(
55 form_handle, actual_subtypes.data(), actual_subtypes.size()));
56 for (size_t i = 0; i < expected_subtypes.size(); ++i)
57 ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]);
58}
59
60void SetAndVerifyFocusableAnnotSubtypes(
61 FPDF_FORMHANDLE form_handle,
62 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) {
63 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(),
64 subtypes.size()));
65 VerifyFocusableAnnotSubtypes(form_handle, subtypes);
66}
67
68void VerifyAnnotationSubtypesAndFocusability(
69 FPDF_FORMHANDLE form_handle,
70 FPDF_PAGE page,
71 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes,
72 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) {
73 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
74 FPDFPage_GetAnnotCount(page));
75 for (size_t i = 0; i < expected_subtypes.size(); ++i) {
76 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
77 ASSERT_TRUE(annot);
78 EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get()));
79
Lei Zhangf245ae62020-05-15 21:49:46 +000080 bool expected_focusable =
81 pdfium::Contains(expected_focusable_subtypes, expected_subtypes[i]);
Lei Zhang9b444002020-04-17 17:35:23 +000082 EXPECT_EQ(expected_focusable,
83 FORM_SetFocusedAnnot(form_handle, annot.get()));
84
85 // Kill the focus so the next test starts in an unfocused state.
86 FORM_ForceToKillFocus(form_handle);
87 }
88}
89
Lei Zhang306874b2021-04-16 00:33:36 +000090void VerifyUriActionInLink(FPDF_DOCUMENT doc,
91 FPDF_LINK link,
92 const std::string& expected_uri) {
93 ASSERT_TRUE(link);
94
95 FPDF_ACTION action = FPDFLink_GetAction(link);
96 ASSERT_TRUE(action);
97 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
98 FPDFAction_GetType(action));
99
100 unsigned long bufsize = FPDFAction_GetURIPath(doc, action, nullptr, 0);
101 ASSERT_EQ(expected_uri.size() + 1, bufsize);
102
103 std::vector<char> buffer(bufsize);
104 EXPECT_EQ(bufsize,
105 FPDFAction_GetURIPath(doc, action, buffer.data(), bufsize));
106 EXPECT_STREQ(expected_uri.c_str(), buffer.data());
107}
108
Lei Zhang9b444002020-04-17 17:35:23 +0000109} // namespace
Jane Liu4fd9a472017-06-01 18:56:09 -0400110
Lei Zhangab41f252018-12-23 03:10:50 +0000111class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -0400112
Tom Sepez204ab052020-06-12 21:33:48 +0000113TEST_F(FPDFAnnotEmbedderTest, SetAP) {
114 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
115 ASSERT_TRUE(doc);
116 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
117 ASSERT_TRUE(page);
118 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
119 ASSERT_TRUE(ap_stream);
120
121 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
122 ASSERT_TRUE(annot);
123
124 // Negative case: FPDFAnnot_SetAP() should fail if bounding rect is not yet
125 // set on the annotation.
126 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
127 ap_stream.get()));
128
129 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
130 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
131
132 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
133 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/255));
134
135 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
136 ap_stream.get()));
137
138 // Verify that appearance stream is created as form XObject
139 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
140 ASSERT_TRUE(context);
141 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
142 ASSERT_TRUE(annot_dict);
143 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
144 ASSERT_TRUE(ap_dict);
145 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
146 ASSERT_TRUE(stream_dict);
147 // Check for non-existence of resources dictionary in case of opaque color
148 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
149 ASSERT_FALSE(resources_dict);
150 ByteString type = stream_dict->GetStringFor(pdfium::annotation::kType);
151 EXPECT_EQ("XObject", type);
152 ByteString sub_type = stream_dict->GetStringFor(pdfium::annotation::kSubtype);
153 EXPECT_EQ("Form", sub_type);
154
155 // Check that the appearance stream is same as we just set.
156 const uint32_t kStreamDataSize =
157 pdfium::size(kStreamData) * sizeof(FPDF_WCHAR);
158 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
159 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
160 ASSERT_EQ(kStreamDataSize, normal_length_bytes);
161 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
162 EXPECT_EQ(kStreamDataSize,
163 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
164 buf.data(), normal_length_bytes));
165 EXPECT_EQ(kStreamData, GetPlatformWString(buf.data()));
166}
167
168TEST_F(FPDFAnnotEmbedderTest, SetAPWithOpacity) {
169 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
170 ASSERT_TRUE(doc);
171 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
172 ASSERT_TRUE(page);
173 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
174 ASSERT_TRUE(ap_stream);
175
176 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
177 ASSERT_TRUE(annot);
178
179 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
180 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/102));
181
182 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
183 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
184
185 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
186 ap_stream.get()));
187
188 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
189 ASSERT_TRUE(context);
190 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
191 ASSERT_TRUE(annot_dict);
192 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
193 ASSERT_TRUE(ap_dict);
194 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
195 ASSERT_TRUE(stream_dict);
196 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
197 ASSERT_TRUE(stream_dict);
198 CPDF_Dictionary* extGState_dict = resources_dict->GetDictFor("ExtGState");
199 ASSERT_TRUE(extGState_dict);
200 CPDF_Dictionary* gs_dict = extGState_dict->GetDictFor("GS");
201 ASSERT_TRUE(gs_dict);
202 ByteString type = gs_dict->GetStringFor(pdfium::annotation::kType);
203 EXPECT_EQ("ExtGState", type);
204 float opacity = gs_dict->GetNumberFor("CA");
205 // Opacity value of 102 is represented as 0.4f (=104/255) in /CA entry.
206 EXPECT_FLOAT_EQ(0.4f, opacity);
207 ByteString blend_mode = gs_dict->GetStringFor("BM");
208 EXPECT_EQ("Normal", blend_mode);
209 bool alpha_source_flag = gs_dict->GetBooleanFor("AIS", true);
210 EXPECT_FALSE(alpha_source_flag);
211}
212
213TEST_F(FPDFAnnotEmbedderTest, InkListAPIValidations) {
214 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
215 ASSERT_TRUE(doc);
216 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
217 ASSERT_TRUE(page);
218
219 // Create a new ink annotation.
220 ScopedFPDFAnnotation ink_annot(
221 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
222 ASSERT_TRUE(ink_annot);
223 CPDF_AnnotContext* context =
224 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
225 ASSERT_TRUE(context);
226 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
227 ASSERT_TRUE(annot_dict);
228
229 static constexpr FS_POINTF kFirstInkStroke[] = {
230 {80.0f, 90.0f}, {81.0f, 91.0f}, {82.0f, 92.0f},
231 {83.0f, 93.0f}, {84.0f, 94.0f}, {85.0f, 95.0f}};
232 static constexpr size_t kFirstStrokePointCount =
233 pdfium::size(kFirstInkStroke);
234
235 static constexpr FS_POINTF kSecondInkStroke[] = {
236 {70.0f, 90.0f}, {71.0f, 91.0f}, {72.0f, 92.0f}};
237 static constexpr size_t kSecondStrokePointCount =
238 pdfium::size(kSecondInkStroke);
239
240 static constexpr FS_POINTF kThirdInkStroke[] = {{60.0f, 90.0f},
241 {61.0f, 91.0f},
242 {62.0f, 92.0f},
243 {63.0f, 93.0f},
244 {64.0f, 94.0f}};
245 static constexpr size_t kThirdStrokePointCount =
246 pdfium::size(kThirdInkStroke);
247
248 // Negative test: |annot| is passed as nullptr.
249 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(nullptr, kFirstInkStroke,
250 kFirstStrokePointCount));
251
252 // Negative test: |annot| is not ink annotation.
253 // Create a new highlight annotation.
254 ScopedFPDFAnnotation highlight_annot(
255 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
256 ASSERT_TRUE(highlight_annot);
257 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(highlight_annot.get(), kFirstInkStroke,
258 kFirstStrokePointCount));
259
260 // Negative test: passing |point_count| as 0.
261 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke, 0));
262
263 // Negative test: passing |points| array as nullptr.
264 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), nullptr,
265 kFirstStrokePointCount));
266
267 // Negative test: passing |point_count| more than ULONG_MAX/2.
268 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
269 ULONG_MAX / 2 + 1));
270
271 // InkStroke should get added to ink annotation. Also inklist should get
272 // created.
273 EXPECT_EQ(0, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke,
274 kFirstStrokePointCount));
275
276 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
277 ASSERT_TRUE(inklist);
278 EXPECT_EQ(1u, inklist->size());
279 EXPECT_EQ(kFirstStrokePointCount * 2, inklist->GetArrayAt(0)->size());
280
281 // Adding another inkStroke to ink annotation with all valid paremeters.
282 // InkList already exists in ink_annot.
283 EXPECT_EQ(1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
284 kSecondStrokePointCount));
285 EXPECT_EQ(2u, inklist->size());
286 EXPECT_EQ(kSecondStrokePointCount * 2, inklist->GetArrayAt(1)->size());
287
288 // Adding one more InkStroke to the ink annotation. |point_count| passed is
289 // less than the data available in |buffer|.
290 EXPECT_EQ(2, FPDFAnnot_AddInkStroke(ink_annot.get(), kThirdInkStroke,
291 kThirdStrokePointCount - 1));
292 EXPECT_EQ(3u, inklist->size());
293 EXPECT_EQ((kThirdStrokePointCount - 1) * 2, inklist->GetArrayAt(2)->size());
294}
295
296TEST_F(FPDFAnnotEmbedderTest, RemoveInkList) {
297 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
298 ASSERT_TRUE(doc);
299 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
300 ASSERT_TRUE(page);
301
302 // Negative test: |annot| is passed as nullptr.
303 EXPECT_FALSE(FPDFAnnot_RemoveInkList(nullptr));
304
305 // Negative test: |annot| is not ink annotation.
306 // Create a new highlight annotation.
307 ScopedFPDFAnnotation highlight_annot(
308 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
309 ASSERT_TRUE(highlight_annot);
310 EXPECT_FALSE(FPDFAnnot_RemoveInkList(highlight_annot.get()));
311
312 // Create a new ink annotation.
313 ScopedFPDFAnnotation ink_annot(
314 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
315 ASSERT_TRUE(ink_annot);
316 CPDF_AnnotContext* context =
317 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
318 ASSERT_TRUE(context);
319 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
320 ASSERT_TRUE(annot_dict);
321
322 static constexpr FS_POINTF kInkStroke[] = {{80.0f, 90.0f}, {81.0f, 91.0f},
323 {82.0f, 92.0f}, {83.0f, 93.0f},
324 {84.0f, 94.0f}, {85.0f, 95.0f}};
325 static constexpr size_t kPointCount = pdfium::size(kInkStroke);
326
327 // InkStroke should get added to ink annotation. Also inklist should get
328 // created.
329 EXPECT_EQ(0,
330 FPDFAnnot_AddInkStroke(ink_annot.get(), kInkStroke, kPointCount));
331
332 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
333 ASSERT_TRUE(inklist);
334 ASSERT_EQ(1u, inklist->size());
335 EXPECT_EQ(kPointCount * 2, inklist->GetArrayAt(0)->size());
336
337 // Remove inklist.
338 EXPECT_TRUE(FPDFAnnot_RemoveInkList(ink_annot.get()));
339 EXPECT_FALSE(annot_dict->KeyExist("InkList"));
340}
341
Lei Zhangab41f252018-12-23 03:10:50 +0000342TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +0000343 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
344 FPDF_PAGE page = LoadPage(0);
345 ASSERT_TRUE(page);
346
347 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
348
349 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
350 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
351 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
352 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
353 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
354
355 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
356
357 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
358
359 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
360 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
361 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
362
363 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
364
Lei Zhang4f556b82019-04-08 16:32:41 +0000365 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +0000366 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +0000367 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
368
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000369 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +0000370 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
371 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
372 EXPECT_EQ(0u,
373 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
374
375 UnloadPage(page);
376}
377
Lei Zhang3d9a0972019-03-04 19:34:09 +0000378TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
379 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
380 FPDF_PAGE page = LoadPage(0);
381 ASSERT_TRUE(page);
382
383 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +0000384 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +0000385
386 UnloadPage(page);
387}
388
Lei Zhangab41f252018-12-23 03:10:50 +0000389TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -0400390 // Open a file with one annotation and load its first page.
391 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000392 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -0400393 ASSERT_TRUE(page);
394
395 // This annotation has a malformed appearance stream, which does not have its
396 // normal appearance defined, only its rollover appearance. In this case, its
397 // normal appearance should be generated, allowing the highlight annotation to
398 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000399 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000400 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -0400401
402 UnloadPage(page);
403}
404
Hui Yingstd5b6f632020-07-22 01:31:59 +0000405TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang03e5e682019-09-16 19:45:55 +0000406#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstd5b6f632020-07-22 01:31:59 +0000407 static const char kChecksum[] = "ec1f4ccbd0aecfdea6d53893387a0101";
Lei Zhang03e5e682019-09-16 19:45:55 +0000408#else
Hui Yingstd5b6f632020-07-22 01:31:59 +0000409 static const char kChecksum[] = "76512832d88017668d9acc7aacd13dae";
Lei Zhang03e5e682019-09-16 19:45:55 +0000410#endif
Henrique Nakashima5098b252018-03-26 21:46:00 +0000411 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000412 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
413 FPDF_PAGE page = LoadPage(0);
414 ASSERT_TRUE(page);
415
Tom Sepeze08d2b12018-04-25 18:49:32 +0000416 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstd5b6f632020-07-22 01:31:59 +0000417 CompareBitmap(bitmap.get(), 595, 842, kChecksum);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000418
419 UnloadPage(page);
420}
421
Lei Zhangab41f252018-12-23 03:10:50 +0000422TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400423 // Open a file with one annotation and load its first page.
424 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000425 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400426 ASSERT_TRUE(page);
427
428 // Check that there is a total of 1 annotation on its first page.
429 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
430
431 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000432 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000433 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000434 ASSERT_TRUE(annot);
435 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400436
Lei Zhanga21d5932018-02-05 18:28:38 +0000437 // Check that the annotation color is yellow.
438 unsigned int R;
439 unsigned int G;
440 unsigned int B;
441 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000442 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000443 &G, &B, &A));
444 EXPECT_EQ(255u, R);
445 EXPECT_EQ(255u, G);
446 EXPECT_EQ(0u, B);
447 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400448
Lei Zhanga21d5932018-02-05 18:28:38 +0000449 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000450 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000451 EXPECT_EQ(FPDF_OBJECT_STRING,
452 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000453 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000454 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000455 ASSERT_EQ(28u, length_bytes);
456 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000457 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000458 length_bytes));
459 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400460
Lei Zhanga21d5932018-02-05 18:28:38 +0000461 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000462 EXPECT_EQ(
463 FPDF_OBJECT_STRING,
464 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000465 length_bytes = FPDFAnnot_GetStringValue(
466 annot.get(), pdfium::annotation::kContents, nullptr, 0);
467 ASSERT_EQ(2690u, length_bytes);
468 buf = GetFPDFWideStringBuffer(length_bytes);
469 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
470 pdfium::annotation::kContents,
471 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000472 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000473 L"This is a note for that highlight annotation. Very long highlight "
474 "annotation. Long long long Long 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 longLong long longLong long longLong long longLong long "
492 "longLong long long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000493 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400494
Lei Zhanga21d5932018-02-05 18:28:38 +0000495 // Check that the quadpoints are correct.
496 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000497 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000498 EXPECT_EQ(115.802643f, quadpoints.x1);
499 EXPECT_EQ(718.913940f, quadpoints.y1);
500 EXPECT_EQ(157.211182f, quadpoints.x4);
501 EXPECT_EQ(706.264465f, quadpoints.y4);
502 }
Tom Sepez507d0192018-11-07 16:37:51 +0000503 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400504}
505
Hui Yingst9b6b1542020-07-27 16:11:12 +0000506TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400507 // Open a file with three annotations and load its first page.
508 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000509 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400510 ASSERT_TRUE(page);
511
512 // Check that there is a total of 3 annotation on its first page.
513 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
514
Lei Zhanga21d5932018-02-05 18:28:38 +0000515 {
516 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000517 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000518 ASSERT_TRUE(annot);
519 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400520
Lei Zhanga21d5932018-02-05 18:28:38 +0000521 // Check that the annotation color is blue with opacity.
522 unsigned int R;
523 unsigned int G;
524 unsigned int B;
525 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000526 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000527 &G, &B, &A));
528 EXPECT_EQ(0u, R);
529 EXPECT_EQ(0u, G);
530 EXPECT_EQ(255u, B);
531 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400532
Lei Zhanga21d5932018-02-05 18:28:38 +0000533 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000534 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
535 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400536
Lei Zhang4f556b82019-04-08 16:32:41 +0000537 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000538 // Note that upon rendering, the rectangle coordinates will be adjusted.
539 FS_RECTF rect;
540 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
541 EXPECT_EQ(351.820404f, rect.left);
542 EXPECT_EQ(583.830688f, rect.bottom);
543 EXPECT_EQ(475.336090f, rect.right);
544 EXPECT_EQ(681.535034f, rect.top);
545 }
Tom Sepezef43c262018-11-07 16:41:32 +0000546 {
Hui Yingst57daee82020-10-09 05:47:20 +0000547#if defined(_SKIA_SUPPORT_) && defined(OS_APPLE)
548 static constexpr char kExpectedHash[] = "fad91b9c968fe8019a774f5e2419b8fc";
549#elif defined(_SKIA_SUPPORT_PATHS_) && defined(OS_APPLE)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000550 static constexpr char kExpectedHash[] = "acddfe688a117ead56af7b249a2cf8a1";
Hui Yingst57daee82020-10-09 05:47:20 +0000551#elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000552 static constexpr char kExpectedHash[] = "1fb0dd8dd5f0b9bb8d076e48eb59296d";
Hui Yingst57daee82020-10-09 05:47:20 +0000553#elif defined(OS_WIN)
Lei Zhang430b5322020-07-06 22:23:36 +0000554 static constexpr char kExpectedHash[] = "49d0a81c636531a337429325273d0508";
555#else
556 static constexpr char kExpectedHash[] = "354002e1c4386d38fdde29ef8d61074a";
Hui Yingst57daee82020-10-09 05:47:20 +0000557#endif
Tom Sepezef43c262018-11-07 16:41:32 +0000558 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang430b5322020-07-06 22:23:36 +0000559 CompareBitmap(bitmap.get(), 612, 792, kExpectedHash);
Tom Sepezef43c262018-11-07 16:41:32 +0000560 }
Tom Sepez507d0192018-11-07 16:37:51 +0000561 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400562}
Jane Liu20eafda2017-06-07 10:33:24 -0400563
Lei Zhangab41f252018-12-23 03:10:50 +0000564TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400565 // Open a file with one annotation and load its first page.
566 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000567 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400568 ASSERT_TRUE(page);
569
570 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400571 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400572
573 UnloadPage(page);
574}
575
Lei Zhangab41f252018-12-23 03:10:50 +0000576TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400577 // Open a file with no annotation and load its first page.
578 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000579 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400580 ASSERT_TRUE(page);
581 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
582
Lei Zhanga21d5932018-02-05 18:28:38 +0000583 {
584 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000585 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000586 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400587
Lei Zhanga21d5932018-02-05 18:28:38 +0000588 // Check that there is now 1 annotations on this page.
589 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400590
Lei Zhanga21d5932018-02-05 18:28:38 +0000591 // Check that the subtype of the annotation is correct.
592 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
593 }
Jane Liue10509a2017-06-20 16:47:41 -0400594
Lei Zhanga21d5932018-02-05 18:28:38 +0000595 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000596 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000597 ASSERT_TRUE(annot);
598 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400599
Lei Zhanga21d5932018-02-05 18:28:38 +0000600 // Set the color of the annotation.
601 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
602 102, 153, 204));
603 // Check that the color has been set correctly.
604 unsigned int R;
605 unsigned int G;
606 unsigned int B;
607 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000608 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000609 &G, &B, &A));
610 EXPECT_EQ(51u, R);
611 EXPECT_EQ(102u, G);
612 EXPECT_EQ(153u, B);
613 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400614
Lei Zhanga21d5932018-02-05 18:28:38 +0000615 // Change the color of the annotation.
616 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
617 153, 102, 51));
618 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000619 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000620 &G, &B, &A));
621 EXPECT_EQ(204u, R);
622 EXPECT_EQ(153u, G);
623 EXPECT_EQ(102u, B);
624 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400625
Lei Zhanga21d5932018-02-05 18:28:38 +0000626 // Set the annotation rectangle.
627 FS_RECTF rect;
628 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
629 EXPECT_EQ(0.f, rect.left);
630 EXPECT_EQ(0.f, rect.right);
631 rect.left = 35;
632 rect.bottom = 150;
633 rect.right = 53;
634 rect.top = 165;
635 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
636 // Check that the annotation rectangle has been set correctly.
637 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
638 EXPECT_EQ(35.f, rect.left);
639 EXPECT_EQ(150.f, rect.bottom);
640 EXPECT_EQ(53.f, rect.right);
641 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400642
Lei Zhanga21d5932018-02-05 18:28:38 +0000643 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000644 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000645 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000646 ASSERT_TRUE(FPDFAnnot_SetStringValue(
647 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000648 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000649 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000650 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000651 ASSERT_EQ(74u, length_bytes);
652 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
653 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
654 pdfium::annotation::kContents,
655 buf.data(), length_bytes));
656 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000657 }
Jane Liu20eafda2017-06-07 10:33:24 -0400658 UnloadPage(page);
659}
660
Lei Zhang81395aa2021-04-16 00:40:46 +0000661TEST_F(FPDFAnnotEmbedderTest, AddAndSaveLinkAnnotation) {
662 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
663 FPDF_PAGE page = LoadPage(0);
664 ASSERT_TRUE(page);
665 {
666 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
667 CompareBitmap(bitmap.get(), 200, 200, pdfium::kHelloWorldChecksum);
668 }
669 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
670
671 constexpr char kUri[] = "https://pdfium.org/";
672
673 {
674 // Add a link annotation to the page and set its URI.
675 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_LINK));
676 ASSERT_TRUE(annot);
677 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
678 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
679 EXPECT_TRUE(FPDFAnnot_SetURI(annot.get(), kUri));
680 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
681
682 // Negative tests:
683 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, nullptr));
684 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
685 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), nullptr));
686 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
687 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, kUri));
688 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
689
690 // Position the link on top of "Hello, world!" without a border.
691 const FS_RECTF kRect = {19.0f, 48.0f, 85.0f, 60.0f};
692 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &kRect));
693 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/0.0f,
694 /*vertical_radius=*/0.0f,
695 /*border_width=*/0.0f));
696
697 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
698 kUri);
699 }
700
701 {
702 // Add an ink annotation to the page. Trying to add a link to it fails.
703 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
704 ASSERT_TRUE(annot);
705 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
706 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
707 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), kUri));
708 }
709
710 // Remove the ink annotation added above for negative testing.
711 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
712 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
713
714 // Save the document, closing the page.
715 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
716 UnloadPage(page);
717
718 // Reopen the document and make sure it still renders the same. Since the link
719 // does not have a border, it does not affect the rendering.
720 ASSERT_TRUE(OpenSavedDocument());
721 page = LoadSavedPage(0);
722 ASSERT_TRUE(page);
723 VerifySavedRendering(page, 200, 200, pdfium::kHelloWorldChecksum);
724 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
725
726 {
727 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
728 ASSERT_TRUE(annot);
729 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
730 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
731 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
732 kUri);
733 }
734
735 CloseSavedPage(page);
736 CloseSavedDocument();
737}
738
Hui Yingstb3490322020-07-22 00:53:29 +0000739TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400740 // Open a file with one annotation and load its first page.
741 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000742 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400743 ASSERT_TRUE(page);
744
745 // Check that there is a total of one annotation on its first page, and verify
746 // its quadpoints.
747 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400748 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000749 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000750 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000751 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000752 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000753 EXPECT_EQ(115.802643f, quadpoints.x1);
754 EXPECT_EQ(718.913940f, quadpoints.y1);
755 EXPECT_EQ(157.211182f, quadpoints.x4);
756 EXPECT_EQ(706.264465f, quadpoints.y4);
757 }
Jane Liu20eafda2017-06-07 10:33:24 -0400758
759 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000760 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000761 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000762 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
763 ASSERT_TRUE(annot);
764 quadpoints.x1 = 140.802643f;
765 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000766 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000767 }
Jane Liu20eafda2017-06-07 10:33:24 -0400768
Lei Zhangec618142021-04-13 17:36:46 +0000769 // Save the document and close the page.
Jane Liu20eafda2017-06-07 10:33:24 -0400770 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000771 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400772
773 // Open the saved document.
Hui Yingst57daee82020-10-09 05:47:20 +0000774#if defined(_SKIA_SUPPORT_) && defined(OS_APPLE)
775 static const char kChecksum[] = "899387ae792390cd0d83cf7e2bbebfb5";
776#elif defined(_SKIA_SUPPORT_PATHS_) && defined(OS_APPLE)
777 static const char kChecksum[] = "e40e235ee35f47ff28dda009aaaf36df";
778#elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstb3490322020-07-22 00:53:29 +0000779 static const char kChecksum[] = "798fa41303381c9ba6d99092f5cd4d2b";
780#else
781 static const char kChecksum[] = "dba153419f67b7c0c0e3d22d3e8910d5";
782#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400783
Lei Zhang0b494052019-01-31 21:41:15 +0000784 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000785 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +0000786 ASSERT_TRUE(page);
Hui Yingstb3490322020-07-22 00:53:29 +0000787 VerifySavedRendering(page, 612, 792, kChecksum);
Jane Liu20eafda2017-06-07 10:33:24 -0400788
789 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000790 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400791
Lei Zhanga21d5932018-02-05 18:28:38 +0000792 {
793 // Check that the second annotation is an underline annotation and verify
794 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000795 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000796 ASSERT_TRUE(new_annot);
797 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
798 FS_QUADPOINTSF new_quadpoints;
799 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000800 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000801 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
802 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
803 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
804 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
805 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400806
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000807 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400808 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400809}
Jane Liu06462752017-06-27 16:41:14 -0400810
Lei Zhangab41f252018-12-23 03:10:50 +0000811TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000812 // Open a file with four annotations and load its first page.
813 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
814 FPDF_PAGE page = LoadPage(0);
815 ASSERT_TRUE(page);
816 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
817
818 // Retrieve the highlight annotation.
819 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
820 ASSERT_TRUE(annot);
821 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
822
823 FS_QUADPOINTSF quadpoints;
824 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
825
826 {
827 // Verify the current one set of quadpoints.
828 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
829
830 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
831 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
832 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
833 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
834 }
835
836 {
837 // Update the quadpoints.
838 FS_QUADPOINTSF new_quadpoints = quadpoints;
839 new_quadpoints.y1 -= 20.f;
840 new_quadpoints.y2 -= 20.f;
841 new_quadpoints.y3 -= 20.f;
842 new_quadpoints.y4 -= 20.f;
843 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
844
845 // Verify added quadpoint set
846 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
847 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
848 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
849 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
850 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
851 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
852 }
853
854 {
855 // Append a new set of quadpoints.
856 FS_QUADPOINTSF new_quadpoints = quadpoints;
857 new_quadpoints.y1 += 20.f;
858 new_quadpoints.y2 += 20.f;
859 new_quadpoints.y3 += 20.f;
860 new_quadpoints.y4 += 20.f;
861 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
862
863 // Verify added quadpoint set
864 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
865 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
866 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
867 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
868 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
869 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
870 }
871
872 {
873 // Setting and getting quadpoints at out-of-bound index should fail
874 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
875 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
876 }
877
878 FPDFPage_CloseAnnot(annot);
879
880 // Retrieve the square annotation
881 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
882
883 {
884 // Check that attempting to set its quadpoints would fail
885 ASSERT_TRUE(squareAnnot);
886 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
887 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
888 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
889 }
890
891 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000892 UnloadPage(page);
893}
894
Hui Yingstb64cd122020-07-27 16:01:02 +0000895// TODO(crbug.com/pdfium/1569): Fix this issue and enable the test for Skia.
896#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +0000897#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
898#else
899#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
900#endif
901TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Hui Yingstb64cd122020-07-27 16:01:02 +0000902#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
903 static const char kMd5Original[] = "00e70eb543c2a6e8f8aafb4ee951d9bf";
Lei Zhang4f556b82019-04-08 16:32:41 +0000904 static const char kMd5ModifiedHighlight[] =
Hui Yingstb64cd122020-07-27 16:01:02 +0000905 "7638c4a8fe4aabbf8704e198f49b3198";
906 static const char kMd5ModifiedSquare[] = "54f507af6af63de877b9cafdab1bbdaa";
907#else
908#if defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000909 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
910 static const char kMd5ModifiedHighlight[] =
911 "66f3caef3a7d488a4fa1ad37fc06310e";
912 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Lei Zhang0c03d632020-07-30 17:05:36 +0000913#elif defined(OS_APPLE)
Hui Yingstb64cd122020-07-27 16:01:02 +0000914 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
915 static const char kMd5ModifiedHighlight[] =
916 "e64bf648f6e9354d1f3eedb47a2c9498";
917 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Jane Liub370e5a2017-08-16 13:24:58 -0400918#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000919 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
920 static const char kMd5ModifiedHighlight[] =
921 "66f3caef3a7d488a4fa1ad37fc06310e";
922 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400923#endif
Hui Yingstb64cd122020-07-27 16:01:02 +0000924#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liub370e5a2017-08-16 13:24:58 -0400925
Jane Liu06462752017-06-27 16:41:14 -0400926 // Open a file with four annotations and load its first page.
927 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000928 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400929 ASSERT_TRUE(page);
930 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
931
Jane Liub370e5a2017-08-16 13:24:58 -0400932 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000933 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000934 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000935 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000936 }
Jane Liub370e5a2017-08-16 13:24:58 -0400937
Jane Liu0c6b07d2017-08-15 10:50:22 -0400938 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400939 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000940
941 // Retrieve the highlight annotation which has its AP stream already defined.
942 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000943 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000944 ASSERT_TRUE(annot);
945 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
946
947 // Check that color cannot be set when an AP stream is defined already.
948 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
949 102, 153, 204));
950
951 // Verify its attachment points.
952 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000953 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000954 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
955 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
956 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
957 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
958
959 // Check that updating the attachment points would succeed.
960 quadpoints.x1 -= 50.f;
961 quadpoints.x2 -= 50.f;
962 quadpoints.x3 -= 50.f;
963 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000964 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000965 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000966 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000967 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
968 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
969 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
970 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
971
972 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000973 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000974 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000975 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000976 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000977
978 // Verify its annotation rectangle.
979 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
980 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
981 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
982 EXPECT_NEAR(136.325f, rect.right, 0.001f);
983 EXPECT_NEAR(721.292f, rect.top, 0.001f);
984
985 // Check that updating the rectangle would succeed.
986 rect.left -= 60.f;
987 rect.right -= 60.f;
988 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
989 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
990 EXPECT_EQ(rect.right, new_rect.right);
991 }
Jane Liu06462752017-06-27 16:41:14 -0400992
Jane Liub370e5a2017-08-16 13:24:58 -0400993 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000994 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000995 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000996 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000997 }
Jane Liub370e5a2017-08-16 13:24:58 -0400998
Lei Zhanga21d5932018-02-05 18:28:38 +0000999 {
1000 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001001 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001002 ASSERT_TRUE(annot);
1003 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -04001004
Lei Zhanga21d5932018-02-05 18:28:38 +00001005 // Check that updating the rectangle would succeed.
1006 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1007 rect.left += 70.f;
1008 rect.right += 70.f;
1009 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1010 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1011 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -04001012
Lei Zhanga21d5932018-02-05 18:28:38 +00001013 // Check that updating the rectangle changes the square annotation's
1014 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001015 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001016 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +00001017 }
Jane Liub370e5a2017-08-16 13:24:58 -04001018
Jane Liu06462752017-06-27 16:41:14 -04001019 UnloadPage(page);
1020}
Jane Liu8ce58f52017-06-29 13:40:22 -04001021
Lei Zhangab41f252018-12-23 03:10:50 +00001022TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +00001023 // Open a file with multiline markup annotations.
1024 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
1025 FPDF_PAGE page = LoadPage(0);
1026 ASSERT_TRUE(page);
1027 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001028 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +00001029 ASSERT_TRUE(annot);
1030
1031 // This is a three line annotation.
1032 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
1033 }
1034 UnloadPage(page);
1035
1036 // null annotation should return 0
1037 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
1038}
1039
Lei Zhangab41f252018-12-23 03:10:50 +00001040TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -04001041 // Open a file with 3 annotations on its first page.
1042 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +00001043 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -04001044 ASSERT_TRUE(page);
1045 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1046
Jane Liu0c6b07d2017-08-15 10:50:22 -04001047 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -04001048
Lei Zhanga21d5932018-02-05 18:28:38 +00001049 // Check that the annotations have the expected rectangle coordinates.
1050 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001051 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001052 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1053 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1054 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001055
Lei Zhanga21d5932018-02-05 18:28:38 +00001056 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001057 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001058 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1059 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
1060 }
1061
1062 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001063 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001064 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1065 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1066 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001067
1068 // Check that nothing happens when attempting to remove an annotation with an
1069 // out-of-bound index.
1070 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
1071 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
1072 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1073
1074 // Remove the second annotation.
1075 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
1076 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1077 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
1078
Lei Zhangec618142021-04-13 17:36:46 +00001079 // Save the document and close the page.
Jane Liu8ce58f52017-06-29 13:40:22 -04001080 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +00001081 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -04001082
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001083 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -04001084 // Open the saved document.
1085 std::string new_file = GetString();
1086 FPDF_FILEACCESS file_access;
1087 memset(&file_access, 0, sizeof(file_access));
1088 file_access.m_FileLen = new_file.size();
1089 file_access.m_GetBlock = GetBlockFromString;
1090 file_access.m_Param = &new_file;
1091 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
1092 ASSERT_TRUE(new_doc);
1093 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
1094 ASSERT_TRUE(new_page);
1095
1096 // Check that the saved document has 2 annotations on the first page.
1097 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
1098
Lei Zhanga21d5932018-02-05 18:28:38 +00001099 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1100 // by verifying their rectangle coordinates.
1101 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001102 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001103 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1104 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1105 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001106
Lei Zhanga21d5932018-02-05 18:28:38 +00001107 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001108 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001109 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1110 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1111 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001112 FPDF_ClosePage(new_page);
1113 FPDF_CloseDocument(new_doc);
1114}
Jane Liu8ce58f52017-06-29 13:40:22 -04001115
Hui Yingst4a21df02020-05-13 03:15:44 +00001116TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001117#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001118 static const char kMd5ModifiedPath[] = "b820e4ae359db95cbac9823937c6da1a";
1119 static const char kMd5TwoPaths[] = "c53837b7bb6a9a21a846aa786526aa56";
1120 static const char kMd5NewAnnot[] = "4f0f4217156e4251036f369184a48967";
Lei Zhang0c03d632020-07-30 17:05:36 +00001121#elif defined(OS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001122 static const char kMd5ModifiedPath[] = "e31421f86c61d4e9cda138f15f561ca3";
1123 static const char kMd5TwoPaths[] = "58d932492f9d485d6a4bc0ba76c04557";
1124 static const char kMd5NewAnnot[] = "61f9ad13f2fd235753db198cf9704773";
Jane Liubaa7ff42017-06-29 19:18:23 -04001125#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001126 static const char kMd5ModifiedPath[] = "980e7636d864f7f7d323a31ad4e8fa04";
1127 static const char kMd5TwoPaths[] = "4c779c394b6790f8cf80305b566b663b";
1128 static const char kMd5NewAnnot[] = "97effd68dcf86273f68d126d6b45152e";
Jane Liubaa7ff42017-06-29 19:18:23 -04001129#endif
1130
1131 // Open a file with two annotations and load its first page.
1132 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001133 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001134 ASSERT_TRUE(page);
1135 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1136
1137 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001138 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001139 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001140 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001141 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001142
Lei Zhanga21d5932018-02-05 18:28:38 +00001143 {
1144 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001145 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001146 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001147
Lei Zhanga21d5932018-02-05 18:28:38 +00001148 // Check that this annotation has one path object and retrieve it.
1149 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001150 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001151 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1152 EXPECT_FALSE(path);
1153 path = FPDFAnnot_GetObject(annot.get(), 0);
1154 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1155 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001156
Lei Zhanga21d5932018-02-05 18:28:38 +00001157 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001158 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001159 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001160
Lei Zhanga21d5932018-02-05 18:28:38 +00001161 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001162 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001163 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001164 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001165 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001166
Lei Zhanga21d5932018-02-05 18:28:38 +00001167 // Add a second path object to the same annotation.
1168 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1169 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001170 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1171 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001172 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1173 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1174 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001175
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001176 // The object is in the annontation, not in the page, so the page object
1177 // array should not change.
1178 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1179
Lei Zhanga21d5932018-02-05 18:28:38 +00001180 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001181 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001182 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001183 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001184 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001185
Lei Zhanga21d5932018-02-05 18:28:38 +00001186 // Delete the newly added path object.
1187 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1188 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001189 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001190 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001191
1192 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001193 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001194 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001195 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001196 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001197
Jane Liubaa7ff42017-06-29 19:18:23 -04001198 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001199
Lei Zhanga21d5932018-02-05 18:28:38 +00001200 {
1201 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001202 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001203 ASSERT_TRUE(annot);
1204 rect.left = 200.f;
1205 rect.bottom = 400.f;
1206 rect.right = 500.f;
1207 rect.top = 600.f;
1208 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001209
Lei Zhanga21d5932018-02-05 18:28:38 +00001210 // Add a new path to the annotation.
1211 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1212 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1213 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1214 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1215 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001216 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1217 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001218 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1219 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1220 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1221
1222 // Check that the annotation's bounding box came from its rectangle.
1223 FS_RECTF new_rect;
1224 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1225 EXPECT_EQ(rect.left, new_rect.left);
1226 EXPECT_EQ(rect.bottom, new_rect.bottom);
1227 EXPECT_EQ(rect.right, new_rect.right);
1228 EXPECT_EQ(rect.top, new_rect.top);
1229 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001230
Lei Zhangec618142021-04-13 17:36:46 +00001231 // Save the document and close the page.
Jane Liubaa7ff42017-06-29 19:18:23 -04001232 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001233 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001234
1235 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001236 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001237 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001238 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001239 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001240
Jane Liu36567742017-07-06 11:13:35 -04001241 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001242 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001243
Lei Zhanga21d5932018-02-05 18:28:38 +00001244 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001245 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001246 ASSERT_TRUE(annot);
1247 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001248
Lei Zhanga21d5932018-02-05 18:28:38 +00001249 // Check that the new annotation's rectangle is as defined.
1250 FS_RECTF new_rect;
1251 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1252 EXPECT_EQ(rect.left, new_rect.left);
1253 EXPECT_EQ(rect.bottom, new_rect.bottom);
1254 EXPECT_EQ(rect.right, new_rect.right);
1255 EXPECT_EQ(rect.top, new_rect.top);
1256 }
1257
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001258 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001259 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001260}
Jane Liub137e752017-07-05 15:04:33 -04001261
Lei Zhangab41f252018-12-23 03:10:50 +00001262TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001263 // Open a file with an annotation and load its first page.
1264 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001265 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001266 ASSERT_TRUE(page);
1267
1268 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001269 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001270 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001271 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1272 }
Jane Liub137e752017-07-05 15:04:33 -04001273
Lei Zhanga21d5932018-02-05 18:28:38 +00001274 {
1275 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001276 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001277 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001278
Lei Zhanga21d5932018-02-05 18:28:38 +00001279 // Check that the original flag values are as expected.
1280 int flags = FPDFAnnot_GetFlags(annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00001281 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_INVISIBLE);
Lei Zhanga21d5932018-02-05 18:28:38 +00001282 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1283 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001284 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOZOOM);
1285 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOROTATE);
1286 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOVIEW);
1287 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_READONLY);
1288 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_LOCKED);
1289 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_TOGGLENOVIEW);
Jane Liub137e752017-07-05 15:04:33 -04001290
Lei Zhanga21d5932018-02-05 18:28:38 +00001291 // Set the HIDDEN flag.
1292 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1293 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1294 flags = FPDFAnnot_GetFlags(annot.get());
1295 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1296 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001297
Lei Zhanga21d5932018-02-05 18:28:38 +00001298 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001299 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001300 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001301 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001302 }
Jane Liub137e752017-07-05 15:04:33 -04001303
Lei Zhanga21d5932018-02-05 18:28:38 +00001304 // Unset the HIDDEN flag.
1305 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1306 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1307 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1308 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1309 flags = FPDFAnnot_GetFlags(annot.get());
1310 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1311 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001312
Lei Zhanga21d5932018-02-05 18:28:38 +00001313 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001314 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001315 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001316 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1317 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001318 }
Jane Liub137e752017-07-05 15:04:33 -04001319
Jane Liub137e752017-07-05 15:04:33 -04001320 UnloadPage(page);
1321}
Jane Liu36567742017-07-06 11:13:35 -04001322
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001323// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1324#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001325#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1326#else
1327#define MAYBE_AddAndModifyImage AddAndModifyImage
1328#endif
1329TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001330#if defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001331 static const char kMd5NewImage[] = "beb7db3647706d7fe4689f92073847aa";
1332 static const char kMd5ModifiedImage[] = "baa9b065469268e215ef958fe6987d6b";
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001333#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001334#if defined(OS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001335 static const char kMd5NewImage[] = "c6fcbceb2f079bef10458ac60db3a10c";
1336 static const char kMd5ModifiedImage[] = "8068eb568e5c1c5fbe84e98f7a980ac3";
Jane Liu36567742017-07-06 11:13:35 -04001337#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001338 static const char kMd5NewImage[] = "62c2706511cb50e32e7caeb82b1d3d49";
1339 static const char kMd5ModifiedImage[] = "83093ce9fac746db69fbd2fb394434ac";
Jane Liu36567742017-07-06 11:13:35 -04001340#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001341#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001342
1343 // Open a file with two annotations and load its first page.
1344 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001345 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001346 ASSERT_TRUE(page);
1347 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1348
1349 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001350 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001351 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001352 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001353 }
Jane Liu36567742017-07-06 11:13:35 -04001354
Jane Liu36567742017-07-06 11:13:35 -04001355 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001356 FPDF_BITMAP image_bitmap;
1357
1358 {
1359 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001360 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001361 ASSERT_TRUE(annot);
1362 FS_RECTF rect;
1363 rect.left = 200.f;
1364 rect.bottom = 600.f;
1365 rect.right = 400.f;
1366 rect.top = 800.f;
1367 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1368
1369 // Add a solid-color translucent image object to the new annotation.
1370 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1371 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1372 0xeeeecccc);
1373 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1374 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1375 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1376 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
Lei Zhangc8601bf2021-06-29 23:19:27 +00001377 static constexpr FS_MATRIX kBitmapScaleMatrix{kBitmapSize, 0, 0,
1378 kBitmapSize, 0, 0};
1379 ASSERT_TRUE(FPDFPageObj_SetMatrix(image_object, &kBitmapScaleMatrix));
Lei Zhanga21d5932018-02-05 18:28:38 +00001380 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1381 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1382 }
Jane Liu36567742017-07-06 11:13:35 -04001383
1384 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001385 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001386 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001387 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001388 }
Jane Liu36567742017-07-06 11:13:35 -04001389
Lei Zhanga21d5932018-02-05 18:28:38 +00001390 {
1391 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001392 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001393 ASSERT_TRUE(annot);
1394 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1395 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1396 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001397
Lei Zhanga21d5932018-02-05 18:28:38 +00001398 // Modify the image in the new annotation.
1399 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1400 0xff000000);
1401 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1402 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1403 }
Jane Liu36567742017-07-06 11:13:35 -04001404
Lei Zhangec618142021-04-13 17:36:46 +00001405 // Save the document and close the page.
Jane Liu36567742017-07-06 11:13:35 -04001406 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001407 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001408 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001409
1410 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001411 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001412}
1413
Hui Yingst6cd754f2020-05-14 04:05:25 +00001414TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001415#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001416 static const char kMd5NewText[] = "310d9de5f17fb288fb243f5dbaf2b6dc";
1417 static const char kMd5ModifiedText[] = "22be42c136c3bf5a8ecea3dd83770a02";
Lei Zhang0c03d632020-07-30 17:05:36 +00001418#elif defined(OS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001419 static const char kMd5NewText[] = "57a0fb3fba33e17de26bcde4c40b9a75";
1420 static const char kMd5ModifiedText[] = "072574999f2e3f36774ee0b5bc94d4dd";
Jane Liu36567742017-07-06 11:13:35 -04001421#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001422 static const char kMd5NewText[] = "1c4198c38f890c208c5cbaad57be4dc6";
1423 static const char kMd5ModifiedText[] = "cfa78d01406865f41f486bd34a8b9f7b";
Jane Liu36567742017-07-06 11:13:35 -04001424#endif
1425
1426 // Open a file with two annotations and load its first page.
1427 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001428 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001429 ASSERT_TRUE(page);
1430 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1431
1432 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001433 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001434 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001435 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001436 }
Jane Liu36567742017-07-06 11:13:35 -04001437
Lei Zhanga21d5932018-02-05 18:28:38 +00001438 {
1439 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001440 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001441 ASSERT_TRUE(annot);
1442 FS_RECTF rect;
1443 rect.left = 200.f;
1444 rect.bottom = 550.f;
1445 rect.right = 450.f;
1446 rect.top = 650.f;
1447 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001448
Lei Zhanga21d5932018-02-05 18:28:38 +00001449 // Add a translucent text object to the new annotation.
1450 FPDF_PAGEOBJECT text_object =
1451 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1452 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001453 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001454 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1455 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001456 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001457 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1458 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1459 }
Jane Liu36567742017-07-06 11:13:35 -04001460
1461 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001462 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001463 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001464 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001465 }
Jane Liu36567742017-07-06 11:13:35 -04001466
Lei Zhanga21d5932018-02-05 18:28:38 +00001467 {
1468 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001469 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001470 ASSERT_TRUE(annot);
1471 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1472 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1473 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001474
Lei Zhanga21d5932018-02-05 18:28:38 +00001475 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001476 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001477 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1478 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1479 }
Jane Liu36567742017-07-06 11:13:35 -04001480
1481 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001482 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001483 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001484 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001485 }
Jane Liu36567742017-07-06 11:13:35 -04001486
1487 // Remove the new annotation, and check that the page renders as before.
1488 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001489 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001490 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001491 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001492 }
Jane Liu36567742017-07-06 11:13:35 -04001493
1494 UnloadPage(page);
1495}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001496
Hui Yingst9b6b1542020-07-27 16:11:12 +00001497TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001498 // Open a file with four annotations and load its first page.
1499 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001500 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001501 ASSERT_TRUE(page);
1502
Lei Zhang4f556b82019-04-08 16:32:41 +00001503 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001504
Lei Zhanga21d5932018-02-05 18:28:38 +00001505 {
1506 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001507 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001508 ASSERT_TRUE(annot);
1509
1510 // Check that a non-existent key does not exist.
1511 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1512
1513 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001514 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001515 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001516 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1517 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1518 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001519
1520 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001521 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001522 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001523 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001524 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001525 ASSERT_EQ(66u, length_bytes);
1526 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1527 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1528 length_bytes));
1529 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1530 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001531
1532 // Check that the string value of the modified date is correct.
1533 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001534 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1535 nullptr, 0);
1536 ASSERT_EQ(44u, length_bytes);
1537 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001538 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001539 buf.data(), length_bytes));
1540 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001541
1542 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001543 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001544 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1545 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001546 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001547
Lei Zhangec618142021-04-13 17:36:46 +00001548 // Save the document and close the page.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001549 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001550 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001551
Hui Yingst9b6b1542020-07-27 16:11:12 +00001552#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00001553 static const char kMd5[] = "aa0e47d966c60d59102a2466542e0e46";
1554#elif defined(OS_APPLE)
1555 static const char kMd5[] = "cd90315b250dfe08265ce0ac335c5f76";
Hui Yingst9b6b1542020-07-27 16:11:12 +00001556#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00001557 static const char kMd5[] = "c4fb6911f2a87f490be196f8898de738";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001558#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001559
1560 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001561 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001562 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001563 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001564 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001565 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001566 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001567
Lei Zhanga21d5932018-02-05 18:28:38 +00001568 // Check that the string value of the modified date is the newly-set value.
1569 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001570 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001571 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001572 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001573 ASSERT_EQ(44u, length_bytes);
1574 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001575 EXPECT_EQ(44u,
1576 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001577 buf.data(), length_bytes));
1578 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001579 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001580
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001581 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001582 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001583}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001584
rycsmith3e785602019-03-05 21:48:36 +00001585TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001586 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001587 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1588 FPDF_PAGE page = LoadPage(0);
1589 ASSERT_TRUE(page);
1590 {
1591 // First two annotations do not have "MaxLen" attribute.
1592 for (int i = 0; i < 2; i++) {
1593 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1594 ASSERT_TRUE(annot);
1595
1596 // Verify that no "MaxLen" key present.
1597 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1598
1599 float value;
1600 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1601 }
1602
1603 // Annotation in index 2 has "MaxLen" of 10.
1604 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1605 ASSERT_TRUE(annot);
1606
1607 // Verify that "MaxLen" key present.
1608 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1609
1610 float value;
1611 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1612 EXPECT_FLOAT_EQ(10.0f, value);
1613
1614 // Check bad inputs.
1615 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1616 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1617 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1618 // Ask for key that exists but is not a number.
1619 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1620 }
1621
1622 UnloadPage(page);
1623}
1624
Lei Zhangab41f252018-12-23 03:10:50 +00001625TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001626 // Open a file with four annotations and load its first page.
1627 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001628 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001629 ASSERT_TRUE(page);
1630
Lei Zhanga21d5932018-02-05 18:28:38 +00001631 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001632 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1633 static constexpr size_t kExpectNormalAPLength = 73970;
1634
Lei Zhanga21d5932018-02-05 18:28:38 +00001635 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001636 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001637 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001638
Lei Zhanga21d5932018-02-05 18:28:38 +00001639 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001640 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001641 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001642 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001643
Lei Zhanga21d5932018-02-05 18:28:38 +00001644 // Check that the string value of an AP is not returned if the buffer is too
1645 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001646 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1647 // Write in the buffer to verify it's not overwritten.
1648 memcpy(buf.data(), "abcdefgh", 8);
1649 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001650 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001651 buf.data(), normal_length_bytes - 1));
1652 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001653
Lei Zhanga21d5932018-02-05 18:28:38 +00001654 // Check that the string value of an AP is returned through a buffer that is
1655 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001656 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001657 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001658 buf.data(), normal_length_bytes));
1659 EXPECT_EQ(kMd5NormalAP,
1660 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1661 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001662
Lei Zhanga21d5932018-02-05 18:28:38 +00001663 // Check that the string value of an AP is returned through a buffer that is
1664 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001665 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1666 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001667 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001668 buf.data(), normal_length_bytes + 2));
1669 EXPECT_EQ(kMd5NormalAP,
1670 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1671 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001672
Lei Zhanga21d5932018-02-05 18:28:38 +00001673 // Check that getting an AP for a mode that does not have an AP returns an
1674 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001675 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001676 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001677 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001678
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001679 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001680 EXPECT_EQ(2u,
1681 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001682 buf.data(), 1000));
1683 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001684
Lei Zhanga21d5932018-02-05 18:28:38 +00001685 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001686 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001687 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001688 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001689 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001690 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001691 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001692
Lei Zhanga21d5932018-02-05 18:28:38 +00001693 // Set the AP correctly now.
1694 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001695 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001696
Lei Zhanga21d5932018-02-05 18:28:38 +00001697 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001698 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001699 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001700 ASSERT_EQ(24u, rollover_length_bytes);
1701 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001702 EXPECT_EQ(24u,
1703 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001704 buf.data(), rollover_length_bytes));
1705 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001706
Lei Zhanga21d5932018-02-05 18:28:38 +00001707 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001708 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1709 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001710 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001711 buf.data(), normal_length_bytes));
1712 EXPECT_EQ(kMd5NormalAP,
1713 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1714 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001715 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001716
1717 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001718 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001719 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001720
Lei Zhang0b494052019-01-31 21:41:15 +00001721 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001722 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001723 ASSERT_TRUE(page);
Lei Zhanga21d5932018-02-05 18:28:38 +00001724 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001725 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001726
Lei Zhanga21d5932018-02-05 18:28:38 +00001727 // Check that the new annotation value is equal to the value we set before
1728 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001729 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001730 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001731 ASSERT_EQ(24u, rollover_length_bytes);
1732 std::vector<FPDF_WCHAR> buf =
1733 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001734 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1735 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001736 buf.data(), rollover_length_bytes));
1737 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001738 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001739
1740 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001741 CloseSavedPage(page);
1742 CloseSavedDocument();
1743}
1744
Lei Zhangab41f252018-12-23 03:10:50 +00001745TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001746 // Open a file with four annotations and load its first page.
1747 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001748 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001749 ASSERT_TRUE(page);
1750
Lei Zhanga21d5932018-02-05 18:28:38 +00001751 {
1752 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001753 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001754 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001755
Lei Zhanga21d5932018-02-05 18:28:38 +00001756 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001757 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001758 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001759 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001760 EXPECT_EQ(73970u,
1761 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1762 nullptr, 0));
1763 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1764 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001765
Lei Zhanga21d5932018-02-05 18:28:38 +00001766 // Check that setting the Down AP to null removes the Down entry but keeps
1767 // Normal intact.
1768 EXPECT_TRUE(
1769 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1770 EXPECT_EQ(73970u,
1771 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1772 nullptr, 0));
1773 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1774 nullptr, 0));
1775 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001776
Lei Zhang75c81712018-02-08 17:22:39 +00001777 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001778}
1779
Lei Zhangab41f252018-12-23 03:10:50 +00001780TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001781 // Open a file with four annotations and load its first page.
1782 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001783 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001784 ASSERT_TRUE(page);
1785
Lei Zhanga21d5932018-02-05 18:28:38 +00001786 {
1787 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001788 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001789 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001790
Lei Zhanga21d5932018-02-05 18:28:38 +00001791 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001792 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001793 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001794 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001795 EXPECT_EQ(73970u,
1796 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1797 nullptr, 0));
1798 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1799 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001800
Lei Zhanga21d5932018-02-05 18:28:38 +00001801 // Check that setting the Normal AP to null removes the whole AP dictionary.
1802 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1803 nullptr));
1804 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1805 nullptr, 0));
1806 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1807 nullptr, 0));
1808 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001809
Lei Zhang75c81712018-02-08 17:22:39 +00001810 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001811}
1812
Lei Zhangab41f252018-12-23 03:10:50 +00001813TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001814 // Open a file with annotations and load its first page.
1815 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001816 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001817 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001818 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001819
Lei Zhanga21d5932018-02-05 18:28:38 +00001820 {
1821 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001822 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001823 ASSERT_TRUE(annot);
1824 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1825 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001826 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001827 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1828 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1829 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001830
Lei Zhanga21d5932018-02-05 18:28:38 +00001831 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001832 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001833 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1834 ASSERT_TRUE(popup);
1835 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1836 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1837 FS_RECTF rect;
1838 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1839 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1840 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001841
Lei Zhanga21d5932018-02-05 18:28:38 +00001842 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1843 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001844 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001845 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1846 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001847
Lei Zhanga21d5932018-02-05 18:28:38 +00001848 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1849 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001850 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001851 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001852 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1853 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001854 }
Jane Liu300bb272017-08-21 14:37:53 -04001855
Jane Liu300bb272017-08-21 14:37:53 -04001856 UnloadPage(page);
1857}
1858
Lei Zhangab41f252018-12-23 03:10:50 +00001859TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001860 // Open file with form text fields.
1861 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001862 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001863 ASSERT_TRUE(page);
1864
Lei Zhanga21d5932018-02-05 18:28:38 +00001865 {
1866 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001867 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001868 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001869
Lei Zhanga21d5932018-02-05 18:28:38 +00001870 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001871 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001872 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001873 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1874 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1875 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001876 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001877 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001878
Lei Zhanga21d5932018-02-05 18:28:38 +00001879 {
1880 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001881 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001882 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001883
Lei Zhanga21d5932018-02-05 18:28:38 +00001884 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001885 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001886 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001887 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1888 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1889 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001890 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1891 }
1892
1893 {
1894 // Retrieve the fourth annotation: user-editable password text field.
1895 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1896 ASSERT_TRUE(annot);
1897
1898 // Check that the flag values are as expected.
1899 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1900 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001901 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1902 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1903 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001904 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001905 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001906
1907 UnloadPage(page);
1908}
1909
Lei Zhangab41f252018-12-23 03:10:50 +00001910TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001911 // Open file with form text fields.
1912 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001913 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001914 ASSERT_TRUE(page);
1915
Lei Zhanga21d5932018-02-05 18:28:38 +00001916 {
1917 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001918 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001919 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001920
Lei Zhanga21d5932018-02-05 18:28:38 +00001921 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001922 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001923 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001924 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1925 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001926 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1927 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001928 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001929 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001930
Lei Zhanga21d5932018-02-05 18:28:38 +00001931 {
1932 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001933 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001934 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001935
Lei Zhanga21d5932018-02-05 18:28:38 +00001936 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001937 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001938 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001939 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1940 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001941 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1942 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001943 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001944 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001945
Lei Zhanga21d5932018-02-05 18:28:38 +00001946 {
1947 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001948 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001949 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001950
Lei Zhanga21d5932018-02-05 18:28:38 +00001951 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001952 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001953 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001954 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1955 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001956 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1957 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001958 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001959 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001960
1961 UnloadPage(page);
1962}
Diana Gage40870db2017-07-19 18:16:03 -07001963
Lei Zhangab41f252018-12-23 03:10:50 +00001964TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001965 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001966 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001967 FPDF_PAGE page = LoadPage(0);
1968 ASSERT_TRUE(page);
1969
1970 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001971 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1972 EXPECT_FALSE(
1973 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001974
Lei Zhang8da98232019-12-11 23:29:33 +00001975 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001976 {
1977 // Verify there is an annotation.
1978 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001979 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001980 EXPECT_TRUE(annot);
1981 }
1982
1983 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001984 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1985 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1986 EXPECT_FALSE(
1987 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001988
1989 UnloadPage(page);
1990}
1991
Lei Zhangab41f252018-12-23 03:10:50 +00001992TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001993 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001994 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001995 FPDF_PAGE page = LoadPage(0);
1996 ASSERT_TRUE(page);
1997
Lei Zhanga21d5932018-02-05 18:28:38 +00001998 {
1999 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002000 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002001 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002002 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002003 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002004
Lei Zhanga21d5932018-02-05 18:28:38 +00002005 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002006 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00002007 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2008 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002009 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2010 }
Diana Gage40870db2017-07-19 18:16:03 -07002011
Lei Zhanga21d5932018-02-05 18:28:38 +00002012 {
2013 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002014 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002015 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002016 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002017 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002018
Lei Zhanga21d5932018-02-05 18:28:38 +00002019 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002020 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002021 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002022 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2023 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002024 }
Diana Gage40870db2017-07-19 18:16:03 -07002025
2026 UnloadPage(page);
2027}
2028
Lei Zhangab41f252018-12-23 03:10:50 +00002029TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07002030 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002031 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002032 FPDF_PAGE page = LoadPage(0);
2033 ASSERT_TRUE(page);
2034
Lei Zhanga21d5932018-02-05 18:28:38 +00002035 {
2036 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002037 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002038 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002039 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002040 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002041
Lei Zhanga21d5932018-02-05 18:28:38 +00002042 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002043 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002044 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002045 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2046 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002047 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2048 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002049 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002050 }
Diana Gage40870db2017-07-19 18:16:03 -07002051
Lei Zhanga21d5932018-02-05 18:28:38 +00002052 {
2053 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002054 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002055 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002056 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002057 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002058
Lei Zhanga21d5932018-02-05 18:28:38 +00002059 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002060 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002061 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002062 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2063 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002064 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2065 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002066 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002067 }
Diana Gage40870db2017-07-19 18:16:03 -07002068
Lei Zhanga21d5932018-02-05 18:28:38 +00002069 {
2070 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002071 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002072 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002073 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002074 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002075
Lei Zhanga21d5932018-02-05 18:28:38 +00002076 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002077 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002078 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002079 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2080 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002081 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2082 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002083 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002084 }
Diana Gage40870db2017-07-19 18:16:03 -07002085
2086 UnloadPage(page);
2087}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002088
Hui Yingst603dcd82020-11-09 22:36:46 +00002089TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
Lei Zhang03e5e682019-09-16 19:45:55 +00002090#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst603dcd82020-11-09 22:36:46 +00002091 static const char kExpectedBitmap[] = "a1ea1ceebb26922fae576cb79ce63af0";
Lei Zhang03e5e682019-09-16 19:45:55 +00002092#else
Lei Zhang992e7e22019-02-04 19:20:58 +00002093 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
Hui Yingst603dcd82020-11-09 22:36:46 +00002094#endif
Tom Andersond4fe5f72021-12-03 20:52:52 +00002095 static constexpr size_t kExpectedSize = 1590;
Lei Zhang992e7e22019-02-04 19:20:58 +00002096
2097 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2098
2099 FPDF_PAGE page = LoadPage(0);
2100 ASSERT_TRUE(page);
2101
2102 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2103 EXPECT_EQ(kExpectedSize, GetString().size());
2104 ClearString();
2105
2106 for (size_t i = 0; i < 10; ++i) {
2107 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2108 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2109
2110 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2111 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2112 // equal, not bigger.
2113 EXPECT_LT(kExpectedSize, GetString().size());
2114 ClearString();
2115 }
2116
2117 UnloadPage(page);
2118}
2119
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002120TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2121 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2122 FPDF_PAGE page = LoadPage(0);
2123 ASSERT_TRUE(page);
2124 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2125
2126 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002127 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002128 static const size_t kBufSize = 12;
2129 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002130
2131 {
2132 // Add a text annotation to the page.
2133 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2134 ASSERT_TRUE(annot);
2135 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2136 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2137
2138 // Make sure there is no test key, add set a value there, and read it back.
2139 std::fill(buf.begin(), buf.end(), 'x');
2140 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002141 kBufSize));
2142 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002143
Lei Zhangf0f67682019-04-08 17:03:21 +00002144 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002145 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2146
2147 std::fill(buf.begin(), buf.end(), 'x');
2148 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002149 kBufSize));
2150 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002151 }
2152
Lei Zhang05ec64c2019-01-09 03:00:06 +00002153 {
2154 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2155 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002156 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002157 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002158 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2159 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2160 // Also do the same test for its appearance string.
2161 std::fill(buf.begin(), buf.end(), 'x');
2162 ASSERT_EQ(2u,
2163 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002164 buf.data(), kBufSize));
2165 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002166
Lei Zhangf0f67682019-04-08 17:03:21 +00002167 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002168 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2169 text.get()));
2170
2171 std::fill(buf.begin(), buf.end(), 'x');
2172 ASSERT_EQ(6u,
2173 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002174 buf.data(), kBufSize));
2175 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002176 }
2177
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002178 UnloadPage(page);
2179
2180 {
2181 // Save a copy, open the copy, and check the annotation again.
2182 // Note that it renders the rotation.
2183 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002184 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002185 FPDF_PAGE saved_page = LoadSavedPage(0);
2186 ASSERT_TRUE(saved_page);
2187
Lei Zhang05ec64c2019-01-09 03:00:06 +00002188 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002189 {
2190 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2191 ASSERT_TRUE(annot);
2192 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2193
2194 std::fill(buf.begin(), buf.end(), 'x');
2195 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002196 kBufSize));
2197 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002198 }
2199
Lei Zhang05ec64c2019-01-09 03:00:06 +00002200 {
2201 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2202 ASSERT_TRUE(annot);
2203 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2204 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2205
2206 std::fill(buf.begin(), buf.end(), 'x');
2207 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002208 kBufSize));
2209 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002210 }
2211
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002212 CloseSavedPage(saved_page);
2213 CloseSavedDocument();
2214 }
2215}
rycsmithcb752f32019-02-21 18:40:53 +00002216
2217TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2218 // Open a file with combobox widget annotations and load its first page.
2219 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2220 FPDF_PAGE page = LoadPage(0);
2221 ASSERT_TRUE(page);
2222
2223 {
2224 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2225 ASSERT_TRUE(annot);
2226
2227 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2228
2229 annot.reset(FPDFPage_GetAnnot(page, 1));
2230 ASSERT_TRUE(annot);
2231
2232 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002233
2234 // Check bad form handle / annot.
2235 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2236 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2237 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002238 }
2239
2240 UnloadPage(page);
2241}
2242
2243TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2244 // Open a file with listbox widget annotations and load its first page.
2245 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2246 FPDF_PAGE page = LoadPage(0);
2247 ASSERT_TRUE(page);
2248
2249 {
2250 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2251 ASSERT_TRUE(annot);
2252
2253 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2254
2255 annot.reset(FPDFPage_GetAnnot(page, 1));
2256 ASSERT_TRUE(annot);
2257
2258 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2259 }
2260
2261 UnloadPage(page);
2262}
2263
2264TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2265 // Open a file with ink annotations and load its first page.
2266 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2267 FPDF_PAGE page = LoadPage(0);
2268 ASSERT_TRUE(page);
2269
2270 {
2271 // annotations do not have "Opt" array and will return -1
2272 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2273 ASSERT_TRUE(annot);
2274
2275 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2276
2277 annot.reset(FPDFPage_GetAnnot(page, 1));
2278 ASSERT_TRUE(annot);
2279
2280 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2281 }
2282
2283 UnloadPage(page);
2284}
2285
2286TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2287 // Open a file with combobox widget annotations and load its first page.
2288 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2289 FPDF_PAGE page = LoadPage(0);
2290 ASSERT_TRUE(page);
2291
2292 {
2293 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2294 ASSERT_TRUE(annot);
2295
2296 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002297 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002298 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002299 ASSERT_EQ(8u, length_bytes);
2300 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002301 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002302 buf.data(), length_bytes));
2303 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002304
2305 annot.reset(FPDFPage_GetAnnot(page, 1));
2306 ASSERT_TRUE(annot);
2307
2308 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002309 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002310 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002311 ASSERT_EQ(12u, length_bytes);
2312 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002313 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002314 buf.data(), length_bytes));
2315 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002316
2317 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002318 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002319 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002320 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002321 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002322 buf.data(), length_bytes));
2323 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002324
Lei Zhange7033c82019-02-26 19:30:49 +00002325 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002326 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2327 nullptr, 0));
2328 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2329 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002330
2331 // Check bad form handle / annot.
2332 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2333 EXPECT_EQ(0u,
2334 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2335 EXPECT_EQ(0u,
2336 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002337 }
2338
2339 UnloadPage(page);
2340}
2341
2342TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2343 // Open a file with listbox widget annotations and load its first page.
2344 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2345 FPDF_PAGE page = LoadPage(0);
2346 ASSERT_TRUE(page);
2347
2348 {
2349 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2350 ASSERT_TRUE(annot);
2351
2352 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002353 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002354 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002355 ASSERT_EQ(8u, length_bytes);
2356 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002357 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002358 buf.data(), length_bytes));
2359 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002360
2361 annot.reset(FPDFPage_GetAnnot(page, 1));
2362 ASSERT_TRUE(annot);
2363
2364 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002365 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002366 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002367 ASSERT_EQ(12u, length_bytes);
2368 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002369 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002370 buf.data(), length_bytes));
2371 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002372
2373 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002374 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002375 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002376 ASSERT_EQ(18u, length_bytes);
2377 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002378 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002379 buf.data(), length_bytes));
2380 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002381
2382 // indices out of range
2383 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2384 nullptr, 0));
2385 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2386 nullptr, 0));
2387 }
2388
2389 UnloadPage(page);
2390}
2391
2392TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2393 // Open a file with ink annotations and load its first page.
2394 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2395 FPDF_PAGE page = LoadPage(0);
2396 ASSERT_TRUE(page);
2397
2398 {
2399 // annotations do not have "Opt" array and will return 0
2400 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2401 ASSERT_TRUE(annot);
2402
2403 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2404 nullptr, 0));
2405
2406 annot.reset(FPDFPage_GetAnnot(page, 1));
2407 ASSERT_TRUE(annot);
2408
2409 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2410 nullptr, 0));
2411 }
2412
2413 UnloadPage(page);
2414}
Ryan Smith09c23b12019-04-25 18:09:06 +00002415
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002416TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2417 // Open a file with combobox widget annotations and load its first page.
2418 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2419 FPDF_PAGE page = LoadPage(0);
2420 ASSERT_TRUE(page);
2421
2422 {
2423 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2424 ASSERT_TRUE(annot);
2425
2426 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2427 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2428 ASSERT_EQ(3, count);
2429 for (int i = 0; i < count; i++) {
2430 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2431 }
2432
2433 annot.reset(FPDFPage_GetAnnot(page, 1));
2434 ASSERT_TRUE(annot);
2435
2436 // Checks for Combobox with Values (/V) object which is just a string.
2437 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2438 ASSERT_EQ(26, count);
2439 for (int i = 0; i < count; i++) {
2440 EXPECT_EQ(i == 1,
2441 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2442 }
2443
2444 // Checks for index outside bound i.e. (index >= CountOption()).
2445 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2446 /*index=*/26));
2447 // Checks for negetive index.
2448 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2449 /*index=*/-1));
2450
2451 // Checks for bad form handle/annot.
2452 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2453 EXPECT_FALSE(
2454 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2455 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2456 }
2457
2458 UnloadPage(page);
2459}
2460
2461TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2462 // Open a file with listbox widget annotations and load its first page.
2463 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2464 FPDF_PAGE page = LoadPage(0);
2465 ASSERT_TRUE(page);
2466
2467 {
2468 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2469 ASSERT_TRUE(annot);
2470
2471 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2472 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2473 ASSERT_EQ(3, count);
2474 for (int i = 0; i < count; i++) {
2475 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2476 }
2477
2478 annot.reset(FPDFPage_GetAnnot(page, 1));
2479 ASSERT_TRUE(annot);
2480
2481 // Checks for Listbox with Values (/V) object which is just a string.
2482 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2483 ASSERT_EQ(26, count);
2484 for (int i = 0; i < count; i++) {
2485 EXPECT_EQ(i == 1,
2486 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2487 }
2488
2489 annot.reset(FPDFPage_GetAnnot(page, 3));
2490 ASSERT_TRUE(annot);
2491
2492 // Checks for Listbox with only Selected indices (/I) object which is an
2493 // array with multiple objects.
2494 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2495 ASSERT_EQ(5, count);
2496 for (int i = 0; i < count; i++) {
2497 bool expected = (i == 1 || i == 3);
2498 EXPECT_EQ(expected,
2499 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2500 }
2501
2502 annot.reset(FPDFPage_GetAnnot(page, 4));
2503 ASSERT_TRUE(annot);
2504
2505 // Checks for Listbox with Values (/V) object which is an array with
2506 // multiple objects.
2507 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2508 ASSERT_EQ(5, count);
2509 for (int i = 0; i < count; i++) {
2510 bool expected = (i == 2 || i == 4);
2511 EXPECT_EQ(expected,
2512 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2513 }
2514
2515 annot.reset(FPDFPage_GetAnnot(page, 5));
2516 ASSERT_TRUE(annot);
2517
2518 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2519 // objects conflict with different lengths.
2520 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2521 ASSERT_EQ(5, count);
2522 for (int i = 0; i < count; i++) {
2523 bool expected = (i == 0 || i == 2);
2524 EXPECT_EQ(expected,
2525 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2526 }
2527 }
2528
2529 UnloadPage(page);
2530}
2531
2532TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2533 // Open a file with multiple form field annotations and load its first page.
2534 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2535 FPDF_PAGE page = LoadPage(0);
2536 ASSERT_TRUE(page);
2537
2538 {
2539 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2540 ASSERT_TRUE(annot);
2541
2542 // Checks for link annotation.
2543 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2544 /*index=*/0));
2545
2546 annot.reset(FPDFPage_GetAnnot(page, 3));
2547 ASSERT_TRUE(annot);
2548
2549 // Checks for text field annotation.
2550 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2551 /*index=*/0));
2552 }
2553
2554 UnloadPage(page);
2555}
2556
Ryan Smith09c23b12019-04-25 18:09:06 +00002557TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2558 // Open a file with combobox annotations and load its first page.
2559 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2560 FPDF_PAGE page = LoadPage(0);
2561 ASSERT_TRUE(page);
2562
2563 {
2564 // All 3 widgets have Tf font size 12.
2565 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2566 ASSERT_TRUE(annot);
2567
2568 float value;
2569 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2570 EXPECT_EQ(12.0, value);
2571
2572 annot.reset(FPDFPage_GetAnnot(page, 1));
2573 ASSERT_TRUE(annot);
2574
2575 float value_two;
2576 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2577 EXPECT_EQ(12.0, value_two);
2578
2579 annot.reset(FPDFPage_GetAnnot(page, 2));
2580 ASSERT_TRUE(annot);
2581
2582 float value_three;
2583 ASSERT_TRUE(
2584 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2585 EXPECT_EQ(12.0, value_three);
2586 }
2587
2588 UnloadPage(page);
2589}
2590
2591TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2592 // Open a file with textfield annotations and load its first page.
2593 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2594 FPDF_PAGE page = LoadPage(0);
2595 ASSERT_TRUE(page);
2596
2597 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002598 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002599 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2600 ASSERT_TRUE(annot);
2601
2602 float value;
2603 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2604 EXPECT_EQ(12.0, value);
2605
2606 annot.reset(FPDFPage_GetAnnot(page, 1));
2607 ASSERT_TRUE(annot);
2608
2609 float value_two;
2610 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2611 EXPECT_EQ(12.0, value_two);
2612
2613 annot.reset(FPDFPage_GetAnnot(page, 2));
2614 ASSERT_TRUE(annot);
2615
2616 float value_three;
2617 ASSERT_TRUE(
2618 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2619 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002620
2621 float value_four;
2622 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2623 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002624 }
2625
2626 UnloadPage(page);
2627}
2628
2629TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2630 // Open a file with ink annotations and load its first page.
2631 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2632 FPDF_PAGE page = LoadPage(0);
2633 ASSERT_TRUE(page);
2634
2635 {
2636 // Annotations that do not have variable text and will return -1.
2637 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2638 ASSERT_TRUE(annot);
2639
2640 float value;
2641 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2642
2643 annot.reset(FPDFPage_GetAnnot(page, 1));
2644 ASSERT_TRUE(annot);
2645
2646 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2647 }
2648
2649 UnloadPage(page);
2650}
2651
2652TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2653 // Open a file with combobox annotations and load its first page.
2654 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2655 FPDF_PAGE page = LoadPage(0);
2656 ASSERT_TRUE(page);
2657
2658 {
2659 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2660 ASSERT_TRUE(annot);
2661
2662 // Check bad form handle / annot.
2663 float value;
2664 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2665 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2666 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2667 }
2668
2669 UnloadPage(page);
2670}
2671
2672TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2673 // Open a file with textfield annotations and load its first page.
2674 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2675 FPDF_PAGE page = LoadPage(0);
2676 ASSERT_TRUE(page);
2677
2678 {
2679 // Obtain the first annotation, a text field with negative font size, -12.
2680 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2681 ASSERT_TRUE(annot);
2682
2683 float value;
2684 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2685 EXPECT_EQ(-12.0, value);
2686 }
2687
2688 UnloadPage(page);
2689}
Ryan Smith23fdf892019-07-17 21:51:26 +00002690
2691TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2692 // Open a file with checkbox and radiobuttons widget annotations and load its
2693 // first page.
2694 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2695 FPDF_PAGE page = LoadPage(0);
2696 ASSERT_TRUE(page);
2697
2698 {
2699 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2700 ASSERT_TRUE(annot);
2701 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2702 }
2703
2704 UnloadPage(page);
2705}
2706
2707TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2708 // Open a file with checkbox and radiobutton widget annotations and load its
2709 // first page.
2710 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2711 FPDF_PAGE page = LoadPage(0);
2712 ASSERT_TRUE(page);
2713
2714 {
2715 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2716 ASSERT_TRUE(annot);
2717 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2718 }
2719
2720 UnloadPage(page);
2721}
2722
2723TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2724 // Open a file with checkbox and radiobutton widget annotations and load its
2725 // first page.
2726 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2727 FPDF_PAGE page = LoadPage(0);
2728 ASSERT_TRUE(page);
2729
2730 {
2731 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2732 ASSERT_TRUE(annot);
2733 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2734
2735 annot.reset(FPDFPage_GetAnnot(page, 6));
2736 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2737
2738 annot.reset(FPDFPage_GetAnnot(page, 7));
2739 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2740 }
2741
2742 UnloadPage(page);
2743}
2744
2745TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2746 // Open a file with checkbox and radiobutton widget annotations and load its
2747 // first page.
2748 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2749 FPDF_PAGE page = LoadPage(0);
2750 ASSERT_TRUE(page);
2751
2752 {
2753 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2754 ASSERT_TRUE(annot);
2755 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2756
2757 annot.reset(FPDFPage_GetAnnot(page, 3));
2758 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2759
2760 annot.reset(FPDFPage_GetAnnot(page, 4));
2761 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2762 }
2763
2764 UnloadPage(page);
2765}
2766
2767TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2768 // Open a file with checkbox and radiobuttons widget annotations and load its
2769 // first page.
2770 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2771 FPDF_PAGE page = LoadPage(0);
2772 ASSERT_TRUE(page);
2773
2774 {
2775 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2776 ASSERT_TRUE(annot);
2777 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2778 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2779 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2780 }
2781
2782 UnloadPage(page);
2783}
2784
2785TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2786 // Open a file with text widget annotations and load its first page.
2787 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2788 FPDF_PAGE page = LoadPage(0);
2789 ASSERT_TRUE(page);
2790
2791 {
2792 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2793 ASSERT_TRUE(annot);
2794 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2795 }
2796
2797 UnloadPage(page);
2798}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002799
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002800TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2801 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002802 FPDF_PAGE page = LoadPage(0);
2803 ASSERT_TRUE(page);
2804
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002805 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2806
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002807 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002808 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002809 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002810 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002811 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002812
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002813 constexpr int kExpectedAnnotTypes[] = {-1,
2814 FPDF_FORMFIELD_COMBOBOX,
2815 FPDF_FORMFIELD_LISTBOX,
2816 FPDF_FORMFIELD_TEXTFIELD,
2817 FPDF_FORMFIELD_CHECKBOX,
2818 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002819
Lei Zhang4501a502020-05-18 16:52:59 +00002820 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002821 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002822 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002823 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002824 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2825 }
2826 UnloadPage(page);
2827}
2828
2829TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2830 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2831 FPDF_PAGE page = LoadPage(0);
2832 ASSERT_TRUE(page);
2833
2834 {
2835 EXPECT_EQ(0u,
2836 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2837
2838 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2839 ASSERT_TRUE(annot);
2840
2841 EXPECT_EQ(0u,
2842 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2843
2844 unsigned long length_bytes =
2845 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2846 ASSERT_EQ(2u, length_bytes);
2847 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2848 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2849 buf.data(), length_bytes));
2850 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2851 }
2852 {
2853 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2854 ASSERT_TRUE(annot);
2855
2856 unsigned long length_bytes =
2857 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2858 ASSERT_EQ(18u, length_bytes);
2859 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2860 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2861 buf.data(), length_bytes));
2862 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2863 }
2864 UnloadPage(page);
2865}
2866
2867TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2868 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2869 FPDF_PAGE page = LoadPage(0);
2870 ASSERT_TRUE(page);
2871
2872 {
2873 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2874 ASSERT_TRUE(annot);
2875
2876 unsigned long length_bytes =
2877 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2878 ASSERT_EQ(2u, length_bytes);
2879 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2880 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2881 buf.data(), length_bytes));
2882 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2883 }
2884 {
2885 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2886 ASSERT_TRUE(annot);
2887
2888 unsigned long length_bytes =
2889 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2890 ASSERT_EQ(14u, length_bytes);
2891 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2892 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2893 buf.data(), length_bytes));
2894 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2895 }
2896 UnloadPage(page);
2897}
2898
2899TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2900 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2901 FPDF_PAGE page = LoadPage(0);
2902 ASSERT_TRUE(page);
2903
2904 {
2905 EXPECT_EQ(0u,
2906 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2907
2908 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2909 ASSERT_TRUE(annot);
2910
2911 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2912
2913 unsigned long length_bytes =
2914 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2915 ASSERT_EQ(18u, length_bytes);
2916 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2917 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2918 buf.data(), length_bytes));
2919 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2920 }
2921 UnloadPage(page);
2922}
2923
2924TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2925 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2926 FPDF_PAGE page = LoadPage(0);
2927 ASSERT_TRUE(page);
2928
2929 {
2930 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2931 ASSERT_TRUE(annot);
2932
2933 unsigned long length_bytes =
2934 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2935 ASSERT_EQ(30u, length_bytes);
2936 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2937 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2938 buf.data(), length_bytes));
2939 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2940 }
2941 UnloadPage(page);
2942}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002943
Lei Zhang9b444002020-04-17 17:35:23 +00002944TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2945 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002946 FPDF_PAGE page = LoadPage(0);
2947 ASSERT_TRUE(page);
2948
Lei Zhang9b444002020-04-17 17:35:23 +00002949 // Verify widgets are by default focusable.
2950 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2951 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002952
Lei Zhang9b444002020-04-17 17:35:23 +00002953 // Expected annot subtypes for page 0 of annots.pdf.
2954 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2955 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2956 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2957 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2958 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002959
Lei Zhang9b444002020-04-17 17:35:23 +00002960 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002961 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002962 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2963 kExpectedAnnotSubtypes,
2964 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002965
Lei Zhang9b444002020-04-17 17:35:23 +00002966 // Make no annotation type focusable using the preferred method.
2967 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2968 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002969
Lei Zhang9b444002020-04-17 17:35:23 +00002970 // Restore the focusable type count back to 1, then set it back to 0 using a
2971 // different method.
2972 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2973 ASSERT_TRUE(
2974 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2975 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002976
Lei Zhang9b444002020-04-17 17:35:23 +00002977 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002978 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002979
Lei Zhang9b444002020-04-17 17:35:23 +00002980 // Now make links focusable.
2981 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2982 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002983
Lei Zhang9b444002020-04-17 17:35:23 +00002984 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002985 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002986 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2987 kExpectedAnnotSubtypes,
2988 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002989
Lei Zhang9b444002020-04-17 17:35:23 +00002990 // Test invalid parameters.
2991 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00002992 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002993 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00002994 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002995 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2996
Lei Zhang9b444002020-04-17 17:35:23 +00002997 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2998 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2999 subtypes.size()));
3000 EXPECT_FALSE(
3001 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
3002 EXPECT_FALSE(
3003 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003004
3005 UnloadPage(page);
3006}
Lei Zhang671aece2020-04-14 19:02:26 +00003007
Hui Yingstea3816d2020-07-28 22:35:11 +00003008TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00003009 ASSERT_TRUE(OpenDocument("annots.pdf"));
3010 FPDF_PAGE page = LoadPage(0);
3011 ASSERT_TRUE(page);
3012
3013 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003014#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003015 static const char kMd5sum[] = "b4c8f1dab175508810c476d078ebc5a6";
Lei Zhang0c03d632020-07-30 17:05:36 +00003016#elif defined(OS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003017 static const char kMd5sum[] = "108a46c517c4eaace9982ee83e8e3296";
Lei Zhang671aece2020-04-14 19:02:26 +00003018#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003019 static const char kMd5sum[] = "5550d8dcb4d1af1f50e8b4bcaef2ee60";
Lei Zhang671aece2020-04-14 19:02:26 +00003020#endif
3021 // Check the initial rendering.
3022 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3023 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3024 }
3025
3026 // Make links and highlights focusable.
3027 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
3028 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00003029 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00003030 ASSERT_TRUE(
3031 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
3032 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3033 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
3034 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
3035 subtypes.size()));
3036 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
3037 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
3038
3039 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003040#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003041 static const char kMd5sum[] = "9173db3a892bc1697eef5cdaed19eda6";
Lei Zhang0c03d632020-07-30 17:05:36 +00003042#elif defined(OS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003043 static const char kMd5sum[] = "eb3869335e7a219e1b5f25c1c6037b97";
Lei Zhang671aece2020-04-14 19:02:26 +00003044#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003045 static const char kMd5sum[] = "805fe7bb751ac4ed2b82bb66efe6db40";
Lei Zhang671aece2020-04-14 19:02:26 +00003046#endif
3047 // Focus the first link and check the rendering.
3048 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3049 ASSERT_TRUE(annot);
3050 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3051 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3052 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3053 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3054 }
3055
3056 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003057#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003058 static const char kMd5sum[] = "174dbdb218c2b14011c9c1db67fe41c3";
Lei Zhang0c03d632020-07-30 17:05:36 +00003059#elif defined(OS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003060 static const char kMd5sum[] = "d20b1978da2362d3942ea0fc6d230997";
Lei Zhang671aece2020-04-14 19:02:26 +00003061#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003062 static const char kMd5sum[] = "c5c5dcb462af3ef5f43b298ec048feef";
Lei Zhang671aece2020-04-14 19:02:26 +00003063#endif
3064 // Focus the first highlight and check the rendering.
3065 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3066 ASSERT_TRUE(annot);
3067 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3068 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3069 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3070 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3071 }
3072
3073 UnloadPage(page);
3074}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003075
3076TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
3077 ASSERT_TRUE(OpenDocument("annots.pdf"));
3078 FPDF_PAGE page = LoadPage(0);
3079 ASSERT_TRUE(page);
3080 {
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003081 constexpr char kExpectedResult[] =
3082 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3083 "fpdf_text.h";
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003084
Lei Zhang306874b2021-04-16 00:33:36 +00003085 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3086 ASSERT_TRUE(annot);
3087 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3088 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()),
3089 kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003090 }
3091
3092 {
3093 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3094 ASSERT_TRUE(annot);
3095 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3096 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3097 }
3098
3099 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3100
3101 UnloadPage(page);
3102}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003103
3104TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3105 // Open a file with radio button widget annotations and load its first page.
3106 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3107 FPDF_PAGE page = LoadPage(0);
3108 ASSERT_TRUE(page);
3109
3110 {
3111 // Checks for bad annot.
3112 EXPECT_EQ(-1,
3113 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3114
3115 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3116 ASSERT_TRUE(annot);
3117
3118 // Checks for bad form handle.
3119 EXPECT_EQ(-1,
3120 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3121
3122 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3123 }
3124
3125 UnloadPage(page);
3126}
3127
3128TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3129 // Open a file with checkbox widget annotations and load its first page.
3130 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3131 FPDF_PAGE page = LoadPage(0);
3132 ASSERT_TRUE(page);
3133
3134 {
3135 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3136 ASSERT_TRUE(annot);
3137 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3138 }
3139
3140 UnloadPage(page);
3141}
3142
3143TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3144 // Open a file with ink annotations and load its first page.
3145 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3146 FPDF_PAGE page = LoadPage(0);
3147 ASSERT_TRUE(page);
3148
3149 {
3150 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3151 ASSERT_TRUE(annot);
3152 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3153 }
3154
3155 UnloadPage(page);
3156}
3157
3158TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3159 // Open a file with radio button widget annotations and load its first page.
3160 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3161 FPDF_PAGE page = LoadPage(0);
3162 ASSERT_TRUE(page);
3163
3164 {
3165 // Checks for bad annot.
3166 EXPECT_EQ(-1,
3167 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3168
3169 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3170 ASSERT_TRUE(annot);
3171
3172 // Checks for bad form handle.
3173 EXPECT_EQ(-1,
3174 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3175
3176 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3177 }
3178
3179 UnloadPage(page);
3180}
3181
3182TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3183 // Open a file with checkbox widget annotations and load its first page.
3184 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3185 FPDF_PAGE page = LoadPage(0);
3186 ASSERT_TRUE(page);
3187
3188 {
3189 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3190 ASSERT_TRUE(annot);
3191 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3192 }
3193
3194 UnloadPage(page);
3195}
3196
3197TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3198 // Open a file with ink annotations and load its first page.
3199 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3200 FPDF_PAGE page = LoadPage(0);
3201 ASSERT_TRUE(page);
3202
3203 {
3204 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3205 ASSERT_TRUE(annot);
3206 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3207 }
3208
3209 UnloadPage(page);
3210}
3211
3212TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3213 // Open a file with radio button widget annotations and load its first page.
3214 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3215 FPDF_PAGE page = LoadPage(0);
3216 ASSERT_TRUE(page);
3217
3218 {
3219 // Checks for bad annot.
3220 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3221 form_handle(), /*annot=*/nullptr,
3222 /*buffer=*/nullptr, /*buflen=*/0));
3223
3224 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3225 ASSERT_TRUE(annot);
3226
3227 // Checks for bad form handle.
3228 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3229 /*hHandle=*/nullptr, annot.get(),
3230 /*buffer=*/nullptr, /*buflen=*/0));
3231
3232 unsigned long length_bytes =
3233 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3234 /*buffer=*/nullptr, /*buflen=*/0);
3235 ASSERT_EQ(14u, length_bytes);
3236 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3237 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3238 buf.data(), length_bytes));
3239 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3240 }
3241
3242 UnloadPage(page);
3243}
3244
3245TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3246 // Open a file with checkbox widget annotations and load its first page.
3247 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3248 FPDF_PAGE page = LoadPage(0);
3249 ASSERT_TRUE(page);
3250
3251 {
3252 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3253 ASSERT_TRUE(annot);
3254
3255 unsigned long length_bytes =
3256 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3257 /*buffer=*/nullptr, /*buflen=*/0);
3258 ASSERT_EQ(8u, length_bytes);
3259 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3260 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3261 buf.data(), length_bytes));
3262 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3263 }
3264
3265 UnloadPage(page);
3266}
3267
3268TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3269 // Open a file with ink annotations and load its first page.
3270 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3271 FPDF_PAGE page = LoadPage(0);
3272 ASSERT_TRUE(page);
3273
3274 {
3275 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3276 ASSERT_TRUE(annot);
3277 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3278 /*buffer=*/nullptr,
3279 /*buflen=*/0));
3280 }
3281
3282 UnloadPage(page);
3283}
Miklos Vajnad1a24fb2020-10-26 18:07:13 +00003284
3285TEST_F(FPDFAnnotEmbedderTest, Redactannotation) {
3286 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3287 FPDF_PAGE page = LoadPage(0);
3288 ASSERT_TRUE(page);
3289 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3290
3291 {
3292 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3293 ASSERT_TRUE(annot);
3294 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3295 }
3296
3297 UnloadPage(page);
3298}
Miklos Vajna09ecef62020-11-10 21:50:38 +00003299
3300TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) {
3301 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3302 FPDF_PAGE page = LoadPage(0);
3303 ASSERT_TRUE(page);
3304 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3305
3306 {
3307 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3308 ASSERT_TRUE(annot);
3309
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003310 // FPDFAnnot_GetVertices() positive testing.
Miklos Vajna09ecef62020-11-10 21:50:38 +00003311 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3312 const size_t kExpectedSize = 3;
3313 ASSERT_EQ(kExpectedSize, size);
3314 std::vector<FS_POINTF> vertices_buffer(size);
3315 EXPECT_EQ(size,
3316 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3317 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3318 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3319 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3320 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3321 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3322 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3323
3324 // FPDFAnnot_GetVertices() negative testing.
3325 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3326
3327 // vertices_buffer is not overwritten if it is too small.
3328 vertices_buffer.resize(1);
3329 vertices_buffer[0].x = 42;
3330 vertices_buffer[0].y = 43;
3331 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3332 vertices_buffer.size());
3333 EXPECT_EQ(kExpectedSize, size);
3334 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3335 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3336 }
3337
3338 {
3339 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3340 ASSERT_TRUE(annot);
3341
3342 // This has an odd number of elements in the vertices array, ignore the last
3343 // element.
3344 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3345 const size_t kExpectedSize = 3;
3346 ASSERT_EQ(kExpectedSize, size);
3347 std::vector<FS_POINTF> vertices_buffer(size);
3348 EXPECT_EQ(size,
3349 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3350 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3351 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3352 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3353 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3354 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3355 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3356 }
3357
3358 {
3359 // Wrong annotation type.
3360 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3361 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3362 }
3363
3364 UnloadPage(page);
3365}
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003366
3367TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) {
3368 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3369 FPDF_PAGE page = LoadPage(0);
3370 ASSERT_TRUE(page);
3371 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3372
3373 {
3374 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3375 ASSERT_TRUE(annot);
3376
3377 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3378 // testing.
3379 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3380 const size_t kExpectedSize = 1;
3381 ASSERT_EQ(kExpectedSize, size);
3382 const unsigned long kPathIndex = 0;
3383 unsigned long path_size =
3384 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3385 const size_t kExpectedPathSize = 3;
3386 ASSERT_EQ(kExpectedPathSize, path_size);
3387 std::vector<FS_POINTF> path_buffer(path_size);
3388 EXPECT_EQ(path_size,
3389 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3390 path_buffer.data(), path_size));
3391 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3392 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3393 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3394 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3395 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3396 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3397
3398 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3399 // testing.
3400 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3401 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3402
3403 // out of bounds path_index.
3404 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3405
3406 // path_buffer is not overwritten if it is too small.
3407 path_buffer.resize(1);
3408 path_buffer[0].x = 42;
3409 path_buffer[0].y = 43;
3410 path_size = FPDFAnnot_GetInkListPath(
3411 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3412 EXPECT_EQ(kExpectedSize, size);
3413 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3414 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3415 }
3416
3417 {
3418 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3419 ASSERT_TRUE(annot);
3420
3421 // This has an odd number of elements in the path array, ignore the last
3422 // element.
3423 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3424 const size_t kExpectedSize = 1;
3425 ASSERT_EQ(kExpectedSize, size);
3426 const unsigned long kPathIndex = 0;
3427 unsigned long path_size =
3428 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3429 const size_t kExpectedPathSize = 3;
3430 ASSERT_EQ(kExpectedPathSize, path_size);
3431 std::vector<FS_POINTF> path_buffer(path_size);
3432 EXPECT_EQ(path_size,
3433 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3434 path_buffer.data(), path_size));
3435 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3436 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3437 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3438 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3439 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3440 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3441 }
3442
3443 {
3444 // Wrong annotation type.
3445 ScopedFPDFAnnotation polygon_annot(
3446 FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON));
3447 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3448 const unsigned long kPathIndex = 0;
3449 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3450 nullptr, 0));
3451 }
3452
3453 UnloadPage(page);
3454}
Miklos Vajna30f45a62020-12-04 19:12:31 +00003455
3456TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) {
3457 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3458 FPDF_PAGE page = LoadPage(0);
3459 ASSERT_TRUE(page);
3460 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3461
3462 {
3463 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3464 ASSERT_TRUE(annot);
3465
3466 // FPDFAnnot_GetVertices() positive testing.
3467 FS_POINTF start;
3468 FS_POINTF end;
3469 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3470 EXPECT_FLOAT_EQ(159.0f, start.x);
3471 EXPECT_FLOAT_EQ(296.0f, start.y);
3472 EXPECT_FLOAT_EQ(472.0f, end.x);
3473 EXPECT_FLOAT_EQ(243.42f, end.y);
3474
3475 // FPDFAnnot_GetVertices() negative testing.
3476 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3477 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3478 }
3479
3480 {
3481 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3482 ASSERT_TRUE(annot);
3483
3484 // Too few elements in the line array.
3485 FS_POINTF start;
3486 FS_POINTF end;
3487 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3488 }
3489
3490 {
3491 // Wrong annotation type.
3492 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3493 FS_POINTF start;
3494 FS_POINTF end;
3495 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3496 }
3497
3498 UnloadPage(page);
3499}
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003500
3501TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) {
3502 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3503 FPDF_PAGE page = LoadPage(0);
3504 ASSERT_TRUE(page);
3505 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3506
3507 {
3508 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3509 ASSERT_TRUE(annot);
3510
3511 // FPDFAnnot_GetBorder() positive testing.
3512 float horizontal_radius;
3513 float vertical_radius;
3514 float border_width;
3515 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3516 &vertical_radius, &border_width));
3517 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3518 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3519 EXPECT_FLOAT_EQ(2.0f, border_width);
3520
3521 // FPDFAnnot_GetBorder() negative testing.
3522 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3523 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3524 }
3525
3526 {
3527 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3528 ASSERT_TRUE(annot);
3529
3530 // Too few elements in the border array.
3531 float horizontal_radius;
3532 float vertical_radius;
3533 float border_width;
3534 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3535 &vertical_radius, &border_width));
Lei Zhang21ea6652021-04-15 23:24:46 +00003536
3537 // FPDFAnnot_SetBorder() positive testing.
3538 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3539 /*vertical_radius=*/3.5f,
3540 /*border_width=*/4.0f));
3541
3542 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3543 &vertical_radius, &border_width));
3544 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3545 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3546 EXPECT_FLOAT_EQ(4.0f, border_width);
3547
3548 // FPDFAnnot_SetBorder() negative testing.
3549 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3550 /*vertical_radius=*/2.5f,
3551 /*border_width=*/3.0f));
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003552 }
3553
3554 UnloadPage(page);
3555}
Lei Zhang24de1492021-04-19 18:06:43 +00003556
3557// Due to https://crbug.com/pdfium/570, the AnnotationBorder test above cannot
3558// actually render the line annotations inside line_annot.pdf. For now, use a
3559// square annotation in annots.pdf for testing.
3560TEST_F(FPDFAnnotEmbedderTest, AnnotationBorderRendering) {
3561 ASSERT_TRUE(OpenDocument("annots.pdf"));
3562 FPDF_PAGE page = LoadPage(1);
3563 ASSERT_TRUE(page);
3564 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
3565
3566#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003567 constexpr char kOriginalChecksum[] = "4f35703e89202bcc8419ca2df739bb4e";
3568 constexpr char kModifiedChecksum[] = "cee0a1b41f33d487af8fb70c4c82e3c9";
Lei Zhang24de1492021-04-19 18:06:43 +00003569#elif defined(OS_APPLE)
Tom Andersond4fe5f72021-12-03 20:52:52 +00003570 constexpr char kOriginalChecksum[] = "522a4a6b6c7eab5bf95ded1f21ea372e";
3571 constexpr char kModifiedChecksum[] = "6844019e07b83cc01723415f58218d06";
Lei Zhang24de1492021-04-19 18:06:43 +00003572#else
Tom Andersond4fe5f72021-12-03 20:52:52 +00003573 constexpr char kOriginalChecksum[] = "12127303aecd80c6288460f7c0d79f3f";
3574 constexpr char kModifiedChecksum[] = "73d06ff4c665fe85029acef30240dcca";
Lei Zhang24de1492021-04-19 18:06:43 +00003575#endif
3576
3577 {
3578 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
3579 ASSERT_TRUE(annot);
3580 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
3581
3582 {
3583 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3584 CompareBitmap(bitmap.get(), 612, 792, kOriginalChecksum);
3585 }
3586
3587 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3588 /*vertical_radius=*/3.5f,
3589 /*border_width=*/4.0f));
3590
3591 {
3592 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3593 CompareBitmap(bitmap.get(), 612, 792, kModifiedChecksum);
3594 }
3595 }
3596
3597 // Save the document and close the page.
3598 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
3599 UnloadPage(page);
3600
3601 ASSERT_TRUE(OpenSavedDocument());
3602 page = LoadSavedPage(1);
3603 ASSERT_TRUE(page);
3604 VerifySavedRendering(page, 612, 792, kModifiedChecksum);
3605
3606 CloseSavedPage(page);
3607 CloseSavedDocument();
3608}