blob: 8dd050e3b8977b9f670d6d833535f56e59919b49 [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 Zhang9b444002020-04-17 17:35:23 +000033#include "third_party/base/span.h"
Lei Zhangc9b72992020-04-22 01:03:52 +000034#include "third_party/base/stl_util.h"
Lei Zhang9b444002020-04-17 17:35:23 +000035
Hui Yingst30bfcc52020-07-27 23:54:40 +000036using pdfium::kAnnotationStampWithApChecksum;
37
Lei Zhang9b444002020-04-17 17:35:23 +000038namespace {
39
Tom Sepez204ab052020-06-12 21:33:48 +000040const wchar_t kStreamData[] =
41 L"/GS gs 0.0 0.0 0.0 RG 4 w 211.8 747.6 m 211.8 744.8 "
42 L"212.6 743.0 214.2 740.8 "
43 L"c 215.4 739.0 216.8 737.1 218.9 736.1 c 220.8 735.1 221.4 733.0 "
44 L"223.7 732.4 c 232.6 729.9 242.0 730.8 251.2 730.8 c 257.5 730.8 "
45 L"263.0 732.9 269.0 734.4 c S";
46
Lei Zhang9b444002020-04-17 17:35:23 +000047void VerifyFocusableAnnotSubtypes(
48 FPDF_FORMHANDLE form_handle,
49 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) {
50 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
51 FPDFAnnot_GetFocusableSubtypesCount(form_handle));
52
53 std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes(
54 expected_subtypes.size());
55 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(
56 form_handle, actual_subtypes.data(), actual_subtypes.size()));
57 for (size_t i = 0; i < expected_subtypes.size(); ++i)
58 ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]);
59}
60
61void SetAndVerifyFocusableAnnotSubtypes(
62 FPDF_FORMHANDLE form_handle,
63 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) {
64 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(),
65 subtypes.size()));
66 VerifyFocusableAnnotSubtypes(form_handle, subtypes);
67}
68
69void VerifyAnnotationSubtypesAndFocusability(
70 FPDF_FORMHANDLE form_handle,
71 FPDF_PAGE page,
72 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes,
73 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) {
74 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
75 FPDFPage_GetAnnotCount(page));
76 for (size_t i = 0; i < expected_subtypes.size(); ++i) {
77 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
78 ASSERT_TRUE(annot);
79 EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get()));
80
Lei Zhangf245ae62020-05-15 21:49:46 +000081 bool expected_focusable =
82 pdfium::Contains(expected_focusable_subtypes, expected_subtypes[i]);
Lei Zhang9b444002020-04-17 17:35:23 +000083 EXPECT_EQ(expected_focusable,
84 FORM_SetFocusedAnnot(form_handle, annot.get()));
85
86 // Kill the focus so the next test starts in an unfocused state.
87 FORM_ForceToKillFocus(form_handle);
88 }
89}
90
Lei Zhang306874b2021-04-16 00:33:36 +000091void VerifyUriActionInLink(FPDF_DOCUMENT doc,
92 FPDF_LINK link,
93 const std::string& expected_uri) {
94 ASSERT_TRUE(link);
95
96 FPDF_ACTION action = FPDFLink_GetAction(link);
97 ASSERT_TRUE(action);
98 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
99 FPDFAction_GetType(action));
100
101 unsigned long bufsize = FPDFAction_GetURIPath(doc, action, nullptr, 0);
102 ASSERT_EQ(expected_uri.size() + 1, bufsize);
103
104 std::vector<char> buffer(bufsize);
105 EXPECT_EQ(bufsize,
106 FPDFAction_GetURIPath(doc, action, buffer.data(), bufsize));
107 EXPECT_STREQ(expected_uri.c_str(), buffer.data());
108}
109
Lei Zhang9b444002020-04-17 17:35:23 +0000110} // namespace
Jane Liu4fd9a472017-06-01 18:56:09 -0400111
Lei Zhangab41f252018-12-23 03:10:50 +0000112class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -0400113
Tom Sepez204ab052020-06-12 21:33:48 +0000114TEST_F(FPDFAnnotEmbedderTest, SetAP) {
115 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
116 ASSERT_TRUE(doc);
117 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
118 ASSERT_TRUE(page);
119 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
120 ASSERT_TRUE(ap_stream);
121
122 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
123 ASSERT_TRUE(annot);
124
125 // Negative case: FPDFAnnot_SetAP() should fail if bounding rect is not yet
126 // set on the annotation.
127 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
128 ap_stream.get()));
129
130 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
131 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
132
133 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
134 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/255));
135
136 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
137 ap_stream.get()));
138
139 // Verify that appearance stream is created as form XObject
140 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
141 ASSERT_TRUE(context);
142 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
143 ASSERT_TRUE(annot_dict);
144 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
145 ASSERT_TRUE(ap_dict);
146 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
147 ASSERT_TRUE(stream_dict);
148 // Check for non-existence of resources dictionary in case of opaque color
149 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
150 ASSERT_FALSE(resources_dict);
151 ByteString type = stream_dict->GetStringFor(pdfium::annotation::kType);
152 EXPECT_EQ("XObject", type);
153 ByteString sub_type = stream_dict->GetStringFor(pdfium::annotation::kSubtype);
154 EXPECT_EQ("Form", sub_type);
155
156 // Check that the appearance stream is same as we just set.
157 const uint32_t kStreamDataSize =
158 pdfium::size(kStreamData) * sizeof(FPDF_WCHAR);
159 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
160 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
161 ASSERT_EQ(kStreamDataSize, normal_length_bytes);
162 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
163 EXPECT_EQ(kStreamDataSize,
164 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
165 buf.data(), normal_length_bytes));
166 EXPECT_EQ(kStreamData, GetPlatformWString(buf.data()));
167}
168
169TEST_F(FPDFAnnotEmbedderTest, SetAPWithOpacity) {
170 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
171 ASSERT_TRUE(doc);
172 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
173 ASSERT_TRUE(page);
174 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
175 ASSERT_TRUE(ap_stream);
176
177 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
178 ASSERT_TRUE(annot);
179
180 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
181 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/102));
182
183 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
184 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
185
186 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
187 ap_stream.get()));
188
189 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
190 ASSERT_TRUE(context);
191 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
192 ASSERT_TRUE(annot_dict);
193 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
194 ASSERT_TRUE(ap_dict);
195 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
196 ASSERT_TRUE(stream_dict);
197 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
198 ASSERT_TRUE(stream_dict);
199 CPDF_Dictionary* extGState_dict = resources_dict->GetDictFor("ExtGState");
200 ASSERT_TRUE(extGState_dict);
201 CPDF_Dictionary* gs_dict = extGState_dict->GetDictFor("GS");
202 ASSERT_TRUE(gs_dict);
203 ByteString type = gs_dict->GetStringFor(pdfium::annotation::kType);
204 EXPECT_EQ("ExtGState", type);
205 float opacity = gs_dict->GetNumberFor("CA");
206 // Opacity value of 102 is represented as 0.4f (=104/255) in /CA entry.
207 EXPECT_FLOAT_EQ(0.4f, opacity);
208 ByteString blend_mode = gs_dict->GetStringFor("BM");
209 EXPECT_EQ("Normal", blend_mode);
210 bool alpha_source_flag = gs_dict->GetBooleanFor("AIS", true);
211 EXPECT_FALSE(alpha_source_flag);
212}
213
214TEST_F(FPDFAnnotEmbedderTest, InkListAPIValidations) {
215 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
216 ASSERT_TRUE(doc);
217 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
218 ASSERT_TRUE(page);
219
220 // Create a new ink annotation.
221 ScopedFPDFAnnotation ink_annot(
222 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
223 ASSERT_TRUE(ink_annot);
224 CPDF_AnnotContext* context =
225 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
226 ASSERT_TRUE(context);
227 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
228 ASSERT_TRUE(annot_dict);
229
230 static constexpr FS_POINTF kFirstInkStroke[] = {
231 {80.0f, 90.0f}, {81.0f, 91.0f}, {82.0f, 92.0f},
232 {83.0f, 93.0f}, {84.0f, 94.0f}, {85.0f, 95.0f}};
233 static constexpr size_t kFirstStrokePointCount =
234 pdfium::size(kFirstInkStroke);
235
236 static constexpr FS_POINTF kSecondInkStroke[] = {
237 {70.0f, 90.0f}, {71.0f, 91.0f}, {72.0f, 92.0f}};
238 static constexpr size_t kSecondStrokePointCount =
239 pdfium::size(kSecondInkStroke);
240
241 static constexpr FS_POINTF kThirdInkStroke[] = {{60.0f, 90.0f},
242 {61.0f, 91.0f},
243 {62.0f, 92.0f},
244 {63.0f, 93.0f},
245 {64.0f, 94.0f}};
246 static constexpr size_t kThirdStrokePointCount =
247 pdfium::size(kThirdInkStroke);
248
249 // Negative test: |annot| is passed as nullptr.
250 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(nullptr, kFirstInkStroke,
251 kFirstStrokePointCount));
252
253 // Negative test: |annot| is not ink annotation.
254 // Create a new highlight annotation.
255 ScopedFPDFAnnotation highlight_annot(
256 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
257 ASSERT_TRUE(highlight_annot);
258 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(highlight_annot.get(), kFirstInkStroke,
259 kFirstStrokePointCount));
260
261 // Negative test: passing |point_count| as 0.
262 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke, 0));
263
264 // Negative test: passing |points| array as nullptr.
265 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), nullptr,
266 kFirstStrokePointCount));
267
268 // Negative test: passing |point_count| more than ULONG_MAX/2.
269 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
270 ULONG_MAX / 2 + 1));
271
272 // InkStroke should get added to ink annotation. Also inklist should get
273 // created.
274 EXPECT_EQ(0, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke,
275 kFirstStrokePointCount));
276
277 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
278 ASSERT_TRUE(inklist);
279 EXPECT_EQ(1u, inklist->size());
280 EXPECT_EQ(kFirstStrokePointCount * 2, inklist->GetArrayAt(0)->size());
281
282 // Adding another inkStroke to ink annotation with all valid paremeters.
283 // InkList already exists in ink_annot.
284 EXPECT_EQ(1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
285 kSecondStrokePointCount));
286 EXPECT_EQ(2u, inklist->size());
287 EXPECT_EQ(kSecondStrokePointCount * 2, inklist->GetArrayAt(1)->size());
288
289 // Adding one more InkStroke to the ink annotation. |point_count| passed is
290 // less than the data available in |buffer|.
291 EXPECT_EQ(2, FPDFAnnot_AddInkStroke(ink_annot.get(), kThirdInkStroke,
292 kThirdStrokePointCount - 1));
293 EXPECT_EQ(3u, inklist->size());
294 EXPECT_EQ((kThirdStrokePointCount - 1) * 2, inklist->GetArrayAt(2)->size());
295}
296
297TEST_F(FPDFAnnotEmbedderTest, RemoveInkList) {
298 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
299 ASSERT_TRUE(doc);
300 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
301 ASSERT_TRUE(page);
302
303 // Negative test: |annot| is passed as nullptr.
304 EXPECT_FALSE(FPDFAnnot_RemoveInkList(nullptr));
305
306 // Negative test: |annot| is not ink annotation.
307 // Create a new highlight annotation.
308 ScopedFPDFAnnotation highlight_annot(
309 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
310 ASSERT_TRUE(highlight_annot);
311 EXPECT_FALSE(FPDFAnnot_RemoveInkList(highlight_annot.get()));
312
313 // Create a new ink annotation.
314 ScopedFPDFAnnotation ink_annot(
315 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
316 ASSERT_TRUE(ink_annot);
317 CPDF_AnnotContext* context =
318 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
319 ASSERT_TRUE(context);
320 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
321 ASSERT_TRUE(annot_dict);
322
323 static constexpr FS_POINTF kInkStroke[] = {{80.0f, 90.0f}, {81.0f, 91.0f},
324 {82.0f, 92.0f}, {83.0f, 93.0f},
325 {84.0f, 94.0f}, {85.0f, 95.0f}};
326 static constexpr size_t kPointCount = pdfium::size(kInkStroke);
327
328 // InkStroke should get added to ink annotation. Also inklist should get
329 // created.
330 EXPECT_EQ(0,
331 FPDFAnnot_AddInkStroke(ink_annot.get(), kInkStroke, kPointCount));
332
333 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
334 ASSERT_TRUE(inklist);
335 ASSERT_EQ(1u, inklist->size());
336 EXPECT_EQ(kPointCount * 2, inklist->GetArrayAt(0)->size());
337
338 // Remove inklist.
339 EXPECT_TRUE(FPDFAnnot_RemoveInkList(ink_annot.get()));
340 EXPECT_FALSE(annot_dict->KeyExist("InkList"));
341}
342
Lei Zhangab41f252018-12-23 03:10:50 +0000343TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +0000344 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
345 FPDF_PAGE page = LoadPage(0);
346 ASSERT_TRUE(page);
347
348 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
349
350 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
351 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
352 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
353 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
354 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
355
356 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
357
358 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
359
360 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
361 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
362 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
363
364 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
365
Lei Zhang4f556b82019-04-08 16:32:41 +0000366 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +0000367 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +0000368 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
369
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000370 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +0000371 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
372 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
373 EXPECT_EQ(0u,
374 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
375
376 UnloadPage(page);
377}
378
Lei Zhang3d9a0972019-03-04 19:34:09 +0000379TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
380 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
381 FPDF_PAGE page = LoadPage(0);
382 ASSERT_TRUE(page);
383
384 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +0000385 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +0000386
387 UnloadPage(page);
388}
389
Lei Zhangab41f252018-12-23 03:10:50 +0000390TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -0400391 // Open a file with one annotation and load its first page.
392 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000393 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -0400394 ASSERT_TRUE(page);
395
396 // This annotation has a malformed appearance stream, which does not have its
397 // normal appearance defined, only its rollover appearance. In this case, its
398 // normal appearance should be generated, allowing the highlight annotation to
399 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000400 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000401 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -0400402
403 UnloadPage(page);
404}
405
Hui Yingstd5b6f632020-07-22 01:31:59 +0000406TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang03e5e682019-09-16 19:45:55 +0000407#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstd5b6f632020-07-22 01:31:59 +0000408 static const char kChecksum[] = "ec1f4ccbd0aecfdea6d53893387a0101";
Lei Zhang03e5e682019-09-16 19:45:55 +0000409#else
Hui Yingstd5b6f632020-07-22 01:31:59 +0000410 static const char kChecksum[] = "76512832d88017668d9acc7aacd13dae";
Lei Zhang03e5e682019-09-16 19:45:55 +0000411#endif
Henrique Nakashima5098b252018-03-26 21:46:00 +0000412 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000413 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
414 FPDF_PAGE page = LoadPage(0);
415 ASSERT_TRUE(page);
416
Tom Sepeze08d2b12018-04-25 18:49:32 +0000417 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstd5b6f632020-07-22 01:31:59 +0000418 CompareBitmap(bitmap.get(), 595, 842, kChecksum);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000419
420 UnloadPage(page);
421}
422
Lei Zhangab41f252018-12-23 03:10:50 +0000423TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400424 // Open a file with one annotation and load its first page.
425 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000426 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400427 ASSERT_TRUE(page);
428
429 // Check that there is a total of 1 annotation on its first page.
430 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
431
432 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000433 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000434 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000435 ASSERT_TRUE(annot);
436 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400437
Lei Zhanga21d5932018-02-05 18:28:38 +0000438 // Check that the annotation color is yellow.
439 unsigned int R;
440 unsigned int G;
441 unsigned int B;
442 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000443 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000444 &G, &B, &A));
445 EXPECT_EQ(255u, R);
446 EXPECT_EQ(255u, G);
447 EXPECT_EQ(0u, B);
448 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400449
Lei Zhanga21d5932018-02-05 18:28:38 +0000450 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000451 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000452 EXPECT_EQ(FPDF_OBJECT_STRING,
453 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000454 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000455 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000456 ASSERT_EQ(28u, length_bytes);
457 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000458 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000459 length_bytes));
460 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400461
Lei Zhanga21d5932018-02-05 18:28:38 +0000462 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000463 EXPECT_EQ(
464 FPDF_OBJECT_STRING,
465 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000466 length_bytes = FPDFAnnot_GetStringValue(
467 annot.get(), pdfium::annotation::kContents, nullptr, 0);
468 ASSERT_EQ(2690u, length_bytes);
469 buf = GetFPDFWideStringBuffer(length_bytes);
470 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
471 pdfium::annotation::kContents,
472 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000473 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000474 L"This is a note for that highlight annotation. Very long highlight "
475 "annotation. Long long long Long long longLong long longLong long "
476 "longLong long longLong long longLong long longLong long longLong long "
477 "longLong long longLong long longLong long longLong long longLong long "
478 "longLong long longLong long longLong long longLong long longLong long "
479 "longLong long longLong long longLong long longLong long longLong long "
480 "longLong long longLong long longLong long longLong long longLong long "
481 "longLong long longLong long longLong long longLong long longLong long "
482 "longLong long longLong long longLong long longLong long longLong long "
483 "longLong long longLong long longLong long longLong long longLong long "
484 "longLong long longLong long longLong long longLong long longLong long "
485 "longLong long longLong long longLong long longLong long longLong long "
486 "longLong long longLong long longLong long longLong long longLong long "
487 "longLong long longLong long longLong long longLong long longLong long "
488 "longLong long longLong long longLong long longLong long longLong long "
489 "longLong long longLong long longLong long longLong long longLong long "
490 "longLong long longLong long longLong long longLong long longLong long "
491 "longLong long longLong long longLong long longLong long longLong long "
492 "longLong long longLong long longLong long longLong long longLong long "
493 "longLong long long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000494 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400495
Lei Zhanga21d5932018-02-05 18:28:38 +0000496 // Check that the quadpoints are correct.
497 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000498 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000499 EXPECT_EQ(115.802643f, quadpoints.x1);
500 EXPECT_EQ(718.913940f, quadpoints.y1);
501 EXPECT_EQ(157.211182f, quadpoints.x4);
502 EXPECT_EQ(706.264465f, quadpoints.y4);
503 }
Tom Sepez507d0192018-11-07 16:37:51 +0000504 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400505}
506
Hui Yingst9b6b1542020-07-27 16:11:12 +0000507TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400508 // Open a file with three annotations and load its first page.
509 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000510 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400511 ASSERT_TRUE(page);
512
513 // Check that there is a total of 3 annotation on its first page.
514 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
515
Lei Zhanga21d5932018-02-05 18:28:38 +0000516 {
517 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000518 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000519 ASSERT_TRUE(annot);
520 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400521
Lei Zhanga21d5932018-02-05 18:28:38 +0000522 // Check that the annotation color is blue with opacity.
523 unsigned int R;
524 unsigned int G;
525 unsigned int B;
526 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000527 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000528 &G, &B, &A));
529 EXPECT_EQ(0u, R);
530 EXPECT_EQ(0u, G);
531 EXPECT_EQ(255u, B);
532 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400533
Lei Zhanga21d5932018-02-05 18:28:38 +0000534 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000535 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
536 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400537
Lei Zhang4f556b82019-04-08 16:32:41 +0000538 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000539 // Note that upon rendering, the rectangle coordinates will be adjusted.
540 FS_RECTF rect;
541 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
542 EXPECT_EQ(351.820404f, rect.left);
543 EXPECT_EQ(583.830688f, rect.bottom);
544 EXPECT_EQ(475.336090f, rect.right);
545 EXPECT_EQ(681.535034f, rect.top);
546 }
Tom Sepezef43c262018-11-07 16:41:32 +0000547 {
Hui Yingst57daee82020-10-09 05:47:20 +0000548#if defined(_SKIA_SUPPORT_) && defined(OS_APPLE)
549 static constexpr char kExpectedHash[] = "fad91b9c968fe8019a774f5e2419b8fc";
550#elif defined(_SKIA_SUPPORT_PATHS_) && defined(OS_APPLE)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000551 static constexpr char kExpectedHash[] = "acddfe688a117ead56af7b249a2cf8a1";
Hui Yingst57daee82020-10-09 05:47:20 +0000552#elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000553 static constexpr char kExpectedHash[] = "1fb0dd8dd5f0b9bb8d076e48eb59296d";
Hui Yingst57daee82020-10-09 05:47:20 +0000554#elif defined(OS_WIN)
Lei Zhang430b5322020-07-06 22:23:36 +0000555 static constexpr char kExpectedHash[] = "49d0a81c636531a337429325273d0508";
556#else
557 static constexpr char kExpectedHash[] = "354002e1c4386d38fdde29ef8d61074a";
Hui Yingst57daee82020-10-09 05:47:20 +0000558#endif
Tom Sepezef43c262018-11-07 16:41:32 +0000559 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang430b5322020-07-06 22:23:36 +0000560 CompareBitmap(bitmap.get(), 612, 792, kExpectedHash);
Tom Sepezef43c262018-11-07 16:41:32 +0000561 }
Tom Sepez507d0192018-11-07 16:37:51 +0000562 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400563}
Jane Liu20eafda2017-06-07 10:33:24 -0400564
Lei Zhangab41f252018-12-23 03:10:50 +0000565TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400566 // Open a file with one annotation and load its first page.
567 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000568 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400569 ASSERT_TRUE(page);
570
571 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400572 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400573
574 UnloadPage(page);
575}
576
Lei Zhangab41f252018-12-23 03:10:50 +0000577TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400578 // Open a file with no annotation and load its first page.
579 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000580 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400581 ASSERT_TRUE(page);
582 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
583
Lei Zhanga21d5932018-02-05 18:28:38 +0000584 {
585 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000586 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000587 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400588
Lei Zhanga21d5932018-02-05 18:28:38 +0000589 // Check that there is now 1 annotations on this page.
590 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400591
Lei Zhanga21d5932018-02-05 18:28:38 +0000592 // Check that the subtype of the annotation is correct.
593 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
594 }
Jane Liue10509a2017-06-20 16:47:41 -0400595
Lei Zhanga21d5932018-02-05 18:28:38 +0000596 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000597 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000598 ASSERT_TRUE(annot);
599 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400600
Lei Zhanga21d5932018-02-05 18:28:38 +0000601 // Set the color of the annotation.
602 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
603 102, 153, 204));
604 // Check that the color has been set correctly.
605 unsigned int R;
606 unsigned int G;
607 unsigned int B;
608 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000609 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000610 &G, &B, &A));
611 EXPECT_EQ(51u, R);
612 EXPECT_EQ(102u, G);
613 EXPECT_EQ(153u, B);
614 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400615
Lei Zhanga21d5932018-02-05 18:28:38 +0000616 // Change the color of the annotation.
617 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
618 153, 102, 51));
619 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000620 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000621 &G, &B, &A));
622 EXPECT_EQ(204u, R);
623 EXPECT_EQ(153u, G);
624 EXPECT_EQ(102u, B);
625 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400626
Lei Zhanga21d5932018-02-05 18:28:38 +0000627 // Set the annotation rectangle.
628 FS_RECTF rect;
629 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
630 EXPECT_EQ(0.f, rect.left);
631 EXPECT_EQ(0.f, rect.right);
632 rect.left = 35;
633 rect.bottom = 150;
634 rect.right = 53;
635 rect.top = 165;
636 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
637 // Check that the annotation rectangle has been set correctly.
638 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
639 EXPECT_EQ(35.f, rect.left);
640 EXPECT_EQ(150.f, rect.bottom);
641 EXPECT_EQ(53.f, rect.right);
642 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400643
Lei Zhanga21d5932018-02-05 18:28:38 +0000644 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000645 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000646 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000647 ASSERT_TRUE(FPDFAnnot_SetStringValue(
648 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000649 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000650 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000651 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000652 ASSERT_EQ(74u, length_bytes);
653 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
654 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
655 pdfium::annotation::kContents,
656 buf.data(), length_bytes));
657 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000658 }
Jane Liu20eafda2017-06-07 10:33:24 -0400659 UnloadPage(page);
660}
661
Lei Zhang81395aa2021-04-16 00:40:46 +0000662TEST_F(FPDFAnnotEmbedderTest, AddAndSaveLinkAnnotation) {
663 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
664 FPDF_PAGE page = LoadPage(0);
665 ASSERT_TRUE(page);
666 {
667 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
668 CompareBitmap(bitmap.get(), 200, 200, pdfium::kHelloWorldChecksum);
669 }
670 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
671
672 constexpr char kUri[] = "https://pdfium.org/";
673
674 {
675 // Add a link annotation to the page and set its URI.
676 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_LINK));
677 ASSERT_TRUE(annot);
678 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
679 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
680 EXPECT_TRUE(FPDFAnnot_SetURI(annot.get(), kUri));
681 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
682
683 // Negative tests:
684 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, nullptr));
685 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
686 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), nullptr));
687 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
688 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, kUri));
689 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
690
691 // Position the link on top of "Hello, world!" without a border.
692 const FS_RECTF kRect = {19.0f, 48.0f, 85.0f, 60.0f};
693 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &kRect));
694 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/0.0f,
695 /*vertical_radius=*/0.0f,
696 /*border_width=*/0.0f));
697
698 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
699 kUri);
700 }
701
702 {
703 // Add an ink annotation to the page. Trying to add a link to it fails.
704 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
705 ASSERT_TRUE(annot);
706 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
707 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
708 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), kUri));
709 }
710
711 // Remove the ink annotation added above for negative testing.
712 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
713 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
714
715 // Save the document, closing the page.
716 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
717 UnloadPage(page);
718
719 // Reopen the document and make sure it still renders the same. Since the link
720 // does not have a border, it does not affect the rendering.
721 ASSERT_TRUE(OpenSavedDocument());
722 page = LoadSavedPage(0);
723 ASSERT_TRUE(page);
724 VerifySavedRendering(page, 200, 200, pdfium::kHelloWorldChecksum);
725 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
726
727 {
728 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
729 ASSERT_TRUE(annot);
730 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
731 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
732 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
733 kUri);
734 }
735
736 CloseSavedPage(page);
737 CloseSavedDocument();
738}
739
Hui Yingstb3490322020-07-22 00:53:29 +0000740TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400741 // Open a file with one annotation and load its first page.
742 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000743 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400744 ASSERT_TRUE(page);
745
746 // Check that there is a total of one annotation on its first page, and verify
747 // its quadpoints.
748 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400749 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000750 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000751 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000752 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000753 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000754 EXPECT_EQ(115.802643f, quadpoints.x1);
755 EXPECT_EQ(718.913940f, quadpoints.y1);
756 EXPECT_EQ(157.211182f, quadpoints.x4);
757 EXPECT_EQ(706.264465f, quadpoints.y4);
758 }
Jane Liu20eafda2017-06-07 10:33:24 -0400759
760 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000761 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000762 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000763 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
764 ASSERT_TRUE(annot);
765 quadpoints.x1 = 140.802643f;
766 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000767 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000768 }
Jane Liu20eafda2017-06-07 10:33:24 -0400769
Lei Zhangec618142021-04-13 17:36:46 +0000770 // Save the document and close the page.
Jane Liu20eafda2017-06-07 10:33:24 -0400771 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000772 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400773
774 // Open the saved document.
Hui Yingst57daee82020-10-09 05:47:20 +0000775#if defined(_SKIA_SUPPORT_) && defined(OS_APPLE)
776 static const char kChecksum[] = "899387ae792390cd0d83cf7e2bbebfb5";
777#elif defined(_SKIA_SUPPORT_PATHS_) && defined(OS_APPLE)
778 static const char kChecksum[] = "e40e235ee35f47ff28dda009aaaf36df";
779#elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstb3490322020-07-22 00:53:29 +0000780 static const char kChecksum[] = "798fa41303381c9ba6d99092f5cd4d2b";
781#else
782 static const char kChecksum[] = "dba153419f67b7c0c0e3d22d3e8910d5";
783#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400784
Lei Zhang0b494052019-01-31 21:41:15 +0000785 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000786 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +0000787 ASSERT_TRUE(page);
Hui Yingstb3490322020-07-22 00:53:29 +0000788 VerifySavedRendering(page, 612, 792, kChecksum);
Jane Liu20eafda2017-06-07 10:33:24 -0400789
790 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000791 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400792
Lei Zhanga21d5932018-02-05 18:28:38 +0000793 {
794 // Check that the second annotation is an underline annotation and verify
795 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000796 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000797 ASSERT_TRUE(new_annot);
798 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
799 FS_QUADPOINTSF new_quadpoints;
800 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000801 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000802 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
803 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
804 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
805 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
806 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400807
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000808 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400809 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400810}
Jane Liu06462752017-06-27 16:41:14 -0400811
Lei Zhangab41f252018-12-23 03:10:50 +0000812TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000813 // Open a file with four annotations and load its first page.
814 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
815 FPDF_PAGE page = LoadPage(0);
816 ASSERT_TRUE(page);
817 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
818
819 // Retrieve the highlight annotation.
820 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
821 ASSERT_TRUE(annot);
822 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
823
824 FS_QUADPOINTSF quadpoints;
825 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
826
827 {
828 // Verify the current one set of quadpoints.
829 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
830
831 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
832 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
833 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
834 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
835 }
836
837 {
838 // Update the quadpoints.
839 FS_QUADPOINTSF new_quadpoints = quadpoints;
840 new_quadpoints.y1 -= 20.f;
841 new_quadpoints.y2 -= 20.f;
842 new_quadpoints.y3 -= 20.f;
843 new_quadpoints.y4 -= 20.f;
844 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
845
846 // Verify added quadpoint set
847 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
848 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
849 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
850 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
851 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
852 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
853 }
854
855 {
856 // Append a new set of quadpoints.
857 FS_QUADPOINTSF new_quadpoints = quadpoints;
858 new_quadpoints.y1 += 20.f;
859 new_quadpoints.y2 += 20.f;
860 new_quadpoints.y3 += 20.f;
861 new_quadpoints.y4 += 20.f;
862 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
863
864 // Verify added quadpoint set
865 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
866 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
867 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
868 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
869 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
870 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
871 }
872
873 {
874 // Setting and getting quadpoints at out-of-bound index should fail
875 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
876 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
877 }
878
879 FPDFPage_CloseAnnot(annot);
880
881 // Retrieve the square annotation
882 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
883
884 {
885 // Check that attempting to set its quadpoints would fail
886 ASSERT_TRUE(squareAnnot);
887 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
888 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
889 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
890 }
891
892 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000893 UnloadPage(page);
894}
895
Hui Yingstb64cd122020-07-27 16:01:02 +0000896// TODO(crbug.com/pdfium/1569): Fix this issue and enable the test for Skia.
897#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +0000898#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
899#else
900#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
901#endif
902TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Hui Yingstb64cd122020-07-27 16:01:02 +0000903#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
904 static const char kMd5Original[] = "00e70eb543c2a6e8f8aafb4ee951d9bf";
Lei Zhang4f556b82019-04-08 16:32:41 +0000905 static const char kMd5ModifiedHighlight[] =
Hui Yingstb64cd122020-07-27 16:01:02 +0000906 "7638c4a8fe4aabbf8704e198f49b3198";
907 static const char kMd5ModifiedSquare[] = "54f507af6af63de877b9cafdab1bbdaa";
908#else
909#if defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000910 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
911 static const char kMd5ModifiedHighlight[] =
912 "66f3caef3a7d488a4fa1ad37fc06310e";
913 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Lei Zhang0c03d632020-07-30 17:05:36 +0000914#elif defined(OS_APPLE)
Hui Yingstb64cd122020-07-27 16:01:02 +0000915 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
916 static const char kMd5ModifiedHighlight[] =
917 "e64bf648f6e9354d1f3eedb47a2c9498";
918 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Jane Liub370e5a2017-08-16 13:24:58 -0400919#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000920 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
921 static const char kMd5ModifiedHighlight[] =
922 "66f3caef3a7d488a4fa1ad37fc06310e";
923 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400924#endif
Hui Yingstb64cd122020-07-27 16:01:02 +0000925#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liub370e5a2017-08-16 13:24:58 -0400926
Jane Liu06462752017-06-27 16:41:14 -0400927 // Open a file with four annotations and load its first page.
928 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000929 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400930 ASSERT_TRUE(page);
931 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
932
Jane Liub370e5a2017-08-16 13:24:58 -0400933 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000934 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000935 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000936 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000937 }
Jane Liub370e5a2017-08-16 13:24:58 -0400938
Jane Liu0c6b07d2017-08-15 10:50:22 -0400939 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400940 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000941
942 // Retrieve the highlight annotation which has its AP stream already defined.
943 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000944 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000945 ASSERT_TRUE(annot);
946 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
947
948 // Check that color cannot be set when an AP stream is defined already.
949 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
950 102, 153, 204));
951
952 // Verify its attachment points.
953 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000954 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000955 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
956 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
957 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
958 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
959
960 // Check that updating the attachment points would succeed.
961 quadpoints.x1 -= 50.f;
962 quadpoints.x2 -= 50.f;
963 quadpoints.x3 -= 50.f;
964 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000965 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000966 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000967 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000968 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
969 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
970 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
971 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
972
973 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000974 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000975 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000976 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000977 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000978
979 // Verify its annotation rectangle.
980 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
981 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
982 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
983 EXPECT_NEAR(136.325f, rect.right, 0.001f);
984 EXPECT_NEAR(721.292f, rect.top, 0.001f);
985
986 // Check that updating the rectangle would succeed.
987 rect.left -= 60.f;
988 rect.right -= 60.f;
989 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
990 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
991 EXPECT_EQ(rect.right, new_rect.right);
992 }
Jane Liu06462752017-06-27 16:41:14 -0400993
Jane Liub370e5a2017-08-16 13:24:58 -0400994 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000995 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000996 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000997 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000998 }
Jane Liub370e5a2017-08-16 13:24:58 -0400999
Lei Zhanga21d5932018-02-05 18:28:38 +00001000 {
1001 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001002 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001003 ASSERT_TRUE(annot);
1004 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -04001005
Lei Zhanga21d5932018-02-05 18:28:38 +00001006 // Check that updating the rectangle would succeed.
1007 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1008 rect.left += 70.f;
1009 rect.right += 70.f;
1010 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1011 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1012 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -04001013
Lei Zhanga21d5932018-02-05 18:28:38 +00001014 // Check that updating the rectangle changes the square annotation's
1015 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001016 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001017 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +00001018 }
Jane Liub370e5a2017-08-16 13:24:58 -04001019
Jane Liu06462752017-06-27 16:41:14 -04001020 UnloadPage(page);
1021}
Jane Liu8ce58f52017-06-29 13:40:22 -04001022
Lei Zhangab41f252018-12-23 03:10:50 +00001023TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +00001024 // Open a file with multiline markup annotations.
1025 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
1026 FPDF_PAGE page = LoadPage(0);
1027 ASSERT_TRUE(page);
1028 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001029 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +00001030 ASSERT_TRUE(annot);
1031
1032 // This is a three line annotation.
1033 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
1034 }
1035 UnloadPage(page);
1036
1037 // null annotation should return 0
1038 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
1039}
1040
Lei Zhangab41f252018-12-23 03:10:50 +00001041TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -04001042 // Open a file with 3 annotations on its first page.
1043 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +00001044 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -04001045 ASSERT_TRUE(page);
1046 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1047
Jane Liu0c6b07d2017-08-15 10:50:22 -04001048 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -04001049
Lei Zhanga21d5932018-02-05 18:28:38 +00001050 // Check that the annotations have the expected rectangle coordinates.
1051 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001052 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001053 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1054 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1055 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001056
Lei Zhanga21d5932018-02-05 18:28:38 +00001057 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001058 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001059 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1060 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
1061 }
1062
1063 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001064 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001065 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1066 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1067 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001068
1069 // Check that nothing happens when attempting to remove an annotation with an
1070 // out-of-bound index.
1071 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
1072 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
1073 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1074
1075 // Remove the second annotation.
1076 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
1077 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1078 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
1079
Lei Zhangec618142021-04-13 17:36:46 +00001080 // Save the document and close the page.
Jane Liu8ce58f52017-06-29 13:40:22 -04001081 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +00001082 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -04001083
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001084 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -04001085 // Open the saved document.
1086 std::string new_file = GetString();
1087 FPDF_FILEACCESS file_access;
1088 memset(&file_access, 0, sizeof(file_access));
1089 file_access.m_FileLen = new_file.size();
1090 file_access.m_GetBlock = GetBlockFromString;
1091 file_access.m_Param = &new_file;
1092 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
1093 ASSERT_TRUE(new_doc);
1094 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
1095 ASSERT_TRUE(new_page);
1096
1097 // Check that the saved document has 2 annotations on the first page.
1098 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
1099
Lei Zhanga21d5932018-02-05 18:28:38 +00001100 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1101 // by verifying their rectangle coordinates.
1102 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001103 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001104 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1105 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1106 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001107
Lei Zhanga21d5932018-02-05 18:28:38 +00001108 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001109 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001110 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1111 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1112 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001113 FPDF_ClosePage(new_page);
1114 FPDF_CloseDocument(new_doc);
1115}
Jane Liu8ce58f52017-06-29 13:40:22 -04001116
Hui Yingst4a21df02020-05-13 03:15:44 +00001117TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001118#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001119#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst4a21df02020-05-13 03:15:44 +00001120 static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa";
1121 static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622";
1122 static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001123#else
Hui Yingst4a21df02020-05-13 03:15:44 +00001124 static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4";
1125 static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f";
1126 static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001127#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst4a21df02020-05-13 03:15:44 +00001128#else
1129#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001130 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
1131 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
1132 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Lei Zhang0c03d632020-07-30 17:05:36 +00001133#elif defined(OS_APPLE)
Hui Yingst4a21df02020-05-13 03:15:44 +00001134 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
1135 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
1136 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Jane Liubaa7ff42017-06-29 19:18:23 -04001137#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001138 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
1139 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
1140 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -04001141#endif
Hui Yingst4a21df02020-05-13 03:15:44 +00001142#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liubaa7ff42017-06-29 19:18:23 -04001143
1144 // Open a file with two annotations and load its first page.
1145 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001146 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001147 ASSERT_TRUE(page);
1148 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1149
1150 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001151 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001152 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001153 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001154 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001155
Lei Zhanga21d5932018-02-05 18:28:38 +00001156 {
1157 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001158 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001159 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001160
Lei Zhanga21d5932018-02-05 18:28:38 +00001161 // Check that this annotation has one path object and retrieve it.
1162 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001163 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001164 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1165 EXPECT_FALSE(path);
1166 path = FPDFAnnot_GetObject(annot.get(), 0);
1167 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1168 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001169
Lei Zhanga21d5932018-02-05 18:28:38 +00001170 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001171 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001172 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001173
Lei Zhanga21d5932018-02-05 18:28:38 +00001174 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001175 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001176 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001177 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001178 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001179
Lei Zhanga21d5932018-02-05 18:28:38 +00001180 // Add a second path object to the same annotation.
1181 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1182 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001183 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1184 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001185 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1186 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1187 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001188
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001189 // The object is in the annontation, not in the page, so the page object
1190 // array should not change.
1191 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1192
Lei Zhanga21d5932018-02-05 18:28:38 +00001193 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001194 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001195 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001196 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001197 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001198
Lei Zhanga21d5932018-02-05 18:28:38 +00001199 // Delete the newly added path object.
1200 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1201 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001202 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001203 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001204
1205 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001206 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001207 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001208 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001209 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001210
Jane Liubaa7ff42017-06-29 19:18:23 -04001211 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001212
Lei Zhanga21d5932018-02-05 18:28:38 +00001213 {
1214 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001215 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001216 ASSERT_TRUE(annot);
1217 rect.left = 200.f;
1218 rect.bottom = 400.f;
1219 rect.right = 500.f;
1220 rect.top = 600.f;
1221 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001222
Lei Zhanga21d5932018-02-05 18:28:38 +00001223 // Add a new path to the annotation.
1224 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1225 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1226 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1227 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1228 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001229 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1230 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001231 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1232 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1233 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1234
1235 // Check that the annotation's bounding box came from its rectangle.
1236 FS_RECTF new_rect;
1237 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1238 EXPECT_EQ(rect.left, new_rect.left);
1239 EXPECT_EQ(rect.bottom, new_rect.bottom);
1240 EXPECT_EQ(rect.right, new_rect.right);
1241 EXPECT_EQ(rect.top, new_rect.top);
1242 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001243
Lei Zhangec618142021-04-13 17:36:46 +00001244 // Save the document and close the page.
Jane Liubaa7ff42017-06-29 19:18:23 -04001245 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001246 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001247
1248 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001249 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001250 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001251 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001252 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001253
Jane Liu36567742017-07-06 11:13:35 -04001254 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001255 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001256
Lei Zhanga21d5932018-02-05 18:28:38 +00001257 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001258 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001259 ASSERT_TRUE(annot);
1260 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001261
Lei Zhanga21d5932018-02-05 18:28:38 +00001262 // Check that the new annotation's rectangle is as defined.
1263 FS_RECTF new_rect;
1264 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1265 EXPECT_EQ(rect.left, new_rect.left);
1266 EXPECT_EQ(rect.bottom, new_rect.bottom);
1267 EXPECT_EQ(rect.right, new_rect.right);
1268 EXPECT_EQ(rect.top, new_rect.top);
1269 }
1270
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001271 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001272 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001273}
Jane Liub137e752017-07-05 15:04:33 -04001274
Lei Zhangab41f252018-12-23 03:10:50 +00001275TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001276 // Open a file with an annotation and load its first page.
1277 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001278 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001279 ASSERT_TRUE(page);
1280
1281 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001282 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001283 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001284 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1285 }
Jane Liub137e752017-07-05 15:04:33 -04001286
Lei Zhanga21d5932018-02-05 18:28:38 +00001287 {
1288 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001289 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001290 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001291
Lei Zhanga21d5932018-02-05 18:28:38 +00001292 // Check that the original flag values are as expected.
1293 int flags = FPDFAnnot_GetFlags(annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00001294 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_INVISIBLE);
Lei Zhanga21d5932018-02-05 18:28:38 +00001295 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1296 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001297 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOZOOM);
1298 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOROTATE);
1299 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOVIEW);
1300 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_READONLY);
1301 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_LOCKED);
1302 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_TOGGLENOVIEW);
Jane Liub137e752017-07-05 15:04:33 -04001303
Lei Zhanga21d5932018-02-05 18:28:38 +00001304 // Set the HIDDEN flag.
1305 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1306 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1307 flags = FPDFAnnot_GetFlags(annot.get());
1308 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1309 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001310
Lei Zhanga21d5932018-02-05 18:28:38 +00001311 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001312 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001313 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001314 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001315 }
Jane Liub137e752017-07-05 15:04:33 -04001316
Lei Zhanga21d5932018-02-05 18:28:38 +00001317 // Unset the HIDDEN flag.
1318 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1319 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1320 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1321 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1322 flags = FPDFAnnot_GetFlags(annot.get());
1323 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1324 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001325
Lei Zhanga21d5932018-02-05 18:28:38 +00001326 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001327 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001328 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001329 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1330 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001331 }
Jane Liub137e752017-07-05 15:04:33 -04001332
Jane Liub137e752017-07-05 15:04:33 -04001333 UnloadPage(page);
1334}
Jane Liu36567742017-07-06 11:13:35 -04001335
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001336// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1337#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001338#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1339#else
1340#define MAYBE_AddAndModifyImage AddAndModifyImage
1341#endif
1342TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001343#if defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001344#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001345 static const char kMd5NewImage[] = "26a8eb30937226a677839379e0d7ae1a";
1346 static const char kMd5ModifiedImage[] = "2985114b32ba1a96be78ee643fe31aa5";
1347#else
1348 static const char kMd5NewImage[] = "14012ab500b4671fa73dd760129a8a93";
1349 static const char kMd5ModifiedImage[] = "5f97f98f58ed04dc393f31460485f1a2";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001350#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001351#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001352#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001353 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
1354 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +00001355#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001356 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
1357 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -04001358#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001359 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
1360 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -04001361#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001362#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001363
1364 // Open a file with two annotations and load its first page.
1365 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001366 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001367 ASSERT_TRUE(page);
1368 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1369
1370 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001371 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001372 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001373 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001374 }
Jane Liu36567742017-07-06 11:13:35 -04001375
Jane Liu36567742017-07-06 11:13:35 -04001376 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001377 FPDF_BITMAP image_bitmap;
1378
1379 {
1380 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001381 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001382 ASSERT_TRUE(annot);
1383 FS_RECTF rect;
1384 rect.left = 200.f;
1385 rect.bottom = 600.f;
1386 rect.right = 400.f;
1387 rect.top = 800.f;
1388 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1389
1390 // Add a solid-color translucent image object to the new annotation.
1391 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1392 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1393 0xeeeecccc);
1394 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1395 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1396 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1397 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1398 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1399 kBitmapSize, 0, 0));
1400 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1401 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1402 }
Jane Liu36567742017-07-06 11:13:35 -04001403
1404 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001405 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001406 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001407 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001408 }
Jane Liu36567742017-07-06 11:13:35 -04001409
Lei Zhanga21d5932018-02-05 18:28:38 +00001410 {
1411 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001412 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001413 ASSERT_TRUE(annot);
1414 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1415 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1416 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001417
Lei Zhanga21d5932018-02-05 18:28:38 +00001418 // Modify the image in the new annotation.
1419 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1420 0xff000000);
1421 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1422 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1423 }
Jane Liu36567742017-07-06 11:13:35 -04001424
Lei Zhangec618142021-04-13 17:36:46 +00001425 // Save the document and close the page.
Jane Liu36567742017-07-06 11:13:35 -04001426 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001427 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001428 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001429
1430 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001431 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001432}
1433
Hui Yingst6cd754f2020-05-14 04:05:25 +00001434TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001435#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001436#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001437 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1438 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001439#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001440 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1441 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001442#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001443#else
1444#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001445 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1446 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Lei Zhang0c03d632020-07-30 17:05:36 +00001447#elif defined(OS_APPLE)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001448 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1449 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001450#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001451 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1452 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001453#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001454#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001455
1456 // Open a file with two annotations and load its first page.
1457 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001458 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001459 ASSERT_TRUE(page);
1460 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1461
1462 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001463 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001464 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001465 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001466 }
Jane Liu36567742017-07-06 11:13:35 -04001467
Lei Zhanga21d5932018-02-05 18:28:38 +00001468 {
1469 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001470 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001471 ASSERT_TRUE(annot);
1472 FS_RECTF rect;
1473 rect.left = 200.f;
1474 rect.bottom = 550.f;
1475 rect.right = 450.f;
1476 rect.top = 650.f;
1477 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001478
Lei Zhanga21d5932018-02-05 18:28:38 +00001479 // Add a translucent text object to the new annotation.
1480 FPDF_PAGEOBJECT text_object =
1481 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1482 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001483 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001484 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1485 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001486 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001487 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1488 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1489 }
Jane Liu36567742017-07-06 11:13:35 -04001490
1491 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001492 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001493 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001494 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001495 }
Jane Liu36567742017-07-06 11:13:35 -04001496
Lei Zhanga21d5932018-02-05 18:28:38 +00001497 {
1498 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001499 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001500 ASSERT_TRUE(annot);
1501 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1502 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1503 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001504
Lei Zhanga21d5932018-02-05 18:28:38 +00001505 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001506 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001507 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1508 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1509 }
Jane Liu36567742017-07-06 11:13:35 -04001510
1511 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001512 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001513 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001514 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001515 }
Jane Liu36567742017-07-06 11:13:35 -04001516
1517 // Remove the new annotation, and check that the page renders as before.
1518 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001519 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001520 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001521 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001522 }
Jane Liu36567742017-07-06 11:13:35 -04001523
1524 UnloadPage(page);
1525}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001526
Hui Yingst9b6b1542020-07-27 16:11:12 +00001527TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001528 // Open a file with four annotations and load its first page.
1529 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001530 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001531 ASSERT_TRUE(page);
1532
Lei Zhang4f556b82019-04-08 16:32:41 +00001533 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001534
Lei Zhanga21d5932018-02-05 18:28:38 +00001535 {
1536 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001537 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001538 ASSERT_TRUE(annot);
1539
1540 // Check that a non-existent key does not exist.
1541 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1542
1543 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001544 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001545 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001546 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1547 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1548 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001549
1550 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001551 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001552 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001553 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001554 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001555 ASSERT_EQ(66u, length_bytes);
1556 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1557 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1558 length_bytes));
1559 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1560 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001561
1562 // Check that the string value of the modified date is correct.
1563 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001564 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1565 nullptr, 0);
1566 ASSERT_EQ(44u, length_bytes);
1567 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001568 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001569 buf.data(), length_bytes));
1570 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001571
1572 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001573 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001574 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1575 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001576 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001577
Lei Zhangec618142021-04-13 17:36:46 +00001578 // Save the document and close the page.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001579 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001580 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001581
Hui Yingst9b6b1542020-07-27 16:11:12 +00001582#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001583#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001584 static const char kMd5[] = "7a2b712ca88d7b71f125ea3f9c88e57a";
1585#else
1586 static const char kMd5[] = "626d25c5aa5baf67d22d9a0e1c23f6aa";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001587#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001588#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001589#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001590 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001591#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001592 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001593#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001594 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001595#endif
Hui Yingst9b6b1542020-07-27 16:11:12 +00001596#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Dan Sinclair971a6742018-03-28 19:23:25 +00001597
1598 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001599 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001600 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001601 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001602 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001603 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001604 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001605
Lei Zhanga21d5932018-02-05 18:28:38 +00001606 // Check that the string value of the modified date is the newly-set value.
1607 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001608 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001609 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001610 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001611 ASSERT_EQ(44u, length_bytes);
1612 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001613 EXPECT_EQ(44u,
1614 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001615 buf.data(), length_bytes));
1616 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001617 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001618
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001619 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001620 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001621}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001622
rycsmith3e785602019-03-05 21:48:36 +00001623TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001624 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001625 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1626 FPDF_PAGE page = LoadPage(0);
1627 ASSERT_TRUE(page);
1628 {
1629 // First two annotations do not have "MaxLen" attribute.
1630 for (int i = 0; i < 2; i++) {
1631 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1632 ASSERT_TRUE(annot);
1633
1634 // Verify that no "MaxLen" key present.
1635 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1636
1637 float value;
1638 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1639 }
1640
1641 // Annotation in index 2 has "MaxLen" of 10.
1642 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1643 ASSERT_TRUE(annot);
1644
1645 // Verify that "MaxLen" key present.
1646 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1647
1648 float value;
1649 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1650 EXPECT_FLOAT_EQ(10.0f, value);
1651
1652 // Check bad inputs.
1653 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1654 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1655 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1656 // Ask for key that exists but is not a number.
1657 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1658 }
1659
1660 UnloadPage(page);
1661}
1662
Lei Zhangab41f252018-12-23 03:10:50 +00001663TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001664 // Open a file with four annotations and load its first page.
1665 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001666 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001667 ASSERT_TRUE(page);
1668
Lei Zhanga21d5932018-02-05 18:28:38 +00001669 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001670 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1671 static constexpr size_t kExpectNormalAPLength = 73970;
1672
Lei Zhanga21d5932018-02-05 18:28:38 +00001673 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001674 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001675 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001676
Lei Zhanga21d5932018-02-05 18:28:38 +00001677 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001678 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001679 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001680 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001681
Lei Zhanga21d5932018-02-05 18:28:38 +00001682 // Check that the string value of an AP is not returned if the buffer is too
1683 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001684 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1685 // Write in the buffer to verify it's not overwritten.
1686 memcpy(buf.data(), "abcdefgh", 8);
1687 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001688 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001689 buf.data(), normal_length_bytes - 1));
1690 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001691
Lei Zhanga21d5932018-02-05 18:28:38 +00001692 // Check that the string value of an AP is returned through a buffer that is
1693 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001694 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001695 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001696 buf.data(), normal_length_bytes));
1697 EXPECT_EQ(kMd5NormalAP,
1698 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1699 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001700
Lei Zhanga21d5932018-02-05 18:28:38 +00001701 // Check that the string value of an AP is returned through a buffer that is
1702 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001703 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1704 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001705 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001706 buf.data(), normal_length_bytes + 2));
1707 EXPECT_EQ(kMd5NormalAP,
1708 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1709 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001710
Lei Zhanga21d5932018-02-05 18:28:38 +00001711 // Check that getting an AP for a mode that does not have an AP returns an
1712 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001713 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001714 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001715 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001716
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001717 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001718 EXPECT_EQ(2u,
1719 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001720 buf.data(), 1000));
1721 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001722
Lei Zhanga21d5932018-02-05 18:28:38 +00001723 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001724 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001725 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001726 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001727 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001728 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001729 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001730
Lei Zhanga21d5932018-02-05 18:28:38 +00001731 // Set the AP correctly now.
1732 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001733 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001734
Lei Zhanga21d5932018-02-05 18:28:38 +00001735 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001736 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001737 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001738 ASSERT_EQ(24u, rollover_length_bytes);
1739 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001740 EXPECT_EQ(24u,
1741 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001742 buf.data(), rollover_length_bytes));
1743 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001744
Lei Zhanga21d5932018-02-05 18:28:38 +00001745 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001746 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1747 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001748 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001749 buf.data(), normal_length_bytes));
1750 EXPECT_EQ(kMd5NormalAP,
1751 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1752 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001753 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001754
1755 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001756 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001757 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001758
Lei Zhang0b494052019-01-31 21:41:15 +00001759 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001760 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001761 ASSERT_TRUE(page);
Lei Zhanga21d5932018-02-05 18:28:38 +00001762 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001763 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001764
Lei Zhanga21d5932018-02-05 18:28:38 +00001765 // Check that the new annotation value is equal to the value we set before
1766 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001767 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001768 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001769 ASSERT_EQ(24u, rollover_length_bytes);
1770 std::vector<FPDF_WCHAR> buf =
1771 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001772 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1773 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001774 buf.data(), rollover_length_bytes));
1775 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001776 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001777
1778 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001779 CloseSavedPage(page);
1780 CloseSavedDocument();
1781}
1782
Lei Zhangab41f252018-12-23 03:10:50 +00001783TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001784 // Open a file with four annotations and load its first page.
1785 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001786 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001787 ASSERT_TRUE(page);
1788
Lei Zhanga21d5932018-02-05 18:28:38 +00001789 {
1790 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001791 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001792 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001793
Lei Zhanga21d5932018-02-05 18:28:38 +00001794 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001795 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001796 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001797 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001798 EXPECT_EQ(73970u,
1799 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1800 nullptr, 0));
1801 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1802 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001803
Lei Zhanga21d5932018-02-05 18:28:38 +00001804 // Check that setting the Down AP to null removes the Down entry but keeps
1805 // Normal intact.
1806 EXPECT_TRUE(
1807 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1808 EXPECT_EQ(73970u,
1809 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1810 nullptr, 0));
1811 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1812 nullptr, 0));
1813 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001814
Lei Zhang75c81712018-02-08 17:22:39 +00001815 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001816}
1817
Lei Zhangab41f252018-12-23 03:10:50 +00001818TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001819 // Open a file with four annotations and load its first page.
1820 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001821 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001822 ASSERT_TRUE(page);
1823
Lei Zhanga21d5932018-02-05 18:28:38 +00001824 {
1825 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001826 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001827 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001828
Lei Zhanga21d5932018-02-05 18:28:38 +00001829 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001830 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001831 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001832 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001833 EXPECT_EQ(73970u,
1834 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1835 nullptr, 0));
1836 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1837 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001838
Lei Zhanga21d5932018-02-05 18:28:38 +00001839 // Check that setting the Normal AP to null removes the whole AP dictionary.
1840 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1841 nullptr));
1842 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1843 nullptr, 0));
1844 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1845 nullptr, 0));
1846 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001847
Lei Zhang75c81712018-02-08 17:22:39 +00001848 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001849}
1850
Lei Zhangab41f252018-12-23 03:10:50 +00001851TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001852 // Open a file with annotations and load its first page.
1853 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001854 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001855 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001856 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001857
Lei Zhanga21d5932018-02-05 18:28:38 +00001858 {
1859 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001860 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001861 ASSERT_TRUE(annot);
1862 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1863 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001864 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001865 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1866 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1867 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001868
Lei Zhanga21d5932018-02-05 18:28:38 +00001869 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001870 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001871 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1872 ASSERT_TRUE(popup);
1873 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1874 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1875 FS_RECTF rect;
1876 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1877 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1878 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001879
Lei Zhanga21d5932018-02-05 18:28:38 +00001880 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1881 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001882 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001883 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1884 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001885
Lei Zhanga21d5932018-02-05 18:28:38 +00001886 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1887 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001888 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001889 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001890 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1891 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001892 }
Jane Liu300bb272017-08-21 14:37:53 -04001893
Jane Liu300bb272017-08-21 14:37:53 -04001894 UnloadPage(page);
1895}
1896
Lei Zhangab41f252018-12-23 03:10:50 +00001897TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001898 // Open file with form text fields.
1899 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001900 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001901 ASSERT_TRUE(page);
1902
Lei Zhanga21d5932018-02-05 18:28:38 +00001903 {
1904 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001905 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001906 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001907
Lei Zhanga21d5932018-02-05 18:28:38 +00001908 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001909 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001910 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001911 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1912 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1913 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001914 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001915 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001916
Lei Zhanga21d5932018-02-05 18:28:38 +00001917 {
1918 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001919 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001920 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001921
Lei Zhanga21d5932018-02-05 18:28:38 +00001922 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001923 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001924 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001925 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1926 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1927 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001928 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1929 }
1930
1931 {
1932 // Retrieve the fourth annotation: user-editable password text field.
1933 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1934 ASSERT_TRUE(annot);
1935
1936 // Check that the flag values are as expected.
1937 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1938 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001939 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1940 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1941 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001942 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001943 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001944
1945 UnloadPage(page);
1946}
1947
Lei Zhangab41f252018-12-23 03:10:50 +00001948TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001949 // Open file with form text fields.
1950 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001951 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001952 ASSERT_TRUE(page);
1953
Lei Zhanga21d5932018-02-05 18:28:38 +00001954 {
1955 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001956 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001957 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001958
Lei Zhanga21d5932018-02-05 18:28:38 +00001959 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001960 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001961 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001962 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1963 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001964 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1965 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001966 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001967 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001968
Lei Zhanga21d5932018-02-05 18:28:38 +00001969 {
1970 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001971 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001972 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001973
Lei Zhanga21d5932018-02-05 18:28:38 +00001974 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001975 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001976 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001977 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1978 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001979 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1980 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001981 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001982 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001983
Lei Zhanga21d5932018-02-05 18:28:38 +00001984 {
1985 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001986 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001987 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001988
Lei Zhanga21d5932018-02-05 18:28:38 +00001989 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001990 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001991 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001992 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1993 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001994 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1995 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00001996 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00001997 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001998
1999 UnloadPage(page);
2000}
Diana Gage40870db2017-07-19 18:16:03 -07002001
Lei Zhangab41f252018-12-23 03:10:50 +00002002TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07002003 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002004 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002005 FPDF_PAGE page = LoadPage(0);
2006 ASSERT_TRUE(page);
2007
2008 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00002009 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
2010 EXPECT_FALSE(
2011 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00002012
Lei Zhang8da98232019-12-11 23:29:33 +00002013 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00002014 {
2015 // Verify there is an annotation.
2016 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002017 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00002018 EXPECT_TRUE(annot);
2019 }
2020
2021 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00002022 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
2023 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
2024 EXPECT_FALSE(
2025 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07002026
2027 UnloadPage(page);
2028}
2029
Lei Zhangab41f252018-12-23 03:10:50 +00002030TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07002031 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002032 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002033 FPDF_PAGE page = LoadPage(0);
2034 ASSERT_TRUE(page);
2035
Lei Zhanga21d5932018-02-05 18:28:38 +00002036 {
2037 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002038 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002039 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002040 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002041 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002042
Lei Zhanga21d5932018-02-05 18:28:38 +00002043 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002044 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Tom Sepez45c2fd02021-05-12 19:46:13 +00002045 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2046 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002047 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2048 }
Diana Gage40870db2017-07-19 18:16:03 -07002049
Lei Zhanga21d5932018-02-05 18:28:38 +00002050 {
2051 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002052 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002053 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002054 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002055 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002056
Lei Zhanga21d5932018-02-05 18:28:38 +00002057 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002058 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002059 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002060 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2061 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002062 }
Diana Gage40870db2017-07-19 18:16:03 -07002063
2064 UnloadPage(page);
2065}
2066
Lei Zhangab41f252018-12-23 03:10:50 +00002067TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07002068 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002069 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002070 FPDF_PAGE page = LoadPage(0);
2071 ASSERT_TRUE(page);
2072
Lei Zhanga21d5932018-02-05 18:28:38 +00002073 {
2074 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002075 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002076 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002077 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002078 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002079
Lei Zhanga21d5932018-02-05 18:28:38 +00002080 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002081 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002082 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002083 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2084 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002085 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2086 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002087 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002088 }
Diana Gage40870db2017-07-19 18:16:03 -07002089
Lei Zhanga21d5932018-02-05 18:28:38 +00002090 {
2091 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002092 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002093 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002094 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002095 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002096
Lei Zhanga21d5932018-02-05 18:28:38 +00002097 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002098 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002099 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002100 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2101 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002102 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2103 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002104 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002105 }
Diana Gage40870db2017-07-19 18:16:03 -07002106
Lei Zhanga21d5932018-02-05 18:28:38 +00002107 {
2108 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002109 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002110 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002111 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002112 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002113
Lei Zhanga21d5932018-02-05 18:28:38 +00002114 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002115 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002116 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002117 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2118 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002119 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2120 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
Tom Sepez45c2fd02021-05-12 19:46:13 +00002121 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
Lei Zhanga21d5932018-02-05 18:28:38 +00002122 }
Diana Gage40870db2017-07-19 18:16:03 -07002123
2124 UnloadPage(page);
2125}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002126
Hui Yingst603dcd82020-11-09 22:36:46 +00002127TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
Lei Zhang03e5e682019-09-16 19:45:55 +00002128#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst603dcd82020-11-09 22:36:46 +00002129 static const char kExpectedBitmap[] = "a1ea1ceebb26922fae576cb79ce63af0";
Lei Zhang03e5e682019-09-16 19:45:55 +00002130#else
Lei Zhang992e7e22019-02-04 19:20:58 +00002131 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
Hui Yingst603dcd82020-11-09 22:36:46 +00002132#endif
2133 static constexpr size_t kExpectedSize = 1609;
Lei Zhang992e7e22019-02-04 19:20:58 +00002134
2135 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2136
2137 FPDF_PAGE page = LoadPage(0);
2138 ASSERT_TRUE(page);
2139
2140 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2141 EXPECT_EQ(kExpectedSize, GetString().size());
2142 ClearString();
2143
2144 for (size_t i = 0; i < 10; ++i) {
2145 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2146 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2147
2148 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2149 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2150 // equal, not bigger.
2151 EXPECT_LT(kExpectedSize, GetString().size());
2152 ClearString();
2153 }
2154
2155 UnloadPage(page);
2156}
2157
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002158TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2159 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2160 FPDF_PAGE page = LoadPage(0);
2161 ASSERT_TRUE(page);
2162 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2163
2164 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002165 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002166 static const size_t kBufSize = 12;
2167 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002168
2169 {
2170 // Add a text annotation to the page.
2171 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2172 ASSERT_TRUE(annot);
2173 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2174 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2175
2176 // Make sure there is no test key, add set a value there, and read it back.
2177 std::fill(buf.begin(), buf.end(), 'x');
2178 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002179 kBufSize));
2180 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002181
Lei Zhangf0f67682019-04-08 17:03:21 +00002182 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002183 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2184
2185 std::fill(buf.begin(), buf.end(), 'x');
2186 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002187 kBufSize));
2188 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002189 }
2190
Lei Zhang05ec64c2019-01-09 03:00:06 +00002191 {
2192 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2193 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002194 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002195 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002196 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2197 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2198 // Also do the same test for its appearance string.
2199 std::fill(buf.begin(), buf.end(), 'x');
2200 ASSERT_EQ(2u,
2201 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002202 buf.data(), kBufSize));
2203 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002204
Lei Zhangf0f67682019-04-08 17:03:21 +00002205 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002206 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2207 text.get()));
2208
2209 std::fill(buf.begin(), buf.end(), 'x');
2210 ASSERT_EQ(6u,
2211 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002212 buf.data(), kBufSize));
2213 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002214 }
2215
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002216 UnloadPage(page);
2217
2218 {
2219 // Save a copy, open the copy, and check the annotation again.
2220 // Note that it renders the rotation.
2221 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002222 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002223 FPDF_PAGE saved_page = LoadSavedPage(0);
2224 ASSERT_TRUE(saved_page);
2225
Lei Zhang05ec64c2019-01-09 03:00:06 +00002226 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002227 {
2228 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2229 ASSERT_TRUE(annot);
2230 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2231
2232 std::fill(buf.begin(), buf.end(), 'x');
2233 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002234 kBufSize));
2235 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002236 }
2237
Lei Zhang05ec64c2019-01-09 03:00:06 +00002238 {
2239 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2240 ASSERT_TRUE(annot);
2241 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2242 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2243
2244 std::fill(buf.begin(), buf.end(), 'x');
2245 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002246 kBufSize));
2247 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002248 }
2249
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002250 CloseSavedPage(saved_page);
2251 CloseSavedDocument();
2252 }
2253}
rycsmithcb752f32019-02-21 18:40:53 +00002254
2255TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2256 // Open a file with combobox widget annotations and load its first page.
2257 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2258 FPDF_PAGE page = LoadPage(0);
2259 ASSERT_TRUE(page);
2260
2261 {
2262 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2263 ASSERT_TRUE(annot);
2264
2265 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2266
2267 annot.reset(FPDFPage_GetAnnot(page, 1));
2268 ASSERT_TRUE(annot);
2269
2270 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002271
2272 // Check bad form handle / annot.
2273 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2274 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2275 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002276 }
2277
2278 UnloadPage(page);
2279}
2280
2281TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2282 // Open a file with listbox widget annotations and load its first page.
2283 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2284 FPDF_PAGE page = LoadPage(0);
2285 ASSERT_TRUE(page);
2286
2287 {
2288 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2289 ASSERT_TRUE(annot);
2290
2291 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2292
2293 annot.reset(FPDFPage_GetAnnot(page, 1));
2294 ASSERT_TRUE(annot);
2295
2296 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2297 }
2298
2299 UnloadPage(page);
2300}
2301
2302TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2303 // Open a file with ink annotations and load its first page.
2304 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2305 FPDF_PAGE page = LoadPage(0);
2306 ASSERT_TRUE(page);
2307
2308 {
2309 // annotations do not have "Opt" array and will return -1
2310 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2311 ASSERT_TRUE(annot);
2312
2313 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2314
2315 annot.reset(FPDFPage_GetAnnot(page, 1));
2316 ASSERT_TRUE(annot);
2317
2318 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2319 }
2320
2321 UnloadPage(page);
2322}
2323
2324TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2325 // Open a file with combobox widget annotations and load its first page.
2326 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2327 FPDF_PAGE page = LoadPage(0);
2328 ASSERT_TRUE(page);
2329
2330 {
2331 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2332 ASSERT_TRUE(annot);
2333
2334 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002335 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002336 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002337 ASSERT_EQ(8u, length_bytes);
2338 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002339 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002340 buf.data(), length_bytes));
2341 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002342
2343 annot.reset(FPDFPage_GetAnnot(page, 1));
2344 ASSERT_TRUE(annot);
2345
2346 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002347 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002348 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002349 ASSERT_EQ(12u, length_bytes);
2350 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002351 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002352 buf.data(), length_bytes));
2353 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002354
2355 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002356 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002357 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002358 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002359 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002360 buf.data(), length_bytes));
2361 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002362
Lei Zhange7033c82019-02-26 19:30:49 +00002363 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002364 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2365 nullptr, 0));
2366 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2367 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002368
2369 // Check bad form handle / annot.
2370 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2371 EXPECT_EQ(0u,
2372 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2373 EXPECT_EQ(0u,
2374 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002375 }
2376
2377 UnloadPage(page);
2378}
2379
2380TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2381 // Open a file with listbox widget annotations and load its first page.
2382 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2383 FPDF_PAGE page = LoadPage(0);
2384 ASSERT_TRUE(page);
2385
2386 {
2387 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2388 ASSERT_TRUE(annot);
2389
2390 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002391 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002392 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002393 ASSERT_EQ(8u, length_bytes);
2394 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002395 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002396 buf.data(), length_bytes));
2397 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002398
2399 annot.reset(FPDFPage_GetAnnot(page, 1));
2400 ASSERT_TRUE(annot);
2401
2402 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002403 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002404 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002405 ASSERT_EQ(12u, length_bytes);
2406 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002407 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002408 buf.data(), length_bytes));
2409 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002410
2411 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002412 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002413 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002414 ASSERT_EQ(18u, length_bytes);
2415 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002416 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002417 buf.data(), length_bytes));
2418 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002419
2420 // indices out of range
2421 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2422 nullptr, 0));
2423 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2424 nullptr, 0));
2425 }
2426
2427 UnloadPage(page);
2428}
2429
2430TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2431 // Open a file with ink annotations and load its first page.
2432 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2433 FPDF_PAGE page = LoadPage(0);
2434 ASSERT_TRUE(page);
2435
2436 {
2437 // annotations do not have "Opt" array and will return 0
2438 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2439 ASSERT_TRUE(annot);
2440
2441 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2442 nullptr, 0));
2443
2444 annot.reset(FPDFPage_GetAnnot(page, 1));
2445 ASSERT_TRUE(annot);
2446
2447 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2448 nullptr, 0));
2449 }
2450
2451 UnloadPage(page);
2452}
Ryan Smith09c23b12019-04-25 18:09:06 +00002453
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002454TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2455 // Open a file with combobox widget annotations and load its first page.
2456 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2457 FPDF_PAGE page = LoadPage(0);
2458 ASSERT_TRUE(page);
2459
2460 {
2461 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2462 ASSERT_TRUE(annot);
2463
2464 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2465 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2466 ASSERT_EQ(3, count);
2467 for (int i = 0; i < count; i++) {
2468 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2469 }
2470
2471 annot.reset(FPDFPage_GetAnnot(page, 1));
2472 ASSERT_TRUE(annot);
2473
2474 // Checks for Combobox with Values (/V) object which is just a string.
2475 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2476 ASSERT_EQ(26, count);
2477 for (int i = 0; i < count; i++) {
2478 EXPECT_EQ(i == 1,
2479 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2480 }
2481
2482 // Checks for index outside bound i.e. (index >= CountOption()).
2483 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2484 /*index=*/26));
2485 // Checks for negetive index.
2486 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2487 /*index=*/-1));
2488
2489 // Checks for bad form handle/annot.
2490 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2491 EXPECT_FALSE(
2492 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2493 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2494 }
2495
2496 UnloadPage(page);
2497}
2498
2499TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2500 // Open a file with listbox widget annotations and load its first page.
2501 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2502 FPDF_PAGE page = LoadPage(0);
2503 ASSERT_TRUE(page);
2504
2505 {
2506 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2507 ASSERT_TRUE(annot);
2508
2509 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2510 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2511 ASSERT_EQ(3, count);
2512 for (int i = 0; i < count; i++) {
2513 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2514 }
2515
2516 annot.reset(FPDFPage_GetAnnot(page, 1));
2517 ASSERT_TRUE(annot);
2518
2519 // Checks for Listbox with Values (/V) object which is just a string.
2520 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2521 ASSERT_EQ(26, count);
2522 for (int i = 0; i < count; i++) {
2523 EXPECT_EQ(i == 1,
2524 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2525 }
2526
2527 annot.reset(FPDFPage_GetAnnot(page, 3));
2528 ASSERT_TRUE(annot);
2529
2530 // Checks for Listbox with only Selected indices (/I) object which is an
2531 // array with multiple objects.
2532 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2533 ASSERT_EQ(5, count);
2534 for (int i = 0; i < count; i++) {
2535 bool expected = (i == 1 || i == 3);
2536 EXPECT_EQ(expected,
2537 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2538 }
2539
2540 annot.reset(FPDFPage_GetAnnot(page, 4));
2541 ASSERT_TRUE(annot);
2542
2543 // Checks for Listbox with Values (/V) object which is an array with
2544 // multiple objects.
2545 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2546 ASSERT_EQ(5, count);
2547 for (int i = 0; i < count; i++) {
2548 bool expected = (i == 2 || i == 4);
2549 EXPECT_EQ(expected,
2550 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2551 }
2552
2553 annot.reset(FPDFPage_GetAnnot(page, 5));
2554 ASSERT_TRUE(annot);
2555
2556 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2557 // objects conflict with different lengths.
2558 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2559 ASSERT_EQ(5, count);
2560 for (int i = 0; i < count; i++) {
2561 bool expected = (i == 0 || i == 2);
2562 EXPECT_EQ(expected,
2563 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2564 }
2565 }
2566
2567 UnloadPage(page);
2568}
2569
2570TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2571 // Open a file with multiple form field annotations and load its first page.
2572 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2573 FPDF_PAGE page = LoadPage(0);
2574 ASSERT_TRUE(page);
2575
2576 {
2577 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2578 ASSERT_TRUE(annot);
2579
2580 // Checks for link annotation.
2581 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2582 /*index=*/0));
2583
2584 annot.reset(FPDFPage_GetAnnot(page, 3));
2585 ASSERT_TRUE(annot);
2586
2587 // Checks for text field annotation.
2588 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2589 /*index=*/0));
2590 }
2591
2592 UnloadPage(page);
2593}
2594
Ryan Smith09c23b12019-04-25 18:09:06 +00002595TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2596 // Open a file with combobox annotations and load its first page.
2597 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2598 FPDF_PAGE page = LoadPage(0);
2599 ASSERT_TRUE(page);
2600
2601 {
2602 // All 3 widgets have Tf font size 12.
2603 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2604 ASSERT_TRUE(annot);
2605
2606 float value;
2607 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2608 EXPECT_EQ(12.0, value);
2609
2610 annot.reset(FPDFPage_GetAnnot(page, 1));
2611 ASSERT_TRUE(annot);
2612
2613 float value_two;
2614 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2615 EXPECT_EQ(12.0, value_two);
2616
2617 annot.reset(FPDFPage_GetAnnot(page, 2));
2618 ASSERT_TRUE(annot);
2619
2620 float value_three;
2621 ASSERT_TRUE(
2622 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2623 EXPECT_EQ(12.0, value_three);
2624 }
2625
2626 UnloadPage(page);
2627}
2628
2629TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2630 // Open a file with textfield annotations and load its first page.
2631 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2632 FPDF_PAGE page = LoadPage(0);
2633 ASSERT_TRUE(page);
2634
2635 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002636 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002637 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2638 ASSERT_TRUE(annot);
2639
2640 float value;
2641 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2642 EXPECT_EQ(12.0, value);
2643
2644 annot.reset(FPDFPage_GetAnnot(page, 1));
2645 ASSERT_TRUE(annot);
2646
2647 float value_two;
2648 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2649 EXPECT_EQ(12.0, value_two);
2650
2651 annot.reset(FPDFPage_GetAnnot(page, 2));
2652 ASSERT_TRUE(annot);
2653
2654 float value_three;
2655 ASSERT_TRUE(
2656 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2657 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002658
2659 float value_four;
2660 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2661 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002662 }
2663
2664 UnloadPage(page);
2665}
2666
2667TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2668 // Open a file with ink annotations and load its first page.
2669 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2670 FPDF_PAGE page = LoadPage(0);
2671 ASSERT_TRUE(page);
2672
2673 {
2674 // Annotations that do not have variable text and will return -1.
2675 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2676 ASSERT_TRUE(annot);
2677
2678 float value;
2679 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2680
2681 annot.reset(FPDFPage_GetAnnot(page, 1));
2682 ASSERT_TRUE(annot);
2683
2684 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2685 }
2686
2687 UnloadPage(page);
2688}
2689
2690TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2691 // Open a file with combobox annotations and load its first page.
2692 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2693 FPDF_PAGE page = LoadPage(0);
2694 ASSERT_TRUE(page);
2695
2696 {
2697 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2698 ASSERT_TRUE(annot);
2699
2700 // Check bad form handle / annot.
2701 float value;
2702 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2703 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2704 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2705 }
2706
2707 UnloadPage(page);
2708}
2709
2710TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2711 // Open a file with textfield annotations and load its first page.
2712 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2713 FPDF_PAGE page = LoadPage(0);
2714 ASSERT_TRUE(page);
2715
2716 {
2717 // Obtain the first annotation, a text field with negative font size, -12.
2718 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2719 ASSERT_TRUE(annot);
2720
2721 float value;
2722 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2723 EXPECT_EQ(-12.0, value);
2724 }
2725
2726 UnloadPage(page);
2727}
Ryan Smith23fdf892019-07-17 21:51:26 +00002728
2729TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2730 // Open a file with checkbox and radiobuttons widget annotations and load its
2731 // first page.
2732 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2733 FPDF_PAGE page = LoadPage(0);
2734 ASSERT_TRUE(page);
2735
2736 {
2737 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2738 ASSERT_TRUE(annot);
2739 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2740 }
2741
2742 UnloadPage(page);
2743}
2744
2745TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2746 // Open a file with checkbox and radiobutton widget annotations and load its
2747 // first page.
2748 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2749 FPDF_PAGE page = LoadPage(0);
2750 ASSERT_TRUE(page);
2751
2752 {
2753 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2754 ASSERT_TRUE(annot);
2755 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2756 }
2757
2758 UnloadPage(page);
2759}
2760
2761TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2762 // Open a file with checkbox and radiobutton widget annotations and load its
2763 // first page.
2764 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2765 FPDF_PAGE page = LoadPage(0);
2766 ASSERT_TRUE(page);
2767
2768 {
2769 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2770 ASSERT_TRUE(annot);
2771 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2772
2773 annot.reset(FPDFPage_GetAnnot(page, 6));
2774 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2775
2776 annot.reset(FPDFPage_GetAnnot(page, 7));
2777 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2778 }
2779
2780 UnloadPage(page);
2781}
2782
2783TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2784 // Open a file with checkbox and radiobutton widget annotations and load its
2785 // first page.
2786 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2787 FPDF_PAGE page = LoadPage(0);
2788 ASSERT_TRUE(page);
2789
2790 {
2791 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2792 ASSERT_TRUE(annot);
2793 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2794
2795 annot.reset(FPDFPage_GetAnnot(page, 3));
2796 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2797
2798 annot.reset(FPDFPage_GetAnnot(page, 4));
2799 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2800 }
2801
2802 UnloadPage(page);
2803}
2804
2805TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2806 // Open a file with checkbox and radiobuttons widget annotations and load its
2807 // first page.
2808 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2809 FPDF_PAGE page = LoadPage(0);
2810 ASSERT_TRUE(page);
2811
2812 {
2813 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2814 ASSERT_TRUE(annot);
2815 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2816 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2817 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2818 }
2819
2820 UnloadPage(page);
2821}
2822
2823TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2824 // Open a file with text widget annotations and load its first page.
2825 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2826 FPDF_PAGE page = LoadPage(0);
2827 ASSERT_TRUE(page);
2828
2829 {
2830 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2831 ASSERT_TRUE(annot);
2832 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2833 }
2834
2835 UnloadPage(page);
2836}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002837
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002838TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2839 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002840 FPDF_PAGE page = LoadPage(0);
2841 ASSERT_TRUE(page);
2842
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002843 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2844
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002845 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002846 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002847 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002848 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002849 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002850
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002851 constexpr int kExpectedAnnotTypes[] = {-1,
2852 FPDF_FORMFIELD_COMBOBOX,
2853 FPDF_FORMFIELD_LISTBOX,
2854 FPDF_FORMFIELD_TEXTFIELD,
2855 FPDF_FORMFIELD_CHECKBOX,
2856 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002857
Lei Zhang4501a502020-05-18 16:52:59 +00002858 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002859 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002860 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002861 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002862 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2863 }
2864 UnloadPage(page);
2865}
2866
2867TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2868 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2869 FPDF_PAGE page = LoadPage(0);
2870 ASSERT_TRUE(page);
2871
2872 {
2873 EXPECT_EQ(0u,
2874 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2875
2876 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2877 ASSERT_TRUE(annot);
2878
2879 EXPECT_EQ(0u,
2880 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2881
2882 unsigned long length_bytes =
2883 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2884 ASSERT_EQ(2u, length_bytes);
2885 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2886 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2887 buf.data(), length_bytes));
2888 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2889 }
2890 {
2891 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2892 ASSERT_TRUE(annot);
2893
2894 unsigned long length_bytes =
2895 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2896 ASSERT_EQ(18u, length_bytes);
2897 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2898 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2899 buf.data(), length_bytes));
2900 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2901 }
2902 UnloadPage(page);
2903}
2904
2905TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2906 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2907 FPDF_PAGE page = LoadPage(0);
2908 ASSERT_TRUE(page);
2909
2910 {
2911 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2912 ASSERT_TRUE(annot);
2913
2914 unsigned long length_bytes =
2915 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2916 ASSERT_EQ(2u, length_bytes);
2917 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2918 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2919 buf.data(), length_bytes));
2920 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2921 }
2922 {
2923 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2924 ASSERT_TRUE(annot);
2925
2926 unsigned long length_bytes =
2927 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2928 ASSERT_EQ(14u, length_bytes);
2929 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2930 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2931 buf.data(), length_bytes));
2932 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2933 }
2934 UnloadPage(page);
2935}
2936
2937TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2938 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2939 FPDF_PAGE page = LoadPage(0);
2940 ASSERT_TRUE(page);
2941
2942 {
2943 EXPECT_EQ(0u,
2944 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2945
2946 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2947 ASSERT_TRUE(annot);
2948
2949 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2950
2951 unsigned long length_bytes =
2952 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2953 ASSERT_EQ(18u, length_bytes);
2954 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2955 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2956 buf.data(), length_bytes));
2957 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2958 }
2959 UnloadPage(page);
2960}
2961
2962TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2963 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2964 FPDF_PAGE page = LoadPage(0);
2965 ASSERT_TRUE(page);
2966
2967 {
2968 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2969 ASSERT_TRUE(annot);
2970
2971 unsigned long length_bytes =
2972 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2973 ASSERT_EQ(30u, length_bytes);
2974 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2975 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2976 buf.data(), length_bytes));
2977 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2978 }
2979 UnloadPage(page);
2980}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002981
Lei Zhang9b444002020-04-17 17:35:23 +00002982TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2983 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002984 FPDF_PAGE page = LoadPage(0);
2985 ASSERT_TRUE(page);
2986
Lei Zhang9b444002020-04-17 17:35:23 +00002987 // Verify widgets are by default focusable.
2988 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2989 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002990
Lei Zhang9b444002020-04-17 17:35:23 +00002991 // Expected annot subtypes for page 0 of annots.pdf.
2992 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2993 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2994 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2995 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2996 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002997
Lei Zhang9b444002020-04-17 17:35:23 +00002998 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002999 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00003000 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
3001 kExpectedAnnotSubtypes,
3002 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003003
Lei Zhang9b444002020-04-17 17:35:23 +00003004 // Make no annotation type focusable using the preferred method.
3005 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
3006 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003007
Lei Zhang9b444002020-04-17 17:35:23 +00003008 // Restore the focusable type count back to 1, then set it back to 0 using a
3009 // different method.
3010 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
3011 ASSERT_TRUE(
3012 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
3013 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003014
Lei Zhang9b444002020-04-17 17:35:23 +00003015 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00003016 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003017
Lei Zhang9b444002020-04-17 17:35:23 +00003018 // Now make links focusable.
3019 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
3020 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003021
Lei Zhang9b444002020-04-17 17:35:23 +00003022 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00003023 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00003024 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
3025 kExpectedAnnotSubtypes,
3026 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003027
Lei Zhang9b444002020-04-17 17:35:23 +00003028 // Test invalid parameters.
3029 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00003030 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00003031 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00003032 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003033 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
3034
Lei Zhang9b444002020-04-17 17:35:23 +00003035 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
3036 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
3037 subtypes.size()));
3038 EXPECT_FALSE(
3039 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
3040 EXPECT_FALSE(
3041 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003042
3043 UnloadPage(page);
3044}
Lei Zhang671aece2020-04-14 19:02:26 +00003045
Hui Yingstea3816d2020-07-28 22:35:11 +00003046TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00003047 ASSERT_TRUE(OpenDocument("annots.pdf"));
3048 FPDF_PAGE page = LoadPage(0);
3049 ASSERT_TRUE(page);
3050
3051 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003052#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3053 static const char kMd5sum[] = "4ca14c670396711194b40ecc2514969b";
3054#else
Lei Zhang671aece2020-04-14 19:02:26 +00003055#if defined(OS_WIN)
3056 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
Lei Zhang0c03d632020-07-30 17:05:36 +00003057#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003058 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
3059#else
3060 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
3061#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003062#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003063 // Check the initial rendering.
3064 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3065 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3066 }
3067
3068 // Make links and highlights focusable.
3069 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
3070 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00003071 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00003072 ASSERT_TRUE(
3073 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
3074 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3075 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
3076 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
3077 subtypes.size()));
3078 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
3079 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
3080
3081 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003082#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3083 static const char kMd5sum[] = "48cb60b3f9bc364c73582aff3418451e";
3084#else
Lei Zhang671aece2020-04-14 19:02:26 +00003085#if defined(OS_WIN)
3086 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
Lei Zhang0c03d632020-07-30 17:05:36 +00003087#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003088 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
3089#else
3090 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
3091#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003092#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003093 // Focus the first link and check the rendering.
3094 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3095 ASSERT_TRUE(annot);
3096 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3097 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3098 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3099 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3100 }
3101
3102 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003103#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3104 static const char kMd5sum[] = "d09869ff0a209daf179da2d7a58142b1";
3105#else
Lei Zhang671aece2020-04-14 19:02:26 +00003106#if defined(OS_WIN)
3107 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
Lei Zhang0c03d632020-07-30 17:05:36 +00003108#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003109 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
3110#else
3111 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
3112#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003113#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003114 // Focus the first highlight and check the rendering.
3115 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3116 ASSERT_TRUE(annot);
3117 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3118 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3119 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3120 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3121 }
3122
3123 UnloadPage(page);
3124}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003125
3126TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
3127 ASSERT_TRUE(OpenDocument("annots.pdf"));
3128 FPDF_PAGE page = LoadPage(0);
3129 ASSERT_TRUE(page);
3130 {
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003131 constexpr char kExpectedResult[] =
3132 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3133 "fpdf_text.h";
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003134
Lei Zhang306874b2021-04-16 00:33:36 +00003135 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3136 ASSERT_TRUE(annot);
3137 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3138 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()),
3139 kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003140 }
3141
3142 {
3143 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3144 ASSERT_TRUE(annot);
3145 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3146 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3147 }
3148
3149 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3150
3151 UnloadPage(page);
3152}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003153
3154TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3155 // Open a file with radio button widget annotations and load its first page.
3156 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3157 FPDF_PAGE page = LoadPage(0);
3158 ASSERT_TRUE(page);
3159
3160 {
3161 // Checks for bad annot.
3162 EXPECT_EQ(-1,
3163 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3164
3165 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3166 ASSERT_TRUE(annot);
3167
3168 // Checks for bad form handle.
3169 EXPECT_EQ(-1,
3170 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3171
3172 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3173 }
3174
3175 UnloadPage(page);
3176}
3177
3178TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3179 // Open a file with checkbox widget annotations and load its first page.
3180 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3181 FPDF_PAGE page = LoadPage(0);
3182 ASSERT_TRUE(page);
3183
3184 {
3185 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3186 ASSERT_TRUE(annot);
3187 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3188 }
3189
3190 UnloadPage(page);
3191}
3192
3193TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3194 // Open a file with ink annotations and load its first page.
3195 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3196 FPDF_PAGE page = LoadPage(0);
3197 ASSERT_TRUE(page);
3198
3199 {
3200 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3201 ASSERT_TRUE(annot);
3202 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3203 }
3204
3205 UnloadPage(page);
3206}
3207
3208TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3209 // Open a file with radio button widget annotations and load its first page.
3210 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3211 FPDF_PAGE page = LoadPage(0);
3212 ASSERT_TRUE(page);
3213
3214 {
3215 // Checks for bad annot.
3216 EXPECT_EQ(-1,
3217 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3218
3219 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3220 ASSERT_TRUE(annot);
3221
3222 // Checks for bad form handle.
3223 EXPECT_EQ(-1,
3224 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3225
3226 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3227 }
3228
3229 UnloadPage(page);
3230}
3231
3232TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3233 // Open a file with checkbox widget annotations and load its first page.
3234 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3235 FPDF_PAGE page = LoadPage(0);
3236 ASSERT_TRUE(page);
3237
3238 {
3239 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3240 ASSERT_TRUE(annot);
3241 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3242 }
3243
3244 UnloadPage(page);
3245}
3246
3247TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3248 // Open a file with ink annotations and load its first page.
3249 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3250 FPDF_PAGE page = LoadPage(0);
3251 ASSERT_TRUE(page);
3252
3253 {
3254 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3255 ASSERT_TRUE(annot);
3256 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3257 }
3258
3259 UnloadPage(page);
3260}
3261
3262TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3263 // Open a file with radio button widget annotations and load its first page.
3264 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3265 FPDF_PAGE page = LoadPage(0);
3266 ASSERT_TRUE(page);
3267
3268 {
3269 // Checks for bad annot.
3270 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3271 form_handle(), /*annot=*/nullptr,
3272 /*buffer=*/nullptr, /*buflen=*/0));
3273
3274 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3275 ASSERT_TRUE(annot);
3276
3277 // Checks for bad form handle.
3278 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3279 /*hHandle=*/nullptr, annot.get(),
3280 /*buffer=*/nullptr, /*buflen=*/0));
3281
3282 unsigned long length_bytes =
3283 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3284 /*buffer=*/nullptr, /*buflen=*/0);
3285 ASSERT_EQ(14u, length_bytes);
3286 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3287 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3288 buf.data(), length_bytes));
3289 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3290 }
3291
3292 UnloadPage(page);
3293}
3294
3295TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3296 // Open a file with checkbox widget annotations and load its first page.
3297 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3298 FPDF_PAGE page = LoadPage(0);
3299 ASSERT_TRUE(page);
3300
3301 {
3302 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3303 ASSERT_TRUE(annot);
3304
3305 unsigned long length_bytes =
3306 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3307 /*buffer=*/nullptr, /*buflen=*/0);
3308 ASSERT_EQ(8u, length_bytes);
3309 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3310 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3311 buf.data(), length_bytes));
3312 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3313 }
3314
3315 UnloadPage(page);
3316}
3317
3318TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3319 // Open a file with ink annotations and load its first page.
3320 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3321 FPDF_PAGE page = LoadPage(0);
3322 ASSERT_TRUE(page);
3323
3324 {
3325 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3326 ASSERT_TRUE(annot);
3327 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3328 /*buffer=*/nullptr,
3329 /*buflen=*/0));
3330 }
3331
3332 UnloadPage(page);
3333}
Miklos Vajnad1a24fb2020-10-26 18:07:13 +00003334
3335TEST_F(FPDFAnnotEmbedderTest, Redactannotation) {
3336 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3337 FPDF_PAGE page = LoadPage(0);
3338 ASSERT_TRUE(page);
3339 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3340
3341 {
3342 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3343 ASSERT_TRUE(annot);
3344 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3345 }
3346
3347 UnloadPage(page);
3348}
Miklos Vajna09ecef62020-11-10 21:50:38 +00003349
3350TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) {
3351 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3352 FPDF_PAGE page = LoadPage(0);
3353 ASSERT_TRUE(page);
3354 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3355
3356 {
3357 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3358 ASSERT_TRUE(annot);
3359
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003360 // FPDFAnnot_GetVertices() positive testing.
Miklos Vajna09ecef62020-11-10 21:50:38 +00003361 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3362 const size_t kExpectedSize = 3;
3363 ASSERT_EQ(kExpectedSize, size);
3364 std::vector<FS_POINTF> vertices_buffer(size);
3365 EXPECT_EQ(size,
3366 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3367 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3368 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3369 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3370 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3371 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3372 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3373
3374 // FPDFAnnot_GetVertices() negative testing.
3375 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3376
3377 // vertices_buffer is not overwritten if it is too small.
3378 vertices_buffer.resize(1);
3379 vertices_buffer[0].x = 42;
3380 vertices_buffer[0].y = 43;
3381 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3382 vertices_buffer.size());
3383 EXPECT_EQ(kExpectedSize, size);
3384 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3385 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3386 }
3387
3388 {
3389 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3390 ASSERT_TRUE(annot);
3391
3392 // This has an odd number of elements in the vertices array, ignore the last
3393 // element.
3394 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3395 const size_t kExpectedSize = 3;
3396 ASSERT_EQ(kExpectedSize, size);
3397 std::vector<FS_POINTF> vertices_buffer(size);
3398 EXPECT_EQ(size,
3399 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3400 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3401 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3402 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3403 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3404 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3405 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3406 }
3407
3408 {
3409 // Wrong annotation type.
3410 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3411 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3412 }
3413
3414 UnloadPage(page);
3415}
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003416
3417TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) {
3418 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3419 FPDF_PAGE page = LoadPage(0);
3420 ASSERT_TRUE(page);
3421 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3422
3423 {
3424 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3425 ASSERT_TRUE(annot);
3426
3427 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3428 // testing.
3429 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3430 const size_t kExpectedSize = 1;
3431 ASSERT_EQ(kExpectedSize, size);
3432 const unsigned long kPathIndex = 0;
3433 unsigned long path_size =
3434 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3435 const size_t kExpectedPathSize = 3;
3436 ASSERT_EQ(kExpectedPathSize, path_size);
3437 std::vector<FS_POINTF> path_buffer(path_size);
3438 EXPECT_EQ(path_size,
3439 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3440 path_buffer.data(), path_size));
3441 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3442 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3443 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3444 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3445 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3446 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3447
3448 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3449 // testing.
3450 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3451 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3452
3453 // out of bounds path_index.
3454 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3455
3456 // path_buffer is not overwritten if it is too small.
3457 path_buffer.resize(1);
3458 path_buffer[0].x = 42;
3459 path_buffer[0].y = 43;
3460 path_size = FPDFAnnot_GetInkListPath(
3461 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3462 EXPECT_EQ(kExpectedSize, size);
3463 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3464 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3465 }
3466
3467 {
3468 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3469 ASSERT_TRUE(annot);
3470
3471 // This has an odd number of elements in the path array, ignore the last
3472 // element.
3473 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3474 const size_t kExpectedSize = 1;
3475 ASSERT_EQ(kExpectedSize, size);
3476 const unsigned long kPathIndex = 0;
3477 unsigned long path_size =
3478 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3479 const size_t kExpectedPathSize = 3;
3480 ASSERT_EQ(kExpectedPathSize, path_size);
3481 std::vector<FS_POINTF> path_buffer(path_size);
3482 EXPECT_EQ(path_size,
3483 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3484 path_buffer.data(), path_size));
3485 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3486 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3487 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3488 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3489 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3490 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3491 }
3492
3493 {
3494 // Wrong annotation type.
3495 ScopedFPDFAnnotation polygon_annot(
3496 FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON));
3497 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3498 const unsigned long kPathIndex = 0;
3499 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3500 nullptr, 0));
3501 }
3502
3503 UnloadPage(page);
3504}
Miklos Vajna30f45a62020-12-04 19:12:31 +00003505
3506TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) {
3507 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3508 FPDF_PAGE page = LoadPage(0);
3509 ASSERT_TRUE(page);
3510 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3511
3512 {
3513 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3514 ASSERT_TRUE(annot);
3515
3516 // FPDFAnnot_GetVertices() positive testing.
3517 FS_POINTF start;
3518 FS_POINTF end;
3519 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3520 EXPECT_FLOAT_EQ(159.0f, start.x);
3521 EXPECT_FLOAT_EQ(296.0f, start.y);
3522 EXPECT_FLOAT_EQ(472.0f, end.x);
3523 EXPECT_FLOAT_EQ(243.42f, end.y);
3524
3525 // FPDFAnnot_GetVertices() negative testing.
3526 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3527 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3528 }
3529
3530 {
3531 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3532 ASSERT_TRUE(annot);
3533
3534 // Too few elements in the line array.
3535 FS_POINTF start;
3536 FS_POINTF end;
3537 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3538 }
3539
3540 {
3541 // Wrong annotation type.
3542 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3543 FS_POINTF start;
3544 FS_POINTF end;
3545 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3546 }
3547
3548 UnloadPage(page);
3549}
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003550
3551TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) {
3552 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3553 FPDF_PAGE page = LoadPage(0);
3554 ASSERT_TRUE(page);
3555 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3556
3557 {
3558 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3559 ASSERT_TRUE(annot);
3560
3561 // FPDFAnnot_GetBorder() positive testing.
3562 float horizontal_radius;
3563 float vertical_radius;
3564 float border_width;
3565 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3566 &vertical_radius, &border_width));
3567 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3568 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3569 EXPECT_FLOAT_EQ(2.0f, border_width);
3570
3571 // FPDFAnnot_GetBorder() negative testing.
3572 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3573 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3574 }
3575
3576 {
3577 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3578 ASSERT_TRUE(annot);
3579
3580 // Too few elements in the border array.
3581 float horizontal_radius;
3582 float vertical_radius;
3583 float border_width;
3584 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3585 &vertical_radius, &border_width));
Lei Zhang21ea6652021-04-15 23:24:46 +00003586
3587 // FPDFAnnot_SetBorder() positive testing.
3588 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3589 /*vertical_radius=*/3.5f,
3590 /*border_width=*/4.0f));
3591
3592 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3593 &vertical_radius, &border_width));
3594 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3595 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3596 EXPECT_FLOAT_EQ(4.0f, border_width);
3597
3598 // FPDFAnnot_SetBorder() negative testing.
3599 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3600 /*vertical_radius=*/2.5f,
3601 /*border_width=*/3.0f));
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003602 }
3603
3604 UnloadPage(page);
3605}
Lei Zhang24de1492021-04-19 18:06:43 +00003606
3607// Due to https://crbug.com/pdfium/570, the AnnotationBorder test above cannot
3608// actually render the line annotations inside line_annot.pdf. For now, use a
3609// square annotation in annots.pdf for testing.
3610TEST_F(FPDFAnnotEmbedderTest, AnnotationBorderRendering) {
3611 ASSERT_TRUE(OpenDocument("annots.pdf"));
3612 FPDF_PAGE page = LoadPage(1);
3613 ASSERT_TRUE(page);
3614 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
3615
3616#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3617 constexpr char kOriginalChecksum[] = "bc9ba381d6d46ff93ed0b5288b763b60";
3618 constexpr char kModifiedChecksum[] = "0f3e10336f67cb6a8731a06d26d01e50";
3619#elif defined(OS_WIN)
3620 constexpr char kOriginalChecksum[] = "f9fcab6ac610ee2347c4eba6be86a90c";
3621 constexpr char kModifiedChecksum[] = "e0e9ad5b67ef84288d446861265898b3";
3622#elif defined(OS_APPLE)
3623 constexpr char kOriginalChecksum[] = "1839f5df5fb4fae10cf3793568e73ede";
3624 constexpr char kModifiedChecksum[] = "abd4f5d1c3b8d8cfc572b389e589da5a";
3625#else
3626 constexpr char kOriginalChecksum[] = "ccf6667b34ec2452bea0b5f1a0194191";
3627 constexpr char kModifiedChecksum[] = "1bbdb473d0757843e82053b0bd3298bc";
3628#endif
3629
3630 {
3631 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
3632 ASSERT_TRUE(annot);
3633 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
3634
3635 {
3636 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3637 CompareBitmap(bitmap.get(), 612, 792, kOriginalChecksum);
3638 }
3639
3640 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3641 /*vertical_radius=*/3.5f,
3642 /*border_width=*/4.0f));
3643
3644 {
3645 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3646 CompareBitmap(bitmap.get(), 612, 792, kModifiedChecksum);
3647 }
3648 }
3649
3650 // Save the document and close the page.
3651 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
3652 UnloadPage(page);
3653
3654 ASSERT_TRUE(OpenSavedDocument());
3655 page = LoadSavedPage(1);
3656 ASSERT_TRUE(page);
3657 VerifySavedRendering(page, 612, 792, kModifiedChecksum);
3658
3659 CloseSavedPage(page);
3660 CloseSavedDocument();
3661}