]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistkeyboardsearchmanagertest.cpp
53ef9ec3cac349d1baba9d14ddae401b00adac5b
[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 "kitemviews/private/kitemlistkeyboardsearchmanager.h"
21
22 #include <QTest>
23 #include <QSignalSpy>
24
25 class KItemListKeyboardSearchManagerTest : public QObject
26 {
27 Q_OBJECT
28
29 private slots:
30 void init();
31
32 void testBasicKeyboardSearch();
33 void testAbortedKeyboardSearch();
34 void testRepeatedKeyPress();
35 void testPressShift();
36
37 private:
38 KItemListKeyboardSearchManager m_keyboardSearchManager;
39 };
40
41 void KItemListKeyboardSearchManagerTest::init()
42 {
43 // Make sure that the previous search string is cleared
44 m_keyboardSearchManager.cancelSearch();
45 }
46
47 void KItemListKeyboardSearchManagerTest::testBasicKeyboardSearch()
48 {
49 QSignalSpy spy(&m_keyboardSearchManager, &KItemListKeyboardSearchManager::changeCurrentItem);
50 QVERIFY(spy.isValid());
51
52 m_keyboardSearchManager.addKeys("f");
53 QCOMPARE(spy.count(), 1);
54 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << false);
55
56 m_keyboardSearchManager.addKeys("i");
57 QCOMPARE(spy.count(), 1);
58 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fi" << false);
59
60 m_keyboardSearchManager.addKeys("l");
61 QCOMPARE(spy.count(), 1);
62 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fil" << false);
63
64 m_keyboardSearchManager.addKeys("e");
65 QCOMPARE(spy.count(), 1);
66 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "file" << false);
67 }
68
69 void KItemListKeyboardSearchManagerTest::testAbortedKeyboardSearch()
70 {
71 // Set the timeout to a small value (the default is 5000 milliseconds)
72 // to save time when running this test.
73 m_keyboardSearchManager.setTimeout(100);
74
75 QSignalSpy spy(&m_keyboardSearchManager, &KItemListKeyboardSearchManager::changeCurrentItem);
76 QVERIFY(spy.isValid());
77
78 m_keyboardSearchManager.addKeys("f");
79 QCOMPARE(spy.count(), 1);
80 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << false);
81
82 m_keyboardSearchManager.addKeys("i");
83 QCOMPARE(spy.count(), 1);
84 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fi" << false);
85
86 // If the delay between two key presses is larger than the chosen timeout,
87 // a new search is started. We add a small safety margin to avoid race conditions.
88 QTest::qWait(m_keyboardSearchManager.timeout() + 10);
89
90 m_keyboardSearchManager.addKeys("l");
91 QCOMPARE(spy.count(), 1);
92 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "l" << true);
93
94 m_keyboardSearchManager.addKeys("e");
95 QCOMPARE(spy.count(), 1);
96 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "le" << false);
97
98 // the selection was deselected, for instance with Esc or a click outside the selection
99 m_keyboardSearchManager.slotSelectionChanged(KItemSet(), KItemSet() << 1);
100
101 m_keyboardSearchManager.addKeys("a");
102 QCOMPARE(spy.count(), 1);
103 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a" << false);
104 }
105
106 void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress()
107 {
108 // If the same key is pressed repeatedly, the next matching item should be highlighted after
109 // each key press. To achieve, that, the manager emits the changeCurrentItem(QString,bool)
110 // signal, where
111 // 1. the string contains the repeated key only once, and
112 // 2. the bool searchFromNextItem is true.
113
114 QSignalSpy spy(&m_keyboardSearchManager, &KItemListKeyboardSearchManager::changeCurrentItem);
115 QVERIFY(spy.isValid());
116
117 m_keyboardSearchManager.addKeys("p");
118 QCOMPARE(spy.count(), 1);
119 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << false);
120
121 m_keyboardSearchManager.addKeys("p");
122 QCOMPARE(spy.count(), 1);
123 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true);
124
125 m_keyboardSearchManager.addKeys("p");
126 QCOMPARE(spy.count(), 1);
127 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true);
128
129 // Now press another key -> the search string contains all pressed keys
130 m_keyboardSearchManager.addKeys("q");
131 QCOMPARE(spy.count(), 1);
132 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "pppq" << false);
133 }
134
135 void KItemListKeyboardSearchManagerTest::testPressShift()
136 {
137 // If the user presses Shift, i.e., to get a character like '_',
138 // KItemListController calls the addKeys(QString) method with an empty
139 // string. Make sure that this does not reset the current search. See
140 // https://bugs.kde.org/show_bug.cgi?id=321286
141
142 QSignalSpy spy(&m_keyboardSearchManager, &KItemListKeyboardSearchManager::changeCurrentItem);
143 QVERIFY(spy.isValid());
144
145 // Simulate that the user enters "a_b".
146 m_keyboardSearchManager.addKeys("a");
147 QCOMPARE(spy.count(), 1);
148 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a" << false);
149
150 m_keyboardSearchManager.addKeys("");
151 QCOMPARE(spy.count(), 0);
152
153 m_keyboardSearchManager.addKeys("_");
154 QCOMPARE(spy.count(), 1);
155 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a_" << false);
156
157 m_keyboardSearchManager.addKeys("b");
158 QCOMPARE(spy.count(), 1);
159 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a_b" << false);
160 }
161
162 QTEST_GUILESS_MAIN(KItemListKeyboardSearchManagerTest)
163
164 #include "kitemlistkeyboardsearchmanagertest.moc"