on xenial help sign in
Fix isPC property of Unity/Platform plugin

- Also add tests

commit411188c6371e9895a43c42b8f5ec1c17ff665c94 authorPete Woods <pete.woods@canonical.com> dateWed 15 Mar 2017, 18:57 (10 years ago) parent46bca5f
7 files changed+183−2
CMakeLists.txt+5 debian/control+2 plugins/Unity/Platform/platform.cpp+9−2 tests/CMakeLists.txt+1 tests/plugins/Unity/CMakeLists.txt+1 tests/plugins/Unity/Platform/CMakeLists.txt+25 tests/plugins/Unity/Platform/platformtest.cpp+140
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+2
@@ -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,
plugins/Unity/Platform/platform.cpp+9−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,13 @@ 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 docs at https://www.freedesktop.org/wiki/Software/systemd/hostnamed/
37+ // the options are "desktop", "laptop", "server", "tablet", "handset", as well
38+ // as the special chassis types "vm" and "container".
3439 m_chassis = iface.property("Chassis").toString();
35− m_isPC = (m_chassis == "desktop" || m_chassis == "laptop" || m_chassis == "server");
40+
41+ // A PC is not a phone or tablet.
42+ m_isPC = !QSet<QString>{"handset", "tablet"}.contains(m_chassis);
3643 m_isMultiSession = seatIface.property("CanMultiSession").toBool() && seatIface.property("CanGraphical").toBool();
3744}
3845
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+25
@@ -0,0 +1,25 @@
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+add_unity8_unittest(platformtest qdbus-simple-test-runner
24+ ARGS "${CMAKE_BINARY_DIR}/platformtest"
25+ )
tests/plugins/Unity/Platform/platformtest.cppnew file+140
@@ -0,0 +1,140 @@
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("desktop") << "desktop" << true;
90+ QTest::newRow("laptop") << "laptop" << true;
91+ QTest::newRow("server") << "server" << true;
92+ QTest::newRow("tablet") << "tablet" << false;
93+ QTest::newRow("handset") << "handset" << false;
94+ QTest::newRow("vm") << "vm" << true;
95+ QTest::newRow("container") << "container" << true;
96+ }
97+
98+ void testIsPC()
99+ {
100+ QFETCH(QString, chassis);
101+ QFETCH(bool, result);
102+
103+ registerLogin1(true, true);
104+ registerHostname1(chassis);
105+ startServices();
106+
107+ Platform p;
108+ QCOMPARE(p.isPC(), result);
109+ QCOMPARE(p.chassis(), chassis);
110+ }
111+
112+ void testIsMultiSession_data()
113+ {
114+ QTest::addColumn<bool>("canMultiSession");
115+ QTest::addColumn<bool>("canGraphical");
116+ QTest::addColumn<bool>("result");
117+
118+ QTest::newRow("") << true << true << true;
119+ QTest::newRow("") << true << false << false;
120+ QTest::newRow("") << false << true << false;
121+ QTest::newRow("") << false << false << false;
122+ }
123+
124+ void testIsMultiSession()
125+ {
126+ QFETCH(bool, canMultiSession);
127+ QFETCH(bool, canGraphical);
128+ QFETCH(bool, result);
129+
130+ registerLogin1(canMultiSession, canGraphical);
131+ registerHostname1("desktop");
132+ startServices();
133+
134+ Platform p;
135+ QCOMPARE(p.isMultiSession(), result);
136+ }
137+};
138+
139+QTEST_GUILESS_MAIN(PlatformTest)
140+#include "platformtest.moc"