on xenial help sign in
Fix Unity/Platform::isPC, and add tests (LP: #1670657)

Approved by: Lukáš Tinkl, Unity8 CI Bot

commitb377bdcce2adf1e37158f04bcb1e1e7cbd463876 authorPete Woods <pete.woods@canonical.com> dateTue 28 Mar 2017, 21:45 (10 years ago) parent582e3087ca7e39
7 files changed+199−2
CMakeLists.txt+5 debian/control+4 plugins/Unity/Platform/platform.cpp+16−2 tests/CMakeLists.txt+1 tests/plugins/Unity/CMakeLists.txt+1 tests/plugins/Unity/Platform/CMakeLists.txt+30 tests/plugins/Unity/Platform/platformtest.cpp+142
CMakeLists.txt+5
@@ -69,6 +69,7 @@ find_package(Qt5Gui 5.6 REQUIRED)
6969find_package(Qt5DBus 5.6 REQUIRED)
7070find_package(Qt5Concurrent 5.6 REQUIRED)
7171find_package(Qt5Sql 5.6 REQUIRED)
72+find_package(Qt5Test 5.6 REQUIRED)
7273
7374pkg_check_modules(APPLICATION_API REQUIRED unity-shell-application=27)
7475pkg_check_modules(GEONAMES REQUIRED geonames>=0.2)
@@ -79,6 +80,10 @@ pkg_check_modules(QMENUMODEL REQUIRED qmenumodel)
7980pkg_check_modules(GD3 REQUIRED gnome-desktop-3.0)
8081pkg_check_modules(UAL REQUIRED ubuntu-app-launch-3)
8182pkg_check_modules(UBUNTUGESTURES REQUIRED UbuntuGestures)
83+pkg_check_modules(QTDBUSTEST REQUIRED
84+ libqtdbustest-1
85+ libqtdbusmock-1
86+ )
8287
8388### Check UbuntuGestures private headers. No pkg-config (.pc) file is provided for them
8489find_path(UBUNTUGESTUREPRIV
debian/control+4
@@ -29,6 +29,8 @@ Build-Depends: cmake,
2929 libpam0g-dev,
3030 libpulse-dev,
3131 libqmenumodel-dev (>= 0.2.12),
32+ libqtdbusmock1-dev (>= 0.7),
33+ libqtdbustest1-dev,
3234 libqt5svg5-dev,
3335 libqt5xmlpatterns5-dev,
3436 libsystemsettings-dev,
@@ -48,6 +50,7 @@ Build-Depends: cmake,
4850 pkg-config,
4951 python3-all:any,
5052 python3-setuptools,
53+ qtdbustest-runner,
5154 qml-module-qt-labs-folderlistmodel,
5255 qml-module-qt-labs-settings,
5356 qml-module-qtqml-statemachine,
@@ -205,6 +208,7 @@ Multi-Arch: same
205208Pre-Depends: ${misc:Pre-Depends},
206209Depends: dbus-test-runner,
207210 parallel,
211+ qtdbustest-runner,
208212 qt5-default,
209213 qtdeclarative5-dev-tools,
210214 unity8 (= ${source:Version}),
plugins/Unity/Platform/platform.cpp+16−2
@@ -17,9 +17,11 @@
1717#include "platform.h"
1818
1919#include <QDBusConnection>
20+#include <QSet>
21+#include <QString>
2022
2123Platform::Platform(QObject *parent)
22− : QObject(parent)
24+ : QObject(parent), m_isPC(true), m_isMultiSession(true)
2325{
2426 QMetaObject::invokeMethod(this, "init");
2527}
@@ -31,8 +33,20 @@ void Platform::init()
3133 QDBusInterface seatIface("org.freedesktop.login1", "/org/freedesktop/login1/seat/self", "org.freedesktop.login1.Seat",
3234 QDBusConnection::systemBus(), this);
3335
36+ // From the source at https://cgit.freedesktop.org/systemd/systemd/tree/src/hostname/hostnamed.c#n130
37+ // "vm\0"
38+ // "container\0"
39+ // "desktop\0"
40+ // "laptop\0"
41+ // "server\0"
42+ // "tablet\0"
43+ // "handset\0"
44+ // "watch\0"
45+ // "embedded\0",
3446 m_chassis = iface.property("Chassis").toString();
35− m_isPC = (m_chassis == "desktop" || m_chassis == "laptop" || m_chassis == "server");
47+
48+ // A PC is not a handset, tablet or watch.
49+ m_isPC = !QSet<QString>{"handset", "tablet", "watch"}.contains(m_chassis);
3650 m_isMultiSession = seatIface.property("CanMultiSession").toBool() && seatIface.property("CanGraphical").toBool();
3751}
3852
tests/CMakeLists.txt+1
@@ -20,6 +20,7 @@ add_subdirectory(uqmlscene)
2020# Use our custom implementation of qmlscene and import dbus-test-runner
2121add_executable(qmlscene ALIAS uqmlscene)
2222import_executables(dbus-test-runner)
23+import_executables(qdbus-simple-test-runner)
2324
2425set(UNITY_PLUGINPATH "${CMAKE_BINARY_DIR}/plugins" CACHE PATH "Path to pre-built unity8 plugin dir.")
2526set(UNITY_MOCKPATH "${CMAKE_BINARY_DIR}/tests/mocks" CACHE PATH "Path to pre-built unity8 mock dir.")
tests/plugins/Unity/CMakeLists.txt+1
@@ -1,3 +1,4 @@
11add_subdirectory(Indicators)
22add_subdirectory(Launcher)
3+add_subdirectory(Platform)
34add_subdirectory(Session)
tests/plugins/Unity/Platform/CMakeLists.txtnew file+30
@@ -0,0 +1,30 @@
1+
2+include_directories(
3+ ${Qt5Core_INCLUDE_DIRS}
4+ ${Qt5DBus_INCLUDE_DIRS}
5+ ${Qt5Test_INCLUDE_DIRS}
6+ ${QTDBUSTEST_INCLUDE_DIRS}
7+ ${CMAKE_CURRENT_BINARY_DIR}
8+ ${CMAKE_SOURCE_DIR}/plugins/Unity/Platform
9+)
10+
11+add_executable(platformtest
12+ platformtest.cpp
13+ ${CMAKE_SOURCE_DIR}/plugins/Unity/Platform/platform.cpp
14+ )
15+
16+target_link_libraries(platformtest
17+ Qt5::Core
18+ Qt5::DBus
19+ Qt5::Test
20+ ${QTDBUSTEST_LDFLAGS}
21+ )
22+
23+
24+install(TARGETS platformtest
25+ DESTINATION "${SHELL_PRIVATE_LIBDIR}/tests/plugins/Unity/Platform"
26+)
27+
28+add_unity8_unittest(Platform qdbus-simple-test-runner
29+ ARGS $<TARGET_FILE:platformtest>
30+ )
tests/plugins/Unity/Platform/platformtest.cppnew file+142
@@ -0,0 +1,142 @@
1+/*
2+ * Copyright (C) 2017 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Authors:
17+ * Pete Woods <pete.woods@canonical.com>
18+ */
19+
20+#include "platform.h"
21+
22+#include <libqtdbustest/DBusTestRunner.h>
23+#include <libqtdbusmock/DBusMock.h>
24+
25+#include <QtTest>
26+#include <QDebug>
27+
28+using namespace QtDBusTest;
29+using namespace QtDBusMock;
30+
31+class PlatformTest: public QObject
32+{
33+
34+Q_OBJECT
35+
36+private:
37+ struct TestFixture
38+ {
39+ TestFixture() :
40+ mock(dbus)
41+ {
42+ }
43+
44+ DBusTestRunner dbus;
45+ DBusMock mock;
46+ };
47+
48+ QScopedPointer<TestFixture> m_data;
49+
50+ void registerLogin1(bool canMultiSession, bool canGraphical)
51+ {
52+ m_data->mock.registerLogin1({{"DefaultSeat", QVariantMap
53+ {
54+ {"CanMultiSession", canMultiSession},
55+ {"CanGraphical", canGraphical}
56+ }}}
57+ );
58+ }
59+
60+ void registerHostname1(const QString& chassis)
61+ {
62+ m_data->mock.registerHostname1({
63+ {"Chassis", chassis}
64+ });
65+ }
66+
67+ void startServices()
68+ {
69+ m_data->dbus.startServices();
70+ }
71+
72+private Q_SLOTS:
73+
74+ void init()
75+ {
76+ m_data.reset(new TestFixture);
77+ }
78+
79+ void cleanup()
80+ {
81+ m_data.reset();
82+ }
83+
84+ void testIsPC_data()
85+ {
86+ QTest::addColumn<QString>("chassis");
87+ QTest::addColumn<bool>("result");
88+
89+ QTest::newRow("vm") << "vm" << true;
90+ QTest::newRow("container") << "container" << true;
91+ QTest::newRow("desktop") << "desktop" << true;
92+ QTest::newRow("laptop") << "laptop" << true;
93+ QTest::newRow("server") << "server" << true;
94+ QTest::newRow("tablet") << "tablet" << false;
95+ QTest::newRow("handset") << "handset" << false;
96+ QTest::newRow("watch") << "watch" << false;
97+ QTest::newRow("embedded") << "embedded" << true;
98+ }
99+
100+ void testIsPC()
101+ {
102+ QFETCH(QString, chassis);
103+ QFETCH(bool, result);
104+
105+ registerLogin1(true, true);
106+ registerHostname1(chassis);
107+ startServices();
108+
109+ Platform p;
110+ QCOMPARE(p.isPC(), result);
111+ QCOMPARE(p.chassis(), chassis);
112+ }
113+
114+ void testIsMultiSession_data()
115+ {
116+ QTest::addColumn<bool>("canMultiSession");
117+ QTest::addColumn<bool>("canGraphical");
118+ QTest::addColumn<bool>("result");
119+
120+ QTest::newRow("") << true << true << true;
121+ QTest::newRow("") << true << false << false;
122+ QTest::newRow("") << false << true << false;
123+ QTest::newRow("") << false << false << false;
124+ }
125+
126+ void testIsMultiSession()
127+ {
128+ QFETCH(bool, canMultiSession);
129+ QFETCH(bool, canGraphical);
130+ QFETCH(bool, result);
131+
132+ registerLogin1(canMultiSession, canGraphical);
133+ registerHostname1("desktop");
134+ startServices();
135+
136+ Platform p;
137+ QCOMPARE(p.isMultiSession(), result);
138+ }
139+};
140+
141+QTEST_GUILESS_MAIN(PlatformTest)
142+#include "platformtest.moc"