blob: 0fb5084c363a850d01721778ecb0569aaefc1e22 [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"
26
27namespace {
28
29void VerifyFocusableAnnotSubtypes(
30 FPDF_FORMHANDLE form_handle,
31 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) {
32 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
33 FPDFAnnot_GetFocusableSubtypesCount(form_handle));
34
35 std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes(
36 expected_subtypes.size());
37 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(
38 form_handle, actual_subtypes.data(), actual_subtypes.size()));
39 for (size_t i = 0; i < expected_subtypes.size(); ++i)
40 ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]);
41}
42
43void SetAndVerifyFocusableAnnotSubtypes(
44 FPDF_FORMHANDLE form_handle,
45 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) {
46 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(),
47 subtypes.size()));
48 VerifyFocusableAnnotSubtypes(form_handle, subtypes);
49}
50
51void VerifyAnnotationSubtypesAndFocusability(
52 FPDF_FORMHANDLE form_handle,
53 FPDF_PAGE page,
54 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes,
55 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) {
56 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
57 FPDFPage_GetAnnotCount(page));
58 for (size_t i = 0; i < expected_subtypes.size(); ++i) {
59 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
60 ASSERT_TRUE(annot);
61 EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get()));
62
63 bool expected_focusable = pdfium::ContainsValue(expected_focusable_subtypes,
64 expected_subtypes[i]);
65 EXPECT_EQ(expected_focusable,
66 FORM_SetFocusedAnnot(form_handle, annot.get()));
67
68 // Kill the focus so the next test starts in an unfocused state.
69 FORM_ForceToKillFocus(form_handle);
70 }
71}
72
73} // namespace
Jane Liu4fd9a472017-06-01 18:56:09 -040074
Lei Zhangab41f252018-12-23 03:10:50 +000075class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040076
Lei Zhangab41f252018-12-23 03:10:50 +000077TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +000078 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
79 FPDF_PAGE page = LoadPage(0);
80 ASSERT_TRUE(page);
81
82 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
83
84 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
85 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
86 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
87 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
88 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
89
90 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
91
92 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
93
94 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
95 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
96 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
97
98 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
99
Lei Zhang4f556b82019-04-08 16:32:41 +0000100 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +0000101 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +0000102 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
103
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000104 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +0000105 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
106 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
107 EXPECT_EQ(0u,
108 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
109
110 UnloadPage(page);
111}
112
Lei Zhang3d9a0972019-03-04 19:34:09 +0000113TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
114 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
115 FPDF_PAGE page = LoadPage(0);
116 ASSERT_TRUE(page);
117
118 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +0000119 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +0000120
121 UnloadPage(page);
122}
123
Lei Zhangab41f252018-12-23 03:10:50 +0000124TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -0400125 // Open a file with one annotation and load its first page.
126 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000127 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -0400128 ASSERT_TRUE(page);
129
130 // This annotation has a malformed appearance stream, which does not have its
131 // normal appearance defined, only its rollover appearance. In this case, its
132 // normal appearance should be generated, allowing the highlight annotation to
133 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000134 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +0000135 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -0400136
137 UnloadPage(page);
138}
139
Lei Zhang03e5e682019-09-16 19:45:55 +0000140// TODO(crbug.com/pdfium/11): Fix this test and enable.
141#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
142#define MAYBE_RenderMultilineMarkupAnnotWithoutAP \
143 DISABLED_RenderMultilineMarkupAnnotWithoutAP
144#else
145#define MAYBE_RenderMultilineMarkupAnnotWithoutAP \
146 RenderMultilineMarkupAnnotWithoutAP
147#endif
148TEST_F(FPDFAnnotEmbedderTest, MAYBE_RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang4f556b82019-04-08 16:32:41 +0000149 static const char kMd5[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +0000150 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000151 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
152 FPDF_PAGE page = LoadPage(0);
153 ASSERT_TRUE(page);
154
Tom Sepeze08d2b12018-04-25 18:49:32 +0000155 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000156 CompareBitmap(bitmap.get(), 595, 842, kMd5);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000157
158 UnloadPage(page);
159}
160
Lei Zhangab41f252018-12-23 03:10:50 +0000161TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400162 // Open a file with one annotation and load its first page.
163 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000164 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400165 ASSERT_TRUE(page);
166
167 // Check that there is a total of 1 annotation on its first page.
168 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
169
170 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000171 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000172 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000173 ASSERT_TRUE(annot);
174 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400175
Lei Zhanga21d5932018-02-05 18:28:38 +0000176 // Check that the annotation color is yellow.
177 unsigned int R;
178 unsigned int G;
179 unsigned int B;
180 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000181 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000182 &G, &B, &A));
183 EXPECT_EQ(255u, R);
184 EXPECT_EQ(255u, G);
185 EXPECT_EQ(0u, B);
186 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400187
Lei Zhanga21d5932018-02-05 18:28:38 +0000188 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000189 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000190 EXPECT_EQ(FPDF_OBJECT_STRING,
191 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000192 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000193 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000194 ASSERT_EQ(28u, length_bytes);
195 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000196 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000197 length_bytes));
198 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400199
Lei Zhanga21d5932018-02-05 18:28:38 +0000200 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000201 EXPECT_EQ(
202 FPDF_OBJECT_STRING,
203 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000204 length_bytes = FPDFAnnot_GetStringValue(
205 annot.get(), pdfium::annotation::kContents, nullptr, 0);
206 ASSERT_EQ(2690u, length_bytes);
207 buf = GetFPDFWideStringBuffer(length_bytes);
208 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
209 pdfium::annotation::kContents,
210 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000211 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000212 L"This is a note for that highlight annotation. Very long highlight "
213 "annotation. Long long long Long long longLong long longLong long "
214 "longLong long longLong long longLong 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 long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000232 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400233
Lei Zhanga21d5932018-02-05 18:28:38 +0000234 // Check that the quadpoints are correct.
235 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000236 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000237 EXPECT_EQ(115.802643f, quadpoints.x1);
238 EXPECT_EQ(718.913940f, quadpoints.y1);
239 EXPECT_EQ(157.211182f, quadpoints.x4);
240 EXPECT_EQ(706.264465f, quadpoints.y4);
241 }
Tom Sepez507d0192018-11-07 16:37:51 +0000242 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400243}
244
Lei Zhang03e5e682019-09-16 19:45:55 +0000245// TODO(crbug.com/pdfium/11): Fix this test and enable.
246#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
247#define MAYBE_ExtractInkMultiple DISABLED_ExtractInkMultiple
248#else
249#define MAYBE_ExtractInkMultiple ExtractInkMultiple
250#endif
251TEST_F(FPDFAnnotEmbedderTest, MAYBE_ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400252 // Open a file with three annotations and load its first page.
253 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000254 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400255 ASSERT_TRUE(page);
256
257 // Check that there is a total of 3 annotation on its first page.
258 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
259
Lei Zhanga21d5932018-02-05 18:28:38 +0000260 {
261 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000262 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000263 ASSERT_TRUE(annot);
264 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400265
Lei Zhanga21d5932018-02-05 18:28:38 +0000266 // Check that the annotation color is blue with opacity.
267 unsigned int R;
268 unsigned int G;
269 unsigned int B;
270 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000271 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000272 &G, &B, &A));
273 EXPECT_EQ(0u, R);
274 EXPECT_EQ(0u, G);
275 EXPECT_EQ(255u, B);
276 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400277
Lei Zhanga21d5932018-02-05 18:28:38 +0000278 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000279 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
280 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400281
Lei Zhang4f556b82019-04-08 16:32:41 +0000282 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000283 // Note that upon rendering, the rectangle coordinates will be adjusted.
284 FS_RECTF rect;
285 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
286 EXPECT_EQ(351.820404f, rect.left);
287 EXPECT_EQ(583.830688f, rect.bottom);
288 EXPECT_EQ(475.336090f, rect.right);
289 EXPECT_EQ(681.535034f, rect.top);
290 }
Tom Sepezef43c262018-11-07 16:41:32 +0000291 {
292 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
293 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
294 }
Tom Sepez507d0192018-11-07 16:37:51 +0000295 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400296}
Jane Liu20eafda2017-06-07 10:33:24 -0400297
Lei Zhangab41f252018-12-23 03:10:50 +0000298TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400299 // Open a file with one annotation and load its first page.
300 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000301 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400302 ASSERT_TRUE(page);
303
304 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400305 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400306
307 UnloadPage(page);
308}
309
Lei Zhangab41f252018-12-23 03:10:50 +0000310TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400311 // Open a file with no annotation and load its first page.
312 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000313 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400314 ASSERT_TRUE(page);
315 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
316
Lei Zhanga21d5932018-02-05 18:28:38 +0000317 {
318 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000319 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000320 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400321
Lei Zhanga21d5932018-02-05 18:28:38 +0000322 // Check that there is now 1 annotations on this page.
323 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400324
Lei Zhanga21d5932018-02-05 18:28:38 +0000325 // Check that the subtype of the annotation is correct.
326 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
327 }
Jane Liue10509a2017-06-20 16:47:41 -0400328
Lei Zhanga21d5932018-02-05 18:28:38 +0000329 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000330 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000331 ASSERT_TRUE(annot);
332 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400333
Lei Zhanga21d5932018-02-05 18:28:38 +0000334 // Set the color of the annotation.
335 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
336 102, 153, 204));
337 // Check that the color has been set correctly.
338 unsigned int R;
339 unsigned int G;
340 unsigned int B;
341 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000342 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000343 &G, &B, &A));
344 EXPECT_EQ(51u, R);
345 EXPECT_EQ(102u, G);
346 EXPECT_EQ(153u, B);
347 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400348
Lei Zhanga21d5932018-02-05 18:28:38 +0000349 // Change the color of the annotation.
350 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
351 153, 102, 51));
352 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000353 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000354 &G, &B, &A));
355 EXPECT_EQ(204u, R);
356 EXPECT_EQ(153u, G);
357 EXPECT_EQ(102u, B);
358 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400359
Lei Zhanga21d5932018-02-05 18:28:38 +0000360 // Set the annotation rectangle.
361 FS_RECTF rect;
362 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
363 EXPECT_EQ(0.f, rect.left);
364 EXPECT_EQ(0.f, rect.right);
365 rect.left = 35;
366 rect.bottom = 150;
367 rect.right = 53;
368 rect.top = 165;
369 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
370 // Check that the annotation rectangle has been set correctly.
371 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
372 EXPECT_EQ(35.f, rect.left);
373 EXPECT_EQ(150.f, rect.bottom);
374 EXPECT_EQ(53.f, rect.right);
375 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400376
Lei Zhanga21d5932018-02-05 18:28:38 +0000377 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000378 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000379 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000380 ASSERT_TRUE(FPDFAnnot_SetStringValue(
381 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000382 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000383 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000384 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000385 ASSERT_EQ(74u, length_bytes);
386 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
387 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
388 pdfium::annotation::kContents,
389 buf.data(), length_bytes));
390 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000391 }
Jane Liu20eafda2017-06-07 10:33:24 -0400392 UnloadPage(page);
393}
394
Lei Zhang03e5e682019-09-16 19:45:55 +0000395// TODO(crbug.com/pdfium/11): Fix this test and enable.
396#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
397#define MAYBE_AddAndSaveUnderlineAnnotation \
398 DISABLED_AddAndSaveUnderlineAnnotation
399#else
400#define MAYBE_AddAndSaveUnderlineAnnotation AddAndSaveUnderlineAnnotation
401#endif
402TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400403 // Open a file with one annotation and load its first page.
404 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000405 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400406 ASSERT_TRUE(page);
407
408 // Check that there is a total of one annotation on its first page, and verify
409 // its quadpoints.
410 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400411 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000412 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000413 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000414 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000415 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000416 EXPECT_EQ(115.802643f, quadpoints.x1);
417 EXPECT_EQ(718.913940f, quadpoints.y1);
418 EXPECT_EQ(157.211182f, quadpoints.x4);
419 EXPECT_EQ(706.264465f, quadpoints.y4);
420 }
Jane Liu20eafda2017-06-07 10:33:24 -0400421
422 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000423 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000424 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000425 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
426 ASSERT_TRUE(annot);
427 quadpoints.x1 = 140.802643f;
428 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000429 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000430 }
Jane Liu20eafda2017-06-07 10:33:24 -0400431
432 // Save the document, closing the page and document.
433 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000434 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400435
436 // Open the saved document.
Lei Zhang4f556b82019-04-08 16:32:41 +0000437 static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400438
Lei Zhang0b494052019-01-31 21:41:15 +0000439 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000440 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000441 VerifySavedRendering(page, 612, 792, kMd5);
Jane Liu20eafda2017-06-07 10:33:24 -0400442
443 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000444 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400445
Lei Zhanga21d5932018-02-05 18:28:38 +0000446 {
447 // Check that the second annotation is an underline annotation and verify
448 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000449 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000450 ASSERT_TRUE(new_annot);
451 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
452 FS_QUADPOINTSF new_quadpoints;
453 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000454 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000455 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
456 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
457 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
458 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
459 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400460
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000461 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400462 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400463}
Jane Liu06462752017-06-27 16:41:14 -0400464
Lei Zhangab41f252018-12-23 03:10:50 +0000465TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000466 // Open a file with four annotations and load its first page.
467 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
468 FPDF_PAGE page = LoadPage(0);
469 ASSERT_TRUE(page);
470 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
471
472 // Retrieve the highlight annotation.
473 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
474 ASSERT_TRUE(annot);
475 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
476
477 FS_QUADPOINTSF quadpoints;
478 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
479
480 {
481 // Verify the current one set of quadpoints.
482 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
483
484 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
485 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
486 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
487 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
488 }
489
490 {
491 // Update the quadpoints.
492 FS_QUADPOINTSF new_quadpoints = quadpoints;
493 new_quadpoints.y1 -= 20.f;
494 new_quadpoints.y2 -= 20.f;
495 new_quadpoints.y3 -= 20.f;
496 new_quadpoints.y4 -= 20.f;
497 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
498
499 // Verify added quadpoint set
500 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
501 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
502 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
503 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
504 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
505 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
506 }
507
508 {
509 // Append a new set of quadpoints.
510 FS_QUADPOINTSF new_quadpoints = quadpoints;
511 new_quadpoints.y1 += 20.f;
512 new_quadpoints.y2 += 20.f;
513 new_quadpoints.y3 += 20.f;
514 new_quadpoints.y4 += 20.f;
515 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
516
517 // Verify added quadpoint set
518 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
519 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
520 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
521 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
522 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
523 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
524 }
525
526 {
527 // Setting and getting quadpoints at out-of-bound index should fail
528 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
529 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
530 }
531
532 FPDFPage_CloseAnnot(annot);
533
534 // Retrieve the square annotation
535 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
536
537 {
538 // Check that attempting to set its quadpoints would fail
539 ASSERT_TRUE(squareAnnot);
540 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
541 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
542 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
543 }
544
545 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000546 UnloadPage(page);
547}
548
Lei Zhang03e5e682019-09-16 19:45:55 +0000549// TODO(crbug.com/pdfium/11): Fix this test and enable.
550#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
551#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
552#else
553#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
554#endif
555TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000556#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +0000557 static const char kMd5Original[] = "fc59468d154f397fd298c69f47ef565a";
Lei Zhang4f556b82019-04-08 16:32:41 +0000558 static const char kMd5ModifiedHighlight[] =
Lei Zhang0f6b4342020-02-25 20:00:39 +0000559 "e64bf648f6e9354d1f3eedb47a2c9498";
560 static const char kMd5ModifiedSquare[] = "a66591662c8e7ad3c6059952e234bebf";
Lei Zhange67bcc72019-04-30 18:55:58 +0000561#elif defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000562 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
563 static const char kMd5ModifiedHighlight[] =
564 "66f3caef3a7d488a4fa1ad37fc06310e";
565 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400566#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000567 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
568 static const char kMd5ModifiedHighlight[] =
569 "66f3caef3a7d488a4fa1ad37fc06310e";
570 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400571#endif
572
Jane Liu06462752017-06-27 16:41:14 -0400573 // Open a file with four annotations and load its first page.
574 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000575 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400576 ASSERT_TRUE(page);
577 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
578
Jane Liub370e5a2017-08-16 13:24:58 -0400579 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000580 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000581 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000582 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000583 }
Jane Liub370e5a2017-08-16 13:24:58 -0400584
Jane Liu0c6b07d2017-08-15 10:50:22 -0400585 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400586 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000587
588 // Retrieve the highlight annotation which has its AP stream already defined.
589 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000590 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000591 ASSERT_TRUE(annot);
592 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
593
594 // Check that color cannot be set when an AP stream is defined already.
595 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
596 102, 153, 204));
597
598 // Verify its attachment points.
599 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000600 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000601 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
602 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
603 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
604 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
605
606 // Check that updating the attachment points would succeed.
607 quadpoints.x1 -= 50.f;
608 quadpoints.x2 -= 50.f;
609 quadpoints.x3 -= 50.f;
610 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000611 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000612 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000613 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000614 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
615 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
616 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
617 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
618
619 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000620 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000621 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000622 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000623 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000624
625 // Verify its annotation rectangle.
626 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
627 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
628 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
629 EXPECT_NEAR(136.325f, rect.right, 0.001f);
630 EXPECT_NEAR(721.292f, rect.top, 0.001f);
631
632 // Check that updating the rectangle would succeed.
633 rect.left -= 60.f;
634 rect.right -= 60.f;
635 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
636 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
637 EXPECT_EQ(rect.right, new_rect.right);
638 }
Jane Liu06462752017-06-27 16:41:14 -0400639
Jane Liub370e5a2017-08-16 13:24:58 -0400640 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000641 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000642 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000643 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000644 }
Jane Liub370e5a2017-08-16 13:24:58 -0400645
Lei Zhanga21d5932018-02-05 18:28:38 +0000646 {
647 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000648 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000649 ASSERT_TRUE(annot);
650 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400651
Lei Zhanga21d5932018-02-05 18:28:38 +0000652 // Check that updating the rectangle would succeed.
653 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
654 rect.left += 70.f;
655 rect.right += 70.f;
656 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
657 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
658 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400659
Lei Zhanga21d5932018-02-05 18:28:38 +0000660 // Check that updating the rectangle changes the square annotation's
661 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000662 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000663 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000664 }
Jane Liub370e5a2017-08-16 13:24:58 -0400665
Jane Liu06462752017-06-27 16:41:14 -0400666 UnloadPage(page);
667}
Jane Liu8ce58f52017-06-29 13:40:22 -0400668
Lei Zhangab41f252018-12-23 03:10:50 +0000669TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000670 // Open a file with multiline markup annotations.
671 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
672 FPDF_PAGE page = LoadPage(0);
673 ASSERT_TRUE(page);
674 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000675 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000676 ASSERT_TRUE(annot);
677
678 // This is a three line annotation.
679 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
680 }
681 UnloadPage(page);
682
683 // null annotation should return 0
684 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
685}
686
Lei Zhangab41f252018-12-23 03:10:50 +0000687TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400688 // Open a file with 3 annotations on its first page.
689 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000690 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400691 ASSERT_TRUE(page);
692 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
693
Jane Liu0c6b07d2017-08-15 10:50:22 -0400694 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400695
Lei Zhanga21d5932018-02-05 18:28:38 +0000696 // Check that the annotations have the expected rectangle coordinates.
697 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000698 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000699 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
700 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
701 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400702
Lei Zhanga21d5932018-02-05 18:28:38 +0000703 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000704 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000705 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
706 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
707 }
708
709 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000710 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000711 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
712 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
713 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400714
715 // Check that nothing happens when attempting to remove an annotation with an
716 // out-of-bound index.
717 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
718 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
719 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
720
721 // Remove the second annotation.
722 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
723 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
724 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
725
726 // Save the document, closing the page and document.
727 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000728 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400729
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400730 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400731 // Open the saved document.
732 std::string new_file = GetString();
733 FPDF_FILEACCESS file_access;
734 memset(&file_access, 0, sizeof(file_access));
735 file_access.m_FileLen = new_file.size();
736 file_access.m_GetBlock = GetBlockFromString;
737 file_access.m_Param = &new_file;
738 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
739 ASSERT_TRUE(new_doc);
740 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
741 ASSERT_TRUE(new_page);
742
743 // Check that the saved document has 2 annotations on the first page.
744 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
745
Lei Zhanga21d5932018-02-05 18:28:38 +0000746 // Check that the remaining 2 annotations are the original 1st and 3rd ones
747 // by verifying their rectangle coordinates.
748 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000749 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000750 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
751 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
752 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400753
Lei Zhanga21d5932018-02-05 18:28:38 +0000754 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000755 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000756 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
757 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
758 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400759 FPDF_ClosePage(new_page);
760 FPDF_CloseDocument(new_doc);
761}
Jane Liu8ce58f52017-06-29 13:40:22 -0400762
Lei Zhang03e5e682019-09-16 19:45:55 +0000763// TODO(crbug.com/pdfium/11): Fix this test and enable.
764#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
765#define MAYBE_AddAndModifyPath DISABLED_AddAndModifyPath
766#else
767#define MAYBE_AddAndModifyPath AddAndModifyPath
768#endif
769TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyPath) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000770#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +0000771 static const char kMd5Original[] = "80d7b6cc7b13a78d77a6151bc846e80b";
772 static const char kMd5ModifiedPath[] = "8cfae6d547fc5d6702f5f1ac631beb5e";
773 static const char kMd5TwoPaths[] = "9677e4892bb02950d3e4dbe74470578f";
774 static const char kMd5NewAnnot[] = "e8ebddac4db8c0a4b556ddf79aa1a26d";
Lei Zhange67bcc72019-04-30 18:55:58 +0000775#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +0000776 static const char kMd5Original[] = "6aa001a77ec05d0f1b0d1d22e28744d4";
777 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
778 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
779 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Jane Liubaa7ff42017-06-29 19:18:23 -0400780#else
Lei Zhanga2b70732019-06-25 08:34:22 +0000781 static const char kMd5Original[] = "b42cef463483e668eaf4055a65e4f1f5";
782 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
783 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
784 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -0400785#endif
786
787 // Open a file with two annotations and load its first page.
788 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000789 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400790 ASSERT_TRUE(page);
791 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
792
793 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000794 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000795 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000796 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000797 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400798
Lei Zhanga21d5932018-02-05 18:28:38 +0000799 {
800 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000801 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000802 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400803
Lei Zhanga21d5932018-02-05 18:28:38 +0000804 // Check that this annotation has one path object and retrieve it.
805 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000806 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000807 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
808 EXPECT_FALSE(path);
809 path = FPDFAnnot_GetObject(annot.get(), 0);
810 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
811 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400812
Lei Zhanga21d5932018-02-05 18:28:38 +0000813 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +0000814 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +0000815 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400816
Lei Zhanga21d5932018-02-05 18:28:38 +0000817 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000818 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000819 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000820 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000821 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400822
Lei Zhanga21d5932018-02-05 18:28:38 +0000823 // Add a second path object to the same annotation.
824 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
825 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +0000826 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
827 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +0000828 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
829 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
830 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400831
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000832 // The object is in the annontation, not in the page, so the page object
833 // array should not change.
834 ASSERT_EQ(32, FPDFPage_CountObjects(page));
835
Lei Zhanga21d5932018-02-05 18:28:38 +0000836 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000837 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000838 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000839 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000840 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400841
Lei Zhanga21d5932018-02-05 18:28:38 +0000842 // Delete the newly added path object.
843 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
844 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000845 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000846 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400847
848 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000849 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000850 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000851 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000852 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400853
Jane Liubaa7ff42017-06-29 19:18:23 -0400854 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400855
Lei Zhanga21d5932018-02-05 18:28:38 +0000856 {
857 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000858 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000859 ASSERT_TRUE(annot);
860 rect.left = 200.f;
861 rect.bottom = 400.f;
862 rect.right = 500.f;
863 rect.top = 600.f;
864 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400865
Lei Zhanga21d5932018-02-05 18:28:38 +0000866 // Add a new path to the annotation.
867 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
868 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
869 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
870 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
871 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +0000872 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
873 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +0000874 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
875 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
876 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
877
878 // Check that the annotation's bounding box came from its rectangle.
879 FS_RECTF new_rect;
880 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
881 EXPECT_EQ(rect.left, new_rect.left);
882 EXPECT_EQ(rect.bottom, new_rect.bottom);
883 EXPECT_EQ(rect.right, new_rect.right);
884 EXPECT_EQ(rect.top, new_rect.top);
885 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400886
887 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400888 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000889 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400890
891 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +0000892 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000893 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000894 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400895
Jane Liu36567742017-07-06 11:13:35 -0400896 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000897 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400898
Lei Zhanga21d5932018-02-05 18:28:38 +0000899 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000900 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000901 ASSERT_TRUE(annot);
902 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400903
Lei Zhanga21d5932018-02-05 18:28:38 +0000904 // Check that the new annotation's rectangle is as defined.
905 FS_RECTF new_rect;
906 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
907 EXPECT_EQ(rect.left, new_rect.left);
908 EXPECT_EQ(rect.bottom, new_rect.bottom);
909 EXPECT_EQ(rect.right, new_rect.right);
910 EXPECT_EQ(rect.top, new_rect.top);
911 }
912
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000913 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400914 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400915}
Jane Liub137e752017-07-05 15:04:33 -0400916
Lei Zhangab41f252018-12-23 03:10:50 +0000917TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400918 // Open a file with an annotation and load its first page.
919 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000920 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400921 ASSERT_TRUE(page);
922
923 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000924 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000925 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000926 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
927 }
Jane Liub137e752017-07-05 15:04:33 -0400928
Lei Zhanga21d5932018-02-05 18:28:38 +0000929 {
930 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000931 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000932 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400933
Lei Zhanga21d5932018-02-05 18:28:38 +0000934 // Check that the original flag values are as expected.
935 int flags = FPDFAnnot_GetFlags(annot.get());
936 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
937 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400938
Lei Zhanga21d5932018-02-05 18:28:38 +0000939 // Set the HIDDEN flag.
940 flags |= FPDF_ANNOT_FLAG_HIDDEN;
941 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
942 flags = FPDFAnnot_GetFlags(annot.get());
943 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
944 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400945
Lei Zhanga21d5932018-02-05 18:28:38 +0000946 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000947 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000948 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000949 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
950 }
Jane Liub137e752017-07-05 15:04:33 -0400951
Lei Zhanga21d5932018-02-05 18:28:38 +0000952 // Unset the HIDDEN flag.
953 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
954 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
955 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
956 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
957 flags = FPDFAnnot_GetFlags(annot.get());
958 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
959 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400960
Lei Zhanga21d5932018-02-05 18:28:38 +0000961 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000962 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000963 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000964 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
965 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000966 }
Jane Liub137e752017-07-05 15:04:33 -0400967
Jane Liub137e752017-07-05 15:04:33 -0400968 UnloadPage(page);
969}
Jane Liu36567742017-07-06 11:13:35 -0400970
Lei Zhang03e5e682019-09-16 19:45:55 +0000971// TODO(crbug.com/pdfium/11): Fix this test and enable.
972#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
973#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
974#else
975#define MAYBE_AddAndModifyImage AddAndModifyImage
976#endif
977TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000978#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +0000979 static const char kMd5Original[] = "80d7b6cc7b13a78d77a6151bc846e80b";
980 static const char kMd5NewImage[] = "dd18709d90c245a12ce0b8c4d092bea9";
981 static const char kMd5ModifiedImage[] = "8d6f478ff8c7e67d49b253f1af587a99";
Lei Zhange67bcc72019-04-30 18:55:58 +0000982#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +0000983 static const char kMd5Original[] = "6aa001a77ec05d0f1b0d1d22e28744d4";
984 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
985 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -0400986#else
Lei Zhanga2b70732019-06-25 08:34:22 +0000987 static const char kMd5Original[] = "b42cef463483e668eaf4055a65e4f1f5";
988 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
989 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -0400990#endif
991
992 // Open a file with two annotations and load its first page.
993 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000994 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400995 ASSERT_TRUE(page);
996 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
997
998 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000999 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001000 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001001 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001002 }
Jane Liu36567742017-07-06 11:13:35 -04001003
Jane Liu36567742017-07-06 11:13:35 -04001004 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +00001005 FPDF_BITMAP image_bitmap;
1006
1007 {
1008 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001009 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001010 ASSERT_TRUE(annot);
1011 FS_RECTF rect;
1012 rect.left = 200.f;
1013 rect.bottom = 600.f;
1014 rect.right = 400.f;
1015 rect.top = 800.f;
1016 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1017
1018 // Add a solid-color translucent image object to the new annotation.
1019 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1020 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1021 0xeeeecccc);
1022 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1023 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1024 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1025 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1026 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
1027 kBitmapSize, 0, 0));
1028 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1029 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1030 }
Jane Liu36567742017-07-06 11:13:35 -04001031
1032 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001033 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001034 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001035 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001036 }
Jane Liu36567742017-07-06 11:13:35 -04001037
Lei Zhanga21d5932018-02-05 18:28:38 +00001038 {
1039 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001040 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001041 ASSERT_TRUE(annot);
1042 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1043 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1044 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -04001045
Lei Zhanga21d5932018-02-05 18:28:38 +00001046 // Modify the image in the new annotation.
1047 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
1048 0xff000000);
1049 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1050 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1051 }
Jane Liu36567742017-07-06 11:13:35 -04001052
1053 // Save the document, closing the page and document.
1054 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001055 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001056 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001057
1058 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001059 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001060}
1061
Lei Zhang03e5e682019-09-16 19:45:55 +00001062// TODO(crbug.com/pdfium/11): Fix this test and enable.
1063#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1064#define MAYBE_AddAndModifyText DISABLED_AddAndModifyText
1065#else
1066#define MAYBE_AddAndModifyText AddAndModifyText
1067#endif
1068TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyText) {
Lei Zhange4cdac52019-04-30 16:45:57 +00001069#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001070 static const char kMd5Original[] = "80d7b6cc7b13a78d77a6151bc846e80b";
1071 static const char kMd5NewText[] = "e657266260b88c964938efe6c9b292da";
1072 static const char kMd5ModifiedText[] = "7accdf2bac64463101783221f53d3188";
Lei Zhange67bcc72019-04-30 18:55:58 +00001073#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001074 static const char kMd5Original[] = "6aa001a77ec05d0f1b0d1d22e28744d4";
1075 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1076 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Jane Liu36567742017-07-06 11:13:35 -04001077#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001078 static const char kMd5Original[] = "b42cef463483e668eaf4055a65e4f1f5";
1079 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1080 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001081#endif
1082
1083 // Open a file with two annotations and load its first page.
1084 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001085 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001086 ASSERT_TRUE(page);
1087 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1088
1089 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001090 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001091 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001092 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001093 }
Jane Liu36567742017-07-06 11:13:35 -04001094
Lei Zhanga21d5932018-02-05 18:28:38 +00001095 {
1096 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001097 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001098 ASSERT_TRUE(annot);
1099 FS_RECTF rect;
1100 rect.left = 200.f;
1101 rect.bottom = 550.f;
1102 rect.right = 450.f;
1103 rect.top = 650.f;
1104 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001105
Lei Zhanga21d5932018-02-05 18:28:38 +00001106 // Add a translucent text object to the new annotation.
1107 FPDF_PAGEOBJECT text_object =
1108 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1109 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001110 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001111 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1112 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001113 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001114 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1115 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1116 }
Jane Liu36567742017-07-06 11:13:35 -04001117
1118 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001119 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001120 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001121 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001122 }
Jane Liu36567742017-07-06 11:13:35 -04001123
Lei Zhanga21d5932018-02-05 18:28:38 +00001124 {
1125 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001126 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001127 ASSERT_TRUE(annot);
1128 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1129 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1130 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001131
Lei Zhanga21d5932018-02-05 18:28:38 +00001132 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001133 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001134 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1135 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1136 }
Jane Liu36567742017-07-06 11:13:35 -04001137
1138 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001139 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001140 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001141 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001142 }
Jane Liu36567742017-07-06 11:13:35 -04001143
1144 // Remove the new annotation, and check that the page renders as before.
1145 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001146 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001147 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001148 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001149 }
Jane Liu36567742017-07-06 11:13:35 -04001150
1151 UnloadPage(page);
1152}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001153
Lei Zhang03e5e682019-09-16 19:45:55 +00001154// TODO(crbug.com/pdfium/11): Fix this test and enable.
1155#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1156#define MAYBE_GetSetStringValue DISABLED_GetSetStringValue
1157#else
1158#define MAYBE_GetSetStringValue GetSetStringValue
1159#endif
1160TEST_F(FPDFAnnotEmbedderTest, MAYBE_GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001161 // Open a file with four annotations and load its first page.
1162 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001163 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001164 ASSERT_TRUE(page);
1165
Lei Zhang4f556b82019-04-08 16:32:41 +00001166 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001167
Lei Zhanga21d5932018-02-05 18:28:38 +00001168 {
1169 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001170 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001171 ASSERT_TRUE(annot);
1172
1173 // Check that a non-existent key does not exist.
1174 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1175
1176 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001177 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001178 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001179 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1180 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1181 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001182
1183 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001184 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001185 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001186 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001187 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001188 ASSERT_EQ(66u, length_bytes);
1189 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1190 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1191 length_bytes));
1192 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1193 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001194
1195 // Check that the string value of the modified date is correct.
1196 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001197 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1198 nullptr, 0);
1199 ASSERT_EQ(44u, length_bytes);
1200 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001201 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001202 buf.data(), length_bytes));
1203 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001204
1205 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001206 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001207 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1208 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001209 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001210
1211 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001212 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001213 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001214
Lei Zhange4cdac52019-04-30 16:45:57 +00001215#if defined(OS_MACOSX)
Lei Zhang0f6b4342020-02-25 20:00:39 +00001216 static const char kMd5[] = "5e7e185b386ad21ca83b0287268c50fb";
Lei Zhange67bcc72019-04-30 18:55:58 +00001217#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001218 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001219#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001220 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001221#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001222
1223 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001224 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001225 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001226 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001227 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001228 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001229
Lei Zhanga21d5932018-02-05 18:28:38 +00001230 // Check that the string value of the modified date is the newly-set value.
1231 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001232 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001233 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001234 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001235 ASSERT_EQ(44u, length_bytes);
1236 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001237 EXPECT_EQ(44u,
1238 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001239 buf.data(), length_bytes));
1240 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001241 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001242
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001243 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001244 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001245}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001246
rycsmith3e785602019-03-05 21:48:36 +00001247TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001248 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001249 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1250 FPDF_PAGE page = LoadPage(0);
1251 ASSERT_TRUE(page);
1252 {
1253 // First two annotations do not have "MaxLen" attribute.
1254 for (int i = 0; i < 2; i++) {
1255 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1256 ASSERT_TRUE(annot);
1257
1258 // Verify that no "MaxLen" key present.
1259 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1260
1261 float value;
1262 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1263 }
1264
1265 // Annotation in index 2 has "MaxLen" of 10.
1266 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1267 ASSERT_TRUE(annot);
1268
1269 // Verify that "MaxLen" key present.
1270 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1271
1272 float value;
1273 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1274 EXPECT_FLOAT_EQ(10.0f, value);
1275
1276 // Check bad inputs.
1277 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1278 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1279 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1280 // Ask for key that exists but is not a number.
1281 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1282 }
1283
1284 UnloadPage(page);
1285}
1286
Lei Zhangab41f252018-12-23 03:10:50 +00001287TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001288 // Open a file with four annotations and load its first page.
1289 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001290 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001291 ASSERT_TRUE(page);
1292
Lei Zhanga21d5932018-02-05 18:28:38 +00001293 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001294 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1295 static constexpr size_t kExpectNormalAPLength = 73970;
1296
Lei Zhanga21d5932018-02-05 18:28:38 +00001297 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001298 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001299 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001300
Lei Zhanga21d5932018-02-05 18:28:38 +00001301 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001302 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001303 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001304 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001305
Lei Zhanga21d5932018-02-05 18:28:38 +00001306 // Check that the string value of an AP is not returned if the buffer is too
1307 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001308 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1309 // Write in the buffer to verify it's not overwritten.
1310 memcpy(buf.data(), "abcdefgh", 8);
1311 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001312 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001313 buf.data(), normal_length_bytes - 1));
1314 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001315
Lei Zhanga21d5932018-02-05 18:28:38 +00001316 // Check that the string value of an AP is returned through a buffer that is
1317 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001318 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001319 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001320 buf.data(), normal_length_bytes));
1321 EXPECT_EQ(kMd5NormalAP,
1322 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1323 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001324
Lei Zhanga21d5932018-02-05 18:28:38 +00001325 // Check that the string value of an AP is returned through a buffer that is
1326 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001327 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1328 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001329 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001330 buf.data(), normal_length_bytes + 2));
1331 EXPECT_EQ(kMd5NormalAP,
1332 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1333 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001334
Lei Zhanga21d5932018-02-05 18:28:38 +00001335 // Check that getting an AP for a mode that does not have an AP returns an
1336 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001337 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001338 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001339 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001340
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001341 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001342 EXPECT_EQ(2u,
1343 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001344 buf.data(), 1000));
1345 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001346
Lei Zhanga21d5932018-02-05 18:28:38 +00001347 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001348 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001349 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001350 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001351 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001352 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001353 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001354
Lei Zhanga21d5932018-02-05 18:28:38 +00001355 // Set the AP correctly now.
1356 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001357 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001358
Lei Zhanga21d5932018-02-05 18:28:38 +00001359 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001360 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001361 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001362 ASSERT_EQ(24u, rollover_length_bytes);
1363 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001364 EXPECT_EQ(24u,
1365 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001366 buf.data(), rollover_length_bytes));
1367 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001368
Lei Zhanga21d5932018-02-05 18:28:38 +00001369 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001370 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1371 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001372 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001373 buf.data(), normal_length_bytes));
1374 EXPECT_EQ(kMd5NormalAP,
1375 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1376 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001377 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001378
1379 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001380 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001381 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001382
Lei Zhang0b494052019-01-31 21:41:15 +00001383 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001384 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001385 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001386 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001387
Lei Zhanga21d5932018-02-05 18:28:38 +00001388 // Check that the new annotation value is equal to the value we set before
1389 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001390 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001391 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001392 ASSERT_EQ(24u, rollover_length_bytes);
1393 std::vector<FPDF_WCHAR> buf =
1394 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001395 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1396 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001397 buf.data(), rollover_length_bytes));
1398 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001399 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001400
1401 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001402 CloseSavedPage(page);
1403 CloseSavedDocument();
1404}
1405
Lei Zhangab41f252018-12-23 03:10:50 +00001406TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001407 // Open a file with four annotations and load its first page.
1408 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001409 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001410 ASSERT_TRUE(page);
1411
Lei Zhanga21d5932018-02-05 18:28:38 +00001412 {
1413 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001414 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001415 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001416
Lei Zhanga21d5932018-02-05 18:28:38 +00001417 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001418 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001419 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001420 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001421 EXPECT_EQ(73970u,
1422 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1423 nullptr, 0));
1424 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1425 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001426
Lei Zhanga21d5932018-02-05 18:28:38 +00001427 // Check that setting the Down AP to null removes the Down entry but keeps
1428 // Normal intact.
1429 EXPECT_TRUE(
1430 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1431 EXPECT_EQ(73970u,
1432 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1433 nullptr, 0));
1434 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1435 nullptr, 0));
1436 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001437
Lei Zhang75c81712018-02-08 17:22:39 +00001438 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001439}
1440
Lei Zhangab41f252018-12-23 03:10:50 +00001441TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001442 // Open a file with four annotations and load its first page.
1443 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001444 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001445 ASSERT_TRUE(page);
1446
Lei Zhanga21d5932018-02-05 18:28:38 +00001447 {
1448 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001449 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001450 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001451
Lei Zhanga21d5932018-02-05 18:28:38 +00001452 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001453 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001454 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001455 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001456 EXPECT_EQ(73970u,
1457 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1458 nullptr, 0));
1459 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1460 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001461
Lei Zhanga21d5932018-02-05 18:28:38 +00001462 // Check that setting the Normal AP to null removes the whole AP dictionary.
1463 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1464 nullptr));
1465 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1466 nullptr, 0));
1467 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1468 nullptr, 0));
1469 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001470
Lei Zhang75c81712018-02-08 17:22:39 +00001471 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001472}
1473
Lei Zhangab41f252018-12-23 03:10:50 +00001474TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001475 // Open a file with annotations and load its first page.
1476 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001477 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001478 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001479 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001480
Lei Zhanga21d5932018-02-05 18:28:38 +00001481 {
1482 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001483 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001484 ASSERT_TRUE(annot);
1485 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1486 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001487 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001488 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1489 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1490 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001491
Lei Zhanga21d5932018-02-05 18:28:38 +00001492 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001493 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001494 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1495 ASSERT_TRUE(popup);
1496 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1497 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1498 FS_RECTF rect;
1499 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1500 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1501 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001502
Lei Zhanga21d5932018-02-05 18:28:38 +00001503 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1504 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001505 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001506 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1507 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001508
Lei Zhanga21d5932018-02-05 18:28:38 +00001509 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1510 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001511 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001512 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001513 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1514 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001515 }
Jane Liu300bb272017-08-21 14:37:53 -04001516
Jane Liu300bb272017-08-21 14:37:53 -04001517 UnloadPage(page);
1518}
1519
Lei Zhangab41f252018-12-23 03:10:50 +00001520TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001521 // Open file with form text fields.
1522 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001523 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001524 ASSERT_TRUE(page);
1525
Lei Zhanga21d5932018-02-05 18:28:38 +00001526 {
1527 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001528 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001529 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001530
Lei Zhanga21d5932018-02-05 18:28:38 +00001531 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001532 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001533 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001534 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001535 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001536
Lei Zhanga21d5932018-02-05 18:28:38 +00001537 {
1538 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001539 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001540 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001541
Lei Zhanga21d5932018-02-05 18:28:38 +00001542 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001543 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001544 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001545 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1546 }
1547
1548 {
1549 // Retrieve the fourth annotation: user-editable password text field.
1550 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1551 ASSERT_TRUE(annot);
1552
1553 // Check that the flag values are as expected.
1554 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1555 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1556 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001557 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001558
1559 UnloadPage(page);
1560}
1561
Lei Zhangab41f252018-12-23 03:10:50 +00001562TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001563 // Open file with form text fields.
1564 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001565 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001566 ASSERT_TRUE(page);
1567
Lei Zhanga21d5932018-02-05 18:28:38 +00001568 {
1569 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001570 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001571 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001572
Lei Zhanga21d5932018-02-05 18:28:38 +00001573 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001574 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001575 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1576 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1577 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1578 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001579
Lei Zhanga21d5932018-02-05 18:28:38 +00001580 {
1581 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001582 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001583 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001584
Lei Zhanga21d5932018-02-05 18:28:38 +00001585 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001586 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001587 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1588 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1589 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1590 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001591
Lei Zhanga21d5932018-02-05 18:28:38 +00001592 {
1593 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001594 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001595 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001596
Lei Zhanga21d5932018-02-05 18:28:38 +00001597 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001598 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001599 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1600 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1601 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1602 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001603
1604 UnloadPage(page);
1605}
Diana Gage40870db2017-07-19 18:16:03 -07001606
Lei Zhangab41f252018-12-23 03:10:50 +00001607TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001608 // Open file with form text fields.
1609 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1610 FPDF_PAGE page = LoadPage(0);
1611 ASSERT_TRUE(page);
1612
1613 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001614 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1615 EXPECT_FALSE(
1616 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001617
Lei Zhang8da98232019-12-11 23:29:33 +00001618 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001619 {
1620 // Verify there is an annotation.
1621 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001622 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001623 EXPECT_TRUE(annot);
1624 }
1625
1626 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001627 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1628 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1629 EXPECT_FALSE(
1630 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001631
1632 UnloadPage(page);
1633}
1634
Lei Zhangab41f252018-12-23 03:10:50 +00001635TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001636 // Open file with form text fields.
1637 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1638 FPDF_PAGE page = LoadPage(0);
1639 ASSERT_TRUE(page);
1640
Lei Zhanga21d5932018-02-05 18:28:38 +00001641 {
1642 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001643 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001644 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001645 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001646 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001647
Lei Zhanga21d5932018-02-05 18:28:38 +00001648 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001649 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001650 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1651 }
Diana Gage40870db2017-07-19 18:16:03 -07001652
Lei Zhanga21d5932018-02-05 18:28:38 +00001653 {
1654 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001655 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001656 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001657 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001658 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001659
Lei Zhanga21d5932018-02-05 18:28:38 +00001660 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001661 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001662 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1663 }
Diana Gage40870db2017-07-19 18:16:03 -07001664
1665 UnloadPage(page);
1666}
1667
Lei Zhangab41f252018-12-23 03:10:50 +00001668TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001669 // Open file with form comboboxes.
1670 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1671 FPDF_PAGE page = LoadPage(0);
1672 ASSERT_TRUE(page);
1673
Lei Zhanga21d5932018-02-05 18:28:38 +00001674 {
1675 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001676 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001677 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001678 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001679 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001680
Lei Zhanga21d5932018-02-05 18:28:38 +00001681 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001682 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001683 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1684 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1685 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1686 }
Diana Gage40870db2017-07-19 18:16:03 -07001687
Lei Zhanga21d5932018-02-05 18:28:38 +00001688 {
1689 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001690 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001691 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001692 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001693 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001694
Lei Zhanga21d5932018-02-05 18:28:38 +00001695 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001696 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001697 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1698 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1699 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1700 }
Diana Gage40870db2017-07-19 18:16:03 -07001701
Lei Zhanga21d5932018-02-05 18:28:38 +00001702 {
1703 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001704 static const FS_POINTF kPoint = {102.0f, 513.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001705 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001706 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001707 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001708
Lei Zhanga21d5932018-02-05 18:28:38 +00001709 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001710 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001711 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1712 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1713 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1714 }
Diana Gage40870db2017-07-19 18:16:03 -07001715
1716 UnloadPage(page);
1717}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001718
Lei Zhang03e5e682019-09-16 19:45:55 +00001719// TODO(crbug.com/pdfium/11): Fix this test and enable.
1720#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1721#define MAYBE_BUG_1206 DISABLED_BUG_1206
1722#else
1723#define MAYBE_BUG_1206 BUG_1206
1724#endif
1725TEST_F(FPDFAnnotEmbedderTest, MAYBE_BUG_1206) {
Lei Zhang992e7e22019-02-04 19:20:58 +00001726 static constexpr size_t kExpectedSize = 1609;
1727 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1728
1729 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1730
1731 FPDF_PAGE page = LoadPage(0);
1732 ASSERT_TRUE(page);
1733
1734 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1735 EXPECT_EQ(kExpectedSize, GetString().size());
1736 ClearString();
1737
1738 for (size_t i = 0; i < 10; ++i) {
1739 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1740 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
1741
1742 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1743 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
1744 // equal, not bigger.
1745 EXPECT_LT(kExpectedSize, GetString().size());
1746 ClearString();
1747 }
1748
1749 UnloadPage(page);
1750}
1751
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001752TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1753 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1754 FPDF_PAGE page = LoadPage(0);
1755 ASSERT_TRUE(page);
1756 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1757
1758 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00001759 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001760 static const size_t kBufSize = 12;
1761 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001762
1763 {
1764 // Add a text annotation to the page.
1765 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1766 ASSERT_TRUE(annot);
1767 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1768 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1769
1770 // Make sure there is no test key, add set a value there, and read it back.
1771 std::fill(buf.begin(), buf.end(), 'x');
1772 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001773 kBufSize));
1774 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001775
Lei Zhangf0f67682019-04-08 17:03:21 +00001776 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001777 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1778
1779 std::fill(buf.begin(), buf.end(), 'x');
1780 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001781 kBufSize));
1782 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001783 }
1784
Lei Zhang05ec64c2019-01-09 03:00:06 +00001785 {
1786 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1787 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00001788 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00001789 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001790 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1791 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1792 // Also do the same test for its appearance string.
1793 std::fill(buf.begin(), buf.end(), 'x');
1794 ASSERT_EQ(2u,
1795 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001796 buf.data(), kBufSize));
1797 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001798
Lei Zhangf0f67682019-04-08 17:03:21 +00001799 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00001800 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1801 text.get()));
1802
1803 std::fill(buf.begin(), buf.end(), 'x');
1804 ASSERT_EQ(6u,
1805 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001806 buf.data(), kBufSize));
1807 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001808 }
1809
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001810 UnloadPage(page);
1811
1812 {
1813 // Save a copy, open the copy, and check the annotation again.
1814 // Note that it renders the rotation.
1815 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001816 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001817 FPDF_PAGE saved_page = LoadSavedPage(0);
1818 ASSERT_TRUE(saved_page);
1819
Lei Zhang05ec64c2019-01-09 03:00:06 +00001820 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001821 {
1822 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1823 ASSERT_TRUE(annot);
1824 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1825
1826 std::fill(buf.begin(), buf.end(), 'x');
1827 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001828 kBufSize));
1829 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001830 }
1831
Lei Zhang05ec64c2019-01-09 03:00:06 +00001832 {
1833 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1834 ASSERT_TRUE(annot);
1835 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1836 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1837
1838 std::fill(buf.begin(), buf.end(), 'x');
1839 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001840 kBufSize));
1841 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001842 }
1843
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001844 CloseSavedPage(saved_page);
1845 CloseSavedDocument();
1846 }
1847}
rycsmithcb752f32019-02-21 18:40:53 +00001848
1849TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
1850 // Open a file with combobox widget annotations and load its first page.
1851 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1852 FPDF_PAGE page = LoadPage(0);
1853 ASSERT_TRUE(page);
1854
1855 {
1856 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1857 ASSERT_TRUE(annot);
1858
1859 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1860
1861 annot.reset(FPDFPage_GetAnnot(page, 1));
1862 ASSERT_TRUE(annot);
1863
1864 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00001865
1866 // Check bad form handle / annot.
1867 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
1868 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
1869 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00001870 }
1871
1872 UnloadPage(page);
1873}
1874
1875TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
1876 // Open a file with listbox widget annotations and load its first page.
1877 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1878 FPDF_PAGE page = LoadPage(0);
1879 ASSERT_TRUE(page);
1880
1881 {
1882 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1883 ASSERT_TRUE(annot);
1884
1885 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1886
1887 annot.reset(FPDFPage_GetAnnot(page, 1));
1888 ASSERT_TRUE(annot);
1889
1890 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1891 }
1892
1893 UnloadPage(page);
1894}
1895
1896TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
1897 // Open a file with ink annotations and load its first page.
1898 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1899 FPDF_PAGE page = LoadPage(0);
1900 ASSERT_TRUE(page);
1901
1902 {
1903 // annotations do not have "Opt" array and will return -1
1904 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1905 ASSERT_TRUE(annot);
1906
1907 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1908
1909 annot.reset(FPDFPage_GetAnnot(page, 1));
1910 ASSERT_TRUE(annot);
1911
1912 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1913 }
1914
1915 UnloadPage(page);
1916}
1917
1918TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
1919 // Open a file with combobox widget annotations and load its first page.
1920 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1921 FPDF_PAGE page = LoadPage(0);
1922 ASSERT_TRUE(page);
1923
1924 {
1925 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1926 ASSERT_TRUE(annot);
1927
1928 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001929 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001930 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001931 ASSERT_EQ(8u, length_bytes);
1932 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001933 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001934 buf.data(), length_bytes));
1935 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001936
1937 annot.reset(FPDFPage_GetAnnot(page, 1));
1938 ASSERT_TRUE(annot);
1939
1940 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001941 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001942 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001943 ASSERT_EQ(12u, length_bytes);
1944 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001945 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001946 buf.data(), length_bytes));
1947 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001948
1949 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001950 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001951 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001952 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001953 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001954 buf.data(), length_bytes));
1955 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001956
Lei Zhange7033c82019-02-26 19:30:49 +00001957 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00001958 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1959 nullptr, 0));
1960 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1961 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00001962
1963 // Check bad form handle / annot.
1964 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
1965 EXPECT_EQ(0u,
1966 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
1967 EXPECT_EQ(0u,
1968 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00001969 }
1970
1971 UnloadPage(page);
1972}
1973
1974TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
1975 // Open a file with listbox widget annotations and load its first page.
1976 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1977 FPDF_PAGE page = LoadPage(0);
1978 ASSERT_TRUE(page);
1979
1980 {
1981 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1982 ASSERT_TRUE(annot);
1983
1984 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001985 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001986 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001987 ASSERT_EQ(8u, length_bytes);
1988 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001989 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001990 buf.data(), length_bytes));
1991 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001992
1993 annot.reset(FPDFPage_GetAnnot(page, 1));
1994 ASSERT_TRUE(annot);
1995
1996 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001997 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001998 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001999 ASSERT_EQ(12u, length_bytes);
2000 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002001 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002002 buf.data(), length_bytes));
2003 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002004
2005 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002006 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00002007 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002008 ASSERT_EQ(18u, length_bytes);
2009 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00002010 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00002011 buf.data(), length_bytes));
2012 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00002013
2014 // indices out of range
2015 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2016 nullptr, 0));
2017 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2018 nullptr, 0));
2019 }
2020
2021 UnloadPage(page);
2022}
2023
2024TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
2025 // Open a file with ink annotations and load its first page.
2026 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2027 FPDF_PAGE page = LoadPage(0);
2028 ASSERT_TRUE(page);
2029
2030 {
2031 // annotations do not have "Opt" array and will return 0
2032 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2033 ASSERT_TRUE(annot);
2034
2035 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2036 nullptr, 0));
2037
2038 annot.reset(FPDFPage_GetAnnot(page, 1));
2039 ASSERT_TRUE(annot);
2040
2041 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2042 nullptr, 0));
2043 }
2044
2045 UnloadPage(page);
2046}
Ryan Smith09c23b12019-04-25 18:09:06 +00002047
2048TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2049 // Open a file with combobox annotations and load its first page.
2050 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2051 FPDF_PAGE page = LoadPage(0);
2052 ASSERT_TRUE(page);
2053
2054 {
2055 // All 3 widgets have Tf font size 12.
2056 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2057 ASSERT_TRUE(annot);
2058
2059 float value;
2060 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2061 EXPECT_EQ(12.0, value);
2062
2063 annot.reset(FPDFPage_GetAnnot(page, 1));
2064 ASSERT_TRUE(annot);
2065
2066 float value_two;
2067 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2068 EXPECT_EQ(12.0, value_two);
2069
2070 annot.reset(FPDFPage_GetAnnot(page, 2));
2071 ASSERT_TRUE(annot);
2072
2073 float value_three;
2074 ASSERT_TRUE(
2075 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2076 EXPECT_EQ(12.0, value_three);
2077 }
2078
2079 UnloadPage(page);
2080}
2081
2082TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2083 // Open a file with textfield annotations and load its first page.
2084 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2085 FPDF_PAGE page = LoadPage(0);
2086 ASSERT_TRUE(page);
2087
2088 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002089 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002090 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2091 ASSERT_TRUE(annot);
2092
2093 float value;
2094 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2095 EXPECT_EQ(12.0, value);
2096
2097 annot.reset(FPDFPage_GetAnnot(page, 1));
2098 ASSERT_TRUE(annot);
2099
2100 float value_two;
2101 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2102 EXPECT_EQ(12.0, value_two);
2103
2104 annot.reset(FPDFPage_GetAnnot(page, 2));
2105 ASSERT_TRUE(annot);
2106
2107 float value_three;
2108 ASSERT_TRUE(
2109 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2110 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002111
2112 float value_four;
2113 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2114 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002115 }
2116
2117 UnloadPage(page);
2118}
2119
2120TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2121 // Open a file with ink annotations and load its first page.
2122 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2123 FPDF_PAGE page = LoadPage(0);
2124 ASSERT_TRUE(page);
2125
2126 {
2127 // Annotations that do not have variable text and will return -1.
2128 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2129 ASSERT_TRUE(annot);
2130
2131 float value;
2132 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2133
2134 annot.reset(FPDFPage_GetAnnot(page, 1));
2135 ASSERT_TRUE(annot);
2136
2137 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2138 }
2139
2140 UnloadPage(page);
2141}
2142
2143TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2144 // Open a file with combobox annotations and load its first page.
2145 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2146 FPDF_PAGE page = LoadPage(0);
2147 ASSERT_TRUE(page);
2148
2149 {
2150 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2151 ASSERT_TRUE(annot);
2152
2153 // Check bad form handle / annot.
2154 float value;
2155 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2156 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2157 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2158 }
2159
2160 UnloadPage(page);
2161}
2162
2163TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2164 // Open a file with textfield annotations and load its first page.
2165 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2166 FPDF_PAGE page = LoadPage(0);
2167 ASSERT_TRUE(page);
2168
2169 {
2170 // Obtain the first annotation, a text field with negative font size, -12.
2171 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2172 ASSERT_TRUE(annot);
2173
2174 float value;
2175 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2176 EXPECT_EQ(-12.0, value);
2177 }
2178
2179 UnloadPage(page);
2180}
Ryan Smith23fdf892019-07-17 21:51:26 +00002181
2182TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2183 // Open a file with checkbox and radiobuttons widget annotations and load its
2184 // first page.
2185 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2186 FPDF_PAGE page = LoadPage(0);
2187 ASSERT_TRUE(page);
2188
2189 {
2190 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2191 ASSERT_TRUE(annot);
2192 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2193 }
2194
2195 UnloadPage(page);
2196}
2197
2198TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2199 // Open a file with checkbox and radiobutton widget annotations and load its
2200 // first page.
2201 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2202 FPDF_PAGE page = LoadPage(0);
2203 ASSERT_TRUE(page);
2204
2205 {
2206 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2207 ASSERT_TRUE(annot);
2208 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2209 }
2210
2211 UnloadPage(page);
2212}
2213
2214TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2215 // Open a file with checkbox and radiobutton widget annotations and load its
2216 // first page.
2217 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2218 FPDF_PAGE page = LoadPage(0);
2219 ASSERT_TRUE(page);
2220
2221 {
2222 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2223 ASSERT_TRUE(annot);
2224 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2225
2226 annot.reset(FPDFPage_GetAnnot(page, 6));
2227 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2228
2229 annot.reset(FPDFPage_GetAnnot(page, 7));
2230 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2231 }
2232
2233 UnloadPage(page);
2234}
2235
2236TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2237 // Open a file with checkbox and radiobutton widget annotations and load its
2238 // first page.
2239 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2240 FPDF_PAGE page = LoadPage(0);
2241 ASSERT_TRUE(page);
2242
2243 {
2244 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2245 ASSERT_TRUE(annot);
2246 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2247
2248 annot.reset(FPDFPage_GetAnnot(page, 3));
2249 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2250
2251 annot.reset(FPDFPage_GetAnnot(page, 4));
2252 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2253 }
2254
2255 UnloadPage(page);
2256}
2257
2258TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2259 // Open a file with checkbox and radiobuttons widget annotations and load its
2260 // first page.
2261 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2262 FPDF_PAGE page = LoadPage(0);
2263 ASSERT_TRUE(page);
2264
2265 {
2266 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2267 ASSERT_TRUE(annot);
2268 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2269 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2270 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2271 }
2272
2273 UnloadPage(page);
2274}
2275
2276TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2277 // Open a file with text widget annotations and load its first page.
2278 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2279 FPDF_PAGE page = LoadPage(0);
2280 ASSERT_TRUE(page);
2281
2282 {
2283 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2284 ASSERT_TRUE(annot);
2285 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2286 }
2287
2288 UnloadPage(page);
2289}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002290
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002291TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2292 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002293 FPDF_PAGE page = LoadPage(0);
2294 ASSERT_TRUE(page);
2295
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002296 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2297
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002298 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002299 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002300 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002301 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002302 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002303
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002304 constexpr int kExpectedAnnotTypes[] = {-1,
2305 FPDF_FORMFIELD_COMBOBOX,
2306 FPDF_FORMFIELD_LISTBOX,
2307 FPDF_FORMFIELD_TEXTFIELD,
2308 FPDF_FORMFIELD_CHECKBOX,
2309 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002310
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002311 for (size_t i = 0; i < FX_ArraySize(kExpectedAnnotTypes); ++i) {
2312 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002313 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002314 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002315 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2316 }
2317 UnloadPage(page);
2318}
2319
2320TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2321 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2322 FPDF_PAGE page = LoadPage(0);
2323 ASSERT_TRUE(page);
2324
2325 {
2326 EXPECT_EQ(0u,
2327 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2328
2329 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2330 ASSERT_TRUE(annot);
2331
2332 EXPECT_EQ(0u,
2333 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2334
2335 unsigned long length_bytes =
2336 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2337 ASSERT_EQ(2u, length_bytes);
2338 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2339 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2340 buf.data(), length_bytes));
2341 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2342 }
2343 {
2344 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2345 ASSERT_TRUE(annot);
2346
2347 unsigned long length_bytes =
2348 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2349 ASSERT_EQ(18u, length_bytes);
2350 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2351 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2352 buf.data(), length_bytes));
2353 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2354 }
2355 UnloadPage(page);
2356}
2357
2358TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2359 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2360 FPDF_PAGE page = LoadPage(0);
2361 ASSERT_TRUE(page);
2362
2363 {
2364 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2365 ASSERT_TRUE(annot);
2366
2367 unsigned long length_bytes =
2368 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2369 ASSERT_EQ(2u, length_bytes);
2370 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2371 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2372 buf.data(), length_bytes));
2373 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2374 }
2375 {
2376 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2377 ASSERT_TRUE(annot);
2378
2379 unsigned long length_bytes =
2380 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2381 ASSERT_EQ(14u, length_bytes);
2382 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2383 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2384 buf.data(), length_bytes));
2385 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2386 }
2387 UnloadPage(page);
2388}
2389
2390TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2391 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2392 FPDF_PAGE page = LoadPage(0);
2393 ASSERT_TRUE(page);
2394
2395 {
2396 EXPECT_EQ(0u,
2397 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2398
2399 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2400 ASSERT_TRUE(annot);
2401
2402 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2403
2404 unsigned long length_bytes =
2405 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2406 ASSERT_EQ(18u, length_bytes);
2407 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2408 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2409 buf.data(), length_bytes));
2410 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2411 }
2412 UnloadPage(page);
2413}
2414
2415TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2416 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2417 FPDF_PAGE page = LoadPage(0);
2418 ASSERT_TRUE(page);
2419
2420 {
2421 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2422 ASSERT_TRUE(annot);
2423
2424 unsigned long length_bytes =
2425 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2426 ASSERT_EQ(30u, length_bytes);
2427 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2428 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2429 buf.data(), length_bytes));
2430 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2431 }
2432 UnloadPage(page);
2433}
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002434
Lei Zhang9b444002020-04-17 17:35:23 +00002435TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotSubtypes) {
2436 ASSERT_TRUE(OpenDocument("annots.pdf"));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002437 FPDF_PAGE page = LoadPage(0);
2438 ASSERT_TRUE(page);
2439
Lei Zhang9b444002020-04-17 17:35:23 +00002440 // Verify widgets are by default focusable.
2441 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
2442 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002443
Lei Zhang9b444002020-04-17 17:35:23 +00002444 // Expected annot subtypes for page 0 of annots.pdf.
2445 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
2446 FPDF_ANNOT_LINK, FPDF_ANNOT_LINK, FPDF_ANNOT_LINK,
2447 FPDF_ANNOT_LINK, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_HIGHLIGHT,
2448 FPDF_ANNOT_POPUP, FPDF_ANNOT_HIGHLIGHT, FPDF_ANNOT_WIDGET,
2449 };
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002450
Lei Zhang9b444002020-04-17 17:35:23 +00002451 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
Ankit Kumar69cab672020-04-20 19:50:41 +00002452 FPDF_ANNOT_WIDGET};
Lei Zhang9b444002020-04-17 17:35:23 +00002453 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2454 kExpectedAnnotSubtypes,
2455 kExpectedDefaultFocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002456
Lei Zhang9b444002020-04-17 17:35:23 +00002457 // Make no annotation type focusable using the preferred method.
2458 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
2459 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002460
Lei Zhang9b444002020-04-17 17:35:23 +00002461 // Restore the focusable type count back to 1, then set it back to 0 using a
2462 // different method.
2463 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
2464 ASSERT_TRUE(
2465 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
2466 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002467
Lei Zhang9b444002020-04-17 17:35:23 +00002468 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
Ankit Kumar906ac572020-04-21 05:58:04 +00002469 kExpectedAnnotSubtypes, {});
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002470
Lei Zhang9b444002020-04-17 17:35:23 +00002471 // Now make links focusable.
2472 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
2473 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002474
Lei Zhang9b444002020-04-17 17:35:23 +00002475 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
Ankit Kumar906ac572020-04-21 05:58:04 +00002476 FPDF_ANNOT_LINK};
Lei Zhang9b444002020-04-17 17:35:23 +00002477 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
2478 kExpectedAnnotSubtypes,
2479 kExpectedLinkocusableSubtypes);
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002480
Lei Zhang9b444002020-04-17 17:35:23 +00002481 // Test invalid parameters.
2482 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
2483 FX_ArraySize(kDefaultSubtypes)));
2484 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
2485 FX_ArraySize(kDefaultSubtypes)));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002486 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
2487
Lei Zhang9b444002020-04-17 17:35:23 +00002488 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
2489 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
2490 subtypes.size()));
2491 EXPECT_FALSE(
2492 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
2493 EXPECT_FALSE(
2494 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
Neha Gupta1eb6ddd2020-03-19 08:37:15 +00002495
2496 UnloadPage(page);
2497}
Lei Zhang671aece2020-04-14 19:02:26 +00002498
2499TEST_F(FPDFAnnotEmbedderTest, FocusableAnnotRendering) {
2500 ASSERT_TRUE(OpenDocument("annots.pdf"));
2501 FPDF_PAGE page = LoadPage(0);
2502 ASSERT_TRUE(page);
2503
2504 {
2505#if defined(OS_WIN)
2506 static const char kMd5sum[] = "3877bec7cb3e3144eaa6d10f38bf7a30";
2507#elif defined(OS_MACOSX)
2508 static const char kMd5sum[] = "04b16db5026b5490a50fb6ff0954c867";
2509#else
2510 static const char kMd5sum[] = "40a7354d1f653127bcdac10e15f81654";
2511#endif
2512 // Check the initial rendering.
2513 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2514 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2515 }
2516
2517 // Make links and highlights focusable.
2518 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
2519 FPDF_ANNOT_HIGHLIGHT};
2520 constexpr int kSubTypesCount = FX_ArraySize(kSubTypes);
2521 ASSERT_TRUE(
2522 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
2523 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
2524 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
2525 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
2526 subtypes.size()));
2527 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
2528 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
2529
2530 {
2531#if defined(OS_WIN)
2532 static const char kMd5sum[] = "a30f1bd1cac022d08ceb100df4940b5f";
2533#elif defined(OS_MACOSX)
2534 static const char kMd5sum[] = "3f984a164f2f6d6e3d69f27fd430e346";
2535#else
2536 static const char kMd5sum[] = "e4c4de73addabf10672c308870e8a4ee";
2537#endif
2538 // Focus the first link and check the rendering.
2539 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2540 ASSERT_TRUE(annot);
2541 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
2542 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2543 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2544 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2545 }
2546
2547 {
2548#if defined(OS_WIN)
2549 static const char kMd5sum[] = "467f5a4db98fcadd5121807ff4e2eb10";
2550#elif defined(OS_MACOSX)
2551 static const char kMd5sum[] = "c6d6f9dc7090e8eaf3867ba714023b1e";
2552#else
2553 static const char kMd5sum[] = "65e831885e16b7ecc977cce2e4a27110";
2554#endif
2555 // Focus the first highlight and check the rendering.
2556 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
2557 ASSERT_TRUE(annot);
2558 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
2559 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
2560 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2561 CompareBitmap(bitmap.get(), 612, 792, kMd5sum);
2562 }
2563
2564 UnloadPage(page);
2565}