on xenial help sign in
Fix setting some AS properties
commit00a56e5d2a2f2139457fa0f971df939d014ca03b authorMichael Terry <michael.terry@canonical.com> dateWed 23 Mar 2016, 16:47 (11 years ago) parentc7e6c92
6 files changed+116−22
plugins/AccountsService/AccountsServiceDBusAdaptor.cpp+12−2 tests/plugins/AccountsService/CMakeLists.txt+2−1 tests/plugins/AccountsService/PropertiesServer.cpp+36−7 tests/plugins/AccountsService/PropertiesServer.h+7 tests/plugins/AccountsService/client.cpp+46−12 tests/plugins/AccountsService/interfaces.xml+13
plugins/AccountsService/AccountsServiceDBusAdaptor.cpp+12−2
@@ -57,8 +57,18 @@ QDBusPendingCall AccountsServiceDBusAdaptor::setUserPropertyAsync(const QString
5757{
5858 QDBusInterface *iface = getUserInterface(user);
5959 if (iface != nullptr && iface->isValid()) {
60− // The value needs to be carefully wrapped
61− return iface->asyncCall(QStringLiteral("Set"), interface, property, QVariant::fromValue(QDBusVariant(value)));
60+ if (interface == QStringLiteral("org.freedesktop.Accounts.User")) {
61+ // Standard AccountsService properties use special set methods.
62+ // It will not let you use the usual DBus property setters.
63+ QDBusInterface accountsIface(iface->service(),
64+ iface->path(),
65+ interface,
66+ iface->connection());
67+ return accountsIface.asyncCall(QStringLiteral("Set") + property, value);
68+ } else {
69+ // The value needs to be carefully wrapped
70+ return iface->asyncCall(QStringLiteral("Set"), interface, property, QVariant::fromValue(QDBusVariant(value)));
71+ }
6272 }
6373 return QDBusPendingCall::fromCompletedCall(QDBusMessage::createError(QDBusError::Other, QStringLiteral("Invalid Interface")));
6474}
tests/plugins/AccountsService/CMakeLists.txt+2−1
@@ -5,7 +5,7 @@ else()
55endif()
66macro(make_dbus_class NAME INTERFACE)
77 if(${CMAKE_CURRENT_SOURCE_DIR}/interfaces.xml IS_NEWER_THAN ${CMAKE_CURRENT_BINARY_DIR}/${NAME}Adaptor.h)
8− execute_process(COMMAND ${QDBUSXML2CPP_EXECUTABLE} -c ${NAME}Adaptor -a ${CMAKE_CURRENT_BINARY_DIR}/${NAME}Adaptor ${CMAKE_CURRENT_SOURCE_DIR}/interfaces.xml ${INTERFACE})
8+ execute_process(COMMAND ${QDBUSXML2CPP_EXECUTABLE} -c ${NAME}Adaptor -i types.h -a ${CMAKE_CURRENT_BINARY_DIR}/${NAME}Adaptor ${CMAKE_CURRENT_SOURCE_DIR}/interfaces.xml ${INTERFACE})
99 endif()
1010endmacro(make_dbus_class)
1111
@@ -20,6 +20,7 @@ make_dbus_class(UscInput com.canonical.Unity.Input)
2020
2121include_directories(
2222 ${CMAKE_CURRENT_BINARY_DIR}
23+ ${CMAKE_CURRENT_SOURCE_DIR}
2324 ${CMAKE_SOURCE_DIR}/plugins/AccountsService
2425 )
2526
tests/plugins/AccountsService/PropertiesServer.cpp+36−7
@@ -25,9 +25,7 @@
2525#include <QDBusMessage>
2626#include <QDBusMetaType>
2727
28−using StringMap = QMap<QString,QString>;
29−using StringMapList = QList<StringMap>;
30−Q_DECLARE_METATYPE(StringMapList)
28+#define IFACE_ACCOUNTS_USER QStringLiteral("org.freedesktop.Accounts.User")
3129
3230PropertiesServer::PropertiesServer(QObject *parent)
3331 : QObject(parent)
@@ -62,6 +60,38 @@ void PropertiesServer::Set(const QString &interface, const QString &property, co
6260{
6361 QVariant newValue = variant.variant();
6462
63+ if (interface == IFACE_ACCOUNTS_USER) {
64+ sendErrorReply(QDBusError::InvalidArgs, QStringLiteral("Property is not writable"));
65+ return;
66+ }
67+
68+ internalSet(interface, property, newValue);
69+}
70+
71+void PropertiesServer::SetBackgroundFile(const QString &backgroundFile)
72+{
73+ internalSet(IFACE_ACCOUNTS_USER, QStringLiteral("BackgroundFile"), backgroundFile);
74+}
75+
76+void PropertiesServer::SetEmail(const QString &email)
77+{
78+ internalSet(IFACE_ACCOUNTS_USER, QStringLiteral("Email"), email);
79+}
80+
81+void PropertiesServer::SetInputSources(const StringMapList &inputSources)
82+{
83+ internalSet(IFACE_ACCOUNTS_USER, QStringLiteral("InputSources"), QVariant::fromValue(inputSources));
84+}
85+
86+void PropertiesServer::SetRealName(const QString &realName)
87+{
88+ internalSet(IFACE_ACCOUNTS_USER, QStringLiteral("RealName"), realName);
89+}
90+
91+void PropertiesServer::internalSet(const QString &interface, const QString &property, const QVariant &variant)
92+{
93+ QVariant newValue = variant;
94+
6595 if (m_properties[interface].contains(property)) {
6696 QVariant& oldValue = m_properties[interface][property];
6797 if (oldValue != newValue) {
@@ -69,14 +99,12 @@ void PropertiesServer::Set(const QString &interface, const QString &property, co
6999 if (interface == QStringLiteral("com.canonical.unity.AccountsService") &&
70100 property == QStringLiteral("LauncherItems")) {
71101 newValue = QVariant::fromValue(qdbus_cast<QList<QVariantMap>>(newValue.value<QDBusArgument>()));
72− } else if (interface == "org.freedesktop.Accounts.User" && property == "InputSources") {
73− newValue = QVariant::fromValue(qdbus_cast<StringMapList>(newValue.value<QDBusArgument>()));
74102 }
75103
76104 oldValue = newValue;
77105
78106 // Special case for user properties.
79− if (interface == "org.freedesktop.Accounts.User") {
107+ if (interface == IFACE_ACCOUNTS_USER) {
80108 Q_EMIT Changed();
81109 } else {
82110 QVariantMap propertyChanges;
@@ -108,6 +136,7 @@ void PropertiesServer::Reset()
108136 m_properties["com.ubuntu.location.providers.here.AccountsService"]["LicenseAccepted"] = false;
109137 m_properties["com.ubuntu.location.providers.here.AccountsService"]["LicenseBasePath"] = "";
110138 m_properties["org.freedesktop.Accounts.User"]["BackgroundFile"] = "";
111− m_properties["org.freedesktop.Accounts.User"]["RealName"] = "";
139+ m_properties["org.freedesktop.Accounts.User"]["Email"] = "";
112140 m_properties["org.freedesktop.Accounts.User"]["InputSources"] = QVariant::fromValue(StringMapList());
141+ m_properties["org.freedesktop.Accounts.User"]["RealName"] = "";
113142}
tests/plugins/AccountsService/PropertiesServer.h+7
@@ -24,6 +24,7 @@
2424#include <QDBusVariant>
2525#include <QObject>
2626#include <QString>
27+#include "types.h"
2728
2829class PropertiesServer: public QObject, protected QDBusContext
2930{
@@ -36,6 +37,10 @@ public Q_SLOTS:
3637 QDBusVariant Get(const QString &interface, const QString &property) const;
3738 QVariantMap GetAll(const QString &interface) const;
3839 void Set(const QString &interface, const QString &property, const QDBusVariant &variant);
40+ void SetBackgroundFile(const QString &backgroundFile);
41+ void SetEmail(const QString &email);
42+ void SetInputSources(const StringMapList &inputSources);
43+ void SetRealName(const QString &realName);
3944
4045 // mock only.
4146 void Reset();
@@ -45,6 +50,8 @@ Q_SIGNALS:
4550 void Changed();
4651
4752private:
53+ void internalSet(const QString &interface, const QString &property, const QVariant &variant);
54+
4855 QHash<QString, QVariantMap> m_properties;
4956};
5057
tests/plugins/AccountsService/client.cpp+46−12
@@ -19,16 +19,13 @@
1919
2020#include "AccountsService.h"
2121#include "AccountsServiceDBusAdaptor.h"
22+#include "types.h"
2223#include <QSignalSpy>
2324#include <QTest>
2425#include <QDebug>
2526#include <QDBusReply>
2627#include <QDBusMetaType>
2728
28−using StringMap = QMap<QString,QString>;
29−using StringMapList = QList<StringMap>;
30−Q_DECLARE_METATYPE(StringMapList)
31−
3229template <class T>
3330QVariant dbusVariant(const T& value) { return QVariant::fromValue(QDBusVariant(value)); }
3431
@@ -263,10 +260,13 @@ private Q_SLOTS:
263260 AccountsService session(this, QTest::currentTestFunction());
264261
265262 QCOMPARE(session.backgroundFile(), QString());
266− ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
267− "org.freedesktop.Accounts.User",
268− "BackgroundFile",
269− dbusVariant("/test/BackgroundFile")));
263+
264+ QDBusInterface accountsIface(m_userInterface->service(),
265+ m_userInterface->path(),
266+ "org.freedesktop.Accounts.User");
267+ ASSERT_DBUS_CALL(accountsIface.asyncCall("SetBackgroundFile",
268+ "/test/BackgroundFile"));
269+
270270 QTRY_COMPARE(session.backgroundFile(), QString("/test/BackgroundFile"));
271271 }
272272
@@ -311,6 +311,39 @@ private Q_SLOTS:
311311 QCOMPARE(arguments.at(0).toInt(), 0);
312312 }
313313
314+ void testSetRealName()
315+ {
316+ AccountsService session(this, QTest::currentTestFunction());
317+
318+ // Just confirm that we can't set normal way
319+ auto message = m_userInterface->call("Set",
320+ "org.freedesktop.Accounts.User",
321+ "RealName",
322+ dbusVariant("Stallman"));
323+ QCOMPARE(message.type(), QDBusMessage::ErrorMessage);
324+ QCOMPARE(message.errorName(), QStringLiteral("org.freedesktop.DBus.Error.InvalidArgs"));
325+
326+ message = m_userInterface->call("Get",
327+ "org.freedesktop.Accounts.User",
328+ "RealName");
329+ QCOMPARE(message.type(), QDBusMessage::ReplyMessage);
330+ auto response = message.arguments().first().value<QDBusVariant>().variant().toString();
331+ QCOMPARE(response, QStringLiteral(""));
332+
333+ // Now try it via our AS wrapper and confirm it did get through, unlike above
334+ session.setRealName("Stallman");
335+
336+ QCOMPARE(session.realName(), QStringLiteral("Stallman"));
337+
338+ message = m_userInterface->call("Get",
339+ "org.freedesktop.Accounts.User",
340+ "RealName");
341+ QCOMPARE(message.type(), QDBusMessage::ReplyMessage);
342+ response = message.arguments().first().value<QDBusVariant>().variant().toString();
343+ QCOMPARE(response, QStringLiteral("Stallman"));
344+
345+ }
346+
314347 void testAsynchronousChangeForKeymaps()
315348 {
316349 AccountsService session(this, QTest::currentTestFunction());
@@ -325,10 +358,11 @@ private Q_SLOTS:
325358 map2.insert("xkb", "fr");
326359 inputSources.append(map2);
327360
328− ASSERT_DBUS_CALL(m_userInterface->asyncCall("Set",
329− "org.freedesktop.Accounts.User",
330− "InputSources",
331− QVariant::fromValue(QDBusVariant(QVariant::fromValue(inputSources)))));
361+ QDBusInterface accountsIface(m_userInterface->service(),
362+ m_userInterface->path(),
363+ "org.freedesktop.Accounts.User");
364+ ASSERT_DBUS_CALL(accountsIface.asyncCall("SetInputSources",
365+ QVariant::fromValue(inputSources)));
332366 QStringList result = {"cz+qwerty", "fr"};
333367 QTRY_COMPARE(session.keymaps(), result);
334368 }
tests/plugins/AccountsService/interfaces.xml+13
@@ -42,6 +42,19 @@
4242 <interface name="com.ubuntu.location.providers.here.AccountsService" />
4343 <interface name="org.freedesktop.Accounts.User">
4444 <signal name="Changed" />
45+ <method name="SetBackgroundFile">
46+ <arg name="backgroundFile" type="s" direction="in" />
47+ </method>
48+ <method name="SetEmail">
49+ <arg name="email" type="s" direction="in" />
50+ </method>
51+ <method name="SetInputSources">
52+ <annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="StringMapList"/>
53+ <arg name="inputSources" type="aa{ss}" direction="in" />
54+ </method>
55+ <method name="SetRealName">
56+ <arg name="realName" type="s" direction="in" />
57+ </method>
4558 </interface>
4659 <interface name="com.canonical.unity.AccountsService.Private" />
4760