blob: 1869e348ab7cd2c84138a43018080136654f900b [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
86 bool expected_focusable = pdfium::ContainsValue(expected_focusable_subtypes,
87 expected_subtypes[i]);
88 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
Lei Zhang03e5e682019-09-16 19:45:55 +00001085// TODO(crbug.com/pdfium/11): Fix this test and enable.
1086#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1087#define MAYBE_AddAndModifyText DISABLED_AddAndModifyText
1088#else
1089#define MAYBE_AddAndModifyText AddAndModifyText
1090#endif
1091TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyText) {
Lei Zhange4cdac52019-04-30 16:45:57 +00001092#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001093 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1094 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Lei Zhange67bcc72019-04-30 18:55:58 +00001095#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001096 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1097 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Jane Liu36567742017-07-06 11:13:35 -04001098#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001099 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1100 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001101#endif
1102
1103 // Open a file with two annotations and load its first page.
1104 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001105 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001106 ASSERT_TRUE(page);
1107 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1108
1109 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001110 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001111 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001112 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001113 }
Jane Liu36567742017-07-06 11:13:35 -04001114
Lei Zhanga21d5932018-02-05 18:28:38 +00001115 {
1116 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001117 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001118 ASSERT_TRUE(annot);
1119 FS_RECTF rect;
1120 rect.left = 200.f;
1121 rect.bottom = 550.f;
1122 rect.right = 450.f;
1123 rect.top = 650.f;
1124 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001125
Lei Zhanga21d5932018-02-05 18:28:38 +00001126 // Add a translucent text object to the new annotation.
1127 FPDF_PAGEOBJECT text_object =
1128 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1129 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001130 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001131 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1132 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001133 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001134 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1135 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1136 }
Jane Liu36567742017-07-06 11:13:35 -04001137
1138 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001139 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001140 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001141 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001142 }
Jane Liu36567742017-07-06 11:13:35 -04001143
Lei Zhanga21d5932018-02-05 18:28:38 +00001144 {
1145 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001146 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001147 ASSERT_TRUE(annot);
1148 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1149 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1150 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001151
Lei Zhanga21d5932018-02-05 18:28:38 +00001152 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001153 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001154 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1155 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1156 }
Jane Liu36567742017-07-06 11:13:35 -04001157
1158 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001159 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001160 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001161 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001162 }
Jane Liu36567742017-07-06 11:13:35 -04001163
1164 // Remove the new annotation, and check that the page renders as before.
1165 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001166 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001167 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Hui Yingstb4baceb2020-04-28 23:46:10 +00001168 CompareBitmap(bitmap.get(), 595, 842, kAnnotationStampWithApChecksum);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001169 }
Jane Liu36567742017-07-06 11:13:35 -04001170
1171 UnloadPage(page);
1172}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001173
Lei Zhang03e5e682019-09-16 19:45:55 +00001174// TODO(crbug.com/pdfium/11): Fix this test and enable.
1175#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1176#define MAYBE_GetSetStringValue DISABLED_GetSetStringValue
1177#else
1178#define MAYBE_GetSetStringValue GetSetStringValue
1179#endif
1180TEST_F(FPDFAnnotEmbedderTest, MAYBE_GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001181 // Open a file with four annotations and load its first page.
1182 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001183 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001184 ASSERT_TRUE(page);
1185
Lei Zhang4f556b82019-04-08 16:32:41 +00001186 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001187
Lei Zhanga21d5932018-02-05 18:28:38 +00001188 {
1189 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001190 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001191 ASSERT_TRUE(annot);
1192
1193 // Check that a non-existent key does not exist.
1194 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1195
1196 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001197 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001198 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001199 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1200 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1201 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001202
1203 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001204 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001205 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001206 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001207 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001208 ASSERT_EQ(66u, length_bytes);
1209 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1210 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1211 length_bytes));
1212 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1213 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001214
1215 // Check that the string value of the modified date is correct.
1216 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001217 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1218 nullptr, 0);
1219 ASSERT_EQ(44u, length_bytes);
1220 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001221 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001222 buf.data(), length_bytes));
1223 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001224
1225 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001226 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001227 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1228 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001229 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001230
1231 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001232 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001233 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001234
Lei Zhange4cdac52019-04-30 16:45:57 +00001235#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001236 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001237#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001238 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001239#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001240 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001241#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001242
1243 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001244 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001245 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001246 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001247 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001248 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001249
Lei Zhanga21d5932018-02-05 18:28:38 +00001250 // Check that the string value of the modified date is the newly-set value.
1251 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001252 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001253 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001254 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001255 ASSERT_EQ(44u, length_bytes);
1256 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001257 EXPECT_EQ(44u,
1258 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001259 buf.data(), length_bytes));
1260 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001261 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001262
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001263 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001264 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001265}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001266
rycsmith3e785602019-03-05 21:48:36 +00001267TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001268 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001269 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1270 FPDF_PAGE page = LoadPage(0);
1271 ASSERT_TRUE(page);
1272 {
1273 // First two annotations do not have "MaxLen" attribute.
1274 for (int i = 0; i < 2; i++) {
1275 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1276 ASSERT_TRUE(annot);
1277
1278 // Verify that no "MaxLen" key present.
1279 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1280
1281 float value;
1282 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1283 }
1284
1285 // Annotation in index 2 has "MaxLen" of 10.
1286 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1287 ASSERT_TRUE(annot);
1288
1289 // Verify that "MaxLen" key present.
1290 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1291
1292 float value;
1293 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1294 EXPECT_FLOAT_EQ(10.0f, value);
1295
1296 // Check bad inputs.
1297 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1298 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1299 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1300 // Ask for key that exists but is not a number.
1301 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1302 }
1303
1304 UnloadPage(page);
1305}
1306
Lei Zhangab41f252018-12-23 03:10:50 +00001307TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001308 // Open a file with four annotations and load its first page.
1309 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001310 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001311 ASSERT_TRUE(page);
1312
Lei Zhanga21d5932018-02-05 18:28:38 +00001313 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001314 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1315 static constexpr size_t kExpectNormalAPLength = 73970;
1316
Lei Zhanga21d5932018-02-05 18:28:38 +00001317 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001318 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001319 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001320
Lei Zhanga21d5932018-02-05 18:28:38 +00001321 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001322 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001323 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001324 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001325
Lei Zhanga21d5932018-02-05 18:28:38 +00001326 // Check that the string value of an AP is not returned if the buffer is too
1327 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001328 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1329 // Write in the buffer to verify it's not overwritten.
1330 memcpy(buf.data(), "abcdefgh", 8);
1331 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001332 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001333 buf.data(), normal_length_bytes - 1));
1334 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001335
Lei Zhanga21d5932018-02-05 18:28:38 +00001336 // Check that the string value of an AP is returned through a buffer that is
1337 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001338 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001339 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001340 buf.data(), normal_length_bytes));
1341 EXPECT_EQ(kMd5NormalAP,
1342 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1343 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001344
Lei Zhanga21d5932018-02-05 18:28:38 +00001345 // Check that the string value of an AP is returned through a buffer that is
1346 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001347 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1348 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001349 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001350 buf.data(), normal_length_bytes + 2));
1351 EXPECT_EQ(kMd5NormalAP,
1352 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1353 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001354
Lei Zhanga21d5932018-02-05 18:28:38 +00001355 // Check that getting an AP for a mode that does not have an AP returns an
1356 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001357 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001358 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001359 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001360
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001361 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001362 EXPECT_EQ(2u,
1363 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001364 buf.data(), 1000));
1365 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001366
Lei Zhanga21d5932018-02-05 18:28:38 +00001367 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001368 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001369 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001370 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001371 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001372 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001373 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001374
Lei Zhanga21d5932018-02-05 18:28:38 +00001375 // Set the AP correctly now.
1376 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001377 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001378
Lei Zhanga21d5932018-02-05 18:28:38 +00001379 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001380 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001381 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001382 ASSERT_EQ(24u, rollover_length_bytes);
1383 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001384 EXPECT_EQ(24u,
1385 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001386 buf.data(), rollover_length_bytes));
1387 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001388
Lei Zhanga21d5932018-02-05 18:28:38 +00001389 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001390 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1391 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001392 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001393 buf.data(), normal_length_bytes));
1394 EXPECT_EQ(kMd5NormalAP,
1395 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1396 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001397 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001398
1399 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001400 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001401 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001402
Lei Zhang0b494052019-01-31 21:41:15 +00001403 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001404 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001405 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001406 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001407
Lei Zhanga21d5932018-02-05 18:28:38 +00001408 // Check that the new annotation value is equal to the value we set before
1409 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001410 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001411 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001412 ASSERT_EQ(24u, rollover_length_bytes);
1413 std::vector<FPDF_WCHAR> buf =
1414 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001415 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1416 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001417 buf.data(), rollover_length_bytes));
1418 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001419 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001420
1421 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001422 CloseSavedPage(page);
1423 CloseSavedDocument();
1424}
1425
Lei Zhangab41f252018-12-23 03:10:50 +00001426TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001427 // Open a file with four annotations and load its first page.
1428 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001429 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001430 ASSERT_TRUE(page);
1431
Lei Zhanga21d5932018-02-05 18:28:38 +00001432 {
1433 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001434 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001435 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001436
Lei Zhanga21d5932018-02-05 18:28:38 +00001437 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001438 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001439 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001440 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001441 EXPECT_EQ(73970u,
1442 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1443 nullptr, 0));
1444 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1445 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001446
Lei Zhanga21d5932018-02-05 18:28:38 +00001447 // Check that setting the Down AP to null removes the Down entry but keeps
1448 // Normal intact.
1449 EXPECT_TRUE(
1450 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1451 EXPECT_EQ(73970u,
1452 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1453 nullptr, 0));
1454 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1455 nullptr, 0));
1456 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001457
Lei Zhang75c81712018-02-08 17:22:39 +00001458 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001459}
1460
Lei Zhangab41f252018-12-23 03:10:50 +00001461TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001462 // Open a file with four annotations and load its first page.
1463 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001464 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001465 ASSERT_TRUE(page);
1466
Lei Zhanga21d5932018-02-05 18:28:38 +00001467 {
1468 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001469 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001470 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001471
Lei Zhanga21d5932018-02-05 18:28:38 +00001472 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001473 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001474 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001475 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001476 EXPECT_EQ(73970u,
1477 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1478 nullptr, 0));
1479 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1480 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001481
Lei Zhanga21d5932018-02-05 18:28:38 +00001482 // Check that setting the Normal AP to null removes the whole AP dictionary.
1483 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1484 nullptr));
1485 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1486 nullptr, 0));
1487 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1488 nullptr, 0));
1489 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001490
Lei Zhang75c81712018-02-08 17:22:39 +00001491 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001492}
1493
Lei Zhangab41f252018-12-23 03:10:50 +00001494TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001495 // Open a file with annotations and load its first page.
1496 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001497 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001498 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001499 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001500
Lei Zhanga21d5932018-02-05 18:28:38 +00001501 {
1502 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001503 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001504 ASSERT_TRUE(annot);
1505 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1506 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001507 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001508 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1509 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1510 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001511
Lei Zhanga21d5932018-02-05 18:28:38 +00001512 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001513 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001514 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1515 ASSERT_TRUE(popup);
1516 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1517 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1518 FS_RECTF rect;
1519 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1520 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1521 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001522
Lei Zhanga21d5932018-02-05 18:28:38 +00001523 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1524 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001525 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001526 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1527 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001528
Lei Zhanga21d5932018-02-05 18:28:38 +00001529 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1530 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001531 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001532 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001533 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1534 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001535 }
Jane Liu300bb272017-08-21 14:37:53 -04001536
Jane Liu300bb272017-08-21 14:37:53 -04001537 UnloadPage(page);
1538}
1539
Lei Zhangab41f252018-12-23 03:10:50 +00001540TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001541 // Open file with form text fields.
1542 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001543 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001544 ASSERT_TRUE(page);
1545
Lei Zhanga21d5932018-02-05 18:28:38 +00001546 {
1547 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001548 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001549 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001550
Lei Zhanga21d5932018-02-05 18:28:38 +00001551 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001552 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001553 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001554 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001555 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001556
Lei Zhanga21d5932018-02-05 18:28:38 +00001557 {
1558 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001559 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001560 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001561
Lei Zhanga21d5932018-02-05 18:28:38 +00001562 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001563 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001564 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001565 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1566 }
1567
1568 {
1569 // Retrieve the fourth annotation: user-editable password text field.
1570 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1571 ASSERT_TRUE(annot);
1572
1573 // Check that the flag values are as expected.
1574 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1575 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1576 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001577 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001578
1579 UnloadPage(page);
1580}
1581
Lei Zhangab41f252018-12-23 03:10:50 +00001582TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001583 // Open file with form text fields.
1584 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001585 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001586 ASSERT_TRUE(page);
1587
Lei Zhanga21d5932018-02-05 18:28:38 +00001588 {
1589 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001590 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001591 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001592
Lei Zhanga21d5932018-02-05 18:28:38 +00001593 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001594 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001595 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1596 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1597 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1598 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001599
Lei Zhanga21d5932018-02-05 18:28:38 +00001600 {
1601 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001602 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001603 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001604
Lei Zhanga21d5932018-02-05 18:28:38 +00001605 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001606 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001607 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1608 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1609 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1610 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001611
Lei Zhanga21d5932018-02-05 18:28:38 +00001612 {
1613 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001614 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001615 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001616
Lei Zhanga21d5932018-02-05 18:28:38 +00001617 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001618 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001619 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1620 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1621 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1622 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001623
1624 UnloadPage(page);
1625}
Diana Gage40870db2017-07-19 18:16:03 -07001626
Lei Zhangab41f252018-12-23 03:10:50 +00001627TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001628 // Open file with form text fields.
1629 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1630 FPDF_PAGE page = LoadPage(0);
1631 ASSERT_TRUE(page);
1632
1633 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001634 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1635 EXPECT_FALSE(
1636 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001637
Lei Zhang8da98232019-12-11 23:29:33 +00001638 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001639 {
1640 // Verify there is an annotation.
1641 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001642 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001643 EXPECT_TRUE(annot);
1644 }
1645
1646 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001647 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1648 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1649 EXPECT_FALSE(
1650 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001651
1652 UnloadPage(page);
1653}
1654
Lei Zhangab41f252018-12-23 03:10:50 +00001655TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001656 // Open file with form text fields.
1657 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1658 FPDF_PAGE page = LoadPage(0);
1659 ASSERT_TRUE(page);
1660
Lei Zhanga21d5932018-02-05 18:28:38 +00001661 {
1662 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001663 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001664 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001665 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001666 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001667
Lei Zhanga21d5932018-02-05 18:28:38 +00001668 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001669 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001670 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1671 }
Diana Gage40870db2017-07-19 18:16:03 -07001672
Lei Zhanga21d5932018-02-05 18:28:38 +00001673 {
1674 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001675 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001676 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001677 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001678 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001679
Lei Zhanga21d5932018-02-05 18:28:38 +00001680 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001681 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001682 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1683 }
Diana Gage40870db2017-07-19 18:16:03 -07001684
1685 UnloadPage(page);
1686}
1687
Lei Zhangab41f252018-12-23 03:10:50 +00001688TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001689 // Open file with form comboboxes.
1690 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1691 FPDF_PAGE page = LoadPage(0);
1692 ASSERT_TRUE(page);
1693
Lei Zhanga21d5932018-02-05 18:28:38 +00001694 {
1695 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001696 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001697 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001698 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001699 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001700
Lei Zhanga21d5932018-02-05 18:28:38 +00001701 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001702 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001703 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1704 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1705 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1706 }
Diana Gage40870db2017-07-19 18:16:03 -07001707
Lei Zhanga21d5932018-02-05 18:28:38 +00001708 {
1709 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001710 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001711 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001712 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001713 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001714
Lei Zhanga21d5932018-02-05 18:28:38 +00001715 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001716 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001717 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1718 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1719 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1720 }
Diana Gage40870db2017-07-19 18:16:03 -07001721
Lei Zhanga21d5932018-02-05 18:28:38 +00001722 {
1723 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001724 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001725 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001726 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001727 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001728
Lei Zhanga21d5932018-02-05 18:28:38 +00001729 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001730 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001731 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1732 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1733 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1734 }
Diana Gage40870db2017-07-19 18:16:03 -07001735
1736 UnloadPage(page);
1737}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001738
Lei Zhang03e5e682019-09-16 19:45:55 +00001739// TODO(crbug.com/pdfium/11): Fix this test and enable.
1740#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1741#define MAYBE_BUG_1206 DISABLED_BUG_1206
1742#else
1743#define MAYBE_BUG_1206 BUG_1206
1744#endif
1745TEST_F(FPDFAnnotEmbedderTest, MAYBE_BUG_1206) {
Lei Zhang992e7e22019-02-04 19:20:58 +00001746 static constexpr size_t kExpectedSize = 1609;
1747 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1748
1749 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1750
1751 FPDF_PAGE page = LoadPage(0);
1752 ASSERT_TRUE(page);
1753
1754 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1755 EXPECT_EQ(kExpectedSize, GetString().size());
1756 ClearString();
1757
1758 for (size_t i = 0; i < 10; ++i) {
1759 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1760 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
1761
1762 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1763 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
1764 // equal, not bigger.
1765 EXPECT_LT(kExpectedSize, GetString().size());
1766 ClearString();
1767 }
1768
1769 UnloadPage(page);
1770}
1771
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001772TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1773 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1774 FPDF_PAGE page = LoadPage(0);
1775 ASSERT_TRUE(page);
1776 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1777
1778 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00001779 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001780 static const size_t kBufSize = 12;
1781 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001782
1783 {
1784 // Add a text annotation to the page.
1785 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1786 ASSERT_TRUE(annot);
1787 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1788 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1789
1790 // Make sure there is no test key, add set a value there, and read it back.
1791 std::fill(buf.begin(), buf.end(), 'x');
1792 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001793 kBufSize));
1794 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001795
Lei Zhangf0f67682019-04-08 17:03:21 +00001796 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001797 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1798
1799 std::fill(buf.begin(), buf.end(), 'x');
1800 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001801 kBufSize));
1802 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001803 }
1804
Lei Zhang05ec64c2019-01-09 03:00:06 +00001805 {
1806 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1807 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00001808 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00001809 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001810 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1811 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1812 // Also do the same test for its appearance string.
1813 std::fill(buf.begin(), buf.end(), 'x');
1814 ASSERT_EQ(2u,
1815 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001816 buf.data(), kBufSize));
1817 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001818
Lei Zhangf0f67682019-04-08 17:03:21 +00001819 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00001820 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1821 text.get()));
1822
1823 std::fill(buf.begin(), buf.end(), 'x');
1824 ASSERT_EQ(6u,
1825 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001826 buf.data(), kBufSize));
1827 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001828 }
1829
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001830 UnloadPage(page);
1831
1832 {
1833 // Save a copy, open the copy, and check the annotation again.
1834 // Note that it renders the rotation.
1835 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001836 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001837 FPDF_PAGE saved_page = LoadSavedPage(0);
1838 ASSERT_TRUE(saved_page);
1839
Lei Zhang05ec64c2019-01-09 03:00:06 +00001840 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001841 {
1842 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1843 ASSERT_TRUE(annot);
1844 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1845
1846 std::fill(buf.begin(), buf.end(), 'x');
1847 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001848 kBufSize));
1849 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001850 }
1851
Lei Zhang05ec64c2019-01-09 03:00:06 +00001852 {
1853 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1854 ASSERT_TRUE(annot);
1855 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1856 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1857
1858 std::fill(buf.begin(), buf.end(), 'x');
1859 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001860 kBufSize));
1861 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001862 }
1863
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001864 CloseSavedPage(saved_page);
1865 CloseSavedDocument();
1866 }
1867}
rycsmithcb752f32019-02-21 18:40:53 +00001868
1869TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
1870 // Open a file with combobox widget annotations and load its first page.
1871 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1872 FPDF_PAGE page = LoadPage(0);
1873 ASSERT_TRUE(page);
1874
1875 {
1876 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1877 ASSERT_TRUE(annot);
1878
1879 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1880
1881 annot.reset(FPDFPage_GetAnnot(page, 1));
1882 ASSERT_TRUE(annot);
1883
1884 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00001885
1886 // Check bad form handle / annot.
1887 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
1888 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
1889 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00001890 }
1891
1892 UnloadPage(page);
1893}
1894
1895TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
1896 // Open a file with listbox widget annotations and load its first page.
1897 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1898 FPDF_PAGE page = LoadPage(0);
1899 ASSERT_TRUE(page);
1900
1901 {
1902 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1903 ASSERT_TRUE(annot);
1904
1905 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1906
1907 annot.reset(FPDFPage_GetAnnot(page, 1));
1908 ASSERT_TRUE(annot);
1909
1910 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1911 }
1912
1913 UnloadPage(page);
1914}
1915
1916TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
1917 // Open a file with ink annotations and load its first page.
1918 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1919 FPDF_PAGE page = LoadPage(0);
1920 ASSERT_TRUE(page);
1921
1922 {
1923 // annotations do not have "Opt" array and will return -1
1924 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1925 ASSERT_TRUE(annot);
1926
1927 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1928
1929 annot.reset(FPDFPage_GetAnnot(page, 1));
1930 ASSERT_TRUE(annot);
1931
1932 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1933 }
1934
1935 UnloadPage(page);
1936}
1937
1938TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
1939 // Open a file with combobox widget annotations and load its first page.
1940 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1941 FPDF_PAGE page = LoadPage(0);
1942 ASSERT_TRUE(page);
1943
1944 {
1945 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1946 ASSERT_TRUE(annot);
1947
1948 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001949 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001950 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001951 ASSERT_EQ(8u, length_bytes);
1952 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001953 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001954 buf.data(), length_bytes));
1955 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001956
1957 annot.reset(FPDFPage_GetAnnot(page, 1));
1958 ASSERT_TRUE(annot);
1959
1960 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001961 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001962 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001963 ASSERT_EQ(12u, length_bytes);
1964 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001965 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001966 buf.data(), length_bytes));
1967 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001968
1969 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001970 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001971 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001972 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001973 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001974 buf.data(), length_bytes));
1975 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001976
Lei Zhange7033c82019-02-26 19:30:49 +00001977 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00001978 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1979 nullptr, 0));
1980 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1981 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00001982
1983 // Check bad form handle / annot.
1984 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
1985 EXPECT_EQ(0u,
1986 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
1987 EXPECT_EQ(0u,
1988 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00001989 }
1990
1991 UnloadPage(page);
1992}
1993
1994TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
1995 // Open a file with listbox widget annotations and load its first page.
1996 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1997 FPDF_PAGE page = LoadPage(0);
1998 ASSERT_TRUE(page);
1999
2000 {
2001 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2002 ASSERT_TRUE(annot);
2003
2004 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002005 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002006 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002007 ASSERT_EQ(8u, length_bytes);
2008 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002009 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002010 buf.data(), length_bytes));
2011 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002012
2013 annot.reset(FPDFPage_GetAnnot(page, 1));
2014 ASSERT_TRUE(annot);
2015
2016 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002017 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002018 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002019 ASSERT_EQ(12u, length_bytes);
2020 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002021 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002022 buf.data(), length_bytes));
2023 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002024
2025 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002026 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002027 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002028 ASSERT_EQ(18u, length_bytes);
2029 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002030 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002031 buf.data(), length_bytes));
2032 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002033
2034 // indices out of range
2035 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2036 nullptr, 0));
2037 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2038 nullptr, 0));
2039 }
2040
2041 UnloadPage(page);
2042}
2043
2044TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2045 // Open a file with ink annotations and load its first page.
2046 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2047 FPDF_PAGE page = LoadPage(0);
2048 ASSERT_TRUE(page);
2049
2050 {
2051 // annotations do not have "Opt" array and will return 0
2052 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2053 ASSERT_TRUE(annot);
2054
2055 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2056 nullptr, 0));
2057
2058 annot.reset(FPDFPage_GetAnnot(page, 1));
2059 ASSERT_TRUE(annot);
2060
2061 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2062 nullptr, 0));
2063 }
2064
2065 UnloadPage(page);
2066}
Ryan Smith09c23b12019-04-25 18:09:06 +00002067
Mansi Awasthi2acdf792020-05-12 08:48:04 +00002068TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedCombobox) {
2069 // Open a file with combobox widget annotations and load its first page.
2070 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2071 FPDF_PAGE page = LoadPage(0);
2072 ASSERT_TRUE(page);
2073
2074 {
2075 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2076 ASSERT_TRUE(annot);
2077
2078 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2079 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2080 ASSERT_EQ(3, count);
2081 for (int i = 0; i < count; i++) {
2082 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2083 }
2084
2085 annot.reset(FPDFPage_GetAnnot(page, 1));
2086 ASSERT_TRUE(annot);
2087
2088 // Checks for Combobox with Values (/V) object which is just a string.
2089 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2090 ASSERT_EQ(26, count);
2091 for (int i = 0; i < count; i++) {
2092 EXPECT_EQ(i == 1,
2093 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2094 }
2095
2096 // Checks for index outside bound i.e. (index >= CountOption()).
2097 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2098 /*index=*/26));
2099 // Checks for negetive index.
2100 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2101 /*index=*/-1));
2102
2103 // Checks for bad form handle/annot.
2104 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2105 EXPECT_FALSE(
2106 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2107 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2108 }
2109
2110 UnloadPage(page);
2111}
2112
2113TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedListbox) {
2114 // Open a file with listbox widget annotations and load its first page.
2115 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2116 FPDF_PAGE page = LoadPage(0);
2117 ASSERT_TRUE(page);
2118
2119 {
2120 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2121 ASSERT_TRUE(annot);
2122
2123 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2124 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2125 ASSERT_EQ(3, count);
2126 for (int i = 0; i < count; i++) {
2127 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2128 }
2129
2130 annot.reset(FPDFPage_GetAnnot(page, 1));
2131 ASSERT_TRUE(annot);
2132
2133 // Checks for Listbox with Values (/V) object which is just a string.
2134 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2135 ASSERT_EQ(26, count);
2136 for (int i = 0; i < count; i++) {
2137 EXPECT_EQ(i == 1,
2138 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2139 }
2140
2141 annot.reset(FPDFPage_GetAnnot(page, 3));
2142 ASSERT_TRUE(annot);
2143
2144 // Checks for Listbox with only Selected indices (/I) object which is an
2145 // array with multiple objects.
2146 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2147 ASSERT_EQ(5, count);
2148 for (int i = 0; i < count; i++) {
2149 bool expected = (i == 1 || i == 3);
2150 EXPECT_EQ(expected,
2151 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2152 }
2153
2154 annot.reset(FPDFPage_GetAnnot(page, 4));
2155 ASSERT_TRUE(annot);
2156
2157 // Checks for Listbox with Values (/V) object which is an array with
2158 // multiple objects.
2159 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2160 ASSERT_EQ(5, count);
2161 for (int i = 0; i < count; i++) {
2162 bool expected = (i == 2 || i == 4);
2163 EXPECT_EQ(expected,
2164 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2165 }
2166
2167 annot.reset(FPDFPage_GetAnnot(page, 5));
2168 ASSERT_TRUE(annot);
2169
2170 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2171 // objects conflict with different lengths.
2172 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2173 ASSERT_EQ(5, count);
2174 for (int i = 0; i < count; i++) {
2175 bool expected = (i == 0 || i == 2);
2176 EXPECT_EQ(expected,
2177 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2178 }
2179 }
2180
2181 UnloadPage(page);
2182}
2183
2184TEST_F(FPDFAnnotEmbedderTest, IsOptionSelectedInvalidAnnotations) {
2185 // Open a file with multiple form field annotations and load its first page.
2186 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2187 FPDF_PAGE page = LoadPage(0);
2188 ASSERT_TRUE(page);
2189
2190 {
2191 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2192 ASSERT_TRUE(annot);
2193
2194 // Checks for link annotation.
2195 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2196 /*index=*/0));
2197
2198 annot.reset(FPDFPage_GetAnnot(page, 3));
2199 ASSERT_TRUE(annot);
2200
2201 // Checks for text field annotation.
2202 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2203 /*index=*/0));
2204 }
2205
2206 UnloadPage(page);
2207}
2208
Ryan Smith09c23b12019-04-25 18:09:06 +00002209TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2210 // Open a file with combobox annotations and load its first page.
2211 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2212 FPDF_PAGE page = LoadPage(0);
2213 ASSERT_TRUE(page);
2214
2215 {
2216 // All 3 widgets have Tf font size 12.
2217 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2218 ASSERT_TRUE(annot);
2219
2220 float value;
2221 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2222 EXPECT_EQ(12.0, value);
2223
2224 annot.reset(FPDFPage_GetAnnot(page, 1));
2225 ASSERT_TRUE(annot);
2226
2227 float value_two;
2228 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2229 EXPECT_EQ(12.0, value_two);
2230
2231 annot.reset(FPDFPage_GetAnnot(page, 2));
2232 ASSERT_TRUE(annot);
2233
2234 float value_three;
2235 ASSERT_TRUE(
2236 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2237 EXPECT_EQ(12.0, value_three);
2238 }
2239
2240 UnloadPage(page);
2241}
2242
2243TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2244 // Open a file with textfield annotations and load its first page.
2245 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2246 FPDF_PAGE page = LoadPage(0);
2247 ASSERT_TRUE(page);
2248
2249 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002250 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002251 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2252 ASSERT_TRUE(annot);
2253
2254 float value;
2255 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2256 EXPECT_EQ(12.0, value);
2257
2258 annot.reset(FPDFPage_GetAnnot(page, 1));
2259 ASSERT_TRUE(annot);
2260
2261 float value_two;
2262 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2263 EXPECT_EQ(12.0, value_two);
2264
2265 annot.reset(FPDFPage_GetAnnot(page, 2));
2266 ASSERT_TRUE(annot);
2267
2268 float value_three;
2269 ASSERT_TRUE(
2270 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2271 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002272
2273 float value_four;
2274 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2275 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002276 }
2277
2278 UnloadPage(page);
2279}
2280
2281TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2282 // Open a file with ink annotations and load its first page.
2283 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2284 FPDF_PAGE page = LoadPage(0);
2285 ASSERT_TRUE(page);
2286
2287 {
2288 // Annotations that do not have variable text and will return -1.
2289 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2290 ASSERT_TRUE(annot);
2291
2292 float value;
2293 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2294
2295 annot.reset(FPDFPage_GetAnnot(page, 1));
2296 ASSERT_TRUE(annot);
2297
2298 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2299 }
2300
2301 UnloadPage(page);
2302}
2303
2304TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2305 // Open a file with combobox annotations and load its first page.
2306 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2307 FPDF_PAGE page = LoadPage(0);
2308 ASSERT_TRUE(page);
2309
2310 {
2311 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2312 ASSERT_TRUE(annot);
2313
2314 // Check bad form handle / annot.
2315 float value;
2316 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2317 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2318 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2319 }
2320
2321 UnloadPage(page);
2322}
2323
2324TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2325 // Open a file with textfield annotations and load its first page.
2326 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2327 FPDF_PAGE page = LoadPage(0);
2328 ASSERT_TRUE(page);
2329
2330 {
2331 // Obtain the first annotation, a text field with negative font size, -12.
2332 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2333 ASSERT_TRUE(annot);
2334
2335 float value;
2336 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2337 EXPECT_EQ(-12.0, value);
2338 }
2339
2340 UnloadPage(page);
2341}
Ryan Smith23fdf892019-07-17 21:51:26 +00002342
2343TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2344 // Open a file with checkbox and radiobuttons widget annotations and load its
2345 // first page.
2346 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2347 FPDF_PAGE page = LoadPage(0);
2348 ASSERT_TRUE(page);
2349
2350 {
2351 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2352 ASSERT_TRUE(annot);
2353 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2354 }
2355
2356 UnloadPage(page);
2357}
2358
2359TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2360 // Open a file with checkbox and radiobutton widget annotations and load its
2361 // first page.
2362 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2363 FPDF_PAGE page = LoadPage(0);
2364 ASSERT_TRUE(page);
2365
2366 {
2367 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2368 ASSERT_TRUE(annot);
2369 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2370 }
2371
2372 UnloadPage(page);
2373}
2374
2375TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2376 // Open a file with checkbox and radiobutton widget annotations and load its
2377 // first page.
2378 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2379 FPDF_PAGE page = LoadPage(0);
2380 ASSERT_TRUE(page);
2381
2382 {
2383 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2384 ASSERT_TRUE(annot);
2385 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2386
2387 annot.reset(FPDFPage_GetAnnot(page, 6));
2388 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2389
2390 annot.reset(FPDFPage_GetAnnot(page, 7));
2391 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2392 }
2393
2394 UnloadPage(page);
2395}
2396
2397TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2398 // Open a file with checkbox and radiobutton widget annotations and load its
2399 // first page.
2400 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2401 FPDF_PAGE page = LoadPage(0);
2402 ASSERT_TRUE(page);
2403
2404 {
2405 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2406 ASSERT_TRUE(annot);
2407 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2408
2409 annot.reset(FPDFPage_GetAnnot(page, 3));
2410 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2411
2412 annot.reset(FPDFPage_GetAnnot(page, 4));
2413 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2414 }
2415
2416 UnloadPage(page);
2417}
2418
2419TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2420 // Open a file with checkbox and radiobuttons widget annotations and load its
2421 // first page.
2422 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2423 FPDF_PAGE page = LoadPage(0);
2424 ASSERT_TRUE(page);
2425
2426 {
2427 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2428 ASSERT_TRUE(annot);
2429 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2430 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2431 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2432 }
2433
2434 UnloadPage(page);
2435}
2436
2437TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2438 // Open a file with text widget annotations and load its first page.
2439 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2440 FPDF_PAGE page = LoadPage(0);
2441 ASSERT_TRUE(page);
2442
2443 {
2444 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2445 ASSERT_TRUE(annot);
2446 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2447 }
2448
2449 UnloadPage(page);
2450}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002451
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002452TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2453 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002454 FPDF_PAGE page = LoadPage(0);
2455 ASSERT_TRUE(page);
2456
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002457 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2458
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002459 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002460 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002461 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002462 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002463 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002464
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002465 constexpr int kExpectedAnnotTypes[] = {-1,
2466 FPDF_FORMFIELD_COMBOBOX,
2467 FPDF_FORMFIELD_LISTBOX,
2468 FPDF_FORMFIELD_TEXTFIELD,
2469 FPDF_FORMFIELD_CHECKBOX,
2470 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002471
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002472 for (size_t i = 0; i < FX_ArraySize(kExpectedAnnotTypes); ++i) {
2473 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002474 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002475 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002476 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2477 }
2478 UnloadPage(page);
2479}
2480
2481TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2482 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2483 FPDF_PAGE page = LoadPage(0);
2484 ASSERT_TRUE(page);
2485
2486 {
2487 EXPECT_EQ(0u,
2488 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2489
2490 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2491 ASSERT_TRUE(annot);
2492
2493 EXPECT_EQ(0u,
2494 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2495
2496 unsigned long length_bytes =
2497 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2498 ASSERT_EQ(2u, length_bytes);
2499 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2500 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2501 buf.data(), length_bytes));
2502 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2503 }
2504 {
2505 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2506 ASSERT_TRUE(annot);
2507
2508 unsigned long length_bytes =
2509 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2510 ASSERT_EQ(18u, length_bytes);
2511 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2512 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2513 buf.data(), length_bytes));
2514 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2515 }
2516 UnloadPage(page);
2517}
2518
2519TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2520 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2521 FPDF_PAGE page = LoadPage(0);
2522 ASSERT_TRUE(page);
2523
2524 {
2525 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2526 ASSERT_TRUE(annot);
2527
2528 unsigned long length_bytes =
2529 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2530 ASSERT_EQ(2u, length_bytes);
2531 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2532 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2533 buf.data(), length_bytes));
2534 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2535 }
2536 {
2537 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2538 ASSERT_TRUE(annot);
2539
2540 unsigned long length_bytes =
2541 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2542 ASSERT_EQ(14u, length_bytes);
2543 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2544 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2545 buf.data(), length_bytes));
2546 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2547 }
2548 UnloadPage(page);
2549}
2550
2551TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2552 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2553 FPDF_PAGE page = LoadPage(0);
2554 ASSERT_TRUE(page);
2555
2556 {
2557 EXPECT_EQ(0u,
2558 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2559
2560 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2561 ASSERT_TRUE(annot);
2562
2563 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2564
2565 unsigned long length_bytes =
2566 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2567 ASSERT_EQ(18u, length_bytes);
2568 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2569 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2570 buf.data(), length_bytes));
2571 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2572 }
2573 UnloadPage(page);
2574}
2575
2576TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2577 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2578 FPDF_PAGE page = LoadPage(0);
2579 ASSERT_TRUE(page);
2580
2581 {
2582 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2583 ASSERT_TRUE(annot);
2584
2585 unsigned long length_bytes =
2586 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2587 ASSERT_EQ(30u, length_bytes);
2588 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2589 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2590 buf.data(), length_bytes));
2591 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2592 }
2593 UnloadPage(page);
2594}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002595
Lei Zhang9b444002020-04-17 17:35:23 +00002596TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2597 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002598 FPDF_PAGE page = LoadPage(0);
2599 ASSERT_TRUE(page);
2600
Lei Zhang9b444002020-04-17 17:35:23 +00002601 // Verify widgets are by default focusable.
2602 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2603 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002604
Lei Zhang9b444002020-04-17 17:35:23 +00002605 // Expected annot subtypes for page 0 of annots.pdf.
2606 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2607 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2608 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2609 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2610 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002611
Lei Zhang9b444002020-04-17 17:35:23 +00002612 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002613 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002614 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2615 kExpectedAnnotSubtypes,
2616 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002617
Lei Zhang9b444002020-04-17 17:35:23 +00002618 // Make no annotation type focusable using the preferred method.
2619 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2620 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002621
Lei Zhang9b444002020-04-17 17:35:23 +00002622 // Restore the focusable type count back to 1, then set it back to 0 using a
2623 // different method.
2624 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2625 ASSERT_TRUE(
2626 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2627 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002628
Lei Zhang9b444002020-04-17 17:35:23 +00002629 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002630 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002631
Lei Zhang9b444002020-04-17 17:35:23 +00002632 // Now make links focusable.
2633 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2634 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002635
Lei Zhang9b444002020-04-17 17:35:23 +00002636 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002637 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002638 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2639 kExpectedAnnotSubtypes,
2640 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002641
Lei Zhang9b444002020-04-17 17:35:23 +00002642 // Test invalid parameters.
2643 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
2644 FX_ArraySize(kDefaultSubtypes)));
2645 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
2646 FX_ArraySize(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002647 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2648
Lei Zhang9b444002020-04-17 17:35:23 +00002649 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2650 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2651 subtypes.size()));
2652 EXPECT_FALSE(
2653 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2654 EXPECT_FALSE(
2655 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002656
2657 UnloadPage(page);
2658}
Lei Zhang671aece2020-04-14 19:02:26 +00002659
Hui Yingst609f7d62020-04-23 23:14:13 +00002660// TODO(crbug.com/pdfium/11): Fix this test and enable.
2661#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
2662#define MAYBE_FocusableAnnotRendering DISABLED_FocusableAnnotRendering
2663#else
2664#define MAYBE_FocusableAnnotRendering FocusableAnnotRendering
2665#endif
2666TEST_F(FPDFAnnotEmbedderTest, MAYBE_FocusableAnnotRendering) {
Lei Zhang671aece2020-04-14 19:02:26 +00002667 ASSERT_TRUE(OpenDocument("annots.pdf"));
2668 FPDF_PAGE page = LoadPage(0);
2669 ASSERT_TRUE(page);
2670
2671 {
2672#if defined(OS_WIN)
2673 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
2674#elif defined(OS_MACOSX)
2675 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
2676#else
2677 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
2678#endif
2679 // Check the initial rendering.
2680 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2681 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2682 }
2683
2684 // Make links and highlights focusable.
2685 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
2686 FPDF_ANNOT_HIGHLIGHT};
2687 constexpr int kSubTypesCount = FX_ArraySize(kSubTypes);
2688 ASSERT_TRUE(
2689 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
2690 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
2691 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
2692 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
2693 subtypes.size()));
2694 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
2695 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
2696
2697 {
2698#if defined(OS_WIN)
2699 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
2700#elif defined(OS_MACOSX)
2701 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
2702#else
2703 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
2704#endif
2705 // Focus the first link and check the rendering.
2706 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2707 ASSERT_TRUE(annot);
2708 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2709 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2710 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2711 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2712 }
2713
2714 {
2715#if defined(OS_WIN)
2716 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
2717#elif defined(OS_MACOSX)
2718 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
2719#else
2720 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
2721#endif
2722 // Focus the first highlight and check the rendering.
2723 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2724 ASSERT_TRUE(annot);
2725 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
2726 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2727 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2728 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2729 }
2730
2731 UnloadPage(page);
2732}
Badhri Ravikumarcd628912020-05-07 19:23:51 +00002733
2734TEST_F(FPDFAnnotEmbedderTest, GetLinkFromAnnotation) {
2735 ASSERT_TRUE(OpenDocument("annots.pdf"));
2736 FPDF_PAGE page = LoadPage(0);
2737 ASSERT_TRUE(page);
2738 {
2739 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
2740 ASSERT_TRUE(annot);
2741 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2742 FPDF_LINK link_annot = FPDFAnnot_GetLink(annot.get());
2743 ASSERT_TRUE(link_annot);
2744
2745 FPDF_ACTION action = FPDFLink_GetAction(link_annot);
2746 ASSERT_TRUE(action);
2747 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
2748 FPDFAction_GetType(action));
2749
2750 constexpr char kExpectedResult[] =
2751 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
2752 "fpdf_text.h";
2753 constexpr unsigned long kExpectedLength = FX_ArraySize(kExpectedResult);
2754 unsigned long bufsize =
2755 FPDFAction_GetURIPath(document(), action, nullptr, 0);
2756 ASSERT_EQ(kExpectedLength, bufsize);
2757
2758 char buffer[1024];
2759 EXPECT_EQ(bufsize,
2760 FPDFAction_GetURIPath(document(), action, buffer, bufsize));
2761 EXPECT_STREQ(kExpectedResult, buffer);
2762 }
2763
2764 {
2765 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2766 ASSERT_TRUE(annot);
2767 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
2768 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
2769 }
2770
2771 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
2772
2773 UnloadPage(page);
2774}