blob: 8ae6d009870eeb6ca70643e59cdedf9ead1130e4 [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"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000018#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040019#include "testing/gtest/include/gtest/gtest.h"
20
Lei Zhangab41f252018-12-23 03:10:50 +000021class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040022
Lei Zhang4afee172018-01-10 20:57:15 +000023std::wstring BufferToWString(const std::vector<char>& buf) {
24 return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000025}
26
Lei Zhang4afee172018-01-10 20:57:15 +000027std::string BufferToString(const std::vector<char>& buf) {
28 return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000029}
30
Lei Zhangab41f252018-12-23 03:10:50 +000031TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +000032 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
33 FPDF_PAGE page = LoadPage(0);
34 ASSERT_TRUE(page);
35
36 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
37
38 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
39 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
40 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
41 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
42 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
43
44 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
45
46 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
47
48 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
49 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
50 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
51
52 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
53
54 static constexpr wchar_t kContents[] = L"Bar";
55 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
56 GetFPDFWideString(kContents);
57 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
58
59 char buffer[128];
60 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
61 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
62 EXPECT_EQ(0u,
63 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
64
65 UnloadPage(page);
66}
67
Lei Zhangab41f252018-12-23 03:10:50 +000068TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -040069 // Open a file with one annotation and load its first page.
70 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000071 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040072 ASSERT_TRUE(page);
73
74 // This annotation has a malformed appearance stream, which does not have its
75 // normal appearance defined, only its rollover appearance. In this case, its
76 // normal appearance should be generated, allowing the highlight annotation to
77 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +000078 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +000079 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040080
81 UnloadPage(page);
82}
83
Lei Zhangab41f252018-12-23 03:10:50 +000084TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Ralf Sipplb3a52402018-03-19 23:30:28 +000085 const char md5_hash[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000086 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000087 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
88 FPDF_PAGE page = LoadPage(0);
89 ASSERT_TRUE(page);
90
Tom Sepeze08d2b12018-04-25 18:49:32 +000091 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Ralf Sipplb3a52402018-03-19 23:30:28 +000092 CompareBitmap(bitmap.get(), 595, 842, md5_hash);
93
94 UnloadPage(page);
95}
96
Lei Zhangab41f252018-12-23 03:10:50 +000097TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -040098 // Open a file with one annotation and load its first page.
99 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000100 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400101 ASSERT_TRUE(page);
102
103 // Check that there is a total of 1 annotation on its first page.
104 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
105
106 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000107 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000108 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000109 ASSERT_TRUE(annot);
110 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400111
Lei Zhanga21d5932018-02-05 18:28:38 +0000112 // Check that the annotation color is yellow.
113 unsigned int R;
114 unsigned int G;
115 unsigned int B;
116 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000117 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000118 &G, &B, &A));
119 EXPECT_EQ(255u, R);
120 EXPECT_EQ(255u, G);
121 EXPECT_EQ(0u, B);
122 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400123
Lei Zhanga21d5932018-02-05 18:28:38 +0000124 // Check that the author is correct.
125 static constexpr char kAuthorKey[] = "T";
126 EXPECT_EQ(FPDF_OBJECT_STRING,
127 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
128 unsigned long len =
129 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
130 std::vector<char> buf(len);
131 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
132 len));
133 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400134
Lei Zhanga21d5932018-02-05 18:28:38 +0000135 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000136 EXPECT_EQ(
137 FPDF_OBJECT_STRING,
138 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
139 len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kContents,
140 nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +0000141 buf.clear();
142 buf.resize(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000143 EXPECT_EQ(2690u,
144 FPDFAnnot_GetStringValue(
145 annot.get(), pdfium::annotation::kContents, buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +0000146 const wchar_t contents[] =
147 L"This is a note for that highlight annotation. Very long highlight "
148 "annotation. Long long long Long long longLong long longLong long "
149 "longLong long longLong long longLong long longLong long longLong long "
150 "longLong long longLong long longLong long longLong long longLong long "
151 "longLong long longLong long longLong long longLong long longLong long "
152 "longLong long longLong long longLong long longLong long longLong long "
153 "longLong long longLong long longLong long longLong long longLong long "
154 "longLong long longLong long longLong long longLong long longLong long "
155 "longLong long longLong long longLong long longLong long longLong long "
156 "longLong long longLong long longLong long longLong long longLong long "
157 "longLong long longLong long longLong long longLong long longLong long "
158 "longLong long longLong long longLong long longLong long longLong long "
159 "longLong long longLong long longLong long longLong long longLong long "
160 "longLong long longLong long longLong long longLong long longLong long "
161 "longLong long longLong long longLong long longLong long longLong long "
162 "longLong long longLong long longLong long longLong long longLong long "
163 "longLong long longLong long longLong long longLong long longLong long "
164 "longLong long longLong long longLong long longLong long longLong long "
165 "longLong long longLong long longLong long longLong long longLong long "
166 "longLong long long. END";
167 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400168
Lei Zhanga21d5932018-02-05 18:28:38 +0000169 // Check that the quadpoints are correct.
170 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000171 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000172 EXPECT_EQ(115.802643f, quadpoints.x1);
173 EXPECT_EQ(718.913940f, quadpoints.y1);
174 EXPECT_EQ(157.211182f, quadpoints.x4);
175 EXPECT_EQ(706.264465f, quadpoints.y4);
176 }
Tom Sepez507d0192018-11-07 16:37:51 +0000177 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400178}
179
Lei Zhangab41f252018-12-23 03:10:50 +0000180TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400181 // Open a file with three annotations and load its first page.
182 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000183 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400184 ASSERT_TRUE(page);
185
186 // Check that there is a total of 3 annotation on its first page.
187 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
188
Lei Zhanga21d5932018-02-05 18:28:38 +0000189 {
190 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000191 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000192 ASSERT_TRUE(annot);
193 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400194
Lei Zhanga21d5932018-02-05 18:28:38 +0000195 // Check that the annotation color is blue with opacity.
196 unsigned int R;
197 unsigned int G;
198 unsigned int B;
199 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000200 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000201 &G, &B, &A));
202 EXPECT_EQ(0u, R);
203 EXPECT_EQ(0u, G);
204 EXPECT_EQ(255u, B);
205 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400206
Lei Zhanga21d5932018-02-05 18:28:38 +0000207 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000208 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
209 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400210
Lei Zhanga21d5932018-02-05 18:28:38 +0000211 // Check that the rectange coordinates are correct.
212 // Note that upon rendering, the rectangle coordinates will be adjusted.
213 FS_RECTF rect;
214 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
215 EXPECT_EQ(351.820404f, rect.left);
216 EXPECT_EQ(583.830688f, rect.bottom);
217 EXPECT_EQ(475.336090f, rect.right);
218 EXPECT_EQ(681.535034f, rect.top);
219 }
Tom Sepezef43c262018-11-07 16:41:32 +0000220 {
221 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
222 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
223 }
Tom Sepez507d0192018-11-07 16:37:51 +0000224 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400225}
Jane Liu20eafda2017-06-07 10:33:24 -0400226
Lei Zhangab41f252018-12-23 03:10:50 +0000227TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400228 // Open a file with one annotation and load its first page.
229 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000230 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400231 ASSERT_TRUE(page);
232
233 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400234 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400235
236 UnloadPage(page);
237}
238
Lei Zhangab41f252018-12-23 03:10:50 +0000239TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400240 // Open a file with no annotation and load its first page.
241 ASSERT_TRUE(OpenDocument("hello_world.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 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
245
Lei Zhanga21d5932018-02-05 18:28:38 +0000246 {
247 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000248 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000249 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400250
Lei Zhanga21d5932018-02-05 18:28:38 +0000251 // Check that there is now 1 annotations on this page.
252 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400253
Lei Zhanga21d5932018-02-05 18:28:38 +0000254 // Check that the subtype of the annotation is correct.
255 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
256 }
Jane Liue10509a2017-06-20 16:47:41 -0400257
Lei Zhanga21d5932018-02-05 18:28:38 +0000258 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000259 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000260 ASSERT_TRUE(annot);
261 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400262
Lei Zhanga21d5932018-02-05 18:28:38 +0000263 // Set the color of the annotation.
264 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
265 102, 153, 204));
266 // Check that the color has been set correctly.
267 unsigned int R;
268 unsigned int G;
269 unsigned int B;
270 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000271 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000272 &G, &B, &A));
273 EXPECT_EQ(51u, R);
274 EXPECT_EQ(102u, G);
275 EXPECT_EQ(153u, B);
276 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400277
Lei Zhanga21d5932018-02-05 18:28:38 +0000278 // Change the color of the annotation.
279 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
280 153, 102, 51));
281 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000282 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000283 &G, &B, &A));
284 EXPECT_EQ(204u, R);
285 EXPECT_EQ(153u, G);
286 EXPECT_EQ(102u, B);
287 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400288
Lei Zhanga21d5932018-02-05 18:28:38 +0000289 // Set the annotation rectangle.
290 FS_RECTF rect;
291 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
292 EXPECT_EQ(0.f, rect.left);
293 EXPECT_EQ(0.f, rect.right);
294 rect.left = 35;
295 rect.bottom = 150;
296 rect.right = 53;
297 rect.top = 165;
298 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
299 // Check that the annotation rectangle has been set correctly.
300 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
301 EXPECT_EQ(35.f, rect.left);
302 EXPECT_EQ(150.f, rect.bottom);
303 EXPECT_EQ(53.f, rect.right);
304 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400305
Lei Zhanga21d5932018-02-05 18:28:38 +0000306 // Set the content of the annotation.
307 static constexpr wchar_t kContents[] =
308 L"Hello! This is a customized content.";
309 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
310 GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000311 ASSERT_TRUE(FPDFAnnot_SetStringValue(
312 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000313 // Check that the content has been set correctly.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000314 unsigned long len = FPDFAnnot_GetStringValue(
315 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +0000316 std::vector<char> buf(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000317 EXPECT_EQ(74u,
318 FPDFAnnot_GetStringValue(
319 annot.get(), pdfium::annotation::kContents, buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +0000320 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
321 }
Jane Liu20eafda2017-06-07 10:33:24 -0400322 UnloadPage(page);
323}
324
Lei Zhangab41f252018-12-23 03:10:50 +0000325TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400326 // Open a file with one annotation and load its first page.
327 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000328 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400329 ASSERT_TRUE(page);
330
331 // Check that there is a total of one annotation on its first page, and verify
332 // its quadpoints.
333 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400334 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000335 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000336 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000337 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000338 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000339 EXPECT_EQ(115.802643f, quadpoints.x1);
340 EXPECT_EQ(718.913940f, quadpoints.y1);
341 EXPECT_EQ(157.211182f, quadpoints.x4);
342 EXPECT_EQ(706.264465f, quadpoints.y4);
343 }
Jane Liu20eafda2017-06-07 10:33:24 -0400344
345 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000346 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000347 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000348 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
349 ASSERT_TRUE(annot);
350 quadpoints.x1 = 140.802643f;
351 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000352 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000353 }
Jane Liu20eafda2017-06-07 10:33:24 -0400354
355 // Save the document, closing the page and document.
356 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000357 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400358
359 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000360 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400361
Lei Zhang0b494052019-01-31 21:41:15 +0000362 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000363 page = LoadSavedPage(0);
364 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400365
366 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000367 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400368
Lei Zhanga21d5932018-02-05 18:28:38 +0000369 {
370 // Check that the second annotation is an underline annotation and verify
371 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000372 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000373 ASSERT_TRUE(new_annot);
374 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
375 FS_QUADPOINTSF new_quadpoints;
376 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000377 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000378 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
379 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
380 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
381 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
382 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400383
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000384 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400385 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400386}
Jane Liu06462752017-06-27 16:41:14 -0400387
Lei Zhangab41f252018-12-23 03:10:50 +0000388TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000389 // Open a file with four annotations and load its first page.
390 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
391 FPDF_PAGE page = LoadPage(0);
392 ASSERT_TRUE(page);
393 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
394
395 // Retrieve the highlight annotation.
396 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
397 ASSERT_TRUE(annot);
398 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
399
400 FS_QUADPOINTSF quadpoints;
401 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
402
403 {
404 // Verify the current one set of quadpoints.
405 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
406
407 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
408 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
409 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
410 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
411 }
412
413 {
414 // Update the quadpoints.
415 FS_QUADPOINTSF new_quadpoints = quadpoints;
416 new_quadpoints.y1 -= 20.f;
417 new_quadpoints.y2 -= 20.f;
418 new_quadpoints.y3 -= 20.f;
419 new_quadpoints.y4 -= 20.f;
420 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
421
422 // Verify added quadpoint set
423 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
424 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
425 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
426 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
427 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
428 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
429 }
430
431 {
432 // Append a new set of quadpoints.
433 FS_QUADPOINTSF new_quadpoints = quadpoints;
434 new_quadpoints.y1 += 20.f;
435 new_quadpoints.y2 += 20.f;
436 new_quadpoints.y3 += 20.f;
437 new_quadpoints.y4 += 20.f;
438 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
439
440 // Verify added quadpoint set
441 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
442 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
443 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
444 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
445 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
446 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
447 }
448
449 {
450 // Setting and getting quadpoints at out-of-bound index should fail
451 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
452 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
453 }
454
455 FPDFPage_CloseAnnot(annot);
456
457 // Retrieve the square annotation
458 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
459
460 {
461 // Check that attempting to set its quadpoints would fail
462 ASSERT_TRUE(squareAnnot);
463 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
464 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
465 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
466 }
467
468 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000469 UnloadPage(page);
470}
471
Lei Zhangab41f252018-12-23 03:10:50 +0000472TEST_F(FPDFAnnotEmbedderTest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400473#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400474 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
475 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
476 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400477#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000478 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
479 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
480 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400481#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000482 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
483 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
484 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400485#endif
486
Jane Liu06462752017-06-27 16:41:14 -0400487 // Open a file with four annotations and load its first page.
488 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000489 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400490 ASSERT_TRUE(page);
491 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
492
Jane Liub370e5a2017-08-16 13:24:58 -0400493 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000494 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000495 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000496 CompareBitmap(bitmap.get(), 612, 792, md5_original);
497 }
Jane Liub370e5a2017-08-16 13:24:58 -0400498
Jane Liu0c6b07d2017-08-15 10:50:22 -0400499 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400500 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000501
502 // Retrieve the highlight annotation which has its AP stream already defined.
503 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000504 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000505 ASSERT_TRUE(annot);
506 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
507
508 // Check that color cannot be set when an AP stream is defined already.
509 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
510 102, 153, 204));
511
512 // Verify its attachment points.
513 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000514 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000515 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
516 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
517 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
518 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
519
520 // Check that updating the attachment points would succeed.
521 quadpoints.x1 -= 50.f;
522 quadpoints.x2 -= 50.f;
523 quadpoints.x3 -= 50.f;
524 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000525 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000526 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000527 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000528 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
529 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
530 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
531 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
532
533 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000534 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000535 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000536 CompareBitmap(bitmap.get(), 612, 792, md5_original);
537 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000538
539 // Verify its annotation rectangle.
540 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
541 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
542 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
543 EXPECT_NEAR(136.325f, rect.right, 0.001f);
544 EXPECT_NEAR(721.292f, rect.top, 0.001f);
545
546 // Check that updating the rectangle would succeed.
547 rect.left -= 60.f;
548 rect.right -= 60.f;
549 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
550 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
551 EXPECT_EQ(rect.right, new_rect.right);
552 }
Jane Liu06462752017-06-27 16:41:14 -0400553
Jane Liub370e5a2017-08-16 13:24:58 -0400554 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000555 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000556 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000557 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
558 }
Jane Liub370e5a2017-08-16 13:24:58 -0400559
Lei Zhanga21d5932018-02-05 18:28:38 +0000560 {
561 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000562 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000563 ASSERT_TRUE(annot);
564 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400565
Lei Zhanga21d5932018-02-05 18:28:38 +0000566 // Check that updating the rectangle would succeed.
567 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
568 rect.left += 70.f;
569 rect.right += 70.f;
570 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
571 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
572 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400573
Lei Zhanga21d5932018-02-05 18:28:38 +0000574 // Check that updating the rectangle changes the square annotation's
575 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000576 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000577 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000578 }
Jane Liub370e5a2017-08-16 13:24:58 -0400579
Jane Liu06462752017-06-27 16:41:14 -0400580 UnloadPage(page);
581}
Jane Liu8ce58f52017-06-29 13:40:22 -0400582
Lei Zhangab41f252018-12-23 03:10:50 +0000583TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000584 // Open a file with multiline markup annotations.
585 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
586 FPDF_PAGE page = LoadPage(0);
587 ASSERT_TRUE(page);
588 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000589 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000590 ASSERT_TRUE(annot);
591
592 // This is a three line annotation.
593 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
594 }
595 UnloadPage(page);
596
597 // null annotation should return 0
598 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
599}
600
Lei Zhangab41f252018-12-23 03:10:50 +0000601TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400602 // Open a file with 3 annotations on its first page.
603 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000604 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400605 ASSERT_TRUE(page);
606 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
607
Jane Liu0c6b07d2017-08-15 10:50:22 -0400608 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400609
Lei Zhanga21d5932018-02-05 18:28:38 +0000610 // Check that the annotations have the expected rectangle coordinates.
611 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000612 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000613 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
614 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
615 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400616
Lei Zhanga21d5932018-02-05 18:28:38 +0000617 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000618 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000619 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
620 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
621 }
622
623 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000624 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000625 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
626 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
627 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400628
629 // Check that nothing happens when attempting to remove an annotation with an
630 // out-of-bound index.
631 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
632 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
633 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
634
635 // Remove the second annotation.
636 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
637 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
638 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
639
640 // Save the document, closing the page and document.
641 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000642 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400643
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400644 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400645 // Open the saved document.
646 std::string new_file = GetString();
647 FPDF_FILEACCESS file_access;
648 memset(&file_access, 0, sizeof(file_access));
649 file_access.m_FileLen = new_file.size();
650 file_access.m_GetBlock = GetBlockFromString;
651 file_access.m_Param = &new_file;
652 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
653 ASSERT_TRUE(new_doc);
654 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
655 ASSERT_TRUE(new_page);
656
657 // Check that the saved document has 2 annotations on the first page.
658 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
659
Lei Zhanga21d5932018-02-05 18:28:38 +0000660 // Check that the remaining 2 annotations are the original 1st and 3rd ones
661 // by verifying their rectangle coordinates.
662 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000663 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000664 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
665 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
666 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400667
Lei Zhanga21d5932018-02-05 18:28:38 +0000668 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000669 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000670 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
671 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
672 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400673 FPDF_ClosePage(new_page);
674 FPDF_CloseDocument(new_doc);
675}
Jane Liu8ce58f52017-06-29 13:40:22 -0400676
Lei Zhangab41f252018-12-23 03:10:50 +0000677TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400678#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400679 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000680 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
681 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
682 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000683#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
684 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
685 const char md5_modified_path[] = "c66293426cbf1f568502d1f7c0728fc7";
686 const char md5_two_paths[] = "122ac4625393b1dfe4be8707b73cbb13";
687 const char md5_new_annot[] = "253c7fd739646ba2568e1e8bfc782336";
Jane Liubaa7ff42017-06-29 19:18:23 -0400688#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000689 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000690 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
691 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
692 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400693#endif
694
695 // Open a file with two annotations and load its first page.
696 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000697 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400698 ASSERT_TRUE(page);
699 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
700
701 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000702 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000703 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000704 CompareBitmap(bitmap.get(), 595, 842, md5_original);
705 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400706
Lei Zhanga21d5932018-02-05 18:28:38 +0000707 {
708 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000709 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000710 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400711
Lei Zhanga21d5932018-02-05 18:28:38 +0000712 // Check that this annotation has one path object and retrieve it.
713 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000714 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000715 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
716 EXPECT_FALSE(path);
717 path = FPDFAnnot_GetObject(annot.get(), 0);
718 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
719 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400720
Lei Zhanga21d5932018-02-05 18:28:38 +0000721 // Modify the color of the path object.
722 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
723 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400724
Lei Zhanga21d5932018-02-05 18:28:38 +0000725 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000726 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000727 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000728 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
729 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400730
Lei Zhanga21d5932018-02-05 18:28:38 +0000731 // Add a second path object to the same annotation.
732 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
733 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
734 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
735 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
736 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
737 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
738 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400739
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000740 // The object is in the annontation, not in the page, so the page object
741 // array should not change.
742 ASSERT_EQ(32, FPDFPage_CountObjects(page));
743
Lei Zhanga21d5932018-02-05 18:28:38 +0000744 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000745 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000746 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000747 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
748 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400749
Lei Zhanga21d5932018-02-05 18:28:38 +0000750 // Delete the newly added path object.
751 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
752 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000753 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000754 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400755
756 // Check that the page renders the same as before.
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_modified_path);
760 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400761
Jane Liubaa7ff42017-06-29 19:18:23 -0400762 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400763
Lei Zhanga21d5932018-02-05 18:28:38 +0000764 {
765 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000766 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000767 ASSERT_TRUE(annot);
768 rect.left = 200.f;
769 rect.bottom = 400.f;
770 rect.right = 500.f;
771 rect.top = 600.f;
772 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400773
Lei Zhanga21d5932018-02-05 18:28:38 +0000774 // Add a new path to the annotation.
775 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
776 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
777 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
778 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
779 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
780 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
781 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
782 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
783 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
784 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
785
786 // Check that the annotation's bounding box came from its rectangle.
787 FS_RECTF new_rect;
788 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
789 EXPECT_EQ(rect.left, new_rect.left);
790 EXPECT_EQ(rect.bottom, new_rect.bottom);
791 EXPECT_EQ(rect.right, new_rect.right);
792 EXPECT_EQ(rect.top, new_rect.top);
793 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400794
795 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400796 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000797 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400798
799 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +0000800 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000801 page = LoadSavedPage(0);
802 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400803
Jane Liu36567742017-07-06 11:13:35 -0400804 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000805 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400806
Lei Zhanga21d5932018-02-05 18:28:38 +0000807 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000808 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000809 ASSERT_TRUE(annot);
810 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400811
Lei Zhanga21d5932018-02-05 18:28:38 +0000812 // Check that the new annotation's rectangle is as defined.
813 FS_RECTF new_rect;
814 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
815 EXPECT_EQ(rect.left, new_rect.left);
816 EXPECT_EQ(rect.bottom, new_rect.bottom);
817 EXPECT_EQ(rect.right, new_rect.right);
818 EXPECT_EQ(rect.top, new_rect.top);
819 }
820
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000821 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400822 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400823}
Jane Liub137e752017-07-05 15:04:33 -0400824
Lei Zhangab41f252018-12-23 03:10:50 +0000825TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400826 // Open a file with an annotation and load its first page.
827 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000828 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400829 ASSERT_TRUE(page);
830
831 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000832 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000833 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000834 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
835 }
Jane Liub137e752017-07-05 15:04:33 -0400836
Lei Zhanga21d5932018-02-05 18:28:38 +0000837 {
838 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000839 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000840 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400841
Lei Zhanga21d5932018-02-05 18:28:38 +0000842 // Check that the original flag values are as expected.
843 int flags = FPDFAnnot_GetFlags(annot.get());
844 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
845 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400846
Lei Zhanga21d5932018-02-05 18:28:38 +0000847 // Set the HIDDEN flag.
848 flags |= FPDF_ANNOT_FLAG_HIDDEN;
849 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
850 flags = FPDFAnnot_GetFlags(annot.get());
851 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
852 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400853
Lei Zhanga21d5932018-02-05 18:28:38 +0000854 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000855 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000856 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000857 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
858 }
Jane Liub137e752017-07-05 15:04:33 -0400859
Lei Zhanga21d5932018-02-05 18:28:38 +0000860 // Unset the HIDDEN flag.
861 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
862 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
863 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
864 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
865 flags = FPDFAnnot_GetFlags(annot.get());
866 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
867 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400868
Lei Zhanga21d5932018-02-05 18:28:38 +0000869 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000870 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000871 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000872 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
873 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000874 }
Jane Liub137e752017-07-05 15:04:33 -0400875
Jane Liub137e752017-07-05 15:04:33 -0400876 UnloadPage(page);
877}
Jane Liu36567742017-07-06 11:13:35 -0400878
Lei Zhangab41f252018-12-23 03:10:50 +0000879TEST_F(FPDFAnnotEmbedderTest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400880#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400881 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
882 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
883 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000884#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
885 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
886 const char md5_new_image[] = "d19c6fcfd9a170802fcfb9adfa13557e";
887 const char md5_modified_image[] = "1273cf2363570a50d1aa0c95b1318197";
Jane Liu36567742017-07-06 11:13:35 -0400888#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000889 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
890 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
891 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400892#endif
893
894 // Open a file with two annotations and load its first page.
895 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000896 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400897 ASSERT_TRUE(page);
898 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
899
900 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000901 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000902 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000903 CompareBitmap(bitmap.get(), 595, 842, md5_original);
904 }
Jane Liu36567742017-07-06 11:13:35 -0400905
Jane Liu36567742017-07-06 11:13:35 -0400906 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000907 FPDF_BITMAP image_bitmap;
908
909 {
910 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000911 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000912 ASSERT_TRUE(annot);
913 FS_RECTF rect;
914 rect.left = 200.f;
915 rect.bottom = 600.f;
916 rect.right = 400.f;
917 rect.top = 800.f;
918 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
919
920 // Add a solid-color translucent image object to the new annotation.
921 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
922 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
923 0xeeeecccc);
924 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
925 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
926 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
927 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
928 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
929 kBitmapSize, 0, 0));
930 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
931 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
932 }
Jane Liu36567742017-07-06 11:13:35 -0400933
934 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000935 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000936 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000937 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
938 }
Jane Liu36567742017-07-06 11:13:35 -0400939
Lei Zhanga21d5932018-02-05 18:28:38 +0000940 {
941 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000942 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000943 ASSERT_TRUE(annot);
944 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
945 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
946 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400947
Lei Zhanga21d5932018-02-05 18:28:38 +0000948 // Modify the image in the new annotation.
949 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
950 0xff000000);
951 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
952 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
953 }
Jane Liu36567742017-07-06 11:13:35 -0400954
955 // Save the document, closing the page and document.
956 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000957 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400958 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400959
960 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400961 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400962}
963
Lei Zhangab41f252018-12-23 03:10:50 +0000964TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400965#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400966 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
967 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
968 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000969#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
970 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
971 const char md5_new_text[] = "554d625b52144816aaabb0dd66962c55";
972 const char md5_modified_text[] = "26e94fbd3af4b1e65479327507600114";
Jane Liu36567742017-07-06 11:13:35 -0400973#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000974 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
975 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
976 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400977#endif
978
979 // Open a file with two annotations and load its first page.
980 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000981 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400982 ASSERT_TRUE(page);
983 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
984
985 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000986 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000987 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000988 CompareBitmap(bitmap.get(), 595, 842, md5_original);
989 }
Jane Liu36567742017-07-06 11:13:35 -0400990
Lei Zhanga21d5932018-02-05 18:28:38 +0000991 {
992 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000993 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000994 ASSERT_TRUE(annot);
995 FS_RECTF rect;
996 rect.left = 200.f;
997 rect.bottom = 550.f;
998 rect.right = 450.f;
999 rect.top = 650.f;
1000 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001001
Lei Zhanga21d5932018-02-05 18:28:38 +00001002 // Add a translucent text object to the new annotation.
1003 FPDF_PAGEOBJECT text_object =
1004 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1005 EXPECT_TRUE(text_object);
1006 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1007 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1008 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1009 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
1010 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1011 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1012 }
Jane Liu36567742017-07-06 11:13:35 -04001013
1014 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001015 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001016 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001017 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
1018 }
Jane Liu36567742017-07-06 11:13:35 -04001019
Lei Zhanga21d5932018-02-05 18:28:38 +00001020 {
1021 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001022 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001023 ASSERT_TRUE(annot);
1024 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1025 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1026 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001027
Lei Zhanga21d5932018-02-05 18:28:38 +00001028 // Modify the text in the new annotation.
1029 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
1030 GetFPDFWideString(L"New text!");
1031 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1032 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1033 }
Jane Liu36567742017-07-06 11:13:35 -04001034
1035 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001036 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001037 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001038 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
1039 }
Jane Liu36567742017-07-06 11:13:35 -04001040
1041 // Remove the new annotation, and check that the page renders as before.
1042 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001043 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001044 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001045 CompareBitmap(bitmap.get(), 595, 842, md5_original);
1046 }
Jane Liu36567742017-07-06 11:13:35 -04001047
1048 UnloadPage(page);
1049}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001050
Lei Zhangab41f252018-12-23 03:10:50 +00001051TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001052 // Open a file with four annotations and load its first page.
1053 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001054 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001055 ASSERT_TRUE(page);
1056
Lei Zhanga21d5932018-02-05 18:28:38 +00001057 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001058
Lei Zhanga21d5932018-02-05 18:28:38 +00001059 {
1060 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001061 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001062 ASSERT_TRUE(annot);
1063
1064 // Check that a non-existent key does not exist.
1065 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1066
1067 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001068 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001069 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001070 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1071 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1072 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001073
1074 // Check that the string value of the hash is correct.
1075 static constexpr char kHashKey[] = "AAPL:Hash";
1076 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1077 unsigned long len =
1078 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1079 std::vector<char> buf(len);
1080 EXPECT_EQ(66u,
1081 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1082 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1083 BufferToWString(buf).c_str());
1084
1085 // Check that the string value of the modified date is correct.
1086 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001087 len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, nullptr,
1088 0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001089 buf.clear();
1090 buf.resize(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001091 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1092 buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +00001093 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1094
1095 // Update the date entry for the annotation.
1096 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1097 GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001098 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1099 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001100 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001101
1102 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001103 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001104 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001105
Dan Sinclair698aed72017-09-26 16:24:49 -04001106#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +03001107 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001108#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1109 const char md5[] = "9ee141f698c3fcb56c050dffd6c82624";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001110#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001111 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001112#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001113
1114 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001115 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001116 page = LoadSavedPage(0);
1117 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001118 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001119 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001120
Lei Zhanga21d5932018-02-05 18:28:38 +00001121 // Check that the string value of the modified date is the newly-set value.
1122 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001123 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
1124 unsigned long len = FPDFAnnot_GetStringValue(
1125 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001126 std::vector<char> buf(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001127 EXPECT_EQ(44u,
1128 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
1129 buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +00001130 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1131 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001132
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001133 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001134 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001135}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001136
Lei Zhangab41f252018-12-23 03:10:50 +00001137TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001138 // Open a file with four annotations and load its first page.
1139 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001140 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001141 ASSERT_TRUE(page);
1142
Lei Zhanga21d5932018-02-05 18:28:38 +00001143 {
1144 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001145 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001146 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001147
Lei Zhanga21d5932018-02-05 18:28:38 +00001148 // Check that the string value of an AP returns the expected length.
1149 unsigned long normal_len = FPDFAnnot_GetAP(
1150 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1151 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001152
Lei Zhanga21d5932018-02-05 18:28:38 +00001153 // Check that the string value of an AP is not returned if the buffer is too
1154 // small. The result buffer should be overwritten with an empty string.
1155 std::vector<char> buf(normal_len - 1);
1156 // Write L"z" in the buffer to verify it's not overwritten.
1157 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1158 EXPECT_EQ(73970u,
1159 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1160 buf.data(), buf.size()));
1161 std::string ap = BufferToString(buf);
1162 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001163
Lei Zhanga21d5932018-02-05 18:28:38 +00001164 // Check that the string value of an AP is returned through a buffer that is
1165 // the right size.
1166 buf.clear();
1167 buf.resize(normal_len);
1168 EXPECT_EQ(73970u,
1169 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1170 buf.data(), buf.size()));
1171 ap = BufferToString(buf);
1172 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1173 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001174
Lei Zhanga21d5932018-02-05 18:28:38 +00001175 // Check that the string value of an AP is returned through a buffer that is
1176 // larger than necessary.
1177 buf.clear();
1178 buf.resize(normal_len + 1);
1179 EXPECT_EQ(73970u,
1180 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1181 buf.data(), buf.size()));
1182 ap = BufferToString(buf);
1183 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1184 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001185
Lei Zhanga21d5932018-02-05 18:28:38 +00001186 // Check that getting an AP for a mode that does not have an AP returns an
1187 // empty string.
1188 unsigned long rollover_len = FPDFAnnot_GetAP(
1189 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1190 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001191
Lei Zhanga21d5932018-02-05 18:28:38 +00001192 buf.clear();
1193 buf.resize(1000);
1194 EXPECT_EQ(2u,
1195 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1196 buf.data(), buf.size()));
1197 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001198
Lei Zhanga21d5932018-02-05 18:28:38 +00001199 // Check that setting the AP for an invalid appearance mode fails.
1200 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1201 GetFPDFWideString(L"new test ap");
1202 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1203 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1204 apText.get()));
1205 EXPECT_FALSE(FPDFAnnot_SetAP(
1206 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001207
Lei Zhanga21d5932018-02-05 18:28:38 +00001208 // Set the AP correctly now.
1209 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1210 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001211
Lei Zhanga21d5932018-02-05 18:28:38 +00001212 // Check that the new annotation value is equal to the value we just set.
1213 rollover_len = FPDFAnnot_GetAP(
1214 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1215 EXPECT_EQ(24u, rollover_len);
1216 buf.clear();
1217 buf.resize(rollover_len);
1218 EXPECT_EQ(24u,
1219 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1220 buf.data(), buf.size()));
1221 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001222
Lei Zhanga21d5932018-02-05 18:28:38 +00001223 // Check that the Normal AP was not touched when the Rollover AP was set.
1224 buf.clear();
1225 buf.resize(normal_len);
1226 EXPECT_EQ(73970u,
1227 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1228 buf.data(), buf.size()));
1229 ap = BufferToString(buf);
1230 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1231 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1232 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001233
1234 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001235 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001236 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001237
Lei Zhang0b494052019-01-31 21:41:15 +00001238 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001239 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001240 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001241 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001242
Lei Zhanga21d5932018-02-05 18:28:38 +00001243 // Check that the new annotation value is equal to the value we set before
1244 // saving.
1245 unsigned long rollover_len = FPDFAnnot_GetAP(
1246 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1247 EXPECT_EQ(24u, rollover_len);
1248 std::vector<char> buf(rollover_len);
1249 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1250 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1251 buf.data(), buf.size()));
1252 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1253 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001254
1255 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001256 CloseSavedPage(page);
1257 CloseSavedDocument();
1258}
1259
Lei Zhangab41f252018-12-23 03:10:50 +00001260TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001261 // Open a file with four annotations and load its first page.
1262 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001263 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001264 ASSERT_TRUE(page);
1265
Lei Zhanga21d5932018-02-05 18:28:38 +00001266 {
1267 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001268 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001269 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001270
Lei Zhanga21d5932018-02-05 18:28:38 +00001271 // Set Down AP. Normal AP is already set.
1272 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1273 GetFPDFWideString(L"new test ap");
1274 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1275 apText.get()));
1276 EXPECT_EQ(73970u,
1277 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1278 nullptr, 0));
1279 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1280 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001281
Lei Zhanga21d5932018-02-05 18:28:38 +00001282 // Check that setting the Down AP to null removes the Down entry but keeps
1283 // Normal intact.
1284 EXPECT_TRUE(
1285 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1286 EXPECT_EQ(73970u,
1287 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1288 nullptr, 0));
1289 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1290 nullptr, 0));
1291 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001292
Lei Zhang75c81712018-02-08 17:22:39 +00001293 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001294}
1295
Lei Zhangab41f252018-12-23 03:10:50 +00001296TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001297 // Open a file with four annotations and load its first page.
1298 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001299 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001300 ASSERT_TRUE(page);
1301
Lei Zhanga21d5932018-02-05 18:28:38 +00001302 {
1303 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001304 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001305 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001306
Lei Zhanga21d5932018-02-05 18:28:38 +00001307 // Set Down AP. Normal AP is already set.
1308 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1309 GetFPDFWideString(L"new test ap");
1310 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1311 apText.get()));
1312 EXPECT_EQ(73970u,
1313 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1314 nullptr, 0));
1315 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1316 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001317
Lei Zhanga21d5932018-02-05 18:28:38 +00001318 // Check that setting the Normal AP to null removes the whole AP dictionary.
1319 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1320 nullptr));
1321 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1322 nullptr, 0));
1323 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1324 nullptr, 0));
1325 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001326
Lei Zhang75c81712018-02-08 17:22:39 +00001327 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001328}
1329
Lei Zhangab41f252018-12-23 03:10:50 +00001330TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001331 // Open a file with annotations and load its first page.
1332 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001333 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001334 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001335 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001336
Lei Zhanga21d5932018-02-05 18:28:38 +00001337 {
1338 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001339 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001340 ASSERT_TRUE(annot);
1341 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1342 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1343 static constexpr char kPopupKey[] = "Popup";
1344 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1345 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1346 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001347
Lei Zhanga21d5932018-02-05 18:28:38 +00001348 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001349 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001350 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1351 ASSERT_TRUE(popup);
1352 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1353 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1354 FS_RECTF rect;
1355 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1356 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1357 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001358
Lei Zhanga21d5932018-02-05 18:28:38 +00001359 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1360 // since "IRT" is not a key in |annot|'s dictionary.
1361 static constexpr char kIRTKey[] = "IRT";
1362 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1363 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001364
Lei Zhanga21d5932018-02-05 18:28:38 +00001365 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1366 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001367 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001368 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001369 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1370 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001371 }
Jane Liu300bb272017-08-21 14:37:53 -04001372
Jane Liu300bb272017-08-21 14:37:53 -04001373 UnloadPage(page);
1374}
1375
Lei Zhangab41f252018-12-23 03:10:50 +00001376TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001377 // Open file with form text fields.
1378 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001379 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001380 ASSERT_TRUE(page);
1381
Lei Zhanga21d5932018-02-05 18:28:38 +00001382 {
1383 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001384 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001385 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001386
Lei Zhanga21d5932018-02-05 18:28:38 +00001387 // Check that the flag values are as expected.
1388 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1389 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1390 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001391
Lei Zhanga21d5932018-02-05 18:28:38 +00001392 {
1393 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001394 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001395 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001396
Lei Zhanga21d5932018-02-05 18:28:38 +00001397 // Check that the flag values are as expected.
1398 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1399 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1400 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001401
1402 UnloadPage(page);
1403}
1404
Lei Zhangab41f252018-12-23 03:10:50 +00001405TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001406 // Open file with form text fields.
1407 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001408 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001409 ASSERT_TRUE(page);
1410
Lei Zhanga21d5932018-02-05 18:28:38 +00001411 {
1412 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001413 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001414 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001415
Lei Zhanga21d5932018-02-05 18:28:38 +00001416 // Check that the flag values are as expected.
1417 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1418 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1419 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1420 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1421 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001422
Lei Zhanga21d5932018-02-05 18:28:38 +00001423 {
1424 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001425 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
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.
1429 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1430 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1431 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1432 EXPECT_FALSE(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 third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001437 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
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.
1441 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1442 EXPECT_TRUE(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
1447 UnloadPage(page);
1448}
Diana Gage40870db2017-07-19 18:16:03 -07001449
Lei Zhangab41f252018-12-23 03:10:50 +00001450TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001451 // Open file with form text fields.
1452 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1453 FPDF_PAGE page = LoadPage(0);
1454 ASSERT_TRUE(page);
1455
1456 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang7557e7b2018-09-14 17:02:40 +00001457 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0));
1458
1459 {
1460 // Verify there is an annotation.
1461 ScopedFPDFAnnotation annot(
1462 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120));
1463 EXPECT_TRUE(annot);
1464 }
1465
1466 // Try other bad inputs at a valid location.
1467 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120));
1468 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120));
1469 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120));
Diana Gage40870db2017-07-19 18:16:03 -07001470
1471 UnloadPage(page);
1472}
1473
Lei Zhangab41f252018-12-23 03:10:50 +00001474TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001475 // Open file with form text fields.
1476 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1477 FPDF_PAGE page = LoadPage(0);
1478 ASSERT_TRUE(page);
1479
Lei Zhanga21d5932018-02-05 18:28:38 +00001480 {
1481 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001482 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001483 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1484 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001485
Lei Zhanga21d5932018-02-05 18:28:38 +00001486 // Check that interactive form annotation flag values are as expected.
1487 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1488 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1489 }
Diana Gage40870db2017-07-19 18:16:03 -07001490
Lei Zhanga21d5932018-02-05 18:28:38 +00001491 {
1492 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001493 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001494 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1495 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001496
Lei Zhanga21d5932018-02-05 18:28:38 +00001497 // Check that interactive form annotation flag values are as expected.
1498 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1499 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1500 }
Diana Gage40870db2017-07-19 18:16:03 -07001501
1502 UnloadPage(page);
1503}
1504
Lei Zhangab41f252018-12-23 03:10:50 +00001505TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001506 // Open file with form comboboxes.
1507 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1508 FPDF_PAGE page = LoadPage(0);
1509 ASSERT_TRUE(page);
1510
Lei Zhanga21d5932018-02-05 18:28:38 +00001511 {
1512 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001513 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001514 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001515 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001516
Lei Zhanga21d5932018-02-05 18:28:38 +00001517 // Check that interactive form annotation flag values are as expected.
1518 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1519 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1520 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1521 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1522 }
Diana Gage40870db2017-07-19 18:16:03 -07001523
Lei Zhanga21d5932018-02-05 18:28:38 +00001524 {
1525 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001526 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001527 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001528 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001529
Lei Zhanga21d5932018-02-05 18:28:38 +00001530 // Check that interactive form annotation flag values are as expected.
1531 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1532 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1533 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1534 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1535 }
Diana Gage40870db2017-07-19 18:16:03 -07001536
Lei Zhanga21d5932018-02-05 18:28:38 +00001537 {
1538 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001539 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001540 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001541 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001542
Lei Zhanga21d5932018-02-05 18:28:38 +00001543 // Check that interactive form annotation flag values are as expected.
1544 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1545 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1546 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1547 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1548 }
Diana Gage40870db2017-07-19 18:16:03 -07001549
1550 UnloadPage(page);
1551}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001552
1553TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1554 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1555 FPDF_PAGE page = LoadPage(0);
1556 ASSERT_TRUE(page);
1557 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1558
1559 static const char kTestKey[] = "test";
1560 static constexpr wchar_t kData[] = L"\xf6\xe4";
1561 std::vector<char> buf(12);
1562
1563 {
1564 // Add a text annotation to the page.
1565 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1566 ASSERT_TRUE(annot);
1567 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1568 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1569
1570 // Make sure there is no test key, add set a value there, and read it back.
1571 std::fill(buf.begin(), buf.end(), 'x');
1572 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1573 buf.size()));
1574 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1575
1576 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1577 GetFPDFWideString(kData);
1578 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1579
1580 std::fill(buf.begin(), buf.end(), 'x');
1581 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1582 buf.size()));
1583 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1584 }
1585
Lei Zhang05ec64c2019-01-09 03:00:06 +00001586 {
1587 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1588 ASSERT_TRUE(annot);
1589 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1590 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1591 // Also do the same test for its appearance string.
1592 std::fill(buf.begin(), buf.end(), 'x');
1593 ASSERT_EQ(2u,
1594 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1595 buf.data(), buf.size()));
1596 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1597
1598 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1599 GetFPDFWideString(kData);
1600 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1601 text.get()));
1602
1603 std::fill(buf.begin(), buf.end(), 'x');
1604 ASSERT_EQ(6u,
1605 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1606 buf.data(), buf.size()));
1607 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1608 }
1609
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001610 UnloadPage(page);
1611
1612 {
1613 // Save a copy, open the copy, and check the annotation again.
1614 // Note that it renders the rotation.
1615 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001616 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001617 FPDF_PAGE saved_page = LoadSavedPage(0);
1618 ASSERT_TRUE(saved_page);
1619
Lei Zhang05ec64c2019-01-09 03:00:06 +00001620 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001621 {
1622 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1623 ASSERT_TRUE(annot);
1624 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1625
1626 std::fill(buf.begin(), buf.end(), 'x');
1627 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1628 buf.size()));
1629 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1630 }
1631
Lei Zhang05ec64c2019-01-09 03:00:06 +00001632 {
1633 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1634 ASSERT_TRUE(annot);
1635 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1636 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1637
1638 std::fill(buf.begin(), buf.end(), 'x');
1639 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1640 buf.size()));
1641 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1642 }
1643
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001644 CloseSavedPage(saved_page);
1645 CloseSavedDocument();
1646 }
1647}