blob: 281eaa043a581fc2002eded01e310be6995b5ff4 [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 Zhanga5c1daf2019-01-31 21:56:47 +000011#include "constants/annotation_common.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040012#include "core/fxcrt/fx_system.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000013#include "public/cpp/fpdf_scopers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040014#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040015#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040016#include "public/fpdfview.h"
17#include "testing/embedder_test.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000018#include "testing/fx_string_testhelpers.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000019#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040020#include "testing/gtest/include/gtest/gtest.h"
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000021#include "testing/utils/hash.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040022
Lei Zhangab41f252018-12-23 03:10:50 +000023class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040024
Lei Zhangab41f252018-12-23 03:10:50 +000025TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +000026 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
27 FPDF_PAGE page = LoadPage(0);
28 ASSERT_TRUE(page);
29
30 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
31
32 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
33 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
34 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
35 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
36 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
37
38 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
39
40 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
41
42 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
43 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
44 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
45
46 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
47
Lei Zhang4f556b82019-04-08 16:32:41 +000048 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +000049 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +000050 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
51
Lei Zhang5bf8c7f2019-04-08 17:50:11 +000052 FPDF_WCHAR buffer[64];
Lei Zhang7557e7b2018-09-14 17:02:40 +000053 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
54 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
55 EXPECT_EQ(0u,
56 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
57
58 UnloadPage(page);
59}
60
Lei Zhang3d9a0972019-03-04 19:34:09 +000061TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
62 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
63 FPDF_PAGE page = LoadPage(0);
64 ASSERT_TRUE(page);
65
66 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +000067 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +000068
69 UnloadPage(page);
70}
71
Lei Zhangab41f252018-12-23 03:10:50 +000072TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -040073 // Open a file with one annotation and load its first page.
74 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000075 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040076 ASSERT_TRUE(page);
77
78 // This annotation has a malformed appearance stream, which does not have its
79 // normal appearance defined, only its rollover appearance. In this case, its
80 // normal appearance should be generated, allowing the highlight annotation to
81 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +000082 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +000083 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040084
85 UnloadPage(page);
86}
87
Lei Zhangab41f252018-12-23 03:10:50 +000088TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang4f556b82019-04-08 16:32:41 +000089 static const char kMd5[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000090 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000091 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
92 FPDF_PAGE page = LoadPage(0);
93 ASSERT_TRUE(page);
94
Tom Sepeze08d2b12018-04-25 18:49:32 +000095 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +000096 CompareBitmap(bitmap.get(), 595, 842, kMd5);
Ralf Sipplb3a52402018-03-19 23:30:28 +000097
98 UnloadPage(page);
99}
100
Lei Zhangab41f252018-12-23 03:10:50 +0000101TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400102 // Open a file with one annotation and load its first page.
103 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000104 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400105 ASSERT_TRUE(page);
106
107 // Check that there is a total of 1 annotation on its first page.
108 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
109
110 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000111 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000112 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000113 ASSERT_TRUE(annot);
114 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400115
Lei Zhanga21d5932018-02-05 18:28:38 +0000116 // Check that the annotation color is yellow.
117 unsigned int R;
118 unsigned int G;
119 unsigned int B;
120 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000121 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000122 &G, &B, &A));
123 EXPECT_EQ(255u, R);
124 EXPECT_EQ(255u, G);
125 EXPECT_EQ(0u, B);
126 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400127
Lei Zhanga21d5932018-02-05 18:28:38 +0000128 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000129 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000130 EXPECT_EQ(FPDF_OBJECT_STRING,
131 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000132 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +0000133 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000134 ASSERT_EQ(28u, length_bytes);
135 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +0000136 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000137 length_bytes));
138 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400139
Lei Zhanga21d5932018-02-05 18:28:38 +0000140 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000141 EXPECT_EQ(
142 FPDF_OBJECT_STRING,
143 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000144 length_bytes = FPDFAnnot_GetStringValue(
145 annot.get(), pdfium::annotation::kContents, nullptr, 0);
146 ASSERT_EQ(2690u, length_bytes);
147 buf = GetFPDFWideStringBuffer(length_bytes);
148 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
149 pdfium::annotation::kContents,
150 buf.data(), length_bytes));
Lei Zhang4f556b82019-04-08 16:32:41 +0000151 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000152 L"This is a note for that highlight annotation. Very long highlight "
153 "annotation. Long long long Long long longLong long longLong long "
154 "longLong long longLong long longLong long longLong long longLong long "
155 "longLong long longLong long longLong long longLong long longLong long "
156 "longLong long longLong long longLong long longLong long longLong long "
157 "longLong long longLong long longLong long longLong long longLong long "
158 "longLong long longLong long longLong long longLong long longLong long "
159 "longLong long longLong long longLong long longLong long longLong long "
160 "longLong long longLong long longLong long longLong long longLong long "
161 "longLong long longLong long longLong long longLong long longLong long "
162 "longLong long longLong long longLong long longLong long longLong long "
163 "longLong long longLong long longLong long longLong long longLong long "
164 "longLong long longLong long longLong 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 long. END";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000172 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400173
Lei Zhanga21d5932018-02-05 18:28:38 +0000174 // Check that the quadpoints are correct.
175 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000176 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000177 EXPECT_EQ(115.802643f, quadpoints.x1);
178 EXPECT_EQ(718.913940f, quadpoints.y1);
179 EXPECT_EQ(157.211182f, quadpoints.x4);
180 EXPECT_EQ(706.264465f, quadpoints.y4);
181 }
Tom Sepez507d0192018-11-07 16:37:51 +0000182 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400183}
184
Lei Zhangab41f252018-12-23 03:10:50 +0000185TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400186 // Open a file with three annotations and load its first page.
187 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000188 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400189 ASSERT_TRUE(page);
190
191 // Check that there is a total of 3 annotation on its first page.
192 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
193
Lei Zhanga21d5932018-02-05 18:28:38 +0000194 {
195 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000196 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000197 ASSERT_TRUE(annot);
198 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400199
Lei Zhanga21d5932018-02-05 18:28:38 +0000200 // Check that the annotation color is blue with opacity.
201 unsigned int R;
202 unsigned int G;
203 unsigned int B;
204 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000205 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000206 &G, &B, &A));
207 EXPECT_EQ(0u, R);
208 EXPECT_EQ(0u, G);
209 EXPECT_EQ(255u, B);
210 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400211
Lei Zhanga21d5932018-02-05 18:28:38 +0000212 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000213 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
214 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400215
Lei Zhang4f556b82019-04-08 16:32:41 +0000216 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000217 // Note that upon rendering, the rectangle coordinates will be adjusted.
218 FS_RECTF rect;
219 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
220 EXPECT_EQ(351.820404f, rect.left);
221 EXPECT_EQ(583.830688f, rect.bottom);
222 EXPECT_EQ(475.336090f, rect.right);
223 EXPECT_EQ(681.535034f, rect.top);
224 }
Tom Sepezef43c262018-11-07 16:41:32 +0000225 {
226 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
227 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
228 }
Tom Sepez507d0192018-11-07 16:37:51 +0000229 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400230}
Jane Liu20eafda2017-06-07 10:33:24 -0400231
Lei Zhangab41f252018-12-23 03:10:50 +0000232TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400233 // Open a file with one annotation and load its first page.
234 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000235 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400236 ASSERT_TRUE(page);
237
238 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400239 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400240
241 UnloadPage(page);
242}
243
Lei Zhangab41f252018-12-23 03:10:50 +0000244TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400245 // Open a file with no annotation and load its first page.
246 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000247 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400248 ASSERT_TRUE(page);
249 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
250
Lei Zhanga21d5932018-02-05 18:28:38 +0000251 {
252 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000253 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000254 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400255
Lei Zhanga21d5932018-02-05 18:28:38 +0000256 // Check that there is now 1 annotations on this page.
257 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400258
Lei Zhanga21d5932018-02-05 18:28:38 +0000259 // Check that the subtype of the annotation is correct.
260 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
261 }
Jane Liue10509a2017-06-20 16:47:41 -0400262
Lei Zhanga21d5932018-02-05 18:28:38 +0000263 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000264 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000265 ASSERT_TRUE(annot);
266 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400267
Lei Zhanga21d5932018-02-05 18:28:38 +0000268 // Set the color of the annotation.
269 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
270 102, 153, 204));
271 // Check that the color has been set correctly.
272 unsigned int R;
273 unsigned int G;
274 unsigned int B;
275 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000276 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000277 &G, &B, &A));
278 EXPECT_EQ(51u, R);
279 EXPECT_EQ(102u, G);
280 EXPECT_EQ(153u, B);
281 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400282
Lei Zhanga21d5932018-02-05 18:28:38 +0000283 // Change the color of the annotation.
284 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
285 153, 102, 51));
286 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000287 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000288 &G, &B, &A));
289 EXPECT_EQ(204u, R);
290 EXPECT_EQ(153u, G);
291 EXPECT_EQ(102u, B);
292 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400293
Lei Zhanga21d5932018-02-05 18:28:38 +0000294 // Set the annotation rectangle.
295 FS_RECTF rect;
296 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
297 EXPECT_EQ(0.f, rect.left);
298 EXPECT_EQ(0.f, rect.right);
299 rect.left = 35;
300 rect.bottom = 150;
301 rect.right = 53;
302 rect.top = 165;
303 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
304 // Check that the annotation rectangle has been set correctly.
305 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
306 EXPECT_EQ(35.f, rect.left);
307 EXPECT_EQ(150.f, rect.bottom);
308 EXPECT_EQ(53.f, rect.right);
309 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400310
Lei Zhanga21d5932018-02-05 18:28:38 +0000311 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000312 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000313 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000314 ASSERT_TRUE(FPDFAnnot_SetStringValue(
315 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000316 // Check that the content has been set correctly.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000317 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000318 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +0000319 ASSERT_EQ(74u, length_bytes);
320 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
321 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
322 pdfium::annotation::kContents,
323 buf.data(), length_bytes));
324 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000325 }
Jane Liu20eafda2017-06-07 10:33:24 -0400326 UnloadPage(page);
327}
328
Lei Zhangab41f252018-12-23 03:10:50 +0000329TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400330 // Open a file with one annotation and load its first page.
331 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000332 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400333 ASSERT_TRUE(page);
334
335 // Check that there is a total of one annotation on its first page, and verify
336 // its quadpoints.
337 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400338 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000339 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000340 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000341 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000342 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000343 EXPECT_EQ(115.802643f, quadpoints.x1);
344 EXPECT_EQ(718.913940f, quadpoints.y1);
345 EXPECT_EQ(157.211182f, quadpoints.x4);
346 EXPECT_EQ(706.264465f, quadpoints.y4);
347 }
Jane Liu20eafda2017-06-07 10:33:24 -0400348
349 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000350 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000351 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000352 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
353 ASSERT_TRUE(annot);
354 quadpoints.x1 = 140.802643f;
355 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000356 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000357 }
Jane Liu20eafda2017-06-07 10:33:24 -0400358
359 // Save the document, closing the page and document.
360 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000361 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400362
363 // Open the saved document.
Lei Zhang4f556b82019-04-08 16:32:41 +0000364 static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400365
Lei Zhang0b494052019-01-31 21:41:15 +0000366 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000367 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000368 VerifySavedRendering(page, 612, 792, kMd5);
Jane Liu20eafda2017-06-07 10:33:24 -0400369
370 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000371 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400372
Lei Zhanga21d5932018-02-05 18:28:38 +0000373 {
374 // Check that the second annotation is an underline annotation and verify
375 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000376 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000377 ASSERT_TRUE(new_annot);
378 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
379 FS_QUADPOINTSF new_quadpoints;
380 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000381 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000382 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
383 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
384 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
385 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
386 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400387
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000388 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400389 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400390}
Jane Liu06462752017-06-27 16:41:14 -0400391
Lei Zhangab41f252018-12-23 03:10:50 +0000392TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000393 // Open a file with four annotations and load its first page.
394 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
395 FPDF_PAGE page = LoadPage(0);
396 ASSERT_TRUE(page);
397 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
398
399 // Retrieve the highlight annotation.
400 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
401 ASSERT_TRUE(annot);
402 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
403
404 FS_QUADPOINTSF quadpoints;
405 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
406
407 {
408 // Verify the current one set of quadpoints.
409 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
410
411 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
412 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
413 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
414 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
415 }
416
417 {
418 // Update the quadpoints.
419 FS_QUADPOINTSF new_quadpoints = quadpoints;
420 new_quadpoints.y1 -= 20.f;
421 new_quadpoints.y2 -= 20.f;
422 new_quadpoints.y3 -= 20.f;
423 new_quadpoints.y4 -= 20.f;
424 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
425
426 // Verify added quadpoint set
427 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
428 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
429 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
430 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
431 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
432 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
433 }
434
435 {
436 // Append a new set of quadpoints.
437 FS_QUADPOINTSF new_quadpoints = quadpoints;
438 new_quadpoints.y1 += 20.f;
439 new_quadpoints.y2 += 20.f;
440 new_quadpoints.y3 += 20.f;
441 new_quadpoints.y4 += 20.f;
442 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
443
444 // Verify added quadpoint set
445 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
446 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
447 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
448 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
449 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
450 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
451 }
452
453 {
454 // Setting and getting quadpoints at out-of-bound index should fail
455 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
456 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
457 }
458
459 FPDFPage_CloseAnnot(annot);
460
461 // Retrieve the square annotation
462 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
463
464 {
465 // Check that attempting to set its quadpoints would fail
466 ASSERT_TRUE(squareAnnot);
467 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
468 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
469 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
470 }
471
472 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000473 UnloadPage(page);
474}
475
Lei Zhangab41f252018-12-23 03:10:50 +0000476TEST_F(FPDFAnnotEmbedderTest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400477#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +0000478 static const char kMd5Original[] = "63af8432fab95a67cdebb7cd0e514941";
479 static const char kMd5ModifiedHighlight[] =
480 "aec26075011349dec9bace891856b5f2";
481 static const char kMd5ModifiedSquare[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400482#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +0000483 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
484 static const char kMd5ModifiedHighlight[] =
485 "66f3caef3a7d488a4fa1ad37fc06310e";
486 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400487#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000488 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
489 static const char kMd5ModifiedHighlight[] =
490 "66f3caef3a7d488a4fa1ad37fc06310e";
491 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400492#endif
493
Jane Liu06462752017-06-27 16:41:14 -0400494 // Open a file with four annotations and load its first page.
495 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000496 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400497 ASSERT_TRUE(page);
498 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
499
Jane Liub370e5a2017-08-16 13:24:58 -0400500 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000501 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000502 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000503 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000504 }
Jane Liub370e5a2017-08-16 13:24:58 -0400505
Jane Liu0c6b07d2017-08-15 10:50:22 -0400506 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400507 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000508
509 // Retrieve the highlight annotation which has its AP stream already defined.
510 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000511 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000512 ASSERT_TRUE(annot);
513 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
514
515 // Check that color cannot be set when an AP stream is defined already.
516 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
517 102, 153, 204));
518
519 // Verify its attachment points.
520 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000521 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000522 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
523 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
524 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
525 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
526
527 // Check that updating the attachment points would succeed.
528 quadpoints.x1 -= 50.f;
529 quadpoints.x2 -= 50.f;
530 quadpoints.x3 -= 50.f;
531 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000532 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000533 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000534 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000535 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
536 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
537 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
538 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
539
540 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000541 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000542 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000543 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000544 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000545
546 // Verify its annotation rectangle.
547 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
548 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
549 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
550 EXPECT_NEAR(136.325f, rect.right, 0.001f);
551 EXPECT_NEAR(721.292f, rect.top, 0.001f);
552
553 // Check that updating the rectangle would succeed.
554 rect.left -= 60.f;
555 rect.right -= 60.f;
556 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
557 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
558 EXPECT_EQ(rect.right, new_rect.right);
559 }
Jane Liu06462752017-06-27 16:41:14 -0400560
Jane Liub370e5a2017-08-16 13:24:58 -0400561 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000562 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000563 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000564 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000565 }
Jane Liub370e5a2017-08-16 13:24:58 -0400566
Lei Zhanga21d5932018-02-05 18:28:38 +0000567 {
568 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000569 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000570 ASSERT_TRUE(annot);
571 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400572
Lei Zhanga21d5932018-02-05 18:28:38 +0000573 // Check that updating the rectangle would succeed.
574 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
575 rect.left += 70.f;
576 rect.right += 70.f;
577 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
578 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
579 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400580
Lei Zhanga21d5932018-02-05 18:28:38 +0000581 // Check that updating the rectangle changes the square annotation's
582 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000583 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000584 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000585 }
Jane Liub370e5a2017-08-16 13:24:58 -0400586
Jane Liu06462752017-06-27 16:41:14 -0400587 UnloadPage(page);
588}
Jane Liu8ce58f52017-06-29 13:40:22 -0400589
Lei Zhangab41f252018-12-23 03:10:50 +0000590TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000591 // Open a file with multiline markup annotations.
592 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
593 FPDF_PAGE page = LoadPage(0);
594 ASSERT_TRUE(page);
595 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000596 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000597 ASSERT_TRUE(annot);
598
599 // This is a three line annotation.
600 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
601 }
602 UnloadPage(page);
603
604 // null annotation should return 0
605 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
606}
607
Lei Zhangab41f252018-12-23 03:10:50 +0000608TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400609 // Open a file with 3 annotations on its first page.
610 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000611 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400612 ASSERT_TRUE(page);
613 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
614
Jane Liu0c6b07d2017-08-15 10:50:22 -0400615 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400616
Lei Zhanga21d5932018-02-05 18:28:38 +0000617 // Check that the annotations have the expected rectangle coordinates.
618 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000619 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000620 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
621 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
622 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400623
Lei Zhanga21d5932018-02-05 18:28:38 +0000624 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000625 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000626 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
627 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
628 }
629
630 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000631 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000632 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
633 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
634 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400635
636 // Check that nothing happens when attempting to remove an annotation with an
637 // out-of-bound index.
638 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
639 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
640 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
641
642 // Remove the second annotation.
643 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
644 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
645 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
646
647 // Save the document, closing the page and document.
648 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000649 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400650
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400651 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400652 // Open the saved document.
653 std::string new_file = GetString();
654 FPDF_FILEACCESS file_access;
655 memset(&file_access, 0, sizeof(file_access));
656 file_access.m_FileLen = new_file.size();
657 file_access.m_GetBlock = GetBlockFromString;
658 file_access.m_Param = &new_file;
659 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
660 ASSERT_TRUE(new_doc);
661 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
662 ASSERT_TRUE(new_page);
663
664 // Check that the saved document has 2 annotations on the first page.
665 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
666
Lei Zhanga21d5932018-02-05 18:28:38 +0000667 // Check that the remaining 2 annotations are the original 1st and 3rd ones
668 // by verifying their rectangle coordinates.
669 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000670 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000671 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
672 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
673 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400674
Lei Zhanga21d5932018-02-05 18:28:38 +0000675 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000676 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000677 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
678 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
679 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400680 FPDF_ClosePage(new_page);
681 FPDF_CloseDocument(new_doc);
682}
Jane Liu8ce58f52017-06-29 13:40:22 -0400683
Lei Zhangab41f252018-12-23 03:10:50 +0000684TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400685#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +0000686 static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2";
687 static const char kMd5ModifiedPath[] = "9059723a045e17478753d2f0eb33bc03";
688 static const char kMd5TwoPaths[] = "7eed0cfba780f1d4dd8068f717d3a6bf";
689 static const char kMd5NewAnnot[] = "1de8212d43b7066a6df042095c2aca61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000690#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +0000691 static const char kMd5Original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
692 static const char kMd5ModifiedPath[] = "c0c2eb2aba73ad15b3240e342fbe0d72";
693 static const char kMd5TwoPaths[] = "2306bf04915fe001b5f4726843d184c8";
694 static const char kMd5NewAnnot[] = "64a319f145768cb09944d2109efe394e";
Jane Liubaa7ff42017-06-29 19:18:23 -0400695#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000696 static const char kMd5Original[] = "964f89bbe8911e540a465cf1a64b7f7e";
697 static const char kMd5ModifiedPath[] = "9a38048fb3ac1b2c9a4b34139caa993c";
698 static const char kMd5TwoPaths[] = "ece3d4df54b3395d6a2bf7a29b17239c";
699 static const char kMd5NewAnnot[] = "4299493de84c249f42f220f30f2bbb67";
Jane Liubaa7ff42017-06-29 19:18:23 -0400700#endif
701
702 // Open a file with two annotations and load its first page.
703 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000704 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400705 ASSERT_TRUE(page);
706 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
707
708 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000709 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000710 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000711 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000712 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400713
Lei Zhanga21d5932018-02-05 18:28:38 +0000714 {
715 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000716 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000717 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400718
Lei Zhanga21d5932018-02-05 18:28:38 +0000719 // Check that this annotation has one path object and retrieve it.
720 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000721 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000722 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
723 EXPECT_FALSE(path);
724 path = FPDFAnnot_GetObject(annot.get(), 0);
725 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
726 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400727
Lei Zhanga21d5932018-02-05 18:28:38 +0000728 // Modify the color of the path object.
729 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
730 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400731
Lei Zhanga21d5932018-02-05 18:28:38 +0000732 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000733 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000734 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000735 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000736 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400737
Lei Zhanga21d5932018-02-05 18:28:38 +0000738 // Add a second path object to the same annotation.
739 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
740 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
741 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
742 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
743 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
744 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
745 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400746
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000747 // The object is in the annontation, not in the page, so the page object
748 // array should not change.
749 ASSERT_EQ(32, FPDFPage_CountObjects(page));
750
Lei Zhanga21d5932018-02-05 18:28:38 +0000751 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000752 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000753 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000754 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000755 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400756
Lei Zhanga21d5932018-02-05 18:28:38 +0000757 // Delete the newly added path object.
758 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
759 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000760 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000761 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400762
763 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000764 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000765 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000766 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000767 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400768
Jane Liubaa7ff42017-06-29 19:18:23 -0400769 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400770
Lei Zhanga21d5932018-02-05 18:28:38 +0000771 {
772 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000773 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000774 ASSERT_TRUE(annot);
775 rect.left = 200.f;
776 rect.bottom = 400.f;
777 rect.right = 500.f;
778 rect.top = 600.f;
779 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400780
Lei Zhanga21d5932018-02-05 18:28:38 +0000781 // Add a new path to the annotation.
782 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
783 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
784 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
785 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
786 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
787 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
788 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
789 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
790 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
791 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
792
793 // Check that the annotation's bounding box came from its rectangle.
794 FS_RECTF new_rect;
795 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
796 EXPECT_EQ(rect.left, new_rect.left);
797 EXPECT_EQ(rect.bottom, new_rect.bottom);
798 EXPECT_EQ(rect.right, new_rect.right);
799 EXPECT_EQ(rect.top, new_rect.top);
800 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400801
802 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400803 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000804 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400805
806 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +0000807 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000808 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000809 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400810
Jane Liu36567742017-07-06 11:13:35 -0400811 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000812 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400813
Lei Zhanga21d5932018-02-05 18:28:38 +0000814 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000815 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000816 ASSERT_TRUE(annot);
817 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400818
Lei Zhanga21d5932018-02-05 18:28:38 +0000819 // Check that the new annotation's rectangle is as defined.
820 FS_RECTF new_rect;
821 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
822 EXPECT_EQ(rect.left, new_rect.left);
823 EXPECT_EQ(rect.bottom, new_rect.bottom);
824 EXPECT_EQ(rect.right, new_rect.right);
825 EXPECT_EQ(rect.top, new_rect.top);
826 }
827
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000828 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400829 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400830}
Jane Liub137e752017-07-05 15:04:33 -0400831
Lei Zhangab41f252018-12-23 03:10:50 +0000832TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400833 // Open a file with an annotation and load its first page.
834 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000835 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400836 ASSERT_TRUE(page);
837
838 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000839 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000840 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000841 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
842 }
Jane Liub137e752017-07-05 15:04:33 -0400843
Lei Zhanga21d5932018-02-05 18:28:38 +0000844 {
845 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000846 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000847 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400848
Lei Zhanga21d5932018-02-05 18:28:38 +0000849 // Check that the original flag values are as expected.
850 int flags = FPDFAnnot_GetFlags(annot.get());
851 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
852 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400853
Lei Zhanga21d5932018-02-05 18:28:38 +0000854 // Set the HIDDEN flag.
855 flags |= FPDF_ANNOT_FLAG_HIDDEN;
856 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
857 flags = FPDFAnnot_GetFlags(annot.get());
858 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
859 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400860
Lei Zhanga21d5932018-02-05 18:28:38 +0000861 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000862 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000863 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000864 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
865 }
Jane Liub137e752017-07-05 15:04:33 -0400866
Lei Zhanga21d5932018-02-05 18:28:38 +0000867 // Unset the HIDDEN flag.
868 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
869 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
870 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
871 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
872 flags = FPDFAnnot_GetFlags(annot.get());
873 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
874 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400875
Lei Zhanga21d5932018-02-05 18:28:38 +0000876 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000877 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000878 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000879 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
880 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000881 }
Jane Liub137e752017-07-05 15:04:33 -0400882
Jane Liub137e752017-07-05 15:04:33 -0400883 UnloadPage(page);
884}
Jane Liu36567742017-07-06 11:13:35 -0400885
Lei Zhangab41f252018-12-23 03:10:50 +0000886TEST_F(FPDFAnnotEmbedderTest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400887#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +0000888 static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2";
889 static const char kMd5NewImage[] = "ff012f5697436dfcaec25b32d1333596";
890 static const char kMd5ModifiedImage[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000891#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +0000892 static const char kMd5Original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
893 static const char kMd5NewImage[] = "d19c6fcfd9a170802fcfb9adfa13557e";
894 static const char kMd5ModifiedImage[] = "1273cf2363570a50d1aa0c95b1318197";
Jane Liu36567742017-07-06 11:13:35 -0400895#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000896 static const char kMd5Original[] = "964f89bbe8911e540a465cf1a64b7f7e";
897 static const char kMd5NewImage[] = "9ea8732dc9d579f68853f16892856208";
898 static const char kMd5ModifiedImage[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400899#endif
900
901 // Open a file with two annotations and load its first page.
902 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000903 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400904 ASSERT_TRUE(page);
905 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
906
907 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000908 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000909 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000910 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000911 }
Jane Liu36567742017-07-06 11:13:35 -0400912
Jane Liu36567742017-07-06 11:13:35 -0400913 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000914 FPDF_BITMAP image_bitmap;
915
916 {
917 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000918 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000919 ASSERT_TRUE(annot);
920 FS_RECTF rect;
921 rect.left = 200.f;
922 rect.bottom = 600.f;
923 rect.right = 400.f;
924 rect.top = 800.f;
925 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
926
927 // Add a solid-color translucent image object to the new annotation.
928 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
929 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
930 0xeeeecccc);
931 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
932 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
933 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
934 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
935 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
936 kBitmapSize, 0, 0));
937 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
938 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
939 }
Jane Liu36567742017-07-06 11:13:35 -0400940
941 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000942 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000943 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000944 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000945 }
Jane Liu36567742017-07-06 11:13:35 -0400946
Lei Zhanga21d5932018-02-05 18:28:38 +0000947 {
948 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000949 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000950 ASSERT_TRUE(annot);
951 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
952 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
953 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400954
Lei Zhanga21d5932018-02-05 18:28:38 +0000955 // Modify the image in the new annotation.
956 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
957 0xff000000);
958 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
959 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
960 }
Jane Liu36567742017-07-06 11:13:35 -0400961
962 // Save the document, closing the page and document.
963 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000964 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400965 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400966
967 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +0000968 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -0400969}
970
Lei Zhangab41f252018-12-23 03:10:50 +0000971TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400972#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +0000973 static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2";
974 static const char kMd5NewText[] = "60031c1b0330cf1e1575f7d46687d429";
975 static const char kMd5ModifiedText[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000976#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +0000977 static const char kMd5Original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
978 static const char kMd5NewText[] = "87d55e09f9096de7e6552f5ae79afd3b";
979 static const char kMd5ModifiedText[] = "26e94fbd3af4b1e65479327507600114";
Jane Liu36567742017-07-06 11:13:35 -0400980#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000981 static const char kMd5Original[] = "964f89bbe8911e540a465cf1a64b7f7e";
982 static const char kMd5NewText[] = "30f3f5b989612ca03827d95f184f0979";
983 static const char kMd5ModifiedText[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400984#endif
985
986 // Open a file with two annotations and load its first page.
987 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000988 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400989 ASSERT_TRUE(page);
990 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
991
992 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000993 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000994 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000995 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000996 }
Jane Liu36567742017-07-06 11:13:35 -0400997
Lei Zhanga21d5932018-02-05 18:28:38 +0000998 {
999 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001000 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001001 ASSERT_TRUE(annot);
1002 FS_RECTF rect;
1003 rect.left = 200.f;
1004 rect.bottom = 550.f;
1005 rect.right = 450.f;
1006 rect.top = 650.f;
1007 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001008
Lei Zhanga21d5932018-02-05 18:28:38 +00001009 // Add a translucent text object to the new annotation.
1010 FPDF_PAGEOBJECT text_object =
1011 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1012 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001013 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001014 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1015 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1016 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
1017 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1018 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1019 }
Jane Liu36567742017-07-06 11:13:35 -04001020
1021 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001022 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001023 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001024 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001025 }
Jane Liu36567742017-07-06 11:13:35 -04001026
Lei Zhanga21d5932018-02-05 18:28:38 +00001027 {
1028 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001029 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001030 ASSERT_TRUE(annot);
1031 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1032 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1033 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001034
Lei Zhanga21d5932018-02-05 18:28:38 +00001035 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001036 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001037 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1038 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1039 }
Jane Liu36567742017-07-06 11:13:35 -04001040
1041 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001042 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001043 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001044 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001045 }
Jane Liu36567742017-07-06 11:13:35 -04001046
1047 // Remove the new annotation, and check that the page renders as before.
1048 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001049 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001050 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001051 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001052 }
Jane Liu36567742017-07-06 11:13:35 -04001053
1054 UnloadPage(page);
1055}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001056
Lei Zhangab41f252018-12-23 03:10:50 +00001057TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001058 // Open a file with four annotations and load its first page.
1059 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001060 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001061 ASSERT_TRUE(page);
1062
Lei Zhang4f556b82019-04-08 16:32:41 +00001063 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001064
Lei Zhanga21d5932018-02-05 18:28:38 +00001065 {
1066 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001067 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001068 ASSERT_TRUE(annot);
1069
1070 // Check that a non-existent key does not exist.
1071 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1072
1073 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001074 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001075 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001076 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1077 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1078 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001079
1080 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001081 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001082 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001083 unsigned long length_bytes =
Lei Zhanga21d5932018-02-05 18:28:38 +00001084 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001085 ASSERT_EQ(66u, length_bytes);
1086 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1087 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1088 length_bytes));
1089 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1090 GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001091
1092 // Check that the string value of the modified date is correct.
1093 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001094 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1095 nullptr, 0);
1096 ASSERT_EQ(44u, length_bytes);
1097 buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001098 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001099 buf.data(), length_bytes));
1100 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001101
1102 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001103 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001104 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1105 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001106 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001107
1108 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001109 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001110 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001111
Dan Sinclair698aed72017-09-26 16:24:49 -04001112#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +00001113 static const char kMd5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001114#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +00001115 static const char kMd5[] = "9ee141f698c3fcb56c050dffd6c82624";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001116#else
Lei Zhang4f556b82019-04-08 16:32:41 +00001117 static const char kMd5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001118#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001119
1120 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001121 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001122 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001123 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001124 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001125 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001126
Lei Zhanga21d5932018-02-05 18:28:38 +00001127 // Check that the string value of the modified date is the newly-set value.
1128 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001129 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001130 unsigned long length_bytes = FPDFAnnot_GetStringValue(
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001131 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001132 ASSERT_EQ(44u, length_bytes);
1133 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001134 EXPECT_EQ(44u,
1135 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001136 buf.data(), length_bytes));
1137 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001138 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001139
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001140 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001141 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001142}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001143
rycsmith3e785602019-03-05 21:48:36 +00001144TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
1145 // Open a file with three text annotations and load its first page.
1146 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1147 FPDF_PAGE page = LoadPage(0);
1148 ASSERT_TRUE(page);
1149 {
1150 // First two annotations do not have "MaxLen" attribute.
1151 for (int i = 0; i < 2; i++) {
1152 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1153 ASSERT_TRUE(annot);
1154
1155 // Verify that no "MaxLen" key present.
1156 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1157
1158 float value;
1159 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1160 }
1161
1162 // Annotation in index 2 has "MaxLen" of 10.
1163 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1164 ASSERT_TRUE(annot);
1165
1166 // Verify that "MaxLen" key present.
1167 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1168
1169 float value;
1170 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1171 EXPECT_FLOAT_EQ(10.0f, value);
1172
1173 // Check bad inputs.
1174 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1175 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1176 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1177 // Ask for key that exists but is not a number.
1178 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1179 }
1180
1181 UnloadPage(page);
1182}
1183
Lei Zhangab41f252018-12-23 03:10:50 +00001184TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001185 // Open a file with four annotations and load its first page.
1186 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001187 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001188 ASSERT_TRUE(page);
1189
Lei Zhanga21d5932018-02-05 18:28:38 +00001190 {
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001191 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1192 static constexpr size_t kExpectNormalAPLength = 73970;
1193
Lei Zhanga21d5932018-02-05 18:28:38 +00001194 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001195 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001196 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001197
Lei Zhanga21d5932018-02-05 18:28:38 +00001198 // Check that the string value of an AP returns the expected length.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001199 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001200 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001201 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001202
Lei Zhanga21d5932018-02-05 18:28:38 +00001203 // Check that the string value of an AP is not returned if the buffer is too
1204 // small. The result buffer should be overwritten with an empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001205 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1206 // Write in the buffer to verify it's not overwritten.
1207 memcpy(buf.data(), "abcdefgh", 8);
1208 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001209 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001210 buf.data(), normal_length_bytes - 1));
1211 EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001212
Lei Zhanga21d5932018-02-05 18:28:38 +00001213 // Check that the string value of an AP is returned through a buffer that is
1214 // the right size.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001215 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001216 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001217 buf.data(), normal_length_bytes));
1218 EXPECT_EQ(kMd5NormalAP,
1219 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1220 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001221
Lei Zhanga21d5932018-02-05 18:28:38 +00001222 // Check that the string value of an AP is returned through a buffer that is
1223 // larger than necessary.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001224 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1225 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001226 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001227 buf.data(), normal_length_bytes + 2));
1228 EXPECT_EQ(kMd5NormalAP,
1229 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1230 normal_length_bytes));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001231
Lei Zhanga21d5932018-02-05 18:28:38 +00001232 // Check that getting an AP for a mode that does not have an AP returns an
1233 // empty string.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001234 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001235 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001236 ASSERT_EQ(2u, rollover_length_bytes);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001237
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001238 buf = GetFPDFWideStringBuffer(1000);
Lei Zhanga21d5932018-02-05 18:28:38 +00001239 EXPECT_EQ(2u,
1240 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001241 buf.data(), 1000));
1242 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001243
Lei Zhanga21d5932018-02-05 18:28:38 +00001244 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001245 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001246 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001247 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001248 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001249 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001250 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001251
Lei Zhanga21d5932018-02-05 18:28:38 +00001252 // Set the AP correctly now.
1253 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001254 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001255
Lei Zhanga21d5932018-02-05 18:28:38 +00001256 // Check that the new annotation value is equal to the value we just set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001257 rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001258 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001259 ASSERT_EQ(24u, rollover_length_bytes);
1260 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001261 EXPECT_EQ(24u,
1262 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001263 buf.data(), rollover_length_bytes));
1264 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001265
Lei Zhanga21d5932018-02-05 18:28:38 +00001266 // Check that the Normal AP was not touched when the Rollover AP was set.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001267 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1268 EXPECT_EQ(kExpectNormalAPLength,
Lei Zhanga21d5932018-02-05 18:28:38 +00001269 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001270 buf.data(), normal_length_bytes));
1271 EXPECT_EQ(kMd5NormalAP,
1272 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()),
1273 normal_length_bytes));
Lei Zhanga21d5932018-02-05 18:28:38 +00001274 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001275
1276 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001277 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001278 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001279
Lei Zhang0b494052019-01-31 21:41:15 +00001280 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001281 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001282 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001283 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001284
Lei Zhanga21d5932018-02-05 18:28:38 +00001285 // Check that the new annotation value is equal to the value we set before
1286 // saving.
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001287 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
Lei Zhanga21d5932018-02-05 18:28:38 +00001288 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001289 ASSERT_EQ(24u, rollover_length_bytes);
1290 std::vector<FPDF_WCHAR> buf =
1291 GetFPDFWideStringBuffer(rollover_length_bytes);
Lei Zhanga21d5932018-02-05 18:28:38 +00001292 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1293 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001294 buf.data(), rollover_length_bytes));
1295 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001296 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001297
1298 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001299 CloseSavedPage(page);
1300 CloseSavedDocument();
1301}
1302
Lei Zhangab41f252018-12-23 03:10:50 +00001303TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001304 // Open a file with four annotations and load its first page.
1305 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001306 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001307 ASSERT_TRUE(page);
1308
Lei Zhanga21d5932018-02-05 18:28:38 +00001309 {
1310 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001311 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001312 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001313
Lei Zhanga21d5932018-02-05 18:28:38 +00001314 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001315 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001316 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001317 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001318 EXPECT_EQ(73970u,
1319 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1320 nullptr, 0));
1321 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1322 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001323
Lei Zhanga21d5932018-02-05 18:28:38 +00001324 // Check that setting the Down AP to null removes the Down entry but keeps
1325 // Normal intact.
1326 EXPECT_TRUE(
1327 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1328 EXPECT_EQ(73970u,
1329 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1330 nullptr, 0));
1331 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1332 nullptr, 0));
1333 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001334
Lei Zhang75c81712018-02-08 17:22:39 +00001335 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001336}
1337
Lei Zhangab41f252018-12-23 03:10:50 +00001338TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001339 // Open a file with four annotations and load its first page.
1340 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001341 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001342 ASSERT_TRUE(page);
1343
Lei Zhanga21d5932018-02-05 18:28:38 +00001344 {
1345 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001346 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001347 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001348
Lei Zhanga21d5932018-02-05 18:28:38 +00001349 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001350 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001351 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001352 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001353 EXPECT_EQ(73970u,
1354 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1355 nullptr, 0));
1356 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1357 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001358
Lei Zhanga21d5932018-02-05 18:28:38 +00001359 // Check that setting the Normal AP to null removes the whole AP dictionary.
1360 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1361 nullptr));
1362 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1363 nullptr, 0));
1364 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1365 nullptr, 0));
1366 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001367
Lei Zhang75c81712018-02-08 17:22:39 +00001368 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001369}
1370
Lei Zhangab41f252018-12-23 03:10:50 +00001371TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001372 // Open a file with annotations and load its first page.
1373 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001374 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001375 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001376 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001377
Lei Zhanga21d5932018-02-05 18:28:38 +00001378 {
1379 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001380 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001381 ASSERT_TRUE(annot);
1382 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1383 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001384 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001385 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1386 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1387 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001388
Lei Zhanga21d5932018-02-05 18:28:38 +00001389 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001390 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001391 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1392 ASSERT_TRUE(popup);
1393 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1394 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1395 FS_RECTF rect;
1396 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1397 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1398 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001399
Lei Zhanga21d5932018-02-05 18:28:38 +00001400 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1401 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001402 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001403 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1404 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001405
Lei Zhanga21d5932018-02-05 18:28:38 +00001406 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1407 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001408 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001409 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001410 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1411 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001412 }
Jane Liu300bb272017-08-21 14:37:53 -04001413
Jane Liu300bb272017-08-21 14:37:53 -04001414 UnloadPage(page);
1415}
1416
Lei Zhangab41f252018-12-23 03:10:50 +00001417TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001418 // Open file with form text fields.
1419 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001420 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001421 ASSERT_TRUE(page);
1422
Lei Zhanga21d5932018-02-05 18:28:38 +00001423 {
1424 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001425 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001426 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001427
Lei Zhanga21d5932018-02-05 18:28:38 +00001428 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001429 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001430 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1431 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001432
Lei Zhanga21d5932018-02-05 18:28:38 +00001433 {
1434 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001435 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001436 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001437
Lei Zhanga21d5932018-02-05 18:28:38 +00001438 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001439 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001440 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1441 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001442
1443 UnloadPage(page);
1444}
1445
Lei Zhangab41f252018-12-23 03:10:50 +00001446TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001447 // Open file with form text fields.
1448 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001449 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001450 ASSERT_TRUE(page);
1451
Lei Zhanga21d5932018-02-05 18:28:38 +00001452 {
1453 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001454 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001455 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001456
Lei Zhanga21d5932018-02-05 18:28:38 +00001457 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001458 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001459 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1460 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1461 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1462 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001463
Lei Zhanga21d5932018-02-05 18:28:38 +00001464 {
1465 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001466 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001467 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001468
Lei Zhanga21d5932018-02-05 18:28:38 +00001469 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001470 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001471 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1472 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1473 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1474 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001475
Lei Zhanga21d5932018-02-05 18:28:38 +00001476 {
1477 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001478 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001479 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001480
Lei Zhanga21d5932018-02-05 18:28:38 +00001481 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001482 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001483 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1484 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1485 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1486 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001487
1488 UnloadPage(page);
1489}
Diana Gage40870db2017-07-19 18:16:03 -07001490
Lei Zhangab41f252018-12-23 03:10:50 +00001491TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001492 // Open file with form text fields.
1493 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1494 FPDF_PAGE page = LoadPage(0);
1495 ASSERT_TRUE(page);
1496
1497 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang7557e7b2018-09-14 17:02:40 +00001498 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0));
1499
1500 {
1501 // Verify there is an annotation.
1502 ScopedFPDFAnnotation annot(
1503 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120));
1504 EXPECT_TRUE(annot);
1505 }
1506
1507 // Try other bad inputs at a valid location.
1508 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120));
1509 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120));
1510 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120));
Diana Gage40870db2017-07-19 18:16:03 -07001511
1512 UnloadPage(page);
1513}
1514
Lei Zhangab41f252018-12-23 03:10:50 +00001515TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001516 // Open file with form text fields.
1517 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1518 FPDF_PAGE page = LoadPage(0);
1519 ASSERT_TRUE(page);
1520
Lei Zhanga21d5932018-02-05 18:28:38 +00001521 {
1522 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001523 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001524 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1525 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001526
Lei Zhanga21d5932018-02-05 18:28:38 +00001527 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001528 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001529 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1530 }
Diana Gage40870db2017-07-19 18:16:03 -07001531
Lei Zhanga21d5932018-02-05 18:28:38 +00001532 {
1533 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001534 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001535 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1536 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001537
Lei Zhanga21d5932018-02-05 18:28:38 +00001538 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001539 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001540 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1541 }
Diana Gage40870db2017-07-19 18:16:03 -07001542
1543 UnloadPage(page);
1544}
1545
Lei Zhangab41f252018-12-23 03:10:50 +00001546TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001547 // Open file with form comboboxes.
1548 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1549 FPDF_PAGE page = LoadPage(0);
1550 ASSERT_TRUE(page);
1551
Lei Zhanga21d5932018-02-05 18:28:38 +00001552 {
1553 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001554 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001555 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001556 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001557
Lei Zhanga21d5932018-02-05 18:28:38 +00001558 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001559 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001560 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1561 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1562 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1563 }
Diana Gage40870db2017-07-19 18:16:03 -07001564
Lei Zhanga21d5932018-02-05 18:28:38 +00001565 {
1566 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001567 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001568 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001569 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001570
Lei Zhanga21d5932018-02-05 18:28:38 +00001571 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001572 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001573 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1574 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1575 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1576 }
Diana Gage40870db2017-07-19 18:16:03 -07001577
Lei Zhanga21d5932018-02-05 18:28:38 +00001578 {
1579 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001580 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001581 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001582 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001583
Lei Zhanga21d5932018-02-05 18:28:38 +00001584 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001585 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001586 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1587 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1588 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1589 }
Diana Gage40870db2017-07-19 18:16:03 -07001590
1591 UnloadPage(page);
1592}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001593
Lei Zhang992e7e22019-02-04 19:20:58 +00001594TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
1595 static constexpr size_t kExpectedSize = 1609;
1596 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1597
1598 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1599
1600 FPDF_PAGE page = LoadPage(0);
1601 ASSERT_TRUE(page);
1602
1603 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1604 EXPECT_EQ(kExpectedSize, GetString().size());
1605 ClearString();
1606
1607 for (size_t i = 0; i < 10; ++i) {
1608 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1609 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
1610
1611 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1612 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
1613 // equal, not bigger.
1614 EXPECT_LT(kExpectedSize, GetString().size());
1615 ClearString();
1616 }
1617
1618 UnloadPage(page);
1619}
1620
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001621TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1622 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1623 FPDF_PAGE page = LoadPage(0);
1624 ASSERT_TRUE(page);
1625 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1626
1627 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00001628 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001629 static const size_t kBufSize = 12;
1630 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001631
1632 {
1633 // Add a text annotation to the page.
1634 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1635 ASSERT_TRUE(annot);
1636 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1637 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1638
1639 // Make sure there is no test key, add set a value there, and read it back.
1640 std::fill(buf.begin(), buf.end(), 'x');
1641 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001642 kBufSize));
1643 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001644
Lei Zhangf0f67682019-04-08 17:03:21 +00001645 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001646 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1647
1648 std::fill(buf.begin(), buf.end(), 'x');
1649 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001650 kBufSize));
1651 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001652 }
1653
Lei Zhang05ec64c2019-01-09 03:00:06 +00001654 {
1655 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1656 ASSERT_TRUE(annot);
1657 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1658 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1659 // Also do the same test for its appearance string.
1660 std::fill(buf.begin(), buf.end(), 'x');
1661 ASSERT_EQ(2u,
1662 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001663 buf.data(), kBufSize));
1664 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001665
Lei Zhangf0f67682019-04-08 17:03:21 +00001666 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00001667 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1668 text.get()));
1669
1670 std::fill(buf.begin(), buf.end(), 'x');
1671 ASSERT_EQ(6u,
1672 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001673 buf.data(), kBufSize));
1674 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001675 }
1676
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001677 UnloadPage(page);
1678
1679 {
1680 // Save a copy, open the copy, and check the annotation again.
1681 // Note that it renders the rotation.
1682 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001683 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001684 FPDF_PAGE saved_page = LoadSavedPage(0);
1685 ASSERT_TRUE(saved_page);
1686
Lei Zhang05ec64c2019-01-09 03:00:06 +00001687 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001688 {
1689 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1690 ASSERT_TRUE(annot);
1691 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1692
1693 std::fill(buf.begin(), buf.end(), 'x');
1694 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001695 kBufSize));
1696 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001697 }
1698
Lei Zhang05ec64c2019-01-09 03:00:06 +00001699 {
1700 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1701 ASSERT_TRUE(annot);
1702 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1703 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1704
1705 std::fill(buf.begin(), buf.end(), 'x');
1706 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001707 kBufSize));
1708 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
Lei Zhang05ec64c2019-01-09 03:00:06 +00001709 }
1710
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001711 CloseSavedPage(saved_page);
1712 CloseSavedDocument();
1713 }
1714}
rycsmithcb752f32019-02-21 18:40:53 +00001715
1716TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
1717 // Open a file with combobox widget annotations and load its first page.
1718 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1719 FPDF_PAGE page = LoadPage(0);
1720 ASSERT_TRUE(page);
1721
1722 {
1723 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1724 ASSERT_TRUE(annot);
1725
1726 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1727
1728 annot.reset(FPDFPage_GetAnnot(page, 1));
1729 ASSERT_TRUE(annot);
1730
1731 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00001732
1733 // Check bad form handle / annot.
1734 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
1735 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
1736 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00001737 }
1738
1739 UnloadPage(page);
1740}
1741
1742TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
1743 // Open a file with listbox widget annotations and load its first page.
1744 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1745 FPDF_PAGE page = LoadPage(0);
1746 ASSERT_TRUE(page);
1747
1748 {
1749 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1750 ASSERT_TRUE(annot);
1751
1752 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1753
1754 annot.reset(FPDFPage_GetAnnot(page, 1));
1755 ASSERT_TRUE(annot);
1756
1757 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1758 }
1759
1760 UnloadPage(page);
1761}
1762
1763TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
1764 // Open a file with ink annotations and load its first page.
1765 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1766 FPDF_PAGE page = LoadPage(0);
1767 ASSERT_TRUE(page);
1768
1769 {
1770 // annotations do not have "Opt" array and will return -1
1771 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1772 ASSERT_TRUE(annot);
1773
1774 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1775
1776 annot.reset(FPDFPage_GetAnnot(page, 1));
1777 ASSERT_TRUE(annot);
1778
1779 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1780 }
1781
1782 UnloadPage(page);
1783}
1784
1785TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
1786 // Open a file with combobox widget annotations and load its first page.
1787 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1788 FPDF_PAGE page = LoadPage(0);
1789 ASSERT_TRUE(page);
1790
1791 {
1792 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1793 ASSERT_TRUE(annot);
1794
1795 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001796 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001797 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001798 ASSERT_EQ(8u, length_bytes);
1799 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001800 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001801 buf.data(), length_bytes));
1802 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001803
1804 annot.reset(FPDFPage_GetAnnot(page, 1));
1805 ASSERT_TRUE(annot);
1806
1807 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001808 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001809 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001810 ASSERT_EQ(12u, length_bytes);
1811 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001812 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001813 buf.data(), length_bytes));
1814 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001815
1816 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001817 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001818 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001819 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001820 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001821 buf.data(), length_bytes));
1822 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001823
Lei Zhange7033c82019-02-26 19:30:49 +00001824 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00001825 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1826 nullptr, 0));
1827 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1828 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00001829
1830 // Check bad form handle / annot.
1831 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
1832 EXPECT_EQ(0u,
1833 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
1834 EXPECT_EQ(0u,
1835 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00001836 }
1837
1838 UnloadPage(page);
1839}
1840
1841TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
1842 // Open a file with listbox widget annotations and load its first page.
1843 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1844 FPDF_PAGE page = LoadPage(0);
1845 ASSERT_TRUE(page);
1846
1847 {
1848 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1849 ASSERT_TRUE(annot);
1850
1851 int index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001852 unsigned long length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001853 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001854 ASSERT_EQ(8u, length_bytes);
1855 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001856 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001857 buf.data(), length_bytes));
1858 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001859
1860 annot.reset(FPDFPage_GetAnnot(page, 1));
1861 ASSERT_TRUE(annot);
1862
1863 index = 0;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001864 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001865 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001866 ASSERT_EQ(12u, length_bytes);
1867 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001868 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001869 buf.data(), length_bytes));
1870 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001871
1872 index = 25;
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001873 length_bytes =
rycsmithcb752f32019-02-21 18:40:53 +00001874 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001875 ASSERT_EQ(18u, length_bytes);
1876 buf = GetFPDFWideStringBuffer(length_bytes);
rycsmithcb752f32019-02-21 18:40:53 +00001877 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
Lei Zhang5bf8c7f2019-04-08 17:50:11 +00001878 buf.data(), length_bytes));
1879 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
rycsmithcb752f32019-02-21 18:40:53 +00001880
1881 // indices out of range
1882 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1883 nullptr, 0));
1884 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1885 nullptr, 0));
1886 }
1887
1888 UnloadPage(page);
1889}
1890
1891TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
1892 // Open a file with ink annotations and load its first page.
1893 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1894 FPDF_PAGE page = LoadPage(0);
1895 ASSERT_TRUE(page);
1896
1897 {
1898 // annotations do not have "Opt" array and will return 0
1899 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1900 ASSERT_TRUE(annot);
1901
1902 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1903 nullptr, 0));
1904
1905 annot.reset(FPDFPage_GetAnnot(page, 1));
1906 ASSERT_TRUE(annot);
1907
1908 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1909 nullptr, 0));
1910 }
1911
1912 UnloadPage(page);
1913}