11ee847caSNicolas Bonnefon #include <iostream>
21ee847caSNicolas Bonnefon
31ee847caSNicolas Bonnefon #include <QTest>
41ee847caSNicolas Bonnefon #include <QSignalSpy>
51ee847caSNicolas Bonnefon
61ee847caSNicolas Bonnefon #include "log.h"
71ee847caSNicolas Bonnefon #include "test_utils.h"
81ee847caSNicolas Bonnefon
91ee847caSNicolas Bonnefon #include "data/logdata.h"
101ee847caSNicolas Bonnefon
111ee847caSNicolas Bonnefon #include "gmock/gmock.h"
121ee847caSNicolas Bonnefon
131ee847caSNicolas Bonnefon #define TMPDIR "/tmp"
141ee847caSNicolas Bonnefon
151ee847caSNicolas Bonnefon static const qint64 SL_NB_LINES = 5000LL;
161ee847caSNicolas Bonnefon static const int SL_LINE_PER_PAGE = 70;
171ee847caSNicolas Bonnefon static const char* sl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n";
181ee847caSNicolas Bonnefon static const int SL_LINE_LENGTH = 83; // Without the final '\n' !
191ee847caSNicolas Bonnefon
201ee847caSNicolas Bonnefon static const char* partial_line_begin = "123... beginning of line.";
211ee847caSNicolas Bonnefon static const char* partial_line_end = " end of line 123.\n";
221ee847caSNicolas Bonnefon
231ee847caSNicolas Bonnefon
241ee847caSNicolas Bonnefon class LogDataChanging : public testing::Test {
251ee847caSNicolas Bonnefon public:
261ee847caSNicolas Bonnefon };
271ee847caSNicolas Bonnefon
TEST_F(LogDataChanging,changingFile)281ee847caSNicolas Bonnefon TEST_F( LogDataChanging, changingFile ) {
291ee847caSNicolas Bonnefon char newLine[90];
301ee847caSNicolas Bonnefon LogData log_data;
311ee847caSNicolas Bonnefon
321ee847caSNicolas Bonnefon SafeQSignalSpy finishedSpy( &log_data, SIGNAL( loadingFinished( LoadingStatus ) ) );
331ee847caSNicolas Bonnefon SafeQSignalSpy progressSpy( &log_data, SIGNAL( loadingProgressed( int ) ) );
341ee847caSNicolas Bonnefon SafeQSignalSpy changedSpy( &log_data,
351ee847caSNicolas Bonnefon SIGNAL( fileChanged( LogData::MonitoredFileStatus ) ) );
361ee847caSNicolas Bonnefon
371ee847caSNicolas Bonnefon // Generate a small file
381ee847caSNicolas Bonnefon QFile file( TMPDIR "/changingfile.txt" );
391ee847caSNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) {
401ee847caSNicolas Bonnefon for (int i = 0; i < 200; i++) {
411ee847caSNicolas Bonnefon snprintf(newLine, 89, sl_format, i);
421ee847caSNicolas Bonnefon file.write( newLine, qstrlen(newLine) );
431ee847caSNicolas Bonnefon }
441ee847caSNicolas Bonnefon }
451ee847caSNicolas Bonnefon file.close();
461ee847caSNicolas Bonnefon
471ee847caSNicolas Bonnefon // Start loading it
481ee847caSNicolas Bonnefon log_data.attachFile( TMPDIR "/changingfile.txt" );
491ee847caSNicolas Bonnefon
501ee847caSNicolas Bonnefon // and wait for the signal
511ee847caSNicolas Bonnefon ASSERT_TRUE( finishedSpy.safeWait() );
521ee847caSNicolas Bonnefon
531ee847caSNicolas Bonnefon // Check we have the small file
541ee847caSNicolas Bonnefon ASSERT_THAT( finishedSpy.count(), 1 );
551ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getNbLine(), 200LL );
561ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getMaxLength(), SL_LINE_LENGTH );
571ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getFileSize(), 200 * (SL_LINE_LENGTH+1LL) );
581ee847caSNicolas Bonnefon
591ee847caSNicolas Bonnefon // Add some data to it
601ee847caSNicolas Bonnefon if ( file.open( QIODevice::Append ) ) {
611ee847caSNicolas Bonnefon for (int i = 0; i < 200; i++) {
621ee847caSNicolas Bonnefon snprintf(newLine, 89, sl_format, i);
631ee847caSNicolas Bonnefon file.write( newLine, qstrlen(newLine) );
641ee847caSNicolas Bonnefon }
651ee847caSNicolas Bonnefon // To test the edge case when the final line is not complete
661ee847caSNicolas Bonnefon file.write( partial_line_begin, qstrlen( partial_line_begin ) );
671ee847caSNicolas Bonnefon }
681ee847caSNicolas Bonnefon file.close();
691ee847caSNicolas Bonnefon
701ee847caSNicolas Bonnefon // and wait for the signals
711ee847caSNicolas Bonnefon ASSERT_TRUE( finishedSpy.wait( 1000 ) );
721ee847caSNicolas Bonnefon
731ee847caSNicolas Bonnefon // Check we have a bigger file
741ee847caSNicolas Bonnefon ASSERT_THAT( changedSpy.count(), 1 );
751ee847caSNicolas Bonnefon ASSERT_THAT( finishedSpy.count(), 2 );
761ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getNbLine(), 401LL );
771ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getMaxLength(), SL_LINE_LENGTH );
781ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getFileSize(), (qint64) (400 * (SL_LINE_LENGTH+1LL)
791ee847caSNicolas Bonnefon + strlen( partial_line_begin ) ) );
801ee847caSNicolas Bonnefon
811ee847caSNicolas Bonnefon {
821ee847caSNicolas Bonnefon SafeQSignalSpy finishedSpy( &log_data, SIGNAL( loadingFinished( LoadingStatus ) ) );
831ee847caSNicolas Bonnefon
841ee847caSNicolas Bonnefon // Add a couple more lines, including the end of the unfinished one.
851ee847caSNicolas Bonnefon if ( file.open( QIODevice::Append ) ) {
861ee847caSNicolas Bonnefon file.write( partial_line_end, qstrlen( partial_line_end ) );
871ee847caSNicolas Bonnefon for (int i = 0; i < 20; i++) {
881ee847caSNicolas Bonnefon snprintf(newLine, 89, sl_format, i);
891ee847caSNicolas Bonnefon file.write( newLine, qstrlen(newLine) );
901ee847caSNicolas Bonnefon }
911ee847caSNicolas Bonnefon }
921ee847caSNicolas Bonnefon file.close();
931ee847caSNicolas Bonnefon
941ee847caSNicolas Bonnefon // and wait for the signals
951ee847caSNicolas Bonnefon ASSERT_TRUE( finishedSpy.wait( 1000 ) );
961ee847caSNicolas Bonnefon
971ee847caSNicolas Bonnefon // Check we have a bigger file
981ee847caSNicolas Bonnefon ASSERT_THAT( changedSpy.count(), 2 );
991ee847caSNicolas Bonnefon ASSERT_THAT( finishedSpy.count(), 1 );
1001ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getNbLine(), 421LL );
1011ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getMaxLength(), SL_LINE_LENGTH );
1021ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getFileSize(), (qint64) ( 420 * (SL_LINE_LENGTH+1LL)
1031ee847caSNicolas Bonnefon + strlen( partial_line_begin ) + strlen( partial_line_end ) ) );
1041ee847caSNicolas Bonnefon }
1051ee847caSNicolas Bonnefon
1061ee847caSNicolas Bonnefon {
1071ee847caSNicolas Bonnefon SafeQSignalSpy finishedSpy( &log_data, SIGNAL( loadingFinished( LoadingStatus ) ) );
1081ee847caSNicolas Bonnefon
1091ee847caSNicolas Bonnefon // Truncate the file
1101ee847caSNicolas Bonnefon QVERIFY( file.open( QIODevice::WriteOnly ) );
1111ee847caSNicolas Bonnefon file.close();
1121ee847caSNicolas Bonnefon
1131ee847caSNicolas Bonnefon // and wait for the signals
1141ee847caSNicolas Bonnefon ASSERT_TRUE( finishedSpy.safeWait() );
1151ee847caSNicolas Bonnefon
1161ee847caSNicolas Bonnefon // Check we have an empty file
1171ee847caSNicolas Bonnefon ASSERT_THAT( changedSpy.count(), 3 );
1181ee847caSNicolas Bonnefon ASSERT_THAT( finishedSpy.count(), 1 );
1191ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getNbLine(), 0LL );
1201ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getMaxLength(), 0 );
1211ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getFileSize(), 0LL );
1221ee847caSNicolas Bonnefon }
1231ee847caSNicolas Bonnefon }
1241ee847caSNicolas Bonnefon
1251ee847caSNicolas Bonnefon class LogDataBehaviour : public testing::Test {
1261ee847caSNicolas Bonnefon public:
LogDataBehaviour()1271ee847caSNicolas Bonnefon LogDataBehaviour() {
1281ee847caSNicolas Bonnefon generateDataFiles();
1291ee847caSNicolas Bonnefon }
1301ee847caSNicolas Bonnefon
generateDataFiles()1311ee847caSNicolas Bonnefon bool generateDataFiles() {
1321ee847caSNicolas Bonnefon char newLine[90];
1331ee847caSNicolas Bonnefon
1341ee847caSNicolas Bonnefon QFile file( TMPDIR "/smalllog.txt" );
1351ee847caSNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) {
1361ee847caSNicolas Bonnefon for (int i = 0; i < SL_NB_LINES; i++) {
1371ee847caSNicolas Bonnefon snprintf(newLine, 89, sl_format, i);
1381ee847caSNicolas Bonnefon file.write( newLine, qstrlen(newLine) );
1391ee847caSNicolas Bonnefon }
1401ee847caSNicolas Bonnefon }
1411ee847caSNicolas Bonnefon else {
1421ee847caSNicolas Bonnefon return false;
1431ee847caSNicolas Bonnefon }
1441ee847caSNicolas Bonnefon file.close();
1451ee847caSNicolas Bonnefon
1461ee847caSNicolas Bonnefon return true;
1471ee847caSNicolas Bonnefon }
1481ee847caSNicolas Bonnefon };
1491ee847caSNicolas Bonnefon
TEST_F(LogDataBehaviour,interruptLoadYieldsAnEmptyFile)1501ee847caSNicolas Bonnefon TEST_F( LogDataBehaviour, interruptLoadYieldsAnEmptyFile ) {
1511ee847caSNicolas Bonnefon LogData log_data;
1521ee847caSNicolas Bonnefon SafeQSignalSpy endSpy( &log_data, SIGNAL( loadingFinished( LoadingStatus ) ) );
1531ee847caSNicolas Bonnefon
1541ee847caSNicolas Bonnefon // Start loading the VBL
1551ee847caSNicolas Bonnefon log_data.attachFile( TMPDIR "/verybiglog.txt" );
1561ee847caSNicolas Bonnefon
1571ee847caSNicolas Bonnefon // Immediately interrupt the loading
1581ee847caSNicolas Bonnefon log_data.interruptLoading();
1591ee847caSNicolas Bonnefon
1601ee847caSNicolas Bonnefon ASSERT_TRUE( endSpy.safeWait( 10000 ) );
1611ee847caSNicolas Bonnefon
1621ee847caSNicolas Bonnefon // Check we have an empty file
1631ee847caSNicolas Bonnefon ASSERT_THAT( endSpy.count(), 1 );
1641ee847caSNicolas Bonnefon QList<QVariant> arguments = endSpy.takeFirst();
1651ee847caSNicolas Bonnefon ASSERT_THAT( arguments.at(0).toInt(),
1661ee847caSNicolas Bonnefon static_cast<int>( LoadingStatus::Interrupted ) );
1671ee847caSNicolas Bonnefon
1681ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getNbLine(), 0LL );
1691ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getMaxLength(), 0 );
1701ee847caSNicolas Bonnefon ASSERT_THAT( log_data.getFileSize(), 0LL );
1711ee847caSNicolas Bonnefon }
1721ee847caSNicolas Bonnefon
TEST_F(LogDataBehaviour,cannotBeReAttached)1731ee847caSNicolas Bonnefon TEST_F( LogDataBehaviour, cannotBeReAttached ) {
1741ee847caSNicolas Bonnefon LogData log_data;
1751ee847caSNicolas Bonnefon SafeQSignalSpy endSpy( &log_data, SIGNAL( loadingFinished( LoadingStatus ) ) );
1761ee847caSNicolas Bonnefon
1771ee847caSNicolas Bonnefon log_data.attachFile( TMPDIR "/smalllog.txt" );
1781ee847caSNicolas Bonnefon endSpy.safeWait( 10000 );
1791ee847caSNicolas Bonnefon
1801ee847caSNicolas Bonnefon ASSERT_THROW( log_data.attachFile( TMPDIR "/verybiglog.txt" ), CantReattachErr );
1811ee847caSNicolas Bonnefon }
182*209000a6SNicolas Bonnefon
TEST_F(LogDataBehaviour,readFunctions)183*209000a6SNicolas Bonnefon TEST_F( LogDataBehaviour, readFunctions ) {
184*209000a6SNicolas Bonnefon LogData log_data;
185*209000a6SNicolas Bonnefon SafeQSignalSpy endSpy( &log_data, SIGNAL( loadingFinished( LoadingStatus ) ) );
186*209000a6SNicolas Bonnefon
187*209000a6SNicolas Bonnefon log_data.attachFile( TMPDIR "/smalllog.txt" );
188*209000a6SNicolas Bonnefon endSpy.safeWait( 10000 );
189*209000a6SNicolas Bonnefon
190*209000a6SNicolas Bonnefon QString ref = QString::fromUtf8( "LOGDATA is a part of glogg, we are going to test it thoroughly, this is line 000012" );
191*209000a6SNicolas Bonnefon
192*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getLineString( 12 ), ref ), 0 );
193*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getExpandedLineString( 12 ), ref ), 0 );
194*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getLines( 11, 3 ).at( 1 ), ref ), 0 );
195*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getExpandedLines( 12, 2 ).at( 0 ), ref ), 0 );
196*209000a6SNicolas Bonnefon }
197*209000a6SNicolas Bonnefon
198*209000a6SNicolas Bonnefon class LogDataMultiByte : public testing::Test {
199*209000a6SNicolas Bonnefon public:
LogDataMultiByte()200*209000a6SNicolas Bonnefon LogDataMultiByte() {
201*209000a6SNicolas Bonnefon generateDataFiles();
202*209000a6SNicolas Bonnefon }
203*209000a6SNicolas Bonnefon
generateDataFiles()204*209000a6SNicolas Bonnefon bool generateDataFiles() {
205*209000a6SNicolas Bonnefon const QString text = QString::fromUtf8( u8"DOM JUAN\n\
206*209000a6SNicolas Bonnefon ou LE FESTIN DE PIERRE\n\
207*209000a6SNicolas Bonnefon COMÉDIE\n\
208*209000a6SNicolas Bonnefon 1665\n\
209*209000a6SNicolas Bonnefon Molière\n\
210*209000a6SNicolas Bonnefon \n\
211*209000a6SNicolas Bonnefon \n\
212*209000a6SNicolas Bonnefon Représentée pour la première fois le 15 février 1665 sur le Théâtre de la salle du Palais-Royal par la Troupe de Monsieur, frère unique du Roi.\n\
213*209000a6SNicolas Bonnefon PERSONNAGES\n\
214*209000a6SNicolas Bonnefon DOM JUAN, fils de Dom Louis.\n\
215*209000a6SNicolas Bonnefon SGANARELLE, valet de Dom Juan.\n\
216*209000a6SNicolas Bonnefon ELVIRE, femme de Dom Juan.\n\
217*209000a6SNicolas Bonnefon GUSMAN, écuyer d'Elvire.\n\
218*209000a6SNicolas Bonnefon DOM CARLOS, frère d'Elvire.\n\
219*209000a6SNicolas Bonnefon DOM ALFONSE, frère d'Elvire\n\
220*209000a6SNicolas Bonnefon DOM LOUIS, père de Dom Juan.\n" );
221*209000a6SNicolas Bonnefon
222*209000a6SNicolas Bonnefon QFile file_utf16le( TMPDIR "/utf16le.txt" );
223*209000a6SNicolas Bonnefon QTextCodec *codec_utf16le = QTextCodec::codecForName( "UTF16LE" );
224*209000a6SNicolas Bonnefon if ( file_utf16le.open( QIODevice::WriteOnly ) ) {
225*209000a6SNicolas Bonnefon file_utf16le.write( codec_utf16le->fromUnicode( text ) );
226*209000a6SNicolas Bonnefon }
227*209000a6SNicolas Bonnefon else {
228*209000a6SNicolas Bonnefon return false;
229*209000a6SNicolas Bonnefon }
230*209000a6SNicolas Bonnefon file_utf16le.close();
231*209000a6SNicolas Bonnefon
232*209000a6SNicolas Bonnefon QFile file_utf16be( TMPDIR "/utf16be.txt" );
233*209000a6SNicolas Bonnefon QTextCodec *codec_utf16be = QTextCodec::codecForName( "UTF16BE" );
234*209000a6SNicolas Bonnefon if ( file_utf16be.open( QIODevice::WriteOnly ) ) {
235*209000a6SNicolas Bonnefon file_utf16be.write( codec_utf16be->fromUnicode( text ) );
236*209000a6SNicolas Bonnefon }
237*209000a6SNicolas Bonnefon else {
238*209000a6SNicolas Bonnefon return false;
239*209000a6SNicolas Bonnefon }
240*209000a6SNicolas Bonnefon file_utf16be.close();
241*209000a6SNicolas Bonnefon
242*209000a6SNicolas Bonnefon return true;
243*209000a6SNicolas Bonnefon }
244*209000a6SNicolas Bonnefon };
245*209000a6SNicolas Bonnefon
TEST_F(LogDataMultiByte,readUtf16LE)246*209000a6SNicolas Bonnefon TEST_F( LogDataMultiByte, readUtf16LE ) {
247*209000a6SNicolas Bonnefon LogData log_data;
248*209000a6SNicolas Bonnefon SafeQSignalSpy endSpy( &log_data, SIGNAL( loadingFinished( LoadingStatus ) ) );
249*209000a6SNicolas Bonnefon
250*209000a6SNicolas Bonnefon log_data.attachFile( TMPDIR "/utf16le.txt" );
251*209000a6SNicolas Bonnefon endSpy.safeWait( 10000 );
252*209000a6SNicolas Bonnefon
253*209000a6SNicolas Bonnefon log_data.setDisplayEncoding( Encoding::ENCODING_UTF16LE );
254*209000a6SNicolas Bonnefon
255*209000a6SNicolas Bonnefon ASSERT_THAT( log_data.getDetectedEncoding(), EncodingSpeculator::Encoding::UTF16LE );
256*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getLineString( 3 ), QStringLiteral( "1665" ) ), 0 );
257*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getExpandedLineString( 4 ), QStringLiteral( "Molière" ) ), 0 );
258*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getLines( 11, 3 ).at( 0 ), QStringLiteral( "ELVIRE, femme de Dom Juan." ) ), 0 );
259*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getLines( 11, 3 ).at( 1 ), QStringLiteral( "GUSMAN, écuyer d'Elvire." ) ), 0 );
260*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getLines( 11, 3 ).at( 2 ), QStringLiteral( "DOM CARLOS, frère d'Elvire." ) ), 0 );
261*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getExpandedLines( 0, 3 ).at( 2 ), QStringLiteral( "COMÉDIE" ) ), 0 );
262*209000a6SNicolas Bonnefon }
263*209000a6SNicolas Bonnefon
TEST_F(LogDataMultiByte,readUtf16BE)264*209000a6SNicolas Bonnefon TEST_F( LogDataMultiByte, readUtf16BE ) {
265*209000a6SNicolas Bonnefon LogData log_data;
266*209000a6SNicolas Bonnefon SafeQSignalSpy endSpy( &log_data, SIGNAL( loadingFinished( LoadingStatus ) ) );
267*209000a6SNicolas Bonnefon
268*209000a6SNicolas Bonnefon log_data.attachFile( TMPDIR "/utf16be.txt" );
269*209000a6SNicolas Bonnefon endSpy.safeWait( 10000 );
270*209000a6SNicolas Bonnefon
271*209000a6SNicolas Bonnefon log_data.setDisplayEncoding( Encoding::ENCODING_UTF16BE );
272*209000a6SNicolas Bonnefon
273*209000a6SNicolas Bonnefon ASSERT_THAT( log_data.getDetectedEncoding(), EncodingSpeculator::Encoding::UTF16BE );
274*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getLineString( 3 ), QStringLiteral( "1665" ) ), 0 );
275*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getExpandedLineString( 4 ), QStringLiteral( "Molière" ) ), 0 );
276*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getLines( 11, 3 ).at( 0 ), QStringLiteral( "ELVIRE, femme de Dom Juan." ) ), 0 );
277*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getLines( 11, 3 ).at( 1 ), QStringLiteral( "GUSMAN, écuyer d'Elvire." ) ), 0 );
278*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getLines( 11, 3 ).at( 2 ), QStringLiteral( "DOM CARLOS, frère d'Elvire." ) ), 0 );
279*209000a6SNicolas Bonnefon ASSERT_THAT( QString::compare( log_data.getExpandedLines( 0, 3 ).at( 2 ), QStringLiteral( "COMÉDIE" ) ), 0 );
280*209000a6SNicolas Bonnefon }
281