blob: 4dfee27c097d527c8939bbc698b627046b1f7843 [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 {
558 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
559 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
560 }
Tom Sepez507d0192018-11-07 16:37:51 +0000561 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400562}
Jane Liu20eafda2017-06-07 10:33:24 -0400563
Lei Zhangab41f252018-12-23 03:10:50 +0000564TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400565 // Open a file with one annotation and load its first page.
566 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000567 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400568 ASSERT_TRUE(page);
569
570 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400571 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400572
573 UnloadPage(page);
574}
575
Lei Zhangab41f252018-12-23 03:10:50 +0000576TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400577 // Open a file with no annotation and load its first page.
578 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000579 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400580 ASSERT_TRUE(page);
581 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
582
Lei Zhanga21d5932018-02-05 18:28:38 +0000583 {
584 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000585 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000586 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400587
Lei Zhanga21d5932018-02-05 18:28:38 +0000588 // Check that there is now 1 annotations on this page.
589 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400590
Lei Zhanga21d5932018-02-05 18:28:38 +0000591 // Check that the subtype of the annotation is correct.
592 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
593 }
Jane Liue10509a2017-06-20 16:47:41 -0400594
Lei Zhanga21d5932018-02-05 18:28:38 +0000595 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000596 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000597 ASSERT_TRUE(annot);
598 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400599
Lei Zhanga21d5932018-02-05 18:28:38 +0000600 // Set the color of the annotation.
601 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
602 102, 153, 204));
603 // Check that the color has been set correctly.
604 unsigned int R;
605 unsigned int G;
606 unsigned int B;
607 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000608 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000609 &G, &B, &A));
610 EXPECT_EQ(51u, R);
611 EXPECT_EQ(102u, G);
612 EXPECT_EQ(153u, B);
613 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400614
Lei Zhanga21d5932018-02-05 18:28:38 +0000615 // Change the color of the annotation.
616 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
617 153, 102, 51));
618 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000619 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000620 &G, &B, &A));
621 EXPECT_EQ(204u, R);
622 EXPECT_EQ(153u, G);
623 EXPECT_EQ(102u, B);
624 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400625
Lei Zhanga21d5932018-02-05 18:28:38 +0000626 // Set the annotation rectangle.
627 FS_RECTF rect;
628 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
629 EXPECT_EQ(0.f, rect.left);
630 EXPECT_EQ(0.f, rect.right);
631 rect.left = 35;
632 rect.bottom = 150;
633 rect.right = 53;
634 rect.top = 165;
635 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
636 // Check that the annotation rectangle has been set correctly.
637 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
638 EXPECT_EQ(35.f, rect.left);
639 EXPECT_EQ(150.f, rect.bottom);
640 EXPECT_EQ(53.f, rect.right);
641 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400642
Lei Zhanga21d5932018-02-05 18:28:38 +0000643 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000644 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000645 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000646 ASSERT_TRUE(FPDFAnnot_SetStringValue(
647 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000648 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000649 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000650 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000651 ASSERT_EQ(74u, length_bytes);
652 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
653 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
654 pdfium::annotation::kContents,
655 buf.data(), length_bytes));
656 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000657 }
Jane Liu20eafda2017-06-07 10:33:24 -0400658 UnloadPage(page);
659}
660
Lei Zhang03e5e682019-09-16 19:45:55 +0000661// TODO(crbug.com/pdfium/11): Fix this test and enable.
662#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
663#define MAYBE_AddAndSaveUnderlineAnnotation \
664 DISABLED_AddAndSaveUnderlineAnnotation
665#else
666#define MAYBE_AddAndSaveUnderlineAnnotation AddAndSaveUnderlineAnnotation
667#endif
668TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400669 // Open a file with one annotation and load its first page.
670 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000671 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400672 ASSERT_TRUE(page);
673
674 // Check that there is a total of one annotation on its first page, and verify
675 // its quadpoints.
676 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400677 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000678 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000679 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000680 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000681 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000682 EXPECT_EQ(115.802643f, quadpoints.x1);
683 EXPECT_EQ(718.913940f, quadpoints.y1);
684 EXPECT_EQ(157.211182f, quadpoints.x4);
685 EXPECT_EQ(706.264465f, quadpoints.y4);
686 }
Jane Liu20eafda2017-06-07 10:33:24 -0400687
688 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000689 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000690 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000691 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
692 ASSERT_TRUE(annot);
693 quadpoints.x1 = 140.802643f;
694 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000695 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000696 }
Jane Liu20eafda2017-06-07 10:33:24 -0400697
698 // Save the document, closing the page and document.
699 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000700 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400701
702 // Open the saved document.
Lei Zhang4f556b82019-04-08 16:32:41 +0000703 static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400704
Lei Zhang0b494052019-01-31 21:41:15 +0000705 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000706 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000707 VerifySavedRendering(page, 612, 792, kMd5);
Jane Liu20eafda2017-06-07 10:33:24 -0400708
709 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000710 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400711
Lei Zhanga21d5932018-02-05 18:28:38 +0000712 {
713 // Check that the second annotation is an underline annotation and verify
714 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000715 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000716 ASSERT_TRUE(new_annot);
717 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
718 FS_QUADPOINTSF new_quadpoints;
719 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000720 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000721 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
722 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
723 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
724 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
725 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400726
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000727 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400728 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400729}
Jane Liu06462752017-06-27 16:41:14 -0400730
Lei Zhangab41f252018-12-23 03:10:50 +0000731TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000732 // Open a file with four annotations and load its first page.
733 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
734 FPDF_PAGE page = LoadPage(0);
735 ASSERT_TRUE(page);
736 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
737
738 // Retrieve the highlight annotation.
739 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
740 ASSERT_TRUE(annot);
741 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
742
743 FS_QUADPOINTSF quadpoints;
744 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
745
746 {
747 // Verify the current one set of quadpoints.
748 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
749
750 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
751 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
752 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
753 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
754 }
755
756 {
757 // Update the quadpoints.
758 FS_QUADPOINTSF new_quadpoints = quadpoints;
759 new_quadpoints.y1 -= 20.f;
760 new_quadpoints.y2 -= 20.f;
761 new_quadpoints.y3 -= 20.f;
762 new_quadpoints.y4 -= 20.f;
763 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
764
765 // Verify added quadpoint set
766 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
767 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
768 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
769 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
770 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
771 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
772 }
773
774 {
775 // Append a new set of quadpoints.
776 FS_QUADPOINTSF new_quadpoints = quadpoints;
777 new_quadpoints.y1 += 20.f;
778 new_quadpoints.y2 += 20.f;
779 new_quadpoints.y3 += 20.f;
780 new_quadpoints.y4 += 20.f;
781 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
782
783 // Verify added quadpoint set
784 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
785 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
786 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
787 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
788 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
789 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
790 }
791
792 {
793 // Setting and getting quadpoints at out-of-bound index should fail
794 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
795 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
796 }
797
798 FPDFPage_CloseAnnot(annot);
799
800 // Retrieve the square annotation
801 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
802
803 {
804 // Check that attempting to set its quadpoints would fail
805 ASSERT_TRUE(squareAnnot);
806 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
807 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
808 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
809 }
810
811 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000812 UnloadPage(page);
813}
814
Lei Zhang03e5e682019-09-16 19:45:55 +0000815// TODO(crbug.com/pdfium/11): Fix this test and enable.
816#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
817#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
818#else
819#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
820#endif
821TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000822#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +0000823 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
Lei Zhang4f556b82019-04-08 16:32:41 +0000824 static const char kMd5ModifiedHighlight[] =
Lei Zhang0f6b4342020-02-25 20:00:39 +0000825 "e64bf648f6e9354d1f3eedb47a2c9498";
826 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Lei Zhange67bcc72019-04-30 18:55:58 +0000827#elif defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000828 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
829 static const char kMd5ModifiedHighlight[] =
830 "66f3caef3a7d488a4fa1ad37fc06310e";
831 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400832#else
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#endif
838
Jane Liu06462752017-06-27 16:41:14 -0400839 // Open a file with four annotations and load its first page.
840 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000841 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400842 ASSERT_TRUE(page);
843 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
844
Jane Liub370e5a2017-08-16 13:24:58 -0400845 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000846 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000847 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000848 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000849 }
Jane Liub370e5a2017-08-16 13:24:58 -0400850
Jane Liu0c6b07d2017-08-15 10:50:22 -0400851 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400852 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000853
854 // Retrieve the highlight annotation which has its AP stream already defined.
855 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000856 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000857 ASSERT_TRUE(annot);
858 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
859
860 // Check that color cannot be set when an AP stream is defined already.
861 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
862 102, 153, 204));
863
864 // Verify its attachment points.
865 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000866 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000867 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
868 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
869 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
870 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
871
872 // Check that updating the attachment points would succeed.
873 quadpoints.x1 -= 50.f;
874 quadpoints.x2 -= 50.f;
875 quadpoints.x3 -= 50.f;
876 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000877 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000878 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000879 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000880 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
881 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
882 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
883 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
884
885 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000886 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000887 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000888 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000889 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000890
891 // Verify its annotation rectangle.
892 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
893 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
894 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
895 EXPECT_NEAR(136.325f, rect.right, 0.001f);
896 EXPECT_NEAR(721.292f, rect.top, 0.001f);
897
898 // Check that updating the rectangle would succeed.
899 rect.left -= 60.f;
900 rect.right -= 60.f;
901 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
902 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
903 EXPECT_EQ(rect.right, new_rect.right);
904 }
Jane Liu06462752017-06-27 16:41:14 -0400905
Jane Liub370e5a2017-08-16 13:24:58 -0400906 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000907 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000908 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000909 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000910 }
Jane Liub370e5a2017-08-16 13:24:58 -0400911
Lei Zhanga21d5932018-02-05 18:28:38 +0000912 {
913 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000914 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000915 ASSERT_TRUE(annot);
916 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400917
Lei Zhanga21d5932018-02-05 18:28:38 +0000918 // Check that updating the rectangle would succeed.
919 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
920 rect.left += 70.f;
921 rect.right += 70.f;
922 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
923 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
924 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400925
Lei Zhanga21d5932018-02-05 18:28:38 +0000926 // Check that updating the rectangle changes the square annotation's
927 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000928 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000929 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000930 }
Jane Liub370e5a2017-08-16 13:24:58 -0400931
Jane Liu06462752017-06-27 16:41:14 -0400932 UnloadPage(page);
933}
Jane Liu8ce58f52017-06-29 13:40:22 -0400934
Lei Zhangab41f252018-12-23 03:10:50 +0000935TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000936 // Open a file with multiline markup annotations.
937 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
938 FPDF_PAGE page = LoadPage(0);
939 ASSERT_TRUE(page);
940 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000941 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000942 ASSERT_TRUE(annot);
943
944 // This is a three line annotation.
945 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
946 }
947 UnloadPage(page);
948
949 // null annotation should return 0
950 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
951}
952
Lei Zhangab41f252018-12-23 03:10:50 +0000953TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400954 // Open a file with 3 annotations on its first page.
955 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000956 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400957 ASSERT_TRUE(page);
958 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
959
Jane Liu0c6b07d2017-08-15 10:50:22 -0400960 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400961
Lei Zhanga21d5932018-02-05 18:28:38 +0000962 // Check that the annotations have the expected rectangle coordinates.
963 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000964 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000965 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
966 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
967 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400968
Lei Zhanga21d5932018-02-05 18:28:38 +0000969 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000970 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000971 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
972 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
973 }
974
975 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000976 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000977 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
978 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
979 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400980
981 // Check that nothing happens when attempting to remove an annotation with an
982 // out-of-bound index.
983 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
984 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
985 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
986
987 // Remove the second annotation.
988 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
989 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
990 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
991
992 // Save the document, closing the page and document.
993 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000994 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400995
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400996 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400997 // Open the saved document.
998 std::string new_file = GetString();
999 FPDF_FILEACCESS file_access;
1000 memset(&file_access, 0, sizeof(file_access));
1001 file_access.m_FileLen = new_file.size();
1002 file_access.m_GetBlock = GetBlockFromString;
1003 file_access.m_Param = &new_file;
1004 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
1005 ASSERT_TRUE(new_doc);
1006 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
1007 ASSERT_TRUE(new_page);
1008
1009 // Check that the saved document has 2 annotations on the first page.
1010 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
1011
Lei Zhanga21d5932018-02-05 18:28:38 +00001012 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1013 // by verifying their rectangle coordinates.
1014 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001015 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001016 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1017 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1018 }
Jane Liu8ce58f52017-06-29 13:40:22 -04001019
Lei Zhanga21d5932018-02-05 18:28:38 +00001020 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001021 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001022 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1023 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1024 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001025 FPDF_ClosePage(new_page);
1026 FPDF_CloseDocument(new_doc);
1027}
Jane Liu8ce58f52017-06-29 13:40:22 -04001028
Hui Yingst4a21df02020-05-13 03:15:44 +00001029TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001030#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst4a21df02020-05-13 03:15:44 +00001031#if defined(OS_LINUX)
1032 static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa";
1033 static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622";
1034 static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001035#else
Hui Yingst4a21df02020-05-13 03:15:44 +00001036 static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4";
1037 static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f";
1038 static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006";
1039#endif // defined(OS_LINUX)
1040#else
1041#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001042 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
1043 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
1044 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Hui Yingst4a21df02020-05-13 03:15:44 +00001045#elif defined(OS_MACOSX)
1046 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
1047 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
1048 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Jane Liubaa7ff42017-06-29 19:18:23 -04001049#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001050 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
1051 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
1052 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -04001053#endif
Hui Yingst4a21df02020-05-13 03:15:44 +00001054#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liubaa7ff42017-06-29 19:18:23 -04001055
1056 // Open a file with two annotations and load its first page.
1057 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001058 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -04001059 ASSERT_TRUE(page);
1060 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1061
1062 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001063 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001064 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001065 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001066 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001067
Lei Zhanga21d5932018-02-05 18:28:38 +00001068 {
1069 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001070 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001071 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001072
Lei Zhanga21d5932018-02-05 18:28:38 +00001073 // Check that this annotation has one path object and retrieve it.
1074 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001075 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001076 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1077 EXPECT_FALSE(path);
1078 path = FPDFAnnot_GetObject(annot.get(), 0);
1079 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
1080 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -04001081
Lei Zhanga21d5932018-02-05 18:28:38 +00001082 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +00001083 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +00001084 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -04001085
Lei Zhanga21d5932018-02-05 18:28:38 +00001086 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001087 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001088 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001089 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001090 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001091
Lei Zhanga21d5932018-02-05 18:28:38 +00001092 // Add a second path object to the same annotation.
1093 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1094 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +00001095 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1096 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +00001097 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1098 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1099 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -04001100
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001101 // The object is in the annontation, not in the page, so the page object
1102 // array should not change.
1103 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1104
Lei Zhanga21d5932018-02-05 18:28:38 +00001105 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001106 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001107 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001108 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001109 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001110
Lei Zhanga21d5932018-02-05 18:28:38 +00001111 // Delete the newly added path object.
1112 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1113 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001114 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +00001115 }
Jane Liu7a9a38b2017-07-11 13:47:37 -04001116
1117 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001118 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001119 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001120 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001121 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001122
Jane Liubaa7ff42017-06-29 19:18:23 -04001123 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -04001124
Lei Zhanga21d5932018-02-05 18:28:38 +00001125 {
1126 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001127 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001128 ASSERT_TRUE(annot);
1129 rect.left = 200.f;
1130 rect.bottom = 400.f;
1131 rect.right = 500.f;
1132 rect.top = 600.f;
1133 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -04001134
Lei Zhanga21d5932018-02-05 18:28:38 +00001135 // Add a new path to the annotation.
1136 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1137 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1138 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1139 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1140 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +00001141 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1142 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +00001143 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1144 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1145 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1146
1147 // Check that the annotation's bounding box came from its rectangle.
1148 FS_RECTF new_rect;
1149 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1150 EXPECT_EQ(rect.left, new_rect.left);
1151 EXPECT_EQ(rect.bottom, new_rect.bottom);
1152 EXPECT_EQ(rect.right, new_rect.right);
1153 EXPECT_EQ(rect.top, new_rect.top);
1154 }
Jane Liubaa7ff42017-06-29 19:18:23 -04001155
1156 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -04001157 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001158 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -04001159
1160 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +00001161 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001162 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001163 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -04001164
Jane Liu36567742017-07-06 11:13:35 -04001165 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001166 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -04001167
Lei Zhanga21d5932018-02-05 18:28:38 +00001168 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001169 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001170 ASSERT_TRUE(annot);
1171 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -04001172
Lei Zhanga21d5932018-02-05 18:28:38 +00001173 // Check that the new annotation's rectangle is as defined.
1174 FS_RECTF new_rect;
1175 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1176 EXPECT_EQ(rect.left, new_rect.left);
1177 EXPECT_EQ(rect.bottom, new_rect.bottom);
1178 EXPECT_EQ(rect.right, new_rect.right);
1179 EXPECT_EQ(rect.top, new_rect.top);
1180 }
1181
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001182 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001183 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -04001184}
Jane Liub137e752017-07-05 15:04:33 -04001185
Lei Zhangab41f252018-12-23 03:10:50 +00001186TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -04001187 // Open a file with an annotation and load its first page.
1188 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001189 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -04001190 ASSERT_TRUE(page);
1191
1192 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001193 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001194 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001195 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1196 }
Jane Liub137e752017-07-05 15:04:33 -04001197
Lei Zhanga21d5932018-02-05 18:28:38 +00001198 {
1199 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001200 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001201 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -04001202
Lei Zhanga21d5932018-02-05 18:28:38 +00001203 // Check that the original flag values are as expected.
1204 int flags = FPDFAnnot_GetFlags(annot.get());
1205 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1206 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001207
Lei Zhanga21d5932018-02-05 18:28:38 +00001208 // Set the HIDDEN flag.
1209 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1210 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1211 flags = FPDFAnnot_GetFlags(annot.get());
1212 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1213 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001214
Lei Zhanga21d5932018-02-05 18:28:38 +00001215 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001216 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001217 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001218 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001219 }
Jane Liub137e752017-07-05 15:04:33 -04001220
Lei Zhanga21d5932018-02-05 18:28:38 +00001221 // Unset the HIDDEN flag.
1222 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1223 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1224 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1225 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1226 flags = FPDFAnnot_GetFlags(annot.get());
1227 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1228 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -04001229
Lei Zhanga21d5932018-02-05 18:28:38 +00001230 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001231 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001232 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001233 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1234 }
Lei Zhanga21d5932018-02-05 18:28:38 +00001235 }
Jane Liub137e752017-07-05 15:04:33 -04001236
Jane Liub137e752017-07-05 15:04:33 -04001237 UnloadPage(page);
1238}
Jane Liu36567742017-07-06 11:13:35 -04001239
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001240// TODO(crbug.com/pdfium/1541): Fix this test and enable.
1241#if defined(_SKIA_SUPPORT_)
Lei Zhang03e5e682019-09-16 19:45:55 +00001242#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1243#else
1244#define MAYBE_AddAndModifyImage AddAndModifyImage
1245#endif
1246TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001247#if defined(_SKIA_SUPPORT_PATHS_)
1248#if defined(OS_LINUX)
1249 static const char kMd5NewImage[] = "26a8eb30937226a677839379e0d7ae1a";
1250 static const char kMd5ModifiedImage[] = "2985114b32ba1a96be78ee643fe31aa5";
1251#else
1252 static const char kMd5NewImage[] = "14012ab500b4671fa73dd760129a8a93";
1253 static const char kMd5ModifiedImage[] = "5f97f98f58ed04dc393f31460485f1a2";
1254#endif // defined(OS_LINUX)
1255#else
Lei Zhange4cdac52019-04-30 16:45:57 +00001256#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001257 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
1258 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +00001259#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001260 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
1261 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -04001262#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001263 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
1264 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -04001265#endif
Hui Yingstdd9a3f62020-06-16 18:14:21 +00001266#endif // defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001267
1268 // Open a file with two annotations and load its first page.
1269 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001270 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001271 ASSERT_TRUE(page);
1272 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1273
1274 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001275 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001276 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001277 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001278 }
Jane Liu36567742017-07-06 11:13:35 -04001279
Jane Liu36567742017-07-06 11:13:35 -04001280 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001281 FPDF_BITMAP image_bitmap;
1282
1283 {
1284 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001285 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001286 ASSERT_TRUE(annot);
1287 FS_RECTF rect;
1288 rect.left = 200.f;
1289 rect.bottom = 600.f;
1290 rect.right = 400.f;
1291 rect.top = 800.f;
1292 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1293
1294 // Add a solid-color translucent image object to the new annotation.
1295 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1296 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1297 0xeeeecccc);
1298 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1299 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1300 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1301 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1302 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1303 kBitmapSize, 0, 0));
1304 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1305 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1306 }
Jane Liu36567742017-07-06 11:13:35 -04001307
1308 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001309 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001310 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001311 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001312 }
Jane Liu36567742017-07-06 11:13:35 -04001313
Lei Zhanga21d5932018-02-05 18:28:38 +00001314 {
1315 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001316 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001317 ASSERT_TRUE(annot);
1318 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1319 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1320 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001321
Lei Zhanga21d5932018-02-05 18:28:38 +00001322 // Modify the image in the new annotation.
1323 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1324 0xff000000);
1325 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1326 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1327 }
Jane Liu36567742017-07-06 11:13:35 -04001328
1329 // Save the document, closing the page and document.
1330 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001331 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001332 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001333
1334 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001335 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001336}
1337
Hui Yingst6cd754f2020-05-14 04:05:25 +00001338TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001339#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001340#if defined(OS_LINUX)
1341 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1342 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001343#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001344 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1345 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
1346#endif // defined(OS_LINUX)
1347#else
1348#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001349 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1350 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Hui Yingst6cd754f2020-05-14 04:05:25 +00001351#elif defined(OS_MACOSX)
1352 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1353 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001354#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001355 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1356 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001357#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001358#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001359
1360 // Open a file with two annotations and load its first page.
1361 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001362 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001363 ASSERT_TRUE(page);
1364 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1365
1366 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001367 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001368 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001369 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001370 }
Jane Liu36567742017-07-06 11:13:35 -04001371
Lei Zhanga21d5932018-02-05 18:28:38 +00001372 {
1373 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001374 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001375 ASSERT_TRUE(annot);
1376 FS_RECTF rect;
1377 rect.left = 200.f;
1378 rect.bottom = 550.f;
1379 rect.right = 450.f;
1380 rect.top = 650.f;
1381 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001382
Lei Zhanga21d5932018-02-05 18:28:38 +00001383 // Add a translucent text object to the new annotation.
1384 FPDF_PAGEOBJECT text_object =
1385 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1386 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001387 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001388 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1389 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001390 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001391 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1392 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1393 }
Jane Liu36567742017-07-06 11:13:35 -04001394
1395 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001396 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001397 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001398 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001399 }
Jane Liu36567742017-07-06 11:13:35 -04001400
Lei Zhanga21d5932018-02-05 18:28:38 +00001401 {
1402 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001403 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001404 ASSERT_TRUE(annot);
1405 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1406 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1407 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001408
Lei Zhanga21d5932018-02-05 18:28:38 +00001409 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001410 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001411 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1412 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1413 }
Jane Liu36567742017-07-06 11:13:35 -04001414
1415 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001416 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001417 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001418 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001419 }
Jane Liu36567742017-07-06 11:13:35 -04001420
1421 // Remove the new annotation, and check that the page renders as before.
1422 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001423 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001424 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001425 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001426 }
Jane Liu36567742017-07-06 11:13:35 -04001427
1428 UnloadPage(page);
1429}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001430
Lei Zhang03e5e682019-09-16 19:45:55 +00001431// TODO(crbug.com/pdfium/11): Fix this test and enable.
1432#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1433#define MAYBE_GetSetStringValue DISABLED_GetSetStringValue
1434#else
1435#define MAYBE_GetSetStringValue GetSetStringValue
1436#endif
1437TEST_F(FPDFAnnotEmbedderTest, MAYBE_GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001438 // Open a file with four annotations and load its first page.
1439 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001440 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001441 ASSERT_TRUE(page);
1442
Lei Zhang4f556b82019-04-08 16:32:41 +00001443 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001444
Lei Zhanga21d5932018-02-05 18:28:38 +00001445 {
1446 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001447 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001448 ASSERT_TRUE(annot);
1449
1450 // Check that a non-existent key does not exist.
1451 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1452
1453 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001454 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001455 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001456 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1457 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1458 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001459
1460 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001461 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001462 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001463 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001464 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001465 ASSERT_EQ(66u, length_bytes);
1466 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1467 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1468 length_bytes));
1469 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1470 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001471
1472 // Check that the string value of the modified date is correct.
1473 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001474 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1475 nullptr, 0);
1476 ASSERT_EQ(44u, length_bytes);
1477 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001478 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001479 buf.data(), length_bytes));
1480 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001481
1482 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001483 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001484 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1485 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001486 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001487
1488 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001489 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001490 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001491
Lei Zhange4cdac52019-04-30 16:45:57 +00001492#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001493 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001494#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001495 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001496#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001497 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001498#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001499
1500 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001501 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001502 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001503 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001504 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001505 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001506
Lei Zhanga21d5932018-02-05 18:28:38 +00001507 // Check that the string value of the modified date is the newly-set value.
1508 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001509 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001510 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001511 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001512 ASSERT_EQ(44u, length_bytes);
1513 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001514 EXPECT_EQ(44u,
1515 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001516 buf.data(), length_bytes));
1517 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001518 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001519
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001520 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001521 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001522}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001523
rycsmith3e785602019-03-05 21:48:36 +00001524TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001525 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001526 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1527 FPDF_PAGE page = LoadPage(0);
1528 ASSERT_TRUE(page);
1529 {
1530 // First two annotations do not have "MaxLen" attribute.
1531 for (int i = 0; i < 2; i++) {
1532 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1533 ASSERT_TRUE(annot);
1534
1535 // Verify that no "MaxLen" key present.
1536 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1537
1538 float value;
1539 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1540 }
1541
1542 // Annotation in index 2 has "MaxLen" of 10.
1543 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1544 ASSERT_TRUE(annot);
1545
1546 // Verify that "MaxLen" key present.
1547 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1548
1549 float value;
1550 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1551 EXPECT_FLOAT_EQ(10.0f, value);
1552
1553 // Check bad inputs.
1554 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1555 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1556 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1557 // Ask for key that exists but is not a number.
1558 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1559 }
1560
1561 UnloadPage(page);
1562}
1563
Lei Zhangab41f252018-12-23 03:10:50 +00001564TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001565 // Open a file with four annotations and load its first page.
1566 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001567 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001568 ASSERT_TRUE(page);
1569
Lei Zhanga21d5932018-02-05 18:28:38 +00001570 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001571 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1572 static constexpr size_t kExpectNormalAPLength = 73970;
1573
Lei Zhanga21d5932018-02-05 18:28:38 +00001574 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001575 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001576 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001577
Lei Zhanga21d5932018-02-05 18:28:38 +00001578 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001579 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001580 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001581 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001582
Lei Zhanga21d5932018-02-05 18:28:38 +00001583 // Check that the string value of an AP is not returned if the buffer is too
1584 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001585 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1586 // Write in the buffer to verify it's not overwritten.
1587 memcpy(buf.data(), "abcdefgh", 8);
1588 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001589 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001590 buf.data(), normal_length_bytes - 1));
1591 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001592
Lei Zhanga21d5932018-02-05 18:28:38 +00001593 // Check that the string value of an AP is returned through a buffer that is
1594 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001595 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001596 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001597 buf.data(), normal_length_bytes));
1598 EXPECT_EQ(kMd5NormalAP,
1599 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1600 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001601
Lei Zhanga21d5932018-02-05 18:28:38 +00001602 // Check that the string value of an AP is returned through a buffer that is
1603 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001604 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1605 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001606 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001607 buf.data(), normal_length_bytes + 2));
1608 EXPECT_EQ(kMd5NormalAP,
1609 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1610 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001611
Lei Zhanga21d5932018-02-05 18:28:38 +00001612 // Check that getting an AP for a mode that does not have an AP returns an
1613 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001614 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001615 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001616 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001617
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001618 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001619 EXPECT_EQ(2u,
1620 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001621 buf.data(), 1000));
1622 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001623
Lei Zhanga21d5932018-02-05 18:28:38 +00001624 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001625 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001626 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001627 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001628 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001629 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001630 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001631
Lei Zhanga21d5932018-02-05 18:28:38 +00001632 // Set the AP correctly now.
1633 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001634 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001635
Lei Zhanga21d5932018-02-05 18:28:38 +00001636 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001637 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001638 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001639 ASSERT_EQ(24u, rollover_length_bytes);
1640 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001641 EXPECT_EQ(24u,
1642 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001643 buf.data(), rollover_length_bytes));
1644 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001645
Lei Zhanga21d5932018-02-05 18:28:38 +00001646 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001647 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1648 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001649 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001650 buf.data(), normal_length_bytes));
1651 EXPECT_EQ(kMd5NormalAP,
1652 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1653 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001654 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001655
1656 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001657 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001658 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001659
Lei Zhang0b494052019-01-31 21:41:15 +00001660 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001661 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001662 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001663 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001664
Lei Zhanga21d5932018-02-05 18:28:38 +00001665 // Check that the new annotation value is equal to the value we set before
1666 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001667 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001668 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001669 ASSERT_EQ(24u, rollover_length_bytes);
1670 std::vector<FPDF_WCHAR> buf =
1671 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001672 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1673 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001674 buf.data(), rollover_length_bytes));
1675 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001676 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001677
1678 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001679 CloseSavedPage(page);
1680 CloseSavedDocument();
1681}
1682
Lei Zhangab41f252018-12-23 03:10:50 +00001683TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001684 // Open a file with four annotations and load its first page.
1685 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001686 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001687 ASSERT_TRUE(page);
1688
Lei Zhanga21d5932018-02-05 18:28:38 +00001689 {
1690 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001691 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001692 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001693
Lei Zhanga21d5932018-02-05 18:28:38 +00001694 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001695 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001696 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001697 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001698 EXPECT_EQ(73970u,
1699 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1700 nullptr, 0));
1701 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1702 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001703
Lei Zhanga21d5932018-02-05 18:28:38 +00001704 // Check that setting the Down AP to null removes the Down entry but keeps
1705 // Normal intact.
1706 EXPECT_TRUE(
1707 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1708 EXPECT_EQ(73970u,
1709 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1710 nullptr, 0));
1711 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1712 nullptr, 0));
1713 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001714
Lei Zhang75c81712018-02-08 17:22:39 +00001715 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001716}
1717
Lei Zhangab41f252018-12-23 03:10:50 +00001718TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001719 // Open a file with four annotations and load its first page.
1720 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001721 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001722 ASSERT_TRUE(page);
1723
Lei Zhanga21d5932018-02-05 18:28:38 +00001724 {
1725 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001726 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001727 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001728
Lei Zhanga21d5932018-02-05 18:28:38 +00001729 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001730 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001731 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001732 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001733 EXPECT_EQ(73970u,
1734 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1735 nullptr, 0));
1736 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1737 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001738
Lei Zhanga21d5932018-02-05 18:28:38 +00001739 // Check that setting the Normal AP to null removes the whole AP dictionary.
1740 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1741 nullptr));
1742 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1743 nullptr, 0));
1744 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1745 nullptr, 0));
1746 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001747
Lei Zhang75c81712018-02-08 17:22:39 +00001748 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001749}
1750
Lei Zhangab41f252018-12-23 03:10:50 +00001751TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001752 // Open a file with annotations and load its first page.
1753 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001754 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001755 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001756 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001757
Lei Zhanga21d5932018-02-05 18:28:38 +00001758 {
1759 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001760 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001761 ASSERT_TRUE(annot);
1762 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1763 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001764 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001765 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1766 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1767 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001768
Lei Zhanga21d5932018-02-05 18:28:38 +00001769 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001770 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001771 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1772 ASSERT_TRUE(popup);
1773 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1774 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1775 FS_RECTF rect;
1776 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1777 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1778 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001779
Lei Zhanga21d5932018-02-05 18:28:38 +00001780 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1781 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001782 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001783 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1784 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001785
Lei Zhanga21d5932018-02-05 18:28:38 +00001786 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1787 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001788 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001789 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001790 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1791 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001792 }
Jane Liu300bb272017-08-21 14:37:53 -04001793
Jane Liu300bb272017-08-21 14:37:53 -04001794 UnloadPage(page);
1795}
1796
Lei Zhangab41f252018-12-23 03:10:50 +00001797TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001798 // Open file with form text fields.
1799 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001800 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001801 ASSERT_TRUE(page);
1802
Lei Zhanga21d5932018-02-05 18:28:38 +00001803 {
1804 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001805 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001806 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001807
Lei Zhanga21d5932018-02-05 18:28:38 +00001808 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001809 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001810 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001811 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001812 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001813
Lei Zhanga21d5932018-02-05 18:28:38 +00001814 {
1815 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001816 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001817 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001818
Lei Zhanga21d5932018-02-05 18:28:38 +00001819 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001820 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001821 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001822 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1823 }
1824
1825 {
1826 // Retrieve the fourth annotation: user-editable password text field.
1827 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1828 ASSERT_TRUE(annot);
1829
1830 // Check that the flag values are as expected.
1831 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1832 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1833 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001834 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001835
1836 UnloadPage(page);
1837}
1838
Lei Zhangab41f252018-12-23 03:10:50 +00001839TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001840 // Open file with form text fields.
1841 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001842 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001843 ASSERT_TRUE(page);
1844
Lei Zhanga21d5932018-02-05 18:28:38 +00001845 {
1846 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001847 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001848 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001849
Lei Zhanga21d5932018-02-05 18:28:38 +00001850 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001851 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001852 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1853 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1854 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1855 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001856
Lei Zhanga21d5932018-02-05 18:28:38 +00001857 {
1858 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001859 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001860 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001861
Lei Zhanga21d5932018-02-05 18:28:38 +00001862 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001863 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001864 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1865 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1866 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1867 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001868
Lei Zhanga21d5932018-02-05 18:28:38 +00001869 {
1870 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001871 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001872 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001873
Lei Zhanga21d5932018-02-05 18:28:38 +00001874 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001875 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001876 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1877 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1878 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1879 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001880
1881 UnloadPage(page);
1882}
Diana Gage40870db2017-07-19 18:16:03 -07001883
Lei Zhangab41f252018-12-23 03:10:50 +00001884TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001885 // Open file with form text fields.
1886 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1887 FPDF_PAGE page = LoadPage(0);
1888 ASSERT_TRUE(page);
1889
1890 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001891 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1892 EXPECT_FALSE(
1893 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001894
Lei Zhang8da98232019-12-11 23:29:33 +00001895 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001896 {
1897 // Verify there is an annotation.
1898 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001899 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001900 EXPECT_TRUE(annot);
1901 }
1902
1903 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001904 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1905 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1906 EXPECT_FALSE(
1907 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001908
1909 UnloadPage(page);
1910}
1911
Lei Zhangab41f252018-12-23 03:10:50 +00001912TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001913 // Open file with form text fields.
1914 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1915 FPDF_PAGE page = LoadPage(0);
1916 ASSERT_TRUE(page);
1917
Lei Zhanga21d5932018-02-05 18:28:38 +00001918 {
1919 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001920 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001921 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001922 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001923 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001924
Lei Zhanga21d5932018-02-05 18:28:38 +00001925 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001926 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001927 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1928 }
Diana Gage40870db2017-07-19 18:16:03 -07001929
Lei Zhanga21d5932018-02-05 18:28:38 +00001930 {
1931 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001932 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001933 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001934 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001935 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001936
Lei Zhanga21d5932018-02-05 18:28:38 +00001937 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001938 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001939 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1940 }
Diana Gage40870db2017-07-19 18:16:03 -07001941
1942 UnloadPage(page);
1943}
1944
Lei Zhangab41f252018-12-23 03:10:50 +00001945TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001946 // Open file with form comboboxes.
1947 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1948 FPDF_PAGE page = LoadPage(0);
1949 ASSERT_TRUE(page);
1950
Lei Zhanga21d5932018-02-05 18:28:38 +00001951 {
1952 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001953 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001954 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001955 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001956 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001957
Lei Zhanga21d5932018-02-05 18:28:38 +00001958 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001959 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001960 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1961 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1962 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1963 }
Diana Gage40870db2017-07-19 18:16:03 -07001964
Lei Zhanga21d5932018-02-05 18:28:38 +00001965 {
1966 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001967 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001968 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001969 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001970 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001971
Lei Zhanga21d5932018-02-05 18:28:38 +00001972 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001973 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001974 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1975 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1976 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1977 }
Diana Gage40870db2017-07-19 18:16:03 -07001978
Lei Zhanga21d5932018-02-05 18:28:38 +00001979 {
1980 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001981 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001982 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001983 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001984 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001985
Lei Zhanga21d5932018-02-05 18:28:38 +00001986 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001987 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001988 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1989 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1990 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1991 }
Diana Gage40870db2017-07-19 18:16:03 -07001992
1993 UnloadPage(page);
1994}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001995
Lei Zhang03e5e682019-09-16 19:45:55 +00001996// TODO(crbug.com/pdfium/11): Fix this test and enable.
1997#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1998#define MAYBE_BUG_1206 DISABLED_BUG_1206
1999#else
2000#define MAYBE_BUG_1206 BUG_1206
2001#endif
2002TEST_F(FPDFAnnotEmbedderTest, MAYBE_BUG_1206) {
Lei Zhang992e7e22019-02-04 19:20:58 +00002003 static constexpr size_t kExpectedSize = 1609;
2004 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
2005
2006 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2007
2008 FPDF_PAGE page = LoadPage(0);
2009 ASSERT_TRUE(page);
2010
2011 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2012 EXPECT_EQ(kExpectedSize, GetString().size());
2013 ClearString();
2014
2015 for (size_t i = 0; i < 10; ++i) {
2016 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2017 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
2018
2019 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2020 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2021 // equal, not bigger.
2022 EXPECT_LT(kExpectedSize, GetString().size());
2023 ClearString();
2024 }
2025
2026 UnloadPage(page);
2027}
2028
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002029TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
2030 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2031 FPDF_PAGE page = LoadPage(0);
2032 ASSERT_TRUE(page);
2033 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2034
2035 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00002036 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002037 static const size_t kBufSize = 12;
2038 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002039
2040 {
2041 // Add a text annotation to the page.
2042 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2043 ASSERT_TRUE(annot);
2044 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2045 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2046
2047 // Make sure there is no test key, add set a value there, and read it back.
2048 std::fill(buf.begin(), buf.end(), 'x');
2049 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002050 kBufSize));
2051 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002052
Lei Zhangf0f67682019-04-08 17:03:21 +00002053 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002054 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2055
2056 std::fill(buf.begin(), buf.end(), 'x');
2057 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002058 kBufSize));
2059 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002060 }
2061
Lei Zhang05ec64c2019-01-09 03:00:06 +00002062 {
2063 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2064 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00002065 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00002066 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002067 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2068 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2069 // Also do the same test for its appearance string.
2070 std::fill(buf.begin(), buf.end(), 'x');
2071 ASSERT_EQ(2u,
2072 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002073 buf.data(), kBufSize));
2074 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002075
Lei Zhangf0f67682019-04-08 17:03:21 +00002076 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00002077 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2078 text.get()));
2079
2080 std::fill(buf.begin(), buf.end(), 'x');
2081 ASSERT_EQ(6u,
2082 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002083 buf.data(), kBufSize));
2084 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002085 }
2086
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002087 UnloadPage(page);
2088
2089 {
2090 // Save a copy, open the copy, and check the annotation again.
2091 // Note that it renders the rotation.
2092 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00002093 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002094 FPDF_PAGE saved_page = LoadSavedPage(0);
2095 ASSERT_TRUE(saved_page);
2096
Lei Zhang05ec64c2019-01-09 03:00:06 +00002097 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002098 {
2099 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2100 ASSERT_TRUE(annot);
2101 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2102
2103 std::fill(buf.begin(), buf.end(), 'x');
2104 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002105 kBufSize));
2106 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002107 }
2108
Lei Zhang05ec64c2019-01-09 03:00:06 +00002109 {
2110 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2111 ASSERT_TRUE(annot);
2112 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2113 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2114
2115 std::fill(buf.begin(), buf.end(), 'x');
2116 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002117 kBufSize));
2118 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00002119 }
2120
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00002121 CloseSavedPage(saved_page);
2122 CloseSavedDocument();
2123 }
2124}
rycsmithcb752f32019-02-21 18:40:53 +00002125
2126TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
2127 // Open a file with combobox widget annotations and load its first page.
2128 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2129 FPDF_PAGE page = LoadPage(0);
2130 ASSERT_TRUE(page);
2131
2132 {
2133 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2134 ASSERT_TRUE(annot);
2135
2136 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2137
2138 annot.reset(FPDFPage_GetAnnot(page, 1));
2139 ASSERT_TRUE(annot);
2140
2141 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00002142
2143 // Check bad form handle / annot.
2144 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2145 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2146 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00002147 }
2148
2149 UnloadPage(page);
2150}
2151
2152TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
2153 // Open a file with listbox widget annotations and load its first page.
2154 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2155 FPDF_PAGE page = LoadPage(0);
2156 ASSERT_TRUE(page);
2157
2158 {
2159 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2160 ASSERT_TRUE(annot);
2161
2162 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2163
2164 annot.reset(FPDFPage_GetAnnot(page, 1));
2165 ASSERT_TRUE(annot);
2166
2167 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2168 }
2169
2170 UnloadPage(page);
2171}
2172
2173TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
2174 // Open a file with ink annotations and load its first page.
2175 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2176 FPDF_PAGE page = LoadPage(0);
2177 ASSERT_TRUE(page);
2178
2179 {
2180 // annotations do not have "Opt" array and will return -1
2181 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2182 ASSERT_TRUE(annot);
2183
2184 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2185
2186 annot.reset(FPDFPage_GetAnnot(page, 1));
2187 ASSERT_TRUE(annot);
2188
2189 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2190 }
2191
2192 UnloadPage(page);
2193}
2194
2195TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
2196 // Open a file with combobox widget annotations and load its first page.
2197 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2198 FPDF_PAGE page = LoadPage(0);
2199 ASSERT_TRUE(page);
2200
2201 {
2202 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2203 ASSERT_TRUE(annot);
2204
2205 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002206 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002207 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002208 ASSERT_EQ(8u, length_bytes);
2209 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002210 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002211 buf.data(), length_bytes));
2212 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002213
2214 annot.reset(FPDFPage_GetAnnot(page, 1));
2215 ASSERT_TRUE(annot);
2216
2217 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002218 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002219 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002220 ASSERT_EQ(12u, length_bytes);
2221 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002222 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002223 buf.data(), length_bytes));
2224 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002225
2226 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002227 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002228 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002229 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002230 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002231 buf.data(), length_bytes));
2232 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002233
Lei Zhange7033c82019-02-26 19:30:49 +00002234 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00002235 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2236 nullptr, 0));
2237 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2238 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00002239
2240 // Check bad form handle / annot.
2241 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2242 EXPECT_EQ(0u,
2243 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2244 EXPECT_EQ(0u,
2245 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00002246 }
2247
2248 UnloadPage(page);
2249}
2250
2251TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
2252 // Open a file with listbox widget annotations and load its first page.
2253 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2254 FPDF_PAGE page = LoadPage(0);
2255 ASSERT_TRUE(page);
2256
2257 {
2258 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2259 ASSERT_TRUE(annot);
2260
2261 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002262 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002263 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002264 ASSERT_EQ(8u, length_bytes);
2265 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002266 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002267 buf.data(), length_bytes));
2268 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002269
2270 annot.reset(FPDFPage_GetAnnot(page, 1));
2271 ASSERT_TRUE(annot);
2272
2273 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002274 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002275 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002276 ASSERT_EQ(12u, length_bytes);
2277 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002278 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002279 buf.data(), length_bytes));
2280 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002281
2282 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002283 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002284 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002285 ASSERT_EQ(18u, length_bytes);
2286 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002287 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002288 buf.data(), length_bytes));
2289 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002290
2291 // indices out of range
2292 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2293 nullptr, 0));
2294 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2295 nullptr, 0));
2296 }
2297
2298 UnloadPage(page);
2299}
2300
2301TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2302 // Open a file with ink annotations and load its first page.
2303 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2304 FPDF_PAGE page = LoadPage(0);
2305 ASSERT_TRUE(page);
2306
2307 {
2308 // annotations do not have "Opt" array and will return 0
2309 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2310 ASSERT_TRUE(annot);
2311
2312 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2313 nullptr, 0));
2314
2315 annot.reset(FPDFPage_GetAnnot(page, 1));
2316 ASSERT_TRUE(annot);
2317
2318 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2319 nullptr, 0));
2320 }
2321
2322 UnloadPage(page);
2323}
Ryan Smith09c23b12019-04-25 18:09:06 +00002324
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002325TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2326 // Open a file with combobox widget annotations and load its first page.
2327 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2328 FPDF_PAGE page = LoadPage(0);
2329 ASSERT_TRUE(page);
2330
2331 {
2332 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2333 ASSERT_TRUE(annot);
2334
2335 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2336 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2337 ASSERT_EQ(3, count);
2338 for (int i = 0; i < count; i++) {
2339 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2340 }
2341
2342 annot.reset(FPDFPage_GetAnnot(page, 1));
2343 ASSERT_TRUE(annot);
2344
2345 // Checks for Combobox with Values (/V) object which is just a string.
2346 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2347 ASSERT_EQ(26, count);
2348 for (int i = 0; i < count; i++) {
2349 EXPECT_EQ(i == 1,
2350 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2351 }
2352
2353 // Checks for index outside bound i.e. (index >= CountOption()).
2354 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2355 /*index=*/26));
2356 // Checks for negetive index.
2357 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2358 /*index=*/-1));
2359
2360 // Checks for bad form handle/annot.
2361 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2362 EXPECT_FALSE(
2363 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2364 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2365 }
2366
2367 UnloadPage(page);
2368}
2369
2370TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2371 // Open a file with listbox widget annotations and load its first page.
2372 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2373 FPDF_PAGE page = LoadPage(0);
2374 ASSERT_TRUE(page);
2375
2376 {
2377 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2378 ASSERT_TRUE(annot);
2379
2380 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2381 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2382 ASSERT_EQ(3, count);
2383 for (int i = 0; i < count; i++) {
2384 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2385 }
2386
2387 annot.reset(FPDFPage_GetAnnot(page, 1));
2388 ASSERT_TRUE(annot);
2389
2390 // Checks for Listbox with Values (/V) object which is just a string.
2391 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2392 ASSERT_EQ(26, count);
2393 for (int i = 0; i < count; i++) {
2394 EXPECT_EQ(i == 1,
2395 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2396 }
2397
2398 annot.reset(FPDFPage_GetAnnot(page, 3));
2399 ASSERT_TRUE(annot);
2400
2401 // Checks for Listbox with only Selected indices (/I) object which is an
2402 // array with multiple objects.
2403 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2404 ASSERT_EQ(5, count);
2405 for (int i = 0; i < count; i++) {
2406 bool expected = (i == 1 || i == 3);
2407 EXPECT_EQ(expected,
2408 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2409 }
2410
2411 annot.reset(FPDFPage_GetAnnot(page, 4));
2412 ASSERT_TRUE(annot);
2413
2414 // Checks for Listbox with Values (/V) object which is an array with
2415 // multiple objects.
2416 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2417 ASSERT_EQ(5, count);
2418 for (int i = 0; i < count; i++) {
2419 bool expected = (i == 2 || i == 4);
2420 EXPECT_EQ(expected,
2421 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2422 }
2423
2424 annot.reset(FPDFPage_GetAnnot(page, 5));
2425 ASSERT_TRUE(annot);
2426
2427 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2428 // objects conflict with different lengths.
2429 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2430 ASSERT_EQ(5, count);
2431 for (int i = 0; i < count; i++) {
2432 bool expected = (i == 0 || i == 2);
2433 EXPECT_EQ(expected,
2434 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2435 }
2436 }
2437
2438 UnloadPage(page);
2439}
2440
2441TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2442 // Open a file with multiple form field annotations and load its first page.
2443 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2444 FPDF_PAGE page = LoadPage(0);
2445 ASSERT_TRUE(page);
2446
2447 {
2448 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2449 ASSERT_TRUE(annot);
2450
2451 // Checks for link annotation.
2452 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2453 /*index=*/0));
2454
2455 annot.reset(FPDFPage_GetAnnot(page, 3));
2456 ASSERT_TRUE(annot);
2457
2458 // Checks for text field annotation.
2459 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2460 /*index=*/0));
2461 }
2462
2463 UnloadPage(page);
2464}
2465
Ryan Smith09c23b12019-04-25 18:09:06 +00002466TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2467 // Open a file with combobox annotations and load its first page.
2468 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2469 FPDF_PAGE page = LoadPage(0);
2470 ASSERT_TRUE(page);
2471
2472 {
2473 // All 3 widgets have Tf font size 12.
2474 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2475 ASSERT_TRUE(annot);
2476
2477 float value;
2478 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2479 EXPECT_EQ(12.0, value);
2480
2481 annot.reset(FPDFPage_GetAnnot(page, 1));
2482 ASSERT_TRUE(annot);
2483
2484 float value_two;
2485 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2486 EXPECT_EQ(12.0, value_two);
2487
2488 annot.reset(FPDFPage_GetAnnot(page, 2));
2489 ASSERT_TRUE(annot);
2490
2491 float value_three;
2492 ASSERT_TRUE(
2493 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2494 EXPECT_EQ(12.0, value_three);
2495 }
2496
2497 UnloadPage(page);
2498}
2499
2500TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2501 // Open a file with textfield annotations and load its first page.
2502 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2503 FPDF_PAGE page = LoadPage(0);
2504 ASSERT_TRUE(page);
2505
2506 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002507 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002508 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2509 ASSERT_TRUE(annot);
2510
2511 float value;
2512 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2513 EXPECT_EQ(12.0, value);
2514
2515 annot.reset(FPDFPage_GetAnnot(page, 1));
2516 ASSERT_TRUE(annot);
2517
2518 float value_two;
2519 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2520 EXPECT_EQ(12.0, value_two);
2521
2522 annot.reset(FPDFPage_GetAnnot(page, 2));
2523 ASSERT_TRUE(annot);
2524
2525 float value_three;
2526 ASSERT_TRUE(
2527 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2528 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002529
2530 float value_four;
2531 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2532 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002533 }
2534
2535 UnloadPage(page);
2536}
2537
2538TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2539 // Open a file with ink annotations and load its first page.
2540 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2541 FPDF_PAGE page = LoadPage(0);
2542 ASSERT_TRUE(page);
2543
2544 {
2545 // Annotations that do not have variable text and will return -1.
2546 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2547 ASSERT_TRUE(annot);
2548
2549 float value;
2550 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2551
2552 annot.reset(FPDFPage_GetAnnot(page, 1));
2553 ASSERT_TRUE(annot);
2554
2555 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2556 }
2557
2558 UnloadPage(page);
2559}
2560
2561TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2562 // Open a file with combobox annotations and load its first page.
2563 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2564 FPDF_PAGE page = LoadPage(0);
2565 ASSERT_TRUE(page);
2566
2567 {
2568 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2569 ASSERT_TRUE(annot);
2570
2571 // Check bad form handle / annot.
2572 float value;
2573 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2574 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2575 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2576 }
2577
2578 UnloadPage(page);
2579}
2580
2581TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2582 // Open a file with textfield annotations and load its first page.
2583 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2584 FPDF_PAGE page = LoadPage(0);
2585 ASSERT_TRUE(page);
2586
2587 {
2588 // Obtain the first annotation, a text field with negative font size, -12.
2589 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2590 ASSERT_TRUE(annot);
2591
2592 float value;
2593 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2594 EXPECT_EQ(-12.0, value);
2595 }
2596
2597 UnloadPage(page);
2598}
Ryan Smith23fdf892019-07-17 21:51:26 +00002599
2600TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2601 // Open a file with checkbox and radiobuttons widget annotations and load its
2602 // first page.
2603 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2604 FPDF_PAGE page = LoadPage(0);
2605 ASSERT_TRUE(page);
2606
2607 {
2608 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2609 ASSERT_TRUE(annot);
2610 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2611 }
2612
2613 UnloadPage(page);
2614}
2615
2616TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2617 // Open a file with checkbox and radiobutton widget annotations and load its
2618 // first page.
2619 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2620 FPDF_PAGE page = LoadPage(0);
2621 ASSERT_TRUE(page);
2622
2623 {
2624 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2625 ASSERT_TRUE(annot);
2626 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2627 }
2628
2629 UnloadPage(page);
2630}
2631
2632TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2633 // Open a file with checkbox and radiobutton widget annotations and load its
2634 // first page.
2635 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2636 FPDF_PAGE page = LoadPage(0);
2637 ASSERT_TRUE(page);
2638
2639 {
2640 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2641 ASSERT_TRUE(annot);
2642 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2643
2644 annot.reset(FPDFPage_GetAnnot(page, 6));
2645 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2646
2647 annot.reset(FPDFPage_GetAnnot(page, 7));
2648 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2649 }
2650
2651 UnloadPage(page);
2652}
2653
2654TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2655 // Open a file with checkbox and radiobutton widget annotations and load its
2656 // first page.
2657 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2658 FPDF_PAGE page = LoadPage(0);
2659 ASSERT_TRUE(page);
2660
2661 {
2662 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2663 ASSERT_TRUE(annot);
2664 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2665
2666 annot.reset(FPDFPage_GetAnnot(page, 3));
2667 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2668
2669 annot.reset(FPDFPage_GetAnnot(page, 4));
2670 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2671 }
2672
2673 UnloadPage(page);
2674}
2675
2676TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2677 // Open a file with checkbox and radiobuttons widget annotations and load its
2678 // first page.
2679 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2680 FPDF_PAGE page = LoadPage(0);
2681 ASSERT_TRUE(page);
2682
2683 {
2684 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2685 ASSERT_TRUE(annot);
2686 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2687 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2688 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2689 }
2690
2691 UnloadPage(page);
2692}
2693
2694TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2695 // Open a file with text widget annotations and load its first page.
2696 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2697 FPDF_PAGE page = LoadPage(0);
2698 ASSERT_TRUE(page);
2699
2700 {
2701 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2702 ASSERT_TRUE(annot);
2703 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2704 }
2705
2706 UnloadPage(page);
2707}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002708
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002709TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2710 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002711 FPDF_PAGE page = LoadPage(0);
2712 ASSERT_TRUE(page);
2713
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002714 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2715
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002716 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002717 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002718 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002719 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002720 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002721
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002722 constexpr int kExpectedAnnotTypes[] = {-1,
2723 FPDF_FORMFIELD_COMBOBOX,
2724 FPDF_FORMFIELD_LISTBOX,
2725 FPDF_FORMFIELD_TEXTFIELD,
2726 FPDF_FORMFIELD_CHECKBOX,
2727 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002728
Lei Zhang4501a502020-05-18 16:52:59 +00002729 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002730 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002731 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002732 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002733 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2734 }
2735 UnloadPage(page);
2736}
2737
2738TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2739 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2740 FPDF_PAGE page = LoadPage(0);
2741 ASSERT_TRUE(page);
2742
2743 {
2744 EXPECT_EQ(0u,
2745 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2746
2747 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2748 ASSERT_TRUE(annot);
2749
2750 EXPECT_EQ(0u,
2751 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2752
2753 unsigned long length_bytes =
2754 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2755 ASSERT_EQ(2u, length_bytes);
2756 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2757 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2758 buf.data(), length_bytes));
2759 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2760 }
2761 {
2762 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2763 ASSERT_TRUE(annot);
2764
2765 unsigned long length_bytes =
2766 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2767 ASSERT_EQ(18u, length_bytes);
2768 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2769 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2770 buf.data(), length_bytes));
2771 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2772 }
2773 UnloadPage(page);
2774}
2775
2776TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2777 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2778 FPDF_PAGE page = LoadPage(0);
2779 ASSERT_TRUE(page);
2780
2781 {
2782 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2783 ASSERT_TRUE(annot);
2784
2785 unsigned long length_bytes =
2786 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2787 ASSERT_EQ(2u, length_bytes);
2788 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2789 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2790 buf.data(), length_bytes));
2791 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2792 }
2793 {
2794 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2795 ASSERT_TRUE(annot);
2796
2797 unsigned long length_bytes =
2798 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2799 ASSERT_EQ(14u, length_bytes);
2800 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2801 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2802 buf.data(), length_bytes));
2803 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2804 }
2805 UnloadPage(page);
2806}
2807
2808TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2809 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2810 FPDF_PAGE page = LoadPage(0);
2811 ASSERT_TRUE(page);
2812
2813 {
2814 EXPECT_EQ(0u,
2815 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2816
2817 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2818 ASSERT_TRUE(annot);
2819
2820 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2821
2822 unsigned long length_bytes =
2823 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2824 ASSERT_EQ(18u, length_bytes);
2825 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2826 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2827 buf.data(), length_bytes));
2828 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2829 }
2830 UnloadPage(page);
2831}
2832
2833TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2834 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2835 FPDF_PAGE page = LoadPage(0);
2836 ASSERT_TRUE(page);
2837
2838 {
2839 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2840 ASSERT_TRUE(annot);
2841
2842 unsigned long length_bytes =
2843 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2844 ASSERT_EQ(30u, length_bytes);
2845 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2846 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2847 buf.data(), length_bytes));
2848 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2849 }
2850 UnloadPage(page);
2851}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002852
Lei Zhang9b444002020-04-17 17:35:23 +00002853TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2854 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002855 FPDF_PAGE page = LoadPage(0);
2856 ASSERT_TRUE(page);
2857
Lei Zhang9b444002020-04-17 17:35:23 +00002858 // Verify widgets are by default focusable.
2859 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2860 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002861
Lei Zhang9b444002020-04-17 17:35:23 +00002862 // Expected annot subtypes for page 0 of annots.pdf.
2863 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2864 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2865 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2866 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2867 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002868
Lei Zhang9b444002020-04-17 17:35:23 +00002869 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002870 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002871 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2872 kExpectedAnnotSubtypes,
2873 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002874
Lei Zhang9b444002020-04-17 17:35:23 +00002875 // Make no annotation type focusable using the preferred method.
2876 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2877 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002878
Lei Zhang9b444002020-04-17 17:35:23 +00002879 // Restore the focusable type count back to 1, then set it back to 0 using a
2880 // different method.
2881 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2882 ASSERT_TRUE(
2883 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2884 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002885
Lei Zhang9b444002020-04-17 17:35:23 +00002886 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002887 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002888
Lei Zhang9b444002020-04-17 17:35:23 +00002889 // Now make links focusable.
2890 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2891 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002892
Lei Zhang9b444002020-04-17 17:35:23 +00002893 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002894 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002895 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2896 kExpectedAnnotSubtypes,
2897 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002898
Lei Zhang9b444002020-04-17 17:35:23 +00002899 // Test invalid parameters.
2900 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00002901 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002902 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00002903 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002904 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2905
Lei Zhang9b444002020-04-17 17:35:23 +00002906 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2907 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2908 subtypes.size()));
2909 EXPECT_FALSE(
2910 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2911 EXPECT_FALSE(
2912 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002913
2914 UnloadPage(page);
2915}
Lei Zhang671aece2020-04-14 19:02:26 +00002916
Hui Yingst609f7d62020-04-23 23:14:13 +00002917// TODO(crbug.com/pdfium/11): Fix this test and enable.
2918#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2919#define MAYBE_FocusableAnnotRendering DISABLED_FocusableAnnotRendering
2920#else
2921#define MAYBE_FocusableAnnotRendering FocusableAnnotRendering
2922#endif
2923TEST_F(FPDFAnnotEmbedderTest, MAYBE_FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00002924 ASSERT_TRUE(OpenDocument("annots.pdf"));
2925 FPDF_PAGE page = LoadPage(0);
2926 ASSERT_TRUE(page);
2927
2928 {
2929#if defined(OS_WIN)
2930 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
2931#elif defined(OS_MACOSX)
2932 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
2933#else
2934 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
2935#endif
2936 // Check the initial rendering.
2937 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2938 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2939 }
2940
2941 // Make links and highlights focusable.
2942 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
2943 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00002944 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00002945 ASSERT_TRUE(
2946 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
2947 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
2948 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
2949 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
2950 subtypes.size()));
2951 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
2952 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
2953
2954 {
2955#if defined(OS_WIN)
2956 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
2957#elif defined(OS_MACOSX)
2958 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
2959#else
2960 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
2961#endif
2962 // Focus the first link and check the rendering.
2963 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2964 ASSERT_TRUE(annot);
2965 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2966 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2967 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2968 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2969 }
2970
2971 {
2972#if defined(OS_WIN)
2973 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
2974#elif defined(OS_MACOSX)
2975 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
2976#else
2977 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
2978#endif
2979 // Focus the first highlight and check the rendering.
2980 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2981 ASSERT_TRUE(annot);
2982 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
2983 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2984 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2985 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2986 }
2987
2988 UnloadPage(page);
2989}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00002990
2991TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
2992 ASSERT_TRUE(OpenDocument("annots.pdf"));
2993 FPDF_PAGE page = LoadPage(0);
2994 ASSERT_TRUE(page);
2995 {
2996 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
2997 ASSERT_TRUE(annot);
2998 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2999 FPDF_LINK link_annot = FPDFAnnot_GetLink(annot.get());
3000 ASSERT_TRUE(link_annot);
3001
3002 FPDF_ACTION action = FPDFLink_GetAction(link_annot);
3003 ASSERT_TRUE(action);
3004 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
3005 FPDFAction_GetType(action));
3006
3007 constexpr char kExpectedResult[] =
3008 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3009 "fpdf_text.h";
Lei Zhang4501a502020-05-18 16:52:59 +00003010 constexpr unsigned long kExpectedLength = pdfium::size(kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00003011 unsigned long bufsize =
3012 FPDFAction_GetURIPath(document(), action, nullptr, 0);
3013 ASSERT_EQ(kExpectedLength, bufsize);
3014
3015 char buffer[1024];
3016 EXPECT_EQ(bufsize,
3017 FPDFAction_GetURIPath(document(), action, buffer, bufsize));
3018 EXPECT_STREQ(kExpectedResult, buffer);
3019 }
3020
3021 {
3022 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3023 ASSERT_TRUE(annot);
3024 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3025 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3026 }
3027
3028 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3029
3030 UnloadPage(page);
3031}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00003032
3033TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
3034 // Open a file with radio button widget annotations and load its first page.
3035 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3036 FPDF_PAGE page = LoadPage(0);
3037 ASSERT_TRUE(page);
3038
3039 {
3040 // Checks for bad annot.
3041 EXPECT_EQ(-1,
3042 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3043
3044 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3045 ASSERT_TRUE(annot);
3046
3047 // Checks for bad form handle.
3048 EXPECT_EQ(-1,
3049 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3050
3051 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3052 }
3053
3054 UnloadPage(page);
3055}
3056
3057TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
3058 // Open a file with checkbox widget annotations and load its first page.
3059 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3060 FPDF_PAGE page = LoadPage(0);
3061 ASSERT_TRUE(page);
3062
3063 {
3064 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3065 ASSERT_TRUE(annot);
3066 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3067 }
3068
3069 UnloadPage(page);
3070}
3071
3072TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
3073 // Open a file with ink annotations and load its first page.
3074 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3075 FPDF_PAGE page = LoadPage(0);
3076 ASSERT_TRUE(page);
3077
3078 {
3079 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3080 ASSERT_TRUE(annot);
3081 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3082 }
3083
3084 UnloadPage(page);
3085}
3086
3087TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
3088 // Open a file with radio button widget annotations and load its first page.
3089 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3090 FPDF_PAGE page = LoadPage(0);
3091 ASSERT_TRUE(page);
3092
3093 {
3094 // Checks for bad annot.
3095 EXPECT_EQ(-1,
3096 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3097
3098 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3099 ASSERT_TRUE(annot);
3100
3101 // Checks for bad form handle.
3102 EXPECT_EQ(-1,
3103 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3104
3105 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3106 }
3107
3108 UnloadPage(page);
3109}
3110
3111TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
3112 // Open a file with checkbox widget annotations and load its first page.
3113 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3114 FPDF_PAGE page = LoadPage(0);
3115 ASSERT_TRUE(page);
3116
3117 {
3118 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3119 ASSERT_TRUE(annot);
3120 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3121 }
3122
3123 UnloadPage(page);
3124}
3125
3126TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
3127 // Open a file with ink annotations and load its first page.
3128 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3129 FPDF_PAGE page = LoadPage(0);
3130 ASSERT_TRUE(page);
3131
3132 {
3133 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3134 ASSERT_TRUE(annot);
3135 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3136 }
3137
3138 UnloadPage(page);
3139}
3140
3141TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
3142 // Open a file with radio button widget annotations and load its first page.
3143 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3144 FPDF_PAGE page = LoadPage(0);
3145 ASSERT_TRUE(page);
3146
3147 {
3148 // Checks for bad annot.
3149 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3150 form_handle(), /*annot=*/nullptr,
3151 /*buffer=*/nullptr, /*buflen=*/0));
3152
3153 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3154 ASSERT_TRUE(annot);
3155
3156 // Checks for bad form handle.
3157 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3158 /*hHandle=*/nullptr, annot.get(),
3159 /*buffer=*/nullptr, /*buflen=*/0));
3160
3161 unsigned long length_bytes =
3162 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3163 /*buffer=*/nullptr, /*buflen=*/0);
3164 ASSERT_EQ(14u, length_bytes);
3165 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3166 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3167 buf.data(), length_bytes));
3168 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3169 }
3170
3171 UnloadPage(page);
3172}
3173
3174TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
3175 // Open a file with checkbox widget annotations and load its first page.
3176 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3177 FPDF_PAGE page = LoadPage(0);
3178 ASSERT_TRUE(page);
3179
3180 {
3181 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3182 ASSERT_TRUE(annot);
3183
3184 unsigned long length_bytes =
3185 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3186 /*buffer=*/nullptr, /*buflen=*/0);
3187 ASSERT_EQ(8u, length_bytes);
3188 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3189 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3190 buf.data(), length_bytes));
3191 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3192 }
3193
3194 UnloadPage(page);
3195}
3196
3197TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
3198 // Open a file with ink annotations and load its first page.
3199 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3200 FPDF_PAGE page = LoadPage(0);
3201 ASSERT_TRUE(page);
3202
3203 {
3204 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3205 ASSERT_TRUE(annot);
3206 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3207 /*buffer=*/nullptr,
3208 /*buflen=*/0));
3209 }
3210
3211 UnloadPage(page);
3212}