Akonadi

qtest_akonadi.h
1 /* This file is based on qtest_kde.h from kdelibs
2  Copyright (C) 2006 David Faure <[email protected]>
3  Copyright (C) 2009 Volker Krause <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #ifndef QTEST_AKONADI_H
21 #define QTEST_AKONADI_H
22 
23 #include "agentinstance.h"
24 #include "agentmanager.h"
25 #include "servermanager.h"
26 #include "collectionpathresolver.h"
27 #include "monitor.h"
28 #include "collectionfetchscope.h"
29 #include "itemfetchscope.h"
30 
31 #include <QTest>
32 #include <QSignalSpy>
33 #include <QTimer>
34 #include <QDBusConnection>
35 #include <QDBusInterface>
36 #include <QDBusReply>
37 
54 #define QTEST_AKONADIMAIN(TestObject) \
55  int main(int argc, char *argv[]) \
56  { \
57  qputenv("LC_ALL", "C"); \
58  qunsetenv("KDE_COLOR_DEBUG"); \
59  QApplication app(argc, argv); \
60  app.setApplicationName(QStringLiteral("qttest")); \
61  app.setOrganizationDomain(QStringLiteral("kde.org")); \
62  app.setOrganizationName(QStringLiteral("KDE")); \
63  QGuiApplication::setQuitOnLastWindowClosed(false); \
64  qRegisterMetaType<QList<QUrl>>(); \
65  int result = 0; \
66  QTimer::singleShot(0, [argc, argv, &result]() { \
67  TestObject tc; \
68  result = QTest::qExec(&tc, argc, argv); \
69  qApp->quit(); \
70  }); \
71  app.exec(); \
72  return result; \
73  }
74 
75 namespace AkonadiTest
76 {
80 void checkTestIsIsolated()
81 {
82  if (qEnvironmentVariableIsEmpty("TESTRUNNER_DB_ENVIRONMENT"))
83  qFatal("This test must be run using ctest, in order to use the testrunner environment. Aborting, to avoid messing up your real akonadi");
84  if (!qgetenv("XDG_DATA_HOME").contains("testrunner"))
85  qFatal("Did you forget to run the test using QTEST_AKONADIMAIN?");
86 }
87 
91 void setAllResourcesOffline()
92 {
93  // switch all resources offline to reduce interference from them
94  const auto lst = Akonadi::AgentManager::self()->instances();
95  for (Akonadi::AgentInstance agent : lst) {
96  agent.setIsOnline(false);
97  }
98 }
99 
100 template<typename Object, typename Func>
101 bool akWaitForSignal(Object sender, Func member, int timeout = 1000)
102 {
103  QSignalSpy spy(sender, member);
104  bool ok = false;
105  [&]() {
106  QTRY_VERIFY_WITH_TIMEOUT(spy.count() > 0, timeout);
107  ok = true;
108  }();
109  return ok;
110 }
111 
112 bool akWaitForSignal(const QObject *sender, const char *member, int timeout = 1000)
113 {
114  QSignalSpy spy(sender, member);
115  bool ok = false;
116  [&]() {
117  QTRY_VERIFY_WITH_TIMEOUT(spy.count() > 0, timeout);
118  ok = true;
119  }();
120  return ok;
121 }
122 
123 qint64 collectionIdFromPath(const QString &path)
124 {
126  bool success = resolver->exec();
127  if (!success) {
128  qDebug() << "path resolution for " << path << " failed: " << resolver->errorText();
129  return -1;
130  }
131  qint64 id = resolver->collection();
132  return id;
133 }
134 
135 QString testrunnerServiceName()
136 {
137  const QString pid = QString::fromLocal8Bit(qgetenv("AKONADI_TESTRUNNER_PID"));
138  Q_ASSERT(!pid.isEmpty());
139  return QStringLiteral("org.kde.Akonadi.Testrunner-") + pid;
140 }
141 
142 bool restartAkonadiServer()
143 {
144  QDBusInterface testrunnerIface(testrunnerServiceName(),
145  QStringLiteral("/"),
146  QStringLiteral("org.kde.Akonadi.Testrunner"),
148  if (!testrunnerIface.isValid()) {
149  qWarning() << "Unable to get a dbus interface to the testrunner!";
150  }
151 
152  QDBusReply<void> reply = testrunnerIface.call(QStringLiteral("restartAkonadiServer"));
153  if (!reply.isValid()) {
154  qWarning() << reply.error();
155  return false;
156  } else if (Akonadi::ServerManager::isRunning()) {
157  return true;
158  } else {
159  bool ok = false;
160  [&]() {
162  QTRY_VERIFY_WITH_TIMEOUT(spy.count() > 0, 10000);
163  ok = true;
164  }();
165  return ok;
166  }
167 }
168 
169 bool trackAkonadiProcess(bool track)
170 {
171  QDBusInterface testrunnerIface(testrunnerServiceName(),
172  QStringLiteral("/"),
173  QStringLiteral("org.kde.Akonadi.Testrunner"),
175  if (!testrunnerIface.isValid()) {
176  qWarning() << "Unable to get a dbus interface to the testrunner!";
177  }
178 
179  QDBusReply<void> reply = testrunnerIface.call(QStringLiteral("trackAkonadiProcess"), track);
180  if (!reply.isValid()) {
181  qWarning() << reply.error();
182  return false;
183  } else {
184  return true;
185  }
186 }
187 
188 std::unique_ptr<Akonadi::Monitor> getTestMonitor()
189 {
190  auto m = new Akonadi::Monitor();
191  m->fetchCollection(true);
192  m->setCollectionMonitored(Akonadi::Collection::root(), true);
193  m->setAllMonitored(true);
194  auto &itemFS = m->itemFetchScope();
195  itemFS.setAncestorRetrieval(Akonadi::ItemFetchScope::All);
196  auto &colFS = m->collectionFetchScope();
197  colFS.setAncestorRetrieval(Akonadi::CollectionFetchScope::All);
198 
199  QSignalSpy readySpy(m, &Akonadi::Monitor::monitorReady);
200  readySpy.wait();
201 
202  return std::unique_ptr<Akonadi::Monitor>(m);
203 }
204 
205 
206 } // namespace
207 
213 #define AKVERIFYEXEC( job ) \
214  QVERIFY2( job->exec(), job->errorString().toUtf8().constData() )
215 
216 #endif
AgentInstance::List instances() const
Returns the list of all available agent instances.
Collection::Id collection() const
Returns the collection id.
bool exec()
static ServerManager * self()
Returns the singleton instance of this class, for connecting to its signals.
bool isValid() const const
QDBusConnection sessionBus()
QString fromLocal8Bit(const char *str, int size)
bool isEmpty() const const
Retrieve all ancestors, up to Collection::root()
static Collection root()
Returns the root collection.
Definition: collection.cpp:303
void started()
Emitted whenever the server becomes fully operational.
const QDBusError & error()
Monitors an item or collection for changes.
Definition: monitor.h:84
QTRY_VERIFY_WITH_TIMEOUT(condition, timeout)
Retrieve all ancestors, up to Collection::root()
static AgentManager * self()
Returns the global instance of the agent manager.
A representation of an agent instance.
static bool isRunning()
Checks if the server is available currently.
QString errorText() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon May 25 2020 22:46:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.