-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_gui.cpp
197 lines (150 loc) · 5.7 KB
/
test_gui.cpp
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
#include <QDebug>
#include <QtTest/QtTest>
#include <QtWidgets>
#include "canvas_widget.hpp"
#include "main_window.hpp"
#include "message_widget.hpp"
#include "repl_widget.hpp"
// ADD YOUR TESTS TO THIS CLASS !!!!!!!
class TestGUI : public QObject {
Q_OBJECT
public:
private slots:
void initTestCase();
void testREPLGood();
void testREPLBad();
void testREPLBad2Good();
void testPoint();
void testLine();
void testArc();
void testEnvRestore();
private:
MainWindow w;
REPLWidget *repl;
QLineEdit *replEdit;
MessageWidget *message;
QLineEdit *messageEdit;
CanvasWidget *canvas;
QGraphicsScene *scene;
};
void TestGUI::initTestCase() {
repl = w.findChild<REPLWidget *>();
QVERIFY2(repl, "Could not find REPLWidget instance in MainWindow instance.");
replEdit = repl->findChild<QLineEdit *>();
QVERIFY2(replEdit,
"Could not find QLineEdit instance in REPLWidget instance.");
message = w.findChild<MessageWidget *>();
QVERIFY2(message,
"Could not find MessageWidget instance in MainWindow instance.");
messageEdit = message->findChild<QLineEdit *>();
QVERIFY2(messageEdit,
"Could not find QLineEdit instance in MessageWidget instance.");
canvas = w.findChild<CanvasWidget *>();
QVERIFY2(canvas,
"Could not find CanvasWidget instance in MainWindow instance.");
scene = canvas->findChild<QGraphicsScene *>();
QVERIFY2(scene,
"Could not find QGraphicsScene instance in CanvasWidget instance.");
}
void TestGUI::testREPLGood() {
QVERIFY(repl && replEdit);
QVERIFY(message && messageEdit);
// send a string to the repl widget
QTest::keyClicks(replEdit, "(define a 1)");
QTest::keyClick(replEdit, Qt::Key_Return, Qt::NoModifier);
// check message
QVERIFY2(messageEdit->isReadOnly(),
"Expected QLineEdit inside MessageWidget to be read-only.");
QCOMPARE(messageEdit->text(), QString("(1)"));
}
void TestGUI::testREPLBad() {
QVERIFY(repl && replEdit);
QVERIFY(message && messageEdit);
// send a string to the repl widget
QTest::keyClicks(replEdit, "(foo)");
QTest::keyClick(replEdit, Qt::Key_Return, Qt::NoModifier);
// check message
QVERIFY2(messageEdit->isReadOnly(),
"Expected QLineEdit inside MessageWidget to be read-only.");
QVERIFY2(messageEdit->text().startsWith("Error"), "Expected error message.");
// check background color and selection
QPalette p = messageEdit->palette();
QCOMPARE(p.highlight().color(), QColor(Qt::red));
QVERIFY2(messageEdit->selectedText().startsWith("Error"),
"Expected error to be selected.");
}
void TestGUI::testREPLBad2Good() {
QVERIFY(repl && replEdit);
QVERIFY(message && messageEdit);
// send a string to the repl widget
QTest::keyClicks(replEdit, "(foo)");
QTest::keyClick(replEdit, Qt::Key_Return, Qt::NoModifier);
// check message
QVERIFY2(messageEdit->isReadOnly(),
"Expected QLineEdit inside MessageWidget to be read-only.");
QVERIFY2(messageEdit->text().startsWith("Error"), "Expected error message.");
// check background color and selection
QPalette p = messageEdit->palette();
QCOMPARE(p.highlight().color(), QColor(Qt::red));
QVERIFY2(messageEdit->selectedText().startsWith("Error"),
"Expected error to be selected.");
// send a string to the repl widget
QTest::keyClicks(replEdit, "(define value 100)");
QTest::keyClick(replEdit, Qt::Key_Return, Qt::NoModifier);
// check message
QVERIFY2(messageEdit->isReadOnly(),
"Expected QLineEdit inside MessageWidget to be read-only.");
QCOMPARE(messageEdit->text(), QString("(100)"));
// check background color and selection
p = messageEdit->palette();
QVERIFY2(p.highlight().color() != QColor(Qt::red),
"Did not expect red highlight on successful eval.");
QVERIFY2(messageEdit->selectedText() == "",
"Expected no selcted text on successful eval.");
}
void TestGUI::testPoint() {
QVERIFY(repl && replEdit);
QVERIFY(canvas && scene);
// send a string to the repl widget
QTest::keyClicks(replEdit, "(draw (point 0 0))");
QTest::keyClick(replEdit, Qt::Key_Return, Qt::NoModifier);
// check canvas
QVERIFY2(scene->itemAt(QPointF(0, 0), QTransform()) != 0,
"Expected a point in the scene. Not found.");
}
void TestGUI::testLine() {
QVERIFY(repl && replEdit);
QVERIFY(canvas && scene);
// send a string to the repl widget
QTest::keyClicks(replEdit, "(draw (line (point 10 0) (point 0 10)))");
QTest::keyClick(replEdit, Qt::Key_Return, Qt::NoModifier);
// check canvas
QVERIFY2(scene->itemAt(QPointF(10, 0), QTransform()) != 0,
"Expected a line in the scene. Not found.");
QVERIFY2(scene->itemAt(QPointF(0, 10), QTransform()) != 0,
"Expected a line in the scene. Not found.");
}
void TestGUI::testArc() {
QVERIFY(repl && replEdit);
QVERIFY(canvas && scene);
// send a string to the repl widget
QTest::keyClicks(replEdit, "(draw (arc (point 0 0) (point 100 0) pi))");
QTest::keyClick(replEdit, Qt::Key_Return, Qt::NoModifier);
// check canvas
QVERIFY2(scene->itemAt(QPointF(100, 0), QTransform()) != 0,
"Expected a point on the arc in the scene. Not found.");
QVERIFY2(scene->itemAt(QPointF(-100, 0), QTransform()) != 0,
"Expected a point on the arc in the scene. Not found.");
}
void TestGUI::testEnvRestore() {
QVERIFY(repl && replEdit);
QVERIFY(canvas && scene);
// send a string to the repl widget
QTest::keyClicks(replEdit, "(begin (draw (point -20 0)) (define pi 3))");
QTest::keyClick(replEdit, Qt::Key_Return, Qt::NoModifier);
// check canvas
QVERIFY2(scene->itemAt(QPointF(-20, 0), QTransform()) == 0,
"Did not expected a point in the scene. One found.");
}
QTEST_MAIN(TestGUI)
#include "test_gui.moc"