summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp
blob: 5f0d226124cdb8bd278a2581a04d3122fae2e5c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <private/qhighdpiscaling_p.h>
#include <qpa/qplatformscreen.h>
#include <qpa/qplatformnativeinterface.h>

#include <QTest>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QStringView>

Q_LOGGING_CATEGORY(lcTests, "qt.gui.tests")

class tst_QHighDpi: public QObject
{
    Q_OBJECT
private: // helpers
    QJsonArray createStandardScreens(const QList<qreal> &dpiValues);
    QGuiApplication *createOffscreenApplication(const QByteArray &jsonConfig);
    QGuiApplication *createStandardOffscreenApp(const QList<qreal> &dpiValues);
    QGuiApplication *createStandardOffscreenApp(const QJsonArray &screens);
    static void standardScreenDpiTestData();

    static void setOffscreenConfiguration(const QJsonObject &configuration);
    static QJsonObject offscreenConfiguration();

private slots:
    void cleanup();
    void qhighdpiscaling_data();
    void qhighdpiscaling();
    void minimumDpr();
    void noscreens();
    void screenDpiAndDpr_data();
    void screenDpiAndDpr();
    void screenDpiChange();
    void environment_QT_SCALE_FACTOR();
    void environment_QT_SCREEN_SCALE_FACTORS_data();
    void environment_QT_SCREEN_SCALE_FACTORS();
    void environment_QT_USE_PHYSICAL_DPI();
    void screenAt_data();
    void screenAt();
    void screenGeometry_data();
    void screenGeometry();
    void windowGeometry_data();
    void windowGeometry();
    void spanningWindows_data();
    void spanningWindows();
    void mouseEvents_data();
    void mouseEvents();
    void mouseVelocity();
    void mouseVelocity_data();
    void setCursor();
    void setCursor_data();
};

/// Offscreen platform plugin test setup
const int standardScreenWidth = 640;
const int standardScreenHeight = 480;
const int standardBaseDpi = 96;
const int standardScreenCount = 3;

QJsonArray tst_QHighDpi::createStandardScreens(const QList<qreal> &dpiValues)
{
    Q_ASSERT(dpiValues.count() == standardScreenCount);

    // Create row of three screens: screen#0 screen#1 screen#2
    return QJsonArray {
        QJsonObject {
            {"name", "screen#0"},
            {"x", -standardScreenWidth},
            {"y", -10},
            {"width", standardScreenWidth},
            {"height", standardScreenHeight},
            {"logicalDpi", dpiValues[0]},
            {"logicalBaseDpi", standardBaseDpi},
            {"dpr", 1}
        },
        QJsonObject {
            {"name", "screen#1"},
            {"x", 0},
            {"y", 0},
            {"width", standardScreenWidth},
            {"height", standardScreenHeight},
            {"logicalDpi", dpiValues[1]},
            {"logicalBaseDpi", standardBaseDpi},
            {"dpr", 1}
        },
        QJsonObject {
            {"name", "screen#2"},
            {"x", standardScreenWidth},
            {"y", 10},
            {"width", standardScreenWidth},
            {"height", standardScreenHeight},
            {"logicalDpi", dpiValues[2]},
            {"logicalBaseDpi", standardBaseDpi},
            {"dpr", 1}
        }
    };
}

QGuiApplication *tst_QHighDpi::createOffscreenApplication(const QByteArray &jsonConfig)
{
    // Write offscreen platform config file
    QFile configFile(QLatin1String("qt-offscreen-test-config.json"));
    if (!configFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
        qFatal("Failed to open test config file: %s", qPrintable(configFile.errorString()));
    configFile.resize(0); // truncate
    if (configFile.write(jsonConfig) == -1)
        qFatal("Could not write config file: %s", qPrintable(configFile.errorString()));
    configFile.close();

    // Create QGuiApplication which loads the offscreen platform plugin
    // Note that argc and argv need to stay valid for the duration of the app lifetime,
    // and may be used at any point. The config file used at app startup only.
    static int argc;
    argc = 3;
    static char *argv[3];
    static QByteArray binaryNameArg = QByteArray("tst_qguiapplication");
    argv[0] = binaryNameArg.data();
    static QByteArray platformArg = QByteArray("-platform");
    argv[1] = platformArg.data();
    static QByteArray offscreenAndFileArg;
    offscreenAndFileArg = QByteArray("offscreen:configfile=") + configFile.fileName().toUtf8();
    argv[2] = offscreenAndFileArg.data();

    QGuiApplication *app = new QGuiApplication(argc, argv);
    configFile.remove(); // config file is needed during QGuiApplication construction only.
    return app;
}

QGuiApplication *tst_QHighDpi::createStandardOffscreenApp(const QList<qreal> &dpiValues)
{
    QJsonArray screens = createStandardScreens(dpiValues);
    return createStandardOffscreenApp(screens);
}

QGuiApplication *tst_QHighDpi::createStandardOffscreenApp(const QJsonArray &screens)
{
    QJsonObject config {
        {"synchronousWindowSystemEvents", true},
        {"windowFrameMargins", false},
        {"screens" , screens},
    };
    return createOffscreenApplication(QJsonDocument(config).toJson());
}

/// Auto test begins

void tst_QHighDpi::standardScreenDpiTestData()
{
    // We run each test under different DPI configurations, each with three screens:
    QTest::addColumn<QList<qreal>>("dpiValues");
    // Standard-DPI sanity check
    QTest::newRow("96") << QList<qreal> { 96, 96, 96 };
    // 2x high DPI
    QTest::newRow("192") << QList<qreal> { 192, 192, 192 };
    // Mixed desktop DPI (1.5x, 1.75x, 2x)
    QTest::newRow("144-168-192") << QList<qreal> { 144, 168, 192 };
    // Densities from Android's DisplayMetrics docs, normalized to base 96 DPI
    QTest::newRow("240-252-360") << QList<qreal> { 400./160 * 96, 420./160 * 96, 600./160 * 96 };
}

void tst_QHighDpi::setOffscreenConfiguration(const QJsonObject &configuration)
{
    Q_ASSERT(qApp->platformName() == QLatin1String("offscreen"));
    QPlatformNativeInterface *platformNativeInterface = qApp->platformNativeInterface();
    auto setConfiguration = reinterpret_cast<void (*)(QJsonObject, QPlatformNativeInterface *)>(
        platformNativeInterface->nativeResourceForIntegration("setConfiguration"));
    setConfiguration(configuration, platformNativeInterface);
}

QJsonObject tst_QHighDpi::offscreenConfiguration()
{
    Q_ASSERT(qApp->platformName() == QLatin1String("offscreen"));
    QPlatformNativeInterface *platformNativeInterface = qApp->platformNativeInterface();
    auto getConfiguration = reinterpret_cast<QJsonObject (*)(QPlatformNativeInterface *)>(
        platformNativeInterface->nativeResourceForIntegration("configuration"));
    return getConfiguration(platformNativeInterface);
}

void tst_QHighDpi::cleanup()
{
    // Some test functions set environment variables. Unset them here,
    // in order to avoid getting confusing follow-on errors on test failures.
    qunsetenv("QT_SCALE_FACTOR");
    qunsetenv("QT_SCREEN_SCALE_FACTORS");
    qunsetenv("QT_USE_PHYSICAL_DPI");
}

void tst_QHighDpi::qhighdpiscaling_data()
{
    standardScreenDpiTestData();
}

// Tests the QHighDpiScaling API directly
void tst_QHighDpi::qhighdpiscaling()
{
    QFETCH(QList<qreal>, dpiValues);
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));

    QHighDpiScaling::setGlobalFactor(2);

    // Verfy that QHighDpiScaling::factor() does not crash on nullptr contexts.
    QScreen *screenContext = nullptr;
    QVERIFY(QHighDpiScaling::factor(screenContext) >= 0);
    QPlatformScreen *platformScreenContext = nullptr;
    QVERIFY(QHighDpiScaling::factor(platformScreenContext) >= 0);
    QWindow *windowContext = nullptr;
    QVERIFY(QHighDpiScaling::factor(windowContext) >= 0);
    QHighDpiScaling::setGlobalFactor(1);
}

void tst_QHighDpi::screenDpiAndDpr_data()
{
    standardScreenDpiTestData();
}

void tst_QHighDpi::screenDpiAndDpr()
{
    QFETCH(QList<qreal>, dpiValues);

    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));
    int i = 0;
    for (QScreen *screen : app->screens()) {
        qreal dpi = dpiValues[i++];

        // verify that the devicePixelRatio equation holds: DPR = DPI / BaseDPI
        QCOMPARE(screen->devicePixelRatio(), dpi / standardBaseDpi);
        QCOMPARE(screen->logicalDotsPerInch(), dpi / screen->devicePixelRatio());

        QWindow window(screen);
        QCOMPARE(window.devicePixelRatio(), screen->devicePixelRatio());
    }
}

void tst_QHighDpi::screenDpiChange()
{
    QList<qreal> dpiValues = { 96, 96, 96};
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));

    QCOMPARE(app->devicePixelRatio(), 1);

    // Set new DPI
    int newDpi = 192;
    QJsonValue config = offscreenConfiguration();
    // API defect until Qt 7, so go indirectly via CBOR
    QCborMap map = QCborMap::fromJsonObject(config.toObject());
    map[QLatin1String("screens")][0][QLatin1String("logicalDpi")] = newDpi;
    map[QLatin1String("screens")][1][QLatin1String("logicalDpi")] = newDpi;
    map[QLatin1String("screens")][2][QLatin1String("logicalDpi")] = newDpi;
    setOffscreenConfiguration(map.toJsonObject());

    // TODO check events

    // Verify that the new DPI is in use
    for (QScreen *screen : app->screens()) {
        QCOMPARE(screen->devicePixelRatio(), newDpi / standardBaseDpi);
        QCOMPARE(screen->logicalDotsPerInch(), newDpi / screen->devicePixelRatio());
        QWindow window(screen);
        QCOMPARE(window.devicePixelRatio(), screen->devicePixelRatio());
    }
    QCOMPARE(app->devicePixelRatio(), newDpi / standardBaseDpi);
}

void tst_QHighDpi::environment_QT_SCALE_FACTOR()
{
    qreal factor = 3.1415;
    qputenv("QT_SCALE_FACTOR", QByteArray::number(factor));

    QList<qreal> dpiValues { 96, 144, 192 };
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));
    int i = 0;
    for (QScreen *screen : app->screens()) {
        // Verify that QT_SCALE_FACTOR applies as a multiplicative factor.
        qreal expextedDpr = (dpiValues[i] / standardBaseDpi) * factor;
        ++i;
        QCOMPARE(screen->devicePixelRatio(), expextedDpr);
        QCOMPARE(screen->logicalDotsPerInch(), 96);
        QWindow window(screen);
        QCOMPARE(window.devicePixelRatio(), expextedDpr);
    }
}

void tst_QHighDpi::environment_QT_SCREEN_SCALE_FACTORS_data()
{
    QTest::addColumn<QList<qreal>>("platformScreenDpi"); // The to-be-overridden values
    QTest::addColumn<QByteArray>("environment");
    QTest::addColumn<QList<qreal>>("expectedDprValues");

    QList<qreal> platformScreenDpi { 192, 216, 240 };
    QList<qreal> fromPlatformScreenDpr { 2, 2.25, 2.5 };
    QList<qreal> fromEnvironmentDpr { 1, 1.5, 2 };

    // Correct env. variable values.
    QTest::newRow("list") << platformScreenDpi << QByteArray("1;1.5;2") << fromEnvironmentDpr;
    QTest::newRow("names") << platformScreenDpi << QByteArray("screen#1=1.5;screen#0=1;screen#2=2") << fromEnvironmentDpr;

    // Various broken env. variable values. Should not crash,
    // and should not change the DPR.
    QTest::newRow("empty") << platformScreenDpi << QByteArray("") << fromPlatformScreenDpr;
    QTest::newRow("bogus-1") << platformScreenDpi << QByteArray("foo=bar") << fromPlatformScreenDpr;
    QTest::newRow("bogus-2") << platformScreenDpi << QByteArray("fo0==2;;=;==;=3") << fromPlatformScreenDpr;
}

void tst_QHighDpi::environment_QT_SCREEN_SCALE_FACTORS()
{
    QFETCH(QList<qreal>, platformScreenDpi);
    QFETCH(QByteArray, environment);
    QFETCH(QList<qreal>, expectedDprValues);

    // Verify that setting QT_SCREEN_SCALE_FACTORS overrides the from-platform-screen-DPI DPR.
    {
        qputenv("QT_SCREEN_SCALE_FACTORS", environment);
        std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(platformScreenDpi));
        int i = 0;
        for (QScreen *screen : app->screens()) {
            qreal expextedDpr = expectedDprValues[i];
            ++i;
            QCOMPARE(screen->devicePixelRatio(), expextedDpr);
            QCOMPARE(screen->logicalDotsPerInch(), 96);
            QWindow window(screen);
            QCOMPARE(window.devicePixelRatio(), expextedDpr);
        }
    }
}

void tst_QHighDpi::environment_QT_USE_PHYSICAL_DPI()
{
    qputenv("QT_USE_PHYSICAL_DPI", "1");

    QList<qreal> dpiValues { 96, 144, 192 };
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));

    // Verify that the device pixel ratio is computed as physicalDpi / baseDpi.
    // (which in practice uses physicalSize since this is what QPlatformScreen provides)

    // The default QPlatformScreen::physicalSize() implementation (which QOffscreenScreen
    // currerently uses) assumes a default DPI of 100 and calculates a fake physical size
    // based on that value. Use DPI 100 here as well: if you have changed the default value
    // in QPlatformScreen and get a test failure then update the value below.
    const qreal platformScreenDefualtDpi = 100;
    qreal expextedDpr = (platformScreenDefualtDpi / qreal(standardBaseDpi));

    for (QScreen *screen : app->screens()) {
        QCOMPARE(screen->devicePixelRatio(), expextedDpr);
        QCOMPARE(screen->logicalDotsPerInch(), 96);
        QWindow window(screen);
        QCOMPARE(window.devicePixelRatio(), expextedDpr);
    }
}

void tst_QHighDpi::minimumDpr()
{
    QList<qreal> dpiValues { 40, 60, 95 };
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));
    for (QScreen *screen : app->screens()) {
        // Qt does not currently support DPR values < 1. Make sure
        // the minimum DPR value is 1, also when the screen reports
        // a low DPI.
        QCOMPARE(screen->devicePixelRatio(), 1);
        QWindow window(screen);
        QCOMPARE(window.devicePixelRatio(), 1);
    }
}

QT_BEGIN_NAMESPACE
extern int qt_defaultDpiX();
extern int qt_defaultDpiY();
QT_END_NAMESPACE

void tst_QHighDpi::noscreens()
{
    // Create application object with a no-screens configuration (should not crash)
    QJsonArray noScreens;
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(noScreens));

    QCOMPARE(qApp->devicePixelRatio(), 1);

    // Test calling qt_defaultDpiX/Y: These may be called early during QGuiApplication
    // initialization, before the platform plugin has created screen objects. They
    // should then 1) not crash and 2) return some default value.
    QCOMPARE(qt_defaultDpiX(), qt_defaultDpiY());
}

void tst_QHighDpi::screenAt_data()
{
    standardScreenDpiTestData();
}

void tst_QHighDpi::screenAt()
{
    QFETCH(QList<qreal>, dpiValues);
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));

    QCOMPARE(app->screens().count(), standardScreenCount); // standard setup

    // Verify that screenAt() returns the correct or no screen for various points,
    // for all screens.
    int i = 0;
    for (QScreen *screen : app->screens()) {
        qreal dpi =  dpiValues[i++];

        // veryfy virtualSiblings and that AA_EnableHighDpiScaling is active
        QCOMPARE(screen->virtualSiblings().count(), standardScreenCount);
        QCOMPARE(screen->geometry().size(), QSize(standardScreenWidth, standardScreenHeight) * (96.0 / dpi));

        // test points on screen
        QCOMPARE(app->screenAt(screen->geometry().center()), screen);
        QCOMPARE(app->screenAt(screen->geometry().topLeft()), screen);
        QCOMPARE(app->screenAt(screen->geometry().bottomRight()), screen);

        // test points off  screen
        QCOMPARE(app->screenAt(screen->geometry().center() + QPoint(0, -1000)), nullptr);
        QCOMPARE(app->screenAt(screen->geometry().topLeft() + QPoint(0, -1)), nullptr);
        QCOMPARE(app->screenAt(screen->geometry().bottomRight() + QPoint(0, +1)), nullptr);

        // check the "gaps" created by Qt::AA_EnableHighDpiScaling: no screen there
        if (dpi > 96) {
            QCOMPARE(app->screenAt(screen->geometry().topLeft() + QPoint(-1, 0)), nullptr);
            QCOMPARE(app->screenAt(screen->geometry().bottomRight() + QPoint(1, 0)), nullptr);
        }
    }
}

void tst_QHighDpi::screenGeometry_data()
{
    standardScreenDpiTestData();
}

void tst_QHighDpi::screenGeometry()
{
    QFETCH(QList<qreal>, dpiValues);
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));

    for (QScreen *screen : app->screens()) {
        QRect geometry = screen->geometry();
        QPoint onScreen = geometry.topLeft() + QPoint(10, 10);
        QPoint onScreenNative = QHighDpi::toNativePixels(onScreen, screen);
        QPoint onScreenBack = QHighDpi::fromNativePixels(onScreenNative, screen);

        QCOMPARE(onScreen, onScreenBack);

        QPoint offScreen = geometry.topLeft() - QPoint(10, 10);
        QPoint offScreenNative = QHighDpi::toNativePixels(offScreen, screen);
        QPoint offScreenBack = QHighDpi::fromNativePixels(offScreenNative, screen);
        QCOMPARE(offScreenBack, offScreenBack);
    }
}

void tst_QHighDpi::windowGeometry_data()
{
    standardScreenDpiTestData();
}

void tst_QHighDpi::windowGeometry()
{
    QFETCH(QList<qreal>, dpiValues);
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));

    auto testWindow = [&app](QWindow *window, QScreen *expectedScreen, QPoint expectedPosition, QSize expectedSize) {

        // Is the window correctly sized and on the correct screen?
        QCOMPARE(window->size(), expectedSize);
        QCOMPARE(window->position(), expectedPosition);
        QCOMPARE(window->screen(), expectedScreen);
        QCOMPARE(app->screenAt(window->mapToGlobal(QPoint(0, 0))), expectedScreen);

        // Round-trip coordinates local->global->local, which should return the starting
        // coordinates, also for coordinates outside the window (and screen)
        auto globalRoundtrip = [](QWindow *window, QPoint pos) {
            QCOMPARE(window->mapFromGlobal(window->mapToGlobal(pos)), pos);
        };
        globalRoundtrip(window, QPoint(10, 10)); // window-interior
        globalRoundtrip(window, QPoint(-5, -5)); // outside window, on same screen
        globalRoundtrip(window, QPoint(standardScreenWidth *2, standardScreenHeight * 2)); // Outside window, outside all screens
        globalRoundtrip(window, QPoint(0, -standardScreenWidth)); // Outside window, on neighbor screen

        // Round-trip float coordinates
        auto globalRoundtripF = [](QWindow *window, QPointF pos) {
            QCOMPARE(window->mapFromGlobal(window->mapToGlobal(pos)), pos);
        };

        globalRoundtripF(window, QPointF(10, 10)); // window-interior
        globalRoundtripF(window, QPointF(10.1, 10.1));
        globalRoundtripF(window, QPointF(10.5, 10.5));
        globalRoundtripF(window, QPointF(10.9, 10.9));
        globalRoundtripF(window, QPointF(-5.5, -5.5)); // outside window, on same screen
        globalRoundtripF(window, QPointF(standardScreenWidth * 2.1, standardScreenHeight * 2.1)); // Outside window, outside all screens
        globalRoundtripF(window, QPointF(0.5, -standardScreenWidth)); // Outside window, on neighbor screen
    };

    // verify window geometry for top-level and child windows on all screens
    for (QScreen *screen : app->screens()) {
        QWindow topLevelWindow;
        QSize topLevelSize(40, 40);
        QPoint topLevelPosition(screen->geometry().center());
        topLevelWindow.resize(topLevelSize);
        topLevelWindow.setPosition(topLevelPosition);
        topLevelWindow.show();
        testWindow(&topLevelWindow, screen, topLevelPosition, topLevelSize);

        QWindow childWindow(&topLevelWindow);
        QSize childSize(20, 20);
        QPoint childPosition(10, 10);
        childWindow.resize(childSize);
        childWindow.setPosition(childPosition);
        childWindow.show();
        testWindow(&childWindow, screen, childPosition, childSize);
    }
}

void tst_QHighDpi::spanningWindows_data()
{
    standardScreenDpiTestData();
}

void tst_QHighDpi::spanningWindows()
{
    QFETCH(QList<qreal>, dpiValues);
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));

    QPoint screen0Center = app->screens()[0]->geometry().center();
    int screenWidth = app->screens()[0]->geometry().width();

    // Create window spanning screen 0 and screen 1
    QWindow window;
    QRect windowGeometry = QRect(screen0Center, QSize(screenWidth - 10, 20));
    windowGeometry.adjust(0, 0, -10, 0); // Make sure the center point is on screen 0
    window.setGeometry(windowGeometry);
    window.show();
    QCOMPARE(window.geometry(), windowGeometry);

    // Device independent screen space may be non-contiguous, in which case global
    // window geometry behaves non-intuitivly when a window spans multiple screens:
    //   - The main screen for the window is defined by the windowing system
    //     (usually the screen with most window coverage), and is reflected
    //     by QWindow::screen()
    //   - screen coordinate linear math does not work for points on the window
    //     extending beyond the main screen - these may be on a different screen
    //     with a non-linear coordinate offset.
    //
    // Local window geometry works (mostly) as before:
    //   - QWindow::mapToGlobal() can map any window-local coordinate to the correct
    //     global coordinate and screen, as long as the coordinate is on the window.
    //   - QWindow::mapFromGlobal() can map any global coordinate to the correct
    //     local coordinate, as long as the coordinate is on screen and on the window.
    //
    // Open issue:
    //   - Mapping coordinates which are outside of the window is iffy; we might
    //     fall back to using/assuming the coordinate system for the main screen
    //     in this case.
    QPoint globalTopLeft = window.mapToGlobal(QPoint(0, 0));
    QSize foo = window.geometry().size() - QSize(1, 1);
    QPoint globalBottomRight = window.mapToGlobal(QPoint(foo.width(), foo.height()));

    QCOMPARE(app->screenAt(globalTopLeft), app->screens()[0]);
    QCOMPARE(app->screenAt(globalBottomRight), app->screens()[1]);
}

void tst_QHighDpi::mouseEvents_data()
{
    standardScreenDpiTestData();
}

void tst_QHighDpi::mouseEvents()
{
    QFETCH(QList<qreal>, dpiValues);
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));

    class MousePressTestWindow : public QWindow {
    public:
        QPoint m_mouseTestPoint;

        MousePressTestWindow(QWindow *parent = nullptr)
            :QWindow(parent)
        {

        }

        void mousePressEvent(QMouseEvent *ev) override
        {
            QCOMPARE(ev->position(), m_mouseTestPoint);
            if (devicePixelRatio() == 1 || devicePixelRatio() == 2) // ### off-by-one error on non-integer dpr
                QCOMPARE(mapFromGlobal(ev->globalPosition()), m_mouseTestPoint);
        }

    };

    // Verify mouse event coordinates for top-level and chlid windows on each screen
    for (QScreen *screen : app->screens()) {
        QPoint mouseTestPoint(10, 10);
        MousePressTestWindow topLevelWindow;
        topLevelWindow.m_mouseTestPoint = mouseTestPoint;
        topLevelWindow.resize(QSize(40, 40));
        topLevelWindow.setPosition(screen->geometry().center());
        topLevelWindow.show();

        QTest::mouseClick(&topLevelWindow, Qt::LeftButton, Qt::KeyboardModifiers(), mouseTestPoint);
        MousePressTestWindow childWindow(&topLevelWindow);
        childWindow.m_mouseTestPoint = mouseTestPoint;
        childWindow.resize(QSize(20, 20));
        childWindow.setPosition(QPoint(15, 15));
        childWindow.show();
        QTest::mouseClick(&childWindow, Qt::LeftButton, Qt::KeyboardModifiers(), mouseTestPoint);
    }

    // Verify mouse event coordinates for a window spanning screen 0 and screen 1
    QPoint screen0Center = app->screens()[0]->geometry().center();
    int screenWidth = app->screens()[0]->geometry().width();
    QSize windowSize = QSize(screenWidth - 10, 20);
    QRect windowGeometry = QRect(screen0Center, windowSize);
    windowGeometry.adjust(0, 0, -10, 0); // Make sure the center point is on screen 0
    MousePressTestWindow window;
    window.setGeometry(windowGeometry);
    window.show();

    QPoint screen0Point(QPoint(10,10));
    QPoint screen1Point(QPoint(windowSize.width() - 20,10));
    QCOMPARE(app->screenAt(window.mapToGlobal(screen0Point)), app->screens()[0]);
    QCOMPARE(app->screenAt(window.mapToGlobal(screen1Point)), app->screens()[1]);

    window.m_mouseTestPoint = screen0Point;
    QTest::mouseClick(&window, Qt::LeftButton, Qt::KeyboardModifiers(), screen0Point);
    window.m_mouseTestPoint = screen1Point;
    QTest::mouseClick(&window, Qt::LeftButton, Qt::KeyboardModifiers(), screen1Point);
}

void tst_QHighDpi::mouseVelocity_data()
{
    standardScreenDpiTestData();
}

void tst_QHighDpi::mouseVelocity()
{
    QFETCH(QList<qreal>, dpiValues);
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));

    class MouseVelocityTestWindow : public QWindow {
    public:
        QVector2D velocity;
        bool decel = false;

        bool event(QEvent *ev) override
        {
            if (!ev->isPointerEvent())
                qCDebug(lcTests) << ev;
            return QWindow::event(ev);
        }

        void mousePressEvent(QMouseEvent *ev) override
        {
            velocity = ev->points().first().velocity();
            qCDebug(lcTests) << "velocity" << velocity << ev;
        }

        void mouseMoveEvent(QMouseEvent *ev) override
        {
            velocity = ev->points().first().velocity();
            if (ev->buttons())
                qDebug(lcTests) << "velocity" << velocity << ev;
        }
    };

    // Verify velocity direction and sign on each screen
    // FYI: Turn on the qt.pointer.velocity logging category to see how it's calculated
    for (QScreen *screen : app->screens()) {
        MouseVelocityTestWindow topLevelWindow;
        topLevelWindow.resize(QSize(120, 120));
        topLevelWindow.setPosition(screen->geometry().center());
        topLevelWindow.show();

        QPoint endP;
        qreal maxVx = 0;
        qreal maxVy = 0;
        qreal minVx = INT_MAX;
        qreal minVy = INT_MAX;
        for (int xDelta = 10; xDelta >= -10; xDelta -= 10) {
            for (int yDelta = 10; yDelta >= -10; yDelta -= 10) {
                QPoint p(60, 60);
                // move closer to p, decelerating, to get the velocity down to a small value
                for (int i = 0; i < 12; ++i) {
                    endP += (p - endP) / 4;
                    QTest::mouseMove(&topLevelWindow, endP, 3 * i);
                }
                qCDebug(lcTests) << "beginning drag with dx" << xDelta << "dy" << yDelta;
                QTest::mouseMove(&topLevelWindow, p, 10);
                QTest::mousePress(&topLevelWindow, Qt::LeftButton, {}, p);
                QVERIFY(qAbs(topLevelWindow.velocity.x()) < 50);
                QVERIFY(qAbs(topLevelWindow.velocity.y()) < 50);
                for (int i = 0; i < 4; ++i) {
                    p += QPoint(xDelta, yDelta);
                    QTest::mouseMove(&topLevelWindow, p, 10);
                    if (xDelta) {
                        // same sign and decent magnitude:
                        // 10 px in 10 ms =~ 1000 px / second; should be in logical coordinates on any screen
                        // but it's not exactly 1000 because of the Kalman filter
                        QVERIFY(topLevelWindow.velocity.x() * xDelta > 0);
                        QVERIFY(qAbs(topLevelWindow.velocity.x()) > 500);
                    } else {
                        QVERIFY(qAbs(topLevelWindow.velocity.x()) < 10);
                    }
                    if (yDelta) {
                        QVERIFY(topLevelWindow.velocity.y() * yDelta > 0);
                        QVERIFY(qAbs(topLevelWindow.velocity.y()) > 500);
                    } else {
                        QVERIFY(qAbs(topLevelWindow.velocity.y()) < 10);
                    }
                    maxVx = qMax(topLevelWindow.velocity.x(), maxVx);
                    maxVy = qMax(topLevelWindow.velocity.y(), maxVy);
                    minVx = qMin(topLevelWindow.velocity.x(), minVx);
                    minVy = qMin(topLevelWindow.velocity.y(), minVy);
                }
                QTest::mouseRelease(&topLevelWindow, Qt::LeftButton, {}, p);
                endP = p; // QED
            }
        }
        qCDebug(lcTests) << "mouse land speed record: forward" << maxVx << maxVy << "reverse" << minVx << minVy;
        // all drags were at the same speed, so max speed should be equal in each direction
        QVERIFY(qAbs(maxVx - maxVy) < 10);
        QVERIFY(qAbs(minVx - minVy) < 10);
        QVERIFY(maxVx + minVx < 10);
        QVERIFY(maxVy + minVy < 10);
    }
}

void tst_QHighDpi::setCursor_data()
{
    standardScreenDpiTestData();
}

void tst_QHighDpi::setCursor()
{
    QFETCH(QList<qreal>, dpiValues);
    std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues));

    for (QScreen *screen : app->screens()) {
        QPoint center = screen->geometry().center();
        QCursor::setPos(center.x(), center.y());
        QCOMPARE(QCursor::pos(), center);
    }
}

#include "tst_qhighdpi.moc"
QTEST_APPLESS_MAIN(tst_QHighDpi);