blob: 52d130d4b6231907e0617caa648edf6b26120371 [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
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00005#include <algorithm>
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00006#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -04007#include <memory>
8#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04009#include <vector>
10
Lei Zhange4cdac52019-04-30 16:45:57 +000011#include "build/build_config.h"
Lei Zhanga5c1daf2019-01-31 21:56:47 +000012#include "constants/annotation_common.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040013#include "core/fxcrt/fx_system.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000014#include "public/cpp/fpdf_scopers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040015#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040016#include "public/fpdf_edit.h"
Mansi Awasthi07bf7e62020-01-24 10:34:17 +000017#include "public/fpdf_formfill.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040018#include "public/fpdfview.h"
19#include "testing/embedder_test.h"
Hui Yingstb4baceb2020-04-28 23:46:10 +000020#include "testing/embedder_test_constants.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000021#include "testing/fx_string_testhelpers.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000022#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040023#include "testing/gtest/include/gtest/gtest.h"
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000024#include "testing/utils/hash.h"
Lei Zhang9b444002020-04-17 17:35:23 +000025#include "third_party/base/span.h"
Lei Zhangc9b72992020-04-22 01:03:52 +000026#include "third_party/base/stl_util.h"
Lei Zhang9b444002020-04-17 17:35:23 +000027
28namespace {
29
Hui Yingst4a21df02020-05-13 03:15:44 +000030#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
31#if defined(OS_LINUX)
32const char kAnnotationStampWithApChecksum[] =
33 "db83eaadc92967e3ac9bebfc6178ca75";
34#else
35const char kAnnotationStampWithApChecksum[] =
36 "3c87b4a8e51245964357fb5f5fbc612b";
37#endif // defined(OS_LINUX)
38#else
Hui Yingstb4baceb2020-04-28 23:46:10 +000039#if defined(OS_WIN)
40const char kAnnotationStampWithApChecksum[] =
41 "6aa001a77ec05d0f1b0d1d22e28744d4";
42#elif defined(OS_MACOSX)
43const char kAnnotationStampWithApChecksum[] =
44 "80d7b6cc7b13a78d77a6151bc846e80b";
45#else
46const char kAnnotationStampWithApChecksum[] =
47 "b42cef463483e668eaf4055a65e4f1f5";
48#endif
Hui Yingst4a21df02020-05-13 03:15:44 +000049#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstb4baceb2020-04-28 23:46:10 +000050
Lei Zhang9b444002020-04-17 17:35:23 +000051void VerifyFocusableAnnotSubtypes(
52 FPDF_FORMHANDLE form_handle,
53 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) {
54 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
55 FPDFAnnot_GetFocusableSubtypesCount(form_handle));
56
57 std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes(
58 expected_subtypes.size());
59 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(
60 form_handle, actual_subtypes.data(), actual_subtypes.size()));
61 for (size_t i = 0; i < expected_subtypes.size(); ++i)
62 ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]);
63}
64
65void SetAndVerifyFocusableAnnotSubtypes(
66 FPDF_FORMHANDLE form_handle,
67 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) {
68 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(),
69 subtypes.size()));
70 VerifyFocusableAnnotSubtypes(form_handle, subtypes);
71}
72
73void VerifyAnnotationSubtypesAndFocusability(
74 FPDF_FORMHANDLE form_handle,
75 FPDF_PAGE page,
76 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes,
77 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) {
78 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
79 FPDFPage_GetAnnotCount(page));
80 for (size_t i = 0; i < expected_subtypes.size(); ++i) {
81 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
82 ASSERT_TRUE(annot);
83 EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get()));
84
Lei Zhangf245ae62020-05-15 21:49:46 +000085 bool expected_focusable =
86 pdfium::Contains(expected_focusable_subtypes, expected_subtypes[i]);
Lei Zhang9b444002020-04-17 17:35:23 +000087 EXPECT_EQ(expected_focusable,
88 FORM_SetFocusedAnnot(form_handle, annot.get()));
89
90 // Kill the focus so the next test starts in an unfocused state.
91 FORM_ForceToKillFocus(form_handle);
92 }
93}
94
95} // namespace
Jane Liu4fd9a472017-06-01 18:56:09 -040096
Lei Zhangab41f252018-12-23 03:10:50 +000097class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040098
Lei Zhangab41f252018-12-23 03:10:50 +000099TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +0000100 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
101 FPDF_PAGE page = LoadPage(0);
102 ASSERT_TRUE(page);
103
104 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
105
106 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
107 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
108 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
109 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
110 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
111
112 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
113
114 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
115
116 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
117 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
118 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
119
120 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
121
Lei Zhang4f556b82019-04-08 16:32:41 +0000122 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +0000123 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +0000124 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
125
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000126 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +0000127 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
128 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
129 EXPECT_EQ(0u,
130 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
131
132 UnloadPage(page);
133}
134
Lei Zhang3d9a0972019-03-04 19:34:09 +0000135TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
136 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
137 FPDF_PAGE page = LoadPage(0);
138 ASSERT_TRUE(page);
139
140 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +0000141 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +0000142
143 UnloadPage(page);
144}
145
Lei Zhangab41f252018-12-23 03:10:50 +0000146TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -0400147 // Open a file with one annotation and load its first page.
148 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000149 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -0400150 ASSERT_TRUE(page);
151
152 // This annotation has a malformed appearance stream, which does not have its
153 // normal appearance defined, only its rollover appearance. In this case, its
154 // normal appearance should be generated, allowing the highlight annotation to
155 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000156 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000157 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -0400158
159 UnloadPage(page);
160}
161
Lei Zhang03e5e682019-09-16 19:45:55 +0000162// TODO(crbug.com/pdfium/11): Fix this test and enable.
163#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
164#define MAYBE_RenderMultilineMarkupAnnotWithoutAP \
165 DISABLED_RenderMultilineMarkupAnnotWithoutAP
166#else
167#define MAYBE_RenderMultilineMarkupAnnotWithoutAP \
168 RenderMultilineMarkupAnnotWithoutAP
169#endif
170TEST_F(FPDFAnnotEmbedderTest, MAYBE_RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang4f556b82019-04-08 16:32:41 +0000171 static const char kMd5[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +0000172 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000173 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
174 FPDF_PAGE page = LoadPage(0);
175 ASSERT_TRUE(page);
176
Tom Sepeze08d2b12018-04-25 18:49:32 +0000177 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000178 CompareBitmap(bitmap.get(), 595, 842, kMd5);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000179
180 UnloadPage(page);
181}
182
Lei Zhangab41f252018-12-23 03:10:50 +0000183TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400184 // Open a file with one annotation and load its first page.
185 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000186 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400187 ASSERT_TRUE(page);
188
189 // Check that there is a total of 1 annotation on its first page.
190 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
191
192 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000193 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000194 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000195 ASSERT_TRUE(annot);
196 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400197
Lei Zhanga21d5932018-02-05 18:28:38 +0000198 // Check that the annotation color is yellow.
199 unsigned int R;
200 unsigned int G;
201 unsigned int B;
202 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000203 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000204 &G, &B, &A));
205 EXPECT_EQ(255u, R);
206 EXPECT_EQ(255u, G);
207 EXPECT_EQ(0u, B);
208 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400209
Lei Zhanga21d5932018-02-05 18:28:38 +0000210 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000211 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000212 EXPECT_EQ(FPDF_OBJECT_STRING,
213 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000214 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000215 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000216 ASSERT_EQ(28u, length_bytes);
217 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000218 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000219 length_bytes));
220 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400221
Lei Zhanga21d5932018-02-05 18:28:38 +0000222 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000223 EXPECT_EQ(
224 FPDF_OBJECT_STRING,
225 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000226 length_bytes = FPDFAnnot_GetStringValue(
227 annot.get(), pdfium::annotation::kContents, nullptr, 0);
228 ASSERT_EQ(2690u, length_bytes);
229 buf = GetFPDFWideStringBuffer(length_bytes);
230 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
231 pdfium::annotation::kContents,
232 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000233 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000234 L"This is a note for that highlight annotation. Very long highlight "
235 "annotation. Long long long Long long longLong long longLong long "
236 "longLong long longLong long longLong long longLong long longLong long "
237 "longLong long longLong long longLong long longLong long longLong long "
238 "longLong long longLong long longLong long longLong long longLong long "
239 "longLong long longLong long longLong long longLong long longLong long "
240 "longLong long longLong long longLong long longLong long longLong long "
241 "longLong long longLong long longLong long longLong long longLong long "
242 "longLong long longLong long longLong long longLong long longLong long "
243 "longLong long longLong long longLong long longLong long longLong long "
244 "longLong long longLong long longLong long longLong long longLong long "
245 "longLong long longLong long longLong long longLong long longLong long "
246 "longLong long longLong long longLong long longLong long longLong long "
247 "longLong long longLong long longLong long longLong long longLong long "
248 "longLong long longLong long longLong long longLong long longLong long "
249 "longLong long longLong long longLong long longLong long longLong long "
250 "longLong long longLong long longLong long longLong long longLong long "
251 "longLong long longLong long longLong long longLong long longLong long "
252 "longLong long longLong long longLong long longLong long longLong long "
253 "longLong long long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000254 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400255
Lei Zhanga21d5932018-02-05 18:28:38 +0000256 // Check that the quadpoints are correct.
257 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000258 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000259 EXPECT_EQ(115.802643f, quadpoints.x1);
260 EXPECT_EQ(718.913940f, quadpoints.y1);
261 EXPECT_EQ(157.211182f, quadpoints.x4);
262 EXPECT_EQ(706.264465f, quadpoints.y4);
263 }
Tom Sepez507d0192018-11-07 16:37:51 +0000264 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400265}
266
Lei Zhang03e5e682019-09-16 19:45:55 +0000267// TODO(crbug.com/pdfium/11): Fix this test and enable.
268#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
269#define MAYBE_ExtractInkMultiple DISABLED_ExtractInkMultiple
270#else
271#define MAYBE_ExtractInkMultiple ExtractInkMultiple
272#endif
273TEST_F(FPDFAnnotEmbedderTest, MAYBE_ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400274 // Open a file with three annotations and load its first page.
275 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000276 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400277 ASSERT_TRUE(page);
278
279 // Check that there is a total of 3 annotation on its first page.
280 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
281
Lei Zhanga21d5932018-02-05 18:28:38 +0000282 {
283 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000284 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000285 ASSERT_TRUE(annot);
286 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400287
Lei Zhanga21d5932018-02-05 18:28:38 +0000288 // Check that the annotation color is blue with opacity.
289 unsigned int R;
290 unsigned int G;
291 unsigned int B;
292 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000293 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000294 &G, &B, &A));
295 EXPECT_EQ(0u, R);
296 EXPECT_EQ(0u, G);
297 EXPECT_EQ(255u, B);
298 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400299
Lei Zhanga21d5932018-02-05 18:28:38 +0000300 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000301 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
302 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400303
Lei Zhang4f556b82019-04-08 16:32:41 +0000304 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000305 // Note that upon rendering, the rectangle coordinates will be adjusted.
306 FS_RECTF rect;
307 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
308 EXPECT_EQ(351.820404f, rect.left);
309 EXPECT_EQ(583.830688f, rect.bottom);
310 EXPECT_EQ(475.336090f, rect.right);
311 EXPECT_EQ(681.535034f, rect.top);
312 }
Tom Sepezef43c262018-11-07 16:41:32 +0000313 {
314 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
315 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
316 }
Tom Sepez507d0192018-11-07 16:37:51 +0000317 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400318}
Jane Liu20eafda2017-06-07 10:33:24 -0400319
Lei Zhangab41f252018-12-23 03:10:50 +0000320TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400321 // Open a file with one annotation and load its first page.
322 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000323 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400324 ASSERT_TRUE(page);
325
326 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400327 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400328
329 UnloadPage(page);
330}
331
Lei Zhangab41f252018-12-23 03:10:50 +0000332TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400333 // Open a file with no annotation and load its first page.
334 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000335 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400336 ASSERT_TRUE(page);
337 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
338
Lei Zhanga21d5932018-02-05 18:28:38 +0000339 {
340 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000341 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000342 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400343
Lei Zhanga21d5932018-02-05 18:28:38 +0000344 // Check that there is now 1 annotations on this page.
345 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400346
Lei Zhanga21d5932018-02-05 18:28:38 +0000347 // Check that the subtype of the annotation is correct.
348 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
349 }
Jane Liue10509a2017-06-20 16:47:41 -0400350
Lei Zhanga21d5932018-02-05 18:28:38 +0000351 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000352 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000353 ASSERT_TRUE(annot);
354 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400355
Lei Zhanga21d5932018-02-05 18:28:38 +0000356 // Set the color of the annotation.
357 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
358 102, 153, 204));
359 // Check that the color has been set correctly.
360 unsigned int R;
361 unsigned int G;
362 unsigned int B;
363 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000364 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000365 &G, &B, &A));
366 EXPECT_EQ(51u, R);
367 EXPECT_EQ(102u, G);
368 EXPECT_EQ(153u, B);
369 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400370
Lei Zhanga21d5932018-02-05 18:28:38 +0000371 // Change the color of the annotation.
372 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
373 153, 102, 51));
374 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000375 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000376 &G, &B, &A));
377 EXPECT_EQ(204u, R);
378 EXPECT_EQ(153u, G);
379 EXPECT_EQ(102u, B);
380 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400381
Lei Zhanga21d5932018-02-05 18:28:38 +0000382 // Set the annotation rectangle.
383 FS_RECTF rect;
384 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
385 EXPECT_EQ(0.f, rect.left);
386 EXPECT_EQ(0.f, rect.right);
387 rect.left = 35;
388 rect.bottom = 150;
389 rect.right = 53;
390 rect.top = 165;
391 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
392 // Check that the annotation rectangle has been set correctly.
393 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
394 EXPECT_EQ(35.f, rect.left);
395 EXPECT_EQ(150.f, rect.bottom);
396 EXPECT_EQ(53.f, rect.right);
397 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400398
Lei Zhanga21d5932018-02-05 18:28:38 +0000399 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000400 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000401 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000402 ASSERT_TRUE(FPDFAnnot_SetStringValue(
403 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000404 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000405 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000406 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000407 ASSERT_EQ(74u, length_bytes);
408 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
409 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
410 pdfium::annotation::kContents,
411 buf.data(), length_bytes));
412 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000413 }
Jane Liu20eafda2017-06-07 10:33:24 -0400414 UnloadPage(page);
415}
416
Lei Zhang03e5e682019-09-16 19:45:55 +0000417// TODO(crbug.com/pdfium/11): Fix this test and enable.
418#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
419#define MAYBE_AddAndSaveUnderlineAnnotation \
420 DISABLED_AddAndSaveUnderlineAnnotation
421#else
422#define MAYBE_AddAndSaveUnderlineAnnotation AddAndSaveUnderlineAnnotation
423#endif
424TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400425 // Open a file with one annotation and load its first page.
426 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000427 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400428 ASSERT_TRUE(page);
429
430 // Check that there is a total of one annotation on its first page, and verify
431 // its quadpoints.
432 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400433 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000434 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000435 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000436 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000437 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000438 EXPECT_EQ(115.802643f, quadpoints.x1);
439 EXPECT_EQ(718.913940f, quadpoints.y1);
440 EXPECT_EQ(157.211182f, quadpoints.x4);
441 EXPECT_EQ(706.264465f, quadpoints.y4);
442 }
Jane Liu20eafda2017-06-07 10:33:24 -0400443
444 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000445 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000446 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000447 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
448 ASSERT_TRUE(annot);
449 quadpoints.x1 = 140.802643f;
450 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000451 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000452 }
Jane Liu20eafda2017-06-07 10:33:24 -0400453
454 // Save the document, closing the page and document.
455 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000456 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400457
458 // Open the saved document.
Lei Zhang4f556b82019-04-08 16:32:41 +0000459 static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400460
Lei Zhang0b494052019-01-31 21:41:15 +0000461 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000462 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000463 VerifySavedRendering(page, 612, 792, kMd5);
Jane Liu20eafda2017-06-07 10:33:24 -0400464
465 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000466 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400467
Lei Zhanga21d5932018-02-05 18:28:38 +0000468 {
469 // Check that the second annotation is an underline annotation and verify
470 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000471 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000472 ASSERT_TRUE(new_annot);
473 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
474 FS_QUADPOINTSF new_quadpoints;
475 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000476 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000477 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
478 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
479 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
480 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
481 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400482
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000483 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400484 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400485}
Jane Liu06462752017-06-27 16:41:14 -0400486
Lei Zhangab41f252018-12-23 03:10:50 +0000487TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000488 // Open a file with four annotations and load its first page.
489 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
490 FPDF_PAGE page = LoadPage(0);
491 ASSERT_TRUE(page);
492 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
493
494 // Retrieve the highlight annotation.
495 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
496 ASSERT_TRUE(annot);
497 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
498
499 FS_QUADPOINTSF quadpoints;
500 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
501
502 {
503 // Verify the current one set of quadpoints.
504 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
505
506 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
507 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
508 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
509 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
510 }
511
512 {
513 // Update the quadpoints.
514 FS_QUADPOINTSF new_quadpoints = quadpoints;
515 new_quadpoints.y1 -= 20.f;
516 new_quadpoints.y2 -= 20.f;
517 new_quadpoints.y3 -= 20.f;
518 new_quadpoints.y4 -= 20.f;
519 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
520
521 // Verify added quadpoint set
522 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
523 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
524 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
525 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
526 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
527 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
528 }
529
530 {
531 // Append a new set of quadpoints.
532 FS_QUADPOINTSF new_quadpoints = quadpoints;
533 new_quadpoints.y1 += 20.f;
534 new_quadpoints.y2 += 20.f;
535 new_quadpoints.y3 += 20.f;
536 new_quadpoints.y4 += 20.f;
537 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
538
539 // Verify added quadpoint set
540 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
541 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
542 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
543 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
544 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
545 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
546 }
547
548 {
549 // Setting and getting quadpoints at out-of-bound index should fail
550 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
551 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
552 }
553
554 FPDFPage_CloseAnnot(annot);
555
556 // Retrieve the square annotation
557 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
558
559 {
560 // Check that attempting to set its quadpoints would fail
561 ASSERT_TRUE(squareAnnot);
562 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
563 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
564 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
565 }
566
567 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000568 UnloadPage(page);
569}
570
Lei Zhang03e5e682019-09-16 19:45:55 +0000571// TODO(crbug.com/pdfium/11): Fix this test and enable.
572#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
573#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
574#else
575#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
576#endif
577TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000578#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +0000579 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
Lei Zhang4f556b82019-04-08 16:32:41 +0000580 static const char kMd5ModifiedHighlight[] =
Lei Zhang0f6b4342020-02-25 20:00:39 +0000581 "e64bf648f6e9354d1f3eedb47a2c9498";
582 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Lei Zhange67bcc72019-04-30 18:55:58 +0000583#elif defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000584 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
585 static const char kMd5ModifiedHighlight[] =
586 "66f3caef3a7d488a4fa1ad37fc06310e";
587 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400588#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000589 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
590 static const char kMd5ModifiedHighlight[] =
591 "66f3caef3a7d488a4fa1ad37fc06310e";
592 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400593#endif
594
Jane Liu06462752017-06-27 16:41:14 -0400595 // Open a file with four annotations and load its first page.
596 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000597 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400598 ASSERT_TRUE(page);
599 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
600
Jane Liub370e5a2017-08-16 13:24:58 -0400601 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000602 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000603 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000604 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000605 }
Jane Liub370e5a2017-08-16 13:24:58 -0400606
Jane Liu0c6b07d2017-08-15 10:50:22 -0400607 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400608 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000609
610 // Retrieve the highlight annotation which has its AP stream already defined.
611 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000612 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000613 ASSERT_TRUE(annot);
614 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
615
616 // Check that color cannot be set when an AP stream is defined already.
617 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
618 102, 153, 204));
619
620 // Verify its attachment points.
621 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000622 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000623 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
624 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
625 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
626 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
627
628 // Check that updating the attachment points would succeed.
629 quadpoints.x1 -= 50.f;
630 quadpoints.x2 -= 50.f;
631 quadpoints.x3 -= 50.f;
632 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000633 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000634 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000635 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000636 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
637 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
638 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
639 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
640
641 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000642 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000643 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000644 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000645 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000646
647 // Verify its annotation rectangle.
648 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
649 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
650 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
651 EXPECT_NEAR(136.325f, rect.right, 0.001f);
652 EXPECT_NEAR(721.292f, rect.top, 0.001f);
653
654 // Check that updating the rectangle would succeed.
655 rect.left -= 60.f;
656 rect.right -= 60.f;
657 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
658 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
659 EXPECT_EQ(rect.right, new_rect.right);
660 }
Jane Liu06462752017-06-27 16:41:14 -0400661
Jane Liub370e5a2017-08-16 13:24:58 -0400662 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000663 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000664 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000665 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000666 }
Jane Liub370e5a2017-08-16 13:24:58 -0400667
Lei Zhanga21d5932018-02-05 18:28:38 +0000668 {
669 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000670 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000671 ASSERT_TRUE(annot);
672 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400673
Lei Zhanga21d5932018-02-05 18:28:38 +0000674 // Check that updating the rectangle would succeed.
675 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
676 rect.left += 70.f;
677 rect.right += 70.f;
678 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
679 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
680 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400681
Lei Zhanga21d5932018-02-05 18:28:38 +0000682 // Check that updating the rectangle changes the square annotation's
683 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000684 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000685 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000686 }
Jane Liub370e5a2017-08-16 13:24:58 -0400687
Jane Liu06462752017-06-27 16:41:14 -0400688 UnloadPage(page);
689}
Jane Liu8ce58f52017-06-29 13:40:22 -0400690
Lei Zhangab41f252018-12-23 03:10:50 +0000691TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000692 // Open a file with multiline markup annotations.
693 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
694 FPDF_PAGE page = LoadPage(0);
695 ASSERT_TRUE(page);
696 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000697 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000698 ASSERT_TRUE(annot);
699
700 // This is a three line annotation.
701 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
702 }
703 UnloadPage(page);
704
705 // null annotation should return 0
706 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
707}
708
Lei Zhangab41f252018-12-23 03:10:50 +0000709TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400710 // Open a file with 3 annotations on its first page.
711 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000712 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400713 ASSERT_TRUE(page);
714 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
715
Jane Liu0c6b07d2017-08-15 10:50:22 -0400716 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400717
Lei Zhanga21d5932018-02-05 18:28:38 +0000718 // Check that the annotations have the expected rectangle coordinates.
719 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000720 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000721 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
722 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
723 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400724
Lei Zhanga21d5932018-02-05 18:28:38 +0000725 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000726 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000727 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
728 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
729 }
730
731 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000732 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000733 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
734 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
735 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400736
737 // Check that nothing happens when attempting to remove an annotation with an
738 // out-of-bound index.
739 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
740 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
741 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
742
743 // Remove the second annotation.
744 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
745 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
746 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
747
748 // Save the document, closing the page and document.
749 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000750 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400751
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400752 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400753 // Open the saved document.
754 std::string new_file = GetString();
755 FPDF_FILEACCESS file_access;
756 memset(&file_access, 0, sizeof(file_access));
757 file_access.m_FileLen = new_file.size();
758 file_access.m_GetBlock = GetBlockFromString;
759 file_access.m_Param = &new_file;
760 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
761 ASSERT_TRUE(new_doc);
762 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
763 ASSERT_TRUE(new_page);
764
765 // Check that the saved document has 2 annotations on the first page.
766 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
767
Lei Zhanga21d5932018-02-05 18:28:38 +0000768 // Check that the remaining 2 annotations are the original 1st and 3rd ones
769 // by verifying their rectangle coordinates.
770 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000771 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000772 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
773 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
774 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400775
Lei Zhanga21d5932018-02-05 18:28:38 +0000776 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000777 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000778 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
779 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
780 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400781 FPDF_ClosePage(new_page);
782 FPDF_CloseDocument(new_doc);
783}
Jane Liu8ce58f52017-06-29 13:40:22 -0400784
Hui Yingst4a21df02020-05-13 03:15:44 +0000785TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +0000786#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst4a21df02020-05-13 03:15:44 +0000787#if defined(OS_LINUX)
788 static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa";
789 static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622";
790 static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c";
Lei Zhang03e5e682019-09-16 19:45:55 +0000791#else
Hui Yingst4a21df02020-05-13 03:15:44 +0000792 static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4";
793 static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f";
794 static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006";
795#endif // defined(OS_LINUX)
796#else
797#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +0000798 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
799 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
800 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Hui Yingst4a21df02020-05-13 03:15:44 +0000801#elif defined(OS_MACOSX)
802 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
803 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
804 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Jane Liubaa7ff42017-06-29 19:18:23 -0400805#else
Lei Zhanga2b70732019-06-25 08:34:22 +0000806 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
807 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
808 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -0400809#endif
Hui Yingst4a21df02020-05-13 03:15:44 +0000810#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liubaa7ff42017-06-29 19:18:23 -0400811
812 // Open a file with two annotations and load its first page.
813 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000814 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400815 ASSERT_TRUE(page);
816 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
817
818 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000819 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000820 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +0000821 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000822 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400823
Lei Zhanga21d5932018-02-05 18:28:38 +0000824 {
825 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000826 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000827 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400828
Lei Zhanga21d5932018-02-05 18:28:38 +0000829 // Check that this annotation has one path object and retrieve it.
830 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000831 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000832 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
833 EXPECT_FALSE(path);
834 path = FPDFAnnot_GetObject(annot.get(), 0);
835 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
836 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400837
Lei Zhanga21d5932018-02-05 18:28:38 +0000838 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +0000839 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +0000840 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400841
Lei Zhanga21d5932018-02-05 18:28:38 +0000842 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000843 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000844 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000845 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000846 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400847
Lei Zhanga21d5932018-02-05 18:28:38 +0000848 // Add a second path object to the same annotation.
849 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
850 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +0000851 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
852 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +0000853 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
854 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
855 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400856
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000857 // The object is in the annontation, not in the page, so the page object
858 // array should not change.
859 ASSERT_EQ(32, FPDFPage_CountObjects(page));
860
Lei Zhanga21d5932018-02-05 18:28:38 +0000861 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000862 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000863 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000864 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000865 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400866
Lei Zhanga21d5932018-02-05 18:28:38 +0000867 // Delete the newly added path object.
868 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
869 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000870 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000871 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400872
873 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000874 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000875 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000876 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000877 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400878
Jane Liubaa7ff42017-06-29 19:18:23 -0400879 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400880
Lei Zhanga21d5932018-02-05 18:28:38 +0000881 {
882 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000883 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000884 ASSERT_TRUE(annot);
885 rect.left = 200.f;
886 rect.bottom = 400.f;
887 rect.right = 500.f;
888 rect.top = 600.f;
889 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400890
Lei Zhanga21d5932018-02-05 18:28:38 +0000891 // Add a new path to the annotation.
892 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
893 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
894 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
895 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
896 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +0000897 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
898 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +0000899 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
900 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
901 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
902
903 // Check that the annotation's bounding box came from its rectangle.
904 FS_RECTF new_rect;
905 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
906 EXPECT_EQ(rect.left, new_rect.left);
907 EXPECT_EQ(rect.bottom, new_rect.bottom);
908 EXPECT_EQ(rect.right, new_rect.right);
909 EXPECT_EQ(rect.top, new_rect.top);
910 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400911
912 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400913 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000914 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400915
916 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +0000917 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000918 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000919 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400920
Jane Liu36567742017-07-06 11:13:35 -0400921 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000922 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400923
Lei Zhanga21d5932018-02-05 18:28:38 +0000924 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000925 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000926 ASSERT_TRUE(annot);
927 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400928
Lei Zhanga21d5932018-02-05 18:28:38 +0000929 // Check that the new annotation's rectangle is as defined.
930 FS_RECTF new_rect;
931 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
932 EXPECT_EQ(rect.left, new_rect.left);
933 EXPECT_EQ(rect.bottom, new_rect.bottom);
934 EXPECT_EQ(rect.right, new_rect.right);
935 EXPECT_EQ(rect.top, new_rect.top);
936 }
937
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000938 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400939 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400940}
Jane Liub137e752017-07-05 15:04:33 -0400941
Lei Zhangab41f252018-12-23 03:10:50 +0000942TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400943 // Open a file with an annotation and load its first page.
944 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000945 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400946 ASSERT_TRUE(page);
947
948 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000949 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000950 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000951 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
952 }
Jane Liub137e752017-07-05 15:04:33 -0400953
Lei Zhanga21d5932018-02-05 18:28:38 +0000954 {
955 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000956 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000957 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400958
Lei Zhanga21d5932018-02-05 18:28:38 +0000959 // Check that the original flag values are as expected.
960 int flags = FPDFAnnot_GetFlags(annot.get());
961 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
962 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400963
Lei Zhanga21d5932018-02-05 18:28:38 +0000964 // Set the HIDDEN flag.
965 flags |= FPDF_ANNOT_FLAG_HIDDEN;
966 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
967 flags = FPDFAnnot_GetFlags(annot.get());
968 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
969 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400970
Lei Zhanga21d5932018-02-05 18:28:38 +0000971 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000972 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000973 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +0000974 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000975 }
Jane Liub137e752017-07-05 15:04:33 -0400976
Lei Zhanga21d5932018-02-05 18:28:38 +0000977 // Unset the HIDDEN flag.
978 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
979 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
980 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
981 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
982 flags = FPDFAnnot_GetFlags(annot.get());
983 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
984 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400985
Lei Zhanga21d5932018-02-05 18:28:38 +0000986 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000987 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000988 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000989 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
990 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000991 }
Jane Liub137e752017-07-05 15:04:33 -0400992
Jane Liub137e752017-07-05 15:04:33 -0400993 UnloadPage(page);
994}
Jane Liu36567742017-07-06 11:13:35 -0400995
Lei Zhang03e5e682019-09-16 19:45:55 +0000996// TODO(crbug.com/pdfium/11): Fix this test and enable.
997#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
998#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
999#else
1000#define MAYBE_AddAndModifyImage AddAndModifyImage
1001#endif
1002TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Lei Zhange4cdac52019-04-30 16:45:57 +00001003#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001004 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
1005 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +00001006#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001007 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
1008 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -04001009#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001010 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
1011 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -04001012#endif
1013
1014 // Open a file with two annotations and load its first page.
1015 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001016 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001017 ASSERT_TRUE(page);
1018 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1019
1020 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001021 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001022 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001023 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001024 }
Jane Liu36567742017-07-06 11:13:35 -04001025
Jane Liu36567742017-07-06 11:13:35 -04001026 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001027 FPDF_BITMAP image_bitmap;
1028
1029 {
1030 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001031 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001032 ASSERT_TRUE(annot);
1033 FS_RECTF rect;
1034 rect.left = 200.f;
1035 rect.bottom = 600.f;
1036 rect.right = 400.f;
1037 rect.top = 800.f;
1038 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1039
1040 // Add a solid-color translucent image object to the new annotation.
1041 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1042 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1043 0xeeeecccc);
1044 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1045 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1046 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1047 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1048 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1049 kBitmapSize, 0, 0));
1050 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1051 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1052 }
Jane Liu36567742017-07-06 11:13:35 -04001053
1054 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001055 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001056 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001057 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001058 }
Jane Liu36567742017-07-06 11:13:35 -04001059
Lei Zhanga21d5932018-02-05 18:28:38 +00001060 {
1061 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001062 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001063 ASSERT_TRUE(annot);
1064 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1065 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1066 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001067
Lei Zhanga21d5932018-02-05 18:28:38 +00001068 // Modify the image in the new annotation.
1069 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1070 0xff000000);
1071 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1072 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1073 }
Jane Liu36567742017-07-06 11:13:35 -04001074
1075 // Save the document, closing the page and document.
1076 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001077 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001078 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001079
1080 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001081 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001082}
1083
Hui Yingst6cd754f2020-05-14 04:05:25 +00001084TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001085#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001086#if defined(OS_LINUX)
1087 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1088 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001089#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001090 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1091 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
1092#endif // defined(OS_LINUX)
1093#else
1094#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001095 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1096 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Hui Yingst6cd754f2020-05-14 04:05:25 +00001097#elif defined(OS_MACOSX)
1098 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1099 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001100#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001101 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1102 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001103#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001104#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001105
1106 // Open a file with two annotations and load its first page.
1107 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001108 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001109 ASSERT_TRUE(page);
1110 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1111
1112 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001113 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001114 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001115 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001116 }
Jane Liu36567742017-07-06 11:13:35 -04001117
Lei Zhanga21d5932018-02-05 18:28:38 +00001118 {
1119 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001120 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001121 ASSERT_TRUE(annot);
1122 FS_RECTF rect;
1123 rect.left = 200.f;
1124 rect.bottom = 550.f;
1125 rect.right = 450.f;
1126 rect.top = 650.f;
1127 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001128
Lei Zhanga21d5932018-02-05 18:28:38 +00001129 // Add a translucent text object to the new annotation.
1130 FPDF_PAGEOBJECT text_object =
1131 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1132 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001133 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001134 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1135 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001136 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001137 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1138 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1139 }
Jane Liu36567742017-07-06 11:13:35 -04001140
1141 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001142 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001143 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001144 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001145 }
Jane Liu36567742017-07-06 11:13:35 -04001146
Lei Zhanga21d5932018-02-05 18:28:38 +00001147 {
1148 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001149 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001150 ASSERT_TRUE(annot);
1151 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1152 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1153 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001154
Lei Zhanga21d5932018-02-05 18:28:38 +00001155 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001156 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001157 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1158 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1159 }
Jane Liu36567742017-07-06 11:13:35 -04001160
1161 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001162 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001163 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001164 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001165 }
Jane Liu36567742017-07-06 11:13:35 -04001166
1167 // Remove the new annotation, and check that the page renders as before.
1168 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001169 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001170 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001171 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001172 }
Jane Liu36567742017-07-06 11:13:35 -04001173
1174 UnloadPage(page);
1175}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001176
Lei Zhang03e5e682019-09-16 19:45:55 +00001177// TODO(crbug.com/pdfium/11): Fix this test and enable.
1178#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1179#define MAYBE_GetSetStringValue DISABLED_GetSetStringValue
1180#else
1181#define MAYBE_GetSetStringValue GetSetStringValue
1182#endif
1183TEST_F(FPDFAnnotEmbedderTest, MAYBE_GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001184 // Open a file with four annotations and load its first page.
1185 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001186 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001187 ASSERT_TRUE(page);
1188
Lei Zhang4f556b82019-04-08 16:32:41 +00001189 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001190
Lei Zhanga21d5932018-02-05 18:28:38 +00001191 {
1192 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001193 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001194 ASSERT_TRUE(annot);
1195
1196 // Check that a non-existent key does not exist.
1197 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1198
1199 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001200 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001201 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001202 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1203 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1204 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001205
1206 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001207 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001208 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001209 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001210 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001211 ASSERT_EQ(66u, length_bytes);
1212 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1213 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1214 length_bytes));
1215 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1216 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001217
1218 // Check that the string value of the modified date is correct.
1219 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001220 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1221 nullptr, 0);
1222 ASSERT_EQ(44u, length_bytes);
1223 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001224 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001225 buf.data(), length_bytes));
1226 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001227
1228 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001229 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001230 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1231 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001232 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001233
1234 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001235 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001236 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001237
Lei Zhange4cdac52019-04-30 16:45:57 +00001238#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001239 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001240#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001241 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001242#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001243 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001244#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001245
1246 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001247 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001248 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001249 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001250 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001251 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001252
Lei Zhanga21d5932018-02-05 18:28:38 +00001253 // Check that the string value of the modified date is the newly-set value.
1254 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001255 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001256 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001257 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001258 ASSERT_EQ(44u, length_bytes);
1259 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001260 EXPECT_EQ(44u,
1261 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001262 buf.data(), length_bytes));
1263 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001264 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001265
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001266 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001267 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001268}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001269
rycsmith3e785602019-03-05 21:48:36 +00001270TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001271 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001272 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1273 FPDF_PAGE page = LoadPage(0);
1274 ASSERT_TRUE(page);
1275 {
1276 // First two annotations do not have "MaxLen" attribute.
1277 for (int i = 0; i < 2; i++) {
1278 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1279 ASSERT_TRUE(annot);
1280
1281 // Verify that no "MaxLen" key present.
1282 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1283
1284 float value;
1285 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1286 }
1287
1288 // Annotation in index 2 has "MaxLen" of 10.
1289 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1290 ASSERT_TRUE(annot);
1291
1292 // Verify that "MaxLen" key present.
1293 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1294
1295 float value;
1296 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1297 EXPECT_FLOAT_EQ(10.0f, value);
1298
1299 // Check bad inputs.
1300 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1301 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1302 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1303 // Ask for key that exists but is not a number.
1304 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1305 }
1306
1307 UnloadPage(page);
1308}
1309
Lei Zhangab41f252018-12-23 03:10:50 +00001310TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001311 // Open a file with four annotations and load its first page.
1312 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001313 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001314 ASSERT_TRUE(page);
1315
Lei Zhanga21d5932018-02-05 18:28:38 +00001316 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001317 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1318 static constexpr size_t kExpectNormalAPLength = 73970;
1319
Lei Zhanga21d5932018-02-05 18:28:38 +00001320 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001321 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001322 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001323
Lei Zhanga21d5932018-02-05 18:28:38 +00001324 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001325 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001326 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001327 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001328
Lei Zhanga21d5932018-02-05 18:28:38 +00001329 // Check that the string value of an AP is not returned if the buffer is too
1330 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001331 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1332 // Write in the buffer to verify it's not overwritten.
1333 memcpy(buf.data(), "abcdefgh", 8);
1334 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001335 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001336 buf.data(), normal_length_bytes - 1));
1337 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001338
Lei Zhanga21d5932018-02-05 18:28:38 +00001339 // Check that the string value of an AP is returned through a buffer that is
1340 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001341 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001342 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001343 buf.data(), normal_length_bytes));
1344 EXPECT_EQ(kMd5NormalAP,
1345 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1346 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001347
Lei Zhanga21d5932018-02-05 18:28:38 +00001348 // Check that the string value of an AP is returned through a buffer that is
1349 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001350 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1351 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001352 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001353 buf.data(), normal_length_bytes + 2));
1354 EXPECT_EQ(kMd5NormalAP,
1355 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1356 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001357
Lei Zhanga21d5932018-02-05 18:28:38 +00001358 // Check that getting an AP for a mode that does not have an AP returns an
1359 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001360 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001361 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001362 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001363
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001364 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001365 EXPECT_EQ(2u,
1366 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001367 buf.data(), 1000));
1368 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001369
Lei Zhanga21d5932018-02-05 18:28:38 +00001370 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001371 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001372 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001373 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001374 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001375 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001376 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001377
Lei Zhanga21d5932018-02-05 18:28:38 +00001378 // Set the AP correctly now.
1379 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001380 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001381
Lei Zhanga21d5932018-02-05 18:28:38 +00001382 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001383 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001384 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001385 ASSERT_EQ(24u, rollover_length_bytes);
1386 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001387 EXPECT_EQ(24u,
1388 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001389 buf.data(), rollover_length_bytes));
1390 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001391
Lei Zhanga21d5932018-02-05 18:28:38 +00001392 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001393 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1394 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001395 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001396 buf.data(), normal_length_bytes));
1397 EXPECT_EQ(kMd5NormalAP,
1398 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1399 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001400 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001401
1402 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001403 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001404 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001405
Lei Zhang0b494052019-01-31 21:41:15 +00001406 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001407 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001408 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001409 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001410
Lei Zhanga21d5932018-02-05 18:28:38 +00001411 // Check that the new annotation value is equal to the value we set before
1412 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001413 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001414 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001415 ASSERT_EQ(24u, rollover_length_bytes);
1416 std::vector<FPDF_WCHAR> buf =
1417 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001418 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1419 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001420 buf.data(), rollover_length_bytes));
1421 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001422 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001423
1424 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001425 CloseSavedPage(page);
1426 CloseSavedDocument();
1427}
1428
Lei Zhangab41f252018-12-23 03:10:50 +00001429TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001430 // Open a file with four annotations and load its first page.
1431 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001432 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001433 ASSERT_TRUE(page);
1434
Lei Zhanga21d5932018-02-05 18:28:38 +00001435 {
1436 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001437 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001438 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001439
Lei Zhanga21d5932018-02-05 18:28:38 +00001440 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001441 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001442 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001443 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001444 EXPECT_EQ(73970u,
1445 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1446 nullptr, 0));
1447 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1448 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001449
Lei Zhanga21d5932018-02-05 18:28:38 +00001450 // Check that setting the Down AP to null removes the Down entry but keeps
1451 // Normal intact.
1452 EXPECT_TRUE(
1453 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1454 EXPECT_EQ(73970u,
1455 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1456 nullptr, 0));
1457 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1458 nullptr, 0));
1459 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001460
Lei Zhang75c81712018-02-08 17:22:39 +00001461 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001462}
1463
Lei Zhangab41f252018-12-23 03:10:50 +00001464TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001465 // Open a file with four annotations and load its first page.
1466 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001467 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001468 ASSERT_TRUE(page);
1469
Lei Zhanga21d5932018-02-05 18:28:38 +00001470 {
1471 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001472 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001473 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001474
Lei Zhanga21d5932018-02-05 18:28:38 +00001475 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001476 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001477 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001478 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001479 EXPECT_EQ(73970u,
1480 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1481 nullptr, 0));
1482 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1483 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001484
Lei Zhanga21d5932018-02-05 18:28:38 +00001485 // Check that setting the Normal AP to null removes the whole AP dictionary.
1486 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1487 nullptr));
1488 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1489 nullptr, 0));
1490 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1491 nullptr, 0));
1492 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001493
Lei Zhang75c81712018-02-08 17:22:39 +00001494 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001495}
1496
Lei Zhangab41f252018-12-23 03:10:50 +00001497TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001498 // Open a file with annotations and load its first page.
1499 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001500 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001501 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001502 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001503
Lei Zhanga21d5932018-02-05 18:28:38 +00001504 {
1505 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001506 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001507 ASSERT_TRUE(annot);
1508 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1509 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001510 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001511 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1512 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1513 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001514
Lei Zhanga21d5932018-02-05 18:28:38 +00001515 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001516 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001517 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1518 ASSERT_TRUE(popup);
1519 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1520 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1521 FS_RECTF rect;
1522 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1523 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1524 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001525
Lei Zhanga21d5932018-02-05 18:28:38 +00001526 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1527 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001528 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001529 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1530 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001531
Lei Zhanga21d5932018-02-05 18:28:38 +00001532 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1533 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001534 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001535 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001536 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1537 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001538 }
Jane Liu300bb272017-08-21 14:37:53 -04001539
Jane Liu300bb272017-08-21 14:37:53 -04001540 UnloadPage(page);
1541}
1542
Lei Zhangab41f252018-12-23 03:10:50 +00001543TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001544 // Open file with form text fields.
1545 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001546 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001547 ASSERT_TRUE(page);
1548
Lei Zhanga21d5932018-02-05 18:28:38 +00001549 {
1550 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001551 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001552 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001553
Lei Zhanga21d5932018-02-05 18:28:38 +00001554 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001555 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001556 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001557 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001558 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001559
Lei Zhanga21d5932018-02-05 18:28:38 +00001560 {
1561 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001562 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001563 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001564
Lei Zhanga21d5932018-02-05 18:28:38 +00001565 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001566 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001567 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001568 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1569 }
1570
1571 {
1572 // Retrieve the fourth annotation: user-editable password text field.
1573 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1574 ASSERT_TRUE(annot);
1575
1576 // Check that the flag values are as expected.
1577 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1578 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1579 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001580 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001581
1582 UnloadPage(page);
1583}
1584
Lei Zhangab41f252018-12-23 03:10:50 +00001585TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001586 // Open file with form text fields.
1587 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001588 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001589 ASSERT_TRUE(page);
1590
Lei Zhanga21d5932018-02-05 18:28:38 +00001591 {
1592 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001593 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001594 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001595
Lei Zhanga21d5932018-02-05 18:28:38 +00001596 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001597 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001598 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1599 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1600 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1601 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001602
Lei Zhanga21d5932018-02-05 18:28:38 +00001603 {
1604 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001605 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001606 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001607
Lei Zhanga21d5932018-02-05 18:28:38 +00001608 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001609 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001610 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1611 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1612 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1613 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001614
Lei Zhanga21d5932018-02-05 18:28:38 +00001615 {
1616 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001617 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001618 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001619
Lei Zhanga21d5932018-02-05 18:28:38 +00001620 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001621 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001622 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1623 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1624 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1625 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001626
1627 UnloadPage(page);
1628}
Diana Gage40870db2017-07-19 18:16:03 -07001629
Lei Zhangab41f252018-12-23 03:10:50 +00001630TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001631 // Open file with form text fields.
1632 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1633 FPDF_PAGE page = LoadPage(0);
1634 ASSERT_TRUE(page);
1635
1636 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001637 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1638 EXPECT_FALSE(
1639 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001640
Lei Zhang8da98232019-12-11 23:29:33 +00001641 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001642 {
1643 // Verify there is an annotation.
1644 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001645 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001646 EXPECT_TRUE(annot);
1647 }
1648
1649 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001650 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1651 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1652 EXPECT_FALSE(
1653 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001654
1655 UnloadPage(page);
1656}
1657
Lei Zhangab41f252018-12-23 03:10:50 +00001658TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001659 // Open file with form text fields.
1660 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1661 FPDF_PAGE page = LoadPage(0);
1662 ASSERT_TRUE(page);
1663
Lei Zhanga21d5932018-02-05 18:28:38 +00001664 {
1665 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001666 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001667 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001668 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001669 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001670
Lei Zhanga21d5932018-02-05 18:28:38 +00001671 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001672 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001673 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1674 }
Diana Gage40870db2017-07-19 18:16:03 -07001675
Lei Zhanga21d5932018-02-05 18:28:38 +00001676 {
1677 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001678 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001679 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001680 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001681 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001682
Lei Zhanga21d5932018-02-05 18:28:38 +00001683 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001684 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001685 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1686 }
Diana Gage40870db2017-07-19 18:16:03 -07001687
1688 UnloadPage(page);
1689}
1690
Lei Zhangab41f252018-12-23 03:10:50 +00001691TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001692 // Open file with form comboboxes.
1693 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1694 FPDF_PAGE page = LoadPage(0);
1695 ASSERT_TRUE(page);
1696
Lei Zhanga21d5932018-02-05 18:28:38 +00001697 {
1698 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001699 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001700 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001701 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001702 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001703
Lei Zhanga21d5932018-02-05 18:28:38 +00001704 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001705 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001706 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1707 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1708 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1709 }
Diana Gage40870db2017-07-19 18:16:03 -07001710
Lei Zhanga21d5932018-02-05 18:28:38 +00001711 {
1712 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001713 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001714 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001715 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001716 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001717
Lei Zhanga21d5932018-02-05 18:28:38 +00001718 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001719 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001720 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1721 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1722 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1723 }
Diana Gage40870db2017-07-19 18:16:03 -07001724
Lei Zhanga21d5932018-02-05 18:28:38 +00001725 {
1726 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001727 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001728 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001729 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001730 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001731
Lei Zhanga21d5932018-02-05 18:28:38 +00001732 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001733 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001734 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1735 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1736 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1737 }
Diana Gage40870db2017-07-19 18:16:03 -07001738
1739 UnloadPage(page);
1740}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001741
Lei Zhang03e5e682019-09-16 19:45:55 +00001742// TODO(crbug.com/pdfium/11): Fix this test and enable.
1743#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1744#define MAYBE_BUG_1206 DISABLED_BUG_1206
1745#else
1746#define MAYBE_BUG_1206 BUG_1206
1747#endif
1748TEST_F(FPDFAnnotEmbedderTest, MAYBE_BUG_1206) {
Lei Zhang992e7e22019-02-04 19:20:58 +00001749 static constexpr size_t kExpectedSize = 1609;
1750 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1751
1752 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1753
1754 FPDF_PAGE page = LoadPage(0);
1755 ASSERT_TRUE(page);
1756
1757 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1758 EXPECT_EQ(kExpectedSize, GetString().size());
1759 ClearString();
1760
1761 for (size_t i = 0; i < 10; ++i) {
1762 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1763 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
1764
1765 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1766 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
1767 // equal, not bigger.
1768 EXPECT_LT(kExpectedSize, GetString().size());
1769 ClearString();
1770 }
1771
1772 UnloadPage(page);
1773}
1774
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001775TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1776 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1777 FPDF_PAGE page = LoadPage(0);
1778 ASSERT_TRUE(page);
1779 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1780
1781 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00001782 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001783 static const size_t kBufSize = 12;
1784 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001785
1786 {
1787 // Add a text annotation to the page.
1788 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1789 ASSERT_TRUE(annot);
1790 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1791 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1792
1793 // Make sure there is no test key, add set a value there, and read it back.
1794 std::fill(buf.begin(), buf.end(), 'x');
1795 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001796 kBufSize));
1797 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001798
Lei Zhangf0f67682019-04-08 17:03:21 +00001799 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001800 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1801
1802 std::fill(buf.begin(), buf.end(), 'x');
1803 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001804 kBufSize));
1805 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001806 }
1807
Lei Zhang05ec64c2019-01-09 03:00:06 +00001808 {
1809 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1810 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00001811 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00001812 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001813 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1814 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1815 // Also do the same test for its appearance string.
1816 std::fill(buf.begin(), buf.end(), 'x');
1817 ASSERT_EQ(2u,
1818 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001819 buf.data(), kBufSize));
1820 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001821
Lei Zhangf0f67682019-04-08 17:03:21 +00001822 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00001823 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1824 text.get()));
1825
1826 std::fill(buf.begin(), buf.end(), 'x');
1827 ASSERT_EQ(6u,
1828 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001829 buf.data(), kBufSize));
1830 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001831 }
1832
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001833 UnloadPage(page);
1834
1835 {
1836 // Save a copy, open the copy, and check the annotation again.
1837 // Note that it renders the rotation.
1838 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001839 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001840 FPDF_PAGE saved_page = LoadSavedPage(0);
1841 ASSERT_TRUE(saved_page);
1842
Lei Zhang05ec64c2019-01-09 03:00:06 +00001843 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001844 {
1845 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1846 ASSERT_TRUE(annot);
1847 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1848
1849 std::fill(buf.begin(), buf.end(), 'x');
1850 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001851 kBufSize));
1852 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001853 }
1854
Lei Zhang05ec64c2019-01-09 03:00:06 +00001855 {
1856 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1857 ASSERT_TRUE(annot);
1858 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1859 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1860
1861 std::fill(buf.begin(), buf.end(), 'x');
1862 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001863 kBufSize));
1864 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001865 }
1866
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001867 CloseSavedPage(saved_page);
1868 CloseSavedDocument();
1869 }
1870}
rycsmithcb752f32019-02-21 18:40:53 +00001871
1872TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
1873 // Open a file with combobox widget annotations and load its first page.
1874 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1875 FPDF_PAGE page = LoadPage(0);
1876 ASSERT_TRUE(page);
1877
1878 {
1879 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1880 ASSERT_TRUE(annot);
1881
1882 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1883
1884 annot.reset(FPDFPage_GetAnnot(page, 1));
1885 ASSERT_TRUE(annot);
1886
1887 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00001888
1889 // Check bad form handle / annot.
1890 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
1891 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
1892 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00001893 }
1894
1895 UnloadPage(page);
1896}
1897
1898TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
1899 // Open a file with listbox widget annotations and load its first page.
1900 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1901 FPDF_PAGE page = LoadPage(0);
1902 ASSERT_TRUE(page);
1903
1904 {
1905 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1906 ASSERT_TRUE(annot);
1907
1908 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1909
1910 annot.reset(FPDFPage_GetAnnot(page, 1));
1911 ASSERT_TRUE(annot);
1912
1913 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1914 }
1915
1916 UnloadPage(page);
1917}
1918
1919TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
1920 // Open a file with ink annotations and load its first page.
1921 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1922 FPDF_PAGE page = LoadPage(0);
1923 ASSERT_TRUE(page);
1924
1925 {
1926 // annotations do not have "Opt" array and will return -1
1927 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1928 ASSERT_TRUE(annot);
1929
1930 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1931
1932 annot.reset(FPDFPage_GetAnnot(page, 1));
1933 ASSERT_TRUE(annot);
1934
1935 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1936 }
1937
1938 UnloadPage(page);
1939}
1940
1941TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
1942 // Open a file with combobox widget annotations and load its first page.
1943 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1944 FPDF_PAGE page = LoadPage(0);
1945 ASSERT_TRUE(page);
1946
1947 {
1948 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1949 ASSERT_TRUE(annot);
1950
1951 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001952 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001953 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001954 ASSERT_EQ(8u, length_bytes);
1955 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001956 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001957 buf.data(), length_bytes));
1958 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001959
1960 annot.reset(FPDFPage_GetAnnot(page, 1));
1961 ASSERT_TRUE(annot);
1962
1963 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001964 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001965 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001966 ASSERT_EQ(12u, length_bytes);
1967 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001968 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001969 buf.data(), length_bytes));
1970 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001971
1972 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001973 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001974 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001975 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001976 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001977 buf.data(), length_bytes));
1978 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001979
Lei Zhange7033c82019-02-26 19:30:49 +00001980 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00001981 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1982 nullptr, 0));
1983 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1984 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00001985
1986 // Check bad form handle / annot.
1987 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
1988 EXPECT_EQ(0u,
1989 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
1990 EXPECT_EQ(0u,
1991 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00001992 }
1993
1994 UnloadPage(page);
1995}
1996
1997TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
1998 // Open a file with listbox widget annotations and load its first page.
1999 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2000 FPDF_PAGE page = LoadPage(0);
2001 ASSERT_TRUE(page);
2002
2003 {
2004 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2005 ASSERT_TRUE(annot);
2006
2007 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002008 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002009 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002010 ASSERT_EQ(8u, length_bytes);
2011 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002012 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002013 buf.data(), length_bytes));
2014 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002015
2016 annot.reset(FPDFPage_GetAnnot(page, 1));
2017 ASSERT_TRUE(annot);
2018
2019 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002020 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002021 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002022 ASSERT_EQ(12u, length_bytes);
2023 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002024 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002025 buf.data(), length_bytes));
2026 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002027
2028 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002029 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002030 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002031 ASSERT_EQ(18u, length_bytes);
2032 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002033 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002034 buf.data(), length_bytes));
2035 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002036
2037 // indices out of range
2038 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2039 nullptr, 0));
2040 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2041 nullptr, 0));
2042 }
2043
2044 UnloadPage(page);
2045}
2046
2047TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2048 // Open a file with ink annotations and load its first page.
2049 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2050 FPDF_PAGE page = LoadPage(0);
2051 ASSERT_TRUE(page);
2052
2053 {
2054 // annotations do not have "Opt" array and will return 0
2055 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2056 ASSERT_TRUE(annot);
2057
2058 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2059 nullptr, 0));
2060
2061 annot.reset(FPDFPage_GetAnnot(page, 1));
2062 ASSERT_TRUE(annot);
2063
2064 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2065 nullptr, 0));
2066 }
2067
2068 UnloadPage(page);
2069}
Ryan Smith09c23b12019-04-25 18:09:06 +00002070
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002071TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2072 // Open a file with combobox widget annotations and load its first page.
2073 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2074 FPDF_PAGE page = LoadPage(0);
2075 ASSERT_TRUE(page);
2076
2077 {
2078 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2079 ASSERT_TRUE(annot);
2080
2081 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2082 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2083 ASSERT_EQ(3, count);
2084 for (int i = 0; i < count; i++) {
2085 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2086 }
2087
2088 annot.reset(FPDFPage_GetAnnot(page, 1));
2089 ASSERT_TRUE(annot);
2090
2091 // Checks for Combobox with Values (/V) object which is just a string.
2092 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2093 ASSERT_EQ(26, count);
2094 for (int i = 0; i < count; i++) {
2095 EXPECT_EQ(i == 1,
2096 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2097 }
2098
2099 // Checks for index outside bound i.e. (index >= CountOption()).
2100 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2101 /*index=*/26));
2102 // Checks for negetive index.
2103 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2104 /*index=*/-1));
2105
2106 // Checks for bad form handle/annot.
2107 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2108 EXPECT_FALSE(
2109 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2110 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2111 }
2112
2113 UnloadPage(page);
2114}
2115
2116TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2117 // Open a file with listbox widget annotations and load its first page.
2118 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2119 FPDF_PAGE page = LoadPage(0);
2120 ASSERT_TRUE(page);
2121
2122 {
2123 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2124 ASSERT_TRUE(annot);
2125
2126 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2127 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2128 ASSERT_EQ(3, count);
2129 for (int i = 0; i < count; i++) {
2130 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2131 }
2132
2133 annot.reset(FPDFPage_GetAnnot(page, 1));
2134 ASSERT_TRUE(annot);
2135
2136 // Checks for Listbox with Values (/V) object which is just a string.
2137 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2138 ASSERT_EQ(26, count);
2139 for (int i = 0; i < count; i++) {
2140 EXPECT_EQ(i == 1,
2141 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2142 }
2143
2144 annot.reset(FPDFPage_GetAnnot(page, 3));
2145 ASSERT_TRUE(annot);
2146
2147 // Checks for Listbox with only Selected indices (/I) object which is an
2148 // array with multiple objects.
2149 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2150 ASSERT_EQ(5, count);
2151 for (int i = 0; i < count; i++) {
2152 bool expected = (i == 1 || i == 3);
2153 EXPECT_EQ(expected,
2154 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2155 }
2156
2157 annot.reset(FPDFPage_GetAnnot(page, 4));
2158 ASSERT_TRUE(annot);
2159
2160 // Checks for Listbox with Values (/V) object which is an array with
2161 // multiple objects.
2162 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2163 ASSERT_EQ(5, count);
2164 for (int i = 0; i < count; i++) {
2165 bool expected = (i == 2 || i == 4);
2166 EXPECT_EQ(expected,
2167 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2168 }
2169
2170 annot.reset(FPDFPage_GetAnnot(page, 5));
2171 ASSERT_TRUE(annot);
2172
2173 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2174 // objects conflict with different lengths.
2175 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2176 ASSERT_EQ(5, count);
2177 for (int i = 0; i < count; i++) {
2178 bool expected = (i == 0 || i == 2);
2179 EXPECT_EQ(expected,
2180 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2181 }
2182 }
2183
2184 UnloadPage(page);
2185}
2186
2187TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2188 // Open a file with multiple form field annotations and load its first page.
2189 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2190 FPDF_PAGE page = LoadPage(0);
2191 ASSERT_TRUE(page);
2192
2193 {
2194 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2195 ASSERT_TRUE(annot);
2196
2197 // Checks for link annotation.
2198 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2199 /*index=*/0));
2200
2201 annot.reset(FPDFPage_GetAnnot(page, 3));
2202 ASSERT_TRUE(annot);
2203
2204 // Checks for text field annotation.
2205 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2206 /*index=*/0));
2207 }
2208
2209 UnloadPage(page);
2210}
2211
Ryan Smith09c23b12019-04-25 18:09:06 +00002212TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2213 // Open a file with combobox annotations and load its first page.
2214 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2215 FPDF_PAGE page = LoadPage(0);
2216 ASSERT_TRUE(page);
2217
2218 {
2219 // All 3 widgets have Tf font size 12.
2220 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2221 ASSERT_TRUE(annot);
2222
2223 float value;
2224 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2225 EXPECT_EQ(12.0, value);
2226
2227 annot.reset(FPDFPage_GetAnnot(page, 1));
2228 ASSERT_TRUE(annot);
2229
2230 float value_two;
2231 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2232 EXPECT_EQ(12.0, value_two);
2233
2234 annot.reset(FPDFPage_GetAnnot(page, 2));
2235 ASSERT_TRUE(annot);
2236
2237 float value_three;
2238 ASSERT_TRUE(
2239 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2240 EXPECT_EQ(12.0, value_three);
2241 }
2242
2243 UnloadPage(page);
2244}
2245
2246TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2247 // Open a file with textfield annotations and load its first page.
2248 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2249 FPDF_PAGE page = LoadPage(0);
2250 ASSERT_TRUE(page);
2251
2252 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002253 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002254 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2255 ASSERT_TRUE(annot);
2256
2257 float value;
2258 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2259 EXPECT_EQ(12.0, value);
2260
2261 annot.reset(FPDFPage_GetAnnot(page, 1));
2262 ASSERT_TRUE(annot);
2263
2264 float value_two;
2265 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2266 EXPECT_EQ(12.0, value_two);
2267
2268 annot.reset(FPDFPage_GetAnnot(page, 2));
2269 ASSERT_TRUE(annot);
2270
2271 float value_three;
2272 ASSERT_TRUE(
2273 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2274 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002275
2276 float value_four;
2277 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2278 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002279 }
2280
2281 UnloadPage(page);
2282}
2283
2284TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2285 // Open a file with ink annotations and load its first page.
2286 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2287 FPDF_PAGE page = LoadPage(0);
2288 ASSERT_TRUE(page);
2289
2290 {
2291 // Annotations that do not have variable text and will return -1.
2292 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2293 ASSERT_TRUE(annot);
2294
2295 float value;
2296 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2297
2298 annot.reset(FPDFPage_GetAnnot(page, 1));
2299 ASSERT_TRUE(annot);
2300
2301 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2302 }
2303
2304 UnloadPage(page);
2305}
2306
2307TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2308 // Open a file with combobox annotations and load its first page.
2309 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2310 FPDF_PAGE page = LoadPage(0);
2311 ASSERT_TRUE(page);
2312
2313 {
2314 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2315 ASSERT_TRUE(annot);
2316
2317 // Check bad form handle / annot.
2318 float value;
2319 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2320 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2321 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2322 }
2323
2324 UnloadPage(page);
2325}
2326
2327TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2328 // Open a file with textfield annotations and load its first page.
2329 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2330 FPDF_PAGE page = LoadPage(0);
2331 ASSERT_TRUE(page);
2332
2333 {
2334 // Obtain the first annotation, a text field with negative font size, -12.
2335 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2336 ASSERT_TRUE(annot);
2337
2338 float value;
2339 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2340 EXPECT_EQ(-12.0, value);
2341 }
2342
2343 UnloadPage(page);
2344}
Ryan Smith23fdf892019-07-17 21:51:26 +00002345
2346TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2347 // Open a file with checkbox and radiobuttons widget annotations and load its
2348 // first page.
2349 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2350 FPDF_PAGE page = LoadPage(0);
2351 ASSERT_TRUE(page);
2352
2353 {
2354 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2355 ASSERT_TRUE(annot);
2356 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2357 }
2358
2359 UnloadPage(page);
2360}
2361
2362TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2363 // Open a file with checkbox and radiobutton widget annotations and load its
2364 // first page.
2365 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2366 FPDF_PAGE page = LoadPage(0);
2367 ASSERT_TRUE(page);
2368
2369 {
2370 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2371 ASSERT_TRUE(annot);
2372 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2373 }
2374
2375 UnloadPage(page);
2376}
2377
2378TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2379 // Open a file with checkbox and radiobutton widget annotations and load its
2380 // first page.
2381 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2382 FPDF_PAGE page = LoadPage(0);
2383 ASSERT_TRUE(page);
2384
2385 {
2386 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2387 ASSERT_TRUE(annot);
2388 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2389
2390 annot.reset(FPDFPage_GetAnnot(page, 6));
2391 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2392
2393 annot.reset(FPDFPage_GetAnnot(page, 7));
2394 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2395 }
2396
2397 UnloadPage(page);
2398}
2399
2400TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2401 // Open a file with checkbox and radiobutton widget annotations and load its
2402 // first page.
2403 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2404 FPDF_PAGE page = LoadPage(0);
2405 ASSERT_TRUE(page);
2406
2407 {
2408 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2409 ASSERT_TRUE(annot);
2410 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2411
2412 annot.reset(FPDFPage_GetAnnot(page, 3));
2413 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2414
2415 annot.reset(FPDFPage_GetAnnot(page, 4));
2416 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2417 }
2418
2419 UnloadPage(page);
2420}
2421
2422TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2423 // Open a file with checkbox and radiobuttons widget annotations and load its
2424 // first page.
2425 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2426 FPDF_PAGE page = LoadPage(0);
2427 ASSERT_TRUE(page);
2428
2429 {
2430 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2431 ASSERT_TRUE(annot);
2432 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2433 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2434 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2435 }
2436
2437 UnloadPage(page);
2438}
2439
2440TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2441 // Open a file with text widget annotations and load its first page.
2442 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2443 FPDF_PAGE page = LoadPage(0);
2444 ASSERT_TRUE(page);
2445
2446 {
2447 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2448 ASSERT_TRUE(annot);
2449 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2450 }
2451
2452 UnloadPage(page);
2453}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002454
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002455TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2456 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002457 FPDF_PAGE page = LoadPage(0);
2458 ASSERT_TRUE(page);
2459
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002460 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2461
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002462 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002463 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002464 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002465 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002466 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002467
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002468 constexpr int kExpectedAnnotTypes[] = {-1,
2469 FPDF_FORMFIELD_COMBOBOX,
2470 FPDF_FORMFIELD_LISTBOX,
2471 FPDF_FORMFIELD_TEXTFIELD,
2472 FPDF_FORMFIELD_CHECKBOX,
2473 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002474
Lei Zhang4501a502020-05-18 16:52:59 +00002475 for (size_t i = 0; i < pdfium::size(kExpectedAnnotTypes); ++i) {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002476 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002477 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002478 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002479 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2480 }
2481 UnloadPage(page);
2482}
2483
2484TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2485 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2486 FPDF_PAGE page = LoadPage(0);
2487 ASSERT_TRUE(page);
2488
2489 {
2490 EXPECT_EQ(0u,
2491 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2492
2493 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2494 ASSERT_TRUE(annot);
2495
2496 EXPECT_EQ(0u,
2497 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2498
2499 unsigned long length_bytes =
2500 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2501 ASSERT_EQ(2u, length_bytes);
2502 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2503 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2504 buf.data(), length_bytes));
2505 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2506 }
2507 {
2508 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2509 ASSERT_TRUE(annot);
2510
2511 unsigned long length_bytes =
2512 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2513 ASSERT_EQ(18u, length_bytes);
2514 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2515 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2516 buf.data(), length_bytes));
2517 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2518 }
2519 UnloadPage(page);
2520}
2521
2522TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2523 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2524 FPDF_PAGE page = LoadPage(0);
2525 ASSERT_TRUE(page);
2526
2527 {
2528 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2529 ASSERT_TRUE(annot);
2530
2531 unsigned long length_bytes =
2532 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2533 ASSERT_EQ(2u, length_bytes);
2534 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2535 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2536 buf.data(), length_bytes));
2537 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2538 }
2539 {
2540 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2541 ASSERT_TRUE(annot);
2542
2543 unsigned long length_bytes =
2544 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2545 ASSERT_EQ(14u, length_bytes);
2546 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2547 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2548 buf.data(), length_bytes));
2549 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2550 }
2551 UnloadPage(page);
2552}
2553
2554TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2555 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2556 FPDF_PAGE page = LoadPage(0);
2557 ASSERT_TRUE(page);
2558
2559 {
2560 EXPECT_EQ(0u,
2561 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2562
2563 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2564 ASSERT_TRUE(annot);
2565
2566 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2567
2568 unsigned long length_bytes =
2569 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2570 ASSERT_EQ(18u, length_bytes);
2571 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2572 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2573 buf.data(), length_bytes));
2574 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2575 }
2576 UnloadPage(page);
2577}
2578
2579TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2580 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2581 FPDF_PAGE page = LoadPage(0);
2582 ASSERT_TRUE(page);
2583
2584 {
2585 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2586 ASSERT_TRUE(annot);
2587
2588 unsigned long length_bytes =
2589 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2590 ASSERT_EQ(30u, length_bytes);
2591 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2592 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2593 buf.data(), length_bytes));
2594 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2595 }
2596 UnloadPage(page);
2597}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002598
Lei Zhang9b444002020-04-17 17:35:23 +00002599TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2600 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002601 FPDF_PAGE page = LoadPage(0);
2602 ASSERT_TRUE(page);
2603
Lei Zhang9b444002020-04-17 17:35:23 +00002604 // Verify widgets are by default focusable.
2605 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2606 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002607
Lei Zhang9b444002020-04-17 17:35:23 +00002608 // Expected annot subtypes for page 0 of annots.pdf.
2609 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2610 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2611 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2612 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2613 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002614
Lei Zhang9b444002020-04-17 17:35:23 +00002615 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002616 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002617 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2618 kExpectedAnnotSubtypes,
2619 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002620
Lei Zhang9b444002020-04-17 17:35:23 +00002621 // Make no annotation type focusable using the preferred method.
2622 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2623 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002624
Lei Zhang9b444002020-04-17 17:35:23 +00002625 // Restore the focusable type count back to 1, then set it back to 0 using a
2626 // different method.
2627 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2628 ASSERT_TRUE(
2629 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2630 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002631
Lei Zhang9b444002020-04-17 17:35:23 +00002632 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002633 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002634
Lei Zhang9b444002020-04-17 17:35:23 +00002635 // Now make links focusable.
2636 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2637 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002638
Lei Zhang9b444002020-04-17 17:35:23 +00002639 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002640 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002641 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2642 kExpectedAnnotSubtypes,
2643 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002644
Lei Zhang9b444002020-04-17 17:35:23 +00002645 // Test invalid parameters.
2646 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
Lei Zhang4501a502020-05-18 16:52:59 +00002647 pdfium::size(kDefaultSubtypes)));
Lei Zhang9b444002020-04-17 17:35:23 +00002648 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
Lei Zhang4501a502020-05-18 16:52:59 +00002649 pdfium::size(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002650 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2651
Lei Zhang9b444002020-04-17 17:35:23 +00002652 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2653 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2654 subtypes.size()));
2655 EXPECT_FALSE(
2656 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2657 EXPECT_FALSE(
2658 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002659
2660 UnloadPage(page);
2661}
Lei Zhang671aece2020-04-14 19:02:26 +00002662
Hui Yingst609f7d62020-04-23 23:14:13 +00002663// TODO(crbug.com/pdfium/11): Fix this test and enable.
2664#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2665#define MAYBE_FocusableAnnotRendering DISABLED_FocusableAnnotRendering
2666#else
2667#define MAYBE_FocusableAnnotRendering FocusableAnnotRendering
2668#endif
2669TEST_F(FPDFAnnotEmbedderTest, MAYBE_FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00002670 ASSERT_TRUE(OpenDocument("annots.pdf"));
2671 FPDF_PAGE page = LoadPage(0);
2672 ASSERT_TRUE(page);
2673
2674 {
2675#if defined(OS_WIN)
2676 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
2677#elif defined(OS_MACOSX)
2678 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
2679#else
2680 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
2681#endif
2682 // Check the initial rendering.
2683 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2684 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2685 }
2686
2687 // Make links and highlights focusable.
2688 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
2689 FPDF_ANNOT_HIGHLIGHT};
Lei Zhang4501a502020-05-18 16:52:59 +00002690 constexpr int kSubTypesCount = pdfium::size(kSubTypes);
Lei Zhang671aece2020-04-14 19:02:26 +00002691 ASSERT_TRUE(
2692 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
2693 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
2694 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
2695 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
2696 subtypes.size()));
2697 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
2698 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
2699
2700 {
2701#if defined(OS_WIN)
2702 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
2703#elif defined(OS_MACOSX)
2704 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
2705#else
2706 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
2707#endif
2708 // Focus the first link and check the rendering.
2709 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2710 ASSERT_TRUE(annot);
2711 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2712 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2713 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2714 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2715 }
2716
2717 {
2718#if defined(OS_WIN)
2719 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
2720#elif defined(OS_MACOSX)
2721 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
2722#else
2723 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
2724#endif
2725 // Focus the first highlight and check the rendering.
2726 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2727 ASSERT_TRUE(annot);
2728 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
2729 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2730 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2731 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2732 }
2733
2734 UnloadPage(page);
2735}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00002736
2737TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
2738 ASSERT_TRUE(OpenDocument("annots.pdf"));
2739 FPDF_PAGE page = LoadPage(0);
2740 ASSERT_TRUE(page);
2741 {
2742 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
2743 ASSERT_TRUE(annot);
2744 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2745 FPDF_LINK link_annot = FPDFAnnot_GetLink(annot.get());
2746 ASSERT_TRUE(link_annot);
2747
2748 FPDF_ACTION action = FPDFLink_GetAction(link_annot);
2749 ASSERT_TRUE(action);
2750 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
2751 FPDFAction_GetType(action));
2752
2753 constexpr char kExpectedResult[] =
2754 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
2755 "fpdf_text.h";
Lei Zhang4501a502020-05-18 16:52:59 +00002756 constexpr unsigned long kExpectedLength = pdfium::size(kExpectedResult);
Badhri Ravikumarcd628912020-05-07 19:23:51 +00002757 unsigned long bufsize =
2758 FPDFAction_GetURIPath(document(), action, nullptr, 0);
2759 ASSERT_EQ(kExpectedLength, bufsize);
2760
2761 char buffer[1024];
2762 EXPECT_EQ(bufsize,
2763 FPDFAction_GetURIPath(document(), action, buffer, bufsize));
2764 EXPECT_STREQ(kExpectedResult, buffer);
2765 }
2766
2767 {
2768 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2769 ASSERT_TRUE(annot);
2770 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
2771 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
2772 }
2773
2774 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
2775
2776 UnloadPage(page);
2777}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00002778
2779TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
2780 // Open a file with radio button widget annotations and load its first page.
2781 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2782 FPDF_PAGE page = LoadPage(0);
2783 ASSERT_TRUE(page);
2784
2785 {
2786 // Checks for bad annot.
2787 EXPECT_EQ(-1,
2788 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
2789
2790 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
2791 ASSERT_TRUE(annot);
2792
2793 // Checks for bad form handle.
2794 EXPECT_EQ(-1,
2795 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
2796
2797 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
2798 }
2799
2800 UnloadPage(page);
2801}
2802
2803TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
2804 // Open a file with checkbox widget annotations and load its first page.
2805 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2806 FPDF_PAGE page = LoadPage(0);
2807 ASSERT_TRUE(page);
2808
2809 {
2810 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2811 ASSERT_TRUE(annot);
2812 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
2813 }
2814
2815 UnloadPage(page);
2816}
2817
2818TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
2819 // Open a file with ink annotations and load its first page.
2820 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2821 FPDF_PAGE page = LoadPage(0);
2822 ASSERT_TRUE(page);
2823
2824 {
2825 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2826 ASSERT_TRUE(annot);
2827 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
2828 }
2829
2830 UnloadPage(page);
2831}
2832
2833TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
2834 // Open a file with radio button widget annotations and load its first page.
2835 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2836 FPDF_PAGE page = LoadPage(0);
2837 ASSERT_TRUE(page);
2838
2839 {
2840 // Checks for bad annot.
2841 EXPECT_EQ(-1,
2842 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
2843
2844 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
2845 ASSERT_TRUE(annot);
2846
2847 // Checks for bad form handle.
2848 EXPECT_EQ(-1,
2849 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
2850
2851 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
2852 }
2853
2854 UnloadPage(page);
2855}
2856
2857TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
2858 // Open a file with checkbox widget annotations and load its first page.
2859 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2860 FPDF_PAGE page = LoadPage(0);
2861 ASSERT_TRUE(page);
2862
2863 {
2864 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2865 ASSERT_TRUE(annot);
2866 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
2867 }
2868
2869 UnloadPage(page);
2870}
2871
2872TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
2873 // Open a file with ink annotations and load its first page.
2874 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2875 FPDF_PAGE page = LoadPage(0);
2876 ASSERT_TRUE(page);
2877
2878 {
2879 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2880 ASSERT_TRUE(annot);
2881 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
2882 }
2883
2884 UnloadPage(page);
2885}
2886
2887TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
2888 // Open a file with radio button widget annotations and load its first page.
2889 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2890 FPDF_PAGE page = LoadPage(0);
2891 ASSERT_TRUE(page);
2892
2893 {
2894 // Checks for bad annot.
2895 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
2896 form_handle(), /*annot=*/nullptr,
2897 /*buffer=*/nullptr, /*buflen=*/0));
2898
2899 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
2900 ASSERT_TRUE(annot);
2901
2902 // Checks for bad form handle.
2903 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
2904 /*hHandle=*/nullptr, annot.get(),
2905 /*buffer=*/nullptr, /*buflen=*/0));
2906
2907 unsigned long length_bytes =
2908 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
2909 /*buffer=*/nullptr, /*buflen=*/0);
2910 ASSERT_EQ(14u, length_bytes);
2911 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2912 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
2913 buf.data(), length_bytes));
2914 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
2915 }
2916
2917 UnloadPage(page);
2918}
2919
2920TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
2921 // Open a file with checkbox widget annotations and load its first page.
2922 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2923 FPDF_PAGE page = LoadPage(0);
2924 ASSERT_TRUE(page);
2925
2926 {
2927 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2928 ASSERT_TRUE(annot);
2929
2930 unsigned long length_bytes =
2931 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
2932 /*buffer=*/nullptr, /*buflen=*/0);
2933 ASSERT_EQ(8u, length_bytes);
2934 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2935 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
2936 buf.data(), length_bytes));
2937 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
2938 }
2939
2940 UnloadPage(page);
2941}
2942
2943TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
2944 // Open a file with ink annotations and load its first page.
2945 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2946 FPDF_PAGE page = LoadPage(0);
2947 ASSERT_TRUE(page);
2948
2949 {
2950 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2951 ASSERT_TRUE(annot);
2952 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
2953 /*buffer=*/nullptr,
2954 /*buflen=*/0));
2955 }
2956
2957 UnloadPage(page);
2958}