1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <config_features.h>
12 #include <string_view>
14 #include <cppunit/TestAssert.h>
15 #include <cppunit/extensions/HelperMacros.h>
17 #include <unotest/bootstrapfixturebase.hxx>
18 #include <unotest/directories.hxx>
20 #include <vcl/VectorGraphicSearch.hxx>
21 #include <vcl/graph.hxx>
22 #include <vcl/graphicfilter.hxx>
23 #include <tools/stream.hxx>
25 class VectorGraphicSearchTest
: public test::BootstrapFixtureBase
27 OUString
getFullUrl(std::u16string_view sFileName
)
29 return m_directories
.getURLFromSrc(u
"/vcl/qa/cppunit/data/") + sFileName
;
33 void testNextPrevious();
34 void testSearchStringChange();
35 void testSearchMatchWholeWord();
36 void testSearchMatchCase();
38 CPPUNIT_TEST_SUITE(VectorGraphicSearchTest
);
40 CPPUNIT_TEST(testNextPrevious
);
41 CPPUNIT_TEST(testSearchStringChange
);
42 CPPUNIT_TEST(testSearchMatchWholeWord
);
43 CPPUNIT_TEST(testSearchMatchCase
);
44 CPPUNIT_TEST_SUITE_END();
47 void VectorGraphicSearchTest::test()
49 OUString aURL
= getFullUrl(u
"Pangram.pdf");
50 SvFileStream
aStream(aURL
, StreamMode::READ
);
51 GraphicFilter
& rGraphicFilter
= GraphicFilter::GetGraphicFilter();
52 Graphic aGraphic
= rGraphicFilter
.ImportUnloadedGraphic(aStream
);
53 aGraphic
.makeAvailable();
55 VectorGraphicSearch
aSearch(aGraphic
);
56 CPPUNIT_ASSERT_EQUAL(true, aSearch
.search("lazy"));
57 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
58 CPPUNIT_ASSERT_EQUAL(34, aSearch
.index());
60 basegfx::B2DSize aSize
= aSearch
.pageSize();
61 CPPUNIT_ASSERT_DOUBLES_EQUAL(21590.00, aSize
.getWidth(), 1E-2);
62 CPPUNIT_ASSERT_DOUBLES_EQUAL(27940.00, aSize
.getHeight(), 1E-2);
64 auto aRectangles
= aSearch
.getTextRectangles();
65 CPPUNIT_ASSERT_EQUAL(size_t(4), aRectangles
.size());
67 // Check first and last
68 CPPUNIT_ASSERT_DOUBLES_EQUAL(8078.61, aRectangles
[0].getMinX(), 1E-2);
69 CPPUNIT_ASSERT_DOUBLES_EQUAL(8179.36, aRectangles
[0].getMaxX(), 1E-2);
70 CPPUNIT_ASSERT_DOUBLES_EQUAL(2101.56, aRectangles
[0].getMinY(), 1E-2);
71 CPPUNIT_ASSERT_DOUBLES_EQUAL(2395.36, aRectangles
[0].getMaxY(), 1E-2);
73 CPPUNIT_ASSERT_DOUBLES_EQUAL(8565.86, aRectangles
[3].getMinX(), 1E-2);
74 CPPUNIT_ASSERT_DOUBLES_EQUAL(8770.76, aRectangles
[3].getMaxX(), 1E-2);
75 CPPUNIT_ASSERT_DOUBLES_EQUAL(2201.05, aRectangles
[3].getMinY(), 1E-2);
76 CPPUNIT_ASSERT_DOUBLES_EQUAL(2486.37, aRectangles
[3].getMaxY(), 1E-2);
78 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
79 CPPUNIT_ASSERT_EQUAL(817, aSearch
.index());
81 aRectangles
= aSearch
.getTextRectangles();
82 CPPUNIT_ASSERT_EQUAL(size_t(4), aRectangles
.size());
84 // Check first and last
85 CPPUNIT_ASSERT_DOUBLES_EQUAL(6562.23, aRectangles
[0].getMinX(), 1E-2);
86 CPPUNIT_ASSERT_DOUBLES_EQUAL(6662.98, aRectangles
[0].getMaxX(), 1E-2);
87 CPPUNIT_ASSERT_DOUBLES_EQUAL(5996.23, aRectangles
[0].getMinY(), 1E-2);
88 CPPUNIT_ASSERT_DOUBLES_EQUAL(6290.02, aRectangles
[0].getMaxY(), 1E-2);
90 CPPUNIT_ASSERT_DOUBLES_EQUAL(7049.48, aRectangles
[3].getMinX(), 1E-2);
91 CPPUNIT_ASSERT_DOUBLES_EQUAL(7254.38, aRectangles
[3].getMaxX(), 1E-2);
92 CPPUNIT_ASSERT_DOUBLES_EQUAL(6095.71, aRectangles
[3].getMinY(), 1E-2);
93 CPPUNIT_ASSERT_DOUBLES_EQUAL(6381.04, aRectangles
[3].getMaxY(), 1E-2);
96 // Test next and previous work as expected to move
97 // between search matches.
98 void VectorGraphicSearchTest::testNextPrevious()
100 OUString aURL
= getFullUrl(u
"Pangram.pdf");
101 SvFileStream
aStream(aURL
, StreamMode::READ
);
102 GraphicFilter
& rGraphicFilter
= GraphicFilter::GetGraphicFilter();
103 Graphic aGraphic
= rGraphicFilter
.ImportUnloadedGraphic(aStream
);
104 aGraphic
.makeAvailable();
106 { // Start from the beginning of the page
107 VectorGraphicSearch
aSearch(aGraphic
);
108 CPPUNIT_ASSERT_EQUAL(true, aSearch
.search("lazy"));
110 // no previous - we are at the begin
111 CPPUNIT_ASSERT_EQUAL(false, aSearch
.previous());
112 CPPUNIT_ASSERT_EQUAL(0, aSearch
.index()); // nothing was yet found, so it is 0
114 // next - first position found
115 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
116 CPPUNIT_ASSERT_EQUAL(34, aSearch
.index());
118 // next - second position found
119 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
120 CPPUNIT_ASSERT_EQUAL(817, aSearch
.index());
122 // next - not found, index unchanged
123 CPPUNIT_ASSERT_EQUAL(false, aSearch
.next());
124 CPPUNIT_ASSERT_EQUAL(817, aSearch
.index());
126 // previous - first position
127 CPPUNIT_ASSERT_EQUAL(true, aSearch
.previous());
128 CPPUNIT_ASSERT_EQUAL(34, aSearch
.index());
130 // previous - not found, index unchanged
131 CPPUNIT_ASSERT_EQUAL(false, aSearch
.previous());
132 CPPUNIT_ASSERT_EQUAL(34, aSearch
.index());
134 // next - second position found
135 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
136 CPPUNIT_ASSERT_EQUAL(817, aSearch
.index());
139 { // Start from the end of the page
140 VectorGraphicSearch
aSearch(aGraphic
);
141 CPPUNIT_ASSERT_EQUAL(true,
142 aSearch
.search("lazy", { SearchStartPosition::End
, false, false }));
144 // no next - we are at the end
145 CPPUNIT_ASSERT_EQUAL(false, aSearch
.next());
146 CPPUNIT_ASSERT_EQUAL(0, aSearch
.index()); // nothing was yet found, so it is 0
148 // previous - second position found
149 CPPUNIT_ASSERT_EQUAL(true, aSearch
.previous());
150 CPPUNIT_ASSERT_EQUAL(817, aSearch
.index());
152 // previous - first position found
153 CPPUNIT_ASSERT_EQUAL(true, aSearch
.previous());
154 CPPUNIT_ASSERT_EQUAL(34, aSearch
.index());
156 // previous - not found, index unchanged
157 CPPUNIT_ASSERT_EQUAL(false, aSearch
.previous());
158 CPPUNIT_ASSERT_EQUAL(34, aSearch
.index());
160 // next - second position
161 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
162 CPPUNIT_ASSERT_EQUAL(817, aSearch
.index());
164 // next - not found, index unchanged
165 CPPUNIT_ASSERT_EQUAL(false, aSearch
.next());
166 CPPUNIT_ASSERT_EQUAL(817, aSearch
.index());
168 // previous - first match found
169 CPPUNIT_ASSERT_EQUAL(true, aSearch
.previous());
170 CPPUNIT_ASSERT_EQUAL(34, aSearch
.index());
174 void VectorGraphicSearchTest::testSearchStringChange()
176 OUString aURL
= getFullUrl(u
"Pangram.pdf");
177 SvFileStream
aStream(aURL
, StreamMode::READ
);
178 GraphicFilter
& rGraphicFilter
= GraphicFilter::GetGraphicFilter();
179 Graphic aGraphic
= rGraphicFilter
.ImportUnloadedGraphic(aStream
);
180 aGraphic
.makeAvailable();
182 VectorGraphicSearch
aSearch(aGraphic
);
184 // Set search to "lazy"
185 CPPUNIT_ASSERT_EQUAL(true, aSearch
.search("lazy"));
187 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
188 CPPUNIT_ASSERT_EQUAL(34, aSearch
.index());
190 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
191 CPPUNIT_ASSERT_EQUAL(817, aSearch
.index());
193 // Change search to "fox"
194 CPPUNIT_ASSERT_EQUAL(true, aSearch
.search("fox"));
196 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
197 CPPUNIT_ASSERT_EQUAL(822, aSearch
.index());
199 // Change search to "Quick"
200 CPPUNIT_ASSERT_EQUAL(true, aSearch
.search("Quick"));
201 CPPUNIT_ASSERT_EQUAL(true, aSearch
.previous());
202 CPPUNIT_ASSERT_EQUAL(784, aSearch
.index());
205 void VectorGraphicSearchTest::testSearchMatchWholeWord()
207 OUString aURL
= getFullUrl(u
"Pangram.pdf");
208 SvFileStream
aStream(aURL
, StreamMode::READ
);
209 GraphicFilter
& rGraphicFilter
= GraphicFilter::GetGraphicFilter();
210 Graphic aGraphic
= rGraphicFilter
.ImportUnloadedGraphic(aStream
);
211 aGraphic
.makeAvailable();
214 VectorGraphicSearch
aSearch(aGraphic
);
215 // Search, whole word disabled - "Flummoxed" - found
216 CPPUNIT_ASSERT_EQUAL(true, aSearch
.search("Flummoxed"));
217 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
218 CPPUNIT_ASSERT_EQUAL(618, aSearch
.index());
221 VectorGraphicSearch
aSearch(aGraphic
);
222 // Search, whole word disabled - "Flummo" - found
223 CPPUNIT_ASSERT_EQUAL(true, aSearch
.search("Flummo"));
224 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
225 CPPUNIT_ASSERT_EQUAL(618, aSearch
.index());
228 VectorGraphicSearch
aSearch(aGraphic
);
229 // Search, whole word enabled - "Flummoxed" - found
230 CPPUNIT_ASSERT_EQUAL(
231 true, aSearch
.search("Flummoxed", { SearchStartPosition::Begin
, false, true }));
232 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
233 CPPUNIT_ASSERT_EQUAL(618, aSearch
.index());
236 VectorGraphicSearch
aSearch(aGraphic
);
237 // Search, whole word enabled - "Flummo" - not found
238 CPPUNIT_ASSERT_EQUAL(true,
239 aSearch
.search("Flummo", { SearchStartPosition::Begin
, false, true }));
240 CPPUNIT_ASSERT_EQUAL(false, aSearch
.next());
244 void VectorGraphicSearchTest::testSearchMatchCase()
246 OUString aURL
= getFullUrl(u
"Pangram.pdf");
247 SvFileStream
aStream(aURL
, StreamMode::READ
);
248 GraphicFilter
& rGraphicFilter
= GraphicFilter::GetGraphicFilter();
249 Graphic aGraphic
= rGraphicFilter
.ImportUnloadedGraphic(aStream
);
250 aGraphic
.makeAvailable();
253 VectorGraphicSearch
aSearch(aGraphic
);
254 // Search "Flummoxed" - case insensitive - found
255 CPPUNIT_ASSERT_EQUAL(true, aSearch
.search("Flummoxed"));
256 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
257 CPPUNIT_ASSERT_EQUAL(618, aSearch
.index());
261 VectorGraphicSearch
aSearch(aGraphic
);
262 // Search "FLUMMOXED" - case insensitive - found
263 CPPUNIT_ASSERT_EQUAL(true, aSearch
.search("FLUMMOXED"));
264 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
265 CPPUNIT_ASSERT_EQUAL(618, aSearch
.index());
269 VectorGraphicSearch
aSearch(aGraphic
);
270 // Search "Flummoxed" - case sensitive - found
271 CPPUNIT_ASSERT_EQUAL(
272 true, aSearch
.search("Flummoxed", { SearchStartPosition::Begin
, true, false }));
273 CPPUNIT_ASSERT_EQUAL(true, aSearch
.next());
274 CPPUNIT_ASSERT_EQUAL(618, aSearch
.index());
278 VectorGraphicSearch
aSearch(aGraphic
);
279 // Search to "FLUMMOXED" - case sensitive - not found
280 CPPUNIT_ASSERT_EQUAL(
281 true, aSearch
.search("FLUMMOXED", { SearchStartPosition::Begin
, true, false }));
282 CPPUNIT_ASSERT_EQUAL(false, aSearch
.next());
286 CPPUNIT_TEST_SUITE_REGISTRATION(VectorGraphicSearchTest
);
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */