blob: 7ed02ee74e5bc2b08115f9285445b0450db30f9d [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
Lei Zhang03e5e682019-09-16 19:45:55 +0000406// TODO(crbug.com/pdfium/11): Fix this test and enable.
407#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
408#define MAYBE_RenderMultilineMarkupAnnotWithoutAP \
409 DISABLED_RenderMultilineMarkupAnnotWithoutAP
410#else
411#define MAYBE_RenderMultilineMarkupAnnotWithoutAP \
412 RenderMultilineMarkupAnnotWithoutAP
413#endif
414TEST_F(FPDFAnnotEmbedderTest, MAYBE_RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang4f556b82019-04-08 16:32:41 +0000415 static const char kMd5[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +0000416 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000417 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
418 FPDF_PAGE page = LoadPage(0);
419 ASSERT_TRUE(page);
420
Tom Sepeze08d2b12018-04-25 18:49:32 +0000421 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000422 CompareBitmap(bitmap.get(), 595, 842, kMd5);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000423
424 UnloadPage(page);
425}
426
Lei Zhangab41f252018-12-23 03:10:50 +0000427TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400428 // Open a file with one annotation and load its first page.
429 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000430 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400431 ASSERT_TRUE(page);
432
433 // Check that there is a total of 1 annotation on its first page.
434 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
435
436 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000437 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000438 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000439 ASSERT_TRUE(annot);
440 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400441
Lei Zhanga21d5932018-02-05 18:28:38 +0000442 // Check that the annotation color is yellow.
443 unsigned int R;
444 unsigned int G;
445 unsigned int B;
446 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000447 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000448 &G, &B, &A));
449 EXPECT_EQ(255u, R);
450 EXPECT_EQ(255u, G);
451 EXPECT_EQ(0u, B);
452 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400453
Lei Zhanga21d5932018-02-05 18:28:38 +0000454 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000455 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000456 EXPECT_EQ(FPDF_OBJECT_STRING,
457 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000458 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000459 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000460 ASSERT_EQ(28u, length_bytes);
461 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000462 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000463 length_bytes));
464 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400465
Lei Zhanga21d5932018-02-05 18:28:38 +0000466 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000467 EXPECT_EQ(
468 FPDF_OBJECT_STRING,
469 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000470 length_bytes = FPDFAnnot_GetStringValue(
471 annot.get(), pdfium::annotation::kContents, nullptr, 0);
472 ASSERT_EQ(2690u, length_bytes);
473 buf = GetFPDFWideStringBuffer(length_bytes);
474 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
475 pdfium::annotation::kContents,
476 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000477 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000478 L"This is a note for that highlight annotation. Very long highlight "
479 "annotation. Long long long Long 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 longLong long longLong long longLong long longLong long "
494 "longLong long longLong long longLong long longLong long longLong long "
495 "longLong long longLong long longLong long longLong long longLong long "
496 "longLong long longLong long longLong long longLong long longLong long "
497 "longLong long long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000498 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400499
Lei Zhanga21d5932018-02-05 18:28:38 +0000500 // Check that the quadpoints are correct.
501 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000502 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000503 EXPECT_EQ(115.802643f, quadpoints.x1);
504 EXPECT_EQ(718.913940f, quadpoints.y1);
505 EXPECT_EQ(157.211182f, quadpoints.x4);
506 EXPECT_EQ(706.264465f, quadpoints.y4);
507 }
Tom Sepez507d0192018-11-07 16:37:51 +0000508 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400509}
510
Lei Zhang03e5e682019-09-16 19:45:55 +0000511// TODO(crbug.com/pdfium/11): Fix this test and enable.
512#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
513#define MAYBE_ExtractInkMultiple DISABLED_ExtractInkMultiple
514#else
515#define MAYBE_ExtractInkMultiple ExtractInkMultiple
516#endif
517TEST_F(FPDFAnnotEmbedderTest, MAYBE_ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400518 // Open a file with three annotations and load its first page.
519 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000520 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400521 ASSERT_TRUE(page);
522
523 // Check that there is a total of 3 annotation on its first page.
524 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
525
Lei Zhanga21d5932018-02-05 18:28:38 +0000526 {
527 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000528 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000529 ASSERT_TRUE(annot);
530 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400531
Lei Zhanga21d5932018-02-05 18:28:38 +0000532 // Check that the annotation color is blue with opacity.
533 unsigned int R;
534 unsigned int G;
535 unsigned int B;
536 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000537 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000538 &G, &B, &A));
539 EXPECT_EQ(0u, R);
540 EXPECT_EQ(0u, G);
541 EXPECT_EQ(255u, B);
542 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400543
Lei Zhanga21d5932018-02-05 18:28:38 +0000544 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000545 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
546 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400547
Lei Zhang4f556b82019-04-08 16:32:41 +0000548 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000549 // Note that upon rendering, the rectangle coordinates will be adjusted.
550 FS_RECTF rect;
551 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
552 EXPECT_EQ(351.820404f, rect.left);
553 EXPECT_EQ(583.830688f, rect.bottom);
554 EXPECT_EQ(475.336090f, rect.right);
555 EXPECT_EQ(681.535034f, rect.top);
556 }
Tom Sepezef43c262018-11-07 16:41:32 +0000557 {
Lei Zhang430b5322020-07-06 22:23:36 +0000558#if defined(OS_WIN)
559 static constexpr char kExpectedHash[] = "49d0a81c636531a337429325273d0508";
560#else
561 static constexpr char kExpectedHash[] = "354002e1c4386d38fdde29ef8d61074a";
562#endif
Tom Sepezef43c262018-11-07 16:41:32 +0000563 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang430b5322020-07-06 22:23:36 +0000564 CompareBitmap(bitmap.get(), 612, 792, kExpectedHash);
Tom Sepezef43c262018-11-07 16:41:32 +0000565 }
Tom Sepez507d0192018-11-07 16:37:51 +0000566 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400567}
Jane Liu20eafda2017-06-07 10:33:24 -0400568
Lei Zhangab41f252018-12-23 03:10:50 +0000569TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400570 // Open a file with one annotation and load its first page.
571 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000572 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400573 ASSERT_TRUE(page);
574
575 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400576 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400577
578 UnloadPage(page);
579}
580
Lei Zhangab41f252018-12-23 03:10:50 +0000581TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400582 // Open a file with no annotation and load its first page.
583 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000584 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400585 ASSERT_TRUE(page);
586 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
587
Lei Zhanga21d5932018-02-05 18:28:38 +0000588 {
589 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000590 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000591 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400592
Lei Zhanga21d5932018-02-05 18:28:38 +0000593 // Check that there is now 1 annotations on this page.
594 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400595
Lei Zhanga21d5932018-02-05 18:28:38 +0000596 // Check that the subtype of the annotation is correct.
597 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
598 }
Jane Liue10509a2017-06-20 16:47:41 -0400599
Lei Zhanga21d5932018-02-05 18:28:38 +0000600 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000601 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000602 ASSERT_TRUE(annot);
603 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400604
Lei Zhanga21d5932018-02-05 18:28:38 +0000605 // Set the color of the annotation.
606 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
607 102, 153, 204));
608 // Check that the color has been set correctly.
609 unsigned int R;
610 unsigned int G;
611 unsigned int B;
612 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000613 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000614 &G, &B, &A));
615 EXPECT_EQ(51u, R);
616 EXPECT_EQ(102u, G);
617 EXPECT_EQ(153u, B);
618 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400619
Lei Zhanga21d5932018-02-05 18:28:38 +0000620 // Change the color of the annotation.
621 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
622 153, 102, 51));
623 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000624 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000625 &G, &B, &A));
626 EXPECT_EQ(204u, R);
627 EXPECT_EQ(153u, G);
628 EXPECT_EQ(102u, B);
629 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400630
Lei Zhanga21d5932018-02-05 18:28:38 +0000631 // Set the annotation rectangle.
632 FS_RECTF rect;
633 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
634 EXPECT_EQ(0.f, rect.left);
635 EXPECT_EQ(0.f, rect.right);
636 rect.left = 35;
637 rect.bottom = 150;
638 rect.right = 53;
639 rect.top = 165;
640 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
641 // Check that the annotation rectangle has been set correctly.
642 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
643 EXPECT_EQ(35.f, rect.left);
644 EXPECT_EQ(150.f, rect.bottom);
645 EXPECT_EQ(53.f, rect.right);
646 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400647
Lei Zhanga21d5932018-02-05 18:28:38 +0000648 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000649 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000650 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000651 ASSERT_TRUE(FPDFAnnot_SetStringValue(
652 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000653 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000654 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000655 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000656 ASSERT_EQ(74u, length_bytes);
657 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
658 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
659 pdfium::annotation::kContents,
660 buf.data(), length_bytes));
661 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000662 }
Jane Liu20eafda2017-06-07 10:33:24 -0400663 UnloadPage(page);
664}
665
Lei Zhang03e5e682019-09-16 19:45:55 +0000666// TODO(crbug.com/pdfium/11): Fix this test and enable.
667#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
668#define MAYBE_AddAndSaveUnderlineAnnotation \
669 DISABLED_AddAndSaveUnderlineAnnotation
670#else
671#define MAYBE_AddAndSaveUnderlineAnnotation AddAndSaveUnderlineAnnotation
672#endif
673TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400674 // Open a file with one annotation and load its first page.
675 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000676 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400677 ASSERT_TRUE(page);
678
679 // Check that there is a total of one annotation on its first page, and verify
680 // its quadpoints.
681 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400682 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000683 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000684 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000685 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000686 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000687 EXPECT_EQ(115.802643f, quadpoints.x1);
688 EXPECT_EQ(718.913940f, quadpoints.y1);
689 EXPECT_EQ(157.211182f, quadpoints.x4);
690 EXPECT_EQ(706.264465f, quadpoints.y4);
691 }
Jane Liu20eafda2017-06-07 10:33:24 -0400692
693 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000694 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000695 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000696 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
697 ASSERT_TRUE(annot);
698 quadpoints.x1 = 140.802643f;
699 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000700 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000701 }
Jane Liu20eafda2017-06-07 10:33:24 -0400702
703 // Save the document, closing the page and document.
704 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000705 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400706
707 // Open the saved document.
Lei Zhang4f556b82019-04-08 16:32:41 +0000708 static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400709
Lei Zhang0b494052019-01-31 21:41:15 +0000710 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000711 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000712 VerifySavedRendering(page, 612, 792, kMd5);
Jane Liu20eafda2017-06-07 10:33:24 -0400713
714 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000715 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400716
Lei Zhanga21d5932018-02-05 18:28:38 +0000717 {
718 // Check that the second annotation is an underline annotation and verify
719 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000720 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000721 ASSERT_TRUE(new_annot);
722 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
723 FS_QUADPOINTSF new_quadpoints;
724 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000725 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000726 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
727 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
728 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
729 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
730 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400731
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000732 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400733 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400734}
Jane Liu06462752017-06-27 16:41:14 -0400735
Lei Zhangab41f252018-12-23 03:10:50 +0000736TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000737 // Open a file with four annotations and load its first page.
738 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
739 FPDF_PAGE page = LoadPage(0);
740 ASSERT_TRUE(page);
741 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
742
743 // Retrieve the highlight annotation.
744 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
745 ASSERT_TRUE(annot);
746 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
747
748 FS_QUADPOINTSF quadpoints;
749 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
750
751 {
752 // Verify the current one set of quadpoints.
753 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
754
755 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
756 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
757 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
758 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
759 }
760
761 {
762 // Update the quadpoints.
763 FS_QUADPOINTSF new_quadpoints = quadpoints;
764 new_quadpoints.y1 -= 20.f;
765 new_quadpoints.y2 -= 20.f;
766 new_quadpoints.y3 -= 20.f;
767 new_quadpoints.y4 -= 20.f;
768 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
769
770 // Verify added quadpoint set
771 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
772 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
773 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
774 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
775 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
776 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
777 }
778
779 {
780 // Append a new set of quadpoints.
781 FS_QUADPOINTSF new_quadpoints = quadpoints;
782 new_quadpoints.y1 += 20.f;
783 new_quadpoints.y2 += 20.f;
784 new_quadpoints.y3 += 20.f;
785 new_quadpoints.y4 += 20.f;
786 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
787
788 // Verify added quadpoint set
789 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
790 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
791 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
792 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
793 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
794 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
795 }
796
797 {
798 // Setting and getting quadpoints at out-of-bound index should fail
799 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
800 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
801 }
802
803 FPDFPage_CloseAnnot(annot);
804
805 // Retrieve the square annotation
806 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
807
808 {
809 // Check that attempting to set its quadpoints would fail
810 ASSERT_TRUE(squareAnnot);
811 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
812 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
813 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
814 }
815
816 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000817 UnloadPage(page);
818}
819
Lei Zhang03e5e682019-09-16 19:45:55 +0000820// TODO(crbug.com/pdfium/11): Fix this test and enable.
821#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
822#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
823#else
824#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
825#endif
826TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000827#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +0000828 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
Lei Zhang4f556b82019-04-08 16:32:41 +0000829 static const char kMd5ModifiedHighlight[] =
Lei Zhang0f6b4342020-02-25 20:00:39 +0000830 "e64bf648f6e9354d1f3eedb47a2c9498";
831 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Lei Zhange67bcc72019-04-30 18:55:58 +0000832#elif defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000833 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
834 static const char kMd5ModifiedHighlight[] =
835 "66f3caef3a7d488a4fa1ad37fc06310e";
836 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400837#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000838 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
839 static const char kMd5ModifiedHighlight[] =
840 "66f3caef3a7d488a4fa1ad37fc06310e";
841 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400842#endif
843
Jane Liu06462752017-06-27 16:41:14 -0400844 // Open a file with four annotations and load its first page.
845 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000846 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400847 ASSERT_TRUE(page);
848 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
849
Jane Liub370e5a2017-08-16 13:24:58 -0400850 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000851 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000852 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000853 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000854 }
Jane Liub370e5a2017-08-16 13:24:58 -0400855
Jane Liu0c6b07d2017-08-15 10:50:22 -0400856 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400857 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000858
859 // Retrieve the highlight annotation which has its AP stream already defined.
860 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000861 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000862 ASSERT_TRUE(annot);
863 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
864
865 // Check that color cannot be set when an AP stream is defined already.
866 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
867 102, 153, 204));
868
869 // Verify its attachment points.
870 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000871 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000872 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
873 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
874 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
875 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
876
877 // Check that updating the attachment points would succeed.
878 quadpoints.x1 -= 50.f;
879 quadpoints.x2 -= 50.f;
880 quadpoints.x3 -= 50.f;
881 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000882 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000883 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000884 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000885 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
886 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
887 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
888 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
889
890 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000891 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000892 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000893 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000894 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000895
896 // Verify its annotation rectangle.
897 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
898 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
899 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
900 EXPECT_NEAR(136.325f, rect.right, 0.001f);
901 EXPECT_NEAR(721.292f, rect.top, 0.001f);
902
903 // Check that updating the rectangle would succeed.
904 rect.left -= 60.f;
905 rect.right -= 60.f;
906 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
907 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
908 EXPECT_EQ(rect.right, new_rect.right);
909 }
Jane Liu06462752017-06-27 16:41:14 -0400910
Jane Liub370e5a2017-08-16 13:24:58 -0400911 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000912 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000913 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000914 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000915 }
Jane Liub370e5a2017-08-16 13:24:58 -0400916
Lei Zhanga21d5932018-02-05 18:28:38 +0000917 {
918 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000919 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000920 ASSERT_TRUE(annot);
921 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400922
Lei Zhanga21d5932018-02-05 18:28:38 +0000923 // Check that updating the rectangle would succeed.
924 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
925 rect.left += 70.f;
926 rect.right += 70.f;
927 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
928 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
929 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400930
Lei Zhanga21d5932018-02-05 18:28:38 +0000931 // Check that updating the rectangle changes the square annotation's
932 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000933 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000934 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000935 }
Jane Liub370e5a2017-08-16 13:24:58 -0400936
Jane Liu06462752017-06-27 16:41:14 -0400937 UnloadPage(page);
938}
Jane Liu8ce58f52017-06-29 13:40:22 -0400939
Lei Zhangab41f252018-12-23 03:10:50 +0000940TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000941 // Open a file with multiline markup annotations.
942 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
943 FPDF_PAGE page = LoadPage(0);
944 ASSERT_TRUE(page);
945 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000946 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000947 ASSERT_TRUE(annot);
948
949 // This is a three line annotation.
950 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
951 }
952 UnloadPage(page);
953
954 // null annotation should return 0
955 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
956}
957
Lei Zhangab41f252018-12-23 03:10:50 +0000958TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400959 // Open a file with 3 annotations on its first page.
960 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000961 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400962 ASSERT_TRUE(page);
963 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
964
Jane Liu0c6b07d2017-08-15 10:50:22 -0400965 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400966
Lei Zhanga21d5932018-02-05 18:28:38 +0000967 // Check that the annotations have the expected rectangle coordinates.
968 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000969 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000970 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
971 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
972 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400973
Lei Zhanga21d5932018-02-05 18:28:38 +0000974 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000975 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000976 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
977 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
978 }
979
980 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000981 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000982 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
983 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
984 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400985
986 // Check that nothing happens when attempting to remove an annotation with an
987 // out-of-bound index.
988 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
989 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
990 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
991
992 // Remove the second annotation.
993 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
994 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
995 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
996
997 // Save the document, closing the page and document.
998 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000999 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -04001000
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001001 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -04001002 // Open the saved document.
1003 std::string new_file = GetString();
1004 FPDF_FILEACCESS file_access;
1005 memset(&file_access, 0, sizeof(file_access));
1006 file_access.m_FileLen = new_file.size();
1007 file_access.m_GetBlock = GetBlockFromString;
1008 file_access.m_Param = &new_file;
1009 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
1010 ASSERT_TRUE(new_doc);
1011 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
1012 ASSERT_TRUE(new_page);
1013
1014 // Check that the saved document has 2 annotations on the first page.
1015 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
1016
Lei Zhanga21d5932018-02-05 18:28:38 +00001017 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1018 // by verifying their rectangle coordinates.
1019 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001020 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001021 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1022 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1023 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001024
Lei Zhanga21d5932018-02-05 18:28:38 +00001025 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001026 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001027 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1028 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1029 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001030 FPDF_ClosePage(new_page);
1031 FPDF_CloseDocument(new_doc);
1032}
Jane Liu8ce58f52017-06-29 13:40:22 -04001033
Hui Yingst4a21df02020-05-13 03:15:44 +00001034TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001035#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst4a21df02020-05-13 03:15:44 +00001036#if defined(OS_LINUX)
1037 static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa";
1038 static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622";
1039 static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001040#else
Hui Yingst4a21df02020-05-13 03:15:44 +00001041 static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4";
1042 static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f";
1043 static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006";
1044#endif // defined(OS_LINUX)
1045#else
1046#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001047 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
1048 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
1049 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Hui Yingst4a21df02020-05-13 03:15:44 +00001050#elif defined(OS_MACOSX)
1051 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
1052 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
1053 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Jane Liubaa7ff42017-06-29 19:18:23 -04001054#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001055 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
1056 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
1057 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -04001058#endif
Hui Yingst4a21df02020-05-13 03:15:44 +00001059#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liubaa7ff42017-06-29 19:18:23 -04001060
1061 // Open a file with two annotations and load its first page.
1062 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001063 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001064 ASSERT_TRUE(page);
1065 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1066
1067 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001068 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001069 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001070 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001071 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001072
Lei Zhanga21d5932018-02-05 18:28:38 +00001073 {
1074 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001075 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001076 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001077
Lei Zhanga21d5932018-02-05 18:28:38 +00001078 // Check that this annotation has one path object and retrieve it.
1079 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001080 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001081 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1082 EXPECT_FALSE(path);
1083 path = FPDFAnnot_GetObject(annot.get(), 0);
1084 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1085 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001086
Lei Zhanga21d5932018-02-05 18:28:38 +00001087 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001088 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001089 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001090
Lei Zhanga21d5932018-02-05 18:28:38 +00001091 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001092 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001093 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001094 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001095 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001096
Lei Zhanga21d5932018-02-05 18:28:38 +00001097 // Add a second path object to the same annotation.
1098 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1099 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001100 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1101 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001102 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1103 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1104 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001105
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001106 // The object is in the annontation, not in the page, so the page object
1107 // array should not change.
1108 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1109
Lei Zhanga21d5932018-02-05 18:28:38 +00001110 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001111 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001112 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001113 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001114 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001115
Lei Zhanga21d5932018-02-05 18:28:38 +00001116 // Delete the newly added path object.
1117 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1118 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001119 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001120 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001121
1122 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001123 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001124 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001125 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001126 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001127
Jane Liubaa7ff42017-06-29 19:18:23 -04001128 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001129
Lei Zhanga21d5932018-02-05 18:28:38 +00001130 {
1131 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001132 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001133 ASSERT_TRUE(annot);
1134 rect.left = 200.f;
1135 rect.bottom = 400.f;
1136 rect.right = 500.f;
1137 rect.top = 600.f;
1138 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001139
Lei Zhanga21d5932018-02-05 18:28:38 +00001140 // Add a new path to the annotation.
1141 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1142 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1143 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1144 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1145 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001146 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1147 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001148 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1149 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1150 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1151
1152 // Check that the annotation's bounding box came from its rectangle.
1153 FS_RECTF new_rect;
1154 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1155 EXPECT_EQ(rect.left, new_rect.left);
1156 EXPECT_EQ(rect.bottom, new_rect.bottom);
1157 EXPECT_EQ(rect.right, new_rect.right);
1158 EXPECT_EQ(rect.top, new_rect.top);
1159 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001160
1161 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -04001162 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001163 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001164
1165 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001166 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001167 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001168 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001169
Jane Liu36567742017-07-06 11:13:35 -04001170 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001171 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001172
Lei Zhanga21d5932018-02-05 18:28:38 +00001173 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001174 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001175 ASSERT_TRUE(annot);
1176 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001177
Lei Zhanga21d5932018-02-05 18:28:38 +00001178 // Check that the new annotation's rectangle is as defined.
1179 FS_RECTF new_rect;
1180 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1181 EXPECT_EQ(rect.left, new_rect.left);
1182 EXPECT_EQ(rect.bottom, new_rect.bottom);
1183 EXPECT_EQ(rect.right, new_rect.right);
1184 EXPECT_EQ(rect.top, new_rect.top);
1185 }
1186
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001187 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001188 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001189}
Jane Liub137e752017-07-05 15:04:33 -04001190
Lei Zhangab41f252018-12-23 03:10:50 +00001191TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001192 // Open a file with an annotation and load its first page.
1193 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001194 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001195 ASSERT_TRUE(page);
1196
1197 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001198 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001199 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001200 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1201 }
Jane Liub137e752017-07-05 15:04:33 -04001202
Lei Zhanga21d5932018-02-05 18:28:38 +00001203 {
1204 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001205 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001206 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001207
Lei Zhanga21d5932018-02-05 18:28:38 +00001208 // Check that the original flag values are as expected.
1209 int flags = FPDFAnnot_GetFlags(annot.get());
1210 EXPECT_FALSE(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 // Set the HIDDEN flag.
1214 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1215 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1216 flags = FPDFAnnot_GetFlags(annot.get());
1217 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1218 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001219
Lei Zhanga21d5932018-02-05 18:28:38 +00001220 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001221 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001222 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001223 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001224 }
Jane Liub137e752017-07-05 15:04:33 -04001225
Lei Zhanga21d5932018-02-05 18:28:38 +00001226 // Unset the HIDDEN flag.
1227 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1228 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1229 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1230 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1231 flags = FPDFAnnot_GetFlags(annot.get());
1232 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1233 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001234
Lei Zhanga21d5932018-02-05 18:28:38 +00001235 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001236 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001237 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001238 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1239 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001240 }
Jane Liub137e752017-07-05 15:04:33 -04001241
Jane Liub137e752017-07-05 15:04:33 -04001242 UnloadPage(page);
1243}
Jane Liu36567742017-07-06 11:13:35 -04001244
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001245// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1246#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001247#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1248#else
1249#define MAYBE_AddAndModifyImage AddAndModifyImage
1250#endif
1251TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001252#if defined(_SKIA_SUPPORT_PATHS_)
1253#if defined(OS_LINUX)
1254 static const char kMd5NewImage[] = "26a8eb30937226a677839379e0d7ae1a";
1255 static const char kMd5ModifiedImage[] = "2985114b32ba1a96be78ee643fe31aa5";
1256#else
1257 static const char kMd5NewImage[] = "14012ab500b4671fa73dd760129a8a93";
1258 static const char kMd5ModifiedImage[] = "5f97f98f58ed04dc393f31460485f1a2";
1259#endif // defined(OS_LINUX)
1260#else
Lei Zhange4cdac52019-04-30 16:45:57 +00001261#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001262 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
1263 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +00001264#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001265 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
1266 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -04001267#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001268 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
1269 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -04001270#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001271#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001272
1273 // Open a file with two annotations and load its first page.
1274 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001275 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001276 ASSERT_TRUE(page);
1277 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1278
1279 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001280 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001281 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001282 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001283 }
Jane Liu36567742017-07-06 11:13:35 -04001284
Jane Liu36567742017-07-06 11:13:35 -04001285 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001286 FPDF_BITMAP image_bitmap;
1287
1288 {
1289 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001290 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001291 ASSERT_TRUE(annot);
1292 FS_RECTF rect;
1293 rect.left = 200.f;
1294 rect.bottom = 600.f;
1295 rect.right = 400.f;
1296 rect.top = 800.f;
1297 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1298
1299 // Add a solid-color translucent image object to the new annotation.
1300 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1301 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1302 0xeeeecccc);
1303 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1304 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1305 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1306 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1307 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1308 kBitmapSize, 0, 0));
1309 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1310 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1311 }
Jane Liu36567742017-07-06 11:13:35 -04001312
1313 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001314 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001315 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001316 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001317 }
Jane Liu36567742017-07-06 11:13:35 -04001318
Lei Zhanga21d5932018-02-05 18:28:38 +00001319 {
1320 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001321 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001322 ASSERT_TRUE(annot);
1323 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1324 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1325 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001326
Lei Zhanga21d5932018-02-05 18:28:38 +00001327 // Modify the image in the new annotation.
1328 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1329 0xff000000);
1330 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1331 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1332 }
Jane Liu36567742017-07-06 11:13:35 -04001333
1334 // Save the document, closing the page and document.
1335 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001336 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001337 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001338
1339 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001340 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001341}
1342
Hui Yingst6cd754f2020-05-14 04:05:25 +00001343TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001344#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001345#if defined(OS_LINUX)
1346 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1347 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001348#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001349 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1350 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
1351#endif // defined(OS_LINUX)
1352#else
1353#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001354 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1355 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Hui Yingst6cd754f2020-05-14 04:05:25 +00001356#elif defined(OS_MACOSX)
1357 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1358 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001359#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001360 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1361 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001362#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001363#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001364
1365 // Open a file with two annotations and load its first page.
1366 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001367 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001368 ASSERT_TRUE(page);
1369 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1370
1371 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001372 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001373 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001374 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001375 }
Jane Liu36567742017-07-06 11:13:35 -04001376
Lei Zhanga21d5932018-02-05 18:28:38 +00001377 {
1378 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001379 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001380 ASSERT_TRUE(annot);
1381 FS_RECTF rect;
1382 rect.left = 200.f;
1383 rect.bottom = 550.f;
1384 rect.right = 450.f;
1385 rect.top = 650.f;
1386 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001387
Lei Zhanga21d5932018-02-05 18:28:38 +00001388 // Add a translucent text object to the new annotation.
1389 FPDF_PAGEOBJECT text_object =
1390 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1391 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001392 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001393 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1394 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001395 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001396 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1397 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1398 }
Jane Liu36567742017-07-06 11:13:35 -04001399
1400 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001401 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001402 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001403 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001404 }
Jane Liu36567742017-07-06 11:13:35 -04001405
Lei Zhanga21d5932018-02-05 18:28:38 +00001406 {
1407 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001408 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001409 ASSERT_TRUE(annot);
1410 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1411 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1412 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001413
Lei Zhanga21d5932018-02-05 18:28:38 +00001414 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001415 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001416 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1417 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1418 }
Jane Liu36567742017-07-06 11:13:35 -04001419
1420 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001421 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001422 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001423 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001424 }
Jane Liu36567742017-07-06 11:13:35 -04001425
1426 // Remove the new annotation, and check that the page renders as before.
1427 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001428 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001429 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001430 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001431 }
Jane Liu36567742017-07-06 11:13:35 -04001432
1433 UnloadPage(page);
1434}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001435
Lei Zhang03e5e682019-09-16 19:45:55 +00001436// TODO(crbug.com/pdfium/11): Fix this test and enable.
1437#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1438#define MAYBE_GetSetStringValue DISABLED_GetSetStringValue
1439#else
1440#define MAYBE_GetSetStringValue GetSetStringValue
1441#endif
1442TEST_F(FPDFAnnotEmbedderTest, MAYBE_GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001443 // Open a file with four annotations and load its first page.
1444 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001445 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001446 ASSERT_TRUE(page);
1447
Lei Zhang4f556b82019-04-08 16:32:41 +00001448 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001449
Lei Zhanga21d5932018-02-05 18:28:38 +00001450 {
1451 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001452 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001453 ASSERT_TRUE(annot);
1454
1455 // Check that a non-existent key does not exist.
1456 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1457
1458 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001459 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001460 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001461 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1462 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1463 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001464
1465 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001466 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001467 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001468 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001469 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001470 ASSERT_EQ(66u, length_bytes);
1471 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1472 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1473 length_bytes));
1474 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1475 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001476
1477 // Check that the string value of the modified date is correct.
1478 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001479 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1480 nullptr, 0);
1481 ASSERT_EQ(44u, length_bytes);
1482 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001483 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001484 buf.data(), length_bytes));
1485 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001486
1487 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001488 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001489 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1490 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001491 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001492
1493 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001494 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001495 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001496
Lei Zhange4cdac52019-04-30 16:45:57 +00001497#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001498 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001499#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001500 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001501#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001502 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001503#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001504
1505 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001506 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001507 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001508 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001509 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001510 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001511
Lei Zhanga21d5932018-02-05 18:28:38 +00001512 // Check that the string value of the modified date is the newly-set value.
1513 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001514 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001515 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001516 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001517 ASSERT_EQ(44u, length_bytes);
1518 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001519 EXPECT_EQ(44u,
1520 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001521 buf.data(), length_bytes));
1522 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001523 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001524
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001525 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001526 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001527}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001528
rycsmith3e785602019-03-05 21:48:36 +00001529TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001530 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001531 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1532 FPDF_PAGE page = LoadPage(0);
1533 ASSERT_TRUE(page);
1534 {
1535 // First two annotations do not have "MaxLen" attribute.
1536 for (int i = 0; i < 2; i++) {
1537 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1538 ASSERT_TRUE(annot);
1539
1540 // Verify that no "MaxLen" key present.
1541 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1542
1543 float value;
1544 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1545 }
1546
1547 // Annotation in index 2 has "MaxLen" of 10.
1548 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1549 ASSERT_TRUE(annot);
1550
1551 // Verify that "MaxLen" key present.
1552 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1553
1554 float value;
1555 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1556 EXPECT_FLOAT_EQ(10.0f, value);
1557
1558 // Check bad inputs.
1559 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1560 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1561 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1562 // Ask for key that exists but is not a number.
1563 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1564 }
1565
1566 UnloadPage(page);
1567}
1568
Lei Zhangab41f252018-12-23 03:10:50 +00001569TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001570 // Open a file with four annotations and load its first page.
1571 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001572 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001573 ASSERT_TRUE(page);
1574
Lei Zhanga21d5932018-02-05 18:28:38 +00001575 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001576 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1577 static constexpr size_t kExpectNormalAPLength = 73970;
1578
Lei Zhanga21d5932018-02-05 18:28:38 +00001579 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001580 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001581 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001582
Lei Zhanga21d5932018-02-05 18:28:38 +00001583 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001584 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001585 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001586 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001587
Lei Zhanga21d5932018-02-05 18:28:38 +00001588 // Check that the string value of an AP is not returned if the buffer is too
1589 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001590 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1591 // Write in the buffer to verify it's not overwritten.
1592 memcpy(buf.data(), "abcdefgh", 8);
1593 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 - 1));
1596 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001597
Lei Zhanga21d5932018-02-05 18:28:38 +00001598 // Check that the string value of an AP is returned through a buffer that is
1599 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001600 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001601 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001602 buf.data(), normal_length_bytes));
1603 EXPECT_EQ(kMd5NormalAP,
1604 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1605 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001606
Lei Zhanga21d5932018-02-05 18:28:38 +00001607 // Check that the string value of an AP is returned through a buffer that is
1608 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001609 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1610 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001611 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001612 buf.data(), normal_length_bytes + 2));
1613 EXPECT_EQ(kMd5NormalAP,
1614 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1615 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001616
Lei Zhanga21d5932018-02-05 18:28:38 +00001617 // Check that getting an AP for a mode that does not have an AP returns an
1618 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001619 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001620 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001621 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001622
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001623 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001624 EXPECT_EQ(2u,
1625 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001626 buf.data(), 1000));
1627 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001628
Lei Zhanga21d5932018-02-05 18:28:38 +00001629 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001630 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001631 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001632 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001633 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001634 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001635 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001636
Lei Zhanga21d5932018-02-05 18:28:38 +00001637 // Set the AP correctly now.
1638 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001639 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001640
Lei Zhanga21d5932018-02-05 18:28:38 +00001641 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001642 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001643 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001644 ASSERT_EQ(24u, rollover_length_bytes);
1645 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001646 EXPECT_EQ(24u,
1647 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001648 buf.data(), rollover_length_bytes));
1649 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001650
Lei Zhanga21d5932018-02-05 18:28:38 +00001651 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001652 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1653 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001654 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001655 buf.data(), normal_length_bytes));
1656 EXPECT_EQ(kMd5NormalAP,
1657 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1658 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001659 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001660
1661 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001662 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001663 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001664
Lei Zhang0b494052019-01-31 21:41:15 +00001665 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001666 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001667 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001668 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001669
Lei Zhanga21d5932018-02-05 18:28:38 +00001670 // Check that the new annotation value is equal to the value we set before
1671 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001672 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001673 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001674 ASSERT_EQ(24u, rollover_length_bytes);
1675 std::vector<FPDF_WCHAR> buf =
1676 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001677 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1678 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001679 buf.data(), rollover_length_bytes));
1680 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001681 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001682
1683 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001684 CloseSavedPage(page);
1685 CloseSavedDocument();
1686}
1687
Lei Zhangab41f252018-12-23 03:10:50 +00001688TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001689 // Open a file with four annotations and load its first page.
1690 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001691 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001692 ASSERT_TRUE(page);
1693
Lei Zhanga21d5932018-02-05 18:28:38 +00001694 {
1695 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001696 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001697 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001698
Lei Zhanga21d5932018-02-05 18:28:38 +00001699 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001700 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001701 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001702 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001703 EXPECT_EQ(73970u,
1704 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1705 nullptr, 0));
1706 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1707 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001708
Lei Zhanga21d5932018-02-05 18:28:38 +00001709 // Check that setting the Down AP to null removes the Down entry but keeps
1710 // Normal intact.
1711 EXPECT_TRUE(
1712 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1713 EXPECT_EQ(73970u,
1714 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1715 nullptr, 0));
1716 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1717 nullptr, 0));
1718 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001719
Lei Zhang75c81712018-02-08 17:22:39 +00001720 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001721}
1722
Lei Zhangab41f252018-12-23 03:10:50 +00001723TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001724 // Open a file with four annotations and load its first page.
1725 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001726 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001727 ASSERT_TRUE(page);
1728
Lei Zhanga21d5932018-02-05 18:28:38 +00001729 {
1730 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001731 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001732 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001733
Lei Zhanga21d5932018-02-05 18:28:38 +00001734 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001735 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001736 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001737 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001738 EXPECT_EQ(73970u,
1739 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1740 nullptr, 0));
1741 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1742 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001743
Lei Zhanga21d5932018-02-05 18:28:38 +00001744 // Check that setting the Normal AP to null removes the whole AP dictionary.
1745 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1746 nullptr));
1747 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1748 nullptr, 0));
1749 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1750 nullptr, 0));
1751 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001752
Lei Zhang75c81712018-02-08 17:22:39 +00001753 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001754}
1755
Lei Zhangab41f252018-12-23 03:10:50 +00001756TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001757 // Open a file with annotations and load its first page.
1758 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001759 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001760 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001761 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001762
Lei Zhanga21d5932018-02-05 18:28:38 +00001763 {
1764 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001765 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001766 ASSERT_TRUE(annot);
1767 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1768 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001769 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001770 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1771 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1772 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001773
Lei Zhanga21d5932018-02-05 18:28:38 +00001774 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001775 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001776 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1777 ASSERT_TRUE(popup);
1778 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1779 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1780 FS_RECTF rect;
1781 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1782 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1783 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001784
Lei Zhanga21d5932018-02-05 18:28:38 +00001785 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1786 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001787 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001788 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1789 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001790
Lei Zhanga21d5932018-02-05 18:28:38 +00001791 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1792 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001793 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001794 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001795 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1796 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001797 }
Jane Liu300bb272017-08-21 14:37:53 -04001798
Jane Liu300bb272017-08-21 14:37:53 -04001799 UnloadPage(page);
1800}
1801
Lei Zhangab41f252018-12-23 03:10:50 +00001802TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001803 // Open file with form text fields.
1804 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001805 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001806 ASSERT_TRUE(page);
1807
Lei Zhanga21d5932018-02-05 18:28:38 +00001808 {
1809 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001810 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001811 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001812
Lei Zhanga21d5932018-02-05 18:28:38 +00001813 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001814 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001815 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001816 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001817 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001818
Lei Zhanga21d5932018-02-05 18:28:38 +00001819 {
1820 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001821 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001822 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001823
Lei Zhanga21d5932018-02-05 18:28:38 +00001824 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001825 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001826 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001827 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1828 }
1829
1830 {
1831 // Retrieve the fourth annotation: user-editable password text field.
1832 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1833 ASSERT_TRUE(annot);
1834
1835 // Check that the flag values are as expected.
1836 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1837 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1838 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001839 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001840
1841 UnloadPage(page);
1842}
1843
Lei Zhangab41f252018-12-23 03:10:50 +00001844TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001845 // Open file with form text fields.
1846 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001847 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001848 ASSERT_TRUE(page);
1849
Lei Zhanga21d5932018-02-05 18:28:38 +00001850 {
1851 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001852 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001853 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001854
Lei Zhanga21d5932018-02-05 18:28:38 +00001855 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001856 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001857 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1858 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1859 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1860 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001861
Lei Zhanga21d5932018-02-05 18:28:38 +00001862 {
1863 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001864 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001865 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001866
Lei Zhanga21d5932018-02-05 18:28:38 +00001867 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001868 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001869 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1870 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1871 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1872 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001873
Lei Zhanga21d5932018-02-05 18:28:38 +00001874 {
1875 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001876 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001877 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001878
Lei Zhanga21d5932018-02-05 18:28:38 +00001879 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001880 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001881 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1882 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1883 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1884 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001885
1886 UnloadPage(page);
1887}
Diana Gage40870db2017-07-19 18:16:03 -07001888
Lei Zhangab41f252018-12-23 03:10:50 +00001889TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001890 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001891 ASSERT_TRUE(OpenDocument("text_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001892 FPDF_PAGE page = LoadPage(0);
1893 ASSERT_TRUE(page);
1894
1895 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001896 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1897 EXPECT_FALSE(
1898 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001899
Lei Zhang8da98232019-12-11 23:29:33 +00001900 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001901 {
1902 // Verify there is an annotation.
1903 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001904 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001905 EXPECT_TRUE(annot);
1906 }
1907
1908 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001909 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1910 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1911 EXPECT_FALSE(
1912 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001913
1914 UnloadPage(page);
1915}
1916
Lei Zhangab41f252018-12-23 03:10:50 +00001917TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001918 // Open file with form text fields.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001919 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001920 FPDF_PAGE page = LoadPage(0);
1921 ASSERT_TRUE(page);
1922
Lei Zhanga21d5932018-02-05 18:28:38 +00001923 {
1924 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001925 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001926 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001927 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001928 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001929
Lei Zhanga21d5932018-02-05 18:28:38 +00001930 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001931 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001932 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1933 }
Diana Gage40870db2017-07-19 18:16:03 -07001934
Lei Zhanga21d5932018-02-05 18:28:38 +00001935 {
1936 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001937 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001938 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001939 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001940 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001941
Lei Zhanga21d5932018-02-05 18:28:38 +00001942 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001943 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001944 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1945 }
Diana Gage40870db2017-07-19 18:16:03 -07001946
1947 UnloadPage(page);
1948}
1949
Lei Zhangab41f252018-12-23 03:10:50 +00001950TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001951 // Open file with form comboboxes.
Daniel Hosseinian5af51b62020-07-18 00:53:43 +00001952 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Diana Gage40870db2017-07-19 18:16:03 -07001953 FPDF_PAGE page = LoadPage(0);
1954 ASSERT_TRUE(page);
1955
Lei Zhanga21d5932018-02-05 18:28:38 +00001956 {
1957 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001958 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001959 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001960 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001961 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001962
Lei Zhanga21d5932018-02-05 18:28:38 +00001963 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001964 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001965 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1966 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1967 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1968 }
Diana Gage40870db2017-07-19 18:16:03 -07001969
Lei Zhanga21d5932018-02-05 18:28:38 +00001970 {
1971 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001972 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001973 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001974 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001975 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001976
Lei Zhanga21d5932018-02-05 18:28:38 +00001977 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001978 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001979 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1980 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1981 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1982 }
Diana Gage40870db2017-07-19 18:16:03 -07001983
Lei Zhanga21d5932018-02-05 18:28:38 +00001984 {
1985 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001986 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001987 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001988 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001989 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001990
Lei Zhanga21d5932018-02-05 18:28:38 +00001991 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001992 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001993 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1994 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1995 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1996 }
Diana Gage40870db2017-07-19 18:16:03 -07001997
1998 UnloadPage(page);
1999}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002000
Lei Zhang03e5e682019-09-16 19:45:55 +00002001// TODO(crbug.com/pdfium/11): Fix this test and enable.
2002#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2003#define MAYBE_BUG_1206 DISABLED_BUG_1206
2004#else
2005#define MAYBE_BUG_1206 BUG_1206
2006#endif
2007TEST_F(FPDFAnnotEmbedderTest, MAYBE_BUG_1206) {
Lei Zhang992e7e22019-02-04 19:20:58 +00002008 static constexpr size_t kExpectedSize = 1609;
2009 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
2010
2011 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2012
2013 FPDF_PAGE page = LoadPage(0);
2014 ASSERT_TRUE(page);
2015
2016 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2017 EXPECT_EQ(kExpectedSize, GetString().size());
2018 ClearString();
2019
2020 for (size_t i = 0; i < 10; ++i) {
2021 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2022 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2023
2024 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2025 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2026 // equal, not bigger.
2027 EXPECT_LT(kExpectedSize, GetString().size());
2028 ClearString();
2029 }
2030
2031 UnloadPage(page);
2032}
2033
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002034TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2035 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2036 FPDF_PAGE page = LoadPage(0);
2037 ASSERT_TRUE(page);
2038 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2039
2040 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002041 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002042 static const size_t kBufSize = 12;
2043 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002044
2045 {
2046 // Add a text annotation to the page.
2047 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2048 ASSERT_TRUE(annot);
2049 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2050 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2051
2052 // Make sure there is no test key, add set a value there, and read it back.
2053 std::fill(buf.begin(), buf.end(), 'x');
2054 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002055 kBufSize));
2056 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002057
Lei Zhangf0f67682019-04-08 17:03:21 +00002058 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002059 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2060
2061 std::fill(buf.begin(), buf.end(), 'x');
2062 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002063 kBufSize));
2064 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002065 }
2066
Lei Zhang05ec64c2019-01-09 03:00:06 +00002067 {
2068 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2069 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002070 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002071 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002072 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2073 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2074 // Also do the same test for its appearance string.
2075 std::fill(buf.begin(), buf.end(), 'x');
2076 ASSERT_EQ(2u,
2077 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002078 buf.data(), kBufSize));
2079 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002080
Lei Zhangf0f67682019-04-08 17:03:21 +00002081 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002082 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2083 text.get()));
2084
2085 std::fill(buf.begin(), buf.end(), 'x');
2086 ASSERT_EQ(6u,
2087 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002088 buf.data(), kBufSize));
2089 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002090 }
2091
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002092 UnloadPage(page);
2093
2094 {
2095 // Save a copy, open the copy, and check the annotation again.
2096 // Note that it renders the rotation.
2097 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002098 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002099 FPDF_PAGE saved_page = LoadSavedPage(0);
2100 ASSERT_TRUE(saved_page);
2101
Lei Zhang05ec64c2019-01-09 03:00:06 +00002102 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002103 {
2104 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2105 ASSERT_TRUE(annot);
2106 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2107
2108 std::fill(buf.begin(), buf.end(), 'x');
2109 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002110 kBufSize));
2111 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002112 }
2113
Lei Zhang05ec64c2019-01-09 03:00:06 +00002114 {
2115 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2116 ASSERT_TRUE(annot);
2117 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2118 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2119
2120 std::fill(buf.begin(), buf.end(), 'x');
2121 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002122 kBufSize));
2123 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002124 }
2125
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002126 CloseSavedPage(saved_page);
2127 CloseSavedDocument();
2128 }
2129}
rycsmithcb752f32019-02-21 18:40:53 +00002130
2131TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2132 // Open a file with combobox widget annotations and load its first page.
2133 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2134 FPDF_PAGE page = LoadPage(0);
2135 ASSERT_TRUE(page);
2136
2137 {
2138 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2139 ASSERT_TRUE(annot);
2140
2141 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2142
2143 annot.reset(FPDFPage_GetAnnot(page, 1));
2144 ASSERT_TRUE(annot);
2145
2146 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002147
2148 // Check bad form handle / annot.
2149 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2150 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2151 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002152 }
2153
2154 UnloadPage(page);
2155}
2156
2157TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2158 // Open a file with listbox widget annotations and load its first page.
2159 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2160 FPDF_PAGE page = LoadPage(0);
2161 ASSERT_TRUE(page);
2162
2163 {
2164 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2165 ASSERT_TRUE(annot);
2166
2167 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2168
2169 annot.reset(FPDFPage_GetAnnot(page, 1));
2170 ASSERT_TRUE(annot);
2171
2172 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2173 }
2174
2175 UnloadPage(page);
2176}
2177
2178TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2179 // Open a file with ink annotations and load its first page.
2180 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2181 FPDF_PAGE page = LoadPage(0);
2182 ASSERT_TRUE(page);
2183
2184 {
2185 // annotations do not have "Opt" array and will return -1
2186 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2187 ASSERT_TRUE(annot);
2188
2189 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2190
2191 annot.reset(FPDFPage_GetAnnot(page, 1));
2192 ASSERT_TRUE(annot);
2193
2194 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2195 }
2196
2197 UnloadPage(page);
2198}
2199
2200TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2201 // Open a file with combobox widget annotations and load its first page.
2202 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2203 FPDF_PAGE page = LoadPage(0);
2204 ASSERT_TRUE(page);
2205
2206 {
2207 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2208 ASSERT_TRUE(annot);
2209
2210 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002211 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002212 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002213 ASSERT_EQ(8u, length_bytes);
2214 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002215 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002216 buf.data(), length_bytes));
2217 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002218
2219 annot.reset(FPDFPage_GetAnnot(page, 1));
2220 ASSERT_TRUE(annot);
2221
2222 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002223 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002224 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002225 ASSERT_EQ(12u, length_bytes);
2226 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002227 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002228 buf.data(), length_bytes));
2229 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002230
2231 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002232 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002233 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002234 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002235 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002236 buf.data(), length_bytes));
2237 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002238
Lei Zhange7033c82019-02-26 19:30:49 +00002239 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002240 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2241 nullptr, 0));
2242 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2243 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002244
2245 // Check bad form handle / annot.
2246 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2247 EXPECT_EQ(0u,
2248 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2249 EXPECT_EQ(0u,
2250 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002251 }
2252
2253 UnloadPage(page);
2254}
2255
2256TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2257 // Open a file with listbox widget annotations and load its first page.
2258 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2259 FPDF_PAGE page = LoadPage(0);
2260 ASSERT_TRUE(page);
2261
2262 {
2263 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2264 ASSERT_TRUE(annot);
2265
2266 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002267 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002268 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002269 ASSERT_EQ(8u, length_bytes);
2270 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002271 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002272 buf.data(), length_bytes));
2273 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002274
2275 annot.reset(FPDFPage_GetAnnot(page, 1));
2276 ASSERT_TRUE(annot);
2277
2278 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002279 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002280 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002281 ASSERT_EQ(12u, length_bytes);
2282 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002283 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002284 buf.data(), length_bytes));
2285 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002286
2287 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002288 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002289 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002290 ASSERT_EQ(18u, length_bytes);
2291 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002292 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002293 buf.data(), length_bytes));
2294 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002295
2296 // indices out of range
2297 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2298 nullptr, 0));
2299 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2300 nullptr, 0));
2301 }
2302
2303 UnloadPage(page);
2304}
2305
2306TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2307 // Open a file with ink annotations and load its first page.
2308 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2309 FPDF_PAGE page = LoadPage(0);
2310 ASSERT_TRUE(page);
2311
2312 {
2313 // annotations do not have "Opt" array and will return 0
2314 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2315 ASSERT_TRUE(annot);
2316
2317 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2318 nullptr, 0));
2319
2320 annot.reset(FPDFPage_GetAnnot(page, 1));
2321 ASSERT_TRUE(annot);
2322
2323 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2324 nullptr, 0));
2325 }
2326
2327 UnloadPage(page);
2328}
Ryan Smith09c23b12019-04-25 18:09:06 +00002329
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002330TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2331 // Open a file with combobox widget annotations and load its first page.
2332 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2333 FPDF_PAGE page = LoadPage(0);
2334 ASSERT_TRUE(page);
2335
2336 {
2337 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2338 ASSERT_TRUE(annot);
2339
2340 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2341 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2342 ASSERT_EQ(3, count);
2343 for (int i = 0; i < count; i++) {
2344 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2345 }
2346
2347 annot.reset(FPDFPage_GetAnnot(page, 1));
2348 ASSERT_TRUE(annot);
2349
2350 // Checks for Combobox with Values (/V) object which is just a string.
2351 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2352 ASSERT_EQ(26, count);
2353 for (int i = 0; i < count; i++) {
2354 EXPECT_EQ(i == 1,
2355 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2356 }
2357
2358 // Checks for index outside bound i.e. (index >= CountOption()).
2359 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2360 /*index=*/26));
2361 // Checks for negetive index.
2362 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2363 /*index=*/-1));
2364
2365 // Checks for bad form handle/annot.
2366 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2367 EXPECT_FALSE(
2368 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2369 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2370 }
2371
2372 UnloadPage(page);
2373}
2374
2375TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2376 // Open a file with listbox widget annotations and load its first page.
2377 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2378 FPDF_PAGE page = LoadPage(0);
2379 ASSERT_TRUE(page);
2380
2381 {
2382 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2383 ASSERT_TRUE(annot);
2384
2385 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2386 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2387 ASSERT_EQ(3, count);
2388 for (int i = 0; i < count; i++) {
2389 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2390 }
2391
2392 annot.reset(FPDFPage_GetAnnot(page, 1));
2393 ASSERT_TRUE(annot);
2394
2395 // Checks for Listbox with Values (/V) object which is just a string.
2396 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2397 ASSERT_EQ(26, count);
2398 for (int i = 0; i < count; i++) {
2399 EXPECT_EQ(i == 1,
2400 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2401 }
2402
2403 annot.reset(FPDFPage_GetAnnot(page, 3));
2404 ASSERT_TRUE(annot);
2405
2406 // Checks for Listbox with only Selected indices (/I) object which is an
2407 // array with multiple objects.
2408 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2409 ASSERT_EQ(5, count);
2410 for (int i = 0; i < count; i++) {
2411 bool expected = (i == 1 || i == 3);
2412 EXPECT_EQ(expected,
2413 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2414 }
2415
2416 annot.reset(FPDFPage_GetAnnot(page, 4));
2417 ASSERT_TRUE(annot);
2418
2419 // Checks for Listbox with Values (/V) object which is an array with
2420 // multiple objects.
2421 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2422 ASSERT_EQ(5, count);
2423 for (int i = 0; i < count; i++) {
2424 bool expected = (i == 2 || i == 4);
2425 EXPECT_EQ(expected,
2426 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2427 }
2428
2429 annot.reset(FPDFPage_GetAnnot(page, 5));
2430 ASSERT_TRUE(annot);
2431
2432 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2433 // objects conflict with different lengths.
2434 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2435 ASSERT_EQ(5, count);
2436 for (int i = 0; i < count; i++) {
2437 bool expected = (i == 0 || i == 2);
2438 EXPECT_EQ(expected,
2439 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2440 }
2441 }
2442
2443 UnloadPage(page);
2444}
2445
2446TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2447 // Open a file with multiple form field annotations and load its first page.
2448 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2449 FPDF_PAGE page = LoadPage(0);
2450 ASSERT_TRUE(page);
2451
2452 {
2453 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2454 ASSERT_TRUE(annot);
2455
2456 // Checks for link annotation.
2457 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2458 /*index=*/0));
2459
2460 annot.reset(FPDFPage_GetAnnot(page, 3));
2461 ASSERT_TRUE(annot);
2462
2463 // Checks for text field annotation.
2464 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2465 /*index=*/0));
2466 }
2467
2468 UnloadPage(page);
2469}
2470
Ryan Smith09c23b12019-04-25 18:09:06 +00002471TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2472 // Open a file with combobox annotations and load its first page.
2473 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2474 FPDF_PAGE page = LoadPage(0);
2475 ASSERT_TRUE(page);
2476
2477 {
2478 // All 3 widgets have Tf font size 12.
2479 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2480 ASSERT_TRUE(annot);
2481
2482 float value;
2483 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2484 EXPECT_EQ(12.0, value);
2485
2486 annot.reset(FPDFPage_GetAnnot(page, 1));
2487 ASSERT_TRUE(annot);
2488
2489 float value_two;
2490 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2491 EXPECT_EQ(12.0, value_two);
2492
2493 annot.reset(FPDFPage_GetAnnot(page, 2));
2494 ASSERT_TRUE(annot);
2495
2496 float value_three;
2497 ASSERT_TRUE(
2498 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2499 EXPECT_EQ(12.0, value_three);
2500 }
2501
2502 UnloadPage(page);
2503}
2504
2505TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2506 // Open a file with textfield annotations and load its first page.
2507 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2508 FPDF_PAGE page = LoadPage(0);
2509 ASSERT_TRUE(page);
2510
2511 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002512 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002513 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2514 ASSERT_TRUE(annot);
2515
2516 float value;
2517 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2518 EXPECT_EQ(12.0, value);
2519
2520 annot.reset(FPDFPage_GetAnnot(page, 1));
2521 ASSERT_TRUE(annot);
2522
2523 float value_two;
2524 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2525 EXPECT_EQ(12.0, value_two);
2526
2527 annot.reset(FPDFPage_GetAnnot(page, 2));
2528 ASSERT_TRUE(annot);
2529
2530 float value_three;
2531 ASSERT_TRUE(
2532 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2533 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002534
2535 float value_four;
2536 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2537 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002538 }
2539
2540 UnloadPage(page);
2541}
2542
2543TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2544 // Open a file with ink annotations and load its first page.
2545 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2546 FPDF_PAGE page = LoadPage(0);
2547 ASSERT_TRUE(page);
2548
2549 {
2550 // Annotations that do not have variable text and will return -1.
2551 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2552 ASSERT_TRUE(annot);
2553
2554 float value;
2555 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2556
2557 annot.reset(FPDFPage_GetAnnot(page, 1));
2558 ASSERT_TRUE(annot);
2559
2560 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2561 }
2562
2563 UnloadPage(page);
2564}
2565
2566TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2567 // Open a file with combobox annotations and load its first page.
2568 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2569 FPDF_PAGE page = LoadPage(0);
2570 ASSERT_TRUE(page);
2571
2572 {
2573 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2574 ASSERT_TRUE(annot);
2575
2576 // Check bad form handle / annot.
2577 float value;
2578 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2579 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2580 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2581 }
2582
2583 UnloadPage(page);
2584}
2585
2586TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2587 // Open a file with textfield annotations and load its first page.
2588 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2589 FPDF_PAGE page = LoadPage(0);
2590 ASSERT_TRUE(page);
2591
2592 {
2593 // Obtain the first annotation, a text field with negative font size, -12.
2594 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2595 ASSERT_TRUE(annot);
2596
2597 float value;
2598 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2599 EXPECT_EQ(-12.0, value);
2600 }
2601
2602 UnloadPage(page);
2603}
Ryan Smith23fdf892019-07-17 21:51:26 +00002604
2605TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2606 // Open a file with checkbox and radiobuttons widget annotations and load its
2607 // first page.
2608 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2609 FPDF_PAGE page = LoadPage(0);
2610 ASSERT_TRUE(page);
2611
2612 {
2613 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2614 ASSERT_TRUE(annot);
2615 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2616 }
2617
2618 UnloadPage(page);
2619}
2620
2621TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2622 // Open a file with checkbox and radiobutton widget annotations and load its
2623 // first page.
2624 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2625 FPDF_PAGE page = LoadPage(0);
2626 ASSERT_TRUE(page);
2627
2628 {
2629 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2630 ASSERT_TRUE(annot);
2631 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2632 }
2633
2634 UnloadPage(page);
2635}
2636
2637TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2638 // Open a file with checkbox and radiobutton widget annotations and load its
2639 // first page.
2640 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2641 FPDF_PAGE page = LoadPage(0);
2642 ASSERT_TRUE(page);
2643
2644 {
2645 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2646 ASSERT_TRUE(annot);
2647 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2648
2649 annot.reset(FPDFPage_GetAnnot(page, 6));
2650 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2651
2652 annot.reset(FPDFPage_GetAnnot(page, 7));
2653 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2654 }
2655
2656 UnloadPage(page);
2657}
2658
2659TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2660 // Open a file with checkbox and radiobutton widget annotations and load its
2661 // first page.
2662 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2663 FPDF_PAGE page = LoadPage(0);
2664 ASSERT_TRUE(page);
2665
2666 {
2667 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2668 ASSERT_TRUE(annot);
2669 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2670
2671 annot.reset(FPDFPage_GetAnnot(page, 3));
2672 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2673
2674 annot.reset(FPDFPage_GetAnnot(page, 4));
2675 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2676 }
2677
2678 UnloadPage(page);
2679}
2680
2681TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2682 // Open a file with checkbox and radiobuttons widget annotations and load its
2683 // first page.
2684 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2685 FPDF_PAGE page = LoadPage(0);
2686 ASSERT_TRUE(page);
2687
2688 {
2689 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2690 ASSERT_TRUE(annot);
2691 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2692 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2693 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2694 }
2695
2696 UnloadPage(page);
2697}
2698
2699TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2700 // Open a file with text widget annotations and load its first page.
2701 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2702 FPDF_PAGE page = LoadPage(0);
2703 ASSERT_TRUE(page);
2704
2705 {
2706 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2707 ASSERT_TRUE(annot);
2708 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2709 }
2710
2711 UnloadPage(page);
2712}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002713
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002714TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2715 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002716 FPDF_PAGE page = LoadPage(0);
2717 ASSERT_TRUE(page);
2718
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002719 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2720
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002721 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002722 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002723 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002724 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002725 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002726
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002727 constexpr int kExpectedAnnotTypes[] = {-1,
2728 FPDF_FORMFIELD_COMBOBOX,
2729 FPDF_FORMFIELD_LISTBOX,
2730 FPDF_FORMFIELD_TEXTFIELD,
2731 FPDF_FORMFIELD_CHECKBOX,
2732 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002733
Lei Zhang4501a502020-05-18 16:52:59 +00002734 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002735 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002736 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002737 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002738 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2739 }
2740 UnloadPage(page);
2741}
2742
2743TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2744 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2745 FPDF_PAGE page = LoadPage(0);
2746 ASSERT_TRUE(page);
2747
2748 {
2749 EXPECT_EQ(0u,
2750 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2751
2752 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2753 ASSERT_TRUE(annot);
2754
2755 EXPECT_EQ(0u,
2756 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2757
2758 unsigned long length_bytes =
2759 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2760 ASSERT_EQ(2u, length_bytes);
2761 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2762 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2763 buf.data(), length_bytes));
2764 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2765 }
2766 {
2767 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2768 ASSERT_TRUE(annot);
2769
2770 unsigned long length_bytes =
2771 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2772 ASSERT_EQ(18u, length_bytes);
2773 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2774 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2775 buf.data(), length_bytes));
2776 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2777 }
2778 UnloadPage(page);
2779}
2780
2781TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2782 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2783 FPDF_PAGE page = LoadPage(0);
2784 ASSERT_TRUE(page);
2785
2786 {
2787 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2788 ASSERT_TRUE(annot);
2789
2790 unsigned long length_bytes =
2791 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2792 ASSERT_EQ(2u, length_bytes);
2793 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2794 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2795 buf.data(), length_bytes));
2796 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2797 }
2798 {
2799 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2800 ASSERT_TRUE(annot);
2801
2802 unsigned long length_bytes =
2803 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2804 ASSERT_EQ(14u, length_bytes);
2805 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2806 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2807 buf.data(), length_bytes));
2808 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2809 }
2810 UnloadPage(page);
2811}
2812
2813TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2814 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2815 FPDF_PAGE page = LoadPage(0);
2816 ASSERT_TRUE(page);
2817
2818 {
2819 EXPECT_EQ(0u,
2820 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2821
2822 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2823 ASSERT_TRUE(annot);
2824
2825 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2826
2827 unsigned long length_bytes =
2828 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2829 ASSERT_EQ(18u, length_bytes);
2830 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2831 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2832 buf.data(), length_bytes));
2833 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2834 }
2835 UnloadPage(page);
2836}
2837
2838TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2839 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2840 FPDF_PAGE page = LoadPage(0);
2841 ASSERT_TRUE(page);
2842
2843 {
2844 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2845 ASSERT_TRUE(annot);
2846
2847 unsigned long length_bytes =
2848 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2849 ASSERT_EQ(30u, length_bytes);
2850 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2851 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2852 buf.data(), length_bytes));
2853 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2854 }
2855 UnloadPage(page);
2856}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002857
Lei Zhang9b444002020-04-17 17:35:23 +00002858TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2859 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002860 FPDF_PAGE page = LoadPage(0);
2861 ASSERT_TRUE(page);
2862
Lei Zhang9b444002020-04-17 17:35:23 +00002863 // Verify widgets are by default focusable.
2864 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2865 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002866
Lei Zhang9b444002020-04-17 17:35:23 +00002867 // Expected annot subtypes for page 0 of annots.pdf.
2868 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2869 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2870 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2871 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2872 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002873
Lei Zhang9b444002020-04-17 17:35:23 +00002874 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002875 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002876 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2877 kExpectedAnnotSubtypes,
2878 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002879
Lei Zhang9b444002020-04-17 17:35:23 +00002880 // Make no annotation type focusable using the preferred method.
2881 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 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 // Restore the focusable type count back to 1, then set it back to 0 using a
2885 // different method.
2886 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2887 ASSERT_TRUE(
2888 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2889 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002890
Lei Zhang9b444002020-04-17 17:35:23 +00002891 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002892 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002893
Lei Zhang9b444002020-04-17 17:35:23 +00002894 // Now make links focusable.
2895 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2896 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002897
Lei Zhang9b444002020-04-17 17:35:23 +00002898 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002899 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002900 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2901 kExpectedAnnotSubtypes,
2902 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002903
Lei Zhang9b444002020-04-17 17:35:23 +00002904 // Test invalid parameters.
2905 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00002906 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002907 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00002908 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002909 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2910
Lei Zhang9b444002020-04-17 17:35:23 +00002911 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2912 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2913 subtypes.size()));
2914 EXPECT_FALSE(
2915 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2916 EXPECT_FALSE(
2917 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002918
2919 UnloadPage(page);
2920}
Lei Zhang671aece2020-04-14 19:02:26 +00002921
Hui Yingst609f7d62020-04-23 23:14:13 +00002922// TODO(crbug.com/pdfium/11): Fix this test and enable.
2923#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2924#define MAYBE_FocusableAnnotRendering DISABLED_FocusableAnnotRendering
2925#else
2926#define MAYBE_FocusableAnnotRendering FocusableAnnotRendering
2927#endif
2928TEST_F(FPDFAnnotEmbedderTest, MAYBE_FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00002929 ASSERT_TRUE(OpenDocument("annots.pdf"));
2930 FPDF_PAGE page = LoadPage(0);
2931 ASSERT_TRUE(page);
2932
2933 {
2934#if defined(OS_WIN)
2935 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
2936#elif defined(OS_MACOSX)
2937 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
2938#else
2939 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
2940#endif
2941 // Check the initial rendering.
2942 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2943 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2944 }
2945
2946 // Make links and highlights focusable.
2947 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
2948 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00002949 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00002950 ASSERT_TRUE(
2951 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
2952 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
2953 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
2954 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
2955 subtypes.size()));
2956 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
2957 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
2958
2959 {
2960#if defined(OS_WIN)
2961 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
2962#elif defined(OS_MACOSX)
2963 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
2964#else
2965 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
2966#endif
2967 // Focus the first link and check the rendering.
2968 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2969 ASSERT_TRUE(annot);
2970 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2971 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2972 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2973 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2974 }
2975
2976 {
2977#if defined(OS_WIN)
2978 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
2979#elif defined(OS_MACOSX)
2980 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
2981#else
2982 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
2983#endif
2984 // Focus the first highlight and check the rendering.
2985 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2986 ASSERT_TRUE(annot);
2987 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
2988 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2989 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2990 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2991 }
2992
2993 UnloadPage(page);
2994}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00002995
2996TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
2997 ASSERT_TRUE(OpenDocument("annots.pdf"));
2998 FPDF_PAGE page = LoadPage(0);
2999 ASSERT_TRUE(page);
3000 {
3001 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3002 ASSERT_TRUE(annot);
3003 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3004 FPDF_LINK link_annot = FPDFAnnot_GetLink(annot.get());
3005 ASSERT_TRUE(link_annot);
3006
3007 FPDF_ACTION action = FPDFLink_GetAction(link_annot);
3008 ASSERT_TRUE(action);
3009 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
3010 FPDFAction_GetType(action));
3011
3012 constexpr char kExpectedResult[] =
3013 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3014 "fpdf_text.h";
Lei Zhang4501a502020-05-18 16:52:59 +00003015 constexpr unsigned long kExpectedLength = pdfium::size(kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003016 unsigned long bufsize =
3017 FPDFAction_GetURIPath(document(), action, nullptr, 0);
3018 ASSERT_EQ(kExpectedLength, bufsize);
3019
3020 char buffer[1024];
3021 EXPECT_EQ(bufsize,
3022 FPDFAction_GetURIPath(document(), action, buffer, bufsize));
3023 EXPECT_STREQ(kExpectedResult, buffer);
3024 }
3025
3026 {
3027 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3028 ASSERT_TRUE(annot);
3029 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3030 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3031 }
3032
3033 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3034
3035 UnloadPage(page);
3036}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003037
3038TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3039 // Open a file with radio button widget annotations and load its first page.
3040 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3041 FPDF_PAGE page = LoadPage(0);
3042 ASSERT_TRUE(page);
3043
3044 {
3045 // Checks for bad annot.
3046 EXPECT_EQ(-1,
3047 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3048
3049 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3050 ASSERT_TRUE(annot);
3051
3052 // Checks for bad form handle.
3053 EXPECT_EQ(-1,
3054 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3055
3056 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3057 }
3058
3059 UnloadPage(page);
3060}
3061
3062TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3063 // Open a file with checkbox widget annotations and load its first page.
3064 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3065 FPDF_PAGE page = LoadPage(0);
3066 ASSERT_TRUE(page);
3067
3068 {
3069 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3070 ASSERT_TRUE(annot);
3071 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3072 }
3073
3074 UnloadPage(page);
3075}
3076
3077TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3078 // Open a file with ink annotations and load its first page.
3079 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3080 FPDF_PAGE page = LoadPage(0);
3081 ASSERT_TRUE(page);
3082
3083 {
3084 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3085 ASSERT_TRUE(annot);
3086 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3087 }
3088
3089 UnloadPage(page);
3090}
3091
3092TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3093 // Open a file with radio button widget annotations and load its first page.
3094 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3095 FPDF_PAGE page = LoadPage(0);
3096 ASSERT_TRUE(page);
3097
3098 {
3099 // Checks for bad annot.
3100 EXPECT_EQ(-1,
3101 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3102
3103 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3104 ASSERT_TRUE(annot);
3105
3106 // Checks for bad form handle.
3107 EXPECT_EQ(-1,
3108 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3109
3110 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3111 }
3112
3113 UnloadPage(page);
3114}
3115
3116TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3117 // Open a file with checkbox widget annotations and load its first page.
3118 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3119 FPDF_PAGE page = LoadPage(0);
3120 ASSERT_TRUE(page);
3121
3122 {
3123 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3124 ASSERT_TRUE(annot);
3125 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3126 }
3127
3128 UnloadPage(page);
3129}
3130
3131TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3132 // Open a file with ink annotations and load its first page.
3133 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3134 FPDF_PAGE page = LoadPage(0);
3135 ASSERT_TRUE(page);
3136
3137 {
3138 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3139 ASSERT_TRUE(annot);
3140 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3141 }
3142
3143 UnloadPage(page);
3144}
3145
3146TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3147 // Open a file with radio button widget annotations and load its first page.
3148 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3149 FPDF_PAGE page = LoadPage(0);
3150 ASSERT_TRUE(page);
3151
3152 {
3153 // Checks for bad annot.
3154 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3155 form_handle(), /*annot=*/nullptr,
3156 /*buffer=*/nullptr, /*buflen=*/0));
3157
3158 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3159 ASSERT_TRUE(annot);
3160
3161 // Checks for bad form handle.
3162 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3163 /*hHandle=*/nullptr, annot.get(),
3164 /*buffer=*/nullptr, /*buflen=*/0));
3165
3166 unsigned long length_bytes =
3167 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3168 /*buffer=*/nullptr, /*buflen=*/0);
3169 ASSERT_EQ(14u, length_bytes);
3170 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3171 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3172 buf.data(), length_bytes));
3173 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3174 }
3175
3176 UnloadPage(page);
3177}
3178
3179TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3180 // Open a file with checkbox widget annotations and load its first page.
3181 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3182 FPDF_PAGE page = LoadPage(0);
3183 ASSERT_TRUE(page);
3184
3185 {
3186 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3187 ASSERT_TRUE(annot);
3188
3189 unsigned long length_bytes =
3190 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3191 /*buffer=*/nullptr, /*buflen=*/0);
3192 ASSERT_EQ(8u, length_bytes);
3193 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3194 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3195 buf.data(), length_bytes));
3196 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3197 }
3198
3199 UnloadPage(page);
3200}
3201
3202TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3203 // Open a file with ink annotations and load its first page.
3204 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3205 FPDF_PAGE page = LoadPage(0);
3206 ASSERT_TRUE(page);
3207
3208 {
3209 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3210 ASSERT_TRUE(annot);
3211 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3212 /*buffer=*/nullptr,
3213 /*buflen=*/0));
3214 }
3215
3216 UnloadPage(page);
3217}