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/private/kitemlistkeyboardsearchmanager.h"
24 class KItemListKeyboardSearchManagerTest
: public QObject
31 void testBasicKeyboardSearch();
32 void testAbortedKeyboardSearch();
33 void testRepeatedKeyPress();
34 void testPressShift();
37 KItemListKeyboardSearchManager m_keyboardSearchManager
;
40 void KItemListKeyboardSearchManagerTest::init()
42 // Make sure that the previous search string is cleared
43 m_keyboardSearchManager
.cancelSearch();
46 void KItemListKeyboardSearchManagerTest::testBasicKeyboardSearch()
48 QSignalSpy
spy(&m_keyboardSearchManager
, SIGNAL(changeCurrentItem(QString
,bool)));
50 m_keyboardSearchManager
.addKeys("f");
51 QCOMPARE(spy
.count(), 1);
52 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "f" << true);
54 m_keyboardSearchManager
.addKeys("i");
55 QCOMPARE(spy
.count(), 1);
56 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fi" << false);
58 m_keyboardSearchManager
.addKeys("l");
59 QCOMPARE(spy
.count(), 1);
60 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fil" << false);
62 m_keyboardSearchManager
.addKeys("e");
63 QCOMPARE(spy
.count(), 1);
64 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "file" << false);
67 void KItemListKeyboardSearchManagerTest::testAbortedKeyboardSearch()
69 // Set the timeout to a small value (the default is 5000 milliseconds)
70 // to save time when running this test.
71 m_keyboardSearchManager
.setTimeout(100);
73 QSignalSpy
spy(&m_keyboardSearchManager
, SIGNAL(changeCurrentItem(QString
,bool)));
75 m_keyboardSearchManager
.addKeys("f");
76 QCOMPARE(spy
.count(), 1);
77 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "f" << true);
79 m_keyboardSearchManager
.addKeys("i");
80 QCOMPARE(spy
.count(), 1);
81 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fi" << false);
83 // If the delay between two key presses is larger than the chosen timeout,
84 // a new search is started. We add a small safety margin to avoid race conditions.
85 QTest::qWait(m_keyboardSearchManager
.timeout() + 10);
87 m_keyboardSearchManager
.addKeys("l");
88 QCOMPARE(spy
.count(), 1);
89 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "l" << true);
91 m_keyboardSearchManager
.addKeys("e");
92 QCOMPARE(spy
.count(), 1);
93 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "le" << false);
96 void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress()
98 // If the same key is pressed repeatedly, the next matching item should be highlighted after
99 // each key press. To achieve, that, the manager emits the changeCurrentItem(QString,bool)
101 // 1. the string contains the repeated key only once, and
102 // 2. the bool searchFromNextItem is true.
104 QSignalSpy
spy(&m_keyboardSearchManager
, SIGNAL(changeCurrentItem(QString
,bool)));
106 m_keyboardSearchManager
.addKeys("p");
107 QCOMPARE(spy
.count(), 1);
108 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
110 m_keyboardSearchManager
.addKeys("p");
111 QCOMPARE(spy
.count(), 1);
112 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
114 m_keyboardSearchManager
.addKeys("p");
115 QCOMPARE(spy
.count(), 1);
116 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
118 // Now press another key -> the search string contains all pressed keys
119 m_keyboardSearchManager
.addKeys("q");
120 QCOMPARE(spy
.count(), 1);
121 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "pppq" << false);
124 void KItemListKeyboardSearchManagerTest::testPressShift()
126 // If the user presses Shift, i.e., to get a character like '_',
127 // KItemListController calls the addKeys(QString) method with an empty
128 // string. Make sure that this does not reset the current search. See
129 // https://bugs.kde.org/show_bug.cgi?id=321286
131 QSignalSpy
spy(&m_keyboardSearchManager
, SIGNAL(changeCurrentItem(QString
,bool)));
133 // Simulate that the user enters "a_b".
134 m_keyboardSearchManager
.addKeys("a");
135 QCOMPARE(spy
.count(), 1);
136 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a" << true);
138 m_keyboardSearchManager
.addKeys("");
139 QCOMPARE(spy
.count(), 0);
141 m_keyboardSearchManager
.addKeys("_");
142 QCOMPARE(spy
.count(), 1);
143 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a_" << false);
145 m_keyboardSearchManager
.addKeys("b");
146 QCOMPARE(spy
.count(), 1);
147 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a_b" << false);
150 QTEST_KDEMAIN(KItemListKeyboardSearchManagerTest
, NoGUI
)
152 #include "kitemlistkeyboardsearchmanagertest.moc"