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