libs: workaround for QSignalSpy in favicon test

TODO: reimplement QSignalSpy to use connections or just write a new class

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-05-24 09:39:49 +03:00
parent 3bcc2da57b
commit 038d9286b0
2 changed files with 67 additions and 19 deletions

View file

@ -1,10 +1,9 @@
/* This file is part of KDE
Copyright (c) 2006 David Faure <faure@kde.org>
/* This file is part of the KDE project
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
License version 2, as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -32,6 +31,9 @@
QTEST_KDEMAIN(FavIconTest, NoGUI)
// D-Bus and QEventLoop don't get along
// #define USE_EVENT_LOOP
static const char s_hostUrl[] = "https://www.google.com/";
static const int s_waitTime = 20000; // in ms
@ -42,9 +44,9 @@ static bool checkNetworkAccess()
QElapsedTimer networkTimer;
networkTimer.start();
KIO::Job* job = KIO::get(KUrl(s_hostUrl), KIO::NoReload, KIO::HideProgressInfo);
if( KIO::NetAccess::synchronousRun(job, 0) ) {
if (KIO::NetAccess::synchronousRun(job, nullptr)) {
s_networkAccess = Yes;
qDebug("Network access OK. Download time %d", networkTimer.elapsed());
qDebug("Network access OK. Download time %lld", networkTimer.elapsed());
} else {
qWarning("%s", qPrintable(KIO::NetAccess::lastErrorString()));
s_networkAccess = No;
@ -65,8 +67,14 @@ static void cleanCache()
FavIconTest::FavIconTest()
: QObject(),
m_favIconModule("org.kde.kded", "/modules/favicons", QDBusConnection::sessionBus())
m_favIconModule("org.kde.kded", "/modules/favicons", QDBusConnection::sessionBus()),
m_iconChanged(false),
m_isHost(false)
{
connect(
&m_favIconModule, SIGNAL(iconChanged(bool,QString,QString)),
this, SLOT(slotIconChanged(bool,QString,QString))
);
connect(
&m_favIconModule, SIGNAL(infoMessage(QString,QString)),
this, SLOT(slotInfoMessage(QString,QString))
@ -98,7 +106,7 @@ void FavIconTest::testSetIconForURL_data()
<< QString::fromLatin1("favicons/github.com");
QTest::newRow("https://140.82.121.3/")
<< QString::fromLatin1("https://140.82.121.3/") << QString::fromLatin1("https://140.82.121.3/favicon.ico")
<< QString::fromLatin1("favicons/lb-140-82-121-3-fra.github.com");
<< QString::fromLatin1("favicons/140.82.121.3"); // lb-140-82-121-3-fra.github.com if not host
}
void FavIconTest::testSetIconForURL()
@ -113,13 +121,12 @@ void FavIconTest::testSetIconForURL()
cleanCache();
#if USE_EVENT_LOOP
QEventLoop eventLoop;
connect(&m_favIconModule, SIGNAL(iconChanged(bool,QString,QString)), &eventLoop, SLOT(quit()));
QSignalSpy spy(&m_favIconModule, SIGNAL(iconChanged(bool,QString,QString)));
QVERIFY(spy.isValid());
QCOMPARE(spy.count(), 0);
m_favIconModule.setIconForUrl(url, icon);
qDebug("called setIconForUrl, waiting");
if (spy.count() < 1) {
@ -131,6 +138,23 @@ void FavIconTest::testSetIconForURL()
QCOMPARE(spy[0][0].toBool(), false);
QCOMPARE(spy[0][1].toString(), url);
QCOMPARE(spy[0][2].toString(), result);
#else
m_iconChanged = false;
m_favIconModule.downloadHostIcon(url);
qDebug("called downloadHostIcon, waiting");
QElapsedTimer elapsedTimer;
elapsedTimer.start();
while (!m_iconChanged && elapsedTimer.elapsed() < s_waitTime) {
QTest::qWait(400);
}
QVERIFY(m_iconChanged);
if (m_isHost) {
QCOMPARE(m_hostOrURL, KUrl(url).host());
} else {
QCOMPARE(m_hostOrURL, url);
}
QCOMPARE(m_iconName, result);
#endif
}
void FavIconTest::testIconForURL_data()
@ -163,25 +187,44 @@ void FavIconTest::testIconForURL()
QString favicon = KMimeType::favIconForUrl(favUrl);
QCOMPARE(favicon, QString());
#if USE_EVENT_LOOP
QEventLoop eventLoop;
// The call to connect() triggers qdbus initialization stuff, while QSignalSpy doesn't...
connect(&m_favIconModule, SIGNAL(iconChanged(bool,QString,QString)), &eventLoop, SLOT(quit()));
QSignalSpy spy(&m_favIconModule, SIGNAL(iconChanged(bool,QString,QString)));
QVERIFY(spy.isValid());
QCOMPARE(spy.count(), 0);
m_favIconModule.downloadHostIcon(url);
qDebug("called downloadHostIcon, waiting");
if (spy.count() < 1) {
QTimer::singleShot(s_waitTime, &eventLoop, SLOT(quit()));
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
}
QCOMPARE(spy.count(), 1);
#else
m_iconChanged = false;
m_favIconModule.downloadHostIcon(url);
qDebug("called downloadHostIcon, waiting");
QElapsedTimer elapsedTimer;
elapsedTimer.start();
while (!m_iconChanged && elapsedTimer.elapsed() < s_waitTime) {
QTest::qWait(400);
}
QVERIFY(m_iconChanged);
#endif
favicon = KMimeType::favIconForUrl(favUrl);
QCOMPARE(favicon, icon);
}
void FavIconTest::slotIconChanged(const bool isHost, const QString &hostOrURL, const QString &iconName)
{
qDebug() << isHost << hostOrURL << iconName;
m_iconChanged = true;
m_isHost = isHost;
m_hostOrURL = hostOrURL;
m_iconName = iconName;
}
void FavIconTest::slotInfoMessage(const QString &iconURL, const QString &msg)
{
qDebug() << iconURL << msg;

View file

@ -1,10 +1,9 @@
/* This file is part of KDE
Copyright (c) 2006 David Faure <faure@kde.org>
/* This file is part of the KDE project
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
License version 2, as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -20,9 +19,10 @@
#ifndef FAVICONTEST_H
#define FAVICONTEST_H
#include <QObject>
#include "favicon_interface.h"
#include <QObject>
class FavIconTest : public QObject
{
Q_OBJECT
@ -36,11 +36,16 @@ private Q_SLOTS:
void testIconForURL_data();
void testIconForURL();
void slotIconChanged(const bool isHost, const QString &hostOrURL, const QString &iconName);
void slotInfoMessage(const QString &iconURL, const QString &msg);
void slotError(const bool isHost, const QString &hostOrURL, const QString &errorString);
private:
void waitForSignal();
bool m_iconChanged;
bool m_isHost;
QString m_hostOrURL;
QString m_iconName;
org::kde::FavIcon m_favIconModule;
};