|
| 1 | +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | + |
| 4 | +#include <gtest/gtest.h> |
| 5 | + |
| 6 | +#include "net/linux/noticeutil.h" |
| 7 | + |
| 8 | +#include <QSignalSpy> |
| 9 | +#include <QStringList> |
| 10 | +#include <QVariantMap> |
| 11 | +#include <QVector> |
| 12 | + |
| 13 | +using namespace cooperation_core; |
| 14 | + |
| 15 | +namespace { |
| 16 | + |
| 17 | +class FakeNoticeUtil : public NoticeUtil |
| 18 | +{ |
| 19 | +public: |
| 20 | + struct NotifyCall |
| 21 | + { |
| 22 | + uint replacesId { 0 }; |
| 23 | + QString body; |
| 24 | + int timeout { 0 }; |
| 25 | + }; |
| 26 | + |
| 27 | + QVector<NotifyCall> calls; |
| 28 | + QVector<uint> closed; |
| 29 | + uint nextId { 1 }; |
| 30 | + |
| 31 | +protected: |
| 32 | + uint sendNotify(const QString &title, const QString &body, const QStringList &actions, |
| 33 | + const QVariantMap &hints, int expireTimeout, uint replacesId) override |
| 34 | + { |
| 35 | + Q_UNUSED(title) |
| 36 | + Q_UNUSED(actions) |
| 37 | + Q_UNUSED(hints) |
| 38 | + calls.append(NotifyCall { replacesId, body, expireTimeout }); |
| 39 | + // Mimics org.freedesktop.Notifications: replacing an existing id |
| 40 | + // returns that same id, a new notification gets a fresh one. |
| 41 | + return replacesId == 0 ? nextId++ : replacesId; |
| 42 | + } |
| 43 | + |
| 44 | + void sendClose(uint notificationId) override |
| 45 | + { |
| 46 | + closed.append(notificationId); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +const QVariantMap kHiddenHints { { "x-deepin-ShowInNotifyCenter", false } }; |
| 51 | + |
| 52 | +} // namespace |
| 53 | + |
| 54 | +TEST(NoticeUtilTest, RequestAfterDismissalStartsNewBubble) |
| 55 | +{ |
| 56 | + FakeNoticeUtil util; |
| 57 | + |
| 58 | + util.notifyMessage("File transfer", "request 1", { "accept" }, kHiddenHints, 10000); |
| 59 | + ASSERT_EQ(1, util.calls.size()); |
| 60 | + EXPECT_EQ(0u, util.calls[0].replacesId); |
| 61 | + |
| 62 | + // Progress updates are the only notifications allowed to replace a bubble. |
| 63 | + util.notifyMessage("File transfer", "progress", { "cancel" }, kHiddenHints, 15000, true); |
| 64 | + ASSERT_EQ(2, util.calls.size()); |
| 65 | + EXPECT_EQ(1u, util.calls[1].replacesId); |
| 66 | + |
| 67 | + // Acting on the request dismisses the notification. |
| 68 | + ASSERT_TRUE(QMetaObject::invokeMethod(&util, "onActionTriggered", |
| 69 | + Q_ARG(uint, 1), Q_ARG(QString, QStringLiteral("accept")))); |
| 70 | + |
| 71 | + // The next request is a new notification, not an update of the dismissed |
| 72 | + // one. Some daemons never present an update for an id they already closed. |
| 73 | + util.notifyMessage("File transfer", "request 2", { "accept" }, kHiddenHints, 10000); |
| 74 | + ASSERT_EQ(3, util.calls.size()); |
| 75 | + EXPECT_EQ(0u, util.calls[2].replacesId); |
| 76 | +} |
| 77 | + |
| 78 | +TEST(NoticeUtilTest, CloseReleasesNotificationId) |
| 79 | +{ |
| 80 | + FakeNoticeUtil util; |
| 81 | + |
| 82 | + util.notifyMessage("File transfer", "request", { "accept" }, kHiddenHints, 10000); |
| 83 | + util.closeNotification(); |
| 84 | + ASSERT_EQ(1, util.closed.size()); |
| 85 | + EXPECT_EQ(1u, util.closed[0]); |
| 86 | + |
| 87 | + util.notifyMessage("File transfer", "another request", { "accept" }, kHiddenHints, 10000); |
| 88 | + ASSERT_EQ(2, util.calls.size()); |
| 89 | + EXPECT_EQ(0u, util.calls[1].replacesId); |
| 90 | +} |
| 91 | + |
| 92 | +TEST(NoticeUtilTest, ActionFromAnotherNotificationIsIgnored) |
| 93 | +{ |
| 94 | + FakeNoticeUtil util; |
| 95 | + QSignalSpy spy(&util, &NoticeUtil::ActionInvoked); |
| 96 | + |
| 97 | + util.notifyMessage("File transfer", "request", { "accept" }, kHiddenHints, 10000); |
| 98 | + ASSERT_TRUE(QMetaObject::invokeMethod(&util, "onActionTriggered", |
| 99 | + Q_ARG(uint, 99), Q_ARG(QString, QStringLiteral("reject")))); |
| 100 | + EXPECT_EQ(0, spy.count()); |
| 101 | + |
| 102 | + // The notification is still on screen, so progress may still replace it. |
| 103 | + util.notifyMessage("File transfer", "progress", { "cancel" }, kHiddenHints, 15000, true); |
| 104 | + ASSERT_EQ(2, util.calls.size()); |
| 105 | + EXPECT_EQ(1u, util.calls[1].replacesId); |
| 106 | +} |
0 commit comments