blob: 13dc11792919833604281b5f98c3da5965fc987e [file] [log] [blame]
Jane Liu4fd9a472017-06-01 18:56:09 -04001// Copyright 2017 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00005#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -04006#include <memory>
7#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04008#include <vector>
9
Jane Liubaa7ff42017-06-29 19:18:23 -040010#include "core/fxcrt/fx_system.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000011#include "public/cpp/fpdf_scopers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040012#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040013#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040014#include "public/fpdfview.h"
15#include "testing/embedder_test.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000016#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040017#include "testing/gtest/include/gtest/gtest.h"
18
Lei Zhangdf064df2017-08-31 02:33:27 -070019static constexpr char kContentsKey[] = "Contents";
20
Nicolas Pena3ff54002017-07-05 11:55:35 -040021class FPDFAnnotEmbeddertest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040022
Lei Zhang4afee172018-01-10 20:57:15 +000023std::wstring BufferToWString(const std::vector<char>& buf) {
24 return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000025}
26
Lei Zhang4afee172018-01-10 20:57:15 +000027std::string BufferToString(const std::vector<char>& buf) {
28 return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000029}
30
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.
Tom Sepeze08d2b12018-04-25 18:49:32 +000041 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +000042 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040043
44 UnloadPage(page);
45}
46
Ralf Sipplb3a52402018-03-19 23:30:28 +000047TEST_F(FPDFAnnotEmbeddertest, RenderMultilineMarkupAnnotWithoutAP) {
48 const char md5_hash[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000049 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000050 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
51 FPDF_PAGE page = LoadPage(0);
52 ASSERT_TRUE(page);
53
Tom Sepeze08d2b12018-04-25 18:49:32 +000054 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Ralf Sipplb3a52402018-03-19 23:30:28 +000055 CompareBitmap(bitmap.get(), 595, 842, md5_hash);
56
57 UnloadPage(page);
58}
59
Jane Liu4fd9a472017-06-01 18:56:09 -040060TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
61 // Open a file with one annotation and load its first page.
62 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000063 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
64 // the FORM API calls in LoadPage() makes this test fail. So use
65 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu4fd9a472017-06-01 18:56:09 -040066 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
67 ASSERT_TRUE(page);
68
69 // Check that there is a total of 1 annotation on its first page.
70 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
71
72 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +000073 {
Tom Sepeze08d2b12018-04-25 18:49:32 +000074 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +000075 ASSERT_TRUE(annot);
76 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -040077
Lei Zhanga21d5932018-02-05 18:28:38 +000078 // Check that the annotation color is yellow.
79 unsigned int R;
80 unsigned int G;
81 unsigned int B;
82 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +000083 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +000084 &G, &B, &A));
85 EXPECT_EQ(255u, R);
86 EXPECT_EQ(255u, G);
87 EXPECT_EQ(0u, B);
88 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -040089
Lei Zhanga21d5932018-02-05 18:28:38 +000090 // Check that the author is correct.
91 static constexpr char kAuthorKey[] = "T";
92 EXPECT_EQ(FPDF_OBJECT_STRING,
93 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
94 unsigned long len =
95 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
96 std::vector<char> buf(len);
97 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
98 len));
99 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400100
Lei Zhanga21d5932018-02-05 18:28:38 +0000101 // Check that the content is correct.
102 EXPECT_EQ(FPDF_OBJECT_STRING,
103 FPDFAnnot_GetValueType(annot.get(), kContentsKey));
104 len = FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
105 buf.clear();
106 buf.resize(len);
107 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
108 buf.data(), len));
109 const wchar_t contents[] =
110 L"This is a note for that highlight annotation. Very long highlight "
111 "annotation. Long long long Long 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 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 long. END";
130 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400131
Lei Zhanga21d5932018-02-05 18:28:38 +0000132 // Check that the quadpoints are correct.
133 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000134 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000135 EXPECT_EQ(115.802643f, quadpoints.x1);
136 EXPECT_EQ(718.913940f, quadpoints.y1);
137 EXPECT_EQ(157.211182f, quadpoints.x4);
138 EXPECT_EQ(706.264465f, quadpoints.y4);
139 }
Lei Zhang75c81712018-02-08 17:22:39 +0000140 FPDF_ClosePage(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400141}
142
143TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
144 // Open a file with three annotations and load its first page.
145 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000146 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
147 // the FORM API calls in LoadPage() makes this test fail. So use
148 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu4fd9a472017-06-01 18:56:09 -0400149 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
150 ASSERT_TRUE(page);
151
152 // Check that there is a total of 3 annotation on its first page.
153 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
154
Lei Zhanga21d5932018-02-05 18:28:38 +0000155 {
156 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000157 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000158 ASSERT_TRUE(annot);
159 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400160
Lei Zhanga21d5932018-02-05 18:28:38 +0000161 // Check that the annotation color is blue with opacity.
162 unsigned int R;
163 unsigned int G;
164 unsigned int B;
165 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000166 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000167 &G, &B, &A));
168 EXPECT_EQ(0u, R);
169 EXPECT_EQ(0u, G);
170 EXPECT_EQ(255u, B);
171 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400172
Lei Zhanga21d5932018-02-05 18:28:38 +0000173 // Check that there is no content.
174 EXPECT_EQ(2u,
175 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400176
Lei Zhanga21d5932018-02-05 18:28:38 +0000177 // Check that the rectange coordinates are correct.
178 // Note that upon rendering, the rectangle coordinates will be adjusted.
179 FS_RECTF rect;
180 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
181 EXPECT_EQ(351.820404f, rect.left);
182 EXPECT_EQ(583.830688f, rect.bottom);
183 EXPECT_EQ(475.336090f, rect.right);
184 EXPECT_EQ(681.535034f, rect.top);
185 }
Lei Zhang75c81712018-02-08 17:22:39 +0000186 FPDF_ClosePage(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400187}
Jane Liu20eafda2017-06-07 10:33:24 -0400188
189TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
190 // Open a file with one annotation and load its first page.
191 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000192 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400193 ASSERT_TRUE(page);
194
195 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400196 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400197
198 UnloadPage(page);
199}
200
Jane Liud321ef92017-06-14 09:56:22 -0400201TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400202 // Open a file with no annotation and load its first page.
203 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000204 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400205 ASSERT_TRUE(page);
206 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
207
Lei Zhanga21d5932018-02-05 18:28:38 +0000208 {
209 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000210 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000211 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400212
Lei Zhanga21d5932018-02-05 18:28:38 +0000213 // Check that there is now 1 annotations on this page.
214 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400215
Lei Zhanga21d5932018-02-05 18:28:38 +0000216 // Check that the subtype of the annotation is correct.
217 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
218 }
Jane Liue10509a2017-06-20 16:47:41 -0400219
Lei Zhanga21d5932018-02-05 18:28:38 +0000220 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000221 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000222 ASSERT_TRUE(annot);
223 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400224
Lei Zhanga21d5932018-02-05 18:28:38 +0000225 // Set the color of the annotation.
226 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
227 102, 153, 204));
228 // Check that the color has been set correctly.
229 unsigned int R;
230 unsigned int G;
231 unsigned int B;
232 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000233 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000234 &G, &B, &A));
235 EXPECT_EQ(51u, R);
236 EXPECT_EQ(102u, G);
237 EXPECT_EQ(153u, B);
238 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400239
Lei Zhanga21d5932018-02-05 18:28:38 +0000240 // Change the color of the annotation.
241 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
242 153, 102, 51));
243 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000244 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000245 &G, &B, &A));
246 EXPECT_EQ(204u, R);
247 EXPECT_EQ(153u, G);
248 EXPECT_EQ(102u, B);
249 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400250
Lei Zhanga21d5932018-02-05 18:28:38 +0000251 // Set the annotation rectangle.
252 FS_RECTF rect;
253 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
254 EXPECT_EQ(0.f, rect.left);
255 EXPECT_EQ(0.f, rect.right);
256 rect.left = 35;
257 rect.bottom = 150;
258 rect.right = 53;
259 rect.top = 165;
260 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
261 // Check that the annotation rectangle has been set correctly.
262 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
263 EXPECT_EQ(35.f, rect.left);
264 EXPECT_EQ(150.f, rect.bottom);
265 EXPECT_EQ(53.f, rect.right);
266 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400267
Lei Zhanga21d5932018-02-05 18:28:38 +0000268 // Set the content of the annotation.
269 static constexpr wchar_t kContents[] =
270 L"Hello! This is a customized content.";
271 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
272 GetFPDFWideString(kContents);
273 ASSERT_TRUE(
274 FPDFAnnot_SetStringValue(annot.get(), kContentsKey, text.get()));
275 // Check that the content has been set correctly.
276 unsigned long len =
277 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
278 std::vector<char> buf(len);
279 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
280 buf.data(), len));
281 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
282 }
Jane Liu20eafda2017-06-07 10:33:24 -0400283 UnloadPage(page);
284}
285
286TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
287 // Open a file with one annotation and load its first page.
288 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000289 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400290 ASSERT_TRUE(page);
291
292 // Check that there is a total of one annotation on its first page, and verify
293 // its quadpoints.
294 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400295 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000296 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000297 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000298 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000299 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000300 EXPECT_EQ(115.802643f, quadpoints.x1);
301 EXPECT_EQ(718.913940f, quadpoints.y1);
302 EXPECT_EQ(157.211182f, quadpoints.x4);
303 EXPECT_EQ(706.264465f, quadpoints.y4);
304 }
Jane Liu20eafda2017-06-07 10:33:24 -0400305
306 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000307 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000308 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000309 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
310 ASSERT_TRUE(annot);
311 quadpoints.x1 = 140.802643f;
312 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000313 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000314 }
Jane Liu20eafda2017-06-07 10:33:24 -0400315
316 // Save the document, closing the page and document.
317 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000318 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400319
320 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000321 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400322
323 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000324 page = LoadSavedPage(0);
325 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400326
327 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000328 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400329
Lei Zhanga21d5932018-02-05 18:28:38 +0000330 {
331 // Check that the second annotation is an underline annotation and verify
332 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000333 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000334 ASSERT_TRUE(new_annot);
335 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
336 FS_QUADPOINTSF new_quadpoints;
337 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000338 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000339 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
340 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
341 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
342 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
343 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400344
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000345 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400346 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400347}
Jane Liu06462752017-06-27 16:41:14 -0400348
Ralf Sippl16381792018-04-12 21:20:26 +0000349TEST_F(FPDFAnnotEmbeddertest, GetAndSetQuadPoints) {
350 // Open a file with four annotations and load its first page.
351 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
352 FPDF_PAGE page = LoadPage(0);
353 ASSERT_TRUE(page);
354 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
355
356 // Retrieve the highlight annotation.
357 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
358 ASSERT_TRUE(annot);
359 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
360
361 FS_QUADPOINTSF quadpoints;
362 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
363
364 {
365 // Verify the current one set of quadpoints.
366 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
367
368 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
369 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
370 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
371 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
372 }
373
374 {
375 // Update the quadpoints.
376 FS_QUADPOINTSF new_quadpoints = quadpoints;
377 new_quadpoints.y1 -= 20.f;
378 new_quadpoints.y2 -= 20.f;
379 new_quadpoints.y3 -= 20.f;
380 new_quadpoints.y4 -= 20.f;
381 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
382
383 // Verify added quadpoint set
384 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
385 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
386 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
387 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
388 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
389 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
390 }
391
392 {
393 // Append a new set of quadpoints.
394 FS_QUADPOINTSF new_quadpoints = quadpoints;
395 new_quadpoints.y1 += 20.f;
396 new_quadpoints.y2 += 20.f;
397 new_quadpoints.y3 += 20.f;
398 new_quadpoints.y4 += 20.f;
399 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
400
401 // Verify added quadpoint set
402 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
403 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
404 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
405 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
406 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
407 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
408 }
409
410 {
411 // Setting and getting quadpoints at out-of-bound index should fail
412 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
413 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
414 }
415
416 FPDFPage_CloseAnnot(annot);
417
418 // Retrieve the square annotation
419 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
420
421 {
422 // Check that attempting to set its quadpoints would fail
423 ASSERT_TRUE(squareAnnot);
424 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
425 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
426 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
427 }
428
429 FPDFPage_CloseAnnot(squareAnnot);
430
431 UnloadPage(page);
432}
433
Jane Liu06462752017-06-27 16:41:14 -0400434TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400435#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400436 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
437 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
438 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400439#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000440 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
441 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
442 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400443#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000444 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
445 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
446 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400447#endif
448
Jane Liu06462752017-06-27 16:41:14 -0400449 // Open a file with four annotations and load its first page.
450 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000451 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400452 ASSERT_TRUE(page);
453 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
454
Jane Liub370e5a2017-08-16 13:24:58 -0400455 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000456 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000457 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000458 CompareBitmap(bitmap.get(), 612, 792, md5_original);
459 }
Jane Liub370e5a2017-08-16 13:24:58 -0400460
Jane Liu0c6b07d2017-08-15 10:50:22 -0400461 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400462 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000463
464 // Retrieve the highlight annotation which has its AP stream already defined.
465 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000466 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000467 ASSERT_TRUE(annot);
468 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
469
470 // Check that color cannot be set when an AP stream is defined already.
471 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
472 102, 153, 204));
473
474 // Verify its attachment points.
475 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000476 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000477 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
478 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
479 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
480 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
481
482 // Check that updating the attachment points would succeed.
483 quadpoints.x1 -= 50.f;
484 quadpoints.x2 -= 50.f;
485 quadpoints.x3 -= 50.f;
486 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000487 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000488 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000489 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000490 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
491 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
492 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
493 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
494
495 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000496 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000497 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000498 CompareBitmap(bitmap.get(), 612, 792, md5_original);
499 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000500
501 // Verify its annotation rectangle.
502 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
503 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
504 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
505 EXPECT_NEAR(136.325f, rect.right, 0.001f);
506 EXPECT_NEAR(721.292f, rect.top, 0.001f);
507
508 // Check that updating the rectangle would succeed.
509 rect.left -= 60.f;
510 rect.right -= 60.f;
511 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
512 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
513 EXPECT_EQ(rect.right, new_rect.right);
514 }
Jane Liu06462752017-06-27 16:41:14 -0400515
Jane Liub370e5a2017-08-16 13:24:58 -0400516 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000517 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000518 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000519 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
520 }
Jane Liub370e5a2017-08-16 13:24:58 -0400521
Lei Zhanga21d5932018-02-05 18:28:38 +0000522 {
523 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000524 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000525 ASSERT_TRUE(annot);
526 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400527
Lei Zhanga21d5932018-02-05 18:28:38 +0000528 // Check that updating the rectangle would succeed.
529 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
530 rect.left += 70.f;
531 rect.right += 70.f;
532 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
533 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
534 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400535
Lei Zhanga21d5932018-02-05 18:28:38 +0000536 // Check that updating the rectangle changes the square annotation's
537 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000538 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000539 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000540 }
Jane Liub370e5a2017-08-16 13:24:58 -0400541
Jane Liu06462752017-06-27 16:41:14 -0400542 UnloadPage(page);
543}
Jane Liu8ce58f52017-06-29 13:40:22 -0400544
Henrique Nakashima5098b252018-03-26 21:46:00 +0000545TEST_F(FPDFAnnotEmbeddertest, CountAttachmentPoints) {
546 // Open a file with multiline markup annotations.
547 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
548 FPDF_PAGE page = LoadPage(0);
549 ASSERT_TRUE(page);
550 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000551 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000552 ASSERT_TRUE(annot);
553
554 // This is a three line annotation.
555 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
556 }
557 UnloadPage(page);
558
559 // null annotation should return 0
560 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
561}
562
Jane Liu8ce58f52017-06-29 13:40:22 -0400563TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
564 // Open a file with 3 annotations on its first page.
565 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000566 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
567 // the FORM API calls in LoadPage() makes this test fail. So use
568 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu8ce58f52017-06-29 13:40:22 -0400569 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
570 ASSERT_TRUE(page);
571 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
572
Jane Liu0c6b07d2017-08-15 10:50:22 -0400573 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400574
Lei Zhanga21d5932018-02-05 18:28:38 +0000575 // Check that the annotations have the expected rectangle coordinates.
576 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000577 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000578 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
579 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
580 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400581
Lei Zhanga21d5932018-02-05 18:28:38 +0000582 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000583 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000584 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
585 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
586 }
587
588 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000589 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000590 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
591 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
592 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400593
594 // Check that nothing happens when attempting to remove an annotation with an
595 // out-of-bound index.
596 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
597 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
598 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
599
600 // Remove the second annotation.
601 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
602 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
603 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
604
605 // Save the document, closing the page and document.
606 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
607 FPDF_ClosePage(page);
608
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400609 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400610 // Open the saved document.
611 std::string new_file = GetString();
612 FPDF_FILEACCESS file_access;
613 memset(&file_access, 0, sizeof(file_access));
614 file_access.m_FileLen = new_file.size();
615 file_access.m_GetBlock = GetBlockFromString;
616 file_access.m_Param = &new_file;
617 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
618 ASSERT_TRUE(new_doc);
619 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
620 ASSERT_TRUE(new_page);
621
622 // Check that the saved document has 2 annotations on the first page.
623 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
624
Lei Zhanga21d5932018-02-05 18:28:38 +0000625 // Check that the remaining 2 annotations are the original 1st and 3rd ones
626 // by verifying their rectangle coordinates.
627 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000628 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000629 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
630 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
631 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400632
Lei Zhanga21d5932018-02-05 18:28:38 +0000633 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000634 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000635 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
636 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
637 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400638 FPDF_ClosePage(new_page);
639 FPDF_CloseDocument(new_doc);
640}
Jane Liu8ce58f52017-06-29 13:40:22 -0400641
Jane Liubaa7ff42017-06-29 19:18:23 -0400642TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400643#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400644 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000645 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
646 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
647 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Jane Liubaa7ff42017-06-29 19:18:23 -0400648#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000649 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000650 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
651 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
652 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400653#endif
654
655 // Open a file with two annotations and load its first page.
656 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000657 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400658 ASSERT_TRUE(page);
659 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
660
661 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000662 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000663 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000664 CompareBitmap(bitmap.get(), 595, 842, md5_original);
665 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400666
Lei Zhanga21d5932018-02-05 18:28:38 +0000667 {
668 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000669 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000670 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400671
Lei Zhanga21d5932018-02-05 18:28:38 +0000672 // Check that this annotation has one path object and retrieve it.
673 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000674 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000675 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
676 EXPECT_FALSE(path);
677 path = FPDFAnnot_GetObject(annot.get(), 0);
678 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
679 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400680
Lei Zhanga21d5932018-02-05 18:28:38 +0000681 // Modify the color of the path object.
682 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
683 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400684
Lei Zhanga21d5932018-02-05 18:28:38 +0000685 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000686 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000687 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000688 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
689 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400690
Lei Zhanga21d5932018-02-05 18:28:38 +0000691 // Add a second path object to the same annotation.
692 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
693 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
694 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
695 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
696 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
697 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
698 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400699
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000700 // The object is in the annontation, not in the page, so the page object
701 // array should not change.
702 ASSERT_EQ(32, FPDFPage_CountObjects(page));
703
Lei Zhanga21d5932018-02-05 18:28:38 +0000704 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000705 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000706 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000707 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
708 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400709
Lei Zhanga21d5932018-02-05 18:28:38 +0000710 // Delete the newly added path object.
711 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
712 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000713 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000714 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400715
716 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000717 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000718 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000719 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
720 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400721
Jane Liubaa7ff42017-06-29 19:18:23 -0400722 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400723
Lei Zhanga21d5932018-02-05 18:28:38 +0000724 {
725 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000726 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000727 ASSERT_TRUE(annot);
728 rect.left = 200.f;
729 rect.bottom = 400.f;
730 rect.right = 500.f;
731 rect.top = 600.f;
732 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400733
Lei Zhanga21d5932018-02-05 18:28:38 +0000734 // Add a new path to the annotation.
735 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
736 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
737 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
738 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
739 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
740 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
741 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
742 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
743 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
744 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
745
746 // Check that the annotation's bounding box came from its rectangle.
747 FS_RECTF new_rect;
748 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
749 EXPECT_EQ(rect.left, new_rect.left);
750 EXPECT_EQ(rect.bottom, new_rect.bottom);
751 EXPECT_EQ(rect.right, new_rect.right);
752 EXPECT_EQ(rect.top, new_rect.top);
753 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400754
755 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400756 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000757 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400758
759 // Open the saved document.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400760 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000761 page = LoadSavedPage(0);
762 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400763
Jane Liu36567742017-07-06 11:13:35 -0400764 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000765 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400766
Lei Zhanga21d5932018-02-05 18:28:38 +0000767 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000768 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000769 ASSERT_TRUE(annot);
770 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400771
Lei Zhanga21d5932018-02-05 18:28:38 +0000772 // Check that the new annotation's rectangle is as defined.
773 FS_RECTF new_rect;
774 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
775 EXPECT_EQ(rect.left, new_rect.left);
776 EXPECT_EQ(rect.bottom, new_rect.bottom);
777 EXPECT_EQ(rect.right, new_rect.right);
778 EXPECT_EQ(rect.top, new_rect.top);
779 }
780
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000781 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400782 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400783}
Jane Liub137e752017-07-05 15:04:33 -0400784
785TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
786 // Open a file with an annotation and load its first page.
787 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000788 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400789 ASSERT_TRUE(page);
790
791 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000792 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000793 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000794 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
795 }
Jane Liub137e752017-07-05 15:04:33 -0400796
Lei Zhanga21d5932018-02-05 18:28:38 +0000797 {
798 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000799 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000800 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400801
Lei Zhanga21d5932018-02-05 18:28:38 +0000802 // Check that the original flag values are as expected.
803 int flags = FPDFAnnot_GetFlags(annot.get());
804 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
805 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400806
Lei Zhanga21d5932018-02-05 18:28:38 +0000807 // Set the HIDDEN flag.
808 flags |= FPDF_ANNOT_FLAG_HIDDEN;
809 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
810 flags = FPDFAnnot_GetFlags(annot.get());
811 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
812 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400813
Lei Zhanga21d5932018-02-05 18:28:38 +0000814 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000815 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000816 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000817 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
818 }
Jane Liub137e752017-07-05 15:04:33 -0400819
Lei Zhanga21d5932018-02-05 18:28:38 +0000820 // Unset the HIDDEN flag.
821 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
822 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
823 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
824 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
825 flags = FPDFAnnot_GetFlags(annot.get());
826 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
827 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400828
Lei Zhanga21d5932018-02-05 18:28:38 +0000829 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000830 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000831 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000832 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
833 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000834 }
Jane Liub137e752017-07-05 15:04:33 -0400835
Jane Liub137e752017-07-05 15:04:33 -0400836 UnloadPage(page);
837}
Jane Liu36567742017-07-06 11:13:35 -0400838
839TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400840#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400841 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
842 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
843 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Jane Liu36567742017-07-06 11:13:35 -0400844#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000845 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
846 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
847 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400848#endif
849
850 // Open a file with two annotations and load its first page.
851 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000852 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400853 ASSERT_TRUE(page);
854 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
855
856 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000857 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000858 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000859 CompareBitmap(bitmap.get(), 595, 842, md5_original);
860 }
Jane Liu36567742017-07-06 11:13:35 -0400861
Jane Liu36567742017-07-06 11:13:35 -0400862 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000863 FPDF_BITMAP image_bitmap;
864
865 {
866 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000867 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000868 ASSERT_TRUE(annot);
869 FS_RECTF rect;
870 rect.left = 200.f;
871 rect.bottom = 600.f;
872 rect.right = 400.f;
873 rect.top = 800.f;
874 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
875
876 // Add a solid-color translucent image object to the new annotation.
877 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
878 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
879 0xeeeecccc);
880 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
881 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
882 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
883 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
884 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
885 kBitmapSize, 0, 0));
886 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
887 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
888 }
Jane Liu36567742017-07-06 11:13:35 -0400889
890 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000891 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000892 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000893 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
894 }
Jane Liu36567742017-07-06 11:13:35 -0400895
Lei Zhanga21d5932018-02-05 18:28:38 +0000896 {
897 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000898 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000899 ASSERT_TRUE(annot);
900 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
901 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
902 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400903
Lei Zhanga21d5932018-02-05 18:28:38 +0000904 // Modify the image in the new annotation.
905 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
906 0xff000000);
907 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
908 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
909 }
Jane Liu36567742017-07-06 11:13:35 -0400910
911 // Save the document, closing the page and document.
912 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000913 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400914 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400915
916 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400917 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400918}
919
920TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400921#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400922 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
923 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
924 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Jane Liu36567742017-07-06 11:13:35 -0400925#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000926 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
927 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
928 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400929#endif
930
931 // Open a file with two annotations and load its first page.
932 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000933 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400934 ASSERT_TRUE(page);
935 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
936
937 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000938 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000939 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000940 CompareBitmap(bitmap.get(), 595, 842, md5_original);
941 }
Jane Liu36567742017-07-06 11:13:35 -0400942
Lei Zhanga21d5932018-02-05 18:28:38 +0000943 {
944 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000945 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000946 ASSERT_TRUE(annot);
947 FS_RECTF rect;
948 rect.left = 200.f;
949 rect.bottom = 550.f;
950 rect.right = 450.f;
951 rect.top = 650.f;
952 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -0400953
Lei Zhanga21d5932018-02-05 18:28:38 +0000954 // Add a translucent text object to the new annotation.
955 FPDF_PAGEOBJECT text_object =
956 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
957 EXPECT_TRUE(text_object);
958 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
959 GetFPDFWideString(L"I'm a translucent text laying on other text.");
960 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
961 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
962 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
963 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
964 }
Jane Liu36567742017-07-06 11:13:35 -0400965
966 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000967 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000968 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000969 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
970 }
Jane Liu36567742017-07-06 11:13:35 -0400971
Lei Zhanga21d5932018-02-05 18:28:38 +0000972 {
973 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000974 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000975 ASSERT_TRUE(annot);
976 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
977 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
978 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -0400979
Lei Zhanga21d5932018-02-05 18:28:38 +0000980 // Modify the text in the new annotation.
981 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
982 GetFPDFWideString(L"New text!");
983 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
984 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
985 }
Jane Liu36567742017-07-06 11:13:35 -0400986
987 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000988 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000989 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000990 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
991 }
Jane Liu36567742017-07-06 11:13:35 -0400992
993 // Remove the new annotation, and check that the page renders as before.
994 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +0000995 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000996 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000997 CompareBitmap(bitmap.get(), 595, 842, md5_original);
998 }
Jane Liu36567742017-07-06 11:13:35 -0400999
1000 UnloadPage(page);
1001}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001002
1003TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
1004 // Open a file with four annotations and load its first page.
1005 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001006 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001007 ASSERT_TRUE(page);
1008
Lei Zhangdf064df2017-08-31 02:33:27 -07001009 static constexpr char kDateKey[] = "M";
Lei Zhanga21d5932018-02-05 18:28:38 +00001010 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001011
Lei Zhanga21d5932018-02-05 18:28:38 +00001012 {
1013 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001014 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001015 ASSERT_TRUE(annot);
1016
1017 // Check that a non-existent key does not exist.
1018 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1019
1020 // Check that the string value of a non-string dictionary entry is empty.
1021 static constexpr char kApKey[] = "AP";
1022 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), kApKey));
1023 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1024 FPDFAnnot_GetValueType(annot.get(), kApKey));
1025 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kApKey, nullptr, 0));
1026
1027 // Check that the string value of the hash is correct.
1028 static constexpr char kHashKey[] = "AAPL:Hash";
1029 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1030 unsigned long len =
1031 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1032 std::vector<char> buf(len);
1033 EXPECT_EQ(66u,
1034 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1035 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1036 BufferToWString(buf).c_str());
1037
1038 // Check that the string value of the modified date is correct.
1039 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1040 len = FPDFAnnot_GetStringValue(annot.get(), kDateKey, nullptr, 0);
1041 buf.clear();
1042 buf.resize(len);
1043 EXPECT_EQ(44u,
1044 FPDFAnnot_GetStringValue(annot.get(), kDateKey, buf.data(), len));
1045 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1046
1047 // Update the date entry for the annotation.
1048 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1049 GetFPDFWideString(kNewDate);
1050 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kDateKey, text.get()));
1051 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001052
1053 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001054 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001055 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001056
Dan Sinclair698aed72017-09-26 16:24:49 -04001057#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +03001058 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001059#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001060 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001061#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001062
1063 // Open the saved annotation.
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001064 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001065 page = LoadSavedPage(0);
1066 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001067 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001068 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001069
Lei Zhanga21d5932018-02-05 18:28:38 +00001070 // Check that the string value of the modified date is the newly-set value.
1071 EXPECT_EQ(FPDF_OBJECT_STRING,
1072 FPDFAnnot_GetValueType(new_annot.get(), kDateKey));
1073 unsigned long len =
1074 FPDFAnnot_GetStringValue(new_annot.get(), kDateKey, nullptr, 0);
1075 std::vector<char> buf(len);
1076 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot.get(), kDateKey,
1077 buf.data(), len));
1078 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1079 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001080
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001081 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001082 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001083}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001084
Henrique Nakashima5970a472018-01-11 22:40:59 +00001085TEST_F(FPDFAnnotEmbeddertest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001086 // Open a file with four annotations and load its first page.
1087 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001088 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001089 ASSERT_TRUE(page);
1090
Lei Zhanga21d5932018-02-05 18:28:38 +00001091 {
1092 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001093 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001094 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001095
Lei Zhanga21d5932018-02-05 18:28:38 +00001096 // Check that the string value of an AP returns the expected length.
1097 unsigned long normal_len = FPDFAnnot_GetAP(
1098 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1099 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001100
Lei Zhanga21d5932018-02-05 18:28:38 +00001101 // Check that the string value of an AP is not returned if the buffer is too
1102 // small. The result buffer should be overwritten with an empty string.
1103 std::vector<char> buf(normal_len - 1);
1104 // Write L"z" in the buffer to verify it's not overwritten.
1105 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1106 EXPECT_EQ(73970u,
1107 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1108 buf.data(), buf.size()));
1109 std::string ap = BufferToString(buf);
1110 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001111
Lei Zhanga21d5932018-02-05 18:28:38 +00001112 // Check that the string value of an AP is returned through a buffer that is
1113 // the right size.
1114 buf.clear();
1115 buf.resize(normal_len);
1116 EXPECT_EQ(73970u,
1117 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1118 buf.data(), buf.size()));
1119 ap = BufferToString(buf);
1120 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1121 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001122
Lei Zhanga21d5932018-02-05 18:28:38 +00001123 // Check that the string value of an AP is returned through a buffer that is
1124 // larger than necessary.
1125 buf.clear();
1126 buf.resize(normal_len + 1);
1127 EXPECT_EQ(73970u,
1128 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1129 buf.data(), buf.size()));
1130 ap = BufferToString(buf);
1131 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1132 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001133
Lei Zhanga21d5932018-02-05 18:28:38 +00001134 // Check that getting an AP for a mode that does not have an AP returns an
1135 // empty string.
1136 unsigned long rollover_len = FPDFAnnot_GetAP(
1137 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1138 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001139
Lei Zhanga21d5932018-02-05 18:28:38 +00001140 buf.clear();
1141 buf.resize(1000);
1142 EXPECT_EQ(2u,
1143 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1144 buf.data(), buf.size()));
1145 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001146
Lei Zhanga21d5932018-02-05 18:28:38 +00001147 // Check that setting the AP for an invalid appearance mode fails.
1148 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1149 GetFPDFWideString(L"new test ap");
1150 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1151 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1152 apText.get()));
1153 EXPECT_FALSE(FPDFAnnot_SetAP(
1154 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001155
Lei Zhanga21d5932018-02-05 18:28:38 +00001156 // Set the AP correctly now.
1157 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1158 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001159
Lei Zhanga21d5932018-02-05 18:28:38 +00001160 // Check that the new annotation value is equal to the value we just set.
1161 rollover_len = FPDFAnnot_GetAP(
1162 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1163 EXPECT_EQ(24u, rollover_len);
1164 buf.clear();
1165 buf.resize(rollover_len);
1166 EXPECT_EQ(24u,
1167 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1168 buf.data(), buf.size()));
1169 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001170
Lei Zhanga21d5932018-02-05 18:28:38 +00001171 // Check that the Normal AP was not touched when the Rollover AP was set.
1172 buf.clear();
1173 buf.resize(normal_len);
1174 EXPECT_EQ(73970u,
1175 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1176 buf.data(), buf.size()));
1177 ap = BufferToString(buf);
1178 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1179 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1180 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001181
1182 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001183 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001184 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001185
1186 OpenSavedDocument();
1187 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001188 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001189 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001190
Lei Zhanga21d5932018-02-05 18:28:38 +00001191 // Check that the new annotation value is equal to the value we set before
1192 // saving.
1193 unsigned long rollover_len = FPDFAnnot_GetAP(
1194 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1195 EXPECT_EQ(24u, rollover_len);
1196 std::vector<char> buf(rollover_len);
1197 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1198 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1199 buf.data(), buf.size()));
1200 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1201 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001202
1203 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001204 CloseSavedPage(page);
1205 CloseSavedDocument();
1206}
1207
1208TEST_F(FPDFAnnotEmbeddertest, RemoveOptionalAP) {
1209 // Open a file with four annotations and load its first page.
1210 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001211 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001212 ASSERT_TRUE(page);
1213
Lei Zhanga21d5932018-02-05 18:28:38 +00001214 {
1215 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001216 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001217 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001218
Lei Zhanga21d5932018-02-05 18:28:38 +00001219 // Set Down AP. Normal AP is already set.
1220 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1221 GetFPDFWideString(L"new test ap");
1222 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1223 apText.get()));
1224 EXPECT_EQ(73970u,
1225 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1226 nullptr, 0));
1227 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1228 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001229
Lei Zhanga21d5932018-02-05 18:28:38 +00001230 // Check that setting the Down AP to null removes the Down entry but keeps
1231 // Normal intact.
1232 EXPECT_TRUE(
1233 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1234 EXPECT_EQ(73970u,
1235 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1236 nullptr, 0));
1237 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1238 nullptr, 0));
1239 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001240
Lei Zhang75c81712018-02-08 17:22:39 +00001241 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001242}
1243
1244TEST_F(FPDFAnnotEmbeddertest, RemoveRequiredAP) {
1245 // Open a file with four annotations and load its first page.
1246 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001247 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001248 ASSERT_TRUE(page);
1249
Lei Zhanga21d5932018-02-05 18:28:38 +00001250 {
1251 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001252 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001253 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001254
Lei Zhanga21d5932018-02-05 18:28:38 +00001255 // Set Down AP. Normal AP is already set.
1256 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1257 GetFPDFWideString(L"new test ap");
1258 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1259 apText.get()));
1260 EXPECT_EQ(73970u,
1261 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1262 nullptr, 0));
1263 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1264 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001265
Lei Zhanga21d5932018-02-05 18:28:38 +00001266 // Check that setting the Normal AP to null removes the whole AP dictionary.
1267 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1268 nullptr));
1269 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1270 nullptr, 0));
1271 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1272 nullptr, 0));
1273 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001274
Lei Zhang75c81712018-02-08 17:22:39 +00001275 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001276}
1277
Jane Liu300bb272017-08-21 14:37:53 -04001278TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) {
1279 // Open a file with annotations and load its first page.
1280 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001281 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001282 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001283 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001284
Lei Zhanga21d5932018-02-05 18:28:38 +00001285 {
1286 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001287 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001288 ASSERT_TRUE(annot);
1289 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1290 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1291 static constexpr char kPopupKey[] = "Popup";
1292 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1293 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1294 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001295
Lei Zhanga21d5932018-02-05 18:28:38 +00001296 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001297 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001298 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1299 ASSERT_TRUE(popup);
1300 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1301 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1302 FS_RECTF rect;
1303 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1304 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1305 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001306
Lei Zhanga21d5932018-02-05 18:28:38 +00001307 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1308 // since "IRT" is not a key in |annot|'s dictionary.
1309 static constexpr char kIRTKey[] = "IRT";
1310 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1311 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001312
Lei Zhanga21d5932018-02-05 18:28:38 +00001313 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1314 // would fail, since its parent is not an annotation.
1315 static constexpr char kPKey[] = "P";
1316 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPKey));
1317 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1318 FPDFAnnot_GetValueType(annot.get(), kPKey));
1319 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kPKey));
1320 }
Jane Liu300bb272017-08-21 14:37:53 -04001321
Jane Liu300bb272017-08-21 14:37:53 -04001322 UnloadPage(page);
1323}
1324
Diana Gage7e0c05d2017-07-19 17:33:33 -07001325TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
1326 // Open file with form text fields.
1327 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001328 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001329 ASSERT_TRUE(page);
1330
Lei Zhanga21d5932018-02-05 18:28:38 +00001331 {
1332 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001333 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001334 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001335
Lei Zhanga21d5932018-02-05 18:28:38 +00001336 // Check that the flag values are as expected.
1337 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1338 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1339 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001340
Lei Zhanga21d5932018-02-05 18:28:38 +00001341 {
1342 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001343 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001344 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001345
Lei Zhanga21d5932018-02-05 18:28:38 +00001346 // Check that the flag values are as expected.
1347 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1348 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1349 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001350
1351 UnloadPage(page);
1352}
1353
1354TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
1355 // Open file with form text fields.
1356 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001357 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001358 ASSERT_TRUE(page);
1359
Lei Zhanga21d5932018-02-05 18:28:38 +00001360 {
1361 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001362 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001363 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001364
Lei Zhanga21d5932018-02-05 18:28:38 +00001365 // Check that the flag values are as expected.
1366 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1367 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1368 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1369 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1370 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001371
Lei Zhanga21d5932018-02-05 18:28:38 +00001372 {
1373 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001374 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001375 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001376
Lei Zhanga21d5932018-02-05 18:28:38 +00001377 // Check that the flag values are as expected.
1378 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1379 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1380 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1381 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1382 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001383
Lei Zhanga21d5932018-02-05 18:28:38 +00001384 {
1385 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001386 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001387 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001388
Lei Zhanga21d5932018-02-05 18:28:38 +00001389 // Check that the flag values are as expected.
1390 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1391 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1392 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1393 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1394 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001395
1396 UnloadPage(page);
1397}
Diana Gage40870db2017-07-19 18:16:03 -07001398
1399TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
1400 // Open file with form text fields.
1401 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1402 FPDF_PAGE page = LoadPage(0);
1403 ASSERT_TRUE(page);
1404
1405 // Attempt to get an annotation where no annotation exists on page.
1406 FPDF_ANNOTATION annot =
1407 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0);
1408 EXPECT_FALSE(annot);
1409
1410 UnloadPage(page);
1411}
1412
1413TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
1414 // Open file with form text fields.
1415 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1416 FPDF_PAGE page = LoadPage(0);
1417 ASSERT_TRUE(page);
1418
Lei Zhanga21d5932018-02-05 18:28:38 +00001419 {
1420 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001421 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001422 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1423 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001424
Lei Zhanga21d5932018-02-05 18:28:38 +00001425 // Check that interactive form annotation flag values are as expected.
1426 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1427 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1428 }
Diana Gage40870db2017-07-19 18:16:03 -07001429
Lei Zhanga21d5932018-02-05 18:28:38 +00001430 {
1431 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001432 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001433 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1434 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001435
Lei Zhanga21d5932018-02-05 18:28:38 +00001436 // Check that interactive form annotation flag values are as expected.
1437 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1438 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1439 }
Diana Gage40870db2017-07-19 18:16:03 -07001440
1441 UnloadPage(page);
1442}
1443
1444TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1445 // Open file with form comboboxes.
1446 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1447 FPDF_PAGE page = LoadPage(0);
1448 ASSERT_TRUE(page);
1449
Lei Zhanga21d5932018-02-05 18:28:38 +00001450 {
1451 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001452 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001453 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001454 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001455
Lei Zhanga21d5932018-02-05 18:28:38 +00001456 // Check that interactive form annotation flag values are as expected.
1457 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1458 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1459 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1460 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1461 }
Diana Gage40870db2017-07-19 18:16:03 -07001462
Lei Zhanga21d5932018-02-05 18:28:38 +00001463 {
1464 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001465 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001466 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001467 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001468
Lei Zhanga21d5932018-02-05 18:28:38 +00001469 // Check that interactive form annotation flag values are as expected.
1470 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1471 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1472 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1473 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1474 }
Diana Gage40870db2017-07-19 18:16:03 -07001475
Lei Zhanga21d5932018-02-05 18:28:38 +00001476 {
1477 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001478 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001479 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001480 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001481
Lei Zhanga21d5932018-02-05 18:28:38 +00001482 // Check that interactive form annotation flag values are as expected.
1483 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1484 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1485 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1486 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1487 }
Diana Gage40870db2017-07-19 18:16:03 -07001488
1489 UnloadPage(page);
1490}