blob: 55d7a741a196f4d6f309e35bc6c42a6b6d0d2b57 [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"
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +000013#include "core/fxcrt/fx_memory.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040014#include "core/fxcrt/fx_system.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000015#include "public/cpp/fpdf_scopers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040016#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040017#include "public/fpdf_edit.h"
Mansi Awasthi07bf7e62020-01-24 10:34:17 +000018#include "public/fpdf_formfill.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040019#include "public/fpdfview.h"
20#include "testing/embedder_test.h"
Hui Yingstb4baceb2020-04-28 23:46:10 +000021#include "testing/embedder_test_constants.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000022#include "testing/fx_string_testhelpers.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000023#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040024#include "testing/gtest/include/gtest/gtest.h"
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000025#include "testing/utils/hash.h"
Lei Zhang9b444002020-04-17 17:35:23 +000026#include "third_party/base/span.h"
Lei Zhangc9b72992020-04-22 01:03:52 +000027#include "third_party/base/stl_util.h"
Lei Zhang9b444002020-04-17 17:35:23 +000028
29namespace {
30
Hui Yingst4a21df02020-05-13 03:15:44 +000031#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
32#if defined(OS_LINUX)
33const char kAnnotationStampWithApChecksum[] =
34 "db83eaadc92967e3ac9bebfc6178ca75";
35#else
36const char kAnnotationStampWithApChecksum[] =
37 "3c87b4a8e51245964357fb5f5fbc612b";
38#endif // defined(OS_LINUX)
39#else
Hui Yingstb4baceb2020-04-28 23:46:10 +000040#if defined(OS_WIN)
41const char kAnnotationStampWithApChecksum[] =
42 "6aa001a77ec05d0f1b0d1d22e28744d4";
43#elif defined(OS_MACOSX)
44const char kAnnotationStampWithApChecksum[] =
45 "80d7b6cc7b13a78d77a6151bc846e80b";
46#else
47const char kAnnotationStampWithApChecksum[] =
48 "b42cef463483e668eaf4055a65e4f1f5";
49#endif
Hui Yingst4a21df02020-05-13 03:15:44 +000050#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingstb4baceb2020-04-28 23:46:10 +000051
Lei Zhang9b444002020-04-17 17:35:23 +000052void VerifyFocusableAnnotSubtypes(
53 FPDF_FORMHANDLE form_handle,
54 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) {
55 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
56 FPDFAnnot_GetFocusableSubtypesCount(form_handle));
57
58 std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes(
59 expected_subtypes.size());
60 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(
61 form_handle, actual_subtypes.data(), actual_subtypes.size()));
62 for (size_t i = 0; i < expected_subtypes.size(); ++i)
63 ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]);
64}
65
66void SetAndVerifyFocusableAnnotSubtypes(
67 FPDF_FORMHANDLE form_handle,
68 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) {
69 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(),
70 subtypes.size()));
71 VerifyFocusableAnnotSubtypes(form_handle, subtypes);
72}
73
74void VerifyAnnotationSubtypesAndFocusability(
75 FPDF_FORMHANDLE form_handle,
76 FPDF_PAGE page,
77 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes,
78 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) {
79 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
80 FPDFPage_GetAnnotCount(page));
81 for (size_t i = 0; i < expected_subtypes.size(); ++i) {
82 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
83 ASSERT_TRUE(annot);
84 EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get()));
85
Lei Zhangf245ae62020-05-15 21:49:46 +000086 bool expected_focusable =
87 pdfium::Contains(expected_focusable_subtypes, expected_subtypes[i]);
Lei Zhang9b444002020-04-17 17:35:23 +000088 EXPECT_EQ(expected_focusable,
89 FORM_SetFocusedAnnot(form_handle, annot.get()));
90
91 // Kill the focus so the next test starts in an unfocused state.
92 FORM_ForceToKillFocus(form_handle);
93 }
94}
95
96} // namespace
Jane Liu4fd9a472017-06-01 18:56:09 -040097
Lei Zhangab41f252018-12-23 03:10:50 +000098class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040099
Lei Zhangab41f252018-12-23 03:10:50 +0000100TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +0000101 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
102 FPDF_PAGE page = LoadPage(0);
103 ASSERT_TRUE(page);
104
105 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
106
107 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
108 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
109 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
110 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
111 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
112
113 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
114
115 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
116
117 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
118 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
119 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
120
121 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
122
Lei Zhang4f556b82019-04-08 16:32:41 +0000123 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +0000124 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +0000125 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
126
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000127 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +0000128 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
129 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
130 EXPECT_EQ(0u,
131 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
132
133 UnloadPage(page);
134}
135
Lei Zhang3d9a0972019-03-04 19:34:09 +0000136TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
137 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
138 FPDF_PAGE page = LoadPage(0);
139 ASSERT_TRUE(page);
140
141 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +0000142 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +0000143
144 UnloadPage(page);
145}
146
Lei Zhangab41f252018-12-23 03:10:50 +0000147TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -0400148 // Open a file with one annotation and load its first page.
149 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000150 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -0400151 ASSERT_TRUE(page);
152
153 // This annotation has a malformed appearance stream, which does not have its
154 // normal appearance defined, only its rollover appearance. In this case, its
155 // normal appearance should be generated, allowing the highlight annotation to
156 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000157 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000158 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -0400159
160 UnloadPage(page);
161}
162
Lei Zhang03e5e682019-09-16 19:45:55 +0000163// TODO(crbug.com/pdfium/11): Fix this test and enable.
164#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
165#define MAYBE_RenderMultilineMarkupAnnotWithoutAP \
166 DISABLED_RenderMultilineMarkupAnnotWithoutAP
167#else
168#define MAYBE_RenderMultilineMarkupAnnotWithoutAP \
169 RenderMultilineMarkupAnnotWithoutAP
170#endif
171TEST_F(FPDFAnnotEmbedderTest, MAYBE_RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang4f556b82019-04-08 16:32:41 +0000172 static const char kMd5[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +0000173 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000174 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
175 FPDF_PAGE page = LoadPage(0);
176 ASSERT_TRUE(page);
177
Tom Sepeze08d2b12018-04-25 18:49:32 +0000178 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000179 CompareBitmap(bitmap.get(), 595, 842, kMd5);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000180
181 UnloadPage(page);
182}
183
Lei Zhangab41f252018-12-23 03:10:50 +0000184TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400185 // Open a file with one annotation and load its first page.
186 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000187 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400188 ASSERT_TRUE(page);
189
190 // Check that there is a total of 1 annotation on its first page.
191 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
192
193 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000194 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000195 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000196 ASSERT_TRUE(annot);
197 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400198
Lei Zhanga21d5932018-02-05 18:28:38 +0000199 // Check that the annotation color is yellow.
200 unsigned int R;
201 unsigned int G;
202 unsigned int B;
203 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000204 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000205 &G, &B, &A));
206 EXPECT_EQ(255u, R);
207 EXPECT_EQ(255u, G);
208 EXPECT_EQ(0u, B);
209 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400210
Lei Zhanga21d5932018-02-05 18:28:38 +0000211 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000212 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000213 EXPECT_EQ(FPDF_OBJECT_STRING,
214 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000215 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000216 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000217 ASSERT_EQ(28u, length_bytes);
218 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000219 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000220 length_bytes));
221 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400222
Lei Zhanga21d5932018-02-05 18:28:38 +0000223 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000224 EXPECT_EQ(
225 FPDF_OBJECT_STRING,
226 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000227 length_bytes = FPDFAnnot_GetStringValue(
228 annot.get(), pdfium::annotation::kContents, nullptr, 0);
229 ASSERT_EQ(2690u, length_bytes);
230 buf = GetFPDFWideStringBuffer(length_bytes);
231 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
232 pdfium::annotation::kContents,
233 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000234 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000235 L"This is a note for that highlight annotation. Very long highlight "
236 "annotation. Long long long Long 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 longLong long longLong long longLong long longLong long "
254 "longLong long long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000255 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400256
Lei Zhanga21d5932018-02-05 18:28:38 +0000257 // Check that the quadpoints are correct.
258 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000259 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000260 EXPECT_EQ(115.802643f, quadpoints.x1);
261 EXPECT_EQ(718.913940f, quadpoints.y1);
262 EXPECT_EQ(157.211182f, quadpoints.x4);
263 EXPECT_EQ(706.264465f, quadpoints.y4);
264 }
Tom Sepez507d0192018-11-07 16:37:51 +0000265 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400266}
267
Lei Zhang03e5e682019-09-16 19:45:55 +0000268// TODO(crbug.com/pdfium/11): Fix this test and enable.
269#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
270#define MAYBE_ExtractInkMultiple DISABLED_ExtractInkMultiple
271#else
272#define MAYBE_ExtractInkMultiple ExtractInkMultiple
273#endif
274TEST_F(FPDFAnnotEmbedderTest, MAYBE_ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400275 // Open a file with three annotations and load its first page.
276 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000277 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400278 ASSERT_TRUE(page);
279
280 // Check that there is a total of 3 annotation on its first page.
281 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
282
Lei Zhanga21d5932018-02-05 18:28:38 +0000283 {
284 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000285 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000286 ASSERT_TRUE(annot);
287 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400288
Lei Zhanga21d5932018-02-05 18:28:38 +0000289 // Check that the annotation color is blue with opacity.
290 unsigned int R;
291 unsigned int G;
292 unsigned int B;
293 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000294 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000295 &G, &B, &A));
296 EXPECT_EQ(0u, R);
297 EXPECT_EQ(0u, G);
298 EXPECT_EQ(255u, B);
299 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400300
Lei Zhanga21d5932018-02-05 18:28:38 +0000301 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000302 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
303 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400304
Lei Zhang4f556b82019-04-08 16:32:41 +0000305 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000306 // Note that upon rendering, the rectangle coordinates will be adjusted.
307 FS_RECTF rect;
308 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
309 EXPECT_EQ(351.820404f, rect.left);
310 EXPECT_EQ(583.830688f, rect.bottom);
311 EXPECT_EQ(475.336090f, rect.right);
312 EXPECT_EQ(681.535034f, rect.top);
313 }
Tom Sepezef43c262018-11-07 16:41:32 +0000314 {
315 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
316 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
317 }
Tom Sepez507d0192018-11-07 16:37:51 +0000318 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400319}
Jane Liu20eafda2017-06-07 10:33:24 -0400320
Lei Zhangab41f252018-12-23 03:10:50 +0000321TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400322 // Open a file with one annotation and load its first page.
323 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000324 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400325 ASSERT_TRUE(page);
326
327 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400328 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400329
330 UnloadPage(page);
331}
332
Lei Zhangab41f252018-12-23 03:10:50 +0000333TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400334 // Open a file with no annotation and load its first page.
335 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000336 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400337 ASSERT_TRUE(page);
338 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
339
Lei Zhanga21d5932018-02-05 18:28:38 +0000340 {
341 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000342 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000343 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400344
Lei Zhanga21d5932018-02-05 18:28:38 +0000345 // Check that there is now 1 annotations on this page.
346 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400347
Lei Zhanga21d5932018-02-05 18:28:38 +0000348 // Check that the subtype of the annotation is correct.
349 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
350 }
Jane Liue10509a2017-06-20 16:47:41 -0400351
Lei Zhanga21d5932018-02-05 18:28:38 +0000352 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000353 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000354 ASSERT_TRUE(annot);
355 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400356
Lei Zhanga21d5932018-02-05 18:28:38 +0000357 // Set the color of the annotation.
358 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
359 102, 153, 204));
360 // Check that the color has been set correctly.
361 unsigned int R;
362 unsigned int G;
363 unsigned int B;
364 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000365 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000366 &G, &B, &A));
367 EXPECT_EQ(51u, R);
368 EXPECT_EQ(102u, G);
369 EXPECT_EQ(153u, B);
370 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400371
Lei Zhanga21d5932018-02-05 18:28:38 +0000372 // Change the color of the annotation.
373 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
374 153, 102, 51));
375 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000376 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000377 &G, &B, &A));
378 EXPECT_EQ(204u, R);
379 EXPECT_EQ(153u, G);
380 EXPECT_EQ(102u, B);
381 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400382
Lei Zhanga21d5932018-02-05 18:28:38 +0000383 // Set the annotation rectangle.
384 FS_RECTF rect;
385 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
386 EXPECT_EQ(0.f, rect.left);
387 EXPECT_EQ(0.f, rect.right);
388 rect.left = 35;
389 rect.bottom = 150;
390 rect.right = 53;
391 rect.top = 165;
392 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
393 // Check that the annotation rectangle has been set correctly.
394 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
395 EXPECT_EQ(35.f, rect.left);
396 EXPECT_EQ(150.f, rect.bottom);
397 EXPECT_EQ(53.f, rect.right);
398 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400399
Lei Zhanga21d5932018-02-05 18:28:38 +0000400 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000401 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000402 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000403 ASSERT_TRUE(FPDFAnnot_SetStringValue(
404 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000405 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000406 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000407 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000408 ASSERT_EQ(74u, length_bytes);
409 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
410 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
411 pdfium::annotation::kContents,
412 buf.data(), length_bytes));
413 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000414 }
Jane Liu20eafda2017-06-07 10:33:24 -0400415 UnloadPage(page);
416}
417
Lei Zhang03e5e682019-09-16 19:45:55 +0000418// TODO(crbug.com/pdfium/11): Fix this test and enable.
419#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
420#define MAYBE_AddAndSaveUnderlineAnnotation \
421 DISABLED_AddAndSaveUnderlineAnnotation
422#else
423#define MAYBE_AddAndSaveUnderlineAnnotation AddAndSaveUnderlineAnnotation
424#endif
425TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400426 // Open a file with one annotation and load its first page.
427 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000428 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400429 ASSERT_TRUE(page);
430
431 // Check that there is a total of one annotation on its first page, and verify
432 // its quadpoints.
433 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400434 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000435 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000436 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000437 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000438 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000439 EXPECT_EQ(115.802643f, quadpoints.x1);
440 EXPECT_EQ(718.913940f, quadpoints.y1);
441 EXPECT_EQ(157.211182f, quadpoints.x4);
442 EXPECT_EQ(706.264465f, quadpoints.y4);
443 }
Jane Liu20eafda2017-06-07 10:33:24 -0400444
445 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000446 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000447 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000448 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
449 ASSERT_TRUE(annot);
450 quadpoints.x1 = 140.802643f;
451 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000452 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000453 }
Jane Liu20eafda2017-06-07 10:33:24 -0400454
455 // Save the document, closing the page and document.
456 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000457 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400458
459 // Open the saved document.
Lei Zhang4f556b82019-04-08 16:32:41 +0000460 static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400461
Lei Zhang0b494052019-01-31 21:41:15 +0000462 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000463 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000464 VerifySavedRendering(page, 612, 792, kMd5);
Jane Liu20eafda2017-06-07 10:33:24 -0400465
466 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000467 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400468
Lei Zhanga21d5932018-02-05 18:28:38 +0000469 {
470 // Check that the second annotation is an underline annotation and verify
471 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000472 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000473 ASSERT_TRUE(new_annot);
474 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
475 FS_QUADPOINTSF new_quadpoints;
476 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000477 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000478 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
479 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
480 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
481 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
482 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400483
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000484 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400485 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400486}
Jane Liu06462752017-06-27 16:41:14 -0400487
Lei Zhangab41f252018-12-23 03:10:50 +0000488TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000489 // Open a file with four annotations and load its first page.
490 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
491 FPDF_PAGE page = LoadPage(0);
492 ASSERT_TRUE(page);
493 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
494
495 // Retrieve the highlight annotation.
496 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
497 ASSERT_TRUE(annot);
498 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
499
500 FS_QUADPOINTSF quadpoints;
501 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
502
503 {
504 // Verify the current one set of quadpoints.
505 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
506
507 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
508 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
509 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
510 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
511 }
512
513 {
514 // Update the quadpoints.
515 FS_QUADPOINTSF new_quadpoints = quadpoints;
516 new_quadpoints.y1 -= 20.f;
517 new_quadpoints.y2 -= 20.f;
518 new_quadpoints.y3 -= 20.f;
519 new_quadpoints.y4 -= 20.f;
520 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
521
522 // Verify added quadpoint set
523 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
524 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
525 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
526 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
527 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
528 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
529 }
530
531 {
532 // Append a new set of quadpoints.
533 FS_QUADPOINTSF new_quadpoints = quadpoints;
534 new_quadpoints.y1 += 20.f;
535 new_quadpoints.y2 += 20.f;
536 new_quadpoints.y3 += 20.f;
537 new_quadpoints.y4 += 20.f;
538 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
539
540 // Verify added quadpoint set
541 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
542 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
543 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
544 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
545 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
546 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
547 }
548
549 {
550 // Setting and getting quadpoints at out-of-bound index should fail
551 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
552 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
553 }
554
555 FPDFPage_CloseAnnot(annot);
556
557 // Retrieve the square annotation
558 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
559
560 {
561 // Check that attempting to set its quadpoints would fail
562 ASSERT_TRUE(squareAnnot);
563 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
564 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
565 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
566 }
567
568 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000569 UnloadPage(page);
570}
571
Lei Zhang03e5e682019-09-16 19:45:55 +0000572// TODO(crbug.com/pdfium/11): Fix this test and enable.
573#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
574#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
575#else
576#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
577#endif
578TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000579#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +0000580 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
Lei Zhang4f556b82019-04-08 16:32:41 +0000581 static const char kMd5ModifiedHighlight[] =
Lei Zhang0f6b4342020-02-25 20:00:39 +0000582 "e64bf648f6e9354d1f3eedb47a2c9498";
583 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Lei Zhange67bcc72019-04-30 18:55:58 +0000584#elif defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000585 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
586 static const char kMd5ModifiedHighlight[] =
587 "66f3caef3a7d488a4fa1ad37fc06310e";
588 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400589#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000590 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
591 static const char kMd5ModifiedHighlight[] =
592 "66f3caef3a7d488a4fa1ad37fc06310e";
593 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400594#endif
595
Jane Liu06462752017-06-27 16:41:14 -0400596 // Open a file with four annotations and load its first page.
597 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000598 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400599 ASSERT_TRUE(page);
600 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
601
Jane Liub370e5a2017-08-16 13:24:58 -0400602 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000603 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000604 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000605 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000606 }
Jane Liub370e5a2017-08-16 13:24:58 -0400607
Jane Liu0c6b07d2017-08-15 10:50:22 -0400608 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400609 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000610
611 // Retrieve the highlight annotation which has its AP stream already defined.
612 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000613 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000614 ASSERT_TRUE(annot);
615 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
616
617 // Check that color cannot be set when an AP stream is defined already.
618 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
619 102, 153, 204));
620
621 // Verify its attachment points.
622 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000623 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000624 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
625 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
626 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
627 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
628
629 // Check that updating the attachment points would succeed.
630 quadpoints.x1 -= 50.f;
631 quadpoints.x2 -= 50.f;
632 quadpoints.x3 -= 50.f;
633 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000634 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000635 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000636 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000637 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
638 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
639 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
640 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
641
642 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000643 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000644 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000645 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000646 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000647
648 // Verify its annotation rectangle.
649 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
650 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
651 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
652 EXPECT_NEAR(136.325f, rect.right, 0.001f);
653 EXPECT_NEAR(721.292f, rect.top, 0.001f);
654
655 // Check that updating the rectangle would succeed.
656 rect.left -= 60.f;
657 rect.right -= 60.f;
658 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
659 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
660 EXPECT_EQ(rect.right, new_rect.right);
661 }
Jane Liu06462752017-06-27 16:41:14 -0400662
Jane Liub370e5a2017-08-16 13:24:58 -0400663 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000664 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000665 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000666 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000667 }
Jane Liub370e5a2017-08-16 13:24:58 -0400668
Lei Zhanga21d5932018-02-05 18:28:38 +0000669 {
670 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000671 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000672 ASSERT_TRUE(annot);
673 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400674
Lei Zhanga21d5932018-02-05 18:28:38 +0000675 // Check that updating the rectangle would succeed.
676 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
677 rect.left += 70.f;
678 rect.right += 70.f;
679 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
680 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
681 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400682
Lei Zhanga21d5932018-02-05 18:28:38 +0000683 // Check that updating the rectangle changes the square annotation's
684 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000685 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000686 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000687 }
Jane Liub370e5a2017-08-16 13:24:58 -0400688
Jane Liu06462752017-06-27 16:41:14 -0400689 UnloadPage(page);
690}
Jane Liu8ce58f52017-06-29 13:40:22 -0400691
Lei Zhangab41f252018-12-23 03:10:50 +0000692TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000693 // Open a file with multiline markup annotations.
694 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
695 FPDF_PAGE page = LoadPage(0);
696 ASSERT_TRUE(page);
697 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000698 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000699 ASSERT_TRUE(annot);
700
701 // This is a three line annotation.
702 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
703 }
704 UnloadPage(page);
705
706 // null annotation should return 0
707 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
708}
709
Lei Zhangab41f252018-12-23 03:10:50 +0000710TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400711 // Open a file with 3 annotations on its first page.
712 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000713 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400714 ASSERT_TRUE(page);
715 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
716
Jane Liu0c6b07d2017-08-15 10:50:22 -0400717 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400718
Lei Zhanga21d5932018-02-05 18:28:38 +0000719 // Check that the annotations have the expected rectangle coordinates.
720 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000721 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000722 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
723 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
724 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400725
Lei Zhanga21d5932018-02-05 18:28:38 +0000726 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000727 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000728 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
729 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
730 }
731
732 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000733 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000734 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
735 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
736 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400737
738 // Check that nothing happens when attempting to remove an annotation with an
739 // out-of-bound index.
740 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
741 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
742 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
743
744 // Remove the second annotation.
745 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
746 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
747 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
748
749 // Save the document, closing the page and document.
750 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000751 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400752
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400753 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400754 // Open the saved document.
755 std::string new_file = GetString();
756 FPDF_FILEACCESS file_access;
757 memset(&file_access, 0, sizeof(file_access));
758 file_access.m_FileLen = new_file.size();
759 file_access.m_GetBlock = GetBlockFromString;
760 file_access.m_Param = &new_file;
761 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
762 ASSERT_TRUE(new_doc);
763 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
764 ASSERT_TRUE(new_page);
765
766 // Check that the saved document has 2 annotations on the first page.
767 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
768
Lei Zhanga21d5932018-02-05 18:28:38 +0000769 // Check that the remaining 2 annotations are the original 1st and 3rd ones
770 // by verifying their rectangle coordinates.
771 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000772 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000773 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
774 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
775 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400776
Lei Zhanga21d5932018-02-05 18:28:38 +0000777 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000778 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000779 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
780 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
781 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400782 FPDF_ClosePage(new_page);
783 FPDF_CloseDocument(new_doc);
784}
Jane Liu8ce58f52017-06-29 13:40:22 -0400785
Hui Yingst4a21df02020-05-13 03:15:44 +0000786TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Lei Zhang03e5e682019-09-16 19:45:55 +0000787#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst4a21df02020-05-13 03:15:44 +0000788#if defined(OS_LINUX)
789 static const char kMd5ModifiedPath[] = "d76382fd57fafad0233f7549f871dafa";
790 static const char kMd5TwoPaths[] = "323151317b8cb62130546c7b8d70f622";
791 static const char kMd5NewAnnot[] = "9a3b02d876620d19787549ee1100b63c";
Lei Zhang03e5e682019-09-16 19:45:55 +0000792#else
Hui Yingst4a21df02020-05-13 03:15:44 +0000793 static const char kMd5ModifiedPath[] = "c9ba60887a312370d9a32198aa53aca4";
794 static const char kMd5TwoPaths[] = "0768d56373094fcdf4ddf3f3364c006f";
795 static const char kMd5NewAnnot[] = "6f7e1c189bcfac90ffccf2a527857006";
796#endif // defined(OS_LINUX)
797#else
798#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +0000799 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
800 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
801 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Hui Yingst4a21df02020-05-13 03:15:44 +0000802#elif defined(OS_MACOSX)
803 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
804 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
805 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Jane Liubaa7ff42017-06-29 19:18:23 -0400806#else
Lei Zhanga2b70732019-06-25 08:34:22 +0000807 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
808 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
809 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -0400810#endif
Hui Yingst4a21df02020-05-13 03:15:44 +0000811#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liubaa7ff42017-06-29 19:18:23 -0400812
813 // Open a file with two annotations and load its first page.
814 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000815 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400816 ASSERT_TRUE(page);
817 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
818
819 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000820 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000821 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +0000822 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000823 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400824
Lei Zhanga21d5932018-02-05 18:28:38 +0000825 {
826 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000827 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000828 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400829
Lei Zhanga21d5932018-02-05 18:28:38 +0000830 // Check that this annotation has one path object and retrieve it.
831 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000832 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000833 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
834 EXPECT_FALSE(path);
835 path = FPDFAnnot_GetObject(annot.get(), 0);
836 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
837 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400838
Lei Zhanga21d5932018-02-05 18:28:38 +0000839 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +0000840 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +0000841 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400842
Lei Zhanga21d5932018-02-05 18:28:38 +0000843 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000844 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000845 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000846 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000847 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400848
Lei Zhanga21d5932018-02-05 18:28:38 +0000849 // Add a second path object to the same annotation.
850 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
851 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +0000852 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
853 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +0000854 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
855 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
856 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400857
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000858 // The object is in the annontation, not in the page, so the page object
859 // array should not change.
860 ASSERT_EQ(32, FPDFPage_CountObjects(page));
861
Lei Zhanga21d5932018-02-05 18:28:38 +0000862 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000863 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000864 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000865 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000866 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400867
Lei Zhanga21d5932018-02-05 18:28:38 +0000868 // Delete the newly added path object.
869 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
870 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000871 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000872 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400873
874 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000875 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000876 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000877 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000878 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400879
Jane Liubaa7ff42017-06-29 19:18:23 -0400880 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400881
Lei Zhanga21d5932018-02-05 18:28:38 +0000882 {
883 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000884 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000885 ASSERT_TRUE(annot);
886 rect.left = 200.f;
887 rect.bottom = 400.f;
888 rect.right = 500.f;
889 rect.top = 600.f;
890 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400891
Lei Zhanga21d5932018-02-05 18:28:38 +0000892 // Add a new path to the annotation.
893 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
894 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
895 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
896 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
897 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +0000898 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
899 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +0000900 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
901 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
902 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
903
904 // Check that the annotation's bounding box came from its rectangle.
905 FS_RECTF new_rect;
906 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
907 EXPECT_EQ(rect.left, new_rect.left);
908 EXPECT_EQ(rect.bottom, new_rect.bottom);
909 EXPECT_EQ(rect.right, new_rect.right);
910 EXPECT_EQ(rect.top, new_rect.top);
911 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400912
913 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400914 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000915 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400916
917 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +0000918 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000919 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000920 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400921
Jane Liu36567742017-07-06 11:13:35 -0400922 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000923 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400924
Lei Zhanga21d5932018-02-05 18:28:38 +0000925 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000926 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000927 ASSERT_TRUE(annot);
928 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400929
Lei Zhanga21d5932018-02-05 18:28:38 +0000930 // Check that the new annotation's rectangle is as defined.
931 FS_RECTF new_rect;
932 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
933 EXPECT_EQ(rect.left, new_rect.left);
934 EXPECT_EQ(rect.bottom, new_rect.bottom);
935 EXPECT_EQ(rect.right, new_rect.right);
936 EXPECT_EQ(rect.top, new_rect.top);
937 }
938
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000939 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400940 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400941}
Jane Liub137e752017-07-05 15:04:33 -0400942
Lei Zhangab41f252018-12-23 03:10:50 +0000943TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400944 // Open a file with an annotation and load its first page.
945 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000946 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400947 ASSERT_TRUE(page);
948
949 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000950 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000951 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000952 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
953 }
Jane Liub137e752017-07-05 15:04:33 -0400954
Lei Zhanga21d5932018-02-05 18:28:38 +0000955 {
956 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000957 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000958 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400959
Lei Zhanga21d5932018-02-05 18:28:38 +0000960 // Check that the original flag values are as expected.
961 int flags = FPDFAnnot_GetFlags(annot.get());
962 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
963 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400964
Lei Zhanga21d5932018-02-05 18:28:38 +0000965 // Set the HIDDEN flag.
966 flags |= FPDF_ANNOT_FLAG_HIDDEN;
967 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
968 flags = FPDFAnnot_GetFlags(annot.get());
969 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
970 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400971
Lei Zhanga21d5932018-02-05 18:28:38 +0000972 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000973 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000974 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +0000975 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000976 }
Jane Liub137e752017-07-05 15:04:33 -0400977
Lei Zhanga21d5932018-02-05 18:28:38 +0000978 // Unset the HIDDEN flag.
979 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
980 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
981 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
982 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
983 flags = FPDFAnnot_GetFlags(annot.get());
984 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
985 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400986
Lei Zhanga21d5932018-02-05 18:28:38 +0000987 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000988 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000989 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000990 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
991 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000992 }
Jane Liub137e752017-07-05 15:04:33 -0400993
Jane Liub137e752017-07-05 15:04:33 -0400994 UnloadPage(page);
995}
Jane Liu36567742017-07-06 11:13:35 -0400996
Lei Zhang03e5e682019-09-16 19:45:55 +0000997// TODO(crbug.com/pdfium/11): Fix this test and enable.
998#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
999#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
1000#else
1001#define MAYBE_AddAndModifyImage AddAndModifyImage
1002#endif
1003TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Lei Zhange4cdac52019-04-30 16:45:57 +00001004#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001005 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
1006 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +00001007#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001008 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
1009 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -04001010#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001011 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
1012 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -04001013#endif
1014
1015 // Open a file with two annotations and load its first page.
1016 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001017 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001018 ASSERT_TRUE(page);
1019 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1020
1021 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001022 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001023 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001024 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001025 }
Jane Liu36567742017-07-06 11:13:35 -04001026
Jane Liu36567742017-07-06 11:13:35 -04001027 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001028 FPDF_BITMAP image_bitmap;
1029
1030 {
1031 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001032 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001033 ASSERT_TRUE(annot);
1034 FS_RECTF rect;
1035 rect.left = 200.f;
1036 rect.bottom = 600.f;
1037 rect.right = 400.f;
1038 rect.top = 800.f;
1039 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1040
1041 // Add a solid-color translucent image object to the new annotation.
1042 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1043 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1044 0xeeeecccc);
1045 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1046 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1047 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1048 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1049 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1050 kBitmapSize, 0, 0));
1051 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1052 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1053 }
Jane Liu36567742017-07-06 11:13:35 -04001054
1055 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001056 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001057 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001058 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001059 }
Jane Liu36567742017-07-06 11:13:35 -04001060
Lei Zhanga21d5932018-02-05 18:28:38 +00001061 {
1062 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001063 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001064 ASSERT_TRUE(annot);
1065 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1066 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1067 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001068
Lei Zhanga21d5932018-02-05 18:28:38 +00001069 // Modify the image in the new annotation.
1070 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1071 0xff000000);
1072 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1073 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1074 }
Jane Liu36567742017-07-06 11:13:35 -04001075
1076 // Save the document, closing the page and document.
1077 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001078 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001079 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001080
1081 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001082 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001083}
1084
Hui Yingst6cd754f2020-05-14 04:05:25 +00001085TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Lei Zhang03e5e682019-09-16 19:45:55 +00001086#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Hui Yingst6cd754f2020-05-14 04:05:25 +00001087#if defined(OS_LINUX)
1088 static const char kMd5NewText[] = "c9d853a5fb6bca31e9696ccc4462c74a";
1089 static const char kMd5ModifiedText[] = "bc681fa9174223983c5e4357e919d36c";
Lei Zhang03e5e682019-09-16 19:45:55 +00001090#else
Hui Yingst6cd754f2020-05-14 04:05:25 +00001091 static const char kMd5NewText[] = "4aaa34e9df2e41d621dbd81b1d535c48";
1092 static const char kMd5ModifiedText[] = "d6ea20beb7834ef4b6d370581ce425fc";
1093#endif // defined(OS_LINUX)
1094#else
1095#if defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001096 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1097 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Hui Yingst6cd754f2020-05-14 04:05:25 +00001098#elif defined(OS_MACOSX)
1099 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1100 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Jane Liu36567742017-07-06 11:13:35 -04001101#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001102 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1103 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001104#endif
Hui Yingst6cd754f2020-05-14 04:05:25 +00001105#endif // defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
Jane Liu36567742017-07-06 11:13:35 -04001106
1107 // Open a file with two annotations and load its first page.
1108 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001109 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001110 ASSERT_TRUE(page);
1111 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1112
1113 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001114 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001115 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001116 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001117 }
Jane Liu36567742017-07-06 11:13:35 -04001118
Lei Zhanga21d5932018-02-05 18:28:38 +00001119 {
1120 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001121 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001122 ASSERT_TRUE(annot);
1123 FS_RECTF rect;
1124 rect.left = 200.f;
1125 rect.bottom = 550.f;
1126 rect.right = 450.f;
1127 rect.top = 650.f;
1128 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001129
Lei Zhanga21d5932018-02-05 18:28:38 +00001130 // Add a translucent text object to the new annotation.
1131 FPDF_PAGEOBJECT text_object =
1132 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1133 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001134 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001135 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1136 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001137 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001138 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1139 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1140 }
Jane Liu36567742017-07-06 11:13:35 -04001141
1142 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001143 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001144 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001145 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001146 }
Jane Liu36567742017-07-06 11:13:35 -04001147
Lei Zhanga21d5932018-02-05 18:28:38 +00001148 {
1149 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001150 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001151 ASSERT_TRUE(annot);
1152 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1153 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1154 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001155
Lei Zhanga21d5932018-02-05 18:28:38 +00001156 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001157 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001158 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1159 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1160 }
Jane Liu36567742017-07-06 11:13:35 -04001161
1162 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001163 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001164 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001165 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001166 }
Jane Liu36567742017-07-06 11:13:35 -04001167
1168 // Remove the new annotation, and check that the page renders as before.
1169 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001170 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001171 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001172 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001173 }
Jane Liu36567742017-07-06 11:13:35 -04001174
1175 UnloadPage(page);
1176}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001177
Lei Zhang03e5e682019-09-16 19:45:55 +00001178// TODO(crbug.com/pdfium/11): Fix this test and enable.
1179#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1180#define MAYBE_GetSetStringValue DISABLED_GetSetStringValue
1181#else
1182#define MAYBE_GetSetStringValue GetSetStringValue
1183#endif
1184TEST_F(FPDFAnnotEmbedderTest, MAYBE_GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001185 // Open a file with four annotations and load its first page.
1186 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001187 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001188 ASSERT_TRUE(page);
1189
Lei Zhang4f556b82019-04-08 16:32:41 +00001190 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001191
Lei Zhanga21d5932018-02-05 18:28:38 +00001192 {
1193 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001194 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001195 ASSERT_TRUE(annot);
1196
1197 // Check that a non-existent key does not exist.
1198 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1199
1200 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001201 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001202 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001203 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1204 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1205 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001206
1207 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001208 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001209 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001210 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001211 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001212 ASSERT_EQ(66u, length_bytes);
1213 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1214 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1215 length_bytes));
1216 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1217 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001218
1219 // Check that the string value of the modified date is correct.
1220 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001221 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1222 nullptr, 0);
1223 ASSERT_EQ(44u, length_bytes);
1224 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001225 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001226 buf.data(), length_bytes));
1227 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001228
1229 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001230 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001231 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1232 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001233 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001234
1235 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001236 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001237 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001238
Lei Zhange4cdac52019-04-30 16:45:57 +00001239#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001240 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001241#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001242 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001243#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001244 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001245#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001246
1247 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001248 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001249 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001250 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001251 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001252 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001253
Lei Zhanga21d5932018-02-05 18:28:38 +00001254 // Check that the string value of the modified date is the newly-set value.
1255 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001256 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001257 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001258 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001259 ASSERT_EQ(44u, length_bytes);
1260 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001261 EXPECT_EQ(44u,
1262 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001263 buf.data(), length_bytes));
1264 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001265 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001266
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001267 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001268 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001269}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001270
rycsmith3e785602019-03-05 21:48:36 +00001271TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001272 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001273 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1274 FPDF_PAGE page = LoadPage(0);
1275 ASSERT_TRUE(page);
1276 {
1277 // First two annotations do not have "MaxLen" attribute.
1278 for (int i = 0; i < 2; i++) {
1279 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1280 ASSERT_TRUE(annot);
1281
1282 // Verify that no "MaxLen" key present.
1283 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1284
1285 float value;
1286 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1287 }
1288
1289 // Annotation in index 2 has "MaxLen" of 10.
1290 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1291 ASSERT_TRUE(annot);
1292
1293 // Verify that "MaxLen" key present.
1294 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1295
1296 float value;
1297 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1298 EXPECT_FLOAT_EQ(10.0f, value);
1299
1300 // Check bad inputs.
1301 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1302 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1303 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1304 // Ask for key that exists but is not a number.
1305 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1306 }
1307
1308 UnloadPage(page);
1309}
1310
Lei Zhangab41f252018-12-23 03:10:50 +00001311TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001312 // Open a file with four annotations and load its first page.
1313 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001314 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001315 ASSERT_TRUE(page);
1316
Lei Zhanga21d5932018-02-05 18:28:38 +00001317 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001318 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1319 static constexpr size_t kExpectNormalAPLength = 73970;
1320
Lei Zhanga21d5932018-02-05 18:28:38 +00001321 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001322 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001323 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001324
Lei Zhanga21d5932018-02-05 18:28:38 +00001325 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001326 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001327 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001328 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001329
Lei Zhanga21d5932018-02-05 18:28:38 +00001330 // Check that the string value of an AP is not returned if the buffer is too
1331 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001332 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1333 // Write in the buffer to verify it's not overwritten.
1334 memcpy(buf.data(), "abcdefgh", 8);
1335 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001336 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001337 buf.data(), normal_length_bytes - 1));
1338 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001339
Lei Zhanga21d5932018-02-05 18:28:38 +00001340 // Check that the string value of an AP is returned through a buffer that is
1341 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001342 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001343 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001344 buf.data(), normal_length_bytes));
1345 EXPECT_EQ(kMd5NormalAP,
1346 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1347 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001348
Lei Zhanga21d5932018-02-05 18:28:38 +00001349 // Check that the string value of an AP is returned through a buffer that is
1350 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001351 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1352 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001353 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001354 buf.data(), normal_length_bytes + 2));
1355 EXPECT_EQ(kMd5NormalAP,
1356 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1357 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001358
Lei Zhanga21d5932018-02-05 18:28:38 +00001359 // Check that getting an AP for a mode that does not have an AP returns an
1360 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001361 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001362 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001363 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001364
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001365 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001366 EXPECT_EQ(2u,
1367 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001368 buf.data(), 1000));
1369 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001370
Lei Zhanga21d5932018-02-05 18:28:38 +00001371 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001372 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001373 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001374 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001375 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001376 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001377 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001378
Lei Zhanga21d5932018-02-05 18:28:38 +00001379 // Set the AP correctly now.
1380 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001381 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001382
Lei Zhanga21d5932018-02-05 18:28:38 +00001383 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001384 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001385 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001386 ASSERT_EQ(24u, rollover_length_bytes);
1387 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001388 EXPECT_EQ(24u,
1389 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001390 buf.data(), rollover_length_bytes));
1391 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001392
Lei Zhanga21d5932018-02-05 18:28:38 +00001393 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001394 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1395 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001396 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001397 buf.data(), normal_length_bytes));
1398 EXPECT_EQ(kMd5NormalAP,
1399 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1400 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001401 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001402
1403 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001404 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001405 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001406
Lei Zhang0b494052019-01-31 21:41:15 +00001407 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001408 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001409 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001410 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001411
Lei Zhanga21d5932018-02-05 18:28:38 +00001412 // Check that the new annotation value is equal to the value we set before
1413 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001414 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001415 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001416 ASSERT_EQ(24u, rollover_length_bytes);
1417 std::vector<FPDF_WCHAR> buf =
1418 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001419 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1420 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001421 buf.data(), rollover_length_bytes));
1422 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001423 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001424
1425 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001426 CloseSavedPage(page);
1427 CloseSavedDocument();
1428}
1429
Lei Zhangab41f252018-12-23 03:10:50 +00001430TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001431 // Open a file with four annotations and load its first page.
1432 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001433 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001434 ASSERT_TRUE(page);
1435
Lei Zhanga21d5932018-02-05 18:28:38 +00001436 {
1437 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001438 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001439 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001440
Lei Zhanga21d5932018-02-05 18:28:38 +00001441 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001442 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001443 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001444 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001445 EXPECT_EQ(73970u,
1446 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1447 nullptr, 0));
1448 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1449 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001450
Lei Zhanga21d5932018-02-05 18:28:38 +00001451 // Check that setting the Down AP to null removes the Down entry but keeps
1452 // Normal intact.
1453 EXPECT_TRUE(
1454 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1455 EXPECT_EQ(73970u,
1456 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1457 nullptr, 0));
1458 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1459 nullptr, 0));
1460 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001461
Lei Zhang75c81712018-02-08 17:22:39 +00001462 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001463}
1464
Lei Zhangab41f252018-12-23 03:10:50 +00001465TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001466 // Open a file with four annotations and load its first page.
1467 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001468 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001469 ASSERT_TRUE(page);
1470
Lei Zhanga21d5932018-02-05 18:28:38 +00001471 {
1472 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001473 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001474 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001475
Lei Zhanga21d5932018-02-05 18:28:38 +00001476 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001477 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001478 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001479 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001480 EXPECT_EQ(73970u,
1481 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1482 nullptr, 0));
1483 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1484 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001485
Lei Zhanga21d5932018-02-05 18:28:38 +00001486 // Check that setting the Normal AP to null removes the whole AP dictionary.
1487 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1488 nullptr));
1489 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1490 nullptr, 0));
1491 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1492 nullptr, 0));
1493 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001494
Lei Zhang75c81712018-02-08 17:22:39 +00001495 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001496}
1497
Lei Zhangab41f252018-12-23 03:10:50 +00001498TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001499 // Open a file with annotations and load its first page.
1500 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001501 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001502 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001503 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001504
Lei Zhanga21d5932018-02-05 18:28:38 +00001505 {
1506 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001507 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001508 ASSERT_TRUE(annot);
1509 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1510 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001511 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001512 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1513 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1514 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001515
Lei Zhanga21d5932018-02-05 18:28:38 +00001516 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001517 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001518 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1519 ASSERT_TRUE(popup);
1520 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1521 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1522 FS_RECTF rect;
1523 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1524 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1525 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001526
Lei Zhanga21d5932018-02-05 18:28:38 +00001527 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1528 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001529 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001530 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1531 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001532
Lei Zhanga21d5932018-02-05 18:28:38 +00001533 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1534 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001535 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001536 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001537 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1538 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001539 }
Jane Liu300bb272017-08-21 14:37:53 -04001540
Jane Liu300bb272017-08-21 14:37:53 -04001541 UnloadPage(page);
1542}
1543
Lei Zhangab41f252018-12-23 03:10:50 +00001544TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001545 // Open file with form text fields.
1546 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001547 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001548 ASSERT_TRUE(page);
1549
Lei Zhanga21d5932018-02-05 18:28:38 +00001550 {
1551 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001552 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001553 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001554
Lei Zhanga21d5932018-02-05 18:28:38 +00001555 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001556 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001557 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001558 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001559 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001560
Lei Zhanga21d5932018-02-05 18:28:38 +00001561 {
1562 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001563 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001564 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001565
Lei Zhanga21d5932018-02-05 18:28:38 +00001566 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001567 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001568 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001569 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1570 }
1571
1572 {
1573 // Retrieve the fourth annotation: user-editable password text field.
1574 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1575 ASSERT_TRUE(annot);
1576
1577 // Check that the flag values are as expected.
1578 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1579 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1580 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001581 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001582
1583 UnloadPage(page);
1584}
1585
Lei Zhangab41f252018-12-23 03:10:50 +00001586TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001587 // Open file with form text fields.
1588 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001589 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001590 ASSERT_TRUE(page);
1591
Lei Zhanga21d5932018-02-05 18:28:38 +00001592 {
1593 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001594 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001595 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001596
Lei Zhanga21d5932018-02-05 18:28:38 +00001597 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001598 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001599 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1600 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1601 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1602 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001603
Lei Zhanga21d5932018-02-05 18:28:38 +00001604 {
1605 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001606 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001607 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001608
Lei Zhanga21d5932018-02-05 18:28:38 +00001609 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001610 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001611 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1612 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1613 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1614 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001615
Lei Zhanga21d5932018-02-05 18:28:38 +00001616 {
1617 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001618 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001619 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001620
Lei Zhanga21d5932018-02-05 18:28:38 +00001621 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001622 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001623 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1624 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1625 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1626 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001627
1628 UnloadPage(page);
1629}
Diana Gage40870db2017-07-19 18:16:03 -07001630
Lei Zhangab41f252018-12-23 03:10:50 +00001631TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001632 // Open file with form text fields.
1633 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1634 FPDF_PAGE page = LoadPage(0);
1635 ASSERT_TRUE(page);
1636
1637 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001638 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1639 EXPECT_FALSE(
1640 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001641
Lei Zhang8da98232019-12-11 23:29:33 +00001642 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001643 {
1644 // Verify there is an annotation.
1645 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001646 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001647 EXPECT_TRUE(annot);
1648 }
1649
1650 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001651 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1652 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1653 EXPECT_FALSE(
1654 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001655
1656 UnloadPage(page);
1657}
1658
Lei Zhangab41f252018-12-23 03:10:50 +00001659TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001660 // Open file with form text fields.
1661 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1662 FPDF_PAGE page = LoadPage(0);
1663 ASSERT_TRUE(page);
1664
Lei Zhanga21d5932018-02-05 18:28:38 +00001665 {
1666 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001667 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001668 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001669 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001670 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001671
Lei Zhanga21d5932018-02-05 18:28:38 +00001672 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001673 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001674 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1675 }
Diana Gage40870db2017-07-19 18:16:03 -07001676
Lei Zhanga21d5932018-02-05 18:28:38 +00001677 {
1678 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001679 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001680 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001681 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001682 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001683
Lei Zhanga21d5932018-02-05 18:28:38 +00001684 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001685 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001686 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1687 }
Diana Gage40870db2017-07-19 18:16:03 -07001688
1689 UnloadPage(page);
1690}
1691
Lei Zhangab41f252018-12-23 03:10:50 +00001692TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001693 // Open file with form comboboxes.
1694 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1695 FPDF_PAGE page = LoadPage(0);
1696 ASSERT_TRUE(page);
1697
Lei Zhanga21d5932018-02-05 18:28:38 +00001698 {
1699 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001700 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001701 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001702 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001703 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001704
Lei Zhanga21d5932018-02-05 18:28:38 +00001705 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001706 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001707 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1708 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1709 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1710 }
Diana Gage40870db2017-07-19 18:16:03 -07001711
Lei Zhanga21d5932018-02-05 18:28:38 +00001712 {
1713 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001714 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001715 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001716 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001717 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001718
Lei Zhanga21d5932018-02-05 18:28:38 +00001719 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001720 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001721 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1722 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1723 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1724 }
Diana Gage40870db2017-07-19 18:16:03 -07001725
Lei Zhanga21d5932018-02-05 18:28:38 +00001726 {
1727 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001728 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001729 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001730 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001731 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001732
Lei Zhanga21d5932018-02-05 18:28:38 +00001733 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001734 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001735 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1736 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1737 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1738 }
Diana Gage40870db2017-07-19 18:16:03 -07001739
1740 UnloadPage(page);
1741}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001742
Lei Zhang03e5e682019-09-16 19:45:55 +00001743// TODO(crbug.com/pdfium/11): Fix this test and enable.
1744#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1745#define MAYBE_BUG_1206 DISABLED_BUG_1206
1746#else
1747#define MAYBE_BUG_1206 BUG_1206
1748#endif
1749TEST_F(FPDFAnnotEmbedderTest, MAYBE_BUG_1206) {
Lei Zhang992e7e22019-02-04 19:20:58 +00001750 static constexpr size_t kExpectedSize = 1609;
1751 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1752
1753 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1754
1755 FPDF_PAGE page = LoadPage(0);
1756 ASSERT_TRUE(page);
1757
1758 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1759 EXPECT_EQ(kExpectedSize, GetString().size());
1760 ClearString();
1761
1762 for (size_t i = 0; i < 10; ++i) {
1763 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1764 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
1765
1766 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1767 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
1768 // equal, not bigger.
1769 EXPECT_LT(kExpectedSize, GetString().size());
1770 ClearString();
1771 }
1772
1773 UnloadPage(page);
1774}
1775
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001776TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1777 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1778 FPDF_PAGE page = LoadPage(0);
1779 ASSERT_TRUE(page);
1780 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1781
1782 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00001783 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001784 static const size_t kBufSize = 12;
1785 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001786
1787 {
1788 // Add a text annotation to the page.
1789 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1790 ASSERT_TRUE(annot);
1791 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1792 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1793
1794 // Make sure there is no test key, add set a value there, and read it back.
1795 std::fill(buf.begin(), buf.end(), 'x');
1796 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001797 kBufSize));
1798 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001799
Lei Zhangf0f67682019-04-08 17:03:21 +00001800 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001801 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1802
1803 std::fill(buf.begin(), buf.end(), 'x');
1804 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001805 kBufSize));
1806 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001807 }
1808
Lei Zhang05ec64c2019-01-09 03:00:06 +00001809 {
1810 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1811 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00001812 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00001813 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001814 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1815 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1816 // Also do the same test for its appearance string.
1817 std::fill(buf.begin(), buf.end(), 'x');
1818 ASSERT_EQ(2u,
1819 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001820 buf.data(), kBufSize));
1821 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001822
Lei Zhangf0f67682019-04-08 17:03:21 +00001823 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00001824 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1825 text.get()));
1826
1827 std::fill(buf.begin(), buf.end(), 'x');
1828 ASSERT_EQ(6u,
1829 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001830 buf.data(), kBufSize));
1831 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001832 }
1833
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001834 UnloadPage(page);
1835
1836 {
1837 // Save a copy, open the copy, and check the annotation again.
1838 // Note that it renders the rotation.
1839 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001840 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001841 FPDF_PAGE saved_page = LoadSavedPage(0);
1842 ASSERT_TRUE(saved_page);
1843
Lei Zhang05ec64c2019-01-09 03:00:06 +00001844 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001845 {
1846 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1847 ASSERT_TRUE(annot);
1848 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1849
1850 std::fill(buf.begin(), buf.end(), 'x');
1851 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001852 kBufSize));
1853 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001854 }
1855
Lei Zhang05ec64c2019-01-09 03:00:06 +00001856 {
1857 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1858 ASSERT_TRUE(annot);
1859 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1860 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1861
1862 std::fill(buf.begin(), buf.end(), 'x');
1863 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001864 kBufSize));
1865 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001866 }
1867
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001868 CloseSavedPage(saved_page);
1869 CloseSavedDocument();
1870 }
1871}
rycsmithcb752f32019-02-21 18:40:53 +00001872
1873TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
1874 // Open a file with combobox widget annotations and load its first page.
1875 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1876 FPDF_PAGE page = LoadPage(0);
1877 ASSERT_TRUE(page);
1878
1879 {
1880 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1881 ASSERT_TRUE(annot);
1882
1883 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1884
1885 annot.reset(FPDFPage_GetAnnot(page, 1));
1886 ASSERT_TRUE(annot);
1887
1888 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00001889
1890 // Check bad form handle / annot.
1891 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
1892 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
1893 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00001894 }
1895
1896 UnloadPage(page);
1897}
1898
1899TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
1900 // Open a file with listbox widget annotations and load its first page.
1901 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1902 FPDF_PAGE page = LoadPage(0);
1903 ASSERT_TRUE(page);
1904
1905 {
1906 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1907 ASSERT_TRUE(annot);
1908
1909 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1910
1911 annot.reset(FPDFPage_GetAnnot(page, 1));
1912 ASSERT_TRUE(annot);
1913
1914 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1915 }
1916
1917 UnloadPage(page);
1918}
1919
1920TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
1921 // Open a file with ink annotations and load its first page.
1922 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1923 FPDF_PAGE page = LoadPage(0);
1924 ASSERT_TRUE(page);
1925
1926 {
1927 // annotations do not have "Opt" array and will return -1
1928 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1929 ASSERT_TRUE(annot);
1930
1931 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1932
1933 annot.reset(FPDFPage_GetAnnot(page, 1));
1934 ASSERT_TRUE(annot);
1935
1936 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1937 }
1938
1939 UnloadPage(page);
1940}
1941
1942TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
1943 // Open a file with combobox widget annotations and load its first page.
1944 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1945 FPDF_PAGE page = LoadPage(0);
1946 ASSERT_TRUE(page);
1947
1948 {
1949 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1950 ASSERT_TRUE(annot);
1951
1952 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001953 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001954 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001955 ASSERT_EQ(8u, length_bytes);
1956 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001957 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001958 buf.data(), length_bytes));
1959 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001960
1961 annot.reset(FPDFPage_GetAnnot(page, 1));
1962 ASSERT_TRUE(annot);
1963
1964 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001965 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001966 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001967 ASSERT_EQ(12u, length_bytes);
1968 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001969 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001970 buf.data(), length_bytes));
1971 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001972
1973 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001974 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001975 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001976 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001977 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001978 buf.data(), length_bytes));
1979 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001980
Lei Zhange7033c82019-02-26 19:30:49 +00001981 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00001982 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1983 nullptr, 0));
1984 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1985 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00001986
1987 // Check bad form handle / annot.
1988 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
1989 EXPECT_EQ(0u,
1990 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
1991 EXPECT_EQ(0u,
1992 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00001993 }
1994
1995 UnloadPage(page);
1996}
1997
1998TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
1999 // Open a file with listbox widget annotations and load its first page.
2000 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2001 FPDF_PAGE page = LoadPage(0);
2002 ASSERT_TRUE(page);
2003
2004 {
2005 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2006 ASSERT_TRUE(annot);
2007
2008 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002009 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002010 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002011 ASSERT_EQ(8u, length_bytes);
2012 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002013 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002014 buf.data(), length_bytes));
2015 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002016
2017 annot.reset(FPDFPage_GetAnnot(page, 1));
2018 ASSERT_TRUE(annot);
2019
2020 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002021 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002022 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002023 ASSERT_EQ(12u, length_bytes);
2024 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002025 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002026 buf.data(), length_bytes));
2027 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002028
2029 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002030 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002031 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002032 ASSERT_EQ(18u, length_bytes);
2033 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002034 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002035 buf.data(), length_bytes));
2036 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002037
2038 // indices out of range
2039 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2040 nullptr, 0));
2041 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2042 nullptr, 0));
2043 }
2044
2045 UnloadPage(page);
2046}
2047
2048TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2049 // Open a file with ink annotations and load its first page.
2050 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2051 FPDF_PAGE page = LoadPage(0);
2052 ASSERT_TRUE(page);
2053
2054 {
2055 // annotations do not have "Opt" array and will return 0
2056 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2057 ASSERT_TRUE(annot);
2058
2059 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2060 nullptr, 0));
2061
2062 annot.reset(FPDFPage_GetAnnot(page, 1));
2063 ASSERT_TRUE(annot);
2064
2065 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2066 nullptr, 0));
2067 }
2068
2069 UnloadPage(page);
2070}
Ryan Smith09c23b12019-04-25 18:09:06 +00002071
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002072TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2073 // Open a file with combobox widget annotations and load its first page.
2074 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2075 FPDF_PAGE page = LoadPage(0);
2076 ASSERT_TRUE(page);
2077
2078 {
2079 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2080 ASSERT_TRUE(annot);
2081
2082 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2083 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2084 ASSERT_EQ(3, count);
2085 for (int i = 0; i < count; i++) {
2086 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2087 }
2088
2089 annot.reset(FPDFPage_GetAnnot(page, 1));
2090 ASSERT_TRUE(annot);
2091
2092 // Checks for Combobox with Values (/V) object which is just a string.
2093 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2094 ASSERT_EQ(26, count);
2095 for (int i = 0; i < count; i++) {
2096 EXPECT_EQ(i == 1,
2097 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2098 }
2099
2100 // Checks for index outside bound i.e. (index >= CountOption()).
2101 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2102 /*index=*/26));
2103 // Checks for negetive index.
2104 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2105 /*index=*/-1));
2106
2107 // Checks for bad form handle/annot.
2108 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2109 EXPECT_FALSE(
2110 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2111 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2112 }
2113
2114 UnloadPage(page);
2115}
2116
2117TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2118 // Open a file with listbox widget annotations and load its first page.
2119 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2120 FPDF_PAGE page = LoadPage(0);
2121 ASSERT_TRUE(page);
2122
2123 {
2124 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2125 ASSERT_TRUE(annot);
2126
2127 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2128 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2129 ASSERT_EQ(3, count);
2130 for (int i = 0; i < count; i++) {
2131 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2132 }
2133
2134 annot.reset(FPDFPage_GetAnnot(page, 1));
2135 ASSERT_TRUE(annot);
2136
2137 // Checks for Listbox with Values (/V) object which is just a string.
2138 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2139 ASSERT_EQ(26, count);
2140 for (int i = 0; i < count; i++) {
2141 EXPECT_EQ(i == 1,
2142 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2143 }
2144
2145 annot.reset(FPDFPage_GetAnnot(page, 3));
2146 ASSERT_TRUE(annot);
2147
2148 // Checks for Listbox with only Selected indices (/I) object which is an
2149 // array with multiple objects.
2150 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2151 ASSERT_EQ(5, count);
2152 for (int i = 0; i < count; i++) {
2153 bool expected = (i == 1 || i == 3);
2154 EXPECT_EQ(expected,
2155 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2156 }
2157
2158 annot.reset(FPDFPage_GetAnnot(page, 4));
2159 ASSERT_TRUE(annot);
2160
2161 // Checks for Listbox with Values (/V) object which is an array with
2162 // multiple objects.
2163 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2164 ASSERT_EQ(5, count);
2165 for (int i = 0; i < count; i++) {
2166 bool expected = (i == 2 || i == 4);
2167 EXPECT_EQ(expected,
2168 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2169 }
2170
2171 annot.reset(FPDFPage_GetAnnot(page, 5));
2172 ASSERT_TRUE(annot);
2173
2174 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2175 // objects conflict with different lengths.
2176 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2177 ASSERT_EQ(5, count);
2178 for (int i = 0; i < count; i++) {
2179 bool expected = (i == 0 || i == 2);
2180 EXPECT_EQ(expected,
2181 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2182 }
2183 }
2184
2185 UnloadPage(page);
2186}
2187
2188TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2189 // Open a file with multiple form field annotations and load its first page.
2190 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2191 FPDF_PAGE page = LoadPage(0);
2192 ASSERT_TRUE(page);
2193
2194 {
2195 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2196 ASSERT_TRUE(annot);
2197
2198 // Checks for link annotation.
2199 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2200 /*index=*/0));
2201
2202 annot.reset(FPDFPage_GetAnnot(page, 3));
2203 ASSERT_TRUE(annot);
2204
2205 // Checks for text field annotation.
2206 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2207 /*index=*/0));
2208 }
2209
2210 UnloadPage(page);
2211}
2212
Ryan Smith09c23b12019-04-25 18:09:06 +00002213TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2214 // Open a file with combobox annotations and load its first page.
2215 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2216 FPDF_PAGE page = LoadPage(0);
2217 ASSERT_TRUE(page);
2218
2219 {
2220 // All 3 widgets have Tf font size 12.
2221 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2222 ASSERT_TRUE(annot);
2223
2224 float value;
2225 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2226 EXPECT_EQ(12.0, value);
2227
2228 annot.reset(FPDFPage_GetAnnot(page, 1));
2229 ASSERT_TRUE(annot);
2230
2231 float value_two;
2232 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2233 EXPECT_EQ(12.0, value_two);
2234
2235 annot.reset(FPDFPage_GetAnnot(page, 2));
2236 ASSERT_TRUE(annot);
2237
2238 float value_three;
2239 ASSERT_TRUE(
2240 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2241 EXPECT_EQ(12.0, value_three);
2242 }
2243
2244 UnloadPage(page);
2245}
2246
2247TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2248 // Open a file with textfield annotations and load its first page.
2249 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2250 FPDF_PAGE page = LoadPage(0);
2251 ASSERT_TRUE(page);
2252
2253 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002254 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002255 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2256 ASSERT_TRUE(annot);
2257
2258 float value;
2259 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2260 EXPECT_EQ(12.0, value);
2261
2262 annot.reset(FPDFPage_GetAnnot(page, 1));
2263 ASSERT_TRUE(annot);
2264
2265 float value_two;
2266 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2267 EXPECT_EQ(12.0, value_two);
2268
2269 annot.reset(FPDFPage_GetAnnot(page, 2));
2270 ASSERT_TRUE(annot);
2271
2272 float value_three;
2273 ASSERT_TRUE(
2274 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2275 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002276
2277 float value_four;
2278 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2279 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002280 }
2281
2282 UnloadPage(page);
2283}
2284
2285TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2286 // Open a file with ink annotations and load its first page.
2287 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2288 FPDF_PAGE page = LoadPage(0);
2289 ASSERT_TRUE(page);
2290
2291 {
2292 // Annotations that do not have variable text and will return -1.
2293 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2294 ASSERT_TRUE(annot);
2295
2296 float value;
2297 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2298
2299 annot.reset(FPDFPage_GetAnnot(page, 1));
2300 ASSERT_TRUE(annot);
2301
2302 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2303 }
2304
2305 UnloadPage(page);
2306}
2307
2308TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2309 // Open a file with combobox annotations and load its first page.
2310 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2311 FPDF_PAGE page = LoadPage(0);
2312 ASSERT_TRUE(page);
2313
2314 {
2315 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2316 ASSERT_TRUE(annot);
2317
2318 // Check bad form handle / annot.
2319 float value;
2320 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2321 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2322 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2323 }
2324
2325 UnloadPage(page);
2326}
2327
2328TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2329 // Open a file with textfield annotations and load its first page.
2330 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2331 FPDF_PAGE page = LoadPage(0);
2332 ASSERT_TRUE(page);
2333
2334 {
2335 // Obtain the first annotation, a text field with negative font size, -12.
2336 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2337 ASSERT_TRUE(annot);
2338
2339 float value;
2340 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2341 EXPECT_EQ(-12.0, value);
2342 }
2343
2344 UnloadPage(page);
2345}
Ryan Smith23fdf892019-07-17 21:51:26 +00002346
2347TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2348 // Open a file with checkbox and radiobuttons widget annotations and load its
2349 // first page.
2350 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2351 FPDF_PAGE page = LoadPage(0);
2352 ASSERT_TRUE(page);
2353
2354 {
2355 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2356 ASSERT_TRUE(annot);
2357 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2358 }
2359
2360 UnloadPage(page);
2361}
2362
2363TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2364 // Open a file with checkbox and radiobutton widget annotations and load its
2365 // first page.
2366 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2367 FPDF_PAGE page = LoadPage(0);
2368 ASSERT_TRUE(page);
2369
2370 {
2371 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2372 ASSERT_TRUE(annot);
2373 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2374 }
2375
2376 UnloadPage(page);
2377}
2378
2379TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2380 // Open a file with checkbox and radiobutton widget annotations and load its
2381 // first page.
2382 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2383 FPDF_PAGE page = LoadPage(0);
2384 ASSERT_TRUE(page);
2385
2386 {
2387 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2388 ASSERT_TRUE(annot);
2389 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2390
2391 annot.reset(FPDFPage_GetAnnot(page, 6));
2392 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2393
2394 annot.reset(FPDFPage_GetAnnot(page, 7));
2395 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2396 }
2397
2398 UnloadPage(page);
2399}
2400
2401TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2402 // Open a file with checkbox and radiobutton widget annotations and load its
2403 // first page.
2404 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2405 FPDF_PAGE page = LoadPage(0);
2406 ASSERT_TRUE(page);
2407
2408 {
2409 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2410 ASSERT_TRUE(annot);
2411 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2412
2413 annot.reset(FPDFPage_GetAnnot(page, 3));
2414 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2415
2416 annot.reset(FPDFPage_GetAnnot(page, 4));
2417 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2418 }
2419
2420 UnloadPage(page);
2421}
2422
2423TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2424 // Open a file with checkbox and radiobuttons widget annotations and load its
2425 // first page.
2426 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2427 FPDF_PAGE page = LoadPage(0);
2428 ASSERT_TRUE(page);
2429
2430 {
2431 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2432 ASSERT_TRUE(annot);
2433 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2434 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2435 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2436 }
2437
2438 UnloadPage(page);
2439}
2440
2441TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2442 // Open a file with text widget annotations and load its first page.
2443 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2444 FPDF_PAGE page = LoadPage(0);
2445 ASSERT_TRUE(page);
2446
2447 {
2448 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2449 ASSERT_TRUE(annot);
2450 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2451 }
2452
2453 UnloadPage(page);
2454}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002455
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002456TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2457 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002458 FPDF_PAGE page = LoadPage(0);
2459 ASSERT_TRUE(page);
2460
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002461 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2462
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002463 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002464 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002465 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002466 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002467 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002468
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002469 constexpr int kExpectedAnnotTypes[] = {-1,
2470 FPDF_FORMFIELD_COMBOBOX,
2471 FPDF_FORMFIELD_LISTBOX,
2472 FPDF_FORMFIELD_TEXTFIELD,
2473 FPDF_FORMFIELD_CHECKBOX,
2474 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002475
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002476 for (size_t i = 0; i < FX_ArraySize(kExpectedAnnotTypes); ++i) {
2477 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002478 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002479 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002480 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2481 }
2482 UnloadPage(page);
2483}
2484
2485TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2486 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2487 FPDF_PAGE page = LoadPage(0);
2488 ASSERT_TRUE(page);
2489
2490 {
2491 EXPECT_EQ(0u,
2492 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2493
2494 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2495 ASSERT_TRUE(annot);
2496
2497 EXPECT_EQ(0u,
2498 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2499
2500 unsigned long length_bytes =
2501 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2502 ASSERT_EQ(2u, length_bytes);
2503 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2504 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2505 buf.data(), length_bytes));
2506 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2507 }
2508 {
2509 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2510 ASSERT_TRUE(annot);
2511
2512 unsigned long length_bytes =
2513 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2514 ASSERT_EQ(18u, length_bytes);
2515 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2516 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2517 buf.data(), length_bytes));
2518 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2519 }
2520 UnloadPage(page);
2521}
2522
2523TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2524 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2525 FPDF_PAGE page = LoadPage(0);
2526 ASSERT_TRUE(page);
2527
2528 {
2529 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2530 ASSERT_TRUE(annot);
2531
2532 unsigned long length_bytes =
2533 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2534 ASSERT_EQ(2u, length_bytes);
2535 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2536 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2537 buf.data(), length_bytes));
2538 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2539 }
2540 {
2541 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2542 ASSERT_TRUE(annot);
2543
2544 unsigned long length_bytes =
2545 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2546 ASSERT_EQ(14u, length_bytes);
2547 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2548 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2549 buf.data(), length_bytes));
2550 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2551 }
2552 UnloadPage(page);
2553}
2554
2555TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2556 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2557 FPDF_PAGE page = LoadPage(0);
2558 ASSERT_TRUE(page);
2559
2560 {
2561 EXPECT_EQ(0u,
2562 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2563
2564 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2565 ASSERT_TRUE(annot);
2566
2567 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2568
2569 unsigned long length_bytes =
2570 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2571 ASSERT_EQ(18u, length_bytes);
2572 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2573 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2574 buf.data(), length_bytes));
2575 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2576 }
2577 UnloadPage(page);
2578}
2579
2580TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2581 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2582 FPDF_PAGE page = LoadPage(0);
2583 ASSERT_TRUE(page);
2584
2585 {
2586 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2587 ASSERT_TRUE(annot);
2588
2589 unsigned long length_bytes =
2590 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2591 ASSERT_EQ(30u, length_bytes);
2592 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2593 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2594 buf.data(), length_bytes));
2595 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2596 }
2597 UnloadPage(page);
2598}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002599
Lei Zhang9b444002020-04-17 17:35:23 +00002600TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2601 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002602 FPDF_PAGE page = LoadPage(0);
2603 ASSERT_TRUE(page);
2604
Lei Zhang9b444002020-04-17 17:35:23 +00002605 // Verify widgets are by default focusable.
2606 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2607 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002608
Lei Zhang9b444002020-04-17 17:35:23 +00002609 // Expected annot subtypes for page 0 of annots.pdf.
2610 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2611 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2612 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2613 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2614 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002615
Lei Zhang9b444002020-04-17 17:35:23 +00002616 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002617 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002618 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2619 kExpectedAnnotSubtypes,
2620 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002621
Lei Zhang9b444002020-04-17 17:35:23 +00002622 // Make no annotation type focusable using the preferred method.
2623 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2624 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002625
Lei Zhang9b444002020-04-17 17:35:23 +00002626 // Restore the focusable type count back to 1, then set it back to 0 using a
2627 // different method.
2628 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2629 ASSERT_TRUE(
2630 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2631 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002632
Lei Zhang9b444002020-04-17 17:35:23 +00002633 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002634 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002635
Lei Zhang9b444002020-04-17 17:35:23 +00002636 // Now make links focusable.
2637 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2638 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002639
Lei Zhang9b444002020-04-17 17:35:23 +00002640 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002641 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002642 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2643 kExpectedAnnotSubtypes,
2644 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002645
Lei Zhang9b444002020-04-17 17:35:23 +00002646 // Test invalid parameters.
2647 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
2648 FX_ArraySize(kDefaultSubtypes)));
2649 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
2650 FX_ArraySize(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002651 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2652
Lei Zhang9b444002020-04-17 17:35:23 +00002653 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2654 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2655 subtypes.size()));
2656 EXPECT_FALSE(
2657 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2658 EXPECT_FALSE(
2659 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002660
2661 UnloadPage(page);
2662}
Lei Zhang671aece2020-04-14 19:02:26 +00002663
Hui Yingst609f7d62020-04-23 23:14:13 +00002664// TODO(crbug.com/pdfium/11): Fix this test and enable.
2665#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2666#define MAYBE_FocusableAnnotRendering DISABLED_FocusableAnnotRendering
2667#else
2668#define MAYBE_FocusableAnnotRendering FocusableAnnotRendering
2669#endif
2670TEST_F(FPDFAnnotEmbedderTest, MAYBE_FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00002671 ASSERT_TRUE(OpenDocument("annots.pdf"));
2672 FPDF_PAGE page = LoadPage(0);
2673 ASSERT_TRUE(page);
2674
2675 {
2676#if defined(OS_WIN)
2677 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
2678#elif defined(OS_MACOSX)
2679 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
2680#else
2681 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
2682#endif
2683 // Check the initial rendering.
2684 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2685 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2686 }
2687
2688 // Make links and highlights focusable.
2689 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
2690 FPDF_ANNOT_HIGHLIGHT};
2691 constexpr int kSubTypesCount = FX_ArraySize(kSubTypes);
2692 ASSERT_TRUE(
2693 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
2694 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
2695 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
2696 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
2697 subtypes.size()));
2698 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
2699 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
2700
2701 {
2702#if defined(OS_WIN)
2703 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
2704#elif defined(OS_MACOSX)
2705 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
2706#else
2707 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
2708#endif
2709 // Focus the first link and check the rendering.
2710 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2711 ASSERT_TRUE(annot);
2712 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2713 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2714 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2715 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2716 }
2717
2718 {
2719#if defined(OS_WIN)
2720 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
2721#elif defined(OS_MACOSX)
2722 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
2723#else
2724 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
2725#endif
2726 // Focus the first highlight and check the rendering.
2727 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2728 ASSERT_TRUE(annot);
2729 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
2730 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2731 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2732 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2733 }
2734
2735 UnloadPage(page);
2736}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00002737
2738TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
2739 ASSERT_TRUE(OpenDocument("annots.pdf"));
2740 FPDF_PAGE page = LoadPage(0);
2741 ASSERT_TRUE(page);
2742 {
2743 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
2744 ASSERT_TRUE(annot);
2745 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2746 FPDF_LINK link_annot = FPDFAnnot_GetLink(annot.get());
2747 ASSERT_TRUE(link_annot);
2748
2749 FPDF_ACTION action = FPDFLink_GetAction(link_annot);
2750 ASSERT_TRUE(action);
2751 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
2752 FPDFAction_GetType(action));
2753
2754 constexpr char kExpectedResult[] =
2755 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
2756 "fpdf_text.h";
2757 constexpr unsigned long kExpectedLength = FX_ArraySize(kExpectedResult);
2758 unsigned long bufsize =
2759 FPDFAction_GetURIPath(document(), action, nullptr, 0);
2760 ASSERT_EQ(kExpectedLength, bufsize);
2761
2762 char buffer[1024];
2763 EXPECT_EQ(bufsize,
2764 FPDFAction_GetURIPath(document(), action, buffer, bufsize));
2765 EXPECT_STREQ(kExpectedResult, buffer);
2766 }
2767
2768 {
2769 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2770 ASSERT_TRUE(annot);
2771 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
2772 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
2773 }
2774
2775 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
2776
2777 UnloadPage(page);
2778}
Mansi Awasthi23bba0e2020-05-15 12:18:25 +00002779
2780TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountRadioButton) {
2781 // Open a file with radio button widget annotations and load its first page.
2782 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2783 FPDF_PAGE page = LoadPage(0);
2784 ASSERT_TRUE(page);
2785
2786 {
2787 // Checks for bad annot.
2788 EXPECT_EQ(-1,
2789 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
2790
2791 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
2792 ASSERT_TRUE(annot);
2793
2794 // Checks for bad form handle.
2795 EXPECT_EQ(-1,
2796 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
2797
2798 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
2799 }
2800
2801 UnloadPage(page);
2802}
2803
2804TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountCheckBox) {
2805 // Open a file with checkbox widget annotations and load its first page.
2806 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2807 FPDF_PAGE page = LoadPage(0);
2808 ASSERT_TRUE(page);
2809
2810 {
2811 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2812 ASSERT_TRUE(annot);
2813 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
2814 }
2815
2816 UnloadPage(page);
2817}
2818
2819TEST_F(FPDFAnnotEmbedderTest, GetFormControlCountInvalidAnnotation) {
2820 // Open a file with ink annotations and load its first page.
2821 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2822 FPDF_PAGE page = LoadPage(0);
2823 ASSERT_TRUE(page);
2824
2825 {
2826 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2827 ASSERT_TRUE(annot);
2828 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
2829 }
2830
2831 UnloadPage(page);
2832}
2833
2834TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexRadioButton) {
2835 // Open a file with radio button widget annotations and load its first page.
2836 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2837 FPDF_PAGE page = LoadPage(0);
2838 ASSERT_TRUE(page);
2839
2840 {
2841 // Checks for bad annot.
2842 EXPECT_EQ(-1,
2843 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
2844
2845 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
2846 ASSERT_TRUE(annot);
2847
2848 // Checks for bad form handle.
2849 EXPECT_EQ(-1,
2850 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
2851
2852 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
2853 }
2854
2855 UnloadPage(page);
2856}
2857
2858TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexCheckBox) {
2859 // Open a file with checkbox widget annotations and load its first page.
2860 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2861 FPDF_PAGE page = LoadPage(0);
2862 ASSERT_TRUE(page);
2863
2864 {
2865 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2866 ASSERT_TRUE(annot);
2867 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
2868 }
2869
2870 UnloadPage(page);
2871}
2872
2873TEST_F(FPDFAnnotEmbedderTest, GetFormControlIndexInvalidAnnotation) {
2874 // Open a file with ink annotations and load its first page.
2875 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2876 FPDF_PAGE page = LoadPage(0);
2877 ASSERT_TRUE(page);
2878
2879 {
2880 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2881 ASSERT_TRUE(annot);
2882 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
2883 }
2884
2885 UnloadPage(page);
2886}
2887
2888TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueRadioButton) {
2889 // Open a file with radio button widget annotations and load its first page.
2890 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2891 FPDF_PAGE page = LoadPage(0);
2892 ASSERT_TRUE(page);
2893
2894 {
2895 // Checks for bad annot.
2896 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
2897 form_handle(), /*annot=*/nullptr,
2898 /*buffer=*/nullptr, /*buflen=*/0));
2899
2900 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
2901 ASSERT_TRUE(annot);
2902
2903 // Checks for bad form handle.
2904 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
2905 /*hHandle=*/nullptr, annot.get(),
2906 /*buffer=*/nullptr, /*buflen=*/0));
2907
2908 unsigned long length_bytes =
2909 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
2910 /*buffer=*/nullptr, /*buflen=*/0);
2911 ASSERT_EQ(14u, length_bytes);
2912 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2913 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
2914 buf.data(), length_bytes));
2915 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
2916 }
2917
2918 UnloadPage(page);
2919}
2920
2921TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueCheckBox) {
2922 // Open a file with checkbox widget annotations and load its first page.
2923 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2924 FPDF_PAGE page = LoadPage(0);
2925 ASSERT_TRUE(page);
2926
2927 {
2928 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2929 ASSERT_TRUE(annot);
2930
2931 unsigned long length_bytes =
2932 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
2933 /*buffer=*/nullptr, /*buflen=*/0);
2934 ASSERT_EQ(8u, length_bytes);
2935 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2936 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
2937 buf.data(), length_bytes));
2938 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
2939 }
2940
2941 UnloadPage(page);
2942}
2943
2944TEST_F(FPDFAnnotEmbedderTest, GetFormFieldExportValueInvalidAnnotation) {
2945 // Open a file with ink annotations and load its first page.
2946 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2947 FPDF_PAGE page = LoadPage(0);
2948 ASSERT_TRUE(page);
2949
2950 {
2951 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2952 ASSERT_TRUE(annot);
2953 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
2954 /*buffer=*/nullptr,
2955 /*buflen=*/0));
2956 }
2957
2958 UnloadPage(page);
2959}