diff options
| author | Frank Reininghaus <[email protected]> | 2011-08-30 13:10:38 +0200 |
|---|---|---|
| committer | Frank Reininghaus <[email protected]> | 2011-08-30 18:22:56 +0200 |
| commit | 9dc7cd79e7f650ccf57520888c069eef5d03a57f (patch) | |
| tree | 2065e7b8ce20ee1127a03af618357258c0e6d0b8 /src/tests/kitemlistkeyboardsearchmanagertest.cpp | |
| parent | 1e897556b035cf950a8071266d18b3e191d4c7d7 (diff) | |
KItemListKeyboardSearchManager improvements and unit tests
This commit implements a 'repeated key search' feature, similar
to QAbstractItemView, and adds unit tests for keyboard searching.
Diffstat (limited to 'src/tests/kitemlistkeyboardsearchmanagertest.cpp')
| -rw-r--r-- | src/tests/kitemlistkeyboardsearchmanagertest.cpp | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/tests/kitemlistkeyboardsearchmanagertest.cpp b/src/tests/kitemlistkeyboardsearchmanagertest.cpp new file mode 100644 index 000000000..be483930d --- /dev/null +++ b/src/tests/kitemlistkeyboardsearchmanagertest.cpp @@ -0,0 +1,121 @@ +/*************************************************************************** + * Copyright (C) 2011 by Frank Reininghaus <[email protected]> * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#include <qtest_kde.h> + +#include "kitemviews/kitemlistkeyboardsearchmanager_p.h" + +class KItemListKeyboardSearchManagerTest : public QObject +{ + Q_OBJECT + +private slots: + void init(); + + void testBasicKeyboardSearch(); + void testAbortedKeyboardSearch(); + void testRepeatedKeyPress(); + +private: + KItemListKeyboardSearchManager m_keyboardSearchManager; +}; + +void KItemListKeyboardSearchManagerTest::init() +{ + // Make sure that the previous search string is cleared + m_keyboardSearchManager.addKeys(""); +} + +void KItemListKeyboardSearchManagerTest::testBasicKeyboardSearch() +{ + QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool))); + + m_keyboardSearchManager.addKeys("f"); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << true); + + m_keyboardSearchManager.addKeys("i"); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fi" << false); + + m_keyboardSearchManager.addKeys("l"); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fil" << false); + + m_keyboardSearchManager.addKeys("e"); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst(), QList<QVariant>() << "file" << false); +} + +void KItemListKeyboardSearchManagerTest::testAbortedKeyboardSearch() +{ + QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool))); + + m_keyboardSearchManager.addKeys("f"); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst(), QList<QVariant>() << "f" << true); + + m_keyboardSearchManager.addKeys("i"); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst(), QList<QVariant>() << "fi" << false); + + // If the delay between two key presses is larger than QApplication::keyboardInputInterval(), + // a new search is started. We add a small safety margin to avoid race conditions. + QTest::qWait(QApplication::keyboardInputInterval() + 10); + + m_keyboardSearchManager.addKeys("l"); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst(), QList<QVariant>() << "l" << true); + + m_keyboardSearchManager.addKeys("e"); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst(), QList<QVariant>() << "le" << false); +} + +void KItemListKeyboardSearchManagerTest::testRepeatedKeyPress() +{ + // If the same key is pressed repeatedly, the next matching item should be highlighted after + // each key press. To achieve, that, the manager emits the changeCurrentItem(QString,bool) + // signal, where + // 1. the string contains the repeated key only once, and + // 2. the bool searchFromNextItem is true. + + QSignalSpy spy(&m_keyboardSearchManager, SIGNAL(changeCurrentItem(QString,bool))); + + m_keyboardSearchManager.addKeys("p"); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true); + + m_keyboardSearchManager.addKeys("p"); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true); + + m_keyboardSearchManager.addKeys("p"); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst(), QList<QVariant>() << "p" << true); + + // Now press another key -> the search string contains all pressed keys + m_keyboardSearchManager.addKeys("q"); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.takeFirst(), QList<QVariant>() << "pppq" << false); +} + +QTEST_KDEMAIN(KItemListKeyboardSearchManagerTest, NoGUI) + +#include "kitemlistkeyboardsearchmanagertest.moc" |
