blob: 6d4521f26ea748d7dccd97f9814eda97140f92df [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"
Jane Liu4fd9a472017-06-01 18:56:09 -040011#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040012#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040013#include "public/fpdfview.h"
14#include "testing/embedder_test.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000015#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040016#include "testing/gtest/include/gtest/gtest.h"
17
Lei Zhangdf064df2017-08-31 02:33:27 -070018static constexpr char kContentsKey[] = "Contents";
19
Nicolas Pena3ff54002017-07-05 11:55:35 -040020class FPDFAnnotEmbeddertest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040021
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000022const std::wstring BufferToWString(std::vector<char>& buf) {
23 return GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()));
24}
25
26const std::string BufferToString(std::vector<char>& buf) {
27 return GetPlatformString(reinterpret_cast<unsigned short*>(buf.data()));
28}
29
Jane Liue17011d2017-06-21 12:18:37 -040030TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) {
31 // Open a file with one annotation and load its first page.
32 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
33 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
34 ASSERT_TRUE(page);
35
36 // This annotation has a malformed appearance stream, which does not have its
37 // normal appearance defined, only its rollover appearance. In this case, its
38 // normal appearance should be generated, allowing the highlight annotation to
39 // still display.
Nicolas Pena3ff54002017-07-05 11:55:35 -040040 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle(), FPDF_ANNOT);
Jane Liue17011d2017-06-21 12:18:37 -040041 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
42 FPDFBitmap_Destroy(bitmap);
43
44 UnloadPage(page);
45}
46
Jane Liu4fd9a472017-06-01 18:56:09 -040047TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
48 // Open a file with one annotation and load its first page.
49 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
50 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
51 ASSERT_TRUE(page);
52
53 // Check that there is a total of 1 annotation on its first page.
54 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
55
56 // Check that the annotation is of type "highlight".
Jane Liud60e9ad2017-06-26 11:28:36 -040057 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
58 ASSERT_TRUE(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -040059 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
60
61 // Check that the annotation color is yellow.
62 unsigned int R;
63 unsigned int G;
64 unsigned int B;
65 unsigned int A;
66 EXPECT_TRUE(
67 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
68 EXPECT_EQ(255u, R);
69 EXPECT_EQ(255u, G);
70 EXPECT_EQ(0u, B);
71 EXPECT_EQ(255u, A);
72
73 // Check that the author is correct.
Lei Zhangdf064df2017-08-31 02:33:27 -070074 static constexpr char kAuthorKey[] = "T";
75 EXPECT_EQ(FPDF_OBJECT_STRING, FPDFAnnot_GetValueType(annot, kAuthorKey));
76 unsigned long len = FPDFAnnot_GetStringValue(annot, kAuthorKey, nullptr, 0);
Jane Liu4fd9a472017-06-01 18:56:09 -040077 std::vector<char> buf(len);
Lei Zhangdf064df2017-08-31 02:33:27 -070078 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot, kAuthorKey, buf.data(), len));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000079 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -040080
81 // Check that the content is correct.
Lei Zhangdf064df2017-08-31 02:33:27 -070082 EXPECT_EQ(FPDF_OBJECT_STRING, FPDFAnnot_GetValueType(annot, kContentsKey));
83 len = FPDFAnnot_GetStringValue(annot, kContentsKey, nullptr, 0);
Jane Liu4fd9a472017-06-01 18:56:09 -040084 buf.clear();
85 buf.resize(len);
Lei Zhangdf064df2017-08-31 02:33:27 -070086 EXPECT_EQ(2690u,
87 FPDFAnnot_GetStringValue(annot, kContentsKey, buf.data(), len));
Jane Liu4fd9a472017-06-01 18:56:09 -040088 const wchar_t contents[] =
89 L"This is a note for that highlight annotation. Very long highlight "
90 "annotation. Long long long Long long longLong long longLong long "
91 "longLong long longLong long longLong long longLong long longLong long "
92 "longLong long longLong long longLong long longLong long longLong long "
93 "longLong long longLong long longLong long longLong long longLong long "
94 "longLong long longLong long longLong long longLong long longLong long "
95 "longLong long longLong long longLong long longLong long longLong long "
96 "longLong long longLong long longLong long longLong long longLong long "
97 "longLong long longLong long longLong long longLong long longLong long "
98 "longLong long longLong long longLong long longLong long longLong long "
99 "longLong long longLong long longLong long longLong long longLong long "
100 "longLong long longLong long longLong 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 long. END";
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000109 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400110
111 // Check that the quadpoints are correct.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400112 FS_QUADPOINTSF quadpoints;
113 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints));
Jane Liu4fd9a472017-06-01 18:56:09 -0400114 EXPECT_EQ(115.802643f, quadpoints.x1);
115 EXPECT_EQ(718.913940f, quadpoints.y1);
116 EXPECT_EQ(157.211182f, quadpoints.x4);
117 EXPECT_EQ(706.264465f, quadpoints.y4);
118
Jane Liue10509a2017-06-20 16:47:41 -0400119 FPDFPage_CloseAnnot(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400120 UnloadPage(page);
121}
122
123TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
124 // Open a file with three annotations and load its first page.
125 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
126 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
127 ASSERT_TRUE(page);
128
129 // Check that there is a total of 3 annotation on its first page.
130 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
131
Jane Liu20eafda2017-06-07 10:33:24 -0400132 // Check that the third annotation is of type "ink".
Jane Liud60e9ad2017-06-26 11:28:36 -0400133 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 2);
134 ASSERT_TRUE(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400135 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot));
136
137 // Check that the annotation color is blue with opacity.
138 unsigned int R;
139 unsigned int G;
140 unsigned int B;
141 unsigned int A;
142 EXPECT_TRUE(
143 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
144 EXPECT_EQ(0u, R);
145 EXPECT_EQ(0u, G);
146 EXPECT_EQ(255u, B);
147 EXPECT_EQ(76u, A);
148
149 // Check that there is no content.
Lei Zhangdf064df2017-08-31 02:33:27 -0700150 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot, kContentsKey, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400151
152 // Check that the rectange coordinates are correct.
153 // Note that upon rendering, the rectangle coordinates will be adjusted.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400154 FS_RECTF rect;
155 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu4fd9a472017-06-01 18:56:09 -0400156 EXPECT_EQ(351.820404f, rect.left);
157 EXPECT_EQ(583.830688f, rect.bottom);
158 EXPECT_EQ(475.336090f, rect.right);
159 EXPECT_EQ(681.535034f, rect.top);
160
Jane Liue10509a2017-06-20 16:47:41 -0400161 FPDFPage_CloseAnnot(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400162 UnloadPage(page);
163}
Jane Liu20eafda2017-06-07 10:33:24 -0400164
165TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
166 // Open a file with one annotation and load its first page.
167 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
168 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
169 ASSERT_TRUE(page);
170
171 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400172 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400173
174 UnloadPage(page);
175}
176
Jane Liud321ef92017-06-14 09:56:22 -0400177TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400178 // Open a file with no annotation and load its first page.
179 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
180 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
181 ASSERT_TRUE(page);
182 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
183
Jane Liueda65252017-06-07 11:31:27 -0400184 // Add a text annotation to the page.
Jane Liud60e9ad2017-06-26 11:28:36 -0400185 FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT);
186 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400187
188 // Check that there is now 1 annotations on this page.
189 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
190
191 // Check that the subtype of the annotation is correct.
192 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot));
Jane Liue10509a2017-06-20 16:47:41 -0400193 FPDFPage_CloseAnnot(annot);
194
Jane Liud60e9ad2017-06-26 11:28:36 -0400195 annot = FPDFPage_GetAnnot(page, 0);
196 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400197 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot));
198
199 // Set the color of the annotation.
200 ASSERT_TRUE(
201 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204));
202 // Check that the color has been set correctly.
203 unsigned int R;
204 unsigned int G;
205 unsigned int B;
206 unsigned int A;
207 EXPECT_TRUE(
208 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
209 EXPECT_EQ(51u, R);
210 EXPECT_EQ(102u, G);
211 EXPECT_EQ(153u, B);
212 EXPECT_EQ(204u, A);
213
214 // Change the color of the annotation.
215 ASSERT_TRUE(
216 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 204, 153, 102, 51));
217 // Check that the color has been set correctly.
218 EXPECT_TRUE(
219 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
220 EXPECT_EQ(204u, R);
221 EXPECT_EQ(153u, G);
222 EXPECT_EQ(102u, B);
223 EXPECT_EQ(51u, A);
224
225 // Set the annotation rectangle.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400226 FS_RECTF rect;
227 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liud60e9ad2017-06-26 11:28:36 -0400228 EXPECT_EQ(0.f, rect.left);
229 EXPECT_EQ(0.f, rect.right);
Jane Liu20eafda2017-06-07 10:33:24 -0400230 rect.left = 35;
231 rect.bottom = 150;
232 rect.right = 53;
233 rect.top = 165;
Jane Liu06462752017-06-27 16:41:14 -0400234 ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect));
Jane Liu20eafda2017-06-07 10:33:24 -0400235 // Check that the annotation rectangle has been set correctly.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400236 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu20eafda2017-06-07 10:33:24 -0400237 EXPECT_EQ(35.f, rect.left);
238 EXPECT_EQ(150.f, rect.bottom);
239 EXPECT_EQ(53.f, rect.right);
240 EXPECT_EQ(165.f, rect.top);
241
242 // Set the content of the annotation.
Lei Zhangdf064df2017-08-31 02:33:27 -0700243 static constexpr wchar_t contents[] = L"Hello! This is a customized content.";
Jane Liu20eafda2017-06-07 10:33:24 -0400244 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
245 GetFPDFWideString(contents);
Lei Zhangdf064df2017-08-31 02:33:27 -0700246 ASSERT_TRUE(FPDFAnnot_SetStringValue(annot, kContentsKey, text.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400247 // Check that the content has been set correctly.
Lei Zhangdf064df2017-08-31 02:33:27 -0700248 unsigned long len = FPDFAnnot_GetStringValue(annot, kContentsKey, nullptr, 0);
Jane Liu20eafda2017-06-07 10:33:24 -0400249 std::vector<char> buf(len);
Lei Zhangdf064df2017-08-31 02:33:27 -0700250 EXPECT_EQ(74u,
251 FPDFAnnot_GetStringValue(annot, kContentsKey, buf.data(), len));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000252 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu20eafda2017-06-07 10:33:24 -0400253
Jane Liue10509a2017-06-20 16:47:41 -0400254 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400255 UnloadPage(page);
256}
257
258TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
259 // Open a file with one annotation and load its first page.
260 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
261 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
262 ASSERT_TRUE(page);
263
264 // Check that there is a total of one annotation on its first page, and verify
265 // its quadpoints.
266 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liud60e9ad2017-06-26 11:28:36 -0400267 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
268 ASSERT_TRUE(annot);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400269 FS_QUADPOINTSF quadpoints;
270 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints));
Jane Liu20eafda2017-06-07 10:33:24 -0400271 EXPECT_EQ(115.802643f, quadpoints.x1);
272 EXPECT_EQ(718.913940f, quadpoints.y1);
273 EXPECT_EQ(157.211182f, quadpoints.x4);
274 EXPECT_EQ(706.264465f, quadpoints.y4);
Jane Liue10509a2017-06-20 16:47:41 -0400275 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400276
277 // Add an underline annotation to the page and set its quadpoints.
Jane Liud60e9ad2017-06-26 11:28:36 -0400278 annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE);
279 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400280 quadpoints.x1 = 140.802643f;
281 quadpoints.x3 = 140.802643f;
Jane Liu06462752017-06-27 16:41:14 -0400282 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, &quadpoints));
Jane Liue10509a2017-06-20 16:47:41 -0400283 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400284
285 // Save the document, closing the page and document.
286 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
287 FPDF_ClosePage(page);
288
289 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000290 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400291
292 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000293 page = LoadSavedPage(0);
294 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400295
296 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000297 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400298
299 // Check that the second annotation is an underline annotation and verify
300 // its quadpoints.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000301 FPDF_ANNOTATION new_annot = FPDFPage_GetAnnot(page, 1);
Jane Liud60e9ad2017-06-26 11:28:36 -0400302 ASSERT_TRUE(new_annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400303 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400304 FS_QUADPOINTSF new_quadpoints;
305 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(new_annot, &new_quadpoints));
Jane Liu20eafda2017-06-07 10:33:24 -0400306 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
307 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
308 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
309 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
310
Jane Liue10509a2017-06-20 16:47:41 -0400311 FPDFPage_CloseAnnot(new_annot);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400312
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000313 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400314 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400315}
Jane Liu06462752017-06-27 16:41:14 -0400316
317TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400318#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400319 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
320 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
321 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400322#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000323 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
324 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
325 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400326#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000327 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
328 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
329 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400330#endif
331
Jane Liu06462752017-06-27 16:41:14 -0400332 // Open a file with four annotations and load its first page.
333 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
334 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
335 ASSERT_TRUE(page);
336 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
337
Jane Liub370e5a2017-08-16 13:24:58 -0400338 // Check that the original file renders correctly.
339 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
340 CompareBitmap(bitmap, 612, 792, md5_original);
341 FPDFBitmap_Destroy(bitmap);
342
Jane Liu06462752017-06-27 16:41:14 -0400343 // Retrieve the highlight annotation which has its AP stream already defined.
344 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
345 ASSERT_TRUE(annot);
346 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
347
348 // Check that color cannot be set when an AP stream is defined already.
349 EXPECT_FALSE(
350 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204));
351
Jane Liub370e5a2017-08-16 13:24:58 -0400352 // Verify its attachment points.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400353 FS_QUADPOINTSF quadpoints;
354 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints));
Jane Liub370e5a2017-08-16 13:24:58 -0400355 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
356 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
357 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
358 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
Jane Liu06462752017-06-27 16:41:14 -0400359
Jane Liub370e5a2017-08-16 13:24:58 -0400360 // Check that updating the attachment points would succeed.
361 quadpoints.x1 -= 50.f;
362 quadpoints.x2 -= 50.f;
363 quadpoints.x3 -= 50.f;
364 quadpoints.x4 -= 50.f;
Jane Liu06462752017-06-27 16:41:14 -0400365 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, &quadpoints));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400366 FS_QUADPOINTSF new_quadpoints;
367 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &new_quadpoints));
Jane Liu06462752017-06-27 16:41:14 -0400368 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
369 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
370 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
371 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
372
Jane Liub370e5a2017-08-16 13:24:58 -0400373 // Check that updating quadpoints does not change the annotation's position.
374 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
375 CompareBitmap(bitmap, 612, 792, md5_original);
376 FPDFBitmap_Destroy(bitmap);
377
378 // Verify its annotation rectangle.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400379 FS_RECTF rect;
380 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu06462752017-06-27 16:41:14 -0400381 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
382 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
383 EXPECT_NEAR(136.325f, rect.right, 0.001f);
384 EXPECT_NEAR(721.292f, rect.top, 0.001f);
385
Jane Liub370e5a2017-08-16 13:24:58 -0400386 // Check that updating the rectangle would succeed.
387 rect.left -= 60.f;
388 rect.right -= 60.f;
Jane Liu06462752017-06-27 16:41:14 -0400389 ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400390 FS_RECTF new_rect;
391 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect));
Jane Liu06462752017-06-27 16:41:14 -0400392 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400393 FPDFPage_CloseAnnot(annot);
394
Jane Liub370e5a2017-08-16 13:24:58 -0400395 // Check that updating the rectangle changes the annotation's position.
396 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
397 CompareBitmap(bitmap, 612, 792, md5_modified_highlight);
398 FPDFBitmap_Destroy(bitmap);
399
Jane Liu06462752017-06-27 16:41:14 -0400400 // Retrieve the square annotation which has its AP stream already defined.
401 annot = FPDFPage_GetAnnot(page, 2);
402 ASSERT_TRUE(annot);
403 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot));
404
Jane Liub370e5a2017-08-16 13:24:58 -0400405 // Check that updating the rectangle would succeed.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400406 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liub370e5a2017-08-16 13:24:58 -0400407 rect.left += 70.f;
408 rect.right += 70.f;
Jane Liu06462752017-06-27 16:41:14 -0400409 ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400410 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect));
Jane Liu06462752017-06-27 16:41:14 -0400411 EXPECT_EQ(rect.right, new_rect.right);
412
Jane Liub370e5a2017-08-16 13:24:58 -0400413 // Check that updating the rectangle changes the square annotation's position.
414 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
415 CompareBitmap(bitmap, 612, 792, md5_modified_square);
416 FPDFBitmap_Destroy(bitmap);
417
Jane Liu06462752017-06-27 16:41:14 -0400418 FPDFPage_CloseAnnot(annot);
419 UnloadPage(page);
420}
Jane Liu8ce58f52017-06-29 13:40:22 -0400421
422TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
423 // Open a file with 3 annotations on its first page.
424 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
425 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
426 ASSERT_TRUE(page);
427 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
428
429 // Check that the annotations have the expected rectangle coordinates.
430 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400431 FS_RECTF rect;
432 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu8ce58f52017-06-29 13:40:22 -0400433 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
434 FPDFPage_CloseAnnot(annot);
435
436 annot = FPDFPage_GetAnnot(page, 1);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400437 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu8ce58f52017-06-29 13:40:22 -0400438 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
439 FPDFPage_CloseAnnot(annot);
440
441 annot = FPDFPage_GetAnnot(page, 2);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400442 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu8ce58f52017-06-29 13:40:22 -0400443 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
444 FPDFPage_CloseAnnot(annot);
445
446 // Check that nothing happens when attempting to remove an annotation with an
447 // out-of-bound index.
448 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
449 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
450 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
451
452 // Remove the second annotation.
453 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
454 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
455 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
456
457 // Save the document, closing the page and document.
458 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
459 FPDF_ClosePage(page);
460
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400461 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400462 // Open the saved document.
463 std::string new_file = GetString();
464 FPDF_FILEACCESS file_access;
465 memset(&file_access, 0, sizeof(file_access));
466 file_access.m_FileLen = new_file.size();
467 file_access.m_GetBlock = GetBlockFromString;
468 file_access.m_Param = &new_file;
469 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
470 ASSERT_TRUE(new_doc);
471 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
472 ASSERT_TRUE(new_page);
473
474 // Check that the saved document has 2 annotations on the first page.
475 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
476
477 // Check that the remaining 2 annotations are the original 1st and 3rd ones by
478 // verifying their rectangle coordinates.
479 annot = FPDFPage_GetAnnot(new_page, 0);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400480 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu8ce58f52017-06-29 13:40:22 -0400481 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
482 FPDFPage_CloseAnnot(annot);
483
484 annot = FPDFPage_GetAnnot(new_page, 1);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400485 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu8ce58f52017-06-29 13:40:22 -0400486 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
487 FPDFPage_CloseAnnot(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400488 FPDF_ClosePage(new_page);
489 FPDF_CloseDocument(new_doc);
490}
Jane Liu8ce58f52017-06-29 13:40:22 -0400491
Jane Liubaa7ff42017-06-29 19:18:23 -0400492TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400493#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400494 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
495 const char md5_modified_path[] = "cf3cea74bd46497520ff6c4d1ea228c8";
496 const char md5_two_paths[] = "e8994452fc4385337bae5522354e10ff";
497 const char md5_new_annot[] = "ee5372b31fede117fc83b9384598aa25";
Jane Liubaa7ff42017-06-29 19:18:23 -0400498#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000499 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
500 const char md5_modified_path[] = "3f77b88ce6048e08e636c9a03921b2e5";
501 const char md5_two_paths[] = "bffbf5ecd15862b9fe553c795400ff8e";
502 const char md5_new_annot[] = "e020534c7eeea76be537c70d6e359a40";
Jane Liubaa7ff42017-06-29 19:18:23 -0400503#endif
504
505 // Open a file with two annotations and load its first page.
506 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
507 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
508 ASSERT_TRUE(page);
509 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
510
511 // Check that the page renders correctly.
512 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400513 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liubaa7ff42017-06-29 19:18:23 -0400514 FPDFBitmap_Destroy(bitmap);
515
516 // Retrieve the stamp annotation which has its AP stream already defined.
517 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
518 ASSERT_TRUE(annot);
519
520 // Check that this annotation has one path object and retrieve it.
Jane Liu36567742017-07-06 11:13:35 -0400521 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot));
522 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot, 1);
Jane Liubaa7ff42017-06-29 19:18:23 -0400523 EXPECT_FALSE(path);
Jane Liu36567742017-07-06 11:13:35 -0400524 path = FPDFAnnot_GetObject(annot, 0);
525 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400526 EXPECT_TRUE(path);
527
528 // Modify the color of the path object.
529 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
Jane Liu36567742017-07-06 11:13:35 -0400530 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot, path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400531
532 // Check that the page with the modified annotation renders correctly.
533 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400534 CompareBitmap(bitmap, 595, 842, md5_modified_path);
535 FPDFBitmap_Destroy(bitmap);
536
537 // Add a second path object to the same annotation.
538 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
539 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
540 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
541 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
542 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
543 EXPECT_TRUE(FPDFAnnot_AppendObject(annot, dot));
544 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot));
545
546 // Check that the page with an annotation with two paths renders correctly.
547 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
548 CompareBitmap(bitmap, 595, 842, md5_two_paths);
549 FPDFBitmap_Destroy(bitmap);
550
551 // Delete the newly added path object.
552 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot, 1));
553 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot));
554 FPDFPage_CloseAnnot(annot);
555
556 // Check that the page renders the same as before.
557 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
558 CompareBitmap(bitmap, 595, 842, md5_modified_path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400559 FPDFBitmap_Destroy(bitmap);
560
561 // Create another stamp annotation and set its annotation rectangle.
562 annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP);
563 ASSERT_TRUE(annot);
564 FS_RECTF rect;
565 rect.left = 200.f;
566 rect.bottom = 400.f;
567 rect.right = 500.f;
568 rect.top = 600.f;
569 EXPECT_TRUE(FPDFAnnot_SetRect(annot, &rect));
570
571 // Add a new path to the annotation.
572 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
573 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
574 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
575 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
576 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
577 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
578 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
579 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
Jane Liu36567742017-07-06 11:13:35 -0400580 EXPECT_TRUE(FPDFAnnot_AppendObject(annot, check));
581 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot));
Jane Liubaa7ff42017-06-29 19:18:23 -0400582
583 // Check that the annotation's bounding box came from its rectangle.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400584 FS_RECTF new_rect;
585 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400586 EXPECT_EQ(rect.left, new_rect.left);
587 EXPECT_EQ(rect.bottom, new_rect.bottom);
588 EXPECT_EQ(rect.right, new_rect.right);
589 EXPECT_EQ(rect.top, new_rect.top);
590
591 // Save the document, closing the page and document.
592 FPDFPage_CloseAnnot(annot);
593 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
594 FPDF_ClosePage(page);
595
596 // Open the saved document.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400597 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000598 page = LoadSavedPage(0);
599 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400600
Jane Liu36567742017-07-06 11:13:35 -0400601 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000602 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
603 annot = FPDFPage_GetAnnot(page, 2);
Jane Liubaa7ff42017-06-29 19:18:23 -0400604 ASSERT_TRUE(annot);
Jane Liu36567742017-07-06 11:13:35 -0400605 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot));
Jane Liubaa7ff42017-06-29 19:18:23 -0400606
607 // Check that the new annotation's rectangle is as defined.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400608 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400609 EXPECT_EQ(rect.left, new_rect.left);
610 EXPECT_EQ(rect.bottom, new_rect.bottom);
611 EXPECT_EQ(rect.right, new_rect.right);
612 EXPECT_EQ(rect.top, new_rect.top);
613
Jane Liubaa7ff42017-06-29 19:18:23 -0400614 FPDFPage_CloseAnnot(annot);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000615 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400616 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400617}
Jane Liub137e752017-07-05 15:04:33 -0400618
619TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
620 // Open a file with an annotation and load its first page.
621 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
622 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
623 ASSERT_TRUE(page);
624
625 // Check that the page renders correctly.
626 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
627 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
628 FPDFBitmap_Destroy(bitmap);
629
630 // Retrieve the annotation.
631 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
632 ASSERT_TRUE(annot);
633
634 // Check that the original flag values are as expected.
635 int flags = FPDFAnnot_GetFlags(annot);
636 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
637 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
638
639 // Set the HIDDEN flag.
640 flags |= FPDF_ANNOT_FLAG_HIDDEN;
641 EXPECT_TRUE(FPDFAnnot_SetFlags(annot, flags));
642 flags = FPDFAnnot_GetFlags(annot);
643 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
644 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
645
646 // Check that the page renders correctly without rendering the annotation.
647 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
648 CompareBitmap(bitmap, 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
649 FPDFBitmap_Destroy(bitmap);
650
651 // Unset the HIDDEN flag.
652 EXPECT_TRUE(FPDFAnnot_SetFlags(annot, FPDF_ANNOT_FLAG_NONE));
653 EXPECT_FALSE(FPDFAnnot_GetFlags(annot));
654 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
655 EXPECT_TRUE(FPDFAnnot_SetFlags(annot, flags));
656 flags = FPDFAnnot_GetFlags(annot);
657 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
658 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
659
660 // Check that the page renders correctly as before.
661 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
662 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
663 FPDFBitmap_Destroy(bitmap);
664
665 FPDFPage_CloseAnnot(annot);
666 UnloadPage(page);
667}
Jane Liu36567742017-07-06 11:13:35 -0400668
669TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400670#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400671 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
672 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
673 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Jane Liu36567742017-07-06 11:13:35 -0400674#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000675 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
676 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
677 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400678#endif
679
680 // Open a file with two annotations and load its first page.
681 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
682 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
683 ASSERT_TRUE(page);
684 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
685
686 // Check that the page renders correctly.
687 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400688 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liu36567742017-07-06 11:13:35 -0400689 FPDFBitmap_Destroy(bitmap);
690
691 // Create a stamp annotation and set its annotation rectangle.
692 FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP);
693 ASSERT_TRUE(annot);
694 FS_RECTF rect;
695 rect.left = 200.f;
696 rect.bottom = 600.f;
697 rect.right = 400.f;
698 rect.top = 800.f;
699 EXPECT_TRUE(FPDFAnnot_SetRect(annot, &rect));
700
701 // Add a solid-color translucent image object to the new annotation.
702 constexpr int kBitmapSize = 200;
703 FPDF_BITMAP image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
704 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, 0xeeeecccc);
705 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
706 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
707 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
708 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
709 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
710 kBitmapSize, 0, 0));
711 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
712 EXPECT_TRUE(FPDFAnnot_AppendObject(annot, image_object));
713 FPDFPage_CloseAnnot(annot);
714
715 // Check that the page renders correctly with the new image object.
716 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400717 CompareBitmap(bitmap, 595, 842, md5_new_image);
Jane Liu36567742017-07-06 11:13:35 -0400718 FPDFBitmap_Destroy(bitmap);
719
720 // Retrieve the newly added stamp annotation and its image object.
721 annot = FPDFPage_GetAnnot(page, 2);
722 ASSERT_TRUE(annot);
723 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot));
724 image_object = FPDFAnnot_GetObject(annot, 0);
725 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
726
727 // Modify the image in the new annotation.
728 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, 0xff000000);
729 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
730 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot, image_object));
731 FPDFPage_CloseAnnot(annot);
732
733 // Save the document, closing the page and document.
734 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
735 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400736 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400737
738 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400739 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400740}
741
742TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400743#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400744 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
745 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
746 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Jane Liu36567742017-07-06 11:13:35 -0400747#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000748 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
749 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
750 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400751#endif
752
753 // Open a file with two annotations and load its first page.
754 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
755 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
756 ASSERT_TRUE(page);
757 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
758
759 // Check that the page renders correctly.
760 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400761 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liu36567742017-07-06 11:13:35 -0400762 FPDFBitmap_Destroy(bitmap);
763
764 // Create a stamp annotation and set its annotation rectangle.
765 FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP);
766 ASSERT_TRUE(annot);
767 FS_RECTF rect;
768 rect.left = 200.f;
769 rect.bottom = 550.f;
770 rect.right = 450.f;
771 rect.top = 650.f;
772 EXPECT_TRUE(FPDFAnnot_SetRect(annot, &rect));
773
774 // Add a translucent text object to the new annotation.
775 FPDF_PAGEOBJECT text_object =
776 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
777 EXPECT_TRUE(text_object);
778 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
779 GetFPDFWideString(L"I'm a translucent text laying on other text.");
780 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
781 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
782 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
783 EXPECT_TRUE(FPDFAnnot_AppendObject(annot, text_object));
784 FPDFPage_CloseAnnot(annot);
785
786 // Check that the page renders correctly with the new text object.
787 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400788 CompareBitmap(bitmap, 595, 842, md5_new_text);
Jane Liu36567742017-07-06 11:13:35 -0400789 FPDFBitmap_Destroy(bitmap);
790
791 // Retrieve the newly added stamp annotation and its text object.
792 annot = FPDFPage_GetAnnot(page, 2);
793 ASSERT_TRUE(annot);
794 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot));
795 text_object = FPDFAnnot_GetObject(annot, 0);
796 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
797
798 // Modify the text in the new annotation.
799 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
800 GetFPDFWideString(L"New text!");
801 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
802 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot, text_object));
803 FPDFPage_CloseAnnot(annot);
804
805 // Check that the page renders correctly with the modified text object.
806 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400807 CompareBitmap(bitmap, 595, 842, md5_modified_text);
Jane Liu36567742017-07-06 11:13:35 -0400808 FPDFBitmap_Destroy(bitmap);
809
810 // Remove the new annotation, and check that the page renders as before.
811 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
812 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400813 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liu36567742017-07-06 11:13:35 -0400814 FPDFBitmap_Destroy(bitmap);
815
816 UnloadPage(page);
817}
Jane Liu2e1a32b2017-07-06 12:01:25 -0400818
819TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
820 // Open a file with four annotations and load its first page.
821 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
822 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
823 ASSERT_TRUE(page);
824
825 // Retrieve the first annotation.
826 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
827 ASSERT_TRUE(annot);
828
829 // Check that a non-existent key does not exist.
Lei Zhangdf064df2017-08-31 02:33:27 -0700830 EXPECT_FALSE(FPDFAnnot_HasKey(annot, "none"));
Jane Liu2e1a32b2017-07-06 12:01:25 -0400831
832 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhangdf064df2017-08-31 02:33:27 -0700833 static constexpr char kApKey[] = "AP";
834 EXPECT_TRUE(FPDFAnnot_HasKey(annot, kApKey));
835 EXPECT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, kApKey));
836 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot, kApKey, nullptr, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -0400837
838 // Check that the string value of the hash is correct.
Lei Zhangdf064df2017-08-31 02:33:27 -0700839 static constexpr char kHashKey[] = "AAPL:Hash";
840 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot, kHashKey));
841 unsigned long len = FPDFAnnot_GetStringValue(annot, kHashKey, nullptr, 0);
Jane Liu2e1a32b2017-07-06 12:01:25 -0400842 std::vector<char> buf(len);
Lei Zhangdf064df2017-08-31 02:33:27 -0700843 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot, kHashKey, buf.data(), len));
Jane Liu2e1a32b2017-07-06 12:01:25 -0400844 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000845 BufferToWString(buf).c_str());
Jane Liu2e1a32b2017-07-06 12:01:25 -0400846
847 // Check that the string value of the modified date is correct.
Lei Zhangdf064df2017-08-31 02:33:27 -0700848 static constexpr char kDateKey[] = "M";
849 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot, kHashKey));
850 len = FPDFAnnot_GetStringValue(annot, kDateKey, nullptr, 0);
Jane Liu2e1a32b2017-07-06 12:01:25 -0400851 buf.clear();
852 buf.resize(len);
Lei Zhangdf064df2017-08-31 02:33:27 -0700853 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot, kDateKey, buf.data(), len));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000854 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
Jane Liu2e1a32b2017-07-06 12:01:25 -0400855
856 // Update the date entry for the annotation.
857 const wchar_t new_date[] = L"D:201706282359Z00'00'";
858 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
859 GetFPDFWideString(new_date);
Lei Zhangdf064df2017-08-31 02:33:27 -0700860 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot, kDateKey, text.get()));
Jane Liu2e1a32b2017-07-06 12:01:25 -0400861
862 // Save the document, closing the page and document.
863 FPDFPage_CloseAnnot(annot);
864 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
865 FPDF_ClosePage(page);
866
867 // Open the saved annotation.
Dan Sinclair698aed72017-09-26 16:24:49 -0400868#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +0300869 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400870#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000871 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400872#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400873 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000874 page = LoadSavedPage(0);
875 VerifySavedRendering(page, 595, 842, md5);
876 FPDF_ANNOTATION new_annot = FPDFPage_GetAnnot(page, 0);
Jane Liu2e1a32b2017-07-06 12:01:25 -0400877
878 // Check that the string value of the modified date is the newly-set value.
Lei Zhangdf064df2017-08-31 02:33:27 -0700879 EXPECT_EQ(FPDF_OBJECT_STRING, FPDFAnnot_GetValueType(new_annot, kDateKey));
880 len = FPDFAnnot_GetStringValue(new_annot, kDateKey, nullptr, 0);
Jane Liu2e1a32b2017-07-06 12:01:25 -0400881 buf.clear();
882 buf.resize(len);
Lei Zhangdf064df2017-08-31 02:33:27 -0700883 EXPECT_EQ(44u,
884 FPDFAnnot_GetStringValue(new_annot, kDateKey, buf.data(), len));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000885 EXPECT_STREQ(new_date, BufferToWString(buf).c_str());
Jane Liu2e1a32b2017-07-06 12:01:25 -0400886
887 FPDFPage_CloseAnnot(new_annot);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000888 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400889 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -0400890}
Diana Gage7e0c05d2017-07-19 17:33:33 -0700891
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000892TEST_F(FPDFAnnotEmbeddertest, GetAP) {
893 // Open a file with four annotations and load its first page.
894 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
895 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
896 ASSERT_TRUE(page);
897
898 // Retrieve the first annotation.
899 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
900 ASSERT_TRUE(annot);
901
902 // Check that the string value of an AP returns the expected length.
903 unsigned long len =
904 FPDFAnnot_GetAP(annot, FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
905 EXPECT_EQ(73970u, len);
906
907 // Check that the string value of an AP is not returned if the buffer is too
908 // small. The result buffer should be overwritten with an empty string.
909 std::vector<char> buf(len - 1);
910 // Write L"z" in the buffer to verify it's not overwritten.
911 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
912 EXPECT_EQ(73970u, FPDFAnnot_GetAP(annot, FPDF_ANNOT_APPEARANCEMODE_NORMAL,
913 buf.data(), len - 1));
914 std::string ap = BufferToString(buf);
915 EXPECT_STREQ("z", ap.c_str());
916
917 // Check that the string value of an AP is returned through a buffer that is
918 // the right size.
919 buf.clear();
920 buf.resize(len);
921 EXPECT_EQ(73970u, FPDFAnnot_GetAP(annot, FPDF_ANNOT_APPEARANCEMODE_NORMAL,
922 buf.data(), len));
923 ap = BufferToString(buf);
924 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
925 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
926
927 // Check that the string value of an AP is returned through a buffer that is
928 // larger than necessary.
929 buf.clear();
930 buf.resize(len + 1);
931 EXPECT_EQ(73970u, FPDFAnnot_GetAP(annot, FPDF_ANNOT_APPEARANCEMODE_NORMAL,
932 buf.data(), len + 1));
933 ap = BufferToString(buf);
934 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
935 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
936
937 // Check that getting an AP for a mode that does not have an AP returns an
938 // empty string.
939 buf.clear();
940 buf.resize(len);
941 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot, FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
942 buf.data(), len));
943 ap = BufferToString(buf);
944 EXPECT_STREQ("", ap.c_str());
945
946 FPDFPage_CloseAnnot(annot);
947 FPDF_ClosePage(page);
948}
949
Jane Liu300bb272017-08-21 14:37:53 -0400950TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) {
951 // Open a file with annotations and load its first page.
952 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
953 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
954 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -0400955 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -0400956
957 // Retrieve the highlight annotation which has its popup defined.
958 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
959 ASSERT_TRUE(annot);
960 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
Jane Liud1ed1ce2017-08-24 12:31:10 -0400961 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot));
Lei Zhangdf064df2017-08-31 02:33:27 -0700962 static constexpr char kPopupKey[] = "Popup";
963 ASSERT_TRUE(FPDFAnnot_HasKey(annot, kPopupKey));
964 ASSERT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -0400965
966 // Retrieve and verify the popup of the highlight annotation.
Lei Zhangdf064df2017-08-31 02:33:27 -0700967 FPDF_ANNOTATION popup = FPDFAnnot_GetLinkedAnnot(annot, kPopupKey);
Jane Liu300bb272017-08-21 14:37:53 -0400968 ASSERT_TRUE(popup);
969 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup));
Jane Liud1ed1ce2017-08-24 12:31:10 -0400970 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup));
Jane Liu300bb272017-08-21 14:37:53 -0400971 FS_RECTF rect;
972 ASSERT_TRUE(FPDFAnnot_GetRect(popup, &rect));
973 EXPECT_NEAR(612.0f, rect.left, 0.001f);
974 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
975
976 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail, since
977 // "IRT" is not a key in |annot|'s dictionary.
Lei Zhangdf064df2017-08-31 02:33:27 -0700978 static constexpr char kIRTKey[] = "IRT";
979 ASSERT_FALSE(FPDFAnnot_HasKey(annot, kIRTKey));
980 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot, kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -0400981
982 // Attempting to retrieve |annot|'s parent dictionary as an annotation would
983 // fail, since its parent is not an annotation.
Lei Zhangdf064df2017-08-31 02:33:27 -0700984 static constexpr char kPKey[] = "P";
985 ASSERT_TRUE(FPDFAnnot_HasKey(annot, kPKey));
986 EXPECT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, kPKey));
987 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot, kPKey));
Jane Liu300bb272017-08-21 14:37:53 -0400988
989 FPDFPage_CloseAnnot(popup);
990 FPDFPage_CloseAnnot(annot);
991 UnloadPage(page);
992}
993
Diana Gage7e0c05d2017-07-19 17:33:33 -0700994TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
995 // Open file with form text fields.
996 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
997 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
998 ASSERT_TRUE(page);
999
1000 // Retrieve the first annotation: user-editable text field.
1001 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
1002 ASSERT_TRUE(annot);
1003
1004 // Check that the flag values are as expected.
1005 int flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1006 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1007 FPDFPage_CloseAnnot(annot);
1008
1009 // Retrieve the second annotation: read-only text field.
1010 annot = FPDFPage_GetAnnot(page, 1);
1011 ASSERT_TRUE(annot);
1012
1013 // Check that the flag values are as expected.
1014 flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1015 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1016 FPDFPage_CloseAnnot(annot);
1017
1018 UnloadPage(page);
1019}
1020
1021TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
1022 // Open file with form text fields.
1023 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1024 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
1025 ASSERT_TRUE(page);
1026
1027 // Retrieve the first annotation: user-editable combobox.
1028 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
1029 ASSERT_TRUE(annot);
1030
1031 // Check that the flag values are as expected.
1032 int flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1033 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1034 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1035 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1036 FPDFPage_CloseAnnot(annot);
1037
1038 // Retrieve the second annotation: regular combobox.
1039 annot = FPDFPage_GetAnnot(page, 1);
1040 ASSERT_TRUE(annot);
1041
1042 // Check that the flag values are as expected.
1043 flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1044 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1045 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1046 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1047 FPDFPage_CloseAnnot(annot);
1048
1049 // Retrieve the third annotation: read-only combobox.
1050 annot = FPDFPage_GetAnnot(page, 2);
1051 ASSERT_TRUE(annot);
1052
1053 // Check that the flag values are as expected.
1054 flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1055 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1056 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1057 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1058 FPDFPage_CloseAnnot(annot);
1059
1060 UnloadPage(page);
1061}
Diana Gage40870db2017-07-19 18:16:03 -07001062
1063TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
1064 // Open file with form text fields.
1065 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1066 FPDF_PAGE page = LoadPage(0);
1067 ASSERT_TRUE(page);
1068
1069 // Attempt to get an annotation where no annotation exists on page.
1070 FPDF_ANNOTATION annot =
1071 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0);
1072 EXPECT_FALSE(annot);
1073
1074 UnloadPage(page);
1075}
1076
1077TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
1078 // Open file with form text fields.
1079 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1080 FPDF_PAGE page = LoadPage(0);
1081 ASSERT_TRUE(page);
1082
1083 // Retrieve user-editable text field annotation.
1084 FPDF_ANNOTATION annot =
1085 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118);
1086 ASSERT_TRUE(annot);
1087
1088 // Check that interactive form annotation flag values are as expected.
1089 int flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1090 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1091 FPDFPage_CloseAnnot(annot);
1092
1093 // Retrieve read-only text field annotation.
1094 annot = FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202);
1095 ASSERT_TRUE(annot);
1096
1097 // Check that interactive form annotation flag values are as expected.
1098 flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1099 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1100 FPDFPage_CloseAnnot(annot);
1101
1102 UnloadPage(page);
1103}
1104
1105TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1106 // Open file with form comboboxes.
1107 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1108 FPDF_PAGE page = LoadPage(0);
1109 ASSERT_TRUE(page);
1110
1111 // Retrieve user-editable combobox annotation.
1112 FPDF_ANNOTATION annot =
1113 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 63);
1114 ASSERT_TRUE(annot);
1115
1116 // Check that interactive form annotation flag values are as expected.
1117 int flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1118 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1119 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1120 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1121 FPDFPage_CloseAnnot(annot);
1122
1123 // Retrieve regular combobox annotation.
1124 annot = FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 113);
1125 ASSERT_TRUE(annot);
1126
1127 // Check that interactive form annotation flag values are as expected.
1128 flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1129 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1130 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1131 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1132 FPDFPage_CloseAnnot(annot);
1133
1134 // Retrieve read-only combobox annotation.
1135 annot = FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 213);
1136 ASSERT_TRUE(annot);
1137
1138 // Check that interactive form annotation flag values are as expected.
1139 flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1140 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1141 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1142 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1143 FPDFPage_CloseAnnot(annot);
1144
1145 UnloadPage(page);
1146}