]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistkeyboardsearchmanagertest.cpp
7d5fc3b9ab94e9dfb142fd001251a644f16a5322
[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 void testPressShift();
35
36 private:
37 KItemListKeyboardSearchManager m_keyboardSearchManager;
38 };
39
40 void KItemListKeyboardSearchManagerTest::init()
41 {
42 // Make sure that the previous search string is cleared
43 m_keyboardSearchManager.cancelSearch();
44 }
45
46 void KItemListKeyboardSearchManagerTest::testBasicKeyboardSearch()
47 {
48 QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool)));
49
50 m_keyboardSearchManager.addKeys("f");
51 QCOMPARE(spy.count(), 1);
52 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << true);
53
54 m_keyboardSearchManager.addKeys("i");
55 QCOMPARE(spy.count(), 1);
56 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fi" << false);
57
58 m_keyboardSearchManager.addKeys("l");
59 QCOMPARE(spy.count(), 1);
60 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fil" << false);
61
62 m_keyboardSearchManager.addKeys("e");
63 QCOMPARE(spy.count(), 1);
64 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "file" << false);
65 }
66
67 void KItemListKeyboardSearchManagerTest::testAbortedKeyboardSearch()
68 {
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);
72
73 QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool)));
74
75 m_keyboardSearchManager.addKeys("f");
76 QCOMPARE(spy.count(), 1);
77 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << true);
78
79 m_keyboardSearchManager.addKeys("i");
80 QCOMPARE(spy.count(), 1);
81 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fi" << false);
82
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);
86
87 m_keyboardSearchManager.addKeys("l");
88 QCOMPARE(spy.count(), 1);
89 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "l" << true);
90
91 m_keyboardSearchManager.addKeys("e");
92 QCOMPARE(spy.count(), 1);
93 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "le" << false);
94 }
95
96 void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress()
97 {
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)
100 // signal, where
101 // 1. the string contains the repeated key only once, and
102 // 2. the bool searchFromNextItem is true.
103
104 QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool)));
105
106 m_keyboardSearchManager.addKeys("p");
107 QCOMPARE(spy.count(), 1);
108 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true);
109
110 m_keyboardSearchManager.addKeys("p");
111 QCOMPARE(spy.count(), 1);
112 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true);
113
114 m_keyboardSearchManager.addKeys("p");
115 QCOMPARE(spy.count(), 1);
116 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true);
117
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);
122 }
123
124 void KItemListKeyboardSearchManagerTest::testPressShift()
125 {
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
130
131 QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool)));
132
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);
137
138 m_keyboardSearchManager.addKeys("");
139 QCOMPARE(spy.count(), 0);
140
141 m_keyboardSearchManager.addKeys("_");
142 QCOMPARE(spy.count(), 1);
143 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a_" << false);
144
145 m_keyboardSearchManager.addKeys("b");
146 QCOMPARE(spy.count(), 1);
147 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a_b" << false);
148 }
149
150 QTEST_KDEMAIN(KItemListKeyboardSearchManagerTest, NoGUI)
151
152 #include "kitemlistkeyboardsearchmanagertest.moc"