File indexing completed on 2024-05-19 16:38:19

0001 /*
0002     SPDX-FileCopyrightText: 2015 Ivan Cukic <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "ResultWatcherTest.h"
0008 
0009 #include <QCoreApplication>
0010 #include <QDBusConnection>
0011 #include <QDBusConnectionInterface>
0012 #include <QDebug>
0013 #include <QString>
0014 #include <QTemporaryDir>
0015 #include <QTest>
0016 #include <QTime>
0017 
0018 #include <KActivities/ResourceInstance>
0019 
0020 #include <query.h>
0021 #include <resultset.h>
0022 #include <resultwatcher.h>
0023 
0024 #include <common/database/Database.h>
0025 #include <common/database/schema/ResourcesDatabaseSchema.h>
0026 
0027 namespace KAStats = KActivities::Stats;
0028 
0029 ResultWatcherTest::ResultWatcherTest(QObject *parent)
0030     : Test(parent)
0031 {
0032 }
0033 
0034 namespace
0035 {
0036 inline void liveSleep(int seconds)
0037 {
0038     qDebug() << "Sleeping for " << seconds << " seconds";
0039     auto start = QTime::currentTime();
0040     while (start.secsTo(QTime::currentTime()) < seconds) {
0041         QCoreApplication::processEvents();
0042     }
0043 }
0044 
0045 #define CHECK_SIGNAL_RESULT(OBJ, SIGN, SECS, TESTARGS, TESTBODY)                                                                                               \
0046     {                                                                                                                                                          \
0047         QObject context;                                                                                                                                       \
0048         bool executed = false;                                                                                                                                 \
0049                                                                                                                                                                \
0050         QObject::connect(OBJ, SIGN, &context, [&] TESTARGS {                                                                                                   \
0051             TESTBODY;                                                                                                                                          \
0052             executed = true;                                                                                                                                   \
0053             qDebug() << "Signal processed";                                                                                                                    \
0054         });                                                                                                                                                    \
0055                                                                                                                                                                \
0056         qDebug() << "Waiting  for the signal at most " << SECS << " seconds";                                                                                  \
0057         auto start = QTime::currentTime();                                                                                                                     \
0058         while (start.secsTo(QTime::currentTime()) < SECS && !executed) {                                                                                       \
0059             QCoreApplication::processEvents();                                                                                                                 \
0060         }                                                                                                                                                      \
0061         QCOMPARE(executed, true);                                                                                                                              \
0062     }
0063 }
0064 
0065 void ResultWatcherTest::testLinkedResources()
0066 {
0067     using namespace KAStats;
0068     using namespace KAStats::Terms;
0069 
0070     KAStats::ResultWatcher watcher(LinkedResources | Agent::global() | Activity::any());
0071 
0072     watcher.linkToActivity(QUrl(QStringLiteral("test://link1")), Activity::current());
0073 
0074     // A signal should arrive soon, waiting for 5 seconds at most
0075     CHECK_SIGNAL_RESULT(&watcher, &KAStats::ResultWatcher::resultLinked, 5, (const QString &uri), QCOMPARE(QStringLiteral("test://link1"), uri));
0076 
0077     watcher.unlinkFromActivity(QUrl(QStringLiteral("test://link1")), Activity::current());
0078 
0079     // A signal should arrive soon, waiting for 5 seconds at most
0080     CHECK_SIGNAL_RESULT(&watcher, &KAStats::ResultWatcher::resultUnlinked, 5, (const QString &uri), QCOMPARE(QStringLiteral("test://link1"), uri));
0081 }
0082 
0083 void ResultWatcherTest::testUsedResources()
0084 {
0085     using namespace KAStats;
0086     using namespace KAStats::Terms;
0087 
0088     KAStats::ResultWatcher watcher(UsedResources | Agent::current() | Activity::any());
0089 
0090     // Opening a resource for a few seconds
0091     {
0092         KActivities::ResourceInstance resource(0);
0093         resource.setUri(QUrl(QStringLiteral("test://test1")));
0094 
0095         liveSleep(3);
0096     }
0097 
0098     // A signal should arrive soon, waiting for 5 seconds at most
0099     CHECK_SIGNAL_RESULT(&watcher, &KAStats::ResultWatcher::resultScoreUpdated, 5, (const QString &uri, double), QCOMPARE(QStringLiteral("test://test1"), uri));
0100 }
0101 
0102 void ResultWatcherTest::initTestCase()
0103 {
0104 }
0105 
0106 void ResultWatcherTest::cleanupTestCase()
0107 {
0108     Q_EMIT testFinished();
0109 }
0110 
0111 #include "moc_ResultWatcherTest.cpp"