File indexing completed on 2024-12-22 04:52:52

0001 /*
0002     SPDX-FileCopyrightText: 2009 Igor Trindade Oliveira <igor_trindade@yahoo.com.br>
0003     based on kdepimlibs/akonadi/tests/benchmarker.cpp wrote by Robert Zwerus <arzie@dds.nl>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "test.h"
0009 
0010 #include <Akonadi/AgentInstanceCreateJob>
0011 #include <Akonadi/CollectionDeleteJob>
0012 #include <Akonadi/CollectionFetchJob>
0013 #include <Akonadi/CollectionFetchScope>
0014 
0015 #include <QDBusConnection>
0016 #include <QDBusInterface>
0017 #include <QDebug>
0018 #include <QTest>
0019 
0020 using namespace Akonadi;
0021 
0022 MakeTest::MakeTest()
0023 {
0024     connect(AgentManager::self(), &AgentManager::instanceRemoved, this, &MakeTest::instanceRemoved);
0025     connect(AgentManager::self(), &AgentManager::instanceStatusChanged, this, &MakeTest::instanceStatusChanged);
0026 }
0027 
0028 void MakeTest::createAgent(const QString &name)
0029 {
0030     const AgentType type = AgentManager::self()->type(name);
0031 
0032     auto job = new AgentInstanceCreateJob(type);
0033     job->exec();
0034     currentInstance = job->instance();
0035 
0036     if (job->error() || !currentInstance.isValid()) {
0037         qDebug() << "  Unable to create resource" << name;
0038         exit(-1);
0039     } else {
0040         qDebug() << "  Created resource instance" << currentInstance.identifier();
0041     }
0042 
0043     QTest::qWait(100); // fix this hack
0044 }
0045 
0046 void MakeTest::configureDBusIface(const QString &name, const QString &dir)
0047 {
0048     auto configIface = new QDBusInterface(QLatin1StringView("org.freedesktop.Akonadi.Resource.") + currentInstance.identifier(),
0049                                           QStringLiteral("/Settings"),
0050                                           QLatin1StringView("org.kde.Akonadi.") + name + QLatin1StringView(".Settings"),
0051                                           QDBusConnection::sessionBus(),
0052                                           this);
0053 
0054     configIface->call(QStringLiteral("setPath"), dir);
0055     configIface->call(QStringLiteral("setReadOnly"), true);
0056 
0057     if (!configIface->isValid()) {
0058         qFatal("Could not configure instance %s.", qPrintable(currentInstance.identifier()));
0059     }
0060 }
0061 
0062 void MakeTest::instanceRemoved(const AgentInstance &instance)
0063 {
0064     Q_UNUSED(instance)
0065     done = true;
0066     // qDebug() << "agent removed:" << instance;
0067 }
0068 
0069 void MakeTest::instanceStatusChanged(const AgentInstance &instance)
0070 {
0071     // qDebug() << "agent status changed:" << agentIdentifier << status << message ;
0072     if (instance == currentInstance) {
0073         if (instance.status() == AgentInstance::Running) {
0074             // qDebug() << "    " << message;
0075         }
0076         if (instance.status() == AgentInstance::Idle) {
0077             done = true;
0078         }
0079     }
0080 }
0081 
0082 void MakeTest::outputStats(const QString &description)
0083 {
0084     output(description + QLatin1StringView("\t\t") + currentAccount + QLatin1StringView("\t\t") + QString::number(timer.elapsed()) + QLatin1Char('\n'));
0085 }
0086 
0087 void MakeTest::output(const QString &message)
0088 {
0089     QTextStream out(stdout);
0090     out << message;
0091 }
0092 
0093 void MakeTest::removeCollections()
0094 {
0095     timer.restart();
0096     qDebug() << "  Removing every folder sequentially.";
0097     auto clj5 = new CollectionFetchJob(Collection::root(), CollectionFetchJob::Recursive);
0098     clj5->fetchScope().setResource(currentInstance.identifier());
0099     clj5->exec();
0100     const Collection::List list5 = clj5->collections();
0101     for (const Collection &collection : list5) {
0102         auto cdj = new CollectionDeleteJob(collection, this);
0103         cdj->exec();
0104     }
0105     outputStats(QStringLiteral("removeallcollections"));
0106 }
0107 
0108 void MakeTest::removeResource()
0109 {
0110     qDebug() << "  Removing resource.";
0111     AgentManager::self()->removeInstance(currentInstance);
0112 }
0113 
0114 void MakeTest::start()
0115 {
0116     runTest();
0117 }
0118 
0119 #include "moc_maketest.cpp"