blob: 54385196602cf94466055f34eefd222e856dfd45 [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"
Lei Zhangb6992dd2019-02-05 23:30:20 +000021#include "testing/fx_string_testhelpers.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000022#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040023#include "testing/gtest/include/gtest/gtest.h"
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000024#include "testing/utils/hash.h"
Lei Zhang9b444002020-04-17 17:35:23 +000025#include "third_party/base/span.h"
Lei Zhangc9b72992020-04-22 01:03:52 +000026#include "third_party/base/stl_util.h"
Lei Zhang9b444002020-04-17 17:35:23 +000027
28namespace {
29
30void VerifyFocusableAnnotSubtypes(
31 FPDF_FORMHANDLE form_handle,
32 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) {
33 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
34 FPDFAnnot_GetFocusableSubtypesCount(form_handle));
35
36 std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes(
37 expected_subtypes.size());
38 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(
39 form_handle, actual_subtypes.data(), actual_subtypes.size()));
40 for (size_t i = 0; i < expected_subtypes.size(); ++i)
41 ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]);
42}
43
44void SetAndVerifyFocusableAnnotSubtypes(
45 FPDF_FORMHANDLE form_handle,
46 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) {
47 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(),
48 subtypes.size()));
49 VerifyFocusableAnnotSubtypes(form_handle, subtypes);
50}
51
52void VerifyAnnotationSubtypesAndFocusability(
53 FPDF_FORMHANDLE form_handle,
54 FPDF_PAGE page,
55 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes,
56 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) {
57 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
58 FPDFPage_GetAnnotCount(page));
59 for (size_t i = 0; i < expected_subtypes.size(); ++i) {
60 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
61 ASSERT_TRUE(annot);
62 EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get()));
63
64 bool expected_focusable = pdfium::ContainsValue(expected_focusable_subtypes,
65 expected_subtypes[i]);
66 EXPECT_EQ(expected_focusable,
67 FORM_SetFocusedAnnot(form_handle, annot.get()));
68
69 // Kill the focus so the next test starts in an unfocused state.
70 FORM_ForceToKillFocus(form_handle);
71 }
72}
73
74} // namespace
Jane Liu4fd9a472017-06-01 18:56:09 -040075
Lei Zhangab41f252018-12-23 03:10:50 +000076class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040077
Lei Zhangab41f252018-12-23 03:10:50 +000078TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +000079 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
80 FPDF_PAGE page = LoadPage(0);
81 ASSERT_TRUE(page);
82
83 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
84
85 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
86 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
87 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
88 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
89 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
90
91 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
92
93 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
94
95 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
96 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
97 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
98
99 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
100
Lei Zhang4f556b82019-04-08 16:32:41 +0000101 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +0000102 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +0000103 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
104
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000105 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +0000106 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
107 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
108 EXPECT_EQ(0u,
109 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
110
111 UnloadPage(page);
112}
113
Lei Zhang3d9a0972019-03-04 19:34:09 +0000114TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
115 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
116 FPDF_PAGE page = LoadPage(0);
117 ASSERT_TRUE(page);
118
119 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +0000120 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +0000121
122 UnloadPage(page);
123}
124
Lei Zhangab41f252018-12-23 03:10:50 +0000125TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -0400126 // Open a file with one annotation and load its first page.
127 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000128 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -0400129 ASSERT_TRUE(page);
130
131 // This annotation has a malformed appearance stream, which does not have its
132 // normal appearance defined, only its rollover appearance. In this case, its
133 // normal appearance should be generated, allowing the highlight annotation to
134 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000135 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000136 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -0400137
138 UnloadPage(page);
139}
140
Lei Zhang03e5e682019-09-16 19:45:55 +0000141// TODO(crbug.com/pdfium/11): Fix this test and enable.
142#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
143#define MAYBE_RenderMultilineMarkupAnnotWithoutAP \
144 DISABLED_RenderMultilineMarkupAnnotWithoutAP
145#else
146#define MAYBE_RenderMultilineMarkupAnnotWithoutAP \
147 RenderMultilineMarkupAnnotWithoutAP
148#endif
149TEST_F(FPDFAnnotEmbedderTest, MAYBE_RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang4f556b82019-04-08 16:32:41 +0000150 static const char kMd5[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +0000151 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000152 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
153 FPDF_PAGE page = LoadPage(0);
154 ASSERT_TRUE(page);
155
Tom Sepeze08d2b12018-04-25 18:49:32 +0000156 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000157 CompareBitmap(bitmap.get(), 595, 842, kMd5);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000158
159 UnloadPage(page);
160}
161
Lei Zhangab41f252018-12-23 03:10:50 +0000162TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400163 // Open a file with one annotation and load its first page.
164 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000165 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400166 ASSERT_TRUE(page);
167
168 // Check that there is a total of 1 annotation on its first page.
169 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
170
171 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000172 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000173 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000174 ASSERT_TRUE(annot);
175 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400176
Lei Zhanga21d5932018-02-05 18:28:38 +0000177 // Check that the annotation color is yellow.
178 unsigned int R;
179 unsigned int G;
180 unsigned int B;
181 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000182 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000183 &G, &B, &A));
184 EXPECT_EQ(255u, R);
185 EXPECT_EQ(255u, G);
186 EXPECT_EQ(0u, B);
187 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400188
Lei Zhanga21d5932018-02-05 18:28:38 +0000189 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000190 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000191 EXPECT_EQ(FPDF_OBJECT_STRING,
192 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000193 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000194 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000195 ASSERT_EQ(28u, length_bytes);
196 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000197 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000198 length_bytes));
199 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400200
Lei Zhanga21d5932018-02-05 18:28:38 +0000201 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000202 EXPECT_EQ(
203 FPDF_OBJECT_STRING,
204 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000205 length_bytes = FPDFAnnot_GetStringValue(
206 annot.get(), pdfium::annotation::kContents, nullptr, 0);
207 ASSERT_EQ(2690u, length_bytes);
208 buf = GetFPDFWideStringBuffer(length_bytes);
209 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
210 pdfium::annotation::kContents,
211 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000212 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000213 L"This is a note for that highlight annotation. Very long highlight "
214 "annotation. Long long long Long long longLong long longLong long "
215 "longLong long longLong long longLong long longLong long longLong long "
216 "longLong long longLong long longLong long longLong long longLong long "
217 "longLong long longLong long longLong long longLong long longLong long "
218 "longLong long longLong long longLong long longLong long longLong long "
219 "longLong long longLong long longLong long longLong long longLong long "
220 "longLong long longLong long longLong long longLong long longLong long "
221 "longLong long longLong long longLong long longLong long longLong long "
222 "longLong long longLong long longLong long longLong long longLong long "
223 "longLong long longLong long longLong long longLong long longLong long "
224 "longLong long longLong long longLong long longLong long longLong long "
225 "longLong long longLong long longLong long longLong long longLong long "
226 "longLong long longLong long longLong long longLong long longLong long "
227 "longLong long longLong long longLong long longLong long longLong long "
228 "longLong long longLong long longLong long longLong long longLong long "
229 "longLong long longLong long longLong long longLong long longLong long "
230 "longLong long longLong long longLong long longLong long longLong long "
231 "longLong long longLong long longLong long longLong long longLong long "
232 "longLong long long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000233 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400234
Lei Zhanga21d5932018-02-05 18:28:38 +0000235 // Check that the quadpoints are correct.
236 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000237 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000238 EXPECT_EQ(115.802643f, quadpoints.x1);
239 EXPECT_EQ(718.913940f, quadpoints.y1);
240 EXPECT_EQ(157.211182f, quadpoints.x4);
241 EXPECT_EQ(706.264465f, quadpoints.y4);
242 }
Tom Sepez507d0192018-11-07 16:37:51 +0000243 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400244}
245
Lei Zhang03e5e682019-09-16 19:45:55 +0000246// TODO(crbug.com/pdfium/11): Fix this test and enable.
247#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
248#define MAYBE_ExtractInkMultiple DISABLED_ExtractInkMultiple
249#else
250#define MAYBE_ExtractInkMultiple ExtractInkMultiple
251#endif
252TEST_F(FPDFAnnotEmbedderTest, MAYBE_ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400253 // Open a file with three annotations and load its first page.
254 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000255 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400256 ASSERT_TRUE(page);
257
258 // Check that there is a total of 3 annotation on its first page.
259 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
260
Lei Zhanga21d5932018-02-05 18:28:38 +0000261 {
262 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000263 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000264 ASSERT_TRUE(annot);
265 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400266
Lei Zhanga21d5932018-02-05 18:28:38 +0000267 // Check that the annotation color is blue with opacity.
268 unsigned int R;
269 unsigned int G;
270 unsigned int B;
271 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000272 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000273 &G, &B, &A));
274 EXPECT_EQ(0u, R);
275 EXPECT_EQ(0u, G);
276 EXPECT_EQ(255u, B);
277 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400278
Lei Zhanga21d5932018-02-05 18:28:38 +0000279 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000280 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
281 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400282
Lei Zhang4f556b82019-04-08 16:32:41 +0000283 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000284 // Note that upon rendering, the rectangle coordinates will be adjusted.
285 FS_RECTF rect;
286 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
287 EXPECT_EQ(351.820404f, rect.left);
288 EXPECT_EQ(583.830688f, rect.bottom);
289 EXPECT_EQ(475.336090f, rect.right);
290 EXPECT_EQ(681.535034f, rect.top);
291 }
Tom Sepezef43c262018-11-07 16:41:32 +0000292 {
293 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
294 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
295 }
Tom Sepez507d0192018-11-07 16:37:51 +0000296 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400297}
Jane Liu20eafda2017-06-07 10:33:24 -0400298
Lei Zhangab41f252018-12-23 03:10:50 +0000299TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400300 // Open a file with one annotation and load its first page.
301 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000302 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400303 ASSERT_TRUE(page);
304
305 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400306 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400307
308 UnloadPage(page);
309}
310
Lei Zhangab41f252018-12-23 03:10:50 +0000311TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400312 // Open a file with no annotation and load its first page.
313 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000314 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400315 ASSERT_TRUE(page);
316 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
317
Lei Zhanga21d5932018-02-05 18:28:38 +0000318 {
319 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000320 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000321 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400322
Lei Zhanga21d5932018-02-05 18:28:38 +0000323 // Check that there is now 1 annotations on this page.
324 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400325
Lei Zhanga21d5932018-02-05 18:28:38 +0000326 // Check that the subtype of the annotation is correct.
327 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
328 }
Jane Liue10509a2017-06-20 16:47:41 -0400329
Lei Zhanga21d5932018-02-05 18:28:38 +0000330 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000331 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000332 ASSERT_TRUE(annot);
333 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400334
Lei Zhanga21d5932018-02-05 18:28:38 +0000335 // Set the color of the annotation.
336 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
337 102, 153, 204));
338 // Check that the color has been set correctly.
339 unsigned int R;
340 unsigned int G;
341 unsigned int B;
342 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000343 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000344 &G, &B, &A));
345 EXPECT_EQ(51u, R);
346 EXPECT_EQ(102u, G);
347 EXPECT_EQ(153u, B);
348 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400349
Lei Zhanga21d5932018-02-05 18:28:38 +0000350 // Change the color of the annotation.
351 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
352 153, 102, 51));
353 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000354 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000355 &G, &B, &A));
356 EXPECT_EQ(204u, R);
357 EXPECT_EQ(153u, G);
358 EXPECT_EQ(102u, B);
359 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400360
Lei Zhanga21d5932018-02-05 18:28:38 +0000361 // Set the annotation rectangle.
362 FS_RECTF rect;
363 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
364 EXPECT_EQ(0.f, rect.left);
365 EXPECT_EQ(0.f, rect.right);
366 rect.left = 35;
367 rect.bottom = 150;
368 rect.right = 53;
369 rect.top = 165;
370 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
371 // Check that the annotation rectangle has been set correctly.
372 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
373 EXPECT_EQ(35.f, rect.left);
374 EXPECT_EQ(150.f, rect.bottom);
375 EXPECT_EQ(53.f, rect.right);
376 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400377
Lei Zhanga21d5932018-02-05 18:28:38 +0000378 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000379 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000380 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000381 ASSERT_TRUE(FPDFAnnot_SetStringValue(
382 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000383 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000384 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000385 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000386 ASSERT_EQ(74u, length_bytes);
387 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
388 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
389 pdfium::annotation::kContents,
390 buf.data(), length_bytes));
391 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000392 }
Jane Liu20eafda2017-06-07 10:33:24 -0400393 UnloadPage(page);
394}
395
Lei Zhang03e5e682019-09-16 19:45:55 +0000396// TODO(crbug.com/pdfium/11): Fix this test and enable.
397#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
398#define MAYBE_AddAndSaveUnderlineAnnotation \
399 DISABLED_AddAndSaveUnderlineAnnotation
400#else
401#define MAYBE_AddAndSaveUnderlineAnnotation AddAndSaveUnderlineAnnotation
402#endif
403TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400404 // Open a file with one annotation and load its first page.
405 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000406 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400407 ASSERT_TRUE(page);
408
409 // Check that there is a total of one annotation on its first page, and verify
410 // its quadpoints.
411 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400412 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000413 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000414 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000415 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000416 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000417 EXPECT_EQ(115.802643f, quadpoints.x1);
418 EXPECT_EQ(718.913940f, quadpoints.y1);
419 EXPECT_EQ(157.211182f, quadpoints.x4);
420 EXPECT_EQ(706.264465f, quadpoints.y4);
421 }
Jane Liu20eafda2017-06-07 10:33:24 -0400422
423 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000424 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000425 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000426 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
427 ASSERT_TRUE(annot);
428 quadpoints.x1 = 140.802643f;
429 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000430 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000431 }
Jane Liu20eafda2017-06-07 10:33:24 -0400432
433 // Save the document, closing the page and document.
434 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000435 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400436
437 // Open the saved document.
Lei Zhang4f556b82019-04-08 16:32:41 +0000438 static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400439
Lei Zhang0b494052019-01-31 21:41:15 +0000440 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000441 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000442 VerifySavedRendering(page, 612, 792, kMd5);
Jane Liu20eafda2017-06-07 10:33:24 -0400443
444 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000445 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400446
Lei Zhanga21d5932018-02-05 18:28:38 +0000447 {
448 // Check that the second annotation is an underline annotation and verify
449 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000450 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000451 ASSERT_TRUE(new_annot);
452 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
453 FS_QUADPOINTSF new_quadpoints;
454 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000455 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000456 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
457 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
458 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
459 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
460 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400461
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000462 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400463 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400464}
Jane Liu06462752017-06-27 16:41:14 -0400465
Lei Zhangab41f252018-12-23 03:10:50 +0000466TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000467 // Open a file with four annotations and load its first page.
468 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
469 FPDF_PAGE page = LoadPage(0);
470 ASSERT_TRUE(page);
471 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
472
473 // Retrieve the highlight annotation.
474 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
475 ASSERT_TRUE(annot);
476 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
477
478 FS_QUADPOINTSF quadpoints;
479 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
480
481 {
482 // Verify the current one set of quadpoints.
483 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
484
485 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
486 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
487 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
488 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
489 }
490
491 {
492 // Update the quadpoints.
493 FS_QUADPOINTSF new_quadpoints = quadpoints;
494 new_quadpoints.y1 -= 20.f;
495 new_quadpoints.y2 -= 20.f;
496 new_quadpoints.y3 -= 20.f;
497 new_quadpoints.y4 -= 20.f;
498 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
499
500 // Verify added quadpoint set
501 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
502 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
503 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
504 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
505 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
506 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
507 }
508
509 {
510 // Append a new set of quadpoints.
511 FS_QUADPOINTSF new_quadpoints = quadpoints;
512 new_quadpoints.y1 += 20.f;
513 new_quadpoints.y2 += 20.f;
514 new_quadpoints.y3 += 20.f;
515 new_quadpoints.y4 += 20.f;
516 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
517
518 // Verify added quadpoint set
519 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
520 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
521 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
522 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
523 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
524 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
525 }
526
527 {
528 // Setting and getting quadpoints at out-of-bound index should fail
529 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
530 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
531 }
532
533 FPDFPage_CloseAnnot(annot);
534
535 // Retrieve the square annotation
536 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
537
538 {
539 // Check that attempting to set its quadpoints would fail
540 ASSERT_TRUE(squareAnnot);
541 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
542 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
543 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
544 }
545
546 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000547 UnloadPage(page);
548}
549
Lei Zhang03e5e682019-09-16 19:45:55 +0000550// TODO(crbug.com/pdfium/11): Fix this test and enable.
551#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
552#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
553#else
554#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
555#endif
556TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000557#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +0000558 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
Lei Zhang4f556b82019-04-08 16:32:41 +0000559 static const char kMd5ModifiedHighlight[] =
Lei Zhang0f6b4342020-02-25 20:00:39 +0000560 "e64bf648f6e9354d1f3eedb47a2c9498";
561 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Lei Zhange67bcc72019-04-30 18:55:58 +0000562#elif defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000563 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
564 static const char kMd5ModifiedHighlight[] =
565 "66f3caef3a7d488a4fa1ad37fc06310e";
566 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400567#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000568 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
569 static const char kMd5ModifiedHighlight[] =
570 "66f3caef3a7d488a4fa1ad37fc06310e";
571 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400572#endif
573
Jane Liu06462752017-06-27 16:41:14 -0400574 // Open a file with four annotations and load its first page.
575 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000576 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400577 ASSERT_TRUE(page);
578 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
579
Jane Liub370e5a2017-08-16 13:24:58 -0400580 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000581 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000582 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000583 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000584 }
Jane Liub370e5a2017-08-16 13:24:58 -0400585
Jane Liu0c6b07d2017-08-15 10:50:22 -0400586 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400587 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000588
589 // Retrieve the highlight annotation which has its AP stream already defined.
590 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000591 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000592 ASSERT_TRUE(annot);
593 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
594
595 // Check that color cannot be set when an AP stream is defined already.
596 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
597 102, 153, 204));
598
599 // Verify its attachment points.
600 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000601 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000602 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
603 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
604 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
605 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
606
607 // Check that updating the attachment points would succeed.
608 quadpoints.x1 -= 50.f;
609 quadpoints.x2 -= 50.f;
610 quadpoints.x3 -= 50.f;
611 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000612 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000613 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000614 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000615 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
616 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
617 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
618 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
619
620 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000621 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000622 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000623 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000624 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000625
626 // Verify its annotation rectangle.
627 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
628 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
629 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
630 EXPECT_NEAR(136.325f, rect.right, 0.001f);
631 EXPECT_NEAR(721.292f, rect.top, 0.001f);
632
633 // Check that updating the rectangle would succeed.
634 rect.left -= 60.f;
635 rect.right -= 60.f;
636 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
637 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
638 EXPECT_EQ(rect.right, new_rect.right);
639 }
Jane Liu06462752017-06-27 16:41:14 -0400640
Jane Liub370e5a2017-08-16 13:24:58 -0400641 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000642 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000643 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000644 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000645 }
Jane Liub370e5a2017-08-16 13:24:58 -0400646
Lei Zhanga21d5932018-02-05 18:28:38 +0000647 {
648 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000649 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000650 ASSERT_TRUE(annot);
651 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400652
Lei Zhanga21d5932018-02-05 18:28:38 +0000653 // Check that updating the rectangle would succeed.
654 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
655 rect.left += 70.f;
656 rect.right += 70.f;
657 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
658 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
659 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400660
Lei Zhanga21d5932018-02-05 18:28:38 +0000661 // Check that updating the rectangle changes the square annotation's
662 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000663 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000664 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000665 }
Jane Liub370e5a2017-08-16 13:24:58 -0400666
Jane Liu06462752017-06-27 16:41:14 -0400667 UnloadPage(page);
668}
Jane Liu8ce58f52017-06-29 13:40:22 -0400669
Lei Zhangab41f252018-12-23 03:10:50 +0000670TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000671 // Open a file with multiline markup annotations.
672 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
673 FPDF_PAGE page = LoadPage(0);
674 ASSERT_TRUE(page);
675 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000676 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000677 ASSERT_TRUE(annot);
678
679 // This is a three line annotation.
680 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
681 }
682 UnloadPage(page);
683
684 // null annotation should return 0
685 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
686}
687
Lei Zhangab41f252018-12-23 03:10:50 +0000688TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400689 // Open a file with 3 annotations on its first page.
690 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000691 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400692 ASSERT_TRUE(page);
693 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
694
Jane Liu0c6b07d2017-08-15 10:50:22 -0400695 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400696
Lei Zhanga21d5932018-02-05 18:28:38 +0000697 // Check that the annotations have the expected rectangle coordinates.
698 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000699 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000700 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
701 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
702 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400703
Lei Zhanga21d5932018-02-05 18:28:38 +0000704 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000705 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000706 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
707 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
708 }
709
710 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000711 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000712 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
713 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
714 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400715
716 // Check that nothing happens when attempting to remove an annotation with an
717 // out-of-bound index.
718 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
719 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
720 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
721
722 // Remove the second annotation.
723 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
724 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
725 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
726
727 // Save the document, closing the page and document.
728 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000729 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400730
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400731 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400732 // Open the saved document.
733 std::string new_file = GetString();
734 FPDF_FILEACCESS file_access;
735 memset(&file_access, 0, sizeof(file_access));
736 file_access.m_FileLen = new_file.size();
737 file_access.m_GetBlock = GetBlockFromString;
738 file_access.m_Param = &new_file;
739 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
740 ASSERT_TRUE(new_doc);
741 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
742 ASSERT_TRUE(new_page);
743
744 // Check that the saved document has 2 annotations on the first page.
745 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
746
Lei Zhanga21d5932018-02-05 18:28:38 +0000747 // Check that the remaining 2 annotations are the original 1st and 3rd ones
748 // by verifying their rectangle coordinates.
749 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000750 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000751 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
752 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
753 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400754
Lei Zhanga21d5932018-02-05 18:28:38 +0000755 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000756 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000757 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
758 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
759 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400760 FPDF_ClosePage(new_page);
761 FPDF_CloseDocument(new_doc);
762}
Jane Liu8ce58f52017-06-29 13:40:22 -0400763
Lei Zhang03e5e682019-09-16 19:45:55 +0000764// TODO(crbug.com/pdfium/11): Fix this test and enable.
765#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
766#define MAYBE_AddAndModifyPath DISABLED_AddAndModifyPath
767#else
768#define MAYBE_AddAndModifyPath AddAndModifyPath
769#endif
770TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyPath) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000771#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +0000772 static const char kMd5Original[] = "80d7b6cc7b13a78d77a6151bc846e80b";
773 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
774 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
775 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Lei Zhange67bcc72019-04-30 18:55:58 +0000776#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +0000777 static const char kMd5Original[] = "6aa001a77ec05d0f1b0d1d22e28744d4";
778 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
779 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
780 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Jane Liubaa7ff42017-06-29 19:18:23 -0400781#else
Lei Zhanga2b70732019-06-25 08:34:22 +0000782 static const char kMd5Original[] = "b42cef463483e668eaf4055a65e4f1f5";
783 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
784 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
785 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -0400786#endif
787
788 // Open a file with two annotations and load its first page.
789 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000790 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400791 ASSERT_TRUE(page);
792 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
793
794 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000795 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000796 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000797 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000798 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400799
Lei Zhanga21d5932018-02-05 18:28:38 +0000800 {
801 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000802 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000803 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400804
Lei Zhanga21d5932018-02-05 18:28:38 +0000805 // Check that this annotation has one path object and retrieve it.
806 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000807 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000808 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
809 EXPECT_FALSE(path);
810 path = FPDFAnnot_GetObject(annot.get(), 0);
811 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
812 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400813
Lei Zhanga21d5932018-02-05 18:28:38 +0000814 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +0000815 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +0000816 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400817
Lei Zhanga21d5932018-02-05 18:28:38 +0000818 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000819 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000820 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000821 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000822 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400823
Lei Zhanga21d5932018-02-05 18:28:38 +0000824 // Add a second path object to the same annotation.
825 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
826 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +0000827 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
828 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +0000829 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
830 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
831 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400832
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000833 // The object is in the annontation, not in the page, so the page object
834 // array should not change.
835 ASSERT_EQ(32, FPDFPage_CountObjects(page));
836
Lei Zhanga21d5932018-02-05 18:28:38 +0000837 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000838 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000839 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000840 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000841 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400842
Lei Zhanga21d5932018-02-05 18:28:38 +0000843 // Delete the newly added path object.
844 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
845 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000846 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000847 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400848
849 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000850 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000851 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000852 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000853 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400854
Jane Liubaa7ff42017-06-29 19:18:23 -0400855 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400856
Lei Zhanga21d5932018-02-05 18:28:38 +0000857 {
858 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000859 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000860 ASSERT_TRUE(annot);
861 rect.left = 200.f;
862 rect.bottom = 400.f;
863 rect.right = 500.f;
864 rect.top = 600.f;
865 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400866
Lei Zhanga21d5932018-02-05 18:28:38 +0000867 // Add a new path to the annotation.
868 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
869 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
870 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
871 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
872 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +0000873 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
874 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +0000875 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
876 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
877 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
878
879 // Check that the annotation's bounding box came from its rectangle.
880 FS_RECTF new_rect;
881 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
882 EXPECT_EQ(rect.left, new_rect.left);
883 EXPECT_EQ(rect.bottom, new_rect.bottom);
884 EXPECT_EQ(rect.right, new_rect.right);
885 EXPECT_EQ(rect.top, new_rect.top);
886 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400887
888 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400889 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000890 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400891
892 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +0000893 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000894 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000895 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400896
Jane Liu36567742017-07-06 11:13:35 -0400897 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000898 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400899
Lei Zhanga21d5932018-02-05 18:28:38 +0000900 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000901 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000902 ASSERT_TRUE(annot);
903 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400904
Lei Zhanga21d5932018-02-05 18:28:38 +0000905 // Check that the new annotation's rectangle is as defined.
906 FS_RECTF new_rect;
907 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
908 EXPECT_EQ(rect.left, new_rect.left);
909 EXPECT_EQ(rect.bottom, new_rect.bottom);
910 EXPECT_EQ(rect.right, new_rect.right);
911 EXPECT_EQ(rect.top, new_rect.top);
912 }
913
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000914 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400915 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400916}
Jane Liub137e752017-07-05 15:04:33 -0400917
Lei Zhangab41f252018-12-23 03:10:50 +0000918TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400919 // Open a file with an annotation and load its first page.
920 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000921 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400922 ASSERT_TRUE(page);
923
924 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000925 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000926 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000927 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
928 }
Jane Liub137e752017-07-05 15:04:33 -0400929
Lei Zhanga21d5932018-02-05 18:28:38 +0000930 {
931 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000932 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000933 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400934
Lei Zhanga21d5932018-02-05 18:28:38 +0000935 // Check that the original flag values are as expected.
936 int flags = FPDFAnnot_GetFlags(annot.get());
937 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
938 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400939
Lei Zhanga21d5932018-02-05 18:28:38 +0000940 // Set the HIDDEN flag.
941 flags |= FPDF_ANNOT_FLAG_HIDDEN;
942 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
943 flags = FPDFAnnot_GetFlags(annot.get());
944 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
945 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400946
Lei Zhanga21d5932018-02-05 18:28:38 +0000947 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000948 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000949 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000950 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
951 }
Jane Liub137e752017-07-05 15:04:33 -0400952
Lei Zhanga21d5932018-02-05 18:28:38 +0000953 // Unset the HIDDEN flag.
954 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
955 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
956 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
957 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
958 flags = FPDFAnnot_GetFlags(annot.get());
959 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
960 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400961
Lei Zhanga21d5932018-02-05 18:28:38 +0000962 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000963 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000964 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000965 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
966 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000967 }
Jane Liub137e752017-07-05 15:04:33 -0400968
Jane Liub137e752017-07-05 15:04:33 -0400969 UnloadPage(page);
970}
Jane Liu36567742017-07-06 11:13:35 -0400971
Lei Zhang03e5e682019-09-16 19:45:55 +0000972// TODO(crbug.com/pdfium/11): Fix this test and enable.
973#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
974#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
975#else
976#define MAYBE_AddAndModifyImage AddAndModifyImage
977#endif
978TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000979#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +0000980 static const char kMd5Original[] = "80d7b6cc7b13a78d77a6151bc846e80b";
981 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
982 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +0000983#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +0000984 static const char kMd5Original[] = "6aa001a77ec05d0f1b0d1d22e28744d4";
985 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
986 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -0400987#else
Lei Zhanga2b70732019-06-25 08:34:22 +0000988 static const char kMd5Original[] = "b42cef463483e668eaf4055a65e4f1f5";
989 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
990 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -0400991#endif
992
993 // Open a file with two annotations and load its first page.
994 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000995 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400996 ASSERT_TRUE(page);
997 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
998
999 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001000 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001001 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001002 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001003 }
Jane Liu36567742017-07-06 11:13:35 -04001004
Jane Liu36567742017-07-06 11:13:35 -04001005 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001006 FPDF_BITMAP image_bitmap;
1007
1008 {
1009 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001010 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001011 ASSERT_TRUE(annot);
1012 FS_RECTF rect;
1013 rect.left = 200.f;
1014 rect.bottom = 600.f;
1015 rect.right = 400.f;
1016 rect.top = 800.f;
1017 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1018
1019 // Add a solid-color translucent image object to the new annotation.
1020 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1021 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1022 0xeeeecccc);
1023 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1024 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1025 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1026 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1027 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1028 kBitmapSize, 0, 0));
1029 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1030 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1031 }
Jane Liu36567742017-07-06 11:13:35 -04001032
1033 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001034 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001035 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001036 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001037 }
Jane Liu36567742017-07-06 11:13:35 -04001038
Lei Zhanga21d5932018-02-05 18:28:38 +00001039 {
1040 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001041 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001042 ASSERT_TRUE(annot);
1043 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1044 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1045 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001046
Lei Zhanga21d5932018-02-05 18:28:38 +00001047 // Modify the image in the new annotation.
1048 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1049 0xff000000);
1050 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1051 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1052 }
Jane Liu36567742017-07-06 11:13:35 -04001053
1054 // Save the document, closing the page and document.
1055 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001056 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001057 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001058
1059 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001060 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001061}
1062
Lei Zhang03e5e682019-09-16 19:45:55 +00001063// TODO(crbug.com/pdfium/11): Fix this test and enable.
1064#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1065#define MAYBE_AddAndModifyText DISABLED_AddAndModifyText
1066#else
1067#define MAYBE_AddAndModifyText AddAndModifyText
1068#endif
1069TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyText) {
Lei Zhange4cdac52019-04-30 16:45:57 +00001070#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001071 static const char kMd5Original[] = "80d7b6cc7b13a78d77a6151bc846e80b";
1072 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1073 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Lei Zhange67bcc72019-04-30 18:55:58 +00001074#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001075 static const char kMd5Original[] = "6aa001a77ec05d0f1b0d1d22e28744d4";
1076 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1077 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Jane Liu36567742017-07-06 11:13:35 -04001078#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001079 static const char kMd5Original[] = "b42cef463483e668eaf4055a65e4f1f5";
1080 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1081 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001082#endif
1083
1084 // Open a file with two annotations and load its first page.
1085 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001086 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001087 ASSERT_TRUE(page);
1088 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1089
1090 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001091 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001092 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001093 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001094 }
Jane Liu36567742017-07-06 11:13:35 -04001095
Lei Zhanga21d5932018-02-05 18:28:38 +00001096 {
1097 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001098 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001099 ASSERT_TRUE(annot);
1100 FS_RECTF rect;
1101 rect.left = 200.f;
1102 rect.bottom = 550.f;
1103 rect.right = 450.f;
1104 rect.top = 650.f;
1105 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001106
Lei Zhanga21d5932018-02-05 18:28:38 +00001107 // Add a translucent text object to the new annotation.
1108 FPDF_PAGEOBJECT text_object =
1109 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1110 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001111 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001112 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1113 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001114 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001115 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1116 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1117 }
Jane Liu36567742017-07-06 11:13:35 -04001118
1119 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001120 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001121 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001122 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001123 }
Jane Liu36567742017-07-06 11:13:35 -04001124
Lei Zhanga21d5932018-02-05 18:28:38 +00001125 {
1126 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001127 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001128 ASSERT_TRUE(annot);
1129 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1130 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1131 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001132
Lei Zhanga21d5932018-02-05 18:28:38 +00001133 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001134 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001135 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1136 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1137 }
Jane Liu36567742017-07-06 11:13:35 -04001138
1139 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001140 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001141 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001142 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001143 }
Jane Liu36567742017-07-06 11:13:35 -04001144
1145 // Remove the new annotation, and check that the page renders as before.
1146 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001147 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001148 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001149 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001150 }
Jane Liu36567742017-07-06 11:13:35 -04001151
1152 UnloadPage(page);
1153}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001154
Lei Zhang03e5e682019-09-16 19:45:55 +00001155// TODO(crbug.com/pdfium/11): Fix this test and enable.
1156#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1157#define MAYBE_GetSetStringValue DISABLED_GetSetStringValue
1158#else
1159#define MAYBE_GetSetStringValue GetSetStringValue
1160#endif
1161TEST_F(FPDFAnnotEmbedderTest, MAYBE_GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001162 // Open a file with four annotations and load its first page.
1163 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001164 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001165 ASSERT_TRUE(page);
1166
Lei Zhang4f556b82019-04-08 16:32:41 +00001167 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001168
Lei Zhanga21d5932018-02-05 18:28:38 +00001169 {
1170 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001171 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001172 ASSERT_TRUE(annot);
1173
1174 // Check that a non-existent key does not exist.
1175 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1176
1177 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001178 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001179 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001180 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1181 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1182 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001183
1184 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001185 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001186 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001187 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001188 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001189 ASSERT_EQ(66u, length_bytes);
1190 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1191 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1192 length_bytes));
1193 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1194 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001195
1196 // Check that the string value of the modified date is correct.
1197 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001198 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1199 nullptr, 0);
1200 ASSERT_EQ(44u, length_bytes);
1201 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001202 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001203 buf.data(), length_bytes));
1204 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001205
1206 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001207 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001208 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1209 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001210 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001211
1212 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001213 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001214 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001215
Lei Zhange4cdac52019-04-30 16:45:57 +00001216#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001217 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001218#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001219 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001220#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001221 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001222#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001223
1224 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001225 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001226 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001227 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001228 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001229 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001230
Lei Zhanga21d5932018-02-05 18:28:38 +00001231 // Check that the string value of the modified date is the newly-set value.
1232 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001233 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001234 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001235 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001236 ASSERT_EQ(44u, length_bytes);
1237 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001238 EXPECT_EQ(44u,
1239 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001240 buf.data(), length_bytes));
1241 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001242 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001243
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001244 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001245 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001246}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001247
rycsmith3e785602019-03-05 21:48:36 +00001248TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001249 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001250 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1251 FPDF_PAGE page = LoadPage(0);
1252 ASSERT_TRUE(page);
1253 {
1254 // First two annotations do not have "MaxLen" attribute.
1255 for (int i = 0; i < 2; i++) {
1256 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1257 ASSERT_TRUE(annot);
1258
1259 // Verify that no "MaxLen" key present.
1260 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1261
1262 float value;
1263 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1264 }
1265
1266 // Annotation in index 2 has "MaxLen" of 10.
1267 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1268 ASSERT_TRUE(annot);
1269
1270 // Verify that "MaxLen" key present.
1271 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1272
1273 float value;
1274 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1275 EXPECT_FLOAT_EQ(10.0f, value);
1276
1277 // Check bad inputs.
1278 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1279 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1280 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1281 // Ask for key that exists but is not a number.
1282 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1283 }
1284
1285 UnloadPage(page);
1286}
1287
Lei Zhangab41f252018-12-23 03:10:50 +00001288TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001289 // Open a file with four annotations and load its first page.
1290 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001291 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001292 ASSERT_TRUE(page);
1293
Lei Zhanga21d5932018-02-05 18:28:38 +00001294 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001295 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1296 static constexpr size_t kExpectNormalAPLength = 73970;
1297
Lei Zhanga21d5932018-02-05 18:28:38 +00001298 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001299 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001300 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001301
Lei Zhanga21d5932018-02-05 18:28:38 +00001302 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001303 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001304 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001305 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001306
Lei Zhanga21d5932018-02-05 18:28:38 +00001307 // Check that the string value of an AP is not returned if the buffer is too
1308 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001309 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1310 // Write in the buffer to verify it's not overwritten.
1311 memcpy(buf.data(), "abcdefgh", 8);
1312 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001313 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001314 buf.data(), normal_length_bytes - 1));
1315 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001316
Lei Zhanga21d5932018-02-05 18:28:38 +00001317 // Check that the string value of an AP is returned through a buffer that is
1318 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001319 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001320 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001321 buf.data(), normal_length_bytes));
1322 EXPECT_EQ(kMd5NormalAP,
1323 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1324 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 returned through a buffer that is
1327 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001328 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1329 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001330 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001331 buf.data(), normal_length_bytes + 2));
1332 EXPECT_EQ(kMd5NormalAP,
1333 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1334 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001335
Lei Zhanga21d5932018-02-05 18:28:38 +00001336 // Check that getting an AP for a mode that does not have an AP returns an
1337 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001338 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001339 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001340 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001341
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001342 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001343 EXPECT_EQ(2u,
1344 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001345 buf.data(), 1000));
1346 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001347
Lei Zhanga21d5932018-02-05 18:28:38 +00001348 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001349 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001350 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001351 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001352 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001353 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001354 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001355
Lei Zhanga21d5932018-02-05 18:28:38 +00001356 // Set the AP correctly now.
1357 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001358 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001359
Lei Zhanga21d5932018-02-05 18:28:38 +00001360 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001361 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001362 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001363 ASSERT_EQ(24u, rollover_length_bytes);
1364 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001365 EXPECT_EQ(24u,
1366 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001367 buf.data(), rollover_length_bytes));
1368 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001369
Lei Zhanga21d5932018-02-05 18:28:38 +00001370 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001371 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1372 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001373 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001374 buf.data(), normal_length_bytes));
1375 EXPECT_EQ(kMd5NormalAP,
1376 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1377 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001378 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001379
1380 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001381 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001382 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001383
Lei Zhang0b494052019-01-31 21:41:15 +00001384 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001385 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001386 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001387 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001388
Lei Zhanga21d5932018-02-05 18:28:38 +00001389 // Check that the new annotation value is equal to the value we set before
1390 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001391 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001392 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001393 ASSERT_EQ(24u, rollover_length_bytes);
1394 std::vector<FPDF_WCHAR> buf =
1395 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001396 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1397 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001398 buf.data(), rollover_length_bytes));
1399 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001400 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001401
1402 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001403 CloseSavedPage(page);
1404 CloseSavedDocument();
1405}
1406
Lei Zhangab41f252018-12-23 03:10:50 +00001407TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001408 // Open a file with four annotations and load its first page.
1409 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001410 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001411 ASSERT_TRUE(page);
1412
Lei Zhanga21d5932018-02-05 18:28:38 +00001413 {
1414 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001415 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001416 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001417
Lei Zhanga21d5932018-02-05 18:28:38 +00001418 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001419 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001420 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001421 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001422 EXPECT_EQ(73970u,
1423 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1424 nullptr, 0));
1425 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1426 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001427
Lei Zhanga21d5932018-02-05 18:28:38 +00001428 // Check that setting the Down AP to null removes the Down entry but keeps
1429 // Normal intact.
1430 EXPECT_TRUE(
1431 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1432 EXPECT_EQ(73970u,
1433 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1434 nullptr, 0));
1435 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1436 nullptr, 0));
1437 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001438
Lei Zhang75c81712018-02-08 17:22:39 +00001439 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001440}
1441
Lei Zhangab41f252018-12-23 03:10:50 +00001442TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001443 // Open a file with four annotations and load its first page.
1444 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001445 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001446 ASSERT_TRUE(page);
1447
Lei Zhanga21d5932018-02-05 18:28:38 +00001448 {
1449 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001450 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001451 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001452
Lei Zhanga21d5932018-02-05 18:28:38 +00001453 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001454 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001455 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001456 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001457 EXPECT_EQ(73970u,
1458 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1459 nullptr, 0));
1460 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1461 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001462
Lei Zhanga21d5932018-02-05 18:28:38 +00001463 // Check that setting the Normal AP to null removes the whole AP dictionary.
1464 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1465 nullptr));
1466 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1467 nullptr, 0));
1468 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1469 nullptr, 0));
1470 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001471
Lei Zhang75c81712018-02-08 17:22:39 +00001472 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001473}
1474
Lei Zhangab41f252018-12-23 03:10:50 +00001475TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001476 // Open a file with annotations and load its first page.
1477 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001478 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001479 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001480 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001481
Lei Zhanga21d5932018-02-05 18:28:38 +00001482 {
1483 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001484 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001485 ASSERT_TRUE(annot);
1486 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1487 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001488 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001489 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1490 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1491 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001492
Lei Zhanga21d5932018-02-05 18:28:38 +00001493 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001494 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001495 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1496 ASSERT_TRUE(popup);
1497 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1498 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1499 FS_RECTF rect;
1500 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1501 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1502 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001503
Lei Zhanga21d5932018-02-05 18:28:38 +00001504 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1505 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001506 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001507 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1508 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001509
Lei Zhanga21d5932018-02-05 18:28:38 +00001510 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1511 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001512 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001513 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001514 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1515 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001516 }
Jane Liu300bb272017-08-21 14:37:53 -04001517
Jane Liu300bb272017-08-21 14:37:53 -04001518 UnloadPage(page);
1519}
1520
Lei Zhangab41f252018-12-23 03:10:50 +00001521TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001522 // Open file with form text fields.
1523 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001524 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001525 ASSERT_TRUE(page);
1526
Lei Zhanga21d5932018-02-05 18:28:38 +00001527 {
1528 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001529 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001530 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001531
Lei Zhanga21d5932018-02-05 18:28:38 +00001532 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001533 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001534 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001535 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001536 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001537
Lei Zhanga21d5932018-02-05 18:28:38 +00001538 {
1539 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001540 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001541 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001542
Lei Zhanga21d5932018-02-05 18:28:38 +00001543 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001544 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001545 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001546 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1547 }
1548
1549 {
1550 // Retrieve the fourth annotation: user-editable password text field.
1551 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1552 ASSERT_TRUE(annot);
1553
1554 // Check that the flag values are as expected.
1555 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1556 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1557 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001558 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001559
1560 UnloadPage(page);
1561}
1562
Lei Zhangab41f252018-12-23 03:10:50 +00001563TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001564 // Open file with form text fields.
1565 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001566 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001567 ASSERT_TRUE(page);
1568
Lei Zhanga21d5932018-02-05 18:28:38 +00001569 {
1570 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001571 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001572 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001573
Lei Zhanga21d5932018-02-05 18:28:38 +00001574 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001575 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001576 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1577 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1578 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1579 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001580
Lei Zhanga21d5932018-02-05 18:28:38 +00001581 {
1582 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001583 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001584 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001585
Lei Zhanga21d5932018-02-05 18:28:38 +00001586 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001587 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001588 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1589 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1590 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1591 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001592
Lei Zhanga21d5932018-02-05 18:28:38 +00001593 {
1594 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001595 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001596 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001597
Lei Zhanga21d5932018-02-05 18:28:38 +00001598 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001599 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001600 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1601 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1602 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1603 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001604
1605 UnloadPage(page);
1606}
Diana Gage40870db2017-07-19 18:16:03 -07001607
Lei Zhangab41f252018-12-23 03:10:50 +00001608TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001609 // Open file with form text fields.
1610 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1611 FPDF_PAGE page = LoadPage(0);
1612 ASSERT_TRUE(page);
1613
1614 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001615 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1616 EXPECT_FALSE(
1617 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001618
Lei Zhang8da98232019-12-11 23:29:33 +00001619 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001620 {
1621 // Verify there is an annotation.
1622 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001623 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001624 EXPECT_TRUE(annot);
1625 }
1626
1627 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001628 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1629 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1630 EXPECT_FALSE(
1631 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001632
1633 UnloadPage(page);
1634}
1635
Lei Zhangab41f252018-12-23 03:10:50 +00001636TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001637 // Open file with form text fields.
1638 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1639 FPDF_PAGE page = LoadPage(0);
1640 ASSERT_TRUE(page);
1641
Lei Zhanga21d5932018-02-05 18:28:38 +00001642 {
1643 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001644 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001645 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001646 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001647 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001648
Lei Zhanga21d5932018-02-05 18:28:38 +00001649 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001650 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001651 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1652 }
Diana Gage40870db2017-07-19 18:16:03 -07001653
Lei Zhanga21d5932018-02-05 18:28:38 +00001654 {
1655 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001656 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001657 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001658 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001659 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001660
Lei Zhanga21d5932018-02-05 18:28:38 +00001661 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001662 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001663 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1664 }
Diana Gage40870db2017-07-19 18:16:03 -07001665
1666 UnloadPage(page);
1667}
1668
Lei Zhangab41f252018-12-23 03:10:50 +00001669TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001670 // Open file with form comboboxes.
1671 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1672 FPDF_PAGE page = LoadPage(0);
1673 ASSERT_TRUE(page);
1674
Lei Zhanga21d5932018-02-05 18:28:38 +00001675 {
1676 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001677 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001678 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001679 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001680 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001681
Lei Zhanga21d5932018-02-05 18:28:38 +00001682 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001683 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001684 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1685 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1686 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1687 }
Diana Gage40870db2017-07-19 18:16:03 -07001688
Lei Zhanga21d5932018-02-05 18:28:38 +00001689 {
1690 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001691 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001692 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001693 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001694 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001695
Lei Zhanga21d5932018-02-05 18:28:38 +00001696 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001697 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001698 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1699 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1700 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1701 }
Diana Gage40870db2017-07-19 18:16:03 -07001702
Lei Zhanga21d5932018-02-05 18:28:38 +00001703 {
1704 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001705 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001706 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001707 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001708 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001709
Lei Zhanga21d5932018-02-05 18:28:38 +00001710 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001711 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001712 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1713 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1714 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1715 }
Diana Gage40870db2017-07-19 18:16:03 -07001716
1717 UnloadPage(page);
1718}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001719
Lei Zhang03e5e682019-09-16 19:45:55 +00001720// TODO(crbug.com/pdfium/11): Fix this test and enable.
1721#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1722#define MAYBE_BUG_1206 DISABLED_BUG_1206
1723#else
1724#define MAYBE_BUG_1206 BUG_1206
1725#endif
1726TEST_F(FPDFAnnotEmbedderTest, MAYBE_BUG_1206) {
Lei Zhang992e7e22019-02-04 19:20:58 +00001727 static constexpr size_t kExpectedSize = 1609;
1728 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1729
1730 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1731
1732 FPDF_PAGE page = LoadPage(0);
1733 ASSERT_TRUE(page);
1734
1735 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1736 EXPECT_EQ(kExpectedSize, GetString().size());
1737 ClearString();
1738
1739 for (size_t i = 0; i < 10; ++i) {
1740 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1741 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
1742
1743 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1744 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
1745 // equal, not bigger.
1746 EXPECT_LT(kExpectedSize, GetString().size());
1747 ClearString();
1748 }
1749
1750 UnloadPage(page);
1751}
1752
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001753TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1754 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1755 FPDF_PAGE page = LoadPage(0);
1756 ASSERT_TRUE(page);
1757 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1758
1759 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00001760 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001761 static const size_t kBufSize = 12;
1762 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001763
1764 {
1765 // Add a text annotation to the page.
1766 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1767 ASSERT_TRUE(annot);
1768 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1769 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1770
1771 // Make sure there is no test key, add set a value there, and read it back.
1772 std::fill(buf.begin(), buf.end(), 'x');
1773 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001774 kBufSize));
1775 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001776
Lei Zhangf0f67682019-04-08 17:03:21 +00001777 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001778 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1779
1780 std::fill(buf.begin(), buf.end(), 'x');
1781 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001782 kBufSize));
1783 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001784 }
1785
Lei Zhang05ec64c2019-01-09 03:00:06 +00001786 {
1787 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1788 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00001789 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00001790 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001791 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1792 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1793 // Also do the same test for its appearance string.
1794 std::fill(buf.begin(), buf.end(), 'x');
1795 ASSERT_EQ(2u,
1796 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001797 buf.data(), kBufSize));
1798 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001799
Lei Zhangf0f67682019-04-08 17:03:21 +00001800 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00001801 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1802 text.get()));
1803
1804 std::fill(buf.begin(), buf.end(), 'x');
1805 ASSERT_EQ(6u,
1806 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001807 buf.data(), kBufSize));
1808 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001809 }
1810
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001811 UnloadPage(page);
1812
1813 {
1814 // Save a copy, open the copy, and check the annotation again.
1815 // Note that it renders the rotation.
1816 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001817 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001818 FPDF_PAGE saved_page = LoadSavedPage(0);
1819 ASSERT_TRUE(saved_page);
1820
Lei Zhang05ec64c2019-01-09 03:00:06 +00001821 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001822 {
1823 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1824 ASSERT_TRUE(annot);
1825 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1826
1827 std::fill(buf.begin(), buf.end(), 'x');
1828 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001829 kBufSize));
1830 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001831 }
1832
Lei Zhang05ec64c2019-01-09 03:00:06 +00001833 {
1834 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1835 ASSERT_TRUE(annot);
1836 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1837 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1838
1839 std::fill(buf.begin(), buf.end(), 'x');
1840 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001841 kBufSize));
1842 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001843 }
1844
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001845 CloseSavedPage(saved_page);
1846 CloseSavedDocument();
1847 }
1848}
rycsmithcb752f32019-02-21 18:40:53 +00001849
1850TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
1851 // Open a file with combobox widget annotations and load its first page.
1852 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1853 FPDF_PAGE page = LoadPage(0);
1854 ASSERT_TRUE(page);
1855
1856 {
1857 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1858 ASSERT_TRUE(annot);
1859
1860 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1861
1862 annot.reset(FPDFPage_GetAnnot(page, 1));
1863 ASSERT_TRUE(annot);
1864
1865 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00001866
1867 // Check bad form handle / annot.
1868 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
1869 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
1870 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00001871 }
1872
1873 UnloadPage(page);
1874}
1875
1876TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
1877 // Open a file with listbox widget annotations and load its first page.
1878 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1879 FPDF_PAGE page = LoadPage(0);
1880 ASSERT_TRUE(page);
1881
1882 {
1883 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1884 ASSERT_TRUE(annot);
1885
1886 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1887
1888 annot.reset(FPDFPage_GetAnnot(page, 1));
1889 ASSERT_TRUE(annot);
1890
1891 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1892 }
1893
1894 UnloadPage(page);
1895}
1896
1897TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
1898 // Open a file with ink annotations and load its first page.
1899 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1900 FPDF_PAGE page = LoadPage(0);
1901 ASSERT_TRUE(page);
1902
1903 {
1904 // annotations do not have "Opt" array and will return -1
1905 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1906 ASSERT_TRUE(annot);
1907
1908 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1909
1910 annot.reset(FPDFPage_GetAnnot(page, 1));
1911 ASSERT_TRUE(annot);
1912
1913 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1914 }
1915
1916 UnloadPage(page);
1917}
1918
1919TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
1920 // Open a file with combobox widget annotations and load its first page.
1921 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1922 FPDF_PAGE page = LoadPage(0);
1923 ASSERT_TRUE(page);
1924
1925 {
1926 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1927 ASSERT_TRUE(annot);
1928
1929 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001930 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001931 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001932 ASSERT_EQ(8u, length_bytes);
1933 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001934 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001935 buf.data(), length_bytes));
1936 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001937
1938 annot.reset(FPDFPage_GetAnnot(page, 1));
1939 ASSERT_TRUE(annot);
1940
1941 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001942 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001943 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001944 ASSERT_EQ(12u, length_bytes);
1945 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001946 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001947 buf.data(), length_bytes));
1948 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001949
1950 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001951 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001952 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001953 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001954 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001955 buf.data(), length_bytes));
1956 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001957
Lei Zhange7033c82019-02-26 19:30:49 +00001958 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00001959 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1960 nullptr, 0));
1961 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1962 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00001963
1964 // Check bad form handle / annot.
1965 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
1966 EXPECT_EQ(0u,
1967 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
1968 EXPECT_EQ(0u,
1969 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00001970 }
1971
1972 UnloadPage(page);
1973}
1974
1975TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
1976 // Open a file with listbox widget annotations and load its first page.
1977 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1978 FPDF_PAGE page = LoadPage(0);
1979 ASSERT_TRUE(page);
1980
1981 {
1982 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1983 ASSERT_TRUE(annot);
1984
1985 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001986 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001987 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001988 ASSERT_EQ(8u, length_bytes);
1989 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001990 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001991 buf.data(), length_bytes));
1992 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001993
1994 annot.reset(FPDFPage_GetAnnot(page, 1));
1995 ASSERT_TRUE(annot);
1996
1997 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001998 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001999 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002000 ASSERT_EQ(12u, length_bytes);
2001 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002002 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002003 buf.data(), length_bytes));
2004 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002005
2006 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002007 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002008 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002009 ASSERT_EQ(18u, length_bytes);
2010 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002011 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002012 buf.data(), length_bytes));
2013 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002014
2015 // indices out of range
2016 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2017 nullptr, 0));
2018 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2019 nullptr, 0));
2020 }
2021
2022 UnloadPage(page);
2023}
2024
2025TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2026 // Open a file with ink annotations and load its first page.
2027 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2028 FPDF_PAGE page = LoadPage(0);
2029 ASSERT_TRUE(page);
2030
2031 {
2032 // annotations do not have "Opt" array and will return 0
2033 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2034 ASSERT_TRUE(annot);
2035
2036 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2037 nullptr, 0));
2038
2039 annot.reset(FPDFPage_GetAnnot(page, 1));
2040 ASSERT_TRUE(annot);
2041
2042 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2043 nullptr, 0));
2044 }
2045
2046 UnloadPage(page);
2047}
Ryan Smith09c23b12019-04-25 18:09:06 +00002048
2049TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2050 // Open a file with combobox annotations and load its first page.
2051 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2052 FPDF_PAGE page = LoadPage(0);
2053 ASSERT_TRUE(page);
2054
2055 {
2056 // All 3 widgets have Tf font size 12.
2057 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2058 ASSERT_TRUE(annot);
2059
2060 float value;
2061 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2062 EXPECT_EQ(12.0, value);
2063
2064 annot.reset(FPDFPage_GetAnnot(page, 1));
2065 ASSERT_TRUE(annot);
2066
2067 float value_two;
2068 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2069 EXPECT_EQ(12.0, value_two);
2070
2071 annot.reset(FPDFPage_GetAnnot(page, 2));
2072 ASSERT_TRUE(annot);
2073
2074 float value_three;
2075 ASSERT_TRUE(
2076 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2077 EXPECT_EQ(12.0, value_three);
2078 }
2079
2080 UnloadPage(page);
2081}
2082
2083TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2084 // Open a file with textfield annotations and load its first page.
2085 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2086 FPDF_PAGE page = LoadPage(0);
2087 ASSERT_TRUE(page);
2088
2089 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002090 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002091 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2092 ASSERT_TRUE(annot);
2093
2094 float value;
2095 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2096 EXPECT_EQ(12.0, value);
2097
2098 annot.reset(FPDFPage_GetAnnot(page, 1));
2099 ASSERT_TRUE(annot);
2100
2101 float value_two;
2102 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2103 EXPECT_EQ(12.0, value_two);
2104
2105 annot.reset(FPDFPage_GetAnnot(page, 2));
2106 ASSERT_TRUE(annot);
2107
2108 float value_three;
2109 ASSERT_TRUE(
2110 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2111 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002112
2113 float value_four;
2114 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2115 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002116 }
2117
2118 UnloadPage(page);
2119}
2120
2121TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2122 // Open a file with ink annotations and load its first page.
2123 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2124 FPDF_PAGE page = LoadPage(0);
2125 ASSERT_TRUE(page);
2126
2127 {
2128 // Annotations that do not have variable text and will return -1.
2129 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2130 ASSERT_TRUE(annot);
2131
2132 float value;
2133 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2134
2135 annot.reset(FPDFPage_GetAnnot(page, 1));
2136 ASSERT_TRUE(annot);
2137
2138 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2139 }
2140
2141 UnloadPage(page);
2142}
2143
2144TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2145 // Open a file with combobox annotations and load its first page.
2146 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2147 FPDF_PAGE page = LoadPage(0);
2148 ASSERT_TRUE(page);
2149
2150 {
2151 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2152 ASSERT_TRUE(annot);
2153
2154 // Check bad form handle / annot.
2155 float value;
2156 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2157 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2158 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2159 }
2160
2161 UnloadPage(page);
2162}
2163
2164TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2165 // Open a file with textfield annotations and load its first page.
2166 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2167 FPDF_PAGE page = LoadPage(0);
2168 ASSERT_TRUE(page);
2169
2170 {
2171 // Obtain the first annotation, a text field with negative font size, -12.
2172 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2173 ASSERT_TRUE(annot);
2174
2175 float value;
2176 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2177 EXPECT_EQ(-12.0, value);
2178 }
2179
2180 UnloadPage(page);
2181}
Ryan Smith23fdf892019-07-17 21:51:26 +00002182
2183TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2184 // Open a file with checkbox and radiobuttons widget annotations and load its
2185 // first page.
2186 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2187 FPDF_PAGE page = LoadPage(0);
2188 ASSERT_TRUE(page);
2189
2190 {
2191 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2192 ASSERT_TRUE(annot);
2193 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2194 }
2195
2196 UnloadPage(page);
2197}
2198
2199TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2200 // Open a file with checkbox and radiobutton widget annotations and load its
2201 // first page.
2202 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2203 FPDF_PAGE page = LoadPage(0);
2204 ASSERT_TRUE(page);
2205
2206 {
2207 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2208 ASSERT_TRUE(annot);
2209 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2210 }
2211
2212 UnloadPage(page);
2213}
2214
2215TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2216 // Open a file with checkbox and radiobutton widget annotations and load its
2217 // first page.
2218 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2219 FPDF_PAGE page = LoadPage(0);
2220 ASSERT_TRUE(page);
2221
2222 {
2223 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2224 ASSERT_TRUE(annot);
2225 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2226
2227 annot.reset(FPDFPage_GetAnnot(page, 6));
2228 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2229
2230 annot.reset(FPDFPage_GetAnnot(page, 7));
2231 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2232 }
2233
2234 UnloadPage(page);
2235}
2236
2237TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2238 // Open a file with checkbox and radiobutton widget annotations and load its
2239 // first page.
2240 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2241 FPDF_PAGE page = LoadPage(0);
2242 ASSERT_TRUE(page);
2243
2244 {
2245 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2246 ASSERT_TRUE(annot);
2247 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2248
2249 annot.reset(FPDFPage_GetAnnot(page, 3));
2250 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2251
2252 annot.reset(FPDFPage_GetAnnot(page, 4));
2253 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2254 }
2255
2256 UnloadPage(page);
2257}
2258
2259TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2260 // Open a file with checkbox and radiobuttons widget annotations and load its
2261 // first page.
2262 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2263 FPDF_PAGE page = LoadPage(0);
2264 ASSERT_TRUE(page);
2265
2266 {
2267 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2268 ASSERT_TRUE(annot);
2269 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2270 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2271 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2272 }
2273
2274 UnloadPage(page);
2275}
2276
2277TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2278 // Open a file with text widget annotations and load its first page.
2279 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2280 FPDF_PAGE page = LoadPage(0);
2281 ASSERT_TRUE(page);
2282
2283 {
2284 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2285 ASSERT_TRUE(annot);
2286 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2287 }
2288
2289 UnloadPage(page);
2290}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002291
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002292TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2293 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002294 FPDF_PAGE page = LoadPage(0);
2295 ASSERT_TRUE(page);
2296
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002297 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2298
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002299 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002300 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002301 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002302 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002303 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002304
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002305 constexpr int kExpectedAnnotTypes[] = {-1,
2306 FPDF_FORMFIELD_COMBOBOX,
2307 FPDF_FORMFIELD_LISTBOX,
2308 FPDF_FORMFIELD_TEXTFIELD,
2309 FPDF_FORMFIELD_CHECKBOX,
2310 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002311
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002312 for (size_t i = 0; i < FX_ArraySize(kExpectedAnnotTypes); ++i) {
2313 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002314 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002315 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002316 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2317 }
2318 UnloadPage(page);
2319}
2320
2321TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2322 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2323 FPDF_PAGE page = LoadPage(0);
2324 ASSERT_TRUE(page);
2325
2326 {
2327 EXPECT_EQ(0u,
2328 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2329
2330 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2331 ASSERT_TRUE(annot);
2332
2333 EXPECT_EQ(0u,
2334 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2335
2336 unsigned long length_bytes =
2337 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2338 ASSERT_EQ(2u, length_bytes);
2339 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2340 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2341 buf.data(), length_bytes));
2342 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2343 }
2344 {
2345 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2346 ASSERT_TRUE(annot);
2347
2348 unsigned long length_bytes =
2349 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2350 ASSERT_EQ(18u, length_bytes);
2351 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2352 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2353 buf.data(), length_bytes));
2354 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2355 }
2356 UnloadPage(page);
2357}
2358
2359TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2360 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2361 FPDF_PAGE page = LoadPage(0);
2362 ASSERT_TRUE(page);
2363
2364 {
2365 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2366 ASSERT_TRUE(annot);
2367
2368 unsigned long length_bytes =
2369 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2370 ASSERT_EQ(2u, length_bytes);
2371 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2372 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2373 buf.data(), length_bytes));
2374 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2375 }
2376 {
2377 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2378 ASSERT_TRUE(annot);
2379
2380 unsigned long length_bytes =
2381 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2382 ASSERT_EQ(14u, length_bytes);
2383 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2384 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2385 buf.data(), length_bytes));
2386 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2387 }
2388 UnloadPage(page);
2389}
2390
2391TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2392 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2393 FPDF_PAGE page = LoadPage(0);
2394 ASSERT_TRUE(page);
2395
2396 {
2397 EXPECT_EQ(0u,
2398 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2399
2400 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2401 ASSERT_TRUE(annot);
2402
2403 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2404
2405 unsigned long length_bytes =
2406 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2407 ASSERT_EQ(18u, length_bytes);
2408 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2409 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2410 buf.data(), length_bytes));
2411 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2412 }
2413 UnloadPage(page);
2414}
2415
2416TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2417 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2418 FPDF_PAGE page = LoadPage(0);
2419 ASSERT_TRUE(page);
2420
2421 {
2422 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2423 ASSERT_TRUE(annot);
2424
2425 unsigned long length_bytes =
2426 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2427 ASSERT_EQ(30u, length_bytes);
2428 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2429 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2430 buf.data(), length_bytes));
2431 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2432 }
2433 UnloadPage(page);
2434}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002435
Lei Zhang9b444002020-04-17 17:35:23 +00002436TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2437 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002438 FPDF_PAGE page = LoadPage(0);
2439 ASSERT_TRUE(page);
2440
Lei Zhang9b444002020-04-17 17:35:23 +00002441 // Verify widgets are by default focusable.
2442 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2443 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002444
Lei Zhang9b444002020-04-17 17:35:23 +00002445 // Expected annot subtypes for page 0 of annots.pdf.
2446 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2447 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2448 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2449 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2450 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002451
Lei Zhang9b444002020-04-17 17:35:23 +00002452 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002453 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002454 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2455 kExpectedAnnotSubtypes,
2456 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002457
Lei Zhang9b444002020-04-17 17:35:23 +00002458 // Make no annotation type focusable using the preferred method.
2459 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2460 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002461
Lei Zhang9b444002020-04-17 17:35:23 +00002462 // Restore the focusable type count back to 1, then set it back to 0 using a
2463 // different method.
2464 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2465 ASSERT_TRUE(
2466 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2467 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002468
Lei Zhang9b444002020-04-17 17:35:23 +00002469 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002470 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002471
Lei Zhang9b444002020-04-17 17:35:23 +00002472 // Now make links focusable.
2473 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2474 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002475
Lei Zhang9b444002020-04-17 17:35:23 +00002476 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002477 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002478 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2479 kExpectedAnnotSubtypes,
2480 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002481
Lei Zhang9b444002020-04-17 17:35:23 +00002482 // Test invalid parameters.
2483 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
2484 FX_ArraySize(kDefaultSubtypes)));
2485 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
2486 FX_ArraySize(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002487 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2488
Lei Zhang9b444002020-04-17 17:35:23 +00002489 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2490 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2491 subtypes.size()));
2492 EXPECT_FALSE(
2493 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2494 EXPECT_FALSE(
2495 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002496
2497 UnloadPage(page);
2498}
Lei Zhang671aece2020-04-14 19:02:26 +00002499
2500TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
2501 ASSERT_TRUE(OpenDocument("annots.pdf"));
2502 FPDF_PAGE page = LoadPage(0);
2503 ASSERT_TRUE(page);
2504
2505 {
2506#if defined(OS_WIN)
2507 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
2508#elif defined(OS_MACOSX)
2509 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
2510#else
2511 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
2512#endif
2513 // Check the initial rendering.
2514 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2515 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2516 }
2517
2518 // Make links and highlights focusable.
2519 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
2520 FPDF_ANNOT_HIGHLIGHT};
2521 constexpr int kSubTypesCount = FX_ArraySize(kSubTypes);
2522 ASSERT_TRUE(
2523 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
2524 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
2525 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
2526 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
2527 subtypes.size()));
2528 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
2529 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
2530
2531 {
2532#if defined(OS_WIN)
2533 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
2534#elif defined(OS_MACOSX)
2535 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
2536#else
2537 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
2538#endif
2539 // Focus the first link and check the rendering.
2540 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2541 ASSERT_TRUE(annot);
2542 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2543 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2544 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2545 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2546 }
2547
2548 {
2549#if defined(OS_WIN)
2550 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
2551#elif defined(OS_MACOSX)
2552 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
2553#else
2554 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
2555#endif
2556 // Focus the first highlight and check the rendering.
2557 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2558 ASSERT_TRUE(annot);
2559 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
2560 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2561 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2562 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2563 }
2564
2565 UnloadPage(page);
2566}