File indexing completed on 2024-11-24 04:43:05

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "mailsenderjobtest.h"
0008 #include "mailsenderjob.h"
0009 
0010 #include <KContacts/Addressee>
0011 
0012 #include <QSignalSpy>
0013 #include <QTest>
0014 
0015 MailSenderJobTest::MailSenderJobTest() = default;
0016 
0017 void MailSenderJobTest::shouldNotSendSignalWhenWeDontSelectItem()
0018 {
0019     Akonadi::Item::List lst;
0020     KABMailSender::MailSenderJob mailsender(lst);
0021     QSignalSpy spy(&mailsender, &KABMailSender::MailSenderJob::sendMails);
0022     mailsender.start();
0023     QCOMPARE(spy.count(), 0);
0024 }
0025 
0026 void MailSenderJobTest::shouldNotSendSignalWhenNoValidAddressItem()
0027 {
0028     Akonadi::Item::List lst;
0029     Akonadi::Item item;
0030     lst << item;
0031     KABMailSender::MailSenderJob mailsender(lst);
0032     QSignalSpy spy(&mailsender, &KABMailSender::MailSenderJob::sendMails);
0033     mailsender.start();
0034     QCOMPARE(spy.count(), 0);
0035 }
0036 
0037 void MailSenderJobTest::shouldNotSendSignalWhenNoEmails()
0038 {
0039     Akonadi::Item::List lst;
0040     Akonadi::Item item;
0041     KContacts::Addressee address;
0042     address.setName(QStringLiteral("foo1"));
0043     item.setPayload<KContacts::Addressee>(address);
0044     lst << item;
0045     KABMailSender::MailSenderJob mailsender(lst);
0046     QSignalSpy spy(&mailsender, &KABMailSender::MailSenderJob::sendMails);
0047     mailsender.start();
0048     QCOMPARE(spy.count(), 0);
0049 }
0050 
0051 void MailSenderJobTest::shouldSendSignalWhenOneEmail()
0052 {
0053     Akonadi::Item::List lst;
0054     Akonadi::Item item;
0055     KContacts::Addressee address;
0056     address.setName(QStringLiteral("foo1"));
0057     KContacts::Email email(QStringLiteral("foo@kde.org"));
0058     email.setPreferred(true);
0059     address.addEmail(email);
0060     item.setPayload<KContacts::Addressee>(address);
0061     lst << item;
0062     KABMailSender::MailSenderJob mailsender(lst);
0063     QSignalSpy spy(&mailsender, &KABMailSender::MailSenderJob::sendMails);
0064     mailsender.start();
0065     QCOMPARE(spy.count(), 1);
0066     const QStringList resultLst = spy.at(0).at(0).toStringList();
0067     QCOMPARE(resultLst.count(), 1);
0068 }
0069 
0070 void MailSenderJobTest::shouldNotSendTwiceEmails()
0071 {
0072     Akonadi::Item::List lst;
0073     Akonadi::Item item;
0074     KContacts::Addressee address;
0075     address.setName(QStringLiteral("foo1"));
0076     KContacts::Email email(QStringLiteral("foo@kde.org"));
0077     email.setPreferred(true);
0078     address.addEmail(email);
0079     item.setPayload<KContacts::Addressee>(address);
0080     lst << item << item;
0081     KABMailSender::MailSenderJob mailsender(lst);
0082     QSignalSpy spy(&mailsender, &KABMailSender::MailSenderJob::sendMails);
0083     mailsender.start();
0084     const QStringList resultLst = spy.at(0).at(0).toStringList();
0085     QCOMPARE(resultLst.count(), 1);
0086 }
0087 
0088 void MailSenderJobTest::shouldNotAddInvalidEmail()
0089 {
0090     Akonadi::Item::List lst;
0091     Akonadi::Item item;
0092     KContacts::Addressee address;
0093     address.setName(QStringLiteral("foo1"));
0094     // Invalid email
0095     KContacts::Email email(QStringLiteral("foo2"));
0096     email.setPreferred(true);
0097     address.addEmail(email);
0098     item.setPayload<KContacts::Addressee>(address);
0099     lst << item;
0100     KABMailSender::MailSenderJob mailsender(lst);
0101     QSignalSpy spy(&mailsender, &KABMailSender::MailSenderJob::sendMails);
0102     mailsender.start();
0103     QCOMPARE(spy.count(), 0);
0104 }
0105 
0106 void MailSenderJobTest::shouldEmitSignalIfThereIsAValidEmail()
0107 {
0108     Akonadi::Item::List lst;
0109     Akonadi::Item item;
0110     KContacts::Addressee address;
0111     address.setName(QStringLiteral("foo1"));
0112     // Invalid email
0113     KContacts::Email email(QStringLiteral("foo2"));
0114     email.setPreferred(true);
0115     address.addEmail(email);
0116     item.setPayload<KContacts::Addressee>(address);
0117     lst << item;
0118 
0119     Akonadi::Item item2;
0120     KContacts::Addressee address2;
0121     address2.setName(QStringLiteral("foo2"));
0122     KContacts::Email email2(QStringLiteral("foo2@kde.org"));
0123     email2.setPreferred(true);
0124     address2.addEmail(email);
0125     item2.setPayload<KContacts::Addressee>(address2);
0126     lst << item2;
0127 
0128     Akonadi::Item item3;
0129     KContacts::Addressee address3;
0130     address3.setName(QStringLiteral("foo3"));
0131     KContacts::Email email3(QStringLiteral("foo3@"));
0132     email3.setPreferred(true);
0133     address3.addEmail(email);
0134     item3.setPayload<KContacts::Addressee>(address3);
0135     lst << item3;
0136 
0137     Akonadi::Item item4;
0138     KContacts::Addressee address4;
0139     address4.setName(QStringLiteral("foo4"));
0140     KContacts::Email email4(QStringLiteral("foo4@kde.org"));
0141     email4.setPreferred(true);
0142     address4.addEmail(email);
0143     item4.setPayload<KContacts::Addressee>(address4);
0144     lst << item4;
0145 
0146     KABMailSender::MailSenderJob mailsender(lst);
0147     QSignalSpy spy(&mailsender, &KABMailSender::MailSenderJob::sendMails);
0148     mailsender.start();
0149     QCOMPARE(spy.count(), 1);
0150     const QStringList resultLst = spy.at(0).at(0).toStringList();
0151     QCOMPARE(resultLst.count(), 2);
0152 }
0153 
0154 // TODO Create unittest for ContactGroup too
0155 
0156 QTEST_MAIN(MailSenderJobTest)
0157 
0158 #include "moc_mailsenderjobtest.cpp"