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 <QtWidgets/QDesktopWidget>
32#include <QtGui/QWindow>
33#include <QDebug>
34
35// the complete class is deprecated
36QT_WARNING_PUSH
37QT_WARNING_DISABLE_DEPRECATED
38class tst_QDesktopWidget : public QObject
39{
40 Q_OBJECT
41
42private slots:
43 void cleanup();
44
45#if QT_DEPRECATED_SINCE(5, 11)
46 void numScreens();
47 void primaryScreen();
48 void screenNumberForQPoint();
49#endif
50 void screenNumberForQWidget();
51 void availableGeometry();
52 void screenGeometry();
53 void topLevels();
54};
55
56void tst_QDesktopWidget::cleanup()
57{
58 QVERIFY(QApplication::topLevelWidgets().isEmpty());
59}
60
61#if QT_DEPRECATED_SINCE(5, 11)
62void tst_QDesktopWidget::numScreens()
63{
64 QDesktopWidget desktop;
65 QVERIFY(desktop.numScreens() > 0);
66}
67
68void tst_QDesktopWidget::primaryScreen()
69{
70 QDesktopWidget desktop;
71 QVERIFY(desktop.primaryScreen() >= 0);
72 QVERIFY(desktop.primaryScreen() < desktop.numScreens());
73}
74#endif
75
76void tst_QDesktopWidget::availableGeometry()
77{
78 QDesktopWidget desktop;
79 QTest::ignoreMessage(type: QtWarningMsg, message: "QDesktopWidget::availableGeometry(): Attempt "
80 "to get the available geometry of a null widget");
81 QRect r = desktop.availableGeometry(widget: nullptr);
82 QVERIFY(r.isNull());
83
84#if QT_DEPRECATED_SINCE(5, 11)
85 QRect total;
86 QRect available;
87
88 for (int i = 0; i < desktop.screenCount(); ++i) {
89 total = desktop.screenGeometry(screen: i);
90 available = desktop.availableGeometry(screen: i);
91 QVERIFY(total.contains(available));
92 }
93
94 total = desktop.screenGeometry();
95 available = desktop.availableGeometry();
96 QVERIFY(total.contains(available));
97 QCOMPARE(desktop.availableGeometry(desktop.primaryScreen()), available);
98 QCOMPARE(desktop.screenGeometry(desktop.primaryScreen()), total);
99#endif
100}
101
102void tst_QDesktopWidget::screenNumberForQWidget()
103{
104 QDesktopWidget desktop;
105
106 QCOMPARE(desktop.screenNumber(nullptr), 0);
107
108 QWidget widget;
109 widget.show();
110 QVERIFY(QTest::qWaitForWindowExposed(&widget));
111 QVERIFY(widget.isVisible());
112
113 int widgetScreen = desktop.screenNumber(widget: &widget);
114 QVERIFY(widgetScreen > -1);
115 QVERIFY(widgetScreen < QGuiApplication::screens().size());
116}
117
118#if QT_DEPRECATED_SINCE(5, 11)
119void tst_QDesktopWidget::screenNumberForQPoint()
120{
121 // make sure QDesktopWidget::screenNumber(QPoint) returns the correct screen
122 QDesktopWidget *desktopWidget = QApplication::desktop();
123 QRect allScreens;
124 for (int i = 0; i < desktopWidget->numScreens(); ++i) {
125 QRect screenGeometry = desktopWidget->screenGeometry(screen: i);
126 QCOMPARE(desktopWidget->screenNumber(screenGeometry.center()), i);
127 allScreens |= screenGeometry;
128 }
129
130 // make sure QDesktopWidget::screenNumber(QPoint) returns a valid screen for points that aren't on any screen
131 int screen;
132 screen = desktopWidget->screenNumber(allScreens.topLeft() - QPoint(1, 1));
133
134 QVERIFY(screen >= 0 && screen < desktopWidget->numScreens());
135 screen = desktopWidget->screenNumber(allScreens.topRight() + QPoint(1, 1));
136 QVERIFY(screen >= 0 && screen < desktopWidget->numScreens());
137 screen = desktopWidget->screenNumber(allScreens.bottomLeft() - QPoint(1, 1));
138 QVERIFY(screen >= 0 && screen < desktopWidget->numScreens());
139 screen = desktopWidget->screenNumber(allScreens.bottomRight() + QPoint(1, 1));
140 QVERIFY(screen >= 0 && screen < desktopWidget->numScreens());
141}
142#endif
143
144void tst_QDesktopWidget::screenGeometry()
145{
146 QDesktopWidget *desktopWidget = QApplication::desktop();
147 QTest::ignoreMessage(type: QtWarningMsg, message: "QDesktopWidget::screenGeometry(): Attempt "
148 "to get the screen geometry of a null widget");
149 QRect r = desktopWidget->screenGeometry(widget: nullptr);
150 QVERIFY(r.isNull());
151 QWidget widget;
152 widget.show();
153 QVERIFY(QTest::qWaitForWindowExposed(&widget));
154 r = desktopWidget->screenGeometry(widget: &widget);
155
156#if QT_DEPRECATED_SINCE(5, 11)
157 QRect total;
158 QRect available;
159 for (int i = 0; i < desktopWidget->screenCount(); ++i) {
160 total = desktopWidget->screenGeometry(screen: i);
161 available = desktopWidget->availableGeometry(screen: i);
162 }
163#endif
164}
165
166void tst_QDesktopWidget::topLevels()
167{
168 // Desktop widgets/windows should not be listed as top-levels.
169 int topLevelDesktopWidgets = 0;
170 int topLevelDesktopWindows = 0;
171 foreach (const QWidget *w, QApplication::topLevelWidgets())
172 if (w->windowType() == Qt::Desktop)
173 topLevelDesktopWidgets++;
174 foreach (const QWindow *w, QGuiApplication::topLevelWindows())
175 if (w->type() == Qt::Desktop)
176 topLevelDesktopWindows++;
177 QCOMPARE(topLevelDesktopWidgets, 0);
178 QCOMPARE(topLevelDesktopWindows, 0);
179}
180QT_WARNING_POP
181
182QTEST_MAIN(tst_QDesktopWidget)
183#include "tst_qdesktopwidget.moc"
184

source code of qtbase/tests/auto/widgets/kernel/qdesktopwidget/tst_qdesktopwidget.cpp