blob: 777d821819cea9671d1390f1ecef067d3303a594 [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
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00005#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -04006#include <memory>
7#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04008#include <vector>
9
Jane Liubaa7ff42017-06-29 19:18:23 -040010#include "core/fxcrt/fx_system.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000011#include "public/cpp/fpdf_scopers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040012#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040013#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040014#include "public/fpdfview.h"
15#include "testing/embedder_test.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000016#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040017#include "testing/gtest/include/gtest/gtest.h"
18
Lei Zhangdf064df2017-08-31 02:33:27 -070019static constexpr char kContentsKey[] = "Contents";
20
Nicolas Pena3ff54002017-07-05 11:55:35 -040021class 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 Zhang7557e7b2018-09-14 17:02:40 +000031TEST_F(FPDFAnnotEmbeddertest, BadParams) {
32 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
Jane Liue17011d2017-06-21 12:18:37 -040068TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) {
69 // 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
Ralf Sipplb3a52402018-03-19 23:30:28 +000084TEST_F(FPDFAnnotEmbeddertest, RenderMultilineMarkupAnnotWithoutAP) {
85 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
Jane Liu4fd9a472017-06-01 18:56:09 -040097TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
98 // 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.
136 EXPECT_EQ(FPDF_OBJECT_STRING,
137 FPDFAnnot_GetValueType(annot.get(), kContentsKey));
138 len = FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
139 buf.clear();
140 buf.resize(len);
141 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
142 buf.data(), len));
143 const wchar_t contents[] =
144 L"This is a note for that highlight annotation. Very long highlight "
145 "annotation. Long long long Long long longLong long longLong long "
146 "longLong long longLong long longLong long longLong long longLong long "
147 "longLong long longLong long longLong long longLong long longLong long "
148 "longLong long longLong long longLong 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 long. END";
164 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400165
Lei Zhanga21d5932018-02-05 18:28:38 +0000166 // Check that the quadpoints are correct.
167 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000168 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000169 EXPECT_EQ(115.802643f, quadpoints.x1);
170 EXPECT_EQ(718.913940f, quadpoints.y1);
171 EXPECT_EQ(157.211182f, quadpoints.x4);
172 EXPECT_EQ(706.264465f, quadpoints.y4);
173 }
Tom Sepez507d0192018-11-07 16:37:51 +0000174 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400175}
176
177TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
178 // Open a file with three annotations and load its first page.
179 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000180 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400181 ASSERT_TRUE(page);
182
183 // Check that there is a total of 3 annotation on its first page.
184 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
185
Lei Zhanga21d5932018-02-05 18:28:38 +0000186 {
187 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000188 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000189 ASSERT_TRUE(annot);
190 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400191
Lei Zhanga21d5932018-02-05 18:28:38 +0000192 // Check that the annotation color is blue with opacity.
193 unsigned int R;
194 unsigned int G;
195 unsigned int B;
196 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000197 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000198 &G, &B, &A));
199 EXPECT_EQ(0u, R);
200 EXPECT_EQ(0u, G);
201 EXPECT_EQ(255u, B);
202 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400203
Lei Zhanga21d5932018-02-05 18:28:38 +0000204 // Check that there is no content.
205 EXPECT_EQ(2u,
206 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400207
Lei Zhanga21d5932018-02-05 18:28:38 +0000208 // Check that the rectange coordinates are correct.
209 // Note that upon rendering, the rectangle coordinates will be adjusted.
210 FS_RECTF rect;
211 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
212 EXPECT_EQ(351.820404f, rect.left);
213 EXPECT_EQ(583.830688f, rect.bottom);
214 EXPECT_EQ(475.336090f, rect.right);
215 EXPECT_EQ(681.535034f, rect.top);
216 }
Tom Sepez507d0192018-11-07 16:37:51 +0000217 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400218}
Jane Liu20eafda2017-06-07 10:33:24 -0400219
220TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
221 // Open a file with one annotation and load its first page.
222 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000223 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400224 ASSERT_TRUE(page);
225
226 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400227 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400228
229 UnloadPage(page);
230}
231
Jane Liud321ef92017-06-14 09:56:22 -0400232TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400233 // Open a file with no annotation and load its first page.
234 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000235 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400236 ASSERT_TRUE(page);
237 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
238
Lei Zhanga21d5932018-02-05 18:28:38 +0000239 {
240 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000241 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000242 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400243
Lei Zhanga21d5932018-02-05 18:28:38 +0000244 // Check that there is now 1 annotations on this page.
245 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400246
Lei Zhanga21d5932018-02-05 18:28:38 +0000247 // Check that the subtype of the annotation is correct.
248 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
249 }
Jane Liue10509a2017-06-20 16:47:41 -0400250
Lei Zhanga21d5932018-02-05 18:28:38 +0000251 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000252 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000253 ASSERT_TRUE(annot);
254 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400255
Lei Zhanga21d5932018-02-05 18:28:38 +0000256 // Set the color of the annotation.
257 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
258 102, 153, 204));
259 // Check that the color has been set correctly.
260 unsigned int R;
261 unsigned int G;
262 unsigned int B;
263 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000264 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000265 &G, &B, &A));
266 EXPECT_EQ(51u, R);
267 EXPECT_EQ(102u, G);
268 EXPECT_EQ(153u, B);
269 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400270
Lei Zhanga21d5932018-02-05 18:28:38 +0000271 // Change the color of the annotation.
272 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
273 153, 102, 51));
274 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000275 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000276 &G, &B, &A));
277 EXPECT_EQ(204u, R);
278 EXPECT_EQ(153u, G);
279 EXPECT_EQ(102u, B);
280 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400281
Lei Zhanga21d5932018-02-05 18:28:38 +0000282 // Set the annotation rectangle.
283 FS_RECTF rect;
284 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
285 EXPECT_EQ(0.f, rect.left);
286 EXPECT_EQ(0.f, rect.right);
287 rect.left = 35;
288 rect.bottom = 150;
289 rect.right = 53;
290 rect.top = 165;
291 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
292 // Check that the annotation rectangle has been set correctly.
293 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
294 EXPECT_EQ(35.f, rect.left);
295 EXPECT_EQ(150.f, rect.bottom);
296 EXPECT_EQ(53.f, rect.right);
297 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400298
Lei Zhanga21d5932018-02-05 18:28:38 +0000299 // Set the content of the annotation.
300 static constexpr wchar_t kContents[] =
301 L"Hello! This is a customized content.";
302 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
303 GetFPDFWideString(kContents);
304 ASSERT_TRUE(
305 FPDFAnnot_SetStringValue(annot.get(), kContentsKey, text.get()));
306 // Check that the content has been set correctly.
307 unsigned long len =
308 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
309 std::vector<char> buf(len);
310 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
311 buf.data(), len));
312 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
313 }
Jane Liu20eafda2017-06-07 10:33:24 -0400314 UnloadPage(page);
315}
316
317TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
318 // Open a file with one annotation and load its first page.
319 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000320 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400321 ASSERT_TRUE(page);
322
323 // Check that there is a total of one annotation on its first page, and verify
324 // its quadpoints.
325 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400326 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000327 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000328 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000329 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000330 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000331 EXPECT_EQ(115.802643f, quadpoints.x1);
332 EXPECT_EQ(718.913940f, quadpoints.y1);
333 EXPECT_EQ(157.211182f, quadpoints.x4);
334 EXPECT_EQ(706.264465f, quadpoints.y4);
335 }
Jane Liu20eafda2017-06-07 10:33:24 -0400336
337 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000338 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000339 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000340 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
341 ASSERT_TRUE(annot);
342 quadpoints.x1 = 140.802643f;
343 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000344 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000345 }
Jane Liu20eafda2017-06-07 10:33:24 -0400346
347 // Save the document, closing the page and document.
348 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000349 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400350
351 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000352 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400353
Tom Sepezb9c3e272018-08-14 18:22:06 +0000354 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000355 page = LoadSavedPage(0);
356 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400357
358 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000359 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400360
Lei Zhanga21d5932018-02-05 18:28:38 +0000361 {
362 // Check that the second annotation is an underline annotation and verify
363 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000364 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000365 ASSERT_TRUE(new_annot);
366 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
367 FS_QUADPOINTSF new_quadpoints;
368 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000369 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000370 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
371 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
372 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
373 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
374 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400375
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000376 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400377 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400378}
Jane Liu06462752017-06-27 16:41:14 -0400379
Ralf Sippl16381792018-04-12 21:20:26 +0000380TEST_F(FPDFAnnotEmbeddertest, GetAndSetQuadPoints) {
381 // Open a file with four annotations and load its first page.
382 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
383 FPDF_PAGE page = LoadPage(0);
384 ASSERT_TRUE(page);
385 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
386
387 // Retrieve the highlight annotation.
388 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
389 ASSERT_TRUE(annot);
390 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
391
392 FS_QUADPOINTSF quadpoints;
393 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
394
395 {
396 // Verify the current one set of quadpoints.
397 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
398
399 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
400 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
401 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
402 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
403 }
404
405 {
406 // Update the quadpoints.
407 FS_QUADPOINTSF new_quadpoints = quadpoints;
408 new_quadpoints.y1 -= 20.f;
409 new_quadpoints.y2 -= 20.f;
410 new_quadpoints.y3 -= 20.f;
411 new_quadpoints.y4 -= 20.f;
412 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
413
414 // Verify added quadpoint set
415 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
416 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
417 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
418 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
419 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
420 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
421 }
422
423 {
424 // Append a new set of quadpoints.
425 FS_QUADPOINTSF new_quadpoints = quadpoints;
426 new_quadpoints.y1 += 20.f;
427 new_quadpoints.y2 += 20.f;
428 new_quadpoints.y3 += 20.f;
429 new_quadpoints.y4 += 20.f;
430 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
431
432 // Verify added quadpoint set
433 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
434 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
435 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
436 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
437 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
438 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
439 }
440
441 {
442 // Setting and getting quadpoints at out-of-bound index should fail
443 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
444 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
445 }
446
447 FPDFPage_CloseAnnot(annot);
448
449 // Retrieve the square annotation
450 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
451
452 {
453 // Check that attempting to set its quadpoints would fail
454 ASSERT_TRUE(squareAnnot);
455 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
456 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
457 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
458 }
459
460 FPDFPage_CloseAnnot(squareAnnot);
461
462 UnloadPage(page);
463}
464
Jane Liu06462752017-06-27 16:41:14 -0400465TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400466#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400467 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
468 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
469 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400470#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000471 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
472 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
473 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400474#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000475 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
476 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
477 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400478#endif
479
Jane Liu06462752017-06-27 16:41:14 -0400480 // Open a file with four annotations and load its first page.
481 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000482 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400483 ASSERT_TRUE(page);
484 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
485
Jane Liub370e5a2017-08-16 13:24:58 -0400486 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000487 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000488 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000489 CompareBitmap(bitmap.get(), 612, 792, md5_original);
490 }
Jane Liub370e5a2017-08-16 13:24:58 -0400491
Jane Liu0c6b07d2017-08-15 10:50:22 -0400492 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400493 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000494
495 // Retrieve the highlight annotation which has its AP stream already defined.
496 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000497 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000498 ASSERT_TRUE(annot);
499 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
500
501 // Check that color cannot be set when an AP stream is defined already.
502 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
503 102, 153, 204));
504
505 // Verify its attachment points.
506 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000507 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000508 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
509 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
510 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
511 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
512
513 // Check that updating the attachment points would succeed.
514 quadpoints.x1 -= 50.f;
515 quadpoints.x2 -= 50.f;
516 quadpoints.x3 -= 50.f;
517 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000518 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000519 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000520 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000521 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
522 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
523 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
524 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
525
526 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000527 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000528 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000529 CompareBitmap(bitmap.get(), 612, 792, md5_original);
530 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000531
532 // Verify its annotation rectangle.
533 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
534 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
535 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
536 EXPECT_NEAR(136.325f, rect.right, 0.001f);
537 EXPECT_NEAR(721.292f, rect.top, 0.001f);
538
539 // Check that updating the rectangle would succeed.
540 rect.left -= 60.f;
541 rect.right -= 60.f;
542 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
543 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
544 EXPECT_EQ(rect.right, new_rect.right);
545 }
Jane Liu06462752017-06-27 16:41:14 -0400546
Jane Liub370e5a2017-08-16 13:24:58 -0400547 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000548 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000549 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000550 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
551 }
Jane Liub370e5a2017-08-16 13:24:58 -0400552
Lei Zhanga21d5932018-02-05 18:28:38 +0000553 {
554 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000555 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000556 ASSERT_TRUE(annot);
557 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400558
Lei Zhanga21d5932018-02-05 18:28:38 +0000559 // Check that updating the rectangle would succeed.
560 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
561 rect.left += 70.f;
562 rect.right += 70.f;
563 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
564 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
565 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400566
Lei Zhanga21d5932018-02-05 18:28:38 +0000567 // Check that updating the rectangle changes the square annotation's
568 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000569 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000570 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000571 }
Jane Liub370e5a2017-08-16 13:24:58 -0400572
Jane Liu06462752017-06-27 16:41:14 -0400573 UnloadPage(page);
574}
Jane Liu8ce58f52017-06-29 13:40:22 -0400575
Henrique Nakashima5098b252018-03-26 21:46:00 +0000576TEST_F(FPDFAnnotEmbeddertest, CountAttachmentPoints) {
577 // Open a file with multiline markup annotations.
578 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
579 FPDF_PAGE page = LoadPage(0);
580 ASSERT_TRUE(page);
581 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000582 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000583 ASSERT_TRUE(annot);
584
585 // This is a three line annotation.
586 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
587 }
588 UnloadPage(page);
589
590 // null annotation should return 0
591 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
592}
593
Jane Liu8ce58f52017-06-29 13:40:22 -0400594TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
595 // Open a file with 3 annotations on its first page.
596 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000597 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400598 ASSERT_TRUE(page);
599 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
600
Jane Liu0c6b07d2017-08-15 10:50:22 -0400601 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400602
Lei Zhanga21d5932018-02-05 18:28:38 +0000603 // Check that the annotations have the expected rectangle coordinates.
604 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000605 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000606 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
607 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
608 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400609
Lei Zhanga21d5932018-02-05 18:28:38 +0000610 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000611 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000612 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
613 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
614 }
615
616 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000617 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000618 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
619 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
620 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400621
622 // Check that nothing happens when attempting to remove an annotation with an
623 // out-of-bound index.
624 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
625 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
626 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
627
628 // Remove the second annotation.
629 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
630 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
631 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
632
633 // Save the document, closing the page and document.
634 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000635 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400636
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400637 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400638 // Open the saved document.
639 std::string new_file = GetString();
640 FPDF_FILEACCESS file_access;
641 memset(&file_access, 0, sizeof(file_access));
642 file_access.m_FileLen = new_file.size();
643 file_access.m_GetBlock = GetBlockFromString;
644 file_access.m_Param = &new_file;
645 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
646 ASSERT_TRUE(new_doc);
647 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
648 ASSERT_TRUE(new_page);
649
650 // Check that the saved document has 2 annotations on the first page.
651 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
652
Lei Zhanga21d5932018-02-05 18:28:38 +0000653 // Check that the remaining 2 annotations are the original 1st and 3rd ones
654 // by verifying their rectangle coordinates.
655 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000656 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000657 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
658 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
659 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400660
Lei Zhanga21d5932018-02-05 18:28:38 +0000661 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000662 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000663 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
664 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
665 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400666 FPDF_ClosePage(new_page);
667 FPDF_CloseDocument(new_doc);
668}
Jane Liu8ce58f52017-06-29 13:40:22 -0400669
Jane Liubaa7ff42017-06-29 19:18:23 -0400670TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400671#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400672 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000673 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
674 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
675 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000676#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
677 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
678 const char md5_modified_path[] = "c66293426cbf1f568502d1f7c0728fc7";
679 const char md5_two_paths[] = "122ac4625393b1dfe4be8707b73cbb13";
680 const char md5_new_annot[] = "253c7fd739646ba2568e1e8bfc782336";
Jane Liubaa7ff42017-06-29 19:18:23 -0400681#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000682 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000683 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
684 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
685 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400686#endif
687
688 // Open a file with two annotations and load its first page.
689 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000690 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400691 ASSERT_TRUE(page);
692 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
693
694 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000695 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000696 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000697 CompareBitmap(bitmap.get(), 595, 842, md5_original);
698 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400699
Lei Zhanga21d5932018-02-05 18:28:38 +0000700 {
701 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000702 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000703 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400704
Lei Zhanga21d5932018-02-05 18:28:38 +0000705 // Check that this annotation has one path object and retrieve it.
706 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000707 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000708 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
709 EXPECT_FALSE(path);
710 path = FPDFAnnot_GetObject(annot.get(), 0);
711 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
712 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400713
Lei Zhanga21d5932018-02-05 18:28:38 +0000714 // Modify the color of the path object.
715 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
716 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400717
Lei Zhanga21d5932018-02-05 18:28:38 +0000718 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000719 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000720 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000721 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
722 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400723
Lei Zhanga21d5932018-02-05 18:28:38 +0000724 // Add a second path object to the same annotation.
725 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
726 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
727 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
728 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
729 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
730 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
731 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400732
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000733 // The object is in the annontation, not in the page, so the page object
734 // array should not change.
735 ASSERT_EQ(32, FPDFPage_CountObjects(page));
736
Lei Zhanga21d5932018-02-05 18:28:38 +0000737 // Check that the page with an annotation with two paths 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_two_paths);
741 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400742
Lei Zhanga21d5932018-02-05 18:28:38 +0000743 // Delete the newly added path object.
744 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
745 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000746 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000747 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400748
749 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000750 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000751 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000752 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
753 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400754
Jane Liubaa7ff42017-06-29 19:18:23 -0400755 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400756
Lei Zhanga21d5932018-02-05 18:28:38 +0000757 {
758 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000759 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000760 ASSERT_TRUE(annot);
761 rect.left = 200.f;
762 rect.bottom = 400.f;
763 rect.right = 500.f;
764 rect.top = 600.f;
765 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400766
Lei Zhanga21d5932018-02-05 18:28:38 +0000767 // Add a new path to the annotation.
768 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
769 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
770 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
771 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
772 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
773 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
774 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
775 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
776 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
777 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
778
779 // Check that the annotation's bounding box came from its rectangle.
780 FS_RECTF new_rect;
781 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
782 EXPECT_EQ(rect.left, new_rect.left);
783 EXPECT_EQ(rect.bottom, new_rect.bottom);
784 EXPECT_EQ(rect.right, new_rect.right);
785 EXPECT_EQ(rect.top, new_rect.top);
786 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400787
788 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400789 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000790 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400791
792 // Open the saved document.
Tom Sepezb9c3e272018-08-14 18:22:06 +0000793 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000794 page = LoadSavedPage(0);
795 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400796
Jane Liu36567742017-07-06 11:13:35 -0400797 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000798 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400799
Lei Zhanga21d5932018-02-05 18:28:38 +0000800 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000801 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000802 ASSERT_TRUE(annot);
803 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400804
Lei Zhanga21d5932018-02-05 18:28:38 +0000805 // Check that the new annotation's rectangle is as defined.
806 FS_RECTF new_rect;
807 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
808 EXPECT_EQ(rect.left, new_rect.left);
809 EXPECT_EQ(rect.bottom, new_rect.bottom);
810 EXPECT_EQ(rect.right, new_rect.right);
811 EXPECT_EQ(rect.top, new_rect.top);
812 }
813
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000814 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400815 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400816}
Jane Liub137e752017-07-05 15:04:33 -0400817
818TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
819 // Open a file with an annotation and load its first page.
820 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000821 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400822 ASSERT_TRUE(page);
823
824 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000825 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000826 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000827 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
828 }
Jane Liub137e752017-07-05 15:04:33 -0400829
Lei Zhanga21d5932018-02-05 18:28:38 +0000830 {
831 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000832 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000833 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400834
Lei Zhanga21d5932018-02-05 18:28:38 +0000835 // Check that the original flag values are as expected.
836 int flags = FPDFAnnot_GetFlags(annot.get());
837 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
838 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400839
Lei Zhanga21d5932018-02-05 18:28:38 +0000840 // Set the HIDDEN flag.
841 flags |= FPDF_ANNOT_FLAG_HIDDEN;
842 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
843 flags = FPDFAnnot_GetFlags(annot.get());
844 EXPECT_TRUE(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 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000848 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000849 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000850 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
851 }
Jane Liub137e752017-07-05 15:04:33 -0400852
Lei Zhanga21d5932018-02-05 18:28:38 +0000853 // Unset the HIDDEN flag.
854 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
855 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
856 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
857 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
858 flags = FPDFAnnot_GetFlags(annot.get());
859 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
860 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400861
Lei Zhanga21d5932018-02-05 18:28:38 +0000862 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000863 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000864 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000865 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
866 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000867 }
Jane Liub137e752017-07-05 15:04:33 -0400868
Jane Liub137e752017-07-05 15:04:33 -0400869 UnloadPage(page);
870}
Jane Liu36567742017-07-06 11:13:35 -0400871
872TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400873#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400874 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
875 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
876 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000877#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
878 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
879 const char md5_new_image[] = "d19c6fcfd9a170802fcfb9adfa13557e";
880 const char md5_modified_image[] = "1273cf2363570a50d1aa0c95b1318197";
Jane Liu36567742017-07-06 11:13:35 -0400881#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000882 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
883 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
884 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400885#endif
886
887 // Open a file with two annotations and load its first page.
888 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000889 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400890 ASSERT_TRUE(page);
891 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
892
893 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000894 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000895 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000896 CompareBitmap(bitmap.get(), 595, 842, md5_original);
897 }
Jane Liu36567742017-07-06 11:13:35 -0400898
Jane Liu36567742017-07-06 11:13:35 -0400899 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000900 FPDF_BITMAP image_bitmap;
901
902 {
903 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000904 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000905 ASSERT_TRUE(annot);
906 FS_RECTF rect;
907 rect.left = 200.f;
908 rect.bottom = 600.f;
909 rect.right = 400.f;
910 rect.top = 800.f;
911 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
912
913 // Add a solid-color translucent image object to the new annotation.
914 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
915 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
916 0xeeeecccc);
917 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
918 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
919 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
920 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
921 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
922 kBitmapSize, 0, 0));
923 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
924 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
925 }
Jane Liu36567742017-07-06 11:13:35 -0400926
927 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000928 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000929 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000930 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
931 }
Jane Liu36567742017-07-06 11:13:35 -0400932
Lei Zhanga21d5932018-02-05 18:28:38 +0000933 {
934 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000935 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000936 ASSERT_TRUE(annot);
937 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
938 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
939 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400940
Lei Zhanga21d5932018-02-05 18:28:38 +0000941 // Modify the image in the new annotation.
942 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
943 0xff000000);
944 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
945 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
946 }
Jane Liu36567742017-07-06 11:13:35 -0400947
948 // Save the document, closing the page and document.
949 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000950 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400951 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400952
953 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400954 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400955}
956
957TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400958#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400959 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
960 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
961 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000962#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
963 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
964 const char md5_new_text[] = "554d625b52144816aaabb0dd66962c55";
965 const char md5_modified_text[] = "26e94fbd3af4b1e65479327507600114";
Jane Liu36567742017-07-06 11:13:35 -0400966#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000967 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
968 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
969 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400970#endif
971
972 // Open a file with two annotations and load its first page.
973 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000974 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400975 ASSERT_TRUE(page);
976 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
977
978 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000979 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000980 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000981 CompareBitmap(bitmap.get(), 595, 842, md5_original);
982 }
Jane Liu36567742017-07-06 11:13:35 -0400983
Lei Zhanga21d5932018-02-05 18:28:38 +0000984 {
985 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000986 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000987 ASSERT_TRUE(annot);
988 FS_RECTF rect;
989 rect.left = 200.f;
990 rect.bottom = 550.f;
991 rect.right = 450.f;
992 rect.top = 650.f;
993 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -0400994
Lei Zhanga21d5932018-02-05 18:28:38 +0000995 // Add a translucent text object to the new annotation.
996 FPDF_PAGEOBJECT text_object =
997 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
998 EXPECT_TRUE(text_object);
999 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1000 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1001 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1002 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
1003 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1004 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1005 }
Jane Liu36567742017-07-06 11:13:35 -04001006
1007 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001008 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001009 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001010 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
1011 }
Jane Liu36567742017-07-06 11:13:35 -04001012
Lei Zhanga21d5932018-02-05 18:28:38 +00001013 {
1014 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001015 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001016 ASSERT_TRUE(annot);
1017 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1018 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1019 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001020
Lei Zhanga21d5932018-02-05 18:28:38 +00001021 // Modify the text in the new annotation.
1022 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
1023 GetFPDFWideString(L"New text!");
1024 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1025 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1026 }
Jane Liu36567742017-07-06 11:13:35 -04001027
1028 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001029 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001030 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001031 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
1032 }
Jane Liu36567742017-07-06 11:13:35 -04001033
1034 // Remove the new annotation, and check that the page renders as before.
1035 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
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_original);
1039 }
Jane Liu36567742017-07-06 11:13:35 -04001040
1041 UnloadPage(page);
1042}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001043
1044TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
1045 // Open a file with four annotations and load its first page.
1046 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001047 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001048 ASSERT_TRUE(page);
1049
Lei Zhangdf064df2017-08-31 02:33:27 -07001050 static constexpr char kDateKey[] = "M";
Lei Zhanga21d5932018-02-05 18:28:38 +00001051 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001052
Lei Zhanga21d5932018-02-05 18:28:38 +00001053 {
1054 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001055 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001056 ASSERT_TRUE(annot);
1057
1058 // Check that a non-existent key does not exist.
1059 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1060
1061 // Check that the string value of a non-string dictionary entry is empty.
1062 static constexpr char kApKey[] = "AP";
1063 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), kApKey));
1064 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1065 FPDFAnnot_GetValueType(annot.get(), kApKey));
1066 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kApKey, nullptr, 0));
1067
1068 // Check that the string value of the hash is correct.
1069 static constexpr char kHashKey[] = "AAPL:Hash";
1070 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1071 unsigned long len =
1072 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1073 std::vector<char> buf(len);
1074 EXPECT_EQ(66u,
1075 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1076 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1077 BufferToWString(buf).c_str());
1078
1079 // Check that the string value of the modified date is correct.
1080 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1081 len = FPDFAnnot_GetStringValue(annot.get(), kDateKey, nullptr, 0);
1082 buf.clear();
1083 buf.resize(len);
1084 EXPECT_EQ(44u,
1085 FPDFAnnot_GetStringValue(annot.get(), kDateKey, buf.data(), len));
1086 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1087
1088 // Update the date entry for the annotation.
1089 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1090 GetFPDFWideString(kNewDate);
1091 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kDateKey, text.get()));
1092 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001093
1094 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001095 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001096 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001097
Dan Sinclair698aed72017-09-26 16:24:49 -04001098#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +03001099 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001100#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1101 const char md5[] = "9ee141f698c3fcb56c050dffd6c82624";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001102#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001103 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001104#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001105
1106 // Open the saved annotation.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001107 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001108 page = LoadSavedPage(0);
1109 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001110 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001111 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001112
Lei Zhanga21d5932018-02-05 18:28:38 +00001113 // Check that the string value of the modified date is the newly-set value.
1114 EXPECT_EQ(FPDF_OBJECT_STRING,
1115 FPDFAnnot_GetValueType(new_annot.get(), kDateKey));
1116 unsigned long len =
1117 FPDFAnnot_GetStringValue(new_annot.get(), kDateKey, nullptr, 0);
1118 std::vector<char> buf(len);
1119 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot.get(), kDateKey,
1120 buf.data(), len));
1121 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1122 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001123
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001124 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001125 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001126}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001127
Henrique Nakashima5970a472018-01-11 22:40:59 +00001128TEST_F(FPDFAnnotEmbeddertest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001129 // Open a file with four annotations and load its first page.
1130 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001131 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001132 ASSERT_TRUE(page);
1133
Lei Zhanga21d5932018-02-05 18:28:38 +00001134 {
1135 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001136 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001137 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001138
Lei Zhanga21d5932018-02-05 18:28:38 +00001139 // Check that the string value of an AP returns the expected length.
1140 unsigned long normal_len = FPDFAnnot_GetAP(
1141 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1142 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001143
Lei Zhanga21d5932018-02-05 18:28:38 +00001144 // Check that the string value of an AP is not returned if the buffer is too
1145 // small. The result buffer should be overwritten with an empty string.
1146 std::vector<char> buf(normal_len - 1);
1147 // Write L"z" in the buffer to verify it's not overwritten.
1148 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1149 EXPECT_EQ(73970u,
1150 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1151 buf.data(), buf.size()));
1152 std::string ap = BufferToString(buf);
1153 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001154
Lei Zhanga21d5932018-02-05 18:28:38 +00001155 // Check that the string value of an AP is returned through a buffer that is
1156 // the right size.
1157 buf.clear();
1158 buf.resize(normal_len);
1159 EXPECT_EQ(73970u,
1160 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1161 buf.data(), buf.size()));
1162 ap = BufferToString(buf);
1163 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1164 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001165
Lei Zhanga21d5932018-02-05 18:28:38 +00001166 // Check that the string value of an AP is returned through a buffer that is
1167 // larger than necessary.
1168 buf.clear();
1169 buf.resize(normal_len + 1);
1170 EXPECT_EQ(73970u,
1171 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1172 buf.data(), buf.size()));
1173 ap = BufferToString(buf);
1174 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1175 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001176
Lei Zhanga21d5932018-02-05 18:28:38 +00001177 // Check that getting an AP for a mode that does not have an AP returns an
1178 // empty string.
1179 unsigned long rollover_len = FPDFAnnot_GetAP(
1180 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1181 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001182
Lei Zhanga21d5932018-02-05 18:28:38 +00001183 buf.clear();
1184 buf.resize(1000);
1185 EXPECT_EQ(2u,
1186 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1187 buf.data(), buf.size()));
1188 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001189
Lei Zhanga21d5932018-02-05 18:28:38 +00001190 // Check that setting the AP for an invalid appearance mode fails.
1191 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1192 GetFPDFWideString(L"new test ap");
1193 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1194 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1195 apText.get()));
1196 EXPECT_FALSE(FPDFAnnot_SetAP(
1197 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001198
Lei Zhanga21d5932018-02-05 18:28:38 +00001199 // Set the AP correctly now.
1200 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1201 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001202
Lei Zhanga21d5932018-02-05 18:28:38 +00001203 // Check that the new annotation value is equal to the value we just set.
1204 rollover_len = FPDFAnnot_GetAP(
1205 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1206 EXPECT_EQ(24u, rollover_len);
1207 buf.clear();
1208 buf.resize(rollover_len);
1209 EXPECT_EQ(24u,
1210 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1211 buf.data(), buf.size()));
1212 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001213
Lei Zhanga21d5932018-02-05 18:28:38 +00001214 // Check that the Normal AP was not touched when the Rollover AP was set.
1215 buf.clear();
1216 buf.resize(normal_len);
1217 EXPECT_EQ(73970u,
1218 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1219 buf.data(), buf.size()));
1220 ap = BufferToString(buf);
1221 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1222 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1223 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001224
1225 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001226 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001227 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001228
Tom Sepezb9c3e272018-08-14 18:22:06 +00001229 OpenSavedDocument(nullptr);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001230 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001231 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001232 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001233
Lei Zhanga21d5932018-02-05 18:28:38 +00001234 // Check that the new annotation value is equal to the value we set before
1235 // saving.
1236 unsigned long rollover_len = FPDFAnnot_GetAP(
1237 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1238 EXPECT_EQ(24u, rollover_len);
1239 std::vector<char> buf(rollover_len);
1240 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1241 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1242 buf.data(), buf.size()));
1243 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1244 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001245
1246 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001247 CloseSavedPage(page);
1248 CloseSavedDocument();
1249}
1250
1251TEST_F(FPDFAnnotEmbeddertest, RemoveOptionalAP) {
1252 // Open a file with four annotations and load its first page.
1253 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001254 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001255 ASSERT_TRUE(page);
1256
Lei Zhanga21d5932018-02-05 18:28:38 +00001257 {
1258 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001259 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001260 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001261
Lei Zhanga21d5932018-02-05 18:28:38 +00001262 // Set Down AP. Normal AP is already set.
1263 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1264 GetFPDFWideString(L"new test ap");
1265 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1266 apText.get()));
1267 EXPECT_EQ(73970u,
1268 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1269 nullptr, 0));
1270 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1271 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001272
Lei Zhanga21d5932018-02-05 18:28:38 +00001273 // Check that setting the Down AP to null removes the Down entry but keeps
1274 // Normal intact.
1275 EXPECT_TRUE(
1276 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1277 EXPECT_EQ(73970u,
1278 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1279 nullptr, 0));
1280 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1281 nullptr, 0));
1282 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001283
Lei Zhang75c81712018-02-08 17:22:39 +00001284 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001285}
1286
1287TEST_F(FPDFAnnotEmbeddertest, RemoveRequiredAP) {
1288 // Open a file with four annotations and load its first page.
1289 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001290 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001291 ASSERT_TRUE(page);
1292
Lei Zhanga21d5932018-02-05 18:28:38 +00001293 {
1294 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001295 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001296 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001297
Lei Zhanga21d5932018-02-05 18:28:38 +00001298 // Set Down AP. Normal AP is already set.
1299 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1300 GetFPDFWideString(L"new test ap");
1301 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1302 apText.get()));
1303 EXPECT_EQ(73970u,
1304 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1305 nullptr, 0));
1306 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1307 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001308
Lei Zhanga21d5932018-02-05 18:28:38 +00001309 // Check that setting the Normal AP to null removes the whole AP dictionary.
1310 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1311 nullptr));
1312 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1313 nullptr, 0));
1314 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1315 nullptr, 0));
1316 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001317
Lei Zhang75c81712018-02-08 17:22:39 +00001318 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001319}
1320
Jane Liu300bb272017-08-21 14:37:53 -04001321TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) {
1322 // Open a file with annotations and load its first page.
1323 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001324 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001325 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001326 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001327
Lei Zhanga21d5932018-02-05 18:28:38 +00001328 {
1329 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001330 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001331 ASSERT_TRUE(annot);
1332 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1333 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1334 static constexpr char kPopupKey[] = "Popup";
1335 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1336 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1337 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001338
Lei Zhanga21d5932018-02-05 18:28:38 +00001339 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001340 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001341 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1342 ASSERT_TRUE(popup);
1343 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1344 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1345 FS_RECTF rect;
1346 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1347 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1348 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001349
Lei Zhanga21d5932018-02-05 18:28:38 +00001350 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1351 // since "IRT" is not a key in |annot|'s dictionary.
1352 static constexpr char kIRTKey[] = "IRT";
1353 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1354 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001355
Lei Zhanga21d5932018-02-05 18:28:38 +00001356 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1357 // would fail, since its parent is not an annotation.
1358 static constexpr char kPKey[] = "P";
1359 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPKey));
1360 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1361 FPDFAnnot_GetValueType(annot.get(), kPKey));
1362 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kPKey));
1363 }
Jane Liu300bb272017-08-21 14:37:53 -04001364
Jane Liu300bb272017-08-21 14:37:53 -04001365 UnloadPage(page);
1366}
1367
Diana Gage7e0c05d2017-07-19 17:33:33 -07001368TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
1369 // Open file with form text fields.
1370 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001371 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001372 ASSERT_TRUE(page);
1373
Lei Zhanga21d5932018-02-05 18:28:38 +00001374 {
1375 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001376 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001377 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001378
Lei Zhanga21d5932018-02-05 18:28:38 +00001379 // Check that the flag values are as expected.
1380 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1381 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1382 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001383
Lei Zhanga21d5932018-02-05 18:28:38 +00001384 {
1385 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001386 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001387 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001388
Lei Zhanga21d5932018-02-05 18:28:38 +00001389 // Check that the flag values are as expected.
1390 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1391 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1392 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001393
1394 UnloadPage(page);
1395}
1396
1397TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
1398 // Open file with form text fields.
1399 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001400 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001401 ASSERT_TRUE(page);
1402
Lei Zhanga21d5932018-02-05 18:28:38 +00001403 {
1404 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001405 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001406 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001407
Lei Zhanga21d5932018-02-05 18:28:38 +00001408 // Check that the flag values are as expected.
1409 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1410 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1411 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1412 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1413 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001414
Lei Zhanga21d5932018-02-05 18:28:38 +00001415 {
1416 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001417 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001418 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001419
Lei Zhanga21d5932018-02-05 18:28:38 +00001420 // Check that the flag values are as expected.
1421 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1422 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1423 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1424 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1425 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001426
Lei Zhanga21d5932018-02-05 18:28:38 +00001427 {
1428 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001429 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001430 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001431
Lei Zhanga21d5932018-02-05 18:28:38 +00001432 // Check that the flag values are as expected.
1433 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1434 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1435 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1436 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1437 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001438
1439 UnloadPage(page);
1440}
Diana Gage40870db2017-07-19 18:16:03 -07001441
1442TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
1443 // Open file with form text fields.
1444 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1445 FPDF_PAGE page = LoadPage(0);
1446 ASSERT_TRUE(page);
1447
1448 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang7557e7b2018-09-14 17:02:40 +00001449 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0));
1450
1451 {
1452 // Verify there is an annotation.
1453 ScopedFPDFAnnotation annot(
1454 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120));
1455 EXPECT_TRUE(annot);
1456 }
1457
1458 // Try other bad inputs at a valid location.
1459 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120));
1460 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120));
1461 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120));
Diana Gage40870db2017-07-19 18:16:03 -07001462
1463 UnloadPage(page);
1464}
1465
1466TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
1467 // Open file with form text fields.
1468 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1469 FPDF_PAGE page = LoadPage(0);
1470 ASSERT_TRUE(page);
1471
Lei Zhanga21d5932018-02-05 18:28:38 +00001472 {
1473 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001474 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001475 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1476 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001477
Lei Zhanga21d5932018-02-05 18:28:38 +00001478 // Check that interactive form annotation flag values are as expected.
1479 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1480 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1481 }
Diana Gage40870db2017-07-19 18:16:03 -07001482
Lei Zhanga21d5932018-02-05 18:28:38 +00001483 {
1484 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001485 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001486 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1487 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001488
Lei Zhanga21d5932018-02-05 18:28:38 +00001489 // Check that interactive form annotation flag values are as expected.
1490 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1491 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1492 }
Diana Gage40870db2017-07-19 18:16:03 -07001493
1494 UnloadPage(page);
1495}
1496
1497TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1498 // Open file with form comboboxes.
1499 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1500 FPDF_PAGE page = LoadPage(0);
1501 ASSERT_TRUE(page);
1502
Lei Zhanga21d5932018-02-05 18:28:38 +00001503 {
1504 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001505 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001506 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001507 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.
1510 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1511 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1512 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1513 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1514 }
Diana Gage40870db2017-07-19 18:16:03 -07001515
Lei Zhanga21d5932018-02-05 18:28:38 +00001516 {
1517 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001518 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001519 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001520 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001521
Lei Zhanga21d5932018-02-05 18:28:38 +00001522 // Check that interactive form annotation flag values are as expected.
1523 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1524 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1525 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1526 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1527 }
Diana Gage40870db2017-07-19 18:16:03 -07001528
Lei Zhanga21d5932018-02-05 18:28:38 +00001529 {
1530 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001531 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001532 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001533 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001534
Lei Zhanga21d5932018-02-05 18:28:38 +00001535 // Check that interactive form annotation flag values are as expected.
1536 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1537 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1538 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1539 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1540 }
Diana Gage40870db2017-07-19 18:16:03 -07001541
1542 UnloadPage(page);
1543}