blob: b97193db46317fbd70be04843e35412eb88a0b2b [file] [log] [blame]
Jane Liu4fd9a472017-06-01 18:56:09 -04001// Copyright 2017 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00005#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -04006#include <memory>
7#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04008#include <vector>
9
Jane Liubaa7ff42017-06-29 19:18:23 -040010#include "core/fxcrt/fx_system.h"
Lei Zhanga21d5932018-02-05 18:28:38 +000011#include "public/cpp/fpdf_deleters.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040012#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040013#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040014#include "public/fpdfview.h"
15#include "testing/embedder_test.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000016#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040017#include "testing/gtest/include/gtest/gtest.h"
18
Lei Zhangdf064df2017-08-31 02:33:27 -070019static constexpr char kContentsKey[] = "Contents";
20
Nicolas Pena3ff54002017-07-05 11:55:35 -040021class FPDFAnnotEmbeddertest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040022
Lei Zhang4afee172018-01-10 20:57:15 +000023std::wstring BufferToWString(const std::vector<char>& buf) {
24 return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000025}
26
Lei Zhang4afee172018-01-10 20:57:15 +000027std::string BufferToString(const std::vector<char>& buf) {
28 return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000029}
30
Jane Liue17011d2017-06-21 12:18:37 -040031TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) {
32 // Open a file with one annotation and load its first page.
33 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000034 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040035 ASSERT_TRUE(page);
36
37 // This annotation has a malformed appearance stream, which does not have its
38 // normal appearance defined, only its rollover appearance. In this case, its
39 // normal appearance should be generated, allowing the highlight annotation to
40 // still display.
Lei Zhanga98e3662018-02-07 20:28:35 +000041 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
42 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
43 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040044
45 UnloadPage(page);
46}
47
Ralf Sipplb3a52402018-03-19 23:30:28 +000048TEST_F(FPDFAnnotEmbeddertest, RenderMultilineMarkupAnnotWithoutAP) {
49 const char md5_hash[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000050 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000051 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
52 FPDF_PAGE page = LoadPage(0);
53 ASSERT_TRUE(page);
54
55 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
56 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
57 CompareBitmap(bitmap.get(), 595, 842, md5_hash);
58
59 UnloadPage(page);
60}
61
Jane Liu4fd9a472017-06-01 18:56:09 -040062TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
63 // Open a file with one annotation and load its first page.
64 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000065 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
66 // the FORM API calls in LoadPage() makes this test fail. So use
67 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu4fd9a472017-06-01 18:56:09 -040068 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
69 ASSERT_TRUE(page);
70
71 // Check that there is a total of 1 annotation on its first page.
72 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
73
74 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +000075 {
76 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
77 FPDFPage_GetAnnot(page, 0));
78 ASSERT_TRUE(annot);
79 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -040080
Lei Zhanga21d5932018-02-05 18:28:38 +000081 // Check that the annotation color is yellow.
82 unsigned int R;
83 unsigned int G;
84 unsigned int B;
85 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +000086 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +000087 &G, &B, &A));
88 EXPECT_EQ(255u, R);
89 EXPECT_EQ(255u, G);
90 EXPECT_EQ(0u, B);
91 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -040092
Lei Zhanga21d5932018-02-05 18:28:38 +000093 // Check that the author is correct.
94 static constexpr char kAuthorKey[] = "T";
95 EXPECT_EQ(FPDF_OBJECT_STRING,
96 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
97 unsigned long len =
98 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
99 std::vector<char> buf(len);
100 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
101 len));
102 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400103
Lei Zhanga21d5932018-02-05 18:28:38 +0000104 // Check that the content is correct.
105 EXPECT_EQ(FPDF_OBJECT_STRING,
106 FPDFAnnot_GetValueType(annot.get(), kContentsKey));
107 len = FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
108 buf.clear();
109 buf.resize(len);
110 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
111 buf.data(), len));
112 const wchar_t contents[] =
113 L"This is a note for that highlight annotation. Very long highlight "
114 "annotation. Long long long Long long longLong long longLong long "
115 "longLong long longLong long longLong long longLong long longLong long "
116 "longLong long longLong long longLong long longLong long longLong long "
117 "longLong long longLong long longLong long longLong long longLong long "
118 "longLong long longLong long longLong long longLong long longLong long "
119 "longLong long longLong long longLong long longLong long longLong long "
120 "longLong long longLong long longLong long longLong long longLong long "
121 "longLong long longLong long longLong long longLong long longLong long "
122 "longLong long longLong long longLong long longLong long longLong long "
123 "longLong long longLong long longLong long longLong long longLong long "
124 "longLong long longLong long longLong long longLong long longLong long "
125 "longLong long longLong long longLong long longLong long longLong long "
126 "longLong long longLong long longLong long longLong long longLong long "
127 "longLong long longLong long longLong long longLong long longLong long "
128 "longLong long longLong long longLong long longLong long longLong long "
129 "longLong long longLong long longLong long longLong long longLong long "
130 "longLong long longLong long longLong long longLong long longLong long "
131 "longLong long longLong long longLong long longLong long longLong long "
132 "longLong long long. END";
133 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400134
Lei Zhanga21d5932018-02-05 18:28:38 +0000135 // Check that the quadpoints are correct.
136 FS_QUADPOINTSF quadpoints;
137 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &quadpoints));
138 EXPECT_EQ(115.802643f, quadpoints.x1);
139 EXPECT_EQ(718.913940f, quadpoints.y1);
140 EXPECT_EQ(157.211182f, quadpoints.x4);
141 EXPECT_EQ(706.264465f, quadpoints.y4);
142 }
Lei Zhang75c81712018-02-08 17:22:39 +0000143 FPDF_ClosePage(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400144}
145
146TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
147 // Open a file with three annotations and load its first page.
148 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000149 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
150 // the FORM API calls in LoadPage() makes this test fail. So use
151 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu4fd9a472017-06-01 18:56:09 -0400152 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
153 ASSERT_TRUE(page);
154
155 // Check that there is a total of 3 annotation on its first page.
156 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
157
Lei Zhanga21d5932018-02-05 18:28:38 +0000158 {
159 // Check that the third annotation is of type "ink".
160 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
161 FPDFPage_GetAnnot(page, 2));
162 ASSERT_TRUE(annot);
163 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400164
Lei Zhanga21d5932018-02-05 18:28:38 +0000165 // Check that the annotation color is blue with opacity.
166 unsigned int R;
167 unsigned int G;
168 unsigned int B;
169 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000170 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000171 &G, &B, &A));
172 EXPECT_EQ(0u, R);
173 EXPECT_EQ(0u, G);
174 EXPECT_EQ(255u, B);
175 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400176
Lei Zhanga21d5932018-02-05 18:28:38 +0000177 // Check that there is no content.
178 EXPECT_EQ(2u,
179 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400180
Lei Zhanga21d5932018-02-05 18:28:38 +0000181 // Check that the rectange coordinates are correct.
182 // Note that upon rendering, the rectangle coordinates will be adjusted.
183 FS_RECTF rect;
184 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
185 EXPECT_EQ(351.820404f, rect.left);
186 EXPECT_EQ(583.830688f, rect.bottom);
187 EXPECT_EQ(475.336090f, rect.right);
188 EXPECT_EQ(681.535034f, rect.top);
189 }
Lei Zhang75c81712018-02-08 17:22:39 +0000190 FPDF_ClosePage(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400191}
Jane Liu20eafda2017-06-07 10:33:24 -0400192
193TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
194 // Open a file with one annotation and load its first page.
195 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000196 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400197 ASSERT_TRUE(page);
198
199 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400200 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400201
202 UnloadPage(page);
203}
204
Jane Liud321ef92017-06-14 09:56:22 -0400205TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400206 // Open a file with no annotation and load its first page.
207 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000208 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400209 ASSERT_TRUE(page);
210 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
211
Lei Zhanga21d5932018-02-05 18:28:38 +0000212 {
213 // Add a text annotation to the page.
214 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
215 FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
216 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400217
Lei Zhanga21d5932018-02-05 18:28:38 +0000218 // Check that there is now 1 annotations on this page.
219 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400220
Lei Zhanga21d5932018-02-05 18:28:38 +0000221 // Check that the subtype of the annotation is correct.
222 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
223 }
Jane Liue10509a2017-06-20 16:47:41 -0400224
Lei Zhanga21d5932018-02-05 18:28:38 +0000225 {
226 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
227 FPDFPage_GetAnnot(page, 0));
228 ASSERT_TRUE(annot);
229 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400230
Lei Zhanga21d5932018-02-05 18:28:38 +0000231 // Set the color of the annotation.
232 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
233 102, 153, 204));
234 // Check that the color has been set correctly.
235 unsigned int R;
236 unsigned int G;
237 unsigned int B;
238 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000239 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000240 &G, &B, &A));
241 EXPECT_EQ(51u, R);
242 EXPECT_EQ(102u, G);
243 EXPECT_EQ(153u, B);
244 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400245
Lei Zhanga21d5932018-02-05 18:28:38 +0000246 // Change the color of the annotation.
247 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
248 153, 102, 51));
249 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000250 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000251 &G, &B, &A));
252 EXPECT_EQ(204u, R);
253 EXPECT_EQ(153u, G);
254 EXPECT_EQ(102u, B);
255 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400256
Lei Zhanga21d5932018-02-05 18:28:38 +0000257 // Set the annotation rectangle.
258 FS_RECTF rect;
259 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
260 EXPECT_EQ(0.f, rect.left);
261 EXPECT_EQ(0.f, rect.right);
262 rect.left = 35;
263 rect.bottom = 150;
264 rect.right = 53;
265 rect.top = 165;
266 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
267 // Check that the annotation rectangle has been set correctly.
268 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
269 EXPECT_EQ(35.f, rect.left);
270 EXPECT_EQ(150.f, rect.bottom);
271 EXPECT_EQ(53.f, rect.right);
272 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400273
Lei Zhanga21d5932018-02-05 18:28:38 +0000274 // Set the content of the annotation.
275 static constexpr wchar_t kContents[] =
276 L"Hello! This is a customized content.";
277 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
278 GetFPDFWideString(kContents);
279 ASSERT_TRUE(
280 FPDFAnnot_SetStringValue(annot.get(), kContentsKey, text.get()));
281 // Check that the content has been set correctly.
282 unsigned long len =
283 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
284 std::vector<char> buf(len);
285 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
286 buf.data(), len));
287 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
288 }
Jane Liu20eafda2017-06-07 10:33:24 -0400289 UnloadPage(page);
290}
291
292TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
293 // Open a file with one annotation and load its first page.
294 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000295 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400296 ASSERT_TRUE(page);
297
298 // Check that there is a total of one annotation on its first page, and verify
299 // its quadpoints.
300 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400301 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000302 {
303 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
304 FPDFPage_GetAnnot(page, 0));
305 ASSERT_TRUE(annot);
306 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &quadpoints));
307 EXPECT_EQ(115.802643f, quadpoints.x1);
308 EXPECT_EQ(718.913940f, quadpoints.y1);
309 EXPECT_EQ(157.211182f, quadpoints.x4);
310 EXPECT_EQ(706.264465f, quadpoints.y4);
311 }
Jane Liu20eafda2017-06-07 10:33:24 -0400312
313 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000314 {
315 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
316 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
317 ASSERT_TRUE(annot);
318 quadpoints.x1 = 140.802643f;
319 quadpoints.x3 = 140.802643f;
320 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), &quadpoints));
321 }
Jane Liu20eafda2017-06-07 10:33:24 -0400322
323 // Save the document, closing the page and document.
324 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000325 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400326
327 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000328 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400329
330 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000331 page = LoadSavedPage(0);
332 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400333
334 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000335 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400336
Lei Zhanga21d5932018-02-05 18:28:38 +0000337 {
338 // Check that the second annotation is an underline annotation and verify
339 // its quadpoints.
340 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
341 FPDFPage_GetAnnot(page, 1));
342 ASSERT_TRUE(new_annot);
343 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
344 FS_QUADPOINTSF new_quadpoints;
345 ASSERT_TRUE(
346 FPDFAnnot_GetAttachmentPoints(new_annot.get(), &new_quadpoints));
347 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
348 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
349 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
350 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
351 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400352
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000353 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400354 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400355}
Jane Liu06462752017-06-27 16:41:14 -0400356
357TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400358#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400359 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
360 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
361 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400362#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000363 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
364 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
365 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400366#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000367 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
368 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
369 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400370#endif
371
Jane Liu06462752017-06-27 16:41:14 -0400372 // Open a file with four annotations and load its first page.
373 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000374 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400375 ASSERT_TRUE(page);
376 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
377
Jane Liub370e5a2017-08-16 13:24:58 -0400378 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000379 {
380 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
381 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
382 CompareBitmap(bitmap.get(), 612, 792, md5_original);
383 }
Jane Liub370e5a2017-08-16 13:24:58 -0400384
Jane Liu0c6b07d2017-08-15 10:50:22 -0400385 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400386 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000387
388 // Retrieve the highlight annotation which has its AP stream already defined.
389 {
390 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
391 FPDFPage_GetAnnot(page, 0));
392 ASSERT_TRUE(annot);
393 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
394
395 // Check that color cannot be set when an AP stream is defined already.
396 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
397 102, 153, 204));
398
399 // Verify its attachment points.
400 FS_QUADPOINTSF quadpoints;
401 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &quadpoints));
402 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
403 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
404 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
405 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
406
407 // Check that updating the attachment points would succeed.
408 quadpoints.x1 -= 50.f;
409 quadpoints.x2 -= 50.f;
410 quadpoints.x3 -= 50.f;
411 quadpoints.x4 -= 50.f;
412 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), &quadpoints));
413 FS_QUADPOINTSF new_quadpoints;
414 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &new_quadpoints));
415 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
416 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
417 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
418 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
419
420 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000421 {
422 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
423 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
424 CompareBitmap(bitmap.get(), 612, 792, md5_original);
425 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000426
427 // Verify its annotation rectangle.
428 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
429 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
430 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
431 EXPECT_NEAR(136.325f, rect.right, 0.001f);
432 EXPECT_NEAR(721.292f, rect.top, 0.001f);
433
434 // Check that updating the rectangle would succeed.
435 rect.left -= 60.f;
436 rect.right -= 60.f;
437 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
438 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
439 EXPECT_EQ(rect.right, new_rect.right);
440 }
Jane Liu06462752017-06-27 16:41:14 -0400441
Jane Liub370e5a2017-08-16 13:24:58 -0400442 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000443 {
444 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
445 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
446 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
447 }
Jane Liub370e5a2017-08-16 13:24:58 -0400448
Lei Zhanga21d5932018-02-05 18:28:38 +0000449 {
450 // Retrieve the square annotation which has its AP stream already defined.
451 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
452 FPDFPage_GetAnnot(page, 2));
453 ASSERT_TRUE(annot);
454 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400455
Lei Zhanga21d5932018-02-05 18:28:38 +0000456 // Check that updating the rectangle would succeed.
457 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
458 rect.left += 70.f;
459 rect.right += 70.f;
460 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
461 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
462 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400463
Lei Zhanga21d5932018-02-05 18:28:38 +0000464 // Check that updating the rectangle changes the square annotation's
465 // position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000466 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
467 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
468 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000469 }
Jane Liub370e5a2017-08-16 13:24:58 -0400470
Jane Liu06462752017-06-27 16:41:14 -0400471 UnloadPage(page);
472}
Jane Liu8ce58f52017-06-29 13:40:22 -0400473
Henrique Nakashima5098b252018-03-26 21:46:00 +0000474TEST_F(FPDFAnnotEmbeddertest, CountAttachmentPoints) {
475 // Open a file with multiline markup annotations.
476 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
477 FPDF_PAGE page = LoadPage(0);
478 ASSERT_TRUE(page);
479 {
480 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
481 FPDFPage_GetAnnot(page, 0));
482 ASSERT_TRUE(annot);
483
484 // This is a three line annotation.
485 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
486 }
487 UnloadPage(page);
488
489 // null annotation should return 0
490 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
491}
492
Jane Liu8ce58f52017-06-29 13:40:22 -0400493TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
494 // Open a file with 3 annotations on its first page.
495 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000496 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
497 // the FORM API calls in LoadPage() makes this test fail. So use
498 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu8ce58f52017-06-29 13:40:22 -0400499 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
500 ASSERT_TRUE(page);
501 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
502
Jane Liu0c6b07d2017-08-15 10:50:22 -0400503 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400504
Lei Zhanga21d5932018-02-05 18:28:38 +0000505 // Check that the annotations have the expected rectangle coordinates.
506 {
507 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
508 FPDFPage_GetAnnot(page, 0));
509 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
510 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
511 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400512
Lei Zhanga21d5932018-02-05 18:28:38 +0000513 {
514 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
515 FPDFPage_GetAnnot(page, 1));
516 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
517 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
518 }
519
520 {
521 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
522 FPDFPage_GetAnnot(page, 2));
523 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
524 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
525 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400526
527 // Check that nothing happens when attempting to remove an annotation with an
528 // out-of-bound index.
529 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
530 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
531 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
532
533 // Remove the second annotation.
534 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
535 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
536 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
537
538 // Save the document, closing the page and document.
539 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
540 FPDF_ClosePage(page);
541
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400542 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400543 // Open the saved document.
544 std::string new_file = GetString();
545 FPDF_FILEACCESS file_access;
546 memset(&file_access, 0, sizeof(file_access));
547 file_access.m_FileLen = new_file.size();
548 file_access.m_GetBlock = GetBlockFromString;
549 file_access.m_Param = &new_file;
550 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
551 ASSERT_TRUE(new_doc);
552 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
553 ASSERT_TRUE(new_page);
554
555 // Check that the saved document has 2 annotations on the first page.
556 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
557
Lei Zhanga21d5932018-02-05 18:28:38 +0000558 // Check that the remaining 2 annotations are the original 1st and 3rd ones
559 // by verifying their rectangle coordinates.
560 {
561 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
562 FPDFPage_GetAnnot(new_page, 0));
563 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
564 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
565 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400566
Lei Zhanga21d5932018-02-05 18:28:38 +0000567 {
568 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
569 FPDFPage_GetAnnot(new_page, 1));
570 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
571 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
572 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400573 FPDF_ClosePage(new_page);
574 FPDF_CloseDocument(new_doc);
575}
Jane Liu8ce58f52017-06-29 13:40:22 -0400576
Jane Liubaa7ff42017-06-29 19:18:23 -0400577TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400578#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400579 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000580 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
581 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
582 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Jane Liubaa7ff42017-06-29 19:18:23 -0400583#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000584 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000585 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
586 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
587 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400588#endif
589
590 // Open a file with two annotations and load its first page.
591 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000592 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400593 ASSERT_TRUE(page);
594 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
595
596 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000597 {
598 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
599 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
600 CompareBitmap(bitmap.get(), 595, 842, md5_original);
601 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400602
Lei Zhanga21d5932018-02-05 18:28:38 +0000603 {
604 // Retrieve the stamp annotation which has its AP stream already defined.
605 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
606 FPDFPage_GetAnnot(page, 0));
607 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400608
Lei Zhanga21d5932018-02-05 18:28:38 +0000609 // Check that this annotation has one path object and retrieve it.
610 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000611 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000612 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
613 EXPECT_FALSE(path);
614 path = FPDFAnnot_GetObject(annot.get(), 0);
615 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
616 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400617
Lei Zhanga21d5932018-02-05 18:28:38 +0000618 // Modify the color of the path object.
619 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
620 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400621
Lei Zhanga21d5932018-02-05 18:28:38 +0000622 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000623 {
624 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
625 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
626 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
627 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400628
Lei Zhanga21d5932018-02-05 18:28:38 +0000629 // Add a second path object to the same annotation.
630 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
631 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
632 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
633 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
634 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
635 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
636 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400637
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000638 // The object is in the annontation, not in the page, so the page object
639 // array should not change.
640 ASSERT_EQ(32, FPDFPage_CountObjects(page));
641
Lei Zhanga21d5932018-02-05 18:28:38 +0000642 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000643 {
644 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
645 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
646 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
647 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400648
Lei Zhanga21d5932018-02-05 18:28:38 +0000649 // Delete the newly added path object.
650 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
651 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000652 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000653 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400654
655 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000656 {
657 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
658 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
659 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
660 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400661
Jane Liubaa7ff42017-06-29 19:18:23 -0400662 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400663
Lei Zhanga21d5932018-02-05 18:28:38 +0000664 {
665 // Create another stamp annotation and set its annotation rectangle.
666 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
667 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
668 ASSERT_TRUE(annot);
669 rect.left = 200.f;
670 rect.bottom = 400.f;
671 rect.right = 500.f;
672 rect.top = 600.f;
673 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400674
Lei Zhanga21d5932018-02-05 18:28:38 +0000675 // Add a new path to the annotation.
676 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
677 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
678 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
679 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
680 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
681 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
682 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
683 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
684 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
685 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
686
687 // Check that the annotation's bounding box came from its rectangle.
688 FS_RECTF new_rect;
689 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
690 EXPECT_EQ(rect.left, new_rect.left);
691 EXPECT_EQ(rect.bottom, new_rect.bottom);
692 EXPECT_EQ(rect.right, new_rect.right);
693 EXPECT_EQ(rect.top, new_rect.top);
694 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400695
696 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400697 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000698 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400699
700 // Open the saved document.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400701 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000702 page = LoadSavedPage(0);
703 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400704
Jane Liu36567742017-07-06 11:13:35 -0400705 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000706 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400707
Lei Zhanga21d5932018-02-05 18:28:38 +0000708 {
709 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
710 FPDFPage_GetAnnot(page, 2));
711 ASSERT_TRUE(annot);
712 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400713
Lei Zhanga21d5932018-02-05 18:28:38 +0000714 // Check that the new annotation's rectangle is as defined.
715 FS_RECTF new_rect;
716 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
717 EXPECT_EQ(rect.left, new_rect.left);
718 EXPECT_EQ(rect.bottom, new_rect.bottom);
719 EXPECT_EQ(rect.right, new_rect.right);
720 EXPECT_EQ(rect.top, new_rect.top);
721 }
722
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000723 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400724 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400725}
Jane Liub137e752017-07-05 15:04:33 -0400726
727TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
728 // Open a file with an annotation and load its first page.
729 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000730 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400731 ASSERT_TRUE(page);
732
733 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000734 {
735 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
736 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
737 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
738 }
Jane Liub137e752017-07-05 15:04:33 -0400739
Lei Zhanga21d5932018-02-05 18:28:38 +0000740 {
741 // Retrieve the annotation.
742 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
743 FPDFPage_GetAnnot(page, 0));
744 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400745
Lei Zhanga21d5932018-02-05 18:28:38 +0000746 // Check that the original flag values are as expected.
747 int flags = FPDFAnnot_GetFlags(annot.get());
748 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
749 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400750
Lei Zhanga21d5932018-02-05 18:28:38 +0000751 // Set the HIDDEN flag.
752 flags |= FPDF_ANNOT_FLAG_HIDDEN;
753 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
754 flags = FPDFAnnot_GetFlags(annot.get());
755 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
756 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400757
Lei Zhanga21d5932018-02-05 18:28:38 +0000758 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000759 {
760 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
761 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
762 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
763 }
Jane Liub137e752017-07-05 15:04:33 -0400764
Lei Zhanga21d5932018-02-05 18:28:38 +0000765 // Unset the HIDDEN flag.
766 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
767 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
768 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
769 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
770 flags = FPDFAnnot_GetFlags(annot.get());
771 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
772 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400773
Lei Zhanga21d5932018-02-05 18:28:38 +0000774 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000775 {
776 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
777 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
778 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
779 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000780 }
Jane Liub137e752017-07-05 15:04:33 -0400781
Jane Liub137e752017-07-05 15:04:33 -0400782 UnloadPage(page);
783}
Jane Liu36567742017-07-06 11:13:35 -0400784
785TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400786#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400787 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
788 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
789 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Jane Liu36567742017-07-06 11:13:35 -0400790#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000791 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
792 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
793 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400794#endif
795
796 // Open a file with two annotations and load its first page.
797 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000798 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400799 ASSERT_TRUE(page);
800 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
801
802 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000803 {
804 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
805 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
806 CompareBitmap(bitmap.get(), 595, 842, md5_original);
807 }
Jane Liu36567742017-07-06 11:13:35 -0400808
Jane Liu36567742017-07-06 11:13:35 -0400809 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000810 FPDF_BITMAP image_bitmap;
811
812 {
813 // Create a stamp annotation and set its annotation rectangle.
814 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
815 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
816 ASSERT_TRUE(annot);
817 FS_RECTF rect;
818 rect.left = 200.f;
819 rect.bottom = 600.f;
820 rect.right = 400.f;
821 rect.top = 800.f;
822 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
823
824 // Add a solid-color translucent image object to the new annotation.
825 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
826 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
827 0xeeeecccc);
828 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
829 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
830 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
831 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
832 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
833 kBitmapSize, 0, 0));
834 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
835 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
836 }
Jane Liu36567742017-07-06 11:13:35 -0400837
838 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000839 {
840 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
841 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
842 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
843 }
Jane Liu36567742017-07-06 11:13:35 -0400844
Lei Zhanga21d5932018-02-05 18:28:38 +0000845 {
846 // Retrieve the newly added stamp annotation and its image object.
847 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
848 FPDFPage_GetAnnot(page, 2));
849 ASSERT_TRUE(annot);
850 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
851 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
852 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400853
Lei Zhanga21d5932018-02-05 18:28:38 +0000854 // Modify the image in the new annotation.
855 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
856 0xff000000);
857 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
858 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
859 }
Jane Liu36567742017-07-06 11:13:35 -0400860
861 // Save the document, closing the page and document.
862 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000863 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400864 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400865
866 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400867 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400868}
869
870TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400871#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400872 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
873 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
874 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Jane Liu36567742017-07-06 11:13:35 -0400875#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000876 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
877 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
878 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400879#endif
880
881 // Open a file with two annotations and load its first page.
882 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000883 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400884 ASSERT_TRUE(page);
885 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
886
887 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000888 {
889 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
890 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
891 CompareBitmap(bitmap.get(), 595, 842, md5_original);
892 }
Jane Liu36567742017-07-06 11:13:35 -0400893
Lei Zhanga21d5932018-02-05 18:28:38 +0000894 {
895 // Create a stamp annotation and set its annotation rectangle.
896 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
897 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
898 ASSERT_TRUE(annot);
899 FS_RECTF rect;
900 rect.left = 200.f;
901 rect.bottom = 550.f;
902 rect.right = 450.f;
903 rect.top = 650.f;
904 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -0400905
Lei Zhanga21d5932018-02-05 18:28:38 +0000906 // Add a translucent text object to the new annotation.
907 FPDF_PAGEOBJECT text_object =
908 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
909 EXPECT_TRUE(text_object);
910 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
911 GetFPDFWideString(L"I'm a translucent text laying on other text.");
912 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
913 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
914 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
915 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
916 }
Jane Liu36567742017-07-06 11:13:35 -0400917
918 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000919 {
920 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
921 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
922 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
923 }
Jane Liu36567742017-07-06 11:13:35 -0400924
Lei Zhanga21d5932018-02-05 18:28:38 +0000925 {
926 // Retrieve the newly added stamp annotation and its text object.
927 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
928 FPDFPage_GetAnnot(page, 2));
929 ASSERT_TRUE(annot);
930 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
931 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
932 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -0400933
Lei Zhanga21d5932018-02-05 18:28:38 +0000934 // Modify the text in the new annotation.
935 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
936 GetFPDFWideString(L"New text!");
937 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
938 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
939 }
Jane Liu36567742017-07-06 11:13:35 -0400940
941 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000942 {
943 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
944 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
945 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
946 }
Jane Liu36567742017-07-06 11:13:35 -0400947
948 // Remove the new annotation, and check that the page renders as before.
949 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +0000950 {
951 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
952 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
953 CompareBitmap(bitmap.get(), 595, 842, md5_original);
954 }
Jane Liu36567742017-07-06 11:13:35 -0400955
956 UnloadPage(page);
957}
Jane Liu2e1a32b2017-07-06 12:01:25 -0400958
959TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
960 // Open a file with four annotations and load its first page.
961 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000962 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -0400963 ASSERT_TRUE(page);
964
Lei Zhangdf064df2017-08-31 02:33:27 -0700965 static constexpr char kDateKey[] = "M";
Lei Zhanga21d5932018-02-05 18:28:38 +0000966 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400967
Lei Zhanga21d5932018-02-05 18:28:38 +0000968 {
969 // Retrieve the first annotation.
970 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
971 FPDFPage_GetAnnot(page, 0));
972 ASSERT_TRUE(annot);
973
974 // Check that a non-existent key does not exist.
975 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
976
977 // Check that the string value of a non-string dictionary entry is empty.
978 static constexpr char kApKey[] = "AP";
979 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), kApKey));
980 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
981 FPDFAnnot_GetValueType(annot.get(), kApKey));
982 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kApKey, nullptr, 0));
983
984 // Check that the string value of the hash is correct.
985 static constexpr char kHashKey[] = "AAPL:Hash";
986 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
987 unsigned long len =
988 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
989 std::vector<char> buf(len);
990 EXPECT_EQ(66u,
991 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
992 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
993 BufferToWString(buf).c_str());
994
995 // Check that the string value of the modified date is correct.
996 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
997 len = FPDFAnnot_GetStringValue(annot.get(), kDateKey, nullptr, 0);
998 buf.clear();
999 buf.resize(len);
1000 EXPECT_EQ(44u,
1001 FPDFAnnot_GetStringValue(annot.get(), kDateKey, buf.data(), len));
1002 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1003
1004 // Update the date entry for the annotation.
1005 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1006 GetFPDFWideString(kNewDate);
1007 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kDateKey, text.get()));
1008 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001009
1010 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001011 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001012 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001013
Dan Sinclair698aed72017-09-26 16:24:49 -04001014#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +03001015 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001016#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001017 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001018#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001019
1020 // Open the saved annotation.
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001021 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001022 page = LoadSavedPage(0);
1023 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001024 {
1025 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
1026 FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001027
Lei Zhanga21d5932018-02-05 18:28:38 +00001028 // Check that the string value of the modified date is the newly-set value.
1029 EXPECT_EQ(FPDF_OBJECT_STRING,
1030 FPDFAnnot_GetValueType(new_annot.get(), kDateKey));
1031 unsigned long len =
1032 FPDFAnnot_GetStringValue(new_annot.get(), kDateKey, nullptr, 0);
1033 std::vector<char> buf(len);
1034 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot.get(), kDateKey,
1035 buf.data(), len));
1036 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1037 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001038
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001039 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001040 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001041}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001042
Henrique Nakashima5970a472018-01-11 22:40:59 +00001043TEST_F(FPDFAnnotEmbeddertest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001044 // Open a file with four annotations and load its first page.
1045 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001046 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001047 ASSERT_TRUE(page);
1048
Lei Zhanga21d5932018-02-05 18:28:38 +00001049 {
1050 // Retrieve the first annotation.
1051 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1052 FPDFPage_GetAnnot(page, 0));
1053 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001054
Lei Zhanga21d5932018-02-05 18:28:38 +00001055 // Check that the string value of an AP returns the expected length.
1056 unsigned long normal_len = FPDFAnnot_GetAP(
1057 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1058 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001059
Lei Zhanga21d5932018-02-05 18:28:38 +00001060 // Check that the string value of an AP is not returned if the buffer is too
1061 // small. The result buffer should be overwritten with an empty string.
1062 std::vector<char> buf(normal_len - 1);
1063 // Write L"z" in the buffer to verify it's not overwritten.
1064 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1065 EXPECT_EQ(73970u,
1066 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1067 buf.data(), buf.size()));
1068 std::string ap = BufferToString(buf);
1069 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001070
Lei Zhanga21d5932018-02-05 18:28:38 +00001071 // Check that the string value of an AP is returned through a buffer that is
1072 // the right size.
1073 buf.clear();
1074 buf.resize(normal_len);
1075 EXPECT_EQ(73970u,
1076 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1077 buf.data(), buf.size()));
1078 ap = BufferToString(buf);
1079 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1080 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001081
Lei Zhanga21d5932018-02-05 18:28:38 +00001082 // Check that the string value of an AP is returned through a buffer that is
1083 // larger than necessary.
1084 buf.clear();
1085 buf.resize(normal_len + 1);
1086 EXPECT_EQ(73970u,
1087 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1088 buf.data(), buf.size()));
1089 ap = BufferToString(buf);
1090 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1091 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001092
Lei Zhanga21d5932018-02-05 18:28:38 +00001093 // Check that getting an AP for a mode that does not have an AP returns an
1094 // empty string.
1095 unsigned long rollover_len = FPDFAnnot_GetAP(
1096 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1097 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001098
Lei Zhanga21d5932018-02-05 18:28:38 +00001099 buf.clear();
1100 buf.resize(1000);
1101 EXPECT_EQ(2u,
1102 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1103 buf.data(), buf.size()));
1104 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001105
Lei Zhanga21d5932018-02-05 18:28:38 +00001106 // Check that setting the AP for an invalid appearance mode fails.
1107 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1108 GetFPDFWideString(L"new test ap");
1109 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1110 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1111 apText.get()));
1112 EXPECT_FALSE(FPDFAnnot_SetAP(
1113 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001114
Lei Zhanga21d5932018-02-05 18:28:38 +00001115 // Set the AP correctly now.
1116 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1117 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001118
Lei Zhanga21d5932018-02-05 18:28:38 +00001119 // Check that the new annotation value is equal to the value we just set.
1120 rollover_len = FPDFAnnot_GetAP(
1121 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1122 EXPECT_EQ(24u, rollover_len);
1123 buf.clear();
1124 buf.resize(rollover_len);
1125 EXPECT_EQ(24u,
1126 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1127 buf.data(), buf.size()));
1128 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001129
Lei Zhanga21d5932018-02-05 18:28:38 +00001130 // Check that the Normal AP was not touched when the Rollover AP was set.
1131 buf.clear();
1132 buf.resize(normal_len);
1133 EXPECT_EQ(73970u,
1134 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1135 buf.data(), buf.size()));
1136 ap = BufferToString(buf);
1137 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1138 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1139 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001140
1141 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001142 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001143 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001144
1145 OpenSavedDocument();
1146 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001147 {
1148 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
1149 FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001150
Lei Zhanga21d5932018-02-05 18:28:38 +00001151 // Check that the new annotation value is equal to the value we set before
1152 // saving.
1153 unsigned long rollover_len = FPDFAnnot_GetAP(
1154 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1155 EXPECT_EQ(24u, rollover_len);
1156 std::vector<char> buf(rollover_len);
1157 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1158 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1159 buf.data(), buf.size()));
1160 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1161 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001162
1163 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001164 CloseSavedPage(page);
1165 CloseSavedDocument();
1166}
1167
1168TEST_F(FPDFAnnotEmbeddertest, RemoveOptionalAP) {
1169 // Open a file with four annotations and load its first page.
1170 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001171 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001172 ASSERT_TRUE(page);
1173
Lei Zhanga21d5932018-02-05 18:28:38 +00001174 {
1175 // Retrieve the first annotation.
1176 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1177 FPDFPage_GetAnnot(page, 0));
1178 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001179
Lei Zhanga21d5932018-02-05 18:28:38 +00001180 // Set Down AP. Normal AP is already set.
1181 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1182 GetFPDFWideString(L"new test ap");
1183 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1184 apText.get()));
1185 EXPECT_EQ(73970u,
1186 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1187 nullptr, 0));
1188 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1189 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001190
Lei Zhanga21d5932018-02-05 18:28:38 +00001191 // Check that setting the Down AP to null removes the Down entry but keeps
1192 // Normal intact.
1193 EXPECT_TRUE(
1194 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1195 EXPECT_EQ(73970u,
1196 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1197 nullptr, 0));
1198 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1199 nullptr, 0));
1200 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001201
Lei Zhang75c81712018-02-08 17:22:39 +00001202 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001203}
1204
1205TEST_F(FPDFAnnotEmbeddertest, RemoveRequiredAP) {
1206 // Open a file with four annotations and load its first page.
1207 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001208 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001209 ASSERT_TRUE(page);
1210
Lei Zhanga21d5932018-02-05 18:28:38 +00001211 {
1212 // Retrieve the first annotation.
1213 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1214 FPDFPage_GetAnnot(page, 0));
1215 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001216
Lei Zhanga21d5932018-02-05 18:28:38 +00001217 // Set Down AP. Normal AP is already set.
1218 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1219 GetFPDFWideString(L"new test ap");
1220 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1221 apText.get()));
1222 EXPECT_EQ(73970u,
1223 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1224 nullptr, 0));
1225 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1226 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001227
Lei Zhanga21d5932018-02-05 18:28:38 +00001228 // Check that setting the Normal AP to null removes the whole AP dictionary.
1229 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1230 nullptr));
1231 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1232 nullptr, 0));
1233 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1234 nullptr, 0));
1235 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001236
Lei Zhang75c81712018-02-08 17:22:39 +00001237 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001238}
1239
Jane Liu300bb272017-08-21 14:37:53 -04001240TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) {
1241 // Open a file with annotations and load its first page.
1242 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001243 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001244 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001245 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001246
Lei Zhanga21d5932018-02-05 18:28:38 +00001247 {
1248 // Retrieve the highlight annotation which has its popup defined.
1249 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1250 FPDFPage_GetAnnot(page, 0));
1251 ASSERT_TRUE(annot);
1252 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1253 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1254 static constexpr char kPopupKey[] = "Popup";
1255 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1256 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1257 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001258
Lei Zhanga21d5932018-02-05 18:28:38 +00001259 // Retrieve and verify the popup of the highlight annotation.
1260 std::unique_ptr<void, FPDFAnnotationDeleter> popup(
1261 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1262 ASSERT_TRUE(popup);
1263 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1264 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1265 FS_RECTF rect;
1266 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1267 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1268 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001269
Lei Zhanga21d5932018-02-05 18:28:38 +00001270 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1271 // since "IRT" is not a key in |annot|'s dictionary.
1272 static constexpr char kIRTKey[] = "IRT";
1273 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1274 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001275
Lei Zhanga21d5932018-02-05 18:28:38 +00001276 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1277 // would fail, since its parent is not an annotation.
1278 static constexpr char kPKey[] = "P";
1279 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPKey));
1280 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1281 FPDFAnnot_GetValueType(annot.get(), kPKey));
1282 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kPKey));
1283 }
Jane Liu300bb272017-08-21 14:37:53 -04001284
Jane Liu300bb272017-08-21 14:37:53 -04001285 UnloadPage(page);
1286}
1287
Diana Gage7e0c05d2017-07-19 17:33:33 -07001288TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
1289 // Open file with form text fields.
1290 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001291 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001292 ASSERT_TRUE(page);
1293
Lei Zhanga21d5932018-02-05 18:28:38 +00001294 {
1295 // Retrieve the first annotation: user-editable text field.
1296 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1297 FPDFPage_GetAnnot(page, 0));
1298 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001299
Lei Zhanga21d5932018-02-05 18:28:38 +00001300 // Check that the flag values are as expected.
1301 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1302 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1303 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001304
Lei Zhanga21d5932018-02-05 18:28:38 +00001305 {
1306 // Retrieve the second annotation: read-only text field.
1307 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1308 FPDFPage_GetAnnot(page, 1));
1309 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001310
Lei Zhanga21d5932018-02-05 18:28:38 +00001311 // Check that the flag values are as expected.
1312 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1313 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1314 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001315
1316 UnloadPage(page);
1317}
1318
1319TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
1320 // Open file with form text fields.
1321 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001322 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001323 ASSERT_TRUE(page);
1324
Lei Zhanga21d5932018-02-05 18:28:38 +00001325 {
1326 // Retrieve the first annotation: user-editable combobox.
1327 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1328 FPDFPage_GetAnnot(page, 0));
1329 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001330
Lei Zhanga21d5932018-02-05 18:28:38 +00001331 // Check that the flag values are as expected.
1332 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1333 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1334 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1335 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1336 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001337
Lei Zhanga21d5932018-02-05 18:28:38 +00001338 {
1339 // Retrieve the second annotation: regular combobox.
1340 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1341 FPDFPage_GetAnnot(page, 1));
1342 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001343
Lei Zhanga21d5932018-02-05 18:28:38 +00001344 // Check that the flag values are as expected.
1345 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1346 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1347 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1348 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1349 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001350
Lei Zhanga21d5932018-02-05 18:28:38 +00001351 {
1352 // Retrieve the third annotation: read-only combobox.
1353 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1354 FPDFPage_GetAnnot(page, 2));
1355 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001356
Lei Zhanga21d5932018-02-05 18:28:38 +00001357 // Check that the flag values are as expected.
1358 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1359 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1360 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1361 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1362 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001363
1364 UnloadPage(page);
1365}
Diana Gage40870db2017-07-19 18:16:03 -07001366
1367TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
1368 // Open file with form text fields.
1369 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1370 FPDF_PAGE page = LoadPage(0);
1371 ASSERT_TRUE(page);
1372
1373 // Attempt to get an annotation where no annotation exists on page.
1374 FPDF_ANNOTATION annot =
1375 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0);
1376 EXPECT_FALSE(annot);
1377
1378 UnloadPage(page);
1379}
1380
1381TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
1382 // Open file with form text fields.
1383 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1384 FPDF_PAGE page = LoadPage(0);
1385 ASSERT_TRUE(page);
1386
Lei Zhanga21d5932018-02-05 18:28:38 +00001387 {
1388 // Retrieve user-editable text field annotation.
1389 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1390 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1391 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001392
Lei Zhanga21d5932018-02-05 18:28:38 +00001393 // Check that interactive form annotation flag values are as expected.
1394 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1395 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1396 }
Diana Gage40870db2017-07-19 18:16:03 -07001397
Lei Zhanga21d5932018-02-05 18:28:38 +00001398 {
1399 // Retrieve read-only text field annotation.
1400 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1401 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1402 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001403
Lei Zhanga21d5932018-02-05 18:28:38 +00001404 // Check that interactive form annotation flag values are as expected.
1405 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1406 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1407 }
Diana Gage40870db2017-07-19 18:16:03 -07001408
1409 UnloadPage(page);
1410}
1411
1412TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1413 // Open file with form comboboxes.
1414 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1415 FPDF_PAGE page = LoadPage(0);
1416 ASSERT_TRUE(page);
1417
Lei Zhanga21d5932018-02-05 18:28:38 +00001418 {
1419 // Retrieve user-editable combobox annotation.
1420 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001421 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001422 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001423
Lei Zhanga21d5932018-02-05 18:28:38 +00001424 // Check that interactive form annotation flag values are as expected.
1425 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1426 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1427 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1428 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1429 }
Diana Gage40870db2017-07-19 18:16:03 -07001430
Lei Zhanga21d5932018-02-05 18:28:38 +00001431 {
1432 // Retrieve regular combobox annotation.
1433 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001434 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001435 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001436
Lei Zhanga21d5932018-02-05 18:28:38 +00001437 // Check that interactive form annotation flag values are as expected.
1438 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1439 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1440 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1441 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1442 }
Diana Gage40870db2017-07-19 18:16:03 -07001443
Lei Zhanga21d5932018-02-05 18:28:38 +00001444 {
1445 // Retrieve read-only combobox annotation.
1446 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001447 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001448 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001449
Lei Zhanga21d5932018-02-05 18:28:38 +00001450 // Check that interactive form annotation flag values are as expected.
1451 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1452 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1453 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1454 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1455 }
Diana Gage40870db2017-07-19 18:16:03 -07001456
1457 UnloadPage(page);
1458}