blob: 94dd88118bfd80d716a396b2f4fa8c56c525e96e [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_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001118#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst4a21df02020-05-13 03:15:44 +00001119 static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa";
1120 static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622";
1121 static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001122#else
Hui Yingst4a21df02020-05-13 03:15:44 +00001123 static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4";
1124 static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f";
1125 static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001126#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst4a21df02020-05-13 03:15:44 +00001127#else
1128#if defined(OS_WIN)
Hui Yingst282f0b82021-08-30 22:59:52 +00001129 static const char kMd5ModifiedPath[] = "15683598de2364925c42a50513dad71f";
1130 static const char kMd5TwoPaths[] = "9e53f315535e6f570560f46934442fbe";
1131 static const char kMd5NewAnnot[] = "5f2a2998b320b1ae035528f2b10a1b38";
Lei Zhang0c03d632020-07-30 17:05:36 +00001132#elif defined(OS_APPLE)
Hui Yingst4a21df02020-05-13 03:15:44 +00001133 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
1134 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
1135 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Jane Liubaa7ff42017-06-29 19:18:23 -04001136#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001137 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
1138 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
1139 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -04001140#endif
Hui Yingst4a21df02020-05-13 03:15:44 +00001141#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liubaa7ff42017-06-29 19:18:23 -04001142
1143 // Open a file with two annotations and load its first page.
1144 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001145 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001146 ASSERT_TRUE(page);
1147 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1148
1149 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001150 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001151 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001152 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001153 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001154
Lei Zhanga21d5932018-02-05 18:28:38 +00001155 {
1156 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001157 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001158 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001159
Lei Zhanga21d5932018-02-05 18:28:38 +00001160 // Check that this annotation has one path object and retrieve it.
1161 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001162 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001163 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1164 EXPECT_FALSE(path);
1165 path = FPDFAnnot_GetObject(annot.get(), 0);
1166 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1167 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001168
Lei Zhanga21d5932018-02-05 18:28:38 +00001169 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001170 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001171 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001172
Lei Zhanga21d5932018-02-05 18:28:38 +00001173 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001174 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001175 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001176 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001177 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001178
Lei Zhanga21d5932018-02-05 18:28:38 +00001179 // Add a second path object to the same annotation.
1180 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1181 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001182 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1183 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001184 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1185 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1186 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001187
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001188 // The object is in the annontation, not in the page, so the page object
1189 // array should not change.
1190 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1191
Lei Zhanga21d5932018-02-05 18:28:38 +00001192 // Check that the page with an annotation with two paths renders correctly.
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, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001196 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001197
Lei Zhanga21d5932018-02-05 18:28:38 +00001198 // Delete the newly added path object.
1199 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1200 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001201 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001202 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001203
1204 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001205 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001206 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001207 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001208 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001209
Jane Liubaa7ff42017-06-29 19:18:23 -04001210 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001211
Lei Zhanga21d5932018-02-05 18:28:38 +00001212 {
1213 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001214 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001215 ASSERT_TRUE(annot);
1216 rect.left = 200.f;
1217 rect.bottom = 400.f;
1218 rect.right = 500.f;
1219 rect.top = 600.f;
1220 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001221
Lei Zhanga21d5932018-02-05 18:28:38 +00001222 // Add a new path to the annotation.
1223 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1224 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1225 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1226 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1227 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001228 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1229 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001230 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1231 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1232 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1233
1234 // Check that the annotation's bounding box came from its rectangle.
1235 FS_RECTF new_rect;
1236 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1237 EXPECT_EQ(rect.left, new_rect.left);
1238 EXPECT_EQ(rect.bottom, new_rect.bottom);
1239 EXPECT_EQ(rect.right, new_rect.right);
1240 EXPECT_EQ(rect.top, new_rect.top);
1241 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001242
Lei Zhangec618142021-04-13 17:36:46 +00001243 // Save the document and close the page.
Jane Liubaa7ff42017-06-29 19:18:23 -04001244 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001245 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001246
1247 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001248 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001249 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001250 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001251 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001252
Jane Liu36567742017-07-06 11:13:35 -04001253 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001254 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001255
Lei Zhanga21d5932018-02-05 18:28:38 +00001256 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001257 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001258 ASSERT_TRUE(annot);
1259 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001260
Lei Zhanga21d5932018-02-05 18:28:38 +00001261 // Check that the new annotation's rectangle is as defined.
1262 FS_RECTF new_rect;
1263 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1264 EXPECT_EQ(rect.left, new_rect.left);
1265 EXPECT_EQ(rect.bottom, new_rect.bottom);
1266 EXPECT_EQ(rect.right, new_rect.right);
1267 EXPECT_EQ(rect.top, new_rect.top);
1268 }
1269
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001270 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001271 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001272}
Jane Liub137e752017-07-05 15:04:33 -04001273
Lei Zhangab41f252018-12-23 03:10:50 +00001274TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001275 // Open a file with an annotation and load its first page.
1276 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001277 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001278 ASSERT_TRUE(page);
1279
1280 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001281 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001282 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001283 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1284 }
Jane Liub137e752017-07-05 15:04:33 -04001285
Lei Zhanga21d5932018-02-05 18:28:38 +00001286 {
1287 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001288 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001289 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001290
Lei Zhanga21d5932018-02-05 18:28:38 +00001291 // Check that the original flag values are as expected.
1292 int flags = FPDFAnnot_GetFlags(annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00001293 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_INVISIBLE);
Lei Zhanga21d5932018-02-05 18:28:38 +00001294 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1295 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001296 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOZOOM);
1297 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOROTATE);
1298 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOVIEW);
1299 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_READONLY);
1300 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_LOCKED);
1301 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_TOGGLENOVIEW);
Jane Liub137e752017-07-05 15:04:33 -04001302
Lei Zhanga21d5932018-02-05 18:28:38 +00001303 // Set the HIDDEN flag.
1304 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1305 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1306 flags = FPDFAnnot_GetFlags(annot.get());
1307 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1308 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001309
Lei Zhanga21d5932018-02-05 18:28:38 +00001310 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001311 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001312 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001313 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001314 }
Jane Liub137e752017-07-05 15:04:33 -04001315
Lei Zhanga21d5932018-02-05 18:28:38 +00001316 // Unset the HIDDEN flag.
1317 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1318 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1319 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1320 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1321 flags = FPDFAnnot_GetFlags(annot.get());
1322 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1323 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001324
Lei Zhanga21d5932018-02-05 18:28:38 +00001325 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001326 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001327 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001328 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1329 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001330 }
Jane Liub137e752017-07-05 15:04:33 -04001331
Jane Liub137e752017-07-05 15:04:33 -04001332 UnloadPage(page);
1333}
Jane Liu36567742017-07-06 11:13:35 -04001334
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001335// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1336#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001337#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1338#else
1339#define MAYBE_AddAndModifyImage AddAndModifyImage
1340#endif
1341TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001342#if defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001343#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001344 static const char kMd5NewImage[] = "26a8eb30937226a677839379e0d7ae1a";
1345 static const char kMd5ModifiedImage[] = "2985114b32ba1a96be78ee643fe31aa5";
1346#else
1347 static const char kMd5NewImage[] = "14012ab500b4671fa73dd760129a8a93";
1348 static const char kMd5ModifiedImage[] = "5f97f98f58ed04dc393f31460485f1a2";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001349#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001350#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001351#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001352 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
1353 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +00001354#elif defined(OS_WIN)
Hui Yingst282f0b82021-08-30 22:59:52 +00001355 static const char kMd5NewImage[] = "a822fffc257c445467aa91876e1f8611";
1356 static const char kMd5ModifiedImage[] = "a5afb12f4f516e1f5f675c67f99e043a";
Jane Liu36567742017-07-06 11:13:35 -04001357#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001358 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
1359 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -04001360#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001361#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001362
1363 // Open a file with two annotations and load its first page.
1364 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001365 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001366 ASSERT_TRUE(page);
1367 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1368
1369 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001370 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001371 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001372 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001373 }
Jane Liu36567742017-07-06 11:13:35 -04001374
Jane Liu36567742017-07-06 11:13:35 -04001375 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001376 FPDF_BITMAP image_bitmap;
1377
1378 {
1379 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001380 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001381 ASSERT_TRUE(annot);
1382 FS_RECTF rect;
1383 rect.left = 200.f;
1384 rect.bottom = 600.f;
1385 rect.right = 400.f;
1386 rect.top = 800.f;
1387 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1388
1389 // Add a solid-color translucent image object to the new annotation.
1390 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1391 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1392 0xeeeecccc);
1393 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1394 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1395 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1396 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
Lei Zhangc8601bf2021-06-29 23:19:27 +00001397 static constexpr FS_MATRIX kBitmapScaleMatrix{kBitmapSize, 0, 0,
1398 kBitmapSize, 0, 0};
1399 ASSERT_TRUE(FPDFPageObj_SetMatrix(image_object, &kBitmapScaleMatrix));
Lei Zhanga21d5932018-02-05 18:28:38 +00001400 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1401 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1402 }
Jane Liu36567742017-07-06 11:13:35 -04001403
1404 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001405 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001406 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001407 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001408 }
Jane Liu36567742017-07-06 11:13:35 -04001409
Lei Zhanga21d5932018-02-05 18:28:38 +00001410 {
1411 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001412 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001413 ASSERT_TRUE(annot);
1414 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1415 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1416 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001417
Lei Zhanga21d5932018-02-05 18:28:38 +00001418 // Modify the image in the new annotation.
1419 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1420 0xff000000);
1421 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1422 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1423 }
Jane Liu36567742017-07-06 11:13:35 -04001424
Lei Zhangec618142021-04-13 17:36:46 +00001425 // Save the document and close the page.
Jane Liu36567742017-07-06 11:13:35 -04001426 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001427 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001428 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001429
1430 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001431 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001432}
1433
Hui Yingst6cd754f2020-05-14 04:05:25 +00001434TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001435#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001436#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001437 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1438 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001439#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001440 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1441 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001442#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001443#else
1444#if defined(OS_WIN)
Hui Yingst282f0b82021-08-30 22:59:52 +00001445 static const char kMd5NewText[] = "31e106afba1b9af2c8613c9d6bd77153";
1446 static const char kMd5ModifiedText[] = "c5ef84b480cd762b0b9dad715beb92b6";
Lei Zhang0c03d632020-07-30 17:05:36 +00001447#elif defined(OS_APPLE)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001448 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1449 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001450#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001451 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1452 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001453#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001454#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001455
1456 // Open a file with two annotations and load its first page.
1457 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001458 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001459 ASSERT_TRUE(page);
1460 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1461
1462 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001463 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001464 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001465 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001466 }
Jane Liu36567742017-07-06 11:13:35 -04001467
Lei Zhanga21d5932018-02-05 18:28:38 +00001468 {
1469 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001470 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001471 ASSERT_TRUE(annot);
1472 FS_RECTF rect;
1473 rect.left = 200.f;
1474 rect.bottom = 550.f;
1475 rect.right = 450.f;
1476 rect.top = 650.f;
1477 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001478
Lei Zhanga21d5932018-02-05 18:28:38 +00001479 // Add a translucent text object to the new annotation.
1480 FPDF_PAGEOBJECT text_object =
1481 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1482 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001483 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001484 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1485 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001486 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001487 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1488 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1489 }
Jane Liu36567742017-07-06 11:13:35 -04001490
1491 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001492 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001493 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001494 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001495 }
Jane Liu36567742017-07-06 11:13:35 -04001496
Lei Zhanga21d5932018-02-05 18:28:38 +00001497 {
1498 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001499 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001500 ASSERT_TRUE(annot);
1501 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1502 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1503 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001504
Lei Zhanga21d5932018-02-05 18:28:38 +00001505 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001506 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001507 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1508 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1509 }
Jane Liu36567742017-07-06 11:13:35 -04001510
1511 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001512 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001513 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001514 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001515 }
Jane Liu36567742017-07-06 11:13:35 -04001516
1517 // Remove the new annotation, and check that the page renders as before.
1518 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001519 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001520 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001521 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001522 }
Jane Liu36567742017-07-06 11:13:35 -04001523
1524 UnloadPage(page);
1525}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001526
Hui Yingst9b6b1542020-07-27 16:11:12 +00001527TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001528 // Open a file with four annotations and load its first page.
1529 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001530 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001531 ASSERT_TRUE(page);
1532
Lei Zhang4f556b82019-04-08 16:32:41 +00001533 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001534
Lei Zhanga21d5932018-02-05 18:28:38 +00001535 {
1536 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001537 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001538 ASSERT_TRUE(annot);
1539
1540 // Check that a non-existent key does not exist.
1541 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1542
1543 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001544 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001545 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001546 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1547 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1548 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001549
1550 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001551 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001552 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001553 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001554 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001555 ASSERT_EQ(66u, length_bytes);
1556 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1557 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1558 length_bytes));
1559 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1560 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001561
1562 // Check that the string value of the modified date is correct.
1563 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001564 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1565 nullptr, 0);
1566 ASSERT_EQ(44u, length_bytes);
1567 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001568 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001569 buf.data(), length_bytes));
1570 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001571
1572 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001573 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001574 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1575 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001576 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001577
Lei Zhangec618142021-04-13 17:36:46 +00001578 // Save the document and close the page.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001579 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001580 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001581
Hui Yingst9b6b1542020-07-27 16:11:12 +00001582#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001583#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001584 static const char kMd5[] = "7a2b712ca88d7b71f125ea3f9c88e57a";
1585#else
1586 static const char kMd5[] = "626d25c5aa5baf67d22d9a0e1c23f6aa";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001587#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001588#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001589#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001590 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001591#elif defined(OS_WIN)
Hui Yingst282f0b82021-08-30 22:59:52 +00001592 static const char kMd5[] = "64631cd3422201a36ea8f7ec63b34ae7";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001593#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001594 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001595#endif
Hui Yingst9b6b1542020-07-27 16:11:12 +00001596#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Dan Sinclair971a6742018-03-28 19:23:25 +00001597
1598 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001599 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001600 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001601 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001602 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001603 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001604 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001605
Lei Zhanga21d5932018-02-05 18:28:38 +00001606 // Check that the string value of the modified date is the newly-set value.
1607 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001608 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001609 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001610 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001611 ASSERT_EQ(44u, length_bytes);
1612 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001613 EXPECT_EQ(44u,
1614 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001615 buf.data(), length_bytes));
1616 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001617 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001618
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001619 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001620 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001621}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001622
rycsmith3e785602019-03-05 21:48:36 +00001623TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001624 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001625 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1626 FPDF_PAGE page = LoadPage(0);
1627 ASSERT_TRUE(page);
1628 {
1629 // First two annotations do not have "MaxLen" attribute.
1630 for (int i = 0; i < 2; i++) {
1631 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1632 ASSERT_TRUE(annot);
1633
1634 // Verify that no "MaxLen" key present.
1635 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1636
1637 float value;
1638 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1639 }
1640
1641 // Annotation in index 2 has "MaxLen" of 10.
1642 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1643 ASSERT_TRUE(annot);
1644
1645 // Verify that "MaxLen" key present.
1646 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1647
1648 float value;
1649 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1650 EXPECT_FLOAT_EQ(10.0f, value);
1651
1652 // Check bad inputs.
1653 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1654 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1655 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1656 // Ask for key that exists but is not a number.
1657 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1658 }
1659
1660 UnloadPage(page);
1661}
1662
Lei Zhangab41f252018-12-23 03:10:50 +00001663TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001664 // Open a file with four annotations and load its first page.
1665 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001666 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001667 ASSERT_TRUE(page);
1668
Lei Zhanga21d5932018-02-05 18:28:38 +00001669 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001670 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1671 static constexpr size_t kExpectNormalAPLength = 73970;
1672
Lei Zhanga21d5932018-02-05 18:28:38 +00001673 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001674 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001675 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001676
Lei Zhanga21d5932018-02-05 18:28:38 +00001677 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001678 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001679 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001680 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001681
Lei Zhanga21d5932018-02-05 18:28:38 +00001682 // Check that the string value of an AP is not returned if the buffer is too
1683 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001684 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1685 // Write in the buffer to verify it's not overwritten.
1686 memcpy(buf.data(), "abcdefgh", 8);
1687 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001688 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001689 buf.data(), normal_length_bytes - 1));
1690 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001691
Lei Zhanga21d5932018-02-05 18:28:38 +00001692 // Check that the string value of an AP is returned through a buffer that is
1693 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001694 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001695 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001696 buf.data(), normal_length_bytes));
1697 EXPECT_EQ(kMd5NormalAP,
1698 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1699 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001700
Lei Zhanga21d5932018-02-05 18:28:38 +00001701 // Check that the string value of an AP is returned through a buffer that is
1702 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001703 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1704 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001705 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001706 buf.data(), normal_length_bytes + 2));
1707 EXPECT_EQ(kMd5NormalAP,
1708 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1709 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001710
Lei Zhanga21d5932018-02-05 18:28:38 +00001711 // Check that getting an AP for a mode that does not have an AP returns an
1712 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001713 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001714 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001715 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001716
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001717 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001718 EXPECT_EQ(2u,
1719 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001720 buf.data(), 1000));
1721 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001722
Lei Zhanga21d5932018-02-05 18:28:38 +00001723 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001724 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001725 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001726 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001727 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001728 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001729 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001730
Lei Zhanga21d5932018-02-05 18:28:38 +00001731 // Set the AP correctly now.
1732 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001733 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001734
Lei Zhanga21d5932018-02-05 18:28:38 +00001735 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001736 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001737 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001738 ASSERT_EQ(24u, rollover_length_bytes);
1739 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001740 EXPECT_EQ(24u,
1741 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001742 buf.data(), rollover_length_bytes));
1743 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001744
Lei Zhanga21d5932018-02-05 18:28:38 +00001745 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001746 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1747 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001748 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001749 buf.data(), normal_length_bytes));
1750 EXPECT_EQ(kMd5NormalAP,
1751 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1752 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001753 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001754
1755 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001756 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001757 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001758
Lei Zhang0b494052019-01-31 21:41:15 +00001759 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001760 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001761 ASSERT_TRUE(page);
Lei Zhanga21d5932018-02-05 18:28:38 +00001762 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001763 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001764
Lei Zhanga21d5932018-02-05 18:28:38 +00001765 // Check that the new annotation value is equal to the value we set before
1766 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001767 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001768 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001769 ASSERT_EQ(24u, rollover_length_bytes);
1770 std::vector<FPDF_WCHAR> buf =
1771 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001772 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1773 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001774 buf.data(), rollover_length_bytes));
1775 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001776 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001777
1778 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001779 CloseSavedPage(page);
1780 CloseSavedDocument();
1781}
1782
Lei Zhangab41f252018-12-23 03:10:50 +00001783TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001784 // Open a file with four annotations and load its first page.
1785 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001786 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001787 ASSERT_TRUE(page);
1788
Lei Zhanga21d5932018-02-05 18:28:38 +00001789 {
1790 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001791 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001792 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001793
Lei Zhanga21d5932018-02-05 18:28:38 +00001794 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001795 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001796 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001797 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001798 EXPECT_EQ(73970u,
1799 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1800 nullptr, 0));
1801 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1802 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001803
Lei Zhanga21d5932018-02-05 18:28:38 +00001804 // Check that setting the Down AP to null removes the Down entry but keeps
1805 // Normal intact.
1806 EXPECT_TRUE(
1807 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1808 EXPECT_EQ(73970u,
1809 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1810 nullptr, 0));
1811 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1812 nullptr, 0));
1813 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001814
Lei Zhang75c81712018-02-08 17:22:39 +00001815 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001816}
1817
Lei Zhangab41f252018-12-23 03:10:50 +00001818TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001819 // Open a file with four annotations and load its first page.
1820 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001821 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001822 ASSERT_TRUE(page);
1823
Lei Zhanga21d5932018-02-05 18:28:38 +00001824 {
1825 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001826 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001827 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001828
Lei Zhanga21d5932018-02-05 18:28:38 +00001829 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001830 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001831 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001832 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001833 EXPECT_EQ(73970u,
1834 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1835 nullptr, 0));
1836 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1837 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001838
Lei Zhanga21d5932018-02-05 18:28:38 +00001839 // Check that setting the Normal AP to null removes the whole AP dictionary.
1840 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1841 nullptr));
1842 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1843 nullptr, 0));
1844 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1845 nullptr, 0));
1846 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001847
Lei Zhang75c81712018-02-08 17:22:39 +00001848 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001849}
1850
Lei Zhangab41f252018-12-23 03:10:50 +00001851TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001852 // Open a file with annotations and load its first page.
1853 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001854 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001855 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001856 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001857
Lei Zhanga21d5932018-02-05 18:28:38 +00001858 {
1859 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001860 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001861 ASSERT_TRUE(annot);
1862 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1863 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001864 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001865 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1866 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1867 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001868
Lei Zhanga21d5932018-02-05 18:28:38 +00001869 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001870 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001871 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1872 ASSERT_TRUE(popup);
1873 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1874 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1875 FS_RECTF rect;
1876 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1877 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1878 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001879
Lei Zhanga21d5932018-02-05 18:28:38 +00001880 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1881 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001882 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001883 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1884 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001885
Lei Zhanga21d5932018-02-05 18:28:38 +00001886 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1887 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001888 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001889 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001890 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1891 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001892 }
Jane Liu300bb272017-08-21 14:37:53 -04001893
Jane Liu300bb272017-08-21 14:37:53 -04001894 UnloadPage(page);
1895}
1896
Lei Zhangab41f252018-12-23 03:10:50 +00001897TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001898 // Open file with form text fields.
1899 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001900 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001901 ASSERT_TRUE(page);
1902
Lei Zhanga21d5932018-02-05 18:28:38 +00001903 {
1904 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001905 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001906 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001907
Lei Zhanga21d5932018-02-05 18:28:38 +00001908 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001909 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001910 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001911 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1912 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1913 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001914 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001915 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001916
Lei Zhanga21d5932018-02-05 18:28:38 +00001917 {
1918 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001919 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001920 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001921
Lei Zhanga21d5932018-02-05 18:28:38 +00001922 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001923 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001924 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001925 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1926 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1927 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001928 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1929 }
1930
1931 {
1932 // Retrieve the fourth annotation: user-editable password text field.
1933 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1934 ASSERT_TRUE(annot);
1935
1936 // Check that the flag values are as expected.
1937 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1938 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);
1941 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001942 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001943 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001944
1945 UnloadPage(page);
1946}
1947
Lei Zhangab41f252018-12-23 03:10:50 +00001948TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001949 // Open file with form text fields.
1950 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001951 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001952 ASSERT_TRUE(page);
1953
Lei Zhanga21d5932018-02-05 18:28:38 +00001954 {
1955 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001956 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001957 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001958
Lei Zhanga21d5932018-02-05 18:28:38 +00001959 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001960 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001961 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001962 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1963 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001964 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1965 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001966 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001967 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001968
Lei Zhanga21d5932018-02-05 18:28:38 +00001969 {
1970 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001971 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001972 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001973
Lei Zhanga21d5932018-02-05 18:28:38 +00001974 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001975 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001976 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001977 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1978 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001979 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1980 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001981 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001982 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001983
Lei Zhanga21d5932018-02-05 18:28:38 +00001984 {
1985 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001986 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001987 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001988
Lei Zhanga21d5932018-02-05 18:28:38 +00001989 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001990 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001991 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001992 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1993 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001994 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1995 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001996 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001997 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001998
1999 UnloadPage(page);
2000}
Diana Gage40870db2017-07-19 18:16:03 -07002001
Lei Zhangab41f252018-12-23 03:10:50 +00002002TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07002003 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002004 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002005 FPDF_PAGE page = LoadPage(0);
2006 ASSERT_TRUE(page);
2007
2008 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00002009 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
2010 EXPECT_FALSE(
2011 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00002012
Lei Zhang8da98232019-12-11 23:29:33 +00002013 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00002014 {
2015 // Verify there is an annotation.
2016 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002017 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00002018 EXPECT_TRUE(annot);
2019 }
2020
2021 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00002022 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
2023 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
2024 EXPECT_FALSE(
2025 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07002026
2027 UnloadPage(page);
2028}
2029
Lei Zhangab41f252018-12-23 03:10:50 +00002030TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07002031 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002032 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002033 FPDF_PAGE page = LoadPage(0);
2034 ASSERT_TRUE(page);
2035
Lei Zhanga21d5932018-02-05 18:28:38 +00002036 {
2037 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002038 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002039 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002040 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002041 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002042
Lei Zhanga21d5932018-02-05 18:28:38 +00002043 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002044 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
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_FALSE(flags & FPDF_FORMFLAG_READONLY);
2048 }
Diana Gage40870db2017-07-19 18:16:03 -07002049
Lei Zhanga21d5932018-02-05 18:28:38 +00002050 {
2051 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002052 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002053 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002054 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002055 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002056
Lei Zhanga21d5932018-02-05 18:28:38 +00002057 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002058 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002059 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002060 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2061 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002062 }
Diana Gage40870db2017-07-19 18:16:03 -07002063
2064 UnloadPage(page);
2065}
2066
Lei Zhangab41f252018-12-23 03:10:50 +00002067TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07002068 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002069 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002070 FPDF_PAGE page = LoadPage(0);
2071 ASSERT_TRUE(page);
2072
Lei Zhanga21d5932018-02-05 18:28:38 +00002073 {
2074 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002075 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002076 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002077 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002078 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002079
Lei Zhanga21d5932018-02-05 18:28:38 +00002080 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002081 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002082 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002083 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2084 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002085 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2086 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002087 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002088 }
Diana Gage40870db2017-07-19 18:16:03 -07002089
Lei Zhanga21d5932018-02-05 18:28:38 +00002090 {
2091 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002092 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002093 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002094 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002095 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002096
Lei Zhanga21d5932018-02-05 18:28:38 +00002097 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002098 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002099 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002100 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2101 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002102 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2103 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002104 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002105 }
Diana Gage40870db2017-07-19 18:16:03 -07002106
Lei Zhanga21d5932018-02-05 18:28:38 +00002107 {
2108 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002109 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002110 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002111 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002112 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002113
Lei Zhanga21d5932018-02-05 18:28:38 +00002114 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002115 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002116 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002117 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2118 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002119 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2120 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002121 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002122 }
Diana Gage40870db2017-07-19 18:16:03 -07002123
2124 UnloadPage(page);
2125}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002126
Hui Yingst603dcd82020-11-09 22:36:46 +00002127TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
Lei Zhang03e5e682019-09-16 19:45:55 +00002128#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst603dcd82020-11-09 22:36:46 +00002129 static const char kExpectedBitmap[] = "a1ea1ceebb26922fae576cb79ce63af0";
Lei Zhang03e5e682019-09-16 19:45:55 +00002130#else
Lei Zhang992e7e22019-02-04 19:20:58 +00002131 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
Hui Yingst603dcd82020-11-09 22:36:46 +00002132#endif
2133 static constexpr size_t kExpectedSize = 1609;
Lei Zhang992e7e22019-02-04 19:20:58 +00002134
2135 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2136
2137 FPDF_PAGE page = LoadPage(0);
2138 ASSERT_TRUE(page);
2139
2140 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2141 EXPECT_EQ(kExpectedSize, GetString().size());
2142 ClearString();
2143
2144 for (size_t i = 0; i < 10; ++i) {
2145 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2146 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2147
2148 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2149 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2150 // equal, not bigger.
2151 EXPECT_LT(kExpectedSize, GetString().size());
2152 ClearString();
2153 }
2154
2155 UnloadPage(page);
2156}
2157
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002158TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2159 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2160 FPDF_PAGE page = LoadPage(0);
2161 ASSERT_TRUE(page);
2162 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2163
2164 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002165 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002166 static const size_t kBufSize = 12;
2167 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002168
2169 {
2170 // Add a text annotation to the page.
2171 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2172 ASSERT_TRUE(annot);
2173 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2174 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2175
2176 // Make sure there is no test key, add set a value there, and read it back.
2177 std::fill(buf.begin(), buf.end(), 'x');
2178 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002179 kBufSize));
2180 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002181
Lei Zhangf0f67682019-04-08 17:03:21 +00002182 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002183 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2184
2185 std::fill(buf.begin(), buf.end(), 'x');
2186 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002187 kBufSize));
2188 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002189 }
2190
Lei Zhang05ec64c2019-01-09 03:00:06 +00002191 {
2192 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2193 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002194 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002195 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002196 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2197 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2198 // Also do the same test for its appearance string.
2199 std::fill(buf.begin(), buf.end(), 'x');
2200 ASSERT_EQ(2u,
2201 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002202 buf.data(), kBufSize));
2203 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002204
Lei Zhangf0f67682019-04-08 17:03:21 +00002205 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002206 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2207 text.get()));
2208
2209 std::fill(buf.begin(), buf.end(), 'x');
2210 ASSERT_EQ(6u,
2211 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002212 buf.data(), kBufSize));
2213 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002214 }
2215
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002216 UnloadPage(page);
2217
2218 {
2219 // Save a copy, open the copy, and check the annotation again.
2220 // Note that it renders the rotation.
2221 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002222 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002223 FPDF_PAGE saved_page = LoadSavedPage(0);
2224 ASSERT_TRUE(saved_page);
2225
Lei Zhang05ec64c2019-01-09 03:00:06 +00002226 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002227 {
2228 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2229 ASSERT_TRUE(annot);
2230 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2231
2232 std::fill(buf.begin(), buf.end(), 'x');
2233 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002234 kBufSize));
2235 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002236 }
2237
Lei Zhang05ec64c2019-01-09 03:00:06 +00002238 {
2239 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2240 ASSERT_TRUE(annot);
2241 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2242 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2243
2244 std::fill(buf.begin(), buf.end(), 'x');
2245 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002246 kBufSize));
2247 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002248 }
2249
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002250 CloseSavedPage(saved_page);
2251 CloseSavedDocument();
2252 }
2253}
rycsmithcb752f32019-02-21 18:40:53 +00002254
2255TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2256 // Open a file with combobox widget annotations and load its first page.
2257 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2258 FPDF_PAGE page = LoadPage(0);
2259 ASSERT_TRUE(page);
2260
2261 {
2262 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2263 ASSERT_TRUE(annot);
2264
2265 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2266
2267 annot.reset(FPDFPage_GetAnnot(page, 1));
2268 ASSERT_TRUE(annot);
2269
2270 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002271
2272 // Check bad form handle / annot.
2273 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2274 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2275 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002276 }
2277
2278 UnloadPage(page);
2279}
2280
2281TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2282 // Open a file with listbox widget annotations and load its first page.
2283 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2284 FPDF_PAGE page = LoadPage(0);
2285 ASSERT_TRUE(page);
2286
2287 {
2288 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2289 ASSERT_TRUE(annot);
2290
2291 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2292
2293 annot.reset(FPDFPage_GetAnnot(page, 1));
2294 ASSERT_TRUE(annot);
2295
2296 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2297 }
2298
2299 UnloadPage(page);
2300}
2301
2302TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2303 // Open a file with ink annotations and load its first page.
2304 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2305 FPDF_PAGE page = LoadPage(0);
2306 ASSERT_TRUE(page);
2307
2308 {
2309 // annotations do not have "Opt" array and will return -1
2310 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2311 ASSERT_TRUE(annot);
2312
2313 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2314
2315 annot.reset(FPDFPage_GetAnnot(page, 1));
2316 ASSERT_TRUE(annot);
2317
2318 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2319 }
2320
2321 UnloadPage(page);
2322}
2323
2324TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2325 // Open a file with combobox widget annotations and load its first page.
2326 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2327 FPDF_PAGE page = LoadPage(0);
2328 ASSERT_TRUE(page);
2329
2330 {
2331 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2332 ASSERT_TRUE(annot);
2333
2334 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002335 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002336 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002337 ASSERT_EQ(8u, length_bytes);
2338 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002339 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002340 buf.data(), length_bytes));
2341 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002342
2343 annot.reset(FPDFPage_GetAnnot(page, 1));
2344 ASSERT_TRUE(annot);
2345
2346 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002347 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002348 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002349 ASSERT_EQ(12u, length_bytes);
2350 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002351 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002352 buf.data(), length_bytes));
2353 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002354
2355 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002356 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002357 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002358 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002359 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002360 buf.data(), length_bytes));
2361 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002362
Lei Zhange7033c82019-02-26 19:30:49 +00002363 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002364 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2365 nullptr, 0));
2366 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2367 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002368
2369 // Check bad form handle / annot.
2370 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2371 EXPECT_EQ(0u,
2372 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2373 EXPECT_EQ(0u,
2374 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002375 }
2376
2377 UnloadPage(page);
2378}
2379
2380TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2381 // Open a file with listbox widget annotations and load its first page.
2382 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2383 FPDF_PAGE page = LoadPage(0);
2384 ASSERT_TRUE(page);
2385
2386 {
2387 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2388 ASSERT_TRUE(annot);
2389
2390 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002391 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002392 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002393 ASSERT_EQ(8u, length_bytes);
2394 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002395 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002396 buf.data(), length_bytes));
2397 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002398
2399 annot.reset(FPDFPage_GetAnnot(page, 1));
2400 ASSERT_TRUE(annot);
2401
2402 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002403 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002404 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002405 ASSERT_EQ(12u, length_bytes);
2406 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002407 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002408 buf.data(), length_bytes));
2409 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002410
2411 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002412 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002413 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002414 ASSERT_EQ(18u, length_bytes);
2415 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002416 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002417 buf.data(), length_bytes));
2418 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002419
2420 // indices out of range
2421 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2422 nullptr, 0));
2423 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2424 nullptr, 0));
2425 }
2426
2427 UnloadPage(page);
2428}
2429
2430TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2431 // Open a file with ink annotations and load its first page.
2432 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2433 FPDF_PAGE page = LoadPage(0);
2434 ASSERT_TRUE(page);
2435
2436 {
2437 // annotations do not have "Opt" array and will return 0
2438 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2439 ASSERT_TRUE(annot);
2440
2441 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2442 nullptr, 0));
2443
2444 annot.reset(FPDFPage_GetAnnot(page, 1));
2445 ASSERT_TRUE(annot);
2446
2447 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2448 nullptr, 0));
2449 }
2450
2451 UnloadPage(page);
2452}
Ryan Smith09c23b12019-04-25 18:09:06 +00002453
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002454TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2455 // Open a file with combobox widget annotations and load its first page.
2456 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2457 FPDF_PAGE page = LoadPage(0);
2458 ASSERT_TRUE(page);
2459
2460 {
2461 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2462 ASSERT_TRUE(annot);
2463
2464 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2465 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2466 ASSERT_EQ(3, count);
2467 for (int i = 0; i < count; i++) {
2468 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2469 }
2470
2471 annot.reset(FPDFPage_GetAnnot(page, 1));
2472 ASSERT_TRUE(annot);
2473
2474 // Checks for Combobox with Values (/V) object which is just a string.
2475 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2476 ASSERT_EQ(26, count);
2477 for (int i = 0; i < count; i++) {
2478 EXPECT_EQ(i == 1,
2479 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2480 }
2481
2482 // Checks for index outside bound i.e. (index >= CountOption()).
2483 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2484 /*index=*/26));
2485 // Checks for negetive index.
2486 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2487 /*index=*/-1));
2488
2489 // Checks for bad form handle/annot.
2490 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2491 EXPECT_FALSE(
2492 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2493 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2494 }
2495
2496 UnloadPage(page);
2497}
2498
2499TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2500 // Open a file with listbox widget annotations and load its first page.
2501 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2502 FPDF_PAGE page = LoadPage(0);
2503 ASSERT_TRUE(page);
2504
2505 {
2506 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2507 ASSERT_TRUE(annot);
2508
2509 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2510 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2511 ASSERT_EQ(3, count);
2512 for (int i = 0; i < count; i++) {
2513 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2514 }
2515
2516 annot.reset(FPDFPage_GetAnnot(page, 1));
2517 ASSERT_TRUE(annot);
2518
2519 // Checks for Listbox with Values (/V) object which is just a string.
2520 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2521 ASSERT_EQ(26, count);
2522 for (int i = 0; i < count; i++) {
2523 EXPECT_EQ(i == 1,
2524 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2525 }
2526
2527 annot.reset(FPDFPage_GetAnnot(page, 3));
2528 ASSERT_TRUE(annot);
2529
2530 // Checks for Listbox with only Selected indices (/I) object which is an
2531 // array with multiple objects.
2532 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2533 ASSERT_EQ(5, count);
2534 for (int i = 0; i < count; i++) {
2535 bool expected = (i == 1 || i == 3);
2536 EXPECT_EQ(expected,
2537 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2538 }
2539
2540 annot.reset(FPDFPage_GetAnnot(page, 4));
2541 ASSERT_TRUE(annot);
2542
2543 // Checks for Listbox with Values (/V) object which is an array with
2544 // multiple objects.
2545 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2546 ASSERT_EQ(5, count);
2547 for (int i = 0; i < count; i++) {
2548 bool expected = (i == 2 || i == 4);
2549 EXPECT_EQ(expected,
2550 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2551 }
2552
2553 annot.reset(FPDFPage_GetAnnot(page, 5));
2554 ASSERT_TRUE(annot);
2555
2556 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2557 // objects conflict with different lengths.
2558 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2559 ASSERT_EQ(5, count);
2560 for (int i = 0; i < count; i++) {
2561 bool expected = (i == 0 || i == 2);
2562 EXPECT_EQ(expected,
2563 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2564 }
2565 }
2566
2567 UnloadPage(page);
2568}
2569
2570TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2571 // Open a file with multiple form field annotations and load its first page.
2572 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2573 FPDF_PAGE page = LoadPage(0);
2574 ASSERT_TRUE(page);
2575
2576 {
2577 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2578 ASSERT_TRUE(annot);
2579
2580 // Checks for link annotation.
2581 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2582 /*index=*/0));
2583
2584 annot.reset(FPDFPage_GetAnnot(page, 3));
2585 ASSERT_TRUE(annot);
2586
2587 // Checks for text field annotation.
2588 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2589 /*index=*/0));
2590 }
2591
2592 UnloadPage(page);
2593}
2594
Ryan Smith09c23b12019-04-25 18:09:06 +00002595TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2596 // Open a file with combobox annotations and load its first page.
2597 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2598 FPDF_PAGE page = LoadPage(0);
2599 ASSERT_TRUE(page);
2600
2601 {
2602 // All 3 widgets have Tf font size 12.
2603 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2604 ASSERT_TRUE(annot);
2605
2606 float value;
2607 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2608 EXPECT_EQ(12.0, value);
2609
2610 annot.reset(FPDFPage_GetAnnot(page, 1));
2611 ASSERT_TRUE(annot);
2612
2613 float value_two;
2614 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2615 EXPECT_EQ(12.0, value_two);
2616
2617 annot.reset(FPDFPage_GetAnnot(page, 2));
2618 ASSERT_TRUE(annot);
2619
2620 float value_three;
2621 ASSERT_TRUE(
2622 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2623 EXPECT_EQ(12.0, value_three);
2624 }
2625
2626 UnloadPage(page);
2627}
2628
2629TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2630 // Open a file with textfield annotations and load its first page.
2631 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2632 FPDF_PAGE page = LoadPage(0);
2633 ASSERT_TRUE(page);
2634
2635 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002636 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002637 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2638 ASSERT_TRUE(annot);
2639
2640 float value;
2641 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2642 EXPECT_EQ(12.0, value);
2643
2644 annot.reset(FPDFPage_GetAnnot(page, 1));
2645 ASSERT_TRUE(annot);
2646
2647 float value_two;
2648 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2649 EXPECT_EQ(12.0, value_two);
2650
2651 annot.reset(FPDFPage_GetAnnot(page, 2));
2652 ASSERT_TRUE(annot);
2653
2654 float value_three;
2655 ASSERT_TRUE(
2656 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2657 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002658
2659 float value_four;
2660 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2661 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002662 }
2663
2664 UnloadPage(page);
2665}
2666
2667TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2668 // Open a file with ink annotations and load its first page.
2669 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2670 FPDF_PAGE page = LoadPage(0);
2671 ASSERT_TRUE(page);
2672
2673 {
2674 // Annotations that do not have variable text and will return -1.
2675 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2676 ASSERT_TRUE(annot);
2677
2678 float value;
2679 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2680
2681 annot.reset(FPDFPage_GetAnnot(page, 1));
2682 ASSERT_TRUE(annot);
2683
2684 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2685 }
2686
2687 UnloadPage(page);
2688}
2689
2690TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2691 // Open a file with combobox annotations and load its first page.
2692 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2693 FPDF_PAGE page = LoadPage(0);
2694 ASSERT_TRUE(page);
2695
2696 {
2697 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2698 ASSERT_TRUE(annot);
2699
2700 // Check bad form handle / annot.
2701 float value;
2702 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2703 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2704 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2705 }
2706
2707 UnloadPage(page);
2708}
2709
2710TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2711 // Open a file with textfield annotations and load its first page.
2712 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2713 FPDF_PAGE page = LoadPage(0);
2714 ASSERT_TRUE(page);
2715
2716 {
2717 // Obtain the first annotation, a text field with negative font size, -12.
2718 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2719 ASSERT_TRUE(annot);
2720
2721 float value;
2722 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2723 EXPECT_EQ(-12.0, value);
2724 }
2725
2726 UnloadPage(page);
2727}
Ryan Smith23fdf892019-07-17 21:51:26 +00002728
2729TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2730 // Open a file with checkbox and radiobuttons widget annotations and load its
2731 // first page.
2732 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2733 FPDF_PAGE page = LoadPage(0);
2734 ASSERT_TRUE(page);
2735
2736 {
2737 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2738 ASSERT_TRUE(annot);
2739 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2740 }
2741
2742 UnloadPage(page);
2743}
2744
2745TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
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, 0));
2754 ASSERT_TRUE(annot);
2755 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2756 }
2757
2758 UnloadPage(page);
2759}
2760
2761TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2762 // Open a file with checkbox and radiobutton widget annotations and load its
2763 // first page.
2764 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2765 FPDF_PAGE page = LoadPage(0);
2766 ASSERT_TRUE(page);
2767
2768 {
2769 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2770 ASSERT_TRUE(annot);
2771 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2772
2773 annot.reset(FPDFPage_GetAnnot(page, 6));
2774 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2775
2776 annot.reset(FPDFPage_GetAnnot(page, 7));
2777 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2778 }
2779
2780 UnloadPage(page);
2781}
2782
2783TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2784 // Open a file with checkbox and radiobutton widget annotations and load its
2785 // first page.
2786 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2787 FPDF_PAGE page = LoadPage(0);
2788 ASSERT_TRUE(page);
2789
2790 {
2791 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2792 ASSERT_TRUE(annot);
2793 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2794
2795 annot.reset(FPDFPage_GetAnnot(page, 3));
2796 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2797
2798 annot.reset(FPDFPage_GetAnnot(page, 4));
2799 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2800 }
2801
2802 UnloadPage(page);
2803}
2804
2805TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2806 // Open a file with checkbox and radiobuttons widget annotations and load its
2807 // first page.
2808 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2809 FPDF_PAGE page = LoadPage(0);
2810 ASSERT_TRUE(page);
2811
2812 {
2813 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2814 ASSERT_TRUE(annot);
2815 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2816 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2817 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2818 }
2819
2820 UnloadPage(page);
2821}
2822
2823TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2824 // Open a file with text widget annotations and load its first page.
2825 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2826 FPDF_PAGE page = LoadPage(0);
2827 ASSERT_TRUE(page);
2828
2829 {
2830 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2831 ASSERT_TRUE(annot);
2832 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2833 }
2834
2835 UnloadPage(page);
2836}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002837
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002838TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2839 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002840 FPDF_PAGE page = LoadPage(0);
2841 ASSERT_TRUE(page);
2842
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002843 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2844
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002845 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002846 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002847 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002848 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002849 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002850
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002851 constexpr int kExpectedAnnotTypes[] = {-1,
2852 FPDF_FORMFIELD_COMBOBOX,
2853 FPDF_FORMFIELD_LISTBOX,
2854 FPDF_FORMFIELD_TEXTFIELD,
2855 FPDF_FORMFIELD_CHECKBOX,
2856 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002857
Lei Zhang4501a502020-05-18 16:52:59 +00002858 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002859 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002860 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002861 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002862 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2863 }
2864 UnloadPage(page);
2865}
2866
2867TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2868 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2869 FPDF_PAGE page = LoadPage(0);
2870 ASSERT_TRUE(page);
2871
2872 {
2873 EXPECT_EQ(0u,
2874 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2875
2876 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2877 ASSERT_TRUE(annot);
2878
2879 EXPECT_EQ(0u,
2880 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2881
2882 unsigned long length_bytes =
2883 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2884 ASSERT_EQ(2u, length_bytes);
2885 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2886 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2887 buf.data(), length_bytes));
2888 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2889 }
2890 {
2891 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2892 ASSERT_TRUE(annot);
2893
2894 unsigned long length_bytes =
2895 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2896 ASSERT_EQ(18u, length_bytes);
2897 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2898 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2899 buf.data(), length_bytes));
2900 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2901 }
2902 UnloadPage(page);
2903}
2904
2905TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2906 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2907 FPDF_PAGE page = LoadPage(0);
2908 ASSERT_TRUE(page);
2909
2910 {
2911 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2912 ASSERT_TRUE(annot);
2913
2914 unsigned long length_bytes =
2915 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2916 ASSERT_EQ(2u, length_bytes);
2917 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2918 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2919 buf.data(), length_bytes));
2920 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2921 }
2922 {
2923 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2924 ASSERT_TRUE(annot);
2925
2926 unsigned long length_bytes =
2927 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2928 ASSERT_EQ(14u, length_bytes);
2929 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2930 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2931 buf.data(), length_bytes));
2932 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2933 }
2934 UnloadPage(page);
2935}
2936
2937TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2938 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2939 FPDF_PAGE page = LoadPage(0);
2940 ASSERT_TRUE(page);
2941
2942 {
2943 EXPECT_EQ(0u,
2944 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2945
2946 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2947 ASSERT_TRUE(annot);
2948
2949 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2950
2951 unsigned long length_bytes =
2952 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2953 ASSERT_EQ(18u, length_bytes);
2954 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2955 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2956 buf.data(), length_bytes));
2957 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2958 }
2959 UnloadPage(page);
2960}
2961
2962TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2963 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2964 FPDF_PAGE page = LoadPage(0);
2965 ASSERT_TRUE(page);
2966
2967 {
2968 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2969 ASSERT_TRUE(annot);
2970
2971 unsigned long length_bytes =
2972 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2973 ASSERT_EQ(30u, length_bytes);
2974 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2975 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2976 buf.data(), length_bytes));
2977 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2978 }
2979 UnloadPage(page);
2980}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002981
Lei Zhang9b444002020-04-17 17:35:23 +00002982TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2983 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002984 FPDF_PAGE page = LoadPage(0);
2985 ASSERT_TRUE(page);
2986
Lei Zhang9b444002020-04-17 17:35:23 +00002987 // Verify widgets are by default focusable.
2988 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2989 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002990
Lei Zhang9b444002020-04-17 17:35:23 +00002991 // Expected annot subtypes for page 0 of annots.pdf.
2992 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2993 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2994 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2995 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2996 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002997
Lei Zhang9b444002020-04-17 17:35:23 +00002998 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002999 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00003000 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
3001 kExpectedAnnotSubtypes,
3002 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003003
Lei Zhang9b444002020-04-17 17:35:23 +00003004 // Make no annotation type focusable using the preferred method.
3005 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
3006 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003007
Lei Zhang9b444002020-04-17 17:35:23 +00003008 // Restore the focusable type count back to 1, then set it back to 0 using a
3009 // different method.
3010 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
3011 ASSERT_TRUE(
3012 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
3013 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003014
Lei Zhang9b444002020-04-17 17:35:23 +00003015 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00003016 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003017
Lei Zhang9b444002020-04-17 17:35:23 +00003018 // Now make links focusable.
3019 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
3020 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003021
Lei Zhang9b444002020-04-17 17:35:23 +00003022 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00003023 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00003024 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
3025 kExpectedAnnotSubtypes,
3026 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003027
Lei Zhang9b444002020-04-17 17:35:23 +00003028 // Test invalid parameters.
3029 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00003030 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00003031 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00003032 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003033 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
3034
Lei Zhang9b444002020-04-17 17:35:23 +00003035 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
3036 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
3037 subtypes.size()));
3038 EXPECT_FALSE(
3039 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
3040 EXPECT_FALSE(
3041 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003042
3043 UnloadPage(page);
3044}
Lei Zhang671aece2020-04-14 19:02:26 +00003045
Hui Yingstea3816d2020-07-28 22:35:11 +00003046TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00003047 ASSERT_TRUE(OpenDocument("annots.pdf"));
3048 FPDF_PAGE page = LoadPage(0);
3049 ASSERT_TRUE(page);
3050
3051 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003052#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3053 static const char kMd5sum[] = "4ca14c670396711194b40ecc2514969b";
3054#else
Lei Zhang671aece2020-04-14 19:02:26 +00003055#if defined(OS_WIN)
3056 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
Lei Zhang0c03d632020-07-30 17:05:36 +00003057#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003058 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
3059#else
3060 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
3061#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003062#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003063 // Check the initial rendering.
3064 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3065 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3066 }
3067
3068 // Make links and highlights focusable.
3069 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
3070 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00003071 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00003072 ASSERT_TRUE(
3073 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
3074 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3075 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
3076 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
3077 subtypes.size()));
3078 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
3079 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
3080
3081 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003082#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3083 static const char kMd5sum[] = "48cb60b3f9bc364c73582aff3418451e";
3084#else
Lei Zhang671aece2020-04-14 19:02:26 +00003085#if defined(OS_WIN)
3086 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
Lei Zhang0c03d632020-07-30 17:05:36 +00003087#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003088 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
3089#else
3090 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
3091#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003092#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003093 // Focus the first link and check the rendering.
3094 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3095 ASSERT_TRUE(annot);
3096 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3097 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3098 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3099 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3100 }
3101
3102 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003103#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3104 static const char kMd5sum[] = "d09869ff0a209daf179da2d7a58142b1";
3105#else
Lei Zhang671aece2020-04-14 19:02:26 +00003106#if defined(OS_WIN)
3107 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
Lei Zhang0c03d632020-07-30 17:05:36 +00003108#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003109 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
3110#else
3111 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
3112#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003113#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003114 // Focus the first highlight and check the rendering.
3115 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3116 ASSERT_TRUE(annot);
3117 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3118 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3119 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3120 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3121 }
3122
3123 UnloadPage(page);
3124}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003125
3126TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
3127 ASSERT_TRUE(OpenDocument("annots.pdf"));
3128 FPDF_PAGE page = LoadPage(0);
3129 ASSERT_TRUE(page);
3130 {
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003131 constexpr char kExpectedResult[] =
3132 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3133 "fpdf_text.h";
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003134
Lei Zhang306874b2021-04-16 00:33:36 +00003135 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3136 ASSERT_TRUE(annot);
3137 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3138 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()),
3139 kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003140 }
3141
3142 {
3143 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3144 ASSERT_TRUE(annot);
3145 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3146 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3147 }
3148
3149 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3150
3151 UnloadPage(page);
3152}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003153
3154TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3155 // Open a file with radio button widget annotations and load its first page.
3156 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3157 FPDF_PAGE page = LoadPage(0);
3158 ASSERT_TRUE(page);
3159
3160 {
3161 // Checks for bad annot.
3162 EXPECT_EQ(-1,
3163 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3164
3165 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3166 ASSERT_TRUE(annot);
3167
3168 // Checks for bad form handle.
3169 EXPECT_EQ(-1,
3170 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3171
3172 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3173 }
3174
3175 UnloadPage(page);
3176}
3177
3178TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3179 // Open a file with checkbox widget annotations and load its first page.
3180 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3181 FPDF_PAGE page = LoadPage(0);
3182 ASSERT_TRUE(page);
3183
3184 {
3185 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3186 ASSERT_TRUE(annot);
3187 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3188 }
3189
3190 UnloadPage(page);
3191}
3192
3193TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3194 // Open a file with ink annotations and load its first page.
3195 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3196 FPDF_PAGE page = LoadPage(0);
3197 ASSERT_TRUE(page);
3198
3199 {
3200 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3201 ASSERT_TRUE(annot);
3202 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3203 }
3204
3205 UnloadPage(page);
3206}
3207
3208TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3209 // Open a file with radio button widget annotations and load its first page.
3210 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3211 FPDF_PAGE page = LoadPage(0);
3212 ASSERT_TRUE(page);
3213
3214 {
3215 // Checks for bad annot.
3216 EXPECT_EQ(-1,
3217 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3218
3219 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3220 ASSERT_TRUE(annot);
3221
3222 // Checks for bad form handle.
3223 EXPECT_EQ(-1,
3224 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3225
3226 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3227 }
3228
3229 UnloadPage(page);
3230}
3231
3232TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3233 // Open a file with checkbox widget annotations and load its first page.
3234 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3235 FPDF_PAGE page = LoadPage(0);
3236 ASSERT_TRUE(page);
3237
3238 {
3239 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3240 ASSERT_TRUE(annot);
3241 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3242 }
3243
3244 UnloadPage(page);
3245}
3246
3247TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3248 // Open a file with ink annotations and load its first page.
3249 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3250 FPDF_PAGE page = LoadPage(0);
3251 ASSERT_TRUE(page);
3252
3253 {
3254 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3255 ASSERT_TRUE(annot);
3256 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3257 }
3258
3259 UnloadPage(page);
3260}
3261
3262TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3263 // Open a file with radio button widget annotations and load its first page.
3264 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3265 FPDF_PAGE page = LoadPage(0);
3266 ASSERT_TRUE(page);
3267
3268 {
3269 // Checks for bad annot.
3270 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3271 form_handle(), /*annot=*/nullptr,
3272 /*buffer=*/nullptr, /*buflen=*/0));
3273
3274 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3275 ASSERT_TRUE(annot);
3276
3277 // Checks for bad form handle.
3278 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3279 /*hHandle=*/nullptr, annot.get(),
3280 /*buffer=*/nullptr, /*buflen=*/0));
3281
3282 unsigned long length_bytes =
3283 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3284 /*buffer=*/nullptr, /*buflen=*/0);
3285 ASSERT_EQ(14u, length_bytes);
3286 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3287 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3288 buf.data(), length_bytes));
3289 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3290 }
3291
3292 UnloadPage(page);
3293}
3294
3295TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3296 // Open a file with checkbox widget annotations and load its first page.
3297 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3298 FPDF_PAGE page = LoadPage(0);
3299 ASSERT_TRUE(page);
3300
3301 {
3302 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3303 ASSERT_TRUE(annot);
3304
3305 unsigned long length_bytes =
3306 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3307 /*buffer=*/nullptr, /*buflen=*/0);
3308 ASSERT_EQ(8u, length_bytes);
3309 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3310 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3311 buf.data(), length_bytes));
3312 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3313 }
3314
3315 UnloadPage(page);
3316}
3317
3318TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3319 // Open a file with ink annotations and load its first page.
3320 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3321 FPDF_PAGE page = LoadPage(0);
3322 ASSERT_TRUE(page);
3323
3324 {
3325 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3326 ASSERT_TRUE(annot);
3327 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3328 /*buffer=*/nullptr,
3329 /*buflen=*/0));
3330 }
3331
3332 UnloadPage(page);
3333}
Miklos Vajnad1a24fb2020-10-26 18:07:13 +00003334
3335TEST_F(FPDFAnnotEmbedderTest, Redactannotation) {
3336 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3337 FPDF_PAGE page = LoadPage(0);
3338 ASSERT_TRUE(page);
3339 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3340
3341 {
3342 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3343 ASSERT_TRUE(annot);
3344 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3345 }
3346
3347 UnloadPage(page);
3348}
Miklos Vajna09ecef62020-11-10 21:50:38 +00003349
3350TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) {
3351 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3352 FPDF_PAGE page = LoadPage(0);
3353 ASSERT_TRUE(page);
3354 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3355
3356 {
3357 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3358 ASSERT_TRUE(annot);
3359
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003360 // FPDFAnnot_GetVertices() positive testing.
Miklos Vajna09ecef62020-11-10 21:50:38 +00003361 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3362 const size_t kExpectedSize = 3;
3363 ASSERT_EQ(kExpectedSize, size);
3364 std::vector<FS_POINTF> vertices_buffer(size);
3365 EXPECT_EQ(size,
3366 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3367 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3368 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3369 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3370 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3371 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3372 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3373
3374 // FPDFAnnot_GetVertices() negative testing.
3375 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3376
3377 // vertices_buffer is not overwritten if it is too small.
3378 vertices_buffer.resize(1);
3379 vertices_buffer[0].x = 42;
3380 vertices_buffer[0].y = 43;
3381 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3382 vertices_buffer.size());
3383 EXPECT_EQ(kExpectedSize, size);
3384 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3385 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3386 }
3387
3388 {
3389 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3390 ASSERT_TRUE(annot);
3391
3392 // This has an odd number of elements in the vertices array, ignore the last
3393 // element.
3394 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3395 const size_t kExpectedSize = 3;
3396 ASSERT_EQ(kExpectedSize, size);
3397 std::vector<FS_POINTF> vertices_buffer(size);
3398 EXPECT_EQ(size,
3399 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3400 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3401 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3402 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3403 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3404 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3405 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3406 }
3407
3408 {
3409 // Wrong annotation type.
3410 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3411 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3412 }
3413
3414 UnloadPage(page);
3415}
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003416
3417TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) {
3418 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3419 FPDF_PAGE page = LoadPage(0);
3420 ASSERT_TRUE(page);
3421 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3422
3423 {
3424 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3425 ASSERT_TRUE(annot);
3426
3427 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3428 // testing.
3429 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3430 const size_t kExpectedSize = 1;
3431 ASSERT_EQ(kExpectedSize, size);
3432 const unsigned long kPathIndex = 0;
3433 unsigned long path_size =
3434 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3435 const size_t kExpectedPathSize = 3;
3436 ASSERT_EQ(kExpectedPathSize, path_size);
3437 std::vector<FS_POINTF> path_buffer(path_size);
3438 EXPECT_EQ(path_size,
3439 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3440 path_buffer.data(), path_size));
3441 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3442 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3443 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3444 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3445 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3446 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3447
3448 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3449 // testing.
3450 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3451 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3452
3453 // out of bounds path_index.
3454 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3455
3456 // path_buffer is not overwritten if it is too small.
3457 path_buffer.resize(1);
3458 path_buffer[0].x = 42;
3459 path_buffer[0].y = 43;
3460 path_size = FPDFAnnot_GetInkListPath(
3461 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3462 EXPECT_EQ(kExpectedSize, size);
3463 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3464 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3465 }
3466
3467 {
3468 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3469 ASSERT_TRUE(annot);
3470
3471 // This has an odd number of elements in the path array, ignore the last
3472 // element.
3473 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3474 const size_t kExpectedSize = 1;
3475 ASSERT_EQ(kExpectedSize, size);
3476 const unsigned long kPathIndex = 0;
3477 unsigned long path_size =
3478 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3479 const size_t kExpectedPathSize = 3;
3480 ASSERT_EQ(kExpectedPathSize, path_size);
3481 std::vector<FS_POINTF> path_buffer(path_size);
3482 EXPECT_EQ(path_size,
3483 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3484 path_buffer.data(), path_size));
3485 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3486 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3487 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3488 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3489 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3490 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3491 }
3492
3493 {
3494 // Wrong annotation type.
3495 ScopedFPDFAnnotation polygon_annot(
3496 FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON));
3497 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3498 const unsigned long kPathIndex = 0;
3499 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3500 nullptr, 0));
3501 }
3502
3503 UnloadPage(page);
3504}
Miklos Vajna30f45a62020-12-04 19:12:31 +00003505
3506TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) {
3507 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3508 FPDF_PAGE page = LoadPage(0);
3509 ASSERT_TRUE(page);
3510 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3511
3512 {
3513 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3514 ASSERT_TRUE(annot);
3515
3516 // FPDFAnnot_GetVertices() positive testing.
3517 FS_POINTF start;
3518 FS_POINTF end;
3519 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3520 EXPECT_FLOAT_EQ(159.0f, start.x);
3521 EXPECT_FLOAT_EQ(296.0f, start.y);
3522 EXPECT_FLOAT_EQ(472.0f, end.x);
3523 EXPECT_FLOAT_EQ(243.42f, end.y);
3524
3525 // FPDFAnnot_GetVertices() negative testing.
3526 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3527 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3528 }
3529
3530 {
3531 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3532 ASSERT_TRUE(annot);
3533
3534 // Too few elements in the line array.
3535 FS_POINTF start;
3536 FS_POINTF end;
3537 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3538 }
3539
3540 {
3541 // Wrong annotation type.
3542 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3543 FS_POINTF start;
3544 FS_POINTF end;
3545 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3546 }
3547
3548 UnloadPage(page);
3549}
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003550
3551TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) {
3552 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3553 FPDF_PAGE page = LoadPage(0);
3554 ASSERT_TRUE(page);
3555 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3556
3557 {
3558 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3559 ASSERT_TRUE(annot);
3560
3561 // FPDFAnnot_GetBorder() positive testing.
3562 float horizontal_radius;
3563 float vertical_radius;
3564 float border_width;
3565 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3566 &vertical_radius, &border_width));
3567 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3568 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3569 EXPECT_FLOAT_EQ(2.0f, border_width);
3570
3571 // FPDFAnnot_GetBorder() negative testing.
3572 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3573 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3574 }
3575
3576 {
3577 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3578 ASSERT_TRUE(annot);
3579
3580 // Too few elements in the border array.
3581 float horizontal_radius;
3582 float vertical_radius;
3583 float border_width;
3584 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3585 &vertical_radius, &border_width));
Lei Zhang21ea6652021-04-15 23:24:46 +00003586
3587 // FPDFAnnot_SetBorder() positive testing.
3588 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3589 /*vertical_radius=*/3.5f,
3590 /*border_width=*/4.0f));
3591
3592 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3593 &vertical_radius, &border_width));
3594 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3595 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3596 EXPECT_FLOAT_EQ(4.0f, border_width);
3597
3598 // FPDFAnnot_SetBorder() negative testing.
3599 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3600 /*vertical_radius=*/2.5f,
3601 /*border_width=*/3.0f));
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003602 }
3603
3604 UnloadPage(page);
3605}
Lei Zhang24de1492021-04-19 18:06:43 +00003606
3607// Due to https://crbug.com/pdfium/570, the AnnotationBorder test above cannot
3608// actually render the line annotations inside line_annot.pdf. For now, use a
3609// square annotation in annots.pdf for testing.
3610TEST_F(FPDFAnnotEmbedderTest, AnnotationBorderRendering) {
3611 ASSERT_TRUE(OpenDocument("annots.pdf"));
3612 FPDF_PAGE page = LoadPage(1);
3613 ASSERT_TRUE(page);
3614 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
3615
3616#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3617 constexpr char kOriginalChecksum[] = "bc9ba381d6d46ff93ed0b5288b763b60";
3618 constexpr char kModifiedChecksum[] = "0f3e10336f67cb6a8731a06d26d01e50";
3619#elif defined(OS_WIN)
Hui Yingst282f0b82021-08-30 22:59:52 +00003620 constexpr char kOriginalChecksum[] = "768b9edd280a535b4223e57bf17940e3";
3621 constexpr char kModifiedChecksum[] = "ab367296994b5dc1a178820626601ffc";
Lei Zhang24de1492021-04-19 18:06:43 +00003622#elif defined(OS_APPLE)
3623 constexpr char kOriginalChecksum[] = "1839f5df5fb4fae10cf3793568e73ede";
3624 constexpr char kModifiedChecksum[] = "abd4f5d1c3b8d8cfc572b389e589da5a";
3625#else
3626 constexpr char kOriginalChecksum[] = "ccf6667b34ec2452bea0b5f1a0194191";
3627 constexpr char kModifiedChecksum[] = "1bbdb473d0757843e82053b0bd3298bc";
3628#endif
3629
3630 {
3631 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
3632 ASSERT_TRUE(annot);
3633 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
3634
3635 {
3636 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3637 CompareBitmap(bitmap.get(), 612, 792, kOriginalChecksum);
3638 }
3639
3640 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3641 /*vertical_radius=*/3.5f,
3642 /*border_width=*/4.0f));
3643
3644 {
3645 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3646 CompareBitmap(bitmap.get(), 612, 792, kModifiedChecksum);
3647 }
3648 }
3649
3650 // Save the document and close the page.
3651 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
3652 UnloadPage(page);
3653
3654 ASSERT_TRUE(OpenSavedDocument());
3655 page = LoadSavedPage(1);
3656 ASSERT_TRUE(page);
3657 VerifySavedRendering(page, 612, 792, kModifiedChecksum);
3658
3659 CloseSavedPage(page);
3660 CloseSavedDocument();
3661}