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 <qapplication.h>
32#include <qfontinfo.h>
33
34
35#include <qpushbutton.h>
36#include <qscrollbar.h>
37#include <qtimer.h>
38
39#include <qdialog.h>
40
41class TstWidget;
42class TstDialog;
43QT_FORWARD_DECLARE_CLASS(QPushButton)
44
45class TestButton : public QPushButton
46{
47public:
48 TestButton(const QString &title, QWidget *parent = nullptr)
49 : QPushButton(title, parent)
50 {}
51protected:
52 bool hitButton(const QPoint &pos) const override
53 {
54 return rect().contains(p: pos);
55 }
56};
57
58class tst_qmouseevent_modal : public QObject
59{
60 Q_OBJECT
61
62private slots:
63 void initTestCase();
64 void cleanupTestCase();
65 void mousePressRelease();
66
67private:
68 TstWidget *w;
69};
70
71class TstWidget : public QWidget
72{
73 Q_OBJECT
74public:
75 TstWidget();
76public slots:
77 void buttonPressed();
78public:
79 TestButton *pb;
80 TstDialog *d;
81};
82
83
84class TstDialog : public QDialog
85{
86 Q_OBJECT
87public:
88 TstDialog( QWidget *mouseWidget, QWidget *parent, const char *name );
89 int count() { return c; }
90protected:
91 void showEvent ( QShowEvent * );
92public slots:
93 void releaseMouse();
94 void closeDialog();
95private:
96 QWidget *m;
97 int c;
98};
99
100void tst_qmouseevent_modal::initTestCase()
101{
102 w = new TstWidget;
103 w->show();
104}
105
106void tst_qmouseevent_modal::cleanupTestCase()
107{
108 delete w;
109 w = 0;
110}
111
112/*
113 Test for task 22500
114*/
115void tst_qmouseevent_modal::mousePressRelease()
116{
117
118 QVERIFY( !w->d->isVisible() );
119 QVERIFY( w->d->count() == 0 );
120
121 QTest::mousePress( widget: w->pb, button: Qt::LeftButton );
122
123 QTRY_VERIFY( !w->d->isVisible() );
124 QVERIFY( w->d->count() == 1 );
125 QVERIFY( !w->pb->isDown() );
126
127 QTest::mousePress( widget: w->pb, button: Qt::LeftButton );
128
129 QTRY_VERIFY( !w->d->isVisible() );
130 QVERIFY( w->d->count() == 2 );
131 QVERIFY( !w->pb->isDown() );
132
133 // With the current QWS mouse handling, the 3rd press would fail...
134
135 QTest::mousePress( widget: w->pb, button: Qt::LeftButton );
136
137 QTRY_VERIFY( !w->d->isVisible() );
138 QVERIFY( w->d->count() == 3 );
139 QVERIFY( !w->pb->isDown() );
140
141 QTest::mousePress( widget: w->pb, button: Qt::LeftButton );
142
143 QTRY_VERIFY( !w->d->isVisible() );
144 QVERIFY( w->d->count() == 4 );
145 QVERIFY( !w->pb->isDown() );
146}
147
148
149TstWidget::TstWidget()
150{
151 pb = new TestButton( "Press me", this );
152 pb->setObjectName("testbutton");
153 QSize s = pb->sizeHint();
154 pb->setGeometry( ax: 5, ay: 5, aw: s.width(), ah: s.height() );
155
156 connect( sender: pb, SIGNAL(pressed()), receiver: this, SLOT(buttonPressed()) );
157
158// QScrollBar *sb = new QScrollBar( Qt::Horizontal, this );
159
160// sb->setGeometry( 5, pb->geometry().bottom() + 5, 100, sb->sizeHint().height() );
161
162 d = new TstDialog( pb, this , 0 );
163}
164
165void TstWidget::buttonPressed()
166{
167 d->exec();
168}
169
170TstDialog::TstDialog( QWidget *mouseWidget, QWidget *parent, const char *name )
171 :QDialog( parent )
172{
173 setObjectName(name);
174 setModal(true);
175 m = mouseWidget;
176 c = 0;
177}
178
179void TstDialog::showEvent ( QShowEvent * )
180{
181 QTimer::singleShot(msec: 1, receiver: this, SLOT(releaseMouse()));
182 QTimer::singleShot(msec: 100, receiver: this, SLOT(closeDialog()));
183}
184
185void TstDialog::releaseMouse()
186{
187 QTest::mouseRelease(widget: m, button: Qt::LeftButton);
188}
189
190void TstDialog::closeDialog()
191{
192 if ( isVisible() ) {
193 c++;
194 accept();
195 }
196}
197
198QTEST_MAIN(tst_qmouseevent_modal)
199#include "tst_qmouseevent_modal.moc"
200

source code of qtbase/tests/auto/gui/kernel/qmouseevent_modal/tst_qmouseevent_modal.cpp