2013-12-14 19:23:18 +05:30
# include <QTest>
2013-12-14 20:32:51 +05:30
# include <QSignalSpy>
2013-12-14 19:23:18 +05:30
# include "TestUtil.h"
2015-02-09 06:21:14 +05:30
# include "updater/UpdateChecker.h"
2013-12-14 20:32:51 +05:30
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 19:23:18 +05:30
2013-12-17 00:40:11 +05:30
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 ( ) ;
}
2013-12-14 19:23:18 +05:30
class UpdateCheckerTest : public QObject
{
Q_OBJECT
private
slots :
void initTestCase ( )
{
}
void cleanupTestCase ( )
{
}
2013-12-14 20:32:51 +05:30
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 23:49:14 +05:30
QTest : : addColumn < bool > ( " valid " ) ;
2013-12-14 20:32:51 +05:30
QTest : : addColumn < QList < UpdateChecker : : ChannelListEntry > > ( " result " ) ;
2013-12-14 23:49:14 +05:30
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 20:32:51 +05:30
QTest : : newRow ( " no channels " )
< < QString ( )
< < findTestDataUrl ( " tests/data/noChannels.json " )
< < false
2013-12-14 23:49:14 +05:30
< < true
2013-12-14 20:32:51 +05:30
< < QList < UpdateChecker : : ChannelListEntry > ( ) ;
QTest : : newRow ( " one channel " )
< < QString ( " develop " )
< < findTestDataUrl ( " tests/data/oneChannel.json " )
< < true
2013-12-14 23:49:14 +05:30
< < true
2013-12-14 20:32:51 +05:30
< < ( 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 23:49:14 +05:30
< < true
2013-12-14 20:32:51 +05:30
< < ( QList < UpdateChecker : : ChannelListEntry > ( )
2016-03-04 03:17:41 +05:30
< < UpdateChecker : : ChannelListEntry { " develop " , " Develop " , " The channel called \" develop \" " , MultiMC_TEST_DATA_URL }
< < UpdateChecker : : ChannelListEntry { " stable " , " Stable " , " It's stable at least " , MultiMC_TEST_DATA_URL }
2013-12-14 20:32:51 +05:30
< < 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-06 03:54:05 +05:30
2013-12-14 20:32:51 +05:30
QFETCH ( QString , channel ) ;
QFETCH ( QString , channelUrl ) ;
QFETCH ( bool , hasChannels ) ;
2013-12-14 23:49:14 +05:30
QFETCH ( bool , valid ) ;
2013-12-14 20:32:51 +05:30
QFETCH ( QList < UpdateChecker : : ChannelListEntry > , result ) ;
2015-02-08 22:26:14 +05:30
UpdateChecker checker ( channelUrl , channel , 0 ) ;
2013-12-14 20:32:51 +05:30
2013-12-14 23:49:14 +05:30
QSignalSpy channelListLoadedSpy ( & checker , SIGNAL ( channelListLoaded ( ) ) ) ;
QVERIFY ( channelListLoadedSpy . isValid ( ) ) ;
2013-12-14 20:32:51 +05:30
2014-07-14 04:27:54 +05:30
checker . updateChanList ( false ) ;
2013-12-14 20:32:51 +05:30
2013-12-14 23:49:14 +05:30
if ( valid )
{
QVERIFY ( channelListLoadedSpy . wait ( ) ) ;
QCOMPARE ( channelListLoadedSpy . size ( ) , 1 ) ;
}
else
{
channelListLoadedSpy . wait ( ) ;
QCOMPARE ( channelListLoadedSpy . size ( ) , 0 ) ;
}
2013-12-14 20:32:51 +05:30
QCOMPARE ( checker . hasChannels ( ) , hasChannels ) ;
QCOMPARE ( checker . getChannelList ( ) , result ) ;
}
2014-01-06 03:54:05 +05:30
2013-12-14 23:49:14 +05:30
void tst_UpdateChecking ( )
{
2015-02-08 22:26:14 +05:30
QString channel = " develop " ;
QString channelUrl = findTestDataUrl ( " tests/data/channels.json " ) ;
int currentBuild = 2 ;
2013-12-14 23:49:14 +05:30
2015-02-08 22:26:14 +05:30
UpdateChecker checker ( channelUrl , channel , currentBuild ) ;
2013-12-14 23:49:14 +05:30
2015-02-08 22:26:14 +05:30
QSignalSpy updateAvailableSpy ( & checker , SIGNAL ( updateAvailable ( GoUpdate : : Status ) ) ) ;
2013-12-14 23:49:14 +05:30
QVERIFY ( updateAvailableSpy . isValid ( ) ) ;
QSignalSpy channelListLoadedSpy ( & checker , SIGNAL ( channelListLoaded ( ) ) ) ;
QVERIFY ( channelListLoadedSpy . isValid ( ) ) ;
2014-07-14 04:27:54 +05:30
checker . updateChanList ( false ) ;
2013-12-14 23:49:14 +05:30
QVERIFY ( channelListLoadedSpy . wait ( ) ) ;
2015-01-31 23:51:47 +05:30
qDebug ( ) < < " CWD: " < < QDir : : current ( ) . absolutePath ( ) ;
checker . m_channels [ 0 ] . url = findTestDataUrl ( " tests/data/ " ) ;
2015-01-31 21:29:03 +05:30
checker . checkForUpdate ( channel , false ) ;
2013-12-14 23:49:14 +05:30
QVERIFY ( updateAvailableSpy . wait ( ) ) ;
2015-02-08 22:26:14 +05:30
auto status = updateAvailableSpy . first ( ) . first ( ) . value < GoUpdate : : Status > ( ) ;
QCOMPARE ( checker . m_channels [ 0 ] . url , status . newRepoUrl ) ;
QCOMPARE ( 3 , status . newVersionId ) ;
QCOMPARE ( currentBuild , status . currentVersionId ) ;
2013-12-14 23:49:14 +05:30
}
2013-12-14 19:23:18 +05:30
} ;
2015-01-31 23:51:47 +05:30
QTEST_GUILESS_MAIN ( UpdateCheckerTest )
2013-12-14 19:23:18 +05:30
# include "tst_UpdateChecker.moc"