aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/unit++/gui.h
blob: 5a90ca389236a1abdb406722ac5e481e81b1daa5 (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
#ifndef __UNITPP_GUI_H
#define __UNITPP_GUI_H
#ifdef GUI
#include "tester.h"
#include <exception>
#include <vector>
#include <qwidget.h>
#include <qcolor.h>
#include <qframe.h>
#include <qlabel.h>
#include <qlistview.h>
#include <qprogressbar.h>
#include <qhbox.h>
#include <qvbox.h>
#include <qpixmap.h>
#include <qpushbutton.h>
#include <qapplication.h>

/// \name unitpp
namespace unitpp {
/// A colored count with a unit.
class cnt_item : public QHBox
{
	Q_OBJECT
private:
	int v;
	QLabel* val;
	QLabel* label;
public:
	cnt_item(QWidget* par, const QString& txt, const QColor& col = black,
			const char* nam = 0);
public slots:
	void value(int v);
	void inc();
};

/// A line with total, ok, fail, and error counts.
class cnt_line : public QHBox
{
	Q_OBJECT
private:
	enum fields { id_max, id_ok, id_fail, id_error, n_id };
	QLabel* label;
	cnt_item* cnts[n_id];
public slots:
	void max(int v);
	void reset();
	void inc_ok();
	void inc_fail();
	void inc_error();
public:
	cnt_line(const QString& txt, QWidget* par = 0, const char* name = 0);
};

/// A cnt_line stacked with a progress bar.
class res_stack : public QVBox
{
	Q_OBJECT
private:
	cnt_line* cnts;
	QProgressBar* bar;
	void inc_progress(bool red);
public slots:
	void max(int max);
	void reset();
	void inc_ok();
	void inc_fail();
	void inc_error();
public:
	res_stack(const QString& txt, QWidget* par=0, const char* name=0);
};
class node;
/// The whole GUI box with test tree, results, and buttons.
class gui : public QVBox
{
	Q_OBJECT
public:
	gui(QApplication& app, QWidget* par = 0, const char* name = 0);
	virtual ~gui();
	QListView* test_tree() { return tree; }
	void add_test(node* n);
	void add_suite(node* n);
	void processEvents(int t);
signals:
	void run();
	void stop();
public slots:
	void totSuites(int v);
	void totTests(int v);
	void reset();
private slots:
	void run_pressed() { emit run(); }
	void stop_pressed() { emit stop(); }
private:
	void nconnect(node* node, res_stack*);
	QApplication& app;
	QListView* tree;
	res_stack* suites;
	res_stack* tests;
	QPushButton* b_run;
	QPushButton* b_stop;
	QPushButton* b_quit;
};
class suite_node;
// a Qt error prevents this from being a ListViewItem...
/**
 * A node in the test tree. An error in Qt prevents this to be derived from
 * QListViewItem, hence the separation.
 */
class node : public QObject
{
	Q_OBJECT
public:
	enum state { none, is_ok, is_fail, is_error };
	/// Create this node under par.
	node(suite_node* par, test&);
	/// Get the associated QListViewItem.
	QListViewItem* lvi() { return item; }
	///
	state status() { return st; }
signals:
	/// [signal] emitted when the test succedes
	void ok();
	/// [signal] emitted when the test fails
	void fail();
	/// [signal] emitted when the test throws an exception
	void error();
public slots:
	/// [slot] Make the test run, and emit appropriate signals.
	virtual void run();
protected:
	/// Make a top level test, directly under the gui.
	node(gui* par, test&);
	/// Set the status of the node, including update of the displayed icon.
	void status(state s) {
		st = s;
		setImg();
	}
private:
	void show_error(assertion_error& e);
	void show_error(const char*);
	QListViewItem* item;
	test& t;
	state st;
	void setImg();
};
/**
 * A specialized node representing a test suite.
 */
class suite_node : public node
{
	typedef std::vector<node*> cctyp;
	cctyp cc; // child container
public:
	/// Inner suite creation.
	suite_node(suite_node* par, suite&);
	/// Top level suite_node.
	suite_node(gui* par, suite&);
	/// Test.
	virtual void run();
	/// Register a node below this.
	void add_child(node* n) { cc.push_back(n); }
};
}
#endif
#endif