1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*
This file is part of KDE
SPDX-FileCopyrightText: 2006-2016 David Faure <faure@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include <KIO/FavIconRequestJob>
#include <KIO/Job>
#include <QDir>
#include <QLoggingCategory>
#include <QStandardPaths>
#include <QTest>
#include <QFutureSynchronizer>
#include <QThreadPool>
#include <QtConcurrentRun>
static const char s_hostUrl[] = "http://www.google.com/index.html";
static const char s_pageUrl[] = "http://www.google.com/somepage.html";
static const char s_iconUrl[] = "http://www.google.com/favicon.ico";
static const char s_altIconUrl[] = "http://www.ibm.com/favicon.ico";
static const char s_thirdIconUrl[] = "http://www.google.fr/favicon.ico";
static const char s_iconUrlForThreadTest[] = "http://www.google.de/favicon.ico";
static enum NetworkAccess { Unknown, Yes, No } s_networkAccess = Unknown;
static bool checkNetworkAccess()
{
if (s_networkAccess == Unknown) {
QElapsedTimer tm;
tm.start();
KIO::Job *job = KIO::get(QUrl(s_iconUrl), KIO::NoReload, KIO::HideProgressInfo);
if (job->exec()) {
s_networkAccess = Yes;
qDebug() << "Network access OK. Download time" << tm.elapsed() << "ms";
} else {
qWarning() << job->errorString();
s_networkAccess = No;
}
}
return s_networkAccess == Yes;
}
class FavIconTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void favIconForUrlShouldBeEmptyInitially();
void hostJobShouldDownloadIconThenUseCache();
void iconUrlJobShouldDownloadIconThenUseCache();
void reloadShouldReload();
void failedDownloadShouldBeRemembered();
void tooBigFaviconShouldAbort();
void simultaneousRequestsShouldWork();
void concurrentRequestsShouldWork();
private:
};
void FavIconTest::initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
// To let ctest exit, we shouldn't start kio_http_cache_cleaner
qputenv("KIO_DISABLE_CACHE_CLEANER", "yes");
// To get KJob::errorString() in English
qputenv("LC_ALL", "en_US.UTF-8");
if (!checkNetworkAccess()) {
QSKIP("no network access", SkipAll);
}
// Ensure we start with no cache on disk
const QString favIconCacheDir = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QStringLiteral("/favicons");
QDir(favIconCacheDir).removeRecursively();
QVERIFY(!QFileInfo::exists(favIconCacheDir));
// Enable debug output
QLoggingCategory::setFilterRules(QStringLiteral("kf.kio.gui.favicons.debug=true"));
}
void FavIconTest::favIconForUrlShouldBeEmptyInitially()
{
QCOMPARE(KIO::favIconForUrl(QUrl(s_hostUrl)), QString());
}
// Waits for start() and checks whether a transfer job was created.
static bool willDownload(KIO::FavIconRequestJob *job)
{
qApp->sendPostedEvents(job, QEvent::MetaCall); // start() is delayed
return job->findChild<KIO::TransferJob *>();
}
void FavIconTest::hostJobShouldDownloadIconThenUseCache()
{
const QUrl url(s_hostUrl);
KIO::FavIconRequestJob *job = new KIO::FavIconRequestJob(url);
QVERIFY(willDownload(job));
QVERIFY(job->exec());
const QString iconFile = job->iconFile();
QVERIFY(iconFile.endsWith(QLatin1String("favicons/www.google.com.png")));
QVERIFY2(QFile::exists(iconFile), qPrintable(iconFile));
QVERIFY(!QIcon(iconFile).isNull()); // pass full path to QIcon
// This requires https://codereview.qt-project.org/148444
// QVERIFY(!QIcon::fromTheme(iconFile).isNull()); // old code ported from kdelibs4 might do that, should work too
// Lookup should give the same result
QCOMPARE(KIO::favIconForUrl(url), iconFile);
// Second job should use the cache
KIO::FavIconRequestJob *secondJob = new KIO::FavIconRequestJob(url);
QVERIFY(!willDownload(secondJob));
QVERIFY(secondJob->exec());
QCOMPARE(secondJob->iconFile(), iconFile);
// The code from the class docu
QString goticonFile;
{
KIO::FavIconRequestJob *favJob = new KIO::FavIconRequestJob(url);
connect(favJob, &KIO::FavIconRequestJob::result, this, [favJob, &goticonFile](KJob *) {
if (!favJob->error()) {
goticonFile = favJob->iconFile();
}
});
QVERIFY(favJob->exec());
}
QCOMPARE(goticonFile, iconFile);
}
void FavIconTest::iconUrlJobShouldDownloadIconThenUseCache()
{
const QUrl url(s_pageUrl);
// Set icon URL to "ibm"
KIO::FavIconRequestJob *job = new KIO::FavIconRequestJob(url);
job->setIconUrl(QUrl(s_altIconUrl));
QVERIFY(willDownload(job));
QVERIFY(job->exec());
const QString iconFile = job->iconFile();
QVERIFY(iconFile.endsWith(QLatin1String("favicons/www.ibm.com.png")));
QVERIFY2(QFile::exists(iconFile), qPrintable(iconFile));
QVERIFY(!QPixmap(iconFile).isNull()); // pass full path to QPixmap (to test this too)
// Lookup should give the same result
QCOMPARE(KIO::favIconForUrl(url), iconFile);
// Second job should use the cache. It doesn't even need the icon url again.
KIO::FavIconRequestJob *secondJob = new KIO::FavIconRequestJob(url);
QVERIFY(!willDownload(secondJob));
QVERIFY(secondJob->exec());
QCOMPARE(secondJob->iconFile(), iconFile);
// Set icon URL to "www.google.fr/favicon.ico"
KIO::FavIconRequestJob *thirdJob = new KIO::FavIconRequestJob(url);
thirdJob->setIconUrl(QUrl(s_thirdIconUrl));
QVERIFY(willDownload(thirdJob));
QVERIFY(thirdJob->exec());
const QString newiconFile = thirdJob->iconFile();
QVERIFY(newiconFile.endsWith(QLatin1String("favicons/www.google.fr.png")));
// Lookup should give the same result
QCOMPARE(KIO::favIconForUrl(url), newiconFile);
}
void FavIconTest::reloadShouldReload()
{
const QUrl url(s_hostUrl);
// First job, to make sure it's in the cache (if the other tests didn't run first)
KIO::FavIconRequestJob *job = new KIO::FavIconRequestJob(url);
QVERIFY(job->exec());
const QString iconFile = job->iconFile();
// Second job should use the cache
KIO::FavIconRequestJob *secondJob = new KIO::FavIconRequestJob(url);
QVERIFY(!willDownload(secondJob));
QVERIFY(secondJob->exec());
QCOMPARE(secondJob->iconFile(), iconFile);
// job with Reload should not use the cache
KIO::FavIconRequestJob *jobWithReload = new KIO::FavIconRequestJob(url, KIO::Reload);
QVERIFY(willDownload(jobWithReload));
QVERIFY(jobWithReload->exec());
QCOMPARE(jobWithReload->iconFile(), iconFile);
}
void FavIconTest::failedDownloadShouldBeRemembered()
{
const QUrl url(s_pageUrl);
// Set icon URL to a non-existing favicon
KIO::FavIconRequestJob *job = new KIO::FavIconRequestJob(url);
job->setIconUrl(QUrl("https://kde.org/doesnotexist/favicon.ico"));
QVERIFY(willDownload(job));
QVERIFY(!job->exec());
QVERIFY(job->iconFile().isEmpty());
qDebug() << job->errorString();
QCOMPARE(job->error(), int(KIO::ERR_DOES_NOT_EXIST));
QCOMPARE(job->errorString(), QStringLiteral("The file or folder https://kde.org/doesnotexist/favicon.ico does not exist."));
// Second job should use the cache and not do anything
KIO::FavIconRequestJob *secondJob = new KIO::FavIconRequestJob(url);
QVERIFY(!willDownload(secondJob));
QVERIFY(!secondJob->exec());
QVERIFY(secondJob->iconFile().isEmpty());
QCOMPARE(job->error(), int(KIO::ERR_DOES_NOT_EXIST));
QCOMPARE(job->errorString(), QStringLiteral("The file or folder https://kde.org/doesnotexist/favicon.ico does not exist."));
}
void FavIconTest::tooBigFaviconShouldAbort()
{
const QUrl url(s_pageUrl);
// Set icon URL to a >65KB file
KIO::FavIconRequestJob *job = new KIO::FavIconRequestJob(url);
job->setIconUrl(QUrl("http://download.kde.org/Attic/4.13.2/src/kcalc-4.13.2.tar.xz"));
QVERIFY(willDownload(job));
QVERIFY(!job->exec());
QCOMPARE(job->error(), int(KIO::ERR_WORKER_DEFINED));
QCOMPARE(job->errorString(), QStringLiteral("Icon file too big, download aborted"));
}
void FavIconTest::simultaneousRequestsShouldWork()
{
const QUrl url(s_hostUrl);
// First job, to find out the iconFile and delete it
QString iconFile;
{
KIO::FavIconRequestJob *job = new KIO::FavIconRequestJob(url);
QVERIFY(job->exec());
iconFile = job->iconFile();
QFile::remove(iconFile);
}
// This is a case we could maybe optimize: not downloading twice in parallel
KIO::FavIconRequestJob *job1 = new KIO::FavIconRequestJob(url);
job1->setAutoDelete(false);
KIO::FavIconRequestJob *job2 = new KIO::FavIconRequestJob(url);
job2->setAutoDelete(false);
QVERIFY(willDownload(job1));
QVERIFY(willDownload(job2));
QVERIFY(job1->exec());
QCOMPARE(job1->iconFile(), iconFile);
QVERIFY(job2->exec());
QCOMPARE(job2->iconFile(), iconFile);
delete job1;
delete job2;
}
static QString getAltIconUrl()
{
const QUrl url(s_pageUrl);
// Set icon URL to one that we haven't downloaded yet
KIO::FavIconRequestJob *job = new KIO::FavIconRequestJob(url);
job->setIconUrl(QUrl(s_iconUrlForThreadTest));
job->exec();
return job->iconFile();
}
void FavIconTest::concurrentRequestsShouldWork()
{
const int numThreads = 3;
QThreadPool tp;
tp.setMaxThreadCount(numThreads);
QVector<QFuture<QString>> futures(numThreads);
for (int i = 0; i < numThreads; ++i) {
futures[i] = QtConcurrent::run(&tp, getAltIconUrl);
}
QVERIFY(tp.waitForDone(60000));
const QString firstResult = futures.at(0).result();
for (int i = 1; i < numThreads; ++i) {
QCOMPARE(futures.at(i).result(), firstResult);
}
QVERIFY(!QPixmap(firstResult).isNull());
}
QTEST_MAIN(FavIconTest)
#include "favicontest.moc"
|