1 /***************************************************************************
2 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include <qtest_kde.h>
22 #include "kitemviews/kitemlistkeyboardsearchmanager_p.h"
24 class KItemListKeyboardSearchManagerTest
: public QObject
31 void testBasicKeyboardSearch();
32 void testAbortedKeyboardSearch();
33 void testRepeatedKeyPress();
36 KItemListKeyboardSearchManager m_keyboardSearchManager
;
39 void KItemListKeyboardSearchManagerTest::init()
41 // Make sure that the previous search string is cleared
42 m_keyboardSearchManager
.addKeys("");
45 void KItemListKeyboardSearchManagerTest::testBasicKeyboardSearch()
47 QSignalSpy
spy(&m_keyboardSearchManager
, SIGNAL(changeCurrentItem(QString
,bool)));
49 m_keyboardSearchManager
.addKeys("f");
50 QCOMPARE(spy
.count(), 1);
51 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "f" << true);
53 m_keyboardSearchManager
.addKeys("i");
54 QCOMPARE(spy
.count(), 1);
55 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fi" << false);
57 m_keyboardSearchManager
.addKeys("l");
58 QCOMPARE(spy
.count(), 1);
59 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fil" << false);
61 m_keyboardSearchManager
.addKeys("e");
62 QCOMPARE(spy
.count(), 1);
63 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "file" << false);
66 void KItemListKeyboardSearchManagerTest::testAbortedKeyboardSearch()
68 QSignalSpy
spy(&m_keyboardSearchManager
, SIGNAL(changeCurrentItem(QString
,bool)));
70 m_keyboardSearchManager
.addKeys("f");
71 QCOMPARE(spy
.count(), 1);
72 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "f" << true);
74 m_keyboardSearchManager
.addKeys("i");
75 QCOMPARE(spy
.count(), 1);
76 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fi" << false);
78 // If the delay between two key presses is larger than 5000 milliseconds,
79 // a new search is started. We add a small safety margin to avoid race conditions.
80 QTest::qWait(5000 + 10);
82 m_keyboardSearchManager
.addKeys("l");
83 QCOMPARE(spy
.count(), 1);
84 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "l" << true);
86 m_keyboardSearchManager
.addKeys("e");
87 QCOMPARE(spy
.count(), 1);
88 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "le" << false);
91 void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress()
93 // If the same key is pressed repeatedly, the next matching item should be highlighted after
94 // each key press. To achieve, that, the manager emits the changeCurrentItem(QString,bool)
96 // 1. the string contains the repeated key only once, and
97 // 2. the bool searchFromNextItem is true.
99 QSignalSpy
spy(&m_keyboardSearchManager
, SIGNAL(changeCurrentItem(QString
,bool)));
101 m_keyboardSearchManager
.addKeys("p");
102 QCOMPARE(spy
.count(), 1);
103 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
105 m_keyboardSearchManager
.addKeys("p");
106 QCOMPARE(spy
.count(), 1);
107 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
109 m_keyboardSearchManager
.addKeys("p");
110 QCOMPARE(spy
.count(), 1);
111 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
113 // Now press another key -> the search string contains all pressed keys
114 m_keyboardSearchManager
.addKeys("q");
115 QCOMPARE(spy
.count(), 1);
116 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "pppq" << false);
119 QTEST_KDEMAIN(KItemListKeyboardSearchManagerTest
, NoGUI
)
121 #include "kitemlistkeyboardsearchmanagertest.moc"