summaryrefslogtreecommitdiffstats
path: root/framework/src/tests/maillistmodeltest.cpp
blob: a18d0d2f4c9d58c65d63904c51c81c66713601d6 (plain)
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
#include <QTest>
#include <QDebug>
#include <QStandardItemModel>
#include <sink/test.h>
#include <sink/store.h>
#include <sink/resourcecontrol.h>
#include "maillistmodel.h"
#include "mailtemplates.h"

class MaillistModelTest : public QObject
{
    Q_OBJECT
private slots:

    void initTestCase()
    {
        Sink::Test::initTest();
    }

    void testMailListModel()
    {
        Sink::ApplicationDomain::DummyResource::create("account1");

        using namespace Sink::ApplicationDomain;
        auto account = ApplicationDomainType::createEntity<SinkAccount>();
        Sink::Store::create(account).exec().waitForFinished();

        auto resource = Sink::ApplicationDomain::DummyResource::create(account.identifier());
        Sink::Store::create(resource).exec().waitForFinished();

        auto folder1 = ApplicationDomainType::createEntity<Folder>(resource.identifier());
        Sink::Store::create(folder1).exec().waitForFinished();

        auto folder2 = ApplicationDomainType::createEntity<Folder>(resource.identifier());
        Sink::Store::create(folder2).exec().waitForFinished();


        auto mail1 = ApplicationDomainType::createEntity<Mail>(resource.identifier());
        mail1.setFolder(folder1);
        KMime::Types::Mailbox from;
        from.fromUnicodeString("from@example.org");
        auto message = MailTemplates::createMessage({}, {"foo@test.com"}, {}, {}, from, "Subject", "Body", false, {}, {}, {});
        mail1.setMimeMessage(message->encodedContent(true));
        Sink::Store::create(mail1).exec().waitForFinished();

        auto mail2 = ApplicationDomainType::createEntity<Mail>(resource.identifier());
        mail2.setFolder(folder2);
        Sink::Store::create(mail2).exec().waitForFinished();

        Sink::ResourceControl::flushMessageQueue(resource.identifier()).exec().waitForFinished();

        {
            MailListModel model;
            model.setParentFolder(QVariant::fromValue(Folder::Ptr::create(folder1)));
            QTRY_COMPARE(model.rowCount({}), 1);

            {
                auto idx = model.index(0, 0, {});
                auto mail = idx.data(MailListModel::DomainObject).value<Mail::Ptr>();
                QVERIFY(mail);
                QVERIFY(!mail->getSubject().isEmpty());
            }
        }
        {
            MailListModel model;
            model.setMail(QVariant::fromValue(Mail::Ptr::create(mail1)));
            QTRY_COMPARE(model.rowCount({}), 1);

            {
                auto idx = model.index(0, 0, {});
                auto mail = idx.data(MailListModel::DomainObject).value<Mail::Ptr>();
                QVERIFY(mail);
                QVERIFY(!mail->getSubject().isEmpty());
                QVERIFY(mail->getFullPayloadAvailable());
            }
        }
    }
};

QTEST_MAIN(MaillistModelTest)
#include "maillistmodeltest.moc"