diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2018-05-04 10:40:38 +0200 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2018-05-07 14:53:18 +0000 |
commit | 630b06f86cdcb29fe65df05d20f31f39f82cbeed (patch) | |
tree | 3743fa52a111efa455ecb4701bc48200aaf0187a /tests/auto/testlib/selftests/keyboard | |
parent | b9835aef1a36688ea99602e25e9c73e645d3851a (diff) |
Add self-tests for QTest::keyPress() and QTest::keyRelease()
They didn't exist up until now, and future patches rely on them, so
add them.
Change-Id: I8afdb9417263b45d43355c688a813bdf99ea5fc8
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
Diffstat (limited to 'tests/auto/testlib/selftests/keyboard')
-rw-r--r-- | tests/auto/testlib/selftests/keyboard/keyboard.pro | 7 | ||||
-rw-r--r-- | tests/auto/testlib/selftests/keyboard/tst_keyboard.cpp | 88 |
2 files changed, 95 insertions, 0 deletions
diff --git a/tests/auto/testlib/selftests/keyboard/keyboard.pro b/tests/auto/testlib/selftests/keyboard/keyboard.pro new file mode 100644 index 0000000000..0097318797 --- /dev/null +++ b/tests/auto/testlib/selftests/keyboard/keyboard.pro @@ -0,0 +1,7 @@ +SOURCES += tst_keyboard.cpp +QT += testlib testlib-private gui gui-private + +macos:CONFIG -= app_bundle +CONFIG -= debug_and_release_target + +TARGET = keyboard diff --git a/tests/auto/testlib/selftests/keyboard/tst_keyboard.cpp b/tests/auto/testlib/selftests/keyboard/tst_keyboard.cpp new file mode 100644 index 0000000000..e185be8532 --- /dev/null +++ b/tests/auto/testlib/selftests/keyboard/tst_keyboard.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/qtest.h> +#include <QtGui/qwindow.h> + +class tst_Keyboard : public QObject +{ + Q_OBJECT + +private slots: + void keyPressAndRelease(); +}; + +class KeyWindow : public QWindow +{ +public: + void keyPressEvent(QKeyEvent *event) override + { + QSharedPointer<QKeyEvent> copiedEvent(new QKeyEvent(event->type(), event->key(), + event->modifiers(), event->text(), event->isAutoRepeat(), event->count())); + mEventOrder.append(copiedEvent); + } + + void keyReleaseEvent(QKeyEvent *event) override + { + QSharedPointer<QKeyEvent> copiedEvent(new QKeyEvent(event->type(), event->key(), + event->modifiers(), event->text(), event->isAutoRepeat(), event->count())); + mEventOrder.append(copiedEvent); + } + + QVector<QSharedPointer<QKeyEvent>> mEventOrder; +}; + +void tst_Keyboard::keyPressAndRelease() +{ + KeyWindow window; + window.show(); + window.setGeometry(100, 100, 200, 200); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + QVERIFY(QTest::qWaitForWindowActive(&window)); + + QTest::keyPress(&window, Qt::Key_A); + QTest::keyRelease(&window, Qt::Key_A); + QCOMPARE(window.mEventOrder.size(), 2); + + const auto pressEvent = window.mEventOrder.at(0); + QCOMPARE(pressEvent->type(), QEvent::KeyPress); + QCOMPARE(pressEvent->key(), Qt::Key_A); + QCOMPARE(pressEvent->modifiers(), Qt::NoModifier); + QCOMPARE(pressEvent->text(), "a"); + QCOMPARE(pressEvent->isAutoRepeat(), false); + + const auto releaseEvent = window.mEventOrder.at(1); + QCOMPARE(releaseEvent->type(), QEvent::KeyRelease); + QCOMPARE(releaseEvent->key(), Qt::Key_A); + QCOMPARE(releaseEvent->modifiers(), Qt::NoModifier); + QCOMPARE(releaseEvent->text(), "a"); + QCOMPARE(releaseEvent->isAutoRepeat(), false); +} + +QTEST_MAIN(tst_Keyboard) +#include "tst_keyboard.moc" |