2013-12-14 13:53:18 +00:00
# include <QTest>
2013-12-14 15:02:51 +00:00
# include <QSignalSpy>
2013-12-14 13:53:18 +00:00
2014-07-01 00:48:09 +01:00
# include "logic/settings/SettingsObject.h"
# include "logic/settings/Setting.h"
2014-01-05 22:24:05 +00:00
2014-04-06 02:59:37 +01:00
# include "BuildConfig.h"
2013-12-14 13:53:18 +00:00
# include "TestUtil.h"
2013-12-14 15:02:51 +00:00
# include "logic/updater/UpdateChecker.h"
Q_DECLARE_METATYPE ( UpdateChecker : : ChannelListEntry )
bool operator = = ( const UpdateChecker : : ChannelListEntry & e1 , const UpdateChecker : : ChannelListEntry & e2 )
{
return e1 . id = = e2 . id & &
e1 . name = = e2 . name & &
e1 . description = = e2 . description & &
e1 . url = = e2 . url ;
}
2013-12-14 13:53:18 +00:00
2013-12-16 19:10:11 +00:00
QDebug operator < < ( QDebug dbg , const UpdateChecker : : ChannelListEntry & c )
{
dbg . nospace ( ) < < " ChannelListEntry(id= " < < c . id < < " name= " < < c . name < < " description= " < < c . description < < " url= " < < c . url < < " ) " ;
return dbg . maybeSpace ( ) ;
}
2014-01-05 22:24:05 +00:00
class ResetSetting
{
public :
ResetSetting ( std : : shared_ptr < Setting > setting ) : setting ( setting ) , oldValue ( setting - > get ( ) ) { }
~ ResetSetting ( )
{
setting - > set ( oldValue ) ;
}
std : : shared_ptr < Setting > setting ;
QVariant oldValue ;
} ;
2013-12-14 13:53:18 +00:00
class UpdateCheckerTest : public QObject
{
Q_OBJECT
private
slots :
void initTestCase ( )
{
}
void cleanupTestCase ( )
{
}
2013-12-14 15:02:51 +00:00
static QString findTestDataUrl ( const char * file )
{
return QUrl : : fromLocalFile ( QFINDTESTDATA ( file ) ) . toString ( ) ;
}
void tst_ChannelListParsing_data ( )
{
QTest : : addColumn < QString > ( " channel " ) ;
QTest : : addColumn < QString > ( " channelUrl " ) ;
QTest : : addColumn < bool > ( " hasChannels " ) ;
2013-12-14 18:19:14 +00:00
QTest : : addColumn < bool > ( " valid " ) ;
2013-12-14 15:02:51 +00:00
QTest : : addColumn < QList < UpdateChecker : : ChannelListEntry > > ( " result " ) ;
2013-12-14 18:19:14 +00:00
QTest : : newRow ( " garbage " )
< < QString ( )
< < findTestDataUrl ( " tests/data/garbageChannels.json " )
< < false
< < false
< < QList < UpdateChecker : : ChannelListEntry > ( ) ;
QTest : : newRow ( " errors " )
< < QString ( )
< < findTestDataUrl ( " tests/data/errorChannels.json " )
< < false
< < true
< < QList < UpdateChecker : : ChannelListEntry > ( ) ;
2013-12-14 15:02:51 +00:00
QTest : : newRow ( " no channels " )
< < QString ( )
< < findTestDataUrl ( " tests/data/noChannels.json " )
< < false
2013-12-14 18:19:14 +00:00
< < true
2013-12-14 15:02:51 +00:00
< < QList < UpdateChecker : : ChannelListEntry > ( ) ;
QTest : : newRow ( " one channel " )
< < QString ( " develop " )
< < findTestDataUrl ( " tests/data/oneChannel.json " )
< < true
2013-12-14 18:19:14 +00:00
< < true
2013-12-14 15:02:51 +00:00
< < ( QList < UpdateChecker : : ChannelListEntry > ( ) < < UpdateChecker : : ChannelListEntry { " develop " , " Develop " , " The channel called \" develop \" " , " http://example.org/stuff " } ) ;
QTest : : newRow ( " several channels " )
< < QString ( " develop " )
< < findTestDataUrl ( " tests/data/channels.json " )
< < true
2013-12-14 18:19:14 +00:00
< < true
2013-12-14 15:02:51 +00:00
< < ( QList < UpdateChecker : : ChannelListEntry > ( )
2014-01-05 22:24:05 +00:00
< < UpdateChecker : : ChannelListEntry { " develop " , " Develop " , " The channel called \" develop \" " , MultiMC_TEST_DATA_PATH }
< < UpdateChecker : : ChannelListEntry { " stable " , " Stable " , " It's stable at least " , MultiMC_TEST_DATA_PATH }
2013-12-14 15:02:51 +00:00
< < UpdateChecker : : ChannelListEntry { " 42 " , " The Channel " , " This is the channel that is going to answer all of your questions " , " https://dent.me/tea " } ) ;
}
void tst_ChannelListParsing ( )
{
2014-01-05 22:24:05 +00:00
ResetSetting resetUpdateChannel ( MMC - > settings ( ) - > getSetting ( " UpdateChannel " ) ) ;
2013-12-14 15:02:51 +00:00
QFETCH ( QString , channel ) ;
QFETCH ( QString , channelUrl ) ;
QFETCH ( bool , hasChannels ) ;
2013-12-14 18:19:14 +00:00
QFETCH ( bool , valid ) ;
2013-12-14 15:02:51 +00:00
QFETCH ( QList < UpdateChecker : : ChannelListEntry > , result ) ;
2014-01-05 22:24:05 +00:00
MMC - > settings ( ) - > set ( " UpdateChannel " , channel ) ;
2013-12-14 15:02:51 +00:00
UpdateChecker checker ;
2013-12-14 18:19:14 +00:00
QSignalSpy channelListLoadedSpy ( & checker , SIGNAL ( channelListLoaded ( ) ) ) ;
QVERIFY ( channelListLoadedSpy . isValid ( ) ) ;
2013-12-14 15:02:51 +00:00
checker . setChannelListUrl ( channelUrl ) ;
2014-07-13 23:57:54 +01:00
checker . updateChanList ( false ) ;
2013-12-14 15:02:51 +00:00
2013-12-14 18:19:14 +00:00
if ( valid )
{
QVERIFY ( channelListLoadedSpy . wait ( ) ) ;
QCOMPARE ( channelListLoadedSpy . size ( ) , 1 ) ;
}
else
{
channelListLoadedSpy . wait ( ) ;
QCOMPARE ( channelListLoadedSpy . size ( ) , 0 ) ;
}
2013-12-14 15:02:51 +00:00
QCOMPARE ( checker . hasChannels ( ) , hasChannels ) ;
QCOMPARE ( checker . getChannelList ( ) , result ) ;
}
2014-01-05 22:24:05 +00:00
2013-12-14 18:19:14 +00:00
void tst_UpdateChecking_data ( )
{
QTest : : addColumn < QString > ( " channel " ) ;
QTest : : addColumn < QString > ( " channelUrl " ) ;
QTest : : addColumn < int > ( " currentBuild " ) ;
QTest : : addColumn < QList < QVariant > > ( " result " ) ;
QTest : : newRow ( " valid channel " )
< < " develop " < < findTestDataUrl ( " tests/data/channels.json " )
< < 2
< < ( QList < QVariant > ( ) < < QString ( ) < < " 1.0.3 " < < 3 ) ;
}
void tst_UpdateChecking ( )
{
2014-01-05 22:24:05 +00:00
ResetSetting resetUpdateChannel ( MMC - > settings ( ) - > getSetting ( " UpdateChannel " ) ) ;
2013-12-14 18:19:14 +00:00
QFETCH ( QString , channel ) ;
QFETCH ( QString , channelUrl ) ;
QFETCH ( int , currentBuild ) ;
QFETCH ( QList < QVariant > , result ) ;
2014-01-05 22:24:05 +00:00
MMC - > settings ( ) - > set ( " UpdateChannel " , channel ) ;
2014-04-05 21:58:47 +01:00
BuildConfig . VERSION_BUILD = currentBuild ;
2013-12-14 18:19:14 +00:00
UpdateChecker checker ;
checker . setChannelListUrl ( channelUrl ) ;
QSignalSpy updateAvailableSpy ( & checker , SIGNAL ( updateAvailable ( QString , QString , int ) ) ) ;
QVERIFY ( updateAvailableSpy . isValid ( ) ) ;
QSignalSpy channelListLoadedSpy ( & checker , SIGNAL ( channelListLoaded ( ) ) ) ;
QVERIFY ( channelListLoadedSpy . isValid ( ) ) ;
2014-07-13 23:57:54 +01:00
checker . updateChanList ( false ) ;
2013-12-14 18:19:14 +00:00
QVERIFY ( channelListLoadedSpy . wait ( ) ) ;
checker . m_channels [ 0 ] . url = QUrl : : fromLocalFile ( QDir : : current ( ) . absoluteFilePath ( " tests/data/ " ) ) . toString ( ) ;
2013-12-24 21:00:07 +00:00
checker . checkForUpdate ( false ) ;
2013-12-14 18:19:14 +00:00
QVERIFY ( updateAvailableSpy . wait ( ) ) ;
QList < QVariant > res = result ;
res [ 0 ] = checker . m_channels [ 0 ] . url ;
QCOMPARE ( updateAvailableSpy . first ( ) , res ) ;
}
2013-12-14 13:53:18 +00:00
} ;
2013-12-14 15:02:51 +00:00
QTEST_GUILESS_MAIN_MULTIMC ( UpdateCheckerTest )
2013-12-14 13:53:18 +00:00
# include "tst_UpdateChecker.moc"