]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistkeyboardsearchmanagertest.cpp
Increase the timeout in KItemListKeyboardSearchManager to 5 seconds
[dolphin.git] / src / tests / kitemlistkeyboardsearchmanagertest.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include <qtest_kde.h>
21
22 #include "kitemviews/kitemlistkeyboardsearchmanager_p.h"
23
24 class KItemListKeyboardSearchManagerTest : public QObject
25 {
26 Q_OBJECT
27
28 private slots:
29 void init();
30
31 void testBasicKeyboardSearch();
32 void testAbortedKeyboardSearch();
33 void testRepeatedKeyPress();
34
35 private:
36 KItemListKeyboardSearchManager m_keyboardSearchManager;
37 };
38
39 void KItemListKeyboardSearchManagerTest::init()
40 {
41 // Make sure that the previous search string is cleared
42 m_keyboardSearchManager.addKeys("");
43 }
44
45 void KItemListKeyboardSearchManagerTest::testBasicKeyboardSearch()
46 {
47 QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool)));
48
49 m_keyboardSearchManager.addKeys("f");
50 QCOMPARE(spy.count(), 1);
51 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << true);
52
53 m_keyboardSearchManager.addKeys("i");
54 QCOMPARE(spy.count(), 1);
55 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fi" << false);
56
57 m_keyboardSearchManager.addKeys("l");
58 QCOMPARE(spy.count(), 1);
59 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fil" << false);
60
61 m_keyboardSearchManager.addKeys("e");
62 QCOMPARE(spy.count(), 1);
63 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "file" << false);
64 }
65
66 void KItemListKeyboardSearchManagerTest::testAbortedKeyboardSearch()
67 {
68 QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool)));
69
70 m_keyboardSearchManager.addKeys("f");
71 QCOMPARE(spy.count(), 1);
72 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << true);
73
74 m_keyboardSearchManager.addKeys("i");
75 QCOMPARE(spy.count(), 1);
76 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fi" << false);
77
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);
81
82 m_keyboardSearchManager.addKeys("l");
83 QCOMPARE(spy.count(), 1);
84 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "l" << true);
85
86 m_keyboardSearchManager.addKeys("e");
87 QCOMPARE(spy.count(), 1);
88 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "le" << false);
89 }
90
91 void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress()
92 {
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)
95 // signal, where
96 // 1. the string contains the repeated key only once, and
97 // 2. the bool searchFromNextItem is true.
98
99 QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool)));
100
101 m_keyboardSearchManager.addKeys("p");
102 QCOMPARE(spy.count(), 1);
103 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true);
104
105 m_keyboardSearchManager.addKeys("p");
106 QCOMPARE(spy.count(), 1);
107 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true);
108
109 m_keyboardSearchManager.addKeys("p");
110 QCOMPARE(spy.count(), 1);
111 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true);
112
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);
117 }
118
119 QTEST_KDEMAIN(KItemListKeyboardSearchManagerTest, NoGUI)
120
121 #include "kitemlistkeyboardsearchmanagertest.moc"