on xenial help sign in
Add tests for AccountsService plugin
commit447982b01960fd20dc232f21b7066fec3a3d7822 authorMichael Terry <michael.terry@canonical.com> dateTue 30 Jul 2013, 13:54 (13 years ago) parent889e2a5
10 files changed+323−2
debian/control+2 tests/plugins/AccountsService/AccountsServer.cpp+35 tests/plugins/AccountsService/AccountsServer.h+39 tests/plugins/AccountsService/CMakeLists.txt+40 tests/plugins/AccountsService/PropertiesServer.cpp+45 tests/plugins/AccountsService/PropertiesServer.h+44 tests/plugins/AccountsService/client.cpp+49 tests/plugins/AccountsService/interfaces.xml+20 tests/plugins/AccountsService/server.cpp+46 tests/plugins/CMakeLists.txt+3−2
debian/control+2
@@ -3,6 +3,7 @@ Section: x11
33Priority: optional
44Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
55Build-Depends: cmake,
6+ dbus-test-runner,
67 debhelper (>= 9),
78 libdee-qt5-dev (>= 3.0),
89 libgl1-mesa-dev[!armhf] | libgl-dev[!armhf],
@@ -27,6 +28,7 @@ Build-Depends: cmake,
2728 python3,
2829 qt5-default,
2930 qtbase5-dev,
31+ qtbase5-dev-tools,
3032 qtbase5-private-dev,
3133 qtdeclarative5-dev,
3234 qtdeclarative5-dev-tools,
tests/plugins/AccountsService/AccountsServer.cppnew file+35
@@ -0,0 +1,35 @@
1+/*
2+ * Copyright 2013 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * version 3 along with this program. If not, see
15+ * <http://www.gnu.org/licenses/>
16+ *
17+ * Authored by: Michael Terry <michael.terry@canonical.com>
18+ */
19+
20+#include "AccountsServer.h"
21+
22+AccountsServer::AccountsServer(QObject *parent)
23+ : QObject(parent)
24+{
25+}
26+
27+QDBusObjectPath AccountsServer::FindUserByName(const QString &user)
28+{
29+ if (user == "testuser") {
30+ return QDBusObjectPath("/user");
31+ } else {
32+ sendErrorReply(QDBusError::InvalidArgs, "Bad user");
33+ return QDBusObjectPath("/");
34+ }
35+}
tests/plugins/AccountsService/AccountsServer.hnew file+39
@@ -0,0 +1,39 @@
1+/*
2+ * Copyright 2013 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * version 3 along with this program. If not, see
15+ * <http://www.gnu.org/licenses/>
16+ *
17+ * Authored by: Michael Terry <michael.terry@canonical.com>
18+ */
19+
20+#ifndef UNITY_ACCOUNTSSERVER_H
21+#define UNITY_ACCOUNTSSERVER_H
22+
23+#include <QtCore/QObject>
24+#include <QtCore/QString>
25+#include <QtDBus/QDBusContext>
26+#include <QtDBus/QDBusObjectPath>
27+
28+class AccountsServer: public QObject, protected QDBusContext
29+{
30+ Q_OBJECT
31+
32+public:
33+ explicit AccountsServer(QObject *parent = 0);
34+
35+public Q_SLOTS:
36+ QDBusObjectPath FindUserByName(const QString &user);
37+};
38+
39+#endif
tests/plugins/AccountsService/CMakeLists.txtnew file+40
@@ -0,0 +1,40 @@
1+macro(make_dbus_class NAME INTERFACE)
2+ if(${CMAKE_CURRENT_SOURCE_DIR}/interfaces.xml IS_NEWER_THAN ${CMAKE_CURRENT_BINARY_DIR}/${NAME}Adaptor.h)
3+ execute_process(COMMAND qdbusxml2cpp -c ${NAME}Adaptor -a ${CMAKE_CURRENT_BINARY_DIR}/${NAME}Adaptor ${CMAKE_CURRENT_SOURCE_DIR}/interfaces.xml ${INTERFACE})
4+ endif()
5+endmacro(make_dbus_class)
6+
7+make_dbus_class(Accounts org.freedesktop.Accounts)
8+make_dbus_class(Properties org.freedesktop.DBus.Properties)
9+
10+include_directories(
11+ ${CMAKE_CURRENT_BINARY_DIR}
12+ ${CMAKE_SOURCE_DIR}/plugins/AccountsService
13+ )
14+
15+add_definitions(-DSM_BUSNAME=sessionBus)
16+
17+add_executable(mock-server
18+ ${CMAKE_CURRENT_BINARY_DIR}/AccountsAdaptor.cpp
19+ ${CMAKE_CURRENT_BINARY_DIR}/PropertiesAdaptor.cpp
20+ server.cpp
21+ AccountsServer.cpp
22+ PropertiesServer.cpp
23+ )
24+qt5_use_modules(mock-server Core DBus)
25+
26+add_executable(test-accountsservice
27+ ${CMAKE_SOURCE_DIR}/plugins/AccountsService/AccountsService.cpp
28+ client.cpp
29+ )
30+qt5_use_modules(test-accountsservice Core DBus Test)
31+
32+add_test(NAME test-accountsservice
33+ COMMAND dbus-test-runner
34+ --task ${CMAKE_CURRENT_BINARY_DIR}/mock-server
35+ --task-name server
36+ --ignore-return
37+ --task ${CMAKE_CURRENT_BINARY_DIR}/test-accountsservice
38+ --task-name client
39+ --wait-for org.freedesktop.Accounts
40+ )
tests/plugins/AccountsService/PropertiesServer.cppnew file+45
@@ -0,0 +1,45 @@
1+/*
2+ * Copyright 2013 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * version 3 along with this program. If not, see
15+ * <http://www.gnu.org/licenses/>
16+ *
17+ * Authored by: Michael Terry <michael.terry@canonical.com>
18+ */
19+
20+#include "PropertiesServer.h"
21+
22+PropertiesServer::PropertiesServer(QObject *parent)
23+ : QObject(parent),
24+ edge_demo(true)
25+{
26+}
27+
28+QDBusVariant PropertiesServer::Get(const QString &interface, const QString &property)
29+{
30+ if (interface == "com.canonical.unity.AccountsService" && property == "demo-edges") {
31+ return QDBusVariant(QVariant(edge_demo));
32+ } else {
33+ sendErrorReply(QDBusError::InvalidArgs, "Bad interface or property");
34+ return QDBusVariant(QVariant());
35+ }
36+}
37+
38+void PropertiesServer::Set(const QString &interface, const QString &property, const QDBusVariant &variant)
39+{
40+ if (interface == "com.canonical.unity.AccountsService" && property == "demo-edges") {
41+ edge_demo = variant.variant().toBool();
42+ } else {
43+ sendErrorReply(QDBusError::InvalidArgs, "Bad interface or property");
44+ }
45+}
tests/plugins/AccountsService/PropertiesServer.hnew file+44
@@ -0,0 +1,44 @@
1+/*
2+ * Copyright 2013 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * version 3 along with this program. If not, see
15+ * <http://www.gnu.org/licenses/>
16+ *
17+ * Authored by: Michael Terry <michael.terry@canonical.com>
18+ */
19+
20+#ifndef UNITY_PROPERTIESSERVER_H
21+#define UNITY_PROPERTIESSERVER_H
22+
23+#include "PropertiesServer.h"
24+#include <QtCore/QObject>
25+#include <QtCore/QString>
26+#include <QtDBus/QDBusContext>
27+#include <QtDBus/QDBusVariant>
28+
29+class PropertiesServer: public QObject, protected QDBusContext
30+{
31+ Q_OBJECT
32+
33+public:
34+ explicit PropertiesServer(QObject *parent = 0);
35+
36+public Q_SLOTS:
37+ QDBusVariant Get(const QString &interface, const QString &property);
38+ void Set(const QString &interface, const QString &property, const QDBusVariant &variant);
39+
40+private:
41+ bool edge_demo;
42+};
43+
44+#endif
tests/plugins/AccountsService/client.cppnew file+49
@@ -0,0 +1,49 @@
1+/*
2+ * Copyright 2013 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * version 3 along with this program. If not, see
15+ * <http://www.gnu.org/licenses/>
16+ *
17+ * Authored by: Michael Terry <michael.terry@canonical.com>
18+ */
19+
20+#include "AccountsService.h"
21+#include <QtTest/QSignalSpy>
22+#include <QtTest/QTest>
23+
24+class AccountsServiceTest : public QObject
25+{
26+ Q_OBJECT
27+
28+private Q_SLOTS:
29+
30+ void testInvalids()
31+ {
32+ // Test various invalid calls
33+ AccountsService session;
34+ QCOMPARE(session.getUserProperty("NOPE", "demo-edges"), QVariant());
35+ QCOMPARE(session.getUserProperty("testuser", "NOPE"), QVariant());
36+ }
37+
38+ void testGetSet()
39+ {
40+ AccountsService session;
41+ QCOMPARE(session.getUserProperty("testuser", "demo-edges"), QVariant(true));
42+ session.setUserProperty("testuser", "demo-edges", QVariant(false));
43+ QCOMPARE(session.getUserProperty("testuser", "demo-edges"), QVariant(false));
44+ }
45+};
46+
47+QTEST_MAIN(AccountsServiceTest)
48+
49+#include "client.moc"
tests/plugins/AccountsService/interfaces.xmlnew file+20
@@ -0,0 +1,20 @@
1+<node>
2+ <interface name="org.freedesktop.DBus.Properties">
3+ <method name="Get">
4+ <arg name="interface" type="s" direction="in" />
5+ <arg name="property" type="s" direction="in" />
6+ <arg name="value" type="v" direction="out" />
7+ </method>
8+ <method name="Set">
9+ <arg name="interface" type="s" direction="in" />
10+ <arg name="property" type="s" direction="in" />
11+ <arg name="value" type="v" direction="in" />
12+ </method>
13+ </interface>
14+ <interface name="org.freedesktop.Accounts">
15+ <method name="FindUserByName">
16+ <arg name="user" type="s" direction="in" />
17+ <arg name="path" type="o" direction="out" />
18+ </method>
19+ </interface>
20+</node>
tests/plugins/AccountsService/server.cppnew file+46
@@ -0,0 +1,46 @@
1+/*
2+ * Copyright 2013 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * version 3 along with this program. If not, see
15+ * <http://www.gnu.org/licenses/>
16+ *
17+ * Authored by: Michael Terry <michael.terry@canonical.com>
18+ */
19+
20+#include "AccountsAdaptor.h"
21+#include "AccountsServer.h"
22+#include "PropertiesAdaptor.h"
23+#include "PropertiesServer.h"
24+#include <QtCore/QCoreApplication>
25+
26+int main(int argc, char *argv[])
27+{
28+ QCoreApplication a(argc, argv);
29+
30+ auto accounts = new AccountsServer();
31+ new AccountsAdaptor(accounts);
32+
33+ auto props = new PropertiesServer();
34+ new PropertiesAdaptor(props);
35+
36+ // We use the session bus for testing. The real plugin uses the system bus
37+ auto connection = QDBusConnection::sessionBus();
38+ if (!connection.registerObject("/user", props))
39+ return 1;
40+ if (!connection.registerObject("/org/freedesktop/Accounts", accounts))
41+ return 1;
42+ if (!connection.registerService("org.freedesktop.Accounts"))
43+ return 1;
44+
45+ return a.exec();
46+}
tests/plugins/CMakeLists.txt+3−2
@@ -1,4 +1,5 @@
1−add_subdirectory(Utils)
1+add_subdirectory(AccountsService)
2+add_subdirectory(ListViewWithPageHeader)
23add_subdirectory(Ubuntu)
34add_subdirectory(Unity)
4−add_subdirectory(ListViewWithPageHeader)
5+add_subdirectory(Utils)