File indexing completed on 2024-12-29 04:49:08

0001 /*
0002     SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "synctest.h"
0008 
0009 #include "maildirsettings.h" // generated
0010 
0011 #include <QDBusInterface>
0012 
0013 #include <QDebug>
0014 
0015 #include <Akonadi/AgentInstance>
0016 #include <Akonadi/AgentInstanceCreateJob>
0017 #include <Akonadi/AgentManager>
0018 #include <Akonadi/Control>
0019 #include <Akonadi/ServerManager>
0020 #include <QSignalSpy>
0021 #include <akonadi/qtest_akonadi.h>
0022 
0023 #define TIMES 100 // How many times to sync.
0024 #define TIMEOUT 10 // How many seconds to wait before declaring the resource dead.
0025 
0026 using namespace Akonadi;
0027 
0028 void SyncTest::initTestCase()
0029 {
0030     AkonadiTest::checkTestIsIsolated();
0031     AgentType maildirType = AgentManager::self()->type(QStringLiteral("akonadi_maildir_resource"));
0032     auto agentCreateJob = new AgentInstanceCreateJob(maildirType);
0033     QVERIFY(agentCreateJob->exec());
0034     mMaildirIdentifier = agentCreateJob->instance().identifier();
0035 
0036     QString service = QLatin1StringView("org.freedesktop.Akonadi.Resource.") + mMaildirIdentifier;
0037     if (Akonadi::ServerManager::hasInstanceIdentifier()) {
0038         service += QLatin1Char('.') + Akonadi::ServerManager::instanceIdentifier();
0039     }
0040 
0041     OrgKdeAkonadiMaildirSettingsInterface interface(service, QStringLiteral("/"), QDBusConnection::sessionBus());
0042     QVERIFY(interface.isValid());
0043     const QString mailPath = QFINDTESTDATA("maildir");
0044     QVERIFY(!mailPath.isEmpty());
0045     QVERIFY(QFile::exists(mailPath));
0046     interface.setPath(mailPath);
0047 }
0048 
0049 void SyncTest::testSync()
0050 {
0051     AgentInstance instance = AgentManager::self()->instance(mMaildirIdentifier);
0052     QVERIFY(instance.isValid());
0053 
0054     for (int i = 0; i < TIMES; ++i) {
0055         QDBusInterface interface(Akonadi::ServerManager::agentServiceName(Akonadi::ServerManager::Resource, mMaildirIdentifier),
0056                                  QStringLiteral("/"),
0057                                  QStringLiteral("org.freedesktop.Akonadi.Resource"),
0058                                  QDBusConnection::sessionBus(),
0059                                  this);
0060         QVERIFY(interface.isValid());
0061         QElapsedTimer t;
0062         t.start();
0063         instance.synchronize();
0064         QSignalSpy spy(&interface, SIGNAL(synchronized()));
0065         QVERIFY(spy.isValid());
0066         QVERIFY(spy.wait(TIMEOUT * 1000));
0067         qDebug() << "Sync attempt" << i << "in" << t.elapsed() << "ms.";
0068     }
0069 }
0070 
0071 QTEST_AKONADIMAIN(SyncTest)
0072 
0073 #include "moc_synctest.cpp"