blob: f99e93a2111eb0e1e883acd2a1ba00519375ce59 [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 Zhang7557e7b2018-09-14 17:02:40 +000056 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
57 GetFPDFWideString(kContents);
58 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
59
60 char buffer[128];
61 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
62 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
63 EXPECT_EQ(0u,
64 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
65
66 UnloadPage(page);
67}
68
Lei Zhang3d9a0972019-03-04 19:34:09 +000069TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
70 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
71 FPDF_PAGE page = LoadPage(0);
72 ASSERT_TRUE(page);
73
74 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +000075 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +000076
77 UnloadPage(page);
78}
79
Lei Zhangab41f252018-12-23 03:10:50 +000080TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -040081 // Open a file with one annotation and load its first page.
82 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000083 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040084 ASSERT_TRUE(page);
85
86 // This annotation has a malformed appearance stream, which does not have its
87 // normal appearance defined, only its rollover appearance. In this case, its
88 // normal appearance should be generated, allowing the highlight annotation to
89 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +000090 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +000091 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040092
93 UnloadPage(page);
94}
95
Lei Zhangab41f252018-12-23 03:10:50 +000096TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Lei Zhang4f556b82019-04-08 16:32:41 +000097 static const char kMd5[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000098 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000099 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
100 FPDF_PAGE page = LoadPage(0);
101 ASSERT_TRUE(page);
102
Tom Sepeze08d2b12018-04-25 18:49:32 +0000103 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000104 CompareBitmap(bitmap.get(), 595, 842, kMd5);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000105
106 UnloadPage(page);
107}
108
Lei Zhangab41f252018-12-23 03:10:50 +0000109TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400110 // Open a file with one annotation and load its first page.
111 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000112 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400113 ASSERT_TRUE(page);
114
115 // Check that there is a total of 1 annotation on its first page.
116 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
117
118 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000119 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000120 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000121 ASSERT_TRUE(annot);
122 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400123
Lei Zhanga21d5932018-02-05 18:28:38 +0000124 // Check that the annotation color is yellow.
125 unsigned int R;
126 unsigned int G;
127 unsigned int B;
128 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000129 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000130 &G, &B, &A));
131 EXPECT_EQ(255u, R);
132 EXPECT_EQ(255u, G);
133 EXPECT_EQ(0u, B);
134 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400135
Lei Zhanga21d5932018-02-05 18:28:38 +0000136 // Check that the author is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +0000137 static const char kAuthorKey[] = "T";
Lei Zhanga21d5932018-02-05 18:28:38 +0000138 EXPECT_EQ(FPDF_OBJECT_STRING,
139 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
140 unsigned long len =
141 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
142 std::vector<char> buf(len);
143 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
144 len));
145 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400146
Lei Zhanga21d5932018-02-05 18:28:38 +0000147 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000148 EXPECT_EQ(
149 FPDF_OBJECT_STRING,
150 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
151 len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kContents,
152 nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +0000153 buf.clear();
154 buf.resize(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000155 EXPECT_EQ(2690u,
156 FPDFAnnot_GetStringValue(
157 annot.get(), pdfium::annotation::kContents, buf.data(), len));
Lei Zhang4f556b82019-04-08 16:32:41 +0000158 static const wchar_t kContents[] =
Lei Zhanga21d5932018-02-05 18:28:38 +0000159 L"This is a note for that highlight annotation. Very long highlight "
160 "annotation. Long long long Long 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 longLong long longLong long longLong long longLong long "
178 "longLong long long. END";
Lei Zhang4f556b82019-04-08 16:32:41 +0000179 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400180
Lei Zhanga21d5932018-02-05 18:28:38 +0000181 // Check that the quadpoints are correct.
182 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000183 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000184 EXPECT_EQ(115.802643f, quadpoints.x1);
185 EXPECT_EQ(718.913940f, quadpoints.y1);
186 EXPECT_EQ(157.211182f, quadpoints.x4);
187 EXPECT_EQ(706.264465f, quadpoints.y4);
188 }
Tom Sepez507d0192018-11-07 16:37:51 +0000189 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400190}
191
Lei Zhangab41f252018-12-23 03:10:50 +0000192TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400193 // Open a file with three annotations and load its first page.
194 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000195 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400196 ASSERT_TRUE(page);
197
198 // Check that there is a total of 3 annotation on its first page.
199 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
200
Lei Zhanga21d5932018-02-05 18:28:38 +0000201 {
202 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000203 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000204 ASSERT_TRUE(annot);
205 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400206
Lei Zhanga21d5932018-02-05 18:28:38 +0000207 // Check that the annotation color is blue with opacity.
208 unsigned int R;
209 unsigned int G;
210 unsigned int B;
211 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000212 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000213 &G, &B, &A));
214 EXPECT_EQ(0u, R);
215 EXPECT_EQ(0u, G);
216 EXPECT_EQ(255u, B);
217 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400218
Lei Zhanga21d5932018-02-05 18:28:38 +0000219 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000220 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
221 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400222
Lei Zhang4f556b82019-04-08 16:32:41 +0000223 // Check that the rectangle coordinates are correct.
Lei Zhanga21d5932018-02-05 18:28:38 +0000224 // Note that upon rendering, the rectangle coordinates will be adjusted.
225 FS_RECTF rect;
226 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
227 EXPECT_EQ(351.820404f, rect.left);
228 EXPECT_EQ(583.830688f, rect.bottom);
229 EXPECT_EQ(475.336090f, rect.right);
230 EXPECT_EQ(681.535034f, rect.top);
231 }
Tom Sepezef43c262018-11-07 16:41:32 +0000232 {
233 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
234 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
235 }
Tom Sepez507d0192018-11-07 16:37:51 +0000236 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400237}
Jane Liu20eafda2017-06-07 10:33:24 -0400238
Lei Zhangab41f252018-12-23 03:10:50 +0000239TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400240 // Open a file with one annotation and load its first page.
241 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000242 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400243 ASSERT_TRUE(page);
244
245 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400246 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400247
248 UnloadPage(page);
249}
250
Lei Zhangab41f252018-12-23 03:10:50 +0000251TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400252 // Open a file with no annotation and load its first page.
253 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000254 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400255 ASSERT_TRUE(page);
256 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
257
Lei Zhanga21d5932018-02-05 18:28:38 +0000258 {
259 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000260 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000261 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400262
Lei Zhanga21d5932018-02-05 18:28:38 +0000263 // Check that there is now 1 annotations on this page.
264 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400265
Lei Zhanga21d5932018-02-05 18:28:38 +0000266 // Check that the subtype of the annotation is correct.
267 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
268 }
Jane Liue10509a2017-06-20 16:47:41 -0400269
Lei Zhanga21d5932018-02-05 18:28:38 +0000270 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000271 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000272 ASSERT_TRUE(annot);
273 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400274
Lei Zhanga21d5932018-02-05 18:28:38 +0000275 // Set the color of the annotation.
276 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
277 102, 153, 204));
278 // Check that the color has been set correctly.
279 unsigned int R;
280 unsigned int G;
281 unsigned int B;
282 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000283 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000284 &G, &B, &A));
285 EXPECT_EQ(51u, R);
286 EXPECT_EQ(102u, G);
287 EXPECT_EQ(153u, B);
288 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400289
Lei Zhanga21d5932018-02-05 18:28:38 +0000290 // Change the color of the annotation.
291 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
292 153, 102, 51));
293 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000294 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000295 &G, &B, &A));
296 EXPECT_EQ(204u, R);
297 EXPECT_EQ(153u, G);
298 EXPECT_EQ(102u, B);
299 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400300
Lei Zhanga21d5932018-02-05 18:28:38 +0000301 // Set the annotation rectangle.
302 FS_RECTF rect;
303 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
304 EXPECT_EQ(0.f, rect.left);
305 EXPECT_EQ(0.f, rect.right);
306 rect.left = 35;
307 rect.bottom = 150;
308 rect.right = 53;
309 rect.top = 165;
310 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
311 // Check that the annotation rectangle has been set correctly.
312 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
313 EXPECT_EQ(35.f, rect.left);
314 EXPECT_EQ(150.f, rect.bottom);
315 EXPECT_EQ(53.f, rect.right);
316 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400317
Lei Zhanga21d5932018-02-05 18:28:38 +0000318 // Set the content of the annotation.
Lei Zhang4f556b82019-04-08 16:32:41 +0000319 static const wchar_t kContents[] = L"Hello! This is a customized content.";
Lei Zhanga21d5932018-02-05 18:28:38 +0000320 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
321 GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000322 ASSERT_TRUE(FPDFAnnot_SetStringValue(
323 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000324 // Check that the content has been set correctly.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000325 unsigned long len = FPDFAnnot_GetStringValue(
326 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +0000327 std::vector<char> buf(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000328 EXPECT_EQ(74u,
329 FPDFAnnot_GetStringValue(
330 annot.get(), pdfium::annotation::kContents, buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +0000331 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
332 }
Jane Liu20eafda2017-06-07 10:33:24 -0400333 UnloadPage(page);
334}
335
Lei Zhangab41f252018-12-23 03:10:50 +0000336TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400337 // Open a file with one annotation and load its first page.
338 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000339 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400340 ASSERT_TRUE(page);
341
342 // Check that there is a total of one annotation on its first page, and verify
343 // its quadpoints.
344 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400345 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000346 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000347 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000348 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000349 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000350 EXPECT_EQ(115.802643f, quadpoints.x1);
351 EXPECT_EQ(718.913940f, quadpoints.y1);
352 EXPECT_EQ(157.211182f, quadpoints.x4);
353 EXPECT_EQ(706.264465f, quadpoints.y4);
354 }
Jane Liu20eafda2017-06-07 10:33:24 -0400355
356 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000357 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000358 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000359 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
360 ASSERT_TRUE(annot);
361 quadpoints.x1 = 140.802643f;
362 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000363 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000364 }
Jane Liu20eafda2017-06-07 10:33:24 -0400365
366 // Save the document, closing the page and document.
367 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000368 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400369
370 // Open the saved document.
Lei Zhang4f556b82019-04-08 16:32:41 +0000371 static const char kMd5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400372
Lei Zhang0b494052019-01-31 21:41:15 +0000373 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000374 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000375 VerifySavedRendering(page, 612, 792, kMd5);
Jane Liu20eafda2017-06-07 10:33:24 -0400376
377 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000378 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400379
Lei Zhanga21d5932018-02-05 18:28:38 +0000380 {
381 // Check that the second annotation is an underline annotation and verify
382 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000383 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000384 ASSERT_TRUE(new_annot);
385 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
386 FS_QUADPOINTSF new_quadpoints;
387 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000388 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000389 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
390 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
391 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
392 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
393 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400394
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000395 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400396 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400397}
Jane Liu06462752017-06-27 16:41:14 -0400398
Lei Zhangab41f252018-12-23 03:10:50 +0000399TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000400 // Open a file with four annotations and load its first page.
401 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
402 FPDF_PAGE page = LoadPage(0);
403 ASSERT_TRUE(page);
404 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
405
406 // Retrieve the highlight annotation.
407 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
408 ASSERT_TRUE(annot);
409 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
410
411 FS_QUADPOINTSF quadpoints;
412 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
413
414 {
415 // Verify the current one set of quadpoints.
416 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
417
418 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
419 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
420 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
421 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
422 }
423
424 {
425 // Update the quadpoints.
426 FS_QUADPOINTSF new_quadpoints = quadpoints;
427 new_quadpoints.y1 -= 20.f;
428 new_quadpoints.y2 -= 20.f;
429 new_quadpoints.y3 -= 20.f;
430 new_quadpoints.y4 -= 20.f;
431 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
432
433 // Verify added quadpoint set
434 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
435 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
436 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
437 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
438 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
439 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
440 }
441
442 {
443 // Append a new set of quadpoints.
444 FS_QUADPOINTSF new_quadpoints = quadpoints;
445 new_quadpoints.y1 += 20.f;
446 new_quadpoints.y2 += 20.f;
447 new_quadpoints.y3 += 20.f;
448 new_quadpoints.y4 += 20.f;
449 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
450
451 // Verify added quadpoint set
452 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
453 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
454 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
455 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
456 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
457 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
458 }
459
460 {
461 // Setting and getting quadpoints at out-of-bound index should fail
462 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
463 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
464 }
465
466 FPDFPage_CloseAnnot(annot);
467
468 // Retrieve the square annotation
469 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
470
471 {
472 // Check that attempting to set its quadpoints would fail
473 ASSERT_TRUE(squareAnnot);
474 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
475 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
476 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
477 }
478
479 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000480 UnloadPage(page);
481}
482
Lei Zhangab41f252018-12-23 03:10:50 +0000483TEST_F(FPDFAnnotEmbedderTest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400484#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +0000485 static const char kMd5Original[] = "63af8432fab95a67cdebb7cd0e514941";
486 static const char kMd5ModifiedHighlight[] =
487 "aec26075011349dec9bace891856b5f2";
488 static const char kMd5ModifiedSquare[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400489#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +0000490 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
491 static const char kMd5ModifiedHighlight[] =
492 "66f3caef3a7d488a4fa1ad37fc06310e";
493 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400494#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000495 static const char kMd5Original[] = "0e27376094f11490f74c65f3dc3a42c5";
496 static const char kMd5ModifiedHighlight[] =
497 "66f3caef3a7d488a4fa1ad37fc06310e";
498 static const char kMd5ModifiedSquare[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400499#endif
500
Jane Liu06462752017-06-27 16:41:14 -0400501 // Open a file with four annotations and load its first page.
502 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000503 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400504 ASSERT_TRUE(page);
505 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
506
Jane Liub370e5a2017-08-16 13:24:58 -0400507 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000508 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000509 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000510 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000511 }
Jane Liub370e5a2017-08-16 13:24:58 -0400512
Jane Liu0c6b07d2017-08-15 10:50:22 -0400513 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400514 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000515
516 // Retrieve the highlight annotation which has its AP stream already defined.
517 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000518 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000519 ASSERT_TRUE(annot);
520 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
521
522 // Check that color cannot be set when an AP stream is defined already.
523 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
524 102, 153, 204));
525
526 // Verify its attachment points.
527 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000528 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000529 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
530 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
531 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
532 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
533
534 // Check that updating the attachment points would succeed.
535 quadpoints.x1 -= 50.f;
536 quadpoints.x2 -= 50.f;
537 quadpoints.x3 -= 50.f;
538 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000539 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000540 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000541 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000542 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
543 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
544 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
545 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
546
547 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000548 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000549 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000550 CompareBitmap(bitmap.get(), 612, 792, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000551 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000552
553 // Verify its annotation rectangle.
554 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
555 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
556 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
557 EXPECT_NEAR(136.325f, rect.right, 0.001f);
558 EXPECT_NEAR(721.292f, rect.top, 0.001f);
559
560 // Check that updating the rectangle would succeed.
561 rect.left -= 60.f;
562 rect.right -= 60.f;
563 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
564 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
565 EXPECT_EQ(rect.right, new_rect.right);
566 }
Jane Liu06462752017-06-27 16:41:14 -0400567
Jane Liub370e5a2017-08-16 13:24:58 -0400568 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000569 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000570 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000571 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedHighlight);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000572 }
Jane Liub370e5a2017-08-16 13:24:58 -0400573
Lei Zhanga21d5932018-02-05 18:28:38 +0000574 {
575 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000576 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000577 ASSERT_TRUE(annot);
578 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400579
Lei Zhanga21d5932018-02-05 18:28:38 +0000580 // Check that updating the rectangle would succeed.
581 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
582 rect.left += 70.f;
583 rect.right += 70.f;
584 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
585 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
586 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400587
Lei Zhanga21d5932018-02-05 18:28:38 +0000588 // Check that updating the rectangle changes the square annotation's
589 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000590 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000591 CompareBitmap(bitmap.get(), 612, 792, kMd5ModifiedSquare);
Lei Zhanga21d5932018-02-05 18:28:38 +0000592 }
Jane Liub370e5a2017-08-16 13:24:58 -0400593
Jane Liu06462752017-06-27 16:41:14 -0400594 UnloadPage(page);
595}
Jane Liu8ce58f52017-06-29 13:40:22 -0400596
Lei Zhangab41f252018-12-23 03:10:50 +0000597TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000598 // Open a file with multiline markup annotations.
599 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
600 FPDF_PAGE page = LoadPage(0);
601 ASSERT_TRUE(page);
602 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000603 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000604 ASSERT_TRUE(annot);
605
606 // This is a three line annotation.
607 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
608 }
609 UnloadPage(page);
610
611 // null annotation should return 0
612 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
613}
614
Lei Zhangab41f252018-12-23 03:10:50 +0000615TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400616 // Open a file with 3 annotations on its first page.
617 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000618 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400619 ASSERT_TRUE(page);
620 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
621
Jane Liu0c6b07d2017-08-15 10:50:22 -0400622 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400623
Lei Zhanga21d5932018-02-05 18:28:38 +0000624 // Check that the annotations have the expected rectangle coordinates.
625 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000626 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000627 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
628 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
629 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400630
Lei Zhanga21d5932018-02-05 18:28:38 +0000631 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000632 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000633 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
634 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
635 }
636
637 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000638 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000639 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
640 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
641 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400642
643 // Check that nothing happens when attempting to remove an annotation with an
644 // out-of-bound index.
645 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
646 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
647 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
648
649 // Remove the second annotation.
650 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
651 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
652 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
653
654 // Save the document, closing the page and document.
655 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000656 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400657
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400658 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400659 // Open the saved document.
660 std::string new_file = GetString();
661 FPDF_FILEACCESS file_access;
662 memset(&file_access, 0, sizeof(file_access));
663 file_access.m_FileLen = new_file.size();
664 file_access.m_GetBlock = GetBlockFromString;
665 file_access.m_Param = &new_file;
666 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
667 ASSERT_TRUE(new_doc);
668 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
669 ASSERT_TRUE(new_page);
670
671 // Check that the saved document has 2 annotations on the first page.
672 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
673
Lei Zhanga21d5932018-02-05 18:28:38 +0000674 // Check that the remaining 2 annotations are the original 1st and 3rd ones
675 // by verifying their rectangle coordinates.
676 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000677 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000678 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
679 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
680 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400681
Lei Zhanga21d5932018-02-05 18:28:38 +0000682 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000683 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000684 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
685 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
686 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400687 FPDF_ClosePage(new_page);
688 FPDF_CloseDocument(new_doc);
689}
Jane Liu8ce58f52017-06-29 13:40:22 -0400690
Lei Zhangab41f252018-12-23 03:10:50 +0000691TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400692#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +0000693 static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2";
694 static const char kMd5ModifiedPath[] = "9059723a045e17478753d2f0eb33bc03";
695 static const char kMd5TwoPaths[] = "7eed0cfba780f1d4dd8068f717d3a6bf";
696 static const char kMd5NewAnnot[] = "1de8212d43b7066a6df042095c2aca61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000697#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +0000698 static const char kMd5Original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
699 static const char kMd5ModifiedPath[] = "c0c2eb2aba73ad15b3240e342fbe0d72";
700 static const char kMd5TwoPaths[] = "2306bf04915fe001b5f4726843d184c8";
701 static const char kMd5NewAnnot[] = "64a319f145768cb09944d2109efe394e";
Jane Liubaa7ff42017-06-29 19:18:23 -0400702#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000703 static const char kMd5Original[] = "964f89bbe8911e540a465cf1a64b7f7e";
704 static const char kMd5ModifiedPath[] = "9a38048fb3ac1b2c9a4b34139caa993c";
705 static const char kMd5TwoPaths[] = "ece3d4df54b3395d6a2bf7a29b17239c";
706 static const char kMd5NewAnnot[] = "4299493de84c249f42f220f30f2bbb67";
Jane Liubaa7ff42017-06-29 19:18:23 -0400707#endif
708
709 // Open a file with two annotations and load its first page.
710 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000711 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400712 ASSERT_TRUE(page);
713 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
714
715 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000716 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000717 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000718 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000719 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400720
Lei Zhanga21d5932018-02-05 18:28:38 +0000721 {
722 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000723 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000724 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400725
Lei Zhanga21d5932018-02-05 18:28:38 +0000726 // Check that this annotation has one path object and retrieve it.
727 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000728 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000729 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
730 EXPECT_FALSE(path);
731 path = FPDFAnnot_GetObject(annot.get(), 0);
732 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
733 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400734
Lei Zhanga21d5932018-02-05 18:28:38 +0000735 // Modify the color of the path object.
736 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
737 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400738
Lei Zhanga21d5932018-02-05 18:28:38 +0000739 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000740 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000741 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000742 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000743 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400744
Lei Zhanga21d5932018-02-05 18:28:38 +0000745 // Add a second path object to the same annotation.
746 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
747 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
748 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
749 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
750 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
751 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
752 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400753
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000754 // The object is in the annontation, not in the page, so the page object
755 // array should not change.
756 ASSERT_EQ(32, FPDFPage_CountObjects(page));
757
Lei Zhanga21d5932018-02-05 18:28:38 +0000758 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000759 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000760 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000761 CompareBitmap(bitmap.get(), 595, 842, kMd5TwoPaths);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000762 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400763
Lei Zhanga21d5932018-02-05 18:28:38 +0000764 // Delete the newly added path object.
765 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
766 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000767 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000768 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400769
770 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000771 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000772 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000773 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedPath);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000774 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400775
Jane Liubaa7ff42017-06-29 19:18:23 -0400776 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400777
Lei Zhanga21d5932018-02-05 18:28:38 +0000778 {
779 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000780 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000781 ASSERT_TRUE(annot);
782 rect.left = 200.f;
783 rect.bottom = 400.f;
784 rect.right = 500.f;
785 rect.top = 600.f;
786 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400787
Lei Zhanga21d5932018-02-05 18:28:38 +0000788 // Add a new path to the annotation.
789 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
790 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
791 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
792 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
793 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
794 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
795 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
796 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
797 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
798 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
799
800 // Check that the annotation's bounding box came from its rectangle.
801 FS_RECTF new_rect;
802 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
803 EXPECT_EQ(rect.left, new_rect.left);
804 EXPECT_EQ(rect.bottom, new_rect.bottom);
805 EXPECT_EQ(rect.right, new_rect.right);
806 EXPECT_EQ(rect.top, new_rect.top);
807 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400808
809 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400810 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000811 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400812
813 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +0000814 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000815 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +0000816 VerifySavedRendering(page, 595, 842, kMd5NewAnnot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400817
Jane Liu36567742017-07-06 11:13:35 -0400818 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000819 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400820
Lei Zhanga21d5932018-02-05 18:28:38 +0000821 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000822 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000823 ASSERT_TRUE(annot);
824 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400825
Lei Zhanga21d5932018-02-05 18:28:38 +0000826 // Check that the new annotation's rectangle is as defined.
827 FS_RECTF new_rect;
828 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
829 EXPECT_EQ(rect.left, new_rect.left);
830 EXPECT_EQ(rect.bottom, new_rect.bottom);
831 EXPECT_EQ(rect.right, new_rect.right);
832 EXPECT_EQ(rect.top, new_rect.top);
833 }
834
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000835 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400836 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400837}
Jane Liub137e752017-07-05 15:04:33 -0400838
Lei Zhangab41f252018-12-23 03:10:50 +0000839TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400840 // Open a file with an annotation and load its first page.
841 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000842 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400843 ASSERT_TRUE(page);
844
845 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000846 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000847 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000848 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
849 }
Jane Liub137e752017-07-05 15:04:33 -0400850
Lei Zhanga21d5932018-02-05 18:28:38 +0000851 {
852 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000853 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000854 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400855
Lei Zhanga21d5932018-02-05 18:28:38 +0000856 // Check that the original flag values are as expected.
857 int flags = FPDFAnnot_GetFlags(annot.get());
858 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
859 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400860
Lei Zhanga21d5932018-02-05 18:28:38 +0000861 // Set the HIDDEN flag.
862 flags |= FPDF_ANNOT_FLAG_HIDDEN;
863 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
864 flags = FPDFAnnot_GetFlags(annot.get());
865 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
866 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400867
Lei Zhanga21d5932018-02-05 18:28:38 +0000868 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000869 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000870 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000871 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
872 }
Jane Liub137e752017-07-05 15:04:33 -0400873
Lei Zhanga21d5932018-02-05 18:28:38 +0000874 // Unset the HIDDEN flag.
875 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
876 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
877 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
878 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
879 flags = FPDFAnnot_GetFlags(annot.get());
880 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
881 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400882
Lei Zhanga21d5932018-02-05 18:28:38 +0000883 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000884 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000885 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000886 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
887 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000888 }
Jane Liub137e752017-07-05 15:04:33 -0400889
Jane Liub137e752017-07-05 15:04:33 -0400890 UnloadPage(page);
891}
Jane Liu36567742017-07-06 11:13:35 -0400892
Lei Zhangab41f252018-12-23 03:10:50 +0000893TEST_F(FPDFAnnotEmbedderTest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400894#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +0000895 static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2";
896 static const char kMd5NewImage[] = "ff012f5697436dfcaec25b32d1333596";
897 static const char kMd5ModifiedImage[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000898#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +0000899 static const char kMd5Original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
900 static const char kMd5NewImage[] = "d19c6fcfd9a170802fcfb9adfa13557e";
901 static const char kMd5ModifiedImage[] = "1273cf2363570a50d1aa0c95b1318197";
Jane Liu36567742017-07-06 11:13:35 -0400902#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000903 static const char kMd5Original[] = "964f89bbe8911e540a465cf1a64b7f7e";
904 static const char kMd5NewImage[] = "9ea8732dc9d579f68853f16892856208";
905 static const char kMd5ModifiedImage[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400906#endif
907
908 // Open a file with two annotations and load its first page.
909 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000910 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400911 ASSERT_TRUE(page);
912 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
913
914 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000915 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000916 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000917 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000918 }
Jane Liu36567742017-07-06 11:13:35 -0400919
Jane Liu36567742017-07-06 11:13:35 -0400920 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000921 FPDF_BITMAP image_bitmap;
922
923 {
924 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000925 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000926 ASSERT_TRUE(annot);
927 FS_RECTF rect;
928 rect.left = 200.f;
929 rect.bottom = 600.f;
930 rect.right = 400.f;
931 rect.top = 800.f;
932 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
933
934 // Add a solid-color translucent image object to the new annotation.
935 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
936 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
937 0xeeeecccc);
938 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
939 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
940 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
941 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
942 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
943 kBitmapSize, 0, 0));
944 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
945 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
946 }
Jane Liu36567742017-07-06 11:13:35 -0400947
948 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000949 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000950 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +0000951 CompareBitmap(bitmap.get(), 595, 842, kMd5NewImage);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000952 }
Jane Liu36567742017-07-06 11:13:35 -0400953
Lei Zhanga21d5932018-02-05 18:28:38 +0000954 {
955 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000956 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000957 ASSERT_TRUE(annot);
958 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
959 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
960 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400961
Lei Zhanga21d5932018-02-05 18:28:38 +0000962 // Modify the image in the new annotation.
963 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
964 0xff000000);
965 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
966 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
967 }
Jane Liu36567742017-07-06 11:13:35 -0400968
969 // Save the document, closing the page and document.
970 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000971 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400972 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400973
974 // Test that the saved document renders the modified image object correctly.
Lei Zhang4f556b82019-04-08 16:32:41 +0000975 VerifySavedDocument(595, 842, kMd5ModifiedImage);
Jane Liu36567742017-07-06 11:13:35 -0400976}
977
Lei Zhangab41f252018-12-23 03:10:50 +0000978TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400979#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +0000980 static const char kMd5Original[] = "c35408717759562d1f8bf33d317483d2";
981 static const char kMd5NewText[] = "60031c1b0330cf1e1575f7d46687d429";
982 static const char kMd5ModifiedText[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000983#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +0000984 static const char kMd5Original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
985 static const char kMd5NewText[] = "87d55e09f9096de7e6552f5ae79afd3b";
986 static const char kMd5ModifiedText[] = "26e94fbd3af4b1e65479327507600114";
Jane Liu36567742017-07-06 11:13:35 -0400987#else
Lei Zhang4f556b82019-04-08 16:32:41 +0000988 static const char kMd5Original[] = "964f89bbe8911e540a465cf1a64b7f7e";
989 static const char kMd5NewText[] = "30f3f5b989612ca03827d95f184f0979";
990 static const char kMd5ModifiedText[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400991#endif
992
993 // Open a file with two annotations and load its first page.
994 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000995 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400996 ASSERT_TRUE(page);
997 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
998
999 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001000 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001001 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001002 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001003 }
Jane Liu36567742017-07-06 11:13:35 -04001004
Lei Zhanga21d5932018-02-05 18:28:38 +00001005 {
1006 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001007 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001008 ASSERT_TRUE(annot);
1009 FS_RECTF rect;
1010 rect.left = 200.f;
1011 rect.bottom = 550.f;
1012 rect.right = 450.f;
1013 rect.top = 650.f;
1014 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001015
Lei Zhanga21d5932018-02-05 18:28:38 +00001016 // Add a translucent text object to the new annotation.
1017 FPDF_PAGEOBJECT text_object =
1018 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1019 EXPECT_TRUE(text_object);
1020 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1021 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1022 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1023 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
1024 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1025 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1026 }
Jane Liu36567742017-07-06 11:13:35 -04001027
1028 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001029 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001030 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001031 CompareBitmap(bitmap.get(), 595, 842, kMd5NewText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001032 }
Jane Liu36567742017-07-06 11:13:35 -04001033
Lei Zhanga21d5932018-02-05 18:28:38 +00001034 {
1035 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001036 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001037 ASSERT_TRUE(annot);
1038 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1039 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1040 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001041
Lei Zhanga21d5932018-02-05 18:28:38 +00001042 // Modify the text in the new annotation.
1043 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
1044 GetFPDFWideString(L"New text!");
1045 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1046 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1047 }
Jane Liu36567742017-07-06 11:13:35 -04001048
1049 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001050 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001051 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001052 CompareBitmap(bitmap.get(), 595, 842, kMd5ModifiedText);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001053 }
Jane Liu36567742017-07-06 11:13:35 -04001054
1055 // Remove the new annotation, and check that the page renders as before.
1056 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001057 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001058 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhang4f556b82019-04-08 16:32:41 +00001059 CompareBitmap(bitmap.get(), 595, 842, kMd5Original);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001060 }
Jane Liu36567742017-07-06 11:13:35 -04001061
1062 UnloadPage(page);
1063}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001064
Lei Zhangab41f252018-12-23 03:10:50 +00001065TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001066 // Open a file with four annotations and load its first page.
1067 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001068 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001069 ASSERT_TRUE(page);
1070
Lei Zhang4f556b82019-04-08 16:32:41 +00001071 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001072
Lei Zhanga21d5932018-02-05 18:28:38 +00001073 {
1074 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001075 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001076 ASSERT_TRUE(annot);
1077
1078 // Check that a non-existent key does not exist.
1079 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1080
1081 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001082 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001083 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001084 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1085 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1086 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001087
1088 // Check that the string value of the hash is correct.
Lei Zhang4f556b82019-04-08 16:32:41 +00001089 static const char kHashKey[] = "AAPL:Hash";
Lei Zhanga21d5932018-02-05 18:28:38 +00001090 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1091 unsigned long len =
1092 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1093 std::vector<char> buf(len);
1094 EXPECT_EQ(66u,
1095 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1096 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1097 BufferToWString(buf).c_str());
1098
1099 // Check that the string value of the modified date is correct.
1100 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001101 len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, nullptr,
1102 0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001103 buf.clear();
1104 buf.resize(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001105 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1106 buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +00001107 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1108
1109 // Update the date entry for the annotation.
1110 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1111 GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001112 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1113 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001114 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001115
1116 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001117 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001118 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001119
Dan Sinclair698aed72017-09-26 16:24:49 -04001120#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang4f556b82019-04-08 16:32:41 +00001121 static const char kMd5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001122#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang4f556b82019-04-08 16:32:41 +00001123 static const char kMd5[] = "9ee141f698c3fcb56c050dffd6c82624";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001124#else
Lei Zhang4f556b82019-04-08 16:32:41 +00001125 static const char kMd5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001126#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001127
1128 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001129 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001130 page = LoadSavedPage(0);
Lei Zhang4f556b82019-04-08 16:32:41 +00001131 VerifySavedRendering(page, 595, 842, kMd5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001132 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001133 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001134
Lei Zhanga21d5932018-02-05 18:28:38 +00001135 // Check that the string value of the modified date is the newly-set value.
1136 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001137 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
1138 unsigned long len = FPDFAnnot_GetStringValue(
1139 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001140 std::vector<char> buf(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001141 EXPECT_EQ(44u,
1142 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
1143 buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +00001144 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1145 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001146
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001147 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001148 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001149}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001150
rycsmith3e785602019-03-05 21:48:36 +00001151TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
1152 // Open a file with three text annotations and load its first page.
1153 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1154 FPDF_PAGE page = LoadPage(0);
1155 ASSERT_TRUE(page);
1156 {
1157 // First two annotations do not have "MaxLen" attribute.
1158 for (int i = 0; i < 2; i++) {
1159 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1160 ASSERT_TRUE(annot);
1161
1162 // Verify that no "MaxLen" key present.
1163 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1164
1165 float value;
1166 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1167 }
1168
1169 // Annotation in index 2 has "MaxLen" of 10.
1170 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1171 ASSERT_TRUE(annot);
1172
1173 // Verify that "MaxLen" key present.
1174 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1175
1176 float value;
1177 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1178 EXPECT_FLOAT_EQ(10.0f, value);
1179
1180 // Check bad inputs.
1181 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1182 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1183 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1184 // Ask for key that exists but is not a number.
1185 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1186 }
1187
1188 UnloadPage(page);
1189}
1190
Lei Zhangab41f252018-12-23 03:10:50 +00001191TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001192 // Open a file with four annotations and load its first page.
1193 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001194 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001195 ASSERT_TRUE(page);
1196
Lei Zhanga21d5932018-02-05 18:28:38 +00001197 {
1198 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001199 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001200 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001201
Lei Zhanga21d5932018-02-05 18:28:38 +00001202 // Check that the string value of an AP returns the expected length.
1203 unsigned long normal_len = FPDFAnnot_GetAP(
1204 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1205 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001206
Lei Zhanga21d5932018-02-05 18:28:38 +00001207 // Check that the string value of an AP is not returned if the buffer is too
1208 // small. The result buffer should be overwritten with an empty string.
1209 std::vector<char> buf(normal_len - 1);
1210 // Write L"z" in the buffer to verify it's not overwritten.
1211 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1212 EXPECT_EQ(73970u,
1213 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1214 buf.data(), buf.size()));
1215 std::string ap = BufferToString(buf);
1216 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001217
Lei Zhanga21d5932018-02-05 18:28:38 +00001218 // Check that the string value of an AP is returned through a buffer that is
1219 // the right size.
1220 buf.clear();
1221 buf.resize(normal_len);
1222 EXPECT_EQ(73970u,
1223 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1224 buf.data(), buf.size()));
1225 ap = BufferToString(buf);
1226 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1227 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001228
Lei Zhanga21d5932018-02-05 18:28:38 +00001229 // Check that the string value of an AP is returned through a buffer that is
1230 // larger than necessary.
1231 buf.clear();
1232 buf.resize(normal_len + 1);
1233 EXPECT_EQ(73970u,
1234 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1235 buf.data(), buf.size()));
1236 ap = BufferToString(buf);
1237 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1238 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001239
Lei Zhanga21d5932018-02-05 18:28:38 +00001240 // Check that getting an AP for a mode that does not have an AP returns an
1241 // empty string.
1242 unsigned long rollover_len = FPDFAnnot_GetAP(
1243 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1244 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001245
Lei Zhanga21d5932018-02-05 18:28:38 +00001246 buf.clear();
1247 buf.resize(1000);
1248 EXPECT_EQ(2u,
1249 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1250 buf.data(), buf.size()));
1251 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001252
Lei Zhanga21d5932018-02-05 18:28:38 +00001253 // Check that setting the AP for an invalid appearance mode fails.
Lei Zhang4f556b82019-04-08 16:32:41 +00001254 std::unique_ptr<unsigned short, pdfium::FreeDeleter> ap_text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001255 GetFPDFWideString(L"new test ap");
Lei Zhang4f556b82019-04-08 16:32:41 +00001256 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001257 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
Lei Zhang4f556b82019-04-08 16:32:41 +00001258 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001259 EXPECT_FALSE(FPDFAnnot_SetAP(
Lei Zhang4f556b82019-04-08 16:32:41 +00001260 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001261
Lei Zhanga21d5932018-02-05 18:28:38 +00001262 // Set the AP correctly now.
1263 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
Lei Zhang4f556b82019-04-08 16:32:41 +00001264 ap_text.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001265
Lei Zhanga21d5932018-02-05 18:28:38 +00001266 // Check that the new annotation value is equal to the value we just set.
1267 rollover_len = FPDFAnnot_GetAP(
1268 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1269 EXPECT_EQ(24u, rollover_len);
1270 buf.clear();
1271 buf.resize(rollover_len);
1272 EXPECT_EQ(24u,
1273 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1274 buf.data(), buf.size()));
1275 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001276
Lei Zhanga21d5932018-02-05 18:28:38 +00001277 // Check that the Normal AP was not touched when the Rollover AP was set.
1278 buf.clear();
1279 buf.resize(normal_len);
1280 EXPECT_EQ(73970u,
1281 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1282 buf.data(), buf.size()));
1283 ap = BufferToString(buf);
1284 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1285 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1286 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001287
1288 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001289 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001290 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001291
Lei Zhang0b494052019-01-31 21:41:15 +00001292 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001293 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001294 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001295 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001296
Lei Zhanga21d5932018-02-05 18:28:38 +00001297 // Check that the new annotation value is equal to the value we set before
1298 // saving.
1299 unsigned long rollover_len = FPDFAnnot_GetAP(
1300 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1301 EXPECT_EQ(24u, rollover_len);
1302 std::vector<char> buf(rollover_len);
1303 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1304 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1305 buf.data(), buf.size()));
1306 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1307 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001308
1309 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001310 CloseSavedPage(page);
1311 CloseSavedDocument();
1312}
1313
Lei Zhangab41f252018-12-23 03:10:50 +00001314TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001315 // Open a file with four annotations and load its first page.
1316 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001317 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001318 ASSERT_TRUE(page);
1319
Lei Zhanga21d5932018-02-05 18:28:38 +00001320 {
1321 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001322 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001323 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001324
Lei Zhanga21d5932018-02-05 18:28:38 +00001325 // Set Down AP. Normal AP is already set.
Lei Zhang4f556b82019-04-08 16:32:41 +00001326 std::unique_ptr<unsigned short, pdfium::FreeDeleter> ap_text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001327 GetFPDFWideString(L"new test ap");
1328 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001329 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001330 EXPECT_EQ(73970u,
1331 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1332 nullptr, 0));
1333 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1334 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001335
Lei Zhanga21d5932018-02-05 18:28:38 +00001336 // Check that setting the Down AP to null removes the Down entry but keeps
1337 // Normal intact.
1338 EXPECT_TRUE(
1339 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1340 EXPECT_EQ(73970u,
1341 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1342 nullptr, 0));
1343 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1344 nullptr, 0));
1345 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001346
Lei Zhang75c81712018-02-08 17:22:39 +00001347 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001348}
1349
Lei Zhangab41f252018-12-23 03:10:50 +00001350TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001351 // Open a file with four annotations and load its first page.
1352 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001353 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001354 ASSERT_TRUE(page);
1355
Lei Zhanga21d5932018-02-05 18:28:38 +00001356 {
1357 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001358 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001359 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001360
Lei Zhanga21d5932018-02-05 18:28:38 +00001361 // Set Down AP. Normal AP is already set.
Lei Zhang4f556b82019-04-08 16:32:41 +00001362 std::unique_ptr<unsigned short, pdfium::FreeDeleter> ap_text =
Lei Zhanga21d5932018-02-05 18:28:38 +00001363 GetFPDFWideString(L"new test ap");
1364 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
Lei Zhang4f556b82019-04-08 16:32:41 +00001365 ap_text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001366 EXPECT_EQ(73970u,
1367 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1368 nullptr, 0));
1369 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1370 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001371
Lei Zhanga21d5932018-02-05 18:28:38 +00001372 // Check that setting the Normal AP to null removes the whole AP dictionary.
1373 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1374 nullptr));
1375 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1376 nullptr, 0));
1377 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1378 nullptr, 0));
1379 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001380
Lei Zhang75c81712018-02-08 17:22:39 +00001381 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001382}
1383
Lei Zhangab41f252018-12-23 03:10:50 +00001384TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001385 // Open a file with annotations and load its first page.
1386 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001387 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001388 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001389 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001390
Lei Zhanga21d5932018-02-05 18:28:38 +00001391 {
1392 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001393 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001394 ASSERT_TRUE(annot);
1395 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1396 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
Lei Zhang4f556b82019-04-08 16:32:41 +00001397 static const char kPopupKey[] = "Popup";
Lei Zhanga21d5932018-02-05 18:28:38 +00001398 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1399 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1400 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001401
Lei Zhanga21d5932018-02-05 18:28:38 +00001402 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001403 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001404 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1405 ASSERT_TRUE(popup);
1406 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1407 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1408 FS_RECTF rect;
1409 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1410 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1411 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001412
Lei Zhanga21d5932018-02-05 18:28:38 +00001413 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1414 // since "IRT" is not a key in |annot|'s dictionary.
Lei Zhang4f556b82019-04-08 16:32:41 +00001415 static const char kIRTKey[] = "IRT";
Lei Zhanga21d5932018-02-05 18:28:38 +00001416 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1417 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001418
Lei Zhanga21d5932018-02-05 18:28:38 +00001419 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1420 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001421 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001422 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001423 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1424 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001425 }
Jane Liu300bb272017-08-21 14:37:53 -04001426
Jane Liu300bb272017-08-21 14:37:53 -04001427 UnloadPage(page);
1428}
1429
Lei Zhangab41f252018-12-23 03:10:50 +00001430TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001431 // Open file with form text fields.
1432 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001433 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001434 ASSERT_TRUE(page);
1435
Lei Zhanga21d5932018-02-05 18:28:38 +00001436 {
1437 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001438 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001439 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001440
Lei Zhanga21d5932018-02-05 18:28:38 +00001441 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001442 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001443 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1444 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001445
Lei Zhanga21d5932018-02-05 18:28:38 +00001446 {
1447 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001448 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001449 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001450
Lei Zhanga21d5932018-02-05 18:28:38 +00001451 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001452 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001453 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1454 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001455
1456 UnloadPage(page);
1457}
1458
Lei Zhangab41f252018-12-23 03:10:50 +00001459TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001460 // Open file with form text fields.
1461 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001462 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001463 ASSERT_TRUE(page);
1464
Lei Zhanga21d5932018-02-05 18:28:38 +00001465 {
1466 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001467 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001468 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001469
Lei Zhanga21d5932018-02-05 18:28:38 +00001470 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001471 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001472 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1473 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1474 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1475 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001476
Lei Zhanga21d5932018-02-05 18:28:38 +00001477 {
1478 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001479 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001480 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001481
Lei Zhanga21d5932018-02-05 18:28:38 +00001482 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001483 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001484 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1485 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1486 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1487 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001488
Lei Zhanga21d5932018-02-05 18:28:38 +00001489 {
1490 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001491 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001492 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001493
Lei Zhanga21d5932018-02-05 18:28:38 +00001494 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001495 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001496 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1497 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1498 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1499 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001500
1501 UnloadPage(page);
1502}
Diana Gage40870db2017-07-19 18:16:03 -07001503
Lei Zhangab41f252018-12-23 03:10:50 +00001504TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001505 // Open file with form text fields.
1506 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1507 FPDF_PAGE page = LoadPage(0);
1508 ASSERT_TRUE(page);
1509
1510 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang7557e7b2018-09-14 17:02:40 +00001511 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0));
1512
1513 {
1514 // Verify there is an annotation.
1515 ScopedFPDFAnnotation annot(
1516 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120));
1517 EXPECT_TRUE(annot);
1518 }
1519
1520 // Try other bad inputs at a valid location.
1521 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120));
1522 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120));
1523 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120));
Diana Gage40870db2017-07-19 18:16:03 -07001524
1525 UnloadPage(page);
1526}
1527
Lei Zhangab41f252018-12-23 03:10:50 +00001528TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001529 // Open file with form text fields.
1530 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1531 FPDF_PAGE page = LoadPage(0);
1532 ASSERT_TRUE(page);
1533
Lei Zhanga21d5932018-02-05 18:28:38 +00001534 {
1535 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001536 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001537 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1538 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001539
Lei Zhanga21d5932018-02-05 18:28:38 +00001540 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001541 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001542 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1543 }
Diana Gage40870db2017-07-19 18:16:03 -07001544
Lei Zhanga21d5932018-02-05 18:28:38 +00001545 {
1546 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001547 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001548 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1549 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001550
Lei Zhanga21d5932018-02-05 18:28:38 +00001551 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001552 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001553 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1554 }
Diana Gage40870db2017-07-19 18:16:03 -07001555
1556 UnloadPage(page);
1557}
1558
Lei Zhangab41f252018-12-23 03:10:50 +00001559TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001560 // Open file with form comboboxes.
1561 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1562 FPDF_PAGE page = LoadPage(0);
1563 ASSERT_TRUE(page);
1564
Lei Zhanga21d5932018-02-05 18:28:38 +00001565 {
1566 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001567 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001568 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001569 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001570
Lei Zhanga21d5932018-02-05 18:28:38 +00001571 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001572 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001573 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1574 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1575 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1576 }
Diana Gage40870db2017-07-19 18:16:03 -07001577
Lei Zhanga21d5932018-02-05 18:28:38 +00001578 {
1579 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001580 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001581 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001582 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001583
Lei Zhanga21d5932018-02-05 18:28:38 +00001584 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001585 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001586 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1587 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1588 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1589 }
Diana Gage40870db2017-07-19 18:16:03 -07001590
Lei Zhanga21d5932018-02-05 18:28:38 +00001591 {
1592 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001593 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001594 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001595 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001596
Lei Zhanga21d5932018-02-05 18:28:38 +00001597 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001598 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001599 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1600 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1601 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1602 }
Diana Gage40870db2017-07-19 18:16:03 -07001603
1604 UnloadPage(page);
1605}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001606
Lei Zhang992e7e22019-02-04 19:20:58 +00001607TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
1608 static constexpr size_t kExpectedSize = 1609;
1609 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1610
1611 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1612
1613 FPDF_PAGE page = LoadPage(0);
1614 ASSERT_TRUE(page);
1615
1616 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1617 EXPECT_EQ(kExpectedSize, GetString().size());
1618 ClearString();
1619
1620 for (size_t i = 0; i < 10; ++i) {
1621 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1622 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
1623
1624 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1625 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
1626 // equal, not bigger.
1627 EXPECT_LT(kExpectedSize, GetString().size());
1628 ClearString();
1629 }
1630
1631 UnloadPage(page);
1632}
1633
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001634TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1635 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1636 FPDF_PAGE page = LoadPage(0);
1637 ASSERT_TRUE(page);
1638 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1639
1640 static const char kTestKey[] = "test";
Lei Zhang4f556b82019-04-08 16:32:41 +00001641 static const wchar_t kData[] = L"\xf6\xe4";
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001642 std::vector<char> buf(12);
1643
1644 {
1645 // Add a text annotation to the page.
1646 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1647 ASSERT_TRUE(annot);
1648 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1649 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1650
1651 // Make sure there is no test key, add set a value there, and read it back.
1652 std::fill(buf.begin(), buf.end(), 'x');
1653 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1654 buf.size()));
1655 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1656
1657 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1658 GetFPDFWideString(kData);
1659 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1660
1661 std::fill(buf.begin(), buf.end(), 'x');
1662 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1663 buf.size()));
1664 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1665 }
1666
Lei Zhang05ec64c2019-01-09 03:00:06 +00001667 {
1668 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1669 ASSERT_TRUE(annot);
1670 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1671 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1672 // Also do the same test for its appearance string.
1673 std::fill(buf.begin(), buf.end(), 'x');
1674 ASSERT_EQ(2u,
1675 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1676 buf.data(), buf.size()));
1677 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1678
1679 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1680 GetFPDFWideString(kData);
1681 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1682 text.get()));
1683
1684 std::fill(buf.begin(), buf.end(), 'x');
1685 ASSERT_EQ(6u,
1686 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1687 buf.data(), buf.size()));
1688 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1689 }
1690
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001691 UnloadPage(page);
1692
1693 {
1694 // Save a copy, open the copy, and check the annotation again.
1695 // Note that it renders the rotation.
1696 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001697 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001698 FPDF_PAGE saved_page = LoadSavedPage(0);
1699 ASSERT_TRUE(saved_page);
1700
Lei Zhang05ec64c2019-01-09 03:00:06 +00001701 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001702 {
1703 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1704 ASSERT_TRUE(annot);
1705 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1706
1707 std::fill(buf.begin(), buf.end(), 'x');
1708 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1709 buf.size()));
1710 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1711 }
1712
Lei Zhang05ec64c2019-01-09 03:00:06 +00001713 {
1714 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1715 ASSERT_TRUE(annot);
1716 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1717 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1718
1719 std::fill(buf.begin(), buf.end(), 'x');
1720 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1721 buf.size()));
1722 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1723 }
1724
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001725 CloseSavedPage(saved_page);
1726 CloseSavedDocument();
1727 }
1728}
rycsmithcb752f32019-02-21 18:40:53 +00001729
1730TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
1731 // Open a file with combobox widget annotations and load its first page.
1732 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1733 FPDF_PAGE page = LoadPage(0);
1734 ASSERT_TRUE(page);
1735
1736 {
1737 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1738 ASSERT_TRUE(annot);
1739
1740 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1741
1742 annot.reset(FPDFPage_GetAnnot(page, 1));
1743 ASSERT_TRUE(annot);
1744
1745 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00001746
1747 // Check bad form handle / annot.
1748 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
1749 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
1750 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00001751 }
1752
1753 UnloadPage(page);
1754}
1755
1756TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
1757 // Open a file with listbox widget annotations and load its first page.
1758 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1759 FPDF_PAGE page = LoadPage(0);
1760 ASSERT_TRUE(page);
1761
1762 {
1763 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1764 ASSERT_TRUE(annot);
1765
1766 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1767
1768 annot.reset(FPDFPage_GetAnnot(page, 1));
1769 ASSERT_TRUE(annot);
1770
1771 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1772 }
1773
1774 UnloadPage(page);
1775}
1776
1777TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
1778 // Open a file with ink annotations and load its first page.
1779 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1780 FPDF_PAGE page = LoadPage(0);
1781 ASSERT_TRUE(page);
1782
1783 {
1784 // annotations do not have "Opt" array and will return -1
1785 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1786 ASSERT_TRUE(annot);
1787
1788 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1789
1790 annot.reset(FPDFPage_GetAnnot(page, 1));
1791 ASSERT_TRUE(annot);
1792
1793 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1794 }
1795
1796 UnloadPage(page);
1797}
1798
1799TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
1800 // Open a file with combobox widget annotations and load its first page.
1801 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1802 FPDF_PAGE page = LoadPage(0);
1803 ASSERT_TRUE(page);
1804
1805 {
1806 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1807 ASSERT_TRUE(annot);
1808
1809 int index = 0;
1810 unsigned long len =
1811 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1812 std::vector<char> buf(len);
1813 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1814 buf.data(), len));
1815 EXPECT_STREQ(L"Foo", BufferToWString(buf).c_str());
1816
1817 annot.reset(FPDFPage_GetAnnot(page, 1));
1818 ASSERT_TRUE(annot);
1819
1820 index = 0;
1821 len =
1822 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1823 buf.clear();
1824 buf.resize(len);
1825 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1826 buf.data(), len));
1827 EXPECT_STREQ(L"Apple", BufferToWString(buf).c_str());
1828
1829 index = 25;
1830 len =
1831 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1832 buf.clear();
1833 buf.resize(len);
1834 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1835 buf.data(), len));
1836 EXPECT_STREQ(L"Zucchini", BufferToWString(buf).c_str());
1837
Lei Zhange7033c82019-02-26 19:30:49 +00001838 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00001839 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1840 nullptr, 0));
1841 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1842 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00001843
1844 // Check bad form handle / annot.
1845 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
1846 EXPECT_EQ(0u,
1847 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
1848 EXPECT_EQ(0u,
1849 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00001850 }
1851
1852 UnloadPage(page);
1853}
1854
1855TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
1856 // Open a file with listbox widget annotations and load its first page.
1857 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1858 FPDF_PAGE page = LoadPage(0);
1859 ASSERT_TRUE(page);
1860
1861 {
1862 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1863 ASSERT_TRUE(annot);
1864
1865 int index = 0;
1866 unsigned long len =
1867 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1868 std::vector<char> buf(len);
1869 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1870 buf.data(), len));
1871 EXPECT_STREQ(L"Foo", BufferToWString(buf).c_str());
1872
1873 annot.reset(FPDFPage_GetAnnot(page, 1));
1874 ASSERT_TRUE(annot);
1875
1876 index = 0;
1877 len =
1878 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1879 buf.clear();
1880 buf.resize(len);
1881 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1882 buf.data(), len));
1883 EXPECT_STREQ(L"Apple", BufferToWString(buf).c_str());
1884
1885 index = 25;
1886 len =
1887 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1888 buf.clear();
1889 buf.resize(len);
1890 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1891 buf.data(), len));
1892 EXPECT_STREQ(L"Zucchini", BufferToWString(buf).c_str());
1893
1894 // indices out of range
1895 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1896 nullptr, 0));
1897 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1898 nullptr, 0));
1899 }
1900
1901 UnloadPage(page);
1902}
1903
1904TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
1905 // Open a file with ink annotations and load its first page.
1906 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1907 FPDF_PAGE page = LoadPage(0);
1908 ASSERT_TRUE(page);
1909
1910 {
1911 // annotations do not have "Opt" array and will return 0
1912 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1913 ASSERT_TRUE(annot);
1914
1915 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1916 nullptr, 0));
1917
1918 annot.reset(FPDFPage_GetAnnot(page, 1));
1919 ASSERT_TRUE(annot);
1920
1921 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1922 nullptr, 0));
1923 }
1924
1925 UnloadPage(page);
1926}