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();
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 // Set the timeout to a small value (the default is 5000 milliseconds)
69 // to save time when running this test.
70 m_keyboardSearchManager
.setTimeout(100);
72 QSignalSpy
spy(&m_keyboardSearchManager
, SIGNAL(changeCurrentItem(QString
,bool)));
74 m_keyboardSearchManager
.addKeys("f");
75 QCOMPARE(spy
.count(), 1);
76 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "f" << true);
78 m_keyboardSearchManager
.addKeys("i");
79 QCOMPARE(spy
.count(), 1);
80 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fi" << false);
82 // If the delay between two key presses is larger than the chosen timeout,
83 // a new search is started. We add a small safety margin to avoid race conditions.
84 QTest::qWait(m_keyboardSearchManager
.timeout() + 10);
86 m_keyboardSearchManager
.addKeys("l");
87 QCOMPARE(spy
.count(), 1);
88 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "l" << true);
90 m_keyboardSearchManager
.addKeys("e");
91 QCOMPARE(spy
.count(), 1);
92 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "le" << false);
95 void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress()
97 // If the same key is pressed repeatedly, the next matching item should be highlighted after
98 // each key press. To achieve, that, the manager emits the changeCurrentItem(QString,bool)
100 // 1. the string contains the repeated key only once, and
101 // 2. the bool searchFromNextItem is true.
103 QSignalSpy
spy(&m_keyboardSearchManager
, SIGNAL(changeCurrentItem(QString
,bool)));
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 m_keyboardSearchManager
.addKeys("p");
114 QCOMPARE(spy
.count(), 1);
115 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
117 // Now press another key -> the search string contains all pressed keys
118 m_keyboardSearchManager
.addKeys("q");
119 QCOMPARE(spy
.count(), 1);
120 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "pppq" << false);
123 QTEST_KDEMAIN(KItemListKeyboardSearchManagerTest
, NoGUI
)
125 #include "kitemlistkeyboardsearchmanagertest.moc"