blob: b96460a395feee12d8575e2caea76dfa98f17127 [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";
50 // Open a file with two multiline markup annotations.
51 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
474TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
475 // Open a file with 3 annotations on its first page.
476 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000477 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
478 // the FORM API calls in LoadPage() makes this test fail. So use
479 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu8ce58f52017-06-29 13:40:22 -0400480 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
481 ASSERT_TRUE(page);
482 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
483
Jane Liu0c6b07d2017-08-15 10:50:22 -0400484 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400485
Lei Zhanga21d5932018-02-05 18:28:38 +0000486 // Check that the annotations have the expected rectangle coordinates.
487 {
488 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
489 FPDFPage_GetAnnot(page, 0));
490 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
491 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
492 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400493
Lei Zhanga21d5932018-02-05 18:28:38 +0000494 {
495 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
496 FPDFPage_GetAnnot(page, 1));
497 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
498 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
499 }
500
501 {
502 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
503 FPDFPage_GetAnnot(page, 2));
504 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
505 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
506 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400507
508 // Check that nothing happens when attempting to remove an annotation with an
509 // out-of-bound index.
510 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
511 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
512 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
513
514 // Remove the second annotation.
515 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
516 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
517 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
518
519 // Save the document, closing the page and document.
520 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
521 FPDF_ClosePage(page);
522
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400523 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400524 // Open the saved document.
525 std::string new_file = GetString();
526 FPDF_FILEACCESS file_access;
527 memset(&file_access, 0, sizeof(file_access));
528 file_access.m_FileLen = new_file.size();
529 file_access.m_GetBlock = GetBlockFromString;
530 file_access.m_Param = &new_file;
531 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
532 ASSERT_TRUE(new_doc);
533 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
534 ASSERT_TRUE(new_page);
535
536 // Check that the saved document has 2 annotations on the first page.
537 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
538
Lei Zhanga21d5932018-02-05 18:28:38 +0000539 // Check that the remaining 2 annotations are the original 1st and 3rd ones
540 // by verifying their rectangle coordinates.
541 {
542 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
543 FPDFPage_GetAnnot(new_page, 0));
544 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
545 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
546 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400547
Lei Zhanga21d5932018-02-05 18:28:38 +0000548 {
549 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
550 FPDFPage_GetAnnot(new_page, 1));
551 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
552 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
553 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400554 FPDF_ClosePage(new_page);
555 FPDF_CloseDocument(new_doc);
556}
Jane Liu8ce58f52017-06-29 13:40:22 -0400557
Jane Liubaa7ff42017-06-29 19:18:23 -0400558TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400559#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400560 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000561 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
562 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
563 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Jane Liubaa7ff42017-06-29 19:18:23 -0400564#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000565 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000566 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
567 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
568 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400569#endif
570
571 // Open a file with two annotations and load its first page.
572 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000573 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400574 ASSERT_TRUE(page);
575 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
576
577 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000578 {
579 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
580 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
581 CompareBitmap(bitmap.get(), 595, 842, md5_original);
582 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400583
Lei Zhanga21d5932018-02-05 18:28:38 +0000584 {
585 // Retrieve the stamp annotation which has its AP stream already defined.
586 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
587 FPDFPage_GetAnnot(page, 0));
588 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400589
Lei Zhanga21d5932018-02-05 18:28:38 +0000590 // Check that this annotation has one path object and retrieve it.
591 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000592 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000593 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
594 EXPECT_FALSE(path);
595 path = FPDFAnnot_GetObject(annot.get(), 0);
596 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
597 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400598
Lei Zhanga21d5932018-02-05 18:28:38 +0000599 // Modify the color of the path object.
600 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
601 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400602
Lei Zhanga21d5932018-02-05 18:28:38 +0000603 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000604 {
605 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
606 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
607 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
608 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400609
Lei Zhanga21d5932018-02-05 18:28:38 +0000610 // Add a second path object to the same annotation.
611 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
612 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
613 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
614 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
615 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
616 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
617 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400618
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000619 // The object is in the annontation, not in the page, so the page object
620 // array should not change.
621 ASSERT_EQ(32, FPDFPage_CountObjects(page));
622
Lei Zhanga21d5932018-02-05 18:28:38 +0000623 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000624 {
625 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
626 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
627 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
628 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400629
Lei Zhanga21d5932018-02-05 18:28:38 +0000630 // Delete the newly added path object.
631 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
632 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000633 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000634 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400635
636 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000637 {
638 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
639 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
640 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
641 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400642
Jane Liubaa7ff42017-06-29 19:18:23 -0400643 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400644
Lei Zhanga21d5932018-02-05 18:28:38 +0000645 {
646 // Create another stamp annotation and set its annotation rectangle.
647 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
648 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
649 ASSERT_TRUE(annot);
650 rect.left = 200.f;
651 rect.bottom = 400.f;
652 rect.right = 500.f;
653 rect.top = 600.f;
654 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400655
Lei Zhanga21d5932018-02-05 18:28:38 +0000656 // Add a new path to the annotation.
657 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
658 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
659 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
660 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
661 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
662 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
663 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
664 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
665 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
666 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
667
668 // Check that the annotation's bounding box came from its rectangle.
669 FS_RECTF new_rect;
670 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
671 EXPECT_EQ(rect.left, new_rect.left);
672 EXPECT_EQ(rect.bottom, new_rect.bottom);
673 EXPECT_EQ(rect.right, new_rect.right);
674 EXPECT_EQ(rect.top, new_rect.top);
675 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400676
677 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400678 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000679 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400680
681 // Open the saved document.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400682 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000683 page = LoadSavedPage(0);
684 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400685
Jane Liu36567742017-07-06 11:13:35 -0400686 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000687 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400688
Lei Zhanga21d5932018-02-05 18:28:38 +0000689 {
690 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
691 FPDFPage_GetAnnot(page, 2));
692 ASSERT_TRUE(annot);
693 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400694
Lei Zhanga21d5932018-02-05 18:28:38 +0000695 // Check that the new annotation's rectangle is as defined.
696 FS_RECTF new_rect;
697 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
698 EXPECT_EQ(rect.left, new_rect.left);
699 EXPECT_EQ(rect.bottom, new_rect.bottom);
700 EXPECT_EQ(rect.right, new_rect.right);
701 EXPECT_EQ(rect.top, new_rect.top);
702 }
703
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000704 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400705 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400706}
Jane Liub137e752017-07-05 15:04:33 -0400707
708TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
709 // Open a file with an annotation and load its first page.
710 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000711 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400712 ASSERT_TRUE(page);
713
714 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000715 {
716 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
717 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
718 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
719 }
Jane Liub137e752017-07-05 15:04:33 -0400720
Lei Zhanga21d5932018-02-05 18:28:38 +0000721 {
722 // Retrieve the annotation.
723 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
724 FPDFPage_GetAnnot(page, 0));
725 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400726
Lei Zhanga21d5932018-02-05 18:28:38 +0000727 // Check that the original flag values are as expected.
728 int flags = FPDFAnnot_GetFlags(annot.get());
729 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
730 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400731
Lei Zhanga21d5932018-02-05 18:28:38 +0000732 // Set the HIDDEN flag.
733 flags |= FPDF_ANNOT_FLAG_HIDDEN;
734 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
735 flags = FPDFAnnot_GetFlags(annot.get());
736 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
737 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400738
Lei Zhanga21d5932018-02-05 18:28:38 +0000739 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000740 {
741 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
742 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
743 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
744 }
Jane Liub137e752017-07-05 15:04:33 -0400745
Lei Zhanga21d5932018-02-05 18:28:38 +0000746 // Unset the HIDDEN flag.
747 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
748 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
749 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
750 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
751 flags = FPDFAnnot_GetFlags(annot.get());
752 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
753 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400754
Lei Zhanga21d5932018-02-05 18:28:38 +0000755 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000756 {
757 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
758 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
759 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
760 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000761 }
Jane Liub137e752017-07-05 15:04:33 -0400762
Jane Liub137e752017-07-05 15:04:33 -0400763 UnloadPage(page);
764}
Jane Liu36567742017-07-06 11:13:35 -0400765
766TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400767#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400768 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
769 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
770 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Jane Liu36567742017-07-06 11:13:35 -0400771#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000772 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
773 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
774 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400775#endif
776
777 // Open a file with two annotations and load its first page.
778 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000779 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400780 ASSERT_TRUE(page);
781 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
782
783 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000784 {
785 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
786 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
787 CompareBitmap(bitmap.get(), 595, 842, md5_original);
788 }
Jane Liu36567742017-07-06 11:13:35 -0400789
Jane Liu36567742017-07-06 11:13:35 -0400790 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000791 FPDF_BITMAP image_bitmap;
792
793 {
794 // Create a stamp annotation and set its annotation rectangle.
795 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
796 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
797 ASSERT_TRUE(annot);
798 FS_RECTF rect;
799 rect.left = 200.f;
800 rect.bottom = 600.f;
801 rect.right = 400.f;
802 rect.top = 800.f;
803 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
804
805 // Add a solid-color translucent image object to the new annotation.
806 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
807 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
808 0xeeeecccc);
809 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
810 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
811 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
812 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
813 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
814 kBitmapSize, 0, 0));
815 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
816 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
817 }
Jane Liu36567742017-07-06 11:13:35 -0400818
819 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000820 {
821 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
822 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
823 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
824 }
Jane Liu36567742017-07-06 11:13:35 -0400825
Lei Zhanga21d5932018-02-05 18:28:38 +0000826 {
827 // Retrieve the newly added stamp annotation and its image object.
828 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
829 FPDFPage_GetAnnot(page, 2));
830 ASSERT_TRUE(annot);
831 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
832 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
833 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400834
Lei Zhanga21d5932018-02-05 18:28:38 +0000835 // Modify the image in the new annotation.
836 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
837 0xff000000);
838 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
839 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
840 }
Jane Liu36567742017-07-06 11:13:35 -0400841
842 // Save the document, closing the page and document.
843 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000844 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400845 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400846
847 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400848 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400849}
850
851TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400852#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400853 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
854 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
855 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Jane Liu36567742017-07-06 11:13:35 -0400856#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000857 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
858 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
859 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400860#endif
861
862 // Open a file with two annotations and load its first page.
863 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000864 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400865 ASSERT_TRUE(page);
866 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
867
868 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000869 {
870 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
871 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
872 CompareBitmap(bitmap.get(), 595, 842, md5_original);
873 }
Jane Liu36567742017-07-06 11:13:35 -0400874
Lei Zhanga21d5932018-02-05 18:28:38 +0000875 {
876 // Create a stamp annotation and set its annotation rectangle.
877 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
878 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
879 ASSERT_TRUE(annot);
880 FS_RECTF rect;
881 rect.left = 200.f;
882 rect.bottom = 550.f;
883 rect.right = 450.f;
884 rect.top = 650.f;
885 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -0400886
Lei Zhanga21d5932018-02-05 18:28:38 +0000887 // Add a translucent text object to the new annotation.
888 FPDF_PAGEOBJECT text_object =
889 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
890 EXPECT_TRUE(text_object);
891 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
892 GetFPDFWideString(L"I'm a translucent text laying on other text.");
893 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
894 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
895 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
896 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
897 }
Jane Liu36567742017-07-06 11:13:35 -0400898
899 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000900 {
901 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
902 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
903 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
904 }
Jane Liu36567742017-07-06 11:13:35 -0400905
Lei Zhanga21d5932018-02-05 18:28:38 +0000906 {
907 // Retrieve the newly added stamp annotation and its text object.
908 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
909 FPDFPage_GetAnnot(page, 2));
910 ASSERT_TRUE(annot);
911 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
912 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
913 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -0400914
Lei Zhanga21d5932018-02-05 18:28:38 +0000915 // Modify the text in the new annotation.
916 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
917 GetFPDFWideString(L"New text!");
918 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
919 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
920 }
Jane Liu36567742017-07-06 11:13:35 -0400921
922 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000923 {
924 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
925 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
926 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
927 }
Jane Liu36567742017-07-06 11:13:35 -0400928
929 // Remove the new annotation, and check that the page renders as before.
930 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +0000931 {
932 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
933 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
934 CompareBitmap(bitmap.get(), 595, 842, md5_original);
935 }
Jane Liu36567742017-07-06 11:13:35 -0400936
937 UnloadPage(page);
938}
Jane Liu2e1a32b2017-07-06 12:01:25 -0400939
940TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
941 // Open a file with four annotations and load its first page.
942 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000943 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -0400944 ASSERT_TRUE(page);
945
Lei Zhangdf064df2017-08-31 02:33:27 -0700946 static constexpr char kDateKey[] = "M";
Lei Zhanga21d5932018-02-05 18:28:38 +0000947 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400948
Lei Zhanga21d5932018-02-05 18:28:38 +0000949 {
950 // Retrieve the first annotation.
951 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
952 FPDFPage_GetAnnot(page, 0));
953 ASSERT_TRUE(annot);
954
955 // Check that a non-existent key does not exist.
956 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
957
958 // Check that the string value of a non-string dictionary entry is empty.
959 static constexpr char kApKey[] = "AP";
960 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), kApKey));
961 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
962 FPDFAnnot_GetValueType(annot.get(), kApKey));
963 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kApKey, nullptr, 0));
964
965 // Check that the string value of the hash is correct.
966 static constexpr char kHashKey[] = "AAPL:Hash";
967 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
968 unsigned long len =
969 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
970 std::vector<char> buf(len);
971 EXPECT_EQ(66u,
972 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
973 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
974 BufferToWString(buf).c_str());
975
976 // Check that the string value of the modified date is correct.
977 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
978 len = FPDFAnnot_GetStringValue(annot.get(), kDateKey, nullptr, 0);
979 buf.clear();
980 buf.resize(len);
981 EXPECT_EQ(44u,
982 FPDFAnnot_GetStringValue(annot.get(), kDateKey, buf.data(), len));
983 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
984
985 // Update the date entry for the annotation.
986 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
987 GetFPDFWideString(kNewDate);
988 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kDateKey, text.get()));
989 }
Jane Liu2e1a32b2017-07-06 12:01:25 -0400990
991 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -0400992 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000993 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -0400994
995 // Open the saved annotation.
Dan Sinclair698aed72017-09-26 16:24:49 -0400996#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +0300997 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400998#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000999 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001000#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001001 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001002 page = LoadSavedPage(0);
1003 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001004 {
1005 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
1006 FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001007
Lei Zhanga21d5932018-02-05 18:28:38 +00001008 // Check that the string value of the modified date is the newly-set value.
1009 EXPECT_EQ(FPDF_OBJECT_STRING,
1010 FPDFAnnot_GetValueType(new_annot.get(), kDateKey));
1011 unsigned long len =
1012 FPDFAnnot_GetStringValue(new_annot.get(), kDateKey, nullptr, 0);
1013 std::vector<char> buf(len);
1014 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot.get(), kDateKey,
1015 buf.data(), len));
1016 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1017 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001018
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001019 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001020 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001021}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001022
Henrique Nakashima5970a472018-01-11 22:40:59 +00001023TEST_F(FPDFAnnotEmbeddertest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001024 // Open a file with four annotations and load its first page.
1025 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001026 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001027 ASSERT_TRUE(page);
1028
Lei Zhanga21d5932018-02-05 18:28:38 +00001029 {
1030 // Retrieve the first annotation.
1031 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1032 FPDFPage_GetAnnot(page, 0));
1033 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001034
Lei Zhanga21d5932018-02-05 18:28:38 +00001035 // Check that the string value of an AP returns the expected length.
1036 unsigned long normal_len = FPDFAnnot_GetAP(
1037 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1038 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001039
Lei Zhanga21d5932018-02-05 18:28:38 +00001040 // Check that the string value of an AP is not returned if the buffer is too
1041 // small. The result buffer should be overwritten with an empty string.
1042 std::vector<char> buf(normal_len - 1);
1043 // Write L"z" in the buffer to verify it's not overwritten.
1044 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1045 EXPECT_EQ(73970u,
1046 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1047 buf.data(), buf.size()));
1048 std::string ap = BufferToString(buf);
1049 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001050
Lei Zhanga21d5932018-02-05 18:28:38 +00001051 // Check that the string value of an AP is returned through a buffer that is
1052 // the right size.
1053 buf.clear();
1054 buf.resize(normal_len);
1055 EXPECT_EQ(73970u,
1056 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1057 buf.data(), buf.size()));
1058 ap = BufferToString(buf);
1059 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1060 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001061
Lei Zhanga21d5932018-02-05 18:28:38 +00001062 // Check that the string value of an AP is returned through a buffer that is
1063 // larger than necessary.
1064 buf.clear();
1065 buf.resize(normal_len + 1);
1066 EXPECT_EQ(73970u,
1067 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1068 buf.data(), buf.size()));
1069 ap = BufferToString(buf);
1070 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1071 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001072
Lei Zhanga21d5932018-02-05 18:28:38 +00001073 // Check that getting an AP for a mode that does not have an AP returns an
1074 // empty string.
1075 unsigned long rollover_len = FPDFAnnot_GetAP(
1076 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1077 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001078
Lei Zhanga21d5932018-02-05 18:28:38 +00001079 buf.clear();
1080 buf.resize(1000);
1081 EXPECT_EQ(2u,
1082 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1083 buf.data(), buf.size()));
1084 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001085
Lei Zhanga21d5932018-02-05 18:28:38 +00001086 // Check that setting the AP for an invalid appearance mode fails.
1087 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1088 GetFPDFWideString(L"new test ap");
1089 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1090 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1091 apText.get()));
1092 EXPECT_FALSE(FPDFAnnot_SetAP(
1093 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001094
Lei Zhanga21d5932018-02-05 18:28:38 +00001095 // Set the AP correctly now.
1096 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1097 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001098
Lei Zhanga21d5932018-02-05 18:28:38 +00001099 // Check that the new annotation value is equal to the value we just set.
1100 rollover_len = FPDFAnnot_GetAP(
1101 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1102 EXPECT_EQ(24u, rollover_len);
1103 buf.clear();
1104 buf.resize(rollover_len);
1105 EXPECT_EQ(24u,
1106 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1107 buf.data(), buf.size()));
1108 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001109
Lei Zhanga21d5932018-02-05 18:28:38 +00001110 // Check that the Normal AP was not touched when the Rollover AP was set.
1111 buf.clear();
1112 buf.resize(normal_len);
1113 EXPECT_EQ(73970u,
1114 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1115 buf.data(), buf.size()));
1116 ap = BufferToString(buf);
1117 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1118 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1119 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001120
1121 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001122 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001123 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001124
1125 OpenSavedDocument();
1126 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001127 {
1128 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
1129 FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001130
Lei Zhanga21d5932018-02-05 18:28:38 +00001131 // Check that the new annotation value is equal to the value we set before
1132 // saving.
1133 unsigned long rollover_len = FPDFAnnot_GetAP(
1134 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1135 EXPECT_EQ(24u, rollover_len);
1136 std::vector<char> buf(rollover_len);
1137 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1138 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1139 buf.data(), buf.size()));
1140 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1141 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001142
1143 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001144 CloseSavedPage(page);
1145 CloseSavedDocument();
1146}
1147
1148TEST_F(FPDFAnnotEmbeddertest, RemoveOptionalAP) {
1149 // Open a file with four annotations and load its first page.
1150 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001151 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001152 ASSERT_TRUE(page);
1153
Lei Zhanga21d5932018-02-05 18:28:38 +00001154 {
1155 // Retrieve the first annotation.
1156 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1157 FPDFPage_GetAnnot(page, 0));
1158 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001159
Lei Zhanga21d5932018-02-05 18:28:38 +00001160 // Set Down AP. Normal AP is already set.
1161 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1162 GetFPDFWideString(L"new test ap");
1163 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1164 apText.get()));
1165 EXPECT_EQ(73970u,
1166 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1167 nullptr, 0));
1168 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1169 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001170
Lei Zhanga21d5932018-02-05 18:28:38 +00001171 // Check that setting the Down AP to null removes the Down entry but keeps
1172 // Normal intact.
1173 EXPECT_TRUE(
1174 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1175 EXPECT_EQ(73970u,
1176 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1177 nullptr, 0));
1178 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1179 nullptr, 0));
1180 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001181
Lei Zhang75c81712018-02-08 17:22:39 +00001182 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001183}
1184
1185TEST_F(FPDFAnnotEmbeddertest, RemoveRequiredAP) {
1186 // Open a file with four annotations and load its first page.
1187 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001188 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001189 ASSERT_TRUE(page);
1190
Lei Zhanga21d5932018-02-05 18:28:38 +00001191 {
1192 // Retrieve the first annotation.
1193 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1194 FPDFPage_GetAnnot(page, 0));
1195 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001196
Lei Zhanga21d5932018-02-05 18:28:38 +00001197 // Set Down AP. Normal AP is already set.
1198 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1199 GetFPDFWideString(L"new test ap");
1200 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1201 apText.get()));
1202 EXPECT_EQ(73970u,
1203 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1204 nullptr, 0));
1205 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1206 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001207
Lei Zhanga21d5932018-02-05 18:28:38 +00001208 // Check that setting the Normal AP to null removes the whole AP dictionary.
1209 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1210 nullptr));
1211 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1212 nullptr, 0));
1213 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1214 nullptr, 0));
1215 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001216
Lei Zhang75c81712018-02-08 17:22:39 +00001217 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001218}
1219
Jane Liu300bb272017-08-21 14:37:53 -04001220TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) {
1221 // Open a file with annotations and load its first page.
1222 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001223 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001224 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001225 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001226
Lei Zhanga21d5932018-02-05 18:28:38 +00001227 {
1228 // Retrieve the highlight annotation which has its popup defined.
1229 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1230 FPDFPage_GetAnnot(page, 0));
1231 ASSERT_TRUE(annot);
1232 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1233 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1234 static constexpr char kPopupKey[] = "Popup";
1235 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1236 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1237 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001238
Lei Zhanga21d5932018-02-05 18:28:38 +00001239 // Retrieve and verify the popup of the highlight annotation.
1240 std::unique_ptr<void, FPDFAnnotationDeleter> popup(
1241 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1242 ASSERT_TRUE(popup);
1243 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1244 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1245 FS_RECTF rect;
1246 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1247 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1248 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001249
Lei Zhanga21d5932018-02-05 18:28:38 +00001250 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1251 // since "IRT" is not a key in |annot|'s dictionary.
1252 static constexpr char kIRTKey[] = "IRT";
1253 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1254 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001255
Lei Zhanga21d5932018-02-05 18:28:38 +00001256 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1257 // would fail, since its parent is not an annotation.
1258 static constexpr char kPKey[] = "P";
1259 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPKey));
1260 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1261 FPDFAnnot_GetValueType(annot.get(), kPKey));
1262 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kPKey));
1263 }
Jane Liu300bb272017-08-21 14:37:53 -04001264
Jane Liu300bb272017-08-21 14:37:53 -04001265 UnloadPage(page);
1266}
1267
Diana Gage7e0c05d2017-07-19 17:33:33 -07001268TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
1269 // Open file with form text fields.
1270 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001271 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001272 ASSERT_TRUE(page);
1273
Lei Zhanga21d5932018-02-05 18:28:38 +00001274 {
1275 // Retrieve the first annotation: user-editable text field.
1276 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1277 FPDFPage_GetAnnot(page, 0));
1278 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001279
Lei Zhanga21d5932018-02-05 18:28:38 +00001280 // Check that the flag values are as expected.
1281 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1282 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1283 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001284
Lei Zhanga21d5932018-02-05 18:28:38 +00001285 {
1286 // Retrieve the second annotation: read-only text field.
1287 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1288 FPDFPage_GetAnnot(page, 1));
1289 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001290
Lei Zhanga21d5932018-02-05 18:28:38 +00001291 // Check that the flag values are as expected.
1292 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1293 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1294 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001295
1296 UnloadPage(page);
1297}
1298
1299TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
1300 // Open file with form text fields.
1301 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001302 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001303 ASSERT_TRUE(page);
1304
Lei Zhanga21d5932018-02-05 18:28:38 +00001305 {
1306 // Retrieve the first annotation: user-editable combobox.
1307 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1308 FPDFPage_GetAnnot(page, 0));
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_FALSE(flags & FPDF_FORMFLAG_READONLY);
1314 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1315 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1316 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001317
Lei Zhanga21d5932018-02-05 18:28:38 +00001318 {
1319 // Retrieve the second annotation: regular combobox.
1320 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1321 FPDFPage_GetAnnot(page, 1));
1322 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001323
Lei Zhanga21d5932018-02-05 18:28:38 +00001324 // Check that the flag values are as expected.
1325 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1326 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1327 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1328 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1329 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001330
Lei Zhanga21d5932018-02-05 18:28:38 +00001331 {
1332 // Retrieve the third annotation: read-only combobox.
1333 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1334 FPDFPage_GetAnnot(page, 2));
1335 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001336
Lei Zhanga21d5932018-02-05 18:28:38 +00001337 // Check that the flag values are as expected.
1338 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1339 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1340 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1341 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1342 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001343
1344 UnloadPage(page);
1345}
Diana Gage40870db2017-07-19 18:16:03 -07001346
1347TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
1348 // Open file with form text fields.
1349 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1350 FPDF_PAGE page = LoadPage(0);
1351 ASSERT_TRUE(page);
1352
1353 // Attempt to get an annotation where no annotation exists on page.
1354 FPDF_ANNOTATION annot =
1355 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0);
1356 EXPECT_FALSE(annot);
1357
1358 UnloadPage(page);
1359}
1360
1361TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
1362 // Open file with form text fields.
1363 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1364 FPDF_PAGE page = LoadPage(0);
1365 ASSERT_TRUE(page);
1366
Lei Zhanga21d5932018-02-05 18:28:38 +00001367 {
1368 // Retrieve user-editable text field annotation.
1369 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1370 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1371 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001372
Lei Zhanga21d5932018-02-05 18:28:38 +00001373 // Check that interactive form annotation flag values are as expected.
1374 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1375 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1376 }
Diana Gage40870db2017-07-19 18:16:03 -07001377
Lei Zhanga21d5932018-02-05 18:28:38 +00001378 {
1379 // Retrieve read-only text field annotation.
1380 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1381 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1382 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001383
Lei Zhanga21d5932018-02-05 18:28:38 +00001384 // Check that interactive form annotation flag values are as expected.
1385 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1386 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1387 }
Diana Gage40870db2017-07-19 18:16:03 -07001388
1389 UnloadPage(page);
1390}
1391
1392TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1393 // Open file with form comboboxes.
1394 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1395 FPDF_PAGE page = LoadPage(0);
1396 ASSERT_TRUE(page);
1397
Lei Zhanga21d5932018-02-05 18:28:38 +00001398 {
1399 // Retrieve user-editable combobox annotation.
1400 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001401 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001402 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_FALSE(flags & FPDF_FORMFLAG_READONLY);
1407 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1408 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1409 }
Diana Gage40870db2017-07-19 18:16:03 -07001410
Lei Zhanga21d5932018-02-05 18:28:38 +00001411 {
1412 // Retrieve regular combobox annotation.
1413 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001414 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001415 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001416
Lei Zhanga21d5932018-02-05 18:28:38 +00001417 // Check that interactive form annotation flag values are as expected.
1418 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1419 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1420 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1421 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1422 }
Diana Gage40870db2017-07-19 18:16:03 -07001423
Lei Zhanga21d5932018-02-05 18:28:38 +00001424 {
1425 // Retrieve read-only combobox annotation.
1426 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001427 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001428 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001429
Lei Zhanga21d5932018-02-05 18:28:38 +00001430 // Check that interactive form annotation flag values are as expected.
1431 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1432 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1433 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1434 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1435 }
Diana Gage40870db2017-07-19 18:16:03 -07001436
1437 UnloadPage(page);
1438}