1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 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 | |
30 | #include <QtTest/QtTest> |
31 | #include <qevent.h> |
32 | #include <qwindow.h> |
33 | |
34 | class MouseEventWidget : public QWindow |
35 | { |
36 | public: |
37 | MouseEventWidget(QWindow *parent = 0) : QWindow(parent) |
38 | { |
39 | } |
40 | bool mousePressEventRecieved; |
41 | bool mouseReleaseEventRecieved; |
42 | int mousePressButton; |
43 | int mousePressButtons; |
44 | int mousePressModifiers; |
45 | int mouseReleaseButton; |
46 | int mouseReleaseButtons; |
47 | int mouseReleaseModifiers; |
48 | protected: |
49 | void mousePressEvent(QMouseEvent *e) |
50 | { |
51 | QWindow::mousePressEvent(e); |
52 | mousePressButton = e->button(); |
53 | mousePressButtons = e->buttons(); |
54 | mousePressModifiers = e->modifiers(); |
55 | mousePressEventRecieved = true; |
56 | e->accept(); |
57 | } |
58 | void mouseReleaseEvent(QMouseEvent *e) |
59 | { |
60 | QWindow::mouseReleaseEvent(e); |
61 | mouseReleaseButton = e->button(); |
62 | mouseReleaseButtons = e->buttons(); |
63 | mouseReleaseModifiers = e->modifiers(); |
64 | mouseReleaseEventRecieved = true; |
65 | e->accept(); |
66 | } |
67 | }; |
68 | |
69 | class tst_QMouseEvent : public QObject |
70 | { |
71 | Q_OBJECT |
72 | |
73 | public slots: |
74 | void initTestCase(); |
75 | void cleanupTestCase(); |
76 | void init(); |
77 | private slots: |
78 | void mouseEventBasic(); |
79 | void checkMousePressEvent_data(); |
80 | void checkMousePressEvent(); |
81 | void checkMouseReleaseEvent_data(); |
82 | void checkMouseReleaseEvent(); |
83 | |
84 | private: |
85 | MouseEventWidget* testMouseWidget; |
86 | }; |
87 | |
88 | void tst_QMouseEvent::initTestCase() |
89 | { |
90 | testMouseWidget = new MouseEventWidget(0); |
91 | testMouseWidget->show(); |
92 | } |
93 | |
94 | void tst_QMouseEvent::cleanupTestCase() |
95 | { |
96 | delete testMouseWidget; |
97 | } |
98 | |
99 | void tst_QMouseEvent::init() |
100 | { |
101 | testMouseWidget->mousePressEventRecieved = false; |
102 | testMouseWidget->mouseReleaseEventRecieved = false; |
103 | testMouseWidget->mousePressButton = 0; |
104 | testMouseWidget->mousePressButtons = 0; |
105 | testMouseWidget->mousePressModifiers = 0; |
106 | testMouseWidget->mouseReleaseButton = 0; |
107 | testMouseWidget->mouseReleaseButtons = 0; |
108 | testMouseWidget->mouseReleaseModifiers = 0; |
109 | } |
110 | |
111 | void tst_QMouseEvent::mouseEventBasic() |
112 | { |
113 | QPointF local(100, 100); |
114 | QPointF scene(200, 200); |
115 | QPointF screen(300, 300); |
116 | QMouseEvent me(QEvent::MouseButtonPress, local, scene, screen, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); |
117 | QCOMPARE(me.isAccepted(), true); |
118 | QCOMPARE(me.button(), Qt::LeftButton); |
119 | QCOMPARE(me.buttons(), Qt::LeftButton); |
120 | QCOMPARE(me.localPos(), local); |
121 | QCOMPARE(me.windowPos(), scene); |
122 | QCOMPARE(me.screenPos(), screen); |
123 | |
124 | QPointF changedLocal(33, 66); |
125 | me.setLocalPos(changedLocal); |
126 | QCOMPARE(me.localPos(), changedLocal); |
127 | QCOMPARE(me.windowPos(), scene); |
128 | QCOMPARE(me.screenPos(), screen); |
129 | } |
130 | |
131 | void tst_QMouseEvent::checkMousePressEvent_data() |
132 | { |
133 | QTest::addColumn<int>(name: "buttonPressed" ); |
134 | QTest::addColumn<int>(name: "keyPressed" ); |
135 | |
136 | QTest::newRow(dataTag: "leftButton-nokey" ) << int(Qt::LeftButton) << int(Qt::NoButton); |
137 | QTest::newRow(dataTag: "leftButton-shiftkey" ) << int(Qt::LeftButton) << int(Qt::ShiftModifier); |
138 | QTest::newRow(dataTag: "leftButton-controlkey" ) << int(Qt::LeftButton) << int(Qt::ControlModifier); |
139 | QTest::newRow(dataTag: "leftButton-altkey" ) << int(Qt::LeftButton) << int(Qt::AltModifier); |
140 | QTest::newRow(dataTag: "leftButton-metakey" ) << int(Qt::LeftButton) << int(Qt::MetaModifier); |
141 | QTest::newRow(dataTag: "rightButton-nokey" ) << int(Qt::RightButton) << int(Qt::NoButton); |
142 | QTest::newRow(dataTag: "rightButton-shiftkey" ) << int(Qt::RightButton) << int(Qt::ShiftModifier); |
143 | QTest::newRow(dataTag: "rightButton-controlkey" ) << int(Qt::RightButton) << int(Qt::ControlModifier); |
144 | QTest::newRow(dataTag: "rightButton-altkey" ) << int(Qt::RightButton) << int(Qt::AltModifier); |
145 | QTest::newRow(dataTag: "rightButton-metakey" ) << int(Qt::RightButton) << int(Qt::MetaModifier); |
146 | QTest::newRow(dataTag: "middleButton-nokey" ) << int(Qt::MiddleButton) << int(Qt::NoButton); |
147 | QTest::newRow(dataTag: "middleButton-shiftkey" ) << int(Qt::MiddleButton) << int(Qt::ShiftModifier); |
148 | QTest::newRow(dataTag: "middleButton-controlkey" ) << int(Qt::MiddleButton) << int(Qt::ControlModifier); |
149 | QTest::newRow(dataTag: "middleButton-altkey" ) << int(Qt::MiddleButton) << int(Qt::AltModifier); |
150 | QTest::newRow(dataTag: "middleButton-metakey" ) << int(Qt::MiddleButton) << int(Qt::MetaModifier); |
151 | } |
152 | |
153 | void tst_QMouseEvent::checkMousePressEvent() |
154 | { |
155 | QFETCH(int,buttonPressed); |
156 | QFETCH(int,keyPressed); |
157 | int button = buttonPressed; |
158 | int buttons = button; |
159 | int modifiers = keyPressed; |
160 | |
161 | QTest::mousePress(window: testMouseWidget, button: Qt::MouseButton(buttonPressed), stateKey: Qt::KeyboardModifiers(keyPressed)); |
162 | qApp->processEvents(); |
163 | QVERIFY(testMouseWidget->mousePressEventRecieved); |
164 | QCOMPARE(testMouseWidget->mousePressButton, button); |
165 | QCOMPARE(testMouseWidget->mousePressButtons, buttons); |
166 | QCOMPARE(testMouseWidget->mousePressModifiers, modifiers); |
167 | |
168 | QTest::mouseRelease(window: testMouseWidget, button: Qt::MouseButton(buttonPressed), stateKey: Qt::KeyboardModifiers(keyPressed)); |
169 | qApp->processEvents(); |
170 | } |
171 | |
172 | void tst_QMouseEvent::checkMouseReleaseEvent_data() |
173 | { |
174 | QTest::addColumn<int>(name: "buttonReleased" ); |
175 | QTest::addColumn<int>(name: "keyPressed" ); |
176 | |
177 | QTest::newRow(dataTag: "leftButton-nokey" ) << int(Qt::LeftButton) << int(Qt::NoButton); |
178 | QTest::newRow(dataTag: "leftButton-shiftkey" ) << int(Qt::LeftButton) << int(Qt::ShiftModifier); |
179 | QTest::newRow(dataTag: "leftButton-controlkey" ) << int(Qt::LeftButton) << int(Qt::ControlModifier); |
180 | QTest::newRow(dataTag: "leftButton-altkey" ) << int(Qt::LeftButton) << int(Qt::AltModifier); |
181 | QTest::newRow(dataTag: "leftButton-metakey" ) << int(Qt::LeftButton) << int(Qt::MetaModifier); |
182 | QTest::newRow(dataTag: "rightButton-nokey" ) << int(Qt::RightButton) << int(Qt::NoButton); |
183 | QTest::newRow(dataTag: "rightButton-shiftkey" ) << int(Qt::RightButton) << int(Qt::ShiftModifier); |
184 | QTest::newRow(dataTag: "rightButton-controlkey" ) << int(Qt::RightButton) << int(Qt::ControlModifier); |
185 | QTest::newRow(dataTag: "rightButton-altkey" ) << int(Qt::RightButton) << int(Qt::AltModifier); |
186 | QTest::newRow(dataTag: "rightButton-metakey" ) << int(Qt::RightButton) << int(Qt::MetaModifier); |
187 | QTest::newRow(dataTag: "middleButton-nokey" ) << int(Qt::MiddleButton) << int(Qt::NoButton); |
188 | QTest::newRow(dataTag: "middleButton-shiftkey" ) << int(Qt::MiddleButton) << int(Qt::ShiftModifier); |
189 | QTest::newRow(dataTag: "middleButton-controlkey" ) << int(Qt::MiddleButton) << int(Qt::ControlModifier); |
190 | QTest::newRow(dataTag: "middleButton-altkey" ) << int(Qt::MiddleButton) << int(Qt::AltModifier); |
191 | QTest::newRow(dataTag: "middleButton-metakey" ) << int(Qt::MiddleButton) << int(Qt::MetaModifier); |
192 | } |
193 | |
194 | void tst_QMouseEvent::checkMouseReleaseEvent() |
195 | { |
196 | QFETCH(int,buttonReleased); |
197 | QFETCH(int,keyPressed); |
198 | int button = buttonReleased; |
199 | int buttons = 0; |
200 | int modifiers = keyPressed; |
201 | |
202 | QTest::mouseClick(window: testMouseWidget, button: Qt::MouseButton(buttonReleased), stateKey: Qt::KeyboardModifiers(keyPressed)); |
203 | qApp->processEvents(); |
204 | QVERIFY(testMouseWidget->mouseReleaseEventRecieved); |
205 | QCOMPARE(testMouseWidget->mouseReleaseButton, button); |
206 | QCOMPARE(testMouseWidget->mouseReleaseButtons, buttons); |
207 | QCOMPARE(testMouseWidget->mouseReleaseModifiers, modifiers); |
208 | } |
209 | |
210 | QTEST_MAIN(tst_QMouseEvent) |
211 | #include "tst_qmouseevent.moc" |
212 | |