1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
// SPDX-FileCopyrightText: 2024 Devin Lin <devin@kde.org>
// SPDX-License-Identifier: GPL-2.0-or-later
#include <KJob>
#include <KNotification>
#include <KNotificationJobUiDelegate>
#include <KNotificationReplyAction>
#include <KUiServerV2JobTracker>
#include <stdlib.h>
#include "tests.h"
NotificationTest::NotificationTest(QObject *parent)
: QObject{parent}
{
}
void BasicNotificationTest::sendNotification(QCoreApplication &app)
{
KNotification *notification = new KNotification(QStringLiteral("notificationTest"));
notification->setComponentName(QStringLiteral("plasma_mobile_notificationtest"));
notification->setIconName(QStringLiteral("notification-active"));
notification->setText("This is a test notification!");
auto action = notification->addAction("Action!");
Q_UNUSED(action)
connect(notification, &KNotification::closed, &app, QCoreApplication::quit);
notification->sendEvent();
}
void UrlNotificationTest::sendNotification(QCoreApplication &app)
{
KNotification *notification = new KNotification(QStringLiteral("notificationTest"));
notification->setComponentName(QStringLiteral("plasma_mobile_notificationtest"));
notification->setTitle("Web link");
notification->setText("I like links!");
notification->setUrls({QUrl{"file:/usr/share/wallpapers/Next/contents/images/1920x1080.png"}});
connect(notification, &KNotification::closed, &app, QCoreApplication::quit);
notification->sendEvent();
}
void ReplyNotificationTest::sendNotification(QCoreApplication &app)
{
KNotification *notification = new KNotification(QStringLiteral("notificationTest"));
notification->setComponentName(QStringLiteral("plasma_mobile_notificationtest"));
notification->setIconName(QStringLiteral("avatar-default-symbolic"));
notification->setTitle("John");
notification->setText("This is great news! Let's meet up tomorrow!");
auto replyAction = std::make_unique<KNotificationReplyAction>("Reply");
replyAction->setPlaceholderText("Reply to John...");
QObject::connect(replyAction.get(), &KNotificationReplyAction::replied, [](const QString &text) {
qDebug() << "you replied with" << text;
});
notification->setReplyAction(std::move(replyAction));
connect(notification, &KNotification::closed, &app, QCoreApplication::quit);
notification->sendEvent();
}
void LowUrgencyNotificationTest::sendNotification(QCoreApplication &app)
{
KNotification *notification = new KNotification(QStringLiteral("notificationTest"));
notification->setComponentName(QStringLiteral("plasma_mobile_notificationtest"));
notification->setIconName(QStringLiteral("notification-inactive"));
notification->setTitle("Low Urgency Notification");
notification->setText("This is not very important...");
notification->setUrgency(KNotification::LowUrgency);
connect(notification, &KNotification::closed, &app, QCoreApplication::quit);
notification->sendEvent();
}
void HighUrgencyNotificationTest::sendNotification(QCoreApplication &app)
{
KNotification *notification = new KNotification(QStringLiteral("notificationTest"));
notification->setComponentName(QStringLiteral("plasma_mobile_notificationtest"));
notification->setIconName(QStringLiteral("notification-active"));
notification->setTitle("Urgent Notification");
notification->setText("This is very urgent! AAAAAA");
notification->setUrgency(KNotification::HighUrgency);
connect(notification, &KNotification::closed, &app, QCoreApplication::quit);
notification->sendEvent();
}
void CriticalUrgencyNotificationTest::sendNotification(QCoreApplication &app)
{
KNotification *notification = new KNotification(QStringLiteral("notificationTest"));
notification->setComponentName(QStringLiteral("plasma_mobile_notificationtest"));
notification->setIconName(QStringLiteral("notification-active"));
notification->setTitle("Critically Urgent Notification");
notification->setText("This is very urgent! AAAAAA");
notification->setUrgency(KNotification::CriticalUrgency);
auto action = notification->addAction("Action!");
Q_UNUSED(action)
connect(notification, &KNotification::closed, &app, QCoreApplication::quit);
notification->sendEvent();
}
FakeJob::FakeJob(QObject *parent)
: KJob{parent}
, m_timer{new QTimer{this}}
{
setTotalAmount(KJob::Bytes, 100);
setProcessedAmount(KJob::Bytes, 0);
connect(m_timer, &QTimer::timeout, this, &FakeJob::timerFinished);
}
void FakeJob::start()
{
setProcessedAmount(KJob::Bytes, 0);
m_timer->start(1000);
QString s_title = "Processing";
QString s_source = "Source";
QString s_destination = "Destination";
Q_EMIT description(this, s_title, {s_source, QStringLiteral("data:[...]")}, {s_destination, QStringLiteral("data:[...]")});
setFinishedNotificationHidden();
}
void FakeJob::timerFinished()
{
if (processedAmount(KJob::Bytes) + 10 < 100) {
setProcessedAmount(KJob::Bytes, processedAmount(KJob::Bytes) + 10);
emitSpeed(rand() % 100);
} else {
setProcessedAmount(KJob::Bytes, 100);
emitResult();
}
}
void JobNotificationTest::sendNotification(QCoreApplication &app)
{
KUiServerV2JobTracker *jobTracker = new KUiServerV2JobTracker{};
KJob *job = new FakeJob{this};
job->setProperty("immediateProgressReporting", true);
job->setProperty("desktopFileName", "org.kde.plasmashell");
connect(job, &KJob::finished, &app, QCoreApplication::quit);
jobTracker->registerJob(job);
job->start();
}
|