1 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
2
3 This file is part of the Trojita Qt IMAP e-mail client,
4 http://trojita.flaska.net/
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License or (at your option) version 3 or any later version
10 accepted by the membership of KDE e.V. (or its successor approved
11 by the membership of KDE e.V.), which shall act as a proxy
12 defined in Section 14 of version 3 of the license.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include <QtTest>
24 #include "test_Imap_Tasks_CreateMailbox.h"
25 #include "Utils/LibMailboxSync.h"
26 #include "Common/MetaTypes.h"
27 #include "Streams/FakeSocket.h"
28 #include "Imap/Model/MemoryCache.h"
29 #include "Imap/Model/Model.h"
30
init()31 void ImapModelCreateMailboxTest::init()
32 {
33 Imap::Mailbox::AbstractCache* cache = new Imap::Mailbox::MemoryCache(this);
34 factory = new Streams::FakeSocketFactory(Imap::CONN_STATE_AUTHENTICATED);
35 Imap::Mailbox::TaskFactoryPtr taskFactory(new Imap::Mailbox::TestingTaskFactory());
36 taskFactoryUnsafe = static_cast<Imap::Mailbox::TestingTaskFactory*>(taskFactory.get());
37 taskFactoryUnsafe->fakeOpenConnectionTask = true;
38 taskFactoryUnsafe->fakeListChildMailboxes = true;
39 model = new Imap::Mailbox::Model(this, cache, Imap::Mailbox::SocketFactoryPtr(factory), std::move(taskFactory));
40 LibMailboxSync::setModelNetworkPolicy(model, Imap::Mailbox::NETWORK_ONLINE);
41 createdSpy = new QSignalSpy(model, SIGNAL(mailboxCreationSucceded(QString)));
42 failedSpy = new QSignalSpy(model, SIGNAL(mailboxCreationFailed(QString,QString)));
43 errorSpy = new QSignalSpy(model, SIGNAL(imapError(QString)));
44 }
45
cleanup()46 void ImapModelCreateMailboxTest::cleanup()
47 {
48 delete model;
49 model = 0;
50 taskFactoryUnsafe = 0;
51 delete createdSpy;
52 createdSpy = 0;
53 delete failedSpy;
54 failedSpy = 0;
55 delete errorSpy;
56 errorSpy = 0;
57 QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
58 }
59
initTestCase()60 void ImapModelCreateMailboxTest::initTestCase()
61 {
62 Common::registerMetaTypes();
63 model = 0;
64 createdSpy = 0;
65 failedSpy = 0;
66 errorSpy = 0;
67 }
68
69 #define SOCK static_cast<Streams::FakeSocket*>( factory->lastSocket() )
70
_initWithOne()71 void ImapModelCreateMailboxTest::_initWithOne()
72 {
73 // Init with one example mailbox
74 taskFactoryUnsafe->fakeListChildMailboxesMap[ QLatin1String("") ] = QStringList() << QStringLiteral("a");
75 model->rowCount( QModelIndex() );
76 QCoreApplication::processEvents();
77 QCoreApplication::processEvents();
78 QCOMPARE( model->rowCount( QModelIndex() ), 2 );
79 QModelIndex idxA = model->index( 1, 0, QModelIndex() );
80 QCOMPARE( model->data( idxA, Qt::DisplayRole ), QVariant(QLatin1String("a")) );
81 QCoreApplication::processEvents();
82 QCoreApplication::processEvents();
83 QCOMPARE( SOCK->writtenStuff(), QByteArray() );
84 QVERIFY( errorSpy->isEmpty() );
85 }
86
_initWithEmpty()87 void ImapModelCreateMailboxTest::_initWithEmpty()
88 {
89 // Init with empty set of mailboxes
90 model->rowCount( QModelIndex() );
91 QCoreApplication::processEvents();
92 QCoreApplication::processEvents();
93 QCOMPARE( model->rowCount( QModelIndex() ), 1 );
94 QCoreApplication::processEvents();
95 QCoreApplication::processEvents();
96 QCOMPARE( SOCK->writtenStuff(), QByteArray() );
97 QVERIFY( errorSpy->isEmpty() );
98 }
99
testCreateOneMore()100 void ImapModelCreateMailboxTest::testCreateOneMore()
101 {
102 _initWithOne();
103
104 // Now test the actual creating process
105 model->createMailbox( QStringLiteral("ahoj") );
106 QCoreApplication::processEvents();
107 QCoreApplication::processEvents();
108 QCOMPARE( SOCK->writtenStuff(), QByteArray("y0 CREATE ahoj\r\n") );
109 SOCK->fakeReading( QByteArray("y0 OK created\r\n") );
110 QCoreApplication::processEvents();
111 QCoreApplication::processEvents();
112 QCoreApplication::processEvents();
113 QCOMPARE( SOCK->writtenStuff(), QByteArray("y1 LIST \"\" ahoj\r\n") );
114 SOCK->fakeReading( QByteArray("* LIST (\\HasNoChildren) \"^\" \"ahoj\"\r\n"
115 "y1 OK list\r\n") );
116 QCoreApplication::processEvents();
117 QCoreApplication::processEvents();
118 QCOMPARE( model->rowCount( QModelIndex() ), 3 );
119 QCoreApplication::processEvents();
120 QCOMPARE( SOCK->writtenStuff(), QByteArray() );
121 QCOMPARE( createdSpy->size(), 1 );
122 QVERIFY( failedSpy->isEmpty() );
123 QVERIFY( errorSpy->isEmpty() );
124 }
125
testCreateEmpty()126 void ImapModelCreateMailboxTest::testCreateEmpty()
127 {
128 _initWithEmpty();
129
130 // Now test the actual creating process
131 model->createMailbox( QStringLiteral("ahoj") );
132 QCoreApplication::processEvents();
133 QCoreApplication::processEvents();
134 QCOMPARE( SOCK->writtenStuff(), QByteArray("y0 CREATE ahoj\r\n") );
135 SOCK->fakeReading( QByteArray("y0 OK created\r\n") );
136 QCoreApplication::processEvents();
137 QCoreApplication::processEvents();
138 QCoreApplication::processEvents();
139 QCOMPARE( SOCK->writtenStuff(), QByteArray("y1 LIST \"\" ahoj\r\n") );
140 SOCK->fakeReading( QByteArray("* LIST (\\HasNoChildren) \"^\" \"ahoj\"\r\n"
141 "y1 OK list\r\n") );
142 QCoreApplication::processEvents();
143 QCoreApplication::processEvents();
144 QCOMPARE( model->rowCount( QModelIndex() ), 2 );
145 QCoreApplication::processEvents();
146 QCOMPARE( SOCK->writtenStuff(), QByteArray() );
147 QCOMPARE( createdSpy->size(), 1 );
148 QVERIFY( failedSpy->isEmpty() );
149 QVERIFY( errorSpy->isEmpty() );
150 }
151
testCreateFail()152 void ImapModelCreateMailboxTest::testCreateFail()
153 {
154 _initWithEmpty();
155
156 // Test failure of the CREATE command
157 model->createMailbox( QStringLiteral("ahoj") );
158 QCoreApplication::processEvents();
159 QCoreApplication::processEvents();
160 QCOMPARE( SOCK->writtenStuff(), QByteArray("y0 CREATE ahoj\r\n") );
161 SOCK->fakeReading( QByteArray("y0 NO muhehe\r\n") );
162 QCoreApplication::processEvents();
163
164 QCOMPARE( model->rowCount( QModelIndex() ), 1 );
165 QCoreApplication::processEvents();
166 QCoreApplication::processEvents();
167 QCOMPARE( SOCK->writtenStuff(), QByteArray() );
168 QCOMPARE( failedSpy->size(), 1 );
169 QVERIFY( createdSpy->isEmpty() );
170 QVERIFY( errorSpy->isEmpty() );
171 }
172
173 QTEST_GUILESS_MAIN( ImapModelCreateMailboxTest )
174