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#include <qscreen.h>
30#include <qpa/qwindowsysteminterface.h>
31
32#include <QtTest/QtTest>
33
34class tst_QScreen: public QObject
35{
36 Q_OBJECT
37
38private slots:
39 void angleBetween_data();
40 void angleBetween();
41 void transformBetween_data();
42 void transformBetween();
43 void orientationChange();
44};
45
46void tst_QScreen::angleBetween_data()
47{
48 QTest::addColumn<uint>(name: "oa");
49 QTest::addColumn<uint>(name: "ob");
50 QTest::addColumn<int>(name: "expected");
51
52 QTest::newRow(dataTag: "Portrait Portrait")
53 << uint(Qt::PortraitOrientation)
54 << uint(Qt::PortraitOrientation)
55 << 0;
56
57 QTest::newRow(dataTag: "Portrait Landscape")
58 << uint(Qt::PortraitOrientation)
59 << uint(Qt::LandscapeOrientation)
60 << 270;
61
62 QTest::newRow(dataTag: "Portrait InvertedPortrait")
63 << uint(Qt::PortraitOrientation)
64 << uint(Qt::InvertedPortraitOrientation)
65 << 180;
66
67 QTest::newRow(dataTag: "Portrait InvertedLandscape")
68 << uint(Qt::PortraitOrientation)
69 << uint(Qt::InvertedLandscapeOrientation)
70 << 90;
71
72 QTest::newRow(dataTag: "InvertedLandscape InvertedPortrait")
73 << uint(Qt::InvertedLandscapeOrientation)
74 << uint(Qt::InvertedPortraitOrientation)
75 << 90;
76
77 QTest::newRow(dataTag: "InvertedLandscape Landscape")
78 << uint(Qt::InvertedLandscapeOrientation)
79 << uint(Qt::LandscapeOrientation)
80 << 180;
81
82 QTest::newRow(dataTag: "Landscape Primary")
83 << uint(Qt::LandscapeOrientation)
84 << uint(Qt::PrimaryOrientation)
85 << QGuiApplication::primaryScreen()->angleBetween(a: Qt::LandscapeOrientation, b: QGuiApplication::primaryScreen()->primaryOrientation());
86}
87
88void tst_QScreen::angleBetween()
89{
90 QFETCH( uint, oa );
91 QFETCH( uint, ob );
92 QFETCH( int, expected );
93
94 Qt::ScreenOrientation a = Qt::ScreenOrientation(oa);
95 Qt::ScreenOrientation b = Qt::ScreenOrientation(ob);
96
97 QCOMPARE(QGuiApplication::primaryScreen()->angleBetween(a, b), expected);
98 QCOMPARE(QGuiApplication::primaryScreen()->angleBetween(b, a), (360 - expected) % 360);
99}
100
101void tst_QScreen::transformBetween_data()
102{
103 QTest::addColumn<uint>(name: "oa");
104 QTest::addColumn<uint>(name: "ob");
105 QTest::addColumn<QRect>(name: "rect");
106 QTest::addColumn<QTransform>(name: "expected");
107
108 QRect rect(0, 0, 480, 640);
109
110 QTest::newRow(dataTag: "Portrait Portrait")
111 << uint(Qt::PortraitOrientation)
112 << uint(Qt::PortraitOrientation)
113 << rect
114 << QTransform();
115
116 QTest::newRow(dataTag: "Portrait Landscape")
117 << uint(Qt::PortraitOrientation)
118 << uint(Qt::LandscapeOrientation)
119 << rect
120 << QTransform(0, -1, 1, 0, 0, rect.height());
121
122 QTest::newRow(dataTag: "Portrait InvertedPortrait")
123 << uint(Qt::PortraitOrientation)
124 << uint(Qt::InvertedPortraitOrientation)
125 << rect
126 << QTransform(-1, 0, 0, -1, rect.width(), rect.height());
127
128 QTest::newRow(dataTag: "Portrait InvertedLandscape")
129 << uint(Qt::PortraitOrientation)
130 << uint(Qt::InvertedLandscapeOrientation)
131 << rect
132 << QTransform(0, 1, -1, 0, rect.width(), 0);
133
134 QTest::newRow(dataTag: "InvertedLandscape InvertedPortrait")
135 << uint(Qt::InvertedLandscapeOrientation)
136 << uint(Qt::InvertedPortraitOrientation)
137 << rect
138 << QTransform(0, 1, -1, 0, rect.width(), 0);
139
140 QTest::newRow(dataTag: "InvertedLandscape Landscape")
141 << uint(Qt::InvertedLandscapeOrientation)
142 << uint(Qt::LandscapeOrientation)
143 << rect
144 << QTransform(-1, 0, 0, -1, rect.width(), rect.height());
145
146 QTest::newRow(dataTag: "Landscape Primary")
147 << uint(Qt::LandscapeOrientation)
148 << uint(Qt::PrimaryOrientation)
149 << rect
150 << QGuiApplication::primaryScreen()->transformBetween(a: Qt::LandscapeOrientation, b: QGuiApplication::primaryScreen()->primaryOrientation(), target: rect);
151}
152
153void tst_QScreen::transformBetween()
154{
155 QFETCH( uint, oa );
156 QFETCH( uint, ob );
157 QFETCH( QRect, rect );
158 QFETCH( QTransform, expected );
159
160 Qt::ScreenOrientation a = Qt::ScreenOrientation(oa);
161 Qt::ScreenOrientation b = Qt::ScreenOrientation(ob);
162
163 QCOMPARE(QGuiApplication::primaryScreen()->transformBetween(a, b, rect), expected);
164}
165
166void tst_QScreen::orientationChange()
167{
168 qRegisterMetaType<Qt::ScreenOrientation>(typeName: "Qt::ScreenOrientation");
169
170 QScreen *screen = QGuiApplication::primaryScreen();
171
172 screen->setOrientationUpdateMask(Qt::LandscapeOrientation | Qt::PortraitOrientation);
173
174 QWindowSystemInterface::handleScreenOrientationChange(screen, newOrientation: Qt::LandscapeOrientation);
175 QWindowSystemInterface::flushWindowSystemEvents();
176 QTRY_COMPARE(screen->orientation(), Qt::LandscapeOrientation);
177
178 QWindowSystemInterface::handleScreenOrientationChange(screen, newOrientation: Qt::PortraitOrientation);
179 QWindowSystemInterface::flushWindowSystemEvents();
180 QTRY_COMPARE(screen->orientation(), Qt::PortraitOrientation);
181
182 QSignalSpy spy(screen, SIGNAL(orientationChanged(Qt::ScreenOrientation)));
183
184 QWindowSystemInterface::handleScreenOrientationChange(screen, newOrientation: Qt::InvertedLandscapeOrientation);
185 QWindowSystemInterface::flushWindowSystemEvents();
186 QWindowSystemInterface::handleScreenOrientationChange(screen, newOrientation: Qt::InvertedPortraitOrientation);
187 QWindowSystemInterface::flushWindowSystemEvents();
188 QWindowSystemInterface::handleScreenOrientationChange(screen, newOrientation: Qt::LandscapeOrientation);
189 QWindowSystemInterface::flushWindowSystemEvents();
190
191 QTRY_COMPARE(screen->orientation(), Qt::LandscapeOrientation);
192 QCOMPARE(spy.count(), 1);
193
194 spy.clear();
195 QWindowSystemInterface::handleScreenOrientationChange(screen, newOrientation: Qt::InvertedLandscapeOrientation);
196 QWindowSystemInterface::flushWindowSystemEvents();
197 QTRY_COMPARE(screen->orientation(), Qt::LandscapeOrientation);
198 QCOMPARE(spy.count(), 0);
199
200 screen->setOrientationUpdateMask(screen->orientationUpdateMask() | Qt::InvertedLandscapeOrientation);
201 QTRY_COMPARE(screen->orientation(), Qt::InvertedLandscapeOrientation);
202 QCOMPARE(spy.count(), 1);
203}
204
205#include <tst_qscreen.moc>
206QTEST_MAIN(tst_QScreen);
207

source code of qtbase/tests/auto/gui/kernel/qscreen/tst_qscreen.cpp