blob: 455ab0b1d2deba42522e4151e15a500f17d69fd3 [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 Yingst9b6b1542020-07-27 16:11:12 +0000529#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang0c03d632020-07-30 17:05:36 +0000530#if defined(OS_APPLE)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000531 static constexpr char kExpectedHash[] = "acddfe688a117ead56af7b249a2cf8a1";
532#else
533 static constexpr char kExpectedHash[] = "1fb0dd8dd5f0b9bb8d076e48eb59296d";
Lei Zhang0c03d632020-07-30 17:05:36 +0000534#endif // defined(OS_APPLE)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000535#else
Lei Zhang430b5322020-07-06 22:23:36 +0000536#if defined(OS_WIN)
537 static constexpr char kExpectedHash[] = "49d0a81c636531a337429325273d0508";
538#else
539 static constexpr char kExpectedHash[] = "354002e1c4386d38fdde29ef8d61074a";
Hui Yingst9b6b1542020-07-27 16:11:12 +0000540#endif // defined(OS_WIN)
541#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Tom Sepezef43c262018-11-07 16:41:32 +0000542 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang430b5322020-07-06 22:23:36 +0000543 CompareBitmap(bitmap.get(), 612, 792, kExpectedHash);
Tom Sepezef43c262018-11-07 16:41:32 +0000544 }
Tom Sepez507d0192018-11-07 16:37:51 +0000545 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400546}
Jane Liu20eafda2017-06-07 10:33:24 -0400547
Lei Zhangab41f252018-12-23 03:10:50 +0000548TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400549 // Open a file with one annotation and load its first page.
550 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000551 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400552 ASSERT_TRUE(page);
553
554 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400555 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400556
557 UnloadPage(page);
558}
559
Lei Zhangab41f252018-12-23 03:10:50 +0000560TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400561 // Open a file with no annotation and load its first page.
562 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000563 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400564 ASSERT_TRUE(page);
565 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
566
Lei Zhanga21d5932018-02-05 18:28:38 +0000567 {
568 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000569 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000570 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400571
Lei Zhanga21d5932018-02-05 18:28:38 +0000572 // Check that there is now 1 annotations on this page.
573 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400574
Lei Zhanga21d5932018-02-05 18:28:38 +0000575 // Check that the subtype of the annotation is correct.
576 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
577 }
Jane Liue10509a2017-06-20 16:47:41 -0400578
Lei Zhanga21d5932018-02-05 18:28:38 +0000579 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000580 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000581 ASSERT_TRUE(annot);
582 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400583
Lei Zhanga21d5932018-02-05 18:28:38 +0000584 // Set the color of the annotation.
585 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
586 102, 153, 204));
587 // Check that the color has been set correctly.
588 unsigned int R;
589 unsigned int G;
590 unsigned int B;
591 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000592 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000593 &G, &B, &A));
594 EXPECT_EQ(51u, R);
595 EXPECT_EQ(102u, G);
596 EXPECT_EQ(153u, B);
597 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400598
Lei Zhanga21d5932018-02-05 18:28:38 +0000599 // Change the color of the annotation.
600 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
601 153, 102, 51));
602 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000603 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000604 &G, &B, &A));
605 EXPECT_EQ(204u, R);
606 EXPECT_EQ(153u, G);
607 EXPECT_EQ(102u, B);
608 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400609
Lei Zhanga21d5932018-02-05 18:28:38 +0000610 // Set the annotation rectangle.
611 FS_RECTF rect;
612 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
613 EXPECT_EQ(0.f, rect.left);
614 EXPECT_EQ(0.f, rect.right);
615 rect.left = 35;
616 rect.bottom = 150;
617 rect.right = 53;
618 rect.top = 165;
619 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
620 // Check that the annotation rectangle has been set correctly.
621 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
622 EXPECT_EQ(35.f, rect.left);
623 EXPECT_EQ(150.f, rect.bottom);
624 EXPECT_EQ(53.f, rect.right);
625 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400626
Lei Zhanga21d5932018-02-05 18:28:38 +0000627 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000628 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000629 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000630 ASSERT_TRUE(FPDFAnnot_SetStringValue(
631 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000632 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000633 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000634 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000635 ASSERT_EQ(74u, length_bytes);
636 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
637 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
638 pdfium::annotation::kContents,
639 buf.data(), length_bytes));
640 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000641 }
Jane Liu20eafda2017-06-07 10:33:24 -0400642 UnloadPage(page);
643}
644
Hui Yingstb3490322020-07-22 00:53:29 +0000645TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400646 // Open a file with one annotation and load its first page.
647 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000648 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400649 ASSERT_TRUE(page);
650
651 // Check that there is a total of one annotation on its first page, and verify
652 // its quadpoints.
653 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400654 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000655 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000656 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000657 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000658 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000659 EXPECT_EQ(115.802643f, quadpoints.x1);
660 EXPECT_EQ(718.913940f, quadpoints.y1);
661 EXPECT_EQ(157.211182f, quadpoints.x4);
662 EXPECT_EQ(706.264465f, quadpoints.y4);
663 }
Jane Liu20eafda2017-06-07 10:33:24 -0400664
665 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000666 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000667 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000668 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
669 ASSERT_TRUE(annot);
670 quadpoints.x1 = 140.802643f;
671 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000672 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000673 }
Jane Liu20eafda2017-06-07 10:33:24 -0400674
675 // Save the document, closing the page and document.
676 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000677 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400678
679 // Open the saved document.
Hui Yingstb3490322020-07-22 00:53:29 +0000680#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
681 static const char kChecksum[] = "798fa41303381c9ba6d99092f5cd4d2b";
682#else
683 static const char kChecksum[] = "dba153419f67b7c0c0e3d22d3e8910d5";
684#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400685
Lei Zhang0b494052019-01-31 21:41:15 +0000686 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000687 page = LoadSavedPage(0);
Hui Yingstb3490322020-07-22 00:53:29 +0000688 VerifySavedRendering(page, 612, 792, kChecksum);
Jane Liu20eafda2017-06-07 10:33:24 -0400689
690 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000691 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400692
Lei Zhanga21d5932018-02-05 18:28:38 +0000693 {
694 // Check that the second annotation is an underline annotation and verify
695 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000696 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000697 ASSERT_TRUE(new_annot);
698 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
699 FS_QUADPOINTSF new_quadpoints;
700 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000701 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000702 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
703 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
704 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
705 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
706 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400707
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000708 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400709 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400710}
Jane Liu06462752017-06-27 16:41:14 -0400711
Lei Zhangab41f252018-12-23 03:10:50 +0000712TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000713 // Open a file with four annotations and load its first page.
714 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
715 FPDF_PAGE page = LoadPage(0);
716 ASSERT_TRUE(page);
717 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
718
719 // Retrieve the highlight annotation.
720 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
721 ASSERT_TRUE(annot);
722 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
723
724 FS_QUADPOINTSF quadpoints;
725 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
726
727 {
728 // Verify the current one set of quadpoints.
729 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
730
731 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
732 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
733 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
734 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
735 }
736
737 {
738 // Update the quadpoints.
739 FS_QUADPOINTSF new_quadpoints = quadpoints;
740 new_quadpoints.y1 -= 20.f;
741 new_quadpoints.y2 -= 20.f;
742 new_quadpoints.y3 -= 20.f;
743 new_quadpoints.y4 -= 20.f;
744 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
745
746 // Verify added quadpoint set
747 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
748 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
749 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
750 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
751 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
752 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
753 }
754
755 {
756 // Append a new set of quadpoints.
757 FS_QUADPOINTSF new_quadpoints = quadpoints;
758 new_quadpoints.y1 += 20.f;
759 new_quadpoints.y2 += 20.f;
760 new_quadpoints.y3 += 20.f;
761 new_quadpoints.y4 += 20.f;
762 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
763
764 // Verify added quadpoint set
765 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
766 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
767 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
768 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
769 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
770 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
771 }
772
773 {
774 // Setting and getting quadpoints at out-of-bound index should fail
775 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
776 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
777 }
778
779 FPDFPage_CloseAnnot(annot);
780
781 // Retrieve the square annotation
782 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
783
784 {
785 // Check that attempting to set its quadpoints would fail
786 ASSERT_TRUE(squareAnnot);
787 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
788 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
789 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
790 }
791
792 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000793 UnloadPage(page);
794}
795
Hui Yingstb64cd122020-07-27 16:01:02 +0000796// TODO(crbug.com/pdfium/1569): Fix this issue and enable the test for Skia.
797#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +0000798#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
799#else
800#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
801#endif
802TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Hui Yingstb64cd122020-07-27 16:01:02 +0000803#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
804 static const char kMd5Original[] = "00e70eb543c2a6e8f8aafb4ee951d9bf";
Lei Zhang4f556b82019-04-08 16:32:41 +0000805 static const char kMd5ModifiedHighlight[] =
Hui Yingstb64cd122020-07-27 16:01:02 +0000806 "7638c4a8fe4aabbf8704e198f49b3198";
807 static const char kMd5ModifiedSquare[] = "54f507af6af63de877b9cafdab1bbdaa";
808#else
809#if defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000810 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
811 static const char kMd5ModifiedHighlight[] =
812 "66f3caef3a7d488a4fa1ad37fc06310e";
813 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Lei Zhang0c03d632020-07-30 17:05:36 +0000814#elif defined(OS_APPLE)
Hui Yingstb64cd122020-07-27 16:01:02 +0000815 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
816 static const char kMd5ModifiedHighlight[] =
817 "e64bf648f6e9354d1f3eedb47a2c9498";
818 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Jane Liub370e5a2017-08-16 13:24:58 -0400819#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000820 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
821 static const char kMd5ModifiedHighlight[] =
822 "66f3caef3a7d488a4fa1ad37fc06310e";
823 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400824#endif
Hui Yingstb64cd122020-07-27 16:01:02 +0000825#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liub370e5a2017-08-16 13:24:58 -0400826
Jane Liu06462752017-06-27 16:41:14 -0400827 // Open a file with four annotations and load its first page.
828 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000829 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400830 ASSERT_TRUE(page);
831 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
832
Jane Liub370e5a2017-08-16 13:24:58 -0400833 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000834 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000835 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000836 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000837 }
Jane Liub370e5a2017-08-16 13:24:58 -0400838
Jane Liu0c6b07d2017-08-15 10:50:22 -0400839 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400840 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000841
842 // Retrieve the highlight annotation which has its AP stream already defined.
843 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000844 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000845 ASSERT_TRUE(annot);
846 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
847
848 // Check that color cannot be set when an AP stream is defined already.
849 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
850 102, 153, 204));
851
852 // Verify its attachment points.
853 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000854 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000855 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
856 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
857 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
858 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
859
860 // Check that updating the attachment points would succeed.
861 quadpoints.x1 -= 50.f;
862 quadpoints.x2 -= 50.f;
863 quadpoints.x3 -= 50.f;
864 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000865 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000866 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000867 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000868 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
869 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
870 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
871 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
872
873 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000874 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000875 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000876 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000877 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000878
879 // Verify its annotation rectangle.
880 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
881 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
882 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
883 EXPECT_NEAR(136.325f, rect.right, 0.001f);
884 EXPECT_NEAR(721.292f, rect.top, 0.001f);
885
886 // Check that updating the rectangle would succeed.
887 rect.left -= 60.f;
888 rect.right -= 60.f;
889 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
890 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
891 EXPECT_EQ(rect.right, new_rect.right);
892 }
Jane Liu06462752017-06-27 16:41:14 -0400893
Jane Liub370e5a2017-08-16 13:24:58 -0400894 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000895 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000896 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000897 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000898 }
Jane Liub370e5a2017-08-16 13:24:58 -0400899
Lei Zhanga21d5932018-02-05 18:28:38 +0000900 {
901 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000902 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000903 ASSERT_TRUE(annot);
904 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400905
Lei Zhanga21d5932018-02-05 18:28:38 +0000906 // Check that updating the rectangle would succeed.
907 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
908 rect.left += 70.f;
909 rect.right += 70.f;
910 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
911 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
912 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400913
Lei Zhanga21d5932018-02-05 18:28:38 +0000914 // Check that updating the rectangle changes the square annotation's
915 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000916 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000917 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000918 }
Jane Liub370e5a2017-08-16 13:24:58 -0400919
Jane Liu06462752017-06-27 16:41:14 -0400920 UnloadPage(page);
921}
Jane Liu8ce58f52017-06-29 13:40:22 -0400922
Lei Zhangab41f252018-12-23 03:10:50 +0000923TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000924 // Open a file with multiline markup annotations.
925 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
926 FPDF_PAGE page = LoadPage(0);
927 ASSERT_TRUE(page);
928 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000929 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000930 ASSERT_TRUE(annot);
931
932 // This is a three line annotation.
933 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
934 }
935 UnloadPage(page);
936
937 // null annotation should return 0
938 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
939}
940
Lei Zhangab41f252018-12-23 03:10:50 +0000941TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400942 // Open a file with 3 annotations on its first page.
943 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000944 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400945 ASSERT_TRUE(page);
946 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
947
Jane Liu0c6b07d2017-08-15 10:50:22 -0400948 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400949
Lei Zhanga21d5932018-02-05 18:28:38 +0000950 // Check that the annotations have the expected rectangle coordinates.
951 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000952 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000953 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
954 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
955 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400956
Lei Zhanga21d5932018-02-05 18:28:38 +0000957 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000958 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000959 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
960 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
961 }
962
963 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000964 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000965 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
966 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
967 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400968
969 // Check that nothing happens when attempting to remove an annotation with an
970 // out-of-bound index.
971 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
972 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
973 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
974
975 // Remove the second annotation.
976 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
977 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
978 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
979
980 // Save the document, closing the page and document.
981 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000982 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400983
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400984 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400985 // Open the saved document.
986 std::string new_file = GetString();
987 FPDF_FILEACCESS file_access;
988 memset(&file_access, 0, sizeof(file_access));
989 file_access.m_FileLen = new_file.size();
990 file_access.m_GetBlock = GetBlockFromString;
991 file_access.m_Param = &new_file;
992 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
993 ASSERT_TRUE(new_doc);
994 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
995 ASSERT_TRUE(new_page);
996
997 // Check that the saved document has 2 annotations on the first page.
998 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
999
Lei Zhanga21d5932018-02-05 18:28:38 +00001000 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1001 // by verifying their rectangle coordinates.
1002 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001003 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001004 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1005 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1006 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001007
Lei Zhanga21d5932018-02-05 18:28:38 +00001008 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001009 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001010 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1011 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1012 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001013 FPDF_ClosePage(new_page);
1014 FPDF_CloseDocument(new_doc);
1015}
Jane Liu8ce58f52017-06-29 13:40:22 -04001016
Hui Yingst4a21df02020-05-13 03:15:44 +00001017TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001018#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001019#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst4a21df02020-05-13 03:15:44 +00001020 static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa";
1021 static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622";
1022 static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001023#else
Hui Yingst4a21df02020-05-13 03:15:44 +00001024 static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4";
1025 static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f";
1026 static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001027#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst4a21df02020-05-13 03:15:44 +00001028#else
1029#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001030 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
1031 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
1032 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Lei Zhang0c03d632020-07-30 17:05:36 +00001033#elif defined(OS_APPLE)
Hui Yingst4a21df02020-05-13 03:15:44 +00001034 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
1035 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
1036 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Jane Liubaa7ff42017-06-29 19:18:23 -04001037#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001038 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
1039 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
1040 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -04001041#endif
Hui Yingst4a21df02020-05-13 03:15:44 +00001042#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liubaa7ff42017-06-29 19:18:23 -04001043
1044 // Open a file with two annotations and load its first page.
1045 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001046 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001047 ASSERT_TRUE(page);
1048 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1049
1050 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001051 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001052 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001053 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001054 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001055
Lei Zhanga21d5932018-02-05 18:28:38 +00001056 {
1057 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001058 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001059 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001060
Lei Zhanga21d5932018-02-05 18:28:38 +00001061 // Check that this annotation has one path object and retrieve it.
1062 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001063 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001064 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1065 EXPECT_FALSE(path);
1066 path = FPDFAnnot_GetObject(annot.get(), 0);
1067 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1068 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001069
Lei Zhanga21d5932018-02-05 18:28:38 +00001070 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001071 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001072 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001073
Lei Zhanga21d5932018-02-05 18:28:38 +00001074 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001075 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001076 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001077 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001078 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001079
Lei Zhanga21d5932018-02-05 18:28:38 +00001080 // Add a second path object to the same annotation.
1081 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1082 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001083 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1084 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001085 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1086 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1087 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001088
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001089 // The object is in the annontation, not in the page, so the page object
1090 // array should not change.
1091 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1092
Lei Zhanga21d5932018-02-05 18:28:38 +00001093 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001094 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001095 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001096 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001097 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001098
Lei Zhanga21d5932018-02-05 18:28:38 +00001099 // Delete the newly added path object.
1100 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1101 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001102 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001103 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001104
1105 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001106 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001107 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001108 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001109 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001110
Jane Liubaa7ff42017-06-29 19:18:23 -04001111 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001112
Lei Zhanga21d5932018-02-05 18:28:38 +00001113 {
1114 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001115 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001116 ASSERT_TRUE(annot);
1117 rect.left = 200.f;
1118 rect.bottom = 400.f;
1119 rect.right = 500.f;
1120 rect.top = 600.f;
1121 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001122
Lei Zhanga21d5932018-02-05 18:28:38 +00001123 // Add a new path to the annotation.
1124 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1125 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1126 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1127 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1128 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001129 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1130 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001131 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1132 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1133 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1134
1135 // Check that the annotation's bounding box came from its rectangle.
1136 FS_RECTF new_rect;
1137 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1138 EXPECT_EQ(rect.left, new_rect.left);
1139 EXPECT_EQ(rect.bottom, new_rect.bottom);
1140 EXPECT_EQ(rect.right, new_rect.right);
1141 EXPECT_EQ(rect.top, new_rect.top);
1142 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001143
1144 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -04001145 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001146 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001147
1148 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001149 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001150 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001151 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001152
Jane Liu36567742017-07-06 11:13:35 -04001153 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001154 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001155
Lei Zhanga21d5932018-02-05 18:28:38 +00001156 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001157 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001158 ASSERT_TRUE(annot);
1159 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001160
Lei Zhanga21d5932018-02-05 18:28:38 +00001161 // Check that the new annotation's rectangle is as defined.
1162 FS_RECTF new_rect;
1163 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1164 EXPECT_EQ(rect.left, new_rect.left);
1165 EXPECT_EQ(rect.bottom, new_rect.bottom);
1166 EXPECT_EQ(rect.right, new_rect.right);
1167 EXPECT_EQ(rect.top, new_rect.top);
1168 }
1169
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001170 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001171 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001172}
Jane Liub137e752017-07-05 15:04:33 -04001173
Lei Zhangab41f252018-12-23 03:10:50 +00001174TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001175 // Open a file with an annotation and load its first page.
1176 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001177 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001178 ASSERT_TRUE(page);
1179
1180 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001181 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001182 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001183 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1184 }
Jane Liub137e752017-07-05 15:04:33 -04001185
Lei Zhanga21d5932018-02-05 18:28:38 +00001186 {
1187 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001188 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001189 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001190
Lei Zhanga21d5932018-02-05 18:28:38 +00001191 // Check that the original flag values are as expected.
1192 int flags = FPDFAnnot_GetFlags(annot.get());
1193 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1194 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001195
Lei Zhanga21d5932018-02-05 18:28:38 +00001196 // Set the HIDDEN flag.
1197 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1198 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1199 flags = FPDFAnnot_GetFlags(annot.get());
1200 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1201 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001202
Lei Zhanga21d5932018-02-05 18:28:38 +00001203 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001204 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001205 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001206 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001207 }
Jane Liub137e752017-07-05 15:04:33 -04001208
Lei Zhanga21d5932018-02-05 18:28:38 +00001209 // Unset the HIDDEN flag.
1210 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1211 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1212 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1213 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1214 flags = FPDFAnnot_GetFlags(annot.get());
1215 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1216 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001217
Lei Zhanga21d5932018-02-05 18:28:38 +00001218 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001219 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001220 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001221 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1222 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001223 }
Jane Liub137e752017-07-05 15:04:33 -04001224
Jane Liub137e752017-07-05 15:04:33 -04001225 UnloadPage(page);
1226}
Jane Liu36567742017-07-06 11:13:35 -04001227
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001228// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1229#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001230#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1231#else
1232#define MAYBE_AddAndModifyImage AddAndModifyImage
1233#endif
1234TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001235#if defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001236#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001237 static const char kMd5NewImage[] = "26a8eb30937226a677839379e0d7ae1a";
1238 static const char kMd5ModifiedImage[] = "2985114b32ba1a96be78ee643fe31aa5";
1239#else
1240 static const char kMd5NewImage[] = "14012ab500b4671fa73dd760129a8a93";
1241 static const char kMd5ModifiedImage[] = "5f97f98f58ed04dc393f31460485f1a2";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001242#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001243#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001244#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001245 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
1246 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +00001247#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001248 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
1249 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -04001250#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001251 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
1252 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -04001253#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001254#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001255
1256 // Open a file with two annotations and load its first page.
1257 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001258 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001259 ASSERT_TRUE(page);
1260 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1261
1262 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001263 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001264 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001265 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001266 }
Jane Liu36567742017-07-06 11:13:35 -04001267
Jane Liu36567742017-07-06 11:13:35 -04001268 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001269 FPDF_BITMAP image_bitmap;
1270
1271 {
1272 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001273 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001274 ASSERT_TRUE(annot);
1275 FS_RECTF rect;
1276 rect.left = 200.f;
1277 rect.bottom = 600.f;
1278 rect.right = 400.f;
1279 rect.top = 800.f;
1280 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1281
1282 // Add a solid-color translucent image object to the new annotation.
1283 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1284 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1285 0xeeeecccc);
1286 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1287 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1288 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1289 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1290 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1291 kBitmapSize, 0, 0));
1292 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1293 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1294 }
Jane Liu36567742017-07-06 11:13:35 -04001295
1296 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001297 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001298 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001299 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001300 }
Jane Liu36567742017-07-06 11:13:35 -04001301
Lei Zhanga21d5932018-02-05 18:28:38 +00001302 {
1303 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001304 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001305 ASSERT_TRUE(annot);
1306 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1307 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1308 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001309
Lei Zhanga21d5932018-02-05 18:28:38 +00001310 // Modify the image in the new annotation.
1311 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1312 0xff000000);
1313 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1314 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1315 }
Jane Liu36567742017-07-06 11:13:35 -04001316
1317 // Save the document, closing the page and document.
1318 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001319 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001320 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001321
1322 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001323 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001324}
1325
Hui Yingst6cd754f2020-05-14 04:05:25 +00001326TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001327#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001328#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001329 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1330 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001331#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001332 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1333 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001334#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001335#else
1336#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001337 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1338 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Lei Zhang0c03d632020-07-30 17:05:36 +00001339#elif defined(OS_APPLE)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001340 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1341 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001342#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001343 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1344 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001345#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001346#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001347
1348 // Open a file with two annotations and load its first page.
1349 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001350 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001351 ASSERT_TRUE(page);
1352 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1353
1354 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001355 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001356 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001357 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001358 }
Jane Liu36567742017-07-06 11:13:35 -04001359
Lei Zhanga21d5932018-02-05 18:28:38 +00001360 {
1361 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001362 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001363 ASSERT_TRUE(annot);
1364 FS_RECTF rect;
1365 rect.left = 200.f;
1366 rect.bottom = 550.f;
1367 rect.right = 450.f;
1368 rect.top = 650.f;
1369 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001370
Lei Zhanga21d5932018-02-05 18:28:38 +00001371 // Add a translucent text object to the new annotation.
1372 FPDF_PAGEOBJECT text_object =
1373 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1374 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001375 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001376 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1377 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001378 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001379 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1380 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1381 }
Jane Liu36567742017-07-06 11:13:35 -04001382
1383 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001384 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001385 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001386 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001387 }
Jane Liu36567742017-07-06 11:13:35 -04001388
Lei Zhanga21d5932018-02-05 18:28:38 +00001389 {
1390 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001391 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001392 ASSERT_TRUE(annot);
1393 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1394 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1395 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001396
Lei Zhanga21d5932018-02-05 18:28:38 +00001397 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001398 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001399 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1400 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1401 }
Jane Liu36567742017-07-06 11:13:35 -04001402
1403 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001404 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001405 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001406 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001407 }
Jane Liu36567742017-07-06 11:13:35 -04001408
1409 // Remove the new annotation, and check that the page renders as before.
1410 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001411 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001412 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001413 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001414 }
Jane Liu36567742017-07-06 11:13:35 -04001415
1416 UnloadPage(page);
1417}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001418
Hui Yingst9b6b1542020-07-27 16:11:12 +00001419TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001420 // Open a file with four annotations and load its first page.
1421 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001422 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001423 ASSERT_TRUE(page);
1424
Lei Zhang4f556b82019-04-08 16:32:41 +00001425 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001426
Lei Zhanga21d5932018-02-05 18:28:38 +00001427 {
1428 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001429 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001430 ASSERT_TRUE(annot);
1431
1432 // Check that a non-existent key does not exist.
1433 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1434
1435 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001436 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001437 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001438 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1439 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1440 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001441
1442 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001443 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001444 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001445 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001446 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001447 ASSERT_EQ(66u, length_bytes);
1448 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1449 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1450 length_bytes));
1451 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1452 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001453
1454 // Check that the string value of the modified date is correct.
1455 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001456 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1457 nullptr, 0);
1458 ASSERT_EQ(44u, length_bytes);
1459 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001460 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001461 buf.data(), length_bytes));
1462 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001463
1464 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001465 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001466 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1467 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001468 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001469
1470 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001471 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001472 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001473
Hui Yingst9b6b1542020-07-27 16:11:12 +00001474#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001475#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001476 static const char kMd5[] = "7a2b712ca88d7b71f125ea3f9c88e57a";
1477#else
1478 static const char kMd5[] = "626d25c5aa5baf67d22d9a0e1c23f6aa";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001479#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001480#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001481#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001482 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001483#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001484 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001485#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001486 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001487#endif
Hui Yingst9b6b1542020-07-27 16:11:12 +00001488#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Dan Sinclair971a6742018-03-28 19:23:25 +00001489
1490 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001491 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001492 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001493 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001494 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001495 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001496
Lei Zhanga21d5932018-02-05 18:28:38 +00001497 // Check that the string value of the modified date is the newly-set value.
1498 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001499 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001500 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001501 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001502 ASSERT_EQ(44u, length_bytes);
1503 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001504 EXPECT_EQ(44u,
1505 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001506 buf.data(), length_bytes));
1507 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001508 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001509
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001510 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001511 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001512}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001513
rycsmith3e785602019-03-05 21:48:36 +00001514TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001515 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001516 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1517 FPDF_PAGE page = LoadPage(0);
1518 ASSERT_TRUE(page);
1519 {
1520 // First two annotations do not have "MaxLen" attribute.
1521 for (int i = 0; i < 2; i++) {
1522 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1523 ASSERT_TRUE(annot);
1524
1525 // Verify that no "MaxLen" key present.
1526 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1527
1528 float value;
1529 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1530 }
1531
1532 // Annotation in index 2 has "MaxLen" of 10.
1533 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1534 ASSERT_TRUE(annot);
1535
1536 // Verify that "MaxLen" key present.
1537 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1538
1539 float value;
1540 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1541 EXPECT_FLOAT_EQ(10.0f, value);
1542
1543 // Check bad inputs.
1544 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1545 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1546 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1547 // Ask for key that exists but is not a number.
1548 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1549 }
1550
1551 UnloadPage(page);
1552}
1553
Lei Zhangab41f252018-12-23 03:10:50 +00001554TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001555 // Open a file with four annotations and load its first page.
1556 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001557 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001558 ASSERT_TRUE(page);
1559
Lei Zhanga21d5932018-02-05 18:28:38 +00001560 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001561 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1562 static constexpr size_t kExpectNormalAPLength = 73970;
1563
Lei Zhanga21d5932018-02-05 18:28:38 +00001564 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001565 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001566 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001567
Lei Zhanga21d5932018-02-05 18:28:38 +00001568 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001569 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001570 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001571 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001572
Lei Zhanga21d5932018-02-05 18:28:38 +00001573 // Check that the string value of an AP is not returned if the buffer is too
1574 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001575 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1576 // Write in the buffer to verify it's not overwritten.
1577 memcpy(buf.data(), "abcdefgh", 8);
1578 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001579 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001580 buf.data(), normal_length_bytes - 1));
1581 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001582
Lei Zhanga21d5932018-02-05 18:28:38 +00001583 // Check that the string value of an AP is returned through a buffer that is
1584 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001585 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001586 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001587 buf.data(), normal_length_bytes));
1588 EXPECT_EQ(kMd5NormalAP,
1589 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1590 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001591
Lei Zhanga21d5932018-02-05 18:28:38 +00001592 // Check that the string value of an AP is returned through a buffer that is
1593 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001594 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1595 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001596 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001597 buf.data(), normal_length_bytes + 2));
1598 EXPECT_EQ(kMd5NormalAP,
1599 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1600 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001601
Lei Zhanga21d5932018-02-05 18:28:38 +00001602 // Check that getting an AP for a mode that does not have an AP returns an
1603 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001604 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001605 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001606 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001607
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001608 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001609 EXPECT_EQ(2u,
1610 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001611 buf.data(), 1000));
1612 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001613
Lei Zhanga21d5932018-02-05 18:28:38 +00001614 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001615 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001616 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001617 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001618 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001619 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001620 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001621
Lei Zhanga21d5932018-02-05 18:28:38 +00001622 // Set the AP correctly now.
1623 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001624 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001625
Lei Zhanga21d5932018-02-05 18:28:38 +00001626 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001627 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001628 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001629 ASSERT_EQ(24u, rollover_length_bytes);
1630 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001631 EXPECT_EQ(24u,
1632 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001633 buf.data(), rollover_length_bytes));
1634 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001635
Lei Zhanga21d5932018-02-05 18:28:38 +00001636 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001637 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1638 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001639 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001640 buf.data(), normal_length_bytes));
1641 EXPECT_EQ(kMd5NormalAP,
1642 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1643 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001644 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001645
1646 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001647 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001648 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001649
Lei Zhang0b494052019-01-31 21:41:15 +00001650 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001651 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001652 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001653 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001654
Lei Zhanga21d5932018-02-05 18:28:38 +00001655 // Check that the new annotation value is equal to the value we set before
1656 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001657 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001658 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001659 ASSERT_EQ(24u, rollover_length_bytes);
1660 std::vector<FPDF_WCHAR> buf =
1661 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001662 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1663 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001664 buf.data(), rollover_length_bytes));
1665 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001666 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001667
1668 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001669 CloseSavedPage(page);
1670 CloseSavedDocument();
1671}
1672
Lei Zhangab41f252018-12-23 03:10:50 +00001673TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001674 // Open a file with four annotations and load its first page.
1675 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001676 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001677 ASSERT_TRUE(page);
1678
Lei Zhanga21d5932018-02-05 18:28:38 +00001679 {
1680 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001681 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001682 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001683
Lei Zhanga21d5932018-02-05 18:28:38 +00001684 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001685 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001686 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001687 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001688 EXPECT_EQ(73970u,
1689 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1690 nullptr, 0));
1691 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1692 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001693
Lei Zhanga21d5932018-02-05 18:28:38 +00001694 // Check that setting the Down AP to null removes the Down entry but keeps
1695 // Normal intact.
1696 EXPECT_TRUE(
1697 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1698 EXPECT_EQ(73970u,
1699 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1700 nullptr, 0));
1701 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1702 nullptr, 0));
1703 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001704
Lei Zhang75c81712018-02-08 17:22:39 +00001705 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001706}
1707
Lei Zhangab41f252018-12-23 03:10:50 +00001708TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001709 // Open a file with four annotations and load its first page.
1710 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001711 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001712 ASSERT_TRUE(page);
1713
Lei Zhanga21d5932018-02-05 18:28:38 +00001714 {
1715 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001716 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001717 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001718
Lei Zhanga21d5932018-02-05 18:28:38 +00001719 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001720 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001721 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001722 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001723 EXPECT_EQ(73970u,
1724 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1725 nullptr, 0));
1726 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1727 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001728
Lei Zhanga21d5932018-02-05 18:28:38 +00001729 // Check that setting the Normal AP to null removes the whole AP dictionary.
1730 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1731 nullptr));
1732 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1733 nullptr, 0));
1734 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1735 nullptr, 0));
1736 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001737
Lei Zhang75c81712018-02-08 17:22:39 +00001738 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001739}
1740
Lei Zhangab41f252018-12-23 03:10:50 +00001741TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001742 // Open a file with annotations and load its first page.
1743 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001744 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001745 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001746 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001747
Lei Zhanga21d5932018-02-05 18:28:38 +00001748 {
1749 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001750 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001751 ASSERT_TRUE(annot);
1752 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1753 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001754 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001755 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1756 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1757 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001758
Lei Zhanga21d5932018-02-05 18:28:38 +00001759 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001760 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001761 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1762 ASSERT_TRUE(popup);
1763 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1764 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1765 FS_RECTF rect;
1766 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1767 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1768 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001769
Lei Zhanga21d5932018-02-05 18:28:38 +00001770 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1771 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001772 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001773 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1774 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001775
Lei Zhanga21d5932018-02-05 18:28:38 +00001776 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1777 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001778 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001779 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001780 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1781 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001782 }
Jane Liu300bb272017-08-21 14:37:53 -04001783
Jane Liu300bb272017-08-21 14:37:53 -04001784 UnloadPage(page);
1785}
1786
Lei Zhangab41f252018-12-23 03:10:50 +00001787TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001788 // Open file with form text fields.
1789 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001790 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001791 ASSERT_TRUE(page);
1792
Lei Zhanga21d5932018-02-05 18:28:38 +00001793 {
1794 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001795 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001796 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001797
Lei Zhanga21d5932018-02-05 18:28:38 +00001798 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001799 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001800 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001801 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001802 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001803
Lei Zhanga21d5932018-02-05 18:28:38 +00001804 {
1805 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001806 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001807 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001808
Lei Zhanga21d5932018-02-05 18:28:38 +00001809 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001810 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001811 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001812 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1813 }
1814
1815 {
1816 // Retrieve the fourth annotation: user-editable password text field.
1817 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1818 ASSERT_TRUE(annot);
1819
1820 // Check that the flag values are as expected.
1821 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1822 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1823 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001824 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001825
1826 UnloadPage(page);
1827}
1828
Lei Zhangab41f252018-12-23 03:10:50 +00001829TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001830 // Open file with form text fields.
1831 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001832 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001833 ASSERT_TRUE(page);
1834
Lei Zhanga21d5932018-02-05 18:28:38 +00001835 {
1836 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001837 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001838 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001839
Lei Zhanga21d5932018-02-05 18:28:38 +00001840 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001841 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001842 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1843 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1844 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1845 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001846
Lei Zhanga21d5932018-02-05 18:28:38 +00001847 {
1848 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001849 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001850 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001851
Lei Zhanga21d5932018-02-05 18:28:38 +00001852 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001853 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001854 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1855 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1856 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1857 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001858
Lei Zhanga21d5932018-02-05 18:28:38 +00001859 {
1860 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001861 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001862 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001863
Lei Zhanga21d5932018-02-05 18:28:38 +00001864 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001865 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001866 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1867 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1868 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1869 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001870
1871 UnloadPage(page);
1872}
Diana Gage40870db2017-07-19 18:16:03 -07001873
Lei Zhangab41f252018-12-23 03:10:50 +00001874TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001875 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001876 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001877 FPDF_PAGE page = LoadPage(0);
1878 ASSERT_TRUE(page);
1879
1880 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001881 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1882 EXPECT_FALSE(
1883 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001884
Lei Zhang8da98232019-12-11 23:29:33 +00001885 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001886 {
1887 // Verify there is an annotation.
1888 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001889 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001890 EXPECT_TRUE(annot);
1891 }
1892
1893 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001894 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1895 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1896 EXPECT_FALSE(
1897 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001898
1899 UnloadPage(page);
1900}
1901
Lei Zhangab41f252018-12-23 03:10:50 +00001902TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001903 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001904 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001905 FPDF_PAGE page = LoadPage(0);
1906 ASSERT_TRUE(page);
1907
Lei Zhanga21d5932018-02-05 18:28:38 +00001908 {
1909 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001910 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001911 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001912 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001913 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001914
Lei Zhanga21d5932018-02-05 18:28:38 +00001915 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001916 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001917 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1918 }
Diana Gage40870db2017-07-19 18:16:03 -07001919
Lei Zhanga21d5932018-02-05 18:28:38 +00001920 {
1921 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001922 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001923 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001924 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001925 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001926
Lei Zhanga21d5932018-02-05 18:28:38 +00001927 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001928 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001929 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1930 }
Diana Gage40870db2017-07-19 18:16:03 -07001931
1932 UnloadPage(page);
1933}
1934
Lei Zhangab41f252018-12-23 03:10:50 +00001935TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001936 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001937 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001938 FPDF_PAGE page = LoadPage(0);
1939 ASSERT_TRUE(page);
1940
Lei Zhanga21d5932018-02-05 18:28:38 +00001941 {
1942 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001943 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001944 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001945 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001946 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001947
Lei Zhanga21d5932018-02-05 18:28:38 +00001948 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001949 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001950 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1951 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1952 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1953 }
Diana Gage40870db2017-07-19 18:16:03 -07001954
Lei Zhanga21d5932018-02-05 18:28:38 +00001955 {
1956 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001957 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001958 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001959 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001960 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001961
Lei Zhanga21d5932018-02-05 18:28:38 +00001962 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001963 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001964 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1965 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1966 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1967 }
Diana Gage40870db2017-07-19 18:16:03 -07001968
Lei Zhanga21d5932018-02-05 18:28:38 +00001969 {
1970 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001971 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001972 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001973 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001974 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001975
Lei Zhanga21d5932018-02-05 18:28:38 +00001976 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001977 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001978 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1979 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1980 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1981 }
Diana Gage40870db2017-07-19 18:16:03 -07001982
1983 UnloadPage(page);
1984}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001985
Lei Zhang03e5e682019-09-16 19:45:55 +00001986// TODO(crbug.com/pdfium/11): Fix this test and enable.
1987#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1988#define MAYBE_BUG_1206 DISABLED_BUG_1206
1989#else
1990#define MAYBE_BUG_1206 BUG_1206
1991#endif
1992TEST_F(FPDFAnnotEmbedderTest, MAYBE_BUG_1206) {
Lei Zhang992e7e22019-02-04 19:20:58 +00001993 static constexpr size_t kExpectedSize = 1609;
1994 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1995
1996 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1997
1998 FPDF_PAGE page = LoadPage(0);
1999 ASSERT_TRUE(page);
2000
2001 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2002 EXPECT_EQ(kExpectedSize, GetString().size());
2003 ClearString();
2004
2005 for (size_t i = 0; i < 10; ++i) {
2006 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2007 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2008
2009 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2010 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2011 // equal, not bigger.
2012 EXPECT_LT(kExpectedSize, GetString().size());
2013 ClearString();
2014 }
2015
2016 UnloadPage(page);
2017}
2018
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002019TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2020 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2021 FPDF_PAGE page = LoadPage(0);
2022 ASSERT_TRUE(page);
2023 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2024
2025 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002026 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002027 static const size_t kBufSize = 12;
2028 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002029
2030 {
2031 // Add a text annotation to the page.
2032 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2033 ASSERT_TRUE(annot);
2034 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2035 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2036
2037 // Make sure there is no test key, add set a value there, and read it back.
2038 std::fill(buf.begin(), buf.end(), 'x');
2039 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002040 kBufSize));
2041 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002042
Lei Zhangf0f67682019-04-08 17:03:21 +00002043 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002044 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2045
2046 std::fill(buf.begin(), buf.end(), 'x');
2047 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002048 kBufSize));
2049 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002050 }
2051
Lei Zhang05ec64c2019-01-09 03:00:06 +00002052 {
2053 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2054 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002055 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002056 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002057 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2058 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2059 // Also do the same test for its appearance string.
2060 std::fill(buf.begin(), buf.end(), 'x');
2061 ASSERT_EQ(2u,
2062 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002063 buf.data(), kBufSize));
2064 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002065
Lei Zhangf0f67682019-04-08 17:03:21 +00002066 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002067 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2068 text.get()));
2069
2070 std::fill(buf.begin(), buf.end(), 'x');
2071 ASSERT_EQ(6u,
2072 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002073 buf.data(), kBufSize));
2074 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002075 }
2076
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002077 UnloadPage(page);
2078
2079 {
2080 // Save a copy, open the copy, and check the annotation again.
2081 // Note that it renders the rotation.
2082 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002083 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002084 FPDF_PAGE saved_page = LoadSavedPage(0);
2085 ASSERT_TRUE(saved_page);
2086
Lei Zhang05ec64c2019-01-09 03:00:06 +00002087 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002088 {
2089 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2090 ASSERT_TRUE(annot);
2091 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2092
2093 std::fill(buf.begin(), buf.end(), 'x');
2094 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002095 kBufSize));
2096 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002097 }
2098
Lei Zhang05ec64c2019-01-09 03:00:06 +00002099 {
2100 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2101 ASSERT_TRUE(annot);
2102 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2103 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2104
2105 std::fill(buf.begin(), buf.end(), 'x');
2106 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002107 kBufSize));
2108 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002109 }
2110
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002111 CloseSavedPage(saved_page);
2112 CloseSavedDocument();
2113 }
2114}
rycsmithcb752f32019-02-21 18:40:53 +00002115
2116TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2117 // Open a file with combobox widget annotations and load its first page.
2118 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2119 FPDF_PAGE page = LoadPage(0);
2120 ASSERT_TRUE(page);
2121
2122 {
2123 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2124 ASSERT_TRUE(annot);
2125
2126 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2127
2128 annot.reset(FPDFPage_GetAnnot(page, 1));
2129 ASSERT_TRUE(annot);
2130
2131 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002132
2133 // Check bad form handle / annot.
2134 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2135 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2136 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002137 }
2138
2139 UnloadPage(page);
2140}
2141
2142TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2143 // Open a file with listbox widget annotations and load its first page.
2144 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2145 FPDF_PAGE page = LoadPage(0);
2146 ASSERT_TRUE(page);
2147
2148 {
2149 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2150 ASSERT_TRUE(annot);
2151
2152 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2153
2154 annot.reset(FPDFPage_GetAnnot(page, 1));
2155 ASSERT_TRUE(annot);
2156
2157 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2158 }
2159
2160 UnloadPage(page);
2161}
2162
2163TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2164 // Open a file with ink annotations and load its first page.
2165 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2166 FPDF_PAGE page = LoadPage(0);
2167 ASSERT_TRUE(page);
2168
2169 {
2170 // annotations do not have "Opt" array and will return -1
2171 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2172 ASSERT_TRUE(annot);
2173
2174 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2175
2176 annot.reset(FPDFPage_GetAnnot(page, 1));
2177 ASSERT_TRUE(annot);
2178
2179 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2180 }
2181
2182 UnloadPage(page);
2183}
2184
2185TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2186 // Open a file with combobox widget annotations and load its first page.
2187 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2188 FPDF_PAGE page = LoadPage(0);
2189 ASSERT_TRUE(page);
2190
2191 {
2192 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2193 ASSERT_TRUE(annot);
2194
2195 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002196 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002197 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002198 ASSERT_EQ(8u, length_bytes);
2199 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002200 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002201 buf.data(), length_bytes));
2202 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002203
2204 annot.reset(FPDFPage_GetAnnot(page, 1));
2205 ASSERT_TRUE(annot);
2206
2207 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002208 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002209 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002210 ASSERT_EQ(12u, length_bytes);
2211 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002212 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002213 buf.data(), length_bytes));
2214 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002215
2216 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002217 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002218 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002219 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002220 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002221 buf.data(), length_bytes));
2222 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002223
Lei Zhange7033c82019-02-26 19:30:49 +00002224 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002225 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2226 nullptr, 0));
2227 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2228 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002229
2230 // Check bad form handle / annot.
2231 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2232 EXPECT_EQ(0u,
2233 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2234 EXPECT_EQ(0u,
2235 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002236 }
2237
2238 UnloadPage(page);
2239}
2240
2241TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2242 // Open a file with listbox widget annotations and load its first page.
2243 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2244 FPDF_PAGE page = LoadPage(0);
2245 ASSERT_TRUE(page);
2246
2247 {
2248 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2249 ASSERT_TRUE(annot);
2250
2251 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002252 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002253 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002254 ASSERT_EQ(8u, length_bytes);
2255 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002256 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002257 buf.data(), length_bytes));
2258 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002259
2260 annot.reset(FPDFPage_GetAnnot(page, 1));
2261 ASSERT_TRUE(annot);
2262
2263 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002264 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002265 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002266 ASSERT_EQ(12u, length_bytes);
2267 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002268 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002269 buf.data(), length_bytes));
2270 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002271
2272 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002273 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002274 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002275 ASSERT_EQ(18u, length_bytes);
2276 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002277 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002278 buf.data(), length_bytes));
2279 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002280
2281 // indices out of range
2282 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2283 nullptr, 0));
2284 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2285 nullptr, 0));
2286 }
2287
2288 UnloadPage(page);
2289}
2290
2291TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2292 // Open a file with ink annotations and load its first page.
2293 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2294 FPDF_PAGE page = LoadPage(0);
2295 ASSERT_TRUE(page);
2296
2297 {
2298 // annotations do not have "Opt" array and will return 0
2299 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2300 ASSERT_TRUE(annot);
2301
2302 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2303 nullptr, 0));
2304
2305 annot.reset(FPDFPage_GetAnnot(page, 1));
2306 ASSERT_TRUE(annot);
2307
2308 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2309 nullptr, 0));
2310 }
2311
2312 UnloadPage(page);
2313}
Ryan Smith09c23b12019-04-25 18:09:06 +00002314
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002315TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2316 // Open a file with combobox widget annotations and load its first page.
2317 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2318 FPDF_PAGE page = LoadPage(0);
2319 ASSERT_TRUE(page);
2320
2321 {
2322 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2323 ASSERT_TRUE(annot);
2324
2325 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2326 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2327 ASSERT_EQ(3, count);
2328 for (int i = 0; i < count; i++) {
2329 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2330 }
2331
2332 annot.reset(FPDFPage_GetAnnot(page, 1));
2333 ASSERT_TRUE(annot);
2334
2335 // Checks for Combobox with Values (/V) object which is just a string.
2336 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2337 ASSERT_EQ(26, count);
2338 for (int i = 0; i < count; i++) {
2339 EXPECT_EQ(i == 1,
2340 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2341 }
2342
2343 // Checks for index outside bound i.e. (index >= CountOption()).
2344 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2345 /*index=*/26));
2346 // Checks for negetive index.
2347 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2348 /*index=*/-1));
2349
2350 // Checks for bad form handle/annot.
2351 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2352 EXPECT_FALSE(
2353 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2354 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2355 }
2356
2357 UnloadPage(page);
2358}
2359
2360TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2361 // Open a file with listbox widget annotations and load its first page.
2362 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2363 FPDF_PAGE page = LoadPage(0);
2364 ASSERT_TRUE(page);
2365
2366 {
2367 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2368 ASSERT_TRUE(annot);
2369
2370 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2371 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2372 ASSERT_EQ(3, count);
2373 for (int i = 0; i < count; i++) {
2374 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2375 }
2376
2377 annot.reset(FPDFPage_GetAnnot(page, 1));
2378 ASSERT_TRUE(annot);
2379
2380 // Checks for Listbox with Values (/V) object which is just a string.
2381 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2382 ASSERT_EQ(26, count);
2383 for (int i = 0; i < count; i++) {
2384 EXPECT_EQ(i == 1,
2385 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2386 }
2387
2388 annot.reset(FPDFPage_GetAnnot(page, 3));
2389 ASSERT_TRUE(annot);
2390
2391 // Checks for Listbox with only Selected indices (/I) object which is an
2392 // array with multiple objects.
2393 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2394 ASSERT_EQ(5, count);
2395 for (int i = 0; i < count; i++) {
2396 bool expected = (i == 1 || i == 3);
2397 EXPECT_EQ(expected,
2398 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2399 }
2400
2401 annot.reset(FPDFPage_GetAnnot(page, 4));
2402 ASSERT_TRUE(annot);
2403
2404 // Checks for Listbox with Values (/V) object which is an array with
2405 // multiple objects.
2406 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2407 ASSERT_EQ(5, count);
2408 for (int i = 0; i < count; i++) {
2409 bool expected = (i == 2 || i == 4);
2410 EXPECT_EQ(expected,
2411 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2412 }
2413
2414 annot.reset(FPDFPage_GetAnnot(page, 5));
2415 ASSERT_TRUE(annot);
2416
2417 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2418 // objects conflict with different lengths.
2419 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2420 ASSERT_EQ(5, count);
2421 for (int i = 0; i < count; i++) {
2422 bool expected = (i == 0 || i == 2);
2423 EXPECT_EQ(expected,
2424 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2425 }
2426 }
2427
2428 UnloadPage(page);
2429}
2430
2431TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2432 // Open a file with multiple form field annotations and load its first page.
2433 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2434 FPDF_PAGE page = LoadPage(0);
2435 ASSERT_TRUE(page);
2436
2437 {
2438 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2439 ASSERT_TRUE(annot);
2440
2441 // Checks for link annotation.
2442 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2443 /*index=*/0));
2444
2445 annot.reset(FPDFPage_GetAnnot(page, 3));
2446 ASSERT_TRUE(annot);
2447
2448 // Checks for text field annotation.
2449 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2450 /*index=*/0));
2451 }
2452
2453 UnloadPage(page);
2454}
2455
Ryan Smith09c23b12019-04-25 18:09:06 +00002456TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2457 // Open a file with combobox annotations and load its first page.
2458 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2459 FPDF_PAGE page = LoadPage(0);
2460 ASSERT_TRUE(page);
2461
2462 {
2463 // All 3 widgets have Tf font size 12.
2464 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2465 ASSERT_TRUE(annot);
2466
2467 float value;
2468 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2469 EXPECT_EQ(12.0, value);
2470
2471 annot.reset(FPDFPage_GetAnnot(page, 1));
2472 ASSERT_TRUE(annot);
2473
2474 float value_two;
2475 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2476 EXPECT_EQ(12.0, value_two);
2477
2478 annot.reset(FPDFPage_GetAnnot(page, 2));
2479 ASSERT_TRUE(annot);
2480
2481 float value_three;
2482 ASSERT_TRUE(
2483 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2484 EXPECT_EQ(12.0, value_three);
2485 }
2486
2487 UnloadPage(page);
2488}
2489
2490TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2491 // Open a file with textfield annotations and load its first page.
2492 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2493 FPDF_PAGE page = LoadPage(0);
2494 ASSERT_TRUE(page);
2495
2496 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002497 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002498 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2499 ASSERT_TRUE(annot);
2500
2501 float value;
2502 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2503 EXPECT_EQ(12.0, value);
2504
2505 annot.reset(FPDFPage_GetAnnot(page, 1));
2506 ASSERT_TRUE(annot);
2507
2508 float value_two;
2509 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2510 EXPECT_EQ(12.0, value_two);
2511
2512 annot.reset(FPDFPage_GetAnnot(page, 2));
2513 ASSERT_TRUE(annot);
2514
2515 float value_three;
2516 ASSERT_TRUE(
2517 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2518 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002519
2520 float value_four;
2521 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2522 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002523 }
2524
2525 UnloadPage(page);
2526}
2527
2528TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2529 // Open a file with ink annotations and load its first page.
2530 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2531 FPDF_PAGE page = LoadPage(0);
2532 ASSERT_TRUE(page);
2533
2534 {
2535 // Annotations that do not have variable text and will return -1.
2536 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2537 ASSERT_TRUE(annot);
2538
2539 float value;
2540 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2541
2542 annot.reset(FPDFPage_GetAnnot(page, 1));
2543 ASSERT_TRUE(annot);
2544
2545 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2546 }
2547
2548 UnloadPage(page);
2549}
2550
2551TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2552 // Open a file with combobox annotations and load its first page.
2553 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2554 FPDF_PAGE page = LoadPage(0);
2555 ASSERT_TRUE(page);
2556
2557 {
2558 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2559 ASSERT_TRUE(annot);
2560
2561 // Check bad form handle / annot.
2562 float value;
2563 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2564 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2565 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2566 }
2567
2568 UnloadPage(page);
2569}
2570
2571TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2572 // Open a file with textfield annotations and load its first page.
2573 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2574 FPDF_PAGE page = LoadPage(0);
2575 ASSERT_TRUE(page);
2576
2577 {
2578 // Obtain the first annotation, a text field with negative font size, -12.
2579 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2580 ASSERT_TRUE(annot);
2581
2582 float value;
2583 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2584 EXPECT_EQ(-12.0, value);
2585 }
2586
2587 UnloadPage(page);
2588}
Ryan Smith23fdf892019-07-17 21:51:26 +00002589
2590TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2591 // Open a file with checkbox and radiobuttons widget annotations and load its
2592 // first page.
2593 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2594 FPDF_PAGE page = LoadPage(0);
2595 ASSERT_TRUE(page);
2596
2597 {
2598 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2599 ASSERT_TRUE(annot);
2600 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2601 }
2602
2603 UnloadPage(page);
2604}
2605
2606TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2607 // Open a file with checkbox and radiobutton widget annotations and load its
2608 // first page.
2609 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2610 FPDF_PAGE page = LoadPage(0);
2611 ASSERT_TRUE(page);
2612
2613 {
2614 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2615 ASSERT_TRUE(annot);
2616 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2617 }
2618
2619 UnloadPage(page);
2620}
2621
2622TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2623 // Open a file with checkbox and radiobutton widget annotations and load its
2624 // first page.
2625 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2626 FPDF_PAGE page = LoadPage(0);
2627 ASSERT_TRUE(page);
2628
2629 {
2630 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2631 ASSERT_TRUE(annot);
2632 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2633
2634 annot.reset(FPDFPage_GetAnnot(page, 6));
2635 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2636
2637 annot.reset(FPDFPage_GetAnnot(page, 7));
2638 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2639 }
2640
2641 UnloadPage(page);
2642}
2643
2644TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2645 // Open a file with checkbox and radiobutton widget annotations and load its
2646 // first page.
2647 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2648 FPDF_PAGE page = LoadPage(0);
2649 ASSERT_TRUE(page);
2650
2651 {
2652 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2653 ASSERT_TRUE(annot);
2654 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2655
2656 annot.reset(FPDFPage_GetAnnot(page, 3));
2657 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2658
2659 annot.reset(FPDFPage_GetAnnot(page, 4));
2660 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2661 }
2662
2663 UnloadPage(page);
2664}
2665
2666TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2667 // Open a file with checkbox and radiobuttons widget annotations and load its
2668 // first page.
2669 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2670 FPDF_PAGE page = LoadPage(0);
2671 ASSERT_TRUE(page);
2672
2673 {
2674 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2675 ASSERT_TRUE(annot);
2676 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2677 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2678 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2679 }
2680
2681 UnloadPage(page);
2682}
2683
2684TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2685 // Open a file with text widget annotations and load its first page.
2686 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2687 FPDF_PAGE page = LoadPage(0);
2688 ASSERT_TRUE(page);
2689
2690 {
2691 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2692 ASSERT_TRUE(annot);
2693 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2694 }
2695
2696 UnloadPage(page);
2697}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002698
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002699TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2700 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002701 FPDF_PAGE page = LoadPage(0);
2702 ASSERT_TRUE(page);
2703
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002704 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2705
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002706 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002707 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002708 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002709 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002710 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002711
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002712 constexpr int kExpectedAnnotTypes[] = {-1,
2713 FPDF_FORMFIELD_COMBOBOX,
2714 FPDF_FORMFIELD_LISTBOX,
2715 FPDF_FORMFIELD_TEXTFIELD,
2716 FPDF_FORMFIELD_CHECKBOX,
2717 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002718
Lei Zhang4501a502020-05-18 16:52:59 +00002719 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002720 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002721 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002722 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002723 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2724 }
2725 UnloadPage(page);
2726}
2727
2728TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2729 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2730 FPDF_PAGE page = LoadPage(0);
2731 ASSERT_TRUE(page);
2732
2733 {
2734 EXPECT_EQ(0u,
2735 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2736
2737 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2738 ASSERT_TRUE(annot);
2739
2740 EXPECT_EQ(0u,
2741 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2742
2743 unsigned long length_bytes =
2744 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2745 ASSERT_EQ(2u, length_bytes);
2746 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2747 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2748 buf.data(), length_bytes));
2749 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2750 }
2751 {
2752 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2753 ASSERT_TRUE(annot);
2754
2755 unsigned long length_bytes =
2756 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2757 ASSERT_EQ(18u, length_bytes);
2758 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2759 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2760 buf.data(), length_bytes));
2761 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2762 }
2763 UnloadPage(page);
2764}
2765
2766TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2767 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2768 FPDF_PAGE page = LoadPage(0);
2769 ASSERT_TRUE(page);
2770
2771 {
2772 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2773 ASSERT_TRUE(annot);
2774
2775 unsigned long length_bytes =
2776 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2777 ASSERT_EQ(2u, length_bytes);
2778 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2779 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2780 buf.data(), length_bytes));
2781 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2782 }
2783 {
2784 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2785 ASSERT_TRUE(annot);
2786
2787 unsigned long length_bytes =
2788 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2789 ASSERT_EQ(14u, length_bytes);
2790 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2791 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2792 buf.data(), length_bytes));
2793 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2794 }
2795 UnloadPage(page);
2796}
2797
2798TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2799 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2800 FPDF_PAGE page = LoadPage(0);
2801 ASSERT_TRUE(page);
2802
2803 {
2804 EXPECT_EQ(0u,
2805 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2806
2807 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2808 ASSERT_TRUE(annot);
2809
2810 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2811
2812 unsigned long length_bytes =
2813 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2814 ASSERT_EQ(18u, length_bytes);
2815 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2816 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2817 buf.data(), length_bytes));
2818 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2819 }
2820 UnloadPage(page);
2821}
2822
2823TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2824 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2825 FPDF_PAGE page = LoadPage(0);
2826 ASSERT_TRUE(page);
2827
2828 {
2829 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2830 ASSERT_TRUE(annot);
2831
2832 unsigned long length_bytes =
2833 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2834 ASSERT_EQ(30u, length_bytes);
2835 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2836 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2837 buf.data(), length_bytes));
2838 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2839 }
2840 UnloadPage(page);
2841}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002842
Lei Zhang9b444002020-04-17 17:35:23 +00002843TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2844 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002845 FPDF_PAGE page = LoadPage(0);
2846 ASSERT_TRUE(page);
2847
Lei Zhang9b444002020-04-17 17:35:23 +00002848 // Verify widgets are by default focusable.
2849 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2850 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002851
Lei Zhang9b444002020-04-17 17:35:23 +00002852 // Expected annot subtypes for page 0 of annots.pdf.
2853 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2854 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2855 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2856 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2857 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002858
Lei Zhang9b444002020-04-17 17:35:23 +00002859 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002860 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002861 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2862 kExpectedAnnotSubtypes,
2863 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002864
Lei Zhang9b444002020-04-17 17:35:23 +00002865 // Make no annotation type focusable using the preferred method.
2866 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2867 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002868
Lei Zhang9b444002020-04-17 17:35:23 +00002869 // Restore the focusable type count back to 1, then set it back to 0 using a
2870 // different method.
2871 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2872 ASSERT_TRUE(
2873 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2874 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002875
Lei Zhang9b444002020-04-17 17:35:23 +00002876 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002877 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002878
Lei Zhang9b444002020-04-17 17:35:23 +00002879 // Now make links focusable.
2880 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2881 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002882
Lei Zhang9b444002020-04-17 17:35:23 +00002883 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002884 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002885 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2886 kExpectedAnnotSubtypes,
2887 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002888
Lei Zhang9b444002020-04-17 17:35:23 +00002889 // Test invalid parameters.
2890 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00002891 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002892 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00002893 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002894 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2895
Lei Zhang9b444002020-04-17 17:35:23 +00002896 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2897 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2898 subtypes.size()));
2899 EXPECT_FALSE(
2900 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2901 EXPECT_FALSE(
2902 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002903
2904 UnloadPage(page);
2905}
Lei Zhang671aece2020-04-14 19:02:26 +00002906
Hui Yingstea3816d2020-07-28 22:35:11 +00002907TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00002908 ASSERT_TRUE(OpenDocument("annots.pdf"));
2909 FPDF_PAGE page = LoadPage(0);
2910 ASSERT_TRUE(page);
2911
2912 {
Hui Yingstea3816d2020-07-28 22:35:11 +00002913#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2914 static const char kMd5sum[] = "4ca14c670396711194b40ecc2514969b";
2915#else
Lei Zhang671aece2020-04-14 19:02:26 +00002916#if defined(OS_WIN)
2917 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
Lei Zhang0c03d632020-07-30 17:05:36 +00002918#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00002919 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
2920#else
2921 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
2922#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00002923#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00002924 // Check the initial rendering.
2925 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2926 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2927 }
2928
2929 // Make links and highlights focusable.
2930 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
2931 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00002932 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00002933 ASSERT_TRUE(
2934 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
2935 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
2936 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
2937 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
2938 subtypes.size()));
2939 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
2940 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
2941
2942 {
Hui Yingstea3816d2020-07-28 22:35:11 +00002943#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2944 static const char kMd5sum[] = "48cb60b3f9bc364c73582aff3418451e";
2945#else
Lei Zhang671aece2020-04-14 19:02:26 +00002946#if defined(OS_WIN)
2947 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
Lei Zhang0c03d632020-07-30 17:05:36 +00002948#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00002949 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
2950#else
2951 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
2952#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00002953#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00002954 // Focus the first link and check the rendering.
2955 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2956 ASSERT_TRUE(annot);
2957 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2958 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2959 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2960 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2961 }
2962
2963 {
Hui Yingstea3816d2020-07-28 22:35:11 +00002964#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2965 static const char kMd5sum[] = "d09869ff0a209daf179da2d7a58142b1";
2966#else
Lei Zhang671aece2020-04-14 19:02:26 +00002967#if defined(OS_WIN)
2968 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
Lei Zhang0c03d632020-07-30 17:05:36 +00002969#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00002970 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
2971#else
2972 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
2973#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00002974#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00002975 // Focus the first highlight and check the rendering.
2976 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2977 ASSERT_TRUE(annot);
2978 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
2979 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2980 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2981 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2982 }
2983
2984 UnloadPage(page);
2985}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00002986
2987TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
2988 ASSERT_TRUE(OpenDocument("annots.pdf"));
2989 FPDF_PAGE page = LoadPage(0);
2990 ASSERT_TRUE(page);
2991 {
2992 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
2993 ASSERT_TRUE(annot);
2994 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2995 FPDF_LINK link_annot = FPDFAnnot_GetLink(annot.get());
2996 ASSERT_TRUE(link_annot);
2997
2998 FPDF_ACTION action = FPDFLink_GetAction(link_annot);
2999 ASSERT_TRUE(action);
3000 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
3001 FPDFAction_GetType(action));
3002
3003 constexpr char kExpectedResult[] =
3004 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3005 "fpdf_text.h";
Lei Zhang4501a502020-05-18 16:52:59 +00003006 constexpr unsigned long kExpectedLength = pdfium::size(kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003007 unsigned long bufsize =
3008 FPDFAction_GetURIPath(document(), action, nullptr, 0);
3009 ASSERT_EQ(kExpectedLength, bufsize);
3010
3011 char buffer[1024];
3012 EXPECT_EQ(bufsize,
3013 FPDFAction_GetURIPath(document(), action, buffer, bufsize));
3014 EXPECT_STREQ(kExpectedResult, buffer);
3015 }
3016
3017 {
3018 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3019 ASSERT_TRUE(annot);
3020 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3021 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3022 }
3023
3024 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3025
3026 UnloadPage(page);
3027}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003028
3029TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3030 // Open a file with radio button widget annotations and load its first page.
3031 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3032 FPDF_PAGE page = LoadPage(0);
3033 ASSERT_TRUE(page);
3034
3035 {
3036 // Checks for bad annot.
3037 EXPECT_EQ(-1,
3038 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3039
3040 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3041 ASSERT_TRUE(annot);
3042
3043 // Checks for bad form handle.
3044 EXPECT_EQ(-1,
3045 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3046
3047 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3048 }
3049
3050 UnloadPage(page);
3051}
3052
3053TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3054 // Open a file with checkbox widget annotations and load its first page.
3055 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3056 FPDF_PAGE page = LoadPage(0);
3057 ASSERT_TRUE(page);
3058
3059 {
3060 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3061 ASSERT_TRUE(annot);
3062 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3063 }
3064
3065 UnloadPage(page);
3066}
3067
3068TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3069 // Open a file with ink annotations and load its first page.
3070 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3071 FPDF_PAGE page = LoadPage(0);
3072 ASSERT_TRUE(page);
3073
3074 {
3075 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3076 ASSERT_TRUE(annot);
3077 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3078 }
3079
3080 UnloadPage(page);
3081}
3082
3083TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3084 // Open a file with radio button widget annotations and load its first page.
3085 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3086 FPDF_PAGE page = LoadPage(0);
3087 ASSERT_TRUE(page);
3088
3089 {
3090 // Checks for bad annot.
3091 EXPECT_EQ(-1,
3092 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3093
3094 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3095 ASSERT_TRUE(annot);
3096
3097 // Checks for bad form handle.
3098 EXPECT_EQ(-1,
3099 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3100
3101 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3102 }
3103
3104 UnloadPage(page);
3105}
3106
3107TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3108 // Open a file with checkbox widget annotations and load its first page.
3109 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3110 FPDF_PAGE page = LoadPage(0);
3111 ASSERT_TRUE(page);
3112
3113 {
3114 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3115 ASSERT_TRUE(annot);
3116 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3117 }
3118
3119 UnloadPage(page);
3120}
3121
3122TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3123 // Open a file with ink annotations and load its first page.
3124 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3125 FPDF_PAGE page = LoadPage(0);
3126 ASSERT_TRUE(page);
3127
3128 {
3129 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3130 ASSERT_TRUE(annot);
3131 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3132 }
3133
3134 UnloadPage(page);
3135}
3136
3137TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3138 // Open a file with radio button widget annotations and load its first page.
3139 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3140 FPDF_PAGE page = LoadPage(0);
3141 ASSERT_TRUE(page);
3142
3143 {
3144 // Checks for bad annot.
3145 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3146 form_handle(), /*annot=*/nullptr,
3147 /*buffer=*/nullptr, /*buflen=*/0));
3148
3149 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3150 ASSERT_TRUE(annot);
3151
3152 // Checks for bad form handle.
3153 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3154 /*hHandle=*/nullptr, annot.get(),
3155 /*buffer=*/nullptr, /*buflen=*/0));
3156
3157 unsigned long length_bytes =
3158 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3159 /*buffer=*/nullptr, /*buflen=*/0);
3160 ASSERT_EQ(14u, length_bytes);
3161 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3162 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3163 buf.data(), length_bytes));
3164 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3165 }
3166
3167 UnloadPage(page);
3168}
3169
3170TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3171 // Open a file with checkbox widget annotations and load its first page.
3172 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3173 FPDF_PAGE page = LoadPage(0);
3174 ASSERT_TRUE(page);
3175
3176 {
3177 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3178 ASSERT_TRUE(annot);
3179
3180 unsigned long length_bytes =
3181 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3182 /*buffer=*/nullptr, /*buflen=*/0);
3183 ASSERT_EQ(8u, length_bytes);
3184 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3185 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3186 buf.data(), length_bytes));
3187 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3188 }
3189
3190 UnloadPage(page);
3191}
3192
3193TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3194 // Open a file with ink annotations and load its first page.
3195 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3196 FPDF_PAGE page = LoadPage(0);
3197 ASSERT_TRUE(page);
3198
3199 {
3200 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3201 ASSERT_TRUE(annot);
3202 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3203 /*buffer=*/nullptr,
3204 /*buflen=*/0));
3205 }
3206
3207 UnloadPage(page);
3208}