1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 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/QtTest> |
30 | |
31 | #include <QtCore/qcoreapplication.h> |
32 | #include <QtGui/qevent.h> |
33 | #include <QtGui/qwindow.h> |
34 | |
35 | class Window : public QWindow |
36 | { |
37 | public: |
38 | ~Window() { reset(); } |
39 | |
40 | void keyPressEvent(QKeyEvent *event) { recordEvent(event); } |
41 | void keyReleaseEvent(QKeyEvent *event) { recordEvent(event); } |
42 | |
43 | void reset() { |
44 | qDeleteAll(begin: keyEvents.begin(), end: keyEvents.end()); |
45 | keyEvents.clear(); |
46 | } |
47 | private: |
48 | void recordEvent(QKeyEvent *event) { |
49 | keyEvents.append(t: new QKeyEvent(event->type(), event->key(), event->modifiers(), event->nativeScanCode(), |
50 | event->nativeVirtualKey(), event->nativeModifiers(), event->text(), |
51 | event->isAutoRepeat(), event->count())); |
52 | } |
53 | |
54 | public: |
55 | QVector<QKeyEvent*> keyEvents; |
56 | }; |
57 | |
58 | class tst_QKeyEvent : public QObject |
59 | { |
60 | Q_OBJECT |
61 | public: |
62 | tst_QKeyEvent(); |
63 | ~tst_QKeyEvent(); |
64 | |
65 | private slots: |
66 | void basicEventDelivery(); |
67 | void modifiers_data(); |
68 | void modifiers(); |
69 | }; |
70 | |
71 | tst_QKeyEvent::tst_QKeyEvent() |
72 | { |
73 | } |
74 | |
75 | tst_QKeyEvent::~tst_QKeyEvent() |
76 | { |
77 | } |
78 | |
79 | void tst_QKeyEvent::basicEventDelivery() |
80 | { |
81 | Window window; |
82 | window.showNormal(); |
83 | QVERIFY(QTest::qWaitForWindowExposed(&window)); |
84 | |
85 | const Qt::Key key = Qt::Key_A; |
86 | const Qt::KeyboardModifier modifiers = Qt::NoModifier; |
87 | |
88 | QTest::keyClick(window: &window, key, modifier: modifiers); |
89 | |
90 | QCOMPARE(window.keyEvents.size(), 2); |
91 | QCOMPARE(window.keyEvents.first()->type(), QKeyEvent::KeyPress); |
92 | QCOMPARE(window.keyEvents.last()->type(), QKeyEvent::KeyRelease); |
93 | foreach (const QKeyEvent *event, window.keyEvents) { |
94 | QCOMPARE(Qt::Key(event->key()), key); |
95 | QCOMPARE(Qt::KeyboardModifiers(event->modifiers()), modifiers); |
96 | } |
97 | } |
98 | |
99 | static bool orderByModifier(const QVector<int> &v1, const QVector<int> &v2) |
100 | { |
101 | if (v1.size() != v2.size()) |
102 | return v1.size() < v2.size(); |
103 | |
104 | for (int i = 0; i < qMin(a: v1.size(), b: v2.size()); ++i) { |
105 | if (v1.at(i) == v2.at(i)) |
106 | continue; |
107 | |
108 | return v1.at(i) < v2.at(i); |
109 | } |
110 | |
111 | return true; |
112 | } |
113 | |
114 | static QByteArray modifiersTestRowName(const QString &keySequence) |
115 | { |
116 | QByteArray result; |
117 | QTextStream str(&result); |
118 | for (int i = 0, size = keySequence.size(); i < size; ++i) { |
119 | const QChar &c = keySequence.at(i); |
120 | const ushort uc = c.unicode(); |
121 | if (uc > 32 && uc < 128) |
122 | str << '"' << c << '"'; |
123 | else |
124 | str << "U+" << Qt::hex << uc << Qt::dec; |
125 | if (i < size - 1) |
126 | str << ','; |
127 | } |
128 | return result; |
129 | } |
130 | |
131 | void tst_QKeyEvent::modifiers_data() |
132 | { |
133 | struct Modifier |
134 | { |
135 | Qt::Key key; |
136 | Qt::KeyboardModifier modifier; |
137 | }; |
138 | static const Modifier modifiers[] = { |
139 | { .key: Qt::Key_Shift, .modifier: Qt::ShiftModifier }, |
140 | { .key: Qt::Key_Control, .modifier: Qt::ControlModifier }, |
141 | { .key: Qt::Key_Alt, .modifier: Qt::AltModifier }, |
142 | { .key: Qt::Key_Meta, .modifier: Qt::MetaModifier }, |
143 | }; |
144 | |
145 | QVector<QVector<int>> modifierCombinations; |
146 | |
147 | // Generate powerset (minus the empty set) of possible modifier combinations |
148 | static const int kNumModifiers = sizeof(modifiers) / sizeof(Modifier); |
149 | for (quint64 bitmask = 1; bitmask < (1 << kNumModifiers) ; ++bitmask) { |
150 | QVector<int> modifierCombination; |
151 | for (quint64 modifier = 0; modifier < kNumModifiers; ++modifier) { |
152 | if (bitmask & (quint64(1) << modifier)) |
153 | modifierCombination.append(t: modifier); |
154 | } |
155 | modifierCombinations.append(t: modifierCombination); |
156 | } |
157 | |
158 | std::sort(first: modifierCombinations.begin(), last: modifierCombinations.end(), comp: orderByModifier); |
159 | |
160 | QTest::addColumn<Qt::KeyboardModifiers>(name: "modifiers" ); |
161 | foreach (const QVector<int> combination, modifierCombinations) { |
162 | int keys[4] = {}; |
163 | Qt::KeyboardModifiers mods; |
164 | for (int i = 0; i < combination.size(); ++i) { |
165 | Modifier modifier = modifiers[combination.at(i)]; |
166 | keys[i] = modifier.key; |
167 | mods |= modifier.modifier; |
168 | } |
169 | QKeySequence keySequence(keys[0], keys[1], keys[2], keys[3]); |
170 | QTest::newRow(dataTag: modifiersTestRowName(keySequence: keySequence.toString(format: QKeySequence::NativeText)).constData()) |
171 | << mods; |
172 | } |
173 | } |
174 | |
175 | void tst_QKeyEvent::modifiers() |
176 | { |
177 | Window window; |
178 | window.showNormal(); |
179 | QVERIFY(QTest::qWaitForWindowExposed(&window)); |
180 | |
181 | const Qt::Key key = Qt::Key_A; |
182 | QFETCH(Qt::KeyboardModifiers, modifiers); |
183 | |
184 | QTest::keyClick(window: &window, key, modifier: modifiers); |
185 | |
186 | int numKeys = qPopulationCount(v: quint64(modifiers)) + 1; |
187 | QCOMPARE(window.keyEvents.size(), numKeys * 2); |
188 | |
189 | for (int i = 0; i < window.keyEvents.size(); ++i) { |
190 | const QKeyEvent *event = window.keyEvents.at(i); |
191 | QCOMPARE(event->type(), i < numKeys ? QKeyEvent::KeyPress : QKeyEvent::KeyRelease); |
192 | if (i == numKeys - 1 || i == numKeys) { |
193 | QCOMPARE(Qt::Key(event->key()), key); |
194 | QCOMPARE(event->modifiers(), modifiers); |
195 | } else { |
196 | QVERIFY(Qt::Key(event->key()) != key); |
197 | } |
198 | } |
199 | } |
200 | |
201 | QTEST_MAIN(tst_QKeyEvent) |
202 | #include "tst_qkeyevent.moc" |
203 | |