| 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 24 matching lines...) Expand all Loading... |
| 35 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); | 35 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); |
| 36 EXPECT_NE(nullptr, textpage); | 36 EXPECT_NE(nullptr, textpage); |
| 37 | 37 |
| 38 static const char expected[] = "Hello, world!\r\nGoodbye, world!"; | 38 static const char expected[] = "Hello, world!\r\nGoodbye, world!"; |
| 39 unsigned short fixed_buffer[128]; | 39 unsigned short fixed_buffer[128]; |
| 40 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 40 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 41 | 41 |
| 42 // Check includes the terminating NUL that is provided. | 42 // Check includes the terminating NUL that is provided. |
| 43 int num_chars = FPDFText_GetText(textpage, 0, 128, fixed_buffer); | 43 int num_chars = FPDFText_GetText(textpage, 0, 128, fixed_buffer); |
| 44 ASSERT_GE(num_chars, 0); | 44 ASSERT_GE(num_chars, 0); |
| 45 EXPECT_EQ(sizeof(expected) - 1, static_cast<size_t>(num_chars)); | 45 EXPECT_EQ(sizeof(expected), static_cast<size_t>(num_chars)); |
| 46 EXPECT_TRUE( | 46 EXPECT_TRUE(check_unsigned_shorts(expected, fixed_buffer, sizeof(expected))); |
| 47 check_unsigned_shorts(expected, fixed_buffer, sizeof(expected) - 1)); | |
| 48 | 47 |
| 49 // Count does not include the terminating NUL in the string literal. | 48 // Count does not include the terminating NUL in the string literal. |
| 50 EXPECT_EQ(sizeof(expected) - 1, FPDFText_CountChars(textpage)); | 49 EXPECT_EQ(sizeof(expected) - 1, FPDFText_CountChars(textpage)); |
| 51 for (size_t i = 0; i < sizeof(expected) - 1; ++i) { | 50 for (size_t i = 0; i < sizeof(expected) - 1; ++i) { |
| 52 EXPECT_EQ(static_cast<unsigned int>(expected[i]), | 51 EXPECT_EQ(static_cast<unsigned int>(expected[i]), |
| 53 FPDFText_GetUnicode(textpage, i)) | 52 FPDFText_GetUnicode(textpage, i)) |
| 54 << " at " << i; | 53 << " at " << i; |
| 55 } | 54 } |
| 56 | 55 |
| 57 EXPECT_EQ(12.0, FPDFText_GetFontSize(textpage, 0)); | 56 EXPECT_EQ(12.0, FPDFText_GetFontSize(textpage, 0)); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 1)); | 118 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 1)); |
| 120 EXPECT_EQ(0xbdbd, fixed_buffer[1]); | 119 EXPECT_EQ(0xbdbd, fixed_buffer[1]); |
| 121 | 120 |
| 122 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 121 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 123 EXPECT_EQ(9, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0, | 122 EXPECT_EQ(9, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0, |
| 124 fixed_buffer, 9)); | 123 fixed_buffer, 9)); |
| 125 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9)); | 124 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9)); |
| 126 EXPECT_EQ(0xbdbd, fixed_buffer[9]); | 125 EXPECT_EQ(0xbdbd, fixed_buffer[9]); |
| 127 | 126 |
| 128 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 127 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 129 EXPECT_EQ(9, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0, | 128 EXPECT_EQ(10, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0, |
| 130 fixed_buffer, 128)); | 129 fixed_buffer, 128)); |
| 131 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9)); | 130 EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9)); |
| 132 EXPECT_EQ(0xbdbd, fixed_buffer[9]); | 131 EXPECT_EQ(0u, fixed_buffer[9]); |
| 132 EXPECT_EQ(0xbdbd, fixed_buffer[10]); |
| 133 | 133 |
| 134 FPDFText_ClosePage(textpage); | 134 FPDFText_ClosePage(textpage); |
| 135 UnloadPage(page); | 135 UnloadPage(page); |
| 136 } | 136 } |
| 137 | 137 |
| 138 TEST_F(FPDFTextEmbeddertest, TextSearch) { | 138 TEST_F(FPDFTextEmbeddertest, TextSearch) { |
| 139 EXPECT_TRUE(OpenDocument("hello_world.pdf")); | 139 EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 140 FPDF_PAGE page = LoadPage(0); | 140 FPDF_PAGE page = LoadPage(0); |
| 141 EXPECT_NE(nullptr, page); | 141 EXPECT_NE(nullptr, page); |
| 142 | 142 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 | 262 |
| 263 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); | 263 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); |
| 264 EXPECT_NE(nullptr, textpage); | 264 EXPECT_NE(nullptr, textpage); |
| 265 | 265 |
| 266 FPDF_PAGELINK pagelink = FPDFLink_LoadWebLinks(textpage); | 266 FPDF_PAGELINK pagelink = FPDFLink_LoadWebLinks(textpage); |
| 267 EXPECT_NE(nullptr, pagelink); | 267 EXPECT_NE(nullptr, pagelink); |
| 268 | 268 |
| 269 // Page contains two HTTP-style URLs. | 269 // Page contains two HTTP-style URLs. |
| 270 EXPECT_EQ(2, FPDFLink_CountWebLinks(pagelink)); | 270 EXPECT_EQ(2, FPDFLink_CountWebLinks(pagelink)); |
| 271 | 271 |
| 272 EXPECT_EQ(0, FPDFLink_GetURL(pagelink, 2, nullptr, 0)); | 272 // Only a terminating NUL required for bogus links. |
| 273 EXPECT_EQ(0, FPDFLink_GetURL(pagelink, 1400, nullptr, 0)); | 273 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 2, nullptr, 0)); |
| 274 EXPECT_EQ(0, FPDFLink_GetURL(pagelink, -1, nullptr, 0)); | 274 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 1400, nullptr, 0)); |
| 275 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, -1, nullptr, 0)); |
| 275 | 276 |
| 276 // Query the number of characters required for each link (incl NUL). | 277 // Query the number of characters required for each link (incl NUL). |
| 277 EXPECT_EQ(24, FPDFLink_GetURL(pagelink, 0, nullptr, 0)); | 278 EXPECT_EQ(25, FPDFLink_GetURL(pagelink, 0, nullptr, 0)); |
| 278 EXPECT_EQ(25, FPDFLink_GetURL(pagelink, 1, nullptr, 0)); | 279 EXPECT_EQ(26, FPDFLink_GetURL(pagelink, 1, nullptr, 0)); |
| 279 | 280 |
| 280 static const char expected_url[] = "http://example.com?q=foo"; | 281 static const char expected_url[] = "http://example.com?q=foo"; |
| 281 unsigned short fixed_buffer[128]; | 282 unsigned short fixed_buffer[128]; |
| 282 | 283 |
| 283 // Retrieve a link with too small a buffer. Buffer will not be | 284 // Retrieve a link with too small a buffer. Buffer will not be |
| 284 // NUL-terminated, but must not be modified past indicated length, | 285 // NUL-terminated, but must not be modified past indicated length, |
| 285 // so pre-fill with a pattern to check write bounds. | 286 // so pre-fill with a pattern to check write bounds. |
| 286 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 287 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 287 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 0, fixed_buffer, 1)); | 288 EXPECT_EQ(1, FPDFLink_GetURL(pagelink, 0, fixed_buffer, 1)); |
| 288 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, 1)); | 289 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, 1)); |
| 289 EXPECT_EQ(0xbdbd, fixed_buffer[1]); | 290 EXPECT_EQ(0xbdbd, fixed_buffer[1]); |
| 290 | 291 |
| 291 // Check buffer that doesn't have space for a terminating NUL. | 292 // Check buffer that doesn't have space for a terminating NUL. |
| 292 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 293 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 293 EXPECT_EQ( | 294 EXPECT_EQ( |
| 294 sizeof(expected_url) - 1, | 295 sizeof(expected_url) - 1, |
| 295 FPDFLink_GetURL(pagelink, 0, fixed_buffer, sizeof(expected_url) - 1)); | 296 FPDFLink_GetURL(pagelink, 0, fixed_buffer, sizeof(expected_url) - 1)); |
| 296 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, | 297 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, |
| 297 sizeof(expected_url) - 1)); | 298 sizeof(expected_url) - 1)); |
| 298 EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url) - 1]); | 299 EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url) - 1]); |
| 299 | 300 |
| 300 // Retreive link with exactly-sized buffer. | 301 // Retreive link with exactly-sized buffer. |
| 301 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 302 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 302 EXPECT_EQ(sizeof(expected_url) - 1, | 303 EXPECT_EQ(sizeof(expected_url), |
| 303 FPDFLink_GetURL(pagelink, 0, fixed_buffer, sizeof(expected_url))); | 304 FPDFLink_GetURL(pagelink, 0, fixed_buffer, sizeof(expected_url))); |
| 304 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, | 305 EXPECT_TRUE( |
| 305 sizeof(expected_url) - 1)); | 306 check_unsigned_shorts(expected_url, fixed_buffer, sizeof(expected_url))); |
| 306 EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url) - 1]); | 307 EXPECT_EQ(0u, fixed_buffer[sizeof(expected_url) - 1]); |
| 308 EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url)]); |
| 307 | 309 |
| 308 // Retreive link with ample-sized-buffer. | 310 // Retreive link with ample-sized-buffer. |
| 309 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); | 311 memset(fixed_buffer, 0xbd, sizeof(fixed_buffer)); |
| 310 EXPECT_EQ(sizeof(expected_url) - 1, | 312 EXPECT_EQ(sizeof(expected_url), |
| 311 FPDFLink_GetURL(pagelink, 0, fixed_buffer, 128)); | 313 FPDFLink_GetURL(pagelink, 0, fixed_buffer, 128)); |
| 312 EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer, | 314 EXPECT_TRUE( |
| 313 sizeof(expected_url) - 1)); | 315 check_unsigned_shorts(expected_url, fixed_buffer, sizeof(expected_url))); |
| 314 EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url) - 1]); | 316 EXPECT_EQ(0u, fixed_buffer[sizeof(expected_url) - 1]); |
| 317 EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url)]); |
| 315 | 318 |
| 316 // Each link rendered in a single rect in this test page. | 319 // Each link rendered in a single rect in this test page. |
| 317 EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 0)); | 320 EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 0)); |
| 318 EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 1)); | 321 EXPECT_EQ(1, FPDFLink_CountRects(pagelink, 1)); |
| 319 | 322 |
| 320 // Each link rendered in a single rect in this test page. | 323 // Each link rendered in a single rect in this test page. |
| 321 EXPECT_EQ(0, FPDFLink_CountRects(pagelink, -1)); | 324 EXPECT_EQ(0, FPDFLink_CountRects(pagelink, -1)); |
| 322 EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 2)); | 325 EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 2)); |
| 323 EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 10000)); | 326 EXPECT_EQ(0, FPDFLink_CountRects(pagelink, 10000)); |
| 324 | 327 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 352 FPDFLink_GetRect(pagelink, -1, 0, &left, &top, &right, &bottom); | 355 FPDFLink_GetRect(pagelink, -1, 0, &left, &top, &right, &bottom); |
| 353 EXPECT_EQ(-2.0, left); | 356 EXPECT_EQ(-2.0, left); |
| 354 EXPECT_EQ(-2.0, right); | 357 EXPECT_EQ(-2.0, right); |
| 355 EXPECT_EQ(-2.0, bottom); | 358 EXPECT_EQ(-2.0, bottom); |
| 356 EXPECT_EQ(-2.0, top); | 359 EXPECT_EQ(-2.0, top); |
| 357 | 360 |
| 358 FPDFLink_CloseWebLinks(pagelink); | 361 FPDFLink_CloseWebLinks(pagelink); |
| 359 FPDFText_ClosePage(textpage); | 362 FPDFText_ClosePage(textpage); |
| 360 UnloadPage(page); | 363 UnloadPage(page); |
| 361 } | 364 } |
| OLD | NEW |