blob: 03e6773acfb9a09b6193cea93c76fbcfa7e552ea [file] [log] [blame]
Jane Liu4fd9a472017-06-01 18:56:09 -04001// Copyright 2017 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Tom Sepez204ab052020-06-12 21:33:48 +00005#include "public/fpdf_annot.h"
6
7#include <limits.h>
8
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00009#include <algorithm>
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000010#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -040011#include <memory>
12#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -040013#include <vector>
14
Lei Zhange4cdac52019-04-30 16:45:57 +000015#include "build/build_config.h"
Lei Zhanga5c1daf2019-01-31 21:56:47 +000016#include "constants/annotation_common.h"
Tom Sepez204ab052020-06-12 21:33:48 +000017#include "core/fpdfapi/page/cpdf_annotcontext.h"
18#include "core/fpdfapi/page/cpdf_pagemodule.h"
19#include "core/fpdfapi/parser/cpdf_array.h"
20#include "core/fpdfapi/parser/cpdf_dictionary.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040021#include "core/fxcrt/fx_system.h"
Tom Sepez204ab052020-06-12 21:33:48 +000022#include "fpdfsdk/cpdfsdk_helpers.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000023#include "public/cpp/fpdf_scopers.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040024#include "public/fpdf_edit.h"
Mansi Awasthi07bf7e62020-01-24 10:34:17 +000025#include "public/fpdf_formfill.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040026#include "public/fpdfview.h"
27#include "testing/embedder_test.h"
Hui Yingstb4baceb2020-04-28 23:46:10 +000028#include "testing/embedder_test_constants.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000029#include "testing/fx_string_testhelpers.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000030#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040031#include "testing/gtest/include/gtest/gtest.h"
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000032#include "testing/utils/hash.h"
Lei Zhang9b444002020-04-17 17:35:23 +000033#include "third_party/base/span.h"
Lei Zhangc9b72992020-04-22 01:03:52 +000034#include "third_party/base/stl_util.h"
Lei Zhang9b444002020-04-17 17:35:23 +000035
Hui Yingst30bfcc52020-07-27 23:54:40 +000036using pdfium::kAnnotationStampWithApChecksum;
37
Lei Zhang9b444002020-04-17 17:35:23 +000038namespace {
39
Tom Sepez204ab052020-06-12 21:33:48 +000040const wchar_t kStreamData[] =
41 L"/GS gs 0.0 0.0 0.0 RG 4 w 211.8 747.6 m 211.8 744.8 "
42 L"212.6 743.0 214.2 740.8 "
43 L"c 215.4 739.0 216.8 737.1 218.9 736.1 c 220.8 735.1 221.4 733.0 "
44 L"223.7 732.4 c 232.6 729.9 242.0 730.8 251.2 730.8 c 257.5 730.8 "
45 L"263.0 732.9 269.0 734.4 c S";
46
Lei Zhang9b444002020-04-17 17:35:23 +000047void VerifyFocusableAnnotSubtypes(
48 FPDF_FORMHANDLE form_handle,
49 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) {
50 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
51 FPDFAnnot_GetFocusableSubtypesCount(form_handle));
52
53 std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes(
54 expected_subtypes.size());
55 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(
56 form_handle, actual_subtypes.data(), actual_subtypes.size()));
57 for (size_t i = 0; i < expected_subtypes.size(); ++i)
58 ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]);
59}
60
61void SetAndVerifyFocusableAnnotSubtypes(
62 FPDF_FORMHANDLE form_handle,
63 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) {
64 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(),
65 subtypes.size()));
66 VerifyFocusableAnnotSubtypes(form_handle, subtypes);
67}
68
69void VerifyAnnotationSubtypesAndFocusability(
70 FPDF_FORMHANDLE form_handle,
71 FPDF_PAGE page,
72 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes,
73 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) {
74 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
75 FPDFPage_GetAnnotCount(page));
76 for (size_t i = 0; i < expected_subtypes.size(); ++i) {
77 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
78 ASSERT_TRUE(annot);
79 EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get()));
80
Lei Zhangf245ae62020-05-15 21:49:46 +000081 bool expected_focusable =
82 pdfium::Contains(expected_focusable_subtypes, expected_subtypes[i]);
Lei Zhang9b444002020-04-17 17:35:23 +000083 EXPECT_EQ(expected_focusable,
84 FORM_SetFocusedAnnot(form_handle, annot.get()));
85
86 // Kill the focus so the next test starts in an unfocused state.
87 FORM_ForceToKillFocus(form_handle);
88 }
89}
90
Lei Zhang306874b2021-04-16 00:33:36 +000091void VerifyUriActionInLink(FPDF_DOCUMENT doc,
92 FPDF_LINK link,
93 const std::string& expected_uri) {
94 ASSERT_TRUE(link);
95
96 FPDF_ACTION action = FPDFLink_GetAction(link);
97 ASSERT_TRUE(action);
98 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
99 FPDFAction_GetType(action));
100
101 unsigned long bufsize = FPDFAction_GetURIPath(doc, action, nullptr, 0);
102 ASSERT_EQ(expected_uri.size() + 1, bufsize);
103
104 std::vector<char> buffer(bufsize);
105 EXPECT_EQ(bufsize,
106 FPDFAction_GetURIPath(doc, action, buffer.data(), bufsize));
107 EXPECT_STREQ(expected_uri.c_str(), buffer.data());
108}
109
Lei Zhang9b444002020-04-17 17:35:23 +0000110} // namespace
Jane Liu4fd9a472017-06-01 18:56:09 -0400111
Lei Zhangab41f252018-12-23 03:10:50 +0000112class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -0400113
Tom Sepez204ab052020-06-12 21:33:48 +0000114TEST_F(FPDFAnnotEmbedderTest, SetAP) {
115 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
116 ASSERT_TRUE(doc);
117 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
118 ASSERT_TRUE(page);
119 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
120 ASSERT_TRUE(ap_stream);
121
122 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
123 ASSERT_TRUE(annot);
124
125 // Negative case: FPDFAnnot_SetAP() should fail if bounding rect is not yet
126 // set on the annotation.
127 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
128 ap_stream.get()));
129
130 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
131 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
132
133 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
134 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/255));
135
136 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
137 ap_stream.get()));
138
139 // Verify that appearance stream is created as form XObject
140 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
141 ASSERT_TRUE(context);
142 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
143 ASSERT_TRUE(annot_dict);
144 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
145 ASSERT_TRUE(ap_dict);
146 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
147 ASSERT_TRUE(stream_dict);
148 // Check for non-existence of resources dictionary in case of opaque color
149 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
150 ASSERT_FALSE(resources_dict);
151 ByteString type = stream_dict->GetStringFor(pdfium::annotation::kType);
152 EXPECT_EQ("XObject", type);
153 ByteString sub_type = stream_dict->GetStringFor(pdfium::annotation::kSubtype);
154 EXPECT_EQ("Form", sub_type);
155
156 // Check that the appearance stream is same as we just set.
157 const uint32_t kStreamDataSize =
158 pdfium::size(kStreamData) * sizeof(FPDF_WCHAR);
159 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
160 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
161 ASSERT_EQ(kStreamDataSize, normal_length_bytes);
162 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
163 EXPECT_EQ(kStreamDataSize,
164 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
165 buf.data(), normal_length_bytes));
166 EXPECT_EQ(kStreamData, GetPlatformWString(buf.data()));
167}
168
169TEST_F(FPDFAnnotEmbedderTest, SetAPWithOpacity) {
170 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
171 ASSERT_TRUE(doc);
172 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
173 ASSERT_TRUE(page);
174 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
175 ASSERT_TRUE(ap_stream);
176
177 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
178 ASSERT_TRUE(annot);
179
180 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
181 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/102));
182
183 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
184 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
185
186 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
187 ap_stream.get()));
188
189 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
190 ASSERT_TRUE(context);
191 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
192 ASSERT_TRUE(annot_dict);
193 CPDF_Dictionary* ap_dict = annot_dict->GetDictFor(pdfium::annotation::kAP);
194 ASSERT_TRUE(ap_dict);
195 CPDF_Dictionary* stream_dict = ap_dict->GetDictFor("N");
196 ASSERT_TRUE(stream_dict);
197 CPDF_Dictionary* resources_dict = stream_dict->GetDictFor("Resources");
198 ASSERT_TRUE(stream_dict);
199 CPDF_Dictionary* extGState_dict = resources_dict->GetDictFor("ExtGState");
200 ASSERT_TRUE(extGState_dict);
201 CPDF_Dictionary* gs_dict = extGState_dict->GetDictFor("GS");
202 ASSERT_TRUE(gs_dict);
203 ByteString type = gs_dict->GetStringFor(pdfium::annotation::kType);
204 EXPECT_EQ("ExtGState", type);
205 float opacity = gs_dict->GetNumberFor("CA");
206 // Opacity value of 102 is represented as 0.4f (=104/255) in /CA entry.
207 EXPECT_FLOAT_EQ(0.4f, opacity);
208 ByteString blend_mode = gs_dict->GetStringFor("BM");
209 EXPECT_EQ("Normal", blend_mode);
210 bool alpha_source_flag = gs_dict->GetBooleanFor("AIS", true);
211 EXPECT_FALSE(alpha_source_flag);
212}
213
214TEST_F(FPDFAnnotEmbedderTest, InkListAPIValidations) {
215 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
216 ASSERT_TRUE(doc);
217 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
218 ASSERT_TRUE(page);
219
220 // Create a new ink annotation.
221 ScopedFPDFAnnotation ink_annot(
222 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
223 ASSERT_TRUE(ink_annot);
224 CPDF_AnnotContext* context =
225 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
226 ASSERT_TRUE(context);
227 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
228 ASSERT_TRUE(annot_dict);
229
230 static constexpr FS_POINTF kFirstInkStroke[] = {
231 {80.0f, 90.0f}, {81.0f, 91.0f}, {82.0f, 92.0f},
232 {83.0f, 93.0f}, {84.0f, 94.0f}, {85.0f, 95.0f}};
233 static constexpr size_t kFirstStrokePointCount =
234 pdfium::size(kFirstInkStroke);
235
236 static constexpr FS_POINTF kSecondInkStroke[] = {
237 {70.0f, 90.0f}, {71.0f, 91.0f}, {72.0f, 92.0f}};
238 static constexpr size_t kSecondStrokePointCount =
239 pdfium::size(kSecondInkStroke);
240
241 static constexpr FS_POINTF kThirdInkStroke[] = {{60.0f, 90.0f},
242 {61.0f, 91.0f},
243 {62.0f, 92.0f},
244 {63.0f, 93.0f},
245 {64.0f, 94.0f}};
246 static constexpr size_t kThirdStrokePointCount =
247 pdfium::size(kThirdInkStroke);
248
249 // Negative test: |annot| is passed as nullptr.
250 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(nullptr, kFirstInkStroke,
251 kFirstStrokePointCount));
252
253 // Negative test: |annot| is not ink annotation.
254 // Create a new highlight annotation.
255 ScopedFPDFAnnotation highlight_annot(
256 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
257 ASSERT_TRUE(highlight_annot);
258 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(highlight_annot.get(), kFirstInkStroke,
259 kFirstStrokePointCount));
260
261 // Negative test: passing |point_count| as 0.
262 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke, 0));
263
264 // Negative test: passing |points| array as nullptr.
265 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), nullptr,
266 kFirstStrokePointCount));
267
268 // Negative test: passing |point_count| more than ULONG_MAX/2.
269 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
270 ULONG_MAX / 2 + 1));
271
272 // InkStroke should get added to ink annotation. Also inklist should get
273 // created.
274 EXPECT_EQ(0, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke,
275 kFirstStrokePointCount));
276
277 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
278 ASSERT_TRUE(inklist);
279 EXPECT_EQ(1u, inklist->size());
280 EXPECT_EQ(kFirstStrokePointCount * 2, inklist->GetArrayAt(0)->size());
281
282 // Adding another inkStroke to ink annotation with all valid paremeters.
283 // InkList already exists in ink_annot.
284 EXPECT_EQ(1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
285 kSecondStrokePointCount));
286 EXPECT_EQ(2u, inklist->size());
287 EXPECT_EQ(kSecondStrokePointCount * 2, inklist->GetArrayAt(1)->size());
288
289 // Adding one more InkStroke to the ink annotation. |point_count| passed is
290 // less than the data available in |buffer|.
291 EXPECT_EQ(2, FPDFAnnot_AddInkStroke(ink_annot.get(), kThirdInkStroke,
292 kThirdStrokePointCount - 1));
293 EXPECT_EQ(3u, inklist->size());
294 EXPECT_EQ((kThirdStrokePointCount - 1) * 2, inklist->GetArrayAt(2)->size());
295}
296
297TEST_F(FPDFAnnotEmbedderTest, RemoveInkList) {
298 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
299 ASSERT_TRUE(doc);
300 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
301 ASSERT_TRUE(page);
302
303 // Negative test: |annot| is passed as nullptr.
304 EXPECT_FALSE(FPDFAnnot_RemoveInkList(nullptr));
305
306 // Negative test: |annot| is not ink annotation.
307 // Create a new highlight annotation.
308 ScopedFPDFAnnotation highlight_annot(
309 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
310 ASSERT_TRUE(highlight_annot);
311 EXPECT_FALSE(FPDFAnnot_RemoveInkList(highlight_annot.get()));
312
313 // Create a new ink annotation.
314 ScopedFPDFAnnotation ink_annot(
315 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
316 ASSERT_TRUE(ink_annot);
317 CPDF_AnnotContext* context =
318 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
319 ASSERT_TRUE(context);
320 CPDF_Dictionary* annot_dict = context->GetAnnotDict();
321 ASSERT_TRUE(annot_dict);
322
323 static constexpr FS_POINTF kInkStroke[] = {{80.0f, 90.0f}, {81.0f, 91.0f},
324 {82.0f, 92.0f}, {83.0f, 93.0f},
325 {84.0f, 94.0f}, {85.0f, 95.0f}};
326 static constexpr size_t kPointCount = pdfium::size(kInkStroke);
327
328 // InkStroke should get added to ink annotation. Also inklist should get
329 // created.
330 EXPECT_EQ(0,
331 FPDFAnnot_AddInkStroke(ink_annot.get(), kInkStroke, kPointCount));
332
333 CPDF_Array* inklist = annot_dict->GetArrayFor("InkList");
334 ASSERT_TRUE(inklist);
335 ASSERT_EQ(1u, inklist->size());
336 EXPECT_EQ(kPointCount * 2, inklist->GetArrayAt(0)->size());
337
338 // Remove inklist.
339 EXPECT_TRUE(FPDFAnnot_RemoveInkList(ink_annot.get()));
340 EXPECT_FALSE(annot_dict->KeyExist("InkList"));
341}
342
Lei Zhangab41f252018-12-23 03:10:50 +0000343TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +0000344 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
345 FPDF_PAGE page = LoadPage(0);
346 ASSERT_TRUE(page);
347
348 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
349
350 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
351 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
352 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
353 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
354 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
355
356 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
357
358 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
359
360 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
361 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
362 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
363
364 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
365
Lei Zhang4f556b82019-04-08 16:32:41 +0000366 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +0000367 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +0000368 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
369
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000370 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +0000371 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
372 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
373 EXPECT_EQ(0u,
374 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
375
376 UnloadPage(page);
377}
378
Lei Zhang3d9a0972019-03-04 19:34:09 +0000379TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
380 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
381 FPDF_PAGE page = LoadPage(0);
382 ASSERT_TRUE(page);
383
384 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +0000385 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +0000386
387 UnloadPage(page);
388}
389
Lei Zhangab41f252018-12-23 03:10:50 +0000390TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -0400391 // Open a file with one annotation and load its first page.
392 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000393 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -0400394 ASSERT_TRUE(page);
395
396 // This annotation has a malformed appearance stream, which does not have its
397 // normal appearance defined, only its rollover appearance. In this case, its
398 // normal appearance should be generated, allowing the highlight annotation to
399 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000400 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000401 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -0400402
403 UnloadPage(page);
404}
405
Hui Yingstd5b6f632020-07-22 01:31:59 +0000406TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang03e5e682019-09-16 19:45:55 +0000407#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstd5b6f632020-07-22 01:31:59 +0000408 static const char kChecksum[] = "ec1f4ccbd0aecfdea6d53893387a0101";
Lei Zhang03e5e682019-09-16 19:45:55 +0000409#else
Hui Yingstd5b6f632020-07-22 01:31:59 +0000410 static const char kChecksum[] = "76512832d88017668d9acc7aacd13dae";
Lei Zhang03e5e682019-09-16 19:45:55 +0000411#endif
Henrique Nakashima5098b252018-03-26 21:46:00 +0000412 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000413 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
414 FPDF_PAGE page = LoadPage(0);
415 ASSERT_TRUE(page);
416
Tom Sepeze08d2b12018-04-25 18:49:32 +0000417 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstd5b6f632020-07-22 01:31:59 +0000418 CompareBitmap(bitmap.get(), 595, 842, kChecksum);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000419
420 UnloadPage(page);
421}
422
Lei Zhangab41f252018-12-23 03:10:50 +0000423TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400424 // Open a file with one annotation and load its first page.
425 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000426 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400427 ASSERT_TRUE(page);
428
429 // Check that there is a total of 1 annotation on its first page.
430 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
431
432 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000433 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000434 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000435 ASSERT_TRUE(annot);
436 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400437
Lei Zhanga21d5932018-02-05 18:28:38 +0000438 // Check that the annotation color is yellow.
439 unsigned int R;
440 unsigned int G;
441 unsigned int B;
442 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000443 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000444 &G, &B, &A));
445 EXPECT_EQ(255u, R);
446 EXPECT_EQ(255u, G);
447 EXPECT_EQ(0u, B);
448 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400449
Lei Zhanga21d5932018-02-05 18:28:38 +0000450 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000451 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000452 EXPECT_EQ(FPDF_OBJECT_STRING,
453 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000454 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000455 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000456 ASSERT_EQ(28u, length_bytes);
457 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000458 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000459 length_bytes));
460 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400461
Lei Zhanga21d5932018-02-05 18:28:38 +0000462 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000463 EXPECT_EQ(
464 FPDF_OBJECT_STRING,
465 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000466 length_bytes = FPDFAnnot_GetStringValue(
467 annot.get(), pdfium::annotation::kContents, nullptr, 0);
468 ASSERT_EQ(2690u, length_bytes);
469 buf = GetFPDFWideStringBuffer(length_bytes);
470 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
471 pdfium::annotation::kContents,
472 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000473 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000474 L"This is a note for that highlight annotation. Very long highlight "
475 "annotation. Long long long Long long longLong long longLong long "
476 "longLong long longLong long longLong long longLong long longLong long "
477 "longLong long longLong long longLong long longLong long longLong long "
478 "longLong long longLong long longLong long longLong long longLong long "
479 "longLong long longLong long longLong long longLong long longLong long "
480 "longLong long longLong long longLong long longLong long longLong long "
481 "longLong long longLong long longLong long longLong long longLong long "
482 "longLong long longLong long longLong long longLong long longLong long "
483 "longLong long longLong long longLong long longLong long longLong long "
484 "longLong long longLong long longLong long longLong long longLong long "
485 "longLong long longLong long longLong long longLong long longLong long "
486 "longLong long longLong long longLong long longLong long longLong long "
487 "longLong long longLong long longLong long longLong long longLong long "
488 "longLong long longLong long longLong long longLong long longLong long "
489 "longLong long longLong long longLong long longLong long longLong long "
490 "longLong long longLong long longLong long longLong long longLong long "
491 "longLong long longLong long longLong long longLong long longLong long "
492 "longLong long longLong long longLong long longLong long longLong long "
493 "longLong long long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000494 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400495
Lei Zhanga21d5932018-02-05 18:28:38 +0000496 // Check that the quadpoints are correct.
497 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000498 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000499 EXPECT_EQ(115.802643f, quadpoints.x1);
500 EXPECT_EQ(718.913940f, quadpoints.y1);
501 EXPECT_EQ(157.211182f, quadpoints.x4);
502 EXPECT_EQ(706.264465f, quadpoints.y4);
503 }
Tom Sepez507d0192018-11-07 16:37:51 +0000504 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400505}
506
Hui Yingst9b6b1542020-07-27 16:11:12 +0000507TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400508 // Open a file with three annotations and load its first page.
509 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000510 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400511 ASSERT_TRUE(page);
512
513 // Check that there is a total of 3 annotation on its first page.
514 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
515
Lei Zhanga21d5932018-02-05 18:28:38 +0000516 {
517 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000518 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000519 ASSERT_TRUE(annot);
520 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400521
Lei Zhanga21d5932018-02-05 18:28:38 +0000522 // Check that the annotation color is blue with opacity.
523 unsigned int R;
524 unsigned int G;
525 unsigned int B;
526 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000527 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000528 &G, &B, &A));
529 EXPECT_EQ(0u, R);
530 EXPECT_EQ(0u, G);
531 EXPECT_EQ(255u, B);
532 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400533
Lei Zhanga21d5932018-02-05 18:28:38 +0000534 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000535 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
536 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400537
Lei Zhang4f556b82019-04-08 16:32:41 +0000538 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000539 // Note that upon rendering, the rectangle coordinates will be adjusted.
540 FS_RECTF rect;
541 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
542 EXPECT_EQ(351.820404f, rect.left);
543 EXPECT_EQ(583.830688f, rect.bottom);
544 EXPECT_EQ(475.336090f, rect.right);
545 EXPECT_EQ(681.535034f, rect.top);
546 }
Tom Sepezef43c262018-11-07 16:41:32 +0000547 {
Hui Yingst57daee82020-10-09 05:47:20 +0000548#if defined(_SKIA_SUPPORT_) && defined(OS_APPLE)
549 static constexpr char kExpectedHash[] = "fad91b9c968fe8019a774f5e2419b8fc";
550#elif defined(_SKIA_SUPPORT_PATHS_) && defined(OS_APPLE)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000551 static constexpr char kExpectedHash[] = "acddfe688a117ead56af7b249a2cf8a1";
Hui Yingst57daee82020-10-09 05:47:20 +0000552#elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst9b6b1542020-07-27 16:11:12 +0000553 static constexpr char kExpectedHash[] = "1fb0dd8dd5f0b9bb8d076e48eb59296d";
Hui Yingst57daee82020-10-09 05:47:20 +0000554#elif defined(OS_WIN)
Lei Zhang430b5322020-07-06 22:23:36 +0000555 static constexpr char kExpectedHash[] = "49d0a81c636531a337429325273d0508";
556#else
557 static constexpr char kExpectedHash[] = "354002e1c4386d38fdde29ef8d61074a";
Hui Yingst57daee82020-10-09 05:47:20 +0000558#endif
Tom Sepezef43c262018-11-07 16:41:32 +0000559 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang430b5322020-07-06 22:23:36 +0000560 CompareBitmap(bitmap.get(), 612, 792, kExpectedHash);
Tom Sepezef43c262018-11-07 16:41:32 +0000561 }
Tom Sepez507d0192018-11-07 16:37:51 +0000562 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400563}
Jane Liu20eafda2017-06-07 10:33:24 -0400564
Lei Zhangab41f252018-12-23 03:10:50 +0000565TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400566 // Open a file with one annotation and load its first page.
567 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000568 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400569 ASSERT_TRUE(page);
570
571 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400572 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400573
574 UnloadPage(page);
575}
576
Lei Zhangab41f252018-12-23 03:10:50 +0000577TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400578 // Open a file with no annotation and load its first page.
579 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000580 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400581 ASSERT_TRUE(page);
582 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
583
Lei Zhanga21d5932018-02-05 18:28:38 +0000584 {
585 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000586 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000587 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400588
Lei Zhanga21d5932018-02-05 18:28:38 +0000589 // Check that there is now 1 annotations on this page.
590 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400591
Lei Zhanga21d5932018-02-05 18:28:38 +0000592 // Check that the subtype of the annotation is correct.
593 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
594 }
Jane Liue10509a2017-06-20 16:47:41 -0400595
Lei Zhanga21d5932018-02-05 18:28:38 +0000596 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000597 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000598 ASSERT_TRUE(annot);
599 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400600
Lei Zhanga21d5932018-02-05 18:28:38 +0000601 // Set the color of the annotation.
602 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
603 102, 153, 204));
604 // Check that the color has been set correctly.
605 unsigned int R;
606 unsigned int G;
607 unsigned int B;
608 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000609 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000610 &G, &B, &A));
611 EXPECT_EQ(51u, R);
612 EXPECT_EQ(102u, G);
613 EXPECT_EQ(153u, B);
614 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400615
Lei Zhanga21d5932018-02-05 18:28:38 +0000616 // Change the color of the annotation.
617 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
618 153, 102, 51));
619 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000620 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000621 &G, &B, &A));
622 EXPECT_EQ(204u, R);
623 EXPECT_EQ(153u, G);
624 EXPECT_EQ(102u, B);
625 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400626
Lei Zhanga21d5932018-02-05 18:28:38 +0000627 // Set the annotation rectangle.
628 FS_RECTF rect;
629 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
630 EXPECT_EQ(0.f, rect.left);
631 EXPECT_EQ(0.f, rect.right);
632 rect.left = 35;
633 rect.bottom = 150;
634 rect.right = 53;
635 rect.top = 165;
636 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
637 // Check that the annotation rectangle has been set correctly.
638 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
639 EXPECT_EQ(35.f, rect.left);
640 EXPECT_EQ(150.f, rect.bottom);
641 EXPECT_EQ(53.f, rect.right);
642 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400643
Lei Zhanga21d5932018-02-05 18:28:38 +0000644 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000645 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000646 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000647 ASSERT_TRUE(FPDFAnnot_SetStringValue(
648 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000649 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000650 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000651 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000652 ASSERT_EQ(74u, length_bytes);
653 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
654 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
655 pdfium::annotation::kContents,
656 buf.data(), length_bytes));
657 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000658 }
Jane Liu20eafda2017-06-07 10:33:24 -0400659 UnloadPage(page);
660}
661
Hui Yingstb3490322020-07-22 00:53:29 +0000662TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400663 // Open a file with one annotation and load its first page.
664 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000665 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400666 ASSERT_TRUE(page);
667
668 // Check that there is a total of one annotation on its first page, and verify
669 // its quadpoints.
670 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400671 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000672 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000673 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000674 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000675 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000676 EXPECT_EQ(115.802643f, quadpoints.x1);
677 EXPECT_EQ(718.913940f, quadpoints.y1);
678 EXPECT_EQ(157.211182f, quadpoints.x4);
679 EXPECT_EQ(706.264465f, quadpoints.y4);
680 }
Jane Liu20eafda2017-06-07 10:33:24 -0400681
682 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000683 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000684 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000685 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
686 ASSERT_TRUE(annot);
687 quadpoints.x1 = 140.802643f;
688 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000689 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000690 }
Jane Liu20eafda2017-06-07 10:33:24 -0400691
Lei Zhangec618142021-04-13 17:36:46 +0000692 // Save the document and close the page.
Jane Liu20eafda2017-06-07 10:33:24 -0400693 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000694 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400695
696 // Open the saved document.
Hui Yingst57daee82020-10-09 05:47:20 +0000697#if defined(_SKIA_SUPPORT_) && defined(OS_APPLE)
698 static const char kChecksum[] = "899387ae792390cd0d83cf7e2bbebfb5";
699#elif defined(_SKIA_SUPPORT_PATHS_) && defined(OS_APPLE)
700 static const char kChecksum[] = "e40e235ee35f47ff28dda009aaaf36df";
701#elif defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstb3490322020-07-22 00:53:29 +0000702 static const char kChecksum[] = "798fa41303381c9ba6d99092f5cd4d2b";
703#else
704 static const char kChecksum[] = "dba153419f67b7c0c0e3d22d3e8910d5";
705#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400706
Lei Zhang0b494052019-01-31 21:41:15 +0000707 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000708 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +0000709 ASSERT_TRUE(page);
Hui Yingstb3490322020-07-22 00:53:29 +0000710 VerifySavedRendering(page, 612, 792, kChecksum);
Jane Liu20eafda2017-06-07 10:33:24 -0400711
712 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000713 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400714
Lei Zhanga21d5932018-02-05 18:28:38 +0000715 {
716 // Check that the second annotation is an underline annotation and verify
717 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000718 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000719 ASSERT_TRUE(new_annot);
720 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
721 FS_QUADPOINTSF new_quadpoints;
722 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000723 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000724 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
725 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
726 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
727 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
728 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400729
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000730 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400731 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400732}
Jane Liu06462752017-06-27 16:41:14 -0400733
Lei Zhangab41f252018-12-23 03:10:50 +0000734TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000735 // Open a file with four annotations and load its first page.
736 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
737 FPDF_PAGE page = LoadPage(0);
738 ASSERT_TRUE(page);
739 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
740
741 // Retrieve the highlight annotation.
742 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
743 ASSERT_TRUE(annot);
744 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
745
746 FS_QUADPOINTSF quadpoints;
747 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
748
749 {
750 // Verify the current one set of quadpoints.
751 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
752
753 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
754 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
755 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
756 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
757 }
758
759 {
760 // Update the quadpoints.
761 FS_QUADPOINTSF new_quadpoints = quadpoints;
762 new_quadpoints.y1 -= 20.f;
763 new_quadpoints.y2 -= 20.f;
764 new_quadpoints.y3 -= 20.f;
765 new_quadpoints.y4 -= 20.f;
766 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
767
768 // Verify added quadpoint set
769 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
770 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
771 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
772 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
773 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
774 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
775 }
776
777 {
778 // Append a new set of quadpoints.
779 FS_QUADPOINTSF new_quadpoints = quadpoints;
780 new_quadpoints.y1 += 20.f;
781 new_quadpoints.y2 += 20.f;
782 new_quadpoints.y3 += 20.f;
783 new_quadpoints.y4 += 20.f;
784 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
785
786 // Verify added quadpoint set
787 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
788 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
789 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
790 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
791 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
792 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
793 }
794
795 {
796 // Setting and getting quadpoints at out-of-bound index should fail
797 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
798 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
799 }
800
801 FPDFPage_CloseAnnot(annot);
802
803 // Retrieve the square annotation
804 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
805
806 {
807 // Check that attempting to set its quadpoints would fail
808 ASSERT_TRUE(squareAnnot);
809 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
810 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
811 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
812 }
813
814 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000815 UnloadPage(page);
816}
817
Hui Yingstb64cd122020-07-27 16:01:02 +0000818// TODO(crbug.com/pdfium/1569): Fix this issue and enable the test for Skia.
819#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +0000820#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
821#else
822#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
823#endif
824TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Hui Yingstb64cd122020-07-27 16:01:02 +0000825#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
826 static const char kMd5Original[] = "00e70eb543c2a6e8f8aafb4ee951d9bf";
Lei Zhang4f556b82019-04-08 16:32:41 +0000827 static const char kMd5ModifiedHighlight[] =
Hui Yingstb64cd122020-07-27 16:01:02 +0000828 "7638c4a8fe4aabbf8704e198f49b3198";
829 static const char kMd5ModifiedSquare[] = "54f507af6af63de877b9cafdab1bbdaa";
830#else
831#if defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000832 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
833 static const char kMd5ModifiedHighlight[] =
834 "66f3caef3a7d488a4fa1ad37fc06310e";
835 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Lei Zhang0c03d632020-07-30 17:05:36 +0000836#elif defined(OS_APPLE)
Hui Yingstb64cd122020-07-27 16:01:02 +0000837 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
838 static const char kMd5ModifiedHighlight[] =
839 "e64bf648f6e9354d1f3eedb47a2c9498";
840 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Jane Liub370e5a2017-08-16 13:24:58 -0400841#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000842 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
843 static const char kMd5ModifiedHighlight[] =
844 "66f3caef3a7d488a4fa1ad37fc06310e";
845 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400846#endif
Hui Yingstb64cd122020-07-27 16:01:02 +0000847#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liub370e5a2017-08-16 13:24:58 -0400848
Jane Liu06462752017-06-27 16:41:14 -0400849 // Open a file with four annotations and load its first page.
850 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000851 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400852 ASSERT_TRUE(page);
853 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
854
Jane Liub370e5a2017-08-16 13:24:58 -0400855 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000856 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000857 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000858 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000859 }
Jane Liub370e5a2017-08-16 13:24:58 -0400860
Jane Liu0c6b07d2017-08-15 10:50:22 -0400861 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400862 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000863
864 // Retrieve the highlight annotation which has its AP stream already defined.
865 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000866 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000867 ASSERT_TRUE(annot);
868 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
869
870 // Check that color cannot be set when an AP stream is defined already.
871 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
872 102, 153, 204));
873
874 // Verify its attachment points.
875 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000876 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000877 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
878 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
879 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
880 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
881
882 // Check that updating the attachment points would succeed.
883 quadpoints.x1 -= 50.f;
884 quadpoints.x2 -= 50.f;
885 quadpoints.x3 -= 50.f;
886 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000887 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000888 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000889 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000890 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
891 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
892 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
893 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
894
895 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000896 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000897 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000898 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000899 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000900
901 // Verify its annotation rectangle.
902 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
903 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
904 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
905 EXPECT_NEAR(136.325f, rect.right, 0.001f);
906 EXPECT_NEAR(721.292f, rect.top, 0.001f);
907
908 // Check that updating the rectangle would succeed.
909 rect.left -= 60.f;
910 rect.right -= 60.f;
911 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
912 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
913 EXPECT_EQ(rect.right, new_rect.right);
914 }
Jane Liu06462752017-06-27 16:41:14 -0400915
Jane Liub370e5a2017-08-16 13:24:58 -0400916 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000917 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000918 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000919 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000920 }
Jane Liub370e5a2017-08-16 13:24:58 -0400921
Lei Zhanga21d5932018-02-05 18:28:38 +0000922 {
923 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000924 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000925 ASSERT_TRUE(annot);
926 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400927
Lei Zhanga21d5932018-02-05 18:28:38 +0000928 // Check that updating the rectangle would succeed.
929 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
930 rect.left += 70.f;
931 rect.right += 70.f;
932 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
933 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
934 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400935
Lei Zhanga21d5932018-02-05 18:28:38 +0000936 // Check that updating the rectangle changes the square annotation's
937 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000938 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000939 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000940 }
Jane Liub370e5a2017-08-16 13:24:58 -0400941
Jane Liu06462752017-06-27 16:41:14 -0400942 UnloadPage(page);
943}
Jane Liu8ce58f52017-06-29 13:40:22 -0400944
Lei Zhangab41f252018-12-23 03:10:50 +0000945TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000946 // Open a file with multiline markup annotations.
947 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
948 FPDF_PAGE page = LoadPage(0);
949 ASSERT_TRUE(page);
950 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000951 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000952 ASSERT_TRUE(annot);
953
954 // This is a three line annotation.
955 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
956 }
957 UnloadPage(page);
958
959 // null annotation should return 0
960 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
961}
962
Lei Zhangab41f252018-12-23 03:10:50 +0000963TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400964 // Open a file with 3 annotations on its first page.
965 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000966 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400967 ASSERT_TRUE(page);
968 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
969
Jane Liu0c6b07d2017-08-15 10:50:22 -0400970 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400971
Lei Zhanga21d5932018-02-05 18:28:38 +0000972 // Check that the annotations have the expected rectangle coordinates.
973 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000974 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000975 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
976 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
977 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400978
Lei Zhanga21d5932018-02-05 18:28:38 +0000979 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000980 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000981 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
982 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
983 }
984
985 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000986 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000987 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
988 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
989 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400990
991 // Check that nothing happens when attempting to remove an annotation with an
992 // out-of-bound index.
993 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
994 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
995 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
996
997 // Remove the second annotation.
998 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
999 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1000 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
1001
Lei Zhangec618142021-04-13 17:36:46 +00001002 // Save the document and close the page.
Jane Liu8ce58f52017-06-29 13:40:22 -04001003 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +00001004 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -04001005
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001006 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -04001007 // Open the saved document.
1008 std::string new_file = GetString();
1009 FPDF_FILEACCESS file_access;
1010 memset(&file_access, 0, sizeof(file_access));
1011 file_access.m_FileLen = new_file.size();
1012 file_access.m_GetBlock = GetBlockFromString;
1013 file_access.m_Param = &new_file;
1014 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
1015 ASSERT_TRUE(new_doc);
1016 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
1017 ASSERT_TRUE(new_page);
1018
1019 // Check that the saved document has 2 annotations on the first page.
1020 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
1021
Lei Zhanga21d5932018-02-05 18:28:38 +00001022 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1023 // by verifying their rectangle coordinates.
1024 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001025 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001026 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1027 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1028 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001029
Lei Zhanga21d5932018-02-05 18:28:38 +00001030 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001031 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001032 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1033 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1034 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001035 FPDF_ClosePage(new_page);
1036 FPDF_CloseDocument(new_doc);
1037}
Jane Liu8ce58f52017-06-29 13:40:22 -04001038
Hui Yingst4a21df02020-05-13 03:15:44 +00001039TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001040#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001041#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst4a21df02020-05-13 03:15:44 +00001042 static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa";
1043 static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622";
1044 static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001045#else
Hui Yingst4a21df02020-05-13 03:15:44 +00001046 static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4";
1047 static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f";
1048 static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001049#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst4a21df02020-05-13 03:15:44 +00001050#else
1051#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001052 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
1053 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
1054 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Lei Zhang0c03d632020-07-30 17:05:36 +00001055#elif defined(OS_APPLE)
Hui Yingst4a21df02020-05-13 03:15:44 +00001056 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
1057 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
1058 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Jane Liubaa7ff42017-06-29 19:18:23 -04001059#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001060 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
1061 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
1062 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -04001063#endif
Hui Yingst4a21df02020-05-13 03:15:44 +00001064#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liubaa7ff42017-06-29 19:18:23 -04001065
1066 // Open a file with two annotations and load its first page.
1067 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001068 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001069 ASSERT_TRUE(page);
1070 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1071
1072 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001073 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001074 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001075 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001076 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001077
Lei Zhanga21d5932018-02-05 18:28:38 +00001078 {
1079 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001080 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001081 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001082
Lei Zhanga21d5932018-02-05 18:28:38 +00001083 // Check that this annotation has one path object and retrieve it.
1084 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001085 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001086 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1087 EXPECT_FALSE(path);
1088 path = FPDFAnnot_GetObject(annot.get(), 0);
1089 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1090 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001091
Lei Zhanga21d5932018-02-05 18:28:38 +00001092 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001093 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001094 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001095
Lei Zhanga21d5932018-02-05 18:28:38 +00001096 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001097 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001098 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001099 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001100 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001101
Lei Zhanga21d5932018-02-05 18:28:38 +00001102 // Add a second path object to the same annotation.
1103 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1104 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001105 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1106 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001107 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1108 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1109 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001110
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001111 // The object is in the annontation, not in the page, so the page object
1112 // array should not change.
1113 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1114
Lei Zhanga21d5932018-02-05 18:28:38 +00001115 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001116 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001117 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001118 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001119 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001120
Lei Zhanga21d5932018-02-05 18:28:38 +00001121 // Delete the newly added path object.
1122 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1123 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001124 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001125 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001126
1127 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001128 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001129 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001130 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001131 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001132
Jane Liubaa7ff42017-06-29 19:18:23 -04001133 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001134
Lei Zhanga21d5932018-02-05 18:28:38 +00001135 {
1136 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001137 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001138 ASSERT_TRUE(annot);
1139 rect.left = 200.f;
1140 rect.bottom = 400.f;
1141 rect.right = 500.f;
1142 rect.top = 600.f;
1143 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001144
Lei Zhanga21d5932018-02-05 18:28:38 +00001145 // Add a new path to the annotation.
1146 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1147 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1148 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1149 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1150 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001151 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1152 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001153 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1154 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1155 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1156
1157 // Check that the annotation's bounding box came from its rectangle.
1158 FS_RECTF new_rect;
1159 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1160 EXPECT_EQ(rect.left, new_rect.left);
1161 EXPECT_EQ(rect.bottom, new_rect.bottom);
1162 EXPECT_EQ(rect.right, new_rect.right);
1163 EXPECT_EQ(rect.top, new_rect.top);
1164 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001165
Lei Zhangec618142021-04-13 17:36:46 +00001166 // Save the document and close the page.
Jane Liubaa7ff42017-06-29 19:18:23 -04001167 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001168 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001169
1170 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001171 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001172 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001173 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001174 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001175
Jane Liu36567742017-07-06 11:13:35 -04001176 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001177 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001178
Lei Zhanga21d5932018-02-05 18:28:38 +00001179 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001180 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001181 ASSERT_TRUE(annot);
1182 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001183
Lei Zhanga21d5932018-02-05 18:28:38 +00001184 // Check that the new annotation's rectangle is as defined.
1185 FS_RECTF new_rect;
1186 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1187 EXPECT_EQ(rect.left, new_rect.left);
1188 EXPECT_EQ(rect.bottom, new_rect.bottom);
1189 EXPECT_EQ(rect.right, new_rect.right);
1190 EXPECT_EQ(rect.top, new_rect.top);
1191 }
1192
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001193 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001194 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001195}
Jane Liub137e752017-07-05 15:04:33 -04001196
Lei Zhangab41f252018-12-23 03:10:50 +00001197TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001198 // Open a file with an annotation and load its first page.
1199 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001200 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001201 ASSERT_TRUE(page);
1202
1203 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001204 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001205 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001206 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1207 }
Jane Liub137e752017-07-05 15:04:33 -04001208
Lei Zhanga21d5932018-02-05 18:28:38 +00001209 {
1210 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001211 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001212 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001213
Lei Zhanga21d5932018-02-05 18:28:38 +00001214 // Check that the original flag values are as expected.
1215 int flags = FPDFAnnot_GetFlags(annot.get());
1216 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1217 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001218
Lei Zhanga21d5932018-02-05 18:28:38 +00001219 // Set the HIDDEN flag.
1220 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1221 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1222 flags = FPDFAnnot_GetFlags(annot.get());
1223 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1224 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001225
Lei Zhanga21d5932018-02-05 18:28:38 +00001226 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001227 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001228 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001229 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001230 }
Jane Liub137e752017-07-05 15:04:33 -04001231
Lei Zhanga21d5932018-02-05 18:28:38 +00001232 // Unset the HIDDEN flag.
1233 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1234 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1235 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1236 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1237 flags = FPDFAnnot_GetFlags(annot.get());
1238 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1239 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001240
Lei Zhanga21d5932018-02-05 18:28:38 +00001241 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001242 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001243 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001244 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1245 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001246 }
Jane Liub137e752017-07-05 15:04:33 -04001247
Jane Liub137e752017-07-05 15:04:33 -04001248 UnloadPage(page);
1249}
Jane Liu36567742017-07-06 11:13:35 -04001250
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001251// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1252#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001253#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1254#else
1255#define MAYBE_AddAndModifyImage AddAndModifyImage
1256#endif
1257TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001258#if defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001259#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001260 static const char kMd5NewImage[] = "26a8eb30937226a677839379e0d7ae1a";
1261 static const char kMd5ModifiedImage[] = "2985114b32ba1a96be78ee643fe31aa5";
1262#else
1263 static const char kMd5NewImage[] = "14012ab500b4671fa73dd760129a8a93";
1264 static const char kMd5ModifiedImage[] = "5f97f98f58ed04dc393f31460485f1a2";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001265#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001266#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001267#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001268 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
1269 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +00001270#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001271 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
1272 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -04001273#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001274 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
1275 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -04001276#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001277#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001278
1279 // Open a file with two annotations and load its first page.
1280 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001281 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001282 ASSERT_TRUE(page);
1283 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1284
1285 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001286 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001287 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001288 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001289 }
Jane Liu36567742017-07-06 11:13:35 -04001290
Jane Liu36567742017-07-06 11:13:35 -04001291 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001292 FPDF_BITMAP image_bitmap;
1293
1294 {
1295 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001296 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001297 ASSERT_TRUE(annot);
1298 FS_RECTF rect;
1299 rect.left = 200.f;
1300 rect.bottom = 600.f;
1301 rect.right = 400.f;
1302 rect.top = 800.f;
1303 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1304
1305 // Add a solid-color translucent image object to the new annotation.
1306 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1307 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1308 0xeeeecccc);
1309 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1310 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1311 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1312 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1313 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1314 kBitmapSize, 0, 0));
1315 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1316 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1317 }
Jane Liu36567742017-07-06 11:13:35 -04001318
1319 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001320 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001321 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001322 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001323 }
Jane Liu36567742017-07-06 11:13:35 -04001324
Lei Zhanga21d5932018-02-05 18:28:38 +00001325 {
1326 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001327 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001328 ASSERT_TRUE(annot);
1329 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1330 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1331 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001332
Lei Zhanga21d5932018-02-05 18:28:38 +00001333 // Modify the image in the new annotation.
1334 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1335 0xff000000);
1336 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1337 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1338 }
Jane Liu36567742017-07-06 11:13:35 -04001339
Lei Zhangec618142021-04-13 17:36:46 +00001340 // Save the document and close the page.
Jane Liu36567742017-07-06 11:13:35 -04001341 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001342 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001343 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001344
1345 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001346 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001347}
1348
Hui Yingst6cd754f2020-05-14 04:05:25 +00001349TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001350#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001351#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001352 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1353 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001354#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001355 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1356 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001357#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001358#else
1359#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001360 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1361 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Lei Zhang0c03d632020-07-30 17:05:36 +00001362#elif defined(OS_APPLE)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001363 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1364 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001365#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001366 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1367 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001368#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001369#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001370
1371 // Open a file with two annotations and load its first page.
1372 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001373 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001374 ASSERT_TRUE(page);
1375 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1376
1377 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001378 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001379 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001380 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001381 }
Jane Liu36567742017-07-06 11:13:35 -04001382
Lei Zhanga21d5932018-02-05 18:28:38 +00001383 {
1384 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001385 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001386 ASSERT_TRUE(annot);
1387 FS_RECTF rect;
1388 rect.left = 200.f;
1389 rect.bottom = 550.f;
1390 rect.right = 450.f;
1391 rect.top = 650.f;
1392 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001393
Lei Zhanga21d5932018-02-05 18:28:38 +00001394 // Add a translucent text object to the new annotation.
1395 FPDF_PAGEOBJECT text_object =
1396 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1397 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001398 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001399 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1400 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001401 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001402 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1403 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1404 }
Jane Liu36567742017-07-06 11:13:35 -04001405
1406 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001407 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001408 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001409 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001410 }
Jane Liu36567742017-07-06 11:13:35 -04001411
Lei Zhanga21d5932018-02-05 18:28:38 +00001412 {
1413 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001414 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001415 ASSERT_TRUE(annot);
1416 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1417 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1418 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001419
Lei Zhanga21d5932018-02-05 18:28:38 +00001420 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001421 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001422 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1423 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1424 }
Jane Liu36567742017-07-06 11:13:35 -04001425
1426 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001427 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001428 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001429 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001430 }
Jane Liu36567742017-07-06 11:13:35 -04001431
1432 // Remove the new annotation, and check that the page renders as before.
1433 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001434 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001435 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001436 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001437 }
Jane Liu36567742017-07-06 11:13:35 -04001438
1439 UnloadPage(page);
1440}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001441
Hui Yingst9b6b1542020-07-27 16:11:12 +00001442TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001443 // Open a file with four annotations and load its first page.
1444 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001445 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001446 ASSERT_TRUE(page);
1447
Lei Zhang4f556b82019-04-08 16:32:41 +00001448 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001449
Lei Zhanga21d5932018-02-05 18:28:38 +00001450 {
1451 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001452 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001453 ASSERT_TRUE(annot);
1454
1455 // Check that a non-existent key does not exist.
1456 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1457
1458 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001459 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001460 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001461 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1462 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1463 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001464
1465 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001466 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001467 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001468 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001469 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001470 ASSERT_EQ(66u, length_bytes);
1471 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1472 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1473 length_bytes));
1474 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1475 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001476
1477 // Check that the string value of the modified date is correct.
1478 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001479 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1480 nullptr, 0);
1481 ASSERT_EQ(44u, length_bytes);
1482 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001483 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001484 buf.data(), length_bytes));
1485 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001486
1487 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001488 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001489 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1490 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001491 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001492
Lei Zhangec618142021-04-13 17:36:46 +00001493 // Save the document and close the page.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001494 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001495 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001496
Hui Yingst9b6b1542020-07-27 16:11:12 +00001497#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhangfe8e7582020-08-05 19:23:16 +00001498#if defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001499 static const char kMd5[] = "7a2b712ca88d7b71f125ea3f9c88e57a";
1500#else
1501 static const char kMd5[] = "626d25c5aa5baf67d22d9a0e1c23f6aa";
Lei Zhangfe8e7582020-08-05 19:23:16 +00001502#endif // defined(OS_LINUX) || defined(OS_CHROMEOS)
Hui Yingst9b6b1542020-07-27 16:11:12 +00001503#else
Lei Zhang0c03d632020-07-30 17:05:36 +00001504#if defined(OS_APPLE)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001505 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001506#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001507 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001508#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001509 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001510#endif
Hui Yingst9b6b1542020-07-27 16:11:12 +00001511#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Dan Sinclair971a6742018-03-28 19:23:25 +00001512
1513 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001514 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001515 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001516 ASSERT_TRUE(page);
Lei Zhang4f556b82019-04-08 16:32:41 +00001517 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001518 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001519 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001520
Lei Zhanga21d5932018-02-05 18:28:38 +00001521 // Check that the string value of the modified date is the newly-set value.
1522 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001523 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001524 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001525 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001526 ASSERT_EQ(44u, length_bytes);
1527 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001528 EXPECT_EQ(44u,
1529 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001530 buf.data(), length_bytes));
1531 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001532 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001533
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001534 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001535 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001536}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001537
rycsmith3e785602019-03-05 21:48:36 +00001538TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001539 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001540 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1541 FPDF_PAGE page = LoadPage(0);
1542 ASSERT_TRUE(page);
1543 {
1544 // First two annotations do not have "MaxLen" attribute.
1545 for (int i = 0; i < 2; i++) {
1546 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1547 ASSERT_TRUE(annot);
1548
1549 // Verify that no "MaxLen" key present.
1550 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1551
1552 float value;
1553 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1554 }
1555
1556 // Annotation in index 2 has "MaxLen" of 10.
1557 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1558 ASSERT_TRUE(annot);
1559
1560 // Verify that "MaxLen" key present.
1561 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1562
1563 float value;
1564 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1565 EXPECT_FLOAT_EQ(10.0f, value);
1566
1567 // Check bad inputs.
1568 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1569 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1570 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1571 // Ask for key that exists but is not a number.
1572 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1573 }
1574
1575 UnloadPage(page);
1576}
1577
Lei Zhangab41f252018-12-23 03:10:50 +00001578TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001579 // Open a file with four annotations and load its first page.
1580 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001581 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001582 ASSERT_TRUE(page);
1583
Lei Zhanga21d5932018-02-05 18:28:38 +00001584 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001585 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1586 static constexpr size_t kExpectNormalAPLength = 73970;
1587
Lei Zhanga21d5932018-02-05 18:28:38 +00001588 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001589 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001590 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001591
Lei Zhanga21d5932018-02-05 18:28:38 +00001592 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001593 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001594 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001595 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001596
Lei Zhanga21d5932018-02-05 18:28:38 +00001597 // Check that the string value of an AP is not returned if the buffer is too
1598 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001599 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1600 // Write in the buffer to verify it's not overwritten.
1601 memcpy(buf.data(), "abcdefgh", 8);
1602 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001603 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001604 buf.data(), normal_length_bytes - 1));
1605 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001606
Lei Zhanga21d5932018-02-05 18:28:38 +00001607 // Check that the string value of an AP is returned through a buffer that is
1608 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001609 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001610 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001611 buf.data(), normal_length_bytes));
1612 EXPECT_EQ(kMd5NormalAP,
1613 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1614 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001615
Lei Zhanga21d5932018-02-05 18:28:38 +00001616 // Check that the string value of an AP is returned through a buffer that is
1617 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001618 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1619 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001620 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001621 buf.data(), normal_length_bytes + 2));
1622 EXPECT_EQ(kMd5NormalAP,
1623 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1624 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001625
Lei Zhanga21d5932018-02-05 18:28:38 +00001626 // Check that getting an AP for a mode that does not have an AP returns an
1627 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001628 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001629 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001630 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001631
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001632 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001633 EXPECT_EQ(2u,
1634 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001635 buf.data(), 1000));
1636 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001637
Lei Zhanga21d5932018-02-05 18:28:38 +00001638 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001639 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001640 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001641 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001642 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001643 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001644 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001645
Lei Zhanga21d5932018-02-05 18:28:38 +00001646 // Set the AP correctly now.
1647 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001648 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001649
Lei Zhanga21d5932018-02-05 18:28:38 +00001650 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001651 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001652 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001653 ASSERT_EQ(24u, rollover_length_bytes);
1654 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001655 EXPECT_EQ(24u,
1656 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001657 buf.data(), rollover_length_bytes));
1658 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001659
Lei Zhanga21d5932018-02-05 18:28:38 +00001660 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001661 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1662 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001663 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001664 buf.data(), normal_length_bytes));
1665 EXPECT_EQ(kMd5NormalAP,
1666 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1667 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001668 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001669
1670 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001671 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001672 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001673
Lei Zhang0b494052019-01-31 21:41:15 +00001674 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001675 page = LoadSavedPage(0);
Lei Zhangec618142021-04-13 17:36:46 +00001676 ASSERT_TRUE(page);
Lei Zhanga21d5932018-02-05 18:28:38 +00001677 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001678 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001679
Lei Zhanga21d5932018-02-05 18:28:38 +00001680 // Check that the new annotation value is equal to the value we set before
1681 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001682 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001683 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001684 ASSERT_EQ(24u, rollover_length_bytes);
1685 std::vector<FPDF_WCHAR> buf =
1686 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001687 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1688 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001689 buf.data(), rollover_length_bytes));
1690 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001691 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001692
1693 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001694 CloseSavedPage(page);
1695 CloseSavedDocument();
1696}
1697
Lei Zhangab41f252018-12-23 03:10:50 +00001698TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001699 // Open a file with four annotations and load its first page.
1700 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001701 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001702 ASSERT_TRUE(page);
1703
Lei Zhanga21d5932018-02-05 18:28:38 +00001704 {
1705 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001706 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001707 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001708
Lei Zhanga21d5932018-02-05 18:28:38 +00001709 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001710 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001711 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001712 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001713 EXPECT_EQ(73970u,
1714 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1715 nullptr, 0));
1716 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1717 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001718
Lei Zhanga21d5932018-02-05 18:28:38 +00001719 // Check that setting the Down AP to null removes the Down entry but keeps
1720 // Normal intact.
1721 EXPECT_TRUE(
1722 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1723 EXPECT_EQ(73970u,
1724 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1725 nullptr, 0));
1726 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1727 nullptr, 0));
1728 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001729
Lei Zhang75c81712018-02-08 17:22:39 +00001730 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001731}
1732
Lei Zhangab41f252018-12-23 03:10:50 +00001733TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001734 // Open a file with four annotations and load its first page.
1735 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001736 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001737 ASSERT_TRUE(page);
1738
Lei Zhanga21d5932018-02-05 18:28:38 +00001739 {
1740 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001741 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001742 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001743
Lei Zhanga21d5932018-02-05 18:28:38 +00001744 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001745 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001746 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001747 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001748 EXPECT_EQ(73970u,
1749 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1750 nullptr, 0));
1751 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1752 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001753
Lei Zhanga21d5932018-02-05 18:28:38 +00001754 // Check that setting the Normal AP to null removes the whole AP dictionary.
1755 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1756 nullptr));
1757 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1758 nullptr, 0));
1759 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1760 nullptr, 0));
1761 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001762
Lei Zhang75c81712018-02-08 17:22:39 +00001763 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001764}
1765
Lei Zhangab41f252018-12-23 03:10:50 +00001766TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001767 // Open a file with annotations and load its first page.
1768 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001769 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001770 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001771 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001772
Lei Zhanga21d5932018-02-05 18:28:38 +00001773 {
1774 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001775 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001776 ASSERT_TRUE(annot);
1777 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1778 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001779 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001780 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1781 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1782 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001783
Lei Zhanga21d5932018-02-05 18:28:38 +00001784 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001785 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001786 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1787 ASSERT_TRUE(popup);
1788 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1789 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1790 FS_RECTF rect;
1791 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1792 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1793 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001794
Lei Zhanga21d5932018-02-05 18:28:38 +00001795 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1796 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001797 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001798 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1799 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001800
Lei Zhanga21d5932018-02-05 18:28:38 +00001801 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1802 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001803 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001804 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001805 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1806 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001807 }
Jane Liu300bb272017-08-21 14:37:53 -04001808
Jane Liu300bb272017-08-21 14:37:53 -04001809 UnloadPage(page);
1810}
1811
Lei Zhangab41f252018-12-23 03:10:50 +00001812TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001813 // Open file with form text fields.
1814 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001815 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001816 ASSERT_TRUE(page);
1817
Lei Zhanga21d5932018-02-05 18:28:38 +00001818 {
1819 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001820 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001821 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001822
Lei Zhanga21d5932018-02-05 18:28:38 +00001823 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001824 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001825 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001826 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001827 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001828
Lei Zhanga21d5932018-02-05 18:28:38 +00001829 {
1830 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001831 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001832 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001833
Lei Zhanga21d5932018-02-05 18:28:38 +00001834 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001835 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001836 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001837 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1838 }
1839
1840 {
1841 // Retrieve the fourth annotation: user-editable password text field.
1842 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1843 ASSERT_TRUE(annot);
1844
1845 // Check that the flag values are as expected.
1846 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1847 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1848 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001849 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001850
1851 UnloadPage(page);
1852}
1853
Lei Zhangab41f252018-12-23 03:10:50 +00001854TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001855 // Open file with form text fields.
1856 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001857 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001858 ASSERT_TRUE(page);
1859
Lei Zhanga21d5932018-02-05 18:28:38 +00001860 {
1861 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001862 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001863 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001864
Lei Zhanga21d5932018-02-05 18:28:38 +00001865 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001866 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001867 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1868 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1869 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1870 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001871
Lei Zhanga21d5932018-02-05 18:28:38 +00001872 {
1873 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001874 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001875 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001876
Lei Zhanga21d5932018-02-05 18:28:38 +00001877 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001878 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001879 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1880 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1881 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1882 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001883
Lei Zhanga21d5932018-02-05 18:28:38 +00001884 {
1885 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001886 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001887 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001888
Lei Zhanga21d5932018-02-05 18:28:38 +00001889 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001890 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001891 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1892 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1893 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1894 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001895
1896 UnloadPage(page);
1897}
Diana Gage40870db2017-07-19 18:16:03 -07001898
Lei Zhangab41f252018-12-23 03:10:50 +00001899TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001900 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001901 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001902 FPDF_PAGE page = LoadPage(0);
1903 ASSERT_TRUE(page);
1904
1905 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001906 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1907 EXPECT_FALSE(
1908 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001909
Lei Zhang8da98232019-12-11 23:29:33 +00001910 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001911 {
1912 // Verify there is an annotation.
1913 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001914 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001915 EXPECT_TRUE(annot);
1916 }
1917
1918 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001919 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1920 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1921 EXPECT_FALSE(
1922 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001923
1924 UnloadPage(page);
1925}
1926
Lei Zhangab41f252018-12-23 03:10:50 +00001927TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001928 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001929 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001930 FPDF_PAGE page = LoadPage(0);
1931 ASSERT_TRUE(page);
1932
Lei Zhanga21d5932018-02-05 18:28:38 +00001933 {
1934 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001935 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001936 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001937 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001938 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001939
Lei Zhanga21d5932018-02-05 18:28:38 +00001940 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001941 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001942 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1943 }
Diana Gage40870db2017-07-19 18:16:03 -07001944
Lei Zhanga21d5932018-02-05 18:28:38 +00001945 {
1946 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001947 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001948 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001949 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001950 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001951
Lei Zhanga21d5932018-02-05 18:28:38 +00001952 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001953 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001954 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1955 }
Diana Gage40870db2017-07-19 18:16:03 -07001956
1957 UnloadPage(page);
1958}
1959
Lei Zhangab41f252018-12-23 03:10:50 +00001960TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001961 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001962 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001963 FPDF_PAGE page = LoadPage(0);
1964 ASSERT_TRUE(page);
1965
Lei Zhanga21d5932018-02-05 18:28:38 +00001966 {
1967 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001968 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001969 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001970 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001971 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001972
Lei Zhanga21d5932018-02-05 18:28:38 +00001973 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001974 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001975 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1976 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1977 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1978 }
Diana Gage40870db2017-07-19 18:16:03 -07001979
Lei Zhanga21d5932018-02-05 18:28:38 +00001980 {
1981 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001982 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001983 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001984 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001985 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001986
Lei Zhanga21d5932018-02-05 18:28:38 +00001987 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001988 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001989 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1990 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1991 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1992 }
Diana Gage40870db2017-07-19 18:16:03 -07001993
Lei Zhanga21d5932018-02-05 18:28:38 +00001994 {
1995 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001996 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001997 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001998 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001999 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07002000
Lei Zhanga21d5932018-02-05 18:28:38 +00002001 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00002002 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00002003 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
2004 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2005 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
2006 }
Diana Gage40870db2017-07-19 18:16:03 -07002007
2008 UnloadPage(page);
2009}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002010
Hui Yingst603dcd82020-11-09 22:36:46 +00002011TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
Lei Zhang03e5e682019-09-16 19:45:55 +00002012#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst603dcd82020-11-09 22:36:46 +00002013 static const char kExpectedBitmap[] = "a1ea1ceebb26922fae576cb79ce63af0";
Lei Zhang03e5e682019-09-16 19:45:55 +00002014#else
Lei Zhang992e7e22019-02-04 19:20:58 +00002015 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
Hui Yingst603dcd82020-11-09 22:36:46 +00002016#endif
2017 static constexpr size_t kExpectedSize = 1609;
Lei Zhang992e7e22019-02-04 19:20:58 +00002018
2019 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2020
2021 FPDF_PAGE page = LoadPage(0);
2022 ASSERT_TRUE(page);
2023
2024 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2025 EXPECT_EQ(kExpectedSize, GetString().size());
2026 ClearString();
2027
2028 for (size_t i = 0; i < 10; ++i) {
2029 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2030 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2031
2032 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2033 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2034 // equal, not bigger.
2035 EXPECT_LT(kExpectedSize, GetString().size());
2036 ClearString();
2037 }
2038
2039 UnloadPage(page);
2040}
2041
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002042TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2043 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2044 FPDF_PAGE page = LoadPage(0);
2045 ASSERT_TRUE(page);
2046 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2047
2048 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002049 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002050 static const size_t kBufSize = 12;
2051 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002052
2053 {
2054 // Add a text annotation to the page.
2055 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2056 ASSERT_TRUE(annot);
2057 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2058 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2059
2060 // Make sure there is no test key, add set a value there, and read it back.
2061 std::fill(buf.begin(), buf.end(), 'x');
2062 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002063 kBufSize));
2064 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002065
Lei Zhangf0f67682019-04-08 17:03:21 +00002066 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002067 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2068
2069 std::fill(buf.begin(), buf.end(), 'x');
2070 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002071 kBufSize));
2072 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002073 }
2074
Lei Zhang05ec64c2019-01-09 03:00:06 +00002075 {
2076 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2077 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002078 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002079 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002080 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2081 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2082 // Also do the same test for its appearance string.
2083 std::fill(buf.begin(), buf.end(), 'x');
2084 ASSERT_EQ(2u,
2085 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002086 buf.data(), kBufSize));
2087 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002088
Lei Zhangf0f67682019-04-08 17:03:21 +00002089 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002090 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2091 text.get()));
2092
2093 std::fill(buf.begin(), buf.end(), 'x');
2094 ASSERT_EQ(6u,
2095 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002096 buf.data(), kBufSize));
2097 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002098 }
2099
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002100 UnloadPage(page);
2101
2102 {
2103 // Save a copy, open the copy, and check the annotation again.
2104 // Note that it renders the rotation.
2105 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002106 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002107 FPDF_PAGE saved_page = LoadSavedPage(0);
2108 ASSERT_TRUE(saved_page);
2109
Lei Zhang05ec64c2019-01-09 03:00:06 +00002110 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002111 {
2112 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2113 ASSERT_TRUE(annot);
2114 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2115
2116 std::fill(buf.begin(), buf.end(), 'x');
2117 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002118 kBufSize));
2119 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002120 }
2121
Lei Zhang05ec64c2019-01-09 03:00:06 +00002122 {
2123 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2124 ASSERT_TRUE(annot);
2125 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2126 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2127
2128 std::fill(buf.begin(), buf.end(), 'x');
2129 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002130 kBufSize));
2131 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002132 }
2133
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002134 CloseSavedPage(saved_page);
2135 CloseSavedDocument();
2136 }
2137}
rycsmithcb752f32019-02-21 18:40:53 +00002138
2139TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2140 // Open a file with combobox widget annotations and load its first page.
2141 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2142 FPDF_PAGE page = LoadPage(0);
2143 ASSERT_TRUE(page);
2144
2145 {
2146 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2147 ASSERT_TRUE(annot);
2148
2149 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2150
2151 annot.reset(FPDFPage_GetAnnot(page, 1));
2152 ASSERT_TRUE(annot);
2153
2154 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002155
2156 // Check bad form handle / annot.
2157 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2158 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2159 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002160 }
2161
2162 UnloadPage(page);
2163}
2164
2165TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2166 // Open a file with listbox widget annotations and load its first page.
2167 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2168 FPDF_PAGE page = LoadPage(0);
2169 ASSERT_TRUE(page);
2170
2171 {
2172 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2173 ASSERT_TRUE(annot);
2174
2175 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2176
2177 annot.reset(FPDFPage_GetAnnot(page, 1));
2178 ASSERT_TRUE(annot);
2179
2180 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2181 }
2182
2183 UnloadPage(page);
2184}
2185
2186TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2187 // Open a file with ink annotations and load its first page.
2188 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2189 FPDF_PAGE page = LoadPage(0);
2190 ASSERT_TRUE(page);
2191
2192 {
2193 // annotations do not have "Opt" array and will return -1
2194 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2195 ASSERT_TRUE(annot);
2196
2197 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2198
2199 annot.reset(FPDFPage_GetAnnot(page, 1));
2200 ASSERT_TRUE(annot);
2201
2202 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2203 }
2204
2205 UnloadPage(page);
2206}
2207
2208TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2209 // Open a file with combobox widget annotations and load its first page.
2210 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2211 FPDF_PAGE page = LoadPage(0);
2212 ASSERT_TRUE(page);
2213
2214 {
2215 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2216 ASSERT_TRUE(annot);
2217
2218 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002219 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002220 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002221 ASSERT_EQ(8u, length_bytes);
2222 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002223 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002224 buf.data(), length_bytes));
2225 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002226
2227 annot.reset(FPDFPage_GetAnnot(page, 1));
2228 ASSERT_TRUE(annot);
2229
2230 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002231 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002232 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002233 ASSERT_EQ(12u, length_bytes);
2234 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002235 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002236 buf.data(), length_bytes));
2237 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002238
2239 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002240 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002241 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002242 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002243 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002244 buf.data(), length_bytes));
2245 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002246
Lei Zhange7033c82019-02-26 19:30:49 +00002247 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002248 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2249 nullptr, 0));
2250 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2251 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002252
2253 // Check bad form handle / annot.
2254 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2255 EXPECT_EQ(0u,
2256 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2257 EXPECT_EQ(0u,
2258 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002259 }
2260
2261 UnloadPage(page);
2262}
2263
2264TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2265 // Open a file with listbox widget annotations and load its first page.
2266 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2267 FPDF_PAGE page = LoadPage(0);
2268 ASSERT_TRUE(page);
2269
2270 {
2271 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2272 ASSERT_TRUE(annot);
2273
2274 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002275 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002276 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002277 ASSERT_EQ(8u, length_bytes);
2278 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002279 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002280 buf.data(), length_bytes));
2281 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002282
2283 annot.reset(FPDFPage_GetAnnot(page, 1));
2284 ASSERT_TRUE(annot);
2285
2286 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002287 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002288 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002289 ASSERT_EQ(12u, length_bytes);
2290 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002291 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002292 buf.data(), length_bytes));
2293 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002294
2295 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002296 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002297 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002298 ASSERT_EQ(18u, length_bytes);
2299 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002300 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002301 buf.data(), length_bytes));
2302 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002303
2304 // indices out of range
2305 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2306 nullptr, 0));
2307 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2308 nullptr, 0));
2309 }
2310
2311 UnloadPage(page);
2312}
2313
2314TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2315 // Open a file with ink annotations and load its first page.
2316 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2317 FPDF_PAGE page = LoadPage(0);
2318 ASSERT_TRUE(page);
2319
2320 {
2321 // annotations do not have "Opt" array and will return 0
2322 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2323 ASSERT_TRUE(annot);
2324
2325 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2326 nullptr, 0));
2327
2328 annot.reset(FPDFPage_GetAnnot(page, 1));
2329 ASSERT_TRUE(annot);
2330
2331 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2332 nullptr, 0));
2333 }
2334
2335 UnloadPage(page);
2336}
Ryan Smith09c23b12019-04-25 18:09:06 +00002337
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002338TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2339 // Open a file with combobox widget annotations and load its first page.
2340 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2341 FPDF_PAGE page = LoadPage(0);
2342 ASSERT_TRUE(page);
2343
2344 {
2345 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2346 ASSERT_TRUE(annot);
2347
2348 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2349 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2350 ASSERT_EQ(3, count);
2351 for (int i = 0; i < count; i++) {
2352 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2353 }
2354
2355 annot.reset(FPDFPage_GetAnnot(page, 1));
2356 ASSERT_TRUE(annot);
2357
2358 // Checks for Combobox with Values (/V) object which is just a string.
2359 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2360 ASSERT_EQ(26, count);
2361 for (int i = 0; i < count; i++) {
2362 EXPECT_EQ(i == 1,
2363 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2364 }
2365
2366 // Checks for index outside bound i.e. (index >= CountOption()).
2367 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2368 /*index=*/26));
2369 // Checks for negetive index.
2370 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2371 /*index=*/-1));
2372
2373 // Checks for bad form handle/annot.
2374 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2375 EXPECT_FALSE(
2376 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2377 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2378 }
2379
2380 UnloadPage(page);
2381}
2382
2383TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2384 // Open a file with listbox widget annotations and load its first page.
2385 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2386 FPDF_PAGE page = LoadPage(0);
2387 ASSERT_TRUE(page);
2388
2389 {
2390 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2391 ASSERT_TRUE(annot);
2392
2393 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2394 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2395 ASSERT_EQ(3, count);
2396 for (int i = 0; i < count; i++) {
2397 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2398 }
2399
2400 annot.reset(FPDFPage_GetAnnot(page, 1));
2401 ASSERT_TRUE(annot);
2402
2403 // Checks for Listbox with Values (/V) object which is just a string.
2404 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2405 ASSERT_EQ(26, count);
2406 for (int i = 0; i < count; i++) {
2407 EXPECT_EQ(i == 1,
2408 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2409 }
2410
2411 annot.reset(FPDFPage_GetAnnot(page, 3));
2412 ASSERT_TRUE(annot);
2413
2414 // Checks for Listbox with only Selected indices (/I) object which is an
2415 // array with multiple objects.
2416 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2417 ASSERT_EQ(5, count);
2418 for (int i = 0; i < count; i++) {
2419 bool expected = (i == 1 || i == 3);
2420 EXPECT_EQ(expected,
2421 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2422 }
2423
2424 annot.reset(FPDFPage_GetAnnot(page, 4));
2425 ASSERT_TRUE(annot);
2426
2427 // Checks for Listbox with Values (/V) object which is an array with
2428 // multiple objects.
2429 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2430 ASSERT_EQ(5, count);
2431 for (int i = 0; i < count; i++) {
2432 bool expected = (i == 2 || i == 4);
2433 EXPECT_EQ(expected,
2434 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2435 }
2436
2437 annot.reset(FPDFPage_GetAnnot(page, 5));
2438 ASSERT_TRUE(annot);
2439
2440 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2441 // objects conflict with different lengths.
2442 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2443 ASSERT_EQ(5, count);
2444 for (int i = 0; i < count; i++) {
2445 bool expected = (i == 0 || i == 2);
2446 EXPECT_EQ(expected,
2447 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2448 }
2449 }
2450
2451 UnloadPage(page);
2452}
2453
2454TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2455 // Open a file with multiple form field annotations and load its first page.
2456 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2457 FPDF_PAGE page = LoadPage(0);
2458 ASSERT_TRUE(page);
2459
2460 {
2461 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2462 ASSERT_TRUE(annot);
2463
2464 // Checks for link annotation.
2465 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2466 /*index=*/0));
2467
2468 annot.reset(FPDFPage_GetAnnot(page, 3));
2469 ASSERT_TRUE(annot);
2470
2471 // Checks for text field annotation.
2472 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2473 /*index=*/0));
2474 }
2475
2476 UnloadPage(page);
2477}
2478
Ryan Smith09c23b12019-04-25 18:09:06 +00002479TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2480 // Open a file with combobox annotations and load its first page.
2481 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2482 FPDF_PAGE page = LoadPage(0);
2483 ASSERT_TRUE(page);
2484
2485 {
2486 // All 3 widgets have Tf font size 12.
2487 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2488 ASSERT_TRUE(annot);
2489
2490 float value;
2491 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2492 EXPECT_EQ(12.0, value);
2493
2494 annot.reset(FPDFPage_GetAnnot(page, 1));
2495 ASSERT_TRUE(annot);
2496
2497 float value_two;
2498 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2499 EXPECT_EQ(12.0, value_two);
2500
2501 annot.reset(FPDFPage_GetAnnot(page, 2));
2502 ASSERT_TRUE(annot);
2503
2504 float value_three;
2505 ASSERT_TRUE(
2506 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2507 EXPECT_EQ(12.0, value_three);
2508 }
2509
2510 UnloadPage(page);
2511}
2512
2513TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2514 // Open a file with textfield annotations and load its first page.
2515 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2516 FPDF_PAGE page = LoadPage(0);
2517 ASSERT_TRUE(page);
2518
2519 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002520 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002521 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2522 ASSERT_TRUE(annot);
2523
2524 float value;
2525 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2526 EXPECT_EQ(12.0, value);
2527
2528 annot.reset(FPDFPage_GetAnnot(page, 1));
2529 ASSERT_TRUE(annot);
2530
2531 float value_two;
2532 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2533 EXPECT_EQ(12.0, value_two);
2534
2535 annot.reset(FPDFPage_GetAnnot(page, 2));
2536 ASSERT_TRUE(annot);
2537
2538 float value_three;
2539 ASSERT_TRUE(
2540 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2541 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002542
2543 float value_four;
2544 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2545 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002546 }
2547
2548 UnloadPage(page);
2549}
2550
2551TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2552 // Open a file with ink annotations and load its first page.
2553 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2554 FPDF_PAGE page = LoadPage(0);
2555 ASSERT_TRUE(page);
2556
2557 {
2558 // Annotations that do not have variable text and will return -1.
2559 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2560 ASSERT_TRUE(annot);
2561
2562 float value;
2563 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2564
2565 annot.reset(FPDFPage_GetAnnot(page, 1));
2566 ASSERT_TRUE(annot);
2567
2568 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2569 }
2570
2571 UnloadPage(page);
2572}
2573
2574TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2575 // Open a file with combobox annotations and load its first page.
2576 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2577 FPDF_PAGE page = LoadPage(0);
2578 ASSERT_TRUE(page);
2579
2580 {
2581 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2582 ASSERT_TRUE(annot);
2583
2584 // Check bad form handle / annot.
2585 float value;
2586 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2587 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2588 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2589 }
2590
2591 UnloadPage(page);
2592}
2593
2594TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2595 // Open a file with textfield annotations and load its first page.
2596 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2597 FPDF_PAGE page = LoadPage(0);
2598 ASSERT_TRUE(page);
2599
2600 {
2601 // Obtain the first annotation, a text field with negative font size, -12.
2602 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2603 ASSERT_TRUE(annot);
2604
2605 float value;
2606 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2607 EXPECT_EQ(-12.0, value);
2608 }
2609
2610 UnloadPage(page);
2611}
Ryan Smith23fdf892019-07-17 21:51:26 +00002612
2613TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2614 // Open a file with checkbox and radiobuttons widget annotations and load its
2615 // first page.
2616 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2617 FPDF_PAGE page = LoadPage(0);
2618 ASSERT_TRUE(page);
2619
2620 {
2621 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2622 ASSERT_TRUE(annot);
2623 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2624 }
2625
2626 UnloadPage(page);
2627}
2628
2629TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2630 // Open a file with checkbox and radiobutton widget annotations and load its
2631 // first page.
2632 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2633 FPDF_PAGE page = LoadPage(0);
2634 ASSERT_TRUE(page);
2635
2636 {
2637 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2638 ASSERT_TRUE(annot);
2639 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2640 }
2641
2642 UnloadPage(page);
2643}
2644
2645TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2646 // Open a file with checkbox and radiobutton widget annotations and load its
2647 // first page.
2648 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2649 FPDF_PAGE page = LoadPage(0);
2650 ASSERT_TRUE(page);
2651
2652 {
2653 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2654 ASSERT_TRUE(annot);
2655 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2656
2657 annot.reset(FPDFPage_GetAnnot(page, 6));
2658 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2659
2660 annot.reset(FPDFPage_GetAnnot(page, 7));
2661 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2662 }
2663
2664 UnloadPage(page);
2665}
2666
2667TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2668 // Open a file with checkbox and radiobutton widget annotations and load its
2669 // first page.
2670 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2671 FPDF_PAGE page = LoadPage(0);
2672 ASSERT_TRUE(page);
2673
2674 {
2675 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2676 ASSERT_TRUE(annot);
2677 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2678
2679 annot.reset(FPDFPage_GetAnnot(page, 3));
2680 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2681
2682 annot.reset(FPDFPage_GetAnnot(page, 4));
2683 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2684 }
2685
2686 UnloadPage(page);
2687}
2688
2689TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2690 // Open a file with checkbox and radiobuttons widget annotations and load its
2691 // first page.
2692 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2693 FPDF_PAGE page = LoadPage(0);
2694 ASSERT_TRUE(page);
2695
2696 {
2697 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2698 ASSERT_TRUE(annot);
2699 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2700 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2701 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2702 }
2703
2704 UnloadPage(page);
2705}
2706
2707TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2708 // Open a file with text widget annotations and load its first page.
2709 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2710 FPDF_PAGE page = LoadPage(0);
2711 ASSERT_TRUE(page);
2712
2713 {
2714 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2715 ASSERT_TRUE(annot);
2716 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2717 }
2718
2719 UnloadPage(page);
2720}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002721
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002722TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2723 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002724 FPDF_PAGE page = LoadPage(0);
2725 ASSERT_TRUE(page);
2726
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002727 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2728
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002729 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002730 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002731 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002732 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002733 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002734
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002735 constexpr int kExpectedAnnotTypes[] = {-1,
2736 FPDF_FORMFIELD_COMBOBOX,
2737 FPDF_FORMFIELD_LISTBOX,
2738 FPDF_FORMFIELD_TEXTFIELD,
2739 FPDF_FORMFIELD_CHECKBOX,
2740 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002741
Lei Zhang4501a502020-05-18 16:52:59 +00002742 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002743 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002744 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002745 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002746 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2747 }
2748 UnloadPage(page);
2749}
2750
2751TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2752 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2753 FPDF_PAGE page = LoadPage(0);
2754 ASSERT_TRUE(page);
2755
2756 {
2757 EXPECT_EQ(0u,
2758 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2759
2760 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2761 ASSERT_TRUE(annot);
2762
2763 EXPECT_EQ(0u,
2764 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2765
2766 unsigned long length_bytes =
2767 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2768 ASSERT_EQ(2u, length_bytes);
2769 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2770 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2771 buf.data(), length_bytes));
2772 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2773 }
2774 {
2775 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2776 ASSERT_TRUE(annot);
2777
2778 unsigned long length_bytes =
2779 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2780 ASSERT_EQ(18u, length_bytes);
2781 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2782 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2783 buf.data(), length_bytes));
2784 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2785 }
2786 UnloadPage(page);
2787}
2788
2789TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2790 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2791 FPDF_PAGE page = LoadPage(0);
2792 ASSERT_TRUE(page);
2793
2794 {
2795 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2796 ASSERT_TRUE(annot);
2797
2798 unsigned long length_bytes =
2799 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2800 ASSERT_EQ(2u, length_bytes);
2801 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2802 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2803 buf.data(), length_bytes));
2804 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2805 }
2806 {
2807 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2808 ASSERT_TRUE(annot);
2809
2810 unsigned long length_bytes =
2811 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2812 ASSERT_EQ(14u, length_bytes);
2813 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2814 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2815 buf.data(), length_bytes));
2816 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2817 }
2818 UnloadPage(page);
2819}
2820
2821TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2822 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2823 FPDF_PAGE page = LoadPage(0);
2824 ASSERT_TRUE(page);
2825
2826 {
2827 EXPECT_EQ(0u,
2828 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2829
2830 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2831 ASSERT_TRUE(annot);
2832
2833 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2834
2835 unsigned long length_bytes =
2836 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2837 ASSERT_EQ(18u, length_bytes);
2838 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2839 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2840 buf.data(), length_bytes));
2841 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2842 }
2843 UnloadPage(page);
2844}
2845
2846TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2847 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2848 FPDF_PAGE page = LoadPage(0);
2849 ASSERT_TRUE(page);
2850
2851 {
2852 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2853 ASSERT_TRUE(annot);
2854
2855 unsigned long length_bytes =
2856 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2857 ASSERT_EQ(30u, length_bytes);
2858 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2859 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2860 buf.data(), length_bytes));
2861 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2862 }
2863 UnloadPage(page);
2864}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002865
Lei Zhang9b444002020-04-17 17:35:23 +00002866TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2867 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002868 FPDF_PAGE page = LoadPage(0);
2869 ASSERT_TRUE(page);
2870
Lei Zhang9b444002020-04-17 17:35:23 +00002871 // Verify widgets are by default focusable.
2872 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2873 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002874
Lei Zhang9b444002020-04-17 17:35:23 +00002875 // Expected annot subtypes for page 0 of annots.pdf.
2876 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2877 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2878 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2879 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2880 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002881
Lei Zhang9b444002020-04-17 17:35:23 +00002882 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002883 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002884 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2885 kExpectedAnnotSubtypes,
2886 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002887
Lei Zhang9b444002020-04-17 17:35:23 +00002888 // Make no annotation type focusable using the preferred method.
2889 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2890 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002891
Lei Zhang9b444002020-04-17 17:35:23 +00002892 // Restore the focusable type count back to 1, then set it back to 0 using a
2893 // different method.
2894 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2895 ASSERT_TRUE(
2896 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2897 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002898
Lei Zhang9b444002020-04-17 17:35:23 +00002899 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002900 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002901
Lei Zhang9b444002020-04-17 17:35:23 +00002902 // Now make links focusable.
2903 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2904 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002905
Lei Zhang9b444002020-04-17 17:35:23 +00002906 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002907 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002908 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2909 kExpectedAnnotSubtypes,
2910 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002911
Lei Zhang9b444002020-04-17 17:35:23 +00002912 // Test invalid parameters.
2913 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00002914 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002915 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00002916 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002917 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2918
Lei Zhang9b444002020-04-17 17:35:23 +00002919 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2920 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2921 subtypes.size()));
2922 EXPECT_FALSE(
2923 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2924 EXPECT_FALSE(
2925 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002926
2927 UnloadPage(page);
2928}
Lei Zhang671aece2020-04-14 19:02:26 +00002929
Hui Yingstea3816d2020-07-28 22:35:11 +00002930TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00002931 ASSERT_TRUE(OpenDocument("annots.pdf"));
2932 FPDF_PAGE page = LoadPage(0);
2933 ASSERT_TRUE(page);
2934
2935 {
Hui Yingstea3816d2020-07-28 22:35:11 +00002936#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2937 static const char kMd5sum[] = "4ca14c670396711194b40ecc2514969b";
2938#else
Lei Zhang671aece2020-04-14 19:02:26 +00002939#if defined(OS_WIN)
2940 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
Lei Zhang0c03d632020-07-30 17:05:36 +00002941#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00002942 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
2943#else
2944 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
2945#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00002946#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00002947 // Check the initial rendering.
2948 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2949 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2950 }
2951
2952 // Make links and highlights focusable.
2953 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
2954 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00002955 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00002956 ASSERT_TRUE(
2957 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
2958 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
2959 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
2960 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
2961 subtypes.size()));
2962 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
2963 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
2964
2965 {
Hui Yingstea3816d2020-07-28 22:35:11 +00002966#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2967 static const char kMd5sum[] = "48cb60b3f9bc364c73582aff3418451e";
2968#else
Lei Zhang671aece2020-04-14 19:02:26 +00002969#if defined(OS_WIN)
2970 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
Lei Zhang0c03d632020-07-30 17:05:36 +00002971#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00002972 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
2973#else
2974 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
2975#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00002976#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00002977 // Focus the first link and check the rendering.
2978 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2979 ASSERT_TRUE(annot);
2980 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2981 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2982 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2983 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2984 }
2985
2986 {
Hui Yingstea3816d2020-07-28 22:35:11 +00002987#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2988 static const char kMd5sum[] = "d09869ff0a209daf179da2d7a58142b1";
2989#else
Lei Zhang671aece2020-04-14 19:02:26 +00002990#if defined(OS_WIN)
2991 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
Lei Zhang0c03d632020-07-30 17:05:36 +00002992#elif defined(OS_APPLE)
Lei Zhang671aece2020-04-14 19:02:26 +00002993 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
2994#else
2995 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
2996#endif
Hui Yingstea3816d2020-07-28 22:35:11 +00002997#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang671aece2020-04-14 19:02:26 +00002998 // Focus the first highlight and check the rendering.
2999 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3000 ASSERT_TRUE(annot);
3001 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3002 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3003 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3004 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
3005 }
3006
3007 UnloadPage(page);
3008}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003009
3010TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
3011 ASSERT_TRUE(OpenDocument("annots.pdf"));
3012 FPDF_PAGE page = LoadPage(0);
3013 ASSERT_TRUE(page);
3014 {
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003015 constexpr char kExpectedResult[] =
3016 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3017 "fpdf_text.h";
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003018
Lei Zhang306874b2021-04-16 00:33:36 +00003019 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3020 ASSERT_TRUE(annot);
3021 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3022 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()),
3023 kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003024 }
3025
3026 {
3027 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3028 ASSERT_TRUE(annot);
3029 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3030 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3031 }
3032
3033 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3034
3035 UnloadPage(page);
3036}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003037
3038TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3039 // Open a file with radio button widget annotations and load its first page.
3040 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3041 FPDF_PAGE page = LoadPage(0);
3042 ASSERT_TRUE(page);
3043
3044 {
3045 // Checks for bad annot.
3046 EXPECT_EQ(-1,
3047 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3048
3049 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3050 ASSERT_TRUE(annot);
3051
3052 // Checks for bad form handle.
3053 EXPECT_EQ(-1,
3054 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3055
3056 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3057 }
3058
3059 UnloadPage(page);
3060}
3061
3062TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3063 // Open a file with checkbox widget annotations and load its first page.
3064 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3065 FPDF_PAGE page = LoadPage(0);
3066 ASSERT_TRUE(page);
3067
3068 {
3069 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3070 ASSERT_TRUE(annot);
3071 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3072 }
3073
3074 UnloadPage(page);
3075}
3076
3077TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3078 // Open a file with ink annotations and load its first page.
3079 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3080 FPDF_PAGE page = LoadPage(0);
3081 ASSERT_TRUE(page);
3082
3083 {
3084 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3085 ASSERT_TRUE(annot);
3086 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3087 }
3088
3089 UnloadPage(page);
3090}
3091
3092TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3093 // Open a file with radio button widget annotations and load its first page.
3094 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3095 FPDF_PAGE page = LoadPage(0);
3096 ASSERT_TRUE(page);
3097
3098 {
3099 // Checks for bad annot.
3100 EXPECT_EQ(-1,
3101 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3102
3103 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3104 ASSERT_TRUE(annot);
3105
3106 // Checks for bad form handle.
3107 EXPECT_EQ(-1,
3108 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3109
3110 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3111 }
3112
3113 UnloadPage(page);
3114}
3115
3116TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3117 // Open a file with checkbox widget annotations and load its first page.
3118 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3119 FPDF_PAGE page = LoadPage(0);
3120 ASSERT_TRUE(page);
3121
3122 {
3123 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3124 ASSERT_TRUE(annot);
3125 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3126 }
3127
3128 UnloadPage(page);
3129}
3130
3131TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3132 // Open a file with ink annotations and load its first page.
3133 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3134 FPDF_PAGE page = LoadPage(0);
3135 ASSERT_TRUE(page);
3136
3137 {
3138 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3139 ASSERT_TRUE(annot);
3140 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3141 }
3142
3143 UnloadPage(page);
3144}
3145
3146TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3147 // Open a file with radio button widget annotations and load its first page.
3148 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3149 FPDF_PAGE page = LoadPage(0);
3150 ASSERT_TRUE(page);
3151
3152 {
3153 // Checks for bad annot.
3154 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3155 form_handle(), /*annot=*/nullptr,
3156 /*buffer=*/nullptr, /*buflen=*/0));
3157
3158 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3159 ASSERT_TRUE(annot);
3160
3161 // Checks for bad form handle.
3162 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3163 /*hHandle=*/nullptr, annot.get(),
3164 /*buffer=*/nullptr, /*buflen=*/0));
3165
3166 unsigned long length_bytes =
3167 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3168 /*buffer=*/nullptr, /*buflen=*/0);
3169 ASSERT_EQ(14u, length_bytes);
3170 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3171 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3172 buf.data(), length_bytes));
3173 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3174 }
3175
3176 UnloadPage(page);
3177}
3178
3179TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3180 // Open a file with checkbox widget annotations and load its first page.
3181 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3182 FPDF_PAGE page = LoadPage(0);
3183 ASSERT_TRUE(page);
3184
3185 {
3186 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3187 ASSERT_TRUE(annot);
3188
3189 unsigned long length_bytes =
3190 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3191 /*buffer=*/nullptr, /*buflen=*/0);
3192 ASSERT_EQ(8u, length_bytes);
3193 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3194 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3195 buf.data(), length_bytes));
3196 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3197 }
3198
3199 UnloadPage(page);
3200}
3201
3202TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3203 // Open a file with ink annotations and load its first page.
3204 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3205 FPDF_PAGE page = LoadPage(0);
3206 ASSERT_TRUE(page);
3207
3208 {
3209 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3210 ASSERT_TRUE(annot);
3211 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3212 /*buffer=*/nullptr,
3213 /*buflen=*/0));
3214 }
3215
3216 UnloadPage(page);
3217}
Miklos Vajnad1a24fb2020-10-26 18:07:13 +00003218
3219TEST_F(FPDFAnnotEmbedderTest, Redactannotation) {
3220 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3221 FPDF_PAGE page = LoadPage(0);
3222 ASSERT_TRUE(page);
3223 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3224
3225 {
3226 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3227 ASSERT_TRUE(annot);
3228 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3229 }
3230
3231 UnloadPage(page);
3232}
Miklos Vajna09ecef62020-11-10 21:50:38 +00003233
3234TEST_F(FPDFAnnotEmbedderTest, PolygonAnnotation) {
3235 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3236 FPDF_PAGE page = LoadPage(0);
3237 ASSERT_TRUE(page);
3238 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3239
3240 {
3241 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3242 ASSERT_TRUE(annot);
3243
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003244 // FPDFAnnot_GetVertices() positive testing.
Miklos Vajna09ecef62020-11-10 21:50:38 +00003245 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3246 const size_t kExpectedSize = 3;
3247 ASSERT_EQ(kExpectedSize, size);
3248 std::vector<FS_POINTF> vertices_buffer(size);
3249 EXPECT_EQ(size,
3250 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3251 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3252 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3253 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3254 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3255 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3256 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3257
3258 // FPDFAnnot_GetVertices() negative testing.
3259 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3260
3261 // vertices_buffer is not overwritten if it is too small.
3262 vertices_buffer.resize(1);
3263 vertices_buffer[0].x = 42;
3264 vertices_buffer[0].y = 43;
3265 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3266 vertices_buffer.size());
3267 EXPECT_EQ(kExpectedSize, size);
3268 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3269 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3270 }
3271
3272 {
3273 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3274 ASSERT_TRUE(annot);
3275
3276 // This has an odd number of elements in the vertices array, ignore the last
3277 // element.
3278 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3279 const size_t kExpectedSize = 3;
3280 ASSERT_EQ(kExpectedSize, size);
3281 std::vector<FS_POINTF> vertices_buffer(size);
3282 EXPECT_EQ(size,
3283 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3284 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3285 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3286 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3287 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3288 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3289 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3290 }
3291
3292 {
3293 // Wrong annotation type.
3294 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3295 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3296 }
3297
3298 UnloadPage(page);
3299}
Miklos Vajna8f7b1ae2020-11-17 16:53:14 +00003300
3301TEST_F(FPDFAnnotEmbedderTest, InkAnnotation) {
3302 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3303 FPDF_PAGE page = LoadPage(0);
3304 ASSERT_TRUE(page);
3305 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3306
3307 {
3308 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3309 ASSERT_TRUE(annot);
3310
3311 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3312 // testing.
3313 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3314 const size_t kExpectedSize = 1;
3315 ASSERT_EQ(kExpectedSize, size);
3316 const unsigned long kPathIndex = 0;
3317 unsigned long path_size =
3318 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3319 const size_t kExpectedPathSize = 3;
3320 ASSERT_EQ(kExpectedPathSize, path_size);
3321 std::vector<FS_POINTF> path_buffer(path_size);
3322 EXPECT_EQ(path_size,
3323 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3324 path_buffer.data(), path_size));
3325 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3326 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3327 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3328 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3329 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3330 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3331
3332 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3333 // testing.
3334 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3335 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3336
3337 // out of bounds path_index.
3338 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3339
3340 // path_buffer is not overwritten if it is too small.
3341 path_buffer.resize(1);
3342 path_buffer[0].x = 42;
3343 path_buffer[0].y = 43;
3344 path_size = FPDFAnnot_GetInkListPath(
3345 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3346 EXPECT_EQ(kExpectedSize, size);
3347 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3348 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3349 }
3350
3351 {
3352 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3353 ASSERT_TRUE(annot);
3354
3355 // This has an odd number of elements in the path array, ignore the last
3356 // element.
3357 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3358 const size_t kExpectedSize = 1;
3359 ASSERT_EQ(kExpectedSize, size);
3360 const unsigned long kPathIndex = 0;
3361 unsigned long path_size =
3362 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3363 const size_t kExpectedPathSize = 3;
3364 ASSERT_EQ(kExpectedPathSize, path_size);
3365 std::vector<FS_POINTF> path_buffer(path_size);
3366 EXPECT_EQ(path_size,
3367 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3368 path_buffer.data(), path_size));
3369 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3370 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3371 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3372 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3373 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3374 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3375 }
3376
3377 {
3378 // Wrong annotation type.
3379 ScopedFPDFAnnotation polygon_annot(
3380 FPDFPage_CreateAnnot(page, FPDF_ANNOT_POLYGON));
3381 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3382 const unsigned long kPathIndex = 0;
3383 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3384 nullptr, 0));
3385 }
3386
3387 UnloadPage(page);
3388}
Miklos Vajna30f45a62020-12-04 19:12:31 +00003389
3390TEST_F(FPDFAnnotEmbedderTest, LineAnnotation) {
3391 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3392 FPDF_PAGE page = LoadPage(0);
3393 ASSERT_TRUE(page);
3394 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3395
3396 {
3397 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3398 ASSERT_TRUE(annot);
3399
3400 // FPDFAnnot_GetVertices() positive testing.
3401 FS_POINTF start;
3402 FS_POINTF end;
3403 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3404 EXPECT_FLOAT_EQ(159.0f, start.x);
3405 EXPECT_FLOAT_EQ(296.0f, start.y);
3406 EXPECT_FLOAT_EQ(472.0f, end.x);
3407 EXPECT_FLOAT_EQ(243.42f, end.y);
3408
3409 // FPDFAnnot_GetVertices() negative testing.
3410 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3411 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3412 }
3413
3414 {
3415 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3416 ASSERT_TRUE(annot);
3417
3418 // Too few elements in the line array.
3419 FS_POINTF start;
3420 FS_POINTF end;
3421 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3422 }
3423
3424 {
3425 // Wrong annotation type.
3426 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3427 FS_POINTF start;
3428 FS_POINTF end;
3429 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3430 }
3431
3432 UnloadPage(page);
3433}
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003434
3435TEST_F(FPDFAnnotEmbedderTest, AnnotationBorder) {
3436 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3437 FPDF_PAGE page = LoadPage(0);
3438 ASSERT_TRUE(page);
3439 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3440
3441 {
3442 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3443 ASSERT_TRUE(annot);
3444
3445 // FPDFAnnot_GetBorder() positive testing.
3446 float horizontal_radius;
3447 float vertical_radius;
3448 float border_width;
3449 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3450 &vertical_radius, &border_width));
3451 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3452 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3453 EXPECT_FLOAT_EQ(2.0f, border_width);
3454
3455 // FPDFAnnot_GetBorder() negative testing.
3456 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3457 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3458 }
3459
3460 {
3461 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3462 ASSERT_TRUE(annot);
3463
3464 // Too few elements in the border array.
3465 float horizontal_radius;
3466 float vertical_radius;
3467 float border_width;
3468 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3469 &vertical_radius, &border_width));
Lei Zhang21ea6652021-04-15 23:24:46 +00003470
3471 // FPDFAnnot_SetBorder() positive testing.
3472 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3473 /*vertical_radius=*/3.5f,
3474 /*border_width=*/4.0f));
3475
3476 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3477 &vertical_radius, &border_width));
3478 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3479 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3480 EXPECT_FLOAT_EQ(4.0f, border_width);
3481
3482 // FPDFAnnot_SetBorder() negative testing.
3483 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3484 /*vertical_radius=*/2.5f,
3485 /*border_width=*/3.0f));
Miklos Vajna305d2ed2020-12-15 17:28:49 +00003486 }
3487
3488 UnloadPage(page);
3489}