2022-05-14 21:28:45 +03:00
|
|
|
/* This file is part of KDE
|
|
|
|
Copyright (c) 2006 David Faure <faure@kde.org>
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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-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-23 06:54:52 +03:00
|
|
|
static const char s_hostUrl[] = "https://www.google.com/";
|
2022-05-23 01:35:46 +03:00
|
|
|
static const int s_waitTime = 20000; // in ms
|
2022-05-14 21:28:45 +03:00
|
|
|
|
|
|
|
enum NetworkAccess { Unknown, Yes, No } s_networkAccess = Unknown;
|
2022-05-23 06:54:52 +03:00
|
|
|
static bool checkNetworkAccess()
|
|
|
|
{
|
|
|
|
if (s_networkAccess == Unknown) {
|
|
|
|
QElapsedTimer networkTimer;
|
|
|
|
networkTimer.start();
|
|
|
|
KIO::Job* job = KIO::get(KUrl(s_hostUrl), KIO::NoReload, KIO::HideProgressInfo);
|
|
|
|
if( KIO::NetAccess::synchronousRun(job, 0) ) {
|
2022-05-14 21:28:45 +03:00
|
|
|
s_networkAccess = Yes;
|
2022-05-23 06:54:52 +03:00
|
|
|
qDebug("Network access OK. Download time %d", 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-14 21:28:45 +03:00
|
|
|
s_networkAccess = No;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s_networkAccess == Yes;
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
kWarning() << "Removing cached icon" << favinfo.filePath();
|
|
|
|
QFile::remove(favinfo.filePath());
|
|
|
|
}
|
|
|
|
}
|
2022-05-14 21:28:45 +03:00
|
|
|
|
|
|
|
FavIconTest::FavIconTest()
|
2022-05-23 06:54:52 +03:00
|
|
|
: QObject(),
|
|
|
|
m_favIconModule("org.kde.kded", "/modules/favicons", QDBusConnection::sessionBus())
|
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::testSetIconForURL_data()
|
2022-05-14 21:28:45 +03:00
|
|
|
{
|
2022-05-23 06:54:52 +03:00
|
|
|
QTest::addColumn<QString>("url");
|
|
|
|
QTest::addColumn<QString>("icon");
|
|
|
|
QTest::addColumn<QString>("result");
|
|
|
|
|
|
|
|
QTest::newRow("https://www.google.com")
|
|
|
|
<< QString::fromLatin1("https://www.google.com") << QString::fromLatin1("https://www.google.com/favicon.ico")
|
|
|
|
<< QString::fromLatin1("favicons/www.google.com");
|
|
|
|
QTest::newRow("https://www.ibm.com")
|
|
|
|
<< QString::fromLatin1("https://www.ibm.com") << QString::fromLatin1("https://www.ibm.com/favicon.ico")
|
|
|
|
<< QString::fromLatin1("favicons/www.ibm.com");
|
|
|
|
QTest::newRow("https://github.com/")
|
|
|
|
<< QString::fromLatin1("https://github.com/") << QString::fromLatin1("https://github.com/favicon.ico")
|
|
|
|
<< QString::fromLatin1("favicons/github.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("https://140.82.121.3/favicon.ico")
|
|
|
|
<< QString::fromLatin1("favicons/lb-140-82-121-3-fra.github.com");
|
2022-05-14 21:28:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void FavIconTest::testSetIconForURL()
|
|
|
|
{
|
2022-05-23 06:54:52 +03:00
|
|
|
QFETCH(QString, url);
|
|
|
|
QFETCH(QString, icon);
|
|
|
|
QFETCH(QString, result);
|
2022-05-22 08:28:43 +03:00
|
|
|
|
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-23 00:09:32 +03:00
|
|
|
|
2022-05-23 06:54:52 +03:00
|
|
|
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.setIconForUrl(url, icon);
|
|
|
|
qDebug("called setIconForUrl, waiting");
|
|
|
|
if (spy.count() < 1) {
|
|
|
|
QTimer::singleShot(s_waitTime, &eventLoop, SLOT(quit()));
|
|
|
|
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
|
|
|
|
}
|
2022-05-14 21:28:45 +03:00
|
|
|
|
2022-05-23 06:54:52 +03:00
|
|
|
QCOMPARE(spy.count(), 1);
|
|
|
|
QCOMPARE(spy[0][0].toBool(), false);
|
|
|
|
QCOMPARE(spy[0][1].toString(), url);
|
|
|
|
QCOMPARE(spy[0][2].toString(), result);
|
|
|
|
}
|
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");
|
|
|
|
QTest::newRow("https://www.ibm.com")
|
|
|
|
<< QString::fromLatin1("https://www.ibm.com") << QString::fromLatin1("favicons/www.ibm.com");
|
|
|
|
QTest::newRow("https://github.com/")
|
|
|
|
<< QString::fromLatin1("https://github.com/") << QString::fromLatin1("favicons/github.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-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-23 06:54:52 +03:00
|
|
|
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);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
#include "moc_favicontest.cpp"
|