|
| 1 | +#include "notebook_app.hpp" |
| 2 | + |
| 3 | +#include <QtTest/QtTest> |
1 | 4 | #include <QTest>
|
| 5 | +#include <QWidget> |
| 6 | +#include <QtWidgets> |
| 7 | +#include <QDebug> |
| 8 | + |
| 9 | +#include <QPlainTextEdit> |
| 10 | +#include <QKeyEvent> |
| 11 | + |
| 12 | +#include <QLayout> |
| 13 | +#include <QGraphicsLayout> |
| 14 | +#include <QGraphicsScene> |
| 15 | +#include <QGraphicsView> |
| 16 | + |
| 17 | +#include <QGraphicsItem> |
| 18 | +#include <QGraphicsTextItem> |
| 19 | +#include <QGraphicsEllipseItem> |
| 20 | +#include <QGraphicsLineItem> |
| 21 | +#include <QRegularExpression> |
2 | 22 |
|
3 |
| -class NotebookTest : public QObject { |
| 23 | +class NotebookTest : public QObject |
| 24 | +{ |
4 | 25 | Q_OBJECT
|
5 | 26 |
|
6 | 27 | private slots:
|
7 | 28 |
|
8 | 29 | void initTestCase();
|
| 30 | + void findInputWidget(); |
| 31 | + void findOutputWidget(); |
9 | 32 |
|
10 | 33 | // TODO: implement additional tests here
|
| 34 | + //void testFindByName(); |
| 35 | + //void testFindByType(); |
| 36 | + //void testFindByRegExp(); |
| 37 | + void testDiscretePlotLayout(); |
| 38 | + |
| 39 | +private: |
| 40 | + NotebookApp notebookApp; |
| 41 | + InputWidget * inputWidget; |
| 42 | + OutputWidget * outputWidget; |
| 43 | +}; |
| 44 | + |
| 45 | +void NotebookTest::initTestCase() |
| 46 | +{ |
| 47 | + notebookApp.show(); |
| 48 | +} |
| 49 | + |
| 50 | +void NotebookTest::findInputWidget() |
| 51 | +{ |
| 52 | + inputWidget = notebookApp.findChild<InputWidget *>("input"); |
| 53 | + QVERIFY2(inputWidget, "Could not find widget with name: 'input'"); |
| 54 | +} |
| 55 | + |
| 56 | +void NotebookTest::findOutputWidget() |
| 57 | +{ |
| 58 | + outputWidget = notebookApp.findChild<OutputWidget *>("output"); |
| 59 | + QVERIFY2(outputWidget, "Could not find widget with name: 'output'"); |
| 60 | +} |
| 61 | + |
| 62 | +//void NotebookTest::testFindByName(){ |
| 63 | +// auto op = notebookApp.findChild<QCheckBox *>("Option 1 CheckBox"); |
| 64 | +// |
| 65 | +// QVERIFY2(op, "Could not find widget with name: 'Option 1 CheckBox'"); |
| 66 | +//} |
| 67 | +// |
| 68 | +//void NotebookTest::testFindByType(){ // Like Java: getOneIntersectingObject |
| 69 | +// auto d = notebookApp.findChild<QDial *>(); |
| 70 | +// |
| 71 | +// QVERIFY2(d, "Could not find widget with type: QDial"); |
| 72 | +//} |
| 73 | +// |
| 74 | +//void NotebookTest::testFindByRegExp(){ // Like Java: getIntersectingObjects |
| 75 | +// auto children = |
| 76 | +// notebookApp.findChildren<QCheckBox *>(QRegularExpression("^Option")); |
| 77 | +// |
| 78 | +// QVERIFY2(children.size() == 3, "Could not find all three option checkboxes"); |
| 79 | +//} |
| 80 | + |
| 81 | +/*********************************\ |
| 82 | +| Instructor's Example Tests: | |
| 83 | +\*********************************/ |
| 84 | +/* |
| 85 | +findLines - find lines in a scene contained within a bounding box |
| 86 | + with a small margin |
| 87 | + */ |
| 88 | +int findLines(QGraphicsScene * scene, QRectF bbox, qreal margin){ |
| 89 | + |
| 90 | + QPainterPath selectPath; |
| 91 | + |
| 92 | + QMarginsF margins(margin, margin, margin, margin); |
| 93 | + selectPath.addRect(bbox.marginsAdded(margins)); |
| 94 | + scene->setSelectionArea(selectPath, Qt::ContainsItemShape); |
11 | 95 |
|
| 96 | + int numlines(0); |
| 97 | + for (auto item : scene->selectedItems()){ |
| 98 | + if(item->type() == QGraphicsLineItem::Type){ |
| 99 | + numlines += 1; |
| 100 | + } |
| 101 | + } |
12 | 102 |
|
| 103 | + return numlines; |
13 | 104 | };
|
14 | 105 |
|
15 |
| -void NotebookTest::initTestCase(){ |
| 106 | +/* |
| 107 | +findPoints - find points in a scene contained within a specified rectangle |
| 108 | + */ |
| 109 | +int findPoints(QGraphicsScene * scene, QPointF center, qreal radius){ |
| 110 | + |
| 111 | + QPainterPath selectPath; |
| 112 | + selectPath.addRect(QRectF(center.x()-radius, center.y()-radius, 2*radius, 2*radius)); |
| 113 | + scene->setSelectionArea(selectPath, Qt::ContainsItemShape); |
| 114 | + |
| 115 | + int numpoints(0); |
| 116 | + for (auto item : scene->selectedItems()){ |
| 117 | + if(item->type() == QGraphicsEllipseItem::Type){ |
| 118 | + numpoints += 1; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return numpoints; |
| 123 | +}; |
| 124 | + |
| 125 | +/* |
| 126 | +findText - find text in a scene centered at a specified point with a given |
| 127 | + rotation and string contents |
| 128 | + */ |
| 129 | +int findText(QGraphicsScene * scene, QPointF center, qreal rotation, QString contents){ |
| 130 | + |
| 131 | + int numtext(0); |
| 132 | + for (auto item : scene->items(center)){ |
| 133 | + if(item->type() == QGraphicsTextItem::Type){ |
| 134 | + QGraphicsTextItem * text = static_cast<QGraphicsTextItem *>(item); |
| 135 | + if((text->toPlainText() == contents) && |
| 136 | + (text->rotation() == rotation) && |
| 137 | + (text->pos() + text->boundingRect().center() == center)) |
| 138 | + { |
| 139 | + numtext += 1; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return numtext; |
| 145 | +}; |
| 146 | + |
| 147 | +/* |
| 148 | +intersectsLine - find lines in a scene that intersect a specified rectangle |
| 149 | + */ |
| 150 | +int intersectsLine(QGraphicsScene * scene, QPointF center, qreal radius){ |
| 151 | + |
| 152 | + QPainterPath selectPath; |
| 153 | + selectPath.addRect(QRectF(center.x()-radius, center.y()-radius, 2*radius, 2*radius)); |
| 154 | + scene->setSelectionArea(selectPath, Qt::IntersectsItemShape); |
| 155 | + |
| 156 | + int numlines(0); |
| 157 | + for (auto item : scene->selectedItems()){ |
| 158 | + if(item->type() == QGraphicsLineItem::Type){ |
| 159 | + numlines += 1; |
| 160 | + } |
| 161 | + } |
16 | 162 |
|
| 163 | + return numlines; |
| 164 | +}; |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | +void NotebookTest::testDiscretePlotLayout() |
| 169 | +{ |
| 170 | + QVERIFY(true); |
| 171 | + |
| 172 | + std::string program = R"( |
| 173 | + (discrete-plot (list (list -1 -1) (list 1 1)) |
| 174 | + (list (list "title" "The Title") |
| 175 | + (list "abscissa-label" "X Label") |
| 176 | + (list "ordinate-label" "Y Label") )) |
| 177 | + )"; |
| 178 | + |
| 179 | + inputWidget->setPlainText(QString::fromStdString(program)); |
| 180 | + QTest::keyClick(inputWidget, Qt::Key_Return, Qt::ShiftModifier); |
| 181 | + |
| 182 | + auto view = outputWidget->findChild<QGraphicsView *>(); |
| 183 | + QVERIFY2(view, "Could not find QGraphicsView as child of OutputWidget"); |
| 184 | + |
| 185 | + auto scene = view->scene(); |
| 186 | + |
| 187 | + // first check total number of items |
| 188 | + // 8 lines + 2 points + 7 text = 17 |
| 189 | + auto items = scene->items(); |
| 190 | + QCOMPARE(items.size(), 17); |
| 191 | + |
| 192 | + // make them all selectable |
| 193 | + for (auto item : items){ |
| 194 | + item->setFlag(QGraphicsItem::ItemIsSelectable); |
| 195 | + } |
| 196 | + |
| 197 | + double scalex = 20.0/2.0; |
| 198 | + double scaley = 20.0/2.0; |
| 199 | + |
| 200 | + double xmin = scalex * (-1); |
| 201 | + double xmax = scalex * ( 1); |
| 202 | + double ymin = scaley * (-1); |
| 203 | + double ymax = scaley * ( 1); |
| 204 | + double xmiddle = (xmax + xmin) / 2; |
| 205 | + double ymiddle = (ymax + ymin) / 2; |
| 206 | + |
| 207 | + // check title |
| 208 | + QCOMPARE(findText(scene, QPointF(xmiddle, -(ymax+3)), 0, QString("The Title")), 1); |
| 209 | + |
| 210 | + // check abscissa label |
| 211 | + QCOMPARE(findText(scene, QPointF(xmiddle, -(ymin-3)), 0, QString("X Label")), 1); |
| 212 | + |
| 213 | + // check ordinate label |
| 214 | + QCOMPARE(findText(scene, QPointF(xmin-3, -ymiddle), -90, QString("Y Label")), 1); |
| 215 | + |
| 216 | + // check abscissa min label |
| 217 | + QCOMPARE(findText(scene, QPointF(xmin, -(ymin-2)), 0, QString("-1")), 1); |
| 218 | + |
| 219 | + // check abscissa max label |
| 220 | + QCOMPARE(findText(scene, QPointF(xmax, -(ymin-2)), 0, QString("1")), 1); |
| 221 | + |
| 222 | + // check ordinate min label |
| 223 | + QCOMPARE(findText(scene, QPointF(xmin-2, -ymin), 0, QString("-1")), 1); |
| 224 | + |
| 225 | + // check ordinate max label |
| 226 | + QCOMPARE(findText(scene, QPointF(xmin-2, -ymax), 0, QString("1")), 1); |
| 227 | + |
| 228 | + // check the bounding box bottom |
| 229 | + QCOMPARE(findLines(scene, QRectF(xmin, -ymin, 20, 0), 0.1), 1); |
| 230 | + |
| 231 | + // check the bounding box top |
| 232 | + QCOMPARE(findLines(scene, QRectF(xmin, -ymax, 20, 0), 0.1), 1); |
| 233 | + |
| 234 | + // check the bounding box left and (-1, -1) stem |
| 235 | + QCOMPARE(findLines(scene, QRectF(xmin, -ymax, 0, 20), 0.1), 2); |
| 236 | + |
| 237 | + // check the bounding box right and (1, 1) stem |
| 238 | + QCOMPARE(findLines(scene, QRectF(xmax, -ymax, 0, 20), 0.1), 2); |
| 239 | + |
| 240 | + // check the abscissa axis |
| 241 | + QCOMPARE(findLines(scene, QRectF(xmin, 0, 20, 0), 0.1), 1); |
| 242 | + |
| 243 | + // check the ordinate axis |
| 244 | + QCOMPARE(findLines(scene, QRectF(0, -ymax, 0, 20), 0.1), 1); |
| 245 | + |
| 246 | + // check the point at (-1,-1) |
| 247 | + QCOMPARE(findPoints(scene, QPointF(-10, 10), 0.6), 1); |
| 248 | + |
| 249 | + // check the point at (1,1) |
| 250 | + QCOMPARE(findPoints(scene, QPointF(10, -10), 0.6), 1); |
17 | 251 | }
|
18 | 252 |
|
19 | 253 |
|
|
0 commit comments