File indexing completed on 2024-04-14 04:43:07
0001 /*************************************************************************** 0002 * Copyright (c) 2010 Rick W. Chen <stuffcorpse@archlinux.us> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU General Public License as published by * 0006 * the Free Software Foundation; either version 2 of the License, or * 0007 * (at your option) any later version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, * 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0012 * GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License * 0015 * along with this program; if not, write to the * 0016 * Free Software Foundation, Inc., * 0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 0018 ***************************************************************************/ 0019 0020 #define DEBUG_PREFIX "TestDebug" 0021 0022 #include "core/support/Debug.h" 0023 #include "config-amarok-test.h" 0024 0025 #include <QStack> 0026 #include <QTest> 0027 0028 class TestDebug : public QObject 0029 { 0030 Q_OBJECT 0031 0032 public: 0033 TestDebug() {} 0034 0035 private slots: 0036 void benchDebugBlock(); 0037 void benchDebugBlock_data(); 0038 0039 private: 0040 void work( bool debugEnabled, bool colorEnabled ); 0041 void work2( bool debugEnabled, bool colorEnabled ); 0042 0043 enum BeginOrEnd { 0044 Begin, 0045 End 0046 }; 0047 void expectMessage( const QString &message, bool debugEnabled ); 0048 void expectBeginEnd( BeginOrEnd type, const QString &message, bool debugEnabled, 0049 bool colorEnabled ); 0050 QString colorize( const QString &string, int colorIndex, bool colorEnabled ); 0051 0052 static QString m_indent; 0053 }; 0054 0055 QString TestDebug::m_indent; 0056 0057 void TestDebug::benchDebugBlock_data() 0058 { 0059 QTest::addColumn<bool>("debugEnabled"); 0060 QTest::addColumn<bool>("colorEnabled"); 0061 0062 QTest::newRow("debug with color") << true << true; 0063 QTest::newRow("debug nocolor") << true << false; 0064 QTest::newRow("nodebug with color") << false << true; 0065 QTest::newRow("nodebug nocolor") << false << false; 0066 } 0067 0068 void TestDebug::benchDebugBlock() 0069 { 0070 QFETCH(bool, debugEnabled); 0071 QFETCH(bool, colorEnabled); 0072 0073 Debug::setDebugEnabled( debugEnabled ); 0074 Debug::setColoredDebug( colorEnabled ); 0075 0076 QVERIFY( Debug::debugEnabled() == debugEnabled ); 0077 QVERIFY( Debug::debugColorEnabled() == colorEnabled ); 0078 0079 QBENCHMARK_ONCE { 0080 work( debugEnabled, colorEnabled ); 0081 } 0082 } 0083 0084 void TestDebug::work( bool debugEnabled, bool colorEnabled ) 0085 { 0086 expectBeginEnd( Begin, __PRETTY_FUNCTION__, debugEnabled, colorEnabled ); 0087 DEBUG_BLOCK 0088 expectMessage( "level 1", debugEnabled ); 0089 debug() << "level 1"; 0090 0091 for( int i = 0; i < 100; ++i ) 0092 { 0093 expectBeginEnd( Begin, __PRETTY_FUNCTION__, debugEnabled, colorEnabled ); 0094 DEBUG_BLOCK 0095 expectMessage( "level 2", debugEnabled ); 0096 debug() << "level 2"; 0097 work2( debugEnabled, colorEnabled ); 0098 expectBeginEnd( End, __PRETTY_FUNCTION__, debugEnabled, colorEnabled ); 0099 } 0100 expectBeginEnd( End, __PRETTY_FUNCTION__, debugEnabled, colorEnabled ); 0101 } 0102 0103 void TestDebug::work2( bool debugEnabled, bool colorEnabled ) 0104 { 0105 expectBeginEnd( Begin, __PRETTY_FUNCTION__, debugEnabled, colorEnabled ); 0106 DEBUG_BLOCK 0107 for( int j = 0; j < 10; ++j ) 0108 { 0109 expectMessage( "limbo", debugEnabled ); 0110 debug() << "limbo"; 0111 } 0112 expectBeginEnd( End, __PRETTY_FUNCTION__, debugEnabled, colorEnabled ); 0113 } 0114 0115 void 0116 TestDebug::expectMessage( const QString &message, bool debugEnabled ) 0117 { 0118 if( !debugEnabled ) 0119 return; 0120 QString exp = QString( "%1:%2 [%3] %4 " ).arg( "amarok", m_indent, DEBUG_PREFIX, message ); 0121 QTest::ignoreMessage( QtDebugMsg, exp.toLocal8Bit() ); 0122 } 0123 0124 void 0125 TestDebug::expectBeginEnd( TestDebug::BeginOrEnd type, const QString &message, 0126 bool debugEnabled, bool colorEnabled ) 0127 { 0128 static int colorIndex = 0; 0129 static QStack<int> colorStack; 0130 if( !debugEnabled ) 0131 return; 0132 0133 QString beginEnd; 0134 QString took; 0135 if( type == Begin ) 0136 { 0137 beginEnd = "BEGIN:"; 0138 colorStack.push( colorIndex ); 0139 colorIndex = (colorIndex + 1) % 5; 0140 } 0141 else 0142 { 0143 beginEnd = "END__:"; 0144 double duration = DEBUG_OVERRIDE_ELAPSED_TIME; 0145 took = ' ' + colorize( QString( "[Took: %1s]" ).arg( duration, 0, 'g', 2 ), 0146 colorStack.top(), colorEnabled ); 0147 m_indent.truncate( m_indent.length() - 2 ); 0148 } 0149 QString exp = QString( "%1:%2 %3 %4%5 " ).arg( "amarok", m_indent, colorize( beginEnd, 0150 colorStack.top(), colorEnabled ), message, took ); 0151 QTest::ignoreMessage( QtDebugMsg, exp.toLocal8Bit() ); 0152 if( type == Begin ) 0153 m_indent.append( " " ); 0154 else 0155 colorStack.pop(); 0156 } 0157 0158 QString 0159 TestDebug::colorize( const QString &string, int colorIndex, bool colorEnabled ) 0160 { 0161 static int colors[] = { 1, 2, 4, 5, 6 }; // from Debug.cpp 0162 if( !colorEnabled ) 0163 return string; 0164 return QString( "\x1b[00;3%1m%2\x1b[00;39m" ).arg( QString::number(colors[colorIndex]), string ); 0165 } 0166 0167 0168 QTEST_GUILESS_MAIN( TestDebug ) 0169 0170 #include "TestDebug.moc"