CPDFDoc_Environment::GetAnnotHandlerMgr() never returns nullptr.
[pdfium.git] / fpdfsdk / src / fpdftext_embeddertest.cpp
index f0d9feb..b3fe9e4 100644 (file)
@@ -2,9 +2,9 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include "../../public/fpdf_text.h"
+#include "../../public/fpdfview.h"
 #include "../../testing/embedder_test.h"
-#include "../../fpdfsdk/include/fpdfview.h"
-#include "../../fpdfsdk/include/fpdftext.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
 namespace {
@@ -25,8 +25,7 @@ static bool check_unsigned_shorts(const char* expected,
 
 }  // namespace
 
-class FPDFTextEmbeddertest : public EmbedderTest {
-};
+class FPDFTextEmbeddertest : public EmbedderTest {};
 
 TEST_F(FPDFTextEmbeddertest, Text) {
   EXPECT_TRUE(OpenDocument("testing/resources/hello_world.pdf"));
@@ -36,18 +35,22 @@ TEST_F(FPDFTextEmbeddertest, Text) {
   FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
   EXPECT_NE(nullptr, textpage);
 
-  const char expected[] = "Hello, world!\r\nGoodbye, world!";
+  static const char expected[] = "Hello, world!\r\nGoodbye, world!";
   unsigned short fixed_buffer[128];
   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
 
   // Check includes the terminating NUL that is provided.
-  EXPECT_EQ(sizeof(expected), FPDFText_GetText(textpage, 0, 128, fixed_buffer));
+  int num_chars = FPDFText_GetText(textpage, 0, 128, fixed_buffer);
+  ASSERT_GE(num_chars, 0);
+  EXPECT_EQ(sizeof(expected), static_cast<size_t>(num_chars));
   EXPECT_TRUE(check_unsigned_shorts(expected, fixed_buffer, sizeof(expected)));
 
   // Count does not include the terminating NUL in the string literal.
   EXPECT_EQ(sizeof(expected) - 1, FPDFText_CountChars(textpage));
   for (size_t i = 0; i < sizeof(expected) - 1; ++i) {
-    EXPECT_EQ(expected[i], FPDFText_GetUnicode(textpage, i)) << " at " << i;
+    EXPECT_EQ(static_cast<unsigned int>(expected[i]),
+              FPDFText_GetUnicode(textpage, i))
+        << " at " << i;
   }
 
   EXPECT_EQ(12.0, FPDFText_GetFontSize(textpage, 0));
@@ -63,18 +66,14 @@ TEST_F(FPDFTextEmbeddertest, Text) {
   EXPECT_NEAR(49.844, bottom, 0.001);
   EXPECT_NEAR(55.520, top, 0.001);
 
-  EXPECT_EQ(4, FPDFText_GetCharIndexAtPos(
-      textpage, 42.0, 50.0, 1.0, 1.0));
-  EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(
-      textpage, 0.0, 0.0, 1.0, 1.0));
-  EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(
-      textpage, 199.0, 199.0, 1.0, 1.0));
+  EXPECT_EQ(4, FPDFText_GetCharIndexAtPos(textpage, 42.0, 50.0, 1.0, 1.0));
+  EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(textpage, 0.0, 0.0, 1.0, 1.0));
+  EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(textpage, 199.0, 199.0, 1.0, 1.0));
 
   // Test out of range indicies.
-  EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(
-      textpage, 42.0, 10000000.0, 1.0, 1.0));
-  EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(
-      textpage, -1.0, 50.0, 1.0, 1.0));
+  EXPECT_EQ(-1,
+            FPDFText_GetCharIndexAtPos(textpage, 42.0, 10000000.0, 1.0, 1.0));
+  EXPECT_EQ(-1, FPDFText_GetCharIndexAtPos(textpage, -1.0, 50.0, 1.0, 1.0));
 
   // Count does not include the terminating NUL in the string literal.
   EXPECT_EQ(2, FPDFText_CountRects(textpage, 0, sizeof(expected) - 1));
@@ -110,30 +109,30 @@ TEST_F(FPDFTextEmbeddertest, Text) {
   EXPECT_EQ(0.0, bottom);
   EXPECT_EQ(0.0, top);
 
-  EXPECT_EQ(9, FPDFText_GetBoundedText(
-      textpage, 41.0, 56.0, 82.0, 48.0, 0, 0));
+  EXPECT_EQ(9, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0, 0, 0));
 
   // Extract starting at character 4 as above.
   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
-  EXPECT_EQ(1, FPDFText_GetBoundedText(
-      textpage, 41.0, 56.0, 82.0, 48.0, fixed_buffer, 1));
+  EXPECT_EQ(1, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0,
+                                       fixed_buffer, 1));
   EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 1));
   EXPECT_EQ(0xbdbd, fixed_buffer[1]);
 
   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
-  EXPECT_EQ(9, FPDFText_GetBoundedText(
-      textpage, 41.0, 56.0, 82.0, 48.0, fixed_buffer, 9));
+  EXPECT_EQ(9, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0,
+                                       fixed_buffer, 9));
   EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9));
   EXPECT_EQ(0xbdbd, fixed_buffer[9]);
 
   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
-  EXPECT_EQ(10, FPDFText_GetBoundedText(
-      textpage, 41.0, 56.0, 82.0, 48.0, fixed_buffer, 128));
+  EXPECT_EQ(10, FPDFText_GetBoundedText(textpage, 41.0, 56.0, 82.0, 48.0,
+                                        fixed_buffer, 128));
   EXPECT_TRUE(check_unsigned_shorts(expected + 4, fixed_buffer, 9));
   EXPECT_EQ(0u, fixed_buffer[9]);
   EXPECT_EQ(0xbdbd, fixed_buffer[10]);
 
   FPDFText_ClosePage(textpage);
+  UnloadPage(page);
 }
 
 TEST_F(FPDFTextEmbeddertest, TextSearch) {
@@ -145,10 +144,10 @@ TEST_F(FPDFTextEmbeddertest, TextSearch) {
   EXPECT_NE(nullptr, textpage);
 
   // Avoid issues with system wchar_t width vs. FPDF_WideString.
-  const unsigned short nope[] = { 'n', 'o', 'p', 'e', '\0' };
-  const unsigned short world[] = { 'w', 'o', 'r', 'l', 'd', '\0' };
-  const unsigned short world_caps[] = { 'W', 'O', 'R', 'L', 'D', '\0' };
-  const unsigned short world_substr[] = { 'o', 'r', 'l', 'd', '\0' };
+  const unsigned short nope[] = {'n', 'o', 'p', 'e', '\0'};
+  const unsigned short world[] = {'w', 'o', 'r', 'l', 'd', '\0'};
+  const unsigned short world_caps[] = {'W', 'O', 'R', 'L', 'D', '\0'};
+  const unsigned short world_substr[] = {'o', 'r', 'l', 'd', '\0'};
 
   // No occurences of "nope" in test page.
   FPDF_SCHHANDLE search = FPDFText_FindStart(textpage, nope, 0, 0);
@@ -202,8 +201,8 @@ TEST_F(FPDFTextEmbeddertest, TextSearch) {
   FPDFText_FindClose(search);
 
   // Exact search unaffected by case sensitiity and whole word flags.
-  search = FPDFText_FindStart(
-      textpage, world, FPDF_MATCHCASE | FPDF_MATCHWHOLEWORD, 0);
+  search = FPDFText_FindStart(textpage, world,
+                              FPDF_MATCHCASE | FPDF_MATCHWHOLEWORD, 0);
   EXPECT_NE(nullptr, search);
   EXPECT_TRUE(FPDFText_FindNext(search));
   EXPECT_EQ(7, FPDFText_GetSchResultIndex(search));
@@ -239,6 +238,7 @@ TEST_F(FPDFTextEmbeddertest, TextSearch) {
   FPDFText_FindClose(search);
 
   FPDFText_ClosePage(textpage);
+  UnloadPage(page);
 }
 
 // Test that the page has characters despite a bad stream length.
@@ -252,6 +252,7 @@ TEST_F(FPDFTextEmbeddertest, StreamLengthPastEndOfFile) {
   EXPECT_EQ(13, FPDFText_CountChars(textpage));
 
   FPDFText_ClosePage(textpage);
+  UnloadPage(page);
 }
 
 TEST_F(FPDFTextEmbeddertest, WebLinks) {
@@ -277,7 +278,7 @@ TEST_F(FPDFTextEmbeddertest, WebLinks) {
   EXPECT_EQ(25, FPDFLink_GetURL(pagelink, 0, nullptr, 0));
   EXPECT_EQ(26, FPDFLink_GetURL(pagelink, 1, nullptr, 0));
 
-  const char expected_url[] = "http://example.com?q=foo";
+  static const char expected_url[] = "http://example.com?q=foo";
   unsigned short fixed_buffer[128];
 
   // Retrieve a link with too small a buffer.  Buffer will not be
@@ -290,27 +291,28 @@ TEST_F(FPDFTextEmbeddertest, WebLinks) {
 
   // Check buffer that doesn't have space for a terminating NUL.
   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
-  EXPECT_EQ(sizeof(expected_url) - 1, FPDFLink_GetURL(
-      pagelink, 0, fixed_buffer, sizeof(expected_url) - 1));
-  EXPECT_TRUE(check_unsigned_shorts(
-      expected_url, fixed_buffer, sizeof(expected_url) - 1));
+  EXPECT_EQ(
+      sizeof(expected_url) - 1,
+      FPDFLink_GetURL(pagelink, 0, fixed_buffer, sizeof(expected_url) - 1));
+  EXPECT_TRUE(check_unsigned_shorts(expected_url, fixed_buffer,
+                                    sizeof(expected_url) - 1));
   EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url) - 1]);
 
   // Retreive link with exactly-sized buffer.
   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
-  EXPECT_EQ(sizeof(expected_url), FPDFLink_GetURL(
-      pagelink, 0, fixed_buffer, sizeof(expected_url)));
-  EXPECT_TRUE(check_unsigned_shorts(
-      expected_url, fixed_buffer, sizeof(expected_url)));
+  EXPECT_EQ(sizeof(expected_url),
+            FPDFLink_GetURL(pagelink, 0, fixed_buffer, sizeof(expected_url)));
+  EXPECT_TRUE(
+      check_unsigned_shorts(expected_url, fixed_buffer, sizeof(expected_url)));
   EXPECT_EQ(0u, fixed_buffer[sizeof(expected_url) - 1]);
   EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url)]);
 
   // Retreive link with ample-sized-buffer.
   memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
-  EXPECT_EQ(sizeof(expected_url), FPDFLink_GetURL(
-      pagelink, 0, fixed_buffer, 128));
-  EXPECT_TRUE(check_unsigned_shorts(
-      expected_url, fixed_buffer, sizeof(expected_url)));
+  EXPECT_EQ(sizeof(expected_url),
+            FPDFLink_GetURL(pagelink, 0, fixed_buffer, 128));
+  EXPECT_TRUE(
+      check_unsigned_shorts(expected_url, fixed_buffer, sizeof(expected_url)));
   EXPECT_EQ(0u, fixed_buffer[sizeof(expected_url) - 1]);
   EXPECT_EQ(0xbdbd, fixed_buffer[sizeof(expected_url)]);
 
@@ -358,4 +360,5 @@ TEST_F(FPDFTextEmbeddertest, WebLinks) {
 
   FPDFLink_CloseWebLinks(pagelink);
   FPDFText_ClosePage(textpage);
+  UnloadPage(page);
 }