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 <QSizeGrip> |
32 | #include <QEvent> |
33 | #include <QLineEdit> |
34 | #include <QVBoxLayout> |
35 | #include <QLabel> |
36 | #include <QMainWindow> |
37 | #include <QStatusBar> |
38 | #include <QMdiArea> |
39 | #include <QMdiSubWindow> |
40 | |
41 | static inline Qt::Corner sizeGripCorner(QWidget *parent, QSizeGrip *sizeGrip) |
42 | { |
43 | if (!parent || !sizeGrip) |
44 | return Qt::TopLeftCorner; |
45 | |
46 | const QPoint sizeGripPos = sizeGrip->mapTo(parent, QPoint(0, 0)); |
47 | bool isAtBottom = sizeGripPos.y() >= parent->height() / 2; |
48 | bool isAtLeft = sizeGripPos.x() <= parent->width() / 2; |
49 | if (isAtLeft) |
50 | return isAtBottom ? Qt::BottomLeftCorner : Qt::TopLeftCorner; |
51 | else |
52 | return isAtBottom ? Qt::BottomRightCorner : Qt::TopRightCorner; |
53 | |
54 | } |
55 | |
56 | Q_DECLARE_METATYPE(Qt::WindowType); |
57 | |
58 | class tst_QSizeGrip : public QObject |
59 | { |
60 | Q_OBJECT |
61 | public slots: |
62 | void cleanup(); |
63 | |
64 | private slots: |
65 | void hideAndShowOnWindowStateChange_data(); |
66 | void hideAndShowOnWindowStateChange(); |
67 | void orientation(); |
68 | void dontCrashOnTLWChange(); |
69 | }; |
70 | |
71 | class TestWidget : public QWidget |
72 | { |
73 | public: |
74 | using QWidget::QWidget; |
75 | |
76 | QSize sizeHint() const override { return QSize(300, 200); } |
77 | void changeEvent(QEvent *event) override |
78 | { |
79 | QWidget::changeEvent(event); |
80 | if (isWindow() && event->type() == QEvent::WindowStateChange) |
81 | QVERIFY(QTest::qWaitForWindowExposed(this)); |
82 | } |
83 | }; |
84 | |
85 | void tst_QSizeGrip::cleanup() |
86 | { |
87 | QVERIFY(QApplication::topLevelWidgets().isEmpty()); |
88 | } |
89 | |
90 | void tst_QSizeGrip::hideAndShowOnWindowStateChange_data() |
91 | { |
92 | QTest::addColumn<Qt::WindowType>(name: "windowType" ); |
93 | QTest::newRow(dataTag: "Qt::Window" ) << Qt::Window; |
94 | QTest::newRow(dataTag: "Qt::SubWindow" ) << Qt::SubWindow; |
95 | } |
96 | |
97 | void tst_QSizeGrip::hideAndShowOnWindowStateChange() |
98 | { |
99 | QFETCH(Qt::WindowType, windowType); |
100 | #ifdef Q_OS_WINRT |
101 | QSKIP("Broken on WinRT - QTBUG-68297" ); |
102 | #endif |
103 | |
104 | QScopedPointer<QWidget> parentWidget; |
105 | if (windowType != Qt::Window) |
106 | parentWidget.reset(other: new QWidget); |
107 | QScopedPointer<TestWidget> widget(new TestWidget(parentWidget.data(), Qt::WindowFlags(windowType))); |
108 | QSizeGrip *sizeGrip = new QSizeGrip(widget.data()); |
109 | |
110 | // Normal. |
111 | if (parentWidget) |
112 | parentWidget->show(); |
113 | else |
114 | widget->show(); |
115 | QTRY_VERIFY(sizeGrip->isVisible()); |
116 | |
117 | widget->showFullScreen(); |
118 | QTRY_VERIFY(!sizeGrip->isVisible()); |
119 | |
120 | widget->showNormal(); |
121 | QTRY_VERIFY(sizeGrip->isVisible()); |
122 | |
123 | widget->showMaximized(); |
124 | #ifndef Q_OS_MAC |
125 | QTRY_VERIFY(!sizeGrip->isVisible()); |
126 | #else |
127 | QEXPECT_FAIL("" , "QTBUG-23681" , Abort); |
128 | QVERIFY(sizeGrip->isVisible()); |
129 | #endif |
130 | |
131 | widget->showNormal(); |
132 | QTRY_VERIFY(sizeGrip->isVisible()); |
133 | |
134 | sizeGrip->hide(); |
135 | QTRY_VERIFY(!sizeGrip->isVisible()); |
136 | |
137 | widget->showFullScreen(); |
138 | widget->showNormal(); |
139 | QTRY_VERIFY(!sizeGrip->isVisible()); |
140 | widget->showMaximized(); |
141 | widget->showNormal(); |
142 | QTRY_VERIFY(!sizeGrip->isVisible()); |
143 | } |
144 | |
145 | void tst_QSizeGrip::orientation() |
146 | { |
147 | #ifdef Q_OS_WINRT |
148 | QSKIP("Broken on WinRT - QTBUG-68297" ); |
149 | #endif |
150 | |
151 | TestWidget widget; |
152 | widget.setLayout(new QVBoxLayout); |
153 | QSizeGrip *sizeGrip = new QSizeGrip(&widget); |
154 | sizeGrip->setFixedSize(sizeGrip->sizeHint()); |
155 | widget.layout()->addWidget(w: sizeGrip); |
156 | widget.layout()->setAlignment(w: sizeGrip, alignment: Qt::AlignBottom | Qt::AlignRight); |
157 | |
158 | widget.show(); |
159 | QCOMPARE(sizeGripCorner(&widget, sizeGrip), Qt::BottomRightCorner); |
160 | |
161 | widget.setLayoutDirection(Qt::RightToLeft); |
162 | qApp->processEvents(); |
163 | QCOMPARE(sizeGripCorner(&widget, sizeGrip), Qt::BottomLeftCorner); |
164 | |
165 | widget.unsetLayoutDirection(); |
166 | qApp->processEvents(); |
167 | QCOMPARE(sizeGripCorner(&widget, sizeGrip), Qt::BottomRightCorner); |
168 | |
169 | widget.layout()->setAlignment(w: sizeGrip, alignment: Qt::AlignTop | Qt::AlignRight); |
170 | qApp->processEvents(); |
171 | QCOMPARE(sizeGripCorner(&widget, sizeGrip), Qt::TopRightCorner); |
172 | |
173 | widget.setLayoutDirection(Qt::RightToLeft); |
174 | qApp->processEvents(); |
175 | QCOMPARE(sizeGripCorner(&widget, sizeGrip), Qt::TopLeftCorner); |
176 | |
177 | widget.unsetLayoutDirection(); |
178 | qApp->processEvents(); |
179 | QCOMPARE(sizeGripCorner(&widget, sizeGrip), Qt::TopRightCorner); |
180 | } |
181 | |
182 | void tst_QSizeGrip::dontCrashOnTLWChange() |
183 | { |
184 | // QTBUG-22867 |
185 | QMdiArea mdiArea; |
186 | mdiArea.show(); |
187 | |
188 | QScopedPointer<QMainWindow> mw(new QMainWindow); |
189 | QMdiSubWindow *mdi = mdiArea.addSubWindow(widget: mw.data()); |
190 | mw->statusBar()->setSizeGripEnabled(true); |
191 | mdiArea.removeSubWindow(widget: mw.data()); |
192 | delete mdi; |
193 | mw->show(); |
194 | |
195 | // the above setup causes a change of TLW for the size grip, |
196 | // and it must not crash. |
197 | #ifdef Q_OS_WINRT |
198 | QEXPECT_FAIL("" , "Broken on WinRT - QTBUG-68297" , Abort); |
199 | #endif |
200 | QVERIFY(QTest::qWaitForWindowExposed(&mdiArea)); |
201 | QVERIFY(QTest::qWaitForWindowExposed(mw.data())); |
202 | } |
203 | |
204 | QTEST_MAIN(tst_QSizeGrip) |
205 | #include "tst_qsizegrip.moc" |
206 | |
207 | |