Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / lib / konq / tests / favicontest.cpp
blob84564e49238502ce918fba218a5af3c9b51c3511
1 /* This file is part of KDE
2 Copyright (c) 2006 David Faure <faure@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "favicontest.h"
21 #include <qtest_kde.h>
22 #include <kio/job.h>
23 #include <kconfiggroup.h>
24 #include <kio/netaccess.h>
25 #include <QDateTime>
26 #include <kmimetype.h>
28 #include "favicontest.moc"
30 QTEST_KDEMAIN( FavIconTest, NoGUI )
32 static const char* s_hostUrl = "http://www.kde.org";
33 static const char* s_iconUrl = "http://www.kde.org/favicon.ico";
34 static const char* s_altIconUrl = "http://www.ibm.com/favicon.ico";
36 static int s_downloadTime; // in ms
38 enum NetworkAccess { Unknown, Yes, No } s_networkAccess = Unknown;
39 static bool checkNetworkAccess() {
40 if ( s_networkAccess == Unknown ) {
41 QTime tm;
42 tm.start();
43 KIO::Job* job = KIO::get( KUrl( s_iconUrl ), KIO::NoReload, KIO::HideProgressInfo );
44 if( KIO::NetAccess::synchronousRun( job, 0 ) ) {
45 s_networkAccess = Yes;
46 s_downloadTime = tm.elapsed();
47 qDebug( "Network access OK. Download time %d", s_downloadTime );
48 } else {
49 qWarning( "%s", qPrintable( KIO::NetAccess::lastErrorString() ) );
50 s_networkAccess = No;
53 return s_networkAccess == Yes;
57 FavIconTest::FavIconTest()
58 : QObject(), m_favIconModule( "org.kde.kded", "/modules/favicons", QDBusConnection::sessionBus() )
62 void FavIconTest::waitForSignal()
64 // Wait for signals - indefinitely
65 QEventLoop eventLoop;
66 QObject::connect(&m_favIconModule, SIGNAL( iconChanged(bool,QString,QString) ), &eventLoop, SLOT(quit()));
67 eventLoop.exec( QEventLoop::ExcludeUserInputEvents );
69 // or: wait for signals for a certain amount of time...
70 // QTest::qWait( s_downloadTime * 2 + 1000 );
71 // qDebug() << QDateTime::currentDateTime() << " waiting done";
74 void FavIconTest::initTestCase()
76 // Disable kwallet, I don't want kwallet wizard to come up ;)
77 KConfig cfg("kwalletrc");
78 KConfigGroup cg( &cfg, "Wallet");
79 cg.writeEntry("First Use", false);
80 cg.writeEntry("Enabled", false);
83 // To avoid hitting the cache, we first set the icon to s_altIconUrl (ibm.com),
84 // then back to s_iconUrl (kde.org) (to avoid messing up the favicons for the user ;)
85 void FavIconTest::testSetIconForURL()
87 if ( !checkNetworkAccess() )
88 QSKIP( "no network access", SkipAll );
90 QSignalSpy spy( &m_favIconModule, SIGNAL( iconChanged(bool,QString,QString) ) );
91 QVERIFY( spy.isValid() );
92 QCOMPARE( spy.count(), 0 );
94 // The call to connect() triggers qdbus initialization stuff, while QSignalSpy doesn't...
95 connect(&m_favIconModule, SIGNAL( iconChanged(bool,QString,QString) ), &m_eventLoop, SLOT(quit()));
97 m_favIconModule.setIconForUrl( QString( s_hostUrl ), QString( s_altIconUrl ) );
99 qDebug( "called first setIconForUrl, waiting" );
100 if ( spy.count() < 1 ) {
101 m_eventLoop.exec( QEventLoop::ExcludeUserInputEvents );
104 QCOMPARE( spy.count(), 1 );
105 QCOMPARE( spy[0][0].toBool(), false );
106 QCOMPARE( spy[0][1].toString(), QString( s_hostUrl ) );
107 QCOMPARE( spy[0][2].toString(), QString( "favicons/www.ibm.com" ) );
109 m_favIconModule.setIconForUrl( QString( s_hostUrl ), QString( s_iconUrl ) );
111 qDebug( "called setIconForUrl again, waiting" );
112 if ( spy.count() < 2 ) {
113 m_eventLoop.exec( QEventLoop::ExcludeUserInputEvents );
116 QCOMPARE( spy.count(), 2 );
117 QCOMPARE( spy[1][0].toBool(), false );
118 QCOMPARE( spy[1][1].toString(), QString( s_hostUrl ) );
119 QCOMPARE( spy[1][2].toString(), QString( "favicons/www.kde.org" ) );
121 disconnect(&m_favIconModule, SIGNAL( iconChanged(bool,QString,QString) ), &m_eventLoop, SLOT(quit()));
124 void FavIconTest::testIconForURL()
126 QString icon = KMimeType::favIconForUrl( KUrl( s_hostUrl ) );
127 if ( icon.isEmpty() && !checkNetworkAccess() )
128 QSKIP( "no network access", SkipAll );
130 QCOMPARE( icon, QString( "favicons/www.kde.org" ) );
133 #if 0
134 // downloadHostIcon does nothing if the icon is available already, so how to test this?
135 // We could delete the icon first, but given that we have a different KDEHOME than the kded module,
136 // that's not really easy...
137 void FavIconTest::testDownloadHostIcon()
139 if ( !checkNetworkAccess() )
140 QSKIP( "no network access", SkipAll );
142 QSignalSpy spy( &m_favIconModule, SIGNAL( iconChanged(bool,QString,QString) ) );
143 QVERIFY( spy.isValid() );
144 QCOMPARE( spy.count(), 0 );
146 qDebug( "called downloadHostIcon, waiting" );
147 m_favIconModule.downloadHostIcon( QString( s_hostUrl ) );
148 waitForSignal();
150 QCOMPARE( spy.count(), 1 );
151 QCOMPARE( mgr.m_isHost, true );
152 QCOMPARE( mgr.m_hostOrURL, KUrl( s_hostUrl ).host() );
153 qDebug( "icon: %s", qPrintable( mgr.m_iconName ) );
155 #endif