blob: 9a3e681270ca9dc87766731a04240dbef7cf731e [file] [log] [blame]
Jane Liu4fd9a472017-06-01 18:56:09 -04001// Copyright 2017 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00005#include <algorithm>
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00006#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -04007#include <memory>
8#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04009#include <vector>
10
Lei Zhanga5c1daf2019-01-31 21:56:47 +000011#include "constants/annotation_common.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040012#include "core/fxcrt/fx_system.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000013#include "public/cpp/fpdf_scopers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040014#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040015#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040016#include "public/fpdfview.h"
17#include "testing/embedder_test.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000018#include "testing/fx_string_testhelpers.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000019#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040020#include "testing/gtest/include/gtest/gtest.h"
21
Lei Zhangab41f252018-12-23 03:10:50 +000022class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040023
Lei Zhang4afee172018-01-10 20:57:15 +000024std::wstring BufferToWString(const std::vector<char>& buf) {
25 return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000026}
27
Lei Zhang4afee172018-01-10 20:57:15 +000028std::string BufferToString(const std::vector<char>& buf) {
29 return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000030}
31
Lei Zhangab41f252018-12-23 03:10:50 +000032TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +000033 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
34 FPDF_PAGE page = LoadPage(0);
35 ASSERT_TRUE(page);
36
37 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
38
39 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
40 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
41 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
42 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
43 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
44
45 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
46
47 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
48
49 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
50 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
51 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
52
53 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
54
55 static constexpr wchar_t kContents[] = L"Bar";
56 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
57 GetFPDFWideString(kContents);
58 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
59
60 char buffer[128];
61 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
62 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
63 EXPECT_EQ(0u,
64 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
65
66 UnloadPage(page);
67}
68
Lei Zhangab41f252018-12-23 03:10:50 +000069TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -040070 // Open a file with one annotation and load its first page.
71 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000072 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040073 ASSERT_TRUE(page);
74
75 // This annotation has a malformed appearance stream, which does not have its
76 // normal appearance defined, only its rollover appearance. In this case, its
77 // normal appearance should be generated, allowing the highlight annotation to
78 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +000079 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +000080 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040081
82 UnloadPage(page);
83}
84
Lei Zhangab41f252018-12-23 03:10:50 +000085TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Ralf Sipplb3a52402018-03-19 23:30:28 +000086 const char md5_hash[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000087 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000088 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
89 FPDF_PAGE page = LoadPage(0);
90 ASSERT_TRUE(page);
91
Tom Sepeze08d2b12018-04-25 18:49:32 +000092 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Ralf Sipplb3a52402018-03-19 23:30:28 +000093 CompareBitmap(bitmap.get(), 595, 842, md5_hash);
94
95 UnloadPage(page);
96}
97
Lei Zhangab41f252018-12-23 03:10:50 +000098TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -040099 // Open a file with one annotation and load its first page.
100 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000101 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400102 ASSERT_TRUE(page);
103
104 // Check that there is a total of 1 annotation on its first page.
105 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
106
107 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000108 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000109 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000110 ASSERT_TRUE(annot);
111 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400112
Lei Zhanga21d5932018-02-05 18:28:38 +0000113 // Check that the annotation color is yellow.
114 unsigned int R;
115 unsigned int G;
116 unsigned int B;
117 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000118 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000119 &G, &B, &A));
120 EXPECT_EQ(255u, R);
121 EXPECT_EQ(255u, G);
122 EXPECT_EQ(0u, B);
123 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400124
Lei Zhanga21d5932018-02-05 18:28:38 +0000125 // Check that the author is correct.
126 static constexpr char kAuthorKey[] = "T";
127 EXPECT_EQ(FPDF_OBJECT_STRING,
128 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
129 unsigned long len =
130 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
131 std::vector<char> buf(len);
132 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
133 len));
134 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400135
Lei Zhanga21d5932018-02-05 18:28:38 +0000136 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000137 EXPECT_EQ(
138 FPDF_OBJECT_STRING,
139 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
140 len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kContents,
141 nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +0000142 buf.clear();
143 buf.resize(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000144 EXPECT_EQ(2690u,
145 FPDFAnnot_GetStringValue(
146 annot.get(), pdfium::annotation::kContents, buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +0000147 const wchar_t contents[] =
148 L"This is a note for that highlight annotation. Very long highlight "
149 "annotation. Long long long Long 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 longLong long longLong long longLong long longLong long "
167 "longLong long long. END";
168 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400169
Lei Zhanga21d5932018-02-05 18:28:38 +0000170 // Check that the quadpoints are correct.
171 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000172 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000173 EXPECT_EQ(115.802643f, quadpoints.x1);
174 EXPECT_EQ(718.913940f, quadpoints.y1);
175 EXPECT_EQ(157.211182f, quadpoints.x4);
176 EXPECT_EQ(706.264465f, quadpoints.y4);
177 }
Tom Sepez507d0192018-11-07 16:37:51 +0000178 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400179}
180
Lei Zhangab41f252018-12-23 03:10:50 +0000181TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400182 // Open a file with three annotations and load its first page.
183 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000184 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400185 ASSERT_TRUE(page);
186
187 // Check that there is a total of 3 annotation on its first page.
188 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
189
Lei Zhanga21d5932018-02-05 18:28:38 +0000190 {
191 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000192 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000193 ASSERT_TRUE(annot);
194 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400195
Lei Zhanga21d5932018-02-05 18:28:38 +0000196 // Check that the annotation color is blue with opacity.
197 unsigned int R;
198 unsigned int G;
199 unsigned int B;
200 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000201 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000202 &G, &B, &A));
203 EXPECT_EQ(0u, R);
204 EXPECT_EQ(0u, G);
205 EXPECT_EQ(255u, B);
206 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400207
Lei Zhanga21d5932018-02-05 18:28:38 +0000208 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000209 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
210 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400211
Lei Zhanga21d5932018-02-05 18:28:38 +0000212 // Check that the rectange coordinates are correct.
213 // Note that upon rendering, the rectangle coordinates will be adjusted.
214 FS_RECTF rect;
215 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
216 EXPECT_EQ(351.820404f, rect.left);
217 EXPECT_EQ(583.830688f, rect.bottom);
218 EXPECT_EQ(475.336090f, rect.right);
219 EXPECT_EQ(681.535034f, rect.top);
220 }
Tom Sepezef43c262018-11-07 16:41:32 +0000221 {
222 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
223 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
224 }
Tom Sepez507d0192018-11-07 16:37:51 +0000225 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400226}
Jane Liu20eafda2017-06-07 10:33:24 -0400227
Lei Zhangab41f252018-12-23 03:10:50 +0000228TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400229 // Open a file with one annotation and load its first page.
230 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000231 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400232 ASSERT_TRUE(page);
233
234 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400235 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400236
237 UnloadPage(page);
238}
239
Lei Zhangab41f252018-12-23 03:10:50 +0000240TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400241 // Open a file with no annotation and load its first page.
242 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000243 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400244 ASSERT_TRUE(page);
245 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
246
Lei Zhanga21d5932018-02-05 18:28:38 +0000247 {
248 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000249 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000250 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400251
Lei Zhanga21d5932018-02-05 18:28:38 +0000252 // Check that there is now 1 annotations on this page.
253 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400254
Lei Zhanga21d5932018-02-05 18:28:38 +0000255 // Check that the subtype of the annotation is correct.
256 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
257 }
Jane Liue10509a2017-06-20 16:47:41 -0400258
Lei Zhanga21d5932018-02-05 18:28:38 +0000259 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000260 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000261 ASSERT_TRUE(annot);
262 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400263
Lei Zhanga21d5932018-02-05 18:28:38 +0000264 // Set the color of the annotation.
265 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
266 102, 153, 204));
267 // Check that the color has been set correctly.
268 unsigned int R;
269 unsigned int G;
270 unsigned int B;
271 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000272 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000273 &G, &B, &A));
274 EXPECT_EQ(51u, R);
275 EXPECT_EQ(102u, G);
276 EXPECT_EQ(153u, B);
277 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400278
Lei Zhanga21d5932018-02-05 18:28:38 +0000279 // Change the color of the annotation.
280 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
281 153, 102, 51));
282 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000283 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000284 &G, &B, &A));
285 EXPECT_EQ(204u, R);
286 EXPECT_EQ(153u, G);
287 EXPECT_EQ(102u, B);
288 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400289
Lei Zhanga21d5932018-02-05 18:28:38 +0000290 // Set the annotation rectangle.
291 FS_RECTF rect;
292 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
293 EXPECT_EQ(0.f, rect.left);
294 EXPECT_EQ(0.f, rect.right);
295 rect.left = 35;
296 rect.bottom = 150;
297 rect.right = 53;
298 rect.top = 165;
299 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
300 // Check that the annotation rectangle has been set correctly.
301 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
302 EXPECT_EQ(35.f, rect.left);
303 EXPECT_EQ(150.f, rect.bottom);
304 EXPECT_EQ(53.f, rect.right);
305 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400306
Lei Zhanga21d5932018-02-05 18:28:38 +0000307 // Set the content of the annotation.
308 static constexpr wchar_t kContents[] =
309 L"Hello! This is a customized content.";
310 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
311 GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000312 ASSERT_TRUE(FPDFAnnot_SetStringValue(
313 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000314 // Check that the content has been set correctly.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000315 unsigned long len = FPDFAnnot_GetStringValue(
316 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +0000317 std::vector<char> buf(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000318 EXPECT_EQ(74u,
319 FPDFAnnot_GetStringValue(
320 annot.get(), pdfium::annotation::kContents, buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +0000321 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
322 }
Jane Liu20eafda2017-06-07 10:33:24 -0400323 UnloadPage(page);
324}
325
Lei Zhangab41f252018-12-23 03:10:50 +0000326TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400327 // Open a file with one annotation and load its first page.
328 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000329 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400330 ASSERT_TRUE(page);
331
332 // Check that there is a total of one annotation on its first page, and verify
333 // its quadpoints.
334 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400335 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000336 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000337 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000338 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000339 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000340 EXPECT_EQ(115.802643f, quadpoints.x1);
341 EXPECT_EQ(718.913940f, quadpoints.y1);
342 EXPECT_EQ(157.211182f, quadpoints.x4);
343 EXPECT_EQ(706.264465f, quadpoints.y4);
344 }
Jane Liu20eafda2017-06-07 10:33:24 -0400345
346 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000347 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000348 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000349 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
350 ASSERT_TRUE(annot);
351 quadpoints.x1 = 140.802643f;
352 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000353 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000354 }
Jane Liu20eafda2017-06-07 10:33:24 -0400355
356 // Save the document, closing the page and document.
357 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000358 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400359
360 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000361 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400362
Lei Zhang0b494052019-01-31 21:41:15 +0000363 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000364 page = LoadSavedPage(0);
365 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400366
367 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000368 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400369
Lei Zhanga21d5932018-02-05 18:28:38 +0000370 {
371 // Check that the second annotation is an underline annotation and verify
372 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000373 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000374 ASSERT_TRUE(new_annot);
375 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
376 FS_QUADPOINTSF new_quadpoints;
377 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000378 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000379 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
380 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
381 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
382 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
383 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400384
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000385 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400386 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400387}
Jane Liu06462752017-06-27 16:41:14 -0400388
Lei Zhangab41f252018-12-23 03:10:50 +0000389TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000390 // Open a file with four annotations and load its first page.
391 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
392 FPDF_PAGE page = LoadPage(0);
393 ASSERT_TRUE(page);
394 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
395
396 // Retrieve the highlight annotation.
397 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
398 ASSERT_TRUE(annot);
399 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
400
401 FS_QUADPOINTSF quadpoints;
402 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
403
404 {
405 // Verify the current one set of quadpoints.
406 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
407
408 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
409 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
410 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
411 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
412 }
413
414 {
415 // Update the quadpoints.
416 FS_QUADPOINTSF new_quadpoints = quadpoints;
417 new_quadpoints.y1 -= 20.f;
418 new_quadpoints.y2 -= 20.f;
419 new_quadpoints.y3 -= 20.f;
420 new_quadpoints.y4 -= 20.f;
421 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
422
423 // Verify added quadpoint set
424 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
425 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
426 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
427 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
428 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
429 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
430 }
431
432 {
433 // Append a new set of quadpoints.
434 FS_QUADPOINTSF new_quadpoints = quadpoints;
435 new_quadpoints.y1 += 20.f;
436 new_quadpoints.y2 += 20.f;
437 new_quadpoints.y3 += 20.f;
438 new_quadpoints.y4 += 20.f;
439 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
440
441 // Verify added quadpoint set
442 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
443 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
444 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
445 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
446 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
447 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
448 }
449
450 {
451 // Setting and getting quadpoints at out-of-bound index should fail
452 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
453 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
454 }
455
456 FPDFPage_CloseAnnot(annot);
457
458 // Retrieve the square annotation
459 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
460
461 {
462 // Check that attempting to set its quadpoints would fail
463 ASSERT_TRUE(squareAnnot);
464 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
465 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
466 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
467 }
468
469 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000470 UnloadPage(page);
471}
472
Lei Zhangab41f252018-12-23 03:10:50 +0000473TEST_F(FPDFAnnotEmbedderTest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400474#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400475 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
476 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
477 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400478#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000479 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
480 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
481 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400482#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000483 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
484 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
485 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400486#endif
487
Jane Liu06462752017-06-27 16:41:14 -0400488 // Open a file with four annotations and load its first page.
489 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000490 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400491 ASSERT_TRUE(page);
492 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
493
Jane Liub370e5a2017-08-16 13:24:58 -0400494 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000495 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000496 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000497 CompareBitmap(bitmap.get(), 612, 792, md5_original);
498 }
Jane Liub370e5a2017-08-16 13:24:58 -0400499
Jane Liu0c6b07d2017-08-15 10:50:22 -0400500 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400501 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000502
503 // Retrieve the highlight annotation which has its AP stream already defined.
504 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000505 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000506 ASSERT_TRUE(annot);
507 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
508
509 // Check that color cannot be set when an AP stream is defined already.
510 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
511 102, 153, 204));
512
513 // Verify its attachment points.
514 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000515 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000516 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
517 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
518 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
519 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
520
521 // Check that updating the attachment points would succeed.
522 quadpoints.x1 -= 50.f;
523 quadpoints.x2 -= 50.f;
524 quadpoints.x3 -= 50.f;
525 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000526 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000527 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000528 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000529 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
530 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
531 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
532 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
533
534 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000535 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000536 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000537 CompareBitmap(bitmap.get(), 612, 792, md5_original);
538 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000539
540 // Verify its annotation rectangle.
541 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
542 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
543 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
544 EXPECT_NEAR(136.325f, rect.right, 0.001f);
545 EXPECT_NEAR(721.292f, rect.top, 0.001f);
546
547 // Check that updating the rectangle would succeed.
548 rect.left -= 60.f;
549 rect.right -= 60.f;
550 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
551 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
552 EXPECT_EQ(rect.right, new_rect.right);
553 }
Jane Liu06462752017-06-27 16:41:14 -0400554
Jane Liub370e5a2017-08-16 13:24:58 -0400555 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000556 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000557 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000558 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
559 }
Jane Liub370e5a2017-08-16 13:24:58 -0400560
Lei Zhanga21d5932018-02-05 18:28:38 +0000561 {
562 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000563 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000564 ASSERT_TRUE(annot);
565 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400566
Lei Zhanga21d5932018-02-05 18:28:38 +0000567 // Check that updating the rectangle would succeed.
568 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
569 rect.left += 70.f;
570 rect.right += 70.f;
571 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
572 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
573 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400574
Lei Zhanga21d5932018-02-05 18:28:38 +0000575 // Check that updating the rectangle changes the square annotation's
576 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000577 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000578 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000579 }
Jane Liub370e5a2017-08-16 13:24:58 -0400580
Jane Liu06462752017-06-27 16:41:14 -0400581 UnloadPage(page);
582}
Jane Liu8ce58f52017-06-29 13:40:22 -0400583
Lei Zhangab41f252018-12-23 03:10:50 +0000584TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000585 // Open a file with multiline markup annotations.
586 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
587 FPDF_PAGE page = LoadPage(0);
588 ASSERT_TRUE(page);
589 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000590 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000591 ASSERT_TRUE(annot);
592
593 // This is a three line annotation.
594 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
595 }
596 UnloadPage(page);
597
598 // null annotation should return 0
599 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
600}
601
Lei Zhangab41f252018-12-23 03:10:50 +0000602TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400603 // Open a file with 3 annotations on its first page.
604 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000605 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400606 ASSERT_TRUE(page);
607 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
608
Jane Liu0c6b07d2017-08-15 10:50:22 -0400609 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400610
Lei Zhanga21d5932018-02-05 18:28:38 +0000611 // Check that the annotations have the expected rectangle coordinates.
612 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000613 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000614 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
615 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
616 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400617
Lei Zhanga21d5932018-02-05 18:28:38 +0000618 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000619 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000620 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
621 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
622 }
623
624 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000625 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000626 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
627 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
628 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400629
630 // Check that nothing happens when attempting to remove an annotation with an
631 // out-of-bound index.
632 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
633 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
634 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
635
636 // Remove the second annotation.
637 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
638 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
639 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
640
641 // Save the document, closing the page and document.
642 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000643 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400644
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400645 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400646 // Open the saved document.
647 std::string new_file = GetString();
648 FPDF_FILEACCESS file_access;
649 memset(&file_access, 0, sizeof(file_access));
650 file_access.m_FileLen = new_file.size();
651 file_access.m_GetBlock = GetBlockFromString;
652 file_access.m_Param = &new_file;
653 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
654 ASSERT_TRUE(new_doc);
655 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
656 ASSERT_TRUE(new_page);
657
658 // Check that the saved document has 2 annotations on the first page.
659 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
660
Lei Zhanga21d5932018-02-05 18:28:38 +0000661 // Check that the remaining 2 annotations are the original 1st and 3rd ones
662 // by verifying their rectangle coordinates.
663 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000664 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000665 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
666 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
667 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400668
Lei Zhanga21d5932018-02-05 18:28:38 +0000669 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000670 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000671 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
672 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
673 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400674 FPDF_ClosePage(new_page);
675 FPDF_CloseDocument(new_doc);
676}
Jane Liu8ce58f52017-06-29 13:40:22 -0400677
Lei Zhangab41f252018-12-23 03:10:50 +0000678TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400679#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400680 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000681 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
682 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
683 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000684#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
685 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
686 const char md5_modified_path[] = "c66293426cbf1f568502d1f7c0728fc7";
687 const char md5_two_paths[] = "122ac4625393b1dfe4be8707b73cbb13";
688 const char md5_new_annot[] = "253c7fd739646ba2568e1e8bfc782336";
Jane Liubaa7ff42017-06-29 19:18:23 -0400689#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000690 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000691 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
692 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
693 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400694#endif
695
696 // Open a file with two annotations and load its first page.
697 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000698 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400699 ASSERT_TRUE(page);
700 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
701
702 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000703 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000704 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000705 CompareBitmap(bitmap.get(), 595, 842, md5_original);
706 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400707
Lei Zhanga21d5932018-02-05 18:28:38 +0000708 {
709 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000710 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000711 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400712
Lei Zhanga21d5932018-02-05 18:28:38 +0000713 // Check that this annotation has one path object and retrieve it.
714 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000715 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000716 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
717 EXPECT_FALSE(path);
718 path = FPDFAnnot_GetObject(annot.get(), 0);
719 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
720 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400721
Lei Zhanga21d5932018-02-05 18:28:38 +0000722 // Modify the color of the path object.
723 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
724 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400725
Lei Zhanga21d5932018-02-05 18:28:38 +0000726 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000727 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000728 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000729 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
730 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400731
Lei Zhanga21d5932018-02-05 18:28:38 +0000732 // Add a second path object to the same annotation.
733 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
734 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
735 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
736 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
737 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
738 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
739 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400740
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000741 // The object is in the annontation, not in the page, so the page object
742 // array should not change.
743 ASSERT_EQ(32, FPDFPage_CountObjects(page));
744
Lei Zhanga21d5932018-02-05 18:28:38 +0000745 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000746 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000747 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000748 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
749 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400750
Lei Zhanga21d5932018-02-05 18:28:38 +0000751 // Delete the newly added path object.
752 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
753 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000754 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000755 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400756
757 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000758 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000759 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000760 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
761 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400762
Jane Liubaa7ff42017-06-29 19:18:23 -0400763 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400764
Lei Zhanga21d5932018-02-05 18:28:38 +0000765 {
766 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000767 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000768 ASSERT_TRUE(annot);
769 rect.left = 200.f;
770 rect.bottom = 400.f;
771 rect.right = 500.f;
772 rect.top = 600.f;
773 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400774
Lei Zhanga21d5932018-02-05 18:28:38 +0000775 // Add a new path to the annotation.
776 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
777 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
778 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
779 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
780 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
781 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
782 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
783 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
784 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
785 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
786
787 // Check that the annotation's bounding box came from its rectangle.
788 FS_RECTF new_rect;
789 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
790 EXPECT_EQ(rect.left, new_rect.left);
791 EXPECT_EQ(rect.bottom, new_rect.bottom);
792 EXPECT_EQ(rect.right, new_rect.right);
793 EXPECT_EQ(rect.top, new_rect.top);
794 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400795
796 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400797 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000798 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400799
800 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +0000801 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000802 page = LoadSavedPage(0);
803 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400804
Jane Liu36567742017-07-06 11:13:35 -0400805 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000806 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400807
Lei Zhanga21d5932018-02-05 18:28:38 +0000808 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000809 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000810 ASSERT_TRUE(annot);
811 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400812
Lei Zhanga21d5932018-02-05 18:28:38 +0000813 // Check that the new annotation's rectangle is as defined.
814 FS_RECTF new_rect;
815 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
816 EXPECT_EQ(rect.left, new_rect.left);
817 EXPECT_EQ(rect.bottom, new_rect.bottom);
818 EXPECT_EQ(rect.right, new_rect.right);
819 EXPECT_EQ(rect.top, new_rect.top);
820 }
821
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000822 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400823 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400824}
Jane Liub137e752017-07-05 15:04:33 -0400825
Lei Zhangab41f252018-12-23 03:10:50 +0000826TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400827 // Open a file with an annotation and load its first page.
828 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000829 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400830 ASSERT_TRUE(page);
831
832 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000833 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000834 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000835 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
836 }
Jane Liub137e752017-07-05 15:04:33 -0400837
Lei Zhanga21d5932018-02-05 18:28:38 +0000838 {
839 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000840 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000841 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400842
Lei Zhanga21d5932018-02-05 18:28:38 +0000843 // Check that the original flag values are as expected.
844 int flags = FPDFAnnot_GetFlags(annot.get());
845 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
846 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400847
Lei Zhanga21d5932018-02-05 18:28:38 +0000848 // Set the HIDDEN flag.
849 flags |= FPDF_ANNOT_FLAG_HIDDEN;
850 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
851 flags = FPDFAnnot_GetFlags(annot.get());
852 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
853 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400854
Lei Zhanga21d5932018-02-05 18:28:38 +0000855 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000856 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000857 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000858 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
859 }
Jane Liub137e752017-07-05 15:04:33 -0400860
Lei Zhanga21d5932018-02-05 18:28:38 +0000861 // Unset the HIDDEN flag.
862 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
863 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
864 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
865 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
866 flags = FPDFAnnot_GetFlags(annot.get());
867 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
868 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400869
Lei Zhanga21d5932018-02-05 18:28:38 +0000870 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000871 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000872 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000873 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
874 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000875 }
Jane Liub137e752017-07-05 15:04:33 -0400876
Jane Liub137e752017-07-05 15:04:33 -0400877 UnloadPage(page);
878}
Jane Liu36567742017-07-06 11:13:35 -0400879
Lei Zhangab41f252018-12-23 03:10:50 +0000880TEST_F(FPDFAnnotEmbedderTest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400881#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400882 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
883 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
884 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000885#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
886 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
887 const char md5_new_image[] = "d19c6fcfd9a170802fcfb9adfa13557e";
888 const char md5_modified_image[] = "1273cf2363570a50d1aa0c95b1318197";
Jane Liu36567742017-07-06 11:13:35 -0400889#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000890 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
891 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
892 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400893#endif
894
895 // Open a file with two annotations and load its first page.
896 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000897 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400898 ASSERT_TRUE(page);
899 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
900
901 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000902 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000903 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000904 CompareBitmap(bitmap.get(), 595, 842, md5_original);
905 }
Jane Liu36567742017-07-06 11:13:35 -0400906
Jane Liu36567742017-07-06 11:13:35 -0400907 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000908 FPDF_BITMAP image_bitmap;
909
910 {
911 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000912 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000913 ASSERT_TRUE(annot);
914 FS_RECTF rect;
915 rect.left = 200.f;
916 rect.bottom = 600.f;
917 rect.right = 400.f;
918 rect.top = 800.f;
919 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
920
921 // Add a solid-color translucent image object to the new annotation.
922 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
923 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
924 0xeeeecccc);
925 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
926 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
927 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
928 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
929 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
930 kBitmapSize, 0, 0));
931 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
932 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
933 }
Jane Liu36567742017-07-06 11:13:35 -0400934
935 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000936 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000937 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000938 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
939 }
Jane Liu36567742017-07-06 11:13:35 -0400940
Lei Zhanga21d5932018-02-05 18:28:38 +0000941 {
942 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000943 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000944 ASSERT_TRUE(annot);
945 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
946 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
947 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400948
Lei Zhanga21d5932018-02-05 18:28:38 +0000949 // Modify the image in the new annotation.
950 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
951 0xff000000);
952 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
953 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
954 }
Jane Liu36567742017-07-06 11:13:35 -0400955
956 // Save the document, closing the page and document.
957 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000958 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400959 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400960
961 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400962 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400963}
964
Lei Zhangab41f252018-12-23 03:10:50 +0000965TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400966#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400967 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
968 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
969 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000970#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
971 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
972 const char md5_new_text[] = "554d625b52144816aaabb0dd66962c55";
973 const char md5_modified_text[] = "26e94fbd3af4b1e65479327507600114";
Jane Liu36567742017-07-06 11:13:35 -0400974#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000975 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
976 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
977 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400978#endif
979
980 // Open a file with two annotations and load its first page.
981 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000982 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400983 ASSERT_TRUE(page);
984 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
985
986 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000987 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000988 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000989 CompareBitmap(bitmap.get(), 595, 842, md5_original);
990 }
Jane Liu36567742017-07-06 11:13:35 -0400991
Lei Zhanga21d5932018-02-05 18:28:38 +0000992 {
993 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000994 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000995 ASSERT_TRUE(annot);
996 FS_RECTF rect;
997 rect.left = 200.f;
998 rect.bottom = 550.f;
999 rect.right = 450.f;
1000 rect.top = 650.f;
1001 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001002
Lei Zhanga21d5932018-02-05 18:28:38 +00001003 // Add a translucent text object to the new annotation.
1004 FPDF_PAGEOBJECT text_object =
1005 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1006 EXPECT_TRUE(text_object);
1007 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1008 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1009 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1010 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
1011 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1012 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1013 }
Jane Liu36567742017-07-06 11:13:35 -04001014
1015 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001016 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001017 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001018 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
1019 }
Jane Liu36567742017-07-06 11:13:35 -04001020
Lei Zhanga21d5932018-02-05 18:28:38 +00001021 {
1022 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001023 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001024 ASSERT_TRUE(annot);
1025 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1026 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1027 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001028
Lei Zhanga21d5932018-02-05 18:28:38 +00001029 // Modify the text in the new annotation.
1030 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
1031 GetFPDFWideString(L"New text!");
1032 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1033 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1034 }
Jane Liu36567742017-07-06 11:13:35 -04001035
1036 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001037 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001038 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001039 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
1040 }
Jane Liu36567742017-07-06 11:13:35 -04001041
1042 // Remove the new annotation, and check that the page renders as before.
1043 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001044 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001045 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001046 CompareBitmap(bitmap.get(), 595, 842, md5_original);
1047 }
Jane Liu36567742017-07-06 11:13:35 -04001048
1049 UnloadPage(page);
1050}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001051
Lei Zhangab41f252018-12-23 03:10:50 +00001052TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001053 // Open a file with four annotations and load its first page.
1054 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001055 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001056 ASSERT_TRUE(page);
1057
Lei Zhanga21d5932018-02-05 18:28:38 +00001058 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001059
Lei Zhanga21d5932018-02-05 18:28:38 +00001060 {
1061 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001062 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001063 ASSERT_TRUE(annot);
1064
1065 // Check that a non-existent key does not exist.
1066 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1067
1068 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001069 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001070 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001071 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1072 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1073 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001074
1075 // Check that the string value of the hash is correct.
1076 static constexpr char kHashKey[] = "AAPL:Hash";
1077 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1078 unsigned long len =
1079 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1080 std::vector<char> buf(len);
1081 EXPECT_EQ(66u,
1082 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1083 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1084 BufferToWString(buf).c_str());
1085
1086 // Check that the string value of the modified date is correct.
1087 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001088 len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, nullptr,
1089 0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001090 buf.clear();
1091 buf.resize(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001092 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1093 buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +00001094 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1095
1096 // Update the date entry for the annotation.
1097 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1098 GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001099 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1100 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001101 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001102
1103 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001104 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001105 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001106
Dan Sinclair698aed72017-09-26 16:24:49 -04001107#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +03001108 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001109#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1110 const char md5[] = "9ee141f698c3fcb56c050dffd6c82624";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001111#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001112 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001113#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001114
1115 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001116 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001117 page = LoadSavedPage(0);
1118 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001119 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001120 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001121
Lei Zhanga21d5932018-02-05 18:28:38 +00001122 // Check that the string value of the modified date is the newly-set value.
1123 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001124 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
1125 unsigned long len = FPDFAnnot_GetStringValue(
1126 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001127 std::vector<char> buf(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001128 EXPECT_EQ(44u,
1129 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
1130 buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +00001131 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1132 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001133
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001134 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001135 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001136}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001137
Lei Zhangab41f252018-12-23 03:10:50 +00001138TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001139 // Open a file with four annotations and load its first page.
1140 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001141 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001142 ASSERT_TRUE(page);
1143
Lei Zhanga21d5932018-02-05 18:28:38 +00001144 {
1145 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001146 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001147 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001148
Lei Zhanga21d5932018-02-05 18:28:38 +00001149 // Check that the string value of an AP returns the expected length.
1150 unsigned long normal_len = FPDFAnnot_GetAP(
1151 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1152 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001153
Lei Zhanga21d5932018-02-05 18:28:38 +00001154 // Check that the string value of an AP is not returned if the buffer is too
1155 // small. The result buffer should be overwritten with an empty string.
1156 std::vector<char> buf(normal_len - 1);
1157 // Write L"z" in the buffer to verify it's not overwritten.
1158 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1159 EXPECT_EQ(73970u,
1160 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1161 buf.data(), buf.size()));
1162 std::string ap = BufferToString(buf);
1163 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001164
Lei Zhanga21d5932018-02-05 18:28:38 +00001165 // Check that the string value of an AP is returned through a buffer that is
1166 // the right size.
1167 buf.clear();
1168 buf.resize(normal_len);
1169 EXPECT_EQ(73970u,
1170 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1171 buf.data(), buf.size()));
1172 ap = BufferToString(buf);
1173 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1174 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001175
Lei Zhanga21d5932018-02-05 18:28:38 +00001176 // Check that the string value of an AP is returned through a buffer that is
1177 // larger than necessary.
1178 buf.clear();
1179 buf.resize(normal_len + 1);
1180 EXPECT_EQ(73970u,
1181 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1182 buf.data(), buf.size()));
1183 ap = BufferToString(buf);
1184 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1185 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001186
Lei Zhanga21d5932018-02-05 18:28:38 +00001187 // Check that getting an AP for a mode that does not have an AP returns an
1188 // empty string.
1189 unsigned long rollover_len = FPDFAnnot_GetAP(
1190 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1191 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001192
Lei Zhanga21d5932018-02-05 18:28:38 +00001193 buf.clear();
1194 buf.resize(1000);
1195 EXPECT_EQ(2u,
1196 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1197 buf.data(), buf.size()));
1198 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001199
Lei Zhanga21d5932018-02-05 18:28:38 +00001200 // Check that setting the AP for an invalid appearance mode fails.
1201 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1202 GetFPDFWideString(L"new test ap");
1203 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1204 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1205 apText.get()));
1206 EXPECT_FALSE(FPDFAnnot_SetAP(
1207 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001208
Lei Zhanga21d5932018-02-05 18:28:38 +00001209 // Set the AP correctly now.
1210 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1211 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001212
Lei Zhanga21d5932018-02-05 18:28:38 +00001213 // Check that the new annotation value is equal to the value we just set.
1214 rollover_len = FPDFAnnot_GetAP(
1215 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1216 EXPECT_EQ(24u, rollover_len);
1217 buf.clear();
1218 buf.resize(rollover_len);
1219 EXPECT_EQ(24u,
1220 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1221 buf.data(), buf.size()));
1222 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001223
Lei Zhanga21d5932018-02-05 18:28:38 +00001224 // Check that the Normal AP was not touched when the Rollover AP was set.
1225 buf.clear();
1226 buf.resize(normal_len);
1227 EXPECT_EQ(73970u,
1228 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1229 buf.data(), buf.size()));
1230 ap = BufferToString(buf);
1231 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1232 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1233 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001234
1235 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001236 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001237 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001238
Lei Zhang0b494052019-01-31 21:41:15 +00001239 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001240 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001241 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001242 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001243
Lei Zhanga21d5932018-02-05 18:28:38 +00001244 // Check that the new annotation value is equal to the value we set before
1245 // saving.
1246 unsigned long rollover_len = FPDFAnnot_GetAP(
1247 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1248 EXPECT_EQ(24u, rollover_len);
1249 std::vector<char> buf(rollover_len);
1250 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1251 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1252 buf.data(), buf.size()));
1253 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1254 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001255
1256 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001257 CloseSavedPage(page);
1258 CloseSavedDocument();
1259}
1260
Lei Zhangab41f252018-12-23 03:10:50 +00001261TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001262 // Open a file with four annotations and load its first page.
1263 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001264 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001265 ASSERT_TRUE(page);
1266
Lei Zhanga21d5932018-02-05 18:28:38 +00001267 {
1268 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001269 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001270 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001271
Lei Zhanga21d5932018-02-05 18:28:38 +00001272 // Set Down AP. Normal AP is already set.
1273 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1274 GetFPDFWideString(L"new test ap");
1275 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1276 apText.get()));
1277 EXPECT_EQ(73970u,
1278 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1279 nullptr, 0));
1280 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1281 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001282
Lei Zhanga21d5932018-02-05 18:28:38 +00001283 // Check that setting the Down AP to null removes the Down entry but keeps
1284 // Normal intact.
1285 EXPECT_TRUE(
1286 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1287 EXPECT_EQ(73970u,
1288 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1289 nullptr, 0));
1290 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1291 nullptr, 0));
1292 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001293
Lei Zhang75c81712018-02-08 17:22:39 +00001294 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001295}
1296
Lei Zhangab41f252018-12-23 03:10:50 +00001297TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001298 // Open a file with four annotations and load its first page.
1299 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001300 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001301 ASSERT_TRUE(page);
1302
Lei Zhanga21d5932018-02-05 18:28:38 +00001303 {
1304 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001305 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001306 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001307
Lei Zhanga21d5932018-02-05 18:28:38 +00001308 // Set Down AP. Normal AP is already set.
1309 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1310 GetFPDFWideString(L"new test ap");
1311 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1312 apText.get()));
1313 EXPECT_EQ(73970u,
1314 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1315 nullptr, 0));
1316 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1317 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001318
Lei Zhanga21d5932018-02-05 18:28:38 +00001319 // Check that setting the Normal AP to null removes the whole AP dictionary.
1320 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1321 nullptr));
1322 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1323 nullptr, 0));
1324 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1325 nullptr, 0));
1326 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001327
Lei Zhang75c81712018-02-08 17:22:39 +00001328 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001329}
1330
Lei Zhangab41f252018-12-23 03:10:50 +00001331TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001332 // Open a file with annotations and load its first page.
1333 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001334 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001335 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001336 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001337
Lei Zhanga21d5932018-02-05 18:28:38 +00001338 {
1339 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001340 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001341 ASSERT_TRUE(annot);
1342 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1343 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1344 static constexpr char kPopupKey[] = "Popup";
1345 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1346 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1347 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001348
Lei Zhanga21d5932018-02-05 18:28:38 +00001349 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001350 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001351 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1352 ASSERT_TRUE(popup);
1353 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1354 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1355 FS_RECTF rect;
1356 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1357 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1358 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001359
Lei Zhanga21d5932018-02-05 18:28:38 +00001360 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1361 // since "IRT" is not a key in |annot|'s dictionary.
1362 static constexpr char kIRTKey[] = "IRT";
1363 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1364 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001365
Lei Zhanga21d5932018-02-05 18:28:38 +00001366 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1367 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001368 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001369 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001370 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1371 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001372 }
Jane Liu300bb272017-08-21 14:37:53 -04001373
Jane Liu300bb272017-08-21 14:37:53 -04001374 UnloadPage(page);
1375}
1376
Lei Zhangab41f252018-12-23 03:10:50 +00001377TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001378 // Open file with form text fields.
1379 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001380 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001381 ASSERT_TRUE(page);
1382
Lei Zhanga21d5932018-02-05 18:28:38 +00001383 {
1384 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001385 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001386 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001387
Lei Zhanga21d5932018-02-05 18:28:38 +00001388 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001389 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001390 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1391 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001392
Lei Zhanga21d5932018-02-05 18:28:38 +00001393 {
1394 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001395 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001396 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001397
Lei Zhanga21d5932018-02-05 18:28:38 +00001398 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001399 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001400 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1401 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001402
1403 UnloadPage(page);
1404}
1405
Lei Zhangab41f252018-12-23 03:10:50 +00001406TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001407 // Open file with form text fields.
1408 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001409 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001410 ASSERT_TRUE(page);
1411
Lei Zhanga21d5932018-02-05 18:28:38 +00001412 {
1413 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001414 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001415 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001416
Lei Zhanga21d5932018-02-05 18:28:38 +00001417 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001418 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001419 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1420 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1421 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1422 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001423
Lei Zhanga21d5932018-02-05 18:28:38 +00001424 {
1425 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001426 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001427 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001428
Lei Zhanga21d5932018-02-05 18:28:38 +00001429 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001430 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001431 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1432 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1433 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1434 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001435
Lei Zhanga21d5932018-02-05 18:28:38 +00001436 {
1437 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001438 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001439 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001440
Lei Zhanga21d5932018-02-05 18:28:38 +00001441 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001442 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001443 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1444 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1445 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1446 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001447
1448 UnloadPage(page);
1449}
Diana Gage40870db2017-07-19 18:16:03 -07001450
Lei Zhangab41f252018-12-23 03:10:50 +00001451TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001452 // Open file with form text fields.
1453 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1454 FPDF_PAGE page = LoadPage(0);
1455 ASSERT_TRUE(page);
1456
1457 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang7557e7b2018-09-14 17:02:40 +00001458 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0));
1459
1460 {
1461 // Verify there is an annotation.
1462 ScopedFPDFAnnotation annot(
1463 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120));
1464 EXPECT_TRUE(annot);
1465 }
1466
1467 // Try other bad inputs at a valid location.
1468 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120));
1469 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120));
1470 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120));
Diana Gage40870db2017-07-19 18:16:03 -07001471
1472 UnloadPage(page);
1473}
1474
Lei Zhangab41f252018-12-23 03:10:50 +00001475TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001476 // Open file with form text fields.
1477 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1478 FPDF_PAGE page = LoadPage(0);
1479 ASSERT_TRUE(page);
1480
Lei Zhanga21d5932018-02-05 18:28:38 +00001481 {
1482 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001483 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001484 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1485 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001486
Lei Zhanga21d5932018-02-05 18:28:38 +00001487 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001488 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001489 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1490 }
Diana Gage40870db2017-07-19 18:16:03 -07001491
Lei Zhanga21d5932018-02-05 18:28:38 +00001492 {
1493 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001494 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001495 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1496 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001497
Lei Zhanga21d5932018-02-05 18:28:38 +00001498 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001499 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001500 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1501 }
Diana Gage40870db2017-07-19 18:16:03 -07001502
1503 UnloadPage(page);
1504}
1505
Lei Zhangab41f252018-12-23 03:10:50 +00001506TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001507 // Open file with form comboboxes.
1508 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1509 FPDF_PAGE page = LoadPage(0);
1510 ASSERT_TRUE(page);
1511
Lei Zhanga21d5932018-02-05 18:28:38 +00001512 {
1513 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001514 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001515 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001516 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001517
Lei Zhanga21d5932018-02-05 18:28:38 +00001518 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001519 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001520 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1521 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1522 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1523 }
Diana Gage40870db2017-07-19 18:16:03 -07001524
Lei Zhanga21d5932018-02-05 18:28:38 +00001525 {
1526 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001527 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001528 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001529 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001530
Lei Zhanga21d5932018-02-05 18:28:38 +00001531 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001532 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001533 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1534 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1535 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1536 }
Diana Gage40870db2017-07-19 18:16:03 -07001537
Lei Zhanga21d5932018-02-05 18:28:38 +00001538 {
1539 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001540 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001541 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001542 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001543
Lei Zhanga21d5932018-02-05 18:28:38 +00001544 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001545 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001546 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1547 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1548 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1549 }
Diana Gage40870db2017-07-19 18:16:03 -07001550
1551 UnloadPage(page);
1552}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001553
Lei Zhang992e7e22019-02-04 19:20:58 +00001554TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
1555 static constexpr size_t kExpectedSize = 1609;
1556 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1557
1558 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1559
1560 FPDF_PAGE page = LoadPage(0);
1561 ASSERT_TRUE(page);
1562
1563 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1564 EXPECT_EQ(kExpectedSize, GetString().size());
1565 ClearString();
1566
1567 for (size_t i = 0; i < 10; ++i) {
1568 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1569 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
1570
1571 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1572 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
1573 // equal, not bigger.
1574 EXPECT_LT(kExpectedSize, GetString().size());
1575 ClearString();
1576 }
1577
1578 UnloadPage(page);
1579}
1580
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001581TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1582 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1583 FPDF_PAGE page = LoadPage(0);
1584 ASSERT_TRUE(page);
1585 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1586
1587 static const char kTestKey[] = "test";
1588 static constexpr wchar_t kData[] = L"\xf6\xe4";
1589 std::vector<char> buf(12);
1590
1591 {
1592 // Add a text annotation to the page.
1593 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1594 ASSERT_TRUE(annot);
1595 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1596 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1597
1598 // Make sure there is no test key, add set a value there, and read it back.
1599 std::fill(buf.begin(), buf.end(), 'x');
1600 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1601 buf.size()));
1602 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1603
1604 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1605 GetFPDFWideString(kData);
1606 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1607
1608 std::fill(buf.begin(), buf.end(), 'x');
1609 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1610 buf.size()));
1611 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1612 }
1613
Lei Zhang05ec64c2019-01-09 03:00:06 +00001614 {
1615 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1616 ASSERT_TRUE(annot);
1617 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1618 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1619 // Also do the same test for its appearance string.
1620 std::fill(buf.begin(), buf.end(), 'x');
1621 ASSERT_EQ(2u,
1622 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1623 buf.data(), buf.size()));
1624 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1625
1626 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1627 GetFPDFWideString(kData);
1628 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1629 text.get()));
1630
1631 std::fill(buf.begin(), buf.end(), 'x');
1632 ASSERT_EQ(6u,
1633 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1634 buf.data(), buf.size()));
1635 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1636 }
1637
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001638 UnloadPage(page);
1639
1640 {
1641 // Save a copy, open the copy, and check the annotation again.
1642 // Note that it renders the rotation.
1643 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001644 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001645 FPDF_PAGE saved_page = LoadSavedPage(0);
1646 ASSERT_TRUE(saved_page);
1647
Lei Zhang05ec64c2019-01-09 03:00:06 +00001648 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001649 {
1650 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1651 ASSERT_TRUE(annot);
1652 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1653
1654 std::fill(buf.begin(), buf.end(), 'x');
1655 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1656 buf.size()));
1657 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1658 }
1659
Lei Zhang05ec64c2019-01-09 03:00:06 +00001660 {
1661 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1662 ASSERT_TRUE(annot);
1663 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1664 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1665
1666 std::fill(buf.begin(), buf.end(), 'x');
1667 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1668 buf.size()));
1669 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1670 }
1671
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001672 CloseSavedPage(saved_page);
1673 CloseSavedDocument();
1674 }
1675}
rycsmithcb752f32019-02-21 18:40:53 +00001676
1677TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
1678 // Open a file with combobox widget annotations and load its first page.
1679 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1680 FPDF_PAGE page = LoadPage(0);
1681 ASSERT_TRUE(page);
1682
1683 {
1684 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1685 ASSERT_TRUE(annot);
1686
1687 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1688
1689 annot.reset(FPDFPage_GetAnnot(page, 1));
1690 ASSERT_TRUE(annot);
1691
1692 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1693 }
1694
1695 UnloadPage(page);
1696}
1697
1698TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
1699 // Open a file with listbox widget annotations and load its first page.
1700 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1701 FPDF_PAGE page = LoadPage(0);
1702 ASSERT_TRUE(page);
1703
1704 {
1705 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1706 ASSERT_TRUE(annot);
1707
1708 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1709
1710 annot.reset(FPDFPage_GetAnnot(page, 1));
1711 ASSERT_TRUE(annot);
1712
1713 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1714 }
1715
1716 UnloadPage(page);
1717}
1718
1719TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
1720 // Open a file with ink annotations and load its first page.
1721 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1722 FPDF_PAGE page = LoadPage(0);
1723 ASSERT_TRUE(page);
1724
1725 {
1726 // annotations do not have "Opt" array and will return -1
1727 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1728 ASSERT_TRUE(annot);
1729
1730 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1731
1732 annot.reset(FPDFPage_GetAnnot(page, 1));
1733 ASSERT_TRUE(annot);
1734
1735 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1736 }
1737
1738 UnloadPage(page);
1739}
1740
1741TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
1742 // Open a file with combobox widget annotations and load its first page.
1743 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1744 FPDF_PAGE page = LoadPage(0);
1745 ASSERT_TRUE(page);
1746
1747 {
1748 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1749 ASSERT_TRUE(annot);
1750
1751 int index = 0;
1752 unsigned long len =
1753 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1754 std::vector<char> buf(len);
1755 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1756 buf.data(), len));
1757 EXPECT_STREQ(L"Foo", BufferToWString(buf).c_str());
1758
1759 annot.reset(FPDFPage_GetAnnot(page, 1));
1760 ASSERT_TRUE(annot);
1761
1762 index = 0;
1763 len =
1764 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1765 buf.clear();
1766 buf.resize(len);
1767 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1768 buf.data(), len));
1769 EXPECT_STREQ(L"Apple", BufferToWString(buf).c_str());
1770
1771 index = 25;
1772 len =
1773 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1774 buf.clear();
1775 buf.resize(len);
1776 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1777 buf.data(), len));
1778 EXPECT_STREQ(L"Zucchini", BufferToWString(buf).c_str());
1779
1780 // indices out of range
1781 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1782 nullptr, 0));
1783 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1784 nullptr, 0));
1785 }
1786
1787 UnloadPage(page);
1788}
1789
1790TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
1791 // Open a file with listbox widget annotations and load its first page.
1792 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1793 FPDF_PAGE page = LoadPage(0);
1794 ASSERT_TRUE(page);
1795
1796 {
1797 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1798 ASSERT_TRUE(annot);
1799
1800 int index = 0;
1801 unsigned long len =
1802 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1803 std::vector<char> buf(len);
1804 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1805 buf.data(), len));
1806 EXPECT_STREQ(L"Foo", BufferToWString(buf).c_str());
1807
1808 annot.reset(FPDFPage_GetAnnot(page, 1));
1809 ASSERT_TRUE(annot);
1810
1811 index = 0;
1812 len =
1813 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1814 buf.clear();
1815 buf.resize(len);
1816 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1817 buf.data(), len));
1818 EXPECT_STREQ(L"Apple", BufferToWString(buf).c_str());
1819
1820 index = 25;
1821 len =
1822 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1823 buf.clear();
1824 buf.resize(len);
1825 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1826 buf.data(), len));
1827 EXPECT_STREQ(L"Zucchini", BufferToWString(buf).c_str());
1828
1829 // indices out of range
1830 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1831 nullptr, 0));
1832 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1833 nullptr, 0));
1834 }
1835
1836 UnloadPage(page);
1837}
1838
1839TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
1840 // Open a file with ink annotations and load its first page.
1841 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1842 FPDF_PAGE page = LoadPage(0);
1843 ASSERT_TRUE(page);
1844
1845 {
1846 // annotations do not have "Opt" array and will return 0
1847 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1848 ASSERT_TRUE(annot);
1849
1850 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1851 nullptr, 0));
1852
1853 annot.reset(FPDFPage_GetAnnot(page, 1));
1854 ASSERT_TRUE(annot);
1855
1856 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1857 nullptr, 0));
1858 }
1859
1860 UnloadPage(page);
1861}