blob: 85f9b682404d244b2943daaa02889d75e2a31c2f [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
16TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
17 // Open a file with one annotation and load its first page.
18 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
19 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
20 ASSERT_TRUE(page);
21
22 // Check that there is a total of 1 annotation on its first page.
23 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
24
25 // Check that the annotation is of type "highlight".
26 FPDF_ANNOTATION annot;
27 ASSERT_TRUE(FPDFPage_GetAnnot(page, 0, &annot));
28 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
29
30 // Check that the annotation color is yellow.
31 unsigned int R;
32 unsigned int G;
33 unsigned int B;
34 unsigned int A;
35 EXPECT_TRUE(
36 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
37 EXPECT_EQ(255u, R);
38 EXPECT_EQ(255u, G);
39 EXPECT_EQ(0u, B);
40 EXPECT_EQ(255u, A);
41
42 // Check that the author is correct.
43 unsigned long len =
44 FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Author, nullptr, 0);
45 std::vector<char> buf(len);
46 EXPECT_EQ(28u, FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Author, buf.data(),
47 len));
48 EXPECT_STREQ(L"Jae Hyun Park",
49 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
50 .c_str());
51
52 // Check that the content is correct.
53 len = FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0);
54 buf.clear();
55 buf.resize(len);
56 EXPECT_EQ(2690u, FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents,
57 buf.data(), len));
58 const wchar_t contents[] =
59 L"This is a note for that highlight annotation. Very long highlight "
60 "annotation. Long long long Long long longLong long longLong long "
61 "longLong long longLong long longLong long longLong long longLong long "
62 "longLong long longLong long longLong long longLong long longLong long "
63 "longLong long longLong long longLong long longLong long longLong long "
64 "longLong long longLong long longLong long longLong long longLong long "
65 "longLong long longLong long longLong long longLong long longLong long "
66 "longLong long longLong long longLong long longLong long longLong long "
67 "longLong long longLong long longLong long longLong long longLong long "
68 "longLong long longLong long longLong long longLong long longLong long "
69 "longLong long longLong long longLong long longLong long longLong long "
70 "longLong long longLong long longLong long longLong long longLong long "
71 "longLong long longLong long longLong long longLong long longLong long "
72 "longLong long longLong long longLong long longLong long longLong long "
73 "longLong long longLong long longLong long longLong long longLong long "
74 "longLong long longLong long longLong long longLong long longLong long "
75 "longLong long longLong long longLong long longLong long longLong long "
76 "longLong long longLong long longLong long longLong long longLong long "
77 "longLong long longLong long longLong long longLong long longLong long "
78 "longLong long long. END";
79 EXPECT_STREQ(contents,
80 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
81 .c_str());
82
83 // Check that the quadpoints are correct.
84 FS_QUADPOINTSF quadpoints;
85 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints));
86 EXPECT_EQ(115.802643f, quadpoints.x1);
87 EXPECT_EQ(718.913940f, quadpoints.y1);
88 EXPECT_EQ(157.211182f, quadpoints.x4);
89 EXPECT_EQ(706.264465f, quadpoints.y4);
90
Jane Liue10509a2017-06-20 16:47:41 -040091 FPDFPage_CloseAnnot(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -040092 UnloadPage(page);
93}
94
95TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
96 // Open a file with three annotations and load its first page.
97 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
98 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
99 ASSERT_TRUE(page);
100
101 // Check that there is a total of 3 annotation on its first page.
102 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
103
Jane Liu20eafda2017-06-07 10:33:24 -0400104 // Check that the third annotation is of type "ink".
Jane Liu4fd9a472017-06-01 18:56:09 -0400105 FPDF_ANNOTATION annot;
106 ASSERT_TRUE(FPDFPage_GetAnnot(page, 2, &annot));
107 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot));
108
109 // Check that the annotation color is blue with opacity.
110 unsigned int R;
111 unsigned int G;
112 unsigned int B;
113 unsigned int A;
114 EXPECT_TRUE(
115 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
116 EXPECT_EQ(0u, R);
117 EXPECT_EQ(0u, G);
118 EXPECT_EQ(255u, B);
119 EXPECT_EQ(76u, A);
120
121 // Check that there is no content.
122 EXPECT_EQ(2u,
123 FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0));
124
125 // Check that the rectange coordinates are correct.
126 // Note that upon rendering, the rectangle coordinates will be adjusted.
127 FS_RECTF rect;
128 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
129 EXPECT_EQ(351.820404f, rect.left);
130 EXPECT_EQ(583.830688f, rect.bottom);
131 EXPECT_EQ(475.336090f, rect.right);
132 EXPECT_EQ(681.535034f, rect.top);
133
Jane Liue10509a2017-06-20 16:47:41 -0400134 FPDFPage_CloseAnnot(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400135 UnloadPage(page);
136}
Jane Liu20eafda2017-06-07 10:33:24 -0400137
138TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
139 // Open a file with one annotation and load its first page.
140 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
141 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
142 ASSERT_TRUE(page);
143
144 // Add an annotation with an illegal subtype.
145 FPDF_ANNOTATION annot;
146 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1, &annot));
147
148 UnloadPage(page);
149}
150
Jane Liud321ef92017-06-14 09:56:22 -0400151TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400152 // Open a file with no annotation and load its first page.
153 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
154 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
155 ASSERT_TRUE(page);
156 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
157
Jane Liueda65252017-06-07 11:31:27 -0400158 // Add a text annotation to the page.
Jane Liu20eafda2017-06-07 10:33:24 -0400159 FPDF_ANNOTATION annot;
160 ASSERT_TRUE(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT, &annot));
161
162 // Check that there is now 1 annotations on this page.
163 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
164
165 // Check that the subtype of the annotation is correct.
166 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot));
Jane Liue10509a2017-06-20 16:47:41 -0400167 FPDFPage_CloseAnnot(annot);
168
Jane Liu20eafda2017-06-07 10:33:24 -0400169 ASSERT_TRUE(FPDFPage_GetAnnot(page, 0, &annot));
170 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot));
171
172 // Set the color of the annotation.
173 ASSERT_TRUE(
174 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204));
175 // Check that the color has been set correctly.
176 unsigned int R;
177 unsigned int G;
178 unsigned int B;
179 unsigned int A;
180 EXPECT_TRUE(
181 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
182 EXPECT_EQ(51u, R);
183 EXPECT_EQ(102u, G);
184 EXPECT_EQ(153u, B);
185 EXPECT_EQ(204u, A);
186
187 // Change the color of the annotation.
188 ASSERT_TRUE(
189 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 204, 153, 102, 51));
190 // Check that the color has been set correctly.
191 EXPECT_TRUE(
192 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
193 EXPECT_EQ(204u, R);
194 EXPECT_EQ(153u, G);
195 EXPECT_EQ(102u, B);
196 EXPECT_EQ(51u, A);
197
198 // Set the annotation rectangle.
199 FS_RECTF rect;
200 EXPECT_FALSE(FPDFAnnot_GetRect(annot, &rect));
201 rect.left = 35;
202 rect.bottom = 150;
203 rect.right = 53;
204 rect.top = 165;
205 ASSERT_TRUE(FPDFAnnot_SetRect(annot, rect));
206 // Check that the annotation rectangle has been set correctly.
207 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
208 EXPECT_EQ(35.f, rect.left);
209 EXPECT_EQ(150.f, rect.bottom);
210 EXPECT_EQ(53.f, rect.right);
211 EXPECT_EQ(165.f, rect.top);
212
213 // Set the content of the annotation.
214 const wchar_t contents[] = L"Hello! This is a customized content.";
215 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
216 GetFPDFWideString(contents);
217 ASSERT_TRUE(
218 FPDFAnnot_SetText(annot, FPDFANNOT_TEXTTYPE_Contents, text.get()));
219 // Check that the content has been set correctly.
220 unsigned long len =
221 FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0);
222 std::vector<char> buf(len);
223 EXPECT_EQ(74u, FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents,
224 buf.data(), len));
225 EXPECT_STREQ(contents,
226 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
227 .c_str());
228
Jane Liue10509a2017-06-20 16:47:41 -0400229 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400230 UnloadPage(page);
231}
232
233TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
234 // Open a file with one annotation and load its first page.
235 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
236 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
237 ASSERT_TRUE(page);
238
239 // Check that there is a total of one annotation on its first page, and verify
240 // its quadpoints.
241 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
242 FPDF_ANNOTATION annot;
243 ASSERT_TRUE(FPDFPage_GetAnnot(page, 0, &annot));
244 FS_QUADPOINTSF quadpoints;
245 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints));
246 EXPECT_EQ(115.802643f, quadpoints.x1);
247 EXPECT_EQ(718.913940f, quadpoints.y1);
248 EXPECT_EQ(157.211182f, quadpoints.x4);
249 EXPECT_EQ(706.264465f, quadpoints.y4);
Jane Liue10509a2017-06-20 16:47:41 -0400250 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400251
252 // Add an underline annotation to the page and set its quadpoints.
253 ASSERT_TRUE(FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE, &annot));
254 quadpoints.x1 = 140.802643f;
255 quadpoints.x3 = 140.802643f;
256 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, quadpoints));
Jane Liue10509a2017-06-20 16:47:41 -0400257 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400258
259 // Save the document, closing the page and document.
260 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
261 FPDF_ClosePage(page);
262
263 // Open the saved document.
264 std::string new_file = GetString();
265 FPDF_FILEACCESS file_access;
266 memset(&file_access, 0, sizeof(file_access));
267 file_access.m_FileLen = new_file.size();
268 file_access.m_GetBlock = GetBlockFromString;
269 file_access.m_Param = &new_file;
270 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
271 ASSERT_TRUE(new_doc);
272 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
273 ASSERT_TRUE(new_page);
274
275 // Check that the saved document has 2 annotations on the first page
276 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
277
278 // Check that the second annotation is an underline annotation and verify
279 // its quadpoints.
280 FPDF_ANNOTATION new_annot;
281 ASSERT_TRUE(FPDFPage_GetAnnot(new_page, 1, &new_annot));
282 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot));
283 FS_QUADPOINTSF new_quadpoints;
284 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(new_annot, &new_quadpoints));
285 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
286 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
287 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
288 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
289
Jane Liue10509a2017-06-20 16:47:41 -0400290 FPDFPage_CloseAnnot(new_annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400291 FPDF_ClosePage(new_page);
292 FPDF_CloseDocument(new_doc);
293}