]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistkeyboardsearchmanagertest.cpp
Allow renaming multiple files without number if extensions are different
[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/private/kitemlistkeyboardsearchmanager.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 // 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);
71
72 QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool)));
73
74 m_keyboardSearchManager.addKeys("f");
75 QCOMPARE(spy.count(), 1);
76 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << true);
77
78 m_keyboardSearchManager.addKeys("i");
79 QCOMPARE(spy.count(), 1);
80 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fi" << false);
81
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);
85
86 m_keyboardSearchManager.addKeys("l");
87 QCOMPARE(spy.count(), 1);
88 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "l" << true);
89
90 m_keyboardSearchManager.addKeys("e");
91 QCOMPARE(spy.count(), 1);
92 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "le" << false);
93 }
94
95 void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress()
96 {
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)
99 // signal, where
100 // 1. the string contains the repeated key only once, and
101 // 2. the bool searchFromNextItem is true.
102
103 QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool)));
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 m_keyboardSearchManager.addKeys("p");
114 QCOMPARE(spy.count(), 1);
115 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true);
116
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);
121 }
122
123 QTEST_KDEMAIN(KItemListKeyboardSearchManagerTest, NoGUI)
124
125 #include "kitemlistkeyboardsearchmanagertest.moc"