blob: b43940b0139e9c8f8279034016cee521b1363a64 [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
55 static constexpr wchar_t kContents[] = L"Bar";
56 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) {
Ralf Sipplb3a52402018-03-19 23:30:28 +000097 const char md5_hash[] = "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);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000104 CompareBitmap(bitmap.get(), 595, 842, md5_hash);
105
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.
137 static constexpr char kAuthorKey[] = "T";
138 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 Zhanga21d5932018-02-05 18:28:38 +0000158 const wchar_t contents[] =
159 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";
179 EXPECT_STREQ(contents, 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 Zhanga21d5932018-02-05 18:28:38 +0000223 // Check that the rectange coordinates are correct.
224 // 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.
319 static constexpr wchar_t kContents[] =
320 L"Hello! This is a customized content.";
321 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
322 GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000323 ASSERT_TRUE(FPDFAnnot_SetStringValue(
324 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000325 // Check that the content has been set correctly.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000326 unsigned long len = FPDFAnnot_GetStringValue(
327 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +0000328 std::vector<char> buf(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000329 EXPECT_EQ(74u,
330 FPDFAnnot_GetStringValue(
331 annot.get(), pdfium::annotation::kContents, buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +0000332 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
333 }
Jane Liu20eafda2017-06-07 10:33:24 -0400334 UnloadPage(page);
335}
336
Lei Zhangab41f252018-12-23 03:10:50 +0000337TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400338 // Open a file with one annotation and load its first page.
339 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000340 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400341 ASSERT_TRUE(page);
342
343 // Check that there is a total of one annotation on its first page, and verify
344 // its quadpoints.
345 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400346 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000347 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000348 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000349 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000350 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000351 EXPECT_EQ(115.802643f, quadpoints.x1);
352 EXPECT_EQ(718.913940f, quadpoints.y1);
353 EXPECT_EQ(157.211182f, quadpoints.x4);
354 EXPECT_EQ(706.264465f, quadpoints.y4);
355 }
Jane Liu20eafda2017-06-07 10:33:24 -0400356
357 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000358 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000359 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000360 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
361 ASSERT_TRUE(annot);
362 quadpoints.x1 = 140.802643f;
363 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000364 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000365 }
Jane Liu20eafda2017-06-07 10:33:24 -0400366
367 // Save the document, closing the page and document.
368 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000369 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400370
371 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000372 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400373
Lei Zhang0b494052019-01-31 21:41:15 +0000374 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000375 page = LoadSavedPage(0);
376 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400377
378 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000379 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400380
Lei Zhanga21d5932018-02-05 18:28:38 +0000381 {
382 // Check that the second annotation is an underline annotation and verify
383 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000384 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000385 ASSERT_TRUE(new_annot);
386 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
387 FS_QUADPOINTSF new_quadpoints;
388 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000389 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000390 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
391 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
392 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
393 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
394 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400395
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000396 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400397 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400398}
Jane Liu06462752017-06-27 16:41:14 -0400399
Lei Zhangab41f252018-12-23 03:10:50 +0000400TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000401 // Open a file with four annotations and load its first page.
402 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
403 FPDF_PAGE page = LoadPage(0);
404 ASSERT_TRUE(page);
405 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
406
407 // Retrieve the highlight annotation.
408 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
409 ASSERT_TRUE(annot);
410 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
411
412 FS_QUADPOINTSF quadpoints;
413 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
414
415 {
416 // Verify the current one set of quadpoints.
417 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
418
419 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
420 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
421 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
422 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
423 }
424
425 {
426 // Update the quadpoints.
427 FS_QUADPOINTSF new_quadpoints = quadpoints;
428 new_quadpoints.y1 -= 20.f;
429 new_quadpoints.y2 -= 20.f;
430 new_quadpoints.y3 -= 20.f;
431 new_quadpoints.y4 -= 20.f;
432 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
433
434 // Verify added quadpoint set
435 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
436 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
437 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
438 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
439 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
440 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
441 }
442
443 {
444 // Append a new set of quadpoints.
445 FS_QUADPOINTSF new_quadpoints = quadpoints;
446 new_quadpoints.y1 += 20.f;
447 new_quadpoints.y2 += 20.f;
448 new_quadpoints.y3 += 20.f;
449 new_quadpoints.y4 += 20.f;
450 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
451
452 // Verify added quadpoint set
453 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
454 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
455 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
456 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
457 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
458 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
459 }
460
461 {
462 // Setting and getting quadpoints at out-of-bound index should fail
463 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
464 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
465 }
466
467 FPDFPage_CloseAnnot(annot);
468
469 // Retrieve the square annotation
470 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
471
472 {
473 // Check that attempting to set its quadpoints would fail
474 ASSERT_TRUE(squareAnnot);
475 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
476 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
477 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
478 }
479
480 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000481 UnloadPage(page);
482}
483
Lei Zhangab41f252018-12-23 03:10:50 +0000484TEST_F(FPDFAnnotEmbedderTest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400485#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400486 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
487 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
488 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400489#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000490 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
491 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
492 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400493#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000494 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
495 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
496 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400497#endif
498
Jane Liu06462752017-06-27 16:41:14 -0400499 // Open a file with four annotations and load its first page.
500 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000501 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400502 ASSERT_TRUE(page);
503 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
504
Jane Liub370e5a2017-08-16 13:24:58 -0400505 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000506 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000507 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000508 CompareBitmap(bitmap.get(), 612, 792, md5_original);
509 }
Jane Liub370e5a2017-08-16 13:24:58 -0400510
Jane Liu0c6b07d2017-08-15 10:50:22 -0400511 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400512 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000513
514 // Retrieve the highlight annotation which has its AP stream already defined.
515 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000516 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000517 ASSERT_TRUE(annot);
518 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
519
520 // Check that color cannot be set when an AP stream is defined already.
521 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
522 102, 153, 204));
523
524 // Verify its attachment points.
525 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000526 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000527 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
528 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
529 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
530 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
531
532 // Check that updating the attachment points would succeed.
533 quadpoints.x1 -= 50.f;
534 quadpoints.x2 -= 50.f;
535 quadpoints.x3 -= 50.f;
536 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000537 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000538 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000539 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000540 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
541 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
542 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
543 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
544
545 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000546 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000547 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000548 CompareBitmap(bitmap.get(), 612, 792, md5_original);
549 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000550
551 // Verify its annotation rectangle.
552 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
553 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
554 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
555 EXPECT_NEAR(136.325f, rect.right, 0.001f);
556 EXPECT_NEAR(721.292f, rect.top, 0.001f);
557
558 // Check that updating the rectangle would succeed.
559 rect.left -= 60.f;
560 rect.right -= 60.f;
561 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
562 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
563 EXPECT_EQ(rect.right, new_rect.right);
564 }
Jane Liu06462752017-06-27 16:41:14 -0400565
Jane Liub370e5a2017-08-16 13:24:58 -0400566 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000567 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000568 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000569 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
570 }
Jane Liub370e5a2017-08-16 13:24:58 -0400571
Lei Zhanga21d5932018-02-05 18:28:38 +0000572 {
573 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000574 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000575 ASSERT_TRUE(annot);
576 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400577
Lei Zhanga21d5932018-02-05 18:28:38 +0000578 // Check that updating the rectangle would succeed.
579 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
580 rect.left += 70.f;
581 rect.right += 70.f;
582 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
583 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
584 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400585
Lei Zhanga21d5932018-02-05 18:28:38 +0000586 // Check that updating the rectangle changes the square annotation's
587 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000588 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000589 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000590 }
Jane Liub370e5a2017-08-16 13:24:58 -0400591
Jane Liu06462752017-06-27 16:41:14 -0400592 UnloadPage(page);
593}
Jane Liu8ce58f52017-06-29 13:40:22 -0400594
Lei Zhangab41f252018-12-23 03:10:50 +0000595TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000596 // Open a file with multiline markup annotations.
597 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
598 FPDF_PAGE page = LoadPage(0);
599 ASSERT_TRUE(page);
600 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000601 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000602 ASSERT_TRUE(annot);
603
604 // This is a three line annotation.
605 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
606 }
607 UnloadPage(page);
608
609 // null annotation should return 0
610 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
611}
612
Lei Zhangab41f252018-12-23 03:10:50 +0000613TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400614 // Open a file with 3 annotations on its first page.
615 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000616 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400617 ASSERT_TRUE(page);
618 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
619
Jane Liu0c6b07d2017-08-15 10:50:22 -0400620 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400621
Lei Zhanga21d5932018-02-05 18:28:38 +0000622 // Check that the annotations have the expected rectangle coordinates.
623 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000624 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000625 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
626 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
627 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400628
Lei Zhanga21d5932018-02-05 18:28:38 +0000629 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000630 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000631 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
632 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
633 }
634
635 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000636 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000637 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
638 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
639 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400640
641 // Check that nothing happens when attempting to remove an annotation with an
642 // out-of-bound index.
643 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
644 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
645 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
646
647 // Remove the second annotation.
648 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
649 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
650 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
651
652 // Save the document, closing the page and document.
653 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000654 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400655
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400656 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400657 // Open the saved document.
658 std::string new_file = GetString();
659 FPDF_FILEACCESS file_access;
660 memset(&file_access, 0, sizeof(file_access));
661 file_access.m_FileLen = new_file.size();
662 file_access.m_GetBlock = GetBlockFromString;
663 file_access.m_Param = &new_file;
664 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
665 ASSERT_TRUE(new_doc);
666 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
667 ASSERT_TRUE(new_page);
668
669 // Check that the saved document has 2 annotations on the first page.
670 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
671
Lei Zhanga21d5932018-02-05 18:28:38 +0000672 // Check that the remaining 2 annotations are the original 1st and 3rd ones
673 // by verifying their rectangle coordinates.
674 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000675 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000676 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
677 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
678 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400679
Lei Zhanga21d5932018-02-05 18:28:38 +0000680 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000681 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000682 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
683 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
684 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400685 FPDF_ClosePage(new_page);
686 FPDF_CloseDocument(new_doc);
687}
Jane Liu8ce58f52017-06-29 13:40:22 -0400688
Lei Zhangab41f252018-12-23 03:10:50 +0000689TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400690#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400691 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000692 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
693 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
694 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000695#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
696 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
697 const char md5_modified_path[] = "c66293426cbf1f568502d1f7c0728fc7";
698 const char md5_two_paths[] = "122ac4625393b1dfe4be8707b73cbb13";
699 const char md5_new_annot[] = "253c7fd739646ba2568e1e8bfc782336";
Jane Liubaa7ff42017-06-29 19:18:23 -0400700#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000701 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000702 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
703 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
704 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400705#endif
706
707 // Open a file with two annotations and load its first page.
708 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000709 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400710 ASSERT_TRUE(page);
711 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
712
713 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000714 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000715 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000716 CompareBitmap(bitmap.get(), 595, 842, md5_original);
717 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400718
Lei Zhanga21d5932018-02-05 18:28:38 +0000719 {
720 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000721 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000722 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400723
Lei Zhanga21d5932018-02-05 18:28:38 +0000724 // Check that this annotation has one path object and retrieve it.
725 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000726 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000727 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
728 EXPECT_FALSE(path);
729 path = FPDFAnnot_GetObject(annot.get(), 0);
730 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
731 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400732
Lei Zhanga21d5932018-02-05 18:28:38 +0000733 // Modify the color of the path object.
734 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
735 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400736
Lei Zhanga21d5932018-02-05 18:28:38 +0000737 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000738 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000739 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000740 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
741 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400742
Lei Zhanga21d5932018-02-05 18:28:38 +0000743 // Add a second path object to the same annotation.
744 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
745 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
746 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
747 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
748 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
749 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
750 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400751
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000752 // The object is in the annontation, not in the page, so the page object
753 // array should not change.
754 ASSERT_EQ(32, FPDFPage_CountObjects(page));
755
Lei Zhanga21d5932018-02-05 18:28:38 +0000756 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000757 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000758 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000759 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
760 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400761
Lei Zhanga21d5932018-02-05 18:28:38 +0000762 // Delete the newly added path object.
763 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
764 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000765 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000766 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400767
768 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000769 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000770 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000771 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
772 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400773
Jane Liubaa7ff42017-06-29 19:18:23 -0400774 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400775
Lei Zhanga21d5932018-02-05 18:28:38 +0000776 {
777 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000778 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000779 ASSERT_TRUE(annot);
780 rect.left = 200.f;
781 rect.bottom = 400.f;
782 rect.right = 500.f;
783 rect.top = 600.f;
784 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400785
Lei Zhanga21d5932018-02-05 18:28:38 +0000786 // Add a new path to the annotation.
787 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
788 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
789 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
790 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
791 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
792 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
793 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
794 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
795 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
796 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
797
798 // Check that the annotation's bounding box came from its rectangle.
799 FS_RECTF new_rect;
800 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
801 EXPECT_EQ(rect.left, new_rect.left);
802 EXPECT_EQ(rect.bottom, new_rect.bottom);
803 EXPECT_EQ(rect.right, new_rect.right);
804 EXPECT_EQ(rect.top, new_rect.top);
805 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400806
807 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400808 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000809 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400810
811 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +0000812 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000813 page = LoadSavedPage(0);
814 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400815
Jane Liu36567742017-07-06 11:13:35 -0400816 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000817 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400818
Lei Zhanga21d5932018-02-05 18:28:38 +0000819 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000820 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000821 ASSERT_TRUE(annot);
822 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400823
Lei Zhanga21d5932018-02-05 18:28:38 +0000824 // Check that the new annotation's rectangle is as defined.
825 FS_RECTF new_rect;
826 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
827 EXPECT_EQ(rect.left, new_rect.left);
828 EXPECT_EQ(rect.bottom, new_rect.bottom);
829 EXPECT_EQ(rect.right, new_rect.right);
830 EXPECT_EQ(rect.top, new_rect.top);
831 }
832
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000833 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400834 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400835}
Jane Liub137e752017-07-05 15:04:33 -0400836
Lei Zhangab41f252018-12-23 03:10:50 +0000837TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400838 // Open a file with an annotation and load its first page.
839 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000840 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400841 ASSERT_TRUE(page);
842
843 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000844 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000845 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000846 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
847 }
Jane Liub137e752017-07-05 15:04:33 -0400848
Lei Zhanga21d5932018-02-05 18:28:38 +0000849 {
850 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000851 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000852 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400853
Lei Zhanga21d5932018-02-05 18:28:38 +0000854 // Check that the original flag values are as expected.
855 int flags = FPDFAnnot_GetFlags(annot.get());
856 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
857 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400858
Lei Zhanga21d5932018-02-05 18:28:38 +0000859 // Set the HIDDEN flag.
860 flags |= FPDF_ANNOT_FLAG_HIDDEN;
861 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
862 flags = FPDFAnnot_GetFlags(annot.get());
863 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
864 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400865
Lei Zhanga21d5932018-02-05 18:28:38 +0000866 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000867 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000868 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000869 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
870 }
Jane Liub137e752017-07-05 15:04:33 -0400871
Lei Zhanga21d5932018-02-05 18:28:38 +0000872 // Unset the HIDDEN flag.
873 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
874 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
875 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
876 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
877 flags = FPDFAnnot_GetFlags(annot.get());
878 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
879 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400880
Lei Zhanga21d5932018-02-05 18:28:38 +0000881 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000882 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000883 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000884 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
885 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000886 }
Jane Liub137e752017-07-05 15:04:33 -0400887
Jane Liub137e752017-07-05 15:04:33 -0400888 UnloadPage(page);
889}
Jane Liu36567742017-07-06 11:13:35 -0400890
Lei Zhangab41f252018-12-23 03:10:50 +0000891TEST_F(FPDFAnnotEmbedderTest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400892#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400893 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
894 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
895 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000896#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
897 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
898 const char md5_new_image[] = "d19c6fcfd9a170802fcfb9adfa13557e";
899 const char md5_modified_image[] = "1273cf2363570a50d1aa0c95b1318197";
Jane Liu36567742017-07-06 11:13:35 -0400900#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000901 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
902 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
903 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400904#endif
905
906 // Open a file with two annotations and load its first page.
907 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000908 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400909 ASSERT_TRUE(page);
910 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
911
912 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000913 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000914 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000915 CompareBitmap(bitmap.get(), 595, 842, md5_original);
916 }
Jane Liu36567742017-07-06 11:13:35 -0400917
Jane Liu36567742017-07-06 11:13:35 -0400918 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000919 FPDF_BITMAP image_bitmap;
920
921 {
922 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000923 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000924 ASSERT_TRUE(annot);
925 FS_RECTF rect;
926 rect.left = 200.f;
927 rect.bottom = 600.f;
928 rect.right = 400.f;
929 rect.top = 800.f;
930 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
931
932 // Add a solid-color translucent image object to the new annotation.
933 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
934 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
935 0xeeeecccc);
936 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
937 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
938 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
939 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
940 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
941 kBitmapSize, 0, 0));
942 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
943 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
944 }
Jane Liu36567742017-07-06 11:13:35 -0400945
946 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000947 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000948 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000949 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
950 }
Jane Liu36567742017-07-06 11:13:35 -0400951
Lei Zhanga21d5932018-02-05 18:28:38 +0000952 {
953 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000954 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000955 ASSERT_TRUE(annot);
956 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
957 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
958 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400959
Lei Zhanga21d5932018-02-05 18:28:38 +0000960 // Modify the image in the new annotation.
961 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
962 0xff000000);
963 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
964 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
965 }
Jane Liu36567742017-07-06 11:13:35 -0400966
967 // Save the document, closing the page and document.
968 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000969 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400970 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400971
972 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400973 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400974}
975
Lei Zhangab41f252018-12-23 03:10:50 +0000976TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400977#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400978 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
979 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
980 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000981#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
982 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
983 const char md5_new_text[] = "554d625b52144816aaabb0dd66962c55";
984 const char md5_modified_text[] = "26e94fbd3af4b1e65479327507600114";
Jane Liu36567742017-07-06 11:13:35 -0400985#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000986 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
987 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
988 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400989#endif
990
991 // Open a file with two annotations and load its first page.
992 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000993 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400994 ASSERT_TRUE(page);
995 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
996
997 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000998 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000999 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001000 CompareBitmap(bitmap.get(), 595, 842, md5_original);
1001 }
Jane Liu36567742017-07-06 11:13:35 -04001002
Lei Zhanga21d5932018-02-05 18:28:38 +00001003 {
1004 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001005 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001006 ASSERT_TRUE(annot);
1007 FS_RECTF rect;
1008 rect.left = 200.f;
1009 rect.bottom = 550.f;
1010 rect.right = 450.f;
1011 rect.top = 650.f;
1012 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001013
Lei Zhanga21d5932018-02-05 18:28:38 +00001014 // Add a translucent text object to the new annotation.
1015 FPDF_PAGEOBJECT text_object =
1016 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1017 EXPECT_TRUE(text_object);
1018 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1019 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1020 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1021 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
1022 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1023 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1024 }
Jane Liu36567742017-07-06 11:13:35 -04001025
1026 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001027 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001028 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001029 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
1030 }
Jane Liu36567742017-07-06 11:13:35 -04001031
Lei Zhanga21d5932018-02-05 18:28:38 +00001032 {
1033 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001034 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001035 ASSERT_TRUE(annot);
1036 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1037 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1038 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001039
Lei Zhanga21d5932018-02-05 18:28:38 +00001040 // Modify the text in the new annotation.
1041 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
1042 GetFPDFWideString(L"New text!");
1043 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1044 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1045 }
Jane Liu36567742017-07-06 11:13:35 -04001046
1047 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001048 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001049 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001050 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
1051 }
Jane Liu36567742017-07-06 11:13:35 -04001052
1053 // Remove the new annotation, and check that the page renders as before.
1054 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001055 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001056 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001057 CompareBitmap(bitmap.get(), 595, 842, md5_original);
1058 }
Jane Liu36567742017-07-06 11:13:35 -04001059
1060 UnloadPage(page);
1061}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001062
Lei Zhangab41f252018-12-23 03:10:50 +00001063TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001064 // Open a file with four annotations and load its first page.
1065 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001066 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001067 ASSERT_TRUE(page);
1068
Lei Zhanga21d5932018-02-05 18:28:38 +00001069 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001070
Lei Zhanga21d5932018-02-05 18:28:38 +00001071 {
1072 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001073 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001074 ASSERT_TRUE(annot);
1075
1076 // Check that a non-existent key does not exist.
1077 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1078
1079 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001080 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001081 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001082 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1083 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1084 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001085
1086 // Check that the string value of the hash is correct.
1087 static constexpr char kHashKey[] = "AAPL:Hash";
1088 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1089 unsigned long len =
1090 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1091 std::vector<char> buf(len);
1092 EXPECT_EQ(66u,
1093 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1094 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1095 BufferToWString(buf).c_str());
1096
1097 // Check that the string value of the modified date is correct.
1098 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001099 len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, nullptr,
1100 0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001101 buf.clear();
1102 buf.resize(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001103 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1104 buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +00001105 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1106
1107 // Update the date entry for the annotation.
1108 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1109 GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001110 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1111 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001112 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001113
1114 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001115 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001116 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001117
Dan Sinclair698aed72017-09-26 16:24:49 -04001118#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +03001119 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001120#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1121 const char md5[] = "9ee141f698c3fcb56c050dffd6c82624";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001122#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001123 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001124#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001125
1126 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001127 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001128 page = LoadSavedPage(0);
1129 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001130 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001131 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001132
Lei Zhanga21d5932018-02-05 18:28:38 +00001133 // Check that the string value of the modified date is the newly-set value.
1134 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001135 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
1136 unsigned long len = FPDFAnnot_GetStringValue(
1137 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001138 std::vector<char> buf(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001139 EXPECT_EQ(44u,
1140 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
1141 buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +00001142 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1143 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001144
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001145 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001146 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001147}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001148
Lei Zhangab41f252018-12-23 03:10:50 +00001149TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001150 // Open a file with four annotations and load its first page.
1151 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001152 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001153 ASSERT_TRUE(page);
1154
Lei Zhanga21d5932018-02-05 18:28:38 +00001155 {
1156 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001157 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001158 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001159
Lei Zhanga21d5932018-02-05 18:28:38 +00001160 // Check that the string value of an AP returns the expected length.
1161 unsigned long normal_len = FPDFAnnot_GetAP(
1162 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1163 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001164
Lei Zhanga21d5932018-02-05 18:28:38 +00001165 // Check that the string value of an AP is not returned if the buffer is too
1166 // small. The result buffer should be overwritten with an empty string.
1167 std::vector<char> buf(normal_len - 1);
1168 // Write L"z" in the buffer to verify it's not overwritten.
1169 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1170 EXPECT_EQ(73970u,
1171 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1172 buf.data(), buf.size()));
1173 std::string ap = BufferToString(buf);
1174 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001175
Lei Zhanga21d5932018-02-05 18:28:38 +00001176 // Check that the string value of an AP is returned through a buffer that is
1177 // the right size.
1178 buf.clear();
1179 buf.resize(normal_len);
1180 EXPECT_EQ(73970u,
1181 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1182 buf.data(), buf.size()));
1183 ap = BufferToString(buf);
1184 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1185 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001186
Lei Zhanga21d5932018-02-05 18:28:38 +00001187 // Check that the string value of an AP is returned through a buffer that is
1188 // larger than necessary.
1189 buf.clear();
1190 buf.resize(normal_len + 1);
1191 EXPECT_EQ(73970u,
1192 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1193 buf.data(), buf.size()));
1194 ap = BufferToString(buf);
1195 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1196 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001197
Lei Zhanga21d5932018-02-05 18:28:38 +00001198 // Check that getting an AP for a mode that does not have an AP returns an
1199 // empty string.
1200 unsigned long rollover_len = FPDFAnnot_GetAP(
1201 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1202 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001203
Lei Zhanga21d5932018-02-05 18:28:38 +00001204 buf.clear();
1205 buf.resize(1000);
1206 EXPECT_EQ(2u,
1207 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1208 buf.data(), buf.size()));
1209 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001210
Lei Zhanga21d5932018-02-05 18:28:38 +00001211 // Check that setting the AP for an invalid appearance mode fails.
1212 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1213 GetFPDFWideString(L"new test ap");
1214 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1215 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1216 apText.get()));
1217 EXPECT_FALSE(FPDFAnnot_SetAP(
1218 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001219
Lei Zhanga21d5932018-02-05 18:28:38 +00001220 // Set the AP correctly now.
1221 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1222 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001223
Lei Zhanga21d5932018-02-05 18:28:38 +00001224 // Check that the new annotation value is equal to the value we just set.
1225 rollover_len = FPDFAnnot_GetAP(
1226 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1227 EXPECT_EQ(24u, rollover_len);
1228 buf.clear();
1229 buf.resize(rollover_len);
1230 EXPECT_EQ(24u,
1231 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1232 buf.data(), buf.size()));
1233 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001234
Lei Zhanga21d5932018-02-05 18:28:38 +00001235 // Check that the Normal AP was not touched when the Rollover AP was set.
1236 buf.clear();
1237 buf.resize(normal_len);
1238 EXPECT_EQ(73970u,
1239 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1240 buf.data(), buf.size()));
1241 ap = BufferToString(buf);
1242 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1243 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1244 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001245
1246 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001247 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001248 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001249
Lei Zhang0b494052019-01-31 21:41:15 +00001250 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001251 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001252 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001253 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001254
Lei Zhanga21d5932018-02-05 18:28:38 +00001255 // Check that the new annotation value is equal to the value we set before
1256 // saving.
1257 unsigned long rollover_len = FPDFAnnot_GetAP(
1258 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1259 EXPECT_EQ(24u, rollover_len);
1260 std::vector<char> buf(rollover_len);
1261 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1262 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1263 buf.data(), buf.size()));
1264 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1265 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001266
1267 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001268 CloseSavedPage(page);
1269 CloseSavedDocument();
1270}
1271
Lei Zhangab41f252018-12-23 03:10:50 +00001272TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001273 // Open a file with four annotations and load its first page.
1274 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001275 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001276 ASSERT_TRUE(page);
1277
Lei Zhanga21d5932018-02-05 18:28:38 +00001278 {
1279 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001280 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001281 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001282
Lei Zhanga21d5932018-02-05 18:28:38 +00001283 // Set Down AP. Normal AP is already set.
1284 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1285 GetFPDFWideString(L"new test ap");
1286 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1287 apText.get()));
1288 EXPECT_EQ(73970u,
1289 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1290 nullptr, 0));
1291 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1292 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001293
Lei Zhanga21d5932018-02-05 18:28:38 +00001294 // Check that setting the Down AP to null removes the Down entry but keeps
1295 // Normal intact.
1296 EXPECT_TRUE(
1297 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1298 EXPECT_EQ(73970u,
1299 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1300 nullptr, 0));
1301 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1302 nullptr, 0));
1303 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001304
Lei Zhang75c81712018-02-08 17:22:39 +00001305 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001306}
1307
Lei Zhangab41f252018-12-23 03:10:50 +00001308TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001309 // Open a file with four annotations and load its first page.
1310 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001311 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001312 ASSERT_TRUE(page);
1313
Lei Zhanga21d5932018-02-05 18:28:38 +00001314 {
1315 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001316 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001317 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001318
Lei Zhanga21d5932018-02-05 18:28:38 +00001319 // Set Down AP. Normal AP is already set.
1320 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1321 GetFPDFWideString(L"new test ap");
1322 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1323 apText.get()));
1324 EXPECT_EQ(73970u,
1325 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1326 nullptr, 0));
1327 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1328 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001329
Lei Zhanga21d5932018-02-05 18:28:38 +00001330 // Check that setting the Normal AP to null removes the whole AP dictionary.
1331 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1332 nullptr));
1333 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1334 nullptr, 0));
1335 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1336 nullptr, 0));
1337 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001338
Lei Zhang75c81712018-02-08 17:22:39 +00001339 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001340}
1341
Lei Zhangab41f252018-12-23 03:10:50 +00001342TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001343 // Open a file with annotations and load its first page.
1344 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001345 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001346 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001347 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001348
Lei Zhanga21d5932018-02-05 18:28:38 +00001349 {
1350 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001351 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001352 ASSERT_TRUE(annot);
1353 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1354 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1355 static constexpr char kPopupKey[] = "Popup";
1356 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1357 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1358 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001359
Lei Zhanga21d5932018-02-05 18:28:38 +00001360 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001361 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001362 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1363 ASSERT_TRUE(popup);
1364 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1365 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1366 FS_RECTF rect;
1367 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1368 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1369 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001370
Lei Zhanga21d5932018-02-05 18:28:38 +00001371 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1372 // since "IRT" is not a key in |annot|'s dictionary.
1373 static constexpr char kIRTKey[] = "IRT";
1374 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1375 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001376
Lei Zhanga21d5932018-02-05 18:28:38 +00001377 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1378 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001379 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001380 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001381 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1382 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001383 }
Jane Liu300bb272017-08-21 14:37:53 -04001384
Jane Liu300bb272017-08-21 14:37:53 -04001385 UnloadPage(page);
1386}
1387
Lei Zhangab41f252018-12-23 03:10:50 +00001388TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001389 // Open file with form text fields.
1390 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001391 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001392 ASSERT_TRUE(page);
1393
Lei Zhanga21d5932018-02-05 18:28:38 +00001394 {
1395 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001396 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001397 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001398
Lei Zhanga21d5932018-02-05 18:28:38 +00001399 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001400 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001401 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1402 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001403
Lei Zhanga21d5932018-02-05 18:28:38 +00001404 {
1405 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001406 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001407 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001408
Lei Zhanga21d5932018-02-05 18:28:38 +00001409 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001410 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001411 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1412 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001413
1414 UnloadPage(page);
1415}
1416
Lei Zhangab41f252018-12-23 03:10:50 +00001417TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001418 // Open file with form text fields.
1419 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001420 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001421 ASSERT_TRUE(page);
1422
Lei Zhanga21d5932018-02-05 18:28:38 +00001423 {
1424 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001425 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001426 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001427
Lei Zhanga21d5932018-02-05 18:28:38 +00001428 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001429 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001430 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1431 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1432 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1433 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001434
Lei Zhanga21d5932018-02-05 18:28:38 +00001435 {
1436 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001437 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001438 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001439
Lei Zhanga21d5932018-02-05 18:28:38 +00001440 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001441 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001442 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1443 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1444 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1445 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001446
Lei Zhanga21d5932018-02-05 18:28:38 +00001447 {
1448 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001449 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001450 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001451
Lei Zhanga21d5932018-02-05 18:28:38 +00001452 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001453 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001454 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1455 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1456 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1457 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001458
1459 UnloadPage(page);
1460}
Diana Gage40870db2017-07-19 18:16:03 -07001461
Lei Zhangab41f252018-12-23 03:10:50 +00001462TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001463 // Open file with form text fields.
1464 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1465 FPDF_PAGE page = LoadPage(0);
1466 ASSERT_TRUE(page);
1467
1468 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang7557e7b2018-09-14 17:02:40 +00001469 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0));
1470
1471 {
1472 // Verify there is an annotation.
1473 ScopedFPDFAnnotation annot(
1474 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120));
1475 EXPECT_TRUE(annot);
1476 }
1477
1478 // Try other bad inputs at a valid location.
1479 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120));
1480 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120));
1481 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120));
Diana Gage40870db2017-07-19 18:16:03 -07001482
1483 UnloadPage(page);
1484}
1485
Lei Zhangab41f252018-12-23 03:10:50 +00001486TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001487 // Open file with form text fields.
1488 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1489 FPDF_PAGE page = LoadPage(0);
1490 ASSERT_TRUE(page);
1491
Lei Zhanga21d5932018-02-05 18:28:38 +00001492 {
1493 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001494 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001495 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1496 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001497
Lei Zhanga21d5932018-02-05 18:28:38 +00001498 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001499 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001500 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1501 }
Diana Gage40870db2017-07-19 18:16:03 -07001502
Lei Zhanga21d5932018-02-05 18:28:38 +00001503 {
1504 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001505 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001506 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1507 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001508
Lei Zhanga21d5932018-02-05 18:28:38 +00001509 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001510 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001511 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1512 }
Diana Gage40870db2017-07-19 18:16:03 -07001513
1514 UnloadPage(page);
1515}
1516
Lei Zhangab41f252018-12-23 03:10:50 +00001517TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001518 // Open file with form comboboxes.
1519 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1520 FPDF_PAGE page = LoadPage(0);
1521 ASSERT_TRUE(page);
1522
Lei Zhanga21d5932018-02-05 18:28:38 +00001523 {
1524 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001525 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001526 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001527 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001528
Lei Zhanga21d5932018-02-05 18:28:38 +00001529 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001530 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001531 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1532 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1533 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1534 }
Diana Gage40870db2017-07-19 18:16:03 -07001535
Lei Zhanga21d5932018-02-05 18:28:38 +00001536 {
1537 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001538 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001539 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001540 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001541
Lei Zhanga21d5932018-02-05 18:28:38 +00001542 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001543 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001544 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1545 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1546 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1547 }
Diana Gage40870db2017-07-19 18:16:03 -07001548
Lei Zhanga21d5932018-02-05 18:28:38 +00001549 {
1550 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001551 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001552 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001553 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001554
Lei Zhanga21d5932018-02-05 18:28:38 +00001555 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001556 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001557 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1558 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1559 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1560 }
Diana Gage40870db2017-07-19 18:16:03 -07001561
1562 UnloadPage(page);
1563}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001564
Lei Zhang992e7e22019-02-04 19:20:58 +00001565TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
1566 static constexpr size_t kExpectedSize = 1609;
1567 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1568
1569 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1570
1571 FPDF_PAGE page = LoadPage(0);
1572 ASSERT_TRUE(page);
1573
1574 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1575 EXPECT_EQ(kExpectedSize, GetString().size());
1576 ClearString();
1577
1578 for (size_t i = 0; i < 10; ++i) {
1579 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1580 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
1581
1582 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1583 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
1584 // equal, not bigger.
1585 EXPECT_LT(kExpectedSize, GetString().size());
1586 ClearString();
1587 }
1588
1589 UnloadPage(page);
1590}
1591
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001592TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1593 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1594 FPDF_PAGE page = LoadPage(0);
1595 ASSERT_TRUE(page);
1596 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1597
1598 static const char kTestKey[] = "test";
1599 static constexpr wchar_t kData[] = L"\xf6\xe4";
1600 std::vector<char> buf(12);
1601
1602 {
1603 // Add a text annotation to the page.
1604 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1605 ASSERT_TRUE(annot);
1606 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1607 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1608
1609 // Make sure there is no test key, add set a value there, and read it back.
1610 std::fill(buf.begin(), buf.end(), 'x');
1611 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1612 buf.size()));
1613 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1614
1615 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1616 GetFPDFWideString(kData);
1617 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1618
1619 std::fill(buf.begin(), buf.end(), 'x');
1620 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1621 buf.size()));
1622 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1623 }
1624
Lei Zhang05ec64c2019-01-09 03:00:06 +00001625 {
1626 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1627 ASSERT_TRUE(annot);
1628 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1629 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1630 // Also do the same test for its appearance string.
1631 std::fill(buf.begin(), buf.end(), 'x');
1632 ASSERT_EQ(2u,
1633 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1634 buf.data(), buf.size()));
1635 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1636
1637 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1638 GetFPDFWideString(kData);
1639 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1640 text.get()));
1641
1642 std::fill(buf.begin(), buf.end(), 'x');
1643 ASSERT_EQ(6u,
1644 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1645 buf.data(), buf.size()));
1646 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1647 }
1648
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001649 UnloadPage(page);
1650
1651 {
1652 // Save a copy, open the copy, and check the annotation again.
1653 // Note that it renders the rotation.
1654 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001655 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001656 FPDF_PAGE saved_page = LoadSavedPage(0);
1657 ASSERT_TRUE(saved_page);
1658
Lei Zhang05ec64c2019-01-09 03:00:06 +00001659 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001660 {
1661 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1662 ASSERT_TRUE(annot);
1663 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1664
1665 std::fill(buf.begin(), buf.end(), 'x');
1666 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1667 buf.size()));
1668 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1669 }
1670
Lei Zhang05ec64c2019-01-09 03:00:06 +00001671 {
1672 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1673 ASSERT_TRUE(annot);
1674 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1675 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1676
1677 std::fill(buf.begin(), buf.end(), 'x');
1678 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1679 buf.size()));
1680 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1681 }
1682
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001683 CloseSavedPage(saved_page);
1684 CloseSavedDocument();
1685 }
1686}
rycsmithcb752f32019-02-21 18:40:53 +00001687
1688TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
1689 // Open a file with combobox widget annotations and load its first page.
1690 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1691 FPDF_PAGE page = LoadPage(0);
1692 ASSERT_TRUE(page);
1693
1694 {
1695 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1696 ASSERT_TRUE(annot);
1697
1698 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1699
1700 annot.reset(FPDFPage_GetAnnot(page, 1));
1701 ASSERT_TRUE(annot);
1702
1703 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00001704
1705 // Check bad form handle / annot.
1706 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
1707 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
1708 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00001709 }
1710
1711 UnloadPage(page);
1712}
1713
1714TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
1715 // Open a file with listbox widget annotations and load its first page.
1716 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1717 FPDF_PAGE page = LoadPage(0);
1718 ASSERT_TRUE(page);
1719
1720 {
1721 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1722 ASSERT_TRUE(annot);
1723
1724 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1725
1726 annot.reset(FPDFPage_GetAnnot(page, 1));
1727 ASSERT_TRUE(annot);
1728
1729 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1730 }
1731
1732 UnloadPage(page);
1733}
1734
1735TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
1736 // Open a file with ink annotations and load its first page.
1737 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1738 FPDF_PAGE page = LoadPage(0);
1739 ASSERT_TRUE(page);
1740
1741 {
1742 // annotations do not have "Opt" array and will return -1
1743 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1744 ASSERT_TRUE(annot);
1745
1746 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1747
1748 annot.reset(FPDFPage_GetAnnot(page, 1));
1749 ASSERT_TRUE(annot);
1750
1751 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1752 }
1753
1754 UnloadPage(page);
1755}
1756
1757TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
1758 // Open a file with combobox widget annotations and load its first page.
1759 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1760 FPDF_PAGE page = LoadPage(0);
1761 ASSERT_TRUE(page);
1762
1763 {
1764 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1765 ASSERT_TRUE(annot);
1766
1767 int index = 0;
1768 unsigned long len =
1769 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1770 std::vector<char> buf(len);
1771 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1772 buf.data(), len));
1773 EXPECT_STREQ(L"Foo", BufferToWString(buf).c_str());
1774
1775 annot.reset(FPDFPage_GetAnnot(page, 1));
1776 ASSERT_TRUE(annot);
1777
1778 index = 0;
1779 len =
1780 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1781 buf.clear();
1782 buf.resize(len);
1783 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1784 buf.data(), len));
1785 EXPECT_STREQ(L"Apple", BufferToWString(buf).c_str());
1786
1787 index = 25;
1788 len =
1789 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1790 buf.clear();
1791 buf.resize(len);
1792 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1793 buf.data(), len));
1794 EXPECT_STREQ(L"Zucchini", BufferToWString(buf).c_str());
1795
Lei Zhange7033c82019-02-26 19:30:49 +00001796 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00001797 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1798 nullptr, 0));
1799 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1800 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00001801
1802 // Check bad form handle / annot.
1803 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
1804 EXPECT_EQ(0u,
1805 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
1806 EXPECT_EQ(0u,
1807 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00001808 }
1809
1810 UnloadPage(page);
1811}
1812
1813TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
1814 // Open a file with listbox widget annotations and load its first page.
1815 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1816 FPDF_PAGE page = LoadPage(0);
1817 ASSERT_TRUE(page);
1818
1819 {
1820 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1821 ASSERT_TRUE(annot);
1822
1823 int index = 0;
1824 unsigned long len =
1825 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1826 std::vector<char> buf(len);
1827 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1828 buf.data(), len));
1829 EXPECT_STREQ(L"Foo", BufferToWString(buf).c_str());
1830
1831 annot.reset(FPDFPage_GetAnnot(page, 1));
1832 ASSERT_TRUE(annot);
1833
1834 index = 0;
1835 len =
1836 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1837 buf.clear();
1838 buf.resize(len);
1839 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1840 buf.data(), len));
1841 EXPECT_STREQ(L"Apple", BufferToWString(buf).c_str());
1842
1843 index = 25;
1844 len =
1845 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1846 buf.clear();
1847 buf.resize(len);
1848 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1849 buf.data(), len));
1850 EXPECT_STREQ(L"Zucchini", BufferToWString(buf).c_str());
1851
1852 // indices out of range
1853 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1854 nullptr, 0));
1855 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1856 nullptr, 0));
1857 }
1858
1859 UnloadPage(page);
1860}
1861
1862TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
1863 // Open a file with ink annotations and load its first page.
1864 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1865 FPDF_PAGE page = LoadPage(0);
1866 ASSERT_TRUE(page);
1867
1868 {
1869 // annotations do not have "Opt" array and will return 0
1870 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1871 ASSERT_TRUE(annot);
1872
1873 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1874 nullptr, 0));
1875
1876 annot.reset(FPDFPage_GetAnnot(page, 1));
1877 ASSERT_TRUE(annot);
1878
1879 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1880 nullptr, 0));
1881 }
1882
1883 UnloadPage(page);
1884}