2 * SPDX-FileCopyrightText: 2011 Frank Reininghaus <frank78ac@googlemail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "kitemviews/private/kitemlistkeyboardsearchmanager.h"
12 class KItemListKeyboardSearchManagerTest
: public QObject
19 void testBasicKeyboardSearch();
20 void testAbortedKeyboardSearch();
21 void testRepeatedKeyPress();
22 void testPressShift();
25 KItemListKeyboardSearchManager m_keyboardSearchManager
;
28 void KItemListKeyboardSearchManagerTest::init()
30 // Make sure that the previous search string is cleared
31 m_keyboardSearchManager
.cancelSearch();
34 void KItemListKeyboardSearchManagerTest::testBasicKeyboardSearch()
36 QSignalSpy
spy(&m_keyboardSearchManager
, &KItemListKeyboardSearchManager::changeCurrentItem
);
37 QVERIFY(spy
.isValid());
39 m_keyboardSearchManager
.addKeys("f");
40 QCOMPARE(spy
.count(), 1);
41 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "f" << false);
43 m_keyboardSearchManager
.addKeys("i");
44 QCOMPARE(spy
.count(), 1);
45 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fi" << false);
47 m_keyboardSearchManager
.addKeys("l");
48 QCOMPARE(spy
.count(), 1);
49 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fil" << false);
51 m_keyboardSearchManager
.addKeys("e");
52 QCOMPARE(spy
.count(), 1);
53 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "file" << false);
56 void KItemListKeyboardSearchManagerTest::testAbortedKeyboardSearch()
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);
62 QSignalSpy
spy(&m_keyboardSearchManager
, &KItemListKeyboardSearchManager::changeCurrentItem
);
63 QVERIFY(spy
.isValid());
65 m_keyboardSearchManager
.addKeys("f");
66 QCOMPARE(spy
.count(), 1);
67 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "f" << false);
69 m_keyboardSearchManager
.addKeys("i");
70 QCOMPARE(spy
.count(), 1);
71 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fi" << false);
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);
77 m_keyboardSearchManager
.addKeys("l");
78 QCOMPARE(spy
.count(), 1);
79 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "l" << true);
81 m_keyboardSearchManager
.addKeys("e");
82 QCOMPARE(spy
.count(), 1);
83 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "le" << false);
85 // the selection was deselected, for instance with Esc or a click outside the selection
86 m_keyboardSearchManager
.slotSelectionChanged(KItemSet(), KItemSet() << 1);
88 m_keyboardSearchManager
.addKeys("a");
89 QCOMPARE(spy
.count(), 1);
90 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a" << false);
93 void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress()
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)
98 // 1. the string contains the repeated key only once, and
99 // 2. the bool searchFromNextItem is true.
101 QSignalSpy
spy(&m_keyboardSearchManager
, &KItemListKeyboardSearchManager::changeCurrentItem
);
102 QVERIFY(spy
.isValid());
104 m_keyboardSearchManager
.addKeys("p");
105 QCOMPARE(spy
.count(), 1);
106 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << false);
108 m_keyboardSearchManager
.addKeys("p");
109 QCOMPARE(spy
.count(), 1);
110 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
112 m_keyboardSearchManager
.addKeys("p");
113 QCOMPARE(spy
.count(), 1);
114 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
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);
122 void KItemListKeyboardSearchManagerTest::testPressShift()
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
129 QSignalSpy
spy(&m_keyboardSearchManager
, &KItemListKeyboardSearchManager::changeCurrentItem
);
130 QVERIFY(spy
.isValid());
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);
137 m_keyboardSearchManager
.addKeys("");
138 QCOMPARE(spy
.count(), 0);
140 m_keyboardSearchManager
.addKeys("_");
141 QCOMPARE(spy
.count(), 1);
142 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a_" << false);
144 m_keyboardSearchManager
.addKeys("b");
145 QCOMPARE(spy
.count(), 1);
146 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a_b" << false);
149 QTEST_GUILESS_MAIN(KItemListKeyboardSearchManagerTest
)
151 #include "kitemlistkeyboardsearchmanagertest.moc"