]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistkeyboardsearchmanagertest.cpp
Merge branch 'master' into frameworks
[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, SIGNAL(changeCurrentItem(QString, bool)));
50 QVERIFY(spy.isValid());
51
52 m_keyboardSearchManager.addKeys("f");
53 QCOMPARE(spy.count(), 1);
54 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << true);
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, SIGNAL(changeCurrentItem(QString, bool)));
76 QVERIFY(spy.isValid());
77
78 m_keyboardSearchManager.addKeys("f");
79 QCOMPARE(spy.count(), 1);
80 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << true);
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
99 void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress()
100 {
101 // If the same key is pressed repeatedly, the next matching item should be highlighted after
102 // each key press. To achieve, that, the manager emits the changeCurrentItem(QString,bool)
103 // signal, where
104 // 1. the string contains the repeated key only once, and
105 // 2. the bool searchFromNextItem is true.
106
107 QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString, bool)));
108 QVERIFY(spy.isValid());
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 m_keyboardSearchManager.addKeys("p");
119 QCOMPARE(spy.count(), 1);
120 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true);
121
122 // Now press another key -> the search string contains all pressed keys
123 m_keyboardSearchManager.addKeys("q");
124 QCOMPARE(spy.count(), 1);
125 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "pppq" << false);
126 }
127
128 void KItemListKeyboardSearchManagerTest::testPressShift()
129 {
130 // If the user presses Shift, i.e., to get a character like '_',
131 // KItemListController calls the addKeys(QString) method with an empty
132 // string. Make sure that this does not reset the current search. See
133 // https://bugs.kde.org/show_bug.cgi?id=321286
134
135 QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString, bool)));
136 QVERIFY(spy.isValid());
137
138 // Simulate that the user enters "a_b".
139 m_keyboardSearchManager.addKeys("a");
140 QCOMPARE(spy.count(), 1);
141 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a" << true);
142
143 m_keyboardSearchManager.addKeys("");
144 QCOMPARE(spy.count(), 0);
145
146 m_keyboardSearchManager.addKeys("_");
147 QCOMPARE(spy.count(), 1);
148 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a_" << false);
149
150 m_keyboardSearchManager.addKeys("b");
151 QCOMPARE(spy.count(), 1);
152 QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a_b" << false);
153 }
154
155 QTEST_MAIN(KItemListKeyboardSearchManagerTest)
156
157 #include "kitemlistkeyboardsearchmanagertest.moc"