2 * SPDX-FileCopyrightText: 2011 Frank Reininghaus <frank78ac@googlemail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "kitemviews/private/kitemlistkeyboardsearchmanager.h"
10 #include <QStandardPaths>
13 class KItemListKeyboardSearchManagerTest
: public QObject
21 void testBasicKeyboardSearch();
22 void testAbortedKeyboardSearch();
23 void testRepeatedKeyPress();
24 void testPressShift();
27 KItemListKeyboardSearchManager m_keyboardSearchManager
;
30 void KItemListKeyboardSearchManagerTest::initTestCase()
32 QStandardPaths::setTestModeEnabled(true);
35 void KItemListKeyboardSearchManagerTest::init()
37 // Make sure that the previous search string is cleared
38 m_keyboardSearchManager
.cancelSearch();
41 void KItemListKeyboardSearchManagerTest::testBasicKeyboardSearch()
43 QSignalSpy
spy(&m_keyboardSearchManager
, &KItemListKeyboardSearchManager::changeCurrentItem
);
44 QVERIFY(spy
.isValid());
46 m_keyboardSearchManager
.addKeys("f");
47 QCOMPARE(spy
.count(), 1);
48 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "f" << true);
50 m_keyboardSearchManager
.addKeys("i");
51 QCOMPARE(spy
.count(), 1);
52 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fi" << false);
54 m_keyboardSearchManager
.addKeys("l");
55 QCOMPARE(spy
.count(), 1);
56 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fil" << false);
58 m_keyboardSearchManager
.addKeys("e");
59 QCOMPARE(spy
.count(), 1);
60 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "file" << false);
63 void KItemListKeyboardSearchManagerTest::testAbortedKeyboardSearch()
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);
69 QSignalSpy
spy(&m_keyboardSearchManager
, &KItemListKeyboardSearchManager::changeCurrentItem
);
70 QVERIFY(spy
.isValid());
72 m_keyboardSearchManager
.addKeys("f");
73 QCOMPARE(spy
.count(), 1);
74 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "f" << true);
76 m_keyboardSearchManager
.addKeys("i");
77 QCOMPARE(spy
.count(), 1);
78 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fi" << false);
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);
84 m_keyboardSearchManager
.addKeys("l");
85 QCOMPARE(spy
.count(), 1);
86 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "l" << true);
88 m_keyboardSearchManager
.addKeys("e");
89 QCOMPARE(spy
.count(), 1);
90 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "le" << false);
92 // the selection was deselected, for instance with Esc or a click outside the selection
93 m_keyboardSearchManager
.slotSelectionChanged(KItemSet(), KItemSet() << 1);
95 m_keyboardSearchManager
.addKeys("a");
96 QCOMPARE(spy
.count(), 1);
97 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a" << true);
100 void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress()
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)
105 // 1. the string contains the repeated key only once, and
106 // 2. the bool searchFromNextItem is true.
108 QSignalSpy
spy(&m_keyboardSearchManager
, &KItemListKeyboardSearchManager::changeCurrentItem
);
109 QVERIFY(spy
.isValid());
111 m_keyboardSearchManager
.addKeys("p");
112 QCOMPARE(spy
.count(), 1);
113 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
115 m_keyboardSearchManager
.addKeys("p");
116 QCOMPARE(spy
.count(), 1);
117 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
119 m_keyboardSearchManager
.addKeys("p");
120 QCOMPARE(spy
.count(), 1);
121 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
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);
129 void KItemListKeyboardSearchManagerTest::testPressShift()
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
136 QSignalSpy
spy(&m_keyboardSearchManager
, &KItemListKeyboardSearchManager::changeCurrentItem
);
137 QVERIFY(spy
.isValid());
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" << true);
144 m_keyboardSearchManager
.addKeys("");
145 QCOMPARE(spy
.count(), 0);
147 m_keyboardSearchManager
.addKeys("_");
148 QCOMPARE(spy
.count(), 1);
149 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a_" << false);
151 m_keyboardSearchManager
.addKeys("b");
152 QCOMPARE(spy
.count(), 1);
153 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a_b" << false);
156 QTEST_GUILESS_MAIN(KItemListKeyboardSearchManagerTest
)
158 #include "kitemlistkeyboardsearchmanagertest.moc"