OLD | NEW |
1 // Copyright 2015 PDFium Authors. All rights reserved. | 1 // Copyright 2015 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "../../public/fpdf_text.h" | 5 #include "../../public/fpdf_text.h" |
6 #include "../../public/fpdfview.h" | 6 #include "../../public/fpdfview.h" |
7 #include "../../testing/embedder_test.h" | 7 #include "../../testing/embedder_test.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 18 matching lines...) Expand all Loading... |
29 }; | 29 }; |
30 | 30 |
31 TEST_F(FPDFTextEmbeddertest, Text) { | 31 TEST_F(FPDFTextEmbeddertest, Text) { |
32 EXPECT_TRUE(OpenDocument("testing/resources/hello_world.pdf")); | 32 EXPECT_TRUE(OpenDocument("testing/resources/hello_world.pdf")); |
33 FPDF_PAGE page = LoadPage(0); | 33 FPDF_PAGE page = LoadPage(0); |
34 EXPECT_NE(nullptr, page); | 34 EXPECT_NE(nullptr, page); |
35 | 35 |
36 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); | 36 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); |
37 EXPECT_NE(nullptr, textpage); | 37 EXPECT_NE(nullptr, textpage); |
38 | 38 |
39 const char expected[] = "Hello, world!\r\nGoodbye, world!"; | 39 static const char expected[] = "Hello, world!\r\nGoodbye, world!"; |
40 unsigned short fixed_buffer[128]; | 40 unsigned short fixed_buffer[128]; |
41 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 41 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
42 | 42 |
43 // Check includes the terminating NUL that is provided. | 43 // Check includes the terminating NUL that is provided. |
44 EXPECT_EQ(sizeof(expected), FPDFText_GetText(textpage, 0, 128, fixed_buffer)); | 44 EXPECT_EQ(sizeof(expected), FPDFText_GetText(textpage, 0, 128, fixed_buffer)); |
45 EXPECT_TRUE(check_unsigned_shorts(expected, fixed_buffer, sizeof(expected))); | 45 EXPECT_TRUE(check_unsigned_shorts(expected, fixed_buffer, sizeof(expected))); |
46 | 46 |
47 // Count does not include the terminating NUL in the string literal. | 47 // Count does not include the terminating NUL in the string literal. |
48 EXPECT_EQ(sizeof(expected) - 1, FPDFText_CountChars(textpage)); | 48 EXPECT_EQ(sizeof(expected) - 1, FPDFText_CountChars(textpage)); |
49 for (size_t i = 0; i < sizeof(expected) - 1; ++i) { | 49 for (size_t i = 0; i < sizeof(expected) - 1; ++i) { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 EXPECT_EQ(0xbdbd, fixed_buffer[9]); | 127 EXPECT_EQ(0xbdbd, fixed_buffer[9]); |
128 | 128 |
129 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 129 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
130 EXPECT_EQ(10, FPDFText_GetBoundedText( | 130 EXPECT_EQ(10, FPDFText_GetBoundedText( |
131 textpage, 41.0, 56.0, 82.0, 48.0, fixed_buffer, 128)); | 131 textpage, 41.0, 56.0, 82.0, 48.0, fixed_buffer, 128)); |
132 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9)); | 132 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9)); |
133 EXPECT_EQ(0u, fixed_buffer[9]); | 133 EXPECT_EQ(0u, fixed_buffer[9]); |
134 EXPECT_EQ(0xbdbd, fixed_buffer[10]); | 134 EXPECT_EQ(0xbdbd, fixed_buffer[10]); |
135 | 135 |
136 FPDFText_ClosePage(textpage); | 136 FPDFText_ClosePage(textpage); |
| 137 UnloadPage(page); |
137 } | 138 } |
138 | 139 |
139 TEST_F(FPDFTextEmbeddertest, TextSearch) { | 140 TEST_F(FPDFTextEmbeddertest, TextSearch) { |
140 EXPECT_TRUE(OpenDocument("testing/resources/hello_world.pdf")); | 141 EXPECT_TRUE(OpenDocument("testing/resources/hello_world.pdf")); |
141 FPDF_PAGE page = LoadPage(0); | 142 FPDF_PAGE page = LoadPage(0); |
142 EXPECT_NE(nullptr, page); | 143 EXPECT_NE(nullptr, page); |
143 | 144 |
144 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); | 145 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); |
145 EXPECT_NE(nullptr, textpage); | 146 EXPECT_NE(nullptr, textpage); |
146 | 147 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 EXPECT_EQ(4, FPDFText_GetSchCount(search)); | 233 EXPECT_EQ(4, FPDFText_GetSchCount(search)); |
233 FPDFText_FindClose(search); | 234 FPDFText_FindClose(search); |
234 | 235 |
235 // But can be made to mach word boundaries, in which case this fails. | 236 // But can be made to mach word boundaries, in which case this fails. |
236 search = FPDFText_FindStart(textpage, world_substr, FPDF_MATCHWHOLEWORD, 0); | 237 search = FPDFText_FindStart(textpage, world_substr, FPDF_MATCHWHOLEWORD, 0); |
237 EXPECT_FALSE(FPDFText_FindNext(search)); | 238 EXPECT_FALSE(FPDFText_FindNext(search)); |
238 // TODO(tsepez): investigate strange index/count values in this state. | 239 // TODO(tsepez): investigate strange index/count values in this state. |
239 FPDFText_FindClose(search); | 240 FPDFText_FindClose(search); |
240 | 241 |
241 FPDFText_ClosePage(textpage); | 242 FPDFText_ClosePage(textpage); |
| 243 UnloadPage(page); |
242 } | 244 } |
243 | 245 |
244 // Test that the page has characters despite a bad stream length. | 246 // Test that the page has characters despite a bad stream length. |
245 TEST_F(FPDFTextEmbeddertest, StreamLengthPastEndOfFile) { | 247 TEST_F(FPDFTextEmbeddertest, StreamLengthPastEndOfFile) { |
246 EXPECT_TRUE(OpenDocument("testing/resources/bug_57.pdf")); | 248 EXPECT_TRUE(OpenDocument("testing/resources/bug_57.pdf")); |
247 FPDF_PAGE page = LoadPage(0); | 249 FPDF_PAGE page = LoadPage(0); |
248 EXPECT_NE(nullptr, page); | 250 EXPECT_NE(nullptr, page); |
249 | 251 |
250 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); | 252 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); |
251 EXPECT_NE(nullptr, textpage); | 253 EXPECT_NE(nullptr, textpage); |
252 EXPECT_EQ(13, FPDFText_CountChars(textpage)); | 254 EXPECT_EQ(13, FPDFText_CountChars(textpage)); |
253 | 255 |
254 FPDFText_ClosePage(textpage); | 256 FPDFText_ClosePage(textpage); |
| 257 UnloadPage(page); |
255 } | 258 } |
256 | 259 |
257 TEST_F(FPDFTextEmbeddertest, WebLinks) { | 260 TEST_F(FPDFTextEmbeddertest, WebLinks) { |
258 EXPECT_TRUE(OpenDocument("testing/resources/weblinks.pdf")); | 261 EXPECT_TRUE(OpenDocument("testing/resources/weblinks.pdf")); |
259 FPDF_PAGE page = LoadPage(0); | 262 FPDF_PAGE page = LoadPage(0); |
260 EXPECT_NE(nullptr, page); | 263 EXPECT_NE(nullptr, page); |
261 | 264 |
262 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); | 265 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); |
263 EXPECT_NE(nullptr, textpage); | 266 EXPECT_NE(nullptr, textpage); |
264 | 267 |
265 FPDF_PAGELINK pagelink = FPDFLink_LoadWebLinks(textpage); | 268 FPDF_PAGELINK pagelink = FPDFLink_LoadWebLinks(textpage); |
266 EXPECT_NE(nullptr, pagelink); | 269 EXPECT_NE(nullptr, pagelink); |
267 | 270 |
268 // Page contains two HTTP-style URLs. | 271 // Page contains two HTTP-style URLs. |
269 EXPECT_EQ(2, FPDFLink_CountWebLinks(pagelink)); | 272 EXPECT_EQ(2, FPDFLink_CountWebLinks(pagelink)); |
270 | 273 |
271 // Only a terminating NUL required for bogus links. | 274 // Only a terminating NUL required for bogus links. |
272 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 2, nullptr, 0)); | 275 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 2, nullptr, 0)); |
273 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 1400, nullptr, 0)); | 276 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 1400, nullptr, 0)); |
274 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, -1, nullptr, 0)); | 277 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, -1, nullptr, 0)); |
275 | 278 |
276 // Query the number of characters required for each link (incl NUL). | 279 // Query the number of characters required for each link (incl NUL). |
277 EXPECT_EQ(25, FPDFLink_GetURL(pagelink, 0, nullptr, 0)); | 280 EXPECT_EQ(25, FPDFLink_GetURL(pagelink, 0, nullptr, 0)); |
278 EXPECT_EQ(26, FPDFLink_GetURL(pagelink, 1, nullptr, 0)); | 281 EXPECT_EQ(26, FPDFLink_GetURL(pagelink, 1, nullptr, 0)); |
279 | 282 |
280 const char expected_url[] = "http://example.com?q=foo"; | 283 static const char expected_url[] = "http://example.com?q=foo"; |
281 unsigned short fixed_buffer[128]; | 284 unsigned short fixed_buffer[128]; |
282 | 285 |
283 // Retrieve a link with too small a buffer. Buffer will not be | 286 // Retrieve a link with too small a buffer. Buffer will not be |
284 // NUL-terminated, but must not be modified past indicated length, | 287 // NUL-terminated, but must not be modified past indicated length, |
285 // so pre-fill with a pattern to check write bounds. | 288 // so pre-fill with a pattern to check write bounds. |
286 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 289 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
287 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 0, fixed_buffer, 1)); | 290 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 0, fixed_buffer, 1)); |
288 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, 1)); | 291 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, 1)); |
289 EXPECT_EQ(0xbdbd, fixed_buffer[1]); | 292 EXPECT_EQ(0xbdbd, fixed_buffer[1]); |
290 | 293 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 top = -2.0; | 354 top = -2.0; |
352 bottom = -2.0; | 355 bottom = -2.0; |
353 FPDFLink_GetRect(pagelink, -1, 0, &left, &top, &right, &bottom); | 356 FPDFLink_GetRect(pagelink, -1, 0, &left, &top, &right, &bottom); |
354 EXPECT_EQ(-2.0, left); | 357 EXPECT_EQ(-2.0, left); |
355 EXPECT_EQ(-2.0, right); | 358 EXPECT_EQ(-2.0, right); |
356 EXPECT_EQ(-2.0, bottom); | 359 EXPECT_EQ(-2.0, bottom); |
357 EXPECT_EQ(-2.0, top); | 360 EXPECT_EQ(-2.0, top); |
358 | 361 |
359 FPDFLink_CloseWebLinks(pagelink); | 362 FPDFLink_CloseWebLinks(pagelink); |
360 FPDFText_ClosePage(textpage); | 363 FPDFText_ClosePage(textpage); |
| 364 UnloadPage(page); |
361 } | 365 } |
OLD | NEW |