blob: 27a76eecfa0b088044ce1fae77e15961352ac6ee [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
5#include <vector>
6
7#include "public/fpdf_annot.h"
8#include "public/fpdfview.h"
9#include "testing/embedder_test.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12class FPDFAnnotEmbeddertest : public EmbedderTest {};
13
14TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
15 // Open a file with one annotation and load its first page.
16 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
17 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
18 ASSERT_TRUE(page);
19
20 // Check that there is a total of 1 annotation on its first page.
21 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
22
23 // Check that the annotation is of type "highlight".
24 FPDF_ANNOTATION annot;
25 ASSERT_TRUE(FPDFPage_GetAnnot(page, 0, &annot));
26 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
27
28 // Check that the annotation color is yellow.
29 unsigned int R;
30 unsigned int G;
31 unsigned int B;
32 unsigned int A;
33 EXPECT_TRUE(
34 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
35 EXPECT_EQ(255u, R);
36 EXPECT_EQ(255u, G);
37 EXPECT_EQ(0u, B);
38 EXPECT_EQ(255u, A);
39
40 // Check that the author is correct.
41 unsigned long len =
42 FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Author, nullptr, 0);
43 std::vector<char> buf(len);
44 EXPECT_EQ(28u, FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Author, buf.data(),
45 len));
46 EXPECT_STREQ(L"Jae Hyun Park",
47 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
48 .c_str());
49
50 // Check that the content is correct.
51 len = FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0);
52 buf.clear();
53 buf.resize(len);
54 EXPECT_EQ(2690u, FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents,
55 buf.data(), len));
56 const wchar_t contents[] =
57 L"This is a note for that highlight annotation. Very long highlight "
58 "annotation. Long long long Long long longLong long longLong long "
59 "longLong long longLong long longLong long longLong long longLong long "
60 "longLong long longLong long longLong 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 long. END";
77 EXPECT_STREQ(contents,
78 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
79 .c_str());
80
81 // Check that the quadpoints are correct.
82 FS_QUADPOINTSF quadpoints;
83 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints));
84 EXPECT_EQ(115.802643f, quadpoints.x1);
85 EXPECT_EQ(718.913940f, quadpoints.y1);
86 EXPECT_EQ(157.211182f, quadpoints.x4);
87 EXPECT_EQ(706.264465f, quadpoints.y4);
88
89 UnloadPage(page);
90}
91
92TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
93 // Open a file with three annotations and load its first page.
94 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
95 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
96 ASSERT_TRUE(page);
97
98 // Check that there is a total of 3 annotation on its first page.
99 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
100
101 // Check that the third annotation of type "ink".
102 FPDF_ANNOTATION annot;
103 ASSERT_TRUE(FPDFPage_GetAnnot(page, 2, &annot));
104 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot));
105
106 // Check that the annotation color is blue with opacity.
107 unsigned int R;
108 unsigned int G;
109 unsigned int B;
110 unsigned int A;
111 EXPECT_TRUE(
112 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
113 EXPECT_EQ(0u, R);
114 EXPECT_EQ(0u, G);
115 EXPECT_EQ(255u, B);
116 EXPECT_EQ(76u, A);
117
118 // Check that there is no content.
119 EXPECT_EQ(2u,
120 FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0));
121
122 // Check that the rectange coordinates are correct.
123 // Note that upon rendering, the rectangle coordinates will be adjusted.
124 FS_RECTF rect;
125 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
126 EXPECT_EQ(351.820404f, rect.left);
127 EXPECT_EQ(583.830688f, rect.bottom);
128 EXPECT_EQ(475.336090f, rect.right);
129 EXPECT_EQ(681.535034f, rect.top);
130
131 UnloadPage(page);
132}