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
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 )
{
2018-07-15 18:21:05 +05:30
qDebug ( ) < < e1 . url < < " vs " < < e2 . url ;
return e1 . id = = e2 . id & &
e1 . name = = e2 . name & &
e1 . description = = e2 . description & &
e1 . url = = e2 . url ;
2013-12-14 20:32:51 +05:30
}
2013-12-14 19:23:18 +05:30
2013-12-17 00:40:11 +05:30
QDebug operator < < ( QDebug dbg , const UpdateChecker : : ChannelListEntry & c )
{
2018-07-15 18:21:05 +05:30
dbg . nospace ( ) < < " ChannelListEntry(id= " < < c . id < < " name= " < < c . name < < " description= " < < c . description < < " url= " < < c . url < < " ) " ;
return dbg . maybeSpace ( ) ;
2013-12-17 00:40:11 +05:30
}
2018-11-26 07:52:20 +05:30
QString findTestDataUrl ( const char * file )
{
return QUrl : : fromLocalFile ( QFINDTESTDATA ( file ) ) . toString ( ) ;
}
2013-12-14 19:23:18 +05:30
class UpdateCheckerTest : public QObject
{
2018-07-15 18:21:05 +05:30
Q_OBJECT
2013-12-14 19:23:18 +05:30
private
slots :
2018-07-15 18:21:05 +05:30
void initTestCase ( )
{
}
void cleanupTestCase ( )
{
}
void tst_ChannelListParsing_data ( )
{
QTest : : addColumn < QString > ( " channel " ) ;
QTest : : addColumn < QString > ( " channelUrl " ) ;
QTest : : addColumn < bool > ( " hasChannels " ) ;
QTest : : addColumn < bool > ( " valid " ) ;
QTest : : addColumn < QList < UpdateChecker : : ChannelListEntry > > ( " result " ) ;
QTest : : newRow ( " garbage " )
< < QString ( )
2022-06-12 05:16:30 +05:30
< < findTestDataUrl ( " testdata/garbageChannels.json " )
2018-07-15 18:21:05 +05:30
< < false
< < false
< < QList < UpdateChecker : : ChannelListEntry > ( ) ;
QTest : : newRow ( " errors " )
< < QString ( )
2022-06-12 05:16:30 +05:30
< < findTestDataUrl ( " testdata/errorChannels.json " )
2018-07-15 18:21:05 +05:30
< < false
< < true
< < QList < UpdateChecker : : ChannelListEntry > ( ) ;
QTest : : newRow ( " no channels " )
< < QString ( )
2022-06-12 05:16:30 +05:30
< < findTestDataUrl ( " testdata/noChannels.json " )
2018-07-15 18:21:05 +05:30
< < false
< < true
< < QList < UpdateChecker : : ChannelListEntry > ( ) ;
QTest : : newRow ( " one channel " )
< < QString ( " develop " )
2022-06-12 05:16:30 +05:30
< < findTestDataUrl ( " testdata/oneChannel.json " )
2018-07-15 18:21:05 +05:30
< < true
< < true
< < ( QList < UpdateChecker : : ChannelListEntry > ( ) < < UpdateChecker : : ChannelListEntry { " develop " , " Develop " , " The channel called \" develop \" " , " http://example.org/stuff " } ) ;
QTest : : newRow ( " several channels " )
< < QString ( " develop " )
2022-06-12 05:16:30 +05:30
< < findTestDataUrl ( " testdata/channels.json " )
2018-07-15 18:21:05 +05:30
< < true
< < true
< < ( QList < UpdateChecker : : ChannelListEntry > ( )
2022-06-12 05:16:30 +05:30
< < UpdateChecker : : ChannelListEntry { " develop " , " Develop " , " The channel called \" develop \" " , findTestDataUrl ( " testdata " ) }
< < UpdateChecker : : ChannelListEntry { " stable " , " Stable " , " It's stable at least " , findTestDataUrl ( " testdata " ) }
2018-07-15 18:21:05 +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 ( )
{
QFETCH ( QString , channel ) ;
QFETCH ( QString , channelUrl ) ;
QFETCH ( bool , hasChannels ) ;
QFETCH ( bool , valid ) ;
QFETCH ( QList < UpdateChecker : : ChannelListEntry > , result ) ;
2021-11-22 03:51:12 +05:30
shared_qobject_ptr < QNetworkAccessManager > nam = new QNetworkAccessManager ( ) ;
UpdateChecker checker ( nam , channelUrl , channel , 0 ) ;
2018-07-15 18:21:05 +05:30
QSignalSpy channelListLoadedSpy ( & checker , SIGNAL ( channelListLoaded ( ) ) ) ;
QVERIFY ( channelListLoadedSpy . isValid ( ) ) ;
checker . updateChanList ( false ) ;
if ( valid )
{
QVERIFY ( channelListLoadedSpy . wait ( ) ) ;
QCOMPARE ( channelListLoadedSpy . size ( ) , 1 ) ;
}
else
{
channelListLoadedSpy . wait ( ) ;
QCOMPARE ( channelListLoadedSpy . size ( ) , 0 ) ;
}
QCOMPARE ( checker . hasChannels ( ) , hasChannels ) ;
QCOMPARE ( checker . getChannelList ( ) , result ) ;
}
void tst_UpdateChecking ( )
{
QString channel = " develop " ;
2022-06-12 05:16:30 +05:30
QString channelUrl = findTestDataUrl ( " testdata/channels.json " ) ;
2018-07-15 18:21:05 +05:30
int currentBuild = 2 ;
2021-11-22 03:51:12 +05:30
shared_qobject_ptr < QNetworkAccessManager > nam = new QNetworkAccessManager ( ) ;
UpdateChecker checker ( nam , channelUrl , channel , currentBuild ) ;
2018-07-15 18:21:05 +05:30
QSignalSpy updateAvailableSpy ( & checker , SIGNAL ( updateAvailable ( GoUpdate : : Status ) ) ) ;
QVERIFY ( updateAvailableSpy . isValid ( ) ) ;
QSignalSpy channelListLoadedSpy ( & checker , SIGNAL ( channelListLoaded ( ) ) ) ;
QVERIFY ( channelListLoadedSpy . isValid ( ) ) ;
checker . updateChanList ( false ) ;
QVERIFY ( channelListLoadedSpy . wait ( ) ) ;
qDebug ( ) < < " CWD: " < < QDir : : current ( ) . absolutePath ( ) ;
2022-06-12 05:16:30 +05:30
checker . m_channels [ 0 ] . url = findTestDataUrl ( " testdata/ " ) ;
2018-07-15 18:21:05 +05:30
checker . checkForUpdate ( channel , false ) ;
QVERIFY ( updateAvailableSpy . wait ( ) ) ;
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 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
2016-04-14 04:53:54 +05:30
# include "UpdateChecker_test.moc"