1 /***************************************************************************
2 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "kitemviews/private/kitemlistkeyboardsearchmanager.h"
25 class KItemListKeyboardSearchManagerTest
: public QObject
32 void testBasicKeyboardSearch();
33 void testAbortedKeyboardSearch();
34 void testRepeatedKeyPress();
35 void testPressShift();
38 KItemListKeyboardSearchManager m_keyboardSearchManager
;
41 void KItemListKeyboardSearchManagerTest::init()
43 // Make sure that the previous search string is cleared
44 m_keyboardSearchManager
.cancelSearch();
47 void KItemListKeyboardSearchManagerTest::testBasicKeyboardSearch()
49 QSignalSpy
spy(&m_keyboardSearchManager
, &KItemListKeyboardSearchManager::changeCurrentItem
);
50 QVERIFY(spy
.isValid());
52 m_keyboardSearchManager
.addKeys("f");
53 QCOMPARE(spy
.count(), 1);
54 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "f" << false);
56 m_keyboardSearchManager
.addKeys("i");
57 QCOMPARE(spy
.count(), 1);
58 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fi" << false);
60 m_keyboardSearchManager
.addKeys("l");
61 QCOMPARE(spy
.count(), 1);
62 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fil" << false);
64 m_keyboardSearchManager
.addKeys("e");
65 QCOMPARE(spy
.count(), 1);
66 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "file" << false);
69 void KItemListKeyboardSearchManagerTest::testAbortedKeyboardSearch()
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);
75 QSignalSpy
spy(&m_keyboardSearchManager
, &KItemListKeyboardSearchManager::changeCurrentItem
);
76 QVERIFY(spy
.isValid());
78 m_keyboardSearchManager
.addKeys("f");
79 QCOMPARE(spy
.count(), 1);
80 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "f" << false);
82 m_keyboardSearchManager
.addKeys("i");
83 QCOMPARE(spy
.count(), 1);
84 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "fi" << false);
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);
90 m_keyboardSearchManager
.addKeys("l");
91 QCOMPARE(spy
.count(), 1);
92 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "l" << true);
94 m_keyboardSearchManager
.addKeys("e");
95 QCOMPARE(spy
.count(), 1);
96 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "le" << false);
98 // the selection was deselected, for instance with Esc or a click outside the selection
99 m_keyboardSearchManager
.slotSelectionChanged(KItemSet(), KItemSet() << 1);
101 m_keyboardSearchManager
.addKeys("a");
102 QCOMPARE(spy
.count(), 1);
103 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a" << false);
106 void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress()
108 // If the same key is pressed repeatedly, the next matching item should be highlighted after
109 // each key press. To achieve, that, the manager emits the changeCurrentItem(QString,bool)
111 // 1. the string contains the repeated key only once, and
112 // 2. the bool searchFromNextItem is true.
114 QSignalSpy
spy(&m_keyboardSearchManager
, &KItemListKeyboardSearchManager::changeCurrentItem
);
115 QVERIFY(spy
.isValid());
117 m_keyboardSearchManager
.addKeys("p");
118 QCOMPARE(spy
.count(), 1);
119 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << false);
121 m_keyboardSearchManager
.addKeys("p");
122 QCOMPARE(spy
.count(), 1);
123 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
125 m_keyboardSearchManager
.addKeys("p");
126 QCOMPARE(spy
.count(), 1);
127 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "p" << true);
129 // Now press another key -> the search string contains all pressed keys
130 m_keyboardSearchManager
.addKeys("q");
131 QCOMPARE(spy
.count(), 1);
132 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "pppq" << false);
135 void KItemListKeyboardSearchManagerTest::testPressShift()
137 // If the user presses Shift, i.e., to get a character like '_',
138 // KItemListController calls the addKeys(QString) method with an empty
139 // string. Make sure that this does not reset the current search. See
140 // https://bugs.kde.org/show_bug.cgi?id=321286
142 QSignalSpy
spy(&m_keyboardSearchManager
, &KItemListKeyboardSearchManager::changeCurrentItem
);
143 QVERIFY(spy
.isValid());
145 // Simulate that the user enters "a_b".
146 m_keyboardSearchManager
.addKeys("a");
147 QCOMPARE(spy
.count(), 1);
148 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a" << false);
150 m_keyboardSearchManager
.addKeys("");
151 QCOMPARE(spy
.count(), 0);
153 m_keyboardSearchManager
.addKeys("_");
154 QCOMPARE(spy
.count(), 1);
155 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a_" << false);
157 m_keyboardSearchManager
.addKeys("b");
158 QCOMPARE(spy
.count(), 1);
159 QCOMPARE(spy
.takeFirst(), QList
<QVariant
>() << "a_b" << false);
162 QTEST_GUILESS_MAIN(KItemListKeyboardSearchManagerTest
)
164 #include "kitemlistkeyboardsearchmanagertest.moc"