blob: 198bbe207fee65110d1dc01253b587403c4a9e27 [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
91} // namespace
Jane Liu4fd9a472017-06-01 18:56:09 -040092
Lei Zhangab41f252018-12-23 03:10:50 +000093class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040094
Tom Sepez204ab052020-06-12 21:33:48 +000095TEST_F(FPDFAnnotEmbedderTest, SetAP) {
96 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
97 ASSERT_TRUE(doc);
98 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
99 ASSERT_TRUE(page);
100 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
101 ASSERT_TRUE(ap_stream);
102
103 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
104 ASSERT_TRUE(annot);
105
106 // Negative case: FPDFAnnot_SetAP() should fail if bounding rect is not yet
107 // set on the annotation.
108 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
109 ap_stream.get()));
110
111 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
112 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
113
114 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
115 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/255));
116
117 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
118 ap_stream.get()));
119
120 // Verify that appearance stream is created as form XObject
121 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
122 ASSERT_TRUE(context);
123 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
124 ASSERT_TRUE(annot_dict);
125 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
126 ASSERT_TRUE(ap_dict);
127 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
128 ASSERT_TRUE(stream_dict);
129 // Check for non-existence of resources dictionary in case of opaque color
130 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
131 ASSERT_FALSE(resources_dict);
132 ByteString type = stream_dict->GetStringFor(pdfium::annotation::kType);
133 EXPECT_EQ("XObject", type);
134 ByteString sub_type = stream_dict->GetStringFor(pdfium::annotation::kSubtype);
135 EXPECT_EQ("Form", sub_type);
136
137 // Check that the appearance stream is same as we just set.
138 const uint32_t kStreamDataSize =
139 pdfium::size(kStreamData) * sizeof(FPDF_WCHAR);
140 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
141 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
142 ASSERT_EQ(kStreamDataSize, normal_length_bytes);
143 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
144 EXPECT_EQ(kStreamDataSize,
145 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
146 buf.data(), normal_length_bytes));
147 EXPECT_EQ(kStreamData, GetPlatformWString(buf.data()));
148}
149
150TEST_F(FPDFAnnotEmbedderTest, SetAPWithOpacity) {
151 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
152 ASSERT_TRUE(doc);
153 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
154 ASSERT_TRUE(page);
155 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
156 ASSERT_TRUE(ap_stream);
157
158 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
159 ASSERT_TRUE(annot);
160
161 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
162 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/102));
163
164 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
165 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
166
167 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
168 ap_stream.get()));
169
170 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
171 ASSERT_TRUE(context);
172 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
173 ASSERT_TRUE(annot_dict);
174 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
175 ASSERT_TRUE(ap_dict);
176 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
177 ASSERT_TRUE(stream_dict);
178 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
179 ASSERT_TRUE(stream_dict);
180 CPDF_Dictionary* extGState_dict = resources_dict->GetDictFor("ExtGState");
181 ASSERT_TRUE(extGState_dict);
182 CPDF_Dictionary* gs_dict = extGState_dict->GetDictFor("GS");
183 ASSERT_TRUE(gs_dict);
184 ByteString type = gs_dict->GetStringFor(pdfium::annotation::kType);
185 EXPECT_EQ("ExtGState", type);
186 float opacity = gs_dict->GetNumberFor("CA");
187 // Opacity value of 102 is represented as 0.4f (=104/255) in /CA entry.
188 EXPECT_FLOAT_EQ(0.4f, opacity);
189 ByteString blend_mode = gs_dict->GetStringFor("BM");
190 EXPECT_EQ("Normal", blend_mode);
191 bool alpha_source_flag = gs_dict->GetBooleanFor("AIS", true);
192 EXPECT_FALSE(alpha_source_flag);
193}
194
195TEST_F(FPDFAnnotEmbedderTest, InkListAPIValidations) {
196 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
197 ASSERT_TRUE(doc);
198 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
199 ASSERT_TRUE(page);
200
201 // Create a new ink annotation.
202 ScopedFPDFAnnotation ink_annot(
203 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
204 ASSERT_TRUE(ink_annot);
205 CPDF_AnnotContext* context =
206 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
207 ASSERT_TRUE(context);
208 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
209 ASSERT_TRUE(annot_dict);
210
211 static constexpr FS_POINTF kFirstInkStroke[] = {
212 {80.0f, 90.0f}, {81.0f, 91.0f}, {82.0f, 92.0f},
213 {83.0f, 93.0f}, {84.0f, 94.0f}, {85.0f, 95.0f}};
214 static constexpr size_t kFirstStrokePointCount =
215 pdfium::size(kFirstInkStroke);
216
217 static constexpr FS_POINTF kSecondInkStroke[] = {
218 {70.0f, 90.0f}, {71.0f, 91.0f}, {72.0f, 92.0f}};
219 static constexpr size_t kSecondStrokePointCount =
220 pdfium::size(kSecondInkStroke);
221
222 static constexpr FS_POINTF kThirdInkStroke[] = {{60.0f, 90.0f},
223 {61.0f, 91.0f},
224 {62.0f, 92.0f},
225 {63.0f, 93.0f},
226 {64.0f, 94.0f}};
227 static constexpr size_t kThirdStrokePointCount =
228 pdfium::size(kThirdInkStroke);
229
230 // Negative test: |annot| is passed as nullptr.
231 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(nullptr, kFirstInkStroke,
232 kFirstStrokePointCount));
233
234 // Negative test: |annot| is not ink annotation.
235 // Create a new highlight annotation.
236 ScopedFPDFAnnotation highlight_annot(
237 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
238 ASSERT_TRUE(highlight_annot);
239 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(highlight_annot.get(), kFirstInkStroke,
240 kFirstStrokePointCount));
241
242 // Negative test: passing |point_count| as 0.
243 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke, 0));
244
245 // Negative test: passing |points| array as nullptr.
246 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), nullptr,
247 kFirstStrokePointCount));
248
249 // Negative test: passing |point_count| more than ULONG_MAX/2.
250 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
251 ULONG_MAX / 2 + 1));
252
253 // InkStroke should get added to ink annotation. Also inklist should get
254 // created.
255 EXPECT_EQ(0, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke,
256 kFirstStrokePointCount));
257
258 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
259 ASSERT_TRUE(inklist);
260 EXPECT_EQ(1u, inklist->size());
261 EXPECT_EQ(kFirstStrokePointCount * 2, inklist->GetArrayAt(0)->size());
262
263 // Adding another inkStroke to ink annotation with all valid paremeters.
264 // InkList already exists in ink_annot.
265 EXPECT_EQ(1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
266 kSecondStrokePointCount));
267 EXPECT_EQ(2u, inklist->size());
268 EXPECT_EQ(kSecondStrokePointCount * 2, inklist->GetArrayAt(1)->size());
269
270 // Adding one more InkStroke to the ink annotation. |point_count| passed is
271 // less than the data available in |buffer|.
272 EXPECT_EQ(2, FPDFAnnot_AddInkStroke(ink_annot.get(), kThirdInkStroke,
273 kThirdStrokePointCount - 1));
274 EXPECT_EQ(3u, inklist->size());
275 EXPECT_EQ((kThirdStrokePointCount - 1) * 2, inklist->GetArrayAt(2)->size());
276}
277
278TEST_F(FPDFAnnotEmbedderTest, RemoveInkList) {
279 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
280 ASSERT_TRUE(doc);
281 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
282 ASSERT_TRUE(page);
283
284 // Negative test: |annot| is passed as nullptr.
285 EXPECT_FALSE(FPDFAnnot_RemoveInkList(nullptr));
286
287 // Negative test: |annot| is not ink annotation.
288 // Create a new highlight annotation.
289 ScopedFPDFAnnotation highlight_annot(
290 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
291 ASSERT_TRUE(highlight_annot);
292 EXPECT_FALSE(FPDFAnnot_RemoveInkList(highlight_annot.get()));
293
294 // Create a new ink annotation.
295 ScopedFPDFAnnotation ink_annot(
296 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
297 ASSERT_TRUE(ink_annot);
298 CPDF_AnnotContext* context =
299 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
300 ASSERT_TRUE(context);
301 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
302 ASSERT_TRUE(annot_dict);
303
304 static constexpr FS_POINTF kInkStroke[] = {{80.0f, 90.0f}, {81.0f, 91.0f},
305 {82.0f, 92.0f}, {83.0f, 93.0f},
306 {84.0f, 94.0f}, {85.0f, 95.0f}};
307 static constexpr size_t kPointCount = pdfium::size(kInkStroke);
308
309 // InkStroke should get added to ink annotation. Also inklist should get
310 // created.
311 EXPECT_EQ(0,
312 FPDFAnnot_AddInkStroke(ink_annot.get(), kInkStroke, kPointCount));
313
314 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
315 ASSERT_TRUE(inklist);
316 ASSERT_EQ(1u, inklist->size());
317 EXPECT_EQ(kPointCount * 2, inklist->GetArrayAt(0)->size());
318
319 // Remove inklist.
320 EXPECT_TRUE(FPDFAnnot_RemoveInkList(ink_annot.get()));
321 EXPECT_FALSE(annot_dict->KeyExist("InkList"));
322}
323
Lei Zhangab41f252018-12-23 03:10:50 +0000324TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +0000325 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
326 FPDF_PAGE page = LoadPage(0);
327 ASSERT_TRUE(page);
328
329 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
330
331 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
332 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
333 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
334 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
335 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
336
337 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
338
339 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
340
341 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
342 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
343 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
344
345 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
346
Lei Zhang4f556b82019-04-08 16:32:41 +0000347 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +0000348 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +0000349 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
350
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000351 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +0000352 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
353 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
354 EXPECT_EQ(0u,
355 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
356
357 UnloadPage(page);
358}
359
Lei Zhang3d9a0972019-03-04 19:34:09 +0000360TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
361 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
362 FPDF_PAGE page = LoadPage(0);
363 ASSERT_TRUE(page);
364
365 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +0000366 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +0000367
368 UnloadPage(page);
369}
370
Lei Zhangab41f252018-12-23 03:10:50 +0000371TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -0400372 // Open a file with one annotation and load its first page.
373 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000374 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -0400375 ASSERT_TRUE(page);
376
377 // This annotation has a malformed appearance stream, which does not have its
378 // normal appearance defined, only its rollover appearance. In this case, its
379 // normal appearance should be generated, allowing the highlight annotation to
380 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000381 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000382 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -0400383
384 UnloadPage(page);
385}
386
Hui Yingstd5b6f632020-07-22 01:31:59 +0000387TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang03e5e682019-09-16 19:45:55 +0000388#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstd5b6f632020-07-22 01:31:59 +0000389 static const char kChecksum[] = "ec1f4ccbd0aecfdea6d53893387a0101";
Lei Zhang03e5e682019-09-16 19:45:55 +0000390#else
Hui Yingstd5b6f632020-07-22 01:31:59 +0000391 static const char kChecksum[] = "76512832d88017668d9acc7aacd13dae";
Lei Zhang03e5e682019-09-16 19:45:55 +0000392#endif
Henrique Nakashima5098b252018-03-26 21:46:00 +0000393 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000394 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
395 FPDF_PAGE page = LoadPage(0);
396 ASSERT_TRUE(page);
397
Tom Sepeze08d2b12018-04-25 18:49:32 +0000398 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstd5b6f632020-07-22 01:31:59 +0000399 CompareBitmap(bitmap.get(), 595, 842, kChecksum);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000400
401 UnloadPage(page);
402}
403
Lei Zhangab41f252018-12-23 03:10:50 +0000404TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400405 // Open a file with one annotation and load its first page.
406 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000407 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400408 ASSERT_TRUE(page);
409
410 // Check that there is a total of 1 annotation on its first page.
411 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
412
413 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000414 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000415 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000416 ASSERT_TRUE(annot);
417 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400418
Lei Zhanga21d5932018-02-05 18:28:38 +0000419 // Check that the annotation color is yellow.
420 unsigned int R;
421 unsigned int G;
422 unsigned int B;
423 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000424 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000425 &G, &B, &A));
426 EXPECT_EQ(255u, R);
427 EXPECT_EQ(255u, G);
428 EXPECT_EQ(0u, B);
429 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400430
Lei Zhanga21d5932018-02-05 18:28:38 +0000431 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000432 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000433 EXPECT_EQ(FPDF_OBJECT_STRING,
434 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000435 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000436 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000437 ASSERT_EQ(28u, length_bytes);
438 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000439 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000440 length_bytes));
441 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400442
Lei Zhanga21d5932018-02-05 18:28:38 +0000443 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000444 EXPECT_EQ(
445 FPDF_OBJECT_STRING,
446 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000447 length_bytes = FPDFAnnot_GetStringValue(
448 annot.get(), pdfium::annotation::kContents, nullptr, 0);
449 ASSERT_EQ(2690u, length_bytes);
450 buf = GetFPDFWideStringBuffer(length_bytes);
451 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
452 pdfium::annotation::kContents,
453 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000454 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000455 L"This is a note for that highlight annotation. Very long highlight "
456 "annotation. Long long long Long long longLong long longLong long "
457 "longLong long longLong long longLong long longLong long longLong long "
458 "longLong long longLong long longLong long longLong long longLong long "
459 "longLong long longLong long longLong long longLong long longLong long "
460 "longLong long longLong long longLong long longLong long longLong long "
461 "longLong long longLong long longLong long longLong long longLong long "
462 "longLong long longLong long longLong long longLong long longLong long "
463 "longLong long longLong long longLong long longLong long longLong long "
464 "longLong long longLong long longLong long longLong long longLong long "
465 "longLong long longLong long longLong long longLong long longLong long "
466 "longLong long longLong long longLong long longLong long longLong long "
467 "longLong long longLong long longLong long longLong long longLong long "
468 "longLong long longLong long longLong long longLong long longLong long "
469 "longLong long longLong long longLong long longLong long longLong long "
470 "longLong long longLong long longLong long longLong long longLong long "
471 "longLong long longLong long longLong long longLong long longLong long "
472 "longLong long longLong long longLong long longLong long longLong long "
473 "longLong long longLong long longLong long longLong long longLong long "
474 "longLong long long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000475 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400476
Lei Zhanga21d5932018-02-05 18:28:38 +0000477 // Check that the quadpoints are correct.
478 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000479 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000480 EXPECT_EQ(115.802643f, quadpoints.x1);
481 EXPECT_EQ(718.913940f, quadpoints.y1);
482 EXPECT_EQ(157.211182f, quadpoints.x4);
483 EXPECT_EQ(706.264465f, quadpoints.y4);
484 }
Tom Sepez507d0192018-11-07 16:37:51 +0000485 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400486}
487
Hui Yingst9b6b1542020-07-27 16:11:12 +0000488TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400489 // Open a file with three annotations and load its first page.
490 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000491 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400492 ASSERT_TRUE(page);
493
494 // Check that there is a total of 3 annotation on its first page.
495 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
496
Lei Zhanga21d5932018-02-05 18:28:38 +0000497 {
498 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000499 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000500 ASSERT_TRUE(annot);
501 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400502
Lei Zhanga21d5932018-02-05 18:28:38 +0000503 // Check that the annotation color is blue with opacity.
504 unsigned int R;
505 unsigned int G;
506 unsigned int B;
507 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000508 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000509 &G, &B, &A));
510 EXPECT_EQ(0u, R);
511 EXPECT_EQ(0u, G);
512 EXPECT_EQ(255u, B);
513 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400514
Lei Zhanga21d5932018-02-05 18:28:38 +0000515 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000516 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
517 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400518
Lei Zhang4f556b82019-04-08 16:32:41 +0000519 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000520 // Note that upon rendering, the rectangle coordinates will be adjusted.
521 FS_RECTF rect;
522 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
523 EXPECT_EQ(351.820404f, rect.left);
524 EXPECT_EQ(583.830688f, rect.bottom);
525 EXPECT_EQ(475.336090f, rect.right);
526 EXPECT_EQ(681.535034f, rect.top);
527 }
Tom Sepezef43c262018-11-07 16:41:32 +0000528 {
Hui Yingst57daee82020-10-09 05:47:20 +0000529#if defined(_SKIA_SUPPORT_) && defined(OS_APPLE)
530 static constexpr char kExpectedHash[] = "fad91b9c968fe8019a774f5e2419b8fc";
531#elif defined(_SKIA_SUPPORT_PATHS_) && defined(OS_APPLE)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000532 static constexpr char kExpectedHash[] = "acddfe688a117ead56af7b249a2cf8a1";
Hui Yingst57daee82020-10-09 05:47:20 +0000533#elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000534 static constexpr char kExpectedHash[] = "1fb0dd8dd5f0b9bb8d076e48eb59296d";
Hui Yingst57daee82020-10-09 05:47:20 +0000535#elif defined(OS_WIN)
Lei Zhang430b5322020-07-06 22:23:36 +0000536 static constexpr char kExpectedHash[] = "49d0a81c636531a337429325273d0508";
537#else
538 static constexpr char kExpectedHash[] = "354002e1c4386d38fdde29ef8d61074a";
Hui Yingst57daee82020-10-09 05:47:20 +0000539#endif
Tom Sepezef43c262018-11-07 16:41:32 +0000540 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang430b5322020-07-06 22:23:36 +0000541 CompareBitmap(bitmap.get(), 612, 792, kExpectedHash);
Tom Sepezef43c262018-11-07 16:41:32 +0000542 }
Tom Sepez507d0192018-11-07 16:37:51 +0000543 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400544}
Jane Liu20eafda2017-06-07 10:33:24 -0400545
Lei Zhangab41f252018-12-23 03:10:50 +0000546TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400547 // Open a file with one annotation and load its first page.
548 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000549 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400550 ASSERT_TRUE(page);
551
552 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400553 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400554
555 UnloadPage(page);
556}
557
Lei Zhangab41f252018-12-23 03:10:50 +0000558TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400559 // Open a file with no annotation and load its first page.
560 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000561 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400562 ASSERT_TRUE(page);
563 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
564
Lei Zhanga21d5932018-02-05 18:28:38 +0000565 {
566 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000567 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000568 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400569
Lei Zhanga21d5932018-02-05 18:28:38 +0000570 // Check that there is now 1 annotations on this page.
571 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400572
Lei Zhanga21d5932018-02-05 18:28:38 +0000573 // Check that the subtype of the annotation is correct.
574 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
575 }
Jane Liue10509a2017-06-20 16:47:41 -0400576
Lei Zhanga21d5932018-02-05 18:28:38 +0000577 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000578 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000579 ASSERT_TRUE(annot);
580 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400581
Lei Zhanga21d5932018-02-05 18:28:38 +0000582 // Set the color of the annotation.
583 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
584 102, 153, 204));
585 // Check that the color has been set correctly.
586 unsigned int R;
587 unsigned int G;
588 unsigned int B;
589 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000590 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000591 &G, &B, &A));
592 EXPECT_EQ(51u, R);
593 EXPECT_EQ(102u, G);
594 EXPECT_EQ(153u, B);
595 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400596
Lei Zhanga21d5932018-02-05 18:28:38 +0000597 // Change the color of the annotation.
598 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
599 153, 102, 51));
600 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000601 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000602 &G, &B, &A));
603 EXPECT_EQ(204u, R);
604 EXPECT_EQ(153u, G);
605 EXPECT_EQ(102u, B);
606 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400607
Lei Zhanga21d5932018-02-05 18:28:38 +0000608 // Set the annotation rectangle.
609 FS_RECTF rect;
610 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
611 EXPECT_EQ(0.f, rect.left);
612 EXPECT_EQ(0.f, rect.right);
613 rect.left = 35;
614 rect.bottom = 150;
615 rect.right = 53;
616 rect.top = 165;
617 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
618 // Check that the annotation rectangle has been set correctly.
619 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
620 EXPECT_EQ(35.f, rect.left);
621 EXPECT_EQ(150.f, rect.bottom);
622 EXPECT_EQ(53.f, rect.right);
623 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400624
Lei Zhanga21d5932018-02-05 18:28:38 +0000625 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000626 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000627 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000628 ASSERT_TRUE(FPDFAnnot_SetStringValue(
629 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000630 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000631 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000632 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000633 ASSERT_EQ(74u, length_bytes);
634 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
635 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
636 pdfium::annotation::kContents,
637 buf.data(), length_bytes));
638 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000639 }
Jane Liu20eafda2017-06-07 10:33:24 -0400640 UnloadPage(page);
641}
642
Hui Yingstb3490322020-07-22 00:53:29 +0000643TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400644 // Open a file with one annotation and load its first page.
645 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000646 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400647 ASSERT_TRUE(page);
648
649 // Check that there is a total of one annotation on its first page, and verify
650 // its quadpoints.
651 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400652 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000653 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000654 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000655 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000656 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000657 EXPECT_EQ(115.802643f, quadpoints.x1);
658 EXPECT_EQ(718.913940f, quadpoints.y1);
659 EXPECT_EQ(157.211182f, quadpoints.x4);
660 EXPECT_EQ(706.264465f, quadpoints.y4);
661 }
Jane Liu20eafda2017-06-07 10:33:24 -0400662
663 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000664 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000665 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000666 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
667 ASSERT_TRUE(annot);
668 quadpoints.x1 = 140.802643f;
669 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000670 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000671 }
Jane Liu20eafda2017-06-07 10:33:24 -0400672
Lei Zhangec618142021-04-13 17:36:46 +0000673 // Save the document and close the page.
Jane Liu20eafda2017-06-07 10:33:24 -0400674 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000675 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400676
677 // Open the saved document.
Hui Yingst57daee82020-10-09 05:47:20 +0000678#if defined(_SKIA_SUPPORT_) && defined(OS_APPLE)
679 static const char kChecksum[] = "899387ae792390cd0d83cf7e2bbebfb5";
680#elif defined(_SKIA_SUPPORT_PATHS_) && defined(OS_APPLE)
681 static const char kChecksum[] = "e40e235ee35f47ff28dda009aaaf36df";
682#elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstb3490322020-07-22 00:53:29 +0000683 static const char kChecksum[] = "798fa41303381c9ba6d99092f5cd4d2b";
684#else
685 static const char kChecksum[] = "dba153419f67b7c0c0e3d22d3e8910d5";
686#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400687
Lei Zhang0b494052019-01-31 21:41:15 +0000688 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000689 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +0000690 ASSERT_TRUE(page);
Hui Yingstb3490322020-07-22 00:53:29 +0000691 VerifySavedRendering(page, 612, 792, kChecksum);
Jane Liu20eafda2017-06-07 10:33:24 -0400692
693 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000694 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400695
Lei Zhanga21d5932018-02-05 18:28:38 +0000696 {
697 // Check that the second annotation is an underline annotation and verify
698 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000699 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000700 ASSERT_TRUE(new_annot);
701 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
702 FS_QUADPOINTSF new_quadpoints;
703 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000704 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000705 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
706 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
707 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
708 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
709 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400710
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000711 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400712 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400713}
Jane Liu06462752017-06-27 16:41:14 -0400714
Lei Zhangab41f252018-12-23 03:10:50 +0000715TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000716 // Open a file with four annotations and load its first page.
717 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
718 FPDF_PAGE page = LoadPage(0);
719 ASSERT_TRUE(page);
720 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
721
722 // Retrieve the highlight annotation.
723 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
724 ASSERT_TRUE(annot);
725 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
726
727 FS_QUADPOINTSF quadpoints;
728 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
729
730 {
731 // Verify the current one set of quadpoints.
732 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
733
734 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
735 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
736 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
737 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
738 }
739
740 {
741 // Update the quadpoints.
742 FS_QUADPOINTSF new_quadpoints = quadpoints;
743 new_quadpoints.y1 -= 20.f;
744 new_quadpoints.y2 -= 20.f;
745 new_quadpoints.y3 -= 20.f;
746 new_quadpoints.y4 -= 20.f;
747 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
748
749 // Verify added quadpoint set
750 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
751 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
752 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
753 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
754 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
755 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
756 }
757
758 {
759 // Append a new set of quadpoints.
760 FS_QUADPOINTSF new_quadpoints = quadpoints;
761 new_quadpoints.y1 += 20.f;
762 new_quadpoints.y2 += 20.f;
763 new_quadpoints.y3 += 20.f;
764 new_quadpoints.y4 += 20.f;
765 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
766
767 // Verify added quadpoint set
768 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
769 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
770 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
771 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
772 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
773 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
774 }
775
776 {
777 // Setting and getting quadpoints at out-of-bound index should fail
778 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
779 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
780 }
781
782 FPDFPage_CloseAnnot(annot);
783
784 // Retrieve the square annotation
785 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
786
787 {
788 // Check that attempting to set its quadpoints would fail
789 ASSERT_TRUE(squareAnnot);
790 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
791 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
792 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
793 }
794
795 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000796 UnloadPage(page);
797}
798
Hui Yingstb64cd122020-07-27 16:01:02 +0000799// TODO(crbug.com/pdfium/1569): Fix this issue and enable the test for Skia.
800#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +0000801#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
802#else
803#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
804#endif
805TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Hui Yingstb64cd122020-07-27 16:01:02 +0000806#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
807 static const char kMd5Original[] = "00e70eb543c2a6e8f8aafb4ee951d9bf";
Lei Zhang4f556b82019-04-08 16:32:41 +0000808 static const char kMd5ModifiedHighlight[] =
Hui Yingstb64cd122020-07-27 16:01:02 +0000809 "7638c4a8fe4aabbf8704e198f49b3198";
810 static const char kMd5ModifiedSquare[] = "54f507af6af63de877b9cafdab1bbdaa";
811#else
812#if defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000813 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
814 static const char kMd5ModifiedHighlight[] =
815 "66f3caef3a7d488a4fa1ad37fc06310e";
816 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Lei Zhang0c03d632020-07-30 17:05:36 +0000817#elif defined(OS_APPLE)
Hui Yingstb64cd122020-07-27 16:01:02 +0000818 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
819 static const char kMd5ModifiedHighlight[] =
820 "e64bf648f6e9354d1f3eedb47a2c9498";
821 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Jane Liub370e5a2017-08-16 13:24:58 -0400822#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000823 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
824 static const char kMd5ModifiedHighlight[] =
825 "66f3caef3a7d488a4fa1ad37fc06310e";
826 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400827#endif
Hui Yingstb64cd122020-07-27 16:01:02 +0000828#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liub370e5a2017-08-16 13:24:58 -0400829
Jane Liu06462752017-06-27 16:41:14 -0400830 // Open a file with four annotations and load its first page.
831 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000832 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400833 ASSERT_TRUE(page);
834 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
835
Jane Liub370e5a2017-08-16 13:24:58 -0400836 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000837 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000838 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000839 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000840 }
Jane Liub370e5a2017-08-16 13:24:58 -0400841
Jane Liu0c6b07d2017-08-15 10:50:22 -0400842 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400843 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000844
845 // Retrieve the highlight annotation which has its AP stream already defined.
846 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000847 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000848 ASSERT_TRUE(annot);
849 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
850
851 // Check that color cannot be set when an AP stream is defined already.
852 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
853 102, 153, 204));
854
855 // Verify its attachment points.
856 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000857 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000858 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
859 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
860 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
861 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
862
863 // Check that updating the attachment points would succeed.
864 quadpoints.x1 -= 50.f;
865 quadpoints.x2 -= 50.f;
866 quadpoints.x3 -= 50.f;
867 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000868 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000869 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000870 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000871 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
872 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
873 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
874 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
875
876 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000877 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000878 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000879 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000880 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000881
882 // Verify its annotation rectangle.
883 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
884 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
885 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
886 EXPECT_NEAR(136.325f, rect.right, 0.001f);
887 EXPECT_NEAR(721.292f, rect.top, 0.001f);
888
889 // Check that updating the rectangle would succeed.
890 rect.left -= 60.f;
891 rect.right -= 60.f;
892 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
893 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
894 EXPECT_EQ(rect.right, new_rect.right);
895 }
Jane Liu06462752017-06-27 16:41:14 -0400896
Jane Liub370e5a2017-08-16 13:24:58 -0400897 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000898 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000899 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000900 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000901 }
Jane Liub370e5a2017-08-16 13:24:58 -0400902
Lei Zhanga21d5932018-02-05 18:28:38 +0000903 {
904 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000905 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000906 ASSERT_TRUE(annot);
907 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400908
Lei Zhanga21d5932018-02-05 18:28:38 +0000909 // Check that updating the rectangle would succeed.
910 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
911 rect.left += 70.f;
912 rect.right += 70.f;
913 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
914 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
915 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400916
Lei Zhanga21d5932018-02-05 18:28:38 +0000917 // Check that updating the rectangle changes the square annotation's
918 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000919 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000920 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000921 }
Jane Liub370e5a2017-08-16 13:24:58 -0400922
Jane Liu06462752017-06-27 16:41:14 -0400923 UnloadPage(page);
924}
Jane Liu8ce58f52017-06-29 13:40:22 -0400925
Lei Zhangab41f252018-12-23 03:10:50 +0000926TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000927 // Open a file with multiline markup annotations.
928 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
929 FPDF_PAGE page = LoadPage(0);
930 ASSERT_TRUE(page);
931 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000932 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000933 ASSERT_TRUE(annot);
934
935 // This is a three line annotation.
936 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
937 }
938 UnloadPage(page);
939
940 // null annotation should return 0
941 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
942}
943
Lei Zhangab41f252018-12-23 03:10:50 +0000944TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400945 // Open a file with 3 annotations on its first page.
946 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000947 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400948 ASSERT_TRUE(page);
949 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
950
Jane Liu0c6b07d2017-08-15 10:50:22 -0400951 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400952
Lei Zhanga21d5932018-02-05 18:28:38 +0000953 // Check that the annotations have the expected rectangle coordinates.
954 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000955 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000956 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
957 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
958 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400959
Lei Zhanga21d5932018-02-05 18:28:38 +0000960 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000961 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000962 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
963 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
964 }
965
966 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000967 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000968 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
969 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
970 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400971
972 // Check that nothing happens when attempting to remove an annotation with an
973 // out-of-bound index.
974 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
975 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
976 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
977
978 // Remove the second annotation.
979 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
980 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
981 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
982
Lei Zhangec618142021-04-13 17:36:46 +0000983 // Save the document and close the page.
Jane Liu8ce58f52017-06-29 13:40:22 -0400984 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000985 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400986
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400987 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400988 // Open the saved document.
989 std::string new_file = GetString();
990 FPDF_FILEACCESS file_access;
991 memset(&file_access, 0, sizeof(file_access));
992 file_access.m_FileLen = new_file.size();
993 file_access.m_GetBlock = GetBlockFromString;
994 file_access.m_Param = &new_file;
995 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
996 ASSERT_TRUE(new_doc);
997 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
998 ASSERT_TRUE(new_page);
999
1000 // Check that the saved document has 2 annotations on the first page.
1001 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
1002
Lei Zhanga21d5932018-02-05 18:28:38 +00001003 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1004 // by verifying their rectangle coordinates.
1005 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001006 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001007 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1008 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1009 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001010
Lei Zhanga21d5932018-02-05 18:28:38 +00001011 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001012 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001013 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1014 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1015 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001016 FPDF_ClosePage(new_page);
1017 FPDF_CloseDocument(new_doc);
1018}
Jane Liu8ce58f52017-06-29 13:40:22 -04001019
Hui Yingst4a21df02020-05-13 03:15:44 +00001020TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001021#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001022#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst4a21df02020-05-13 03:15:44 +00001023 static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa";
1024 static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622";
1025 static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001026#else
Hui Yingst4a21df02020-05-13 03:15:44 +00001027 static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4";
1028 static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f";
1029 static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001030#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst4a21df02020-05-13 03:15:44 +00001031#else
1032#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001033 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
1034 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
1035 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Lei Zhang0c03d632020-07-30 17:05:36 +00001036#elif defined(OS_APPLE)
Hui Yingst4a21df02020-05-13 03:15:44 +00001037 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
1038 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
1039 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Jane Liubaa7ff42017-06-29 19:18:23 -04001040#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001041 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
1042 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
1043 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -04001044#endif
Hui Yingst4a21df02020-05-13 03:15:44 +00001045#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liubaa7ff42017-06-29 19:18:23 -04001046
1047 // Open a file with two annotations and load its first page.
1048 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001049 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001050 ASSERT_TRUE(page);
1051 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1052
1053 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001054 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001055 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001056 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001057 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001058
Lei Zhanga21d5932018-02-05 18:28:38 +00001059 {
1060 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001061 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001062 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001063
Lei Zhanga21d5932018-02-05 18:28:38 +00001064 // Check that this annotation has one path object and retrieve it.
1065 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001066 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001067 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1068 EXPECT_FALSE(path);
1069 path = FPDFAnnot_GetObject(annot.get(), 0);
1070 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1071 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001072
Lei Zhanga21d5932018-02-05 18:28:38 +00001073 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001074 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001075 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001076
Lei Zhanga21d5932018-02-05 18:28:38 +00001077 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001078 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001079 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001080 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001081 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001082
Lei Zhanga21d5932018-02-05 18:28:38 +00001083 // Add a second path object to the same annotation.
1084 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1085 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001086 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1087 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001088 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1089 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1090 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001091
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001092 // The object is in the annontation, not in the page, so the page object
1093 // array should not change.
1094 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1095
Lei Zhanga21d5932018-02-05 18:28:38 +00001096 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001097 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001098 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001099 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001100 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001101
Lei Zhanga21d5932018-02-05 18:28:38 +00001102 // Delete the newly added path object.
1103 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1104 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001105 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001106 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001107
1108 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001109 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001110 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001111 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001112 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001113
Jane Liubaa7ff42017-06-29 19:18:23 -04001114 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001115
Lei Zhanga21d5932018-02-05 18:28:38 +00001116 {
1117 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001118 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001119 ASSERT_TRUE(annot);
1120 rect.left = 200.f;
1121 rect.bottom = 400.f;
1122 rect.right = 500.f;
1123 rect.top = 600.f;
1124 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001125
Lei Zhanga21d5932018-02-05 18:28:38 +00001126 // Add a new path to the annotation.
1127 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1128 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1129 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1130 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1131 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001132 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1133 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001134 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1135 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1136 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1137
1138 // Check that the annotation's bounding box came from its rectangle.
1139 FS_RECTF new_rect;
1140 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1141 EXPECT_EQ(rect.left, new_rect.left);
1142 EXPECT_EQ(rect.bottom, new_rect.bottom);
1143 EXPECT_EQ(rect.right, new_rect.right);
1144 EXPECT_EQ(rect.top, new_rect.top);
1145 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001146
Lei Zhangec618142021-04-13 17:36:46 +00001147 // Save the document and close the page.
Jane Liubaa7ff42017-06-29 19:18:23 -04001148 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001149 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001150
1151 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001152 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001153 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001154 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001155 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001156
Jane Liu36567742017-07-06 11:13:35 -04001157 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001158 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001159
Lei Zhanga21d5932018-02-05 18:28:38 +00001160 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001161 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001162 ASSERT_TRUE(annot);
1163 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001164
Lei Zhanga21d5932018-02-05 18:28:38 +00001165 // Check that the new annotation's rectangle is as defined.
1166 FS_RECTF new_rect;
1167 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1168 EXPECT_EQ(rect.left, new_rect.left);
1169 EXPECT_EQ(rect.bottom, new_rect.bottom);
1170 EXPECT_EQ(rect.right, new_rect.right);
1171 EXPECT_EQ(rect.top, new_rect.top);
1172 }
1173
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001174 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001175 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001176}
Jane Liub137e752017-07-05 15:04:33 -04001177
Lei Zhangab41f252018-12-23 03:10:50 +00001178TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001179 // Open a file with an annotation and load its first page.
1180 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001181 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001182 ASSERT_TRUE(page);
1183
1184 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001185 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001186 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001187 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1188 }
Jane Liub137e752017-07-05 15:04:33 -04001189
Lei Zhanga21d5932018-02-05 18:28:38 +00001190 {
1191 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001192 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001193 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001194
Lei Zhanga21d5932018-02-05 18:28:38 +00001195 // Check that the original flag values are as expected.
1196 int flags = FPDFAnnot_GetFlags(annot.get());
1197 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1198 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001199
Lei Zhanga21d5932018-02-05 18:28:38 +00001200 // Set the HIDDEN flag.
1201 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1202 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1203 flags = FPDFAnnot_GetFlags(annot.get());
1204 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1205 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001206
Lei Zhanga21d5932018-02-05 18:28:38 +00001207 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001208 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001209 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001210 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001211 }
Jane Liub137e752017-07-05 15:04:33 -04001212
Lei Zhanga21d5932018-02-05 18:28:38 +00001213 // Unset the HIDDEN flag.
1214 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1215 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1216 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1217 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1218 flags = FPDFAnnot_GetFlags(annot.get());
1219 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1220 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001221
Lei Zhanga21d5932018-02-05 18:28:38 +00001222 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001223 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001224 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001225 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1226 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001227 }
Jane Liub137e752017-07-05 15:04:33 -04001228
Jane Liub137e752017-07-05 15:04:33 -04001229 UnloadPage(page);
1230}
Jane Liu36567742017-07-06 11:13:35 -04001231
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001232// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1233#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001234#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1235#else
1236#define MAYBE_AddAndModifyImage AddAndModifyImage
1237#endif
1238TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001239#if defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001240#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001241 static const char kMd5NewImage[] = "26a8eb30937226a677839379e0d7ae1a";
1242 static const char kMd5ModifiedImage[] = "2985114b32ba1a96be78ee643fe31aa5";
1243#else
1244 static const char kMd5NewImage[] = "14012ab500b4671fa73dd760129a8a93";
1245 static const char kMd5ModifiedImage[] = "5f97f98f58ed04dc393f31460485f1a2";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001246#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001247#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001248#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001249 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
1250 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +00001251#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001252 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
1253 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -04001254#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001255 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
1256 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -04001257#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001258#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001259
1260 // Open a file with two annotations and load its first page.
1261 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001262 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001263 ASSERT_TRUE(page);
1264 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1265
1266 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001267 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001268 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001269 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001270 }
Jane Liu36567742017-07-06 11:13:35 -04001271
Jane Liu36567742017-07-06 11:13:35 -04001272 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001273 FPDF_BITMAP image_bitmap;
1274
1275 {
1276 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001277 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001278 ASSERT_TRUE(annot);
1279 FS_RECTF rect;
1280 rect.left = 200.f;
1281 rect.bottom = 600.f;
1282 rect.right = 400.f;
1283 rect.top = 800.f;
1284 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1285
1286 // Add a solid-color translucent image object to the new annotation.
1287 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1288 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1289 0xeeeecccc);
1290 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1291 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1292 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1293 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1294 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1295 kBitmapSize, 0, 0));
1296 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1297 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1298 }
Jane Liu36567742017-07-06 11:13:35 -04001299
1300 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001301 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001302 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001303 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001304 }
Jane Liu36567742017-07-06 11:13:35 -04001305
Lei Zhanga21d5932018-02-05 18:28:38 +00001306 {
1307 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001308 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001309 ASSERT_TRUE(annot);
1310 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1311 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1312 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001313
Lei Zhanga21d5932018-02-05 18:28:38 +00001314 // Modify the image in the new annotation.
1315 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1316 0xff000000);
1317 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1318 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1319 }
Jane Liu36567742017-07-06 11:13:35 -04001320
Lei Zhangec618142021-04-13 17:36:46 +00001321 // Save the document and close the page.
Jane Liu36567742017-07-06 11:13:35 -04001322 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001323 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001324 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001325
1326 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001327 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001328}
1329
Hui Yingst6cd754f2020-05-14 04:05:25 +00001330TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001331#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001332#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001333 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1334 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001335#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001336 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1337 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001338#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001339#else
1340#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001341 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1342 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Lei Zhang0c03d632020-07-30 17:05:36 +00001343#elif defined(OS_APPLE)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001344 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1345 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001346#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001347 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1348 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001349#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001350#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001351
1352 // Open a file with two annotations and load its first page.
1353 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001354 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001355 ASSERT_TRUE(page);
1356 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1357
1358 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001359 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001360 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001361 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001362 }
Jane Liu36567742017-07-06 11:13:35 -04001363
Lei Zhanga21d5932018-02-05 18:28:38 +00001364 {
1365 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001366 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001367 ASSERT_TRUE(annot);
1368 FS_RECTF rect;
1369 rect.left = 200.f;
1370 rect.bottom = 550.f;
1371 rect.right = 450.f;
1372 rect.top = 650.f;
1373 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001374
Lei Zhanga21d5932018-02-05 18:28:38 +00001375 // Add a translucent text object to the new annotation.
1376 FPDF_PAGEOBJECT text_object =
1377 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1378 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001379 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001380 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1381 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001382 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001383 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1384 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1385 }
Jane Liu36567742017-07-06 11:13:35 -04001386
1387 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001388 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001389 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001390 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001391 }
Jane Liu36567742017-07-06 11:13:35 -04001392
Lei Zhanga21d5932018-02-05 18:28:38 +00001393 {
1394 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001395 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001396 ASSERT_TRUE(annot);
1397 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1398 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1399 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001400
Lei Zhanga21d5932018-02-05 18:28:38 +00001401 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001402 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001403 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1404 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1405 }
Jane Liu36567742017-07-06 11:13:35 -04001406
1407 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001408 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001409 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001410 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001411 }
Jane Liu36567742017-07-06 11:13:35 -04001412
1413 // Remove the new annotation, and check that the page renders as before.
1414 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001415 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001416 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001417 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001418 }
Jane Liu36567742017-07-06 11:13:35 -04001419
1420 UnloadPage(page);
1421}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001422
Hui Yingst9b6b1542020-07-27 16:11:12 +00001423TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001424 // Open a file with four annotations and load its first page.
1425 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001426 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001427 ASSERT_TRUE(page);
1428
Lei Zhang4f556b82019-04-08 16:32:41 +00001429 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001430
Lei Zhanga21d5932018-02-05 18:28:38 +00001431 {
1432 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001433 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001434 ASSERT_TRUE(annot);
1435
1436 // Check that a non-existent key does not exist.
1437 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1438
1439 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001440 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001441 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001442 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1443 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1444 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001445
1446 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001447 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001448 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001449 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001450 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001451 ASSERT_EQ(66u, length_bytes);
1452 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1453 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1454 length_bytes));
1455 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1456 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001457
1458 // Check that the string value of the modified date is correct.
1459 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001460 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1461 nullptr, 0);
1462 ASSERT_EQ(44u, length_bytes);
1463 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001464 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001465 buf.data(), length_bytes));
1466 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001467
1468 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001469 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001470 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1471 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001472 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001473
Lei Zhangec618142021-04-13 17:36:46 +00001474 // Save the document and close the page.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001475 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001476 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001477
Hui Yingst9b6b1542020-07-27 16:11:12 +00001478#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001479#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001480 static const char kMd5[] = "7a2b712ca88d7b71f125ea3f9c88e57a";
1481#else
1482 static const char kMd5[] = "626d25c5aa5baf67d22d9a0e1c23f6aa";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001483#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001484#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001485#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001486 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001487#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001488 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001489#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001490 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001491#endif
Hui Yingst9b6b1542020-07-27 16:11:12 +00001492#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Dan Sinclair971a6742018-03-28 19:23:25 +00001493
1494 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001495 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001496 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001497 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001498 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001499 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001500 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001501
Lei Zhanga21d5932018-02-05 18:28:38 +00001502 // Check that the string value of the modified date is the newly-set value.
1503 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001504 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001505 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001506 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001507 ASSERT_EQ(44u, length_bytes);
1508 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001509 EXPECT_EQ(44u,
1510 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001511 buf.data(), length_bytes));
1512 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001513 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001514
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001515 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001516 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001517}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001518
rycsmith3e785602019-03-05 21:48:36 +00001519TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001520 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001521 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1522 FPDF_PAGE page = LoadPage(0);
1523 ASSERT_TRUE(page);
1524 {
1525 // First two annotations do not have "MaxLen" attribute.
1526 for (int i = 0; i < 2; i++) {
1527 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1528 ASSERT_TRUE(annot);
1529
1530 // Verify that no "MaxLen" key present.
1531 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1532
1533 float value;
1534 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1535 }
1536
1537 // Annotation in index 2 has "MaxLen" of 10.
1538 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1539 ASSERT_TRUE(annot);
1540
1541 // Verify that "MaxLen" key present.
1542 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1543
1544 float value;
1545 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1546 EXPECT_FLOAT_EQ(10.0f, value);
1547
1548 // Check bad inputs.
1549 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1550 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1551 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1552 // Ask for key that exists but is not a number.
1553 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1554 }
1555
1556 UnloadPage(page);
1557}
1558
Lei Zhangab41f252018-12-23 03:10:50 +00001559TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001560 // Open a file with four annotations and load its first page.
1561 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001562 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001563 ASSERT_TRUE(page);
1564
Lei Zhanga21d5932018-02-05 18:28:38 +00001565 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001566 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1567 static constexpr size_t kExpectNormalAPLength = 73970;
1568
Lei Zhanga21d5932018-02-05 18:28:38 +00001569 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001570 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001571 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001572
Lei Zhanga21d5932018-02-05 18:28:38 +00001573 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001574 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001575 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001576 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001577
Lei Zhanga21d5932018-02-05 18:28:38 +00001578 // Check that the string value of an AP is not returned if the buffer is too
1579 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001580 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1581 // Write in the buffer to verify it's not overwritten.
1582 memcpy(buf.data(), "abcdefgh", 8);
1583 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001584 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001585 buf.data(), normal_length_bytes - 1));
1586 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001587
Lei Zhanga21d5932018-02-05 18:28:38 +00001588 // Check that the string value of an AP is returned through a buffer that is
1589 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001590 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001591 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001592 buf.data(), normal_length_bytes));
1593 EXPECT_EQ(kMd5NormalAP,
1594 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1595 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001596
Lei Zhanga21d5932018-02-05 18:28:38 +00001597 // Check that the string value of an AP is returned through a buffer that is
1598 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001599 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1600 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001601 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001602 buf.data(), normal_length_bytes + 2));
1603 EXPECT_EQ(kMd5NormalAP,
1604 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1605 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001606
Lei Zhanga21d5932018-02-05 18:28:38 +00001607 // Check that getting an AP for a mode that does not have an AP returns an
1608 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001609 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001610 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001611 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001612
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001613 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001614 EXPECT_EQ(2u,
1615 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001616 buf.data(), 1000));
1617 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001618
Lei Zhanga21d5932018-02-05 18:28:38 +00001619 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001620 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001621 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001622 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001623 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001624 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001625 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001626
Lei Zhanga21d5932018-02-05 18:28:38 +00001627 // Set the AP correctly now.
1628 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001629 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001630
Lei Zhanga21d5932018-02-05 18:28:38 +00001631 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001632 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001633 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001634 ASSERT_EQ(24u, rollover_length_bytes);
1635 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001636 EXPECT_EQ(24u,
1637 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001638 buf.data(), rollover_length_bytes));
1639 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001640
Lei Zhanga21d5932018-02-05 18:28:38 +00001641 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001642 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1643 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001644 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001645 buf.data(), normal_length_bytes));
1646 EXPECT_EQ(kMd5NormalAP,
1647 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1648 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001649 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001650
1651 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001652 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001653 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001654
Lei Zhang0b494052019-01-31 21:41:15 +00001655 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001656 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001657 ASSERT_TRUE(page);
Lei Zhanga21d5932018-02-05 18:28:38 +00001658 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001659 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001660
Lei Zhanga21d5932018-02-05 18:28:38 +00001661 // Check that the new annotation value is equal to the value we set before
1662 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001663 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001664 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001665 ASSERT_EQ(24u, rollover_length_bytes);
1666 std::vector<FPDF_WCHAR> buf =
1667 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001668 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1669 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001670 buf.data(), rollover_length_bytes));
1671 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001672 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001673
1674 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001675 CloseSavedPage(page);
1676 CloseSavedDocument();
1677}
1678
Lei Zhangab41f252018-12-23 03:10:50 +00001679TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001680 // Open a file with four annotations and load its first page.
1681 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001682 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001683 ASSERT_TRUE(page);
1684
Lei Zhanga21d5932018-02-05 18:28:38 +00001685 {
1686 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001687 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001688 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001689
Lei Zhanga21d5932018-02-05 18:28:38 +00001690 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001691 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001692 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001693 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001694 EXPECT_EQ(73970u,
1695 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1696 nullptr, 0));
1697 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1698 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001699
Lei Zhanga21d5932018-02-05 18:28:38 +00001700 // Check that setting the Down AP to null removes the Down entry but keeps
1701 // Normal intact.
1702 EXPECT_TRUE(
1703 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1704 EXPECT_EQ(73970u,
1705 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1706 nullptr, 0));
1707 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1708 nullptr, 0));
1709 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001710
Lei Zhang75c81712018-02-08 17:22:39 +00001711 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001712}
1713
Lei Zhangab41f252018-12-23 03:10:50 +00001714TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001715 // Open a file with four annotations and load its first page.
1716 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001717 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001718 ASSERT_TRUE(page);
1719
Lei Zhanga21d5932018-02-05 18:28:38 +00001720 {
1721 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001722 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001723 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001724
Lei Zhanga21d5932018-02-05 18:28:38 +00001725 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001726 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001727 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001728 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001729 EXPECT_EQ(73970u,
1730 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1731 nullptr, 0));
1732 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1733 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001734
Lei Zhanga21d5932018-02-05 18:28:38 +00001735 // Check that setting the Normal AP to null removes the whole AP dictionary.
1736 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1737 nullptr));
1738 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1739 nullptr, 0));
1740 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1741 nullptr, 0));
1742 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001743
Lei Zhang75c81712018-02-08 17:22:39 +00001744 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001745}
1746
Lei Zhangab41f252018-12-23 03:10:50 +00001747TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001748 // Open a file with annotations and load its first page.
1749 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001750 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001751 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001752 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001753
Lei Zhanga21d5932018-02-05 18:28:38 +00001754 {
1755 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001756 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001757 ASSERT_TRUE(annot);
1758 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1759 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001760 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001761 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1762 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1763 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001764
Lei Zhanga21d5932018-02-05 18:28:38 +00001765 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001766 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001767 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1768 ASSERT_TRUE(popup);
1769 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1770 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1771 FS_RECTF rect;
1772 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1773 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1774 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001775
Lei Zhanga21d5932018-02-05 18:28:38 +00001776 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1777 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001778 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001779 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1780 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001781
Lei Zhanga21d5932018-02-05 18:28:38 +00001782 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1783 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001784 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001785 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001786 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1787 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001788 }
Jane Liu300bb272017-08-21 14:37:53 -04001789
Jane Liu300bb272017-08-21 14:37:53 -04001790 UnloadPage(page);
1791}
1792
Lei Zhangab41f252018-12-23 03:10:50 +00001793TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001794 // Open file with form text fields.
1795 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001796 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001797 ASSERT_TRUE(page);
1798
Lei Zhanga21d5932018-02-05 18:28:38 +00001799 {
1800 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001801 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001802 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001803
Lei Zhanga21d5932018-02-05 18:28:38 +00001804 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001805 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001806 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001807 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001808 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001809
Lei Zhanga21d5932018-02-05 18:28:38 +00001810 {
1811 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001812 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001813 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001814
Lei Zhanga21d5932018-02-05 18:28:38 +00001815 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001816 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001817 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001818 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1819 }
1820
1821 {
1822 // Retrieve the fourth annotation: user-editable password text field.
1823 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1824 ASSERT_TRUE(annot);
1825
1826 // Check that the flag values are as expected.
1827 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1828 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1829 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001830 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001831
1832 UnloadPage(page);
1833}
1834
Lei Zhangab41f252018-12-23 03:10:50 +00001835TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001836 // Open file with form text fields.
1837 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001838 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001839 ASSERT_TRUE(page);
1840
Lei Zhanga21d5932018-02-05 18:28:38 +00001841 {
1842 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001843 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001844 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001845
Lei Zhanga21d5932018-02-05 18:28:38 +00001846 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001847 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001848 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1849 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1850 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1851 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001852
Lei Zhanga21d5932018-02-05 18:28:38 +00001853 {
1854 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001855 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001856 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001857
Lei Zhanga21d5932018-02-05 18:28:38 +00001858 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001859 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001860 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1861 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1862 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1863 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001864
Lei Zhanga21d5932018-02-05 18:28:38 +00001865 {
1866 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001867 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001868 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001869
Lei Zhanga21d5932018-02-05 18:28:38 +00001870 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001871 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001872 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1873 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1874 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1875 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001876
1877 UnloadPage(page);
1878}
Diana Gage40870db2017-07-19 18:16:03 -07001879
Lei Zhangab41f252018-12-23 03:10:50 +00001880TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001881 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001882 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001883 FPDF_PAGE page = LoadPage(0);
1884 ASSERT_TRUE(page);
1885
1886 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001887 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1888 EXPECT_FALSE(
1889 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001890
Lei Zhang8da98232019-12-11 23:29:33 +00001891 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001892 {
1893 // Verify there is an annotation.
1894 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001895 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001896 EXPECT_TRUE(annot);
1897 }
1898
1899 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001900 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1901 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1902 EXPECT_FALSE(
1903 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001904
1905 UnloadPage(page);
1906}
1907
Lei Zhangab41f252018-12-23 03:10:50 +00001908TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001909 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001910 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001911 FPDF_PAGE page = LoadPage(0);
1912 ASSERT_TRUE(page);
1913
Lei Zhanga21d5932018-02-05 18:28:38 +00001914 {
1915 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001916 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001917 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001918 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001919 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001920
Lei Zhanga21d5932018-02-05 18:28:38 +00001921 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001922 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001923 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1924 }
Diana Gage40870db2017-07-19 18:16:03 -07001925
Lei Zhanga21d5932018-02-05 18:28:38 +00001926 {
1927 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001928 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001929 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001930 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001931 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001932
Lei Zhanga21d5932018-02-05 18:28:38 +00001933 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001934 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001935 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1936 }
Diana Gage40870db2017-07-19 18:16:03 -07001937
1938 UnloadPage(page);
1939}
1940
Lei Zhangab41f252018-12-23 03:10:50 +00001941TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001942 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001943 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001944 FPDF_PAGE page = LoadPage(0);
1945 ASSERT_TRUE(page);
1946
Lei Zhanga21d5932018-02-05 18:28:38 +00001947 {
1948 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001949 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001950 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001951 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001952 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001953
Lei Zhanga21d5932018-02-05 18:28:38 +00001954 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001955 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001956 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1957 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1958 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1959 }
Diana Gage40870db2017-07-19 18:16:03 -07001960
Lei Zhanga21d5932018-02-05 18:28:38 +00001961 {
1962 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001963 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001964 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001965 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001966 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001967
Lei Zhanga21d5932018-02-05 18:28:38 +00001968 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001969 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001970 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1971 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1972 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1973 }
Diana Gage40870db2017-07-19 18:16:03 -07001974
Lei Zhanga21d5932018-02-05 18:28:38 +00001975 {
1976 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001977 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001978 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001979 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001980 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001981
Lei Zhanga21d5932018-02-05 18:28:38 +00001982 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001983 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001984 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1985 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1986 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1987 }
Diana Gage40870db2017-07-19 18:16:03 -07001988
1989 UnloadPage(page);
1990}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001991
Hui Yingst603dcd82020-11-09 22:36:46 +00001992TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001993#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst603dcd82020-11-09 22:36:46 +00001994 static const char kExpectedBitmap[] = "a1ea1ceebb26922fae576cb79ce63af0";
Lei Zhang03e5e682019-09-16 19:45:55 +00001995#else
Lei Zhang992e7e22019-02-04 19:20:58 +00001996 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
Hui Yingst603dcd82020-11-09 22:36:46 +00001997#endif
1998 static constexpr size_t kExpectedSize = 1609;
Lei Zhang992e7e22019-02-04 19:20:58 +00001999
2000 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2001
2002 FPDF_PAGE page = LoadPage(0);
2003 ASSERT_TRUE(page);
2004
2005 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2006 EXPECT_EQ(kExpectedSize, GetString().size());
2007 ClearString();
2008
2009 for (size_t i = 0; i < 10; ++i) {
2010 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2011 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2012
2013 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2014 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2015 // equal, not bigger.
2016 EXPECT_LT(kExpectedSize, GetString().size());
2017 ClearString();
2018 }
2019
2020 UnloadPage(page);
2021}
2022
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002023TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2024 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2025 FPDF_PAGE page = LoadPage(0);
2026 ASSERT_TRUE(page);
2027 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2028
2029 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002030 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002031 static const size_t kBufSize = 12;
2032 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002033
2034 {
2035 // Add a text annotation to the page.
2036 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2037 ASSERT_TRUE(annot);
2038 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2039 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2040
2041 // Make sure there is no test key, add set a value there, and read it back.
2042 std::fill(buf.begin(), buf.end(), 'x');
2043 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002044 kBufSize));
2045 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002046
Lei Zhangf0f67682019-04-08 17:03:21 +00002047 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002048 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2049
2050 std::fill(buf.begin(), buf.end(), 'x');
2051 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002052 kBufSize));
2053 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002054 }
2055
Lei Zhang05ec64c2019-01-09 03:00:06 +00002056 {
2057 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2058 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002059 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002060 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002061 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2062 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2063 // Also do the same test for its appearance string.
2064 std::fill(buf.begin(), buf.end(), 'x');
2065 ASSERT_EQ(2u,
2066 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002067 buf.data(), kBufSize));
2068 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002069
Lei Zhangf0f67682019-04-08 17:03:21 +00002070 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002071 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2072 text.get()));
2073
2074 std::fill(buf.begin(), buf.end(), 'x');
2075 ASSERT_EQ(6u,
2076 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002077 buf.data(), kBufSize));
2078 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002079 }
2080
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002081 UnloadPage(page);
2082
2083 {
2084 // Save a copy, open the copy, and check the annotation again.
2085 // Note that it renders the rotation.
2086 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002087 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002088 FPDF_PAGE saved_page = LoadSavedPage(0);
2089 ASSERT_TRUE(saved_page);
2090
Lei Zhang05ec64c2019-01-09 03:00:06 +00002091 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002092 {
2093 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2094 ASSERT_TRUE(annot);
2095 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2096
2097 std::fill(buf.begin(), buf.end(), 'x');
2098 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002099 kBufSize));
2100 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002101 }
2102
Lei Zhang05ec64c2019-01-09 03:00:06 +00002103 {
2104 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2105 ASSERT_TRUE(annot);
2106 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2107 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2108
2109 std::fill(buf.begin(), buf.end(), 'x');
2110 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002111 kBufSize));
2112 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002113 }
2114
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002115 CloseSavedPage(saved_page);
2116 CloseSavedDocument();
2117 }
2118}
rycsmithcb752f32019-02-21 18:40:53 +00002119
2120TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2121 // Open a file with combobox widget annotations and load its first page.
2122 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2123 FPDF_PAGE page = LoadPage(0);
2124 ASSERT_TRUE(page);
2125
2126 {
2127 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2128 ASSERT_TRUE(annot);
2129
2130 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2131
2132 annot.reset(FPDFPage_GetAnnot(page, 1));
2133 ASSERT_TRUE(annot);
2134
2135 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002136
2137 // Check bad form handle / annot.
2138 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2139 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2140 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002141 }
2142
2143 UnloadPage(page);
2144}
2145
2146TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2147 // Open a file with listbox widget annotations and load its first page.
2148 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2149 FPDF_PAGE page = LoadPage(0);
2150 ASSERT_TRUE(page);
2151
2152 {
2153 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2154 ASSERT_TRUE(annot);
2155
2156 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2157
2158 annot.reset(FPDFPage_GetAnnot(page, 1));
2159 ASSERT_TRUE(annot);
2160
2161 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2162 }
2163
2164 UnloadPage(page);
2165}
2166
2167TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2168 // Open a file with ink annotations and load its first page.
2169 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2170 FPDF_PAGE page = LoadPage(0);
2171 ASSERT_TRUE(page);
2172
2173 {
2174 // annotations do not have "Opt" array and will return -1
2175 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2176 ASSERT_TRUE(annot);
2177
2178 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2179
2180 annot.reset(FPDFPage_GetAnnot(page, 1));
2181 ASSERT_TRUE(annot);
2182
2183 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2184 }
2185
2186 UnloadPage(page);
2187}
2188
2189TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2190 // Open a file with combobox widget annotations and load its first page.
2191 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2192 FPDF_PAGE page = LoadPage(0);
2193 ASSERT_TRUE(page);
2194
2195 {
2196 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2197 ASSERT_TRUE(annot);
2198
2199 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002200 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002201 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002202 ASSERT_EQ(8u, length_bytes);
2203 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002204 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002205 buf.data(), length_bytes));
2206 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002207
2208 annot.reset(FPDFPage_GetAnnot(page, 1));
2209 ASSERT_TRUE(annot);
2210
2211 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002212 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002213 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002214 ASSERT_EQ(12u, length_bytes);
2215 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002216 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002217 buf.data(), length_bytes));
2218 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002219
2220 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002221 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002222 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002223 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002224 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002225 buf.data(), length_bytes));
2226 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002227
Lei Zhange7033c82019-02-26 19:30:49 +00002228 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002229 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2230 nullptr, 0));
2231 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2232 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002233
2234 // Check bad form handle / annot.
2235 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2236 EXPECT_EQ(0u,
2237 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2238 EXPECT_EQ(0u,
2239 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002240 }
2241
2242 UnloadPage(page);
2243}
2244
2245TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2246 // Open a file with listbox widget annotations and load its first page.
2247 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2248 FPDF_PAGE page = LoadPage(0);
2249 ASSERT_TRUE(page);
2250
2251 {
2252 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2253 ASSERT_TRUE(annot);
2254
2255 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002256 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002257 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002258 ASSERT_EQ(8u, length_bytes);
2259 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002260 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002261 buf.data(), length_bytes));
2262 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002263
2264 annot.reset(FPDFPage_GetAnnot(page, 1));
2265 ASSERT_TRUE(annot);
2266
2267 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002268 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002269 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002270 ASSERT_EQ(12u, length_bytes);
2271 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002272 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002273 buf.data(), length_bytes));
2274 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002275
2276 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002277 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002278 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002279 ASSERT_EQ(18u, length_bytes);
2280 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002281 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002282 buf.data(), length_bytes));
2283 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002284
2285 // indices out of range
2286 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2287 nullptr, 0));
2288 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2289 nullptr, 0));
2290 }
2291
2292 UnloadPage(page);
2293}
2294
2295TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2296 // Open a file with ink annotations and load its first page.
2297 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2298 FPDF_PAGE page = LoadPage(0);
2299 ASSERT_TRUE(page);
2300
2301 {
2302 // annotations do not have "Opt" array and will return 0
2303 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2304 ASSERT_TRUE(annot);
2305
2306 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2307 nullptr, 0));
2308
2309 annot.reset(FPDFPage_GetAnnot(page, 1));
2310 ASSERT_TRUE(annot);
2311
2312 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2313 nullptr, 0));
2314 }
2315
2316 UnloadPage(page);
2317}
Ryan Smith09c23b12019-04-25 18:09:06 +00002318
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002319TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2320 // Open a file with combobox widget annotations and load its first page.
2321 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2322 FPDF_PAGE page = LoadPage(0);
2323 ASSERT_TRUE(page);
2324
2325 {
2326 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2327 ASSERT_TRUE(annot);
2328
2329 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2330 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2331 ASSERT_EQ(3, count);
2332 for (int i = 0; i < count; i++) {
2333 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2334 }
2335
2336 annot.reset(FPDFPage_GetAnnot(page, 1));
2337 ASSERT_TRUE(annot);
2338
2339 // Checks for Combobox with Values (/V) object which is just a string.
2340 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2341 ASSERT_EQ(26, count);
2342 for (int i = 0; i < count; i++) {
2343 EXPECT_EQ(i == 1,
2344 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2345 }
2346
2347 // Checks for index outside bound i.e. (index >= CountOption()).
2348 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2349 /*index=*/26));
2350 // Checks for negetive index.
2351 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2352 /*index=*/-1));
2353
2354 // Checks for bad form handle/annot.
2355 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2356 EXPECT_FALSE(
2357 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2358 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2359 }
2360
2361 UnloadPage(page);
2362}
2363
2364TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2365 // Open a file with listbox widget annotations and load its first page.
2366 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2367 FPDF_PAGE page = LoadPage(0);
2368 ASSERT_TRUE(page);
2369
2370 {
2371 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2372 ASSERT_TRUE(annot);
2373
2374 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2375 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2376 ASSERT_EQ(3, count);
2377 for (int i = 0; i < count; i++) {
2378 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2379 }
2380
2381 annot.reset(FPDFPage_GetAnnot(page, 1));
2382 ASSERT_TRUE(annot);
2383
2384 // Checks for Listbox with Values (/V) object which is just a string.
2385 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2386 ASSERT_EQ(26, count);
2387 for (int i = 0; i < count; i++) {
2388 EXPECT_EQ(i == 1,
2389 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2390 }
2391
2392 annot.reset(FPDFPage_GetAnnot(page, 3));
2393 ASSERT_TRUE(annot);
2394
2395 // Checks for Listbox with only Selected indices (/I) object which is an
2396 // array with multiple objects.
2397 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2398 ASSERT_EQ(5, count);
2399 for (int i = 0; i < count; i++) {
2400 bool expected = (i == 1 || i == 3);
2401 EXPECT_EQ(expected,
2402 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2403 }
2404
2405 annot.reset(FPDFPage_GetAnnot(page, 4));
2406 ASSERT_TRUE(annot);
2407
2408 // Checks for Listbox with Values (/V) object which is an array with
2409 // multiple objects.
2410 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2411 ASSERT_EQ(5, count);
2412 for (int i = 0; i < count; i++) {
2413 bool expected = (i == 2 || i == 4);
2414 EXPECT_EQ(expected,
2415 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2416 }
2417
2418 annot.reset(FPDFPage_GetAnnot(page, 5));
2419 ASSERT_TRUE(annot);
2420
2421 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2422 // objects conflict with different lengths.
2423 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2424 ASSERT_EQ(5, count);
2425 for (int i = 0; i < count; i++) {
2426 bool expected = (i == 0 || i == 2);
2427 EXPECT_EQ(expected,
2428 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2429 }
2430 }
2431
2432 UnloadPage(page);
2433}
2434
2435TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2436 // Open a file with multiple form field annotations and load its first page.
2437 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2438 FPDF_PAGE page = LoadPage(0);
2439 ASSERT_TRUE(page);
2440
2441 {
2442 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2443 ASSERT_TRUE(annot);
2444
2445 // Checks for link annotation.
2446 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2447 /*index=*/0));
2448
2449 annot.reset(FPDFPage_GetAnnot(page, 3));
2450 ASSERT_TRUE(annot);
2451
2452 // Checks for text field annotation.
2453 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2454 /*index=*/0));
2455 }
2456
2457 UnloadPage(page);
2458}
2459
Ryan Smith09c23b12019-04-25 18:09:06 +00002460TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2461 // Open a file with combobox annotations and load its first page.
2462 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2463 FPDF_PAGE page = LoadPage(0);
2464 ASSERT_TRUE(page);
2465
2466 {
2467 // All 3 widgets have Tf font size 12.
2468 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2469 ASSERT_TRUE(annot);
2470
2471 float value;
2472 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2473 EXPECT_EQ(12.0, value);
2474
2475 annot.reset(FPDFPage_GetAnnot(page, 1));
2476 ASSERT_TRUE(annot);
2477
2478 float value_two;
2479 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2480 EXPECT_EQ(12.0, value_two);
2481
2482 annot.reset(FPDFPage_GetAnnot(page, 2));
2483 ASSERT_TRUE(annot);
2484
2485 float value_three;
2486 ASSERT_TRUE(
2487 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2488 EXPECT_EQ(12.0, value_three);
2489 }
2490
2491 UnloadPage(page);
2492}
2493
2494TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2495 // Open a file with textfield annotations and load its first page.
2496 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2497 FPDF_PAGE page = LoadPage(0);
2498 ASSERT_TRUE(page);
2499
2500 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002501 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002502 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2503 ASSERT_TRUE(annot);
2504
2505 float value;
2506 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2507 EXPECT_EQ(12.0, value);
2508
2509 annot.reset(FPDFPage_GetAnnot(page, 1));
2510 ASSERT_TRUE(annot);
2511
2512 float value_two;
2513 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2514 EXPECT_EQ(12.0, value_two);
2515
2516 annot.reset(FPDFPage_GetAnnot(page, 2));
2517 ASSERT_TRUE(annot);
2518
2519 float value_three;
2520 ASSERT_TRUE(
2521 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2522 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002523
2524 float value_four;
2525 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2526 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002527 }
2528
2529 UnloadPage(page);
2530}
2531
2532TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2533 // Open a file with ink annotations and load its first page.
2534 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2535 FPDF_PAGE page = LoadPage(0);
2536 ASSERT_TRUE(page);
2537
2538 {
2539 // Annotations that do not have variable text and will return -1.
2540 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2541 ASSERT_TRUE(annot);
2542
2543 float value;
2544 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2545
2546 annot.reset(FPDFPage_GetAnnot(page, 1));
2547 ASSERT_TRUE(annot);
2548
2549 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2550 }
2551
2552 UnloadPage(page);
2553}
2554
2555TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2556 // Open a file with combobox annotations and load its first page.
2557 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2558 FPDF_PAGE page = LoadPage(0);
2559 ASSERT_TRUE(page);
2560
2561 {
2562 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2563 ASSERT_TRUE(annot);
2564
2565 // Check bad form handle / annot.
2566 float value;
2567 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2568 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2569 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2570 }
2571
2572 UnloadPage(page);
2573}
2574
2575TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2576 // Open a file with textfield annotations and load its first page.
2577 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2578 FPDF_PAGE page = LoadPage(0);
2579 ASSERT_TRUE(page);
2580
2581 {
2582 // Obtain the first annotation, a text field with negative font size, -12.
2583 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2584 ASSERT_TRUE(annot);
2585
2586 float value;
2587 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2588 EXPECT_EQ(-12.0, value);
2589 }
2590
2591 UnloadPage(page);
2592}
Ryan Smith23fdf892019-07-17 21:51:26 +00002593
2594TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2595 // Open a file with checkbox and radiobuttons widget annotations and load its
2596 // first page.
2597 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2598 FPDF_PAGE page = LoadPage(0);
2599 ASSERT_TRUE(page);
2600
2601 {
2602 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2603 ASSERT_TRUE(annot);
2604 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2605 }
2606
2607 UnloadPage(page);
2608}
2609
2610TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2611 // Open a file with checkbox and radiobutton widget annotations and load its
2612 // first page.
2613 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2614 FPDF_PAGE page = LoadPage(0);
2615 ASSERT_TRUE(page);
2616
2617 {
2618 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2619 ASSERT_TRUE(annot);
2620 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2621 }
2622
2623 UnloadPage(page);
2624}
2625
2626TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2627 // Open a file with checkbox and radiobutton widget annotations and load its
2628 // first page.
2629 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2630 FPDF_PAGE page = LoadPage(0);
2631 ASSERT_TRUE(page);
2632
2633 {
2634 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2635 ASSERT_TRUE(annot);
2636 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2637
2638 annot.reset(FPDFPage_GetAnnot(page, 6));
2639 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2640
2641 annot.reset(FPDFPage_GetAnnot(page, 7));
2642 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2643 }
2644
2645 UnloadPage(page);
2646}
2647
2648TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2649 // Open a file with checkbox and radiobutton widget annotations and load its
2650 // first page.
2651 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2652 FPDF_PAGE page = LoadPage(0);
2653 ASSERT_TRUE(page);
2654
2655 {
2656 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2657 ASSERT_TRUE(annot);
2658 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2659
2660 annot.reset(FPDFPage_GetAnnot(page, 3));
2661 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2662
2663 annot.reset(FPDFPage_GetAnnot(page, 4));
2664 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2665 }
2666
2667 UnloadPage(page);
2668}
2669
2670TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2671 // Open a file with checkbox and radiobuttons widget annotations and load its
2672 // first page.
2673 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2674 FPDF_PAGE page = LoadPage(0);
2675 ASSERT_TRUE(page);
2676
2677 {
2678 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2679 ASSERT_TRUE(annot);
2680 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2681 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2682 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2683 }
2684
2685 UnloadPage(page);
2686}
2687
2688TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2689 // Open a file with text widget annotations and load its first page.
2690 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2691 FPDF_PAGE page = LoadPage(0);
2692 ASSERT_TRUE(page);
2693
2694 {
2695 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2696 ASSERT_TRUE(annot);
2697 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2698 }
2699
2700 UnloadPage(page);
2701}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002702
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002703TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2704 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002705 FPDF_PAGE page = LoadPage(0);
2706 ASSERT_TRUE(page);
2707
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002708 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2709
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002710 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002711 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002712 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002713 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002714 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002715
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002716 constexpr int kExpectedAnnotTypes[] = {-1,
2717 FPDF_FORMFIELD_COMBOBOX,
2718 FPDF_FORMFIELD_LISTBOX,
2719 FPDF_FORMFIELD_TEXTFIELD,
2720 FPDF_FORMFIELD_CHECKBOX,
2721 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002722
Lei Zhang4501a502020-05-18 16:52:59 +00002723 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002724 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002725 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002726 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002727 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2728 }
2729 UnloadPage(page);
2730}
2731
2732TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2733 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2734 FPDF_PAGE page = LoadPage(0);
2735 ASSERT_TRUE(page);
2736
2737 {
2738 EXPECT_EQ(0u,
2739 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2740
2741 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2742 ASSERT_TRUE(annot);
2743
2744 EXPECT_EQ(0u,
2745 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2746
2747 unsigned long length_bytes =
2748 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2749 ASSERT_EQ(2u, length_bytes);
2750 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2751 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2752 buf.data(), length_bytes));
2753 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2754 }
2755 {
2756 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2757 ASSERT_TRUE(annot);
2758
2759 unsigned long length_bytes =
2760 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2761 ASSERT_EQ(18u, length_bytes);
2762 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2763 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2764 buf.data(), length_bytes));
2765 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2766 }
2767 UnloadPage(page);
2768}
2769
2770TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2771 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2772 FPDF_PAGE page = LoadPage(0);
2773 ASSERT_TRUE(page);
2774
2775 {
2776 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2777 ASSERT_TRUE(annot);
2778
2779 unsigned long length_bytes =
2780 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2781 ASSERT_EQ(2u, length_bytes);
2782 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2783 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2784 buf.data(), length_bytes));
2785 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2786 }
2787 {
2788 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2789 ASSERT_TRUE(annot);
2790
2791 unsigned long length_bytes =
2792 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2793 ASSERT_EQ(14u, length_bytes);
2794 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2795 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2796 buf.data(), length_bytes));
2797 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2798 }
2799 UnloadPage(page);
2800}
2801
2802TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2803 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2804 FPDF_PAGE page = LoadPage(0);
2805 ASSERT_TRUE(page);
2806
2807 {
2808 EXPECT_EQ(0u,
2809 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2810
2811 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2812 ASSERT_TRUE(annot);
2813
2814 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2815
2816 unsigned long length_bytes =
2817 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2818 ASSERT_EQ(18u, length_bytes);
2819 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2820 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2821 buf.data(), length_bytes));
2822 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2823 }
2824 UnloadPage(page);
2825}
2826
2827TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2828 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2829 FPDF_PAGE page = LoadPage(0);
2830 ASSERT_TRUE(page);
2831
2832 {
2833 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2834 ASSERT_TRUE(annot);
2835
2836 unsigned long length_bytes =
2837 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2838 ASSERT_EQ(30u, length_bytes);
2839 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2840 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2841 buf.data(), length_bytes));
2842 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2843 }
2844 UnloadPage(page);
2845}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002846
Lei Zhang9b444002020-04-17 17:35:23 +00002847TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2848 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002849 FPDF_PAGE page = LoadPage(0);
2850 ASSERT_TRUE(page);
2851
Lei Zhang9b444002020-04-17 17:35:23 +00002852 // Verify widgets are by default focusable.
2853 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2854 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002855
Lei Zhang9b444002020-04-17 17:35:23 +00002856 // Expected annot subtypes for page 0 of annots.pdf.
2857 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2858 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2859 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2860 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2861 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002862
Lei Zhang9b444002020-04-17 17:35:23 +00002863 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002864 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002865 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2866 kExpectedAnnotSubtypes,
2867 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002868
Lei Zhang9b444002020-04-17 17:35:23 +00002869 // Make no annotation type focusable using the preferred method.
2870 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2871 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002872
Lei Zhang9b444002020-04-17 17:35:23 +00002873 // Restore the focusable type count back to 1, then set it back to 0 using a
2874 // different method.
2875 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2876 ASSERT_TRUE(
2877 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2878 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002879
Lei Zhang9b444002020-04-17 17:35:23 +00002880 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002881 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002882
Lei Zhang9b444002020-04-17 17:35:23 +00002883 // Now make links focusable.
2884 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2885 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002886
Lei Zhang9b444002020-04-17 17:35:23 +00002887 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002888 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002889 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2890 kExpectedAnnotSubtypes,
2891 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002892
Lei Zhang9b444002020-04-17 17:35:23 +00002893 // Test invalid parameters.
2894 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00002895 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002896 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00002897 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002898 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2899
Lei Zhang9b444002020-04-17 17:35:23 +00002900 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2901 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2902 subtypes.size()));
2903 EXPECT_FALSE(
2904 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2905 EXPECT_FALSE(
2906 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002907
2908 UnloadPage(page);
2909}
Lei Zhang671aece2020-04-14 19:02:26 +00002910
Hui Yingstea3816d2020-07-28 22:35:11 +00002911TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00002912 ASSERT_TRUE(OpenDocument("annots.pdf"));
2913 FPDF_PAGE page = LoadPage(0);
2914 ASSERT_TRUE(page);
2915
2916 {
Hui Yingstea3816d2020-07-28 22:35:11 +00002917#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2918 static const char kMd5sum[] = "4ca14c670396711194b40ecc2514969b";
2919#else
Lei Zhang671aece2020-04-14 19:02:26 +00002920#if defined(OS_WIN)
2921 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
Lei Zhang0c03d632020-07-30 17:05:36 +00002922#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00002923 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
2924#else
2925 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
2926#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00002927#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00002928 // Check the initial rendering.
2929 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2930 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2931 }
2932
2933 // Make links and highlights focusable.
2934 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
2935 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00002936 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00002937 ASSERT_TRUE(
2938 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
2939 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
2940 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
2941 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
2942 subtypes.size()));
2943 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
2944 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
2945
2946 {
Hui Yingstea3816d2020-07-28 22:35:11 +00002947#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2948 static const char kMd5sum[] = "48cb60b3f9bc364c73582aff3418451e";
2949#else
Lei Zhang671aece2020-04-14 19:02:26 +00002950#if defined(OS_WIN)
2951 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
Lei Zhang0c03d632020-07-30 17:05:36 +00002952#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00002953 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
2954#else
2955 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
2956#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00002957#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00002958 // Focus the first link and check the rendering.
2959 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2960 ASSERT_TRUE(annot);
2961 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2962 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2963 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2964 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2965 }
2966
2967 {
Hui Yingstea3816d2020-07-28 22:35:11 +00002968#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2969 static const char kMd5sum[] = "d09869ff0a209daf179da2d7a58142b1";
2970#else
Lei Zhang671aece2020-04-14 19:02:26 +00002971#if defined(OS_WIN)
2972 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
Lei Zhang0c03d632020-07-30 17:05:36 +00002973#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00002974 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
2975#else
2976 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
2977#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00002978#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00002979 // Focus the first highlight and check the rendering.
2980 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2981 ASSERT_TRUE(annot);
2982 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
2983 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2984 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2985 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2986 }
2987
2988 UnloadPage(page);
2989}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00002990
2991TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
2992 ASSERT_TRUE(OpenDocument("annots.pdf"));
2993 FPDF_PAGE page = LoadPage(0);
2994 ASSERT_TRUE(page);
2995 {
2996 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
2997 ASSERT_TRUE(annot);
2998 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2999 FPDF_LINK link_annot = FPDFAnnot_GetLink(annot.get());
3000 ASSERT_TRUE(link_annot);
3001
3002 FPDF_ACTION action = FPDFLink_GetAction(link_annot);
3003 ASSERT_TRUE(action);
3004 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
3005 FPDFAction_GetType(action));
3006
3007 constexpr char kExpectedResult[] =
3008 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3009 "fpdf_text.h";
Lei Zhang4501a502020-05-18 16:52:59 +00003010 constexpr unsigned long kExpectedLength = pdfium::size(kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003011 unsigned long bufsize =
3012 FPDFAction_GetURIPath(document(), action, nullptr, 0);
3013 ASSERT_EQ(kExpectedLength, bufsize);
3014
3015 char buffer[1024];
3016 EXPECT_EQ(bufsize,
3017 FPDFAction_GetURIPath(document(), action, buffer, bufsize));
3018 EXPECT_STREQ(kExpectedResult, buffer);
3019 }
3020
3021 {
3022 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3023 ASSERT_TRUE(annot);
3024 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3025 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3026 }
3027
3028 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3029
3030 UnloadPage(page);
3031}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003032
3033TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3034 // Open a file with radio button widget annotations and load its first page.
3035 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3036 FPDF_PAGE page = LoadPage(0);
3037 ASSERT_TRUE(page);
3038
3039 {
3040 // Checks for bad annot.
3041 EXPECT_EQ(-1,
3042 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3043
3044 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3045 ASSERT_TRUE(annot);
3046
3047 // Checks for bad form handle.
3048 EXPECT_EQ(-1,
3049 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3050
3051 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3052 }
3053
3054 UnloadPage(page);
3055}
3056
3057TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3058 // Open a file with checkbox widget annotations and load its first page.
3059 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3060 FPDF_PAGE page = LoadPage(0);
3061 ASSERT_TRUE(page);
3062
3063 {
3064 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3065 ASSERT_TRUE(annot);
3066 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3067 }
3068
3069 UnloadPage(page);
3070}
3071
3072TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3073 // Open a file with ink annotations and load its first page.
3074 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3075 FPDF_PAGE page = LoadPage(0);
3076 ASSERT_TRUE(page);
3077
3078 {
3079 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3080 ASSERT_TRUE(annot);
3081 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3082 }
3083
3084 UnloadPage(page);
3085}
3086
3087TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3088 // Open a file with radio button widget annotations and load its first page.
3089 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3090 FPDF_PAGE page = LoadPage(0);
3091 ASSERT_TRUE(page);
3092
3093 {
3094 // Checks for bad annot.
3095 EXPECT_EQ(-1,
3096 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3097
3098 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3099 ASSERT_TRUE(annot);
3100
3101 // Checks for bad form handle.
3102 EXPECT_EQ(-1,
3103 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3104
3105 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3106 }
3107
3108 UnloadPage(page);
3109}
3110
3111TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3112 // Open a file with checkbox widget annotations and load its first page.
3113 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3114 FPDF_PAGE page = LoadPage(0);
3115 ASSERT_TRUE(page);
3116
3117 {
3118 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3119 ASSERT_TRUE(annot);
3120 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3121 }
3122
3123 UnloadPage(page);
3124}
3125
3126TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3127 // Open a file with ink annotations and load its first page.
3128 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3129 FPDF_PAGE page = LoadPage(0);
3130 ASSERT_TRUE(page);
3131
3132 {
3133 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3134 ASSERT_TRUE(annot);
3135 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3136 }
3137
3138 UnloadPage(page);
3139}
3140
3141TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3142 // Open a file with radio button widget annotations and load its first page.
3143 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3144 FPDF_PAGE page = LoadPage(0);
3145 ASSERT_TRUE(page);
3146
3147 {
3148 // Checks for bad annot.
3149 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3150 form_handle(), /*annot=*/nullptr,
3151 /*buffer=*/nullptr, /*buflen=*/0));
3152
3153 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3154 ASSERT_TRUE(annot);
3155
3156 // Checks for bad form handle.
3157 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3158 /*hHandle=*/nullptr, annot.get(),
3159 /*buffer=*/nullptr, /*buflen=*/0));
3160
3161 unsigned long length_bytes =
3162 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3163 /*buffer=*/nullptr, /*buflen=*/0);
3164 ASSERT_EQ(14u, length_bytes);
3165 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3166 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3167 buf.data(), length_bytes));
3168 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3169 }
3170
3171 UnloadPage(page);
3172}
3173
3174TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3175 // Open a file with checkbox widget annotations and load its first page.
3176 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3177 FPDF_PAGE page = LoadPage(0);
3178 ASSERT_TRUE(page);
3179
3180 {
3181 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3182 ASSERT_TRUE(annot);
3183
3184 unsigned long length_bytes =
3185 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3186 /*buffer=*/nullptr, /*buflen=*/0);
3187 ASSERT_EQ(8u, length_bytes);
3188 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3189 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3190 buf.data(), length_bytes));
3191 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3192 }
3193
3194 UnloadPage(page);
3195}
3196
3197TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3198 // Open a file with ink annotations and load its first page.
3199 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3200 FPDF_PAGE page = LoadPage(0);
3201 ASSERT_TRUE(page);
3202
3203 {
3204 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3205 ASSERT_TRUE(annot);
3206 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3207 /*buffer=*/nullptr,
3208 /*buflen=*/0));
3209 }
3210
3211 UnloadPage(page);
3212}
Miklos Vajnad1a24fb2020-10-26 18:07:13 +00003213
3214TEST_F(FPDFAnnotEmbedderTest, Redactannotation) {
3215 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3216 FPDF_PAGE page = LoadPage(0);
3217 ASSERT_TRUE(page);
3218 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3219
3220 {
3221 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3222 ASSERT_TRUE(annot);
3223 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3224 }
3225
3226 UnloadPage(page);
3227}
Miklos Vajna09ecef62020-11-10 21:50:38 +00003228
3229TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) {
3230 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3231 FPDF_PAGE page = LoadPage(0);
3232 ASSERT_TRUE(page);
3233 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3234
3235 {
3236 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3237 ASSERT_TRUE(annot);
3238
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003239 // FPDFAnnot_GetVertices() positive testing.
Miklos Vajna09ecef62020-11-10 21:50:38 +00003240 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3241 const size_t kExpectedSize = 3;
3242 ASSERT_EQ(kExpectedSize, size);
3243 std::vector<FS_POINTF> vertices_buffer(size);
3244 EXPECT_EQ(size,
3245 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3246 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3247 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3248 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3249 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3250 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3251 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3252
3253 // FPDFAnnot_GetVertices() negative testing.
3254 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3255
3256 // vertices_buffer is not overwritten if it is too small.
3257 vertices_buffer.resize(1);
3258 vertices_buffer[0].x = 42;
3259 vertices_buffer[0].y = 43;
3260 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3261 vertices_buffer.size());
3262 EXPECT_EQ(kExpectedSize, size);
3263 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3264 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3265 }
3266
3267 {
3268 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3269 ASSERT_TRUE(annot);
3270
3271 // This has an odd number of elements in the vertices array, ignore the last
3272 // element.
3273 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3274 const size_t kExpectedSize = 3;
3275 ASSERT_EQ(kExpectedSize, size);
3276 std::vector<FS_POINTF> vertices_buffer(size);
3277 EXPECT_EQ(size,
3278 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3279 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3280 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3281 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3282 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3283 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3284 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3285 }
3286
3287 {
3288 // Wrong annotation type.
3289 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3290 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3291 }
3292
3293 UnloadPage(page);
3294}
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003295
3296TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) {
3297 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3298 FPDF_PAGE page = LoadPage(0);
3299 ASSERT_TRUE(page);
3300 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3301
3302 {
3303 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3304 ASSERT_TRUE(annot);
3305
3306 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3307 // testing.
3308 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3309 const size_t kExpectedSize = 1;
3310 ASSERT_EQ(kExpectedSize, size);
3311 const unsigned long kPathIndex = 0;
3312 unsigned long path_size =
3313 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3314 const size_t kExpectedPathSize = 3;
3315 ASSERT_EQ(kExpectedPathSize, path_size);
3316 std::vector<FS_POINTF> path_buffer(path_size);
3317 EXPECT_EQ(path_size,
3318 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3319 path_buffer.data(), path_size));
3320 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3321 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3322 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3323 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3324 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3325 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3326
3327 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3328 // testing.
3329 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3330 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3331
3332 // out of bounds path_index.
3333 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3334
3335 // path_buffer is not overwritten if it is too small.
3336 path_buffer.resize(1);
3337 path_buffer[0].x = 42;
3338 path_buffer[0].y = 43;
3339 path_size = FPDFAnnot_GetInkListPath(
3340 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3341 EXPECT_EQ(kExpectedSize, size);
3342 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3343 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3344 }
3345
3346 {
3347 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3348 ASSERT_TRUE(annot);
3349
3350 // This has an odd number of elements in the path array, ignore the last
3351 // element.
3352 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3353 const size_t kExpectedSize = 1;
3354 ASSERT_EQ(kExpectedSize, size);
3355 const unsigned long kPathIndex = 0;
3356 unsigned long path_size =
3357 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3358 const size_t kExpectedPathSize = 3;
3359 ASSERT_EQ(kExpectedPathSize, path_size);
3360 std::vector<FS_POINTF> path_buffer(path_size);
3361 EXPECT_EQ(path_size,
3362 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3363 path_buffer.data(), path_size));
3364 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3365 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3366 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3367 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3368 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3369 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3370 }
3371
3372 {
3373 // Wrong annotation type.
3374 ScopedFPDFAnnotation polygon_annot(
3375 FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON));
3376 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3377 const unsigned long kPathIndex = 0;
3378 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3379 nullptr, 0));
3380 }
3381
3382 UnloadPage(page);
3383}
Miklos Vajna30f45a62020-12-04 19:12:31 +00003384
3385TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) {
3386 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3387 FPDF_PAGE page = LoadPage(0);
3388 ASSERT_TRUE(page);
3389 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3390
3391 {
3392 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3393 ASSERT_TRUE(annot);
3394
3395 // FPDFAnnot_GetVertices() positive testing.
3396 FS_POINTF start;
3397 FS_POINTF end;
3398 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3399 EXPECT_FLOAT_EQ(159.0f, start.x);
3400 EXPECT_FLOAT_EQ(296.0f, start.y);
3401 EXPECT_FLOAT_EQ(472.0f, end.x);
3402 EXPECT_FLOAT_EQ(243.42f, end.y);
3403
3404 // FPDFAnnot_GetVertices() negative testing.
3405 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3406 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3407 }
3408
3409 {
3410 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3411 ASSERT_TRUE(annot);
3412
3413 // Too few elements in the line array.
3414 FS_POINTF start;
3415 FS_POINTF end;
3416 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3417 }
3418
3419 {
3420 // Wrong annotation type.
3421 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3422 FS_POINTF start;
3423 FS_POINTF end;
3424 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3425 }
3426
3427 UnloadPage(page);
3428}
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003429
3430TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) {
3431 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3432 FPDF_PAGE page = LoadPage(0);
3433 ASSERT_TRUE(page);
3434 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3435
3436 {
3437 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3438 ASSERT_TRUE(annot);
3439
3440 // FPDFAnnot_GetBorder() positive testing.
3441 float horizontal_radius;
3442 float vertical_radius;
3443 float border_width;
3444 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3445 &vertical_radius, &border_width));
3446 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3447 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3448 EXPECT_FLOAT_EQ(2.0f, border_width);
3449
3450 // FPDFAnnot_GetBorder() negative testing.
3451 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3452 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3453 }
3454
3455 {
3456 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3457 ASSERT_TRUE(annot);
3458
3459 // Too few elements in the border array.
3460 float horizontal_radius;
3461 float vertical_radius;
3462 float border_width;
3463 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3464 &vertical_radius, &border_width));
Lei Zhang21ea6652021-04-15 23:24:46 +00003465
3466 // FPDFAnnot_SetBorder() positive testing.
3467 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3468 /*vertical_radius=*/3.5f,
3469 /*border_width=*/4.0f));
3470
3471 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3472 &vertical_radius, &border_width));
3473 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3474 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3475 EXPECT_FLOAT_EQ(4.0f, border_width);
3476
3477 // FPDFAnnot_SetBorder() negative testing.
3478 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3479 /*vertical_radius=*/2.5f,
3480 /*border_width=*/3.0f));
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003481 }
3482
3483 UnloadPage(page);
3484}