blob: 61cd2aae3d3a9545a4495cd022bc6d667b32fa1c [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
Jane Liu4fd9a472017-06-01 18:56:09 -040048TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
49 // Open a file with one annotation and load its first page.
50 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000051 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
52 // the FORM API calls in LoadPage() makes this test fail. So use
53 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu4fd9a472017-06-01 18:56:09 -040054 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
55 ASSERT_TRUE(page);
56
57 // Check that there is a total of 1 annotation on its first page.
58 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
59
60 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +000061 {
62 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
63 FPDFPage_GetAnnot(page, 0));
64 ASSERT_TRUE(annot);
65 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -040066
Lei Zhanga21d5932018-02-05 18:28:38 +000067 // Check that the annotation color is yellow.
68 unsigned int R;
69 unsigned int G;
70 unsigned int B;
71 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +000072 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +000073 &G, &B, &A));
74 EXPECT_EQ(255u, R);
75 EXPECT_EQ(255u, G);
76 EXPECT_EQ(0u, B);
77 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -040078
Lei Zhanga21d5932018-02-05 18:28:38 +000079 // Check that the author is correct.
80 static constexpr char kAuthorKey[] = "T";
81 EXPECT_EQ(FPDF_OBJECT_STRING,
82 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
83 unsigned long len =
84 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
85 std::vector<char> buf(len);
86 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
87 len));
88 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -040089
Lei Zhanga21d5932018-02-05 18:28:38 +000090 // Check that the content is correct.
91 EXPECT_EQ(FPDF_OBJECT_STRING,
92 FPDFAnnot_GetValueType(annot.get(), kContentsKey));
93 len = FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
94 buf.clear();
95 buf.resize(len);
96 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
97 buf.data(), len));
98 const wchar_t contents[] =
99 L"This is a note for that highlight annotation. Very long highlight "
100 "annotation. Long long long Long long longLong long longLong long "
101 "longLong long longLong long longLong long longLong long longLong long "
102 "longLong long longLong long longLong long longLong long longLong long "
103 "longLong long longLong long longLong long longLong long longLong long "
104 "longLong long longLong long longLong long longLong long longLong long "
105 "longLong long longLong long longLong long longLong long longLong long "
106 "longLong long longLong long longLong long longLong long longLong long "
107 "longLong long longLong long longLong long longLong long longLong long "
108 "longLong long longLong long longLong long longLong long longLong long "
109 "longLong long longLong long longLong long longLong long longLong long "
110 "longLong long longLong long longLong long longLong long longLong long "
111 "longLong long longLong long longLong long longLong long longLong long "
112 "longLong long longLong long longLong long longLong long longLong long "
113 "longLong long longLong long longLong long longLong long longLong long "
114 "longLong long longLong long longLong 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 long. END";
119 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400120
Lei Zhanga21d5932018-02-05 18:28:38 +0000121 // Check that the quadpoints are correct.
122 FS_QUADPOINTSF quadpoints;
123 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &quadpoints));
124 EXPECT_EQ(115.802643f, quadpoints.x1);
125 EXPECT_EQ(718.913940f, quadpoints.y1);
126 EXPECT_EQ(157.211182f, quadpoints.x4);
127 EXPECT_EQ(706.264465f, quadpoints.y4);
128 }
Lei Zhang75c81712018-02-08 17:22:39 +0000129 FPDF_ClosePage(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400130}
131
132TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
133 // Open a file with three annotations and load its first page.
134 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000135 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
136 // the FORM API calls in LoadPage() makes this test fail. So use
137 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu4fd9a472017-06-01 18:56:09 -0400138 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
139 ASSERT_TRUE(page);
140
141 // Check that there is a total of 3 annotation on its first page.
142 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
143
Lei Zhanga21d5932018-02-05 18:28:38 +0000144 {
145 // Check that the third annotation is of type "ink".
146 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
147 FPDFPage_GetAnnot(page, 2));
148 ASSERT_TRUE(annot);
149 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400150
Lei Zhanga21d5932018-02-05 18:28:38 +0000151 // Check that the annotation color is blue with opacity.
152 unsigned int R;
153 unsigned int G;
154 unsigned int B;
155 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000156 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000157 &G, &B, &A));
158 EXPECT_EQ(0u, R);
159 EXPECT_EQ(0u, G);
160 EXPECT_EQ(255u, B);
161 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400162
Lei Zhanga21d5932018-02-05 18:28:38 +0000163 // Check that there is no content.
164 EXPECT_EQ(2u,
165 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400166
Lei Zhanga21d5932018-02-05 18:28:38 +0000167 // Check that the rectange coordinates are correct.
168 // Note that upon rendering, the rectangle coordinates will be adjusted.
169 FS_RECTF rect;
170 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
171 EXPECT_EQ(351.820404f, rect.left);
172 EXPECT_EQ(583.830688f, rect.bottom);
173 EXPECT_EQ(475.336090f, rect.right);
174 EXPECT_EQ(681.535034f, rect.top);
175 }
Lei Zhang75c81712018-02-08 17:22:39 +0000176 FPDF_ClosePage(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400177}
Jane Liu20eafda2017-06-07 10:33:24 -0400178
179TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
180 // Open a file with one annotation and load its first page.
181 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000182 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400183 ASSERT_TRUE(page);
184
185 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400186 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400187
188 UnloadPage(page);
189}
190
Jane Liud321ef92017-06-14 09:56:22 -0400191TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400192 // Open a file with no annotation and load its first page.
193 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000194 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400195 ASSERT_TRUE(page);
196 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
197
Lei Zhanga21d5932018-02-05 18:28:38 +0000198 {
199 // Add a text annotation to the page.
200 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
201 FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
202 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400203
Lei Zhanga21d5932018-02-05 18:28:38 +0000204 // Check that there is now 1 annotations on this page.
205 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400206
Lei Zhanga21d5932018-02-05 18:28:38 +0000207 // Check that the subtype of the annotation is correct.
208 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
209 }
Jane Liue10509a2017-06-20 16:47:41 -0400210
Lei Zhanga21d5932018-02-05 18:28:38 +0000211 {
212 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
213 FPDFPage_GetAnnot(page, 0));
214 ASSERT_TRUE(annot);
215 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400216
Lei Zhanga21d5932018-02-05 18:28:38 +0000217 // Set the color of the annotation.
218 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
219 102, 153, 204));
220 // Check that the color has been set correctly.
221 unsigned int R;
222 unsigned int G;
223 unsigned int B;
224 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000225 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000226 &G, &B, &A));
227 EXPECT_EQ(51u, R);
228 EXPECT_EQ(102u, G);
229 EXPECT_EQ(153u, B);
230 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400231
Lei Zhanga21d5932018-02-05 18:28:38 +0000232 // Change the color of the annotation.
233 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
234 153, 102, 51));
235 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000236 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000237 &G, &B, &A));
238 EXPECT_EQ(204u, R);
239 EXPECT_EQ(153u, G);
240 EXPECT_EQ(102u, B);
241 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400242
Lei Zhanga21d5932018-02-05 18:28:38 +0000243 // Set the annotation rectangle.
244 FS_RECTF rect;
245 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
246 EXPECT_EQ(0.f, rect.left);
247 EXPECT_EQ(0.f, rect.right);
248 rect.left = 35;
249 rect.bottom = 150;
250 rect.right = 53;
251 rect.top = 165;
252 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
253 // Check that the annotation rectangle has been set correctly.
254 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
255 EXPECT_EQ(35.f, rect.left);
256 EXPECT_EQ(150.f, rect.bottom);
257 EXPECT_EQ(53.f, rect.right);
258 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400259
Lei Zhanga21d5932018-02-05 18:28:38 +0000260 // Set the content of the annotation.
261 static constexpr wchar_t kContents[] =
262 L"Hello! This is a customized content.";
263 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
264 GetFPDFWideString(kContents);
265 ASSERT_TRUE(
266 FPDFAnnot_SetStringValue(annot.get(), kContentsKey, text.get()));
267 // Check that the content has been set correctly.
268 unsigned long len =
269 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
270 std::vector<char> buf(len);
271 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
272 buf.data(), len));
273 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
274 }
Jane Liu20eafda2017-06-07 10:33:24 -0400275 UnloadPage(page);
276}
277
278TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
279 // Open a file with one annotation and load its first page.
280 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000281 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400282 ASSERT_TRUE(page);
283
284 // Check that there is a total of one annotation on its first page, and verify
285 // its quadpoints.
286 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400287 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000288 {
289 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
290 FPDFPage_GetAnnot(page, 0));
291 ASSERT_TRUE(annot);
292 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &quadpoints));
293 EXPECT_EQ(115.802643f, quadpoints.x1);
294 EXPECT_EQ(718.913940f, quadpoints.y1);
295 EXPECT_EQ(157.211182f, quadpoints.x4);
296 EXPECT_EQ(706.264465f, quadpoints.y4);
297 }
Jane Liu20eafda2017-06-07 10:33:24 -0400298
299 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000300 {
301 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
302 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
303 ASSERT_TRUE(annot);
304 quadpoints.x1 = 140.802643f;
305 quadpoints.x3 = 140.802643f;
306 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), &quadpoints));
307 }
Jane Liu20eafda2017-06-07 10:33:24 -0400308
309 // Save the document, closing the page and document.
310 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000311 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400312
313 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000314 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400315
316 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000317 page = LoadSavedPage(0);
318 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400319
320 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000321 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400322
Lei Zhanga21d5932018-02-05 18:28:38 +0000323 {
324 // Check that the second annotation is an underline annotation and verify
325 // its quadpoints.
326 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
327 FPDFPage_GetAnnot(page, 1));
328 ASSERT_TRUE(new_annot);
329 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
330 FS_QUADPOINTSF new_quadpoints;
331 ASSERT_TRUE(
332 FPDFAnnot_GetAttachmentPoints(new_annot.get(), &new_quadpoints));
333 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
334 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
335 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
336 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
337 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400338
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000339 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400340 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400341}
Jane Liu06462752017-06-27 16:41:14 -0400342
343TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400344#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400345 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
346 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
347 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400348#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000349 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
350 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
351 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400352#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000353 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
354 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
355 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400356#endif
357
Jane Liu06462752017-06-27 16:41:14 -0400358 // Open a file with four annotations and load its first page.
359 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000360 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400361 ASSERT_TRUE(page);
362 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
363
Jane Liub370e5a2017-08-16 13:24:58 -0400364 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000365 {
366 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
367 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
368 CompareBitmap(bitmap.get(), 612, 792, md5_original);
369 }
Jane Liub370e5a2017-08-16 13:24:58 -0400370
Jane Liu0c6b07d2017-08-15 10:50:22 -0400371 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400372 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000373
374 // Retrieve the highlight annotation which has its AP stream already defined.
375 {
376 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
377 FPDFPage_GetAnnot(page, 0));
378 ASSERT_TRUE(annot);
379 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
380
381 // Check that color cannot be set when an AP stream is defined already.
382 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
383 102, 153, 204));
384
385 // Verify its attachment points.
386 FS_QUADPOINTSF quadpoints;
387 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &quadpoints));
388 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
389 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
390 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
391 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
392
393 // Check that updating the attachment points would succeed.
394 quadpoints.x1 -= 50.f;
395 quadpoints.x2 -= 50.f;
396 quadpoints.x3 -= 50.f;
397 quadpoints.x4 -= 50.f;
398 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), &quadpoints));
399 FS_QUADPOINTSF new_quadpoints;
400 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &new_quadpoints));
401 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
402 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
403 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
404 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
405
406 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000407 {
408 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
409 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
410 CompareBitmap(bitmap.get(), 612, 792, md5_original);
411 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000412
413 // Verify its annotation rectangle.
414 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
415 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
416 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
417 EXPECT_NEAR(136.325f, rect.right, 0.001f);
418 EXPECT_NEAR(721.292f, rect.top, 0.001f);
419
420 // Check that updating the rectangle would succeed.
421 rect.left -= 60.f;
422 rect.right -= 60.f;
423 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
424 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
425 EXPECT_EQ(rect.right, new_rect.right);
426 }
Jane Liu06462752017-06-27 16:41:14 -0400427
Jane Liub370e5a2017-08-16 13:24:58 -0400428 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000429 {
430 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
431 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
432 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
433 }
Jane Liub370e5a2017-08-16 13:24:58 -0400434
Lei Zhanga21d5932018-02-05 18:28:38 +0000435 {
436 // Retrieve the square annotation which has its AP stream already defined.
437 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
438 FPDFPage_GetAnnot(page, 2));
439 ASSERT_TRUE(annot);
440 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400441
Lei Zhanga21d5932018-02-05 18:28:38 +0000442 // Check that updating the rectangle would succeed.
443 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
444 rect.left += 70.f;
445 rect.right += 70.f;
446 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
447 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
448 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400449
Lei Zhanga21d5932018-02-05 18:28:38 +0000450 // Check that updating the rectangle changes the square annotation's
451 // position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000452 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
453 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
454 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000455 }
Jane Liub370e5a2017-08-16 13:24:58 -0400456
Jane Liu06462752017-06-27 16:41:14 -0400457 UnloadPage(page);
458}
Jane Liu8ce58f52017-06-29 13:40:22 -0400459
460TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
461 // Open a file with 3 annotations on its first page.
462 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000463 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
464 // the FORM API calls in LoadPage() makes this test fail. So use
465 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu8ce58f52017-06-29 13:40:22 -0400466 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
467 ASSERT_TRUE(page);
468 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
469
Jane Liu0c6b07d2017-08-15 10:50:22 -0400470 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400471
Lei Zhanga21d5932018-02-05 18:28:38 +0000472 // Check that the annotations have the expected rectangle coordinates.
473 {
474 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
475 FPDFPage_GetAnnot(page, 0));
476 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
477 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
478 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400479
Lei Zhanga21d5932018-02-05 18:28:38 +0000480 {
481 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
482 FPDFPage_GetAnnot(page, 1));
483 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
484 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
485 }
486
487 {
488 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
489 FPDFPage_GetAnnot(page, 2));
490 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
491 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
492 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400493
494 // Check that nothing happens when attempting to remove an annotation with an
495 // out-of-bound index.
496 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
497 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
498 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
499
500 // Remove the second annotation.
501 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
502 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
503 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
504
505 // Save the document, closing the page and document.
506 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
507 FPDF_ClosePage(page);
508
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400509 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400510 // Open the saved document.
511 std::string new_file = GetString();
512 FPDF_FILEACCESS file_access;
513 memset(&file_access, 0, sizeof(file_access));
514 file_access.m_FileLen = new_file.size();
515 file_access.m_GetBlock = GetBlockFromString;
516 file_access.m_Param = &new_file;
517 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
518 ASSERT_TRUE(new_doc);
519 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
520 ASSERT_TRUE(new_page);
521
522 // Check that the saved document has 2 annotations on the first page.
523 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
524
Lei Zhanga21d5932018-02-05 18:28:38 +0000525 // Check that the remaining 2 annotations are the original 1st and 3rd ones
526 // by verifying their rectangle coordinates.
527 {
528 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
529 FPDFPage_GetAnnot(new_page, 0));
530 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
531 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
532 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400533
Lei Zhanga21d5932018-02-05 18:28:38 +0000534 {
535 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
536 FPDFPage_GetAnnot(new_page, 1));
537 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
538 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
539 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400540 FPDF_ClosePage(new_page);
541 FPDF_CloseDocument(new_doc);
542}
Jane Liu8ce58f52017-06-29 13:40:22 -0400543
Jane Liubaa7ff42017-06-29 19:18:23 -0400544TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400545#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400546 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000547 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
548 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
549 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Jane Liubaa7ff42017-06-29 19:18:23 -0400550#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000551 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000552 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
553 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
554 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400555#endif
556
557 // Open a file with two annotations and load its first page.
558 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000559 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400560 ASSERT_TRUE(page);
561 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
562
563 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000564 {
565 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
566 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
567 CompareBitmap(bitmap.get(), 595, 842, md5_original);
568 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400569
Lei Zhanga21d5932018-02-05 18:28:38 +0000570 {
571 // Retrieve the stamp annotation which has its AP stream already defined.
572 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
573 FPDFPage_GetAnnot(page, 0));
574 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400575
Lei Zhanga21d5932018-02-05 18:28:38 +0000576 // Check that this annotation has one path object and retrieve it.
577 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000578 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000579 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
580 EXPECT_FALSE(path);
581 path = FPDFAnnot_GetObject(annot.get(), 0);
582 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
583 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400584
Lei Zhanga21d5932018-02-05 18:28:38 +0000585 // Modify the color of the path object.
586 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
587 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400588
Lei Zhanga21d5932018-02-05 18:28:38 +0000589 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000590 {
591 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
592 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
593 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
594 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400595
Lei Zhanga21d5932018-02-05 18:28:38 +0000596 // Add a second path object to the same annotation.
597 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
598 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
599 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
600 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
601 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
602 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
603 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400604
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000605 // The object is in the annontation, not in the page, so the page object
606 // array should not change.
607 ASSERT_EQ(32, FPDFPage_CountObjects(page));
608
Lei Zhanga21d5932018-02-05 18:28:38 +0000609 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000610 {
611 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
612 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
613 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
614 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400615
Lei Zhanga21d5932018-02-05 18:28:38 +0000616 // Delete the newly added path object.
617 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
618 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000619 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000620 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400621
622 // Check that the page renders the same as before.
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 Liubaa7ff42017-06-29 19:18:23 -0400628
Jane Liubaa7ff42017-06-29 19:18:23 -0400629 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400630
Lei Zhanga21d5932018-02-05 18:28:38 +0000631 {
632 // Create another stamp annotation and set its annotation rectangle.
633 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
634 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
635 ASSERT_TRUE(annot);
636 rect.left = 200.f;
637 rect.bottom = 400.f;
638 rect.right = 500.f;
639 rect.top = 600.f;
640 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400641
Lei Zhanga21d5932018-02-05 18:28:38 +0000642 // Add a new path to the annotation.
643 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
644 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
645 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
646 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
647 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
648 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
649 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
650 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
651 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
652 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
653
654 // Check that the annotation's bounding box came from its rectangle.
655 FS_RECTF new_rect;
656 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
657 EXPECT_EQ(rect.left, new_rect.left);
658 EXPECT_EQ(rect.bottom, new_rect.bottom);
659 EXPECT_EQ(rect.right, new_rect.right);
660 EXPECT_EQ(rect.top, new_rect.top);
661 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400662
663 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400664 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000665 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400666
667 // Open the saved document.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400668 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000669 page = LoadSavedPage(0);
670 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400671
Jane Liu36567742017-07-06 11:13:35 -0400672 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000673 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400674
Lei Zhanga21d5932018-02-05 18:28:38 +0000675 {
676 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
677 FPDFPage_GetAnnot(page, 2));
678 ASSERT_TRUE(annot);
679 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400680
Lei Zhanga21d5932018-02-05 18:28:38 +0000681 // Check that the new annotation's rectangle is as defined.
682 FS_RECTF new_rect;
683 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
684 EXPECT_EQ(rect.left, new_rect.left);
685 EXPECT_EQ(rect.bottom, new_rect.bottom);
686 EXPECT_EQ(rect.right, new_rect.right);
687 EXPECT_EQ(rect.top, new_rect.top);
688 }
689
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000690 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400691 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400692}
Jane Liub137e752017-07-05 15:04:33 -0400693
694TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
695 // Open a file with an annotation and load its first page.
696 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000697 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400698 ASSERT_TRUE(page);
699
700 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000701 {
702 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
703 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
704 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
705 }
Jane Liub137e752017-07-05 15:04:33 -0400706
Lei Zhanga21d5932018-02-05 18:28:38 +0000707 {
708 // Retrieve the annotation.
709 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
710 FPDFPage_GetAnnot(page, 0));
711 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400712
Lei Zhanga21d5932018-02-05 18:28:38 +0000713 // Check that the original flag values are as expected.
714 int flags = FPDFAnnot_GetFlags(annot.get());
715 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
716 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400717
Lei Zhanga21d5932018-02-05 18:28:38 +0000718 // Set the HIDDEN flag.
719 flags |= FPDF_ANNOT_FLAG_HIDDEN;
720 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
721 flags = FPDFAnnot_GetFlags(annot.get());
722 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
723 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400724
Lei Zhanga21d5932018-02-05 18:28:38 +0000725 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000726 {
727 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
728 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
729 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
730 }
Jane Liub137e752017-07-05 15:04:33 -0400731
Lei Zhanga21d5932018-02-05 18:28:38 +0000732 // Unset the HIDDEN flag.
733 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
734 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
735 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
736 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
737 flags = FPDFAnnot_GetFlags(annot.get());
738 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
739 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400740
Lei Zhanga21d5932018-02-05 18:28:38 +0000741 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000742 {
743 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
744 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
745 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
746 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000747 }
Jane Liub137e752017-07-05 15:04:33 -0400748
Jane Liub137e752017-07-05 15:04:33 -0400749 UnloadPage(page);
750}
Jane Liu36567742017-07-06 11:13:35 -0400751
752TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400753#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400754 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
755 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
756 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Jane Liu36567742017-07-06 11:13:35 -0400757#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000758 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
759 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
760 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400761#endif
762
763 // Open a file with two annotations and load its first page.
764 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000765 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400766 ASSERT_TRUE(page);
767 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
768
769 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000770 {
771 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
772 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
773 CompareBitmap(bitmap.get(), 595, 842, md5_original);
774 }
Jane Liu36567742017-07-06 11:13:35 -0400775
Jane Liu36567742017-07-06 11:13:35 -0400776 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000777 FPDF_BITMAP image_bitmap;
778
779 {
780 // Create a stamp annotation and set its annotation rectangle.
781 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
782 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
783 ASSERT_TRUE(annot);
784 FS_RECTF rect;
785 rect.left = 200.f;
786 rect.bottom = 600.f;
787 rect.right = 400.f;
788 rect.top = 800.f;
789 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
790
791 // Add a solid-color translucent image object to the new annotation.
792 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
793 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
794 0xeeeecccc);
795 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
796 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
797 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
798 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
799 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
800 kBitmapSize, 0, 0));
801 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
802 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
803 }
Jane Liu36567742017-07-06 11:13:35 -0400804
805 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000806 {
807 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
808 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
809 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
810 }
Jane Liu36567742017-07-06 11:13:35 -0400811
Lei Zhanga21d5932018-02-05 18:28:38 +0000812 {
813 // Retrieve the newly added stamp annotation and its image object.
814 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
815 FPDFPage_GetAnnot(page, 2));
816 ASSERT_TRUE(annot);
817 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
818 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
819 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400820
Lei Zhanga21d5932018-02-05 18:28:38 +0000821 // Modify the image in the new annotation.
822 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
823 0xff000000);
824 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
825 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
826 }
Jane Liu36567742017-07-06 11:13:35 -0400827
828 // Save the document, closing the page and document.
829 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000830 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400831 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400832
833 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400834 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400835}
836
837TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400838#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400839 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
840 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
841 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Jane Liu36567742017-07-06 11:13:35 -0400842#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000843 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
844 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
845 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400846#endif
847
848 // Open a file with two annotations and load its first page.
849 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000850 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400851 ASSERT_TRUE(page);
852 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
853
854 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000855 {
856 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
857 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
858 CompareBitmap(bitmap.get(), 595, 842, md5_original);
859 }
Jane Liu36567742017-07-06 11:13:35 -0400860
Lei Zhanga21d5932018-02-05 18:28:38 +0000861 {
862 // Create a stamp annotation and set its annotation rectangle.
863 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
864 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
865 ASSERT_TRUE(annot);
866 FS_RECTF rect;
867 rect.left = 200.f;
868 rect.bottom = 550.f;
869 rect.right = 450.f;
870 rect.top = 650.f;
871 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -0400872
Lei Zhanga21d5932018-02-05 18:28:38 +0000873 // Add a translucent text object to the new annotation.
874 FPDF_PAGEOBJECT text_object =
875 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
876 EXPECT_TRUE(text_object);
877 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
878 GetFPDFWideString(L"I'm a translucent text laying on other text.");
879 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
880 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
881 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
882 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
883 }
Jane Liu36567742017-07-06 11:13:35 -0400884
885 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000886 {
887 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
888 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
889 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
890 }
Jane Liu36567742017-07-06 11:13:35 -0400891
Lei Zhanga21d5932018-02-05 18:28:38 +0000892 {
893 // Retrieve the newly added stamp annotation and its text object.
894 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
895 FPDFPage_GetAnnot(page, 2));
896 ASSERT_TRUE(annot);
897 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
898 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
899 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -0400900
Lei Zhanga21d5932018-02-05 18:28:38 +0000901 // Modify the text in the new annotation.
902 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
903 GetFPDFWideString(L"New text!");
904 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
905 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
906 }
Jane Liu36567742017-07-06 11:13:35 -0400907
908 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000909 {
910 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
911 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
912 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
913 }
Jane Liu36567742017-07-06 11:13:35 -0400914
915 // Remove the new annotation, and check that the page renders as before.
916 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +0000917 {
918 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
919 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
920 CompareBitmap(bitmap.get(), 595, 842, md5_original);
921 }
Jane Liu36567742017-07-06 11:13:35 -0400922
923 UnloadPage(page);
924}
Jane Liu2e1a32b2017-07-06 12:01:25 -0400925
926TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
927 // Open a file with four annotations and load its first page.
928 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000929 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -0400930 ASSERT_TRUE(page);
931
Lei Zhangdf064df2017-08-31 02:33:27 -0700932 static constexpr char kDateKey[] = "M";
Lei Zhanga21d5932018-02-05 18:28:38 +0000933 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400934
Lei Zhanga21d5932018-02-05 18:28:38 +0000935 {
936 // Retrieve the first annotation.
937 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
938 FPDFPage_GetAnnot(page, 0));
939 ASSERT_TRUE(annot);
940
941 // Check that a non-existent key does not exist.
942 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
943
944 // Check that the string value of a non-string dictionary entry is empty.
945 static constexpr char kApKey[] = "AP";
946 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), kApKey));
947 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
948 FPDFAnnot_GetValueType(annot.get(), kApKey));
949 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kApKey, nullptr, 0));
950
951 // Check that the string value of the hash is correct.
952 static constexpr char kHashKey[] = "AAPL:Hash";
953 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
954 unsigned long len =
955 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
956 std::vector<char> buf(len);
957 EXPECT_EQ(66u,
958 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
959 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
960 BufferToWString(buf).c_str());
961
962 // Check that the string value of the modified date is correct.
963 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
964 len = FPDFAnnot_GetStringValue(annot.get(), kDateKey, nullptr, 0);
965 buf.clear();
966 buf.resize(len);
967 EXPECT_EQ(44u,
968 FPDFAnnot_GetStringValue(annot.get(), kDateKey, buf.data(), len));
969 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
970
971 // Update the date entry for the annotation.
972 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
973 GetFPDFWideString(kNewDate);
974 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kDateKey, text.get()));
975 }
Jane Liu2e1a32b2017-07-06 12:01:25 -0400976
977 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -0400978 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000979 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -0400980
981 // Open the saved annotation.
Dan Sinclair698aed72017-09-26 16:24:49 -0400982#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +0300983 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400984#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000985 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400986#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400987 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000988 page = LoadSavedPage(0);
989 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +0000990 {
991 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
992 FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -0400993
Lei Zhanga21d5932018-02-05 18:28:38 +0000994 // Check that the string value of the modified date is the newly-set value.
995 EXPECT_EQ(FPDF_OBJECT_STRING,
996 FPDFAnnot_GetValueType(new_annot.get(), kDateKey));
997 unsigned long len =
998 FPDFAnnot_GetStringValue(new_annot.get(), kDateKey, nullptr, 0);
999 std::vector<char> buf(len);
1000 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot.get(), kDateKey,
1001 buf.data(), len));
1002 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1003 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001004
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001005 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001006 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001007}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001008
Henrique Nakashima5970a472018-01-11 22:40:59 +00001009TEST_F(FPDFAnnotEmbeddertest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001010 // Open a file with four annotations and load its first page.
1011 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001012 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001013 ASSERT_TRUE(page);
1014
Lei Zhanga21d5932018-02-05 18:28:38 +00001015 {
1016 // Retrieve the first annotation.
1017 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1018 FPDFPage_GetAnnot(page, 0));
1019 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001020
Lei Zhanga21d5932018-02-05 18:28:38 +00001021 // Check that the string value of an AP returns the expected length.
1022 unsigned long normal_len = FPDFAnnot_GetAP(
1023 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1024 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001025
Lei Zhanga21d5932018-02-05 18:28:38 +00001026 // Check that the string value of an AP is not returned if the buffer is too
1027 // small. The result buffer should be overwritten with an empty string.
1028 std::vector<char> buf(normal_len - 1);
1029 // Write L"z" in the buffer to verify it's not overwritten.
1030 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1031 EXPECT_EQ(73970u,
1032 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1033 buf.data(), buf.size()));
1034 std::string ap = BufferToString(buf);
1035 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001036
Lei Zhanga21d5932018-02-05 18:28:38 +00001037 // Check that the string value of an AP is returned through a buffer that is
1038 // the right size.
1039 buf.clear();
1040 buf.resize(normal_len);
1041 EXPECT_EQ(73970u,
1042 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1043 buf.data(), buf.size()));
1044 ap = BufferToString(buf);
1045 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1046 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001047
Lei Zhanga21d5932018-02-05 18:28:38 +00001048 // Check that the string value of an AP is returned through a buffer that is
1049 // larger than necessary.
1050 buf.clear();
1051 buf.resize(normal_len + 1);
1052 EXPECT_EQ(73970u,
1053 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1054 buf.data(), buf.size()));
1055 ap = BufferToString(buf);
1056 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1057 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001058
Lei Zhanga21d5932018-02-05 18:28:38 +00001059 // Check that getting an AP for a mode that does not have an AP returns an
1060 // empty string.
1061 unsigned long rollover_len = FPDFAnnot_GetAP(
1062 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1063 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001064
Lei Zhanga21d5932018-02-05 18:28:38 +00001065 buf.clear();
1066 buf.resize(1000);
1067 EXPECT_EQ(2u,
1068 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1069 buf.data(), buf.size()));
1070 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001071
Lei Zhanga21d5932018-02-05 18:28:38 +00001072 // Check that setting the AP for an invalid appearance mode fails.
1073 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1074 GetFPDFWideString(L"new test ap");
1075 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1076 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1077 apText.get()));
1078 EXPECT_FALSE(FPDFAnnot_SetAP(
1079 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001080
Lei Zhanga21d5932018-02-05 18:28:38 +00001081 // Set the AP correctly now.
1082 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1083 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001084
Lei Zhanga21d5932018-02-05 18:28:38 +00001085 // Check that the new annotation value is equal to the value we just set.
1086 rollover_len = FPDFAnnot_GetAP(
1087 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1088 EXPECT_EQ(24u, rollover_len);
1089 buf.clear();
1090 buf.resize(rollover_len);
1091 EXPECT_EQ(24u,
1092 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1093 buf.data(), buf.size()));
1094 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001095
Lei Zhanga21d5932018-02-05 18:28:38 +00001096 // Check that the Normal AP was not touched when the Rollover AP was set.
1097 buf.clear();
1098 buf.resize(normal_len);
1099 EXPECT_EQ(73970u,
1100 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1101 buf.data(), buf.size()));
1102 ap = BufferToString(buf);
1103 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1104 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1105 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001106
1107 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001108 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001109 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001110
1111 OpenSavedDocument();
1112 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001113 {
1114 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
1115 FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001116
Lei Zhanga21d5932018-02-05 18:28:38 +00001117 // Check that the new annotation value is equal to the value we set before
1118 // saving.
1119 unsigned long rollover_len = FPDFAnnot_GetAP(
1120 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1121 EXPECT_EQ(24u, rollover_len);
1122 std::vector<char> buf(rollover_len);
1123 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1124 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1125 buf.data(), buf.size()));
1126 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1127 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001128
1129 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001130 CloseSavedPage(page);
1131 CloseSavedDocument();
1132}
1133
1134TEST_F(FPDFAnnotEmbeddertest, RemoveOptionalAP) {
1135 // Open a file with four annotations and load its first page.
1136 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001137 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001138 ASSERT_TRUE(page);
1139
Lei Zhanga21d5932018-02-05 18:28:38 +00001140 {
1141 // Retrieve the first annotation.
1142 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1143 FPDFPage_GetAnnot(page, 0));
1144 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001145
Lei Zhanga21d5932018-02-05 18:28:38 +00001146 // Set Down AP. Normal AP is already set.
1147 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1148 GetFPDFWideString(L"new test ap");
1149 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1150 apText.get()));
1151 EXPECT_EQ(73970u,
1152 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1153 nullptr, 0));
1154 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1155 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001156
Lei Zhanga21d5932018-02-05 18:28:38 +00001157 // Check that setting the Down AP to null removes the Down entry but keeps
1158 // Normal intact.
1159 EXPECT_TRUE(
1160 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1161 EXPECT_EQ(73970u,
1162 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1163 nullptr, 0));
1164 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1165 nullptr, 0));
1166 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001167
Lei Zhang75c81712018-02-08 17:22:39 +00001168 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001169}
1170
1171TEST_F(FPDFAnnotEmbeddertest, RemoveRequiredAP) {
1172 // Open a file with four annotations and load its first page.
1173 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001174 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001175 ASSERT_TRUE(page);
1176
Lei Zhanga21d5932018-02-05 18:28:38 +00001177 {
1178 // Retrieve the first annotation.
1179 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1180 FPDFPage_GetAnnot(page, 0));
1181 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001182
Lei Zhanga21d5932018-02-05 18:28:38 +00001183 // Set Down AP. Normal AP is already set.
1184 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1185 GetFPDFWideString(L"new test ap");
1186 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1187 apText.get()));
1188 EXPECT_EQ(73970u,
1189 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1190 nullptr, 0));
1191 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1192 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001193
Lei Zhanga21d5932018-02-05 18:28:38 +00001194 // Check that setting the Normal AP to null removes the whole AP dictionary.
1195 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1196 nullptr));
1197 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1198 nullptr, 0));
1199 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1200 nullptr, 0));
1201 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001202
Lei Zhang75c81712018-02-08 17:22:39 +00001203 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001204}
1205
Jane Liu300bb272017-08-21 14:37:53 -04001206TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) {
1207 // Open a file with annotations and load its first page.
1208 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001209 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001210 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001211 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001212
Lei Zhanga21d5932018-02-05 18:28:38 +00001213 {
1214 // Retrieve the highlight annotation which has its popup defined.
1215 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1216 FPDFPage_GetAnnot(page, 0));
1217 ASSERT_TRUE(annot);
1218 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1219 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1220 static constexpr char kPopupKey[] = "Popup";
1221 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1222 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1223 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001224
Lei Zhanga21d5932018-02-05 18:28:38 +00001225 // Retrieve and verify the popup of the highlight annotation.
1226 std::unique_ptr<void, FPDFAnnotationDeleter> popup(
1227 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1228 ASSERT_TRUE(popup);
1229 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1230 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1231 FS_RECTF rect;
1232 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1233 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1234 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001235
Lei Zhanga21d5932018-02-05 18:28:38 +00001236 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1237 // since "IRT" is not a key in |annot|'s dictionary.
1238 static constexpr char kIRTKey[] = "IRT";
1239 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1240 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001241
Lei Zhanga21d5932018-02-05 18:28:38 +00001242 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1243 // would fail, since its parent is not an annotation.
1244 static constexpr char kPKey[] = "P";
1245 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPKey));
1246 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1247 FPDFAnnot_GetValueType(annot.get(), kPKey));
1248 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kPKey));
1249 }
Jane Liu300bb272017-08-21 14:37:53 -04001250
Jane Liu300bb272017-08-21 14:37:53 -04001251 UnloadPage(page);
1252}
1253
Diana Gage7e0c05d2017-07-19 17:33:33 -07001254TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
1255 // Open file with form text fields.
1256 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001257 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001258 ASSERT_TRUE(page);
1259
Lei Zhanga21d5932018-02-05 18:28:38 +00001260 {
1261 // Retrieve the first annotation: user-editable text field.
1262 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1263 FPDFPage_GetAnnot(page, 0));
1264 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001265
Lei Zhanga21d5932018-02-05 18:28:38 +00001266 // Check that the flag values are as expected.
1267 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1268 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1269 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001270
Lei Zhanga21d5932018-02-05 18:28:38 +00001271 {
1272 // Retrieve the second annotation: read-only text field.
1273 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1274 FPDFPage_GetAnnot(page, 1));
1275 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001276
Lei Zhanga21d5932018-02-05 18:28:38 +00001277 // Check that the flag values are as expected.
1278 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1279 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1280 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001281
1282 UnloadPage(page);
1283}
1284
1285TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
1286 // Open file with form text fields.
1287 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001288 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001289 ASSERT_TRUE(page);
1290
Lei Zhanga21d5932018-02-05 18:28:38 +00001291 {
1292 // Retrieve the first annotation: user-editable combobox.
1293 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1294 FPDFPage_GetAnnot(page, 0));
1295 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001296
Lei Zhanga21d5932018-02-05 18:28:38 +00001297 // Check that the flag values are as expected.
1298 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1299 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1300 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1301 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1302 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001303
Lei Zhanga21d5932018-02-05 18:28:38 +00001304 {
1305 // Retrieve the second annotation: regular combobox.
1306 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1307 FPDFPage_GetAnnot(page, 1));
1308 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001309
Lei Zhanga21d5932018-02-05 18:28:38 +00001310 // Check that the flag values are as expected.
1311 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1312 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1313 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1314 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1315 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001316
Lei Zhanga21d5932018-02-05 18:28:38 +00001317 {
1318 // Retrieve the third annotation: read-only combobox.
1319 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1320 FPDFPage_GetAnnot(page, 2));
1321 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001322
Lei Zhanga21d5932018-02-05 18:28:38 +00001323 // Check that the flag values are as expected.
1324 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1325 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1326 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1327 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1328 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001329
1330 UnloadPage(page);
1331}
Diana Gage40870db2017-07-19 18:16:03 -07001332
1333TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
1334 // Open file with form text fields.
1335 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1336 FPDF_PAGE page = LoadPage(0);
1337 ASSERT_TRUE(page);
1338
1339 // Attempt to get an annotation where no annotation exists on page.
1340 FPDF_ANNOTATION annot =
1341 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0);
1342 EXPECT_FALSE(annot);
1343
1344 UnloadPage(page);
1345}
1346
1347TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
1348 // Open file with form text fields.
1349 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1350 FPDF_PAGE page = LoadPage(0);
1351 ASSERT_TRUE(page);
1352
Lei Zhanga21d5932018-02-05 18:28:38 +00001353 {
1354 // Retrieve user-editable text field annotation.
1355 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1356 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1357 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001358
Lei Zhanga21d5932018-02-05 18:28:38 +00001359 // Check that interactive form annotation flag values are as expected.
1360 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1361 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1362 }
Diana Gage40870db2017-07-19 18:16:03 -07001363
Lei Zhanga21d5932018-02-05 18:28:38 +00001364 {
1365 // Retrieve read-only text field annotation.
1366 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1367 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1368 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001369
Lei Zhanga21d5932018-02-05 18:28:38 +00001370 // Check that interactive form annotation flag values are as expected.
1371 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1372 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1373 }
Diana Gage40870db2017-07-19 18:16:03 -07001374
1375 UnloadPage(page);
1376}
1377
1378TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1379 // Open file with form comboboxes.
1380 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1381 FPDF_PAGE page = LoadPage(0);
1382 ASSERT_TRUE(page);
1383
Lei Zhanga21d5932018-02-05 18:28:38 +00001384 {
1385 // Retrieve user-editable combobox annotation.
1386 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001387 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001388 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001389
Lei Zhanga21d5932018-02-05 18:28:38 +00001390 // Check that interactive form annotation flag values are as expected.
1391 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1392 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1393 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1394 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1395 }
Diana Gage40870db2017-07-19 18:16:03 -07001396
Lei Zhanga21d5932018-02-05 18:28:38 +00001397 {
1398 // Retrieve regular combobox annotation.
1399 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001400 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001401 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001402
Lei Zhanga21d5932018-02-05 18:28:38 +00001403 // Check that interactive form annotation flag values are as expected.
1404 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1405 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1406 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1407 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1408 }
Diana Gage40870db2017-07-19 18:16:03 -07001409
Lei Zhanga21d5932018-02-05 18:28:38 +00001410 {
1411 // Retrieve read-only combobox annotation.
1412 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001413 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001414 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001415
Lei Zhanga21d5932018-02-05 18:28:38 +00001416 // Check that interactive form annotation flag values are as expected.
1417 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1418 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1419 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1420 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1421 }
Diana Gage40870db2017-07-19 18:16:03 -07001422
1423 UnloadPage(page);
1424}