blob: f77fef0e31a43d405a8a35185af84a282c37e483 [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));
1399 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1400 kBitmapSize, 0, 0));
1401 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1402 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1403 }
Jane Liu36567742017-07-06 11:13:35 -04001404
1405 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001406 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001407 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001408 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001409 }
Jane Liu36567742017-07-06 11:13:35 -04001410
Lei Zhanga21d5932018-02-05 18:28:38 +00001411 {
1412 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001413 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001414 ASSERT_TRUE(annot);
1415 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1416 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1417 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001418
Lei Zhanga21d5932018-02-05 18:28:38 +00001419 // Modify the image in the new annotation.
1420 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1421 0xff000000);
1422 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1423 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1424 }
Jane Liu36567742017-07-06 11:13:35 -04001425
Lei Zhangec618142021-04-13 17:36:46 +00001426 // Save the document and close the page.
Jane Liu36567742017-07-06 11:13:35 -04001427 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001428 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001429 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001430
1431 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001432 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001433}
1434
Hui Yingst6cd754f2020-05-14 04:05:25 +00001435TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001436#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001437#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001438 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1439 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001440#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001441 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1442 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001443#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001444#else
1445#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001446 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1447 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Lei Zhang0c03d632020-07-30 17:05:36 +00001448#elif defined(OS_APPLE)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001449 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1450 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001451#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001452 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1453 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001454#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001455#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001456
1457 // Open a file with two annotations and load its first page.
1458 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001459 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001460 ASSERT_TRUE(page);
1461 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1462
1463 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001464 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001465 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001466 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001467 }
Jane Liu36567742017-07-06 11:13:35 -04001468
Lei Zhanga21d5932018-02-05 18:28:38 +00001469 {
1470 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001471 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001472 ASSERT_TRUE(annot);
1473 FS_RECTF rect;
1474 rect.left = 200.f;
1475 rect.bottom = 550.f;
1476 rect.right = 450.f;
1477 rect.top = 650.f;
1478 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001479
Lei Zhanga21d5932018-02-05 18:28:38 +00001480 // Add a translucent text object to the new annotation.
1481 FPDF_PAGEOBJECT text_object =
1482 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1483 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001484 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001485 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1486 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001487 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001488 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1489 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1490 }
Jane Liu36567742017-07-06 11:13:35 -04001491
1492 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001493 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001494 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001495 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001496 }
Jane Liu36567742017-07-06 11:13:35 -04001497
Lei Zhanga21d5932018-02-05 18:28:38 +00001498 {
1499 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001500 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001501 ASSERT_TRUE(annot);
1502 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1503 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1504 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001505
Lei Zhanga21d5932018-02-05 18:28:38 +00001506 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001507 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001508 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1509 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1510 }
Jane Liu36567742017-07-06 11:13:35 -04001511
1512 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001513 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001514 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001515 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001516 }
Jane Liu36567742017-07-06 11:13:35 -04001517
1518 // Remove the new annotation, and check that the page renders as before.
1519 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001520 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001521 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001522 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001523 }
Jane Liu36567742017-07-06 11:13:35 -04001524
1525 UnloadPage(page);
1526}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001527
Hui Yingst9b6b1542020-07-27 16:11:12 +00001528TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001529 // Open a file with four annotations and load its first page.
1530 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001531 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001532 ASSERT_TRUE(page);
1533
Lei Zhang4f556b82019-04-08 16:32:41 +00001534 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001535
Lei Zhanga21d5932018-02-05 18:28:38 +00001536 {
1537 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001538 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001539 ASSERT_TRUE(annot);
1540
1541 // Check that a non-existent key does not exist.
1542 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1543
1544 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001545 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001546 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001547 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1548 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1549 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001550
1551 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001552 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001553 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001554 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001555 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001556 ASSERT_EQ(66u, length_bytes);
1557 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1558 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1559 length_bytes));
1560 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1561 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001562
1563 // Check that the string value of the modified date is correct.
1564 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001565 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1566 nullptr, 0);
1567 ASSERT_EQ(44u, length_bytes);
1568 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001569 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001570 buf.data(), length_bytes));
1571 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001572
1573 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001574 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001575 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1576 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001577 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001578
Lei Zhangec618142021-04-13 17:36:46 +00001579 // Save the document and close the page.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001580 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001581 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001582
Hui Yingst9b6b1542020-07-27 16:11:12 +00001583#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001584#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001585 static const char kMd5[] = "7a2b712ca88d7b71f125ea3f9c88e57a";
1586#else
1587 static const char kMd5[] = "626d25c5aa5baf67d22d9a0e1c23f6aa";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001588#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001589#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001590#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001591 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001592#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001593 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001594#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001595 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001596#endif
Hui Yingst9b6b1542020-07-27 16:11:12 +00001597#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Dan Sinclair971a6742018-03-28 19:23:25 +00001598
1599 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001600 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001601 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001602 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001603 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001604 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001605 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001606
Lei Zhanga21d5932018-02-05 18:28:38 +00001607 // Check that the string value of the modified date is the newly-set value.
1608 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001609 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001610 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001611 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001612 ASSERT_EQ(44u, length_bytes);
1613 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001614 EXPECT_EQ(44u,
1615 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001616 buf.data(), length_bytes));
1617 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001618 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001619
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001620 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001621 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001622}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001623
rycsmith3e785602019-03-05 21:48:36 +00001624TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001625 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001626 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1627 FPDF_PAGE page = LoadPage(0);
1628 ASSERT_TRUE(page);
1629 {
1630 // First two annotations do not have "MaxLen" attribute.
1631 for (int i = 0; i < 2; i++) {
1632 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1633 ASSERT_TRUE(annot);
1634
1635 // Verify that no "MaxLen" key present.
1636 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1637
1638 float value;
1639 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1640 }
1641
1642 // Annotation in index 2 has "MaxLen" of 10.
1643 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1644 ASSERT_TRUE(annot);
1645
1646 // Verify that "MaxLen" key present.
1647 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1648
1649 float value;
1650 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1651 EXPECT_FLOAT_EQ(10.0f, value);
1652
1653 // Check bad inputs.
1654 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1655 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1656 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1657 // Ask for key that exists but is not a number.
1658 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1659 }
1660
1661 UnloadPage(page);
1662}
1663
Lei Zhangab41f252018-12-23 03:10:50 +00001664TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001665 // Open a file with four annotations and load its first page.
1666 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001667 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001668 ASSERT_TRUE(page);
1669
Lei Zhanga21d5932018-02-05 18:28:38 +00001670 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001671 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1672 static constexpr size_t kExpectNormalAPLength = 73970;
1673
Lei Zhanga21d5932018-02-05 18:28:38 +00001674 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001675 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001676 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001677
Lei Zhanga21d5932018-02-05 18:28:38 +00001678 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001679 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001680 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001681 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001682
Lei Zhanga21d5932018-02-05 18:28:38 +00001683 // Check that the string value of an AP is not returned if the buffer is too
1684 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001685 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1686 // Write in the buffer to verify it's not overwritten.
1687 memcpy(buf.data(), "abcdefgh", 8);
1688 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001689 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001690 buf.data(), normal_length_bytes - 1));
1691 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001692
Lei Zhanga21d5932018-02-05 18:28:38 +00001693 // Check that the string value of an AP is returned through a buffer that is
1694 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001695 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001696 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001697 buf.data(), normal_length_bytes));
1698 EXPECT_EQ(kMd5NormalAP,
1699 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1700 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001701
Lei Zhanga21d5932018-02-05 18:28:38 +00001702 // Check that the string value of an AP is returned through a buffer that is
1703 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001704 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1705 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001706 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001707 buf.data(), normal_length_bytes + 2));
1708 EXPECT_EQ(kMd5NormalAP,
1709 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1710 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001711
Lei Zhanga21d5932018-02-05 18:28:38 +00001712 // Check that getting an AP for a mode that does not have an AP returns an
1713 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001714 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001715 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001716 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001717
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001718 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001719 EXPECT_EQ(2u,
1720 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001721 buf.data(), 1000));
1722 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001723
Lei Zhanga21d5932018-02-05 18:28:38 +00001724 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001725 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001726 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001727 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001728 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001729 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001730 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001731
Lei Zhanga21d5932018-02-05 18:28:38 +00001732 // Set the AP correctly now.
1733 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001734 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001735
Lei Zhanga21d5932018-02-05 18:28:38 +00001736 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001737 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001738 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001739 ASSERT_EQ(24u, rollover_length_bytes);
1740 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001741 EXPECT_EQ(24u,
1742 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001743 buf.data(), rollover_length_bytes));
1744 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001745
Lei Zhanga21d5932018-02-05 18:28:38 +00001746 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001747 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1748 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001749 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001750 buf.data(), normal_length_bytes));
1751 EXPECT_EQ(kMd5NormalAP,
1752 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1753 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001754 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001755
1756 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001757 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001758 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001759
Lei Zhang0b494052019-01-31 21:41:15 +00001760 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001761 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001762 ASSERT_TRUE(page);
Lei Zhanga21d5932018-02-05 18:28:38 +00001763 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001764 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001765
Lei Zhanga21d5932018-02-05 18:28:38 +00001766 // Check that the new annotation value is equal to the value we set before
1767 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001768 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001769 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001770 ASSERT_EQ(24u, rollover_length_bytes);
1771 std::vector<FPDF_WCHAR> buf =
1772 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001773 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1774 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001775 buf.data(), rollover_length_bytes));
1776 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001777 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001778
1779 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001780 CloseSavedPage(page);
1781 CloseSavedDocument();
1782}
1783
Lei Zhangab41f252018-12-23 03:10:50 +00001784TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001785 // Open a file with four annotations and load its first page.
1786 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001787 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001788 ASSERT_TRUE(page);
1789
Lei Zhanga21d5932018-02-05 18:28:38 +00001790 {
1791 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001792 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001793 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001794
Lei Zhanga21d5932018-02-05 18:28:38 +00001795 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001796 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001797 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001798 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001799 EXPECT_EQ(73970u,
1800 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1801 nullptr, 0));
1802 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1803 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001804
Lei Zhanga21d5932018-02-05 18:28:38 +00001805 // Check that setting the Down AP to null removes the Down entry but keeps
1806 // Normal intact.
1807 EXPECT_TRUE(
1808 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1809 EXPECT_EQ(73970u,
1810 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1811 nullptr, 0));
1812 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1813 nullptr, 0));
1814 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001815
Lei Zhang75c81712018-02-08 17:22:39 +00001816 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001817}
1818
Lei Zhangab41f252018-12-23 03:10:50 +00001819TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001820 // Open a file with four annotations and load its first page.
1821 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001822 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001823 ASSERT_TRUE(page);
1824
Lei Zhanga21d5932018-02-05 18:28:38 +00001825 {
1826 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001827 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001828 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001829
Lei Zhanga21d5932018-02-05 18:28:38 +00001830 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001831 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001832 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001833 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001834 EXPECT_EQ(73970u,
1835 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1836 nullptr, 0));
1837 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1838 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001839
Lei Zhanga21d5932018-02-05 18:28:38 +00001840 // Check that setting the Normal AP to null removes the whole AP dictionary.
1841 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1842 nullptr));
1843 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1844 nullptr, 0));
1845 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1846 nullptr, 0));
1847 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001848
Lei Zhang75c81712018-02-08 17:22:39 +00001849 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001850}
1851
Lei Zhangab41f252018-12-23 03:10:50 +00001852TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001853 // Open a file with annotations and load its first page.
1854 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001855 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001856 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001857 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001858
Lei Zhanga21d5932018-02-05 18:28:38 +00001859 {
1860 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001861 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001862 ASSERT_TRUE(annot);
1863 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1864 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001865 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001866 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1867 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1868 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001869
Lei Zhanga21d5932018-02-05 18:28:38 +00001870 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001871 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001872 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1873 ASSERT_TRUE(popup);
1874 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1875 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1876 FS_RECTF rect;
1877 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1878 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1879 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001880
Lei Zhanga21d5932018-02-05 18:28:38 +00001881 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1882 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001883 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001884 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1885 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001886
Lei Zhanga21d5932018-02-05 18:28:38 +00001887 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1888 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001889 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001890 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001891 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1892 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001893 }
Jane Liu300bb272017-08-21 14:37:53 -04001894
Jane Liu300bb272017-08-21 14:37:53 -04001895 UnloadPage(page);
1896}
1897
Lei Zhangab41f252018-12-23 03:10:50 +00001898TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001899 // Open file with form text fields.
1900 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001901 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001902 ASSERT_TRUE(page);
1903
Lei Zhanga21d5932018-02-05 18:28:38 +00001904 {
1905 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001906 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001907 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001908
Lei Zhanga21d5932018-02-05 18:28:38 +00001909 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001910 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001911 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001912 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1913 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1914 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001915 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001916 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001917
Lei Zhanga21d5932018-02-05 18:28:38 +00001918 {
1919 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001920 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001921 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001922
Lei Zhanga21d5932018-02-05 18:28:38 +00001923 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001924 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001925 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001926 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1927 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1928 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001929 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1930 }
1931
1932 {
1933 // Retrieve the fourth annotation: user-editable password text field.
1934 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1935 ASSERT_TRUE(annot);
1936
1937 // Check that the flag values are as expected.
1938 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1939 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001940 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1941 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1942 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001943 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001944 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001945
1946 UnloadPage(page);
1947}
1948
Lei Zhangab41f252018-12-23 03:10:50 +00001949TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001950 // Open file with form text fields.
1951 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001952 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001953 ASSERT_TRUE(page);
1954
Lei Zhanga21d5932018-02-05 18:28:38 +00001955 {
1956 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001957 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001958 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001959
Lei Zhanga21d5932018-02-05 18:28:38 +00001960 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001961 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001962 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001963 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1964 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001965 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1966 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001967 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001968 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001969
Lei Zhanga21d5932018-02-05 18:28:38 +00001970 {
1971 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001972 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001973 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001974
Lei Zhanga21d5932018-02-05 18:28:38 +00001975 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001976 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001977 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001978 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1979 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001980 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1981 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001982 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001983 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001984
Lei Zhanga21d5932018-02-05 18:28:38 +00001985 {
1986 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001987 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001988 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001989
Lei Zhanga21d5932018-02-05 18:28:38 +00001990 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001991 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001992 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001993 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1994 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001995 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1996 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001997 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001998 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001999
2000 UnloadPage(page);
2001}
Diana Gage40870db2017-07-19 18:16:03 -07002002
Lei Zhangab41f252018-12-23 03:10:50 +00002003TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07002004 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002005 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002006 FPDF_PAGE page = LoadPage(0);
2007 ASSERT_TRUE(page);
2008
2009 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00002010 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
2011 EXPECT_FALSE(
2012 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00002013
Lei Zhang8da98232019-12-11 23:29:33 +00002014 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00002015 {
2016 // Verify there is an annotation.
2017 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002018 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00002019 EXPECT_TRUE(annot);
2020 }
2021
2022 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00002023 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
2024 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
2025 EXPECT_FALSE(
2026 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07002027
2028 UnloadPage(page);
2029}
2030
Lei Zhangab41f252018-12-23 03:10:50 +00002031TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07002032 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002033 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002034 FPDF_PAGE page = LoadPage(0);
2035 ASSERT_TRUE(page);
2036
Lei Zhanga21d5932018-02-05 18:28:38 +00002037 {
2038 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002039 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002040 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002041 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002042 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002043
Lei Zhanga21d5932018-02-05 18:28:38 +00002044 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002045 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00002046 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2047 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002048 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2049 }
Diana Gage40870db2017-07-19 18:16:03 -07002050
Lei Zhanga21d5932018-02-05 18:28:38 +00002051 {
2052 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002053 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002054 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002055 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002056 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002057
Lei Zhanga21d5932018-02-05 18:28:38 +00002058 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002059 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002060 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002061 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2062 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002063 }
Diana Gage40870db2017-07-19 18:16:03 -07002064
2065 UnloadPage(page);
2066}
2067
Lei Zhangab41f252018-12-23 03:10:50 +00002068TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07002069 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002070 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002071 FPDF_PAGE page = LoadPage(0);
2072 ASSERT_TRUE(page);
2073
Lei Zhanga21d5932018-02-05 18:28:38 +00002074 {
2075 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002076 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002077 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002078 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002079 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002080
Lei Zhanga21d5932018-02-05 18:28:38 +00002081 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002082 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002083 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002084 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2085 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002086 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2087 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002088 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002089 }
Diana Gage40870db2017-07-19 18:16:03 -07002090
Lei Zhanga21d5932018-02-05 18:28:38 +00002091 {
2092 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002093 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002094 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002095 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002096 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002097
Lei Zhanga21d5932018-02-05 18:28:38 +00002098 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002099 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002100 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002101 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2102 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002103 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2104 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002105 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002106 }
Diana Gage40870db2017-07-19 18:16:03 -07002107
Lei Zhanga21d5932018-02-05 18:28:38 +00002108 {
2109 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002110 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002111 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002112 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002113 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002114
Lei Zhanga21d5932018-02-05 18:28:38 +00002115 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002116 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002117 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002118 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2119 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002120 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2121 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002122 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002123 }
Diana Gage40870db2017-07-19 18:16:03 -07002124
2125 UnloadPage(page);
2126}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002127
Hui Yingst603dcd82020-11-09 22:36:46 +00002128TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
Lei Zhang03e5e682019-09-16 19:45:55 +00002129#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst603dcd82020-11-09 22:36:46 +00002130 static const char kExpectedBitmap[] = "a1ea1ceebb26922fae576cb79ce63af0";
Lei Zhang03e5e682019-09-16 19:45:55 +00002131#else
Lei Zhang992e7e22019-02-04 19:20:58 +00002132 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
Hui Yingst603dcd82020-11-09 22:36:46 +00002133#endif
2134 static constexpr size_t kExpectedSize = 1609;
Lei Zhang992e7e22019-02-04 19:20:58 +00002135
2136 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2137
2138 FPDF_PAGE page = LoadPage(0);
2139 ASSERT_TRUE(page);
2140
2141 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2142 EXPECT_EQ(kExpectedSize, GetString().size());
2143 ClearString();
2144
2145 for (size_t i = 0; i < 10; ++i) {
2146 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2147 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2148
2149 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2150 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2151 // equal, not bigger.
2152 EXPECT_LT(kExpectedSize, GetString().size());
2153 ClearString();
2154 }
2155
2156 UnloadPage(page);
2157}
2158
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002159TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2160 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2161 FPDF_PAGE page = LoadPage(0);
2162 ASSERT_TRUE(page);
2163 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2164
2165 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002166 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002167 static const size_t kBufSize = 12;
2168 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002169
2170 {
2171 // Add a text annotation to the page.
2172 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2173 ASSERT_TRUE(annot);
2174 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2175 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2176
2177 // Make sure there is no test key, add set a value there, and read it back.
2178 std::fill(buf.begin(), buf.end(), 'x');
2179 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002180 kBufSize));
2181 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002182
Lei Zhangf0f67682019-04-08 17:03:21 +00002183 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002184 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2185
2186 std::fill(buf.begin(), buf.end(), 'x');
2187 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002188 kBufSize));
2189 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002190 }
2191
Lei Zhang05ec64c2019-01-09 03:00:06 +00002192 {
2193 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2194 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002195 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002196 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002197 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2198 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2199 // Also do the same test for its appearance string.
2200 std::fill(buf.begin(), buf.end(), 'x');
2201 ASSERT_EQ(2u,
2202 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002203 buf.data(), kBufSize));
2204 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002205
Lei Zhangf0f67682019-04-08 17:03:21 +00002206 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002207 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2208 text.get()));
2209
2210 std::fill(buf.begin(), buf.end(), 'x');
2211 ASSERT_EQ(6u,
2212 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002213 buf.data(), kBufSize));
2214 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002215 }
2216
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002217 UnloadPage(page);
2218
2219 {
2220 // Save a copy, open the copy, and check the annotation again.
2221 // Note that it renders the rotation.
2222 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002223 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002224 FPDF_PAGE saved_page = LoadSavedPage(0);
2225 ASSERT_TRUE(saved_page);
2226
Lei Zhang05ec64c2019-01-09 03:00:06 +00002227 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002228 {
2229 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2230 ASSERT_TRUE(annot);
2231 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2232
2233 std::fill(buf.begin(), buf.end(), 'x');
2234 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002235 kBufSize));
2236 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002237 }
2238
Lei Zhang05ec64c2019-01-09 03:00:06 +00002239 {
2240 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2241 ASSERT_TRUE(annot);
2242 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2243 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2244
2245 std::fill(buf.begin(), buf.end(), 'x');
2246 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002247 kBufSize));
2248 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002249 }
2250
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002251 CloseSavedPage(saved_page);
2252 CloseSavedDocument();
2253 }
2254}
rycsmithcb752f32019-02-21 18:40:53 +00002255
2256TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2257 // Open a file with combobox widget annotations and load its first page.
2258 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2259 FPDF_PAGE page = LoadPage(0);
2260 ASSERT_TRUE(page);
2261
2262 {
2263 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2264 ASSERT_TRUE(annot);
2265
2266 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2267
2268 annot.reset(FPDFPage_GetAnnot(page, 1));
2269 ASSERT_TRUE(annot);
2270
2271 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002272
2273 // Check bad form handle / annot.
2274 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2275 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2276 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002277 }
2278
2279 UnloadPage(page);
2280}
2281
2282TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2283 // Open a file with listbox widget annotations and load its first page.
2284 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2285 FPDF_PAGE page = LoadPage(0);
2286 ASSERT_TRUE(page);
2287
2288 {
2289 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2290 ASSERT_TRUE(annot);
2291
2292 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2293
2294 annot.reset(FPDFPage_GetAnnot(page, 1));
2295 ASSERT_TRUE(annot);
2296
2297 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2298 }
2299
2300 UnloadPage(page);
2301}
2302
2303TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2304 // Open a file with ink annotations and load its first page.
2305 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2306 FPDF_PAGE page = LoadPage(0);
2307 ASSERT_TRUE(page);
2308
2309 {
2310 // annotations do not have "Opt" array and will return -1
2311 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2312 ASSERT_TRUE(annot);
2313
2314 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2315
2316 annot.reset(FPDFPage_GetAnnot(page, 1));
2317 ASSERT_TRUE(annot);
2318
2319 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2320 }
2321
2322 UnloadPage(page);
2323}
2324
2325TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2326 // Open a file with combobox widget annotations and load its first page.
2327 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2328 FPDF_PAGE page = LoadPage(0);
2329 ASSERT_TRUE(page);
2330
2331 {
2332 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2333 ASSERT_TRUE(annot);
2334
2335 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002336 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002337 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002338 ASSERT_EQ(8u, length_bytes);
2339 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002340 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002341 buf.data(), length_bytes));
2342 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002343
2344 annot.reset(FPDFPage_GetAnnot(page, 1));
2345 ASSERT_TRUE(annot);
2346
2347 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002348 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002349 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002350 ASSERT_EQ(12u, length_bytes);
2351 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002352 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002353 buf.data(), length_bytes));
2354 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002355
2356 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002357 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002358 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002359 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002360 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002361 buf.data(), length_bytes));
2362 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002363
Lei Zhange7033c82019-02-26 19:30:49 +00002364 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002365 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2366 nullptr, 0));
2367 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2368 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002369
2370 // Check bad form handle / annot.
2371 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2372 EXPECT_EQ(0u,
2373 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2374 EXPECT_EQ(0u,
2375 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002376 }
2377
2378 UnloadPage(page);
2379}
2380
2381TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2382 // Open a file with listbox widget annotations and load its first page.
2383 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2384 FPDF_PAGE page = LoadPage(0);
2385 ASSERT_TRUE(page);
2386
2387 {
2388 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2389 ASSERT_TRUE(annot);
2390
2391 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002392 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002393 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002394 ASSERT_EQ(8u, length_bytes);
2395 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002396 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002397 buf.data(), length_bytes));
2398 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002399
2400 annot.reset(FPDFPage_GetAnnot(page, 1));
2401 ASSERT_TRUE(annot);
2402
2403 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002404 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002405 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002406 ASSERT_EQ(12u, length_bytes);
2407 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002408 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002409 buf.data(), length_bytes));
2410 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002411
2412 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002413 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002414 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002415 ASSERT_EQ(18u, length_bytes);
2416 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002417 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002418 buf.data(), length_bytes));
2419 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002420
2421 // indices out of range
2422 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2423 nullptr, 0));
2424 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2425 nullptr, 0));
2426 }
2427
2428 UnloadPage(page);
2429}
2430
2431TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2432 // Open a file with ink annotations and load its first page.
2433 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2434 FPDF_PAGE page = LoadPage(0);
2435 ASSERT_TRUE(page);
2436
2437 {
2438 // annotations do not have "Opt" array and will return 0
2439 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2440 ASSERT_TRUE(annot);
2441
2442 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2443 nullptr, 0));
2444
2445 annot.reset(FPDFPage_GetAnnot(page, 1));
2446 ASSERT_TRUE(annot);
2447
2448 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2449 nullptr, 0));
2450 }
2451
2452 UnloadPage(page);
2453}
Ryan Smith09c23b12019-04-25 18:09:06 +00002454
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002455TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2456 // Open a file with combobox widget annotations and load its first page.
2457 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2458 FPDF_PAGE page = LoadPage(0);
2459 ASSERT_TRUE(page);
2460
2461 {
2462 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2463 ASSERT_TRUE(annot);
2464
2465 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2466 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2467 ASSERT_EQ(3, count);
2468 for (int i = 0; i < count; i++) {
2469 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2470 }
2471
2472 annot.reset(FPDFPage_GetAnnot(page, 1));
2473 ASSERT_TRUE(annot);
2474
2475 // Checks for Combobox with Values (/V) object which is just a string.
2476 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2477 ASSERT_EQ(26, count);
2478 for (int i = 0; i < count; i++) {
2479 EXPECT_EQ(i == 1,
2480 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2481 }
2482
2483 // Checks for index outside bound i.e. (index >= CountOption()).
2484 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2485 /*index=*/26));
2486 // Checks for negetive index.
2487 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2488 /*index=*/-1));
2489
2490 // Checks for bad form handle/annot.
2491 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2492 EXPECT_FALSE(
2493 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2494 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2495 }
2496
2497 UnloadPage(page);
2498}
2499
2500TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2501 // Open a file with listbox widget annotations and load its first page.
2502 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2503 FPDF_PAGE page = LoadPage(0);
2504 ASSERT_TRUE(page);
2505
2506 {
2507 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2508 ASSERT_TRUE(annot);
2509
2510 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2511 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2512 ASSERT_EQ(3, count);
2513 for (int i = 0; i < count; i++) {
2514 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2515 }
2516
2517 annot.reset(FPDFPage_GetAnnot(page, 1));
2518 ASSERT_TRUE(annot);
2519
2520 // Checks for Listbox with Values (/V) object which is just a string.
2521 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2522 ASSERT_EQ(26, count);
2523 for (int i = 0; i < count; i++) {
2524 EXPECT_EQ(i == 1,
2525 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2526 }
2527
2528 annot.reset(FPDFPage_GetAnnot(page, 3));
2529 ASSERT_TRUE(annot);
2530
2531 // Checks for Listbox with only Selected indices (/I) object which is an
2532 // array with multiple objects.
2533 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2534 ASSERT_EQ(5, count);
2535 for (int i = 0; i < count; i++) {
2536 bool expected = (i == 1 || i == 3);
2537 EXPECT_EQ(expected,
2538 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2539 }
2540
2541 annot.reset(FPDFPage_GetAnnot(page, 4));
2542 ASSERT_TRUE(annot);
2543
2544 // Checks for Listbox with Values (/V) object which is an array with
2545 // multiple objects.
2546 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2547 ASSERT_EQ(5, count);
2548 for (int i = 0; i < count; i++) {
2549 bool expected = (i == 2 || i == 4);
2550 EXPECT_EQ(expected,
2551 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2552 }
2553
2554 annot.reset(FPDFPage_GetAnnot(page, 5));
2555 ASSERT_TRUE(annot);
2556
2557 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2558 // objects conflict with different lengths.
2559 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2560 ASSERT_EQ(5, count);
2561 for (int i = 0; i < count; i++) {
2562 bool expected = (i == 0 || i == 2);
2563 EXPECT_EQ(expected,
2564 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2565 }
2566 }
2567
2568 UnloadPage(page);
2569}
2570
2571TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2572 // Open a file with multiple form field annotations and load its first page.
2573 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2574 FPDF_PAGE page = LoadPage(0);
2575 ASSERT_TRUE(page);
2576
2577 {
2578 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2579 ASSERT_TRUE(annot);
2580
2581 // Checks for link annotation.
2582 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2583 /*index=*/0));
2584
2585 annot.reset(FPDFPage_GetAnnot(page, 3));
2586 ASSERT_TRUE(annot);
2587
2588 // Checks for text field annotation.
2589 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2590 /*index=*/0));
2591 }
2592
2593 UnloadPage(page);
2594}
2595
Ryan Smith09c23b12019-04-25 18:09:06 +00002596TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2597 // Open a file with combobox annotations and load its first page.
2598 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2599 FPDF_PAGE page = LoadPage(0);
2600 ASSERT_TRUE(page);
2601
2602 {
2603 // All 3 widgets have Tf font size 12.
2604 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2605 ASSERT_TRUE(annot);
2606
2607 float value;
2608 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2609 EXPECT_EQ(12.0, value);
2610
2611 annot.reset(FPDFPage_GetAnnot(page, 1));
2612 ASSERT_TRUE(annot);
2613
2614 float value_two;
2615 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2616 EXPECT_EQ(12.0, value_two);
2617
2618 annot.reset(FPDFPage_GetAnnot(page, 2));
2619 ASSERT_TRUE(annot);
2620
2621 float value_three;
2622 ASSERT_TRUE(
2623 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2624 EXPECT_EQ(12.0, value_three);
2625 }
2626
2627 UnloadPage(page);
2628}
2629
2630TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2631 // Open a file with textfield annotations and load its first page.
2632 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2633 FPDF_PAGE page = LoadPage(0);
2634 ASSERT_TRUE(page);
2635
2636 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002637 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002638 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2639 ASSERT_TRUE(annot);
2640
2641 float value;
2642 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2643 EXPECT_EQ(12.0, value);
2644
2645 annot.reset(FPDFPage_GetAnnot(page, 1));
2646 ASSERT_TRUE(annot);
2647
2648 float value_two;
2649 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2650 EXPECT_EQ(12.0, value_two);
2651
2652 annot.reset(FPDFPage_GetAnnot(page, 2));
2653 ASSERT_TRUE(annot);
2654
2655 float value_three;
2656 ASSERT_TRUE(
2657 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2658 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002659
2660 float value_four;
2661 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2662 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002663 }
2664
2665 UnloadPage(page);
2666}
2667
2668TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2669 // Open a file with ink annotations and load its first page.
2670 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2671 FPDF_PAGE page = LoadPage(0);
2672 ASSERT_TRUE(page);
2673
2674 {
2675 // Annotations that do not have variable text and will return -1.
2676 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2677 ASSERT_TRUE(annot);
2678
2679 float value;
2680 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2681
2682 annot.reset(FPDFPage_GetAnnot(page, 1));
2683 ASSERT_TRUE(annot);
2684
2685 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2686 }
2687
2688 UnloadPage(page);
2689}
2690
2691TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2692 // Open a file with combobox annotations and load its first page.
2693 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2694 FPDF_PAGE page = LoadPage(0);
2695 ASSERT_TRUE(page);
2696
2697 {
2698 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2699 ASSERT_TRUE(annot);
2700
2701 // Check bad form handle / annot.
2702 float value;
2703 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2704 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2705 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2706 }
2707
2708 UnloadPage(page);
2709}
2710
2711TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2712 // Open a file with textfield annotations and load its first page.
2713 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2714 FPDF_PAGE page = LoadPage(0);
2715 ASSERT_TRUE(page);
2716
2717 {
2718 // Obtain the first annotation, a text field with negative font size, -12.
2719 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2720 ASSERT_TRUE(annot);
2721
2722 float value;
2723 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2724 EXPECT_EQ(-12.0, value);
2725 }
2726
2727 UnloadPage(page);
2728}
Ryan Smith23fdf892019-07-17 21:51:26 +00002729
2730TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2731 // Open a file with checkbox and radiobuttons widget annotations and load its
2732 // first page.
2733 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2734 FPDF_PAGE page = LoadPage(0);
2735 ASSERT_TRUE(page);
2736
2737 {
2738 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2739 ASSERT_TRUE(annot);
2740 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2741 }
2742
2743 UnloadPage(page);
2744}
2745
2746TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2747 // Open a file with checkbox and radiobutton widget annotations and load its
2748 // first page.
2749 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2750 FPDF_PAGE page = LoadPage(0);
2751 ASSERT_TRUE(page);
2752
2753 {
2754 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2755 ASSERT_TRUE(annot);
2756 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2757 }
2758
2759 UnloadPage(page);
2760}
2761
2762TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2763 // Open a file with checkbox and radiobutton widget annotations and load its
2764 // first page.
2765 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2766 FPDF_PAGE page = LoadPage(0);
2767 ASSERT_TRUE(page);
2768
2769 {
2770 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2771 ASSERT_TRUE(annot);
2772 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2773
2774 annot.reset(FPDFPage_GetAnnot(page, 6));
2775 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2776
2777 annot.reset(FPDFPage_GetAnnot(page, 7));
2778 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2779 }
2780
2781 UnloadPage(page);
2782}
2783
2784TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2785 // Open a file with checkbox and radiobutton widget annotations and load its
2786 // first page.
2787 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2788 FPDF_PAGE page = LoadPage(0);
2789 ASSERT_TRUE(page);
2790
2791 {
2792 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2793 ASSERT_TRUE(annot);
2794 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2795
2796 annot.reset(FPDFPage_GetAnnot(page, 3));
2797 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2798
2799 annot.reset(FPDFPage_GetAnnot(page, 4));
2800 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2801 }
2802
2803 UnloadPage(page);
2804}
2805
2806TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2807 // Open a file with checkbox and radiobuttons widget annotations and load its
2808 // first page.
2809 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2810 FPDF_PAGE page = LoadPage(0);
2811 ASSERT_TRUE(page);
2812
2813 {
2814 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2815 ASSERT_TRUE(annot);
2816 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2817 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2818 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2819 }
2820
2821 UnloadPage(page);
2822}
2823
2824TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2825 // Open a file with text widget annotations and load its first page.
2826 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2827 FPDF_PAGE page = LoadPage(0);
2828 ASSERT_TRUE(page);
2829
2830 {
2831 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2832 ASSERT_TRUE(annot);
2833 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2834 }
2835
2836 UnloadPage(page);
2837}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002838
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002839TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2840 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002841 FPDF_PAGE page = LoadPage(0);
2842 ASSERT_TRUE(page);
2843
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002844 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2845
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002846 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002847 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002848 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002849 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002850 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002851
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002852 constexpr int kExpectedAnnotTypes[] = {-1,
2853 FPDF_FORMFIELD_COMBOBOX,
2854 FPDF_FORMFIELD_LISTBOX,
2855 FPDF_FORMFIELD_TEXTFIELD,
2856 FPDF_FORMFIELD_CHECKBOX,
2857 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002858
Lei Zhang4501a502020-05-18 16:52:59 +00002859 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002860 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002861 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002862 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002863 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2864 }
2865 UnloadPage(page);
2866}
2867
2868TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2869 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2870 FPDF_PAGE page = LoadPage(0);
2871 ASSERT_TRUE(page);
2872
2873 {
2874 EXPECT_EQ(0u,
2875 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2876
2877 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2878 ASSERT_TRUE(annot);
2879
2880 EXPECT_EQ(0u,
2881 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2882
2883 unsigned long length_bytes =
2884 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2885 ASSERT_EQ(2u, length_bytes);
2886 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2887 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2888 buf.data(), length_bytes));
2889 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2890 }
2891 {
2892 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2893 ASSERT_TRUE(annot);
2894
2895 unsigned long length_bytes =
2896 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2897 ASSERT_EQ(18u, length_bytes);
2898 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2899 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2900 buf.data(), length_bytes));
2901 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2902 }
2903 UnloadPage(page);
2904}
2905
2906TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2907 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2908 FPDF_PAGE page = LoadPage(0);
2909 ASSERT_TRUE(page);
2910
2911 {
2912 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2913 ASSERT_TRUE(annot);
2914
2915 unsigned long length_bytes =
2916 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2917 ASSERT_EQ(2u, length_bytes);
2918 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2919 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2920 buf.data(), length_bytes));
2921 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2922 }
2923 {
2924 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2925 ASSERT_TRUE(annot);
2926
2927 unsigned long length_bytes =
2928 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2929 ASSERT_EQ(14u, length_bytes);
2930 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2931 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2932 buf.data(), length_bytes));
2933 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2934 }
2935 UnloadPage(page);
2936}
2937
2938TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2939 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2940 FPDF_PAGE page = LoadPage(0);
2941 ASSERT_TRUE(page);
2942
2943 {
2944 EXPECT_EQ(0u,
2945 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2946
2947 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2948 ASSERT_TRUE(annot);
2949
2950 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2951
2952 unsigned long length_bytes =
2953 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2954 ASSERT_EQ(18u, length_bytes);
2955 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2956 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2957 buf.data(), length_bytes));
2958 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2959 }
2960 UnloadPage(page);
2961}
2962
2963TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2964 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2965 FPDF_PAGE page = LoadPage(0);
2966 ASSERT_TRUE(page);
2967
2968 {
2969 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2970 ASSERT_TRUE(annot);
2971
2972 unsigned long length_bytes =
2973 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2974 ASSERT_EQ(30u, length_bytes);
2975 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2976 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2977 buf.data(), length_bytes));
2978 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2979 }
2980 UnloadPage(page);
2981}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002982
Lei Zhang9b444002020-04-17 17:35:23 +00002983TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2984 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002985 FPDF_PAGE page = LoadPage(0);
2986 ASSERT_TRUE(page);
2987
Lei Zhang9b444002020-04-17 17:35:23 +00002988 // Verify widgets are by default focusable.
2989 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2990 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002991
Lei Zhang9b444002020-04-17 17:35:23 +00002992 // Expected annot subtypes for page 0 of annots.pdf.
2993 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2994 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2995 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2996 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2997 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002998
Lei Zhang9b444002020-04-17 17:35:23 +00002999 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00003000 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00003001 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
3002 kExpectedAnnotSubtypes,
3003 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003004
Lei Zhang9b444002020-04-17 17:35:23 +00003005 // Make no annotation type focusable using the preferred method.
3006 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
3007 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003008
Lei Zhang9b444002020-04-17 17:35:23 +00003009 // Restore the focusable type count back to 1, then set it back to 0 using a
3010 // different method.
3011 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
3012 ASSERT_TRUE(
3013 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
3014 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003015
Lei Zhang9b444002020-04-17 17:35:23 +00003016 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00003017 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003018
Lei Zhang9b444002020-04-17 17:35:23 +00003019 // Now make links focusable.
3020 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
3021 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003022
Lei Zhang9b444002020-04-17 17:35:23 +00003023 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00003024 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00003025 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
3026 kExpectedAnnotSubtypes,
3027 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003028
Lei Zhang9b444002020-04-17 17:35:23 +00003029 // Test invalid parameters.
3030 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00003031 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00003032 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00003033 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003034 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
3035
Lei Zhang9b444002020-04-17 17:35:23 +00003036 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
3037 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
3038 subtypes.size()));
3039 EXPECT_FALSE(
3040 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
3041 EXPECT_FALSE(
3042 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003043
3044 UnloadPage(page);
3045}
Lei Zhang671aece2020-04-14 19:02:26 +00003046
Hui Yingstea3816d2020-07-28 22:35:11 +00003047TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00003048 ASSERT_TRUE(OpenDocument("annots.pdf"));
3049 FPDF_PAGE page = LoadPage(0);
3050 ASSERT_TRUE(page);
3051
3052 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003053#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3054 static const char kMd5sum[] = "4ca14c670396711194b40ecc2514969b";
3055#else
Lei Zhang671aece2020-04-14 19:02:26 +00003056#if defined(OS_WIN)
3057 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
Lei Zhang0c03d632020-07-30 17:05:36 +00003058#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003059 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
3060#else
3061 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
3062#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003063#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003064 // Check the initial rendering.
3065 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3066 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3067 }
3068
3069 // Make links and highlights focusable.
3070 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
3071 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00003072 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00003073 ASSERT_TRUE(
3074 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
3075 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3076 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
3077 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
3078 subtypes.size()));
3079 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
3080 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
3081
3082 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003083#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3084 static const char kMd5sum[] = "48cb60b3f9bc364c73582aff3418451e";
3085#else
Lei Zhang671aece2020-04-14 19:02:26 +00003086#if defined(OS_WIN)
3087 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
Lei Zhang0c03d632020-07-30 17:05:36 +00003088#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003089 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
3090#else
3091 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
3092#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003093#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003094 // Focus the first link and check the rendering.
3095 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3096 ASSERT_TRUE(annot);
3097 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3098 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3099 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3100 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3101 }
3102
3103 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003104#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3105 static const char kMd5sum[] = "d09869ff0a209daf179da2d7a58142b1";
3106#else
Lei Zhang671aece2020-04-14 19:02:26 +00003107#if defined(OS_WIN)
3108 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
Lei Zhang0c03d632020-07-30 17:05:36 +00003109#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003110 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
3111#else
3112 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
3113#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003114#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003115 // Focus the first highlight and check the rendering.
3116 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3117 ASSERT_TRUE(annot);
3118 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3119 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3120 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3121 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3122 }
3123
3124 UnloadPage(page);
3125}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003126
3127TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
3128 ASSERT_TRUE(OpenDocument("annots.pdf"));
3129 FPDF_PAGE page = LoadPage(0);
3130 ASSERT_TRUE(page);
3131 {
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003132 constexpr char kExpectedResult[] =
3133 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3134 "fpdf_text.h";
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003135
Lei Zhang306874b2021-04-16 00:33:36 +00003136 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3137 ASSERT_TRUE(annot);
3138 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3139 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()),
3140 kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003141 }
3142
3143 {
3144 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3145 ASSERT_TRUE(annot);
3146 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3147 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3148 }
3149
3150 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3151
3152 UnloadPage(page);
3153}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003154
3155TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3156 // Open a file with radio button widget annotations and load its first page.
3157 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3158 FPDF_PAGE page = LoadPage(0);
3159 ASSERT_TRUE(page);
3160
3161 {
3162 // Checks for bad annot.
3163 EXPECT_EQ(-1,
3164 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3165
3166 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3167 ASSERT_TRUE(annot);
3168
3169 // Checks for bad form handle.
3170 EXPECT_EQ(-1,
3171 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3172
3173 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3174 }
3175
3176 UnloadPage(page);
3177}
3178
3179TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3180 // Open a file with checkbox widget annotations and load its first page.
3181 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3182 FPDF_PAGE page = LoadPage(0);
3183 ASSERT_TRUE(page);
3184
3185 {
3186 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3187 ASSERT_TRUE(annot);
3188 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3189 }
3190
3191 UnloadPage(page);
3192}
3193
3194TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3195 // Open a file with ink annotations and load its first page.
3196 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3197 FPDF_PAGE page = LoadPage(0);
3198 ASSERT_TRUE(page);
3199
3200 {
3201 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3202 ASSERT_TRUE(annot);
3203 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3204 }
3205
3206 UnloadPage(page);
3207}
3208
3209TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3210 // Open a file with radio button widget annotations and load its first page.
3211 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3212 FPDF_PAGE page = LoadPage(0);
3213 ASSERT_TRUE(page);
3214
3215 {
3216 // Checks for bad annot.
3217 EXPECT_EQ(-1,
3218 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3219
3220 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3221 ASSERT_TRUE(annot);
3222
3223 // Checks for bad form handle.
3224 EXPECT_EQ(-1,
3225 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3226
3227 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3228 }
3229
3230 UnloadPage(page);
3231}
3232
3233TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3234 // Open a file with checkbox widget annotations and load its first page.
3235 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3236 FPDF_PAGE page = LoadPage(0);
3237 ASSERT_TRUE(page);
3238
3239 {
3240 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3241 ASSERT_TRUE(annot);
3242 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3243 }
3244
3245 UnloadPage(page);
3246}
3247
3248TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3249 // Open a file with ink annotations and load its first page.
3250 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3251 FPDF_PAGE page = LoadPage(0);
3252 ASSERT_TRUE(page);
3253
3254 {
3255 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3256 ASSERT_TRUE(annot);
3257 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3258 }
3259
3260 UnloadPage(page);
3261}
3262
3263TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3264 // Open a file with radio button widget annotations and load its first page.
3265 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3266 FPDF_PAGE page = LoadPage(0);
3267 ASSERT_TRUE(page);
3268
3269 {
3270 // Checks for bad annot.
3271 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3272 form_handle(), /*annot=*/nullptr,
3273 /*buffer=*/nullptr, /*buflen=*/0));
3274
3275 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3276 ASSERT_TRUE(annot);
3277
3278 // Checks for bad form handle.
3279 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3280 /*hHandle=*/nullptr, annot.get(),
3281 /*buffer=*/nullptr, /*buflen=*/0));
3282
3283 unsigned long length_bytes =
3284 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3285 /*buffer=*/nullptr, /*buflen=*/0);
3286 ASSERT_EQ(14u, length_bytes);
3287 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3288 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3289 buf.data(), length_bytes));
3290 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3291 }
3292
3293 UnloadPage(page);
3294}
3295
3296TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3297 // Open a file with checkbox widget annotations and load its first page.
3298 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3299 FPDF_PAGE page = LoadPage(0);
3300 ASSERT_TRUE(page);
3301
3302 {
3303 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3304 ASSERT_TRUE(annot);
3305
3306 unsigned long length_bytes =
3307 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3308 /*buffer=*/nullptr, /*buflen=*/0);
3309 ASSERT_EQ(8u, length_bytes);
3310 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3311 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3312 buf.data(), length_bytes));
3313 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3314 }
3315
3316 UnloadPage(page);
3317}
3318
3319TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3320 // Open a file with ink annotations and load its first page.
3321 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3322 FPDF_PAGE page = LoadPage(0);
3323 ASSERT_TRUE(page);
3324
3325 {
3326 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3327 ASSERT_TRUE(annot);
3328 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3329 /*buffer=*/nullptr,
3330 /*buflen=*/0));
3331 }
3332
3333 UnloadPage(page);
3334}
Miklos Vajnad1a24fb2020-10-26 18:07:13 +00003335
3336TEST_F(FPDFAnnotEmbedderTest, Redactannotation) {
3337 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3338 FPDF_PAGE page = LoadPage(0);
3339 ASSERT_TRUE(page);
3340 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3341
3342 {
3343 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3344 ASSERT_TRUE(annot);
3345 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3346 }
3347
3348 UnloadPage(page);
3349}
Miklos Vajna09ecef62020-11-10 21:50:38 +00003350
3351TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) {
3352 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3353 FPDF_PAGE page = LoadPage(0);
3354 ASSERT_TRUE(page);
3355 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3356
3357 {
3358 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3359 ASSERT_TRUE(annot);
3360
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003361 // FPDFAnnot_GetVertices() positive testing.
Miklos Vajna09ecef62020-11-10 21:50:38 +00003362 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3363 const size_t kExpectedSize = 3;
3364 ASSERT_EQ(kExpectedSize, size);
3365 std::vector<FS_POINTF> vertices_buffer(size);
3366 EXPECT_EQ(size,
3367 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3368 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3369 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3370 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3371 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3372 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3373 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3374
3375 // FPDFAnnot_GetVertices() negative testing.
3376 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3377
3378 // vertices_buffer is not overwritten if it is too small.
3379 vertices_buffer.resize(1);
3380 vertices_buffer[0].x = 42;
3381 vertices_buffer[0].y = 43;
3382 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3383 vertices_buffer.size());
3384 EXPECT_EQ(kExpectedSize, size);
3385 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3386 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3387 }
3388
3389 {
3390 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3391 ASSERT_TRUE(annot);
3392
3393 // This has an odd number of elements in the vertices array, ignore the last
3394 // element.
3395 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3396 const size_t kExpectedSize = 3;
3397 ASSERT_EQ(kExpectedSize, size);
3398 std::vector<FS_POINTF> vertices_buffer(size);
3399 EXPECT_EQ(size,
3400 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3401 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3402 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3403 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3404 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3405 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3406 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3407 }
3408
3409 {
3410 // Wrong annotation type.
3411 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3412 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3413 }
3414
3415 UnloadPage(page);
3416}
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003417
3418TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) {
3419 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3420 FPDF_PAGE page = LoadPage(0);
3421 ASSERT_TRUE(page);
3422 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3423
3424 {
3425 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3426 ASSERT_TRUE(annot);
3427
3428 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3429 // testing.
3430 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3431 const size_t kExpectedSize = 1;
3432 ASSERT_EQ(kExpectedSize, size);
3433 const unsigned long kPathIndex = 0;
3434 unsigned long path_size =
3435 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3436 const size_t kExpectedPathSize = 3;
3437 ASSERT_EQ(kExpectedPathSize, path_size);
3438 std::vector<FS_POINTF> path_buffer(path_size);
3439 EXPECT_EQ(path_size,
3440 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3441 path_buffer.data(), path_size));
3442 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3443 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3444 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3445 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3446 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3447 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3448
3449 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3450 // testing.
3451 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3452 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3453
3454 // out of bounds path_index.
3455 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3456
3457 // path_buffer is not overwritten if it is too small.
3458 path_buffer.resize(1);
3459 path_buffer[0].x = 42;
3460 path_buffer[0].y = 43;
3461 path_size = FPDFAnnot_GetInkListPath(
3462 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3463 EXPECT_EQ(kExpectedSize, size);
3464 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3465 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3466 }
3467
3468 {
3469 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3470 ASSERT_TRUE(annot);
3471
3472 // This has an odd number of elements in the path array, ignore the last
3473 // element.
3474 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3475 const size_t kExpectedSize = 1;
3476 ASSERT_EQ(kExpectedSize, size);
3477 const unsigned long kPathIndex = 0;
3478 unsigned long path_size =
3479 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3480 const size_t kExpectedPathSize = 3;
3481 ASSERT_EQ(kExpectedPathSize, path_size);
3482 std::vector<FS_POINTF> path_buffer(path_size);
3483 EXPECT_EQ(path_size,
3484 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3485 path_buffer.data(), path_size));
3486 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3487 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3488 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3489 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3490 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3491 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3492 }
3493
3494 {
3495 // Wrong annotation type.
3496 ScopedFPDFAnnotation polygon_annot(
3497 FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON));
3498 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3499 const unsigned long kPathIndex = 0;
3500 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3501 nullptr, 0));
3502 }
3503
3504 UnloadPage(page);
3505}
Miklos Vajna30f45a62020-12-04 19:12:31 +00003506
3507TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) {
3508 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3509 FPDF_PAGE page = LoadPage(0);
3510 ASSERT_TRUE(page);
3511 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3512
3513 {
3514 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3515 ASSERT_TRUE(annot);
3516
3517 // FPDFAnnot_GetVertices() positive testing.
3518 FS_POINTF start;
3519 FS_POINTF end;
3520 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3521 EXPECT_FLOAT_EQ(159.0f, start.x);
3522 EXPECT_FLOAT_EQ(296.0f, start.y);
3523 EXPECT_FLOAT_EQ(472.0f, end.x);
3524 EXPECT_FLOAT_EQ(243.42f, end.y);
3525
3526 // FPDFAnnot_GetVertices() negative testing.
3527 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3528 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3529 }
3530
3531 {
3532 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3533 ASSERT_TRUE(annot);
3534
3535 // Too few elements in the line array.
3536 FS_POINTF start;
3537 FS_POINTF end;
3538 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3539 }
3540
3541 {
3542 // Wrong annotation type.
3543 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3544 FS_POINTF start;
3545 FS_POINTF end;
3546 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3547 }
3548
3549 UnloadPage(page);
3550}
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003551
3552TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) {
3553 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3554 FPDF_PAGE page = LoadPage(0);
3555 ASSERT_TRUE(page);
3556 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3557
3558 {
3559 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3560 ASSERT_TRUE(annot);
3561
3562 // FPDFAnnot_GetBorder() positive testing.
3563 float horizontal_radius;
3564 float vertical_radius;
3565 float border_width;
3566 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3567 &vertical_radius, &border_width));
3568 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3569 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3570 EXPECT_FLOAT_EQ(2.0f, border_width);
3571
3572 // FPDFAnnot_GetBorder() negative testing.
3573 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3574 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3575 }
3576
3577 {
3578 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3579 ASSERT_TRUE(annot);
3580
3581 // Too few elements in the border array.
3582 float horizontal_radius;
3583 float vertical_radius;
3584 float border_width;
3585 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3586 &vertical_radius, &border_width));
Lei Zhang21ea6652021-04-15 23:24:46 +00003587
3588 // FPDFAnnot_SetBorder() positive testing.
3589 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3590 /*vertical_radius=*/3.5f,
3591 /*border_width=*/4.0f));
3592
3593 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3594 &vertical_radius, &border_width));
3595 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3596 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3597 EXPECT_FLOAT_EQ(4.0f, border_width);
3598
3599 // FPDFAnnot_SetBorder() negative testing.
3600 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3601 /*vertical_radius=*/2.5f,
3602 /*border_width=*/3.0f));
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003603 }
3604
3605 UnloadPage(page);
3606}
Lei Zhang24de1492021-04-19 18:06:43 +00003607
3608// Due to https://crbug.com/pdfium/570, the AnnotationBorder test above cannot
3609// actually render the line annotations inside line_annot.pdf. For now, use a
3610// square annotation in annots.pdf for testing.
3611TEST_F(FPDFAnnotEmbedderTest, AnnotationBorderRendering) {
3612 ASSERT_TRUE(OpenDocument("annots.pdf"));
3613 FPDF_PAGE page = LoadPage(1);
3614 ASSERT_TRUE(page);
3615 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
3616
3617#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3618 constexpr char kOriginalChecksum[] = "bc9ba381d6d46ff93ed0b5288b763b60";
3619 constexpr char kModifiedChecksum[] = "0f3e10336f67cb6a8731a06d26d01e50";
3620#elif defined(OS_WIN)
3621 constexpr char kOriginalChecksum[] = "f9fcab6ac610ee2347c4eba6be86a90c";
3622 constexpr char kModifiedChecksum[] = "e0e9ad5b67ef84288d446861265898b3";
3623#elif defined(OS_APPLE)
3624 constexpr char kOriginalChecksum[] = "1839f5df5fb4fae10cf3793568e73ede";
3625 constexpr char kModifiedChecksum[] = "abd4f5d1c3b8d8cfc572b389e589da5a";
3626#else
3627 constexpr char kOriginalChecksum[] = "ccf6667b34ec2452bea0b5f1a0194191";
3628 constexpr char kModifiedChecksum[] = "1bbdb473d0757843e82053b0bd3298bc";
3629#endif
3630
3631 {
3632 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
3633 ASSERT_TRUE(annot);
3634 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
3635
3636 {
3637 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3638 CompareBitmap(bitmap.get(), 612, 792, kOriginalChecksum);
3639 }
3640
3641 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3642 /*vertical_radius=*/3.5f,
3643 /*border_width=*/4.0f));
3644
3645 {
3646 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3647 CompareBitmap(bitmap.get(), 612, 792, kModifiedChecksum);
3648 }
3649 }
3650
3651 // Save the document and close the page.
3652 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
3653 UnloadPage(page);
3654
3655 ASSERT_TRUE(OpenSavedDocument());
3656 page = LoadSavedPage(1);
3657 ASSERT_TRUE(page);
3658 VerifySavedRendering(page, 612, 792, kModifiedChecksum);
3659
3660 CloseSavedPage(page);
3661 CloseSavedDocument();
3662}