blob: 65bd92c2b9485779b3bde9a17cfeb235c9d9e634 [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>
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000010#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -040011#include <memory>
12#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -040013#include <vector>
14
Lei Zhange4cdac52019-04-30 16:45:57 +000015#include "build/build_config.h"
Lei Zhanga5c1daf2019-01-31 21:56:47 +000016#include "constants/annotation_common.h"
Tom Sepez204ab052020-06-12 21:33:48 +000017#include "core/fpdfapi/page/cpdf_annotcontext.h"
18#include "core/fpdfapi/page/cpdf_pagemodule.h"
19#include "core/fpdfapi/parser/cpdf_array.h"
20#include "core/fpdfapi/parser/cpdf_dictionary.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040021#include "core/fxcrt/fx_system.h"
Tom Sepez204ab052020-06-12 21:33:48 +000022#include "fpdfsdk/cpdfsdk_helpers.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000023#include "public/cpp/fpdf_scopers.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040024#include "public/fpdf_edit.h"
Mansi Awasthi07bf7e62020-01-24 10:34:17 +000025#include "public/fpdf_formfill.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040026#include "public/fpdfview.h"
27#include "testing/embedder_test.h"
Hui Yingstb4baceb2020-04-28 23:46:10 +000028#include "testing/embedder_test_constants.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000029#include "testing/fx_string_testhelpers.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000030#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040031#include "testing/gtest/include/gtest/gtest.h"
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000032#include "testing/utils/hash.h"
Lei Zhang87e3d7a2021-06-17 19:40:28 +000033#include "third_party/base/containers/contains.h"
Lei Zhang532886d2021-06-17 19:10:08 +000034#include "third_party/base/cxx17_backports.h"
Lei Zhang9b444002020-04-17 17:35:23 +000035#include "third_party/base/span.h"
36
Hui Yingst30bfcc52020-07-27 23:54:40 +000037using pdfium::kAnnotationStampWithApChecksum;
38
Lei Zhang9b444002020-04-17 17:35:23 +000039namespace {
40
Tom Sepez204ab052020-06-12 21:33:48 +000041const wchar_t kStreamData[] =
42 L"/GS gs 0.0 0.0 0.0 RG 4 w 211.8 747.6 m 211.8 744.8 "
43 L"212.6 743.0 214.2 740.8 "
44 L"c 215.4 739.0 216.8 737.1 218.9 736.1 c 220.8 735.1 221.4 733.0 "
45 L"223.7 732.4 c 232.6 729.9 242.0 730.8 251.2 730.8 c 257.5 730.8 "
46 L"263.0 732.9 269.0 734.4 c S";
47
Lei Zhang9b444002020-04-17 17:35:23 +000048void VerifyFocusableAnnotSubtypes(
49 FPDF_FORMHANDLE form_handle,
50 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) {
51 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
52 FPDFAnnot_GetFocusableSubtypesCount(form_handle));
53
54 std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes(
55 expected_subtypes.size());
56 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(
57 form_handle, actual_subtypes.data(), actual_subtypes.size()));
58 for (size_t i = 0; i < expected_subtypes.size(); ++i)
59 ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]);
60}
61
62void SetAndVerifyFocusableAnnotSubtypes(
63 FPDF_FORMHANDLE form_handle,
64 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) {
65 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(),
66 subtypes.size()));
67 VerifyFocusableAnnotSubtypes(form_handle, subtypes);
68}
69
70void VerifyAnnotationSubtypesAndFocusability(
71 FPDF_FORMHANDLE form_handle,
72 FPDF_PAGE page,
73 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes,
74 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) {
75 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
76 FPDFPage_GetAnnotCount(page));
77 for (size_t i = 0; i < expected_subtypes.size(); ++i) {
78 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
79 ASSERT_TRUE(annot);
80 EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get()));
81
Lei Zhangf245ae62020-05-15 21:49:46 +000082 bool expected_focusable =
83 pdfium::Contains(expected_focusable_subtypes, expected_subtypes[i]);
Lei Zhang9b444002020-04-17 17:35:23 +000084 EXPECT_EQ(expected_focusable,
85 FORM_SetFocusedAnnot(form_handle, annot.get()));
86
87 // Kill the focus so the next test starts in an unfocused state.
88 FORM_ForceToKillFocus(form_handle);
89 }
90}
91
Lei Zhang306874b2021-04-16 00:33:36 +000092void VerifyUriActionInLink(FPDF_DOCUMENT doc,
93 FPDF_LINK link,
94 const std::string& expected_uri) {
95 ASSERT_TRUE(link);
96
97 FPDF_ACTION action = FPDFLink_GetAction(link);
98 ASSERT_TRUE(action);
99 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
100 FPDFAction_GetType(action));
101
102 unsigned long bufsize = FPDFAction_GetURIPath(doc, action, nullptr, 0);
103 ASSERT_EQ(expected_uri.size() + 1, bufsize);
104
105 std::vector<char> buffer(bufsize);
106 EXPECT_EQ(bufsize,
107 FPDFAction_GetURIPath(doc, action, buffer.data(), bufsize));
108 EXPECT_STREQ(expected_uri.c_str(), buffer.data());
109}
110
Lei Zhang9b444002020-04-17 17:35:23 +0000111} // namespace
Jane Liu4fd9a472017-06-01 18:56:09 -0400112
Lei Zhangab41f252018-12-23 03:10:50 +0000113class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -0400114
Tom Sepez204ab052020-06-12 21:33:48 +0000115TEST_F(FPDFAnnotEmbedderTest, SetAP) {
116 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
117 ASSERT_TRUE(doc);
118 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
119 ASSERT_TRUE(page);
120 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
121 ASSERT_TRUE(ap_stream);
122
123 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
124 ASSERT_TRUE(annot);
125
126 // Negative case: FPDFAnnot_SetAP() should fail if bounding rect is not yet
127 // set on the annotation.
128 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
129 ap_stream.get()));
130
131 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
132 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
133
134 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
135 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/255));
136
137 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
138 ap_stream.get()));
139
140 // Verify that appearance stream is created as form XObject
141 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
142 ASSERT_TRUE(context);
143 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
144 ASSERT_TRUE(annot_dict);
145 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
146 ASSERT_TRUE(ap_dict);
147 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
148 ASSERT_TRUE(stream_dict);
149 // Check for non-existence of resources dictionary in case of opaque color
150 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
151 ASSERT_FALSE(resources_dict);
152 ByteString type = stream_dict->GetStringFor(pdfium::annotation::kType);
153 EXPECT_EQ("XObject", type);
154 ByteString sub_type = stream_dict->GetStringFor(pdfium::annotation::kSubtype);
155 EXPECT_EQ("Form", sub_type);
156
157 // Check that the appearance stream is same as we just set.
158 const uint32_t kStreamDataSize =
159 pdfium::size(kStreamData) * sizeof(FPDF_WCHAR);
160 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
161 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
162 ASSERT_EQ(kStreamDataSize, normal_length_bytes);
163 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
164 EXPECT_EQ(kStreamDataSize,
165 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
166 buf.data(), normal_length_bytes));
167 EXPECT_EQ(kStreamData, GetPlatformWString(buf.data()));
168}
169
170TEST_F(FPDFAnnotEmbedderTest, SetAPWithOpacity) {
171 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
172 ASSERT_TRUE(doc);
173 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
174 ASSERT_TRUE(page);
175 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
176 ASSERT_TRUE(ap_stream);
177
178 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
179 ASSERT_TRUE(annot);
180
181 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
182 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/102));
183
184 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
185 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
186
187 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
188 ap_stream.get()));
189
190 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
191 ASSERT_TRUE(context);
192 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
193 ASSERT_TRUE(annot_dict);
194 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
195 ASSERT_TRUE(ap_dict);
196 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
197 ASSERT_TRUE(stream_dict);
198 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
199 ASSERT_TRUE(stream_dict);
200 CPDF_Dictionary* extGState_dict = resources_dict->GetDictFor("ExtGState");
201 ASSERT_TRUE(extGState_dict);
202 CPDF_Dictionary* gs_dict = extGState_dict->GetDictFor("GS");
203 ASSERT_TRUE(gs_dict);
204 ByteString type = gs_dict->GetStringFor(pdfium::annotation::kType);
205 EXPECT_EQ("ExtGState", type);
206 float opacity = gs_dict->GetNumberFor("CA");
207 // Opacity value of 102 is represented as 0.4f (=104/255) in /CA entry.
208 EXPECT_FLOAT_EQ(0.4f, opacity);
209 ByteString blend_mode = gs_dict->GetStringFor("BM");
210 EXPECT_EQ("Normal", blend_mode);
211 bool alpha_source_flag = gs_dict->GetBooleanFor("AIS", true);
212 EXPECT_FALSE(alpha_source_flag);
213}
214
215TEST_F(FPDFAnnotEmbedderTest, InkListAPIValidations) {
216 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
217 ASSERT_TRUE(doc);
218 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
219 ASSERT_TRUE(page);
220
221 // Create a new ink annotation.
222 ScopedFPDFAnnotation ink_annot(
223 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
224 ASSERT_TRUE(ink_annot);
225 CPDF_AnnotContext* context =
226 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
227 ASSERT_TRUE(context);
228 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
229 ASSERT_TRUE(annot_dict);
230
231 static constexpr FS_POINTF kFirstInkStroke[] = {
232 {80.0f, 90.0f}, {81.0f, 91.0f}, {82.0f, 92.0f},
233 {83.0f, 93.0f}, {84.0f, 94.0f}, {85.0f, 95.0f}};
234 static constexpr size_t kFirstStrokePointCount =
235 pdfium::size(kFirstInkStroke);
236
237 static constexpr FS_POINTF kSecondInkStroke[] = {
238 {70.0f, 90.0f}, {71.0f, 91.0f}, {72.0f, 92.0f}};
239 static constexpr size_t kSecondStrokePointCount =
240 pdfium::size(kSecondInkStroke);
241
242 static constexpr FS_POINTF kThirdInkStroke[] = {{60.0f, 90.0f},
243 {61.0f, 91.0f},
244 {62.0f, 92.0f},
245 {63.0f, 93.0f},
246 {64.0f, 94.0f}};
247 static constexpr size_t kThirdStrokePointCount =
248 pdfium::size(kThirdInkStroke);
249
250 // Negative test: |annot| is passed as nullptr.
251 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(nullptr, kFirstInkStroke,
252 kFirstStrokePointCount));
253
254 // Negative test: |annot| is not ink annotation.
255 // Create a new highlight annotation.
256 ScopedFPDFAnnotation highlight_annot(
257 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
258 ASSERT_TRUE(highlight_annot);
259 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(highlight_annot.get(), kFirstInkStroke,
260 kFirstStrokePointCount));
261
262 // Negative test: passing |point_count| as 0.
263 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke, 0));
264
265 // Negative test: passing |points| array as nullptr.
266 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), nullptr,
267 kFirstStrokePointCount));
268
269 // Negative test: passing |point_count| more than ULONG_MAX/2.
270 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
271 ULONG_MAX / 2 + 1));
272
273 // InkStroke should get added to ink annotation. Also inklist should get
274 // created.
275 EXPECT_EQ(0, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke,
276 kFirstStrokePointCount));
277
278 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
279 ASSERT_TRUE(inklist);
280 EXPECT_EQ(1u, inklist->size());
281 EXPECT_EQ(kFirstStrokePointCount * 2, inklist->GetArrayAt(0)->size());
282
283 // Adding another inkStroke to ink annotation with all valid paremeters.
284 // InkList already exists in ink_annot.
285 EXPECT_EQ(1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
286 kSecondStrokePointCount));
287 EXPECT_EQ(2u, inklist->size());
288 EXPECT_EQ(kSecondStrokePointCount * 2, inklist->GetArrayAt(1)->size());
289
290 // Adding one more InkStroke to the ink annotation. |point_count| passed is
291 // less than the data available in |buffer|.
292 EXPECT_EQ(2, FPDFAnnot_AddInkStroke(ink_annot.get(), kThirdInkStroke,
293 kThirdStrokePointCount - 1));
294 EXPECT_EQ(3u, inklist->size());
295 EXPECT_EQ((kThirdStrokePointCount - 1) * 2, inklist->GetArrayAt(2)->size());
296}
297
298TEST_F(FPDFAnnotEmbedderTest, RemoveInkList) {
299 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
300 ASSERT_TRUE(doc);
301 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
302 ASSERT_TRUE(page);
303
304 // Negative test: |annot| is passed as nullptr.
305 EXPECT_FALSE(FPDFAnnot_RemoveInkList(nullptr));
306
307 // Negative test: |annot| is not ink annotation.
308 // Create a new highlight annotation.
309 ScopedFPDFAnnotation highlight_annot(
310 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
311 ASSERT_TRUE(highlight_annot);
312 EXPECT_FALSE(FPDFAnnot_RemoveInkList(highlight_annot.get()));
313
314 // Create a new ink annotation.
315 ScopedFPDFAnnotation ink_annot(
316 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
317 ASSERT_TRUE(ink_annot);
318 CPDF_AnnotContext* context =
319 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
320 ASSERT_TRUE(context);
321 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
322 ASSERT_TRUE(annot_dict);
323
324 static constexpr FS_POINTF kInkStroke[] = {{80.0f, 90.0f}, {81.0f, 91.0f},
325 {82.0f, 92.0f}, {83.0f, 93.0f},
326 {84.0f, 94.0f}, {85.0f, 95.0f}};
327 static constexpr size_t kPointCount = pdfium::size(kInkStroke);
328
329 // InkStroke should get added to ink annotation. Also inklist should get
330 // created.
331 EXPECT_EQ(0,
332 FPDFAnnot_AddInkStroke(ink_annot.get(), kInkStroke, kPointCount));
333
334 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
335 ASSERT_TRUE(inklist);
336 ASSERT_EQ(1u, inklist->size());
337 EXPECT_EQ(kPointCount * 2, inklist->GetArrayAt(0)->size());
338
339 // Remove inklist.
340 EXPECT_TRUE(FPDFAnnot_RemoveInkList(ink_annot.get()));
341 EXPECT_FALSE(annot_dict->KeyExist("InkList"));
342}
343
Lei Zhangab41f252018-12-23 03:10:50 +0000344TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +0000345 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
346 FPDF_PAGE page = LoadPage(0);
347 ASSERT_TRUE(page);
348
349 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
350
351 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
352 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
353 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
354 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
355 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
356
357 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
358
359 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
360
361 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
362 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
363 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
364
365 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
366
Lei Zhang4f556b82019-04-08 16:32:41 +0000367 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +0000368 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +0000369 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
370
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000371 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +0000372 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
373 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
374 EXPECT_EQ(0u,
375 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
376
377 UnloadPage(page);
378}
379
Lei Zhang3d9a0972019-03-04 19:34:09 +0000380TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
381 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
382 FPDF_PAGE page = LoadPage(0);
383 ASSERT_TRUE(page);
384
385 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +0000386 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +0000387
388 UnloadPage(page);
389}
390
Lei Zhangab41f252018-12-23 03:10:50 +0000391TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -0400392 // Open a file with one annotation and load its first page.
393 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000394 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -0400395 ASSERT_TRUE(page);
396
397 // This annotation has a malformed appearance stream, which does not have its
398 // normal appearance defined, only its rollover appearance. In this case, its
399 // normal appearance should be generated, allowing the highlight annotation to
400 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000401 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000402 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -0400403
404 UnloadPage(page);
405}
406
Hui Yingstd5b6f632020-07-22 01:31:59 +0000407TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang03e5e682019-09-16 19:45:55 +0000408#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstd5b6f632020-07-22 01:31:59 +0000409 static const char kChecksum[] = "ec1f4ccbd0aecfdea6d53893387a0101";
Lei Zhang03e5e682019-09-16 19:45:55 +0000410#else
Hui Yingstd5b6f632020-07-22 01:31:59 +0000411 static const char kChecksum[] = "76512832d88017668d9acc7aacd13dae";
Lei Zhang03e5e682019-09-16 19:45:55 +0000412#endif
Henrique Nakashima5098b252018-03-26 21:46:00 +0000413 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000414 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
415 FPDF_PAGE page = LoadPage(0);
416 ASSERT_TRUE(page);
417
Tom Sepeze08d2b12018-04-25 18:49:32 +0000418 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstd5b6f632020-07-22 01:31:59 +0000419 CompareBitmap(bitmap.get(), 595, 842, kChecksum);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000420
421 UnloadPage(page);
422}
423
Lei Zhangab41f252018-12-23 03:10:50 +0000424TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400425 // Open a file with one annotation and load its first page.
426 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000427 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400428 ASSERT_TRUE(page);
429
430 // Check that there is a total of 1 annotation on its first page.
431 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
432
433 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000434 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000435 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000436 ASSERT_TRUE(annot);
437 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400438
Lei Zhanga21d5932018-02-05 18:28:38 +0000439 // Check that the annotation color is yellow.
440 unsigned int R;
441 unsigned int G;
442 unsigned int B;
443 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000444 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000445 &G, &B, &A));
446 EXPECT_EQ(255u, R);
447 EXPECT_EQ(255u, G);
448 EXPECT_EQ(0u, B);
449 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400450
Lei Zhanga21d5932018-02-05 18:28:38 +0000451 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000452 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000453 EXPECT_EQ(FPDF_OBJECT_STRING,
454 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000455 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000456 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000457 ASSERT_EQ(28u, length_bytes);
458 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000459 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000460 length_bytes));
461 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400462
Lei Zhanga21d5932018-02-05 18:28:38 +0000463 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000464 EXPECT_EQ(
465 FPDF_OBJECT_STRING,
466 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000467 length_bytes = FPDFAnnot_GetStringValue(
468 annot.get(), pdfium::annotation::kContents, nullptr, 0);
469 ASSERT_EQ(2690u, length_bytes);
470 buf = GetFPDFWideStringBuffer(length_bytes);
471 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
472 pdfium::annotation::kContents,
473 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000474 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000475 L"This is a note for that highlight annotation. Very long highlight "
476 "annotation. Long long long Long 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 longLong long longLong long longLong long longLong long "
493 "longLong long longLong long longLong long longLong long longLong long "
494 "longLong long long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000495 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400496
Lei Zhanga21d5932018-02-05 18:28:38 +0000497 // Check that the quadpoints are correct.
498 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000499 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000500 EXPECT_EQ(115.802643f, quadpoints.x1);
501 EXPECT_EQ(718.913940f, quadpoints.y1);
502 EXPECT_EQ(157.211182f, quadpoints.x4);
503 EXPECT_EQ(706.264465f, quadpoints.y4);
504 }
Tom Sepez507d0192018-11-07 16:37:51 +0000505 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400506}
507
Hui Yingst9b6b1542020-07-27 16:11:12 +0000508TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400509 // Open a file with three annotations and load its first page.
510 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000511 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400512 ASSERT_TRUE(page);
513
514 // Check that there is a total of 3 annotation on its first page.
515 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
516
Lei Zhanga21d5932018-02-05 18:28:38 +0000517 {
518 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000519 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000520 ASSERT_TRUE(annot);
521 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400522
Lei Zhanga21d5932018-02-05 18:28:38 +0000523 // Check that the annotation color is blue with opacity.
524 unsigned int R;
525 unsigned int G;
526 unsigned int B;
527 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000528 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000529 &G, &B, &A));
530 EXPECT_EQ(0u, R);
531 EXPECT_EQ(0u, G);
532 EXPECT_EQ(255u, B);
533 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400534
Lei Zhanga21d5932018-02-05 18:28:38 +0000535 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000536 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
537 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400538
Lei Zhang4f556b82019-04-08 16:32:41 +0000539 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000540 // Note that upon rendering, the rectangle coordinates will be adjusted.
541 FS_RECTF rect;
542 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
543 EXPECT_EQ(351.820404f, rect.left);
544 EXPECT_EQ(583.830688f, rect.bottom);
545 EXPECT_EQ(475.336090f, rect.right);
546 EXPECT_EQ(681.535034f, rect.top);
547 }
Tom Sepezef43c262018-11-07 16:41:32 +0000548 {
Hui Yingst57daee82020-10-09 05:47:20 +0000549#if defined(_SKIA_SUPPORT_) && defined(OS_APPLE)
550 static constexpr char kExpectedHash[] = "fad91b9c968fe8019a774f5e2419b8fc";
551#elif defined(_SKIA_SUPPORT_PATHS_) && defined(OS_APPLE)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000552 static constexpr char kExpectedHash[] = "acddfe688a117ead56af7b249a2cf8a1";
Hui Yingst57daee82020-10-09 05:47:20 +0000553#elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000554 static constexpr char kExpectedHash[] = "1fb0dd8dd5f0b9bb8d076e48eb59296d";
Hui Yingst57daee82020-10-09 05:47:20 +0000555#elif defined(OS_WIN)
Lei Zhang430b5322020-07-06 22:23:36 +0000556 static constexpr char kExpectedHash[] = "49d0a81c636531a337429325273d0508";
557#else
558 static constexpr char kExpectedHash[] = "354002e1c4386d38fdde29ef8d61074a";
Hui Yingst57daee82020-10-09 05:47:20 +0000559#endif
Tom Sepezef43c262018-11-07 16:41:32 +0000560 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang430b5322020-07-06 22:23:36 +0000561 CompareBitmap(bitmap.get(), 612, 792, kExpectedHash);
Tom Sepezef43c262018-11-07 16:41:32 +0000562 }
Tom Sepez507d0192018-11-07 16:37:51 +0000563 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400564}
Jane Liu20eafda2017-06-07 10:33:24 -0400565
Lei Zhangab41f252018-12-23 03:10:50 +0000566TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400567 // Open a file with one annotation and load its first page.
568 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000569 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400570 ASSERT_TRUE(page);
571
572 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400573 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400574
575 UnloadPage(page);
576}
577
Lei Zhangab41f252018-12-23 03:10:50 +0000578TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400579 // Open a file with no annotation and load its first page.
580 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000581 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400582 ASSERT_TRUE(page);
583 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
584
Lei Zhanga21d5932018-02-05 18:28:38 +0000585 {
586 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000587 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000588 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400589
Lei Zhanga21d5932018-02-05 18:28:38 +0000590 // Check that there is now 1 annotations on this page.
591 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400592
Lei Zhanga21d5932018-02-05 18:28:38 +0000593 // Check that the subtype of the annotation is correct.
594 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
595 }
Jane Liue10509a2017-06-20 16:47:41 -0400596
Lei Zhanga21d5932018-02-05 18:28:38 +0000597 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000598 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000599 ASSERT_TRUE(annot);
600 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400601
Lei Zhanga21d5932018-02-05 18:28:38 +0000602 // Set the color of the annotation.
603 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
604 102, 153, 204));
605 // Check that the color has been set correctly.
606 unsigned int R;
607 unsigned int G;
608 unsigned int B;
609 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000610 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000611 &G, &B, &A));
612 EXPECT_EQ(51u, R);
613 EXPECT_EQ(102u, G);
614 EXPECT_EQ(153u, B);
615 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400616
Lei Zhanga21d5932018-02-05 18:28:38 +0000617 // Change the color of the annotation.
618 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
619 153, 102, 51));
620 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000621 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000622 &G, &B, &A));
623 EXPECT_EQ(204u, R);
624 EXPECT_EQ(153u, G);
625 EXPECT_EQ(102u, B);
626 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400627
Lei Zhanga21d5932018-02-05 18:28:38 +0000628 // Set the annotation rectangle.
629 FS_RECTF rect;
630 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
631 EXPECT_EQ(0.f, rect.left);
632 EXPECT_EQ(0.f, rect.right);
633 rect.left = 35;
634 rect.bottom = 150;
635 rect.right = 53;
636 rect.top = 165;
637 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
638 // Check that the annotation rectangle has been set correctly.
639 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
640 EXPECT_EQ(35.f, rect.left);
641 EXPECT_EQ(150.f, rect.bottom);
642 EXPECT_EQ(53.f, rect.right);
643 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400644
Lei Zhanga21d5932018-02-05 18:28:38 +0000645 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000646 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000647 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000648 ASSERT_TRUE(FPDFAnnot_SetStringValue(
649 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000650 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000651 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000652 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000653 ASSERT_EQ(74u, length_bytes);
654 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
655 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
656 pdfium::annotation::kContents,
657 buf.data(), length_bytes));
658 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000659 }
Jane Liu20eafda2017-06-07 10:33:24 -0400660 UnloadPage(page);
661}
662
Lei Zhang81395aa2021-04-16 00:40:46 +0000663TEST_F(FPDFAnnotEmbedderTest, AddAndSaveLinkAnnotation) {
664 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
665 FPDF_PAGE page = LoadPage(0);
666 ASSERT_TRUE(page);
667 {
668 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
669 CompareBitmap(bitmap.get(), 200, 200, pdfium::kHelloWorldChecksum);
670 }
671 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
672
673 constexpr char kUri[] = "https://pdfium.org/";
674
675 {
676 // Add a link annotation to the page and set its URI.
677 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_LINK));
678 ASSERT_TRUE(annot);
679 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
680 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
681 EXPECT_TRUE(FPDFAnnot_SetURI(annot.get(), kUri));
682 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
683
684 // Negative tests:
685 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, nullptr));
686 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
687 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), nullptr));
688 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
689 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, kUri));
690 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
691
692 // Position the link on top of "Hello, world!" without a border.
693 const FS_RECTF kRect = {19.0f, 48.0f, 85.0f, 60.0f};
694 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &kRect));
695 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/0.0f,
696 /*vertical_radius=*/0.0f,
697 /*border_width=*/0.0f));
698
699 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
700 kUri);
701 }
702
703 {
704 // Add an ink annotation to the page. Trying to add a link to it fails.
705 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
706 ASSERT_TRUE(annot);
707 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
708 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
709 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), kUri));
710 }
711
712 // Remove the ink annotation added above for negative testing.
713 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
714 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
715
716 // Save the document, closing the page.
717 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
718 UnloadPage(page);
719
720 // Reopen the document and make sure it still renders the same. Since the link
721 // does not have a border, it does not affect the rendering.
722 ASSERT_TRUE(OpenSavedDocument());
723 page = LoadSavedPage(0);
724 ASSERT_TRUE(page);
725 VerifySavedRendering(page, 200, 200, pdfium::kHelloWorldChecksum);
726 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
727
728 {
729 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
730 ASSERT_TRUE(annot);
731 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
732 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
733 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
734 kUri);
735 }
736
737 CloseSavedPage(page);
738 CloseSavedDocument();
739}
740
Hui Yingstb3490322020-07-22 00:53:29 +0000741TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400742 // Open a file with one annotation and load its first page.
743 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000744 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400745 ASSERT_TRUE(page);
746
747 // Check that there is a total of one annotation on its first page, and verify
748 // its quadpoints.
749 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400750 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000751 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000752 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000753 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000754 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000755 EXPECT_EQ(115.802643f, quadpoints.x1);
756 EXPECT_EQ(718.913940f, quadpoints.y1);
757 EXPECT_EQ(157.211182f, quadpoints.x4);
758 EXPECT_EQ(706.264465f, quadpoints.y4);
759 }
Jane Liu20eafda2017-06-07 10:33:24 -0400760
761 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000762 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000763 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000764 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
765 ASSERT_TRUE(annot);
766 quadpoints.x1 = 140.802643f;
767 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000768 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000769 }
Jane Liu20eafda2017-06-07 10:33:24 -0400770
Lei Zhangec618142021-04-13 17:36:46 +0000771 // Save the document and close the page.
Jane Liu20eafda2017-06-07 10:33:24 -0400772 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000773 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400774
775 // Open the saved document.
Hui Yingst57daee82020-10-09 05:47:20 +0000776#if defined(_SKIA_SUPPORT_) && defined(OS_APPLE)
777 static const char kChecksum[] = "899387ae792390cd0d83cf7e2bbebfb5";
778#elif defined(_SKIA_SUPPORT_PATHS_) && defined(OS_APPLE)
779 static const char kChecksum[] = "e40e235ee35f47ff28dda009aaaf36df";
780#elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstb3490322020-07-22 00:53:29 +0000781 static const char kChecksum[] = "798fa41303381c9ba6d99092f5cd4d2b";
782#else
783 static const char kChecksum[] = "dba153419f67b7c0c0e3d22d3e8910d5";
784#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400785
Lei Zhang0b494052019-01-31 21:41:15 +0000786 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000787 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +0000788 ASSERT_TRUE(page);
Hui Yingstb3490322020-07-22 00:53:29 +0000789 VerifySavedRendering(page, 612, 792, kChecksum);
Jane Liu20eafda2017-06-07 10:33:24 -0400790
791 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000792 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400793
Lei Zhanga21d5932018-02-05 18:28:38 +0000794 {
795 // Check that the second annotation is an underline annotation and verify
796 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000797 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000798 ASSERT_TRUE(new_annot);
799 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
800 FS_QUADPOINTSF new_quadpoints;
801 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000802 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000803 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
804 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
805 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
806 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
807 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400808
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000809 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400810 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400811}
Jane Liu06462752017-06-27 16:41:14 -0400812
Lei Zhangab41f252018-12-23 03:10:50 +0000813TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000814 // Open a file with four annotations and load its first page.
815 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
816 FPDF_PAGE page = LoadPage(0);
817 ASSERT_TRUE(page);
818 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
819
820 // Retrieve the highlight annotation.
821 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
822 ASSERT_TRUE(annot);
823 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
824
825 FS_QUADPOINTSF quadpoints;
826 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
827
828 {
829 // Verify the current one set of quadpoints.
830 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
831
832 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
833 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
834 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
835 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
836 }
837
838 {
839 // Update the quadpoints.
840 FS_QUADPOINTSF new_quadpoints = quadpoints;
841 new_quadpoints.y1 -= 20.f;
842 new_quadpoints.y2 -= 20.f;
843 new_quadpoints.y3 -= 20.f;
844 new_quadpoints.y4 -= 20.f;
845 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
846
847 // Verify added quadpoint set
848 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
849 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
850 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
851 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
852 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
853 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
854 }
855
856 {
857 // Append a new set of quadpoints.
858 FS_QUADPOINTSF new_quadpoints = quadpoints;
859 new_quadpoints.y1 += 20.f;
860 new_quadpoints.y2 += 20.f;
861 new_quadpoints.y3 += 20.f;
862 new_quadpoints.y4 += 20.f;
863 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
864
865 // Verify added quadpoint set
866 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
867 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
868 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
869 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
870 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
871 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
872 }
873
874 {
875 // Setting and getting quadpoints at out-of-bound index should fail
876 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
877 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
878 }
879
880 FPDFPage_CloseAnnot(annot);
881
882 // Retrieve the square annotation
883 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
884
885 {
886 // Check that attempting to set its quadpoints would fail
887 ASSERT_TRUE(squareAnnot);
888 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
889 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
890 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
891 }
892
893 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000894 UnloadPage(page);
895}
896
Hui Yingstb64cd122020-07-27 16:01:02 +0000897// TODO(crbug.com/pdfium/1569): Fix this issue and enable the test for Skia.
898#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +0000899#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
900#else
901#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
902#endif
903TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Hui Yingstb64cd122020-07-27 16:01:02 +0000904#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
905 static const char kMd5Original[] = "00e70eb543c2a6e8f8aafb4ee951d9bf";
Lei Zhang4f556b82019-04-08 16:32:41 +0000906 static const char kMd5ModifiedHighlight[] =
Hui Yingstb64cd122020-07-27 16:01:02 +0000907 "7638c4a8fe4aabbf8704e198f49b3198";
908 static const char kMd5ModifiedSquare[] = "54f507af6af63de877b9cafdab1bbdaa";
909#else
910#if defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000911 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
912 static const char kMd5ModifiedHighlight[] =
913 "66f3caef3a7d488a4fa1ad37fc06310e";
914 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Lei Zhang0c03d632020-07-30 17:05:36 +0000915#elif defined(OS_APPLE)
Hui Yingstb64cd122020-07-27 16:01:02 +0000916 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
917 static const char kMd5ModifiedHighlight[] =
918 "e64bf648f6e9354d1f3eedb47a2c9498";
919 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Jane Liub370e5a2017-08-16 13:24:58 -0400920#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000921 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
922 static const char kMd5ModifiedHighlight[] =
923 "66f3caef3a7d488a4fa1ad37fc06310e";
924 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400925#endif
Hui Yingstb64cd122020-07-27 16:01:02 +0000926#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liub370e5a2017-08-16 13:24:58 -0400927
Jane Liu06462752017-06-27 16:41:14 -0400928 // Open a file with four annotations and load its first page.
929 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000930 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400931 ASSERT_TRUE(page);
932 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
933
Jane Liub370e5a2017-08-16 13:24:58 -0400934 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000935 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000936 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000937 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000938 }
Jane Liub370e5a2017-08-16 13:24:58 -0400939
Jane Liu0c6b07d2017-08-15 10:50:22 -0400940 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400941 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000942
943 // Retrieve the highlight annotation which has its AP stream already defined.
944 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000945 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000946 ASSERT_TRUE(annot);
947 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
948
949 // Check that color cannot be set when an AP stream is defined already.
950 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
951 102, 153, 204));
952
953 // Verify its attachment points.
954 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000955 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000956 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
957 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
958 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
959 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
960
961 // Check that updating the attachment points would succeed.
962 quadpoints.x1 -= 50.f;
963 quadpoints.x2 -= 50.f;
964 quadpoints.x3 -= 50.f;
965 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000966 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000967 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000968 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000969 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
970 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
971 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
972 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
973
974 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000975 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000976 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000977 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000978 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000979
980 // Verify its annotation rectangle.
981 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
982 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
983 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
984 EXPECT_NEAR(136.325f, rect.right, 0.001f);
985 EXPECT_NEAR(721.292f, rect.top, 0.001f);
986
987 // Check that updating the rectangle would succeed.
988 rect.left -= 60.f;
989 rect.right -= 60.f;
990 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
991 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
992 EXPECT_EQ(rect.right, new_rect.right);
993 }
Jane Liu06462752017-06-27 16:41:14 -0400994
Jane Liub370e5a2017-08-16 13:24:58 -0400995 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000996 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000997 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000998 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000999 }
Jane Liub370e5a2017-08-16 13:24:58 -04001000
Lei Zhanga21d5932018-02-05 18:28:38 +00001001 {
1002 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001003 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001004 ASSERT_TRUE(annot);
1005 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -04001006
Lei Zhanga21d5932018-02-05 18:28:38 +00001007 // Check that updating the rectangle would succeed.
1008 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1009 rect.left += 70.f;
1010 rect.right += 70.f;
1011 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1012 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1013 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -04001014
Lei Zhanga21d5932018-02-05 18:28:38 +00001015 // Check that updating the rectangle changes the square annotation's
1016 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001017 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001018 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +00001019 }
Jane Liub370e5a2017-08-16 13:24:58 -04001020
Jane Liu06462752017-06-27 16:41:14 -04001021 UnloadPage(page);
1022}
Jane Liu8ce58f52017-06-29 13:40:22 -04001023
Lei Zhangab41f252018-12-23 03:10:50 +00001024TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +00001025 // Open a file with multiline markup annotations.
1026 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
1027 FPDF_PAGE page = LoadPage(0);
1028 ASSERT_TRUE(page);
1029 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001030 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +00001031 ASSERT_TRUE(annot);
1032
1033 // This is a three line annotation.
1034 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
1035 }
1036 UnloadPage(page);
1037
1038 // null annotation should return 0
1039 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
1040}
1041
Lei Zhangab41f252018-12-23 03:10:50 +00001042TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -04001043 // Open a file with 3 annotations on its first page.
1044 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +00001045 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -04001046 ASSERT_TRUE(page);
1047 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1048
Jane Liu0c6b07d2017-08-15 10:50:22 -04001049 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -04001050
Lei Zhanga21d5932018-02-05 18:28:38 +00001051 // Check that the annotations have the expected rectangle coordinates.
1052 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001053 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001054 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1055 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1056 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001057
Lei Zhanga21d5932018-02-05 18:28:38 +00001058 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001059 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001060 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1061 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
1062 }
1063
1064 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001065 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001066 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1067 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1068 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001069
1070 // Check that nothing happens when attempting to remove an annotation with an
1071 // out-of-bound index.
1072 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
1073 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
1074 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1075
1076 // Remove the second annotation.
1077 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
1078 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1079 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
1080
Lei Zhangec618142021-04-13 17:36:46 +00001081 // Save the document and close the page.
Jane Liu8ce58f52017-06-29 13:40:22 -04001082 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +00001083 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -04001084
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001085 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -04001086 // Open the saved document.
1087 std::string new_file = GetString();
1088 FPDF_FILEACCESS file_access;
1089 memset(&file_access, 0, sizeof(file_access));
1090 file_access.m_FileLen = new_file.size();
1091 file_access.m_GetBlock = GetBlockFromString;
1092 file_access.m_Param = &new_file;
1093 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
1094 ASSERT_TRUE(new_doc);
1095 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
1096 ASSERT_TRUE(new_page);
1097
1098 // Check that the saved document has 2 annotations on the first page.
1099 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
1100
Lei Zhanga21d5932018-02-05 18:28:38 +00001101 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1102 // by verifying their rectangle coordinates.
1103 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001104 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001105 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1106 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1107 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001108
Lei Zhanga21d5932018-02-05 18:28:38 +00001109 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001110 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001111 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1112 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1113 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001114 FPDF_ClosePage(new_page);
1115 FPDF_CloseDocument(new_doc);
1116}
Jane Liu8ce58f52017-06-29 13:40:22 -04001117
Hui Yingst4a21df02020-05-13 03:15:44 +00001118TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001119#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001120#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst4a21df02020-05-13 03:15:44 +00001121 static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa";
1122 static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622";
1123 static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001124#else
Hui Yingst4a21df02020-05-13 03:15:44 +00001125 static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4";
1126 static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f";
1127 static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001128#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst4a21df02020-05-13 03:15:44 +00001129#else
1130#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001131 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
1132 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
1133 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Lei Zhang0c03d632020-07-30 17:05:36 +00001134#elif defined(OS_APPLE)
Hui Yingst4a21df02020-05-13 03:15:44 +00001135 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
1136 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
1137 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Jane Liubaa7ff42017-06-29 19:18:23 -04001138#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001139 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
1140 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
1141 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -04001142#endif
Hui Yingst4a21df02020-05-13 03:15:44 +00001143#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liubaa7ff42017-06-29 19:18:23 -04001144
1145 // Open a file with two annotations and load its first page.
1146 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001147 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001148 ASSERT_TRUE(page);
1149 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1150
1151 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001152 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001153 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001154 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001155 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001156
Lei Zhanga21d5932018-02-05 18:28:38 +00001157 {
1158 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001159 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001160 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001161
Lei Zhanga21d5932018-02-05 18:28:38 +00001162 // Check that this annotation has one path object and retrieve it.
1163 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001164 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001165 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1166 EXPECT_FALSE(path);
1167 path = FPDFAnnot_GetObject(annot.get(), 0);
1168 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1169 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001170
Lei Zhanga21d5932018-02-05 18:28:38 +00001171 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001172 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001173 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001174
Lei Zhanga21d5932018-02-05 18:28:38 +00001175 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001176 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001177 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001178 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001179 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001180
Lei Zhanga21d5932018-02-05 18:28:38 +00001181 // Add a second path object to the same annotation.
1182 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1183 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001184 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1185 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001186 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1187 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1188 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001189
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001190 // The object is in the annontation, not in the page, so the page object
1191 // array should not change.
1192 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1193
Lei Zhanga21d5932018-02-05 18:28:38 +00001194 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001195 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001196 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001197 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001198 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001199
Lei Zhanga21d5932018-02-05 18:28:38 +00001200 // Delete the newly added path object.
1201 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1202 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001203 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001204 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001205
1206 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001207 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001208 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001209 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001210 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001211
Jane Liubaa7ff42017-06-29 19:18:23 -04001212 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001213
Lei Zhanga21d5932018-02-05 18:28:38 +00001214 {
1215 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001216 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001217 ASSERT_TRUE(annot);
1218 rect.left = 200.f;
1219 rect.bottom = 400.f;
1220 rect.right = 500.f;
1221 rect.top = 600.f;
1222 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001223
Lei Zhanga21d5932018-02-05 18:28:38 +00001224 // Add a new path to the annotation.
1225 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1226 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1227 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1228 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1229 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001230 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1231 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001232 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1233 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1234 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1235
1236 // Check that the annotation's bounding box came from its rectangle.
1237 FS_RECTF new_rect;
1238 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1239 EXPECT_EQ(rect.left, new_rect.left);
1240 EXPECT_EQ(rect.bottom, new_rect.bottom);
1241 EXPECT_EQ(rect.right, new_rect.right);
1242 EXPECT_EQ(rect.top, new_rect.top);
1243 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001244
Lei Zhangec618142021-04-13 17:36:46 +00001245 // Save the document and close the page.
Jane Liubaa7ff42017-06-29 19:18:23 -04001246 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001247 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001248
1249 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001250 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001251 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001252 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001253 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001254
Jane Liu36567742017-07-06 11:13:35 -04001255 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001256 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001257
Lei Zhanga21d5932018-02-05 18:28:38 +00001258 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001259 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001260 ASSERT_TRUE(annot);
1261 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001262
Lei Zhanga21d5932018-02-05 18:28:38 +00001263 // Check that the new annotation's rectangle is as defined.
1264 FS_RECTF new_rect;
1265 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1266 EXPECT_EQ(rect.left, new_rect.left);
1267 EXPECT_EQ(rect.bottom, new_rect.bottom);
1268 EXPECT_EQ(rect.right, new_rect.right);
1269 EXPECT_EQ(rect.top, new_rect.top);
1270 }
1271
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001272 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001273 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001274}
Jane Liub137e752017-07-05 15:04:33 -04001275
Lei Zhangab41f252018-12-23 03:10:50 +00001276TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001277 // Open a file with an annotation and load its first page.
1278 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001279 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001280 ASSERT_TRUE(page);
1281
1282 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001283 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001284 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001285 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1286 }
Jane Liub137e752017-07-05 15:04:33 -04001287
Lei Zhanga21d5932018-02-05 18:28:38 +00001288 {
1289 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001290 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001291 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001292
Lei Zhanga21d5932018-02-05 18:28:38 +00001293 // Check that the original flag values are as expected.
1294 int flags = FPDFAnnot_GetFlags(annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00001295 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_INVISIBLE);
Lei Zhanga21d5932018-02-05 18:28:38 +00001296 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1297 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001298 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOZOOM);
1299 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOROTATE);
1300 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOVIEW);
1301 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_READONLY);
1302 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_LOCKED);
1303 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_TOGGLENOVIEW);
Jane Liub137e752017-07-05 15:04:33 -04001304
Lei Zhanga21d5932018-02-05 18:28:38 +00001305 // Set the HIDDEN flag.
1306 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1307 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1308 flags = FPDFAnnot_GetFlags(annot.get());
1309 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1310 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001311
Lei Zhanga21d5932018-02-05 18:28:38 +00001312 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001313 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001314 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001315 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001316 }
Jane Liub137e752017-07-05 15:04:33 -04001317
Lei Zhanga21d5932018-02-05 18:28:38 +00001318 // Unset the HIDDEN flag.
1319 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1320 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1321 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1322 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1323 flags = FPDFAnnot_GetFlags(annot.get());
1324 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1325 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001326
Lei Zhanga21d5932018-02-05 18:28:38 +00001327 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001328 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001329 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001330 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1331 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001332 }
Jane Liub137e752017-07-05 15:04:33 -04001333
Jane Liub137e752017-07-05 15:04:33 -04001334 UnloadPage(page);
1335}
Jane Liu36567742017-07-06 11:13:35 -04001336
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001337// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1338#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001339#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1340#else
1341#define MAYBE_AddAndModifyImage AddAndModifyImage
1342#endif
1343TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001344#if defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001345#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001346 static const char kMd5NewImage[] = "26a8eb30937226a677839379e0d7ae1a";
1347 static const char kMd5ModifiedImage[] = "2985114b32ba1a96be78ee643fe31aa5";
1348#else
1349 static const char kMd5NewImage[] = "14012ab500b4671fa73dd760129a8a93";
1350 static const char kMd5ModifiedImage[] = "5f97f98f58ed04dc393f31460485f1a2";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001351#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001352#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001353#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001354 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
1355 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +00001356#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001357 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
1358 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -04001359#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001360 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
1361 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -04001362#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001363#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001364
1365 // Open a file with two annotations and load its first page.
1366 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001367 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001368 ASSERT_TRUE(page);
1369 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1370
1371 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001372 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001373 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001374 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001375 }
Jane Liu36567742017-07-06 11:13:35 -04001376
Jane Liu36567742017-07-06 11:13:35 -04001377 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001378 FPDF_BITMAP image_bitmap;
1379
1380 {
1381 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001382 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001383 ASSERT_TRUE(annot);
1384 FS_RECTF rect;
1385 rect.left = 200.f;
1386 rect.bottom = 600.f;
1387 rect.right = 400.f;
1388 rect.top = 800.f;
1389 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1390
1391 // Add a solid-color translucent image object to the new annotation.
1392 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1393 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1394 0xeeeecccc);
1395 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1396 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1397 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1398 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
Lei Zhangc8601bf2021-06-29 23:19:27 +00001399 static constexpr FS_MATRIX kBitmapScaleMatrix{kBitmapSize, 0, 0,
1400 kBitmapSize, 0, 0};
1401 ASSERT_TRUE(FPDFPageObj_SetMatrix(image_object, &kBitmapScaleMatrix));
Lei Zhanga21d5932018-02-05 18:28:38 +00001402 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1403 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1404 }
Jane Liu36567742017-07-06 11:13:35 -04001405
1406 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001407 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001408 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001409 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001410 }
Jane Liu36567742017-07-06 11:13:35 -04001411
Lei Zhanga21d5932018-02-05 18:28:38 +00001412 {
1413 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001414 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001415 ASSERT_TRUE(annot);
1416 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1417 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1418 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001419
Lei Zhanga21d5932018-02-05 18:28:38 +00001420 // Modify the image in the new annotation.
1421 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1422 0xff000000);
1423 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1424 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1425 }
Jane Liu36567742017-07-06 11:13:35 -04001426
Lei Zhangec618142021-04-13 17:36:46 +00001427 // Save the document and close the page.
Jane Liu36567742017-07-06 11:13:35 -04001428 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001429 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001430 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001431
1432 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001433 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001434}
1435
Hui Yingst6cd754f2020-05-14 04:05:25 +00001436TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001437#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001438#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001439 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1440 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001441#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001442 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1443 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001444#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001445#else
1446#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001447 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1448 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Lei Zhang0c03d632020-07-30 17:05:36 +00001449#elif defined(OS_APPLE)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001450 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1451 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001452#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001453 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1454 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001455#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001456#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001457
1458 // Open a file with two annotations and load its first page.
1459 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001460 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001461 ASSERT_TRUE(page);
1462 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1463
1464 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001465 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001466 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001467 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001468 }
Jane Liu36567742017-07-06 11:13:35 -04001469
Lei Zhanga21d5932018-02-05 18:28:38 +00001470 {
1471 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001472 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001473 ASSERT_TRUE(annot);
1474 FS_RECTF rect;
1475 rect.left = 200.f;
1476 rect.bottom = 550.f;
1477 rect.right = 450.f;
1478 rect.top = 650.f;
1479 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001480
Lei Zhanga21d5932018-02-05 18:28:38 +00001481 // Add a translucent text object to the new annotation.
1482 FPDF_PAGEOBJECT text_object =
1483 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1484 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001485 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001486 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1487 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001488 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001489 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1490 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1491 }
Jane Liu36567742017-07-06 11:13:35 -04001492
1493 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001494 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001495 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001496 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001497 }
Jane Liu36567742017-07-06 11:13:35 -04001498
Lei Zhanga21d5932018-02-05 18:28:38 +00001499 {
1500 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001501 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001502 ASSERT_TRUE(annot);
1503 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1504 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1505 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001506
Lei Zhanga21d5932018-02-05 18:28:38 +00001507 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001508 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001509 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1510 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1511 }
Jane Liu36567742017-07-06 11:13:35 -04001512
1513 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001514 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001515 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001516 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001517 }
Jane Liu36567742017-07-06 11:13:35 -04001518
1519 // Remove the new annotation, and check that the page renders as before.
1520 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001521 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001522 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001523 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001524 }
Jane Liu36567742017-07-06 11:13:35 -04001525
1526 UnloadPage(page);
1527}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001528
Hui Yingst9b6b1542020-07-27 16:11:12 +00001529TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001530 // Open a file with four annotations and load its first page.
1531 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001532 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001533 ASSERT_TRUE(page);
1534
Lei Zhang4f556b82019-04-08 16:32:41 +00001535 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001536
Lei Zhanga21d5932018-02-05 18:28:38 +00001537 {
1538 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001539 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001540 ASSERT_TRUE(annot);
1541
1542 // Check that a non-existent key does not exist.
1543 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1544
1545 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001546 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001547 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001548 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1549 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1550 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001551
1552 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001553 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001554 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001555 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001556 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001557 ASSERT_EQ(66u, length_bytes);
1558 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1559 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1560 length_bytes));
1561 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1562 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001563
1564 // Check that the string value of the modified date is correct.
1565 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001566 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1567 nullptr, 0);
1568 ASSERT_EQ(44u, length_bytes);
1569 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001570 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001571 buf.data(), length_bytes));
1572 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001573
1574 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001575 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001576 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1577 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001578 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001579
Lei Zhangec618142021-04-13 17:36:46 +00001580 // Save the document and close the page.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001581 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001582 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001583
Hui Yingst9b6b1542020-07-27 16:11:12 +00001584#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001585#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001586 static const char kMd5[] = "7a2b712ca88d7b71f125ea3f9c88e57a";
1587#else
1588 static const char kMd5[] = "626d25c5aa5baf67d22d9a0e1c23f6aa";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001589#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001590#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001591#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001592 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001593#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001594 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001595#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001596 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001597#endif
Hui Yingst9b6b1542020-07-27 16:11:12 +00001598#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Dan Sinclair971a6742018-03-28 19:23:25 +00001599
1600 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001601 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001602 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001603 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001604 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001605 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001606 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001607
Lei Zhanga21d5932018-02-05 18:28:38 +00001608 // Check that the string value of the modified date is the newly-set value.
1609 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001610 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001611 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001612 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001613 ASSERT_EQ(44u, length_bytes);
1614 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001615 EXPECT_EQ(44u,
1616 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001617 buf.data(), length_bytes));
1618 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001619 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001620
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001621 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001622 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001623}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001624
rycsmith3e785602019-03-05 21:48:36 +00001625TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001626 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001627 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1628 FPDF_PAGE page = LoadPage(0);
1629 ASSERT_TRUE(page);
1630 {
1631 // First two annotations do not have "MaxLen" attribute.
1632 for (int i = 0; i < 2; i++) {
1633 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1634 ASSERT_TRUE(annot);
1635
1636 // Verify that no "MaxLen" key present.
1637 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1638
1639 float value;
1640 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1641 }
1642
1643 // Annotation in index 2 has "MaxLen" of 10.
1644 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1645 ASSERT_TRUE(annot);
1646
1647 // Verify that "MaxLen" key present.
1648 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1649
1650 float value;
1651 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1652 EXPECT_FLOAT_EQ(10.0f, value);
1653
1654 // Check bad inputs.
1655 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1656 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1657 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1658 // Ask for key that exists but is not a number.
1659 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1660 }
1661
1662 UnloadPage(page);
1663}
1664
Lei Zhangab41f252018-12-23 03:10:50 +00001665TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001666 // Open a file with four annotations and load its first page.
1667 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001668 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001669 ASSERT_TRUE(page);
1670
Lei Zhanga21d5932018-02-05 18:28:38 +00001671 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001672 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1673 static constexpr size_t kExpectNormalAPLength = 73970;
1674
Lei Zhanga21d5932018-02-05 18:28:38 +00001675 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001676 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001677 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001678
Lei Zhanga21d5932018-02-05 18:28:38 +00001679 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001680 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001681 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001682 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001683
Lei Zhanga21d5932018-02-05 18:28:38 +00001684 // Check that the string value of an AP is not returned if the buffer is too
1685 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001686 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1687 // Write in the buffer to verify it's not overwritten.
1688 memcpy(buf.data(), "abcdefgh", 8);
1689 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001690 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001691 buf.data(), normal_length_bytes - 1));
1692 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001693
Lei Zhanga21d5932018-02-05 18:28:38 +00001694 // Check that the string value of an AP is returned through a buffer that is
1695 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001696 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001697 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001698 buf.data(), normal_length_bytes));
1699 EXPECT_EQ(kMd5NormalAP,
1700 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1701 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001702
Lei Zhanga21d5932018-02-05 18:28:38 +00001703 // Check that the string value of an AP is returned through a buffer that is
1704 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001705 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1706 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001707 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001708 buf.data(), normal_length_bytes + 2));
1709 EXPECT_EQ(kMd5NormalAP,
1710 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1711 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001712
Lei Zhanga21d5932018-02-05 18:28:38 +00001713 // Check that getting an AP for a mode that does not have an AP returns an
1714 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001715 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001716 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001717 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001718
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001719 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001720 EXPECT_EQ(2u,
1721 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001722 buf.data(), 1000));
1723 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001724
Lei Zhanga21d5932018-02-05 18:28:38 +00001725 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001726 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001727 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001728 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001729 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001730 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001731 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001732
Lei Zhanga21d5932018-02-05 18:28:38 +00001733 // Set the AP correctly now.
1734 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001735 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001736
Lei Zhanga21d5932018-02-05 18:28:38 +00001737 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001738 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001739 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001740 ASSERT_EQ(24u, rollover_length_bytes);
1741 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001742 EXPECT_EQ(24u,
1743 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001744 buf.data(), rollover_length_bytes));
1745 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001746
Lei Zhanga21d5932018-02-05 18:28:38 +00001747 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001748 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1749 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001750 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001751 buf.data(), normal_length_bytes));
1752 EXPECT_EQ(kMd5NormalAP,
1753 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1754 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001755 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001756
1757 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001758 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001759 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001760
Lei Zhang0b494052019-01-31 21:41:15 +00001761 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001762 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001763 ASSERT_TRUE(page);
Lei Zhanga21d5932018-02-05 18:28:38 +00001764 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001765 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001766
Lei Zhanga21d5932018-02-05 18:28:38 +00001767 // Check that the new annotation value is equal to the value we set before
1768 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001769 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001770 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001771 ASSERT_EQ(24u, rollover_length_bytes);
1772 std::vector<FPDF_WCHAR> buf =
1773 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001774 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1775 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001776 buf.data(), rollover_length_bytes));
1777 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001778 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001779
1780 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001781 CloseSavedPage(page);
1782 CloseSavedDocument();
1783}
1784
Lei Zhangab41f252018-12-23 03:10:50 +00001785TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001786 // Open a file with four annotations and load its first page.
1787 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001788 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001789 ASSERT_TRUE(page);
1790
Lei Zhanga21d5932018-02-05 18:28:38 +00001791 {
1792 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001793 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001794 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001795
Lei Zhanga21d5932018-02-05 18:28:38 +00001796 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001797 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001798 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001799 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001800 EXPECT_EQ(73970u,
1801 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1802 nullptr, 0));
1803 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1804 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001805
Lei Zhanga21d5932018-02-05 18:28:38 +00001806 // Check that setting the Down AP to null removes the Down entry but keeps
1807 // Normal intact.
1808 EXPECT_TRUE(
1809 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1810 EXPECT_EQ(73970u,
1811 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1812 nullptr, 0));
1813 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1814 nullptr, 0));
1815 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001816
Lei Zhang75c81712018-02-08 17:22:39 +00001817 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001818}
1819
Lei Zhangab41f252018-12-23 03:10:50 +00001820TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001821 // Open a file with four annotations and load its first page.
1822 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001823 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001824 ASSERT_TRUE(page);
1825
Lei Zhanga21d5932018-02-05 18:28:38 +00001826 {
1827 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001828 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001829 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001830
Lei Zhanga21d5932018-02-05 18:28:38 +00001831 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001832 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001833 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001834 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001835 EXPECT_EQ(73970u,
1836 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1837 nullptr, 0));
1838 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1839 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001840
Lei Zhanga21d5932018-02-05 18:28:38 +00001841 // Check that setting the Normal AP to null removes the whole AP dictionary.
1842 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1843 nullptr));
1844 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1845 nullptr, 0));
1846 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1847 nullptr, 0));
1848 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001849
Lei Zhang75c81712018-02-08 17:22:39 +00001850 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001851}
1852
Lei Zhangab41f252018-12-23 03:10:50 +00001853TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001854 // Open a file with annotations and load its first page.
1855 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001856 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001857 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001858 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001859
Lei Zhanga21d5932018-02-05 18:28:38 +00001860 {
1861 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001862 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001863 ASSERT_TRUE(annot);
1864 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1865 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001866 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001867 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1868 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1869 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001870
Lei Zhanga21d5932018-02-05 18:28:38 +00001871 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001872 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001873 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1874 ASSERT_TRUE(popup);
1875 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1876 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1877 FS_RECTF rect;
1878 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1879 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1880 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001881
Lei Zhanga21d5932018-02-05 18:28:38 +00001882 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1883 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001884 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001885 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1886 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001887
Lei Zhanga21d5932018-02-05 18:28:38 +00001888 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1889 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001890 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001891 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001892 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1893 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001894 }
Jane Liu300bb272017-08-21 14:37:53 -04001895
Jane Liu300bb272017-08-21 14:37:53 -04001896 UnloadPage(page);
1897}
1898
Lei Zhangab41f252018-12-23 03:10:50 +00001899TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001900 // Open file with form text fields.
1901 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001902 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001903 ASSERT_TRUE(page);
1904
Lei Zhanga21d5932018-02-05 18:28:38 +00001905 {
1906 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001907 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001908 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001909
Lei Zhanga21d5932018-02-05 18:28:38 +00001910 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001911 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001912 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001913 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1914 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1915 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001916 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001917 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001918
Lei Zhanga21d5932018-02-05 18:28:38 +00001919 {
1920 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001921 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001922 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001923
Lei Zhanga21d5932018-02-05 18:28:38 +00001924 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001925 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001926 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001927 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1928 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1929 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001930 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1931 }
1932
1933 {
1934 // Retrieve the fourth annotation: user-editable password text field.
1935 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1936 ASSERT_TRUE(annot);
1937
1938 // Check that the flag values are as expected.
1939 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1940 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001941 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1942 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1943 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001944 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001945 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001946
1947 UnloadPage(page);
1948}
1949
Lei Zhangab41f252018-12-23 03:10:50 +00001950TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001951 // Open file with form text fields.
1952 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001953 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001954 ASSERT_TRUE(page);
1955
Lei Zhanga21d5932018-02-05 18:28:38 +00001956 {
1957 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001958 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001959 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001960
Lei Zhanga21d5932018-02-05 18:28:38 +00001961 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001962 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001963 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001964 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1965 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001966 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1967 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001968 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001969 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001970
Lei Zhanga21d5932018-02-05 18:28:38 +00001971 {
1972 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001973 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001974 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001975
Lei Zhanga21d5932018-02-05 18:28:38 +00001976 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001977 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001978 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001979 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1980 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001981 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1982 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001983 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001984 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001985
Lei Zhanga21d5932018-02-05 18:28:38 +00001986 {
1987 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001988 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001989 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001990
Lei Zhanga21d5932018-02-05 18:28:38 +00001991 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001992 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001993 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001994 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1995 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001996 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1997 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001998 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001999 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07002000
2001 UnloadPage(page);
2002}
Diana Gage40870db2017-07-19 18:16:03 -07002003
Lei Zhangab41f252018-12-23 03:10:50 +00002004TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07002005 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002006 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002007 FPDF_PAGE page = LoadPage(0);
2008 ASSERT_TRUE(page);
2009
2010 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00002011 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
2012 EXPECT_FALSE(
2013 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00002014
Lei Zhang8da98232019-12-11 23:29:33 +00002015 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00002016 {
2017 // Verify there is an annotation.
2018 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002019 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00002020 EXPECT_TRUE(annot);
2021 }
2022
2023 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00002024 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
2025 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
2026 EXPECT_FALSE(
2027 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07002028
2029 UnloadPage(page);
2030}
2031
Lei Zhangab41f252018-12-23 03:10:50 +00002032TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07002033 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002034 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002035 FPDF_PAGE page = LoadPage(0);
2036 ASSERT_TRUE(page);
2037
Lei Zhanga21d5932018-02-05 18:28:38 +00002038 {
2039 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002040 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002041 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002042 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002043 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002044
Lei Zhanga21d5932018-02-05 18:28:38 +00002045 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002046 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00002047 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2048 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002049 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2050 }
Diana Gage40870db2017-07-19 18:16:03 -07002051
Lei Zhanga21d5932018-02-05 18:28:38 +00002052 {
2053 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002054 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002055 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002056 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002057 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002058
Lei Zhanga21d5932018-02-05 18:28:38 +00002059 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002060 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002061 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002062 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2063 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002064 }
Diana Gage40870db2017-07-19 18:16:03 -07002065
2066 UnloadPage(page);
2067}
2068
Lei Zhangab41f252018-12-23 03:10:50 +00002069TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07002070 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002071 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002072 FPDF_PAGE page = LoadPage(0);
2073 ASSERT_TRUE(page);
2074
Lei Zhanga21d5932018-02-05 18:28:38 +00002075 {
2076 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002077 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002078 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002079 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002080 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002081
Lei Zhanga21d5932018-02-05 18:28:38 +00002082 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002083 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002084 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002085 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2086 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002087 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2088 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002089 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002090 }
Diana Gage40870db2017-07-19 18:16:03 -07002091
Lei Zhanga21d5932018-02-05 18:28:38 +00002092 {
2093 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002094 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002095 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002096 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002097 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002098
Lei Zhanga21d5932018-02-05 18:28:38 +00002099 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002100 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002101 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002102 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2103 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002104 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2105 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002106 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002107 }
Diana Gage40870db2017-07-19 18:16:03 -07002108
Lei Zhanga21d5932018-02-05 18:28:38 +00002109 {
2110 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002111 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002112 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002113 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002114 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002115
Lei Zhanga21d5932018-02-05 18:28:38 +00002116 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002117 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002118 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002119 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2120 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002121 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2122 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002123 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002124 }
Diana Gage40870db2017-07-19 18:16:03 -07002125
2126 UnloadPage(page);
2127}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002128
Hui Yingst603dcd82020-11-09 22:36:46 +00002129TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
Lei Zhang03e5e682019-09-16 19:45:55 +00002130#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst603dcd82020-11-09 22:36:46 +00002131 static const char kExpectedBitmap[] = "a1ea1ceebb26922fae576cb79ce63af0";
Lei Zhang03e5e682019-09-16 19:45:55 +00002132#else
Lei Zhang992e7e22019-02-04 19:20:58 +00002133 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
Hui Yingst603dcd82020-11-09 22:36:46 +00002134#endif
2135 static constexpr size_t kExpectedSize = 1609;
Lei Zhang992e7e22019-02-04 19:20:58 +00002136
2137 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2138
2139 FPDF_PAGE page = LoadPage(0);
2140 ASSERT_TRUE(page);
2141
2142 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2143 EXPECT_EQ(kExpectedSize, GetString().size());
2144 ClearString();
2145
2146 for (size_t i = 0; i < 10; ++i) {
2147 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2148 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2149
2150 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2151 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2152 // equal, not bigger.
2153 EXPECT_LT(kExpectedSize, GetString().size());
2154 ClearString();
2155 }
2156
2157 UnloadPage(page);
2158}
2159
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002160TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2161 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2162 FPDF_PAGE page = LoadPage(0);
2163 ASSERT_TRUE(page);
2164 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2165
2166 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002167 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002168 static const size_t kBufSize = 12;
2169 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002170
2171 {
2172 // Add a text annotation to the page.
2173 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2174 ASSERT_TRUE(annot);
2175 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2176 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2177
2178 // Make sure there is no test key, add set a value there, and read it back.
2179 std::fill(buf.begin(), buf.end(), 'x');
2180 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002181 kBufSize));
2182 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002183
Lei Zhangf0f67682019-04-08 17:03:21 +00002184 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002185 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2186
2187 std::fill(buf.begin(), buf.end(), 'x');
2188 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002189 kBufSize));
2190 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002191 }
2192
Lei Zhang05ec64c2019-01-09 03:00:06 +00002193 {
2194 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2195 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002196 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002197 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002198 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2199 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2200 // Also do the same test for its appearance string.
2201 std::fill(buf.begin(), buf.end(), 'x');
2202 ASSERT_EQ(2u,
2203 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002204 buf.data(), kBufSize));
2205 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002206
Lei Zhangf0f67682019-04-08 17:03:21 +00002207 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002208 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2209 text.get()));
2210
2211 std::fill(buf.begin(), buf.end(), 'x');
2212 ASSERT_EQ(6u,
2213 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002214 buf.data(), kBufSize));
2215 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002216 }
2217
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002218 UnloadPage(page);
2219
2220 {
2221 // Save a copy, open the copy, and check the annotation again.
2222 // Note that it renders the rotation.
2223 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002224 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002225 FPDF_PAGE saved_page = LoadSavedPage(0);
2226 ASSERT_TRUE(saved_page);
2227
Lei Zhang05ec64c2019-01-09 03:00:06 +00002228 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002229 {
2230 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2231 ASSERT_TRUE(annot);
2232 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2233
2234 std::fill(buf.begin(), buf.end(), 'x');
2235 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002236 kBufSize));
2237 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002238 }
2239
Lei Zhang05ec64c2019-01-09 03:00:06 +00002240 {
2241 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2242 ASSERT_TRUE(annot);
2243 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2244 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2245
2246 std::fill(buf.begin(), buf.end(), 'x');
2247 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002248 kBufSize));
2249 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002250 }
2251
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002252 CloseSavedPage(saved_page);
2253 CloseSavedDocument();
2254 }
2255}
rycsmithcb752f32019-02-21 18:40:53 +00002256
2257TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2258 // Open a file with combobox widget annotations and load its first page.
2259 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2260 FPDF_PAGE page = LoadPage(0);
2261 ASSERT_TRUE(page);
2262
2263 {
2264 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2265 ASSERT_TRUE(annot);
2266
2267 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2268
2269 annot.reset(FPDFPage_GetAnnot(page, 1));
2270 ASSERT_TRUE(annot);
2271
2272 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002273
2274 // Check bad form handle / annot.
2275 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2276 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2277 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002278 }
2279
2280 UnloadPage(page);
2281}
2282
2283TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2284 // Open a file with listbox widget annotations and load its first page.
2285 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2286 FPDF_PAGE page = LoadPage(0);
2287 ASSERT_TRUE(page);
2288
2289 {
2290 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2291 ASSERT_TRUE(annot);
2292
2293 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2294
2295 annot.reset(FPDFPage_GetAnnot(page, 1));
2296 ASSERT_TRUE(annot);
2297
2298 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2299 }
2300
2301 UnloadPage(page);
2302}
2303
2304TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2305 // Open a file with ink annotations and load its first page.
2306 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2307 FPDF_PAGE page = LoadPage(0);
2308 ASSERT_TRUE(page);
2309
2310 {
2311 // annotations do not have "Opt" array and will return -1
2312 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2313 ASSERT_TRUE(annot);
2314
2315 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2316
2317 annot.reset(FPDFPage_GetAnnot(page, 1));
2318 ASSERT_TRUE(annot);
2319
2320 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2321 }
2322
2323 UnloadPage(page);
2324}
2325
2326TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2327 // Open a file with combobox widget annotations and load its first page.
2328 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2329 FPDF_PAGE page = LoadPage(0);
2330 ASSERT_TRUE(page);
2331
2332 {
2333 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2334 ASSERT_TRUE(annot);
2335
2336 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002337 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002338 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002339 ASSERT_EQ(8u, length_bytes);
2340 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002341 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002342 buf.data(), length_bytes));
2343 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002344
2345 annot.reset(FPDFPage_GetAnnot(page, 1));
2346 ASSERT_TRUE(annot);
2347
2348 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002349 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002350 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002351 ASSERT_EQ(12u, length_bytes);
2352 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002353 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002354 buf.data(), length_bytes));
2355 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002356
2357 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002358 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002359 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002360 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002361 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002362 buf.data(), length_bytes));
2363 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002364
Lei Zhange7033c82019-02-26 19:30:49 +00002365 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002366 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2367 nullptr, 0));
2368 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2369 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002370
2371 // Check bad form handle / annot.
2372 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2373 EXPECT_EQ(0u,
2374 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2375 EXPECT_EQ(0u,
2376 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002377 }
2378
2379 UnloadPage(page);
2380}
2381
2382TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2383 // Open a file with listbox widget annotations and load its first page.
2384 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2385 FPDF_PAGE page = LoadPage(0);
2386 ASSERT_TRUE(page);
2387
2388 {
2389 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2390 ASSERT_TRUE(annot);
2391
2392 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002393 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002394 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002395 ASSERT_EQ(8u, length_bytes);
2396 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002397 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002398 buf.data(), length_bytes));
2399 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002400
2401 annot.reset(FPDFPage_GetAnnot(page, 1));
2402 ASSERT_TRUE(annot);
2403
2404 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002405 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002406 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002407 ASSERT_EQ(12u, length_bytes);
2408 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002409 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002410 buf.data(), length_bytes));
2411 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002412
2413 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002414 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002415 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002416 ASSERT_EQ(18u, length_bytes);
2417 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002418 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002419 buf.data(), length_bytes));
2420 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002421
2422 // indices out of range
2423 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2424 nullptr, 0));
2425 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2426 nullptr, 0));
2427 }
2428
2429 UnloadPage(page);
2430}
2431
2432TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2433 // Open a file with ink annotations and load its first page.
2434 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2435 FPDF_PAGE page = LoadPage(0);
2436 ASSERT_TRUE(page);
2437
2438 {
2439 // annotations do not have "Opt" array and will return 0
2440 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2441 ASSERT_TRUE(annot);
2442
2443 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2444 nullptr, 0));
2445
2446 annot.reset(FPDFPage_GetAnnot(page, 1));
2447 ASSERT_TRUE(annot);
2448
2449 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2450 nullptr, 0));
2451 }
2452
2453 UnloadPage(page);
2454}
Ryan Smith09c23b12019-04-25 18:09:06 +00002455
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002456TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2457 // Open a file with combobox widget annotations and load its first page.
2458 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2459 FPDF_PAGE page = LoadPage(0);
2460 ASSERT_TRUE(page);
2461
2462 {
2463 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2464 ASSERT_TRUE(annot);
2465
2466 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2467 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2468 ASSERT_EQ(3, count);
2469 for (int i = 0; i < count; i++) {
2470 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2471 }
2472
2473 annot.reset(FPDFPage_GetAnnot(page, 1));
2474 ASSERT_TRUE(annot);
2475
2476 // Checks for Combobox with Values (/V) object which is just a string.
2477 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2478 ASSERT_EQ(26, count);
2479 for (int i = 0; i < count; i++) {
2480 EXPECT_EQ(i == 1,
2481 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2482 }
2483
2484 // Checks for index outside bound i.e. (index >= CountOption()).
2485 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2486 /*index=*/26));
2487 // Checks for negetive index.
2488 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2489 /*index=*/-1));
2490
2491 // Checks for bad form handle/annot.
2492 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2493 EXPECT_FALSE(
2494 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2495 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2496 }
2497
2498 UnloadPage(page);
2499}
2500
2501TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2502 // Open a file with listbox widget annotations and load its first page.
2503 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2504 FPDF_PAGE page = LoadPage(0);
2505 ASSERT_TRUE(page);
2506
2507 {
2508 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2509 ASSERT_TRUE(annot);
2510
2511 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2512 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2513 ASSERT_EQ(3, count);
2514 for (int i = 0; i < count; i++) {
2515 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2516 }
2517
2518 annot.reset(FPDFPage_GetAnnot(page, 1));
2519 ASSERT_TRUE(annot);
2520
2521 // Checks for Listbox with Values (/V) object which is just a string.
2522 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2523 ASSERT_EQ(26, count);
2524 for (int i = 0; i < count; i++) {
2525 EXPECT_EQ(i == 1,
2526 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2527 }
2528
2529 annot.reset(FPDFPage_GetAnnot(page, 3));
2530 ASSERT_TRUE(annot);
2531
2532 // Checks for Listbox with only Selected indices (/I) object which is an
2533 // array with multiple objects.
2534 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2535 ASSERT_EQ(5, count);
2536 for (int i = 0; i < count; i++) {
2537 bool expected = (i == 1 || i == 3);
2538 EXPECT_EQ(expected,
2539 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2540 }
2541
2542 annot.reset(FPDFPage_GetAnnot(page, 4));
2543 ASSERT_TRUE(annot);
2544
2545 // Checks for Listbox with Values (/V) object which is an array with
2546 // multiple objects.
2547 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2548 ASSERT_EQ(5, count);
2549 for (int i = 0; i < count; i++) {
2550 bool expected = (i == 2 || i == 4);
2551 EXPECT_EQ(expected,
2552 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2553 }
2554
2555 annot.reset(FPDFPage_GetAnnot(page, 5));
2556 ASSERT_TRUE(annot);
2557
2558 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2559 // objects conflict with different lengths.
2560 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2561 ASSERT_EQ(5, count);
2562 for (int i = 0; i < count; i++) {
2563 bool expected = (i == 0 || i == 2);
2564 EXPECT_EQ(expected,
2565 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2566 }
2567 }
2568
2569 UnloadPage(page);
2570}
2571
2572TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2573 // Open a file with multiple form field annotations and load its first page.
2574 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2575 FPDF_PAGE page = LoadPage(0);
2576 ASSERT_TRUE(page);
2577
2578 {
2579 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2580 ASSERT_TRUE(annot);
2581
2582 // Checks for link annotation.
2583 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2584 /*index=*/0));
2585
2586 annot.reset(FPDFPage_GetAnnot(page, 3));
2587 ASSERT_TRUE(annot);
2588
2589 // Checks for text field annotation.
2590 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2591 /*index=*/0));
2592 }
2593
2594 UnloadPage(page);
2595}
2596
Ryan Smith09c23b12019-04-25 18:09:06 +00002597TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2598 // Open a file with combobox annotations and load its first page.
2599 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2600 FPDF_PAGE page = LoadPage(0);
2601 ASSERT_TRUE(page);
2602
2603 {
2604 // All 3 widgets have Tf font size 12.
2605 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2606 ASSERT_TRUE(annot);
2607
2608 float value;
2609 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2610 EXPECT_EQ(12.0, value);
2611
2612 annot.reset(FPDFPage_GetAnnot(page, 1));
2613 ASSERT_TRUE(annot);
2614
2615 float value_two;
2616 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2617 EXPECT_EQ(12.0, value_two);
2618
2619 annot.reset(FPDFPage_GetAnnot(page, 2));
2620 ASSERT_TRUE(annot);
2621
2622 float value_three;
2623 ASSERT_TRUE(
2624 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2625 EXPECT_EQ(12.0, value_three);
2626 }
2627
2628 UnloadPage(page);
2629}
2630
2631TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2632 // Open a file with textfield annotations and load its first page.
2633 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2634 FPDF_PAGE page = LoadPage(0);
2635 ASSERT_TRUE(page);
2636
2637 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002638 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002639 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2640 ASSERT_TRUE(annot);
2641
2642 float value;
2643 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2644 EXPECT_EQ(12.0, value);
2645
2646 annot.reset(FPDFPage_GetAnnot(page, 1));
2647 ASSERT_TRUE(annot);
2648
2649 float value_two;
2650 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2651 EXPECT_EQ(12.0, value_two);
2652
2653 annot.reset(FPDFPage_GetAnnot(page, 2));
2654 ASSERT_TRUE(annot);
2655
2656 float value_three;
2657 ASSERT_TRUE(
2658 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2659 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002660
2661 float value_four;
2662 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2663 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002664 }
2665
2666 UnloadPage(page);
2667}
2668
2669TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2670 // Open a file with ink annotations and load its first page.
2671 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2672 FPDF_PAGE page = LoadPage(0);
2673 ASSERT_TRUE(page);
2674
2675 {
2676 // Annotations that do not have variable text and will return -1.
2677 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2678 ASSERT_TRUE(annot);
2679
2680 float value;
2681 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2682
2683 annot.reset(FPDFPage_GetAnnot(page, 1));
2684 ASSERT_TRUE(annot);
2685
2686 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2687 }
2688
2689 UnloadPage(page);
2690}
2691
2692TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2693 // Open a file with combobox annotations and load its first page.
2694 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2695 FPDF_PAGE page = LoadPage(0);
2696 ASSERT_TRUE(page);
2697
2698 {
2699 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2700 ASSERT_TRUE(annot);
2701
2702 // Check bad form handle / annot.
2703 float value;
2704 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2705 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2706 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2707 }
2708
2709 UnloadPage(page);
2710}
2711
2712TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2713 // Open a file with textfield annotations and load its first page.
2714 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2715 FPDF_PAGE page = LoadPage(0);
2716 ASSERT_TRUE(page);
2717
2718 {
2719 // Obtain the first annotation, a text field with negative font size, -12.
2720 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2721 ASSERT_TRUE(annot);
2722
2723 float value;
2724 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2725 EXPECT_EQ(-12.0, value);
2726 }
2727
2728 UnloadPage(page);
2729}
Ryan Smith23fdf892019-07-17 21:51:26 +00002730
2731TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2732 // Open a file with checkbox and radiobuttons widget annotations and load its
2733 // first page.
2734 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2735 FPDF_PAGE page = LoadPage(0);
2736 ASSERT_TRUE(page);
2737
2738 {
2739 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2740 ASSERT_TRUE(annot);
2741 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2742 }
2743
2744 UnloadPage(page);
2745}
2746
2747TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2748 // Open a file with checkbox and radiobutton widget annotations and load its
2749 // first page.
2750 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2751 FPDF_PAGE page = LoadPage(0);
2752 ASSERT_TRUE(page);
2753
2754 {
2755 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2756 ASSERT_TRUE(annot);
2757 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2758 }
2759
2760 UnloadPage(page);
2761}
2762
2763TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2764 // Open a file with checkbox and radiobutton widget annotations and load its
2765 // first page.
2766 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2767 FPDF_PAGE page = LoadPage(0);
2768 ASSERT_TRUE(page);
2769
2770 {
2771 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2772 ASSERT_TRUE(annot);
2773 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2774
2775 annot.reset(FPDFPage_GetAnnot(page, 6));
2776 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2777
2778 annot.reset(FPDFPage_GetAnnot(page, 7));
2779 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2780 }
2781
2782 UnloadPage(page);
2783}
2784
2785TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2786 // Open a file with checkbox and radiobutton widget annotations and load its
2787 // first page.
2788 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2789 FPDF_PAGE page = LoadPage(0);
2790 ASSERT_TRUE(page);
2791
2792 {
2793 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2794 ASSERT_TRUE(annot);
2795 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2796
2797 annot.reset(FPDFPage_GetAnnot(page, 3));
2798 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2799
2800 annot.reset(FPDFPage_GetAnnot(page, 4));
2801 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2802 }
2803
2804 UnloadPage(page);
2805}
2806
2807TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2808 // Open a file with checkbox and radiobuttons widget annotations and load its
2809 // first page.
2810 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2811 FPDF_PAGE page = LoadPage(0);
2812 ASSERT_TRUE(page);
2813
2814 {
2815 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2816 ASSERT_TRUE(annot);
2817 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2818 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2819 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2820 }
2821
2822 UnloadPage(page);
2823}
2824
2825TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2826 // Open a file with text widget annotations and load its first page.
2827 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2828 FPDF_PAGE page = LoadPage(0);
2829 ASSERT_TRUE(page);
2830
2831 {
2832 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2833 ASSERT_TRUE(annot);
2834 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2835 }
2836
2837 UnloadPage(page);
2838}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002839
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002840TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2841 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002842 FPDF_PAGE page = LoadPage(0);
2843 ASSERT_TRUE(page);
2844
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002845 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2846
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002847 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002848 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002849 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002850 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002851 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002852
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002853 constexpr int kExpectedAnnotTypes[] = {-1,
2854 FPDF_FORMFIELD_COMBOBOX,
2855 FPDF_FORMFIELD_LISTBOX,
2856 FPDF_FORMFIELD_TEXTFIELD,
2857 FPDF_FORMFIELD_CHECKBOX,
2858 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002859
Lei Zhang4501a502020-05-18 16:52:59 +00002860 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002861 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002862 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002863 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002864 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2865 }
2866 UnloadPage(page);
2867}
2868
2869TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2870 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2871 FPDF_PAGE page = LoadPage(0);
2872 ASSERT_TRUE(page);
2873
2874 {
2875 EXPECT_EQ(0u,
2876 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2877
2878 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2879 ASSERT_TRUE(annot);
2880
2881 EXPECT_EQ(0u,
2882 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2883
2884 unsigned long length_bytes =
2885 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2886 ASSERT_EQ(2u, length_bytes);
2887 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2888 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2889 buf.data(), length_bytes));
2890 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2891 }
2892 {
2893 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2894 ASSERT_TRUE(annot);
2895
2896 unsigned long length_bytes =
2897 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2898 ASSERT_EQ(18u, length_bytes);
2899 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2900 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2901 buf.data(), length_bytes));
2902 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2903 }
2904 UnloadPage(page);
2905}
2906
2907TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2908 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2909 FPDF_PAGE page = LoadPage(0);
2910 ASSERT_TRUE(page);
2911
2912 {
2913 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2914 ASSERT_TRUE(annot);
2915
2916 unsigned long length_bytes =
2917 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2918 ASSERT_EQ(2u, length_bytes);
2919 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2920 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2921 buf.data(), length_bytes));
2922 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2923 }
2924 {
2925 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2926 ASSERT_TRUE(annot);
2927
2928 unsigned long length_bytes =
2929 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2930 ASSERT_EQ(14u, length_bytes);
2931 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2932 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2933 buf.data(), length_bytes));
2934 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2935 }
2936 UnloadPage(page);
2937}
2938
2939TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2940 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2941 FPDF_PAGE page = LoadPage(0);
2942 ASSERT_TRUE(page);
2943
2944 {
2945 EXPECT_EQ(0u,
2946 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2947
2948 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2949 ASSERT_TRUE(annot);
2950
2951 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2952
2953 unsigned long length_bytes =
2954 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2955 ASSERT_EQ(18u, length_bytes);
2956 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2957 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2958 buf.data(), length_bytes));
2959 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2960 }
2961 UnloadPage(page);
2962}
2963
2964TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2965 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2966 FPDF_PAGE page = LoadPage(0);
2967 ASSERT_TRUE(page);
2968
2969 {
2970 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2971 ASSERT_TRUE(annot);
2972
2973 unsigned long length_bytes =
2974 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2975 ASSERT_EQ(30u, length_bytes);
2976 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2977 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2978 buf.data(), length_bytes));
2979 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2980 }
2981 UnloadPage(page);
2982}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002983
Lei Zhang9b444002020-04-17 17:35:23 +00002984TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2985 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002986 FPDF_PAGE page = LoadPage(0);
2987 ASSERT_TRUE(page);
2988
Lei Zhang9b444002020-04-17 17:35:23 +00002989 // Verify widgets are by default focusable.
2990 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2991 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002992
Lei Zhang9b444002020-04-17 17:35:23 +00002993 // Expected annot subtypes for page 0 of annots.pdf.
2994 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2995 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2996 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2997 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2998 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002999
Lei Zhang9b444002020-04-17 17:35:23 +00003000 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00003001 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00003002 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
3003 kExpectedAnnotSubtypes,
3004 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003005
Lei Zhang9b444002020-04-17 17:35:23 +00003006 // Make no annotation type focusable using the preferred method.
3007 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
3008 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003009
Lei Zhang9b444002020-04-17 17:35:23 +00003010 // Restore the focusable type count back to 1, then set it back to 0 using a
3011 // different method.
3012 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
3013 ASSERT_TRUE(
3014 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
3015 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003016
Lei Zhang9b444002020-04-17 17:35:23 +00003017 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00003018 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003019
Lei Zhang9b444002020-04-17 17:35:23 +00003020 // Now make links focusable.
3021 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
3022 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003023
Lei Zhang9b444002020-04-17 17:35:23 +00003024 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00003025 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00003026 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
3027 kExpectedAnnotSubtypes,
3028 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003029
Lei Zhang9b444002020-04-17 17:35:23 +00003030 // Test invalid parameters.
3031 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00003032 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00003033 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00003034 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003035 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
3036
Lei Zhang9b444002020-04-17 17:35:23 +00003037 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
3038 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
3039 subtypes.size()));
3040 EXPECT_FALSE(
3041 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
3042 EXPECT_FALSE(
3043 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003044
3045 UnloadPage(page);
3046}
Lei Zhang671aece2020-04-14 19:02:26 +00003047
Hui Yingstea3816d2020-07-28 22:35:11 +00003048TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00003049 ASSERT_TRUE(OpenDocument("annots.pdf"));
3050 FPDF_PAGE page = LoadPage(0);
3051 ASSERT_TRUE(page);
3052
3053 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003054#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3055 static const char kMd5sum[] = "4ca14c670396711194b40ecc2514969b";
3056#else
Lei Zhang671aece2020-04-14 19:02:26 +00003057#if defined(OS_WIN)
3058 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
Lei Zhang0c03d632020-07-30 17:05:36 +00003059#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003060 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
3061#else
3062 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
3063#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003064#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003065 // Check the initial rendering.
3066 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3067 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3068 }
3069
3070 // Make links and highlights focusable.
3071 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
3072 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00003073 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00003074 ASSERT_TRUE(
3075 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
3076 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3077 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
3078 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
3079 subtypes.size()));
3080 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
3081 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
3082
3083 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003084#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3085 static const char kMd5sum[] = "48cb60b3f9bc364c73582aff3418451e";
3086#else
Lei Zhang671aece2020-04-14 19:02:26 +00003087#if defined(OS_WIN)
3088 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
Lei Zhang0c03d632020-07-30 17:05:36 +00003089#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003090 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
3091#else
3092 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
3093#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003094#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003095 // Focus the first link and check the rendering.
3096 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3097 ASSERT_TRUE(annot);
3098 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3099 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3100 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3101 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3102 }
3103
3104 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003105#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3106 static const char kMd5sum[] = "d09869ff0a209daf179da2d7a58142b1";
3107#else
Lei Zhang671aece2020-04-14 19:02:26 +00003108#if defined(OS_WIN)
3109 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
Lei Zhang0c03d632020-07-30 17:05:36 +00003110#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003111 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
3112#else
3113 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
3114#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003115#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003116 // Focus the first highlight and check the rendering.
3117 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3118 ASSERT_TRUE(annot);
3119 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3120 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3121 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3122 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3123 }
3124
3125 UnloadPage(page);
3126}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003127
3128TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
3129 ASSERT_TRUE(OpenDocument("annots.pdf"));
3130 FPDF_PAGE page = LoadPage(0);
3131 ASSERT_TRUE(page);
3132 {
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003133 constexpr char kExpectedResult[] =
3134 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3135 "fpdf_text.h";
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003136
Lei Zhang306874b2021-04-16 00:33:36 +00003137 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3138 ASSERT_TRUE(annot);
3139 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3140 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()),
3141 kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003142 }
3143
3144 {
3145 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3146 ASSERT_TRUE(annot);
3147 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3148 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3149 }
3150
3151 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3152
3153 UnloadPage(page);
3154}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003155
3156TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3157 // Open a file with radio button widget annotations and load its first page.
3158 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3159 FPDF_PAGE page = LoadPage(0);
3160 ASSERT_TRUE(page);
3161
3162 {
3163 // Checks for bad annot.
3164 EXPECT_EQ(-1,
3165 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3166
3167 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3168 ASSERT_TRUE(annot);
3169
3170 // Checks for bad form handle.
3171 EXPECT_EQ(-1,
3172 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3173
3174 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3175 }
3176
3177 UnloadPage(page);
3178}
3179
3180TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3181 // Open a file with checkbox widget annotations and load its first page.
3182 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3183 FPDF_PAGE page = LoadPage(0);
3184 ASSERT_TRUE(page);
3185
3186 {
3187 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3188 ASSERT_TRUE(annot);
3189 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3190 }
3191
3192 UnloadPage(page);
3193}
3194
3195TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3196 // Open a file with ink annotations and load its first page.
3197 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3198 FPDF_PAGE page = LoadPage(0);
3199 ASSERT_TRUE(page);
3200
3201 {
3202 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3203 ASSERT_TRUE(annot);
3204 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3205 }
3206
3207 UnloadPage(page);
3208}
3209
3210TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3211 // Open a file with radio button widget annotations and load its first page.
3212 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3213 FPDF_PAGE page = LoadPage(0);
3214 ASSERT_TRUE(page);
3215
3216 {
3217 // Checks for bad annot.
3218 EXPECT_EQ(-1,
3219 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3220
3221 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3222 ASSERT_TRUE(annot);
3223
3224 // Checks for bad form handle.
3225 EXPECT_EQ(-1,
3226 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3227
3228 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3229 }
3230
3231 UnloadPage(page);
3232}
3233
3234TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3235 // Open a file with checkbox widget annotations and load its first page.
3236 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3237 FPDF_PAGE page = LoadPage(0);
3238 ASSERT_TRUE(page);
3239
3240 {
3241 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3242 ASSERT_TRUE(annot);
3243 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3244 }
3245
3246 UnloadPage(page);
3247}
3248
3249TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3250 // Open a file with ink annotations and load its first page.
3251 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3252 FPDF_PAGE page = LoadPage(0);
3253 ASSERT_TRUE(page);
3254
3255 {
3256 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3257 ASSERT_TRUE(annot);
3258 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3259 }
3260
3261 UnloadPage(page);
3262}
3263
3264TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3265 // Open a file with radio button widget annotations and load its first page.
3266 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3267 FPDF_PAGE page = LoadPage(0);
3268 ASSERT_TRUE(page);
3269
3270 {
3271 // Checks for bad annot.
3272 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3273 form_handle(), /*annot=*/nullptr,
3274 /*buffer=*/nullptr, /*buflen=*/0));
3275
3276 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3277 ASSERT_TRUE(annot);
3278
3279 // Checks for bad form handle.
3280 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3281 /*hHandle=*/nullptr, annot.get(),
3282 /*buffer=*/nullptr, /*buflen=*/0));
3283
3284 unsigned long length_bytes =
3285 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3286 /*buffer=*/nullptr, /*buflen=*/0);
3287 ASSERT_EQ(14u, length_bytes);
3288 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3289 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3290 buf.data(), length_bytes));
3291 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3292 }
3293
3294 UnloadPage(page);
3295}
3296
3297TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3298 // Open a file with checkbox widget annotations and load its first page.
3299 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3300 FPDF_PAGE page = LoadPage(0);
3301 ASSERT_TRUE(page);
3302
3303 {
3304 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3305 ASSERT_TRUE(annot);
3306
3307 unsigned long length_bytes =
3308 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3309 /*buffer=*/nullptr, /*buflen=*/0);
3310 ASSERT_EQ(8u, length_bytes);
3311 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3312 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3313 buf.data(), length_bytes));
3314 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3315 }
3316
3317 UnloadPage(page);
3318}
3319
3320TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3321 // Open a file with ink annotations and load its first page.
3322 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3323 FPDF_PAGE page = LoadPage(0);
3324 ASSERT_TRUE(page);
3325
3326 {
3327 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3328 ASSERT_TRUE(annot);
3329 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3330 /*buffer=*/nullptr,
3331 /*buflen=*/0));
3332 }
3333
3334 UnloadPage(page);
3335}
Miklos Vajnad1a24fb2020-10-26 18:07:13 +00003336
3337TEST_F(FPDFAnnotEmbedderTest, Redactannotation) {
3338 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3339 FPDF_PAGE page = LoadPage(0);
3340 ASSERT_TRUE(page);
3341 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3342
3343 {
3344 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3345 ASSERT_TRUE(annot);
3346 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3347 }
3348
3349 UnloadPage(page);
3350}
Miklos Vajna09ecef62020-11-10 21:50:38 +00003351
3352TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) {
3353 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3354 FPDF_PAGE page = LoadPage(0);
3355 ASSERT_TRUE(page);
3356 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3357
3358 {
3359 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3360 ASSERT_TRUE(annot);
3361
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003362 // FPDFAnnot_GetVertices() positive testing.
Miklos Vajna09ecef62020-11-10 21:50:38 +00003363 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3364 const size_t kExpectedSize = 3;
3365 ASSERT_EQ(kExpectedSize, size);
3366 std::vector<FS_POINTF> vertices_buffer(size);
3367 EXPECT_EQ(size,
3368 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3369 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3370 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3371 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3372 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3373 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3374 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3375
3376 // FPDFAnnot_GetVertices() negative testing.
3377 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3378
3379 // vertices_buffer is not overwritten if it is too small.
3380 vertices_buffer.resize(1);
3381 vertices_buffer[0].x = 42;
3382 vertices_buffer[0].y = 43;
3383 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3384 vertices_buffer.size());
3385 EXPECT_EQ(kExpectedSize, size);
3386 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3387 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3388 }
3389
3390 {
3391 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3392 ASSERT_TRUE(annot);
3393
3394 // This has an odd number of elements in the vertices array, ignore the last
3395 // element.
3396 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3397 const size_t kExpectedSize = 3;
3398 ASSERT_EQ(kExpectedSize, size);
3399 std::vector<FS_POINTF> vertices_buffer(size);
3400 EXPECT_EQ(size,
3401 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3402 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3403 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3404 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3405 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3406 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3407 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3408 }
3409
3410 {
3411 // Wrong annotation type.
3412 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3413 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3414 }
3415
3416 UnloadPage(page);
3417}
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003418
3419TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) {
3420 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3421 FPDF_PAGE page = LoadPage(0);
3422 ASSERT_TRUE(page);
3423 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3424
3425 {
3426 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3427 ASSERT_TRUE(annot);
3428
3429 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3430 // testing.
3431 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3432 const size_t kExpectedSize = 1;
3433 ASSERT_EQ(kExpectedSize, size);
3434 const unsigned long kPathIndex = 0;
3435 unsigned long path_size =
3436 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3437 const size_t kExpectedPathSize = 3;
3438 ASSERT_EQ(kExpectedPathSize, path_size);
3439 std::vector<FS_POINTF> path_buffer(path_size);
3440 EXPECT_EQ(path_size,
3441 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3442 path_buffer.data(), path_size));
3443 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3444 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3445 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3446 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3447 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3448 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3449
3450 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3451 // testing.
3452 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3453 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3454
3455 // out of bounds path_index.
3456 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3457
3458 // path_buffer is not overwritten if it is too small.
3459 path_buffer.resize(1);
3460 path_buffer[0].x = 42;
3461 path_buffer[0].y = 43;
3462 path_size = FPDFAnnot_GetInkListPath(
3463 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3464 EXPECT_EQ(kExpectedSize, size);
3465 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3466 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3467 }
3468
3469 {
3470 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3471 ASSERT_TRUE(annot);
3472
3473 // This has an odd number of elements in the path array, ignore the last
3474 // element.
3475 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3476 const size_t kExpectedSize = 1;
3477 ASSERT_EQ(kExpectedSize, size);
3478 const unsigned long kPathIndex = 0;
3479 unsigned long path_size =
3480 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3481 const size_t kExpectedPathSize = 3;
3482 ASSERT_EQ(kExpectedPathSize, path_size);
3483 std::vector<FS_POINTF> path_buffer(path_size);
3484 EXPECT_EQ(path_size,
3485 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3486 path_buffer.data(), path_size));
3487 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3488 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3489 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3490 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3491 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3492 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3493 }
3494
3495 {
3496 // Wrong annotation type.
3497 ScopedFPDFAnnotation polygon_annot(
3498 FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON));
3499 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3500 const unsigned long kPathIndex = 0;
3501 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3502 nullptr, 0));
3503 }
3504
3505 UnloadPage(page);
3506}
Miklos Vajna30f45a62020-12-04 19:12:31 +00003507
3508TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) {
3509 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3510 FPDF_PAGE page = LoadPage(0);
3511 ASSERT_TRUE(page);
3512 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3513
3514 {
3515 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3516 ASSERT_TRUE(annot);
3517
3518 // FPDFAnnot_GetVertices() positive testing.
3519 FS_POINTF start;
3520 FS_POINTF end;
3521 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3522 EXPECT_FLOAT_EQ(159.0f, start.x);
3523 EXPECT_FLOAT_EQ(296.0f, start.y);
3524 EXPECT_FLOAT_EQ(472.0f, end.x);
3525 EXPECT_FLOAT_EQ(243.42f, end.y);
3526
3527 // FPDFAnnot_GetVertices() negative testing.
3528 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3529 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3530 }
3531
3532 {
3533 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3534 ASSERT_TRUE(annot);
3535
3536 // Too few elements in the line array.
3537 FS_POINTF start;
3538 FS_POINTF end;
3539 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3540 }
3541
3542 {
3543 // Wrong annotation type.
3544 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3545 FS_POINTF start;
3546 FS_POINTF end;
3547 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3548 }
3549
3550 UnloadPage(page);
3551}
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003552
3553TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) {
3554 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3555 FPDF_PAGE page = LoadPage(0);
3556 ASSERT_TRUE(page);
3557 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3558
3559 {
3560 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3561 ASSERT_TRUE(annot);
3562
3563 // FPDFAnnot_GetBorder() positive testing.
3564 float horizontal_radius;
3565 float vertical_radius;
3566 float border_width;
3567 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3568 &vertical_radius, &border_width));
3569 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3570 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3571 EXPECT_FLOAT_EQ(2.0f, border_width);
3572
3573 // FPDFAnnot_GetBorder() negative testing.
3574 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3575 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3576 }
3577
3578 {
3579 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3580 ASSERT_TRUE(annot);
3581
3582 // Too few elements in the border array.
3583 float horizontal_radius;
3584 float vertical_radius;
3585 float border_width;
3586 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3587 &vertical_radius, &border_width));
Lei Zhang21ea6652021-04-15 23:24:46 +00003588
3589 // FPDFAnnot_SetBorder() positive testing.
3590 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3591 /*vertical_radius=*/3.5f,
3592 /*border_width=*/4.0f));
3593
3594 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3595 &vertical_radius, &border_width));
3596 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3597 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3598 EXPECT_FLOAT_EQ(4.0f, border_width);
3599
3600 // FPDFAnnot_SetBorder() negative testing.
3601 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3602 /*vertical_radius=*/2.5f,
3603 /*border_width=*/3.0f));
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003604 }
3605
3606 UnloadPage(page);
3607}
Lei Zhang24de1492021-04-19 18:06:43 +00003608
3609// Due to https://crbug.com/pdfium/570, the AnnotationBorder test above cannot
3610// actually render the line annotations inside line_annot.pdf. For now, use a
3611// square annotation in annots.pdf for testing.
3612TEST_F(FPDFAnnotEmbedderTest, AnnotationBorderRendering) {
3613 ASSERT_TRUE(OpenDocument("annots.pdf"));
3614 FPDF_PAGE page = LoadPage(1);
3615 ASSERT_TRUE(page);
3616 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
3617
3618#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3619 constexpr char kOriginalChecksum[] = "bc9ba381d6d46ff93ed0b5288b763b60";
3620 constexpr char kModifiedChecksum[] = "0f3e10336f67cb6a8731a06d26d01e50";
3621#elif defined(OS_WIN)
3622 constexpr char kOriginalChecksum[] = "f9fcab6ac610ee2347c4eba6be86a90c";
3623 constexpr char kModifiedChecksum[] = "e0e9ad5b67ef84288d446861265898b3";
3624#elif defined(OS_APPLE)
3625 constexpr char kOriginalChecksum[] = "1839f5df5fb4fae10cf3793568e73ede";
3626 constexpr char kModifiedChecksum[] = "abd4f5d1c3b8d8cfc572b389e589da5a";
3627#else
3628 constexpr char kOriginalChecksum[] = "ccf6667b34ec2452bea0b5f1a0194191";
3629 constexpr char kModifiedChecksum[] = "1bbdb473d0757843e82053b0bd3298bc";
3630#endif
3631
3632 {
3633 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
3634 ASSERT_TRUE(annot);
3635 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
3636
3637 {
3638 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3639 CompareBitmap(bitmap.get(), 612, 792, kOriginalChecksum);
3640 }
3641
3642 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3643 /*vertical_radius=*/3.5f,
3644 /*border_width=*/4.0f));
3645
3646 {
3647 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3648 CompareBitmap(bitmap.get(), 612, 792, kModifiedChecksum);
3649 }
3650 }
3651
3652 // Save the document and close the page.
3653 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
3654 UnloadPage(page);
3655
3656 ASSERT_TRUE(OpenSavedDocument());
3657 page = LoadSavedPage(1);
3658 ASSERT_TRUE(page);
3659 VerifySavedRendering(page, 612, 792, kModifiedChecksum);
3660
3661 CloseSavedPage(page);
3662 CloseSavedDocument();
3663}