Engauge Digitizer 2
Loading...
Searching...
No Matches
TestGuidelines.cpp
Go to the documentation of this file.
1#include "CmdAddPointAxis.h"
2#include "CmdDelete.h"
3#include "CmdGong.h"
4#include "GuidelineAbstract.h"
5#include "Guidelines.h"
6#include "Logger.h"
7#include "MainWindow.h"
8#include <QApplication>
9#include <QSettings>
10#include <QTextStream>
11#include <QThread>
12#include <QtTest/QtTest>
13#include "Settings.h"
14#include "Test/TestGuidelines.h"
15
16using namespace std;
17
18const int NUMBER_TESTS = 3;
19
20QTEST_MAIN (TestGuidelines)
21
22// TestGuidelines::Result
23TestGuidelines::Result::Result (bool pass,
24 const QString &problem) :
25 m_pass (pass),
26 m_problem (problem)
27{
28}
29
30bool TestGuidelines::Result::pass () const
31{
32 return m_pass;
33}
34
35QString TestGuidelines::Result::problem () const
36{
37 return m_problem;
38}
39
40// TestGuidelines
42 QObject(parent),
43 m_mainWindow (nullptr)
44{
45}
46
47void TestGuidelines::cleanupTestCase ()
48{
49}
50
51TestGuidelines::Result TestGuidelines::compareExpectedAndGot (const QVector<int> &countsExpectedXT,
52 const QVector<int> &countsExpectedYR)
53{
54 Guidelines &guidelines = m_mainWindow->guidelines();
55 const GuidelineContainerPrivate &containerXT = guidelines.guidelineContainerPrivateXT ();
56 const GuidelineContainerPrivate &containerYR = guidelines.guidelineContainerPrivateYR ();
57
58 GuidelineContainerPrivate::const_iterator itr;
59
60 QVector<int> countsGotXT (NUM_GUIDELINE_STATES);
61 for (itr = containerXT.begin(); itr != containerXT.end(); itr++) {
62 const GuidelineAbstract *guideline = *itr;
63
64 GuidelineState state = guidelineStateFromString (guideline->stateName ());
65 countsGotXT [state] += 1;
66 }
67
68 QVector<int> countsGotYR (NUM_GUIDELINE_STATES);
69 for (itr = containerYR.begin(); itr != containerYR.end(); itr++) {
70 const GuidelineAbstract *guideline = *itr;
71
72 GuidelineState state = guidelineStateFromString (guideline->stateName ());
73 countsGotYR [state] += 1;
74 }
75
76 // Compare expected and got counts
77 bool success = true;
78 for (int state = 0; state < NUM_GUIDELINE_STATES; state++) {
79
80 // We look for a difference, except for the discarded state which is not important
81 if (countsExpectedXT [state] != countsGotXT [state] ||
82 countsExpectedYR [state] != countsGotYR [state]) {
83 if (state != GUIDELINE_STATE_DISCARDED) {
84
85 success = false;
86 break;
87 }
88 }
89 }
90
91 // Debug
92 if (success) {
93
94 return Result (true,
95 "");
96
97 } else {
98
99 QString text;
100 QTextStream str (&text);
101 str << "Expected/got=";
102 for (int state = 0; state < NUM_GUIDELINE_STATES; state++) {
103 if ((countsExpectedXT [state] != 0) ||
104 (countsExpectedYR [state] != 0) ||
105 (countsGotXT [state] != 0) ||
106 (countsGotYR [state] != 0)) {
107
108 str << guidelineStateAsString (static_cast<GuidelineState> (state)) << "=";
109 if (state == GUIDELINE_STATE_DISCARDED) {
110 str << "ARBITRARY";
111 } else {
112 str << countsExpectedXT [state];
113 }
114 str << "/" << countsGotXT [state] << " and ";
115 if (state == GUIDELINE_STATE_DISCARDED) {
116 str << "ARBITRARY";
117 } else {
118 str << countsExpectedYR [state];
119 }
120 str << "/" << countsGotYR [state] << " ";
121 }
122 }
123
124 return Result (false,
125 text);
126 }
127}
128
129GuidelineState TestGuidelines::guidelineStateFromString (const QString &string) const
130{
131 for (int i = 0; i < NUM_GUIDELINE_STATES; i++) {
132 GuidelineState state = static_cast<GuidelineState> (i);
133 if (string == guidelineStateAsString (state)) {
134 return state;
135 }
136 }
137
139}
140
141void TestGuidelines::initTestCase ()
142{
143 turnOffChecklist ();
144
145 // Start MainWindow
146 const bool NO_DROP_REGRESSION = false;
147 const QString NO_ERROR_REPORT_LOG_FILE;
148 const QString NO_REGRESSION_OPEN_FILE;
149 const bool NO_GNUPLOT_LOG_FILES = false;
150 const bool NO_REGRESSION_IMPORT = false;
151 const bool NO_RESET = false;
152 const bool NO_EXPORT_ONLY = false;
153 const bool NO_EXPORT_IMAGE_ONLY = false;
154 const QString NO_EXPORT_IMAGE_EXTENSION;
155 const bool DEBUG_FLAG = false;
156 QStringList importFile;
157 const QStringList NO_COMMAND_LINE;
158
159 importFile << "../samples/inverse.jpg";
160
161 initializeLogging ("engauge_test",
162 "engauge_test.log",
163 DEBUG_FLAG);
164
165 m_mainWindow = new MainWindow (NO_ERROR_REPORT_LOG_FILE,
170 NO_RESET,
172 NO_EXPORT_IMAGE_ONLY,
173 NO_EXPORT_IMAGE_EXTENSION,
174 importFile,
176
177 m_mainWindow->show ();
178
179 test00StartupWithoutTransformationPrepare ();
180
181 // We cannot return until every test has finished, since as soon as this
182 // finishes, QtTest will start calling the private slots
183 while (m_results.size () < NUMBER_TESTS) {
184 qApp->processEvents ();
185 }
186}
187
189{
190 // Expected and got counts
191 QVector<int> countsExpectedXT (NUM_GUIDELINE_STATES), countsExpectedYR (NUM_GUIDELINE_STATES);
192
193 m_results.push_back (compareExpectedAndGot (countsExpectedXT,
194 countsExpectedYR));
195
196 // Connect to next test here
197 test01AfterAddingTransformationPrepare ();
198}
199
200void TestGuidelines::test00StartupWithoutTransformationPrepare ()
201{
202 const int FIVE_SECONDS = 5000;
203
204 // Setup for first test in the chain. We use a timer to give the gui time to
205 // start up
206 connect (&m_showTimer, SIGNAL (timeout ()),
207 this, SLOT (test00StartupWithoutTransformation ()));
208
209 m_showTimer.setSingleShot (true);
210 m_showTimer.start (FIVE_SECONDS);
211}
212
213void TestGuidelines::test00StartupWithoutTransformationReport ()
214{
215 // If there is no result for this test then NUMBER_TESTS is off
216 const Result &result = m_results.front ();
217 if (!result.pass ()) {
218 cout << result.problem().toLatin1().data() << endl;
219 }
220
221 bool pass = result.pass ();
222 m_results.pop_front ();
223
224 QVERIFY (pass);
225}
226
228{
229 // Expected and got counts
230 QVector<int> countsExpectedXT (NUM_GUIDELINE_STATES), countsExpectedYR (NUM_GUIDELINE_STATES);
231
232 m_results.push_back (compareExpectedAndGot (countsExpectedXT,
233 countsExpectedYR));
234
235 // Connect to next test here
236 test02AfterRemovingTransformationPrepare ();
237}
238
239void TestGuidelines::test01AfterAddingTransformationPrepare ()
240{
241 // Setup for next test in the chain
242 disconnect (m_mainWindow, SIGNAL (signalGong ()),
243 this, SLOT (test00StartupWithoutTransformation ()));
244 connect (m_mainWindow, SIGNAL (signalGong ()),
245 this, SLOT (test01AfterAddingTransformation ()));
246
247 QPointF posScreen0 (400, 400);
248 QPointF posScreen1 (600, 400);
249 QPointF posScreen2 (400, 200);
250 QPointF posGraph0 (40, 40);
251 QPointF posGraph1 (60, 40);
252 QPointF posGraph2 (40, 20);
253 CmdAddPointAxis *cmd0 = new CmdAddPointAxis (*m_mainWindow,
254 m_mainWindow->cmdMediator()->document(),
255 posScreen0,
256 posGraph0,
257 0.0,
258 false);
259 CmdAddPointAxis *cmd1 = new CmdAddPointAxis (*m_mainWindow,
260 m_mainWindow->cmdMediator()->document(),
261 posScreen1,
262 posGraph1,
263 1.0,
264 false);
265 CmdAddPointAxis *cmd2 = new CmdAddPointAxis (*m_mainWindow,
266 m_mainWindow->cmdMediator()->document(),
267 posScreen2,
268 posGraph2,
269 2.0,
270 false);
271 CmdGong *cmd3 = new CmdGong (*m_mainWindow,
272 m_mainWindow->cmdMediator()->document());
273
274 m_mainWindow->cmdMediator()->push (cmd0);
275 m_mainWindow->cmdMediator()->push (cmd1);
276 m_mainWindow->cmdMediator()->push (cmd2);
277 m_mainWindow->cmdMediator()->push (cmd3);
278}
279
280void TestGuidelines::test01AfterAddingTransformationReport ()
281{
282 // If there is no result for this test then NUMBER_TESTS is off
283 const Result &result = m_results.front ();
284 if (!result.pass ()) {
285 cout << result.problem().toLatin1().data() << endl;
286 }
287
288 bool pass = result.pass ();
289 m_results.pop_front ();
290
291 QVERIFY (pass);
292}
293
295{
296 // Expected and got counts
297 QVector<int> countsExpectedXT (NUM_GUIDELINE_STATES), countsExpectedYR (NUM_GUIDELINE_STATES);
298
299 m_results.push_back (compareExpectedAndGot (countsExpectedXT,
300 countsExpectedYR));
301
302 // Connect to next test here
303 //test03... ();
304}
305
306void TestGuidelines::test02AfterRemovingTransformationPrepare ()
307{
308 // Setup for next test in the chain
309 disconnect (m_mainWindow, SIGNAL (signalGong ()),
310 this, SLOT (test01AfterAddingTransformation ()));
311 connect (m_mainWindow, SIGNAL (signalGong ()),
312 this, SLOT (test02AfterRemovingTransformation ()));
313
314 const QString POINT2_IDENTIFIER ("Axes\tpoint\t2");
315 QStringList pointsToDelete;
316 pointsToDelete << POINT2_IDENTIFIER;
317
318 CmdDelete *cmd0 = new CmdDelete (*m_mainWindow,
319 m_mainWindow->cmdMediator()->document(),
320 pointsToDelete);
321 CmdGong *cmd1 = new CmdGong (*m_mainWindow,
322 m_mainWindow->cmdMediator()->document());
323
324 m_mainWindow->cmdMediator()->push (cmd0);
325 m_mainWindow->cmdMediator()->push (cmd1);
326}
327
328void TestGuidelines::test02AfterRemovingTransformationReport ()
329{
330 // If there is no result for this test then NUMBER_TESTS is off
331 const Result &result = m_results.front ();
332 if (!result.pass ()) {
333 cout << result.problem().toLatin1().data() << endl;
334 }
335
336 bool pass = result.pass ();
337 m_results.pop_front ();
338
339 QVERIFY (pass);
340}
341
342void TestGuidelines::turnOffChecklist ()
343{
344 // Turn off checklist
345 QSettings settings (SETTINGS_ENGAUGE, SETTINGS_DIGITIZER);
346
347 settings.beginGroup (SETTINGS_GROUP_MAIN_WINDOW);
348 settings.setValue (SETTINGS_CHECKLIST_GUIDE_WIZARD, false);
349 settings.endGroup ();
350}
QString guidelineStateAsString(GuidelineState state)
GuidelineState
Set of possible Guideline states. See class Guideline for more information.
@ GUIDELINE_STATE_DISCARDED
@ NUM_GUIDELINE_STATES
QList< GuidelineAbstract * > GuidelineContainerPrivate
Definition Guidelines.h:24
void initializeLogging(const QString &name, const QString &filename, bool isDebug)
Definition Logger.cpp:21
const QString SETTINGS_ENGAUGE
const QString SETTINGS_CHECKLIST_GUIDE_WIZARD
const QString SETTINGS_GROUP_MAIN_WINDOW
const QString SETTINGS_DIGITIZER
const bool NO_EXPORT_ONLY
const QStringList NO_COMMAND_LINE
const QString NO_ERROR_REPORT_LOG_FILE
const bool NO_GNUPLOT_LOG_FILES
const QString NO_REGRESSION_OPEN_FILE
const bool NO_REGRESSION_IMPORT
const bool NO_DROP_REGRESSION
const bool DEBUG_FLAG
const int NUMBER_TESTS
Command for adding one axis point.
Command for deleting all selected Points.
Definition CmdDelete.h:19
Command for sending a satisfying signal to the unit test framework to indicate completion of queued c...
Definition CmdGong.h:16
Document & document()
Provide the Document to commands, primarily for undo/redo processing.
QString stateName() const
Current state name for debugging and unit test only.
CmdMediator * cmdMediator()
Accessor for commands to process the Document.
Unit test of guidelines.
void test02AfterRemovingTransformation()
void test01AfterAddingTransformation()
void test00StartupWithoutTransformation()
TestGuidelines(QObject *parent=0)
Single constructor.