-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotebook_test.cpp
224 lines (165 loc) · 6.19 KB
/
notebook_test.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
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
#include <QTest>
#include "notebook_app.hpp"
#include "input_widget.hpp"
#include "output_widget.hpp"
class NotebookTest : public QObject {
Q_OBJECT
private slots:
void initTestCase();
void testDiscretePlotLayout();
void testMakeText();
// TODO: implement additional tests here
private:
NotebookApp notebook;
};
void NotebookTest::initTestCase(){
}
/*
findLines - find lines in a scene contained within a bounding box
with a small margin
*/
int findLines(QGraphicsScene * scene, QRectF bbox, qreal margin) {
QPainterPath selectPath;
QMarginsF margins(margin, margin, margin, margin);
selectPath.addRect(bbox.marginsAdded(margins));
scene->setSelectionArea(selectPath, Qt::ContainsItemShape);
int numlines(0);
foreach(auto item, scene->selectedItems()) {
if (item->type() == QGraphicsLineItem::Type) {
numlines += 1;
}
}
return numlines;
}
/*
findPoints - find points in a scene contained within a specified rectangle
*/
int findPoints(QGraphicsScene * scene, QPointF center, qreal radius) {
QPainterPath selectPath;
selectPath.addRect(QRectF(center.x() - radius, center.y() - radius, 2 * radius, 2 * radius));
scene->setSelectionArea(selectPath, Qt::ContainsItemShape);
int numpoints(0);
foreach(auto item, scene->selectedItems()) {
if (item->type() == QGraphicsEllipseItem::Type) {
numpoints += 1;
}
}
return numpoints;
}
/*
findText - find text in a scene centered at a specified point with a given
rotation and string contents
*/
int findText(QGraphicsScene * scene, QPointF center, qreal rotation, QString contents) {
int numtext(0);
foreach(auto item, scene->items(center)) {
if (item->type() == QGraphicsTextItem::Type) {
QGraphicsTextItem * text = static_cast<QGraphicsTextItem *>(item);
if ((text->toPlainText() == contents) &&
(text->rotation() == rotation) &&
(text->pos() + text->boundingRect().center() == center)) {
numtext += 1;
}
}
}
return numtext;
}
/*
intersectsLine - find lines in a scene that intersect a specified rectangle
*/
int intersectsLine(QGraphicsScene * scene, QPointF center, qreal radius) {
QPainterPath selectPath;
selectPath.addRect(QRectF(center.x() - radius, center.y() - radius, 2 * radius, 2 * radius));
scene->setSelectionArea(selectPath, Qt::IntersectsItemShape);
int numlines(0);
foreach(auto item, scene->selectedItems()) {
if (item->type() == QGraphicsLineItem::Type) {
numlines += 1;
}
}
return numlines;
}
void NotebookTest::testDiscretePlotLayout() {
std::string program = R"(
(discrete-plot (list (list -1 -1) (list 1 1))
(list (list "title" "The Title")
(list "abscissa-label" "X Label")
(list "ordinate-label" "Y Label") ))
)";
auto inputWidget = notebook.findChild<InputWidget *>("input");
auto outputWidget = notebook.findChild<OutputWidget *>("output");
inputWidget->setPlainText(QString::fromStdString(program));
QTest::keyClick(inputWidget, Qt::Key_Return, Qt::ShiftModifier);
auto view = outputWidget->findChild<QGraphicsView *>();
QVERIFY2(view, "Could not find QGraphicsView as child of OutputWidget");
auto scene = view->scene();
// first check total number of items
// 8 lines + 2 points + 7 text = 17
auto items = scene->items();
QCOMPARE(items.size(), 17);
// make them all selectable
foreach(auto item, items) {
item->setFlag(QGraphicsItem::ItemIsSelectable);
}
double scalex = 20.0 / 2.0;
double scaley = 20.0 / 2.0;
double xmin = scalex * -1;
double xmax = scalex * 1;
double ymin = scaley * -1;
double ymax = scaley * 1;
double xmiddle = (xmax + xmin) / 2;
double ymiddle = (ymax + ymin) / 2;
// check title
QCOMPARE(findText(scene, QPointF(xmiddle, -(ymax + 3)), 0, QString("The Title")), 1);
// check abscissa label
QCOMPARE(findText(scene, QPointF(xmiddle, -(ymin - 3)), 0, QString("X Label")), 1);
// check ordinate label
QCOMPARE(findText(scene, QPointF(xmin - 3, -ymiddle), -90, QString("Y Label")), 1);
// check abscissa min label
QCOMPARE(findText(scene, QPointF(xmin, -(ymin - 2)), 0, QString("-1")), 1);
// check abscissa max label
QCOMPARE(findText(scene, QPointF(xmax, -(ymin - 2)), 0, QString("1")), 1);
// check ordinate min label
QCOMPARE(findText(scene, QPointF(xmin - 2, -ymin), 0, QString("-1")), 1);
// check ordinate max label
QCOMPARE(findText(scene, QPointF(xmin - 2, -ymax), 0, QString("1")), 1);
// check the bounding box bottom
QCOMPARE(findLines(scene, QRectF(xmin, -ymin, 20, 0), 0.1), 1);
// check the bounding box top
QCOMPARE(findLines(scene, QRectF(xmin, -ymax, 20, 0), 0.1), 1);
// check the bounding box left and (-1, -1) stem
QCOMPARE(findLines(scene, QRectF(xmin, -ymax, 0, 20), 0.1), 2);
// check the bounding box right and (1, 1) stem
QCOMPARE(findLines(scene, QRectF(xmax, -ymax, 0, 20), 0.1), 2);
// check the abscissa axis
QCOMPARE(findLines(scene, QRectF(xmin, 0, 20, 0), 0.1), 1);
// check the ordinate axis
QCOMPARE(findLines(scene, QRectF(0, -ymax, 0, 20), 0.1), 1);
// check the point at (-1,-1)
QCOMPARE(findPoints(scene, QPointF(-10, 10), 0.6), 1);
// check the point at (1,1)
QCOMPARE(findPoints(scene, QPointF(10, -10), 0.6), 1);
}
void NotebookTest::testMakeText() {
std::string program = R"(
(define x (set-property "position" (make-point 4 7) (make-text "Hello World!")))
)";
auto inputWidget = notebook.findChild<InputWidget *>("input");
auto outputWidget = notebook.findChild<OutputWidget *>("output");
inputWidget->setPlainText(QString::fromStdString(program));
QTest::keyClick(inputWidget, Qt::Key_Return, Qt::ShiftModifier);
auto view = outputWidget->findChild<QGraphicsView *>();
QVERIFY2(view, "Could not find QGraphicsView as child of OutputWidget");
auto scene = view->scene();
// first check total number of items
// 8 lines + 2 points + 7 text = 17
auto items = scene->items();
QCOMPARE(items.size(), 1);
// make them all selectable
foreach(auto item, items) {
item->setFlag(QGraphicsItem::ItemIsSelectable);
}
QCOMPARE(findText(scene, QPointF(4, 7), 0, QString("Hello World!")), 1);
}
QTEST_MAIN(NotebookTest)
#include "notebook_test.moc"