blob: 19b22f5bfd3c866e17ed7b25c5387cacd5525e40 [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"
21
Lei Zhangab41f252018-12-23 03:10:50 +000022class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040023
Lei Zhang4afee172018-01-10 20:57:15 +000024std::wstring BufferToWString(const std::vector<char>& buf) {
25 return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000026}
27
Lei Zhang4afee172018-01-10 20:57:15 +000028std::string BufferToString(const std::vector<char>& buf) {
29 return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000030}
31
Lei Zhangab41f252018-12-23 03:10:50 +000032TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +000033 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
34 FPDF_PAGE page = LoadPage(0);
35 ASSERT_TRUE(page);
36
37 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
38
39 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
40 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
41 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
42 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
43 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
44
45 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
46
47 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
48
49 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
50 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
51 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
52
53 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
54
Lei Zhang4f556b82019-04-08 16:32:41 +000055 static const wchar_t kContents[] = L"Bar";
Lei Zhangf0f67682019-04-08 17:03:21 +000056 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhang7557e7b2018-09-14 17:02:40 +000057 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
58
59 char buffer[128];
60 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
61 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
62 EXPECT_EQ(0u,
63 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
64
65 UnloadPage(page);
66}
67
Lei Zhang3d9a0972019-03-04 19:34:09 +000068TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
69 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
70 FPDF_PAGE page = LoadPage(0);
71 ASSERT_TRUE(page);
72
73 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +000074 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +000075
76 UnloadPage(page);
77}
78
Lei Zhangab41f252018-12-23 03:10:50 +000079TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -040080 // Open a file with one annotation and load its first page.
81 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000082 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040083 ASSERT_TRUE(page);
84
85 // This annotation has a malformed appearance stream, which does not have its
86 // normal appearance defined, only its rollover appearance. In this case, its
87 // normal appearance should be generated, allowing the highlight annotation to
88 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +000089 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +000090 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040091
92 UnloadPage(page);
93}
94
Lei Zhangab41f252018-12-23 03:10:50 +000095TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang4f556b82019-04-08 16:32:41 +000096 static const char kMd5[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000097 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000098 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
99 FPDF_PAGE page = LoadPage(0);
100 ASSERT_TRUE(page);
101
Tom Sepeze08d2b12018-04-25 18:49:32 +0000102 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000103 CompareBitmap(bitmap.get(), 595, 842, kMd5);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000104
105 UnloadPage(page);
106}
107
Lei Zhangab41f252018-12-23 03:10:50 +0000108TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400109 // Open a file with one annotation and load its first page.
110 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000111 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400112 ASSERT_TRUE(page);
113
114 // Check that there is a total of 1 annotation on its first page.
115 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
116
117 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000118 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000119 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000120 ASSERT_TRUE(annot);
121 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400122
Lei Zhanga21d5932018-02-05 18:28:38 +0000123 // Check that the annotation color is yellow.
124 unsigned int R;
125 unsigned int G;
126 unsigned int B;
127 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000128 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000129 &G, &B, &A));
130 EXPECT_EQ(255u, R);
131 EXPECT_EQ(255u, G);
132 EXPECT_EQ(0u, B);
133 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400134
Lei Zhanga21d5932018-02-05 18:28:38 +0000135 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000136 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000137 EXPECT_EQ(FPDF_OBJECT_STRING,
138 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
139 unsigned long len =
140 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
141 std::vector<char> buf(len);
142 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
143 len));
144 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400145
Lei Zhanga21d5932018-02-05 18:28:38 +0000146 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000147 EXPECT_EQ(
148 FPDF_OBJECT_STRING,
149 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
150 len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kContents,
151 nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +0000152 buf.clear();
153 buf.resize(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000154 EXPECT_EQ(2690u,
155 FPDFAnnot_GetStringValue(
156 annot.get(), pdfium::annotation::kContents, buf.data(), len));
Lei Zhang4f556b82019-04-08 16:32:41 +0000157 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000158 L"This is a note for that highlight annotation. Very long highlight "
159 "annotation. Long long long Long 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 longLong long longLong long longLong long longLong long "
172 "longLong long longLong long longLong long longLong long longLong long "
173 "longLong long longLong long longLong long longLong long longLong long "
174 "longLong long longLong long longLong long longLong long longLong long "
175 "longLong long longLong long longLong long longLong long longLong long "
176 "longLong long longLong long longLong long longLong long longLong long "
177 "longLong long long. END";
Lei Zhang4f556b82019-04-08 16:32:41 +0000178 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400179
Lei Zhanga21d5932018-02-05 18:28:38 +0000180 // Check that the quadpoints are correct.
181 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000182 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000183 EXPECT_EQ(115.802643f, quadpoints.x1);
184 EXPECT_EQ(718.913940f, quadpoints.y1);
185 EXPECT_EQ(157.211182f, quadpoints.x4);
186 EXPECT_EQ(706.264465f, quadpoints.y4);
187 }
Tom Sepez507d0192018-11-07 16:37:51 +0000188 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400189}
190
Lei Zhangab41f252018-12-23 03:10:50 +0000191TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400192 // Open a file with three annotations and load its first page.
193 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000194 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400195 ASSERT_TRUE(page);
196
197 // Check that there is a total of 3 annotation on its first page.
198 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
199
Lei Zhanga21d5932018-02-05 18:28:38 +0000200 {
201 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000202 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000203 ASSERT_TRUE(annot);
204 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400205
Lei Zhanga21d5932018-02-05 18:28:38 +0000206 // Check that the annotation color is blue with opacity.
207 unsigned int R;
208 unsigned int G;
209 unsigned int B;
210 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000211 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000212 &G, &B, &A));
213 EXPECT_EQ(0u, R);
214 EXPECT_EQ(0u, G);
215 EXPECT_EQ(255u, B);
216 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400217
Lei Zhanga21d5932018-02-05 18:28:38 +0000218 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000219 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
220 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400221
Lei Zhang4f556b82019-04-08 16:32:41 +0000222 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000223 // Note that upon rendering, the rectangle coordinates will be adjusted.
224 FS_RECTF rect;
225 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
226 EXPECT_EQ(351.820404f, rect.left);
227 EXPECT_EQ(583.830688f, rect.bottom);
228 EXPECT_EQ(475.336090f, rect.right);
229 EXPECT_EQ(681.535034f, rect.top);
230 }
Tom Sepezef43c262018-11-07 16:41:32 +0000231 {
232 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
233 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
234 }
Tom Sepez507d0192018-11-07 16:37:51 +0000235 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400236}
Jane Liu20eafda2017-06-07 10:33:24 -0400237
Lei Zhangab41f252018-12-23 03:10:50 +0000238TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400239 // Open a file with one annotation and load its first page.
240 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000241 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400242 ASSERT_TRUE(page);
243
244 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400245 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400246
247 UnloadPage(page);
248}
249
Lei Zhangab41f252018-12-23 03:10:50 +0000250TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400251 // Open a file with no annotation and load its first page.
252 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000253 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400254 ASSERT_TRUE(page);
255 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
256
Lei Zhanga21d5932018-02-05 18:28:38 +0000257 {
258 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000259 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000260 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400261
Lei Zhanga21d5932018-02-05 18:28:38 +0000262 // Check that there is now 1 annotations on this page.
263 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400264
Lei Zhanga21d5932018-02-05 18:28:38 +0000265 // Check that the subtype of the annotation is correct.
266 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
267 }
Jane Liue10509a2017-06-20 16:47:41 -0400268
Lei Zhanga21d5932018-02-05 18:28:38 +0000269 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000270 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000271 ASSERT_TRUE(annot);
272 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400273
Lei Zhanga21d5932018-02-05 18:28:38 +0000274 // Set the color of the annotation.
275 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
276 102, 153, 204));
277 // Check that the color has been set correctly.
278 unsigned int R;
279 unsigned int G;
280 unsigned int B;
281 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000282 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000283 &G, &B, &A));
284 EXPECT_EQ(51u, R);
285 EXPECT_EQ(102u, G);
286 EXPECT_EQ(153u, B);
287 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400288
Lei Zhanga21d5932018-02-05 18:28:38 +0000289 // Change the color of the annotation.
290 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
291 153, 102, 51));
292 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000293 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000294 &G, &B, &A));
295 EXPECT_EQ(204u, R);
296 EXPECT_EQ(153u, G);
297 EXPECT_EQ(102u, B);
298 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400299
Lei Zhanga21d5932018-02-05 18:28:38 +0000300 // Set the annotation rectangle.
301 FS_RECTF rect;
302 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
303 EXPECT_EQ(0.f, rect.left);
304 EXPECT_EQ(0.f, rect.right);
305 rect.left = 35;
306 rect.bottom = 150;
307 rect.right = 53;
308 rect.top = 165;
309 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
310 // Check that the annotation rectangle has been set correctly.
311 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
312 EXPECT_EQ(35.f, rect.left);
313 EXPECT_EQ(150.f, rect.bottom);
314 EXPECT_EQ(53.f, rect.right);
315 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400316
Lei Zhanga21d5932018-02-05 18:28:38 +0000317 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000318 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhangf0f67682019-04-08 17:03:21 +0000319 ScopedFPDFWideString text = GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000320 ASSERT_TRUE(FPDFAnnot_SetStringValue(
321 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000322 // Check that the content has been set correctly.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000323 unsigned long len = FPDFAnnot_GetStringValue(
324 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +0000325 std::vector<char> buf(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000326 EXPECT_EQ(74u,
327 FPDFAnnot_GetStringValue(
328 annot.get(), pdfium::annotation::kContents, buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +0000329 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
330 }
Jane Liu20eafda2017-06-07 10:33:24 -0400331 UnloadPage(page);
332}
333
Lei Zhangab41f252018-12-23 03:10:50 +0000334TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400335 // Open a file with one annotation and load its first page.
336 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000337 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400338 ASSERT_TRUE(page);
339
340 // Check that there is a total of one annotation on its first page, and verify
341 // its quadpoints.
342 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400343 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000344 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000345 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000346 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000347 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000348 EXPECT_EQ(115.802643f, quadpoints.x1);
349 EXPECT_EQ(718.913940f, quadpoints.y1);
350 EXPECT_EQ(157.211182f, quadpoints.x4);
351 EXPECT_EQ(706.264465f, quadpoints.y4);
352 }
Jane Liu20eafda2017-06-07 10:33:24 -0400353
354 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000355 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000356 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000357 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
358 ASSERT_TRUE(annot);
359 quadpoints.x1 = 140.802643f;
360 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000361 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000362 }
Jane Liu20eafda2017-06-07 10:33:24 -0400363
364 // Save the document, closing the page and document.
365 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000366 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400367
368 // Open the saved document.
Lei Zhang4f556b82019-04-08 16:32:41 +0000369 static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400370
Lei Zhang0b494052019-01-31 21:41:15 +0000371 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000372 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000373 VerifySavedRendering(page, 612, 792, kMd5);
Jane Liu20eafda2017-06-07 10:33:24 -0400374
375 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000376 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400377
Lei Zhanga21d5932018-02-05 18:28:38 +0000378 {
379 // Check that the second annotation is an underline annotation and verify
380 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000381 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000382 ASSERT_TRUE(new_annot);
383 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
384 FS_QUADPOINTSF new_quadpoints;
385 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000386 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000387 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
388 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
389 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
390 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
391 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400392
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000393 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400394 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400395}
Jane Liu06462752017-06-27 16:41:14 -0400396
Lei Zhangab41f252018-12-23 03:10:50 +0000397TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000398 // Open a file with four annotations and load its first page.
399 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
400 FPDF_PAGE page = LoadPage(0);
401 ASSERT_TRUE(page);
402 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
403
404 // Retrieve the highlight annotation.
405 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
406 ASSERT_TRUE(annot);
407 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
408
409 FS_QUADPOINTSF quadpoints;
410 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
411
412 {
413 // Verify the current one set of quadpoints.
414 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
415
416 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
417 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
418 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
419 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
420 }
421
422 {
423 // Update the quadpoints.
424 FS_QUADPOINTSF new_quadpoints = quadpoints;
425 new_quadpoints.y1 -= 20.f;
426 new_quadpoints.y2 -= 20.f;
427 new_quadpoints.y3 -= 20.f;
428 new_quadpoints.y4 -= 20.f;
429 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
430
431 // Verify added quadpoint set
432 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
433 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
434 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
435 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
436 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
437 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
438 }
439
440 {
441 // Append a new set of quadpoints.
442 FS_QUADPOINTSF new_quadpoints = quadpoints;
443 new_quadpoints.y1 += 20.f;
444 new_quadpoints.y2 += 20.f;
445 new_quadpoints.y3 += 20.f;
446 new_quadpoints.y4 += 20.f;
447 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
448
449 // Verify added quadpoint set
450 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
451 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
452 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
453 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
454 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
455 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
456 }
457
458 {
459 // Setting and getting quadpoints at out-of-bound index should fail
460 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
461 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
462 }
463
464 FPDFPage_CloseAnnot(annot);
465
466 // Retrieve the square annotation
467 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
468
469 {
470 // Check that attempting to set its quadpoints would fail
471 ASSERT_TRUE(squareAnnot);
472 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
473 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
474 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
475 }
476
477 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000478 UnloadPage(page);
479}
480
Lei Zhangab41f252018-12-23 03:10:50 +0000481TEST_F(FPDFAnnotEmbedderTest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400482#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +0000483 static const char kMd5Original[] = "63af8432fab95a67cdebb7cd0e514941";
484 static const char kMd5ModifiedHighlight[] =
485 "aec26075011349dec9bace891856b5f2";
486 static const char kMd5ModifiedSquare[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400487#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
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#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000493 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
494 static const char kMd5ModifiedHighlight[] =
495 "66f3caef3a7d488a4fa1ad37fc06310e";
496 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400497#endif
498
Jane Liu06462752017-06-27 16:41:14 -0400499 // Open a file with four annotations and load its first page.
500 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000501 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400502 ASSERT_TRUE(page);
503 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
504
Jane Liub370e5a2017-08-16 13:24:58 -0400505 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000506 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000507 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000508 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000509 }
Jane Liub370e5a2017-08-16 13:24:58 -0400510
Jane Liu0c6b07d2017-08-15 10:50:22 -0400511 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400512 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000513
514 // Retrieve the highlight annotation which has its AP stream already defined.
515 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000516 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000517 ASSERT_TRUE(annot);
518 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
519
520 // Check that color cannot be set when an AP stream is defined already.
521 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
522 102, 153, 204));
523
524 // Verify its attachment points.
525 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000526 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000527 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
528 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
529 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
530 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
531
532 // Check that updating the attachment points would succeed.
533 quadpoints.x1 -= 50.f;
534 quadpoints.x2 -= 50.f;
535 quadpoints.x3 -= 50.f;
536 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000537 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000538 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000539 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000540 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
541 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
542 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
543 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
544
545 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000546 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000547 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000548 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000549 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000550
551 // Verify its annotation rectangle.
552 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
553 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
554 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
555 EXPECT_NEAR(136.325f, rect.right, 0.001f);
556 EXPECT_NEAR(721.292f, rect.top, 0.001f);
557
558 // Check that updating the rectangle would succeed.
559 rect.left -= 60.f;
560 rect.right -= 60.f;
561 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
562 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
563 EXPECT_EQ(rect.right, new_rect.right);
564 }
Jane Liu06462752017-06-27 16:41:14 -0400565
Jane Liub370e5a2017-08-16 13:24:58 -0400566 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000567 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000568 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000569 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000570 }
Jane Liub370e5a2017-08-16 13:24:58 -0400571
Lei Zhanga21d5932018-02-05 18:28:38 +0000572 {
573 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000574 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000575 ASSERT_TRUE(annot);
576 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400577
Lei Zhanga21d5932018-02-05 18:28:38 +0000578 // Check that updating the rectangle would succeed.
579 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
580 rect.left += 70.f;
581 rect.right += 70.f;
582 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
583 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
584 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400585
Lei Zhanga21d5932018-02-05 18:28:38 +0000586 // Check that updating the rectangle changes the square annotation's
587 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000588 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000589 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000590 }
Jane Liub370e5a2017-08-16 13:24:58 -0400591
Jane Liu06462752017-06-27 16:41:14 -0400592 UnloadPage(page);
593}
Jane Liu8ce58f52017-06-29 13:40:22 -0400594
Lei Zhangab41f252018-12-23 03:10:50 +0000595TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000596 // Open a file with multiline markup annotations.
597 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
598 FPDF_PAGE page = LoadPage(0);
599 ASSERT_TRUE(page);
600 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000601 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000602 ASSERT_TRUE(annot);
603
604 // This is a three line annotation.
605 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
606 }
607 UnloadPage(page);
608
609 // null annotation should return 0
610 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
611}
612
Lei Zhangab41f252018-12-23 03:10:50 +0000613TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400614 // Open a file with 3 annotations on its first page.
615 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000616 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400617 ASSERT_TRUE(page);
618 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
619
Jane Liu0c6b07d2017-08-15 10:50:22 -0400620 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400621
Lei Zhanga21d5932018-02-05 18:28:38 +0000622 // Check that the annotations have the expected rectangle coordinates.
623 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000624 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000625 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
626 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
627 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400628
Lei Zhanga21d5932018-02-05 18:28:38 +0000629 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000630 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000631 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
632 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
633 }
634
635 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000636 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000637 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
638 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
639 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400640
641 // Check that nothing happens when attempting to remove an annotation with an
642 // out-of-bound index.
643 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
644 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
645 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
646
647 // Remove the second annotation.
648 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
649 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
650 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
651
652 // Save the document, closing the page and document.
653 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000654 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400655
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400656 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400657 // Open the saved document.
658 std::string new_file = GetString();
659 FPDF_FILEACCESS file_access;
660 memset(&file_access, 0, sizeof(file_access));
661 file_access.m_FileLen = new_file.size();
662 file_access.m_GetBlock = GetBlockFromString;
663 file_access.m_Param = &new_file;
664 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
665 ASSERT_TRUE(new_doc);
666 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
667 ASSERT_TRUE(new_page);
668
669 // Check that the saved document has 2 annotations on the first page.
670 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
671
Lei Zhanga21d5932018-02-05 18:28:38 +0000672 // Check that the remaining 2 annotations are the original 1st and 3rd ones
673 // by verifying their rectangle coordinates.
674 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000675 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000676 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
677 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
678 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400679
Lei Zhanga21d5932018-02-05 18:28:38 +0000680 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000681 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000682 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
683 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
684 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400685 FPDF_ClosePage(new_page);
686 FPDF_CloseDocument(new_doc);
687}
Jane Liu8ce58f52017-06-29 13:40:22 -0400688
Lei Zhangab41f252018-12-23 03:10:50 +0000689TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400690#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +0000691 static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2";
692 static const char kMd5ModifiedPath[] = "9059723a045e17478753d2f0eb33bc03";
693 static const char kMd5TwoPaths[] = "7eed0cfba780f1d4dd8068f717d3a6bf";
694 static const char kMd5NewAnnot[] = "1de8212d43b7066a6df042095c2aca61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000695#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +0000696 static const char kMd5Original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
697 static const char kMd5ModifiedPath[] = "c0c2eb2aba73ad15b3240e342fbe0d72";
698 static const char kMd5TwoPaths[] = "2306bf04915fe001b5f4726843d184c8";
699 static const char kMd5NewAnnot[] = "64a319f145768cb09944d2109efe394e";
Jane Liubaa7ff42017-06-29 19:18:23 -0400700#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000701 static const char kMd5Original[] = "964f89bbe8911e540a465cf1a64b7f7e";
702 static const char kMd5ModifiedPath[] = "9a38048fb3ac1b2c9a4b34139caa993c";
703 static const char kMd5TwoPaths[] = "ece3d4df54b3395d6a2bf7a29b17239c";
704 static const char kMd5NewAnnot[] = "4299493de84c249f42f220f30f2bbb67";
Jane Liubaa7ff42017-06-29 19:18:23 -0400705#endif
706
707 // Open a file with two annotations and load its first page.
708 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000709 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400710 ASSERT_TRUE(page);
711 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
712
713 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000714 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000715 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000716 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000717 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400718
Lei Zhanga21d5932018-02-05 18:28:38 +0000719 {
720 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000721 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000722 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400723
Lei Zhanga21d5932018-02-05 18:28:38 +0000724 // Check that this annotation has one path object and retrieve it.
725 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000726 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000727 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
728 EXPECT_FALSE(path);
729 path = FPDFAnnot_GetObject(annot.get(), 0);
730 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
731 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400732
Lei Zhanga21d5932018-02-05 18:28:38 +0000733 // Modify the color of the path object.
734 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
735 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400736
Lei Zhanga21d5932018-02-05 18:28:38 +0000737 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000738 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000739 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000740 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000741 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400742
Lei Zhanga21d5932018-02-05 18:28:38 +0000743 // Add a second path object to the same annotation.
744 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
745 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
746 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
747 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
748 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
749 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
750 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400751
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000752 // The object is in the annontation, not in the page, so the page object
753 // array should not change.
754 ASSERT_EQ(32, FPDFPage_CountObjects(page));
755
Lei Zhanga21d5932018-02-05 18:28:38 +0000756 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000757 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000758 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000759 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000760 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400761
Lei Zhanga21d5932018-02-05 18:28:38 +0000762 // Delete the newly added path object.
763 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
764 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000765 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000766 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400767
768 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000769 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000770 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000771 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000772 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400773
Jane Liubaa7ff42017-06-29 19:18:23 -0400774 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400775
Lei Zhanga21d5932018-02-05 18:28:38 +0000776 {
777 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000778 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000779 ASSERT_TRUE(annot);
780 rect.left = 200.f;
781 rect.bottom = 400.f;
782 rect.right = 500.f;
783 rect.top = 600.f;
784 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400785
Lei Zhanga21d5932018-02-05 18:28:38 +0000786 // Add a new path to the annotation.
787 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
788 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
789 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
790 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
791 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
792 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
793 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
794 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
795 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
796 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
797
798 // Check that the annotation's bounding box came from its rectangle.
799 FS_RECTF new_rect;
800 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
801 EXPECT_EQ(rect.left, new_rect.left);
802 EXPECT_EQ(rect.bottom, new_rect.bottom);
803 EXPECT_EQ(rect.right, new_rect.right);
804 EXPECT_EQ(rect.top, new_rect.top);
805 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400806
807 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400808 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000809 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400810
811 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +0000812 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000813 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000814 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400815
Jane Liu36567742017-07-06 11:13:35 -0400816 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000817 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400818
Lei Zhanga21d5932018-02-05 18:28:38 +0000819 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000820 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000821 ASSERT_TRUE(annot);
822 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400823
Lei Zhanga21d5932018-02-05 18:28:38 +0000824 // Check that the new annotation's rectangle is as defined.
825 FS_RECTF new_rect;
826 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
827 EXPECT_EQ(rect.left, new_rect.left);
828 EXPECT_EQ(rect.bottom, new_rect.bottom);
829 EXPECT_EQ(rect.right, new_rect.right);
830 EXPECT_EQ(rect.top, new_rect.top);
831 }
832
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000833 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400834 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400835}
Jane Liub137e752017-07-05 15:04:33 -0400836
Lei Zhangab41f252018-12-23 03:10:50 +0000837TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400838 // Open a file with an annotation and load its first page.
839 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000840 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400841 ASSERT_TRUE(page);
842
843 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000844 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000845 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000846 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
847 }
Jane Liub137e752017-07-05 15:04:33 -0400848
Lei Zhanga21d5932018-02-05 18:28:38 +0000849 {
850 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000851 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000852 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400853
Lei Zhanga21d5932018-02-05 18:28:38 +0000854 // Check that the original flag values are as expected.
855 int flags = FPDFAnnot_GetFlags(annot.get());
856 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
857 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400858
Lei Zhanga21d5932018-02-05 18:28:38 +0000859 // Set the HIDDEN flag.
860 flags |= FPDF_ANNOT_FLAG_HIDDEN;
861 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
862 flags = FPDFAnnot_GetFlags(annot.get());
863 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
864 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400865
Lei Zhanga21d5932018-02-05 18:28:38 +0000866 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000867 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000868 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000869 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
870 }
Jane Liub137e752017-07-05 15:04:33 -0400871
Lei Zhanga21d5932018-02-05 18:28:38 +0000872 // Unset the HIDDEN flag.
873 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
874 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
875 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
876 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
877 flags = FPDFAnnot_GetFlags(annot.get());
878 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
879 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400880
Lei Zhanga21d5932018-02-05 18:28:38 +0000881 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000882 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000883 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000884 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
885 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000886 }
Jane Liub137e752017-07-05 15:04:33 -0400887
Jane Liub137e752017-07-05 15:04:33 -0400888 UnloadPage(page);
889}
Jane Liu36567742017-07-06 11:13:35 -0400890
Lei Zhangab41f252018-12-23 03:10:50 +0000891TEST_F(FPDFAnnotEmbedderTest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400892#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +0000893 static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2";
894 static const char kMd5NewImage[] = "ff012f5697436dfcaec25b32d1333596";
895 static const char kMd5ModifiedImage[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000896#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +0000897 static const char kMd5Original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
898 static const char kMd5NewImage[] = "d19c6fcfd9a170802fcfb9adfa13557e";
899 static const char kMd5ModifiedImage[] = "1273cf2363570a50d1aa0c95b1318197";
Jane Liu36567742017-07-06 11:13:35 -0400900#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000901 static const char kMd5Original[] = "964f89bbe8911e540a465cf1a64b7f7e";
902 static const char kMd5NewImage[] = "9ea8732dc9d579f68853f16892856208";
903 static const char kMd5ModifiedImage[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400904#endif
905
906 // Open a file with two annotations and load its first page.
907 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000908 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400909 ASSERT_TRUE(page);
910 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
911
912 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000913 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000914 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000915 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000916 }
Jane Liu36567742017-07-06 11:13:35 -0400917
Jane Liu36567742017-07-06 11:13:35 -0400918 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000919 FPDF_BITMAP image_bitmap;
920
921 {
922 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000923 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000924 ASSERT_TRUE(annot);
925 FS_RECTF rect;
926 rect.left = 200.f;
927 rect.bottom = 600.f;
928 rect.right = 400.f;
929 rect.top = 800.f;
930 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
931
932 // Add a solid-color translucent image object to the new annotation.
933 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
934 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
935 0xeeeecccc);
936 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
937 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
938 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
939 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
940 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
941 kBitmapSize, 0, 0));
942 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
943 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
944 }
Jane Liu36567742017-07-06 11:13:35 -0400945
946 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000947 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000948 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000949 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000950 }
Jane Liu36567742017-07-06 11:13:35 -0400951
Lei Zhanga21d5932018-02-05 18:28:38 +0000952 {
953 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000954 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000955 ASSERT_TRUE(annot);
956 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
957 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
958 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400959
Lei Zhanga21d5932018-02-05 18:28:38 +0000960 // Modify the image in the new annotation.
961 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
962 0xff000000);
963 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
964 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
965 }
Jane Liu36567742017-07-06 11:13:35 -0400966
967 // Save the document, closing the page and document.
968 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000969 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400970 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400971
972 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +0000973 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -0400974}
975
Lei Zhangab41f252018-12-23 03:10:50 +0000976TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400977#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +0000978 static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2";
979 static const char kMd5NewText[] = "60031c1b0330cf1e1575f7d46687d429";
980 static const char kMd5ModifiedText[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000981#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +0000982 static const char kMd5Original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
983 static const char kMd5NewText[] = "87d55e09f9096de7e6552f5ae79afd3b";
984 static const char kMd5ModifiedText[] = "26e94fbd3af4b1e65479327507600114";
Jane Liu36567742017-07-06 11:13:35 -0400985#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000986 static const char kMd5Original[] = "964f89bbe8911e540a465cf1a64b7f7e";
987 static const char kMd5NewText[] = "30f3f5b989612ca03827d95f184f0979";
988 static const char kMd5ModifiedText[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400989#endif
990
991 // Open a file with two annotations and load its first page.
992 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000993 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400994 ASSERT_TRUE(page);
995 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
996
997 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000998 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000999 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001000 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001001 }
Jane Liu36567742017-07-06 11:13:35 -04001002
Lei Zhanga21d5932018-02-05 18:28:38 +00001003 {
1004 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001005 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001006 ASSERT_TRUE(annot);
1007 FS_RECTF rect;
1008 rect.left = 200.f;
1009 rect.bottom = 550.f;
1010 rect.right = 450.f;
1011 rect.top = 650.f;
1012 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001013
Lei Zhanga21d5932018-02-05 18:28:38 +00001014 // Add a translucent text object to the new annotation.
1015 FPDF_PAGEOBJECT text_object =
1016 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1017 EXPECT_TRUE(text_object);
Lei Zhangf0f67682019-04-08 17:03:21 +00001018 ScopedFPDFWideString text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001019 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1020 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1021 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
1022 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1023 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1024 }
Jane Liu36567742017-07-06 11:13:35 -04001025
1026 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001027 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001028 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001029 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001030 }
Jane Liu36567742017-07-06 11:13:35 -04001031
Lei Zhanga21d5932018-02-05 18:28:38 +00001032 {
1033 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001034 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001035 ASSERT_TRUE(annot);
1036 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1037 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1038 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001039
Lei Zhanga21d5932018-02-05 18:28:38 +00001040 // Modify the text in the new annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001041 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
Lei Zhanga21d5932018-02-05 18:28:38 +00001042 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1043 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1044 }
Jane Liu36567742017-07-06 11:13:35 -04001045
1046 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001047 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001048 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001049 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001050 }
Jane Liu36567742017-07-06 11:13:35 -04001051
1052 // Remove the new annotation, and check that the page renders as before.
1053 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001054 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001055 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001056 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001057 }
Jane Liu36567742017-07-06 11:13:35 -04001058
1059 UnloadPage(page);
1060}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001061
Lei Zhangab41f252018-12-23 03:10:50 +00001062TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001063 // Open a file with four annotations and load its first page.
1064 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001065 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001066 ASSERT_TRUE(page);
1067
Lei Zhang4f556b82019-04-08 16:32:41 +00001068 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001069
Lei Zhanga21d5932018-02-05 18:28:38 +00001070 {
1071 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001072 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001073 ASSERT_TRUE(annot);
1074
1075 // Check that a non-existent key does not exist.
1076 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1077
1078 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001079 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001080 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001081 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1082 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1083 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001084
1085 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001086 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001087 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1088 unsigned long len =
1089 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1090 std::vector<char> buf(len);
1091 EXPECT_EQ(66u,
1092 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1093 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1094 BufferToWString(buf).c_str());
1095
1096 // Check that the string value of the modified date is correct.
1097 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001098 len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, nullptr,
1099 0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001100 buf.clear();
1101 buf.resize(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001102 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1103 buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +00001104 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1105
1106 // Update the date entry for the annotation.
Lei Zhangf0f67682019-04-08 17:03:21 +00001107 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001108 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1109 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001110 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001111
1112 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001113 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001114 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001115
Dan Sinclair698aed72017-09-26 16:24:49 -04001116#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +00001117 static const char kMd5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001118#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +00001119 static const char kMd5[] = "9ee141f698c3fcb56c050dffd6c82624";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001120#else
Lei Zhang4f556b82019-04-08 16:32:41 +00001121 static const char kMd5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001122#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001123
1124 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001125 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001126 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001127 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001128 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001129 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001130
Lei Zhanga21d5932018-02-05 18:28:38 +00001131 // Check that the string value of the modified date is the newly-set value.
1132 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001133 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
1134 unsigned long len = FPDFAnnot_GetStringValue(
1135 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001136 std::vector<char> buf(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001137 EXPECT_EQ(44u,
1138 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
1139 buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +00001140 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1141 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001142
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001143 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001144 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001145}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001146
rycsmith3e785602019-03-05 21:48:36 +00001147TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
1148 // Open a file with three text annotations and load its first page.
1149 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1150 FPDF_PAGE page = LoadPage(0);
1151 ASSERT_TRUE(page);
1152 {
1153 // First two annotations do not have "MaxLen" attribute.
1154 for (int i = 0; i < 2; i++) {
1155 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1156 ASSERT_TRUE(annot);
1157
1158 // Verify that no "MaxLen" key present.
1159 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1160
1161 float value;
1162 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1163 }
1164
1165 // Annotation in index 2 has "MaxLen" of 10.
1166 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1167 ASSERT_TRUE(annot);
1168
1169 // Verify that "MaxLen" key present.
1170 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1171
1172 float value;
1173 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1174 EXPECT_FLOAT_EQ(10.0f, value);
1175
1176 // Check bad inputs.
1177 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1178 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1179 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1180 // Ask for key that exists but is not a number.
1181 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1182 }
1183
1184 UnloadPage(page);
1185}
1186
Lei Zhangab41f252018-12-23 03:10:50 +00001187TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001188 // Open a file with four annotations and load its first page.
1189 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001190 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001191 ASSERT_TRUE(page);
1192
Lei Zhanga21d5932018-02-05 18:28:38 +00001193 {
1194 // 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.
1199 unsigned long normal_len = FPDFAnnot_GetAP(
1200 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1201 EXPECT_EQ(73970u, normal_len);
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.
1205 std::vector<char> buf(normal_len - 1);
1206 // Write L"z" in the buffer to verify it's not overwritten.
1207 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1208 EXPECT_EQ(73970u,
1209 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1210 buf.data(), buf.size()));
1211 std::string ap = BufferToString(buf);
1212 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001213
Lei Zhanga21d5932018-02-05 18:28:38 +00001214 // Check that the string value of an AP is returned through a buffer that is
1215 // the right size.
1216 buf.clear();
1217 buf.resize(normal_len);
1218 EXPECT_EQ(73970u,
1219 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1220 buf.data(), buf.size()));
1221 ap = BufferToString(buf);
1222 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1223 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001224
Lei Zhanga21d5932018-02-05 18:28:38 +00001225 // Check that the string value of an AP is returned through a buffer that is
1226 // larger than necessary.
1227 buf.clear();
1228 buf.resize(normal_len + 1);
1229 EXPECT_EQ(73970u,
1230 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1231 buf.data(), buf.size()));
1232 ap = BufferToString(buf);
1233 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1234 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001235
Lei Zhanga21d5932018-02-05 18:28:38 +00001236 // Check that getting an AP for a mode that does not have an AP returns an
1237 // empty string.
1238 unsigned long rollover_len = FPDFAnnot_GetAP(
1239 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1240 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001241
Lei Zhanga21d5932018-02-05 18:28:38 +00001242 buf.clear();
1243 buf.resize(1000);
1244 EXPECT_EQ(2u,
1245 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1246 buf.data(), buf.size()));
1247 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001248
Lei Zhanga21d5932018-02-05 18:28:38 +00001249 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhangf0f67682019-04-08 17:03:21 +00001250 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001251 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001252 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001253 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001254 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001255 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001256
Lei Zhanga21d5932018-02-05 18:28:38 +00001257 // Set the AP correctly now.
1258 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001259 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001260
Lei Zhanga21d5932018-02-05 18:28:38 +00001261 // Check that the new annotation value is equal to the value we just set.
1262 rollover_len = FPDFAnnot_GetAP(
1263 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1264 EXPECT_EQ(24u, rollover_len);
1265 buf.clear();
1266 buf.resize(rollover_len);
1267 EXPECT_EQ(24u,
1268 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1269 buf.data(), buf.size()));
1270 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001271
Lei Zhanga21d5932018-02-05 18:28:38 +00001272 // Check that the Normal AP was not touched when the Rollover AP was set.
1273 buf.clear();
1274 buf.resize(normal_len);
1275 EXPECT_EQ(73970u,
1276 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1277 buf.data(), buf.size()));
1278 ap = BufferToString(buf);
1279 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1280 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1281 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001282
1283 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001284 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001285 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001286
Lei Zhang0b494052019-01-31 21:41:15 +00001287 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001288 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001289 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001290 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001291
Lei Zhanga21d5932018-02-05 18:28:38 +00001292 // Check that the new annotation value is equal to the value we set before
1293 // saving.
1294 unsigned long rollover_len = FPDFAnnot_GetAP(
1295 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1296 EXPECT_EQ(24u, rollover_len);
1297 std::vector<char> buf(rollover_len);
1298 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1299 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1300 buf.data(), buf.size()));
1301 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1302 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001303
1304 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001305 CloseSavedPage(page);
1306 CloseSavedDocument();
1307}
1308
Lei Zhangab41f252018-12-23 03:10:50 +00001309TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001310 // Open a file with four annotations and load its first page.
1311 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001312 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001313 ASSERT_TRUE(page);
1314
Lei Zhanga21d5932018-02-05 18:28:38 +00001315 {
1316 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001317 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001318 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001319
Lei Zhanga21d5932018-02-05 18:28:38 +00001320 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001321 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001322 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001323 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001324 EXPECT_EQ(73970u,
1325 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1326 nullptr, 0));
1327 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1328 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001329
Lei Zhanga21d5932018-02-05 18:28:38 +00001330 // Check that setting the Down AP to null removes the Down entry but keeps
1331 // Normal intact.
1332 EXPECT_TRUE(
1333 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1334 EXPECT_EQ(73970u,
1335 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1336 nullptr, 0));
1337 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1338 nullptr, 0));
1339 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001340
Lei Zhang75c81712018-02-08 17:22:39 +00001341 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001342}
1343
Lei Zhangab41f252018-12-23 03:10:50 +00001344TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001345 // Open a file with four annotations and load its first page.
1346 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001347 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001348 ASSERT_TRUE(page);
1349
Lei Zhanga21d5932018-02-05 18:28:38 +00001350 {
1351 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001352 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001353 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001354
Lei Zhanga21d5932018-02-05 18:28:38 +00001355 // Set Down AP. Normal AP is already set.
Lei Zhangf0f67682019-04-08 17:03:21 +00001356 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
Lei Zhanga21d5932018-02-05 18:28:38 +00001357 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001358 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001359 EXPECT_EQ(73970u,
1360 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1361 nullptr, 0));
1362 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1363 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001364
Lei Zhanga21d5932018-02-05 18:28:38 +00001365 // Check that setting the Normal AP to null removes the whole AP dictionary.
1366 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1367 nullptr));
1368 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1369 nullptr, 0));
1370 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1371 nullptr, 0));
1372 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001373
Lei Zhang75c81712018-02-08 17:22:39 +00001374 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001375}
1376
Lei Zhangab41f252018-12-23 03:10:50 +00001377TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001378 // Open a file with annotations and load its first page.
1379 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001380 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001381 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001382 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001383
Lei Zhanga21d5932018-02-05 18:28:38 +00001384 {
1385 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001386 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001387 ASSERT_TRUE(annot);
1388 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1389 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001390 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001391 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1392 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1393 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001394
Lei Zhanga21d5932018-02-05 18:28:38 +00001395 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001396 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001397 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1398 ASSERT_TRUE(popup);
1399 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1400 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1401 FS_RECTF rect;
1402 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1403 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1404 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001405
Lei Zhanga21d5932018-02-05 18:28:38 +00001406 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1407 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001408 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001409 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1410 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001411
Lei Zhanga21d5932018-02-05 18:28:38 +00001412 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1413 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001414 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001415 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001416 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1417 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001418 }
Jane Liu300bb272017-08-21 14:37:53 -04001419
Jane Liu300bb272017-08-21 14:37:53 -04001420 UnloadPage(page);
1421}
1422
Lei Zhangab41f252018-12-23 03:10:50 +00001423TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001424 // Open file with form text fields.
1425 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001426 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001427 ASSERT_TRUE(page);
1428
Lei Zhanga21d5932018-02-05 18:28:38 +00001429 {
1430 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001431 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001432 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001433
Lei Zhanga21d5932018-02-05 18:28:38 +00001434 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001435 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001436 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1437 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001438
Lei Zhanga21d5932018-02-05 18:28:38 +00001439 {
1440 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001441 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001442 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001443
Lei Zhanga21d5932018-02-05 18:28:38 +00001444 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001445 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001446 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1447 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001448
1449 UnloadPage(page);
1450}
1451
Lei Zhangab41f252018-12-23 03:10:50 +00001452TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001453 // Open file with form text fields.
1454 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001455 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001456 ASSERT_TRUE(page);
1457
Lei Zhanga21d5932018-02-05 18:28:38 +00001458 {
1459 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001460 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001461 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001462
Lei Zhanga21d5932018-02-05 18:28:38 +00001463 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001464 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001465 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1466 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1467 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1468 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001469
Lei Zhanga21d5932018-02-05 18:28:38 +00001470 {
1471 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001472 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001473 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001474
Lei Zhanga21d5932018-02-05 18:28:38 +00001475 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001476 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001477 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1478 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1479 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1480 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001481
Lei Zhanga21d5932018-02-05 18:28:38 +00001482 {
1483 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001484 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001485 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001486
Lei Zhanga21d5932018-02-05 18:28:38 +00001487 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001488 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001489 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1490 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1491 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1492 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001493
1494 UnloadPage(page);
1495}
Diana Gage40870db2017-07-19 18:16:03 -07001496
Lei Zhangab41f252018-12-23 03:10:50 +00001497TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001498 // Open file with form text fields.
1499 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1500 FPDF_PAGE page = LoadPage(0);
1501 ASSERT_TRUE(page);
1502
1503 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang7557e7b2018-09-14 17:02:40 +00001504 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0));
1505
1506 {
1507 // Verify there is an annotation.
1508 ScopedFPDFAnnotation annot(
1509 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120));
1510 EXPECT_TRUE(annot);
1511 }
1512
1513 // Try other bad inputs at a valid location.
1514 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120));
1515 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120));
1516 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120));
Diana Gage40870db2017-07-19 18:16:03 -07001517
1518 UnloadPage(page);
1519}
1520
Lei Zhangab41f252018-12-23 03:10:50 +00001521TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001522 // Open file with form text fields.
1523 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1524 FPDF_PAGE page = LoadPage(0);
1525 ASSERT_TRUE(page);
1526
Lei Zhanga21d5932018-02-05 18:28:38 +00001527 {
1528 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001529 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001530 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1531 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001532
Lei Zhanga21d5932018-02-05 18:28:38 +00001533 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001534 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001535 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1536 }
Diana Gage40870db2017-07-19 18:16:03 -07001537
Lei Zhanga21d5932018-02-05 18:28:38 +00001538 {
1539 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001540 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001541 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1542 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001543
Lei Zhanga21d5932018-02-05 18:28:38 +00001544 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001545 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001546 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1547 }
Diana Gage40870db2017-07-19 18:16:03 -07001548
1549 UnloadPage(page);
1550}
1551
Lei Zhangab41f252018-12-23 03:10:50 +00001552TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001553 // Open file with form comboboxes.
1554 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1555 FPDF_PAGE page = LoadPage(0);
1556 ASSERT_TRUE(page);
1557
Lei Zhanga21d5932018-02-05 18:28:38 +00001558 {
1559 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001560 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001561 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001562 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001563
Lei Zhanga21d5932018-02-05 18:28:38 +00001564 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001565 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001566 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1567 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1568 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1569 }
Diana Gage40870db2017-07-19 18:16:03 -07001570
Lei Zhanga21d5932018-02-05 18:28:38 +00001571 {
1572 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001573 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001574 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001575 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001576
Lei Zhanga21d5932018-02-05 18:28:38 +00001577 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001578 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001579 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1580 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1581 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1582 }
Diana Gage40870db2017-07-19 18:16:03 -07001583
Lei Zhanga21d5932018-02-05 18:28:38 +00001584 {
1585 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001586 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001587 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001588 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001589
Lei Zhanga21d5932018-02-05 18:28:38 +00001590 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001591 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001592 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1593 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1594 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1595 }
Diana Gage40870db2017-07-19 18:16:03 -07001596
1597 UnloadPage(page);
1598}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001599
Lei Zhang992e7e22019-02-04 19:20:58 +00001600TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
1601 static constexpr size_t kExpectedSize = 1609;
1602 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1603
1604 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1605
1606 FPDF_PAGE page = LoadPage(0);
1607 ASSERT_TRUE(page);
1608
1609 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1610 EXPECT_EQ(kExpectedSize, GetString().size());
1611 ClearString();
1612
1613 for (size_t i = 0; i < 10; ++i) {
1614 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1615 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
1616
1617 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1618 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
1619 // equal, not bigger.
1620 EXPECT_LT(kExpectedSize, GetString().size());
1621 ClearString();
1622 }
1623
1624 UnloadPage(page);
1625}
1626
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001627TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1628 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1629 FPDF_PAGE page = LoadPage(0);
1630 ASSERT_TRUE(page);
1631 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1632
1633 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00001634 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001635 std::vector<char> buf(12);
1636
1637 {
1638 // Add a text annotation to the page.
1639 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1640 ASSERT_TRUE(annot);
1641 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1642 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1643
1644 // Make sure there is no test key, add set a value there, and read it back.
1645 std::fill(buf.begin(), buf.end(), 'x');
1646 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1647 buf.size()));
1648 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1649
Lei Zhangf0f67682019-04-08 17:03:21 +00001650 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001651 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1652
1653 std::fill(buf.begin(), buf.end(), 'x');
1654 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1655 buf.size()));
1656 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1657 }
1658
Lei Zhang05ec64c2019-01-09 03:00:06 +00001659 {
1660 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1661 ASSERT_TRUE(annot);
1662 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1663 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1664 // Also do the same test for its appearance string.
1665 std::fill(buf.begin(), buf.end(), 'x');
1666 ASSERT_EQ(2u,
1667 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1668 buf.data(), buf.size()));
1669 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1670
Lei Zhangf0f67682019-04-08 17:03:21 +00001671 ScopedFPDFWideString text = GetFPDFWideString(kData);
Lei Zhang05ec64c2019-01-09 03:00:06 +00001672 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1673 text.get()));
1674
1675 std::fill(buf.begin(), buf.end(), 'x');
1676 ASSERT_EQ(6u,
1677 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1678 buf.data(), buf.size()));
1679 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1680 }
1681
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001682 UnloadPage(page);
1683
1684 {
1685 // Save a copy, open the copy, and check the annotation again.
1686 // Note that it renders the rotation.
1687 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001688 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001689 FPDF_PAGE saved_page = LoadSavedPage(0);
1690 ASSERT_TRUE(saved_page);
1691
Lei Zhang05ec64c2019-01-09 03:00:06 +00001692 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001693 {
1694 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1695 ASSERT_TRUE(annot);
1696 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1697
1698 std::fill(buf.begin(), buf.end(), 'x');
1699 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1700 buf.size()));
1701 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1702 }
1703
Lei Zhang05ec64c2019-01-09 03:00:06 +00001704 {
1705 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1706 ASSERT_TRUE(annot);
1707 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1708 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1709
1710 std::fill(buf.begin(), buf.end(), 'x');
1711 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1712 buf.size()));
1713 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1714 }
1715
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001716 CloseSavedPage(saved_page);
1717 CloseSavedDocument();
1718 }
1719}
rycsmithcb752f32019-02-21 18:40:53 +00001720
1721TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
1722 // Open a file with combobox widget annotations and load its first page.
1723 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1724 FPDF_PAGE page = LoadPage(0);
1725 ASSERT_TRUE(page);
1726
1727 {
1728 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1729 ASSERT_TRUE(annot);
1730
1731 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1732
1733 annot.reset(FPDFPage_GetAnnot(page, 1));
1734 ASSERT_TRUE(annot);
1735
1736 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00001737
1738 // Check bad form handle / annot.
1739 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
1740 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
1741 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00001742 }
1743
1744 UnloadPage(page);
1745}
1746
1747TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
1748 // Open a file with listbox widget annotations and load its first page.
1749 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1750 FPDF_PAGE page = LoadPage(0);
1751 ASSERT_TRUE(page);
1752
1753 {
1754 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1755 ASSERT_TRUE(annot);
1756
1757 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1758
1759 annot.reset(FPDFPage_GetAnnot(page, 1));
1760 ASSERT_TRUE(annot);
1761
1762 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1763 }
1764
1765 UnloadPage(page);
1766}
1767
1768TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
1769 // Open a file with ink annotations and load its first page.
1770 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1771 FPDF_PAGE page = LoadPage(0);
1772 ASSERT_TRUE(page);
1773
1774 {
1775 // annotations do not have "Opt" array and will return -1
1776 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1777 ASSERT_TRUE(annot);
1778
1779 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1780
1781 annot.reset(FPDFPage_GetAnnot(page, 1));
1782 ASSERT_TRUE(annot);
1783
1784 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1785 }
1786
1787 UnloadPage(page);
1788}
1789
1790TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
1791 // Open a file with combobox widget annotations and load its first page.
1792 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1793 FPDF_PAGE page = LoadPage(0);
1794 ASSERT_TRUE(page);
1795
1796 {
1797 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1798 ASSERT_TRUE(annot);
1799
1800 int index = 0;
1801 unsigned long len =
1802 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1803 std::vector<char> buf(len);
1804 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1805 buf.data(), len));
1806 EXPECT_STREQ(L"Foo", BufferToWString(buf).c_str());
1807
1808 annot.reset(FPDFPage_GetAnnot(page, 1));
1809 ASSERT_TRUE(annot);
1810
1811 index = 0;
1812 len =
1813 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1814 buf.clear();
1815 buf.resize(len);
1816 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1817 buf.data(), len));
1818 EXPECT_STREQ(L"Apple", BufferToWString(buf).c_str());
1819
1820 index = 25;
1821 len =
1822 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1823 buf.clear();
1824 buf.resize(len);
1825 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1826 buf.data(), len));
1827 EXPECT_STREQ(L"Zucchini", BufferToWString(buf).c_str());
1828
Lei Zhange7033c82019-02-26 19:30:49 +00001829 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00001830 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1831 nullptr, 0));
1832 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1833 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00001834
1835 // Check bad form handle / annot.
1836 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
1837 EXPECT_EQ(0u,
1838 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
1839 EXPECT_EQ(0u,
1840 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00001841 }
1842
1843 UnloadPage(page);
1844}
1845
1846TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
1847 // Open a file with listbox widget annotations and load its first page.
1848 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1849 FPDF_PAGE page = LoadPage(0);
1850 ASSERT_TRUE(page);
1851
1852 {
1853 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1854 ASSERT_TRUE(annot);
1855
1856 int index = 0;
1857 unsigned long len =
1858 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1859 std::vector<char> buf(len);
1860 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1861 buf.data(), len));
1862 EXPECT_STREQ(L"Foo", BufferToWString(buf).c_str());
1863
1864 annot.reset(FPDFPage_GetAnnot(page, 1));
1865 ASSERT_TRUE(annot);
1866
1867 index = 0;
1868 len =
1869 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1870 buf.clear();
1871 buf.resize(len);
1872 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1873 buf.data(), len));
1874 EXPECT_STREQ(L"Apple", BufferToWString(buf).c_str());
1875
1876 index = 25;
1877 len =
1878 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1879 buf.clear();
1880 buf.resize(len);
1881 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1882 buf.data(), len));
1883 EXPECT_STREQ(L"Zucchini", BufferToWString(buf).c_str());
1884
1885 // indices out of range
1886 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1887 nullptr, 0));
1888 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1889 nullptr, 0));
1890 }
1891
1892 UnloadPage(page);
1893}
1894
1895TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
1896 // Open a file with ink annotations and load its first page.
1897 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1898 FPDF_PAGE page = LoadPage(0);
1899 ASSERT_TRUE(page);
1900
1901 {
1902 // annotations do not have "Opt" array and will return 0
1903 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1904 ASSERT_TRUE(annot);
1905
1906 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1907 nullptr, 0));
1908
1909 annot.reset(FPDFPage_GetAnnot(page, 1));
1910 ASSERT_TRUE(annot);
1911
1912 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1913 nullptr, 0));
1914 }
1915
1916 UnloadPage(page);
1917}