1/****************************************************************************
2**
3** Copyright (C) 2018 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/qtest.h>
30#include <QtGui/qwindow.h>
31
32class tst_Keyboard : public QObject
33{
34 Q_OBJECT
35
36private slots:
37 void keyPressAndRelease();
38};
39
40class KeyWindow : public QWindow
41{
42public:
43 void keyPressEvent(QKeyEvent *event) override
44 {
45 QSharedPointer<QKeyEvent> copiedEvent(new QKeyEvent(event->type(), event->key(),
46 event->modifiers(), event->text(), event->isAutoRepeat(), event->count()));
47 mEventOrder.append(t: copiedEvent);
48 }
49
50 void keyReleaseEvent(QKeyEvent *event) override
51 {
52 QSharedPointer<QKeyEvent> copiedEvent(new QKeyEvent(event->type(), event->key(),
53 event->modifiers(), event->text(), event->isAutoRepeat(), event->count()));
54 mEventOrder.append(t: copiedEvent);
55 }
56
57 QVector<QSharedPointer<QKeyEvent>> mEventOrder;
58};
59
60void tst_Keyboard::keyPressAndRelease()
61{
62 KeyWindow window;
63 window.show();
64 window.setGeometry(posx: 100, posy: 100, w: 200, h: 200);
65 QVERIFY(QTest::qWaitForWindowExposed(&window));
66 QVERIFY(QTest::qWaitForWindowActive(&window));
67
68 QTest::keyPress(window: &window, key: Qt::Key_A);
69 QTest::keyRelease(window: &window, key: Qt::Key_A);
70 QCOMPARE(window.mEventOrder.size(), 2);
71
72 const auto pressEvent = window.mEventOrder.at(i: 0);
73 QCOMPARE(pressEvent->type(), QEvent::KeyPress);
74 QCOMPARE(pressEvent->key(), Qt::Key_A);
75 QCOMPARE(pressEvent->modifiers(), Qt::NoModifier);
76 QCOMPARE(pressEvent->text(), "a");
77 QCOMPARE(pressEvent->isAutoRepeat(), false);
78
79 const auto releaseEvent = window.mEventOrder.at(i: 1);
80 QCOMPARE(releaseEvent->type(), QEvent::KeyRelease);
81 QCOMPARE(releaseEvent->key(), Qt::Key_A);
82 QCOMPARE(releaseEvent->modifiers(), Qt::NoModifier);
83 QCOMPARE(releaseEvent->text(), "a");
84 QCOMPARE(releaseEvent->isAutoRepeat(), false);
85}
86
87QTEST_MAIN(tst_Keyboard)
88#include "tst_keyboard.moc"
89

source code of qtbase/tests/auto/testlib/selftests/keyboard/tst_keyboard.cpp