File indexing completed on 2024-04-28 05:45:21

0001 /*
0002  *   SPDX-FileCopyrightText: 2011 Frank Reininghaus <frank78ac@googlemail.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kitemviews/private/kitemlistkeyboardsearchmanager.h"
0008 
0009 #include <QSignalSpy>
0010 #include <QStandardPaths>
0011 #include <QTest>
0012 
0013 class KItemListKeyboardSearchManagerTest : public QObject
0014 {
0015     Q_OBJECT
0016 
0017 private Q_SLOTS:
0018     void initTestCase();
0019     void init();
0020 
0021     void testBasicKeyboardSearch();
0022     void testAbortedKeyboardSearch();
0023     void testRepeatedKeyPress();
0024     void testPressShift();
0025 
0026 private:
0027     KItemListKeyboardSearchManager m_keyboardSearchManager;
0028 };
0029 
0030 void KItemListKeyboardSearchManagerTest::initTestCase()
0031 {
0032     QStandardPaths::setTestModeEnabled(true);
0033 }
0034 
0035 void KItemListKeyboardSearchManagerTest::init()
0036 {
0037     // Make sure that the previous search string is cleared
0038     m_keyboardSearchManager.cancelSearch();
0039 }
0040 
0041 void KItemListKeyboardSearchManagerTest::testBasicKeyboardSearch()
0042 {
0043     QSignalSpy spy(&m_keyboardSearchManager, &KItemListKeyboardSearchManager::changeCurrentItem);
0044     QVERIFY(spy.isValid());
0045 
0046     m_keyboardSearchManager.addKeys("f");
0047     QCOMPARE(spy.count(), 1);
0048     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << false);
0049 
0050     m_keyboardSearchManager.addKeys("i");
0051     QCOMPARE(spy.count(), 1);
0052     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fi" << false);
0053 
0054     m_keyboardSearchManager.addKeys("l");
0055     QCOMPARE(spy.count(), 1);
0056     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fil" << false);
0057 
0058     m_keyboardSearchManager.addKeys("e");
0059     QCOMPARE(spy.count(), 1);
0060     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "file" << false);
0061 }
0062 
0063 void KItemListKeyboardSearchManagerTest::testAbortedKeyboardSearch()
0064 {
0065     // Set the timeout to a small value (the default is 5000 milliseconds)
0066     // to save time when running this test.
0067     m_keyboardSearchManager.setTimeout(100);
0068 
0069     QSignalSpy spy(&m_keyboardSearchManager, &KItemListKeyboardSearchManager::changeCurrentItem);
0070     QVERIFY(spy.isValid());
0071 
0072     m_keyboardSearchManager.addKeys("f");
0073     QCOMPARE(spy.count(), 1);
0074     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << false);
0075 
0076     m_keyboardSearchManager.addKeys("i");
0077     QCOMPARE(spy.count(), 1);
0078     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fi" << false);
0079 
0080     // If the delay between two key presses is larger than the chosen timeout,
0081     // a new search is started. We add a small safety margin to avoid race conditions.
0082     QTest::qWait(m_keyboardSearchManager.timeout() + 10);
0083 
0084     m_keyboardSearchManager.addKeys("l");
0085     QCOMPARE(spy.count(), 1);
0086     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "l" << true);
0087 
0088     m_keyboardSearchManager.addKeys("e");
0089     QCOMPARE(spy.count(), 1);
0090     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "le" << false);
0091 
0092     // the selection was deselected, for instance with Esc or a click outside the selection
0093     m_keyboardSearchManager.slotSelectionChanged(KItemSet(), KItemSet() << 1);
0094 
0095     m_keyboardSearchManager.addKeys("a");
0096     QCOMPARE(spy.count(), 1);
0097     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a" << false);
0098 }
0099 
0100 void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress()
0101 {
0102     // If the same key is pressed repeatedly, the next matching item should be highlighted after
0103     // each key press. To achieve, that, the manager emits the changeCurrentItem(QString,bool)
0104     // signal, where
0105     // 1. the string contains the repeated key only once, and
0106     // 2. the bool searchFromNextItem is true.
0107 
0108     QSignalSpy spy(&m_keyboardSearchManager, &KItemListKeyboardSearchManager::changeCurrentItem);
0109     QVERIFY(spy.isValid());
0110 
0111     m_keyboardSearchManager.addKeys("p");
0112     QCOMPARE(spy.count(), 1);
0113     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << false);
0114 
0115     m_keyboardSearchManager.addKeys("p");
0116     QCOMPARE(spy.count(), 1);
0117     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true);
0118 
0119     m_keyboardSearchManager.addKeys("p");
0120     QCOMPARE(spy.count(), 1);
0121     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true);
0122 
0123     // Now press another key -> the search string contains all pressed keys
0124     m_keyboardSearchManager.addKeys("q");
0125     QCOMPARE(spy.count(), 1);
0126     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "pppq" << false);
0127 }
0128 
0129 void KItemListKeyboardSearchManagerTest::testPressShift()
0130 {
0131     // If the user presses Shift, i.e., to get a character like '_',
0132     // KItemListController calls the addKeys(QString) method with an empty
0133     // string. Make sure that this does not reset the current search. See
0134     // https://bugs.kde.org/show_bug.cgi?id=321286
0135 
0136     QSignalSpy spy(&m_keyboardSearchManager, &KItemListKeyboardSearchManager::changeCurrentItem);
0137     QVERIFY(spy.isValid());
0138 
0139     // Simulate that the user enters "a_b".
0140     m_keyboardSearchManager.addKeys("a");
0141     QCOMPARE(spy.count(), 1);
0142     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a" << false);
0143 
0144     m_keyboardSearchManager.addKeys("");
0145     QCOMPARE(spy.count(), 0);
0146 
0147     m_keyboardSearchManager.addKeys("_");
0148     QCOMPARE(spy.count(), 1);
0149     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a_" << false);
0150 
0151     m_keyboardSearchManager.addKeys("b");
0152     QCOMPARE(spy.count(), 1);
0153     QCOMPARE(spy.takeFirst(), QList<QVariant>() << "a_b" << false);
0154 }
0155 
0156 QTEST_GUILESS_MAIN(KItemListKeyboardSearchManagerTest)
0157 
0158 #include "kitemlistkeyboardsearchmanagertest.moc"