1 // Copyright 2019 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 "public/cpp/fpdf_scopers.h"
6 #include "public/fpdf_searchex.h"
7 #include "testing/embedder_test.h"
8
9 class FPDFSearchExEmbedderTest : public EmbedderTest {};
10
TEST_F(FPDFSearchExEmbedderTest,GetCharIndexFromTextIndex)11 TEST_F(FPDFSearchExEmbedderTest, GetCharIndexFromTextIndex) {
12 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
13 FPDF_PAGE page = LoadPage(0);
14 ASSERT_TRUE(page);
15
16 {
17 ScopedFPDFTextPage textpage(FPDFText_LoadPage(page));
18 ASSERT_TRUE(textpage);
19
20 EXPECT_EQ(-2, FPDFText_GetCharIndexFromTextIndex(textpage.get(), -2));
21 EXPECT_EQ(-1, FPDFText_GetCharIndexFromTextIndex(textpage.get(), -1));
22 EXPECT_EQ(0, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 0));
23 EXPECT_EQ(1, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 1));
24 EXPECT_EQ(2, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 2));
25 EXPECT_EQ(5, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 5));
26 EXPECT_EQ(10, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 10));
27 EXPECT_EQ(29, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 29));
28 EXPECT_EQ(-1, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 30));
29 }
30
31 UnloadPage(page);
32 }
33
TEST_F(FPDFSearchExEmbedderTest,GetCharIndexFromTextIndexWithNonPrintableChar)34 TEST_F(FPDFSearchExEmbedderTest,
35 GetCharIndexFromTextIndexWithNonPrintableChar) {
36 ASSERT_TRUE(OpenDocument("bug_1139.pdf"));
37 FPDF_PAGE page = LoadPage(0);
38 ASSERT_TRUE(page);
39
40 {
41 ScopedFPDFTextPage textpage(FPDFText_LoadPage(page));
42 ASSERT_TRUE(textpage);
43
44 EXPECT_EQ(-1, FPDFText_GetCharIndexFromTextIndex(textpage.get(), -2));
45 EXPECT_EQ(0, FPDFText_GetCharIndexFromTextIndex(textpage.get(), -1));
46 EXPECT_EQ(1, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 0));
47 EXPECT_EQ(2, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 1));
48 EXPECT_EQ(3, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 2));
49 EXPECT_EQ(6, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 5));
50 EXPECT_EQ(11, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 10));
51 EXPECT_EQ(30, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 29));
52 EXPECT_EQ(-1, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 30));
53 EXPECT_EQ(-1, FPDFText_GetCharIndexFromTextIndex(textpage.get(), 31));
54 }
55
56 UnloadPage(page);
57 }
58
TEST_F(FPDFSearchExEmbedderTest,GetCharIndexFromTextIndexInvalid)59 TEST_F(FPDFSearchExEmbedderTest, GetCharIndexFromTextIndexInvalid) {
60 EXPECT_EQ(-1, FPDFText_GetCharIndexFromTextIndex(nullptr, -2));
61 EXPECT_EQ(-1, FPDFText_GetCharIndexFromTextIndex(nullptr, -1));
62 EXPECT_EQ(-1, FPDFText_GetCharIndexFromTextIndex(nullptr, 0));
63 EXPECT_EQ(-1, FPDFText_GetCharIndexFromTextIndex(nullptr, 1));
64 EXPECT_EQ(-1, FPDFText_GetCharIndexFromTextIndex(nullptr, 2));
65 }
66
TEST_F(FPDFSearchExEmbedderTest,GetTextIndexFromCharIndex)67 TEST_F(FPDFSearchExEmbedderTest, GetTextIndexFromCharIndex) {
68 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
69 FPDF_PAGE page = LoadPage(0);
70 ASSERT_TRUE(page);
71
72 {
73 ScopedFPDFTextPage textpage(FPDFText_LoadPage(page));
74 ASSERT_TRUE(textpage);
75
76 EXPECT_EQ(-1, FPDFText_GetTextIndexFromCharIndex(textpage.get(), -2));
77 EXPECT_EQ(-1, FPDFText_GetTextIndexFromCharIndex(textpage.get(), -1));
78 EXPECT_EQ(0, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 0));
79 EXPECT_EQ(1, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 1));
80 EXPECT_EQ(2, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 2));
81 EXPECT_EQ(5, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 5));
82 EXPECT_EQ(10, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 10));
83 EXPECT_EQ(29, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 29));
84 EXPECT_EQ(-1, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 30));
85 }
86
87 UnloadPage(page);
88 }
89
TEST_F(FPDFSearchExEmbedderTest,GetTextIndexFromCharIndexWithNonPrintableChar)90 TEST_F(FPDFSearchExEmbedderTest,
91 GetTextIndexFromCharIndexWithNonPrintableChar) {
92 ASSERT_TRUE(OpenDocument("bug_1139.pdf"));
93 FPDF_PAGE page = LoadPage(0);
94 ASSERT_TRUE(page);
95
96 {
97 ScopedFPDFTextPage textpage(FPDFText_LoadPage(page));
98 ASSERT_TRUE(textpage);
99
100 EXPECT_EQ(-1, FPDFText_GetTextIndexFromCharIndex(textpage.get(), -2));
101 EXPECT_EQ(-1, FPDFText_GetTextIndexFromCharIndex(textpage.get(), -1));
102 EXPECT_EQ(-1, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 0));
103 EXPECT_EQ(0, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 1));
104 EXPECT_EQ(1, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 2));
105 EXPECT_EQ(4, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 5));
106 EXPECT_EQ(9, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 10));
107 EXPECT_EQ(28, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 29));
108 EXPECT_EQ(29, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 30));
109 EXPECT_EQ(-1, FPDFText_GetTextIndexFromCharIndex(textpage.get(), 31));
110 }
111
112 UnloadPage(page);
113 }
114
TEST_F(FPDFSearchExEmbedderTest,GetTextIndexFromCharIndexInvalid)115 TEST_F(FPDFSearchExEmbedderTest, GetTextIndexFromCharIndexInvalid) {
116 EXPECT_EQ(-1, FPDFText_GetTextIndexFromCharIndex(nullptr, -2));
117 EXPECT_EQ(-1, FPDFText_GetTextIndexFromCharIndex(nullptr, -1));
118 EXPECT_EQ(-1, FPDFText_GetTextIndexFromCharIndex(nullptr, 0));
119 EXPECT_EQ(-1, FPDFText_GetTextIndexFromCharIndex(nullptr, 1));
120 EXPECT_EQ(-1, FPDFText_GetTextIndexFromCharIndex(nullptr, 2));
121 }
122