Changeset View
Changeset View
Standalone View
Standalone View
libkworkspace/autotests/testPlatformDetection.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Martin Flöser <mgraesslin@kde.org> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Lesser General Public | ||||
6 | * License as published by the Free Software Foundation; either | ||||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | * later version accepted by the membership of KDE e.V. (or its | ||||
9 | * successor approved by the membership of KDE e.V.), which shall | ||||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | * | ||||
12 | * This library is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | * Lesser General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU Lesser General Public | ||||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include <QtTest/QtTest> | ||||
22 | | ||||
23 | #include "../kworkspace.h" | ||||
24 | | ||||
25 | #include <vector> | ||||
26 | | ||||
27 | class TestPlatformDetection : public QObject | ||||
28 | { | ||||
29 | Q_OBJECT | ||||
30 | private Q_SLOTS: | ||||
31 | void init(); | ||||
32 | void testPlatformSelection_data(); | ||||
33 | void testPlatformSelection(); | ||||
34 | void testArguments_data(); | ||||
35 | void testArguments(); | ||||
36 | void testQtQpaPlatformIsSet_data(); | ||||
37 | void testQtQpaPlatformIsSet(); | ||||
38 | }; | ||||
39 | | ||||
40 | void TestPlatformDetection::init() | ||||
41 | { | ||||
42 | qunsetenv("QT_QPA_PLATFORM"); | ||||
43 | qunsetenv("XDG_SESSION_TYPE"); | ||||
44 | } | ||||
45 | | ||||
46 | void TestPlatformDetection::testPlatformSelection_data() | ||||
47 | { | ||||
48 | QTest::addColumn<QByteArray>("xdgSessionType"); | ||||
49 | QTest::addColumn<QByteArray>("expectedQtQpaPlatform"); | ||||
50 | | ||||
51 | QTest::newRow("wayland") << QByteArrayLiteral("wayland") << QByteArrayLiteral("wayland"); | ||||
52 | QTest::newRow("x11") << QByteArrayLiteral("x11") << QByteArrayLiteral("xcb"); | ||||
53 | QTest::newRow("unknown") << QByteArrayLiteral("mir") << QByteArray(); | ||||
54 | } | ||||
55 | | ||||
56 | void TestPlatformDetection::testPlatformSelection() | ||||
57 | { | ||||
58 | QVERIFY(!qEnvironmentVariableIsSet("QT_QPA_PLATFORM")); | ||||
59 | QFETCH(QByteArray, xdgSessionType); | ||||
60 | qputenv("XDG_SESSION_TYPE", xdgSessionType); | ||||
61 | | ||||
62 | std::vector<QByteArray> cppArgv{ | ||||
63 | QByteArrayLiteral("testPlatformDetction") | ||||
64 | }; | ||||
65 | std::vector<char*> argv; | ||||
66 | for (QByteArray &arg : cppArgv) { | ||||
67 | argv.push_back(arg.data()); | ||||
68 | } | ||||
69 | KWorkSpace::platformDetection(1, argv.data()); | ||||
70 | QTEST(qgetenv("QT_QPA_PLATFORM"), "expectedQtQpaPlatform"); | ||||
71 | } | ||||
72 | | ||||
73 | void TestPlatformDetection::testArguments_data() | ||||
74 | { | ||||
75 | QTest::addColumn<QByteArray>("arg"); | ||||
76 | | ||||
77 | QTest::newRow("-platform") << QByteArrayLiteral("-platform"); | ||||
78 | QTest::newRow("--platform") << QByteArrayLiteral("--platform"); | ||||
79 | QTest::newRow("-platform=wayland") << QByteArrayLiteral("-platform=wayland"); | ||||
80 | QTest::newRow("--platform=wayland") << QByteArrayLiteral("--platform=wayland"); | ||||
81 | } | ||||
82 | | ||||
83 | void TestPlatformDetection::testArguments() | ||||
84 | { | ||||
85 | QVERIFY(!qEnvironmentVariableIsSet("QT_QPA_PLATFORM")); | ||||
86 | qputenv("XDG_SESSION_TYPE", "wayland"); | ||||
87 | | ||||
88 | QFETCH(QByteArray, arg); | ||||
89 | std::vector<QByteArray> cppArgv{ | ||||
90 | QByteArrayLiteral("testPlatformDetction"), | ||||
91 | arg, | ||||
92 | QByteArrayLiteral("wayland") | ||||
93 | }; | ||||
94 | std::vector<char*> argv; | ||||
95 | for (QByteArray &arg : cppArgv) { | ||||
96 | argv.push_back(arg.data()); | ||||
97 | } | ||||
98 | KWorkSpace::platformDetection(3, argv.data()); | ||||
99 | QVERIFY(!qEnvironmentVariableIsSet("QT_QPA_PLATFORM")); | ||||
100 | } | ||||
101 | | ||||
102 | void TestPlatformDetection::testQtQpaPlatformIsSet_data() | ||||
103 | { | ||||
104 | QTest::addColumn<QByteArray>("qtQpaPlatform"); | ||||
105 | QTest::addColumn<QByteArray>("xdgSessionType"); | ||||
106 | | ||||
107 | QTest::newRow("xcb - x11") << QByteArrayLiteral("xcb") << QByteArrayLiteral("x11"); | ||||
108 | QTest::newRow("xcb - wayland") << QByteArrayLiteral("xcb") << QByteArrayLiteral("wayland"); | ||||
109 | QTest::newRow("wayland - x11") << QByteArrayLiteral("wayland") << QByteArrayLiteral("x11"); | ||||
110 | QTest::newRow("wayland - wayland") << QByteArrayLiteral("wayland") << QByteArrayLiteral("wayland"); | ||||
111 | QTest::newRow("windows - x11") << QByteArrayLiteral("windows") << QByteArrayLiteral("x11"); | ||||
112 | } | ||||
113 | | ||||
114 | void TestPlatformDetection::testQtQpaPlatformIsSet() | ||||
115 | { | ||||
116 | // test verifies that if QT_QPA_PLATFORM is set the env variable does not get adjusted | ||||
117 | QFETCH(QByteArray, qtQpaPlatform); | ||||
118 | QFETCH(QByteArray, xdgSessionType); | ||||
119 | qputenv("QT_QPA_PLATFORM", qtQpaPlatform); | ||||
120 | qputenv("XDG_SESSION_TYPE", xdgSessionType); | ||||
121 | | ||||
122 | KWorkSpace::platformDetection(0, nullptr); | ||||
123 | QCOMPARE(qgetenv("QT_QPA_PLATFORM"), qtQpaPlatform); | ||||
124 | } | ||||
125 | | ||||
126 | QTEST_GUILESS_MAIN(TestPlatformDetection) | ||||
127 | | ||||
128 | #include "testPlatformDetection.moc" |