File indexing completed on 2024-06-16 05:01:56

0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include <QtTest>
0024 #include "test_Imap_Tasks_CreateMailbox.h"
0025 #include "Utils/LibMailboxSync.h"
0026 #include "Common/MetaTypes.h"
0027 #include "Streams/FakeSocket.h"
0028 #include "Imap/Model/MemoryCache.h"
0029 #include "Imap/Model/Model.h"
0030 
0031 void ImapModelCreateMailboxTest::init()
0032 {
0033     auto cache = std::make_shared<Imap::Mailbox::MemoryCache>();
0034     factory = new Streams::FakeSocketFactory(Imap::CONN_STATE_AUTHENTICATED);
0035     Imap::Mailbox::TaskFactoryPtr taskFactory(new Imap::Mailbox::TestingTaskFactory());
0036     taskFactoryUnsafe = static_cast<Imap::Mailbox::TestingTaskFactory*>(taskFactory.get());
0037     taskFactoryUnsafe->fakeOpenConnectionTask = true;
0038     taskFactoryUnsafe->fakeListChildMailboxes = true;
0039     model = new Imap::Mailbox::Model(this, cache, Imap::Mailbox::SocketFactoryPtr(factory), std::move(taskFactory));
0040     LibMailboxSync::setModelNetworkPolicy(model, Imap::Mailbox::NETWORK_ONLINE);
0041     createdSpy = new QSignalSpy(model, SIGNAL(mailboxCreationSucceded(QString)));
0042     failedSpy = new QSignalSpy(model, SIGNAL(mailboxCreationFailed(QString,QString)));
0043     errorSpy = new QSignalSpy(model, SIGNAL(imapError(QString)));
0044 }
0045 
0046 void ImapModelCreateMailboxTest::cleanup()
0047 {
0048     delete model;
0049     model = 0;
0050     taskFactoryUnsafe = 0;
0051     delete createdSpy;
0052     createdSpy = 0;
0053     delete failedSpy;
0054     failedSpy = 0;
0055     delete errorSpy;
0056     errorSpy = 0;
0057     QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
0058 }
0059 
0060 void ImapModelCreateMailboxTest::initTestCase()
0061 {
0062     Common::registerMetaTypes();
0063     model = 0;
0064     createdSpy = 0;
0065     failedSpy = 0;
0066     errorSpy = 0;
0067 }
0068 
0069 #define SOCK static_cast<Streams::FakeSocket*>( factory->lastSocket() )
0070 
0071 void ImapModelCreateMailboxTest::_initWithOne()
0072 {
0073     // Init with one example mailbox
0074     taskFactoryUnsafe->fakeListChildMailboxesMap[ QLatin1String("") ] = QStringList() << QStringLiteral("a");
0075     model->rowCount( QModelIndex() );
0076     QCoreApplication::processEvents();
0077     QCoreApplication::processEvents();
0078     QCOMPARE( model->rowCount( QModelIndex() ), 2 );
0079     QModelIndex idxA = model->index( 1, 0, QModelIndex() );
0080     QCOMPARE( model->data( idxA, Qt::DisplayRole ), QVariant(QLatin1String("a")) );
0081     QCoreApplication::processEvents();
0082     QCoreApplication::processEvents();
0083     QCOMPARE( SOCK->writtenStuff(), QByteArray() );
0084     QVERIFY( errorSpy->isEmpty() );
0085 }
0086 
0087 void ImapModelCreateMailboxTest::_initWithEmpty()
0088 {
0089     // Init with empty set of mailboxes
0090     model->rowCount( QModelIndex() );
0091     QCoreApplication::processEvents();
0092     QCoreApplication::processEvents();
0093     QCOMPARE( model->rowCount( QModelIndex() ), 1 );
0094     QCoreApplication::processEvents();
0095     QCoreApplication::processEvents();
0096     QCOMPARE( SOCK->writtenStuff(), QByteArray() );
0097     QVERIFY( errorSpy->isEmpty() );
0098 }
0099 
0100 void ImapModelCreateMailboxTest::testCreateOneMore()
0101 {
0102     _initWithOne();
0103 
0104     // Now test the actual creating process
0105     model->createMailbox( QStringLiteral("ahoj") );
0106     QCoreApplication::processEvents();
0107     QCoreApplication::processEvents();
0108     QCOMPARE( SOCK->writtenStuff(), QByteArray("y0 CREATE ahoj\r\n") );
0109     SOCK->fakeReading( QByteArray("y0 OK created\r\n") );
0110     QCoreApplication::processEvents();
0111     QCoreApplication::processEvents();
0112     QCoreApplication::processEvents();
0113     QCOMPARE( SOCK->writtenStuff(), QByteArray("y1 LIST \"\" ahoj\r\n") );
0114     SOCK->fakeReading( QByteArray("* LIST (\\HasNoChildren) \"^\" \"ahoj\"\r\n"
0115             "y1 OK list\r\n") );
0116     QCoreApplication::processEvents();
0117     QCoreApplication::processEvents();
0118     QCOMPARE( model->rowCount( QModelIndex() ), 3 );
0119     QCoreApplication::processEvents();
0120     QCOMPARE( SOCK->writtenStuff(), QByteArray() );
0121     QCOMPARE( createdSpy->size(), 1 );
0122     QVERIFY( failedSpy->isEmpty() );
0123     QVERIFY( errorSpy->isEmpty() );
0124 }
0125 
0126 void ImapModelCreateMailboxTest::testCreateEmpty()
0127 {
0128     _initWithEmpty();
0129 
0130     // Now test the actual creating process
0131     model->createMailbox( QStringLiteral("ahoj") );
0132     QCoreApplication::processEvents();
0133     QCoreApplication::processEvents();
0134     QCOMPARE( SOCK->writtenStuff(), QByteArray("y0 CREATE ahoj\r\n") );
0135     SOCK->fakeReading( QByteArray("y0 OK created\r\n") );
0136     QCoreApplication::processEvents();
0137     QCoreApplication::processEvents();
0138     QCoreApplication::processEvents();
0139     QCOMPARE( SOCK->writtenStuff(), QByteArray("y1 LIST \"\" ahoj\r\n") );
0140     SOCK->fakeReading( QByteArray("* LIST (\\HasNoChildren) \"^\" \"ahoj\"\r\n"
0141             "y1 OK list\r\n") );
0142     QCoreApplication::processEvents();
0143     QCoreApplication::processEvents();
0144     QCOMPARE( model->rowCount( QModelIndex() ), 2 );
0145     QCoreApplication::processEvents();
0146     QCOMPARE( SOCK->writtenStuff(), QByteArray() );
0147     QCOMPARE( createdSpy->size(), 1 );
0148     QVERIFY( failedSpy->isEmpty() );
0149     QVERIFY( errorSpy->isEmpty() );
0150 }
0151 
0152 void ImapModelCreateMailboxTest::testCreateFail()
0153 {
0154     _initWithEmpty();
0155 
0156     // Test failure of the CREATE command
0157     model->createMailbox( QStringLiteral("ahoj") );
0158     QCoreApplication::processEvents();
0159     QCoreApplication::processEvents();
0160     QCOMPARE( SOCK->writtenStuff(), QByteArray("y0 CREATE ahoj\r\n") );
0161     SOCK->fakeReading( QByteArray("y0 NO muhehe\r\n") );
0162     QCoreApplication::processEvents();
0163 
0164     QCOMPARE( model->rowCount( QModelIndex() ), 1 );
0165     QCoreApplication::processEvents();
0166     QCoreApplication::processEvents();
0167     QCOMPARE( SOCK->writtenStuff(), QByteArray() );
0168     QCOMPARE( failedSpy->size(), 1 );
0169     QVERIFY( createdSpy->isEmpty() );
0170     QVERIFY( errorSpy->isEmpty() );
0171 }
0172 
0173 QTEST_GUILESS_MAIN( ImapModelCreateMailboxTest )