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
|
/*
SPDX-FileCopyrightText: 2017 David Edmundson <davidedmundson@kde.org>
SPDX-FileCopyrightText: 2020 Kai Uwe Broulik <kde@broulik.de>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include <QDebug>
#include <QObject>
#include <QtTest>
#include "notification.h"
#include "notificationsmodel.h"
#include "server.h"
namespace NotificationManager
{
class NotificationTest : public QObject
{
Q_OBJECT
public:
NotificationTest()
{
}
private Q_SLOTS:
void parse_data();
void parse();
void compressNotificationRemoval();
};
void NotificationTest::parse_data()
{
QTest::addColumn<QString>("messageIn");
QTest::addColumn<QString>("expectedOut");
// clang-format off
QTest::newRow("basic no HTML") << "I am a notification" << "I am a notification";
QTest::newRow("whitespace") << " I am a notification " << "I am a notification";
QTest::newRow("basic html") << "I am <b>the</b> notification" << "I am <b>the</b> notification";
QTest::newRow("nested html") << "I am <i><b>the</b></i> notification" << "I am <i><b>the</b></i> notification";
QTest::newRow("no extra tags") << "I am <blink>the</blink> notification" << "I am the notification";
QTest::newRow("no extra attrs") << "I am <b style=\"font-weight:20\">the</b> notification" << "I am <b>the</b> notification";
QTest::newRow("newlines") << "I am\nthe\nnotification" << "I am<br/>the<br/>notification";
QTest::newRow("multinewlines") << "I am\n\nthe\n\n\nnotification" << "I am<br/>the<br/>notification";
QTest::newRow("amp") << "me&you" << "me&you";
QTest::newRow("double escape") << "foo & <bar>" << "foo & <bar>";
QTest::newRow("quotes") << "'foo'" << "'foo'";//as label can't handle this normally valid entity
QTest::newRow("image normal") << "This is <img src=\"file:://foo/boo.png\" alt=\"cheese\"/> and more text" << "This is <img src=\"file:://foo/boo.png\" alt=\"cheese\"/> and more text";
//this input is technically wrong, so the output is also wrong, but QTextHtmlParser does the "right" thing
QTest::newRow("image normal no close") << "This is <img src=\"file:://foo/boo.png\" alt=\"cheese\"> and more text" << "This is <img src=\"file:://foo/boo.png\" alt=\"cheese\"> and more text</img>";
QTest::newRow("image remote URL") << "This is <img src=\"http://foo.com/boo.png\" alt=\"cheese\" /> and more text" << "This is <img alt=\"cheese\"/> and more text";
//more bad formatted options. To some extent actual output doesn't matter. Garbage in, garbage out.
//the important thing is that it doesn't contain anything that could be parsed as the remote URL
QTest::newRow("image remote URL no close") << "This is <img src=\"http://foo.com/boo.png>\" alt=\"cheese\"> and more text" << "This is <img alt=\"cheese\"> and more text</img>";
QTest::newRow("image remote URL double open") << "This is <<img src=\"http://foo.com/boo.png>\" and more text" << "This is ";
QTest::newRow("image remote URL no entity close") << "This is <img src=\"http://foo.com/boo.png\" and more text" << "This is ";
QTest::newRow("image remote URL space in element name") << "This is < img src=\"http://foo.com/boo.png\" alt=\"cheese\" /> and more text" << "This is ";
QTest::newRow("link") << "This is a link <a href=\"http://foo.com/boo\"/> and more text" << "This is a link <a href=\"http://foo.com/boo\"/> and more text";
// clang-format on
}
void NotificationTest::parse()
{
QFETCH(QString, messageIn);
QFETCH(QString, expectedOut);
NotificationManager::Notification notification;
notification.setBody(messageIn);
expectedOut = "<?xml version=\"1.0\"?><html>" + expectedOut + "</html>\n";
QCOMPARE(notification.body(), expectedOut);
}
void NotificationTest::compressNotificationRemoval()
{
const int notificationCount = 10;
const int gapId = 4;
auto model = NotificationsModel::createNotificationsModel();
QSignalSpy rowsRemovedSpy(model.data(), &QAbstractItemModel::rowsRemoved);
QVERIFY(rowsRemovedSpy.isValid());
for (uint i = 1; i <= notificationCount; ++i) {
Notification notification{i};
notification.setSummary(QStringLiteral("Notification %1").arg(i));
model->onNotificationAdded(notification);
}
QCOMPARE(model->rowCount(), notificationCount);
for (uint i = 1; i <= notificationCount; ++i) {
// Leave a gap inbetween
if (i != gapId) {
model->onNotificationRemoved(i, Server::CloseReason::Revoked);
}
}
// We should have two ranges that we ended up removing
QTRY_COMPARE(rowsRemovedSpy.count(), 2);
// The fact that it emits row removal in reverse order is an implementation detail
// We only really care that the number of rows emitted matches our expectation
int removedCount = 0;
for (const auto &removedEmission : rowsRemovedSpy) {
const int from = removedEmission.at(1).toInt();
const int to = removedEmission.at(2).toInt();
removedCount += (to - from) + 1;
}
QCOMPARE(removedCount, notificationCount - 1);
QCOMPARE(model->rowCount(), 1);
rowsRemovedSpy.clear();
// Removing a random non-existing notification should noop
model->onNotificationRemoved(3, Server::CloseReason::Revoked);
QTRY_COMPARE(rowsRemovedSpy.count(), 0);
QCOMPARE(model->rowCount(), 1);
rowsRemovedSpy.clear();
// Now remove the last one
model->onNotificationRemoved(gapId, Server::CloseReason::Revoked);
QTRY_COMPARE(rowsRemovedSpy.count(), 1);
QCOMPARE(rowsRemovedSpy.at(0).at(1), 0); // from
QCOMPARE(rowsRemovedSpy.at(0).at(2), 0); // to
QCOMPARE(model->rowCount(), 0);
}
} // namespace NotificationManager
QTEST_GUILESS_MAIN(NotificationManager::NotificationTest)
#include "notifications_test.moc"
|