2015-12-05 22:13:34 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2015 Holger Kaelberer <holger.k@elberer.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2019-03-23 16:29:26 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2015-12-05 22:13:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QSocketNotifier>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QTest>
|
2016-08-27 12:59:25 +01:00
|
|
|
#include <QBuffer>
|
2015-12-05 22:13:34 +00:00
|
|
|
#include <QStandardPaths>
|
2016-08-27 12:59:25 +01:00
|
|
|
#include <QImage>
|
|
|
|
#include <QColor>
|
2015-12-05 22:13:34 +00:00
|
|
|
|
2016-01-07 16:37:35 +00:00
|
|
|
#include <kiconloader.h>
|
|
|
|
|
2015-12-05 22:13:34 +00:00
|
|
|
#include "core/daemon.h"
|
|
|
|
#include "core/device.h"
|
2019-06-05 03:42:06 +01:00
|
|
|
#include "testdevice.h"
|
2015-12-05 22:13:34 +00:00
|
|
|
#include "core/kdeconnectplugin.h"
|
|
|
|
#include "kdeconnect-version.h"
|
2016-05-25 19:49:13 +01:00
|
|
|
#include "plugins/sendnotifications/sendnotificationsplugin.h"
|
|
|
|
#include "plugins/sendnotifications/notificationslistener.h"
|
|
|
|
#include "plugins/sendnotifications/notifyingapplication.h"
|
2015-12-05 22:13:34 +00:00
|
|
|
|
|
|
|
// Tweaked NotificationsPlugin for testing
|
2016-05-25 19:49:13 +01:00
|
|
|
class TestNotificationsPlugin : public SendNotificationsPlugin
|
2015-12-05 22:13:34 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2017-09-03 20:39:44 +01:00
|
|
|
explicit TestNotificationsPlugin(QObject* parent, const QVariantList& args)
|
2016-05-25 19:49:13 +01:00
|
|
|
: SendNotificationsPlugin(parent, args)
|
2015-12-05 22:13:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-20 08:05:02 +01:00
|
|
|
~TestNotificationsPlugin() override = default;
|
2015-12-05 22:13:34 +00:00
|
|
|
|
|
|
|
// allow to access notificationsListener for testing:
|
|
|
|
NotificationsListener* getNotificationsListener() const
|
|
|
|
{
|
|
|
|
return notificationsListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setNotificationsListener(NotificationsListener* value)
|
|
|
|
{
|
|
|
|
notificationsListener = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// Tweaked NotificationsListener for testing:
|
|
|
|
class TestedNotificationsListener: public NotificationsListener
|
|
|
|
{
|
2016-11-26 15:05:56 +00:00
|
|
|
Q_OBJECT
|
2015-12-05 22:13:34 +00:00
|
|
|
public:
|
2016-05-25 19:49:13 +01:00
|
|
|
explicit TestedNotificationsListener(KdeConnectPlugin* aPlugin)
|
|
|
|
: NotificationsListener(aPlugin)
|
2015-12-05 22:13:34 +00:00
|
|
|
{}
|
|
|
|
|
2016-06-20 08:05:02 +01:00
|
|
|
~TestedNotificationsListener() override
|
2015-12-05 22:13:34 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
QHash<QString, NotifyingApplication>& getApplications()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
return m_applications;
|
2015-12-05 22:13:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setApplications(const QHash<QString, NotifyingApplication>& value)
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
m_applications = value;
|
2015-12-05 22:13:34 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 23:29:05 +00:00
|
|
|
QSharedPointer<QIODevice> iconForIconName(const QString& iconName) const {
|
|
|
|
return NotificationsListener::iconForIconName(iconName);
|
|
|
|
}
|
|
|
|
|
2016-08-27 12:59:25 +01:00
|
|
|
protected:
|
|
|
|
bool parseImageDataArgument(const QVariant& argument, int& width,
|
|
|
|
int& height, int& rowStride, int& bitsPerSample,
|
|
|
|
int& channels, bool& hasAlpha,
|
|
|
|
QByteArray& imageData) const override
|
|
|
|
{
|
2016-11-26 14:38:08 +00:00
|
|
|
width = argument.toMap().value(QStringLiteral("width")).toInt();
|
|
|
|
height = argument.toMap().value(QStringLiteral("height")).toInt();
|
|
|
|
rowStride = argument.toMap().value(QStringLiteral("rowStride")).toInt();
|
|
|
|
bitsPerSample = argument.toMap().value(QStringLiteral("bitsPerSample")).toInt();
|
|
|
|
channels = argument.toMap().value(QStringLiteral("channels")).toInt();
|
|
|
|
hasAlpha = argument.toMap().value(QStringLiteral("hasAlpha")).toBool();
|
|
|
|
imageData = argument.toMap().value(QStringLiteral("imageData")).toByteArray();
|
2016-08-27 12:59:25 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:13:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class TestNotificationListener : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
TestNotificationListener()
|
|
|
|
: plugin(nullptr)
|
|
|
|
{
|
|
|
|
QStandardPaths::setTestModeEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void testNotify();
|
|
|
|
|
|
|
|
private:
|
|
|
|
TestNotificationsPlugin* plugin;
|
|
|
|
};
|
|
|
|
|
|
|
|
void TestNotificationListener::testNotify()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// set things up:
|
|
|
|
//
|
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
QString dId(QStringLiteral("testid"));
|
2017-09-03 20:39:44 +01:00
|
|
|
TestDevice* d = new TestDevice(nullptr, dId);
|
2016-01-07 16:37:35 +00:00
|
|
|
|
|
|
|
int proxiedNotifications = 0;
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(proxiedNotifications, d->getSentPackets());
|
2015-12-05 22:13:34 +00:00
|
|
|
plugin = new TestNotificationsPlugin(this,
|
|
|
|
QVariantList({ QVariant::fromValue<Device*>(d),
|
2019-06-10 15:40:28 +01:00
|
|
|
QStringLiteral("notifications_plugin"),
|
|
|
|
{QStringLiteral("kdeconnect.notification")},
|
|
|
|
QStringLiteral("preferences-desktop-notification")}));
|
2015-12-05 22:13:34 +00:00
|
|
|
QVERIFY(plugin->getNotificationsListener());
|
|
|
|
delete plugin->getNotificationsListener();
|
|
|
|
|
|
|
|
// inject our tweaked NotificationsListener:
|
2016-05-25 19:49:13 +01:00
|
|
|
TestedNotificationsListener* listener = new TestedNotificationsListener(plugin);
|
2015-12-05 22:13:34 +00:00
|
|
|
QVERIFY(listener);
|
|
|
|
plugin->setNotificationsListener(listener);
|
|
|
|
QCOMPARE(listener, plugin->getNotificationsListener());
|
|
|
|
|
|
|
|
// make sure config is default:
|
2016-11-26 14:38:08 +00:00
|
|
|
plugin->config()->set(QStringLiteral("generalPersistent"), false);
|
|
|
|
plugin->config()->set(QStringLiteral("generalIncludeBody"), true);
|
|
|
|
plugin->config()->set(QStringLiteral("generalUrgency"), 0);
|
2019-06-10 15:40:28 +01:00
|
|
|
QCOMPARE(plugin->config()->get<bool>(QStringLiteral("generalPersistent")), false);
|
|
|
|
QCOMPARE(plugin->config()->get<bool>(QStringLiteral("generalIncludeBody")), true);
|
|
|
|
QCOMPARE(plugin->config()->get<bool>(QStringLiteral("generalUrgency")), false);
|
2015-12-05 22:13:34 +00:00
|
|
|
|
|
|
|
// applications are modified directly:
|
|
|
|
listener->getApplications().clear();
|
|
|
|
QCOMPARE(listener->getApplications().count(), 0);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Go !!!
|
|
|
|
//
|
|
|
|
|
|
|
|
uint replacesId = 99;
|
|
|
|
uint retId;
|
2016-11-26 14:38:08 +00:00
|
|
|
QString appName(QStringLiteral("some-appName"));
|
|
|
|
QString body(QStringLiteral("some-body"));
|
|
|
|
QString icon(QStringLiteral("some-icon"));
|
|
|
|
QString summary(QStringLiteral("some-summary"));
|
2015-12-05 22:13:34 +00:00
|
|
|
|
|
|
|
// regular Notify call that is synchronized ...
|
2016-01-07 16:37:35 +00:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
// ... should return replacesId,
|
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
// ... have triggered sending a packet
|
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2016-01-07 16:37:35 +00:00
|
|
|
// ... with our properties,
|
2019-06-10 15:40:28 +01:00
|
|
|
QCOMPARE(d->getLastPacket()->get<uint>(QStringLiteral("id")), replacesId);
|
|
|
|
QCOMPARE(d->getLastPacket()->get<QString>(QStringLiteral("appName")), appName);
|
|
|
|
QCOMPARE(d->getLastPacket()->get<QString>(QStringLiteral("ticker")), summary + QStringLiteral(": ") + body);
|
|
|
|
QCOMPARE(d->getLastPacket()->get<bool>(QStringLiteral("isClearable")), true);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(d->getLastPacket()->hasPayload(), false);
|
2016-01-07 16:37:35 +00:00
|
|
|
|
2015-12-05 22:13:34 +00:00
|
|
|
// ... and create a new application internally that is initialized correctly:
|
|
|
|
QCOMPARE(listener->getApplications().count(), 1);
|
2016-01-07 16:37:35 +00:00
|
|
|
QVERIFY(listener->getApplications().contains(appName));
|
|
|
|
QVERIFY(listener->getApplications()[appName].active);
|
|
|
|
QCOMPARE(listener->getApplications()[appName].name, appName);
|
|
|
|
QVERIFY(listener->getApplications()[appName].blacklistExpression.pattern().isEmpty());
|
|
|
|
QCOMPARE(listener->getApplications()[appName].name, appName);
|
|
|
|
QCOMPARE(listener->getApplications()[appName].icon, icon);
|
2015-12-05 22:13:34 +00:00
|
|
|
|
|
|
|
// another one, with other timeout and urgency values:
|
2016-11-26 14:38:08 +00:00
|
|
|
QString appName2(QStringLiteral("some-appName2"));
|
|
|
|
QString body2(QStringLiteral("some-body2"));
|
|
|
|
QString icon2(QStringLiteral("some-icon2"));
|
|
|
|
QString summary2(QStringLiteral("some-summary2"));
|
2016-01-07 16:37:35 +00:00
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
retId = listener->Notify(appName2, replacesId+1, icon2, summary2, body2, {}, {{QStringLiteral("urgency"), 2}}, 10);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, replacesId+1);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2019-06-10 15:40:28 +01:00
|
|
|
QCOMPARE(d->getLastPacket()->get<uint>(QStringLiteral("id")), replacesId+1);
|
|
|
|
QCOMPARE(d->getLastPacket()->get<QString>(QStringLiteral("appName")), appName2);
|
|
|
|
QCOMPARE(d->getLastPacket()->get<QString>(QStringLiteral("ticker")), summary2 + QStringLiteral(": ") + body2);
|
|
|
|
QCOMPARE(d->getLastPacket()->get<bool>(QStringLiteral("isClearable")), false); // timeout != 0
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(d->getLastPacket()->hasPayload(), false);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(listener->getApplications().count(), 2);
|
2016-01-07 16:37:35 +00:00
|
|
|
QVERIFY(listener->getApplications().contains(appName2));
|
|
|
|
QVERIFY(listener->getApplications().contains(appName));
|
2015-12-05 22:13:34 +00:00
|
|
|
|
|
|
|
// if persistent-only is set, timeouts > 0 are not synced:
|
2016-11-26 14:38:08 +00:00
|
|
|
plugin->config()->set(QStringLiteral("generalPersistent"), true);
|
2016-01-07 16:37:35 +00:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 1);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, 0U);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(proxiedNotifications, d->getSentPackets());
|
2016-01-07 16:37:35 +00:00
|
|
|
retId = listener->Notify(appName2, replacesId, icon2, summary2, body2, {}, {{}}, 3);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, 0U);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(proxiedNotifications, d->getSentPackets());
|
2015-12-05 22:13:34 +00:00
|
|
|
// but timeout == 0 is
|
2016-01-07 16:37:35 +00:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2016-11-26 14:38:08 +00:00
|
|
|
plugin->config()->set(QStringLiteral("generalPersistent"), false);
|
2015-12-05 22:13:34 +00:00
|
|
|
|
|
|
|
// if min-urgency is set, lower urgency levels are not synced:
|
2016-11-26 14:38:08 +00:00
|
|
|
plugin->config()->set(QStringLiteral("generalUrgency"), 1);
|
2019-06-10 15:40:28 +01:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{QStringLiteral("urgency"), 0}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, 0U);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(proxiedNotifications, d->getSentPackets());
|
2015-12-05 22:13:34 +00:00
|
|
|
// equal urgency is
|
2019-06-10 15:40:28 +01:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{QStringLiteral("urgency"), 1}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2015-12-05 22:13:34 +00:00
|
|
|
// higher urgency as well
|
2019-06-10 15:40:28 +01:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{QStringLiteral("urgency"), 2}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2016-11-26 14:38:08 +00:00
|
|
|
plugin->config()->set(QStringLiteral("generalUrgency"), 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
|
|
|
|
// notifications for a deactivated application are not synced:
|
2016-01-07 16:37:35 +00:00
|
|
|
QVERIFY(listener->getApplications().contains(appName));
|
|
|
|
listener->getApplications()[appName].active = false;
|
|
|
|
QVERIFY(!listener->getApplications()[appName].active);
|
2019-06-10 15:40:28 +01:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{QStringLiteral("urgency"), 0}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, 0U);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(proxiedNotifications, d->getSentPackets());
|
2015-12-05 22:13:34 +00:00
|
|
|
// others are still:
|
2016-01-07 16:37:35 +00:00
|
|
|
retId = listener->Notify(appName2, replacesId+1, icon2, summary2, body2, {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, replacesId+1);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2015-12-05 22:13:34 +00:00
|
|
|
// back to normal:
|
2016-01-07 16:37:35 +00:00
|
|
|
listener->getApplications()[appName].active = true;
|
|
|
|
QVERIFY(listener->getApplications()[appName].active);
|
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2015-12-05 22:13:34 +00:00
|
|
|
|
|
|
|
// notifications with blacklisted subjects are not synced:
|
2016-01-07 16:37:35 +00:00
|
|
|
QVERIFY(listener->getApplications().contains(appName));
|
2016-11-26 14:38:08 +00:00
|
|
|
listener->getApplications()[appName].blacklistExpression.setPattern(QStringLiteral("black[12]|foo(bar|baz)"));
|
|
|
|
retId = listener->Notify(appName, replacesId, icon, QStringLiteral("summary black1"), body, {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, 0U);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(proxiedNotifications, d->getSentPackets());
|
2016-11-26 14:38:08 +00:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, QStringLiteral("summary foobar"), body, {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, 0U);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(proxiedNotifications, d->getSentPackets());
|
2015-12-05 22:13:34 +00:00
|
|
|
// other subjects are synced:
|
2016-11-26 14:38:08 +00:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, QStringLiteral("summary foo"), body, {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2016-11-26 14:38:08 +00:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, QStringLiteral("summary black3"), body, {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2015-12-05 22:13:34 +00:00
|
|
|
// also body is checked by blacklist if requested:
|
2016-11-26 14:38:08 +00:00
|
|
|
plugin->config()->set(QStringLiteral("generalIncludeBody"), true);
|
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, QStringLiteral("body black1"), {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, 0U);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(proxiedNotifications, d->getSentPackets());
|
2016-11-26 14:38:08 +00:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, QStringLiteral("body foobaz"), {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, 0U);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(proxiedNotifications, d->getSentPackets());
|
2015-12-05 22:13:34 +00:00
|
|
|
// body does not matter if inclusion was not requested:
|
2016-11-26 14:38:08 +00:00
|
|
|
plugin->config()->set(QStringLiteral("generalIncludeBody"), false);
|
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, QStringLiteral("body black1"), {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2016-01-07 16:37:35 +00:00
|
|
|
// without body, also ticker value is different:
|
2019-06-10 15:40:28 +01:00
|
|
|
QCOMPARE(d->getLastPacket()->get<QString>(QStringLiteral("ticker")), summary);
|
2016-11-26 14:38:08 +00:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, QStringLiteral("body foobaz"), {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2015-12-05 22:13:34 +00:00
|
|
|
|
|
|
|
// back to normal:
|
2016-11-26 14:38:08 +00:00
|
|
|
listener->getApplications()[appName].blacklistExpression.setPattern(QLatin1String(""));
|
|
|
|
plugin->config()->set(QStringLiteral("generalIncludeBody"), true);
|
2016-01-07 16:37:35 +00:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, body, {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2016-01-07 16:37:35 +00:00
|
|
|
retId = listener->Notify(appName2, replacesId, icon2, summary2, body2, {}, {{}}, 0);
|
2015-12-05 22:13:34 +00:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
2016-01-07 16:37:35 +00:00
|
|
|
|
|
|
|
// icon synchronization:
|
2016-08-27 12:59:25 +01:00
|
|
|
QStringList iconPaths;
|
|
|
|
// appIcon
|
2016-01-07 16:37:35 +00:00
|
|
|
int count = 0;
|
2017-07-20 15:14:07 +01:00
|
|
|
const QStringList icons = KIconLoader::global()->queryIcons(-KIconLoader::SizeEnormous, KIconLoader::Application);
|
|
|
|
for (const auto& iconName : icons) {
|
2016-11-26 14:38:08 +00:00
|
|
|
if (!iconName.endsWith(QLatin1String(".png")))
|
2016-01-07 16:37:35 +00:00
|
|
|
continue;
|
|
|
|
if (count++ > 3) // max 3 iterations
|
|
|
|
break;
|
2016-08-27 12:59:25 +01:00
|
|
|
iconPaths.append(iconName); // memorize some paths for later
|
2016-01-07 16:37:35 +00:00
|
|
|
|
|
|
|
// existing icons are sync-ed if requested
|
2016-11-26 14:38:08 +00:00
|
|
|
plugin->config()->set(QStringLiteral("generalSynchronizeIcons"), true);
|
2016-01-07 16:37:35 +00:00
|
|
|
QFileInfo fi(iconName);
|
|
|
|
retId = listener->Notify(appName, replacesId, fi.baseName(), summary, body, {}, {{}}, 0);
|
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
|
|
|
QVERIFY(d->getLastPacket()->hasPayload());
|
2019-02-12 23:29:05 +00:00
|
|
|
QCOMPARE(d->getLastPacket()->payloadSize(), listener->iconForIconName(fi.baseName())->size());
|
2018-10-07 19:23:20 +01:00
|
|
|
// works also with absolute paths
|
2016-08-27 12:59:25 +01:00
|
|
|
retId = listener->Notify(appName, replacesId, iconName, summary, body, {}, {{}}, 0);
|
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
|
|
|
QVERIFY(d->getLastPacket()->hasPayload());
|
|
|
|
QCOMPARE(d->getLastPacket()->payloadSize(), fi.size());
|
2016-08-27 12:59:25 +01:00
|
|
|
// extensions other than png are not accepted:
|
2019-06-10 15:40:28 +01:00
|
|
|
retId = listener->Notify(appName, replacesId, iconName + QStringLiteral(".svg"), summary, body, {}, {{}}, 0);
|
2016-08-27 12:59:25 +01:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
|
|
|
QVERIFY(!d->getLastPacket()->hasPayload());
|
2016-01-07 16:37:35 +00:00
|
|
|
|
2016-08-27 12:59:25 +01:00
|
|
|
// if sync not requested no payload:
|
2016-11-26 14:38:08 +00:00
|
|
|
plugin->config()->set(QStringLiteral("generalSynchronizeIcons"), false);
|
2016-01-07 16:37:35 +00:00
|
|
|
retId = listener->Notify(appName, replacesId, fi.baseName(), summary, body, {}, {{}}, 0);
|
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
|
|
|
QVERIFY(!d->getLastPacket()->hasPayload());
|
|
|
|
QCOMPARE(d->getLastPacket()->payloadSize(), 0);
|
2016-01-07 16:37:35 +00:00
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
plugin->config()->set(QStringLiteral("generalSynchronizeIcons"), true);
|
2016-08-27 12:59:25 +01:00
|
|
|
|
|
|
|
// image-path in hints
|
|
|
|
if (iconPaths.size() > 0) {
|
2019-06-10 15:40:28 +01:00
|
|
|
retId = listener->Notify(appName, replacesId, iconPaths.size() > 1 ? iconPaths[1] : icon, summary, body, {}, {{QStringLiteral("image-path"), iconPaths[0]}}, 0);
|
2016-08-27 12:59:25 +01:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
|
|
|
QVERIFY(d->getLastPacket()->hasPayload());
|
2016-08-27 12:59:25 +01:00
|
|
|
QFileInfo hintsFi(iconPaths[0]);
|
|
|
|
// image-path has priority over appIcon parameter:
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(d->getLastPacket()->payloadSize(), hintsFi.size());
|
2016-08-27 12:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// image_path in hints
|
|
|
|
if (iconPaths.size() > 0) {
|
2019-06-10 15:40:28 +01:00
|
|
|
retId = listener->Notify(appName, replacesId, iconPaths.size() > 1 ? iconPaths[1] : icon, summary, body, {}, {{QStringLiteral("image_path"), iconPaths[0]}}, 0);
|
2016-08-27 12:59:25 +01:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
|
|
|
QVERIFY(d->getLastPacket()->hasPayload());
|
2016-08-27 12:59:25 +01:00
|
|
|
QFileInfo hintsFi(iconPaths[0]);
|
|
|
|
// image_path has priority over appIcon parameter:
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(d->getLastPacket()->payloadSize(), hintsFi.size());
|
2016-08-27 12:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// image-data in hints
|
|
|
|
// set up:
|
2017-09-03 20:39:44 +01:00
|
|
|
QBuffer* buffer;
|
2016-08-27 12:59:25 +01:00
|
|
|
QImage image;
|
|
|
|
int width = 2, height = 2, rowStride = 4*width, bitsPerSample = 8,
|
|
|
|
channels = 4;
|
|
|
|
bool hasAlpha = 1;
|
|
|
|
char rawData[] = { 0x01, 0x02, 0x03, 0x04, // raw rgba data
|
|
|
|
0x11, 0x12, 0x13, 0x14,
|
|
|
|
0x21, 0x22, 0x23, 0x24,
|
|
|
|
0x31, 0x32, 0x33, 0x34 };
|
2019-06-10 15:40:28 +01:00
|
|
|
QVariantMap imageData = {{QStringLiteral("width"), width}, {QStringLiteral("height"), height}, {QStringLiteral("rowStride"), rowStride},
|
|
|
|
{QStringLiteral("bitsPerSample"), bitsPerSample}, {QStringLiteral("channels"), channels},
|
|
|
|
{QStringLiteral("hasAlpha"), hasAlpha}, {QStringLiteral("imageData"), QByteArray(rawData, sizeof(rawData))}};
|
2016-08-27 12:59:25 +01:00
|
|
|
QVariantMap hints;
|
|
|
|
#define COMPARE_PIXEL(x, y) \
|
|
|
|
QCOMPARE(qRed(image.pixel(x,y)), (int)rawData[x*4 + y*rowStride + 0]); \
|
|
|
|
QCOMPARE(qGreen(image.pixel(x,y)), (int)rawData[x*4 + y*rowStride + 1]); \
|
|
|
|
QCOMPARE(qBlue(image.pixel(x,y)), (int)rawData[x*4 + y*rowStride + 2]); \
|
|
|
|
QCOMPARE(qAlpha(image.pixel(x,y)), (int)rawData[x*4 + y*rowStride + 3]);
|
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
hints.insert(QStringLiteral("image-data"), imageData);
|
2016-08-27 12:59:25 +01:00
|
|
|
if (iconPaths.size() > 0)
|
2016-11-26 14:38:08 +00:00
|
|
|
hints.insert(QStringLiteral("image-path"), iconPaths[0]);
|
2016-08-27 12:59:25 +01:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, body, {}, hints, 0);
|
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
|
|
|
QVERIFY(d->getLastPacket()->hasPayload());
|
|
|
|
buffer = dynamic_cast<QBuffer*>(d->getLastPacket()->payload().data());
|
|
|
|
QCOMPARE(d->getLastPacket()->payloadSize(), buffer->size());
|
2016-08-27 12:59:25 +01:00
|
|
|
// image-data is attached as png data
|
|
|
|
QVERIFY(image.loadFromData(reinterpret_cast<const uchar*>(buffer->data().constData()), buffer->size(), "PNG"));
|
|
|
|
// image-data has priority over image-path:
|
2019-07-19 22:45:56 +01:00
|
|
|
QCOMPARE(image.sizeInBytes(), rowStride*height);
|
2016-08-27 12:59:25 +01:00
|
|
|
// rgba -> argb conversion was done correctly:
|
|
|
|
COMPARE_PIXEL(0,0);
|
|
|
|
COMPARE_PIXEL(1,0);
|
|
|
|
COMPARE_PIXEL(0,1);
|
|
|
|
COMPARE_PIXEL(1,1);
|
|
|
|
|
|
|
|
// same for image_data in hints
|
|
|
|
hints.clear();
|
2016-11-26 14:38:08 +00:00
|
|
|
hints.insert(QStringLiteral("image-data"), imageData);
|
2016-08-27 12:59:25 +01:00
|
|
|
if (iconPaths.size() > 0)
|
2016-11-26 14:38:08 +00:00
|
|
|
hints.insert(QStringLiteral("image_path"), iconPaths[0]);
|
2016-08-27 12:59:25 +01:00
|
|
|
retId = listener->Notify(appName, replacesId, icon, summary, body, {}, hints, 0);
|
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
|
|
|
QVERIFY(d->getLastPacket()->hasPayload());
|
|
|
|
buffer = dynamic_cast<QBuffer*>(d->getLastPacket()->payload().data());
|
|
|
|
QCOMPARE(d->getLastPacket()->payloadSize(), buffer->size());
|
2016-08-27 12:59:25 +01:00
|
|
|
// image-data is attached as png data
|
|
|
|
QVERIFY(image.loadFromData(reinterpret_cast<const uchar*>(buffer->data().constData()), buffer->size(), "PNG"));
|
|
|
|
// image_data has priority over image_path/image-path:
|
2019-07-19 22:45:56 +01:00
|
|
|
QCOMPARE(image.sizeInBytes(), rowStride*height);
|
2016-08-27 12:59:25 +01:00
|
|
|
// rgba -> argb conversion was done correctly:
|
|
|
|
COMPARE_PIXEL(0,0);
|
|
|
|
COMPARE_PIXEL(1,0);
|
|
|
|
COMPARE_PIXEL(0,1);
|
|
|
|
COMPARE_PIXEL(1,1);
|
|
|
|
|
|
|
|
// same for icon_data, which has lowest priority
|
|
|
|
hints.clear();
|
2016-11-26 14:38:08 +00:00
|
|
|
hints.insert(QStringLiteral("icon_data"), imageData);
|
|
|
|
retId = listener->Notify(appName, replacesId, QLatin1String(""), summary, body, {}, hints, 0);
|
2016-08-27 12:59:25 +01:00
|
|
|
QCOMPARE(retId, replacesId);
|
2018-03-04 19:48:51 +00:00
|
|
|
QCOMPARE(++proxiedNotifications, d->getSentPackets());
|
|
|
|
QVERIFY(d->getLastPacket());
|
|
|
|
QVERIFY(d->getLastPacket()->hasPayload());
|
|
|
|
buffer = dynamic_cast<QBuffer*>(d->getLastPacket()->payload().data());
|
2016-08-27 12:59:25 +01:00
|
|
|
// image-data is attached as png data
|
|
|
|
QVERIFY(image.loadFromData(reinterpret_cast<const uchar*>(buffer->data().constData()), buffer->size(), "PNG"));
|
2019-07-19 22:45:56 +01:00
|
|
|
QCOMPARE(image.sizeInBytes(), rowStride*height);
|
2016-08-27 12:59:25 +01:00
|
|
|
// rgba -> argb conversion was done correctly:
|
|
|
|
COMPARE_PIXEL(0,0);
|
|
|
|
COMPARE_PIXEL(1,0);
|
|
|
|
COMPARE_PIXEL(0,1);
|
|
|
|
COMPARE_PIXEL(1,1);
|
|
|
|
|
|
|
|
#undef COMPARE_PIXEL
|
2015-12-05 22:13:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(TestNotificationListener);
|
|
|
|
|
|
|
|
#include "testnotificationlistener.moc"
|