2022-05-24 09:39:49 +03:00
|
|
|
/* This file is part of the KDE project
|
|
|
|
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
|
2022-05-14 21:28:45 +03:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
2022-05-24 09:39:49 +03:00
|
|
|
License version 2, as published by the Free Software Foundation.
|
2022-05-14 21:28:45 +03:00
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "favicontest.h"
|
2022-05-23 06:54:52 +03:00
|
|
|
|
2022-05-14 21:28:45 +03:00
|
|
|
#include <qtest_kde.h>
|
|
|
|
#include <kio/job.h>
|
|
|
|
#include <kconfiggroup.h>
|
|
|
|
#include <kio/netaccess.h>
|
|
|
|
#include <kmimetype.h>
|
2022-05-23 06:54:52 +03:00
|
|
|
#include <kstandarddirs.h>
|
|
|
|
#include <kdebug.h>
|
2022-05-14 21:28:45 +03:00
|
|
|
|
2022-05-27 07:50:57 +03:00
|
|
|
#include <QImageReader>
|
2022-05-23 06:54:52 +03:00
|
|
|
#include <QElapsedTimer>
|
|
|
|
#include <QEventLoop>
|
2022-05-14 21:28:45 +03:00
|
|
|
|
2022-05-23 06:54:52 +03:00
|
|
|
QTEST_KDEMAIN(FavIconTest, NoGUI)
|
2022-05-14 21:28:45 +03:00
|
|
|
|
2022-05-24 09:39:49 +03:00
|
|
|
// D-Bus and QEventLoop don't get along
|
|
|
|
// #define USE_EVENT_LOOP
|
|
|
|
|
2022-05-23 06:54:52 +03:00
|
|
|
static const char s_hostUrl[] = "https://www.google.com/";
|
2022-05-27 07:50:57 +03:00
|
|
|
static const char s_icoPath[] = KDESRCDIR "designer.ico";
|
2022-05-23 01:35:46 +03:00
|
|
|
static const int s_waitTime = 20000; // in ms
|
2022-05-14 21:28:45 +03:00
|
|
|
|
2022-05-27 07:50:57 +03:00
|
|
|
enum CheckStatus { Unknown, Yes, No };
|
|
|
|
CheckStatus s_networkAccess = CheckStatus::Unknown;
|
2022-05-23 06:54:52 +03:00
|
|
|
static bool checkNetworkAccess()
|
|
|
|
{
|
2022-05-27 07:50:57 +03:00
|
|
|
if (s_networkAccess == CheckStatus::Unknown) {
|
2022-05-23 06:54:52 +03:00
|
|
|
QElapsedTimer networkTimer;
|
|
|
|
networkTimer.start();
|
|
|
|
KIO::Job* job = KIO::get(KUrl(s_hostUrl), KIO::NoReload, KIO::HideProgressInfo);
|
2022-05-24 09:39:49 +03:00
|
|
|
if (KIO::NetAccess::synchronousRun(job, nullptr)) {
|
2022-05-14 21:28:45 +03:00
|
|
|
s_networkAccess = Yes;
|
2022-05-24 09:39:49 +03:00
|
|
|
qDebug("Network access OK. Download time %lld", networkTimer.elapsed());
|
2022-05-14 21:28:45 +03:00
|
|
|
} else {
|
2022-05-23 06:54:52 +03:00
|
|
|
qWarning("%s", qPrintable(KIO::NetAccess::lastErrorString()));
|
2022-05-27 07:50:57 +03:00
|
|
|
s_networkAccess = CheckStatus::No;
|
2022-05-14 21:28:45 +03:00
|
|
|
}
|
|
|
|
}
|
2022-05-27 07:50:57 +03:00
|
|
|
return s_networkAccess == CheckStatus::Yes;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckStatus s_icoReadable = CheckStatus::Unknown;
|
|
|
|
static bool checkICOReadable()
|
|
|
|
{
|
|
|
|
if (s_icoReadable == CheckStatus::Unknown) {
|
|
|
|
QFile icofile(s_icoPath);
|
|
|
|
icofile.open(QFile::ReadOnly);
|
|
|
|
QImageReader icoimagereader(&icofile);
|
|
|
|
if (icoimagereader.canRead()) {
|
|
|
|
s_icoReadable = Yes;
|
|
|
|
qDebug("ICO is readable");
|
|
|
|
} else {
|
|
|
|
qWarning("%s", qPrintable(icoimagereader.errorString()));
|
|
|
|
s_icoReadable = CheckStatus::No;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s_icoReadable == CheckStatus::Yes;
|
2022-05-14 21:28:45 +03:00
|
|
|
}
|
|
|
|
|
2022-05-23 06:54:52 +03:00
|
|
|
static void cleanCache()
|
|
|
|
{
|
|
|
|
QDir faviconsdir(KGlobal::dirs()->saveLocation("cache", QString::fromLatin1("favicons/")));
|
|
|
|
// qDebug() << Q_FUNC_INFO << faviconsdir.absolutePath();
|
|
|
|
foreach (const QFileInfo &favinfo, faviconsdir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot)) {
|
2022-05-24 08:39:43 +03:00
|
|
|
qDebug() << "Removing cached icon" << favinfo.filePath();
|
2022-05-23 06:54:52 +03:00
|
|
|
QFile::remove(favinfo.filePath());
|
|
|
|
}
|
|
|
|
}
|
2022-05-14 21:28:45 +03:00
|
|
|
|
|
|
|
FavIconTest::FavIconTest()
|
2022-05-23 06:54:52 +03:00
|
|
|
: QObject(),
|
2022-05-24 09:39:49 +03:00
|
|
|
m_iconChanged(false),
|
2022-06-05 22:06:42 +03:00
|
|
|
m_favIconModule("org.kde.kded", "/modules/favicons", QDBusConnection::sessionBus())
|
2022-05-14 21:28:45 +03:00
|
|
|
{
|
2022-05-24 09:39:49 +03:00
|
|
|
connect(
|
2023-06-07 20:57:44 +03:00
|
|
|
&m_favIconModule, SIGNAL(iconChanged(QString,QString)),
|
|
|
|
this, SLOT(slotIconChanged(QString,QString))
|
2022-05-24 08:39:43 +03:00
|
|
|
);
|
2022-05-14 21:28:45 +03:00
|
|
|
}
|
|
|
|
|
2022-05-23 06:54:52 +03:00
|
|
|
void FavIconTest::initTestCase()
|
2022-05-14 21:28:45 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-23 06:54:52 +03:00
|
|
|
void FavIconTest::testIconForURL_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("url");
|
|
|
|
QTest::addColumn<QString>("icon");
|
|
|
|
|
|
|
|
QTest::newRow("https://www.google.com")
|
|
|
|
<< QString::fromLatin1("https://www.google.com") << QString::fromLatin1("favicons/www.google.com");
|
2023-06-07 20:57:44 +03:00
|
|
|
QTest::newRow("https://www.ibm.com/foo?bar=baz")
|
2022-05-23 06:54:52 +03:00
|
|
|
<< QString::fromLatin1("https://www.ibm.com") << QString::fromLatin1("favicons/www.ibm.com");
|
2023-06-07 20:57:44 +03:00
|
|
|
QTest::newRow("https://www.wpoven.com/") // NOTE: favicon.png
|
|
|
|
<< QString::fromLatin1("https://www.wpoven.com/") << QString::fromLatin1("favicons/www.wpoven.com");
|
2022-05-24 08:06:19 +03:00
|
|
|
QTest::newRow("https://140.82.121.3/")
|
|
|
|
<< QString::fromLatin1("https://140.82.121.3/") << QString::fromLatin1("favicons/140.82.121.3");
|
2022-05-14 21:28:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void FavIconTest::testIconForURL()
|
|
|
|
{
|
2022-05-23 06:54:52 +03:00
|
|
|
QFETCH(QString, url);
|
|
|
|
QFETCH(QString, icon);
|
2022-05-14 21:28:45 +03:00
|
|
|
|
2022-05-27 07:50:57 +03:00
|
|
|
if (!checkICOReadable()) {
|
|
|
|
QSKIP("ico not readable", SkipAll);
|
|
|
|
}
|
|
|
|
|
2022-05-23 06:54:52 +03:00
|
|
|
if (!checkNetworkAccess()) {
|
|
|
|
QSKIP("no network access", SkipAll);
|
|
|
|
}
|
2022-05-14 21:28:45 +03:00
|
|
|
|
2022-05-23 06:54:52 +03:00
|
|
|
cleanCache();
|
2022-05-14 21:28:45 +03:00
|
|
|
|
2022-05-23 06:54:52 +03:00
|
|
|
const KUrl favUrl(url);
|
|
|
|
QString favicon = KMimeType::favIconForUrl(favUrl);
|
|
|
|
QCOMPARE(favicon, QString());
|
2022-05-14 21:28:45 +03:00
|
|
|
|
2022-05-24 09:39:49 +03:00
|
|
|
#if USE_EVENT_LOOP
|
2022-05-23 06:54:52 +03:00
|
|
|
QEventLoop eventLoop;
|
2023-06-07 20:57:44 +03:00
|
|
|
connect(&m_favIconModule, SIGNAL(iconChanged(QString,QString)), &eventLoop, SLOT(quit()));
|
|
|
|
QSignalSpy spy(&m_favIconModule, SIGNAL(iconChanged(QString,QString)));
|
2022-05-23 06:54:52 +03:00
|
|
|
QVERIFY(spy.isValid());
|
|
|
|
QCOMPARE(spy.count(), 0);
|
2023-06-07 20:57:44 +03:00
|
|
|
m_favIconModule.downloadUrlIcon(url);
|
2022-05-23 06:54:52 +03:00
|
|
|
qDebug("called downloadHostIcon, waiting");
|
|
|
|
if (spy.count() < 1) {
|
|
|
|
QTimer::singleShot(s_waitTime, &eventLoop, SLOT(quit()));
|
|
|
|
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
|
|
|
|
}
|
2022-05-24 09:39:49 +03:00
|
|
|
QCOMPARE(spy.count(), 1);
|
|
|
|
#else
|
|
|
|
m_iconChanged = false;
|
2023-06-07 20:57:44 +03:00
|
|
|
m_favIconModule.downloadUrlIcon(url);
|
2022-05-24 09:39:49 +03:00
|
|
|
qDebug("called downloadHostIcon, waiting");
|
|
|
|
QElapsedTimer elapsedTimer;
|
|
|
|
elapsedTimer.start();
|
|
|
|
while (!m_iconChanged && elapsedTimer.elapsed() < s_waitTime) {
|
|
|
|
QTest::qWait(400);
|
|
|
|
}
|
|
|
|
QVERIFY(m_iconChanged);
|
|
|
|
#endif
|
2022-05-14 21:28:45 +03:00
|
|
|
|
2022-05-23 06:54:52 +03:00
|
|
|
favicon = KMimeType::favIconForUrl(favUrl);
|
|
|
|
QCOMPARE(favicon, icon);
|
2022-05-14 21:28:45 +03:00
|
|
|
}
|
2022-05-23 06:54:52 +03:00
|
|
|
|
2023-06-07 20:57:44 +03:00
|
|
|
void FavIconTest::slotIconChanged(const QString &url, const QString &iconName)
|
2022-05-24 09:39:49 +03:00
|
|
|
{
|
2023-06-07 20:57:44 +03:00
|
|
|
qDebug() << url << iconName;
|
2022-05-24 09:39:49 +03:00
|
|
|
m_iconChanged = true;
|
2023-06-07 20:57:44 +03:00
|
|
|
m_url = url;
|
2022-05-24 09:39:49 +03:00
|
|
|
m_iconName = iconName;
|
|
|
|
}
|
|
|
|
|
2022-05-23 06:54:52 +03:00
|
|
|
#include "moc_favicontest.cpp"
|