~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to CppUnit/WinTestRunner/src/TestRunnerDlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// TestRunnerDlg.cpp
 
3
//
 
4
// $Id: //poco/1.2/CppUnit/WinTestRunner/src/TestRunnerDlg.cpp#1 $
 
5
//
 
6
 
 
7
 
 
8
#include <afxwin.h>
 
9
#include <afxext.h>
 
10
#include <afxcmn.h>
 
11
#include <mmsystem.h>
 
12
#include "TestRunnerDlg.h"
 
13
#include "ActiveTest.h"
 
14
#include "GUITestResult.h"
 
15
#include "ProgressBar.h"
 
16
#include "CppUnit/TestSuite.h"
 
17
#include "TestRunnerDlg.h"
 
18
 
 
19
 
 
20
namespace CppUnit {
 
21
 
 
22
 
 
23
TestRunnerDlg::TestRunnerDlg(CWnd* pParent): CDialog(TestRunnerDlg::IDD, pParent)
 
24
{
 
25
    //{{AFX_DATA_INIT(TestRunnerDlg)
 
26
        // NOTE: the ClassWizard will add member initialization here
 
27
    //}}AFX_DATA_INIT
 
28
 
 
29
    _testsProgress     = 0;
 
30
    _selectedTest      = 0;
 
31
}
 
32
 
 
33
 
 
34
void TestRunnerDlg::DoDataExchange(CDataExchange* pDX)
 
35
{
 
36
        CDialog::DoDataExchange(pDX);
 
37
        //{{AFX_DATA_MAP(TestRunnerDlg)
 
38
        // NOTE: the ClassWizard will add DDX and DDV calls here
 
39
        //}}AFX_DATA_MAP
 
40
}
 
41
 
 
42
 
 
43
BEGIN_MESSAGE_MAP(TestRunnerDlg, CDialog)
 
44
    //{{AFX_MSG_MAP(TestRunnerDlg)
 
45
    ON_BN_CLICKED(ID_RUN, OnRun)
 
46
    ON_BN_CLICKED(ID_STOP, OnStop)
 
47
    ON_CBN_SELCHANGE(IDC_COMBO_TEST, OnSelchangeComboTest)
 
48
        ON_BN_CLICKED(IDC_CHK_AUTORUN, OnBnClickedAutorun)
 
49
    ON_WM_PAINT()
 
50
    //}}AFX_MSG_MAP
 
51
END_MESSAGE_MAP()
 
52
 
 
53
 
 
54
BOOL TestRunnerDlg::OnInitDialog()
 
55
{
 
56
        CDialog::OnInitDialog();
 
57
 
 
58
        CListCtrl   *listCtrl = (CListCtrl *)GetDlgItem (IDC_LIST);
 
59
        CComboBox   *comboBox = (CComboBox *)GetDlgItem (IDC_COMBO_TEST);
 
60
 
 
61
        ASSERT (listCtrl);
 
62
        ASSERT (comboBox);
 
63
 
 
64
        CString title;
 
65
        GetWindowText(title);
 
66
#if defined(_DEBUG)
 
67
        title.Append(" [debug]");
 
68
#else
 
69
        title.Append(" [release]");
 
70
#endif
 
71
        SetWindowText(title);
 
72
 
 
73
        listCtrl->InsertColumn (0,"Type", LVCFMT_LEFT, 16 + listCtrl->GetStringWidth ("Type"), 1);
 
74
        listCtrl->InsertColumn (1,"Name", LVCFMT_LEFT, 16 * listCtrl->GetStringWidth ("X"), 2);
 
75
        listCtrl->InsertColumn (2,"Failed Condition", LVCFMT_LEFT, 24 * listCtrl->GetStringWidth ("M"), 3);
 
76
        listCtrl->InsertColumn (3,"Line", LVCFMT_LEFT, 16 + listCtrl->GetStringWidth ("0000"), 4);
 
77
        listCtrl->InsertColumn (4,"File Name", LVCFMT_LEFT, 36 * listCtrl->GetStringWidth ("M"), 5);
 
78
 
 
79
        int numberOfCases = 0;
 
80
 
 
81
        CWinApp* pApp = AfxGetApp();
 
82
        CString lastTestCS = pApp->GetProfileString("Tests", "lastTest");
 
83
        std::string lastTest((LPCSTR) lastTestCS);
 
84
        int sel = -1;
 
85
        for (std::vector<TestInfo>::iterator it = _tests.begin (); it != _tests.end (); ++it)
 
86
        {
 
87
                std::string cbName(it->level*4, ' ');
 
88
                cbName.append(it->pTest->toString());
 
89
                comboBox->AddString (cbName.c_str ());
 
90
                if (sel < 0)
 
91
                {
 
92
                        if (lastTest.empty() || lastTest == it->pTest->toString())
 
93
                        {
 
94
                                _selectedTest = it->pTest;
 
95
                                sel = numberOfCases;
 
96
                        }
 
97
                }
 
98
                numberOfCases++;
 
99
        }
 
100
 
 
101
        if (numberOfCases > 0)
 
102
        {
 
103
                if (sel < 0)
 
104
                {
 
105
                        _selectedTest = _tests[0].pTest;
 
106
                        sel = 0;
 
107
                }
 
108
                comboBox->SetCurSel (sel);
 
109
        }
 
110
        else
 
111
        {
 
112
                beRunDisabled ();
 
113
        }
 
114
        CWnd *pProgress = GetDlgItem(IDC_PROGRESS);
 
115
        CRect rect;
 
116
        pProgress->GetWindowRect(&rect);
 
117
        _testsProgress = new ProgressBar (this, rect);
 
118
 
 
119
        CButton* autoRunBtn = (CButton*) GetDlgItem(IDC_CHK_AUTORUN);
 
120
        autoRunBtn->SetCheck(pApp->GetProfileInt("Tests", "autoRun", BST_UNCHECKED));
 
121
 
 
122
        reset ();
 
123
 
 
124
        if (autoRunBtn->GetCheck() == BST_CHECKED)
 
125
        {
 
126
                OnRun();
 
127
        }
 
128
 
 
129
        return TRUE;  // return TRUE unless you set the focus to a control
 
130
                                        // EXCEPTION: OCX Property Pages should return FALSE
 
131
}
 
132
 
 
133
 
 
134
TestRunnerDlg::~TestRunnerDlg ()
 
135
{
 
136
    freeState ();
 
137
    delete _testsProgress;
 
138
}
 
139
 
 
140
 
 
141
void TestRunnerDlg::OnRun()
 
142
{
 
143
        if (_selectedTest == 0)
 
144
                return;
 
145
 
 
146
    freeState       ();
 
147
    reset           ();
 
148
 
 
149
    beRunning       ();
 
150
 
 
151
    int numberOfTests = _selectedTest->countTestCases ();
 
152
 
 
153
    _testsProgress->start (numberOfTests);
 
154
 
 
155
    _result            = new GUITestResult ((TestRunnerDlg *)this);
 
156
    _activeTest        = new ActiveTest (_selectedTest);
 
157
 
 
158
    _testStartTime     = timeGetTime ();
 
159
 
 
160
    _activeTest->run (_result);
 
161
 
 
162
    _testEndTime       = timeGetTime ();
 
163
 
 
164
}
 
165
 
 
166
 
 
167
void TestRunnerDlg::addListEntry(const std::string& type, TestResult *result, Test *test, CppUnitException *e)
 
168
{
 
169
    char        stage [80];
 
170
    LV_ITEM     lvi;
 
171
    CListCtrl   *listCtrl       = (CListCtrl *)GetDlgItem (IDC_LIST);
 
172
    int         currentEntry    = result->testErrors () + result->testFailures () -1;
 
173
 
 
174
    sprintf (stage, "%s", type.c_str ());
 
175
 
 
176
    lvi.mask        = LVIF_TEXT;
 
177
    lvi.iItem       = currentEntry;
 
178
    lvi.iSubItem    = 0;
 
179
    lvi.pszText     = stage;
 
180
    lvi.iImage      = 0;
 
181
    lvi.stateMask   = 0;
 
182
    lvi.state       = 0;
 
183
 
 
184
    listCtrl->InsertItem (&lvi);
 
185
 
 
186
    // Set class string
 
187
    listCtrl->SetItemText (currentEntry, 1, test->toString ().c_str ());
 
188
 
 
189
    // Set the asserted text
 
190
    listCtrl->SetItemText(currentEntry, 2, e->what ());
 
191
 
 
192
    // Set the line number
 
193
    if (e->lineNumber () == CppUnitException::CPPUNIT_UNKNOWNLINENUMBER)
 
194
        sprintf (stage, "<unknown>");
 
195
    else
 
196
        sprintf (stage, "%ld", e->lineNumber ());
 
197
 
 
198
    listCtrl->SetItemText(currentEntry, 3, stage);
 
199
 
 
200
    // Set the file name
 
201
    listCtrl->SetItemText(currentEntry, 4, e->fileName ().c_str ());
 
202
 
 
203
    listCtrl->RedrawItems (currentEntry, currentEntry);
 
204
    listCtrl->UpdateWindow ();
 
205
 
 
206
}
 
207
 
 
208
 
 
209
void TestRunnerDlg::addError (TestResult *result, Test *test, CppUnitException *e)
 
210
{
 
211
    addListEntry ("Error", result, test, e);
 
212
    _errors++;
 
213
 
 
214
    updateCountsDisplay ();
 
215
 
 
216
}
 
217
 
 
218
 
 
219
void TestRunnerDlg::addFailure (TestResult *result, Test *test, CppUnitException *e)
 
220
{
 
221
    addListEntry ("Failure", result, test, e);
 
222
    _failures++;
 
223
 
 
224
    updateCountsDisplay ();
 
225
 
 
226
}
 
227
 
 
228
 
 
229
void TestRunnerDlg::endTest (TestResult *result, Test *test)
 
230
{
 
231
        if (_selectedTest == 0)
 
232
                return;
 
233
 
 
234
    _testsRun++;
 
235
    updateCountsDisplay ();
 
236
    _testsProgress->step (_failures == 0 && _errors == 0);
 
237
 
 
238
    _testEndTime   = timeGetTime ();
 
239
 
 
240
    updateCountsDisplay ();
 
241
 
 
242
    if (_testsRun >= _selectedTest->countTestCases ())
 
243
        beIdle ();
 
244
}
 
245
 
 
246
 
 
247
void TestRunnerDlg::beRunning ()
 
248
{
 
249
    CButton *runButton = (CButton *)GetDlgItem (ID_RUN);
 
250
    CButton *closeButton = (CButton *)GetDlgItem (IDOK);
 
251
 
 
252
    runButton->EnableWindow (FALSE);
 
253
    closeButton->EnableWindow (FALSE);
 
254
 
 
255
}
 
256
 
 
257
 
 
258
void TestRunnerDlg::beIdle ()
 
259
{
 
260
    CButton *runButton = (CButton *)GetDlgItem (ID_RUN);
 
261
    CButton *closeButton = (CButton *)GetDlgItem (IDOK);
 
262
 
 
263
    runButton->EnableWindow (TRUE);
 
264
    closeButton->EnableWindow (TRUE);
 
265
 
 
266
}
 
267
 
 
268
 
 
269
void TestRunnerDlg::beRunDisabled ()
 
270
{
 
271
    CButton *runButton = (CButton *)GetDlgItem (ID_RUN);
 
272
    CButton *closeButton = (CButton *)GetDlgItem (IDOK);
 
273
    CButton *stopButton = (CButton *)GetDlgItem (ID_STOP);
 
274
 
 
275
    runButton->EnableWindow (FALSE);
 
276
    stopButton->EnableWindow (FALSE);
 
277
    closeButton->EnableWindow (TRUE);
 
278
 
 
279
}
 
280
 
 
281
 
 
282
void TestRunnerDlg::freeState ()
 
283
{
 
284
    delete _activeTest;
 
285
    delete _result;
 
286
 
 
287
}
 
288
 
 
289
 
 
290
void TestRunnerDlg::reset ()
 
291
{
 
292
    _testsRun      = 0;
 
293
    _errors        = 0;
 
294
    _failures      = 0;
 
295
    _testEndTime   = _testStartTime;
 
296
 
 
297
    updateCountsDisplay ();
 
298
 
 
299
    _activeTest    = 0;
 
300
    _result        = 0;
 
301
 
 
302
    CListCtrl *listCtrl = (CListCtrl *)GetDlgItem (IDC_LIST);
 
303
 
 
304
    listCtrl->DeleteAllItems ();
 
305
    _testsProgress->reset ();
 
306
 
 
307
}
 
308
 
 
309
 
 
310
void TestRunnerDlg::updateCountsDisplay ()
 
311
{
 
312
    CStatic *statTestsRun   = (CStatic *)GetDlgItem (IDC_STATIC_RUNS);
 
313
    CStatic *statErrors     = (CStatic *)GetDlgItem (IDC_STATIC_ERRORS);
 
314
    CStatic *statFailures   = (CStatic *)GetDlgItem (IDC_STATIC_FAILURES);
 
315
    CEdit *editTime         = (CEdit *)GetDlgItem (IDC_EDIT_TIME);
 
316
 
 
317
    CString argumentString;
 
318
 
 
319
    argumentString.Format ("%d", _testsRun);
 
320
    statTestsRun    ->SetWindowText (argumentString);
 
321
 
 
322
    argumentString.Format ("%d", _errors);
 
323
    statErrors      ->SetWindowText (argumentString);
 
324
 
 
325
    argumentString.Format ("%d", _failures);
 
326
    statFailures    ->SetWindowText (argumentString);
 
327
 
 
328
    argumentString.Format ("Execution time: %3.3lf seconds", (_testEndTime - _testStartTime) / 1000.0);
 
329
    editTime        ->SetWindowText (argumentString);
 
330
 
 
331
 
 
332
}
 
333
 
 
334
 
 
335
void TestRunnerDlg::OnStop()
 
336
{
 
337
    if (_result)
 
338
        _result->stop ();
 
339
 
 
340
    beIdle ();
 
341
 
 
342
}
 
343
 
 
344
 
 
345
void TestRunnerDlg::OnOK()
 
346
{
 
347
    if (_result)
 
348
        _result->stop ();
 
349
 
 
350
    CDialog::OnOK ();
 
351
}
 
352
 
 
353
 
 
354
void TestRunnerDlg::OnSelchangeComboTest()
 
355
{
 
356
    CComboBox   *testsSelection = (CComboBox *)GetDlgItem (IDC_COMBO_TEST);
 
357
 
 
358
    int currentSelection = testsSelection->GetCurSel ();
 
359
 
 
360
    if (currentSelection >= 0 && currentSelection < _tests.size ())
 
361
    {
 
362
        _selectedTest = (_tests.begin () + currentSelection)->pTest;
 
363
        beIdle ();
 
364
                CWinApp* pApp = AfxGetApp();
 
365
                pApp->WriteProfileString("Tests", "lastTest", _selectedTest->toString().c_str());
 
366
    }
 
367
    else
 
368
    {
 
369
        _selectedTest = 0;
 
370
        beRunDisabled ();
 
371
 
 
372
    }
 
373
 
 
374
    freeState ();
 
375
    reset ();
 
376
 
 
377
}
 
378
 
 
379
 
 
380
void TestRunnerDlg::OnBnClickedAutorun()
 
381
{
 
382
    CButton   *autoRunBtn = (CButton *)GetDlgItem (IDC_CHK_AUTORUN);
 
383
        CWinApp* pApp = AfxGetApp();
 
384
        pApp->WriteProfileInt("Tests", "autoRun", autoRunBtn->GetCheck());
 
385
}
 
386
 
 
387
 
 
388
void TestRunnerDlg::OnPaint()
 
389
{
 
390
    CPaintDC dc (this);
 
391
 
 
392
    _testsProgress->paint (dc);
 
393
}
 
394
 
 
395
 
 
396
void TestRunnerDlg::setTests(const std::vector<Test*>& tests)
 
397
{
 
398
        _tests.clear();
 
399
        for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
 
400
        {
 
401
                addTest(*it, 0);
 
402
        }
 
403
}
 
404
 
 
405
 
 
406
void TestRunnerDlg::addTest(Test* pTest, int level)
 
407
{
 
408
        TestInfo ti;
 
409
        ti.pTest = pTest;
 
410
        ti.level = level;
 
411
        _tests.push_back(ti);
 
412
        TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest);
 
413
        if (pSuite)
 
414
        {
 
415
                const std::vector<Test*>& tests = pSuite->tests();
 
416
                for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
 
417
                {
 
418
                        addTest(*it, level + 1);
 
419
                }
 
420
        }
 
421
}
 
422
 
 
423
 
 
424
} // namespace CppUnit