File indexing completed on 2025-01-05 04:58:20

0001 /*
0002   SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "blacklistbalooemaillisttest.h"
0009 #include "../blacklistbalooemaillist.h"
0010 #include <QTest>
0011 
0012 BlackListBalooEmailListTest::BlackListBalooEmailListTest(QObject *parent)
0013     : QObject(parent)
0014 {
0015 }
0016 
0017 BlackListBalooEmailListTest::~BlackListBalooEmailListTest() = default;
0018 
0019 void BlackListBalooEmailListTest::shouldHaveDefaultValue()
0020 {
0021     PimCommon::BlackListBalooEmailList blackList;
0022     QVERIFY(blackList.count() == 0);
0023     QVERIFY(blackList.isSortingEnabled());
0024     QCOMPARE(blackList.selectionMode(), QAbstractItemView::ExtendedSelection);
0025 }
0026 
0027 void BlackListBalooEmailListTest::shouldFillListEmail()
0028 {
0029     PimCommon::BlackListBalooEmailList blackList;
0030     QCOMPARE(blackList.setEmailFound(QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bla@kde.org") << QStringLiteral("bli@kde.org")), 3);
0031     QCOMPARE(blackList.count(), 3);
0032     for (int i = 0; i < blackList.count(); ++i) {
0033         QListWidgetItem *item = blackList.item(i);
0034         QVERIFY(item);
0035         auto blackListItem = static_cast<PimCommon::BlackListBalooEmailListItem *>(item);
0036         QVERIFY(!blackListItem->initializeStatus());
0037         QCOMPARE(blackListItem->checkState(), Qt::Unchecked);
0038     }
0039     QVERIFY(blackList.blackListItemChanged().isEmpty());
0040 }
0041 
0042 void BlackListBalooEmailListTest::shouldFillListWithAlreadyBlackListedEmail()
0043 {
0044     PimCommon::BlackListBalooEmailList blackList;
0045     const QStringList emails = QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bla@kde.org") << QStringLiteral("bli@kde.org");
0046     blackList.setEmailBlackList(emails);
0047     QCOMPARE(blackList.setEmailFound(emails), 3);
0048 
0049     QCOMPARE(blackList.count(), 3);
0050     for (int i = 0; i < blackList.count(); ++i) {
0051         QListWidgetItem *item = blackList.item(i);
0052         QVERIFY(item);
0053         auto blackListItem = static_cast<PimCommon::BlackListBalooEmailListItem *>(item);
0054         QVERIFY(blackListItem->initializeStatus());
0055         QCOMPARE(blackListItem->checkState(), Qt::Checked);
0056     }
0057 
0058     QVERIFY(blackList.blackListItemChanged().isEmpty());
0059 }
0060 
0061 void BlackListBalooEmailListTest::shouldReturnChangedItems()
0062 {
0063     PimCommon::BlackListBalooEmailList blackList;
0064     const QStringList emails = QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bla@kde.org") << QStringLiteral("bli@kde.org");
0065     blackList.setEmailBlackList(emails);
0066     QCOMPARE(blackList.setEmailFound(emails), 3);
0067     QListWidgetItem *item = blackList.item(1);
0068     QVERIFY(item);
0069     item->setCheckState(Qt::Unchecked);
0070     QVERIFY(!blackList.blackListItemChanged().isEmpty());
0071 }
0072 
0073 void BlackListBalooEmailListTest::shouldNotAddDuplicateEmails()
0074 {
0075     PimCommon::BlackListBalooEmailList blackList;
0076     const QStringList emails = QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bli@kde.org") << QStringLiteral("bli@kde.org");
0077     blackList.setEmailBlackList(emails);
0078     QCOMPARE(blackList.setEmailFound(emails), 2);
0079 
0080     QCOMPARE(blackList.count(), 2);
0081 }
0082 
0083 void BlackListBalooEmailListTest::shouldExcludeDomain()
0084 {
0085     PimCommon::BlackListBalooEmailList blackList;
0086     blackList.setExcludeDomains(QStringList() << QStringLiteral("kde.org") << QStringLiteral("toto.fr"));
0087     QStringList emails = QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("bli@fr.fr") << QStringLiteral("bli@toto.fr");
0088     blackList.setEmailBlackList(emails);
0089     QCOMPARE(blackList.setEmailFound(emails), 1);
0090 
0091     QCOMPARE(blackList.count(), 1);
0092 
0093     blackList.setExcludeDomains(QStringList() << QStringLiteral("kde.org") << QStringLiteral("toto.fr"));
0094     emails = QStringList() << QStringLiteral("<foo@kde.org>") << QStringLiteral("bli@fr.fr") << QStringLiteral("bli@toto.fr");
0095     blackList.setEmailBlackList(emails);
0096     QCOMPARE(blackList.setEmailFound(emails), 1);
0097     QCOMPARE(blackList.count(), 1);
0098 }
0099 
0100 void BlackListBalooEmailListTest::shouldAvoidSameEmailWithDifferentCase()
0101 {
0102     PimCommon::BlackListBalooEmailList blackList;
0103     const QStringList emails = QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("Foo@kde.org") << QStringLiteral("foo@kde.ORG");
0104     QCOMPARE(blackList.setEmailFound(emails), 1);
0105 
0106     QCOMPARE(blackList.count(), 1);
0107 }
0108 
0109 void BlackListBalooEmailListTest::shouldAvoidSameEmailWithDisplayNameOrNot()
0110 {
0111     PimCommon::BlackListBalooEmailList blackList;
0112     const QStringList emails = QStringList() << QStringLiteral("foo@kde.org") << QStringLiteral("Bla Blo <Foo@kde.org>");
0113     QCOMPARE(blackList.setEmailFound(emails), 1);
0114 
0115     QCOMPARE(blackList.count(), 1);
0116 }
0117 
0118 QTEST_MAIN(BlackListBalooEmailListTest)
0119 
0120 #include "moc_blacklistbalooemaillisttest.cpp"