blob: e69ee160eb001fc0bc9bc83a08cb16691aacf754 [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"
Jane Liu4fd9a472017-06-01 18:56:09 -040025
Lei Zhangab41f252018-12-23 03:10:50 +000026class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040027
Lei Zhangab41f252018-12-23 03:10:50 +000028TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +000029 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
30 FPDF_PAGE page = LoadPage(0);
31 ASSERT_TRUE(page);
32
33 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
34
35 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
36 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
37 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
38 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
39 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
40
41 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
42
43 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
44
45 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
46 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
47 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
48
49 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
50
Lei Zhang4f556b82019-04-08 16:32:41 +000051 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +000052 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +000053 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
54
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000055 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +000056 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
57 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
58 EXPECT_EQ(0u,
59 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
60
61 UnloadPage(page);
62}
63
Lei Zhang3d9a0972019-03-04 19:34:09 +000064TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
65 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
66 FPDF_PAGE page = LoadPage(0);
67 ASSERT_TRUE(page);
68
69 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +000070 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +000071
72 UnloadPage(page);
73}
74
Lei Zhangab41f252018-12-23 03:10:50 +000075TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -040076 // Open a file with one annotation and load its first page.
77 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000078 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040079 ASSERT_TRUE(page);
80
81 // This annotation has a malformed appearance stream, which does not have its
82 // normal appearance defined, only its rollover appearance. In this case, its
83 // normal appearance should be generated, allowing the highlight annotation to
84 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +000085 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +000086 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040087
88 UnloadPage(page);
89}
90
Lei Zhang03e5e682019-09-16 19:45:55 +000091// TODO(crbug.com/pdfium/11): Fix this test and enable.
92#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
93#define MAYBE_RenderMultilineMarkupAnnotWithoutAP \
94 DISABLED_RenderMultilineMarkupAnnotWithoutAP
95#else
96#define MAYBE_RenderMultilineMarkupAnnotWithoutAP \
97 RenderMultilineMarkupAnnotWithoutAP
98#endif
99TEST_F(FPDFAnnotEmbedderTest, MAYBE_RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang4f556b82019-04-08 16:32:41 +0000100 static const char kMd5[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +0000101 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +0000102 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
103 FPDF_PAGE page = LoadPage(0);
104 ASSERT_TRUE(page);
105
Tom Sepeze08d2b12018-04-25 18:49:32 +0000106 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000107 CompareBitmap(bitmap.get(), 595, 842, kMd5);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000108
109 UnloadPage(page);
110}
111
Lei Zhangab41f252018-12-23 03:10:50 +0000112TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400113 // Open a file with one annotation and load its first page.
114 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000115 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400116 ASSERT_TRUE(page);
117
118 // Check that there is a total of 1 annotation on its first page.
119 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
120
121 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000122 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000123 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000124 ASSERT_TRUE(annot);
125 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400126
Lei Zhanga21d5932018-02-05 18:28:38 +0000127 // Check that the annotation color is yellow.
128 unsigned int R;
129 unsigned int G;
130 unsigned int B;
131 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000132 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000133 &G, &B, &A));
134 EXPECT_EQ(255u, R);
135 EXPECT_EQ(255u, G);
136 EXPECT_EQ(0u, B);
137 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400138
Lei Zhanga21d5932018-02-05 18:28:38 +0000139 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000140 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000141 EXPECT_EQ(FPDF_OBJECT_STRING,
142 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000143 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000144 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000145 ASSERT_EQ(28u, length_bytes);
146 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000147 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000148 length_bytes));
149 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400150
Lei Zhanga21d5932018-02-05 18:28:38 +0000151 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000152 EXPECT_EQ(
153 FPDF_OBJECT_STRING,
154 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000155 length_bytes = FPDFAnnot_GetStringValue(
156 annot.get(), pdfium::annotation::kContents, nullptr, 0);
157 ASSERT_EQ(2690u, length_bytes);
158 buf = GetFPDFWideStringBuffer(length_bytes);
159 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
160 pdfium::annotation::kContents,
161 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000162 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000163 L"This is a note for that highlight annotation. Very long highlight "
164 "annotation. Long long long Long long longLong long longLong long "
165 "longLong long longLong long longLong long longLong long longLong long "
166 "longLong long longLong long longLong long longLong long longLong long "
167 "longLong long longLong long longLong long longLong long longLong long "
168 "longLong long longLong long longLong long longLong long longLong long "
169 "longLong long longLong long longLong long longLong long longLong long "
170 "longLong long longLong long longLong long longLong long longLong long "
171 "longLong long longLong long longLong long longLong long longLong long "
172 "longLong long longLong long longLong long longLong long longLong long "
173 "longLong long longLong long longLong long longLong long longLong long "
174 "longLong long longLong long longLong long longLong long longLong long "
175 "longLong long longLong long longLong long longLong long longLong long "
176 "longLong long longLong long longLong long longLong long longLong long "
177 "longLong long longLong long longLong long longLong long longLong long "
178 "longLong long longLong long longLong long longLong long longLong long "
179 "longLong long longLong long longLong long longLong long longLong long "
180 "longLong long longLong long longLong long longLong long longLong long "
181 "longLong long longLong long longLong long longLong long longLong long "
182 "longLong long long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000183 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400184
Lei Zhanga21d5932018-02-05 18:28:38 +0000185 // Check that the quadpoints are correct.
186 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000187 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000188 EXPECT_EQ(115.802643f, quadpoints.x1);
189 EXPECT_EQ(718.913940f, quadpoints.y1);
190 EXPECT_EQ(157.211182f, quadpoints.x4);
191 EXPECT_EQ(706.264465f, quadpoints.y4);
192 }
Tom Sepez507d0192018-11-07 16:37:51 +0000193 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400194}
195
Lei Zhang03e5e682019-09-16 19:45:55 +0000196// TODO(crbug.com/pdfium/11): Fix this test and enable.
197#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
198#define MAYBE_ExtractInkMultiple DISABLED_ExtractInkMultiple
199#else
200#define MAYBE_ExtractInkMultiple ExtractInkMultiple
201#endif
202TEST_F(FPDFAnnotEmbedderTest, MAYBE_ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400203 // Open a file with three annotations and load its first page.
204 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000205 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400206 ASSERT_TRUE(page);
207
208 // Check that there is a total of 3 annotation on its first page.
209 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
210
Lei Zhanga21d5932018-02-05 18:28:38 +0000211 {
212 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000213 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000214 ASSERT_TRUE(annot);
215 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400216
Lei Zhanga21d5932018-02-05 18:28:38 +0000217 // Check that the annotation color is blue with opacity.
218 unsigned int R;
219 unsigned int G;
220 unsigned int B;
221 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000222 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000223 &G, &B, &A));
224 EXPECT_EQ(0u, R);
225 EXPECT_EQ(0u, G);
226 EXPECT_EQ(255u, B);
227 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400228
Lei Zhanga21d5932018-02-05 18:28:38 +0000229 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000230 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
231 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400232
Lei Zhang4f556b82019-04-08 16:32:41 +0000233 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000234 // Note that upon rendering, the rectangle coordinates will be adjusted.
235 FS_RECTF rect;
236 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
237 EXPECT_EQ(351.820404f, rect.left);
238 EXPECT_EQ(583.830688f, rect.bottom);
239 EXPECT_EQ(475.336090f, rect.right);
240 EXPECT_EQ(681.535034f, rect.top);
241 }
Tom Sepezef43c262018-11-07 16:41:32 +0000242 {
243 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
244 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
245 }
Tom Sepez507d0192018-11-07 16:37:51 +0000246 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400247}
Jane Liu20eafda2017-06-07 10:33:24 -0400248
Lei Zhangab41f252018-12-23 03:10:50 +0000249TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400250 // Open a file with one annotation and load its first page.
251 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000252 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400253 ASSERT_TRUE(page);
254
255 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400256 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400257
258 UnloadPage(page);
259}
260
Lei Zhangab41f252018-12-23 03:10:50 +0000261TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400262 // Open a file with no annotation and load its first page.
263 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000264 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400265 ASSERT_TRUE(page);
266 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
267
Lei Zhanga21d5932018-02-05 18:28:38 +0000268 {
269 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000270 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000271 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400272
Lei Zhanga21d5932018-02-05 18:28:38 +0000273 // Check that there is now 1 annotations on this page.
274 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400275
Lei Zhanga21d5932018-02-05 18:28:38 +0000276 // Check that the subtype of the annotation is correct.
277 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
278 }
Jane Liue10509a2017-06-20 16:47:41 -0400279
Lei Zhanga21d5932018-02-05 18:28:38 +0000280 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000281 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000282 ASSERT_TRUE(annot);
283 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400284
Lei Zhanga21d5932018-02-05 18:28:38 +0000285 // Set the color of the annotation.
286 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
287 102, 153, 204));
288 // Check that the color has been set correctly.
289 unsigned int R;
290 unsigned int G;
291 unsigned int B;
292 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000293 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000294 &G, &B, &A));
295 EXPECT_EQ(51u, R);
296 EXPECT_EQ(102u, G);
297 EXPECT_EQ(153u, B);
298 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400299
Lei Zhanga21d5932018-02-05 18:28:38 +0000300 // Change the color of the annotation.
301 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
302 153, 102, 51));
303 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000304 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000305 &G, &B, &A));
306 EXPECT_EQ(204u, R);
307 EXPECT_EQ(153u, G);
308 EXPECT_EQ(102u, B);
309 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400310
Lei Zhanga21d5932018-02-05 18:28:38 +0000311 // Set the annotation rectangle.
312 FS_RECTF rect;
313 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
314 EXPECT_EQ(0.f, rect.left);
315 EXPECT_EQ(0.f, rect.right);
316 rect.left = 35;
317 rect.bottom = 150;
318 rect.right = 53;
319 rect.top = 165;
320 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
321 // Check that the annotation rectangle has been set correctly.
322 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
323 EXPECT_EQ(35.f, rect.left);
324 EXPECT_EQ(150.f, rect.bottom);
325 EXPECT_EQ(53.f, rect.right);
326 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400327
Lei Zhanga21d5932018-02-05 18:28:38 +0000328 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000329 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000330 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000331 ASSERT_TRUE(FPDFAnnot_SetStringValue(
332 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000333 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000334 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000335 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000336 ASSERT_EQ(74u, length_bytes);
337 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
338 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
339 pdfium::annotation::kContents,
340 buf.data(), length_bytes));
341 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000342 }
Jane Liu20eafda2017-06-07 10:33:24 -0400343 UnloadPage(page);
344}
345
Lei Zhang03e5e682019-09-16 19:45:55 +0000346// TODO(crbug.com/pdfium/11): Fix this test and enable.
347#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
348#define MAYBE_AddAndSaveUnderlineAnnotation \
349 DISABLED_AddAndSaveUnderlineAnnotation
350#else
351#define MAYBE_AddAndSaveUnderlineAnnotation AddAndSaveUnderlineAnnotation
352#endif
353TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400354 // Open a file with one annotation and load its first page.
355 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000356 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400357 ASSERT_TRUE(page);
358
359 // Check that there is a total of one annotation on its first page, and verify
360 // its quadpoints.
361 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400362 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000363 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000364 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000365 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000366 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000367 EXPECT_EQ(115.802643f, quadpoints.x1);
368 EXPECT_EQ(718.913940f, quadpoints.y1);
369 EXPECT_EQ(157.211182f, quadpoints.x4);
370 EXPECT_EQ(706.264465f, quadpoints.y4);
371 }
Jane Liu20eafda2017-06-07 10:33:24 -0400372
373 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000374 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000375 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000376 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
377 ASSERT_TRUE(annot);
378 quadpoints.x1 = 140.802643f;
379 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000380 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000381 }
Jane Liu20eafda2017-06-07 10:33:24 -0400382
383 // Save the document, closing the page and document.
384 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000385 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400386
387 // Open the saved document.
Lei Zhang4f556b82019-04-08 16:32:41 +0000388 static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400389
Lei Zhang0b494052019-01-31 21:41:15 +0000390 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000391 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000392 VerifySavedRendering(page, 612, 792, kMd5);
Jane Liu20eafda2017-06-07 10:33:24 -0400393
394 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000395 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400396
Lei Zhanga21d5932018-02-05 18:28:38 +0000397 {
398 // Check that the second annotation is an underline annotation and verify
399 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000400 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000401 ASSERT_TRUE(new_annot);
402 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
403 FS_QUADPOINTSF new_quadpoints;
404 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000405 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000406 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
407 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
408 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
409 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
410 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400411
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000412 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400413 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400414}
Jane Liu06462752017-06-27 16:41:14 -0400415
Lei Zhangab41f252018-12-23 03:10:50 +0000416TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000417 // Open a file with four annotations and load its first page.
418 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
419 FPDF_PAGE page = LoadPage(0);
420 ASSERT_TRUE(page);
421 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
422
423 // Retrieve the highlight annotation.
424 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
425 ASSERT_TRUE(annot);
426 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
427
428 FS_QUADPOINTSF quadpoints;
429 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
430
431 {
432 // Verify the current one set of quadpoints.
433 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
434
435 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
436 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
437 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
438 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
439 }
440
441 {
442 // Update the quadpoints.
443 FS_QUADPOINTSF new_quadpoints = quadpoints;
444 new_quadpoints.y1 -= 20.f;
445 new_quadpoints.y2 -= 20.f;
446 new_quadpoints.y3 -= 20.f;
447 new_quadpoints.y4 -= 20.f;
448 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
449
450 // Verify added quadpoint set
451 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
452 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
453 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
454 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
455 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
456 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
457 }
458
459 {
460 // Append a new set of quadpoints.
461 FS_QUADPOINTSF new_quadpoints = quadpoints;
462 new_quadpoints.y1 += 20.f;
463 new_quadpoints.y2 += 20.f;
464 new_quadpoints.y3 += 20.f;
465 new_quadpoints.y4 += 20.f;
466 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
467
468 // Verify added quadpoint set
469 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
470 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
471 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
472 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
473 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
474 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
475 }
476
477 {
478 // Setting and getting quadpoints at out-of-bound index should fail
479 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
480 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
481 }
482
483 FPDFPage_CloseAnnot(annot);
484
485 // Retrieve the square annotation
486 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
487
488 {
489 // Check that attempting to set its quadpoints would fail
490 ASSERT_TRUE(squareAnnot);
491 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
492 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
493 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
494 }
495
496 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000497 UnloadPage(page);
498}
499
Lei Zhang03e5e682019-09-16 19:45:55 +0000500// TODO(crbug.com/pdfium/11): Fix this test and enable.
501#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
502#define MAYBE_ModifyRectQuadpointsWithAP DISABLED_ModifyRectQuadpointsWithAP
503#else
504#define MAYBE_ModifyRectQuadpointsWithAP ModifyRectQuadpointsWithAP
505#endif
506TEST_F(FPDFAnnotEmbedderTest, MAYBE_ModifyRectQuadpointsWithAP) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000507#if defined(OS_MACOSX)
Lei Zhang4f556b82019-04-08 16:32:41 +0000508 static const char kMd5Original[] = "63af8432fab95a67cdebb7cd0e514941";
509 static const char kMd5ModifiedHighlight[] =
510 "aec26075011349dec9bace891856b5f2";
511 static const char kMd5ModifiedSquare[] = "057f57a32be95975775e5ec513fdcb56";
Lei Zhange67bcc72019-04-30 18:55:58 +0000512#elif defined(OS_WIN)
Lei Zhang4f556b82019-04-08 16:32:41 +0000513 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
514 static const char kMd5ModifiedHighlight[] =
515 "66f3caef3a7d488a4fa1ad37fc06310e";
516 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400517#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000518 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
519 static const char kMd5ModifiedHighlight[] =
520 "66f3caef3a7d488a4fa1ad37fc06310e";
521 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400522#endif
523
Jane Liu06462752017-06-27 16:41:14 -0400524 // Open a file with four annotations and load its first page.
525 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000526 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400527 ASSERT_TRUE(page);
528 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
529
Jane Liub370e5a2017-08-16 13:24:58 -0400530 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000531 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000532 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000533 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000534 }
Jane Liub370e5a2017-08-16 13:24:58 -0400535
Jane Liu0c6b07d2017-08-15 10:50:22 -0400536 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400537 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000538
539 // Retrieve the highlight annotation which has its AP stream already defined.
540 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000541 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000542 ASSERT_TRUE(annot);
543 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
544
545 // Check that color cannot be set when an AP stream is defined already.
546 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
547 102, 153, 204));
548
549 // Verify its attachment points.
550 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000551 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000552 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
553 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
554 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
555 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
556
557 // Check that updating the attachment points would succeed.
558 quadpoints.x1 -= 50.f;
559 quadpoints.x2 -= 50.f;
560 quadpoints.x3 -= 50.f;
561 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000562 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000563 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000564 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000565 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
566 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
567 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
568 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
569
570 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000571 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000572 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000573 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000574 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000575
576 // Verify its annotation rectangle.
577 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
578 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
579 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
580 EXPECT_NEAR(136.325f, rect.right, 0.001f);
581 EXPECT_NEAR(721.292f, rect.top, 0.001f);
582
583 // Check that updating the rectangle would succeed.
584 rect.left -= 60.f;
585 rect.right -= 60.f;
586 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
587 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
588 EXPECT_EQ(rect.right, new_rect.right);
589 }
Jane Liu06462752017-06-27 16:41:14 -0400590
Jane Liub370e5a2017-08-16 13:24:58 -0400591 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000592 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000593 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000594 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000595 }
Jane Liub370e5a2017-08-16 13:24:58 -0400596
Lei Zhanga21d5932018-02-05 18:28:38 +0000597 {
598 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000599 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000600 ASSERT_TRUE(annot);
601 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400602
Lei Zhanga21d5932018-02-05 18:28:38 +0000603 // Check that updating the rectangle would succeed.
604 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
605 rect.left += 70.f;
606 rect.right += 70.f;
607 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
608 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
609 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400610
Lei Zhanga21d5932018-02-05 18:28:38 +0000611 // Check that updating the rectangle changes the square annotation's
612 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000613 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000614 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000615 }
Jane Liub370e5a2017-08-16 13:24:58 -0400616
Jane Liu06462752017-06-27 16:41:14 -0400617 UnloadPage(page);
618}
Jane Liu8ce58f52017-06-29 13:40:22 -0400619
Lei Zhangab41f252018-12-23 03:10:50 +0000620TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000621 // Open a file with multiline markup annotations.
622 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
623 FPDF_PAGE page = LoadPage(0);
624 ASSERT_TRUE(page);
625 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000626 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000627 ASSERT_TRUE(annot);
628
629 // This is a three line annotation.
630 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
631 }
632 UnloadPage(page);
633
634 // null annotation should return 0
635 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
636}
637
Lei Zhangab41f252018-12-23 03:10:50 +0000638TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400639 // Open a file with 3 annotations on its first page.
640 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000641 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400642 ASSERT_TRUE(page);
643 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
644
Jane Liu0c6b07d2017-08-15 10:50:22 -0400645 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400646
Lei Zhanga21d5932018-02-05 18:28:38 +0000647 // Check that the annotations have the expected rectangle coordinates.
648 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000649 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000650 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
651 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
652 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400653
Lei Zhanga21d5932018-02-05 18:28:38 +0000654 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000655 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000656 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
657 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
658 }
659
660 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000661 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000662 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
663 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
664 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400665
666 // Check that nothing happens when attempting to remove an annotation with an
667 // out-of-bound index.
668 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
669 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
670 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
671
672 // Remove the second annotation.
673 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
674 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
675 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
676
677 // Save the document, closing the page and document.
678 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000679 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400680
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400681 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400682 // Open the saved document.
683 std::string new_file = GetString();
684 FPDF_FILEACCESS file_access;
685 memset(&file_access, 0, sizeof(file_access));
686 file_access.m_FileLen = new_file.size();
687 file_access.m_GetBlock = GetBlockFromString;
688 file_access.m_Param = &new_file;
689 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
690 ASSERT_TRUE(new_doc);
691 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
692 ASSERT_TRUE(new_page);
693
694 // Check that the saved document has 2 annotations on the first page.
695 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
696
Lei Zhanga21d5932018-02-05 18:28:38 +0000697 // Check that the remaining 2 annotations are the original 1st and 3rd ones
698 // by verifying their rectangle coordinates.
699 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000700 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000701 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
702 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
703 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400704
Lei Zhanga21d5932018-02-05 18:28:38 +0000705 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000706 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000707 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
708 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
709 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400710 FPDF_ClosePage(new_page);
711 FPDF_CloseDocument(new_doc);
712}
Jane Liu8ce58f52017-06-29 13:40:22 -0400713
Lei Zhang03e5e682019-09-16 19:45:55 +0000714// TODO(crbug.com/pdfium/11): Fix this test and enable.
715#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
716#define MAYBE_AddAndModifyPath DISABLED_AddAndModifyPath
717#else
718#define MAYBE_AddAndModifyPath AddAndModifyPath
719#endif
720TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyPath) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000721#if defined(OS_MACOSX)
Lei Zhang4f556b82019-04-08 16:32:41 +0000722 static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2";
723 static const char kMd5ModifiedPath[] = "9059723a045e17478753d2f0eb33bc03";
724 static const char kMd5TwoPaths[] = "7eed0cfba780f1d4dd8068f717d3a6bf";
725 static const char kMd5NewAnnot[] = "1de8212d43b7066a6df042095c2aca61";
Lei Zhange67bcc72019-04-30 18:55:58 +0000726#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +0000727 static const char kMd5Original[] = "6aa001a77ec05d0f1b0d1d22e28744d4";
728 static const char kMd5ModifiedPath[] = "a7a8d675a6ddbcbdfecee65a33ba19e1";
729 static const char kMd5TwoPaths[] = "7c0bdd4552329704c47a7cce47edbbd6";
730 static const char kMd5NewAnnot[] = "3c48d492b4f62941fed0fb62f729f31e";
Jane Liubaa7ff42017-06-29 19:18:23 -0400731#else
Lei Zhanga2b70732019-06-25 08:34:22 +0000732 static const char kMd5Original[] = "b42cef463483e668eaf4055a65e4f1f5";
733 static const char kMd5ModifiedPath[] = "6ff77d6d1fec4ea571fabe0c7a19b517";
734 static const char kMd5TwoPaths[] = "ca37ad549e74ac5b359a055708f3e7b6";
735 static const char kMd5NewAnnot[] = "0d7a0e33fbf41ff7fa5d732ab2c5edff";
Jane Liubaa7ff42017-06-29 19:18:23 -0400736#endif
737
738 // Open a file with two annotations and load its first page.
739 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000740 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400741 ASSERT_TRUE(page);
742 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
743
744 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000745 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000746 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000747 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000748 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400749
Lei Zhanga21d5932018-02-05 18:28:38 +0000750 {
751 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000752 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000753 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400754
Lei Zhanga21d5932018-02-05 18:28:38 +0000755 // Check that this annotation has one path object and retrieve it.
756 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000757 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000758 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
759 EXPECT_FALSE(path);
760 path = FPDFAnnot_GetObject(annot.get(), 0);
761 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
762 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400763
Lei Zhanga21d5932018-02-05 18:28:38 +0000764 // Modify the color of the path object.
Lei Zhang3475b482019-05-13 18:30:57 +0000765 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
Lei Zhanga21d5932018-02-05 18:28:38 +0000766 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400767
Lei Zhanga21d5932018-02-05 18:28:38 +0000768 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000769 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000770 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000771 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000772 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400773
Lei Zhanga21d5932018-02-05 18:28:38 +0000774 // Add a second path object to the same annotation.
775 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
776 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
Lei Zhang3475b482019-05-13 18:30:57 +0000777 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
778 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
Lei Zhanga21d5932018-02-05 18:28:38 +0000779 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
780 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
781 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400782
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000783 // The object is in the annontation, not in the page, so the page object
784 // array should not change.
785 ASSERT_EQ(32, FPDFPage_CountObjects(page));
786
Lei Zhanga21d5932018-02-05 18:28:38 +0000787 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000788 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000789 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000790 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000791 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400792
Lei Zhanga21d5932018-02-05 18:28:38 +0000793 // Delete the newly added path object.
794 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
795 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000796 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000797 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400798
799 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000800 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000801 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000802 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000803 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400804
Jane Liubaa7ff42017-06-29 19:18:23 -0400805 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400806
Lei Zhanga21d5932018-02-05 18:28:38 +0000807 {
808 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000809 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000810 ASSERT_TRUE(annot);
811 rect.left = 200.f;
812 rect.bottom = 400.f;
813 rect.right = 500.f;
814 rect.top = 600.f;
815 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400816
Lei Zhanga21d5932018-02-05 18:28:38 +0000817 // Add a new path to the annotation.
818 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
819 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
820 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
821 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
822 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
Lei Zhang3475b482019-05-13 18:30:57 +0000823 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
824 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
Lei Zhanga21d5932018-02-05 18:28:38 +0000825 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
826 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
827 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
828
829 // Check that the annotation's bounding box came from its rectangle.
830 FS_RECTF new_rect;
831 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
832 EXPECT_EQ(rect.left, new_rect.left);
833 EXPECT_EQ(rect.bottom, new_rect.bottom);
834 EXPECT_EQ(rect.right, new_rect.right);
835 EXPECT_EQ(rect.top, new_rect.top);
836 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400837
838 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400839 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000840 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400841
842 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +0000843 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000844 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000845 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400846
Jane Liu36567742017-07-06 11:13:35 -0400847 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000848 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400849
Lei Zhanga21d5932018-02-05 18:28:38 +0000850 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000851 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000852 ASSERT_TRUE(annot);
853 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400854
Lei Zhanga21d5932018-02-05 18:28:38 +0000855 // Check that the new annotation's rectangle is as defined.
856 FS_RECTF new_rect;
857 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
858 EXPECT_EQ(rect.left, new_rect.left);
859 EXPECT_EQ(rect.bottom, new_rect.bottom);
860 EXPECT_EQ(rect.right, new_rect.right);
861 EXPECT_EQ(rect.top, new_rect.top);
862 }
863
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000864 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400865 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400866}
Jane Liub137e752017-07-05 15:04:33 -0400867
Lei Zhangab41f252018-12-23 03:10:50 +0000868TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400869 // Open a file with an annotation and load its first page.
870 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000871 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400872 ASSERT_TRUE(page);
873
874 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000875 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000876 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000877 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
878 }
Jane Liub137e752017-07-05 15:04:33 -0400879
Lei Zhanga21d5932018-02-05 18:28:38 +0000880 {
881 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000882 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000883 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400884
Lei Zhanga21d5932018-02-05 18:28:38 +0000885 // Check that the original flag values are as expected.
886 int flags = FPDFAnnot_GetFlags(annot.get());
887 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
888 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400889
Lei Zhanga21d5932018-02-05 18:28:38 +0000890 // Set the HIDDEN flag.
891 flags |= FPDF_ANNOT_FLAG_HIDDEN;
892 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
893 flags = FPDFAnnot_GetFlags(annot.get());
894 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
895 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400896
Lei Zhanga21d5932018-02-05 18:28:38 +0000897 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000898 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000899 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000900 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
901 }
Jane Liub137e752017-07-05 15:04:33 -0400902
Lei Zhanga21d5932018-02-05 18:28:38 +0000903 // Unset the HIDDEN flag.
904 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
905 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
906 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
907 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
908 flags = FPDFAnnot_GetFlags(annot.get());
909 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
910 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400911
Lei Zhanga21d5932018-02-05 18:28:38 +0000912 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000913 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000914 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000915 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
916 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000917 }
Jane Liub137e752017-07-05 15:04:33 -0400918
Jane Liub137e752017-07-05 15:04:33 -0400919 UnloadPage(page);
920}
Jane Liu36567742017-07-06 11:13:35 -0400921
Lei Zhang03e5e682019-09-16 19:45:55 +0000922// TODO(crbug.com/pdfium/11): Fix this test and enable.
923#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
924#define MAYBE_AddAndModifyImage DISABLED_AddAndModifyImage
925#else
926#define MAYBE_AddAndModifyImage AddAndModifyImage
927#endif
928TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyImage) {
Lei Zhange4cdac52019-04-30 16:45:57 +0000929#if defined(OS_MACOSX)
Lei Zhang4f556b82019-04-08 16:32:41 +0000930 static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2";
931 static const char kMd5NewImage[] = "ff012f5697436dfcaec25b32d1333596";
932 static const char kMd5ModifiedImage[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhange67bcc72019-04-30 18:55:58 +0000933#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +0000934 static const char kMd5Original[] = "6aa001a77ec05d0f1b0d1d22e28744d4";
935 static const char kMd5NewImage[] = "3d77d06a971bcb9fb54db082f1082c8b";
936 static const char kMd5ModifiedImage[] = "dc4f4afc26c345418330d31c065020e1";
Jane Liu36567742017-07-06 11:13:35 -0400937#else
Lei Zhanga2b70732019-06-25 08:34:22 +0000938 static const char kMd5Original[] = "b42cef463483e668eaf4055a65e4f1f5";
939 static const char kMd5NewImage[] = "528e6243dc29d54f36b61e0d3287d935";
940 static const char kMd5ModifiedImage[] = "6d9e59f3e57a1ff82fb258356b7eb731";
Jane Liu36567742017-07-06 11:13:35 -0400941#endif
942
943 // Open a file with two annotations and load its first page.
944 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000945 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400946 ASSERT_TRUE(page);
947 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
948
949 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000950 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000951 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000952 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000953 }
Jane Liu36567742017-07-06 11:13:35 -0400954
Jane Liu36567742017-07-06 11:13:35 -0400955 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000956 FPDF_BITMAP image_bitmap;
957
958 {
959 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000960 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000961 ASSERT_TRUE(annot);
962 FS_RECTF rect;
963 rect.left = 200.f;
964 rect.bottom = 600.f;
965 rect.right = 400.f;
966 rect.top = 800.f;
967 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
968
969 // Add a solid-color translucent image object to the new annotation.
970 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
971 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
972 0xeeeecccc);
973 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
974 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
975 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
976 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
977 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
978 kBitmapSize, 0, 0));
979 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
980 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
981 }
Jane Liu36567742017-07-06 11:13:35 -0400982
983 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000984 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000985 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000986 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000987 }
Jane Liu36567742017-07-06 11:13:35 -0400988
Lei Zhanga21d5932018-02-05 18:28:38 +0000989 {
990 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000991 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000992 ASSERT_TRUE(annot);
993 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
994 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
995 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400996
Lei Zhanga21d5932018-02-05 18:28:38 +0000997 // Modify the image in the new annotation.
998 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
999 0xff000000);
1000 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1001 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1002 }
Jane Liu36567742017-07-06 11:13:35 -04001003
1004 // Save the document, closing the page and document.
1005 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001006 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001007 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -04001008
1009 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +00001010 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -04001011}
1012
Lei Zhang03e5e682019-09-16 19:45:55 +00001013// TODO(crbug.com/pdfium/11): Fix this test and enable.
1014#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1015#define MAYBE_AddAndModifyText DISABLED_AddAndModifyText
1016#else
1017#define MAYBE_AddAndModifyText AddAndModifyText
1018#endif
1019TEST_F(FPDFAnnotEmbedderTest, MAYBE_AddAndModifyText) {
Lei Zhange4cdac52019-04-30 16:45:57 +00001020#if defined(OS_MACOSX)
Lei Zhang4f556b82019-04-08 16:32:41 +00001021 static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2";
1022 static const char kMd5NewText[] = "60031c1b0330cf1e1575f7d46687d429";
1023 static const char kMd5ModifiedText[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhange67bcc72019-04-30 18:55:58 +00001024#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001025 static const char kMd5Original[] = "6aa001a77ec05d0f1b0d1d22e28744d4";
1026 static const char kMd5NewText[] = "204cc01749a70b8afc246a4ca33c7eb6";
1027 static const char kMd5ModifiedText[] = "641261a45e8dfd68c89b80bfd237660d";
Jane Liu36567742017-07-06 11:13:35 -04001028#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001029 static const char kMd5Original[] = "b42cef463483e668eaf4055a65e4f1f5";
1030 static const char kMd5NewText[] = "00197ad6206f763febad5719e5935306";
1031 static const char kMd5ModifiedText[] = "85853bc0aaa5a4e3af04e58b9cbfff23";
Jane Liu36567742017-07-06 11:13:35 -04001032#endif
1033
1034 // Open a file with two annotations and load its first page.
1035 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001036 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -04001037 ASSERT_TRUE(page);
1038 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1039
1040 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001041 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001042 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001043 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001044 }
Jane Liu36567742017-07-06 11:13:35 -04001045
Lei Zhanga21d5932018-02-05 18:28:38 +00001046 {
1047 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001048 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001049 ASSERT_TRUE(annot);
1050 FS_RECTF rect;
1051 rect.left = 200.f;
1052 rect.bottom = 550.f;
1053 rect.right = 450.f;
1054 rect.top = 650.f;
1055 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001056
Lei Zhanga21d5932018-02-05 18:28:38 +00001057 // Add a translucent text object to the new annotation.
1058 FPDF_PAGEOBJECT text_object =
1059 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1060 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001061 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001062 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1063 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
Lei Zhang3475b482019-05-13 18:30:57 +00001064 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
Lei Zhanga21d5932018-02-05 18:28:38 +00001065 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1066 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1067 }
Jane Liu36567742017-07-06 11:13:35 -04001068
1069 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001070 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001071 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001072 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001073 }
Jane Liu36567742017-07-06 11:13:35 -04001074
Lei Zhanga21d5932018-02-05 18:28:38 +00001075 {
1076 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001077 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001078 ASSERT_TRUE(annot);
1079 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1080 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1081 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001082
Lei Zhanga21d5932018-02-05 18:28:38 +00001083 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001084 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001085 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1086 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1087 }
Jane Liu36567742017-07-06 11:13:35 -04001088
1089 // Check that the page renders correctly with the modified text object.
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, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001093 }
Jane Liu36567742017-07-06 11:13:35 -04001094
1095 // Remove the new annotation, and check that the page renders as before.
1096 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001097 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001098 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001099 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001100 }
Jane Liu36567742017-07-06 11:13:35 -04001101
1102 UnloadPage(page);
1103}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001104
Lei Zhang03e5e682019-09-16 19:45:55 +00001105// TODO(crbug.com/pdfium/11): Fix this test and enable.
1106#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1107#define MAYBE_GetSetStringValue DISABLED_GetSetStringValue
1108#else
1109#define MAYBE_GetSetStringValue GetSetStringValue
1110#endif
1111TEST_F(FPDFAnnotEmbedderTest, MAYBE_GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001112 // Open a file with four annotations and load its first page.
1113 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001114 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001115 ASSERT_TRUE(page);
1116
Lei Zhang4f556b82019-04-08 16:32:41 +00001117 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001118
Lei Zhanga21d5932018-02-05 18:28:38 +00001119 {
1120 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001121 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001122 ASSERT_TRUE(annot);
1123
1124 // Check that a non-existent key does not exist.
1125 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1126
1127 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001128 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001129 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001130 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1131 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1132 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001133
1134 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001135 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001136 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001137 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001138 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001139 ASSERT_EQ(66u, length_bytes);
1140 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1141 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1142 length_bytes));
1143 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1144 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001145
1146 // Check that the string value of the modified date is correct.
1147 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001148 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1149 nullptr, 0);
1150 ASSERT_EQ(44u, length_bytes);
1151 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001152 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001153 buf.data(), length_bytes));
1154 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001155
1156 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001157 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001158 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1159 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001160 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001161
1162 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001163 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001164 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001165
Lei Zhange4cdac52019-04-30 16:45:57 +00001166#if defined(OS_MACOSX)
Lei Zhang4f556b82019-04-08 16:32:41 +00001167 static const char kMd5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhange67bcc72019-04-30 18:55:58 +00001168#elif defined(OS_WIN)
Lei Zhanga2b70732019-06-25 08:34:22 +00001169 static const char kMd5[] = "20b612ebd46babcb44c48c903e2c5a48";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001170#else
Lei Zhanga2b70732019-06-25 08:34:22 +00001171 static const char kMd5[] = "1d7bea2042c6fea0558ff2aef05811b5";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001172#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001173
1174 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001175 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001176 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001177 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001178 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001179 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001180
Lei Zhanga21d5932018-02-05 18:28:38 +00001181 // Check that the string value of the modified date is the newly-set value.
1182 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001183 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001184 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001185 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001186 ASSERT_EQ(44u, length_bytes);
1187 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001188 EXPECT_EQ(44u,
1189 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001190 buf.data(), length_bytes));
1191 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001192 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001193
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001194 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001195 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001196}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001197
rycsmith3e785602019-03-05 21:48:36 +00001198TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001199 // Open a file with four text annotations and load its first page.
rycsmith3e785602019-03-05 21:48:36 +00001200 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1201 FPDF_PAGE page = LoadPage(0);
1202 ASSERT_TRUE(page);
1203 {
1204 // First two annotations do not have "MaxLen" attribute.
1205 for (int i = 0; i < 2; i++) {
1206 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1207 ASSERT_TRUE(annot);
1208
1209 // Verify that no "MaxLen" key present.
1210 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1211
1212 float value;
1213 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1214 }
1215
1216 // Annotation in index 2 has "MaxLen" of 10.
1217 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1218 ASSERT_TRUE(annot);
1219
1220 // Verify that "MaxLen" key present.
1221 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1222
1223 float value;
1224 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1225 EXPECT_FLOAT_EQ(10.0f, value);
1226
1227 // Check bad inputs.
1228 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1229 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1230 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1231 // Ask for key that exists but is not a number.
1232 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1233 }
1234
1235 UnloadPage(page);
1236}
1237
Lei Zhangab41f252018-12-23 03:10:50 +00001238TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001239 // Open a file with four annotations and load its first page.
1240 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001241 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001242 ASSERT_TRUE(page);
1243
Lei Zhanga21d5932018-02-05 18:28:38 +00001244 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001245 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1246 static constexpr size_t kExpectNormalAPLength = 73970;
1247
Lei Zhanga21d5932018-02-05 18:28:38 +00001248 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001249 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001250 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001251
Lei Zhanga21d5932018-02-05 18:28:38 +00001252 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001253 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001254 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001255 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001256
Lei Zhanga21d5932018-02-05 18:28:38 +00001257 // Check that the string value of an AP is not returned if the buffer is too
1258 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001259 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1260 // Write in the buffer to verify it's not overwritten.
1261 memcpy(buf.data(), "abcdefgh", 8);
1262 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001263 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001264 buf.data(), normal_length_bytes - 1));
1265 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001266
Lei Zhanga21d5932018-02-05 18:28:38 +00001267 // Check that the string value of an AP is returned through a buffer that is
1268 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001269 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001270 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001271 buf.data(), normal_length_bytes));
1272 EXPECT_EQ(kMd5NormalAP,
1273 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1274 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001275
Lei Zhanga21d5932018-02-05 18:28:38 +00001276 // Check that the string value of an AP is returned through a buffer that is
1277 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001278 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1279 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001280 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001281 buf.data(), normal_length_bytes + 2));
1282 EXPECT_EQ(kMd5NormalAP,
1283 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1284 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001285
Lei Zhanga21d5932018-02-05 18:28:38 +00001286 // Check that getting an AP for a mode that does not have an AP returns an
1287 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001288 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001289 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001290 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001291
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001292 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001293 EXPECT_EQ(2u,
1294 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001295 buf.data(), 1000));
1296 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001297
Lei Zhanga21d5932018-02-05 18:28:38 +00001298 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001299 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001300 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001301 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001302 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001303 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001304 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001305
Lei Zhanga21d5932018-02-05 18:28:38 +00001306 // Set the AP correctly now.
1307 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001308 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001309
Lei Zhanga21d5932018-02-05 18:28:38 +00001310 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001311 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001312 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001313 ASSERT_EQ(24u, rollover_length_bytes);
1314 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001315 EXPECT_EQ(24u,
1316 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001317 buf.data(), rollover_length_bytes));
1318 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001319
Lei Zhanga21d5932018-02-05 18:28:38 +00001320 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001321 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1322 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001323 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001324 buf.data(), normal_length_bytes));
1325 EXPECT_EQ(kMd5NormalAP,
1326 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1327 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001328 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001329
1330 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001331 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001332 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001333
Lei Zhang0b494052019-01-31 21:41:15 +00001334 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001335 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001336 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001337 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001338
Lei Zhanga21d5932018-02-05 18:28:38 +00001339 // Check that the new annotation value is equal to the value we set before
1340 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001341 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001342 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001343 ASSERT_EQ(24u, rollover_length_bytes);
1344 std::vector<FPDF_WCHAR> buf =
1345 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001346 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1347 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001348 buf.data(), rollover_length_bytes));
1349 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001350 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001351
1352 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001353 CloseSavedPage(page);
1354 CloseSavedDocument();
1355}
1356
Lei Zhangab41f252018-12-23 03:10:50 +00001357TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001358 // Open a file with four annotations and load its first page.
1359 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001360 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001361 ASSERT_TRUE(page);
1362
Lei Zhanga21d5932018-02-05 18:28:38 +00001363 {
1364 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001365 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001366 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001367
Lei Zhanga21d5932018-02-05 18:28:38 +00001368 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001369 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001370 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001371 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001372 EXPECT_EQ(73970u,
1373 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1374 nullptr, 0));
1375 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1376 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001377
Lei Zhanga21d5932018-02-05 18:28:38 +00001378 // Check that setting the Down AP to null removes the Down entry but keeps
1379 // Normal intact.
1380 EXPECT_TRUE(
1381 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1382 EXPECT_EQ(73970u,
1383 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1384 nullptr, 0));
1385 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1386 nullptr, 0));
1387 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001388
Lei Zhang75c81712018-02-08 17:22:39 +00001389 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001390}
1391
Lei Zhangab41f252018-12-23 03:10:50 +00001392TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001393 // Open a file with four annotations and load its first page.
1394 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001395 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001396 ASSERT_TRUE(page);
1397
Lei Zhanga21d5932018-02-05 18:28:38 +00001398 {
1399 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001400 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001401 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001402
Lei Zhanga21d5932018-02-05 18:28:38 +00001403 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001404 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001405 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001406 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001407 EXPECT_EQ(73970u,
1408 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1409 nullptr, 0));
1410 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1411 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001412
Lei Zhanga21d5932018-02-05 18:28:38 +00001413 // Check that setting the Normal AP to null removes the whole AP dictionary.
1414 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1415 nullptr));
1416 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1417 nullptr, 0));
1418 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1419 nullptr, 0));
1420 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001421
Lei Zhang75c81712018-02-08 17:22:39 +00001422 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001423}
1424
Lei Zhangab41f252018-12-23 03:10:50 +00001425TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001426 // Open a file with annotations and load its first page.
1427 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001428 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001429 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001430 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001431
Lei Zhanga21d5932018-02-05 18:28:38 +00001432 {
1433 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001434 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001435 ASSERT_TRUE(annot);
1436 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1437 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001438 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001439 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1440 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1441 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001442
Lei Zhanga21d5932018-02-05 18:28:38 +00001443 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001444 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001445 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1446 ASSERT_TRUE(popup);
1447 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1448 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1449 FS_RECTF rect;
1450 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1451 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1452 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001453
Lei Zhanga21d5932018-02-05 18:28:38 +00001454 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1455 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001456 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001457 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1458 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001459
Lei Zhanga21d5932018-02-05 18:28:38 +00001460 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1461 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001462 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001463 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001464 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1465 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001466 }
Jane Liu300bb272017-08-21 14:37:53 -04001467
Jane Liu300bb272017-08-21 14:37:53 -04001468 UnloadPage(page);
1469}
1470
Lei Zhangab41f252018-12-23 03:10:50 +00001471TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001472 // Open file with form text fields.
1473 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001474 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001475 ASSERT_TRUE(page);
1476
Lei Zhanga21d5932018-02-05 18:28:38 +00001477 {
1478 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001479 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001480 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001481
Lei Zhanga21d5932018-02-05 18:28:38 +00001482 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001483 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001484 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001485 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001486 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001487
Lei Zhanga21d5932018-02-05 18:28:38 +00001488 {
1489 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001490 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001491 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001492
Lei Zhanga21d5932018-02-05 18:28:38 +00001493 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001494 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001495 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00001496 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1497 }
1498
1499 {
1500 // Retrieve the fourth annotation: user-editable password text field.
1501 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
1502 ASSERT_TRUE(annot);
1503
1504 // Check that the flag values are as expected.
1505 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1506 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1507 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
Lei Zhanga21d5932018-02-05 18:28:38 +00001508 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001509
1510 UnloadPage(page);
1511}
1512
Lei Zhangab41f252018-12-23 03:10:50 +00001513TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001514 // Open file with form text fields.
1515 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001516 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001517 ASSERT_TRUE(page);
1518
Lei Zhanga21d5932018-02-05 18:28:38 +00001519 {
1520 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001521 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001522 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001523
Lei Zhanga21d5932018-02-05 18:28:38 +00001524 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001525 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001526 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1527 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1528 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1529 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001530
Lei Zhanga21d5932018-02-05 18:28:38 +00001531 {
1532 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001533 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001534 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001535
Lei Zhanga21d5932018-02-05 18:28:38 +00001536 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001537 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001538 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1539 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1540 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1541 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001542
Lei Zhanga21d5932018-02-05 18:28:38 +00001543 {
1544 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001545 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001546 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001547
Lei Zhanga21d5932018-02-05 18:28:38 +00001548 // Check that the flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001549 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001550 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1551 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1552 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1553 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001554
1555 UnloadPage(page);
1556}
Diana Gage40870db2017-07-19 18:16:03 -07001557
Lei Zhangab41f252018-12-23 03:10:50 +00001558TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001559 // Open file with form text fields.
1560 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1561 FPDF_PAGE page = LoadPage(0);
1562 ASSERT_TRUE(page);
1563
1564 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang8da98232019-12-11 23:29:33 +00001565 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
1566 EXPECT_FALSE(
1567 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001568
Lei Zhang8da98232019-12-11 23:29:33 +00001569 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
Lei Zhang7557e7b2018-09-14 17:02:40 +00001570 {
1571 // Verify there is an annotation.
1572 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001573 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
Lei Zhang7557e7b2018-09-14 17:02:40 +00001574 EXPECT_TRUE(annot);
1575 }
1576
1577 // Try other bad inputs at a valid location.
Lei Zhang8da98232019-12-11 23:29:33 +00001578 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
1579 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
1580 EXPECT_FALSE(
1581 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
Diana Gage40870db2017-07-19 18:16:03 -07001582
1583 UnloadPage(page);
1584}
1585
Lei Zhangab41f252018-12-23 03:10:50 +00001586TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001587 // Open file with form text fields.
1588 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1589 FPDF_PAGE page = LoadPage(0);
1590 ASSERT_TRUE(page);
1591
Lei Zhanga21d5932018-02-05 18:28:38 +00001592 {
1593 // Retrieve user-editable text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001594 static const FS_POINTF kPoint = {105.0f, 118.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001595 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001596 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001597 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001598
Lei Zhanga21d5932018-02-05 18:28:38 +00001599 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001600 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001601 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1602 }
Diana Gage40870db2017-07-19 18:16:03 -07001603
Lei Zhanga21d5932018-02-05 18:28:38 +00001604 {
1605 // Retrieve read-only text field annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001606 static const FS_POINTF kPoint = {105.0f, 202.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001607 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001608 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001609 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001610
Lei Zhanga21d5932018-02-05 18:28:38 +00001611 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001612 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001613 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1614 }
Diana Gage40870db2017-07-19 18:16:03 -07001615
1616 UnloadPage(page);
1617}
1618
Lei Zhangab41f252018-12-23 03:10:50 +00001619TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001620 // Open file with form comboboxes.
1621 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1622 FPDF_PAGE page = LoadPage(0);
1623 ASSERT_TRUE(page);
1624
Lei Zhanga21d5932018-02-05 18:28:38 +00001625 {
1626 // Retrieve user-editable combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001627 static const FS_POINTF kPoint = {102.0f, 363.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001628 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001629 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001630 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001631
Lei Zhanga21d5932018-02-05 18:28:38 +00001632 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001633 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001634 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1635 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1636 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1637 }
Diana Gage40870db2017-07-19 18:16:03 -07001638
Lei Zhanga21d5932018-02-05 18:28:38 +00001639 {
1640 // Retrieve regular combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001641 static const FS_POINTF kPoint = {102.0f, 413.0f};
Tom Sepeze08d2b12018-04-25 18:49:32 +00001642 ScopedFPDFAnnotation annot(
Lei Zhang8da98232019-12-11 23:29:33 +00001643 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
Lei Zhanga21d5932018-02-05 18:28:38 +00001644 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001645
Lei Zhanga21d5932018-02-05 18:28:38 +00001646 // Check that interactive form annotation flag values are as expected.
Lei Zhanga9d33bd2019-07-31 05:37:31 +00001647 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001648 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1649 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1650 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1651 }
Diana Gage40870db2017-07-19 18:16:03 -07001652
Lei Zhanga21d5932018-02-05 18:28:38 +00001653 {
1654 // Retrieve read-only combobox annotation.
Lei Zhang8da98232019-12-11 23:29:33 +00001655 static const FS_POINTF kPoint = {102.0f, 513.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 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1664 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1665 }
Diana Gage40870db2017-07-19 18:16:03 -07001666
1667 UnloadPage(page);
1668}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001669
Lei Zhang03e5e682019-09-16 19:45:55 +00001670// TODO(crbug.com/pdfium/11): Fix this test and enable.
1671#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
1672#define MAYBE_BUG_1206 DISABLED_BUG_1206
1673#else
1674#define MAYBE_BUG_1206 BUG_1206
1675#endif
1676TEST_F(FPDFAnnotEmbedderTest, MAYBE_BUG_1206) {
Lei Zhang992e7e22019-02-04 19:20:58 +00001677 static constexpr size_t kExpectedSize = 1609;
1678 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1679
1680 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1681
1682 FPDF_PAGE page = LoadPage(0);
1683 ASSERT_TRUE(page);
1684
1685 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1686 EXPECT_EQ(kExpectedSize, GetString().size());
1687 ClearString();
1688
1689 for (size_t i = 0; i < 10; ++i) {
1690 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1691 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
1692
1693 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1694 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
1695 // equal, not bigger.
1696 EXPECT_LT(kExpectedSize, GetString().size());
1697 ClearString();
1698 }
1699
1700 UnloadPage(page);
1701}
1702
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001703TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1704 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1705 FPDF_PAGE page = LoadPage(0);
1706 ASSERT_TRUE(page);
1707 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1708
1709 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00001710 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001711 static const size_t kBufSize = 12;
1712 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001713
1714 {
1715 // Add a text annotation to the page.
1716 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1717 ASSERT_TRUE(annot);
1718 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1719 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1720
1721 // Make sure there is no test key, add set a value there, and read it back.
1722 std::fill(buf.begin(), buf.end(), 'x');
1723 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001724 kBufSize));
1725 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001726
Lei Zhangf0f67682019-04-08 17:03:21 +00001727 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001728 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1729
1730 std::fill(buf.begin(), buf.end(), 'x');
1731 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001732 kBufSize));
1733 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001734 }
1735
Lei Zhang05ec64c2019-01-09 03:00:06 +00001736 {
1737 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1738 ASSERT_TRUE(annot);
Shikha Walia87ad4172019-11-08 20:55:19 +00001739 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
Shikha Waliab54d7ad2019-11-06 02:06:33 +00001740 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001741 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1742 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1743 // Also do the same test for its appearance string.
1744 std::fill(buf.begin(), buf.end(), 'x');
1745 ASSERT_EQ(2u,
1746 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001747 buf.data(), kBufSize));
1748 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001749
Lei Zhangf0f67682019-04-08 17:03:21 +00001750 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00001751 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1752 text.get()));
1753
1754 std::fill(buf.begin(), buf.end(), 'x');
1755 ASSERT_EQ(6u,
1756 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001757 buf.data(), kBufSize));
1758 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001759 }
1760
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001761 UnloadPage(page);
1762
1763 {
1764 // Save a copy, open the copy, and check the annotation again.
1765 // Note that it renders the rotation.
1766 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001767 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001768 FPDF_PAGE saved_page = LoadSavedPage(0);
1769 ASSERT_TRUE(saved_page);
1770
Lei Zhang05ec64c2019-01-09 03:00:06 +00001771 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001772 {
1773 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1774 ASSERT_TRUE(annot);
1775 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1776
1777 std::fill(buf.begin(), buf.end(), 'x');
1778 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001779 kBufSize));
1780 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001781 }
1782
Lei Zhang05ec64c2019-01-09 03:00:06 +00001783 {
1784 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1785 ASSERT_TRUE(annot);
1786 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1787 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1788
1789 std::fill(buf.begin(), buf.end(), 'x');
1790 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001791 kBufSize));
1792 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001793 }
1794
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001795 CloseSavedPage(saved_page);
1796 CloseSavedDocument();
1797 }
1798}
rycsmithcb752f32019-02-21 18:40:53 +00001799
1800TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
1801 // Open a file with combobox widget annotations and load its first page.
1802 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1803 FPDF_PAGE page = LoadPage(0);
1804 ASSERT_TRUE(page);
1805
1806 {
1807 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1808 ASSERT_TRUE(annot);
1809
1810 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1811
1812 annot.reset(FPDFPage_GetAnnot(page, 1));
1813 ASSERT_TRUE(annot);
1814
1815 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00001816
1817 // Check bad form handle / annot.
1818 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
1819 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
1820 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00001821 }
1822
1823 UnloadPage(page);
1824}
1825
1826TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
1827 // Open a file with listbox widget annotations and load its first page.
1828 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1829 FPDF_PAGE page = LoadPage(0);
1830 ASSERT_TRUE(page);
1831
1832 {
1833 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1834 ASSERT_TRUE(annot);
1835
1836 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1837
1838 annot.reset(FPDFPage_GetAnnot(page, 1));
1839 ASSERT_TRUE(annot);
1840
1841 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1842 }
1843
1844 UnloadPage(page);
1845}
1846
1847TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
1848 // Open a file with ink annotations and load its first page.
1849 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1850 FPDF_PAGE page = LoadPage(0);
1851 ASSERT_TRUE(page);
1852
1853 {
1854 // annotations do not have "Opt" array and will return -1
1855 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1856 ASSERT_TRUE(annot);
1857
1858 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1859
1860 annot.reset(FPDFPage_GetAnnot(page, 1));
1861 ASSERT_TRUE(annot);
1862
1863 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1864 }
1865
1866 UnloadPage(page);
1867}
1868
1869TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
1870 // Open a file with combobox widget annotations and load its first page.
1871 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1872 FPDF_PAGE page = LoadPage(0);
1873 ASSERT_TRUE(page);
1874
1875 {
1876 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1877 ASSERT_TRUE(annot);
1878
1879 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001880 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001881 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001882 ASSERT_EQ(8u, length_bytes);
1883 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001884 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001885 buf.data(), length_bytes));
1886 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001887
1888 annot.reset(FPDFPage_GetAnnot(page, 1));
1889 ASSERT_TRUE(annot);
1890
1891 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001892 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001893 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001894 ASSERT_EQ(12u, length_bytes);
1895 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001896 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001897 buf.data(), length_bytes));
1898 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001899
1900 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001901 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001902 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001903 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001904 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001905 buf.data(), length_bytes));
1906 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001907
Lei Zhange7033c82019-02-26 19:30:49 +00001908 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00001909 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1910 nullptr, 0));
1911 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1912 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00001913
1914 // Check bad form handle / annot.
1915 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
1916 EXPECT_EQ(0u,
1917 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
1918 EXPECT_EQ(0u,
1919 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00001920 }
1921
1922 UnloadPage(page);
1923}
1924
1925TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
1926 // Open a file with listbox widget annotations and load its first page.
1927 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1928 FPDF_PAGE page = LoadPage(0);
1929 ASSERT_TRUE(page);
1930
1931 {
1932 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1933 ASSERT_TRUE(annot);
1934
1935 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001936 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001937 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001938 ASSERT_EQ(8u, length_bytes);
1939 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001940 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001941 buf.data(), length_bytes));
1942 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001943
1944 annot.reset(FPDFPage_GetAnnot(page, 1));
1945 ASSERT_TRUE(annot);
1946
1947 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001948 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001949 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001950 ASSERT_EQ(12u, length_bytes);
1951 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001952 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001953 buf.data(), length_bytes));
1954 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001955
1956 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001957 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001958 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001959 ASSERT_EQ(18u, length_bytes);
1960 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001961 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001962 buf.data(), length_bytes));
1963 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001964
1965 // indices out of range
1966 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1967 nullptr, 0));
1968 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1969 nullptr, 0));
1970 }
1971
1972 UnloadPage(page);
1973}
1974
1975TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
1976 // Open a file with ink annotations and load its first page.
1977 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1978 FPDF_PAGE page = LoadPage(0);
1979 ASSERT_TRUE(page);
1980
1981 {
1982 // annotations do not have "Opt" array and will return 0
1983 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1984 ASSERT_TRUE(annot);
1985
1986 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1987 nullptr, 0));
1988
1989 annot.reset(FPDFPage_GetAnnot(page, 1));
1990 ASSERT_TRUE(annot);
1991
1992 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1993 nullptr, 0));
1994 }
1995
1996 UnloadPage(page);
1997}
Ryan Smith09c23b12019-04-25 18:09:06 +00001998
1999TEST_F(FPDFAnnotEmbedderTest, GetFontSizeCombobox) {
2000 // Open a file with combobox annotations and load its first page.
2001 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2002 FPDF_PAGE page = LoadPage(0);
2003 ASSERT_TRUE(page);
2004
2005 {
2006 // All 3 widgets have Tf font size 12.
2007 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2008 ASSERT_TRUE(annot);
2009
2010 float value;
2011 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2012 EXPECT_EQ(12.0, value);
2013
2014 annot.reset(FPDFPage_GetAnnot(page, 1));
2015 ASSERT_TRUE(annot);
2016
2017 float value_two;
2018 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2019 EXPECT_EQ(12.0, value_two);
2020
2021 annot.reset(FPDFPage_GetAnnot(page, 2));
2022 ASSERT_TRUE(annot);
2023
2024 float value_three;
2025 ASSERT_TRUE(
2026 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2027 EXPECT_EQ(12.0, value_three);
2028 }
2029
2030 UnloadPage(page);
2031}
2032
2033TEST_F(FPDFAnnotEmbedderTest, GetFontSizeTextField) {
2034 // Open a file with textfield annotations and load its first page.
2035 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2036 FPDF_PAGE page = LoadPage(0);
2037 ASSERT_TRUE(page);
2038
2039 {
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002040 // All 4 widgets have Tf font size 12.
Ryan Smith09c23b12019-04-25 18:09:06 +00002041 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2042 ASSERT_TRUE(annot);
2043
2044 float value;
2045 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2046 EXPECT_EQ(12.0, value);
2047
2048 annot.reset(FPDFPage_GetAnnot(page, 1));
2049 ASSERT_TRUE(annot);
2050
2051 float value_two;
2052 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2053 EXPECT_EQ(12.0, value_two);
2054
2055 annot.reset(FPDFPage_GetAnnot(page, 2));
2056 ASSERT_TRUE(annot);
2057
2058 float value_three;
2059 ASSERT_TRUE(
2060 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2061 EXPECT_EQ(12.0, value_three);
Mansi Awasthi0b5da672020-01-23 18:17:18 +00002062
2063 float value_four;
2064 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2065 EXPECT_EQ(12.0, value_four);
Ryan Smith09c23b12019-04-25 18:09:06 +00002066 }
2067
2068 UnloadPage(page);
2069}
2070
2071TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidAnnotationTypes) {
2072 // Open a file with ink annotations and load its first page.
2073 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2074 FPDF_PAGE page = LoadPage(0);
2075 ASSERT_TRUE(page);
2076
2077 {
2078 // Annotations that do not have variable text and will return -1.
2079 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2080 ASSERT_TRUE(annot);
2081
2082 float value;
2083 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2084
2085 annot.reset(FPDFPage_GetAnnot(page, 1));
2086 ASSERT_TRUE(annot);
2087
2088 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2089 }
2090
2091 UnloadPage(page);
2092}
2093
2094TEST_F(FPDFAnnotEmbedderTest, GetFontSizeInvalidArguments) {
2095 // Open a file with combobox annotations and load its first page.
2096 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2097 FPDF_PAGE page = LoadPage(0);
2098 ASSERT_TRUE(page);
2099
2100 {
2101 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2102 ASSERT_TRUE(annot);
2103
2104 // Check bad form handle / annot.
2105 float value;
2106 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2107 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2108 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2109 }
2110
2111 UnloadPage(page);
2112}
2113
2114TEST_F(FPDFAnnotEmbedderTest, GetFontSizeNegative) {
2115 // Open a file with textfield annotations and load its first page.
2116 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2117 FPDF_PAGE page = LoadPage(0);
2118 ASSERT_TRUE(page);
2119
2120 {
2121 // Obtain the first annotation, a text field with negative font size, -12.
2122 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2123 ASSERT_TRUE(annot);
2124
2125 float value;
2126 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2127 EXPECT_EQ(-12.0, value);
2128 }
2129
2130 UnloadPage(page);
2131}
Ryan Smith23fdf892019-07-17 21:51:26 +00002132
2133TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckbox) {
2134 // Open a file with checkbox and radiobuttons widget annotations and load its
2135 // first page.
2136 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2137 FPDF_PAGE page = LoadPage(0);
2138 ASSERT_TRUE(page);
2139
2140 {
2141 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2142 ASSERT_TRUE(annot);
2143 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2144 }
2145
2146 UnloadPage(page);
2147}
2148
2149TEST_F(FPDFAnnotEmbedderTest, IsCheckedCheckboxReadOnly) {
2150 // Open a file with checkbox and radiobutton widget annotations and load its
2151 // first page.
2152 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2153 FPDF_PAGE page = LoadPage(0);
2154 ASSERT_TRUE(page);
2155
2156 {
2157 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2158 ASSERT_TRUE(annot);
2159 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2160 }
2161
2162 UnloadPage(page);
2163}
2164
2165TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButton) {
2166 // Open a file with checkbox and radiobutton widget annotations and load its
2167 // first page.
2168 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2169 FPDF_PAGE page = LoadPage(0);
2170 ASSERT_TRUE(page);
2171
2172 {
2173 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2174 ASSERT_TRUE(annot);
2175 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2176
2177 annot.reset(FPDFPage_GetAnnot(page, 6));
2178 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2179
2180 annot.reset(FPDFPage_GetAnnot(page, 7));
2181 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2182 }
2183
2184 UnloadPage(page);
2185}
2186
2187TEST_F(FPDFAnnotEmbedderTest, IsCheckedRadioButtonReadOnly) {
2188 // Open a file with checkbox and radiobutton widget annotations and load its
2189 // first page.
2190 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2191 FPDF_PAGE page = LoadPage(0);
2192 ASSERT_TRUE(page);
2193
2194 {
2195 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2196 ASSERT_TRUE(annot);
2197 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2198
2199 annot.reset(FPDFPage_GetAnnot(page, 3));
2200 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2201
2202 annot.reset(FPDFPage_GetAnnot(page, 4));
2203 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2204 }
2205
2206 UnloadPage(page);
2207}
2208
2209TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidArguments) {
2210 // Open a file with checkbox and radiobuttons widget annotations and load its
2211 // first page.
2212 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2213 FPDF_PAGE page = LoadPage(0);
2214 ASSERT_TRUE(page);
2215
2216 {
2217 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2218 ASSERT_TRUE(annot);
2219 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2220 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2221 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2222 }
2223
2224 UnloadPage(page);
2225}
2226
2227TEST_F(FPDFAnnotEmbedderTest, IsCheckedInvalidWidgetType) {
2228 // Open a file with text widget annotations and load its first page.
2229 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2230 FPDF_PAGE page = LoadPage(0);
2231 ASSERT_TRUE(page);
2232
2233 {
2234 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2235 ASSERT_TRUE(annot);
2236 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2237 }
2238
2239 UnloadPage(page);
2240}
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002241
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002242TEST_F(FPDFAnnotEmbedderTest, GetFormFieldType) {
2243 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002244 FPDF_PAGE page = LoadPage(0);
2245 ASSERT_TRUE(page);
2246
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002247 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2248
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002249 {
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002250 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002251 ASSERT_TRUE(annot);
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002252 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002253 }
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002254
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002255 constexpr int kExpectedAnnotTypes[] = {-1,
2256 FPDF_FORMFIELD_COMBOBOX,
2257 FPDF_FORMFIELD_LISTBOX,
2258 FPDF_FORMFIELD_TEXTFIELD,
2259 FPDF_FORMFIELD_CHECKBOX,
2260 FPDF_FORMFIELD_RADIOBUTTON};
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002261
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002262 for (size_t i = 0; i < FX_ArraySize(kExpectedAnnotTypes); ++i) {
2263 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002264 ASSERT_TRUE(annot);
Ankit Kumar8c7e4ad2020-02-07 08:56:49 +00002265 EXPECT_EQ(kExpectedAnnotTypes[i],
Mansi Awasthi07bf7e62020-01-24 10:34:17 +00002266 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2267 }
2268 UnloadPage(page);
2269}
2270
2271TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueTextField) {
2272 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2273 FPDF_PAGE page = LoadPage(0);
2274 ASSERT_TRUE(page);
2275
2276 {
2277 EXPECT_EQ(0u,
2278 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2279
2280 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2281 ASSERT_TRUE(annot);
2282
2283 EXPECT_EQ(0u,
2284 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2285
2286 unsigned long length_bytes =
2287 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2288 ASSERT_EQ(2u, length_bytes);
2289 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2290 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2291 buf.data(), length_bytes));
2292 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2293 }
2294 {
2295 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2296 ASSERT_TRUE(annot);
2297
2298 unsigned long length_bytes =
2299 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2300 ASSERT_EQ(18u, length_bytes);
2301 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2302 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2303 buf.data(), length_bytes));
2304 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
2305 }
2306 UnloadPage(page);
2307}
2308
2309TEST_F(FPDFAnnotEmbedderTest, GetFormFieldValueComboBox) {
2310 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2311 FPDF_PAGE page = LoadPage(0);
2312 ASSERT_TRUE(page);
2313
2314 {
2315 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2316 ASSERT_TRUE(annot);
2317
2318 unsigned long length_bytes =
2319 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2320 ASSERT_EQ(2u, length_bytes);
2321 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2322 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2323 buf.data(), length_bytes));
2324 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2325 }
2326 {
2327 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2328 ASSERT_TRUE(annot);
2329
2330 unsigned long length_bytes =
2331 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2332 ASSERT_EQ(14u, length_bytes);
2333 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2334 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
2335 buf.data(), length_bytes));
2336 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
2337 }
2338 UnloadPage(page);
2339}
2340
2341TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameTextField) {
2342 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2343 FPDF_PAGE page = LoadPage(0);
2344 ASSERT_TRUE(page);
2345
2346 {
2347 EXPECT_EQ(0u,
2348 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
2349
2350 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2351 ASSERT_TRUE(annot);
2352
2353 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
2354
2355 unsigned long length_bytes =
2356 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2357 ASSERT_EQ(18u, length_bytes);
2358 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2359 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2360 buf.data(), length_bytes));
2361 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
2362 }
2363 UnloadPage(page);
2364}
2365
2366TEST_F(FPDFAnnotEmbedderTest, GetFormFieldNameComboBox) {
2367 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2368 FPDF_PAGE page = LoadPage(0);
2369 ASSERT_TRUE(page);
2370
2371 {
2372 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2373 ASSERT_TRUE(annot);
2374
2375 unsigned long length_bytes =
2376 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
2377 ASSERT_EQ(30u, length_bytes);
2378 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2379 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
2380 buf.data(), length_bytes));
2381 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
2382 }
2383 UnloadPage(page);
2384}