Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 1 | // 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 Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 5 | #include <memory> |
| 6 | #include <string> |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 7 | #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 Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 14 | class FPDFAnnotEmbeddertest : public EmbedderTest, public TestSaver {}; |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 15 | |
| 16 | TEST_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 | |
| 91 | UnloadPage(page); |
| 92 | } |
| 93 | |
| 94 | TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) { |
| 95 | // Open a file with three annotations and load its first page. |
| 96 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 97 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 98 | ASSERT_TRUE(page); |
| 99 | |
| 100 | // Check that there is a total of 3 annotation on its first page. |
| 101 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 102 | |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 103 | // Check that the third annotation is of type "ink". |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 104 | FPDF_ANNOTATION annot; |
| 105 | ASSERT_TRUE(FPDFPage_GetAnnot(page, 2, &annot)); |
| 106 | EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot)); |
| 107 | |
| 108 | // Check that the annotation color is blue with opacity. |
| 109 | unsigned int R; |
| 110 | unsigned int G; |
| 111 | unsigned int B; |
| 112 | unsigned int A; |
| 113 | EXPECT_TRUE( |
| 114 | FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)); |
| 115 | EXPECT_EQ(0u, R); |
| 116 | EXPECT_EQ(0u, G); |
| 117 | EXPECT_EQ(255u, B); |
| 118 | EXPECT_EQ(76u, A); |
| 119 | |
| 120 | // Check that there is no content. |
| 121 | EXPECT_EQ(2u, |
| 122 | FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0)); |
| 123 | |
| 124 | // Check that the rectange coordinates are correct. |
| 125 | // Note that upon rendering, the rectangle coordinates will be adjusted. |
| 126 | FS_RECTF rect; |
| 127 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
| 128 | EXPECT_EQ(351.820404f, rect.left); |
| 129 | EXPECT_EQ(583.830688f, rect.bottom); |
| 130 | EXPECT_EQ(475.336090f, rect.right); |
| 131 | EXPECT_EQ(681.535034f, rect.top); |
| 132 | |
| 133 | UnloadPage(page); |
| 134 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 135 | |
| 136 | TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) { |
| 137 | // Open a file with one annotation and load its first page. |
| 138 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
| 139 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 140 | ASSERT_TRUE(page); |
| 141 | |
| 142 | // Add an annotation with an illegal subtype. |
| 143 | FPDF_ANNOTATION annot; |
| 144 | ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1, &annot)); |
| 145 | |
| 146 | UnloadPage(page); |
| 147 | } |
| 148 | |
| 149 | TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) { |
| 150 | // Open a file with no annotation and load its first page. |
| 151 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 152 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 153 | ASSERT_TRUE(page); |
| 154 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 155 | |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame^] | 156 | // Add a text annotation to the page. |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 157 | FPDF_ANNOTATION annot; |
| 158 | ASSERT_TRUE(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT, &annot)); |
| 159 | |
| 160 | // Check that there is now 1 annotations on this page. |
| 161 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 162 | |
| 163 | // Check that the subtype of the annotation is correct. |
| 164 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot)); |
| 165 | ASSERT_TRUE(FPDFPage_GetAnnot(page, 0, &annot)); |
| 166 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot)); |
| 167 | |
| 168 | // Set the color of the annotation. |
| 169 | ASSERT_TRUE( |
| 170 | FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204)); |
| 171 | // Check that the color has been set correctly. |
| 172 | unsigned int R; |
| 173 | unsigned int G; |
| 174 | unsigned int B; |
| 175 | unsigned int A; |
| 176 | EXPECT_TRUE( |
| 177 | FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)); |
| 178 | EXPECT_EQ(51u, R); |
| 179 | EXPECT_EQ(102u, G); |
| 180 | EXPECT_EQ(153u, B); |
| 181 | EXPECT_EQ(204u, A); |
| 182 | |
| 183 | // Change the color of the annotation. |
| 184 | ASSERT_TRUE( |
| 185 | FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 204, 153, 102, 51)); |
| 186 | // Check that the color has been set correctly. |
| 187 | EXPECT_TRUE( |
| 188 | FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)); |
| 189 | EXPECT_EQ(204u, R); |
| 190 | EXPECT_EQ(153u, G); |
| 191 | EXPECT_EQ(102u, B); |
| 192 | EXPECT_EQ(51u, A); |
| 193 | |
| 194 | // Set the annotation rectangle. |
| 195 | FS_RECTF rect; |
| 196 | EXPECT_FALSE(FPDFAnnot_GetRect(annot, &rect)); |
| 197 | rect.left = 35; |
| 198 | rect.bottom = 150; |
| 199 | rect.right = 53; |
| 200 | rect.top = 165; |
| 201 | ASSERT_TRUE(FPDFAnnot_SetRect(annot, rect)); |
| 202 | // Check that the annotation rectangle has been set correctly. |
| 203 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
| 204 | EXPECT_EQ(35.f, rect.left); |
| 205 | EXPECT_EQ(150.f, rect.bottom); |
| 206 | EXPECT_EQ(53.f, rect.right); |
| 207 | EXPECT_EQ(165.f, rect.top); |
| 208 | |
| 209 | // Set the content of the annotation. |
| 210 | const wchar_t contents[] = L"Hello! This is a customized content."; |
| 211 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 212 | GetFPDFWideString(contents); |
| 213 | ASSERT_TRUE( |
| 214 | FPDFAnnot_SetText(annot, FPDFANNOT_TEXTTYPE_Contents, text.get())); |
| 215 | // Check that the content has been set correctly. |
| 216 | unsigned long len = |
| 217 | FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0); |
| 218 | std::vector<char> buf(len); |
| 219 | EXPECT_EQ(74u, FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, |
| 220 | buf.data(), len)); |
| 221 | EXPECT_STREQ(contents, |
| 222 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 223 | .c_str()); |
| 224 | |
| 225 | UnloadPage(page); |
| 226 | } |
| 227 | |
| 228 | TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) { |
| 229 | // Open a file with one annotation and load its first page. |
| 230 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
| 231 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 232 | ASSERT_TRUE(page); |
| 233 | |
| 234 | // Check that there is a total of one annotation on its first page, and verify |
| 235 | // its quadpoints. |
| 236 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 237 | FPDF_ANNOTATION annot; |
| 238 | ASSERT_TRUE(FPDFPage_GetAnnot(page, 0, &annot)); |
| 239 | FS_QUADPOINTSF quadpoints; |
| 240 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints)); |
| 241 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 242 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 243 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 244 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 245 | |
| 246 | // Add an underline annotation to the page and set its quadpoints. |
| 247 | ASSERT_TRUE(FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE, &annot)); |
| 248 | quadpoints.x1 = 140.802643f; |
| 249 | quadpoints.x3 = 140.802643f; |
| 250 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, quadpoints)); |
| 251 | |
| 252 | // Save the document, closing the page and document. |
| 253 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 254 | FPDF_ClosePage(page); |
| 255 | |
| 256 | // Open the saved document. |
| 257 | std::string new_file = GetString(); |
| 258 | FPDF_FILEACCESS file_access; |
| 259 | memset(&file_access, 0, sizeof(file_access)); |
| 260 | file_access.m_FileLen = new_file.size(); |
| 261 | file_access.m_GetBlock = GetBlockFromString; |
| 262 | file_access.m_Param = &new_file; |
| 263 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 264 | ASSERT_TRUE(new_doc); |
| 265 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 266 | ASSERT_TRUE(new_page); |
| 267 | |
| 268 | // Check that the saved document has 2 annotations on the first page |
| 269 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page)); |
| 270 | |
| 271 | // Check that the second annotation is an underline annotation and verify |
| 272 | // its quadpoints. |
| 273 | FPDF_ANNOTATION new_annot; |
| 274 | ASSERT_TRUE(FPDFPage_GetAnnot(new_page, 1, &new_annot)); |
| 275 | EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot)); |
| 276 | FS_QUADPOINTSF new_quadpoints; |
| 277 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(new_annot, &new_quadpoints)); |
| 278 | EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f); |
| 279 | EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f); |
| 280 | EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f); |
| 281 | EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f); |
| 282 | |
| 283 | FPDF_ClosePage(new_page); |
| 284 | FPDF_CloseDocument(new_doc); |
| 285 | } |