| 1 |
// Copyright (C) 2019 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "mockcompositor.h" |
| 5 |
#include <QtGui/QScreen> |
| 6 |
#include <QtGui/QRasterWindow> |
| 7 |
|
| 8 |
using namespace MockCompositor; |
| 9 |
|
| 10 |
class tst_output : public QObject, private DefaultCompositor |
| 11 |
{ |
| 12 |
Q_OBJECT |
| 13 |
private slots: |
| 14 |
void initTestCase() |
| 15 |
{ |
| 16 |
m_config.autoConfigure = true; |
| 17 |
m_config.autoEnter = false; |
| 18 |
} |
| 19 |
void cleanup() |
| 20 |
{ |
| 21 |
QCOMPOSITOR_COMPARE(getAll<Output>().size(), 1); // Only the default output should be left |
| 22 |
QTRY_COMPARE(QGuiApplication::screens().size(), 1); |
| 23 |
QTRY_VERIFY2(isClean(), qPrintable(dirtyMessage())); |
| 24 |
} |
| 25 |
void primaryScreen(); |
| 26 |
void secondaryHiDpiScreen(); |
| 27 |
void addScreenWithGeometryChange(); |
| 28 |
void windowScreens(); |
| 29 |
void removePrimaryScreen(); |
| 30 |
void screenOrder(); |
| 31 |
void removeAllScreens(); |
| 32 |
}; |
| 33 |
|
| 34 |
void tst_output::primaryScreen() |
| 35 |
{ |
| 36 |
// Verify that the client has bound to the output global |
| 37 |
QCOMPOSITOR_TRY_COMPARE(output()->resourceMap().size(), 1); |
| 38 |
QTRY_VERIFY(QGuiApplication::primaryScreen()); |
| 39 |
QScreen *screen = QGuiApplication::primaryScreen(); |
| 40 |
QCOMPARE(screen->manufacturer(), "Make"); |
| 41 |
QCOMPARE(screen->model(), "Model"); |
| 42 |
QCOMPARE(screen->size(), QSize(1920, 1080)); |
| 43 |
QCOMPARE(screen->refreshRate(), 60); |
| 44 |
QCOMPARE(qRound(screen->physicalDotsPerInch()), 96 / screen->devicePixelRatio()); |
| 45 |
QCOMPARE(screen->devicePixelRatio(), 1); |
| 46 |
QCOMPARE(screen->logicalDotsPerInch(), 96); |
| 47 |
} |
| 48 |
|
| 49 |
void tst_output::secondaryHiDpiScreen() |
| 50 |
{ |
| 51 |
exec([&] { |
| 52 |
OutputData d; |
| 53 |
d.position = {1920, 0}; // in global compositor space (not pixels) |
| 54 |
d.mode.resolution = {800, 640}; |
| 55 |
d.physicalSize = d.mode.physicalSizeForDpi(200); |
| 56 |
d.scale = 2; |
| 57 |
add<Output>(d); |
| 58 |
}); |
| 59 |
|
| 60 |
// Verify that the client has bound to the output global |
| 61 |
QCOMPOSITOR_TRY_VERIFY(output(1) && output(1)->resourceMap().size() == 1); |
| 62 |
|
| 63 |
QTRY_COMPARE(QGuiApplication::screens().size(), 2); |
| 64 |
QScreen *screen = QGuiApplication::screens()[1]; |
| 65 |
QCOMPARE(screen->refreshRate(), 60); |
| 66 |
QCOMPARE(screen->devicePixelRatio(), 2); |
| 67 |
QCOMPARE(screen->logicalDotsPerInch(), 96); |
| 68 |
|
| 69 |
// Dots currently means device pixels, not actual pixels (see QTBUG-62649) |
| 70 |
QCOMPARE(qRound(screen->physicalDotsPerInch() * screen->devicePixelRatio()), 200); |
| 71 |
|
| 72 |
// Size is in logical pixel coordinates |
| 73 |
QCOMPARE(screen->size(), QSize(800, 640) / 2); |
| 74 |
QCOMPARE(screen->geometry(), QRect(QPoint(1920, 0), QSize(400, 320))); |
| 75 |
QCOMPARE(screen->virtualGeometry(), QRect(QPoint(0, 0), QSize(1920 + 800 / 2, 1080))); |
| 76 |
|
| 77 |
exec([&] { remove(output(1)); }); |
| 78 |
} |
| 79 |
|
| 80 |
// QTBUG-62044 |
| 81 |
void tst_output::addScreenWithGeometryChange() |
| 82 |
{ |
| 83 |
const QPoint initialPosition = exec([&] { return output(0)->m_data.position; }); |
| 84 |
|
| 85 |
exec([&] { |
| 86 |
auto *oldOutput = output(0); |
| 87 |
auto *newOutput = add<Output>(); |
| 88 |
newOutput->m_data.mode.resolution = {1280, 720}; |
| 89 |
// Move the primary output to the right |
| 90 |
QPoint newPosition(newOutput->m_data.mode.resolution.width(), 0); |
| 91 |
Q_ASSERT(newPosition != initialPosition); |
| 92 |
oldOutput->m_data.position = newPosition; |
| 93 |
oldOutput->sendGeometry(); |
| 94 |
oldOutput->sendDone(); |
| 95 |
}); |
| 96 |
|
| 97 |
QTRY_COMPARE(QGuiApplication::screens().size(), 2); |
| 98 |
QTRY_COMPARE(QGuiApplication::primaryScreen()->geometry(), QRect(QPoint(1280, 0), QSize(1920, 1080))); |
| 99 |
|
| 100 |
// Remove the extra output and move the old one back |
| 101 |
exec([&] { |
| 102 |
remove(output(1)); |
| 103 |
output()->m_data.position = initialPosition; |
| 104 |
output()->sendGeometry(); |
| 105 |
output()->sendDone(); |
| 106 |
}); |
| 107 |
QTRY_COMPARE(QGuiApplication::screens().size(), 1); |
| 108 |
QTRY_COMPARE(QGuiApplication::primaryScreen()->geometry(), QRect(QPoint(0, 0), QSize(1920, 1080))); |
| 109 |
} |
| 110 |
|
| 111 |
void tst_output::windowScreens() |
| 112 |
{ |
| 113 |
QRasterWindow window; |
| 114 |
window.resize(400, 320); |
| 115 |
window.show(); |
| 116 |
QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial); |
| 117 |
|
| 118 |
QTRY_COMPARE(QGuiApplication::screens().size(), 1); |
| 119 |
QScreen *primaryScreen = QGuiApplication::screens().first(); |
| 120 |
QCOMPARE(window.screen(), primaryScreen); |
| 121 |
|
| 122 |
exec([&] { add<Output>(); }); |
| 123 |
|
| 124 |
QTRY_COMPARE(QGuiApplication::screens().size(), 2); |
| 125 |
QScreen *secondaryScreen = QGuiApplication::screens().at(1); |
| 126 |
QVERIFY(secondaryScreen); |
| 127 |
|
| 128 |
window.setScreen(secondaryScreen); |
| 129 |
QCOMPARE(window.screen(), secondaryScreen); |
| 130 |
|
| 131 |
exec([&] { |
| 132 |
xdgToplevel()->surface()->sendEnter(output(0)); |
| 133 |
xdgToplevel()->surface()->sendEnter(output(1)); |
| 134 |
}); |
| 135 |
|
| 136 |
QTRY_COMPARE(window.screen(), primaryScreen); |
| 137 |
|
| 138 |
exec([&] { |
| 139 |
xdgToplevel()->surface()->sendLeave(output(0)); |
| 140 |
}); |
| 141 |
QTRY_COMPARE(window.screen(), secondaryScreen); |
| 142 |
|
| 143 |
exec([&] { |
| 144 |
remove(output(1)); |
| 145 |
}); |
| 146 |
QTRY_COMPARE(QGuiApplication::screens().size(), 1); |
| 147 |
QCOMPARE(window.screen(), primaryScreen); |
| 148 |
} |
| 149 |
|
| 150 |
void tst_output::removePrimaryScreen() |
| 151 |
{ |
| 152 |
QRasterWindow window; |
| 153 |
window.resize(400, 320); |
| 154 |
window.show(); |
| 155 |
QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial); |
| 156 |
|
| 157 |
QTRY_COMPARE(QGuiApplication::screens().size(), 1); |
| 158 |
QScreen *primaryScreen = QGuiApplication::screens().first(); |
| 159 |
QCOMPARE(window.screen(), primaryScreen); |
| 160 |
|
| 161 |
// Add a clone of the primary output |
| 162 |
exec([&] { add<Output>(output()->m_data); }); |
| 163 |
|
| 164 |
QTRY_COMPARE(QGuiApplication::screens().size(), 2); |
| 165 |
QTRY_COMPARE(QGuiApplication::primaryScreen()->virtualSiblings().size(), 2); |
| 166 |
QScreen *secondaryScreen = QGuiApplication::screens().at(1); |
| 167 |
QVERIFY(secondaryScreen); |
| 168 |
|
| 169 |
exec([&] { remove(output()); }); |
| 170 |
QTRY_COMPARE(QGuiApplication::screens().size(), 1); |
| 171 |
|
| 172 |
exec([&] { |
| 173 |
auto *surface = xdgToplevel()->surface(); |
| 174 |
pointer()->sendEnter(surface, {32, 32}); |
| 175 |
pointer()->sendFrame(client()); |
| 176 |
pointer()->sendButton(client(), BTN_LEFT, 1); |
| 177 |
pointer()->sendFrame(client()); |
| 178 |
pointer()->sendButton(client(), BTN_LEFT, 0); |
| 179 |
pointer()->sendFrame(client()); |
| 180 |
}); |
| 181 |
|
| 182 |
// Wait to make sure mouse events dont't cause a crash now that the screen has changed |
| 183 |
xdgPingAndWaitForPong(); |
| 184 |
} |
| 185 |
|
| 186 |
// QTBUG-72828 |
| 187 |
void tst_output::screenOrder() |
| 188 |
{ |
| 189 |
exec([&] { |
| 190 |
add<Output>()->m_data.model = "Screen 1"; |
| 191 |
add<Output>()->m_data.model = "Screen 2"; |
| 192 |
}); |
| 193 |
|
| 194 |
QTRY_COMPARE(QGuiApplication::screens().size(), 3); |
| 195 |
const auto screens = QGuiApplication::screens(); |
| 196 |
|
| 197 |
QCOMPARE(screens[1]->model(), "Screen 1"); |
| 198 |
QCOMPARE(screens[2]->model(), "Screen 2"); |
| 199 |
|
| 200 |
exec([&] { |
| 201 |
remove(output(2)); |
| 202 |
remove(output(1)); |
| 203 |
}); |
| 204 |
} |
| 205 |
|
| 206 |
// This is different from tst_nooutput::noScreens because here we have a screen at platform |
| 207 |
// integration initialization, which we then remove. |
| 208 |
void tst_output::removeAllScreens() |
| 209 |
{ |
| 210 |
QRasterWindow window1; |
| 211 |
window1.resize(400, 320); |
| 212 |
window1.show(); |
| 213 |
QCOMPOSITOR_TRY_VERIFY(xdgSurface(0) && xdgSurface(0)->m_committedConfigureSerial); |
| 214 |
|
| 215 |
const QString wlOutputPrimaryScreenModel = QGuiApplication::primaryScreen()->model(); |
| 216 |
|
| 217 |
// Get screen info so we can restore it after |
| 218 |
auto screenInfo = exec([&] { return output()->m_data; }); |
| 219 |
exec([&] { remove(output()); }); |
| 220 |
|
| 221 |
// Make sure the wl_output is actually removed before we continue |
| 222 |
QTRY_VERIFY(!QGuiApplication::primaryScreen() || QGuiApplication::primaryScreen()->model() != wlOutputPrimaryScreenModel); |
| 223 |
|
| 224 |
// Adding a window while there are no screens should also work |
| 225 |
QRasterWindow window2; |
| 226 |
window2.resize(400, 320); |
| 227 |
window2.show(); |
| 228 |
|
| 229 |
exec([&] { add<Output>(screenInfo); }); |
| 230 |
|
| 231 |
// Things should be back to normal |
| 232 |
QTRY_VERIFY(QGuiApplication::primaryScreen()); |
| 233 |
QTRY_COMPARE(QGuiApplication::primaryScreen()->model(), wlOutputPrimaryScreenModel); |
| 234 |
|
| 235 |
// Test that we don't leave any fake screens around after we get a wl_output back. |
| 236 |
QTRY_COMPARE(QGuiApplication::screens().size(), 1); |
| 237 |
|
| 238 |
// Qt may choose to recreate/hide windows in response to changing screens, so give the client |
| 239 |
// some time to potentially mess up before we verify that the windows are visible. |
| 240 |
xdgPingAndWaitForPong(); |
| 241 |
|
| 242 |
// Windows should be visible after we've reconnected the screen |
| 243 |
QCOMPOSITOR_TRY_VERIFY(xdgToplevel(0) && xdgToplevel(0)->m_xdgSurface->m_committedConfigureSerial); |
| 244 |
QCOMPOSITOR_TRY_VERIFY(xdgToplevel(1) && xdgToplevel(1)->m_xdgSurface->m_committedConfigureSerial); |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
QCOMPOSITOR_TEST_MAIN(tst_output) |
| 249 |
#include "tst_output.moc" |