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