File: searchtest.cpp

package info (click to toggle)
okular 4%3A4.14.2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,584 kB
  • ctags: 7,898
  • sloc: cpp: 73,406; ansic: 3,964; xml: 57; sh: 42; makefile: 7
file content (434 lines) | stat: -rw-r--r-- 15,724 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/***************************************************************************
 *   Copyright (C) 2013 by Albert Astals Cid <aacid@kde.org>               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include <qtest_kde.h>

#include "../core/document.h"
#include "../core/page.h"
#include "../core/textpage.h"
#include "../settings_core.h"

Q_DECLARE_METATYPE(Okular::Document::SearchStatus)

class SearchFinishedReceiver : public QObject
{
    Q_OBJECT

    private slots:
        void searchFinished(int id, Okular::Document::SearchStatus status)
        {
            m_id = id;
            m_status = status;
        }
    
    public:
        int m_id;
        Okular::Document::SearchStatus m_status;
};
    
class SearchTest : public QObject
{
    Q_OBJECT

    private slots:
        void initTestCase();
        void testNextAndPrevious();
        void test311232();
        void test323262();
        void test323263();
        void testDottedI();
        void testHyphenAtEndOfLineWithoutYOverlap();
        void testHyphenWithYOverlap();
        void testHyphenAtEndOfPage();
        void testOneColumn();
        void testTwoColumns();
};

void SearchTest::initTestCase()
{
    qRegisterMetaType<Okular::Document::SearchStatus>();
    Okular::SettingsCore::instance( "searchtest" );
}

static void createTextPage(const QVector<QString>& text, const QVector<Okular::NormalizedRect>& rect,
                                        Okular::TextPage*& tp, Okular::Page*& page) {

    tp = new Okular::TextPage();
    for (int i = 0; i < text.size(); i++) {
        tp->append(text[i], new Okular::NormalizedRect(rect[i]));
    }

    //The Page::setTextPage method invokes the layout analysis algorithms tested by some tests here
    //and also sets the tp->d->m_page field (the latter was used in older versions of Okular by
    //TextPage::stringLengthAdaptedWithHyphen).
    //Note that calling "delete page;" will delete the TextPage as well.
    page = new Okular::Page(1, 100, 100, Okular::Rotation0);
    page -> setTextPage(tp);
}

#define CREATE_PAGE \
  QCOMPARE(text.size(), rect.size()); \
  Okular::Page* page; \
  Okular::TextPage* tp; \
  createTextPage(text, rect, tp, page);

#define TEST_NEXT_PREV(searchType, expectedStatus) \
{ \
   Okular::RegularAreaRect* result = tp->findText(0, searchString, searchType, Qt::CaseSensitive, NULL); \
   QCOMPARE(!!result, expectedStatus); \
   delete result; \
}

//The test testNextAndPrevious checks that
//a) if one starts a new search, then the first or last match is found, depending on the search direction
//   (2 cases: FromTop/FromBottom)
//b) if the last search has found a match,
//   then clicking the "Next" button moves to the next occurrence an "Previous" to the previous one
//   (if there is any). Altogether there are four combinations of the last search and new search
//   direction: Next-Next, Previous-Previous, Next-Previous, Previous-Next; the first two combination
//   have two subcases (the new search may give a match or not, so altogether 6 cases to test).
//This gives 8 cases altogether. By taking into account the cases where the last search has given no match,
//we would have 4 more cases (Next (no match)-Next, Next (no match)-Previous, Previous (no match)-Previous,
//Previous (no match)-Next), but those are more the business of Okular::Document::searchText rather than
//Okular::TextPage (at least in the multi-page case).

//   We have four test situations: four documents and four corresponding search strings.
//   The first situation (document="ababa", search string="b") is a generic one where the
//two matches are not side-by-side and neither the first character nor the last character of
//the document match. The only special thing is that the search string has only length 1.
//   The second situation (document="abab", search string="ab") is notable for that the two occurrences
//of the search string are side-by-side with no characters in between, so some off-by-one errors
//would be detected by this test. As the first match starts at the beginning at the document the
//last match ends at the end of the document, it also detects off-by-one errors for finding the first/last match.
//   The third situation (document="abababa", search string="aba") is notable for it shows whether
//the next match is allowed to contain letters from the previous one: currently it is not
//(as in the majority of browsers, viewers and editors), and therefore "abababa" is considered to
//contain not three but two occurrences of "aba" (if one starts search from the beginning of the document).
//   The fourth situation (document="a ba b", search string="a b") demonstrates the case when one TinyTextEntity
//contains multiple characters that are contained in different matches (namely, the middle "ba" is one TinyTextEntity);
//in particular, since these matches are side-by-side, this test would detect some off-by-one
//offset errors.

void SearchTest::testNextAndPrevious()
{

#define TEST_NEXT_PREV_SITUATION_COUNT 4

    QVector<QString> texts[TEST_NEXT_PREV_SITUATION_COUNT] = {
        QVector<QString>() << "a" << "b" << "a" << "b" << "a",
        QVector<QString>() << "a" << "b" << "a" << "b",
        QVector<QString>() << "a" << "b" << "a" << "b" << "a" << "b" << "a" ,
        QVector<QString>() << "a" << " " << "ba" << " " << "b"
    };

    QString searchStrings[TEST_NEXT_PREV_SITUATION_COUNT] = {
        "b",
        "ab",
        "aba",
        "a b"
    };

    for (int i = 0; i < TEST_NEXT_PREV_SITUATION_COUNT; i++) {
        const QVector<QString>& text = texts[i];
        const QString& searchString = searchStrings[i];

        QVector<Okular::NormalizedRect> rect; \

        for (int i = 0; i < text.size(); i++) {
            rect << Okular::NormalizedRect(0.1*i, 0.0, 0.1*(i+1), 0.1); \
        }

        CREATE_PAGE;

        //Test 3 of the 8 cases listed above:
        //FromTop, Next-Next (match) and Next-Next (no match)
        TEST_NEXT_PREV(Okular::FromTop, true);
        TEST_NEXT_PREV(Okular::NextResult, true);
        TEST_NEXT_PREV(Okular::NextResult, false);

        //Test 5 cases: FromBottom, Previous-Previous (match), Previous-Next,
        //Next-Previous, Previous-Previous (no match)
        TEST_NEXT_PREV(Okular::FromBottom, true);
        TEST_NEXT_PREV(Okular::PreviousResult, true);
        TEST_NEXT_PREV(Okular::NextResult, true);
        TEST_NEXT_PREV(Okular::PreviousResult, true);
        TEST_NEXT_PREV(Okular::PreviousResult, false);

        delete page;
    }
}

void SearchTest::test311232()
{
    Okular::Document d(0);
    SearchFinishedReceiver receiver;
    QSignalSpy spy(&d, SIGNAL(searchFinished(int,Okular::Document::SearchStatus)));

    QObject::connect(&d, SIGNAL(searchFinished(int,Okular::Document::SearchStatus)), &receiver, SLOT(searchFinished(int,Okular::Document::SearchStatus)));
    
    const QString testFile = KDESRCDIR "data/file1.pdf";
    const KMimeType::Ptr mime = KMimeType::findByPath( testFile );
    d.openDocument(testFile, KUrl(), mime);
    
    const int searchId = 0;
    d.searchText(searchId, " i ", true, Qt::CaseSensitive, Okular::Document::NextMatch, false, QColor());
    QTime t;
    t.start();
    while (spy.count() != 1 && t.elapsed() < 500)
        qApp->processEvents();
    QCOMPARE(spy.count(), 1);
    QCOMPARE(receiver.m_id, searchId);
    QCOMPARE(receiver.m_status, Okular::Document::MatchFound);
    
    
    d.continueSearch( searchId, Okular::Document::PreviousMatch );
    t.start();
    while (spy.count() != 2 && t.elapsed() < 500)
        qApp->processEvents();
    QCOMPARE(spy.count(), 2);
    QCOMPARE(receiver.m_id, searchId);
    QCOMPARE(receiver.m_status, Okular::Document::NoMatchFound);
}

void SearchTest::test323262()
{
    QVector<QString> text;
    text << "a\n";

    QVector<Okular::NormalizedRect> rect;
    rect << Okular::NormalizedRect(1, 2, 3, 4);

    CREATE_PAGE;

    Okular::RegularAreaRect* result = tp->findText(0, "a", Okular::FromBottom, Qt::CaseSensitive, NULL);
    QVERIFY(result);
    delete result;

    delete page;
}

void SearchTest::test323263()
{
    QVector<QString> text;
    text << "a" << "a" << "b";

    QVector<Okular::NormalizedRect> rect;
    rect << Okular::NormalizedRect(0, 0, 1, 1)
         << Okular::NormalizedRect(1, 0, 2, 1)
         << Okular::NormalizedRect(2, 0, 3, 1);

    CREATE_PAGE;

    Okular::RegularAreaRect* result = tp->findText(0, "ab", Okular::FromTop, Qt::CaseSensitive, NULL);
    QVERIFY(result);
    Okular::RegularAreaRect expected;
    expected.append(rect[1]);
    expected.append(rect[2]);
    expected.simplify();
    QCOMPARE(*result, expected);
    delete result;

    delete page;
}

void SearchTest::testDottedI()
{
    //Earlier versions of okular had the bug that the letter "İ" (capital dotter i) did not match itself
    //in case-insensitive mode (this was caused by an unnecessary call of toLower() and the fact that
    //QString::fromUtf8("İ").compare(QString::fromUtf8("İ").toLower(), Qt::CaseInsensitive) == FALSE,
    //at least in Qt 4.8).

    //In the future it would be nice to add support for matching "İ"<->"i" and "I"<->"ı" in case-insensitive
    //mode as well (QString::compare does not match them, at least in non-Turkish locales, since it follows
    //the Unicode case-folding rules http://www.unicode.org/Public/6.2.0/ucd/CaseFolding.txt).

    QVector<QString> text;
    text << QString::fromUtf8("İ");

    QVector<Okular::NormalizedRect> rect;
    rect << Okular::NormalizedRect(1, 2, 3, 4);

    CREATE_PAGE;

    Okular::RegularAreaRect* result = tp->findText(0, QString::fromUtf8("İ"), Okular::FromTop, Qt::CaseInsensitive, NULL);
    QVERIFY(result);
    delete result;

    delete page;
}

void SearchTest::testHyphenAtEndOfLineWithoutYOverlap()
{
    QVector<QString> text;
    text << "super-"
         << "cali-\n"
         << "fragilistic" << "-"
         << "expiali" << "-\n"
         << "docious";

    QVector<Okular::NormalizedRect> rect;
    rect << Okular::NormalizedRect(0.4, 0.0, 0.9, 0.1)
         << Okular::NormalizedRect(0.0, 0.1, 0.6, 0.2)
         << Okular::NormalizedRect(0.0, 0.2, 0.8, 0.3) << Okular::NormalizedRect(0.8, 0.2, 0.9, 0.3)
         << Okular::NormalizedRect(0.0, 0.3, 0.8, 0.4) << Okular::NormalizedRect(0.8, 0.3, 0.9, 0.4)
         << Okular::NormalizedRect(0.0, 0.4, 0.7, 0.5);

    CREATE_PAGE;

    Okular::RegularAreaRect* result = tp->findText(0, "supercalifragilisticexpialidocious",
                                                 Okular::FromTop, Qt::CaseSensitive, NULL);
    QVERIFY(result);
    Okular::RegularAreaRect expected;
    for (int i = 0; i < text.size(); i++) {
        expected.append(rect[i]);
    }
    expected.simplify();
    QCOMPARE(*result, expected);
    delete result;

    delete page;
}

#define CREATE_PAGE_AND_TEST_SEARCH(searchString, matchExpected) \
{ \
    CREATE_PAGE; \
 \
    Okular::RegularAreaRect* result = tp->findText(0, searchString, \
               Okular::FromTop, Qt::CaseSensitive, NULL); \
 \
    QCOMPARE(!!result, matchExpected); \
 \
    delete result; \
    delete page; \
}

void SearchTest::testHyphenWithYOverlap()
{
    QVector<QString> text;
    text << "a-"
         << "b";

    QVector<Okular::NormalizedRect> rect(2);

    //different lines (50% y-coordinate overlap), first rectangle has larger height
    rect[0] = Okular::NormalizedRect(0.0, 0.0,  0.9, 0.35);
    rect[1] = Okular::NormalizedRect(0.0, 0.3, 0.2, 0.4);
    CREATE_PAGE_AND_TEST_SEARCH("ab", true);

    //different lines (50% y-coordinate overlap), second rectangle has larger height
    rect[0] = Okular::NormalizedRect(0.0, 0.0,  0.9, 0.1);
    rect[1] = Okular::NormalizedRect(0.0, 0.05, 0.2, 0.4);
    CREATE_PAGE_AND_TEST_SEARCH("ab", true);

    //same line (90% y-coordinate overlap), first rectangle has larger height
    rect[0] = Okular::NormalizedRect(0.0, 0.0,  0.4, 0.2);
    rect[1] = Okular::NormalizedRect(0.4, 0.11, 0.6, 0.21);
    CREATE_PAGE_AND_TEST_SEARCH("ab", false);
    CREATE_PAGE_AND_TEST_SEARCH("a-b", true);

    //same line (90% y-coordinate overlap), second rectangle has larger height
    rect[0] = Okular::NormalizedRect(0.0, 0.0, 0.4, 0.1);
    rect[1] = Okular::NormalizedRect(0.4, 0.01, 0.6, 0.2);
    CREATE_PAGE_AND_TEST_SEARCH("ab", false);
    CREATE_PAGE_AND_TEST_SEARCH("a-b", true);
}

void SearchTest::testHyphenAtEndOfPage()
{
    //Tests for segmentation fault that would occur if
    //we tried look ahead (for determining whether the
    //next character is at the same line) at the end of the page.

    QVector<QString> text;
    text << "a-";

    QVector<Okular::NormalizedRect> rect;
    rect << Okular::NormalizedRect(0, 0, 1, 1);

    CREATE_PAGE;

    {
        Okular::RegularAreaRect* result = tp->findText(0, "a",
            Okular::FromTop, Qt::CaseSensitive, NULL);
        QVERIFY(result);
        delete result;
    }

    {
        Okular::RegularAreaRect* result = tp->findText(0, "a",
            Okular::FromBottom, Qt::CaseSensitive, NULL);
        QVERIFY(result);
        delete result;
    }

    delete page;
}

void SearchTest::testOneColumn()
{
  //Tests that the layout analysis algorithm does not create too many columns.
  //Bug 326207 was caused by the fact that if all the horizontal breaks in a line
  //had the same length and were smaller than vertical breaks between lines then
  //the horizontal breaks were treated as column separators.
  //(Note that "same length" means "same length after rounding rectangles to integer pixels".
  //The resolution used by the XY Cut algorithm with a square page is 1000 x 1000,
  //and the horizontal spaces in the example are 0.1, so they are indeed both exactly 100 pixels.)

  QVector<QString> text;
  text << "Only" << "one" << "column"
       << "here";

  //characters and line breaks have length 0.05, word breaks 0.1
  QVector<Okular::NormalizedRect> rect;
  rect << Okular::NormalizedRect(0.0,  0.0,  0.2,  0.1)
       << Okular::NormalizedRect(0.3,  0.0,  0.5,  0.1)
       << Okular::NormalizedRect(0.6,  0.0,  0.9,  0.1)
       << Okular::NormalizedRect(0.0,  0.15, 0.2,  0.25);

  CREATE_PAGE;

  Okular::RegularAreaRect* result = tp->findText(0, "Only one column",
      Okular::FromTop, Qt::CaseSensitive, NULL);
  QVERIFY(result);
  delete result;

  delete page;
}

void SearchTest::testTwoColumns()
{
  //Tests that the layout analysis algorithm can detect two columns.

  QVector<QString> text;
  text << "This" << "text" << "in" << "two"
       << "is" << "set"    << "columns.";

  //characters, word breaks and line breaks have length 0.05
  QVector<Okular::NormalizedRect> rect;
  rect << Okular::NormalizedRect(0.0,  0.0,  0.20, 0.1)
       << Okular::NormalizedRect(0.25, 0.0,  0.45, 0.1)
       << Okular::NormalizedRect(0.6,  0.0,  0.7,  0.1)
       << Okular::NormalizedRect(0.75, 0.0,  0.9,  0.1)
       << Okular::NormalizedRect(0.0,  0.15, 0.1,  0.25)
       << Okular::NormalizedRect(0.15, 0.15, 0.3,  0.25)
       << Okular::NormalizedRect(0.6,  0.15, 1.0,  0.25);

  CREATE_PAGE;

  Okular::RegularAreaRect* result = tp->findText(0, "This text in",
      Okular::FromTop, Qt::CaseSensitive, NULL);
  QVERIFY(!result);
  delete result;

  delete page;
}

QTEST_KDEMAIN( SearchTest, GUI )

#include "searchtest.moc"