blob: c5fef249fa7f978f7736a75d61977c400cf8c582 [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());
1294 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1295 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001296
Lei Zhanga21d5932018-02-05 18:28:38 +00001297 // Set the HIDDEN flag.
1298 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1299 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1300 flags = FPDFAnnot_GetFlags(annot.get());
1301 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1302 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001303
Lei Zhanga21d5932018-02-05 18:28:38 +00001304 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001305 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001306 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001307 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001308 }
Jane Liub137e752017-07-05 15:04:33 -04001309
Lei Zhanga21d5932018-02-05 18:28:38 +00001310 // Unset the HIDDEN flag.
1311 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1312 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1313 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1314 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1315 flags = FPDFAnnot_GetFlags(annot.get());
1316 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1317 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001318
Lei Zhanga21d5932018-02-05 18:28:38 +00001319 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001320 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001321 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001322 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1323 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001324 }
Jane Liub137e752017-07-05 15:04:33 -04001325
Jane Liub137e752017-07-05 15:04:33 -04001326 UnloadPage(page);
1327}
Jane Liu36567742017-07-06 11:13:35 -04001328
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001329// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1330#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001331#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1332#else
1333#define MAYBE_AddAndModifyImage AddAndModifyImage
1334#endif
1335TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001336#if defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001337#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001338 static const char kMd5NewImage[] = "26a8eb30937226a677839379e0d7ae1a";
1339 static const char kMd5ModifiedImage[] = "2985114b32ba1a96be78ee643fe31aa5";
1340#else
1341 static const char kMd5NewImage[] = "14012ab500b4671fa73dd760129a8a93";
1342 static const char kMd5ModifiedImage[] = "5f97f98f58ed04dc393f31460485f1a2";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001343#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001344#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001345#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001346 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
1347 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +00001348#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001349 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
1350 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -04001351#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001352 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
1353 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -04001354#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001355#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001356
1357 // Open a file with two annotations and load its first page.
1358 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001359 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001360 ASSERT_TRUE(page);
1361 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1362
1363 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001364 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001365 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001366 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001367 }
Jane Liu36567742017-07-06 11:13:35 -04001368
Jane Liu36567742017-07-06 11:13:35 -04001369 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001370 FPDF_BITMAP image_bitmap;
1371
1372 {
1373 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001374 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001375 ASSERT_TRUE(annot);
1376 FS_RECTF rect;
1377 rect.left = 200.f;
1378 rect.bottom = 600.f;
1379 rect.right = 400.f;
1380 rect.top = 800.f;
1381 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1382
1383 // Add a solid-color translucent image object to the new annotation.
1384 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1385 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1386 0xeeeecccc);
1387 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1388 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1389 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1390 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1391 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1392 kBitmapSize, 0, 0));
1393 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1394 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1395 }
Jane Liu36567742017-07-06 11:13:35 -04001396
1397 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001398 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001399 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001400 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001401 }
Jane Liu36567742017-07-06 11:13:35 -04001402
Lei Zhanga21d5932018-02-05 18:28:38 +00001403 {
1404 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001405 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001406 ASSERT_TRUE(annot);
1407 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1408 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1409 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001410
Lei Zhanga21d5932018-02-05 18:28:38 +00001411 // Modify the image in the new annotation.
1412 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1413 0xff000000);
1414 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1415 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1416 }
Jane Liu36567742017-07-06 11:13:35 -04001417
Lei Zhangec618142021-04-13 17:36:46 +00001418 // Save the document and close the page.
Jane Liu36567742017-07-06 11:13:35 -04001419 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001420 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001421 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001422
1423 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001424 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001425}
1426
Hui Yingst6cd754f2020-05-14 04:05:25 +00001427TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001428#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001429#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001430 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1431 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001432#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001433 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1434 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001435#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001436#else
1437#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001438 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1439 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Lei Zhang0c03d632020-07-30 17:05:36 +00001440#elif defined(OS_APPLE)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001441 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1442 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001443#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001444 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1445 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001446#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001447#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001448
1449 // Open a file with two annotations and load its first page.
1450 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001451 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001452 ASSERT_TRUE(page);
1453 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1454
1455 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001456 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001457 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001458 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001459 }
Jane Liu36567742017-07-06 11:13:35 -04001460
Lei Zhanga21d5932018-02-05 18:28:38 +00001461 {
1462 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001463 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001464 ASSERT_TRUE(annot);
1465 FS_RECTF rect;
1466 rect.left = 200.f;
1467 rect.bottom = 550.f;
1468 rect.right = 450.f;
1469 rect.top = 650.f;
1470 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001471
Lei Zhanga21d5932018-02-05 18:28:38 +00001472 // Add a translucent text object to the new annotation.
1473 FPDF_PAGEOBJECT text_object =
1474 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1475 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001476 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001477 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1478 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001479 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001480 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1481 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1482 }
Jane Liu36567742017-07-06 11:13:35 -04001483
1484 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001485 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001486 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001487 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001488 }
Jane Liu36567742017-07-06 11:13:35 -04001489
Lei Zhanga21d5932018-02-05 18:28:38 +00001490 {
1491 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001492 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001493 ASSERT_TRUE(annot);
1494 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1495 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1496 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001497
Lei Zhanga21d5932018-02-05 18:28:38 +00001498 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001499 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001500 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1501 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1502 }
Jane Liu36567742017-07-06 11:13:35 -04001503
1504 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001505 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001506 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001507 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001508 }
Jane Liu36567742017-07-06 11:13:35 -04001509
1510 // Remove the new annotation, and check that the page renders as before.
1511 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001512 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001513 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001514 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001515 }
Jane Liu36567742017-07-06 11:13:35 -04001516
1517 UnloadPage(page);
1518}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001519
Hui Yingst9b6b1542020-07-27 16:11:12 +00001520TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001521 // Open a file with four annotations and load its first page.
1522 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001523 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001524 ASSERT_TRUE(page);
1525
Lei Zhang4f556b82019-04-08 16:32:41 +00001526 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001527
Lei Zhanga21d5932018-02-05 18:28:38 +00001528 {
1529 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001530 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001531 ASSERT_TRUE(annot);
1532
1533 // Check that a non-existent key does not exist.
1534 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1535
1536 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001537 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001538 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001539 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1540 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1541 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001542
1543 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001544 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001545 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001546 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001547 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001548 ASSERT_EQ(66u, length_bytes);
1549 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1550 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1551 length_bytes));
1552 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1553 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001554
1555 // Check that the string value of the modified date is correct.
1556 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001557 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1558 nullptr, 0);
1559 ASSERT_EQ(44u, length_bytes);
1560 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001561 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001562 buf.data(), length_bytes));
1563 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001564
1565 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001566 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001567 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1568 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001569 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001570
Lei Zhangec618142021-04-13 17:36:46 +00001571 // Save the document and close the page.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001572 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001573 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001574
Hui Yingst9b6b1542020-07-27 16:11:12 +00001575#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001576#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001577 static const char kMd5[] = "7a2b712ca88d7b71f125ea3f9c88e57a";
1578#else
1579 static const char kMd5[] = "626d25c5aa5baf67d22d9a0e1c23f6aa";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001580#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001581#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001582#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001583 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001584#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001585 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001586#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001587 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001588#endif
Hui Yingst9b6b1542020-07-27 16:11:12 +00001589#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Dan Sinclair971a6742018-03-28 19:23:25 +00001590
1591 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001592 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001593 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001594 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001595 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001596 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001597 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001598
Lei Zhanga21d5932018-02-05 18:28:38 +00001599 // Check that the string value of the modified date is the newly-set value.
1600 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001601 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001602 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001603 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001604 ASSERT_EQ(44u, length_bytes);
1605 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001606 EXPECT_EQ(44u,
1607 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001608 buf.data(), length_bytes));
1609 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001610 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001611
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001612 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001613 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001614}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001615
rycsmith3e785602019-03-05 21:48:36 +00001616TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001617 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001618 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1619 FPDF_PAGE page = LoadPage(0);
1620 ASSERT_TRUE(page);
1621 {
1622 // First two annotations do not have "MaxLen" attribute.
1623 for (int i = 0; i < 2; i++) {
1624 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1625 ASSERT_TRUE(annot);
1626
1627 // Verify that no "MaxLen" key present.
1628 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1629
1630 float value;
1631 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1632 }
1633
1634 // Annotation in index 2 has "MaxLen" of 10.
1635 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1636 ASSERT_TRUE(annot);
1637
1638 // Verify that "MaxLen" key present.
1639 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1640
1641 float value;
1642 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1643 EXPECT_FLOAT_EQ(10.0f, value);
1644
1645 // Check bad inputs.
1646 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1647 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1648 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1649 // Ask for key that exists but is not a number.
1650 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1651 }
1652
1653 UnloadPage(page);
1654}
1655
Lei Zhangab41f252018-12-23 03:10:50 +00001656TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001657 // Open a file with four annotations and load its first page.
1658 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001659 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001660 ASSERT_TRUE(page);
1661
Lei Zhanga21d5932018-02-05 18:28:38 +00001662 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001663 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1664 static constexpr size_t kExpectNormalAPLength = 73970;
1665
Lei Zhanga21d5932018-02-05 18:28:38 +00001666 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001667 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001668 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001669
Lei Zhanga21d5932018-02-05 18:28:38 +00001670 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001671 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001672 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001673 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001674
Lei Zhanga21d5932018-02-05 18:28:38 +00001675 // Check that the string value of an AP is not returned if the buffer is too
1676 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001677 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1678 // Write in the buffer to verify it's not overwritten.
1679 memcpy(buf.data(), "abcdefgh", 8);
1680 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001681 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001682 buf.data(), normal_length_bytes - 1));
1683 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001684
Lei Zhanga21d5932018-02-05 18:28:38 +00001685 // Check that the string value of an AP is returned through a buffer that is
1686 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001687 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));
1690 EXPECT_EQ(kMd5NormalAP,
1691 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1692 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001693
Lei Zhanga21d5932018-02-05 18:28:38 +00001694 // Check that the string value of an AP is returned through a buffer that is
1695 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001696 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1697 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001698 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001699 buf.data(), normal_length_bytes + 2));
1700 EXPECT_EQ(kMd5NormalAP,
1701 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1702 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001703
Lei Zhanga21d5932018-02-05 18:28:38 +00001704 // Check that getting an AP for a mode that does not have an AP returns an
1705 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001706 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001707 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001708 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001709
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001710 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001711 EXPECT_EQ(2u,
1712 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001713 buf.data(), 1000));
1714 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001715
Lei Zhanga21d5932018-02-05 18:28:38 +00001716 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001717 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001718 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001719 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001720 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001721 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001722 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001723
Lei Zhanga21d5932018-02-05 18:28:38 +00001724 // Set the AP correctly now.
1725 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001726 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001727
Lei Zhanga21d5932018-02-05 18:28:38 +00001728 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001729 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001730 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001731 ASSERT_EQ(24u, rollover_length_bytes);
1732 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001733 EXPECT_EQ(24u,
1734 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001735 buf.data(), rollover_length_bytes));
1736 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001737
Lei Zhanga21d5932018-02-05 18:28:38 +00001738 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001739 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1740 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001741 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001742 buf.data(), normal_length_bytes));
1743 EXPECT_EQ(kMd5NormalAP,
1744 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1745 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001746 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001747
1748 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001749 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001750 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001751
Lei Zhang0b494052019-01-31 21:41:15 +00001752 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001753 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001754 ASSERT_TRUE(page);
Lei Zhanga21d5932018-02-05 18:28:38 +00001755 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001756 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001757
Lei Zhanga21d5932018-02-05 18:28:38 +00001758 // Check that the new annotation value is equal to the value we set before
1759 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001760 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001761 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001762 ASSERT_EQ(24u, rollover_length_bytes);
1763 std::vector<FPDF_WCHAR> buf =
1764 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001765 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1766 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001767 buf.data(), rollover_length_bytes));
1768 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001769 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001770
1771 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001772 CloseSavedPage(page);
1773 CloseSavedDocument();
1774}
1775
Lei Zhangab41f252018-12-23 03:10:50 +00001776TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001777 // Open a file with four annotations and load its first page.
1778 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001779 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001780 ASSERT_TRUE(page);
1781
Lei Zhanga21d5932018-02-05 18:28:38 +00001782 {
1783 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001784 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001785 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001786
Lei Zhanga21d5932018-02-05 18:28:38 +00001787 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001788 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001789 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001790 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001791 EXPECT_EQ(73970u,
1792 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1793 nullptr, 0));
1794 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1795 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001796
Lei Zhanga21d5932018-02-05 18:28:38 +00001797 // Check that setting the Down AP to null removes the Down entry but keeps
1798 // Normal intact.
1799 EXPECT_TRUE(
1800 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1801 EXPECT_EQ(73970u,
1802 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1803 nullptr, 0));
1804 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1805 nullptr, 0));
1806 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001807
Lei Zhang75c81712018-02-08 17:22:39 +00001808 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001809}
1810
Lei Zhangab41f252018-12-23 03:10:50 +00001811TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001812 // Open a file with four annotations and load its first page.
1813 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001814 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001815 ASSERT_TRUE(page);
1816
Lei Zhanga21d5932018-02-05 18:28:38 +00001817 {
1818 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001819 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001820 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001821
Lei Zhanga21d5932018-02-05 18:28:38 +00001822 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001823 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001824 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001825 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001826 EXPECT_EQ(73970u,
1827 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1828 nullptr, 0));
1829 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1830 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001831
Lei Zhanga21d5932018-02-05 18:28:38 +00001832 // Check that setting the Normal AP to null removes the whole AP dictionary.
1833 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1834 nullptr));
1835 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1836 nullptr, 0));
1837 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1838 nullptr, 0));
1839 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001840
Lei Zhang75c81712018-02-08 17:22:39 +00001841 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001842}
1843
Lei Zhangab41f252018-12-23 03:10:50 +00001844TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001845 // Open a file with annotations and load its first page.
1846 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001847 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001848 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001849 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001850
Lei Zhanga21d5932018-02-05 18:28:38 +00001851 {
1852 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001853 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001854 ASSERT_TRUE(annot);
1855 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1856 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001857 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001858 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1859 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1860 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001861
Lei Zhanga21d5932018-02-05 18:28:38 +00001862 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001863 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001864 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1865 ASSERT_TRUE(popup);
1866 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1867 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1868 FS_RECTF rect;
1869 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1870 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1871 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001872
Lei Zhanga21d5932018-02-05 18:28:38 +00001873 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1874 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001875 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001876 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1877 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001878
Lei Zhanga21d5932018-02-05 18:28:38 +00001879 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1880 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001881 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001882 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001883 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1884 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001885 }
Jane Liu300bb272017-08-21 14:37:53 -04001886
Jane Liu300bb272017-08-21 14:37:53 -04001887 UnloadPage(page);
1888}
1889
Lei Zhangab41f252018-12-23 03:10:50 +00001890TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001891 // Open file with form text fields.
1892 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001893 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001894 ASSERT_TRUE(page);
1895
Lei Zhanga21d5932018-02-05 18:28:38 +00001896 {
1897 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001898 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001899 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001900
Lei Zhanga21d5932018-02-05 18:28:38 +00001901 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001902 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001903 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001904 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001905 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001906
Lei Zhanga21d5932018-02-05 18:28:38 +00001907 {
1908 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001909 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001910 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001911
Lei Zhanga21d5932018-02-05 18:28:38 +00001912 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001913 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001914 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001915 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1916 }
1917
1918 {
1919 // Retrieve the fourth annotation: user-editable password text field.
1920 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1921 ASSERT_TRUE(annot);
1922
1923 // Check that the flag values are as expected.
1924 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1925 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1926 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001927 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001928
1929 UnloadPage(page);
1930}
1931
Lei Zhangab41f252018-12-23 03:10:50 +00001932TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001933 // Open file with form text fields.
1934 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001935 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001936 ASSERT_TRUE(page);
1937
Lei Zhanga21d5932018-02-05 18:28:38 +00001938 {
1939 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001940 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001941 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001942
Lei Zhanga21d5932018-02-05 18:28:38 +00001943 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001944 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001945 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1946 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1947 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1948 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001949
Lei Zhanga21d5932018-02-05 18:28:38 +00001950 {
1951 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001952 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001953 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001954
Lei Zhanga21d5932018-02-05 18:28:38 +00001955 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001956 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001957 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1958 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1959 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1960 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001961
Lei Zhanga21d5932018-02-05 18:28:38 +00001962 {
1963 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001964 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001965 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001966
Lei Zhanga21d5932018-02-05 18:28:38 +00001967 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001968 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001969 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1970 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1971 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1972 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001973
1974 UnloadPage(page);
1975}
Diana Gage40870db2017-07-19 18:16:03 -07001976
Lei Zhangab41f252018-12-23 03:10:50 +00001977TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001978 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001979 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001980 FPDF_PAGE page = LoadPage(0);
1981 ASSERT_TRUE(page);
1982
1983 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001984 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1985 EXPECT_FALSE(
1986 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001987
Lei Zhang8da98232019-12-11 23:29:33 +00001988 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001989 {
1990 // Verify there is an annotation.
1991 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001992 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001993 EXPECT_TRUE(annot);
1994 }
1995
1996 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001997 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1998 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1999 EXPECT_FALSE(
2000 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07002001
2002 UnloadPage(page);
2003}
2004
Lei Zhangab41f252018-12-23 03:10:50 +00002005TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07002006 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002007 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002008 FPDF_PAGE page = LoadPage(0);
2009 ASSERT_TRUE(page);
2010
Lei Zhanga21d5932018-02-05 18:28:38 +00002011 {
2012 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002013 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002014 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002015 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002016 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002017
Lei Zhanga21d5932018-02-05 18:28:38 +00002018 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002019 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002020 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2021 }
Diana Gage40870db2017-07-19 18:16:03 -07002022
Lei Zhanga21d5932018-02-05 18:28:38 +00002023 {
2024 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002025 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002026 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002027 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002028 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002029
Lei Zhanga21d5932018-02-05 18:28:38 +00002030 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002031 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002032 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
2033 }
Diana Gage40870db2017-07-19 18:16:03 -07002034
2035 UnloadPage(page);
2036}
2037
Lei Zhangab41f252018-12-23 03:10:50 +00002038TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07002039 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00002040 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07002041 FPDF_PAGE page = LoadPage(0);
2042 ASSERT_TRUE(page);
2043
Lei Zhanga21d5932018-02-05 18:28:38 +00002044 {
2045 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002046 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002047 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002048 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002049 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002050
Lei Zhanga21d5932018-02-05 18:28:38 +00002051 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002052 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002053 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2054 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2055 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
2056 }
Diana Gage40870db2017-07-19 18:16:03 -07002057
Lei Zhanga21d5932018-02-05 18:28:38 +00002058 {
2059 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002060 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002061 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002062 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002063 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002064
Lei Zhanga21d5932018-02-05 18:28:38 +00002065 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002066 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002067 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2068 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2069 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
2070 }
Diana Gage40870db2017-07-19 18:16:03 -07002071
Lei Zhanga21d5932018-02-05 18:28:38 +00002072 {
2073 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00002074 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00002075 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00002076 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00002077 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002078
Lei Zhanga21d5932018-02-05 18:28:38 +00002079 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002080 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002081 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
2082 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2083 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
2084 }
Diana Gage40870db2017-07-19 18:16:03 -07002085
2086 UnloadPage(page);
2087}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002088
Hui Yingst603dcd82020-11-09 22:36:46 +00002089TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
Lei Zhang03e5e682019-09-16 19:45:55 +00002090#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst603dcd82020-11-09 22:36:46 +00002091 static const char kExpectedBitmap[] = "a1ea1ceebb26922fae576cb79ce63af0";
Lei Zhang03e5e682019-09-16 19:45:55 +00002092#else
Lei Zhang992e7e22019-02-04 19:20:58 +00002093 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
Hui Yingst603dcd82020-11-09 22:36:46 +00002094#endif
2095 static constexpr size_t kExpectedSize = 1609;
Lei Zhang992e7e22019-02-04 19:20:58 +00002096
2097 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2098
2099 FPDF_PAGE page = LoadPage(0);
2100 ASSERT_TRUE(page);
2101
2102 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2103 EXPECT_EQ(kExpectedSize, GetString().size());
2104 ClearString();
2105
2106 for (size_t i = 0; i < 10; ++i) {
2107 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2108 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2109
2110 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2111 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2112 // equal, not bigger.
2113 EXPECT_LT(kExpectedSize, GetString().size());
2114 ClearString();
2115 }
2116
2117 UnloadPage(page);
2118}
2119
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002120TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2121 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2122 FPDF_PAGE page = LoadPage(0);
2123 ASSERT_TRUE(page);
2124 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2125
2126 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002127 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002128 static const size_t kBufSize = 12;
2129 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002130
2131 {
2132 // Add a text annotation to the page.
2133 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2134 ASSERT_TRUE(annot);
2135 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2136 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2137
2138 // Make sure there is no test key, add set a value there, and read it back.
2139 std::fill(buf.begin(), buf.end(), 'x');
2140 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002141 kBufSize));
2142 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002143
Lei Zhangf0f67682019-04-08 17:03:21 +00002144 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002145 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2146
2147 std::fill(buf.begin(), buf.end(), 'x');
2148 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002149 kBufSize));
2150 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002151 }
2152
Lei Zhang05ec64c2019-01-09 03:00:06 +00002153 {
2154 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2155 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002156 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002157 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002158 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2159 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2160 // Also do the same test for its appearance string.
2161 std::fill(buf.begin(), buf.end(), 'x');
2162 ASSERT_EQ(2u,
2163 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002164 buf.data(), kBufSize));
2165 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002166
Lei Zhangf0f67682019-04-08 17:03:21 +00002167 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002168 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2169 text.get()));
2170
2171 std::fill(buf.begin(), buf.end(), 'x');
2172 ASSERT_EQ(6u,
2173 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002174 buf.data(), kBufSize));
2175 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002176 }
2177
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002178 UnloadPage(page);
2179
2180 {
2181 // Save a copy, open the copy, and check the annotation again.
2182 // Note that it renders the rotation.
2183 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002184 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002185 FPDF_PAGE saved_page = LoadSavedPage(0);
2186 ASSERT_TRUE(saved_page);
2187
Lei Zhang05ec64c2019-01-09 03:00:06 +00002188 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002189 {
2190 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2191 ASSERT_TRUE(annot);
2192 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2193
2194 std::fill(buf.begin(), buf.end(), 'x');
2195 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002196 kBufSize));
2197 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002198 }
2199
Lei Zhang05ec64c2019-01-09 03:00:06 +00002200 {
2201 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2202 ASSERT_TRUE(annot);
2203 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2204 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2205
2206 std::fill(buf.begin(), buf.end(), 'x');
2207 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002208 kBufSize));
2209 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002210 }
2211
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002212 CloseSavedPage(saved_page);
2213 CloseSavedDocument();
2214 }
2215}
rycsmithcb752f32019-02-21 18:40:53 +00002216
2217TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2218 // Open a file with combobox widget annotations and load its first page.
2219 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2220 FPDF_PAGE page = LoadPage(0);
2221 ASSERT_TRUE(page);
2222
2223 {
2224 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2225 ASSERT_TRUE(annot);
2226
2227 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2228
2229 annot.reset(FPDFPage_GetAnnot(page, 1));
2230 ASSERT_TRUE(annot);
2231
2232 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002233
2234 // Check bad form handle / annot.
2235 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2236 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2237 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002238 }
2239
2240 UnloadPage(page);
2241}
2242
2243TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2244 // Open a file with listbox widget annotations and load its first page.
2245 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2246 FPDF_PAGE page = LoadPage(0);
2247 ASSERT_TRUE(page);
2248
2249 {
2250 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2251 ASSERT_TRUE(annot);
2252
2253 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2254
2255 annot.reset(FPDFPage_GetAnnot(page, 1));
2256 ASSERT_TRUE(annot);
2257
2258 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2259 }
2260
2261 UnloadPage(page);
2262}
2263
2264TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2265 // Open a file with ink annotations and load its first page.
2266 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2267 FPDF_PAGE page = LoadPage(0);
2268 ASSERT_TRUE(page);
2269
2270 {
2271 // annotations do not have "Opt" array and will return -1
2272 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2273 ASSERT_TRUE(annot);
2274
2275 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2276
2277 annot.reset(FPDFPage_GetAnnot(page, 1));
2278 ASSERT_TRUE(annot);
2279
2280 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2281 }
2282
2283 UnloadPage(page);
2284}
2285
2286TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2287 // Open a file with combobox widget annotations and load its first page.
2288 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2289 FPDF_PAGE page = LoadPage(0);
2290 ASSERT_TRUE(page);
2291
2292 {
2293 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2294 ASSERT_TRUE(annot);
2295
2296 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002297 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002298 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002299 ASSERT_EQ(8u, length_bytes);
2300 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002301 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002302 buf.data(), length_bytes));
2303 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002304
2305 annot.reset(FPDFPage_GetAnnot(page, 1));
2306 ASSERT_TRUE(annot);
2307
2308 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002309 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002310 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002311 ASSERT_EQ(12u, length_bytes);
2312 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002313 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002314 buf.data(), length_bytes));
2315 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002316
2317 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002318 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002319 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002320 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002321 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002322 buf.data(), length_bytes));
2323 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002324
Lei Zhange7033c82019-02-26 19:30:49 +00002325 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002326 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2327 nullptr, 0));
2328 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2329 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002330
2331 // Check bad form handle / annot.
2332 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2333 EXPECT_EQ(0u,
2334 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2335 EXPECT_EQ(0u,
2336 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002337 }
2338
2339 UnloadPage(page);
2340}
2341
2342TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2343 // Open a file with listbox widget annotations and load its first page.
2344 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2345 FPDF_PAGE page = LoadPage(0);
2346 ASSERT_TRUE(page);
2347
2348 {
2349 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2350 ASSERT_TRUE(annot);
2351
2352 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002353 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002354 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002355 ASSERT_EQ(8u, length_bytes);
2356 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002357 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002358 buf.data(), length_bytes));
2359 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002360
2361 annot.reset(FPDFPage_GetAnnot(page, 1));
2362 ASSERT_TRUE(annot);
2363
2364 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002365 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002366 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002367 ASSERT_EQ(12u, length_bytes);
2368 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002369 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002370 buf.data(), length_bytes));
2371 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002372
2373 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002374 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002375 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002376 ASSERT_EQ(18u, length_bytes);
2377 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002378 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002379 buf.data(), length_bytes));
2380 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002381
2382 // indices out of range
2383 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2384 nullptr, 0));
2385 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2386 nullptr, 0));
2387 }
2388
2389 UnloadPage(page);
2390}
2391
2392TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2393 // Open a file with ink annotations and load its first page.
2394 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2395 FPDF_PAGE page = LoadPage(0);
2396 ASSERT_TRUE(page);
2397
2398 {
2399 // annotations do not have "Opt" array and will return 0
2400 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2401 ASSERT_TRUE(annot);
2402
2403 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2404 nullptr, 0));
2405
2406 annot.reset(FPDFPage_GetAnnot(page, 1));
2407 ASSERT_TRUE(annot);
2408
2409 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2410 nullptr, 0));
2411 }
2412
2413 UnloadPage(page);
2414}
Ryan Smith09c23b12019-04-25 18:09:06 +00002415
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002416TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2417 // Open a file with combobox widget annotations and load its first page.
2418 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2419 FPDF_PAGE page = LoadPage(0);
2420 ASSERT_TRUE(page);
2421
2422 {
2423 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2424 ASSERT_TRUE(annot);
2425
2426 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2427 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2428 ASSERT_EQ(3, count);
2429 for (int i = 0; i < count; i++) {
2430 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2431 }
2432
2433 annot.reset(FPDFPage_GetAnnot(page, 1));
2434 ASSERT_TRUE(annot);
2435
2436 // Checks for Combobox with Values (/V) object which is just a string.
2437 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2438 ASSERT_EQ(26, count);
2439 for (int i = 0; i < count; i++) {
2440 EXPECT_EQ(i == 1,
2441 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2442 }
2443
2444 // Checks for index outside bound i.e. (index >= CountOption()).
2445 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2446 /*index=*/26));
2447 // Checks for negetive index.
2448 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2449 /*index=*/-1));
2450
2451 // Checks for bad form handle/annot.
2452 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2453 EXPECT_FALSE(
2454 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2455 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2456 }
2457
2458 UnloadPage(page);
2459}
2460
2461TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2462 // Open a file with listbox widget annotations and load its first page.
2463 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2464 FPDF_PAGE page = LoadPage(0);
2465 ASSERT_TRUE(page);
2466
2467 {
2468 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2469 ASSERT_TRUE(annot);
2470
2471 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2472 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2473 ASSERT_EQ(3, count);
2474 for (int i = 0; i < count; i++) {
2475 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2476 }
2477
2478 annot.reset(FPDFPage_GetAnnot(page, 1));
2479 ASSERT_TRUE(annot);
2480
2481 // Checks for Listbox with Values (/V) object which is just a string.
2482 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2483 ASSERT_EQ(26, count);
2484 for (int i = 0; i < count; i++) {
2485 EXPECT_EQ(i == 1,
2486 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2487 }
2488
2489 annot.reset(FPDFPage_GetAnnot(page, 3));
2490 ASSERT_TRUE(annot);
2491
2492 // Checks for Listbox with only Selected indices (/I) object which is an
2493 // array with multiple objects.
2494 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2495 ASSERT_EQ(5, count);
2496 for (int i = 0; i < count; i++) {
2497 bool expected = (i == 1 || i == 3);
2498 EXPECT_EQ(expected,
2499 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2500 }
2501
2502 annot.reset(FPDFPage_GetAnnot(page, 4));
2503 ASSERT_TRUE(annot);
2504
2505 // Checks for Listbox with Values (/V) object which is an array with
2506 // multiple objects.
2507 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2508 ASSERT_EQ(5, count);
2509 for (int i = 0; i < count; i++) {
2510 bool expected = (i == 2 || i == 4);
2511 EXPECT_EQ(expected,
2512 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2513 }
2514
2515 annot.reset(FPDFPage_GetAnnot(page, 5));
2516 ASSERT_TRUE(annot);
2517
2518 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2519 // objects conflict with different lengths.
2520 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2521 ASSERT_EQ(5, count);
2522 for (int i = 0; i < count; i++) {
2523 bool expected = (i == 0 || i == 2);
2524 EXPECT_EQ(expected,
2525 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2526 }
2527 }
2528
2529 UnloadPage(page);
2530}
2531
2532TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2533 // Open a file with multiple form field annotations and load its first page.
2534 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2535 FPDF_PAGE page = LoadPage(0);
2536 ASSERT_TRUE(page);
2537
2538 {
2539 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2540 ASSERT_TRUE(annot);
2541
2542 // Checks for link annotation.
2543 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2544 /*index=*/0));
2545
2546 annot.reset(FPDFPage_GetAnnot(page, 3));
2547 ASSERT_TRUE(annot);
2548
2549 // Checks for text field annotation.
2550 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2551 /*index=*/0));
2552 }
2553
2554 UnloadPage(page);
2555}
2556
Ryan Smith09c23b12019-04-25 18:09:06 +00002557TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2558 // Open a file with combobox annotations and load its first page.
2559 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2560 FPDF_PAGE page = LoadPage(0);
2561 ASSERT_TRUE(page);
2562
2563 {
2564 // All 3 widgets have Tf font size 12.
2565 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2566 ASSERT_TRUE(annot);
2567
2568 float value;
2569 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2570 EXPECT_EQ(12.0, value);
2571
2572 annot.reset(FPDFPage_GetAnnot(page, 1));
2573 ASSERT_TRUE(annot);
2574
2575 float value_two;
2576 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2577 EXPECT_EQ(12.0, value_two);
2578
2579 annot.reset(FPDFPage_GetAnnot(page, 2));
2580 ASSERT_TRUE(annot);
2581
2582 float value_three;
2583 ASSERT_TRUE(
2584 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2585 EXPECT_EQ(12.0, value_three);
2586 }
2587
2588 UnloadPage(page);
2589}
2590
2591TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2592 // Open a file with textfield annotations and load its first page.
2593 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2594 FPDF_PAGE page = LoadPage(0);
2595 ASSERT_TRUE(page);
2596
2597 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002598 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002599 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2600 ASSERT_TRUE(annot);
2601
2602 float value;
2603 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2604 EXPECT_EQ(12.0, value);
2605
2606 annot.reset(FPDFPage_GetAnnot(page, 1));
2607 ASSERT_TRUE(annot);
2608
2609 float value_two;
2610 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2611 EXPECT_EQ(12.0, value_two);
2612
2613 annot.reset(FPDFPage_GetAnnot(page, 2));
2614 ASSERT_TRUE(annot);
2615
2616 float value_three;
2617 ASSERT_TRUE(
2618 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2619 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002620
2621 float value_four;
2622 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2623 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002624 }
2625
2626 UnloadPage(page);
2627}
2628
2629TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2630 // Open a file with ink annotations and load its first page.
2631 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2632 FPDF_PAGE page = LoadPage(0);
2633 ASSERT_TRUE(page);
2634
2635 {
2636 // Annotations that do not have variable text and will return -1.
2637 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2638 ASSERT_TRUE(annot);
2639
2640 float value;
2641 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2642
2643 annot.reset(FPDFPage_GetAnnot(page, 1));
2644 ASSERT_TRUE(annot);
2645
2646 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2647 }
2648
2649 UnloadPage(page);
2650}
2651
2652TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2653 // Open a file with combobox annotations and load its first page.
2654 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2655 FPDF_PAGE page = LoadPage(0);
2656 ASSERT_TRUE(page);
2657
2658 {
2659 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2660 ASSERT_TRUE(annot);
2661
2662 // Check bad form handle / annot.
2663 float value;
2664 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2665 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2666 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2667 }
2668
2669 UnloadPage(page);
2670}
2671
2672TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2673 // Open a file with textfield annotations and load its first page.
2674 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2675 FPDF_PAGE page = LoadPage(0);
2676 ASSERT_TRUE(page);
2677
2678 {
2679 // Obtain the first annotation, a text field with negative font size, -12.
2680 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2681 ASSERT_TRUE(annot);
2682
2683 float value;
2684 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2685 EXPECT_EQ(-12.0, value);
2686 }
2687
2688 UnloadPage(page);
2689}
Ryan Smith23fdf892019-07-17 21:51:26 +00002690
2691TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2692 // Open a file with checkbox and radiobuttons widget annotations and load its
2693 // first page.
2694 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2695 FPDF_PAGE page = LoadPage(0);
2696 ASSERT_TRUE(page);
2697
2698 {
2699 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2700 ASSERT_TRUE(annot);
2701 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2702 }
2703
2704 UnloadPage(page);
2705}
2706
2707TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2708 // Open a file with checkbox and radiobutton widget annotations and load its
2709 // first page.
2710 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2711 FPDF_PAGE page = LoadPage(0);
2712 ASSERT_TRUE(page);
2713
2714 {
2715 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2716 ASSERT_TRUE(annot);
2717 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2718 }
2719
2720 UnloadPage(page);
2721}
2722
2723TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2724 // Open a file with checkbox and radiobutton widget annotations and load its
2725 // first page.
2726 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2727 FPDF_PAGE page = LoadPage(0);
2728 ASSERT_TRUE(page);
2729
2730 {
2731 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2732 ASSERT_TRUE(annot);
2733 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2734
2735 annot.reset(FPDFPage_GetAnnot(page, 6));
2736 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2737
2738 annot.reset(FPDFPage_GetAnnot(page, 7));
2739 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2740 }
2741
2742 UnloadPage(page);
2743}
2744
2745TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2746 // Open a file with checkbox and radiobutton widget annotations and load its
2747 // first page.
2748 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2749 FPDF_PAGE page = LoadPage(0);
2750 ASSERT_TRUE(page);
2751
2752 {
2753 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2754 ASSERT_TRUE(annot);
2755 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2756
2757 annot.reset(FPDFPage_GetAnnot(page, 3));
2758 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2759
2760 annot.reset(FPDFPage_GetAnnot(page, 4));
2761 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2762 }
2763
2764 UnloadPage(page);
2765}
2766
2767TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2768 // Open a file with checkbox and radiobuttons widget annotations and load its
2769 // first page.
2770 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2771 FPDF_PAGE page = LoadPage(0);
2772 ASSERT_TRUE(page);
2773
2774 {
2775 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2776 ASSERT_TRUE(annot);
2777 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2778 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2779 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2780 }
2781
2782 UnloadPage(page);
2783}
2784
2785TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2786 // Open a file with text widget annotations and load its first page.
2787 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2788 FPDF_PAGE page = LoadPage(0);
2789 ASSERT_TRUE(page);
2790
2791 {
2792 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2793 ASSERT_TRUE(annot);
2794 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2795 }
2796
2797 UnloadPage(page);
2798}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002799
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002800TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2801 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002802 FPDF_PAGE page = LoadPage(0);
2803 ASSERT_TRUE(page);
2804
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002805 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2806
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002807 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002808 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002809 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002810 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002811 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002812
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002813 constexpr int kExpectedAnnotTypes[] = {-1,
2814 FPDF_FORMFIELD_COMBOBOX,
2815 FPDF_FORMFIELD_LISTBOX,
2816 FPDF_FORMFIELD_TEXTFIELD,
2817 FPDF_FORMFIELD_CHECKBOX,
2818 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002819
Lei Zhang4501a502020-05-18 16:52:59 +00002820 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002821 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002822 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002823 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002824 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2825 }
2826 UnloadPage(page);
2827}
2828
2829TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2830 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2831 FPDF_PAGE page = LoadPage(0);
2832 ASSERT_TRUE(page);
2833
2834 {
2835 EXPECT_EQ(0u,
2836 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2837
2838 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2839 ASSERT_TRUE(annot);
2840
2841 EXPECT_EQ(0u,
2842 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2843
2844 unsigned long length_bytes =
2845 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2846 ASSERT_EQ(2u, length_bytes);
2847 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2848 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2849 buf.data(), length_bytes));
2850 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2851 }
2852 {
2853 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2854 ASSERT_TRUE(annot);
2855
2856 unsigned long length_bytes =
2857 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2858 ASSERT_EQ(18u, length_bytes);
2859 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2860 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2861 buf.data(), length_bytes));
2862 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2863 }
2864 UnloadPage(page);
2865}
2866
2867TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2868 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2869 FPDF_PAGE page = LoadPage(0);
2870 ASSERT_TRUE(page);
2871
2872 {
2873 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2874 ASSERT_TRUE(annot);
2875
2876 unsigned long length_bytes =
2877 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2878 ASSERT_EQ(2u, length_bytes);
2879 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2880 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2881 buf.data(), length_bytes));
2882 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2883 }
2884 {
2885 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2886 ASSERT_TRUE(annot);
2887
2888 unsigned long length_bytes =
2889 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2890 ASSERT_EQ(14u, length_bytes);
2891 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2892 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2893 buf.data(), length_bytes));
2894 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2895 }
2896 UnloadPage(page);
2897}
2898
2899TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2900 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2901 FPDF_PAGE page = LoadPage(0);
2902 ASSERT_TRUE(page);
2903
2904 {
2905 EXPECT_EQ(0u,
2906 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2907
2908 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2909 ASSERT_TRUE(annot);
2910
2911 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2912
2913 unsigned long length_bytes =
2914 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2915 ASSERT_EQ(18u, length_bytes);
2916 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2917 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2918 buf.data(), length_bytes));
2919 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2920 }
2921 UnloadPage(page);
2922}
2923
2924TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2925 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2926 FPDF_PAGE page = LoadPage(0);
2927 ASSERT_TRUE(page);
2928
2929 {
2930 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2931 ASSERT_TRUE(annot);
2932
2933 unsigned long length_bytes =
2934 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2935 ASSERT_EQ(30u, length_bytes);
2936 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2937 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2938 buf.data(), length_bytes));
2939 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2940 }
2941 UnloadPage(page);
2942}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002943
Lei Zhang9b444002020-04-17 17:35:23 +00002944TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2945 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002946 FPDF_PAGE page = LoadPage(0);
2947 ASSERT_TRUE(page);
2948
Lei Zhang9b444002020-04-17 17:35:23 +00002949 // Verify widgets are by default focusable.
2950 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2951 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002952
Lei Zhang9b444002020-04-17 17:35:23 +00002953 // Expected annot subtypes for page 0 of annots.pdf.
2954 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2955 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2956 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2957 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2958 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002959
Lei Zhang9b444002020-04-17 17:35:23 +00002960 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002961 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002962 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2963 kExpectedAnnotSubtypes,
2964 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002965
Lei Zhang9b444002020-04-17 17:35:23 +00002966 // Make no annotation type focusable using the preferred method.
2967 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2968 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002969
Lei Zhang9b444002020-04-17 17:35:23 +00002970 // Restore the focusable type count back to 1, then set it back to 0 using a
2971 // different method.
2972 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2973 ASSERT_TRUE(
2974 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2975 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002976
Lei Zhang9b444002020-04-17 17:35:23 +00002977 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002978 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002979
Lei Zhang9b444002020-04-17 17:35:23 +00002980 // Now make links focusable.
2981 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2982 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002983
Lei Zhang9b444002020-04-17 17:35:23 +00002984 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002985 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002986 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2987 kExpectedAnnotSubtypes,
2988 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002989
Lei Zhang9b444002020-04-17 17:35:23 +00002990 // Test invalid parameters.
2991 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00002992 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002993 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00002994 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002995 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2996
Lei Zhang9b444002020-04-17 17:35:23 +00002997 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2998 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2999 subtypes.size()));
3000 EXPECT_FALSE(
3001 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
3002 EXPECT_FALSE(
3003 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00003004
3005 UnloadPage(page);
3006}
Lei Zhang671aece2020-04-14 19:02:26 +00003007
Hui Yingstea3816d2020-07-28 22:35:11 +00003008TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00003009 ASSERT_TRUE(OpenDocument("annots.pdf"));
3010 FPDF_PAGE page = LoadPage(0);
3011 ASSERT_TRUE(page);
3012
3013 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003014#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3015 static const char kMd5sum[] = "4ca14c670396711194b40ecc2514969b";
3016#else
Lei Zhang671aece2020-04-14 19:02:26 +00003017#if defined(OS_WIN)
3018 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
Lei Zhang0c03d632020-07-30 17:05:36 +00003019#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003020 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
3021#else
3022 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
3023#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003024#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003025 // Check the initial rendering.
3026 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3027 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3028 }
3029
3030 // Make links and highlights focusable.
3031 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
3032 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00003033 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00003034 ASSERT_TRUE(
3035 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
3036 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3037 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
3038 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
3039 subtypes.size()));
3040 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
3041 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
3042
3043 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003044#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3045 static const char kMd5sum[] = "48cb60b3f9bc364c73582aff3418451e";
3046#else
Lei Zhang671aece2020-04-14 19:02:26 +00003047#if defined(OS_WIN)
3048 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
Lei Zhang0c03d632020-07-30 17:05:36 +00003049#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003050 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
3051#else
3052 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
3053#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003054#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003055 // Focus the first link and check the rendering.
3056 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3057 ASSERT_TRUE(annot);
3058 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3059 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3060 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3061 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3062 }
3063
3064 {
Hui Yingstea3816d2020-07-28 22:35:11 +00003065#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
3066 static const char kMd5sum[] = "d09869ff0a209daf179da2d7a58142b1";
3067#else
Lei Zhang671aece2020-04-14 19:02:26 +00003068#if defined(OS_WIN)
3069 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
Lei Zhang0c03d632020-07-30 17:05:36 +00003070#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00003071 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
3072#else
3073 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
3074#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00003075#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00003076 // Focus the first highlight and check the rendering.
3077 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3078 ASSERT_TRUE(annot);
3079 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3080 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3081 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3082 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3083 }
3084
3085 UnloadPage(page);
3086}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003087
3088TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
3089 ASSERT_TRUE(OpenDocument("annots.pdf"));
3090 FPDF_PAGE page = LoadPage(0);
3091 ASSERT_TRUE(page);
3092 {
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003093 constexpr char kExpectedResult[] =
3094 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3095 "fpdf_text.h";
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003096
Lei Zhang306874b2021-04-16 00:33:36 +00003097 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3098 ASSERT_TRUE(annot);
3099 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3100 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()),
3101 kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003102 }
3103
3104 {
3105 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3106 ASSERT_TRUE(annot);
3107 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3108 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3109 }
3110
3111 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3112
3113 UnloadPage(page);
3114}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003115
3116TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3117 // Open a file with radio button widget annotations and load its first page.
3118 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3119 FPDF_PAGE page = LoadPage(0);
3120 ASSERT_TRUE(page);
3121
3122 {
3123 // Checks for bad annot.
3124 EXPECT_EQ(-1,
3125 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3126
3127 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3128 ASSERT_TRUE(annot);
3129
3130 // Checks for bad form handle.
3131 EXPECT_EQ(-1,
3132 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3133
3134 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3135 }
3136
3137 UnloadPage(page);
3138}
3139
3140TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3141 // Open a file with checkbox widget annotations and load its first page.
3142 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3143 FPDF_PAGE page = LoadPage(0);
3144 ASSERT_TRUE(page);
3145
3146 {
3147 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3148 ASSERT_TRUE(annot);
3149 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3150 }
3151
3152 UnloadPage(page);
3153}
3154
3155TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3156 // Open a file with ink annotations and load its first page.
3157 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3158 FPDF_PAGE page = LoadPage(0);
3159 ASSERT_TRUE(page);
3160
3161 {
3162 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3163 ASSERT_TRUE(annot);
3164 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3165 }
3166
3167 UnloadPage(page);
3168}
3169
3170TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3171 // Open a file with radio button widget annotations and load its first page.
3172 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3173 FPDF_PAGE page = LoadPage(0);
3174 ASSERT_TRUE(page);
3175
3176 {
3177 // Checks for bad annot.
3178 EXPECT_EQ(-1,
3179 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3180
3181 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3182 ASSERT_TRUE(annot);
3183
3184 // Checks for bad form handle.
3185 EXPECT_EQ(-1,
3186 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3187
3188 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3189 }
3190
3191 UnloadPage(page);
3192}
3193
3194TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3195 // Open a file with checkbox widget annotations and load its first page.
3196 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3197 FPDF_PAGE page = LoadPage(0);
3198 ASSERT_TRUE(page);
3199
3200 {
3201 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3202 ASSERT_TRUE(annot);
3203 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3204 }
3205
3206 UnloadPage(page);
3207}
3208
3209TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3210 // Open a file with ink annotations and load its first page.
3211 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3212 FPDF_PAGE page = LoadPage(0);
3213 ASSERT_TRUE(page);
3214
3215 {
3216 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3217 ASSERT_TRUE(annot);
3218 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3219 }
3220
3221 UnloadPage(page);
3222}
3223
3224TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3225 // Open a file with radio button widget annotations and load its first page.
3226 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3227 FPDF_PAGE page = LoadPage(0);
3228 ASSERT_TRUE(page);
3229
3230 {
3231 // Checks for bad annot.
3232 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3233 form_handle(), /*annot=*/nullptr,
3234 /*buffer=*/nullptr, /*buflen=*/0));
3235
3236 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3237 ASSERT_TRUE(annot);
3238
3239 // Checks for bad form handle.
3240 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3241 /*hHandle=*/nullptr, annot.get(),
3242 /*buffer=*/nullptr, /*buflen=*/0));
3243
3244 unsigned long length_bytes =
3245 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3246 /*buffer=*/nullptr, /*buflen=*/0);
3247 ASSERT_EQ(14u, length_bytes);
3248 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3249 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3250 buf.data(), length_bytes));
3251 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3252 }
3253
3254 UnloadPage(page);
3255}
3256
3257TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3258 // Open a file with checkbox widget annotations and load its first page.
3259 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3260 FPDF_PAGE page = LoadPage(0);
3261 ASSERT_TRUE(page);
3262
3263 {
3264 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3265 ASSERT_TRUE(annot);
3266
3267 unsigned long length_bytes =
3268 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3269 /*buffer=*/nullptr, /*buflen=*/0);
3270 ASSERT_EQ(8u, length_bytes);
3271 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3272 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3273 buf.data(), length_bytes));
3274 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3275 }
3276
3277 UnloadPage(page);
3278}
3279
3280TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3281 // Open a file with ink annotations and load its first page.
3282 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3283 FPDF_PAGE page = LoadPage(0);
3284 ASSERT_TRUE(page);
3285
3286 {
3287 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3288 ASSERT_TRUE(annot);
3289 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3290 /*buffer=*/nullptr,
3291 /*buflen=*/0));
3292 }
3293
3294 UnloadPage(page);
3295}
Miklos Vajnad1a24fb2020-10-26 18:07:13 +00003296
3297TEST_F(FPDFAnnotEmbedderTest, Redactannotation) {
3298 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3299 FPDF_PAGE page = LoadPage(0);
3300 ASSERT_TRUE(page);
3301 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3302
3303 {
3304 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3305 ASSERT_TRUE(annot);
3306 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3307 }
3308
3309 UnloadPage(page);
3310}
Miklos Vajna09ecef62020-11-10 21:50:38 +00003311
3312TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) {
3313 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3314 FPDF_PAGE page = LoadPage(0);
3315 ASSERT_TRUE(page);
3316 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3317
3318 {
3319 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3320 ASSERT_TRUE(annot);
3321
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003322 // FPDFAnnot_GetVertices() positive testing.
Miklos Vajna09ecef62020-11-10 21:50:38 +00003323 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3324 const size_t kExpectedSize = 3;
3325 ASSERT_EQ(kExpectedSize, size);
3326 std::vector<FS_POINTF> vertices_buffer(size);
3327 EXPECT_EQ(size,
3328 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3329 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3330 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3331 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3332 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3333 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3334 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3335
3336 // FPDFAnnot_GetVertices() negative testing.
3337 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3338
3339 // vertices_buffer is not overwritten if it is too small.
3340 vertices_buffer.resize(1);
3341 vertices_buffer[0].x = 42;
3342 vertices_buffer[0].y = 43;
3343 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3344 vertices_buffer.size());
3345 EXPECT_EQ(kExpectedSize, size);
3346 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3347 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3348 }
3349
3350 {
3351 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3352 ASSERT_TRUE(annot);
3353
3354 // This has an odd number of elements in the vertices array, ignore the last
3355 // element.
3356 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3357 const size_t kExpectedSize = 3;
3358 ASSERT_EQ(kExpectedSize, size);
3359 std::vector<FS_POINTF> vertices_buffer(size);
3360 EXPECT_EQ(size,
3361 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3362 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3363 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3364 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3365 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3366 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3367 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3368 }
3369
3370 {
3371 // Wrong annotation type.
3372 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3373 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3374 }
3375
3376 UnloadPage(page);
3377}
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003378
3379TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) {
3380 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3381 FPDF_PAGE page = LoadPage(0);
3382 ASSERT_TRUE(page);
3383 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3384
3385 {
3386 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3387 ASSERT_TRUE(annot);
3388
3389 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3390 // testing.
3391 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3392 const size_t kExpectedSize = 1;
3393 ASSERT_EQ(kExpectedSize, size);
3394 const unsigned long kPathIndex = 0;
3395 unsigned long path_size =
3396 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3397 const size_t kExpectedPathSize = 3;
3398 ASSERT_EQ(kExpectedPathSize, path_size);
3399 std::vector<FS_POINTF> path_buffer(path_size);
3400 EXPECT_EQ(path_size,
3401 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3402 path_buffer.data(), path_size));
3403 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3404 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3405 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3406 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3407 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3408 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3409
3410 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3411 // testing.
3412 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3413 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3414
3415 // out of bounds path_index.
3416 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3417
3418 // path_buffer is not overwritten if it is too small.
3419 path_buffer.resize(1);
3420 path_buffer[0].x = 42;
3421 path_buffer[0].y = 43;
3422 path_size = FPDFAnnot_GetInkListPath(
3423 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3424 EXPECT_EQ(kExpectedSize, size);
3425 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3426 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3427 }
3428
3429 {
3430 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3431 ASSERT_TRUE(annot);
3432
3433 // This has an odd number of elements in the path array, ignore the last
3434 // element.
3435 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3436 const size_t kExpectedSize = 1;
3437 ASSERT_EQ(kExpectedSize, size);
3438 const unsigned long kPathIndex = 0;
3439 unsigned long path_size =
3440 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3441 const size_t kExpectedPathSize = 3;
3442 ASSERT_EQ(kExpectedPathSize, path_size);
3443 std::vector<FS_POINTF> path_buffer(path_size);
3444 EXPECT_EQ(path_size,
3445 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3446 path_buffer.data(), path_size));
3447 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3448 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3449 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3450 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3451 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3452 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3453 }
3454
3455 {
3456 // Wrong annotation type.
3457 ScopedFPDFAnnotation polygon_annot(
3458 FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON));
3459 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3460 const unsigned long kPathIndex = 0;
3461 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3462 nullptr, 0));
3463 }
3464
3465 UnloadPage(page);
3466}
Miklos Vajna30f45a62020-12-04 19:12:31 +00003467
3468TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) {
3469 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3470 FPDF_PAGE page = LoadPage(0);
3471 ASSERT_TRUE(page);
3472 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3473
3474 {
3475 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3476 ASSERT_TRUE(annot);
3477
3478 // FPDFAnnot_GetVertices() positive testing.
3479 FS_POINTF start;
3480 FS_POINTF end;
3481 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3482 EXPECT_FLOAT_EQ(159.0f, start.x);
3483 EXPECT_FLOAT_EQ(296.0f, start.y);
3484 EXPECT_FLOAT_EQ(472.0f, end.x);
3485 EXPECT_FLOAT_EQ(243.42f, end.y);
3486
3487 // FPDFAnnot_GetVertices() negative testing.
3488 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3489 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3490 }
3491
3492 {
3493 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3494 ASSERT_TRUE(annot);
3495
3496 // Too few elements in the line array.
3497 FS_POINTF start;
3498 FS_POINTF end;
3499 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3500 }
3501
3502 {
3503 // Wrong annotation type.
3504 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3505 FS_POINTF start;
3506 FS_POINTF end;
3507 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3508 }
3509
3510 UnloadPage(page);
3511}
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003512
3513TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) {
3514 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3515 FPDF_PAGE page = LoadPage(0);
3516 ASSERT_TRUE(page);
3517 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3518
3519 {
3520 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3521 ASSERT_TRUE(annot);
3522
3523 // FPDFAnnot_GetBorder() positive testing.
3524 float horizontal_radius;
3525 float vertical_radius;
3526 float border_width;
3527 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3528 &vertical_radius, &border_width));
3529 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3530 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3531 EXPECT_FLOAT_EQ(2.0f, border_width);
3532
3533 // FPDFAnnot_GetBorder() negative testing.
3534 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3535 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3536 }
3537
3538 {
3539 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3540 ASSERT_TRUE(annot);
3541
3542 // Too few elements in the border array.
3543 float horizontal_radius;
3544 float vertical_radius;
3545 float border_width;
3546 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3547 &vertical_radius, &border_width));
Lei Zhang21ea6652021-04-15 23:24:46 +00003548
3549 // FPDFAnnot_SetBorder() positive testing.
3550 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3551 /*vertical_radius=*/3.5f,
3552 /*border_width=*/4.0f));
3553
3554 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3555 &vertical_radius, &border_width));
3556 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3557 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3558 EXPECT_FLOAT_EQ(4.0f, border_width);
3559
3560 // FPDFAnnot_SetBorder() negative testing.
3561 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3562 /*vertical_radius=*/2.5f,
3563 /*border_width=*/3.0f));
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003564 }
3565
3566 UnloadPage(page);
3567}