File indexing completed on 2024-11-17 04:45:03

0001 /*
0002    SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003    SPDX-FileContributor: Kevin Ottens <kevin@kdab.com>
0004 
0005    SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "imaptestbase.h"
0009 
0010 #include "expungecollectiontask.h"
0011 #include <QTest>
0012 class TestExpungeCollectionTask : public ImapTestBase
0013 {
0014     Q_OBJECT
0015 
0016 private Q_SLOTS:
0017     void shouldDeleteMailBox_data()
0018     {
0019         QTest::addColumn<Akonadi::Collection>("collection");
0020         QTest::addColumn<QList<QByteArray>>("scenario");
0021         QTest::addColumn<QStringList>("callNames");
0022 
0023         Akonadi::Collection collection;
0024         QList<QByteArray> scenario;
0025         QStringList callNames;
0026 
0027         collection = createCollectionChain(QStringLiteral("/INBOX/Foo"));
0028 
0029         scenario.clear();
0030         scenario << defaultPoolConnectionScenario() << "C: A000003 SELECT \"INBOX/Foo\""
0031                  << "S: A000003 OK select done"
0032                  << "C: A000004 EXPUNGE"
0033                  << "S: A000004 OK expunge done";
0034 
0035         callNames.clear();
0036         callNames << QStringLiteral("taskDone");
0037 
0038         QTest::newRow("normal case") << collection << scenario << callNames;
0039 
0040         // We keep the same collection
0041 
0042         scenario.clear();
0043         scenario << defaultPoolConnectionScenario() << "C: A000003 SELECT \"INBOX/Foo\""
0044                  << "S: A000003 NO select failed";
0045 
0046         callNames.clear();
0047         callNames << QStringLiteral("cancelTask");
0048 
0049         QTest::newRow("select failed") << collection << scenario << callNames;
0050 
0051         // We keep the same collection
0052 
0053         scenario.clear();
0054         scenario << defaultPoolConnectionScenario() << "C: A000003 SELECT \"INBOX/Foo\""
0055                  << "S: A000003 OK select done"
0056                  << "C: A000004 EXPUNGE"
0057                  << "S: A000004 NO expunge failed";
0058 
0059         callNames.clear();
0060         callNames << QStringLiteral("cancelTask");
0061 
0062         QTest::newRow("expunge failed") << collection << scenario << callNames;
0063 
0064         // We keep the same collection
0065 
0066         scenario.clear();
0067         scenario << defaultPoolConnectionScenario() << "C: A000003 SELECT \"INBOX/Foo\""
0068                  << "S: A000003 OK [READ-ONLY] select done";
0069 
0070         callNames.clear();
0071         callNames << QStringLiteral("taskDone");
0072 
0073         QTest::newRow("read-only mailbox") << collection << scenario << callNames;
0074     }
0075 
0076     void shouldDeleteMailBox()
0077     {
0078         QFETCH(Akonadi::Collection, collection);
0079         QFETCH(QList<QByteArray>, scenario);
0080         QFETCH(QStringList, callNames);
0081 
0082         FakeServer server;
0083         server.setScenario(scenario);
0084         server.startAndWait();
0085 
0086         SessionPool pool(1);
0087 
0088         pool.setPasswordRequester(createDefaultRequester());
0089         QVERIFY(pool.connect(createDefaultAccount()));
0090         QVERIFY(waitForSignal(&pool, SIGNAL(connectDone(int, QString))));
0091 
0092         DummyResourceState::Ptr state = DummyResourceState::Ptr(new DummyResourceState);
0093         state->setCollection(collection);
0094         auto task = new ExpungeCollectionTask(state);
0095         task->start(&pool);
0096 
0097         QTRY_COMPARE(state->calls().count(), callNames.size());
0098         for (int i = 0; i < callNames.size(); i++) {
0099             QString command = QString::fromUtf8(state->calls().at(i).first);
0100             QVariant parameter = state->calls().at(i).second;
0101 
0102             if (command == QLatin1StringView("cancelTask") && callNames[i] != QLatin1StringView("cancelTask")) {
0103                 qDebug() << "Got a cancel:" << parameter.toString();
0104             }
0105 
0106             QCOMPARE(command, callNames[i]);
0107 
0108             if (command == QLatin1StringView("cancelTask")) {
0109                 QVERIFY(!parameter.toString().isEmpty());
0110             }
0111         }
0112 
0113         QVERIFY(server.isAllScenarioDone());
0114 
0115         server.quit();
0116     }
0117 };
0118 
0119 QTEST_GUILESS_MAIN(TestExpungeCollectionTask)
0120 
0121 #include "testexpungecollectiontask.moc"