blob: 7d1301af8c968641a970ce18d575079df3679ab4 [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
36namespace {
37
Tom Sepez204ab052020-06-12 21:33:48 +000038const wchar_t kStreamData[] =
39 L"/GS gs 0.0 0.0 0.0 RG 4 w 211.8 747.6 m 211.8 744.8 "
40 L"212.6 743.0 214.2 740.8 "
41 L"c 215.4 739.0 216.8 737.1 218.9 736.1 c 220.8 735.1 221.4 733.0 "
42 L"223.7 732.4 c 232.6 729.9 242.0 730.8 251.2 730.8 c 257.5 730.8 "
43 L"263.0 732.9 269.0 734.4 c S";
44
Hui Yingst4a21df02020-05-13 03:15:44 +000045#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
46#if defined(OS_LINUX)
47const char kAnnotationStampWithApChecksum[] =
48 "db83eaadc92967e3ac9bebfc6178ca75";
49#else
50const char kAnnotationStampWithApChecksum[] =
51 "3c87b4a8e51245964357fb5f5fbc612b";
52#endif // defined(OS_LINUX)
53#else
Hui Yingstb4baceb2020-04-28 23:46:10 +000054#if defined(OS_WIN)
55const char kAnnotationStampWithApChecksum[] =
56 "6aa001a77ec05d0f1b0d1d22e28744d4";
57#elif defined(OS_MACOSX)
58const char kAnnotationStampWithApChecksum[] =
59 "80d7b6cc7b13a78d77a6151bc846e80b";
60#else
61const char kAnnotationStampWithApChecksum[] =
62 "b42cef463483e668eaf4055a65e4f1f5";
63#endif
Hui Yingst4a21df02020-05-13 03:15:44 +000064#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstb4baceb2020-04-28 23:46:10 +000065
Lei Zhang9b444002020-04-17 17:35:23 +000066void VerifyFocusableAnnotSubtypes(
67 FPDF_FORMHANDLE form_handle,
68 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) {
69 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
70 FPDFAnnot_GetFocusableSubtypesCount(form_handle));
71
72 std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes(
73 expected_subtypes.size());
74 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(
75 form_handle, actual_subtypes.data(), actual_subtypes.size()));
76 for (size_t i = 0; i < expected_subtypes.size(); ++i)
77 ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]);
78}
79
80void SetAndVerifyFocusableAnnotSubtypes(
81 FPDF_FORMHANDLE form_handle,
82 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) {
83 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(),
84 subtypes.size()));
85 VerifyFocusableAnnotSubtypes(form_handle, subtypes);
86}
87
88void VerifyAnnotationSubtypesAndFocusability(
89 FPDF_FORMHANDLE form_handle,
90 FPDF_PAGE page,
91 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes,
92 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) {
93 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
94 FPDFPage_GetAnnotCount(page));
95 for (size_t i = 0; i < expected_subtypes.size(); ++i) {
96 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
97 ASSERT_TRUE(annot);
98 EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get()));
99
Lei Zhangf245ae62020-05-15 21:49:46 +0000100 bool expected_focusable =
101 pdfium::Contains(expected_focusable_subtypes, expected_subtypes[i]);
Lei Zhang9b444002020-04-17 17:35:23 +0000102 EXPECT_EQ(expected_focusable,
103 FORM_SetFocusedAnnot(form_handle, annot.get()));
104
105 // Kill the focus so the next test starts in an unfocused state.
106 FORM_ForceToKillFocus(form_handle);
107 }
108}
109
110} // 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
Lei Zhang03e5e682019-09-16 19:45:55 +0000507// TODO(crbug.com/pdfium/11): Fix this test and enable.
508#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
509#define MAYBE_ExtractInkMultiple DISABLED_ExtractInkMultiple
510#else
511#define MAYBE_ExtractInkMultiple ExtractInkMultiple
512#endif
513TEST_F(FPDFAnnotEmbedderTest, MAYBE_ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400514 // Open a file with three annotations and load its first page.
515 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000516 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400517 ASSERT_TRUE(page);
518
519 // Check that there is a total of 3 annotation on its first page.
520 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
521
Lei Zhanga21d5932018-02-05 18:28:38 +0000522 {
523 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000524 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000525 ASSERT_TRUE(annot);
526 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400527
Lei Zhanga21d5932018-02-05 18:28:38 +0000528 // Check that the annotation color is blue with opacity.
529 unsigned int R;
530 unsigned int G;
531 unsigned int B;
532 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000533 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000534 &G, &B, &A));
535 EXPECT_EQ(0u, R);
536 EXPECT_EQ(0u, G);
537 EXPECT_EQ(255u, B);
538 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400539
Lei Zhanga21d5932018-02-05 18:28:38 +0000540 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000541 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
542 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400543
Lei Zhang4f556b82019-04-08 16:32:41 +0000544 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000545 // Note that upon rendering, the rectangle coordinates will be adjusted.
546 FS_RECTF rect;
547 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
548 EXPECT_EQ(351.820404f, rect.left);
549 EXPECT_EQ(583.830688f, rect.bottom);
550 EXPECT_EQ(475.336090f, rect.right);
551 EXPECT_EQ(681.535034f, rect.top);
552 }
Tom Sepezef43c262018-11-07 16:41:32 +0000553 {
Lei Zhang430b5322020-07-06 22:23:36 +0000554#if defined(OS_WIN)
555 static constexpr char kExpectedHash[] = "49d0a81c636531a337429325273d0508";
556#else
557 static constexpr char kExpectedHash[] = "354002e1c4386d38fdde29ef8d61074a";
558#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
692 // Save the document, closing the page and document.
693 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 Yingstb3490322020-07-22 00:53:29 +0000697#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
698 static const char kChecksum[] = "798fa41303381c9ba6d99092f5cd4d2b";
699#else
700 static const char kChecksum[] = "dba153419f67b7c0c0e3d22d3e8910d5";
701#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400702
Lei Zhang0b494052019-01-31 21:41:15 +0000703 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000704 page = LoadSavedPage(0);
Hui Yingstb3490322020-07-22 00:53:29 +0000705 VerifySavedRendering(page, 612, 792, kChecksum);
Jane Liu20eafda2017-06-07 10:33:24 -0400706
707 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000708 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400709
Lei Zhanga21d5932018-02-05 18:28:38 +0000710 {
711 // Check that the second annotation is an underline annotation and verify
712 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000713 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000714 ASSERT_TRUE(new_annot);
715 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
716 FS_QUADPOINTSF new_quadpoints;
717 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000718 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000719 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
720 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
721 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
722 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
723 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400724
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000725 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400726 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400727}
Jane Liu06462752017-06-27 16:41:14 -0400728
Lei Zhangab41f252018-12-23 03:10:50 +0000729TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000730 // Open a file with four annotations and load its first page.
731 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
732 FPDF_PAGE page = LoadPage(0);
733 ASSERT_TRUE(page);
734 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
735
736 // Retrieve the highlight annotation.
737 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
738 ASSERT_TRUE(annot);
739 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
740
741 FS_QUADPOINTSF quadpoints;
742 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
743
744 {
745 // Verify the current one set of quadpoints.
746 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
747
748 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
749 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
750 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
751 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
752 }
753
754 {
755 // Update the quadpoints.
756 FS_QUADPOINTSF new_quadpoints = quadpoints;
757 new_quadpoints.y1 -= 20.f;
758 new_quadpoints.y2 -= 20.f;
759 new_quadpoints.y3 -= 20.f;
760 new_quadpoints.y4 -= 20.f;
761 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
762
763 // Verify added quadpoint set
764 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
765 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
766 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
767 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
768 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
769 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
770 }
771
772 {
773 // Append a new set of quadpoints.
774 FS_QUADPOINTSF new_quadpoints = quadpoints;
775 new_quadpoints.y1 += 20.f;
776 new_quadpoints.y2 += 20.f;
777 new_quadpoints.y3 += 20.f;
778 new_quadpoints.y4 += 20.f;
779 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
780
781 // Verify added quadpoint set
782 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
783 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
784 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
785 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
786 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
787 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
788 }
789
790 {
791 // Setting and getting quadpoints at out-of-bound index should fail
792 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
793 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
794 }
795
796 FPDFPage_CloseAnnot(annot);
797
798 // Retrieve the square annotation
799 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
800
801 {
802 // Check that attempting to set its quadpoints would fail
803 ASSERT_TRUE(squareAnnot);
804 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
805 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
806 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
807 }
808
809 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000810 UnloadPage(page);
811}
812
Lei Zhang03e5e682019-09-16 19:45:55 +0000813// TODO(crbug.com/pdfium/11): Fix this test and enable.
814#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
815#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
816#else
817#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
818#endif
819TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000820#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +0000821 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
Lei Zhang4f556b82019-04-08 16:32:41 +0000822 static const char kMd5ModifiedHighlight[] =
Lei Zhang0f6b4342020-02-25 20:00:39 +0000823 "e64bf648f6e9354d1f3eedb47a2c9498";
824 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Lei Zhange67bcc72019-04-30 18:55:58 +0000825#elif defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000826 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
827 static const char kMd5ModifiedHighlight[] =
828 "66f3caef3a7d488a4fa1ad37fc06310e";
829 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400830#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000831 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
832 static const char kMd5ModifiedHighlight[] =
833 "66f3caef3a7d488a4fa1ad37fc06310e";
834 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400835#endif
836
Jane Liu06462752017-06-27 16:41:14 -0400837 // Open a file with four annotations and load its first page.
838 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000839 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400840 ASSERT_TRUE(page);
841 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
842
Jane Liub370e5a2017-08-16 13:24:58 -0400843 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000844 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000845 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000846 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000847 }
Jane Liub370e5a2017-08-16 13:24:58 -0400848
Jane Liu0c6b07d2017-08-15 10:50:22 -0400849 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400850 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000851
852 // Retrieve the highlight annotation which has its AP stream already defined.
853 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000854 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000855 ASSERT_TRUE(annot);
856 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
857
858 // Check that color cannot be set when an AP stream is defined already.
859 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
860 102, 153, 204));
861
862 // Verify its attachment points.
863 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000864 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000865 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
866 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
867 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
868 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
869
870 // Check that updating the attachment points would succeed.
871 quadpoints.x1 -= 50.f;
872 quadpoints.x2 -= 50.f;
873 quadpoints.x3 -= 50.f;
874 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000875 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000876 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000877 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000878 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
879 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
880 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
881 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
882
883 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000884 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000885 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000886 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000887 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000888
889 // Verify its annotation rectangle.
890 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
891 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
892 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
893 EXPECT_NEAR(136.325f, rect.right, 0.001f);
894 EXPECT_NEAR(721.292f, rect.top, 0.001f);
895
896 // Check that updating the rectangle would succeed.
897 rect.left -= 60.f;
898 rect.right -= 60.f;
899 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
900 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
901 EXPECT_EQ(rect.right, new_rect.right);
902 }
Jane Liu06462752017-06-27 16:41:14 -0400903
Jane Liub370e5a2017-08-16 13:24:58 -0400904 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000905 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000906 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000907 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000908 }
Jane Liub370e5a2017-08-16 13:24:58 -0400909
Lei Zhanga21d5932018-02-05 18:28:38 +0000910 {
911 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000912 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000913 ASSERT_TRUE(annot);
914 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400915
Lei Zhanga21d5932018-02-05 18:28:38 +0000916 // Check that updating the rectangle would succeed.
917 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
918 rect.left += 70.f;
919 rect.right += 70.f;
920 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
921 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
922 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400923
Lei Zhanga21d5932018-02-05 18:28:38 +0000924 // Check that updating the rectangle changes the square annotation's
925 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000926 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000927 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000928 }
Jane Liub370e5a2017-08-16 13:24:58 -0400929
Jane Liu06462752017-06-27 16:41:14 -0400930 UnloadPage(page);
931}
Jane Liu8ce58f52017-06-29 13:40:22 -0400932
Lei Zhangab41f252018-12-23 03:10:50 +0000933TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000934 // Open a file with multiline markup annotations.
935 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
936 FPDF_PAGE page = LoadPage(0);
937 ASSERT_TRUE(page);
938 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000939 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000940 ASSERT_TRUE(annot);
941
942 // This is a three line annotation.
943 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
944 }
945 UnloadPage(page);
946
947 // null annotation should return 0
948 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
949}
950
Lei Zhangab41f252018-12-23 03:10:50 +0000951TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400952 // Open a file with 3 annotations on its first page.
953 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000954 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400955 ASSERT_TRUE(page);
956 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
957
Jane Liu0c6b07d2017-08-15 10:50:22 -0400958 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400959
Lei Zhanga21d5932018-02-05 18:28:38 +0000960 // Check that the annotations have the expected rectangle coordinates.
961 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000962 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000963 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
964 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
965 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400966
Lei Zhanga21d5932018-02-05 18:28:38 +0000967 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000968 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000969 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
970 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
971 }
972
973 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000974 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000975 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
976 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
977 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400978
979 // Check that nothing happens when attempting to remove an annotation with an
980 // out-of-bound index.
981 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
982 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
983 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
984
985 // Remove the second annotation.
986 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
987 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
988 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
989
990 // Save the document, closing the page and document.
991 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000992 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400993
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400994 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400995 // Open the saved document.
996 std::string new_file = GetString();
997 FPDF_FILEACCESS file_access;
998 memset(&file_access, 0, sizeof(file_access));
999 file_access.m_FileLen = new_file.size();
1000 file_access.m_GetBlock = GetBlockFromString;
1001 file_access.m_Param = &new_file;
1002 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
1003 ASSERT_TRUE(new_doc);
1004 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
1005 ASSERT_TRUE(new_page);
1006
1007 // Check that the saved document has 2 annotations on the first page.
1008 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
1009
Lei Zhanga21d5932018-02-05 18:28:38 +00001010 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1011 // by verifying their rectangle coordinates.
1012 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001013 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001014 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1015 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1016 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001017
Lei Zhanga21d5932018-02-05 18:28:38 +00001018 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001019 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001020 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1021 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1022 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001023 FPDF_ClosePage(new_page);
1024 FPDF_CloseDocument(new_doc);
1025}
Jane Liu8ce58f52017-06-29 13:40:22 -04001026
Hui Yingst4a21df02020-05-13 03:15:44 +00001027TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001028#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst4a21df02020-05-13 03:15:44 +00001029#if defined(OS_LINUX)
1030 static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa";
1031 static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622";
1032 static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001033#else
Hui Yingst4a21df02020-05-13 03:15:44 +00001034 static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4";
1035 static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f";
1036 static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006";
1037#endif // defined(OS_LINUX)
1038#else
1039#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001040 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
1041 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
1042 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Hui Yingst4a21df02020-05-13 03:15:44 +00001043#elif defined(OS_MACOSX)
1044 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
1045 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
1046 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Jane Liubaa7ff42017-06-29 19:18:23 -04001047#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001048 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
1049 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
1050 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -04001051#endif
Hui Yingst4a21df02020-05-13 03:15:44 +00001052#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liubaa7ff42017-06-29 19:18:23 -04001053
1054 // Open a file with two annotations and load its first page.
1055 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001056 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001057 ASSERT_TRUE(page);
1058 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1059
1060 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001061 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001062 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001063 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001064 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001065
Lei Zhanga21d5932018-02-05 18:28:38 +00001066 {
1067 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001068 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001069 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001070
Lei Zhanga21d5932018-02-05 18:28:38 +00001071 // Check that this annotation has one path object and retrieve it.
1072 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001073 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001074 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1075 EXPECT_FALSE(path);
1076 path = FPDFAnnot_GetObject(annot.get(), 0);
1077 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1078 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001079
Lei Zhanga21d5932018-02-05 18:28:38 +00001080 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001081 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001082 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001083
Lei Zhanga21d5932018-02-05 18:28:38 +00001084 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001085 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001086 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001087 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001088 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001089
Lei Zhanga21d5932018-02-05 18:28:38 +00001090 // Add a second path object to the same annotation.
1091 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1092 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001093 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1094 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001095 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1096 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1097 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001098
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001099 // The object is in the annontation, not in the page, so the page object
1100 // array should not change.
1101 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1102
Lei Zhanga21d5932018-02-05 18:28:38 +00001103 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001104 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001105 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001106 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001107 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001108
Lei Zhanga21d5932018-02-05 18:28:38 +00001109 // Delete the newly added path object.
1110 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1111 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001112 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001113 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001114
1115 // Check that the page renders the same as before.
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, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001119 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001120
Jane Liubaa7ff42017-06-29 19:18:23 -04001121 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001122
Lei Zhanga21d5932018-02-05 18:28:38 +00001123 {
1124 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001125 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001126 ASSERT_TRUE(annot);
1127 rect.left = 200.f;
1128 rect.bottom = 400.f;
1129 rect.right = 500.f;
1130 rect.top = 600.f;
1131 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001132
Lei Zhanga21d5932018-02-05 18:28:38 +00001133 // Add a new path to the annotation.
1134 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1135 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1136 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1137 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1138 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001139 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1140 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001141 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1142 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1143 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1144
1145 // Check that the annotation's bounding box came from its rectangle.
1146 FS_RECTF new_rect;
1147 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1148 EXPECT_EQ(rect.left, new_rect.left);
1149 EXPECT_EQ(rect.bottom, new_rect.bottom);
1150 EXPECT_EQ(rect.right, new_rect.right);
1151 EXPECT_EQ(rect.top, new_rect.top);
1152 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001153
1154 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -04001155 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001156 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001157
1158 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001159 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001160 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001161 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001162
Jane Liu36567742017-07-06 11:13:35 -04001163 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001164 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001165
Lei Zhanga21d5932018-02-05 18:28:38 +00001166 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001167 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001168 ASSERT_TRUE(annot);
1169 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001170
Lei Zhanga21d5932018-02-05 18:28:38 +00001171 // Check that the new annotation's rectangle is as defined.
1172 FS_RECTF new_rect;
1173 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1174 EXPECT_EQ(rect.left, new_rect.left);
1175 EXPECT_EQ(rect.bottom, new_rect.bottom);
1176 EXPECT_EQ(rect.right, new_rect.right);
1177 EXPECT_EQ(rect.top, new_rect.top);
1178 }
1179
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001180 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001181 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001182}
Jane Liub137e752017-07-05 15:04:33 -04001183
Lei Zhangab41f252018-12-23 03:10:50 +00001184TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001185 // Open a file with an annotation and load its first page.
1186 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001187 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001188 ASSERT_TRUE(page);
1189
1190 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001191 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001192 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001193 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1194 }
Jane Liub137e752017-07-05 15:04:33 -04001195
Lei Zhanga21d5932018-02-05 18:28:38 +00001196 {
1197 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001198 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001199 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001200
Lei Zhanga21d5932018-02-05 18:28:38 +00001201 // Check that the original flag values are as expected.
1202 int flags = FPDFAnnot_GetFlags(annot.get());
1203 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1204 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001205
Lei Zhanga21d5932018-02-05 18:28:38 +00001206 // Set the HIDDEN flag.
1207 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1208 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1209 flags = FPDFAnnot_GetFlags(annot.get());
1210 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1211 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001212
Lei Zhanga21d5932018-02-05 18:28:38 +00001213 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001214 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001215 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001216 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001217 }
Jane Liub137e752017-07-05 15:04:33 -04001218
Lei Zhanga21d5932018-02-05 18:28:38 +00001219 // Unset the HIDDEN flag.
1220 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1221 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1222 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1223 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1224 flags = FPDFAnnot_GetFlags(annot.get());
1225 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1226 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001227
Lei Zhanga21d5932018-02-05 18:28:38 +00001228 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001229 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001230 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001231 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1232 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001233 }
Jane Liub137e752017-07-05 15:04:33 -04001234
Jane Liub137e752017-07-05 15:04:33 -04001235 UnloadPage(page);
1236}
Jane Liu36567742017-07-06 11:13:35 -04001237
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001238// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1239#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001240#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1241#else
1242#define MAYBE_AddAndModifyImage AddAndModifyImage
1243#endif
1244TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001245#if defined(_SKIA_SUPPORT_PATHS_)
1246#if defined(OS_LINUX)
1247 static const char kMd5NewImage[] = "26a8eb30937226a677839379e0d7ae1a";
1248 static const char kMd5ModifiedImage[] = "2985114b32ba1a96be78ee643fe31aa5";
1249#else
1250 static const char kMd5NewImage[] = "14012ab500b4671fa73dd760129a8a93";
1251 static const char kMd5ModifiedImage[] = "5f97f98f58ed04dc393f31460485f1a2";
1252#endif // defined(OS_LINUX)
1253#else
Lei Zhange4cdac52019-04-30 16:45:57 +00001254#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001255 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
1256 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +00001257#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001258 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
1259 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -04001260#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001261 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
1262 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -04001263#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001264#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001265
1266 // Open a file with two annotations and load its first page.
1267 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001268 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001269 ASSERT_TRUE(page);
1270 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1271
1272 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001273 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001274 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001275 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001276 }
Jane Liu36567742017-07-06 11:13:35 -04001277
Jane Liu36567742017-07-06 11:13:35 -04001278 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001279 FPDF_BITMAP image_bitmap;
1280
1281 {
1282 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001283 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001284 ASSERT_TRUE(annot);
1285 FS_RECTF rect;
1286 rect.left = 200.f;
1287 rect.bottom = 600.f;
1288 rect.right = 400.f;
1289 rect.top = 800.f;
1290 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1291
1292 // Add a solid-color translucent image object to the new annotation.
1293 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1294 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1295 0xeeeecccc);
1296 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1297 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1298 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1299 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1300 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1301 kBitmapSize, 0, 0));
1302 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1303 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1304 }
Jane Liu36567742017-07-06 11:13:35 -04001305
1306 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001307 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001308 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001309 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001310 }
Jane Liu36567742017-07-06 11:13:35 -04001311
Lei Zhanga21d5932018-02-05 18:28:38 +00001312 {
1313 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001314 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001315 ASSERT_TRUE(annot);
1316 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1317 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1318 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001319
Lei Zhanga21d5932018-02-05 18:28:38 +00001320 // Modify the image in the new annotation.
1321 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1322 0xff000000);
1323 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1324 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1325 }
Jane Liu36567742017-07-06 11:13:35 -04001326
1327 // Save the document, closing the page and document.
1328 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001329 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001330 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001331
1332 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001333 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001334}
1335
Hui Yingst6cd754f2020-05-14 04:05:25 +00001336TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001337#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001338#if defined(OS_LINUX)
1339 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1340 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001341#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001342 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1343 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
1344#endif // defined(OS_LINUX)
1345#else
1346#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001347 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1348 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Hui Yingst6cd754f2020-05-14 04:05:25 +00001349#elif defined(OS_MACOSX)
1350 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1351 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001352#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001353 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1354 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001355#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001356#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001357
1358 // Open a file with two annotations and load its first page.
1359 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001360 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001361 ASSERT_TRUE(page);
1362 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1363
1364 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001365 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001366 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001367 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001368 }
Jane Liu36567742017-07-06 11:13:35 -04001369
Lei Zhanga21d5932018-02-05 18:28:38 +00001370 {
1371 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001372 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001373 ASSERT_TRUE(annot);
1374 FS_RECTF rect;
1375 rect.left = 200.f;
1376 rect.bottom = 550.f;
1377 rect.right = 450.f;
1378 rect.top = 650.f;
1379 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001380
Lei Zhanga21d5932018-02-05 18:28:38 +00001381 // Add a translucent text object to the new annotation.
1382 FPDF_PAGEOBJECT text_object =
1383 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1384 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001385 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001386 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1387 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001388 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001389 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1390 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1391 }
Jane Liu36567742017-07-06 11:13:35 -04001392
1393 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001394 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001395 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001396 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001397 }
Jane Liu36567742017-07-06 11:13:35 -04001398
Lei Zhanga21d5932018-02-05 18:28:38 +00001399 {
1400 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001401 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001402 ASSERT_TRUE(annot);
1403 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1404 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1405 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001406
Lei Zhanga21d5932018-02-05 18:28:38 +00001407 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001408 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001409 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1410 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1411 }
Jane Liu36567742017-07-06 11:13:35 -04001412
1413 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001414 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001415 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001416 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001417 }
Jane Liu36567742017-07-06 11:13:35 -04001418
1419 // Remove the new annotation, and check that the page renders as before.
1420 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001421 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001422 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001423 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001424 }
Jane Liu36567742017-07-06 11:13:35 -04001425
1426 UnloadPage(page);
1427}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001428
Lei Zhang03e5e682019-09-16 19:45:55 +00001429// TODO(crbug.com/pdfium/11): Fix this test and enable.
1430#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1431#define MAYBE_GetSetStringValue DISABLED_GetSetStringValue
1432#else
1433#define MAYBE_GetSetStringValue GetSetStringValue
1434#endif
1435TEST_F(FPDFAnnotEmbedderTest, MAYBE_GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001436 // Open a file with four annotations and load its first page.
1437 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001438 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001439 ASSERT_TRUE(page);
1440
Lei Zhang4f556b82019-04-08 16:32:41 +00001441 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001442
Lei Zhanga21d5932018-02-05 18:28:38 +00001443 {
1444 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001445 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001446 ASSERT_TRUE(annot);
1447
1448 // Check that a non-existent key does not exist.
1449 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1450
1451 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001452 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001453 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001454 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1455 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1456 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001457
1458 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001459 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001460 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001461 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001462 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001463 ASSERT_EQ(66u, length_bytes);
1464 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1465 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1466 length_bytes));
1467 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1468 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001469
1470 // Check that the string value of the modified date is correct.
1471 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001472 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1473 nullptr, 0);
1474 ASSERT_EQ(44u, length_bytes);
1475 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001476 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001477 buf.data(), length_bytes));
1478 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001479
1480 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001481 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001482 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1483 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001484 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001485
1486 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001487 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001488 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001489
Lei Zhange4cdac52019-04-30 16:45:57 +00001490#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001491 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001492#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001493 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001494#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001495 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001496#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001497
1498 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001499 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001500 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001501 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001502 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001503 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001504
Lei Zhanga21d5932018-02-05 18:28:38 +00001505 // Check that the string value of the modified date is the newly-set value.
1506 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001507 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001508 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001509 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001510 ASSERT_EQ(44u, length_bytes);
1511 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001512 EXPECT_EQ(44u,
1513 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001514 buf.data(), length_bytes));
1515 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001516 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001517
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001518 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001519 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001520}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001521
rycsmith3e785602019-03-05 21:48:36 +00001522TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001523 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001524 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1525 FPDF_PAGE page = LoadPage(0);
1526 ASSERT_TRUE(page);
1527 {
1528 // First two annotations do not have "MaxLen" attribute.
1529 for (int i = 0; i < 2; i++) {
1530 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1531 ASSERT_TRUE(annot);
1532
1533 // Verify that no "MaxLen" key present.
1534 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1535
1536 float value;
1537 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1538 }
1539
1540 // Annotation in index 2 has "MaxLen" of 10.
1541 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1542 ASSERT_TRUE(annot);
1543
1544 // Verify that "MaxLen" key present.
1545 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1546
1547 float value;
1548 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1549 EXPECT_FLOAT_EQ(10.0f, value);
1550
1551 // Check bad inputs.
1552 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1553 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1554 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1555 // Ask for key that exists but is not a number.
1556 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1557 }
1558
1559 UnloadPage(page);
1560}
1561
Lei Zhangab41f252018-12-23 03:10:50 +00001562TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001563 // Open a file with four annotations and load its first page.
1564 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001565 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001566 ASSERT_TRUE(page);
1567
Lei Zhanga21d5932018-02-05 18:28:38 +00001568 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001569 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1570 static constexpr size_t kExpectNormalAPLength = 73970;
1571
Lei Zhanga21d5932018-02-05 18:28:38 +00001572 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001573 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001574 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001575
Lei Zhanga21d5932018-02-05 18:28:38 +00001576 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001577 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001578 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001579 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001580
Lei Zhanga21d5932018-02-05 18:28:38 +00001581 // Check that the string value of an AP is not returned if the buffer is too
1582 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001583 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1584 // Write in the buffer to verify it's not overwritten.
1585 memcpy(buf.data(), "abcdefgh", 8);
1586 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001587 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001588 buf.data(), normal_length_bytes - 1));
1589 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001590
Lei Zhanga21d5932018-02-05 18:28:38 +00001591 // Check that the string value of an AP is returned through a buffer that is
1592 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001593 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001594 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001595 buf.data(), normal_length_bytes));
1596 EXPECT_EQ(kMd5NormalAP,
1597 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1598 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001599
Lei Zhanga21d5932018-02-05 18:28:38 +00001600 // Check that the string value of an AP is returned through a buffer that is
1601 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001602 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1603 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001604 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001605 buf.data(), normal_length_bytes + 2));
1606 EXPECT_EQ(kMd5NormalAP,
1607 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1608 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001609
Lei Zhanga21d5932018-02-05 18:28:38 +00001610 // Check that getting an AP for a mode that does not have an AP returns an
1611 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001612 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001613 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001614 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001615
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001616 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001617 EXPECT_EQ(2u,
1618 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001619 buf.data(), 1000));
1620 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001621
Lei Zhanga21d5932018-02-05 18:28:38 +00001622 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001623 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001624 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001625 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001626 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001627 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001628 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001629
Lei Zhanga21d5932018-02-05 18:28:38 +00001630 // Set the AP correctly now.
1631 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001632 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001633
Lei Zhanga21d5932018-02-05 18:28:38 +00001634 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001635 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001636 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001637 ASSERT_EQ(24u, rollover_length_bytes);
1638 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001639 EXPECT_EQ(24u,
1640 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001641 buf.data(), rollover_length_bytes));
1642 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001643
Lei Zhanga21d5932018-02-05 18:28:38 +00001644 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001645 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1646 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001647 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001648 buf.data(), normal_length_bytes));
1649 EXPECT_EQ(kMd5NormalAP,
1650 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1651 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001652 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001653
1654 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001655 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001656 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001657
Lei Zhang0b494052019-01-31 21:41:15 +00001658 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001659 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001660 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001661 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001662
Lei Zhanga21d5932018-02-05 18:28:38 +00001663 // Check that the new annotation value is equal to the value we set before
1664 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001665 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001666 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001667 ASSERT_EQ(24u, rollover_length_bytes);
1668 std::vector<FPDF_WCHAR> buf =
1669 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001670 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1671 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001672 buf.data(), rollover_length_bytes));
1673 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001674 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001675
1676 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001677 CloseSavedPage(page);
1678 CloseSavedDocument();
1679}
1680
Lei Zhangab41f252018-12-23 03:10:50 +00001681TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001682 // Open a file with four annotations and load its first page.
1683 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001684 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001685 ASSERT_TRUE(page);
1686
Lei Zhanga21d5932018-02-05 18:28:38 +00001687 {
1688 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001689 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001690 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001691
Lei Zhanga21d5932018-02-05 18:28:38 +00001692 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001693 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001694 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001695 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001696 EXPECT_EQ(73970u,
1697 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1698 nullptr, 0));
1699 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1700 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001701
Lei Zhanga21d5932018-02-05 18:28:38 +00001702 // Check that setting the Down AP to null removes the Down entry but keeps
1703 // Normal intact.
1704 EXPECT_TRUE(
1705 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1706 EXPECT_EQ(73970u,
1707 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1708 nullptr, 0));
1709 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1710 nullptr, 0));
1711 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001712
Lei Zhang75c81712018-02-08 17:22:39 +00001713 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001714}
1715
Lei Zhangab41f252018-12-23 03:10:50 +00001716TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001717 // Open a file with four annotations and load its first page.
1718 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001719 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001720 ASSERT_TRUE(page);
1721
Lei Zhanga21d5932018-02-05 18:28:38 +00001722 {
1723 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001724 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001725 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001726
Lei Zhanga21d5932018-02-05 18:28:38 +00001727 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001728 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001729 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001730 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001731 EXPECT_EQ(73970u,
1732 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1733 nullptr, 0));
1734 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1735 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001736
Lei Zhanga21d5932018-02-05 18:28:38 +00001737 // Check that setting the Normal AP to null removes the whole AP dictionary.
1738 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1739 nullptr));
1740 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1741 nullptr, 0));
1742 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1743 nullptr, 0));
1744 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001745
Lei Zhang75c81712018-02-08 17:22:39 +00001746 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001747}
1748
Lei Zhangab41f252018-12-23 03:10:50 +00001749TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001750 // Open a file with annotations and load its first page.
1751 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001752 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001753 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001754 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001755
Lei Zhanga21d5932018-02-05 18:28:38 +00001756 {
1757 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001758 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001759 ASSERT_TRUE(annot);
1760 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1761 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001762 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001763 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1764 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1765 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001766
Lei Zhanga21d5932018-02-05 18:28:38 +00001767 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001768 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001769 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1770 ASSERT_TRUE(popup);
1771 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1772 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1773 FS_RECTF rect;
1774 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1775 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1776 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001777
Lei Zhanga21d5932018-02-05 18:28:38 +00001778 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1779 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001780 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001781 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1782 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001783
Lei Zhanga21d5932018-02-05 18:28:38 +00001784 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1785 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001786 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001787 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001788 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1789 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001790 }
Jane Liu300bb272017-08-21 14:37:53 -04001791
Jane Liu300bb272017-08-21 14:37:53 -04001792 UnloadPage(page);
1793}
1794
Lei Zhangab41f252018-12-23 03:10:50 +00001795TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001796 // Open file with form text fields.
1797 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001798 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001799 ASSERT_TRUE(page);
1800
Lei Zhanga21d5932018-02-05 18:28:38 +00001801 {
1802 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001803 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001804 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001805
Lei Zhanga21d5932018-02-05 18:28:38 +00001806 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001807 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001808 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001809 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001810 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001811
Lei Zhanga21d5932018-02-05 18:28:38 +00001812 {
1813 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001814 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001815 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001816
Lei Zhanga21d5932018-02-05 18:28:38 +00001817 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001818 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001819 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001820 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1821 }
1822
1823 {
1824 // Retrieve the fourth annotation: user-editable password text field.
1825 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1826 ASSERT_TRUE(annot);
1827
1828 // Check that the flag values are as expected.
1829 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1830 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1831 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001832 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001833
1834 UnloadPage(page);
1835}
1836
Lei Zhangab41f252018-12-23 03:10:50 +00001837TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001838 // Open file with form text fields.
1839 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001840 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001841 ASSERT_TRUE(page);
1842
Lei Zhanga21d5932018-02-05 18:28:38 +00001843 {
1844 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001845 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001846 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001847
Lei Zhanga21d5932018-02-05 18:28:38 +00001848 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001849 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001850 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1851 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1852 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1853 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001854
Lei Zhanga21d5932018-02-05 18:28:38 +00001855 {
1856 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001857 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001858 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001859
Lei Zhanga21d5932018-02-05 18:28:38 +00001860 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001861 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001862 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1863 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1864 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1865 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001866
Lei Zhanga21d5932018-02-05 18:28:38 +00001867 {
1868 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001869 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001870 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001871
Lei Zhanga21d5932018-02-05 18:28:38 +00001872 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001873 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001874 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1875 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1876 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1877 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001878
1879 UnloadPage(page);
1880}
Diana Gage40870db2017-07-19 18:16:03 -07001881
Lei Zhangab41f252018-12-23 03:10:50 +00001882TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001883 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001884 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001885 FPDF_PAGE page = LoadPage(0);
1886 ASSERT_TRUE(page);
1887
1888 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001889 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1890 EXPECT_FALSE(
1891 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001892
Lei Zhang8da98232019-12-11 23:29:33 +00001893 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001894 {
1895 // Verify there is an annotation.
1896 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001897 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001898 EXPECT_TRUE(annot);
1899 }
1900
1901 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001902 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1903 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1904 EXPECT_FALSE(
1905 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001906
1907 UnloadPage(page);
1908}
1909
Lei Zhangab41f252018-12-23 03:10:50 +00001910TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001911 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001912 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001913 FPDF_PAGE page = LoadPage(0);
1914 ASSERT_TRUE(page);
1915
Lei Zhanga21d5932018-02-05 18:28:38 +00001916 {
1917 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001918 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001919 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001920 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001921 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001922
Lei Zhanga21d5932018-02-05 18:28:38 +00001923 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001924 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001925 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1926 }
Diana Gage40870db2017-07-19 18:16:03 -07001927
Lei Zhanga21d5932018-02-05 18:28:38 +00001928 {
1929 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001930 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001931 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001932 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001933 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001934
Lei Zhanga21d5932018-02-05 18:28:38 +00001935 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001936 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001937 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1938 }
Diana Gage40870db2017-07-19 18:16:03 -07001939
1940 UnloadPage(page);
1941}
1942
Lei Zhangab41f252018-12-23 03:10:50 +00001943TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001944 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001945 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001946 FPDF_PAGE page = LoadPage(0);
1947 ASSERT_TRUE(page);
1948
Lei Zhanga21d5932018-02-05 18:28:38 +00001949 {
1950 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001951 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001952 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001953 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001954 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001955
Lei Zhanga21d5932018-02-05 18:28:38 +00001956 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001957 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001958 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1959 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1960 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1961 }
Diana Gage40870db2017-07-19 18:16:03 -07001962
Lei Zhanga21d5932018-02-05 18:28:38 +00001963 {
1964 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001965 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001966 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001967 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001968 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001969
Lei Zhanga21d5932018-02-05 18:28:38 +00001970 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001971 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001972 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1973 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1974 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1975 }
Diana Gage40870db2017-07-19 18:16:03 -07001976
Lei Zhanga21d5932018-02-05 18:28:38 +00001977 {
1978 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001979 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001980 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001981 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001982 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001983
Lei Zhanga21d5932018-02-05 18:28:38 +00001984 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001985 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001986 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1987 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1988 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1989 }
Diana Gage40870db2017-07-19 18:16:03 -07001990
1991 UnloadPage(page);
1992}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001993
Lei Zhang03e5e682019-09-16 19:45:55 +00001994// TODO(crbug.com/pdfium/11): Fix this test and enable.
1995#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1996#define MAYBE_BUG_1206 DISABLED_BUG_1206
1997#else
1998#define MAYBE_BUG_1206 BUG_1206
1999#endif
2000TEST_F(FPDFAnnotEmbedderTest, MAYBE_BUG_1206) {
Lei Zhang992e7e22019-02-04 19:20:58 +00002001 static constexpr size_t kExpectedSize = 1609;
2002 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
2003
2004 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2005
2006 FPDF_PAGE page = LoadPage(0);
2007 ASSERT_TRUE(page);
2008
2009 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2010 EXPECT_EQ(kExpectedSize, GetString().size());
2011 ClearString();
2012
2013 for (size_t i = 0; i < 10; ++i) {
2014 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2015 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2016
2017 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2018 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2019 // equal, not bigger.
2020 EXPECT_LT(kExpectedSize, GetString().size());
2021 ClearString();
2022 }
2023
2024 UnloadPage(page);
2025}
2026
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002027TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2028 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2029 FPDF_PAGE page = LoadPage(0);
2030 ASSERT_TRUE(page);
2031 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2032
2033 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002034 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002035 static const size_t kBufSize = 12;
2036 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002037
2038 {
2039 // Add a text annotation to the page.
2040 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2041 ASSERT_TRUE(annot);
2042 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2043 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2044
2045 // Make sure there is no test key, add set a value there, and read it back.
2046 std::fill(buf.begin(), buf.end(), 'x');
2047 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002048 kBufSize));
2049 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002050
Lei Zhangf0f67682019-04-08 17:03:21 +00002051 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002052 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2053
2054 std::fill(buf.begin(), buf.end(), 'x');
2055 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002056 kBufSize));
2057 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002058 }
2059
Lei Zhang05ec64c2019-01-09 03:00:06 +00002060 {
2061 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2062 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002063 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002064 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002065 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2066 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2067 // Also do the same test for its appearance string.
2068 std::fill(buf.begin(), buf.end(), 'x');
2069 ASSERT_EQ(2u,
2070 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002071 buf.data(), kBufSize));
2072 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002073
Lei Zhangf0f67682019-04-08 17:03:21 +00002074 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002075 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2076 text.get()));
2077
2078 std::fill(buf.begin(), buf.end(), 'x');
2079 ASSERT_EQ(6u,
2080 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002081 buf.data(), kBufSize));
2082 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002083 }
2084
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002085 UnloadPage(page);
2086
2087 {
2088 // Save a copy, open the copy, and check the annotation again.
2089 // Note that it renders the rotation.
2090 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002091 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002092 FPDF_PAGE saved_page = LoadSavedPage(0);
2093 ASSERT_TRUE(saved_page);
2094
Lei Zhang05ec64c2019-01-09 03:00:06 +00002095 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002096 {
2097 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2098 ASSERT_TRUE(annot);
2099 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2100
2101 std::fill(buf.begin(), buf.end(), 'x');
2102 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002103 kBufSize));
2104 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002105 }
2106
Lei Zhang05ec64c2019-01-09 03:00:06 +00002107 {
2108 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2109 ASSERT_TRUE(annot);
2110 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2111 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2112
2113 std::fill(buf.begin(), buf.end(), 'x');
2114 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002115 kBufSize));
2116 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002117 }
2118
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002119 CloseSavedPage(saved_page);
2120 CloseSavedDocument();
2121 }
2122}
rycsmithcb752f32019-02-21 18:40:53 +00002123
2124TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2125 // Open a file with combobox widget annotations and load its first page.
2126 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2127 FPDF_PAGE page = LoadPage(0);
2128 ASSERT_TRUE(page);
2129
2130 {
2131 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2132 ASSERT_TRUE(annot);
2133
2134 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2135
2136 annot.reset(FPDFPage_GetAnnot(page, 1));
2137 ASSERT_TRUE(annot);
2138
2139 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002140
2141 // Check bad form handle / annot.
2142 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2143 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2144 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002145 }
2146
2147 UnloadPage(page);
2148}
2149
2150TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2151 // Open a file with listbox widget annotations and load its first page.
2152 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2153 FPDF_PAGE page = LoadPage(0);
2154 ASSERT_TRUE(page);
2155
2156 {
2157 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2158 ASSERT_TRUE(annot);
2159
2160 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2161
2162 annot.reset(FPDFPage_GetAnnot(page, 1));
2163 ASSERT_TRUE(annot);
2164
2165 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2166 }
2167
2168 UnloadPage(page);
2169}
2170
2171TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2172 // Open a file with ink annotations and load its first page.
2173 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2174 FPDF_PAGE page = LoadPage(0);
2175 ASSERT_TRUE(page);
2176
2177 {
2178 // annotations do not have "Opt" array and will return -1
2179 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2180 ASSERT_TRUE(annot);
2181
2182 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2183
2184 annot.reset(FPDFPage_GetAnnot(page, 1));
2185 ASSERT_TRUE(annot);
2186
2187 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2188 }
2189
2190 UnloadPage(page);
2191}
2192
2193TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2194 // Open a file with combobox widget annotations and load its first page.
2195 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2196 FPDF_PAGE page = LoadPage(0);
2197 ASSERT_TRUE(page);
2198
2199 {
2200 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2201 ASSERT_TRUE(annot);
2202
2203 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002204 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002205 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002206 ASSERT_EQ(8u, length_bytes);
2207 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002208 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002209 buf.data(), length_bytes));
2210 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002211
2212 annot.reset(FPDFPage_GetAnnot(page, 1));
2213 ASSERT_TRUE(annot);
2214
2215 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002216 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002217 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002218 ASSERT_EQ(12u, length_bytes);
2219 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002220 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002221 buf.data(), length_bytes));
2222 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002223
2224 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002225 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002226 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002227 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002228 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002229 buf.data(), length_bytes));
2230 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002231
Lei Zhange7033c82019-02-26 19:30:49 +00002232 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002233 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2234 nullptr, 0));
2235 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2236 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002237
2238 // Check bad form handle / annot.
2239 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2240 EXPECT_EQ(0u,
2241 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2242 EXPECT_EQ(0u,
2243 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002244 }
2245
2246 UnloadPage(page);
2247}
2248
2249TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2250 // Open a file with listbox widget annotations and load its first page.
2251 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2252 FPDF_PAGE page = LoadPage(0);
2253 ASSERT_TRUE(page);
2254
2255 {
2256 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2257 ASSERT_TRUE(annot);
2258
2259 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002260 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002261 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002262 ASSERT_EQ(8u, length_bytes);
2263 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002264 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002265 buf.data(), length_bytes));
2266 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002267
2268 annot.reset(FPDFPage_GetAnnot(page, 1));
2269 ASSERT_TRUE(annot);
2270
2271 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002272 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002273 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002274 ASSERT_EQ(12u, length_bytes);
2275 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002276 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002277 buf.data(), length_bytes));
2278 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002279
2280 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002281 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002282 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002283 ASSERT_EQ(18u, length_bytes);
2284 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002285 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002286 buf.data(), length_bytes));
2287 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002288
2289 // indices out of range
2290 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2291 nullptr, 0));
2292 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2293 nullptr, 0));
2294 }
2295
2296 UnloadPage(page);
2297}
2298
2299TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2300 // Open a file with ink annotations and load its first page.
2301 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2302 FPDF_PAGE page = LoadPage(0);
2303 ASSERT_TRUE(page);
2304
2305 {
2306 // annotations do not have "Opt" array and will return 0
2307 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2308 ASSERT_TRUE(annot);
2309
2310 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2311 nullptr, 0));
2312
2313 annot.reset(FPDFPage_GetAnnot(page, 1));
2314 ASSERT_TRUE(annot);
2315
2316 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2317 nullptr, 0));
2318 }
2319
2320 UnloadPage(page);
2321}
Ryan Smith09c23b12019-04-25 18:09:06 +00002322
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002323TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2324 // Open a file with combobox widget annotations and load its first page.
2325 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2326 FPDF_PAGE page = LoadPage(0);
2327 ASSERT_TRUE(page);
2328
2329 {
2330 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2331 ASSERT_TRUE(annot);
2332
2333 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2334 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2335 ASSERT_EQ(3, count);
2336 for (int i = 0; i < count; i++) {
2337 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2338 }
2339
2340 annot.reset(FPDFPage_GetAnnot(page, 1));
2341 ASSERT_TRUE(annot);
2342
2343 // Checks for Combobox with Values (/V) object which is just a string.
2344 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2345 ASSERT_EQ(26, count);
2346 for (int i = 0; i < count; i++) {
2347 EXPECT_EQ(i == 1,
2348 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2349 }
2350
2351 // Checks for index outside bound i.e. (index >= CountOption()).
2352 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2353 /*index=*/26));
2354 // Checks for negetive index.
2355 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2356 /*index=*/-1));
2357
2358 // Checks for bad form handle/annot.
2359 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2360 EXPECT_FALSE(
2361 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2362 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2363 }
2364
2365 UnloadPage(page);
2366}
2367
2368TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2369 // Open a file with listbox widget annotations and load its first page.
2370 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2371 FPDF_PAGE page = LoadPage(0);
2372 ASSERT_TRUE(page);
2373
2374 {
2375 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2376 ASSERT_TRUE(annot);
2377
2378 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2379 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2380 ASSERT_EQ(3, count);
2381 for (int i = 0; i < count; i++) {
2382 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2383 }
2384
2385 annot.reset(FPDFPage_GetAnnot(page, 1));
2386 ASSERT_TRUE(annot);
2387
2388 // Checks for Listbox with Values (/V) object which is just a string.
2389 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2390 ASSERT_EQ(26, count);
2391 for (int i = 0; i < count; i++) {
2392 EXPECT_EQ(i == 1,
2393 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2394 }
2395
2396 annot.reset(FPDFPage_GetAnnot(page, 3));
2397 ASSERT_TRUE(annot);
2398
2399 // Checks for Listbox with only Selected indices (/I) object which is an
2400 // array with multiple objects.
2401 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2402 ASSERT_EQ(5, count);
2403 for (int i = 0; i < count; i++) {
2404 bool expected = (i == 1 || i == 3);
2405 EXPECT_EQ(expected,
2406 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2407 }
2408
2409 annot.reset(FPDFPage_GetAnnot(page, 4));
2410 ASSERT_TRUE(annot);
2411
2412 // Checks for Listbox with Values (/V) object which is an array with
2413 // multiple objects.
2414 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2415 ASSERT_EQ(5, count);
2416 for (int i = 0; i < count; i++) {
2417 bool expected = (i == 2 || i == 4);
2418 EXPECT_EQ(expected,
2419 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2420 }
2421
2422 annot.reset(FPDFPage_GetAnnot(page, 5));
2423 ASSERT_TRUE(annot);
2424
2425 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2426 // objects conflict with different lengths.
2427 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2428 ASSERT_EQ(5, count);
2429 for (int i = 0; i < count; i++) {
2430 bool expected = (i == 0 || i == 2);
2431 EXPECT_EQ(expected,
2432 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2433 }
2434 }
2435
2436 UnloadPage(page);
2437}
2438
2439TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2440 // Open a file with multiple form field annotations and load its first page.
2441 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2442 FPDF_PAGE page = LoadPage(0);
2443 ASSERT_TRUE(page);
2444
2445 {
2446 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2447 ASSERT_TRUE(annot);
2448
2449 // Checks for link annotation.
2450 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2451 /*index=*/0));
2452
2453 annot.reset(FPDFPage_GetAnnot(page, 3));
2454 ASSERT_TRUE(annot);
2455
2456 // Checks for text field annotation.
2457 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2458 /*index=*/0));
2459 }
2460
2461 UnloadPage(page);
2462}
2463
Ryan Smith09c23b12019-04-25 18:09:06 +00002464TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2465 // Open a file with combobox annotations and load its first page.
2466 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2467 FPDF_PAGE page = LoadPage(0);
2468 ASSERT_TRUE(page);
2469
2470 {
2471 // All 3 widgets have Tf font size 12.
2472 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2473 ASSERT_TRUE(annot);
2474
2475 float value;
2476 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2477 EXPECT_EQ(12.0, value);
2478
2479 annot.reset(FPDFPage_GetAnnot(page, 1));
2480 ASSERT_TRUE(annot);
2481
2482 float value_two;
2483 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2484 EXPECT_EQ(12.0, value_two);
2485
2486 annot.reset(FPDFPage_GetAnnot(page, 2));
2487 ASSERT_TRUE(annot);
2488
2489 float value_three;
2490 ASSERT_TRUE(
2491 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2492 EXPECT_EQ(12.0, value_three);
2493 }
2494
2495 UnloadPage(page);
2496}
2497
2498TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2499 // Open a file with textfield annotations and load its first page.
2500 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2501 FPDF_PAGE page = LoadPage(0);
2502 ASSERT_TRUE(page);
2503
2504 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002505 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002506 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2507 ASSERT_TRUE(annot);
2508
2509 float value;
2510 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2511 EXPECT_EQ(12.0, value);
2512
2513 annot.reset(FPDFPage_GetAnnot(page, 1));
2514 ASSERT_TRUE(annot);
2515
2516 float value_two;
2517 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2518 EXPECT_EQ(12.0, value_two);
2519
2520 annot.reset(FPDFPage_GetAnnot(page, 2));
2521 ASSERT_TRUE(annot);
2522
2523 float value_three;
2524 ASSERT_TRUE(
2525 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2526 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002527
2528 float value_four;
2529 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2530 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002531 }
2532
2533 UnloadPage(page);
2534}
2535
2536TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2537 // Open a file with ink annotations and load its first page.
2538 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2539 FPDF_PAGE page = LoadPage(0);
2540 ASSERT_TRUE(page);
2541
2542 {
2543 // Annotations that do not have variable text and will return -1.
2544 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2545 ASSERT_TRUE(annot);
2546
2547 float value;
2548 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2549
2550 annot.reset(FPDFPage_GetAnnot(page, 1));
2551 ASSERT_TRUE(annot);
2552
2553 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2554 }
2555
2556 UnloadPage(page);
2557}
2558
2559TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2560 // Open a file with combobox annotations and load its first page.
2561 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2562 FPDF_PAGE page = LoadPage(0);
2563 ASSERT_TRUE(page);
2564
2565 {
2566 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2567 ASSERT_TRUE(annot);
2568
2569 // Check bad form handle / annot.
2570 float value;
2571 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2572 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2573 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2574 }
2575
2576 UnloadPage(page);
2577}
2578
2579TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2580 // Open a file with textfield annotations and load its first page.
2581 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2582 FPDF_PAGE page = LoadPage(0);
2583 ASSERT_TRUE(page);
2584
2585 {
2586 // Obtain the first annotation, a text field with negative font size, -12.
2587 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2588 ASSERT_TRUE(annot);
2589
2590 float value;
2591 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2592 EXPECT_EQ(-12.0, value);
2593 }
2594
2595 UnloadPage(page);
2596}
Ryan Smith23fdf892019-07-17 21:51:26 +00002597
2598TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2599 // Open a file with checkbox and radiobuttons widget annotations and load its
2600 // first page.
2601 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2602 FPDF_PAGE page = LoadPage(0);
2603 ASSERT_TRUE(page);
2604
2605 {
2606 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2607 ASSERT_TRUE(annot);
2608 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2609 }
2610
2611 UnloadPage(page);
2612}
2613
2614TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2615 // Open a file with checkbox and radiobutton widget annotations and load its
2616 // first page.
2617 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2618 FPDF_PAGE page = LoadPage(0);
2619 ASSERT_TRUE(page);
2620
2621 {
2622 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2623 ASSERT_TRUE(annot);
2624 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2625 }
2626
2627 UnloadPage(page);
2628}
2629
2630TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2631 // Open a file with checkbox and radiobutton widget annotations and load its
2632 // first page.
2633 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2634 FPDF_PAGE page = LoadPage(0);
2635 ASSERT_TRUE(page);
2636
2637 {
2638 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2639 ASSERT_TRUE(annot);
2640 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2641
2642 annot.reset(FPDFPage_GetAnnot(page, 6));
2643 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2644
2645 annot.reset(FPDFPage_GetAnnot(page, 7));
2646 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2647 }
2648
2649 UnloadPage(page);
2650}
2651
2652TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2653 // Open a file with checkbox and radiobutton widget annotations and load its
2654 // first page.
2655 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2656 FPDF_PAGE page = LoadPage(0);
2657 ASSERT_TRUE(page);
2658
2659 {
2660 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2661 ASSERT_TRUE(annot);
2662 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2663
2664 annot.reset(FPDFPage_GetAnnot(page, 3));
2665 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2666
2667 annot.reset(FPDFPage_GetAnnot(page, 4));
2668 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2669 }
2670
2671 UnloadPage(page);
2672}
2673
2674TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2675 // Open a file with checkbox and radiobuttons widget annotations and load its
2676 // first page.
2677 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2678 FPDF_PAGE page = LoadPage(0);
2679 ASSERT_TRUE(page);
2680
2681 {
2682 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2683 ASSERT_TRUE(annot);
2684 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2685 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2686 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2687 }
2688
2689 UnloadPage(page);
2690}
2691
2692TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2693 // Open a file with text widget annotations and load its first page.
2694 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2695 FPDF_PAGE page = LoadPage(0);
2696 ASSERT_TRUE(page);
2697
2698 {
2699 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2700 ASSERT_TRUE(annot);
2701 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2702 }
2703
2704 UnloadPage(page);
2705}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002706
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002707TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2708 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002709 FPDF_PAGE page = LoadPage(0);
2710 ASSERT_TRUE(page);
2711
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002712 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2713
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002714 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002715 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002716 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002717 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002718 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002719
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002720 constexpr int kExpectedAnnotTypes[] = {-1,
2721 FPDF_FORMFIELD_COMBOBOX,
2722 FPDF_FORMFIELD_LISTBOX,
2723 FPDF_FORMFIELD_TEXTFIELD,
2724 FPDF_FORMFIELD_CHECKBOX,
2725 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002726
Lei Zhang4501a502020-05-18 16:52:59 +00002727 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002728 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002729 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002730 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002731 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2732 }
2733 UnloadPage(page);
2734}
2735
2736TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2737 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2738 FPDF_PAGE page = LoadPage(0);
2739 ASSERT_TRUE(page);
2740
2741 {
2742 EXPECT_EQ(0u,
2743 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2744
2745 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2746 ASSERT_TRUE(annot);
2747
2748 EXPECT_EQ(0u,
2749 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2750
2751 unsigned long length_bytes =
2752 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2753 ASSERT_EQ(2u, length_bytes);
2754 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2755 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2756 buf.data(), length_bytes));
2757 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2758 }
2759 {
2760 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2761 ASSERT_TRUE(annot);
2762
2763 unsigned long length_bytes =
2764 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2765 ASSERT_EQ(18u, length_bytes);
2766 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2767 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2768 buf.data(), length_bytes));
2769 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2770 }
2771 UnloadPage(page);
2772}
2773
2774TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2775 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2776 FPDF_PAGE page = LoadPage(0);
2777 ASSERT_TRUE(page);
2778
2779 {
2780 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2781 ASSERT_TRUE(annot);
2782
2783 unsigned long length_bytes =
2784 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2785 ASSERT_EQ(2u, length_bytes);
2786 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2787 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2788 buf.data(), length_bytes));
2789 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2790 }
2791 {
2792 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2793 ASSERT_TRUE(annot);
2794
2795 unsigned long length_bytes =
2796 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2797 ASSERT_EQ(14u, length_bytes);
2798 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2799 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2800 buf.data(), length_bytes));
2801 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2802 }
2803 UnloadPage(page);
2804}
2805
2806TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2807 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2808 FPDF_PAGE page = LoadPage(0);
2809 ASSERT_TRUE(page);
2810
2811 {
2812 EXPECT_EQ(0u,
2813 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2814
2815 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2816 ASSERT_TRUE(annot);
2817
2818 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2819
2820 unsigned long length_bytes =
2821 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2822 ASSERT_EQ(18u, length_bytes);
2823 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2824 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2825 buf.data(), length_bytes));
2826 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2827 }
2828 UnloadPage(page);
2829}
2830
2831TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2832 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2833 FPDF_PAGE page = LoadPage(0);
2834 ASSERT_TRUE(page);
2835
2836 {
2837 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2838 ASSERT_TRUE(annot);
2839
2840 unsigned long length_bytes =
2841 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2842 ASSERT_EQ(30u, length_bytes);
2843 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2844 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2845 buf.data(), length_bytes));
2846 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2847 }
2848 UnloadPage(page);
2849}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002850
Lei Zhang9b444002020-04-17 17:35:23 +00002851TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2852 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002853 FPDF_PAGE page = LoadPage(0);
2854 ASSERT_TRUE(page);
2855
Lei Zhang9b444002020-04-17 17:35:23 +00002856 // Verify widgets are by default focusable.
2857 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2858 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002859
Lei Zhang9b444002020-04-17 17:35:23 +00002860 // Expected annot subtypes for page 0 of annots.pdf.
2861 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2862 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2863 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2864 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2865 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002866
Lei Zhang9b444002020-04-17 17:35:23 +00002867 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002868 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002869 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2870 kExpectedAnnotSubtypes,
2871 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002872
Lei Zhang9b444002020-04-17 17:35:23 +00002873 // Make no annotation type focusable using the preferred method.
2874 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2875 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002876
Lei Zhang9b444002020-04-17 17:35:23 +00002877 // Restore the focusable type count back to 1, then set it back to 0 using a
2878 // different method.
2879 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2880 ASSERT_TRUE(
2881 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2882 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002883
Lei Zhang9b444002020-04-17 17:35:23 +00002884 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002885 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002886
Lei Zhang9b444002020-04-17 17:35:23 +00002887 // Now make links focusable.
2888 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2889 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002890
Lei Zhang9b444002020-04-17 17:35:23 +00002891 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002892 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002893 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2894 kExpectedAnnotSubtypes,
2895 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002896
Lei Zhang9b444002020-04-17 17:35:23 +00002897 // Test invalid parameters.
2898 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00002899 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002900 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00002901 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002902 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2903
Lei Zhang9b444002020-04-17 17:35:23 +00002904 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2905 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2906 subtypes.size()));
2907 EXPECT_FALSE(
2908 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2909 EXPECT_FALSE(
2910 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002911
2912 UnloadPage(page);
2913}
Lei Zhang671aece2020-04-14 19:02:26 +00002914
Hui Yingst609f7d62020-04-23 23:14:13 +00002915// TODO(crbug.com/pdfium/11): Fix this test and enable.
2916#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2917#define MAYBE_FocusableAnnotRendering DISABLED_FocusableAnnotRendering
2918#else
2919#define MAYBE_FocusableAnnotRendering FocusableAnnotRendering
2920#endif
2921TEST_F(FPDFAnnotEmbedderTest, MAYBE_FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00002922 ASSERT_TRUE(OpenDocument("annots.pdf"));
2923 FPDF_PAGE page = LoadPage(0);
2924 ASSERT_TRUE(page);
2925
2926 {
2927#if defined(OS_WIN)
2928 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
2929#elif defined(OS_MACOSX)
2930 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
2931#else
2932 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
2933#endif
2934 // Check the initial rendering.
2935 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2936 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2937 }
2938
2939 // Make links and highlights focusable.
2940 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
2941 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00002942 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00002943 ASSERT_TRUE(
2944 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
2945 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
2946 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
2947 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
2948 subtypes.size()));
2949 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
2950 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
2951
2952 {
2953#if defined(OS_WIN)
2954 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
2955#elif defined(OS_MACOSX)
2956 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
2957#else
2958 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
2959#endif
2960 // Focus the first link and check the rendering.
2961 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2962 ASSERT_TRUE(annot);
2963 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2964 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2965 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2966 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2967 }
2968
2969 {
2970#if defined(OS_WIN)
2971 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
2972#elif defined(OS_MACOSX)
2973 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
2974#else
2975 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
2976#endif
2977 // Focus the first highlight and check the rendering.
2978 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2979 ASSERT_TRUE(annot);
2980 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, 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 UnloadPage(page);
2987}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00002988
2989TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
2990 ASSERT_TRUE(OpenDocument("annots.pdf"));
2991 FPDF_PAGE page = LoadPage(0);
2992 ASSERT_TRUE(page);
2993 {
2994 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
2995 ASSERT_TRUE(annot);
2996 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2997 FPDF_LINK link_annot = FPDFAnnot_GetLink(annot.get());
2998 ASSERT_TRUE(link_annot);
2999
3000 FPDF_ACTION action = FPDFLink_GetAction(link_annot);
3001 ASSERT_TRUE(action);
3002 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
3003 FPDFAction_GetType(action));
3004
3005 constexpr char kExpectedResult[] =
3006 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3007 "fpdf_text.h";
Lei Zhang4501a502020-05-18 16:52:59 +00003008 constexpr unsigned long kExpectedLength = pdfium::size(kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003009 unsigned long bufsize =
3010 FPDFAction_GetURIPath(document(), action, nullptr, 0);
3011 ASSERT_EQ(kExpectedLength, bufsize);
3012
3013 char buffer[1024];
3014 EXPECT_EQ(bufsize,
3015 FPDFAction_GetURIPath(document(), action, buffer, bufsize));
3016 EXPECT_STREQ(kExpectedResult, buffer);
3017 }
3018
3019 {
3020 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3021 ASSERT_TRUE(annot);
3022 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3023 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3024 }
3025
3026 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3027
3028 UnloadPage(page);
3029}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003030
3031TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3032 // Open a file with radio button widget annotations and load its first page.
3033 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3034 FPDF_PAGE page = LoadPage(0);
3035 ASSERT_TRUE(page);
3036
3037 {
3038 // Checks for bad annot.
3039 EXPECT_EQ(-1,
3040 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3041
3042 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3043 ASSERT_TRUE(annot);
3044
3045 // Checks for bad form handle.
3046 EXPECT_EQ(-1,
3047 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3048
3049 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3050 }
3051
3052 UnloadPage(page);
3053}
3054
3055TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3056 // Open a file with checkbox widget annotations and load its first page.
3057 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3058 FPDF_PAGE page = LoadPage(0);
3059 ASSERT_TRUE(page);
3060
3061 {
3062 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3063 ASSERT_TRUE(annot);
3064 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3065 }
3066
3067 UnloadPage(page);
3068}
3069
3070TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3071 // Open a file with ink annotations and load its first page.
3072 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3073 FPDF_PAGE page = LoadPage(0);
3074 ASSERT_TRUE(page);
3075
3076 {
3077 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3078 ASSERT_TRUE(annot);
3079 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3080 }
3081
3082 UnloadPage(page);
3083}
3084
3085TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3086 // Open a file with radio button widget annotations and load its first page.
3087 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3088 FPDF_PAGE page = LoadPage(0);
3089 ASSERT_TRUE(page);
3090
3091 {
3092 // Checks for bad annot.
3093 EXPECT_EQ(-1,
3094 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3095
3096 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3097 ASSERT_TRUE(annot);
3098
3099 // Checks for bad form handle.
3100 EXPECT_EQ(-1,
3101 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3102
3103 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3104 }
3105
3106 UnloadPage(page);
3107}
3108
3109TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3110 // Open a file with checkbox widget annotations and load its first page.
3111 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3112 FPDF_PAGE page = LoadPage(0);
3113 ASSERT_TRUE(page);
3114
3115 {
3116 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3117 ASSERT_TRUE(annot);
3118 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3119 }
3120
3121 UnloadPage(page);
3122}
3123
3124TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3125 // Open a file with ink annotations and load its first page.
3126 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3127 FPDF_PAGE page = LoadPage(0);
3128 ASSERT_TRUE(page);
3129
3130 {
3131 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3132 ASSERT_TRUE(annot);
3133 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3134 }
3135
3136 UnloadPage(page);
3137}
3138
3139TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3140 // Open a file with radio button widget annotations and load its first page.
3141 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3142 FPDF_PAGE page = LoadPage(0);
3143 ASSERT_TRUE(page);
3144
3145 {
3146 // Checks for bad annot.
3147 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3148 form_handle(), /*annot=*/nullptr,
3149 /*buffer=*/nullptr, /*buflen=*/0));
3150
3151 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3152 ASSERT_TRUE(annot);
3153
3154 // Checks for bad form handle.
3155 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3156 /*hHandle=*/nullptr, annot.get(),
3157 /*buffer=*/nullptr, /*buflen=*/0));
3158
3159 unsigned long length_bytes =
3160 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3161 /*buffer=*/nullptr, /*buflen=*/0);
3162 ASSERT_EQ(14u, length_bytes);
3163 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3164 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3165 buf.data(), length_bytes));
3166 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3167 }
3168
3169 UnloadPage(page);
3170}
3171
3172TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3173 // Open a file with checkbox widget annotations and load its first page.
3174 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3175 FPDF_PAGE page = LoadPage(0);
3176 ASSERT_TRUE(page);
3177
3178 {
3179 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3180 ASSERT_TRUE(annot);
3181
3182 unsigned long length_bytes =
3183 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3184 /*buffer=*/nullptr, /*buflen=*/0);
3185 ASSERT_EQ(8u, length_bytes);
3186 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3187 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3188 buf.data(), length_bytes));
3189 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3190 }
3191
3192 UnloadPage(page);
3193}
3194
3195TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3196 // Open a file with ink annotations and load its first page.
3197 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3198 FPDF_PAGE page = LoadPage(0);
3199 ASSERT_TRUE(page);
3200
3201 {
3202 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3203 ASSERT_TRUE(annot);
3204 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3205 /*buffer=*/nullptr,
3206 /*buflen=*/0));
3207 }
3208
3209 UnloadPage(page);
3210}