blob: b7dee88b7874cc79f2b83777572e6fcc816fa733 [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
Jane Liu20eafda2017-06-07 10:33:24 -04005#include <memory>
6#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04007#include <vector>
8
9#include "public/fpdf_annot.h"
10#include "public/fpdfview.h"
11#include "testing/embedder_test.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
Jane Liu20eafda2017-06-07 10:33:24 -040014class FPDFAnnotEmbeddertest : public EmbedderTest, public TestSaver {};
Jane Liu4fd9a472017-06-01 18:56:09 -040015
Jane Liue17011d2017-06-21 12:18:37 -040016TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) {
17 // Open a file with one annotation and load its first page.
18 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
19 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
20 ASSERT_TRUE(page);
21
22 // This annotation has a malformed appearance stream, which does not have its
23 // normal appearance defined, only its rollover appearance. In this case, its
24 // normal appearance should be generated, allowing the highlight annotation to
25 // still display.
26 FPDF_BITMAP bitmap = RenderPageWithFlags(page, FPDF_ANNOT);
27 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
28 FPDFBitmap_Destroy(bitmap);
29
30 UnloadPage(page);
31}
32
Jane Liu4fd9a472017-06-01 18:56:09 -040033TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
34 // Open a file with one annotation and load its first page.
35 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
36 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
37 ASSERT_TRUE(page);
38
39 // Check that there is a total of 1 annotation on its first page.
40 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
41
42 // Check that the annotation is of type "highlight".
43 FPDF_ANNOTATION annot;
44 ASSERT_TRUE(FPDFPage_GetAnnot(page, 0, &annot));
45 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
46
47 // Check that the annotation color is yellow.
48 unsigned int R;
49 unsigned int G;
50 unsigned int B;
51 unsigned int A;
52 EXPECT_TRUE(
53 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
54 EXPECT_EQ(255u, R);
55 EXPECT_EQ(255u, G);
56 EXPECT_EQ(0u, B);
57 EXPECT_EQ(255u, A);
58
59 // Check that the author is correct.
60 unsigned long len =
61 FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Author, nullptr, 0);
62 std::vector<char> buf(len);
63 EXPECT_EQ(28u, FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Author, buf.data(),
64 len));
65 EXPECT_STREQ(L"Jae Hyun Park",
66 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
67 .c_str());
68
69 // Check that the content is correct.
70 len = FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0);
71 buf.clear();
72 buf.resize(len);
73 EXPECT_EQ(2690u, FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents,
74 buf.data(), len));
75 const wchar_t contents[] =
76 L"This is a note for that highlight annotation. Very long highlight "
77 "annotation. Long long long Long long longLong long longLong long "
78 "longLong long longLong long longLong long longLong long longLong long "
79 "longLong long longLong long longLong long longLong long longLong long "
80 "longLong long longLong long longLong long longLong long longLong long "
81 "longLong long longLong long longLong long longLong long longLong long "
82 "longLong long longLong long longLong long longLong long longLong long "
83 "longLong long longLong long longLong long longLong long longLong long "
84 "longLong long longLong long longLong long longLong long longLong long "
85 "longLong long longLong long longLong long longLong long longLong long "
86 "longLong long longLong long longLong long longLong long longLong long "
87 "longLong long longLong long longLong long longLong long longLong long "
88 "longLong long longLong long longLong long longLong long longLong long "
89 "longLong long longLong long longLong long longLong long longLong long "
90 "longLong long longLong long longLong 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 long. END";
96 EXPECT_STREQ(contents,
97 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
98 .c_str());
99
100 // Check that the quadpoints are correct.
101 FS_QUADPOINTSF quadpoints;
102 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints));
103 EXPECT_EQ(115.802643f, quadpoints.x1);
104 EXPECT_EQ(718.913940f, quadpoints.y1);
105 EXPECT_EQ(157.211182f, quadpoints.x4);
106 EXPECT_EQ(706.264465f, quadpoints.y4);
107
Jane Liue10509a2017-06-20 16:47:41 -0400108 FPDFPage_CloseAnnot(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400109 UnloadPage(page);
110}
111
112TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
113 // Open a file with three annotations and load its first page.
114 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
115 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
116 ASSERT_TRUE(page);
117
118 // Check that there is a total of 3 annotation on its first page.
119 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
120
Jane Liu20eafda2017-06-07 10:33:24 -0400121 // Check that the third annotation is of type "ink".
Jane Liu4fd9a472017-06-01 18:56:09 -0400122 FPDF_ANNOTATION annot;
123 ASSERT_TRUE(FPDFPage_GetAnnot(page, 2, &annot));
124 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot));
125
126 // Check that the annotation color is blue with opacity.
127 unsigned int R;
128 unsigned int G;
129 unsigned int B;
130 unsigned int A;
131 EXPECT_TRUE(
132 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
133 EXPECT_EQ(0u, R);
134 EXPECT_EQ(0u, G);
135 EXPECT_EQ(255u, B);
136 EXPECT_EQ(76u, A);
137
138 // Check that there is no content.
139 EXPECT_EQ(2u,
140 FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0));
141
142 // Check that the rectange coordinates are correct.
143 // Note that upon rendering, the rectangle coordinates will be adjusted.
144 FS_RECTF rect;
145 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
146 EXPECT_EQ(351.820404f, rect.left);
147 EXPECT_EQ(583.830688f, rect.bottom);
148 EXPECT_EQ(475.336090f, rect.right);
149 EXPECT_EQ(681.535034f, rect.top);
150
Jane Liue10509a2017-06-20 16:47:41 -0400151 FPDFPage_CloseAnnot(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400152 UnloadPage(page);
153}
Jane Liu20eafda2017-06-07 10:33:24 -0400154
155TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
156 // Open a file with one annotation and load its first page.
157 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
158 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
159 ASSERT_TRUE(page);
160
161 // Add an annotation with an illegal subtype.
162 FPDF_ANNOTATION annot;
163 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1, &annot));
164
165 UnloadPage(page);
166}
167
Jane Liud321ef92017-06-14 09:56:22 -0400168TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400169 // Open a file with no annotation and load its first page.
170 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
171 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
172 ASSERT_TRUE(page);
173 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
174
Jane Liueda65252017-06-07 11:31:27 -0400175 // Add a text annotation to the page.
Jane Liu20eafda2017-06-07 10:33:24 -0400176 FPDF_ANNOTATION annot;
177 ASSERT_TRUE(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT, &annot));
178
179 // Check that there is now 1 annotations on this page.
180 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
181
182 // Check that the subtype of the annotation is correct.
183 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot));
Jane Liue10509a2017-06-20 16:47:41 -0400184 FPDFPage_CloseAnnot(annot);
185
Jane Liu20eafda2017-06-07 10:33:24 -0400186 ASSERT_TRUE(FPDFPage_GetAnnot(page, 0, &annot));
187 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot));
188
189 // Set the color of the annotation.
190 ASSERT_TRUE(
191 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204));
192 // Check that the color has been set correctly.
193 unsigned int R;
194 unsigned int G;
195 unsigned int B;
196 unsigned int A;
197 EXPECT_TRUE(
198 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
199 EXPECT_EQ(51u, R);
200 EXPECT_EQ(102u, G);
201 EXPECT_EQ(153u, B);
202 EXPECT_EQ(204u, A);
203
204 // Change the color of the annotation.
205 ASSERT_TRUE(
206 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 204, 153, 102, 51));
207 // Check that the color has been set correctly.
208 EXPECT_TRUE(
209 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
210 EXPECT_EQ(204u, R);
211 EXPECT_EQ(153u, G);
212 EXPECT_EQ(102u, B);
213 EXPECT_EQ(51u, A);
214
215 // Set the annotation rectangle.
216 FS_RECTF rect;
217 EXPECT_FALSE(FPDFAnnot_GetRect(annot, &rect));
218 rect.left = 35;
219 rect.bottom = 150;
220 rect.right = 53;
221 rect.top = 165;
222 ASSERT_TRUE(FPDFAnnot_SetRect(annot, rect));
223 // Check that the annotation rectangle has been set correctly.
224 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
225 EXPECT_EQ(35.f, rect.left);
226 EXPECT_EQ(150.f, rect.bottom);
227 EXPECT_EQ(53.f, rect.right);
228 EXPECT_EQ(165.f, rect.top);
229
230 // Set the content of the annotation.
231 const wchar_t contents[] = L"Hello! This is a customized content.";
232 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
233 GetFPDFWideString(contents);
234 ASSERT_TRUE(
235 FPDFAnnot_SetText(annot, FPDFANNOT_TEXTTYPE_Contents, text.get()));
236 // Check that the content has been set correctly.
237 unsigned long len =
238 FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0);
239 std::vector<char> buf(len);
240 EXPECT_EQ(74u, FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents,
241 buf.data(), len));
242 EXPECT_STREQ(contents,
243 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
244 .c_str());
245
Jane Liue10509a2017-06-20 16:47:41 -0400246 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400247 UnloadPage(page);
248}
249
250TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
251 // Open a file with one annotation and load its first page.
252 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
253 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
254 ASSERT_TRUE(page);
255
256 // Check that there is a total of one annotation on its first page, and verify
257 // its quadpoints.
258 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
259 FPDF_ANNOTATION annot;
260 ASSERT_TRUE(FPDFPage_GetAnnot(page, 0, &annot));
261 FS_QUADPOINTSF quadpoints;
262 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints));
263 EXPECT_EQ(115.802643f, quadpoints.x1);
264 EXPECT_EQ(718.913940f, quadpoints.y1);
265 EXPECT_EQ(157.211182f, quadpoints.x4);
266 EXPECT_EQ(706.264465f, quadpoints.y4);
Jane Liue10509a2017-06-20 16:47:41 -0400267 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400268
269 // Add an underline annotation to the page and set its quadpoints.
270 ASSERT_TRUE(FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE, &annot));
271 quadpoints.x1 = 140.802643f;
272 quadpoints.x3 = 140.802643f;
273 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, quadpoints));
Jane Liue10509a2017-06-20 16:47:41 -0400274 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400275
276 // Save the document, closing the page and document.
277 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
278 FPDF_ClosePage(page);
279
280 // Open the saved document.
281 std::string new_file = GetString();
282 FPDF_FILEACCESS file_access;
283 memset(&file_access, 0, sizeof(file_access));
284 file_access.m_FileLen = new_file.size();
285 file_access.m_GetBlock = GetBlockFromString;
286 file_access.m_Param = &new_file;
287 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
288 ASSERT_TRUE(new_doc);
289 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
290 ASSERT_TRUE(new_page);
291
292 // Check that the saved document has 2 annotations on the first page
293 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
294
295 // Check that the second annotation is an underline annotation and verify
296 // its quadpoints.
297 FPDF_ANNOTATION new_annot;
298 ASSERT_TRUE(FPDFPage_GetAnnot(new_page, 1, &new_annot));
299 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot));
300 FS_QUADPOINTSF new_quadpoints;
301 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(new_annot, &new_quadpoints));
302 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
303 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
304 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
305 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
306
Jane Liue10509a2017-06-20 16:47:41 -0400307 FPDFPage_CloseAnnot(new_annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400308 FPDF_ClosePage(new_page);
309 FPDF_CloseDocument(new_doc);
310}