summaryrefslogtreecommitdiff
path: root/cxxtest/QtGui.h
blob: 889825106ab5f49ef35923670abf8074474eb894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#ifndef __cxxtest__QtGui_h__
#define __cxxtest__QtGui_h__

//
// The QtGui displays a simple progress bar using the Qt Toolkit.  It
// has been tested with versions 2.x and 3.x.
// 
// Apart from normal Qt command-line arguments, it accepts the following options:
//   -minimized    Start minimized, pop up on error
//   -keep         Don't close the window at the end
//   -title TITLE  Set the window caption
//
// If both are -minimized and -keep specified, GUI will only keep the
// window if it's in focus.
//

#include <cxxtest/Gui.h>

#include <qapplication.h>
#include <qglobal.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qmessagebox.h>
#include <qpixmap.h>
#include <qprogressbar.h>
#include <qstatusbar.h>

namespace CxxTest
{
    class QtGui : public GuiListener
    {
    public:
        void enterGui( int &argc, char **argv )
        {
            parseCommandLine( argc, argv );
            createApplication( argc, argv );
        }

        void enterWorld( const WorldDescription &wd )
        {
            createWindow( wd );
            processEvents();
        }

        void guiEnterSuite( const char *suiteName )
        {
            showSuiteName( suiteName );
        }

        void guiEnterTest( const char *suiteName, const char *testName )
        {
            setCaption( suiteName, testName );
            advanceProgressBar();
            showTestName( testName );
            showTestsDone( _progressBar->progress() );
            processEvents();
        }

        void yellowBar()
        {
            setColor( 255, 255, 0 );
            setIcon( QMessageBox::Warning );
            getTotalTests();
            processEvents();
        }
        
        void redBar()
        {
            if ( _startMinimized && _mainWindow->isMinimized() )
                showNormal();
            setColor( 255, 0, 0 );
            setIcon( QMessageBox::Critical );
            getTotalTests();
            processEvents();
        }

        void leaveGui()
        {
            if ( keep() ) {
                showSummary();
                _application->exec();
            }
            else
                _mainWindow->close( true );
        }

    private:
        QString _title;
        bool _startMinimized, _keep;
        unsigned _numTotalTests;
        QString _strTotalTests;
        QApplication *_application;
        QWidget *_mainWindow;
        QVBoxLayout *_layout;
        QProgressBar *_progressBar;
        QStatusBar *_statusBar;
        QLabel *_suiteName, *_testName, *_testsDone;

        void parseCommandLine( int argc, char **argv )
        {
            _startMinimized = _keep = false;
            _title = argv[0];
            
            for ( int i = 1; i < argc; ++ i ) {
                QString arg( argv[i] );
                if ( arg == "-minimized" )
                    _startMinimized = true;
                else if ( arg == "-keep" )
                    _keep = true;
                else if ( arg == "-title" && (i + 1 < argc) )
                    _title = argv[++i];
            }
        }

        void createApplication( int &argc, char **argv )
        {
            _application = new QApplication( argc, argv );
        }       
        
        void createWindow( const WorldDescription &wd )
        {
            getTotalTests( wd );            
            createMainWindow();
            createProgressBar();
            createStatusBar();
            setMainWidget();
            if ( _startMinimized )
                showMinimized();
            else
                showNormal();
        }

        void getTotalTests()
        {
            getTotalTests( tracker().world() );
        }

        void getTotalTests( const WorldDescription &wd )
        {
            _numTotalTests = wd.numTotalTests();
            char s[WorldDescription::MAX_STRLEN_TOTAL_TESTS];
            _strTotalTests = wd.strTotalTests( s );
        }

        void createMainWindow()
        {
            _mainWindow = new QWidget();
            _layout = new QVBoxLayout( _mainWindow );
        }

        void createProgressBar()
        {
            _layout->addWidget( _progressBar = new QProgressBar( _numTotalTests, _mainWindow ) );
            _progressBar->setProgress( 0 );
            setColor( 0, 255, 0 );
            setIcon( QMessageBox::Information );
        }

        void createStatusBar()
        {
            _layout->addWidget( _statusBar = new QStatusBar( _mainWindow ) );
            _statusBar->addWidget( _suiteName = new QLabel( _statusBar ), 2 );
            _statusBar->addWidget( _testName = new QLabel( _statusBar ), 4 );
            _statusBar->addWidget( _testsDone = new QLabel( _statusBar ), 1 );
        }

        void setMainWidget()
        {
            _application->setMainWidget( _mainWindow );
        }

        void showMinimized()
        {
            _mainWindow->showMinimized();
        }

        void showNormal()
        {
            _mainWindow->showNormal();
            centerWindow();
        }

        void setCaption( const QString &suiteName, const QString &testName )
        {
            _mainWindow->setCaption( _title + " - " + suiteName + "::" + testName + "()" );
        }

        void showSuiteName( const QString &suiteName )
        {
            _suiteName->setText( "class " + suiteName );
        }

        void advanceProgressBar()
        {
            _progressBar->setProgress( _progressBar->progress() + 1 );
        }

        void showTestName( const QString &testName )
        {
            _testName->setText( testName + "()" );
        }

        void showTestsDone( unsigned testsDone )
        {
            _testsDone->setText( asString( testsDone ) + " of " + _strTotalTests );
        }

        static QString asString( unsigned n )
        {
            return QString::number( n );
        }

        void setColor( int r, int g, int b )
        {
            QPalette palette = _progressBar->palette();
            palette.setColor( QColorGroup::Highlight, QColor( r, g, b ) );
            _progressBar->setPalette( palette );
        }

        void setIcon( QMessageBox::Icon icon )
        {
#if QT_VERSION >= 0x030000
            _mainWindow->setIcon( QMessageBox::standardIcon( icon ) );
#else // Qt version < 3.0.0
            _mainWindow->setIcon( QMessageBox::standardIcon( icon, QApplication::style().guiStyle() ) );
#endif // QT_VERSION
        }

        void processEvents()
        {
            _application->processEvents();
        }

        void centerWindow()
        {
            QWidget *desktop = QApplication::desktop();
            int xCenter = desktop->x() + (desktop->width() / 2);
            int yCenter = desktop->y() + (desktop->height() / 2);
            
            int windowWidth = (desktop->width() * 4) / 5;
            int windowHeight = _mainWindow->height();
            _mainWindow->setGeometry( xCenter - (windowWidth / 2), yCenter - (windowHeight / 2), windowWidth, windowHeight );
        }

        bool keep()
        {
            if ( !_keep )
                return false;
            if ( !_startMinimized )
                return true;
            return (_mainWindow == _application->activeWindow());
        }

        void showSummary()
        {
            QString summary = _strTotalTests + (_numTotalTests == 1 ? " test" : " tests");
            if ( tracker().failedTests() )
                summary = "Failed " + asString( tracker().failedTests() ) + " of " + summary;
            else
                summary = summary + " passed";

            _mainWindow->setCaption( _title + " - " + summary );

            _statusBar->removeWidget( _suiteName );
            _statusBar->removeWidget( _testName );
            _testsDone->setText( summary );
        }
    };
};

#endif // __cxxtest__QtGui_h__