on xenial help sign in
Fix Unity/Platform::isPC, and add tests (LP: #1670657)
b377bdcPete Woods
CMakeLists.txt+5
@@ -69,6 +69,7 @@ find_package(Qt5Gui 5.6 REQUIRED)
69find_package(Qt5DBus 5.6 REQUIRED)
69find_package(Qt5DBus 5.6 REQUIRED)
70find_package(Qt5Concurrent 5.6 REQUIRED)
70find_package(Qt5Concurrent 5.6 REQUIRED)
71find_package(Qt5Sql 5.6 REQUIRED)
71find_package(Qt5Sql 5.6 REQUIRED)
72+find_package(Qt5Test 5.6 REQUIRED)
72
73
73pkg_check_modules(APPLICATION_API REQUIRED unity-shell-application=27)
74pkg_check_modules(APPLICATION_API REQUIRED unity-shell-application=27)
74pkg_check_modules(GEONAMES REQUIRED geonames>=0.2)
75pkg_check_modules(GEONAMES REQUIRED geonames>=0.2)
@@ -79,6 +80,10 @@ pkg_check_modules(QMENUMODEL REQUIRED qmenumodel)
79pkg_check_modules(GD3 REQUIRED gnome-desktop-3.0)
80pkg_check_modules(GD3 REQUIRED gnome-desktop-3.0)
80pkg_check_modules(UAL REQUIRED ubuntu-app-launch-3)
81pkg_check_modules(UAL REQUIRED ubuntu-app-launch-3)
81pkg_check_modules(UBUNTUGESTURES REQUIRED UbuntuGestures)
82pkg_check_modules(UBUNTUGESTURES REQUIRED UbuntuGestures)
83+pkg_check_modules(QTDBUSTEST REQUIRED
84+ libqtdbustest-1
85+ libqtdbusmock-1
86+ )
82
87
83### Check UbuntuGestures private headers. No pkg-config (.pc) file is provided for them
88### Check UbuntuGestures private headers. No pkg-config (.pc) file is provided for them
84find_path(UBUNTUGESTUREPRIV
89find_path(UBUNTUGESTUREPRIV
debian/control+4
@@ -29,6 +29,8 @@ Build-Depends: cmake,
29 libpam0g-dev,
29 libpam0g-dev,
30 libpulse-dev,
30 libpulse-dev,
31 libqmenumodel-dev (>= 0.2.12),
31 libqmenumodel-dev (>= 0.2.12),
32+ libqtdbusmock1-dev (>= 0.7),
33+ libqtdbustest1-dev,
32 libqt5svg5-dev,
34 libqt5svg5-dev,
33 libqt5xmlpatterns5-dev,
35 libqt5xmlpatterns5-dev,
34 libsystemsettings-dev,
36 libsystemsettings-dev,
@@ -48,6 +50,7 @@ Build-Depends: cmake,
48 pkg-config,
50 pkg-config,
49 python3-all:any,
51 python3-all:any,
50 python3-setuptools,
52 python3-setuptools,
53+ qtdbustest-runner,
51 qml-module-qt-labs-folderlistmodel,
54 qml-module-qt-labs-folderlistmodel,
52 qml-module-qt-labs-settings,
55 qml-module-qt-labs-settings,
53 qml-module-qtqml-statemachine,
56 qml-module-qtqml-statemachine,
@@ -205,6 +208,7 @@ Multi-Arch: same
205Pre-Depends: ${misc:Pre-Depends},
208Pre-Depends: ${misc:Pre-Depends},
206Depends: dbus-test-runner,
209Depends: dbus-test-runner,
207 parallel,
210 parallel,
211+ qtdbustest-runner,
208 qt5-default,
212 qt5-default,
209 qtdeclarative5-dev-tools,
213 qtdeclarative5-dev-tools,
210 unity8 (= ${source:Version}),
214 unity8 (= ${source:Version}),
plugins/Unity/Platform/platform.cpp+16−2
@@ -17,9 +17,11 @@
17#include "platform.h"
17#include "platform.h"
18
18
19#include <QDBusConnection>
19#include <QDBusConnection>
20+#include <QSet>
21+#include <QString>
20
22
21Platform::Platform(QObject *parent)
23Platform::Platform(QObject *parent)
22− : QObject(parent)
24+ : QObject(parent), m_isPC(true), m_isMultiSession(true)
23{
25{
24 QMetaObject::invokeMethod(this, "init");
26 QMetaObject::invokeMethod(this, "init");
25}
27}
@@ -31,8 +33,20 @@ void Platform::init()
31 QDBusInterface seatIface("org.freedesktop.login1", "/org/freedesktop/login1/seat/self", "org.freedesktop.login1.Seat",
33 QDBusInterface seatIface("org.freedesktop.login1", "/org/freedesktop/login1/seat/self", "org.freedesktop.login1.Seat",
32 QDBusConnection::systemBus(), this);
34 QDBusConnection::systemBus(), this);
33
35
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",
34 m_chassis = iface.property("Chassis").toString();
46 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);
36 m_isMultiSession = seatIface.property("CanMultiSession").toBool() && seatIface.property("CanGraphical").toBool();
50 m_isMultiSession = seatIface.property("CanMultiSession").toBool() && seatIface.property("CanGraphical").toBool();
37}
51}
38
52
tests/CMakeLists.txt+1
@@ -20,6 +20,7 @@ add_subdirectory(uqmlscene)
20# Use our custom implementation of qmlscene and import dbus-test-runner
20# Use our custom implementation of qmlscene and import dbus-test-runner
21add_executable(qmlscene ALIAS uqmlscene)
21add_executable(qmlscene ALIAS uqmlscene)
22import_executables(dbus-test-runner)
22import_executables(dbus-test-runner)
23+import_executables(qdbus-simple-test-runner)
23
24
24set(UNITY_PLUGINPATH "${CMAKE_BINARY_DIR}/plugins" CACHE PATH "Path to pre-built unity8 plugin dir.")
25set(UNITY_PLUGINPATH "${CMAKE_BINARY_DIR}/plugins" CACHE PATH "Path to pre-built unity8 plugin dir.")
25set(UNITY_MOCKPATH "${CMAKE_BINARY_DIR}/tests/mocks" CACHE PATH "Path to pre-built unity8 mock dir.")
26set(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 @@
1add_subdirectory(Indicators)
1add_subdirectory(Indicators)
2add_subdirectory(Launcher)
2add_subdirectory(Launcher)
3+add_subdirectory(Platform)
3add_subdirectory(Session)
4add_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"