1 /*=============================================================================
2
3 Library: CTK
4
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics
7
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 =============================================================================*/
21
22 #include "ctkCmdLineModuleSignalTester.h"
23
24 #include <QDebug>
25
26 //-----------------------------------------------------------------------------
ctkCmdLineModuleSignalTester()27 ctkCmdLineModuleSignalTester::ctkCmdLineModuleSignalTester()
28 {
29 connect(&Watcher, SIGNAL(started()), SLOT(moduleStarted()));
30 connect(&Watcher, SIGNAL(canceled()), SLOT(moduleCanceled()));
31 connect(&Watcher, SIGNAL(finished()), SLOT(moduleFinished()));
32
33 connect(&Watcher, SIGNAL(paused()), SLOT(modulePaused()));
34 connect(&Watcher, SIGNAL(resumed()), SLOT(moduleResumed()));
35
36 connect(&Watcher, SIGNAL(resultReadyAt(int)), SLOT(resultReadyAt(int)));
37 connect(&Watcher, SIGNAL(resultsReadyAt(int,int)), SLOT(resultReadyAt(int,int)));
38
39 connect(&Watcher, SIGNAL(progressRangeChanged(int,int)), SLOT(progressRangeChanged(int,int)));
40 connect(&Watcher, SIGNAL(progressTextChanged(QString)), SLOT(progressTextChanged(QString)));
41 connect(&Watcher, SIGNAL(progressValueChanged(int)), SLOT(progressValueChanged(int)));
42
43 connect(&Watcher, SIGNAL(outputDataReady()), SLOT(outputDataReady()));
44 connect(&Watcher, SIGNAL(errorDataReady()), SLOT(errorDataReady()));
45 }
46
47 //-----------------------------------------------------------------------------
setFuture(const ctkCmdLineModuleFuture & future)48 void ctkCmdLineModuleSignalTester::setFuture(const ctkCmdLineModuleFuture &future)
49 {
50 this->Watcher.setFuture(future);
51 }
52
53 //-----------------------------------------------------------------------------
watcher()54 ctkCmdLineModuleFutureWatcher *ctkCmdLineModuleSignalTester::watcher()
55 {
56 return &this->Watcher;
57 }
58
59 //-----------------------------------------------------------------------------
moduleStarted()60 void ctkCmdLineModuleSignalTester::moduleStarted()
61 {
62 Events.push_back("module.started");
63 }
64
65 //-----------------------------------------------------------------------------
moduleFinished()66 void ctkCmdLineModuleSignalTester::moduleFinished()
67 {
68 Events.push_back("module.finished");
69 }
70
71 //-----------------------------------------------------------------------------
moduleProgressValueChanged(int progress)72 void ctkCmdLineModuleSignalTester::moduleProgressValueChanged(int progress)
73 {
74 Events.push_back(QString("module.progressValueChanged(%1)").arg(progress));
75 }
76
77 //-----------------------------------------------------------------------------
moduleProgressTextChanged(const QString & text)78 void ctkCmdLineModuleSignalTester::moduleProgressTextChanged(const QString& text)
79 {
80 Events.push_back(QString("module.progressTextChanged(\"%1\")").arg(text));
81 }
82
83 //-----------------------------------------------------------------------------
modulePaused()84 void ctkCmdLineModuleSignalTester::modulePaused()
85 {
86 Events.push_back("module.paused");
87 }
88
89 //-----------------------------------------------------------------------------
moduleResumed()90 void ctkCmdLineModuleSignalTester::moduleResumed()
91 {
92 Events.push_back("module.resumed");
93 }
94
95 //-----------------------------------------------------------------------------
moduleCanceled()96 void ctkCmdLineModuleSignalTester::moduleCanceled()
97 {
98 Events.push_back("module.canceled");
99 }
100
101 //-----------------------------------------------------------------------------
resultReadyAt(int resultIndex)102 void ctkCmdLineModuleSignalTester::resultReadyAt(int resultIndex)
103 {
104 Events.push_back(QString("module.resultReadyAt(%1)").arg(resultIndex));
105 Results.push_back(Watcher.resultAt(resultIndex));
106 }
107
108 //-----------------------------------------------------------------------------
resultReadyAt(int beginIndex,int endIndex)109 void ctkCmdLineModuleSignalTester::resultReadyAt(int beginIndex, int endIndex)
110 {
111 Events.push_back(QString("module.resultReadyAt(%1,%2)").arg(beginIndex).arg(endIndex));
112 }
113
114 //-----------------------------------------------------------------------------
progressRangeChanged(int minimum,int maximum)115 void ctkCmdLineModuleSignalTester::progressRangeChanged(int minimum, int maximum)
116 {
117 Events.push_back(QString("module.progressRangeChanged(%1,%2)").arg(minimum).arg(maximum));
118 }
119
120 //-----------------------------------------------------------------------------
progressValueChanged(int progressValue)121 void ctkCmdLineModuleSignalTester::progressValueChanged(int progressValue)
122 {
123 Events.push_back(QString("module.progressValueChanged(%1)").arg(progressValue));
124 }
125
126 //-----------------------------------------------------------------------------
progressTextChanged(const QString & progressText)127 void ctkCmdLineModuleSignalTester::progressTextChanged(const QString &progressText)
128 {
129 Events.push_back(QString("module.progressTextChanged(%1)").arg(progressText));
130 }
131
132 //-----------------------------------------------------------------------------
outputDataReady()133 void ctkCmdLineModuleSignalTester::outputDataReady()
134 {
135 Events.push_back("module.outputReady");
136 }
137
138 //-----------------------------------------------------------------------------
errorDataReady()139 void ctkCmdLineModuleSignalTester::errorDataReady()
140 {
141 Events.push_back("module.errorReady");
142 }
143
144 //-----------------------------------------------------------------------------
checkSignals(const QList<QString> & expectedSignals)145 bool ctkCmdLineModuleSignalTester::checkSignals(const QList<QString>& expectedSignals)
146 {
147 if (Events.size() != expectedSignals.size())
148 {
149 dumpSignals(expectedSignals);
150 return false;
151 }
152
153 for (int i=0; i < expectedSignals.size(); ++i)
154 {
155 if (expectedSignals[i] != Events[i])
156 {
157 dumpSignals(expectedSignals);
158 return false;
159 }
160 }
161 return true;
162 }
163
164 //-----------------------------------------------------------------------------
dumpSignals(const QList<QString> & expectedSignals)165 void ctkCmdLineModuleSignalTester::dumpSignals(const QList<QString>& expectedSignals)
166 {
167 int max = Events.size() > expectedSignals.size() ? Events.size() : expectedSignals.size();
168 qDebug() << "Expected signal -- Actual signal";
169 for (int i = 0; i < max; ++i)
170 {
171 QString sig = i < Events.size() ? Events[i] : QString();
172 if (i < expectedSignals.size())
173 {
174 qDebug() << " " << expectedSignals[i] << "--" << sig;
175 }
176 else
177 {
178 qDebug() << " " << "- NONE - " << "--" << sig;
179 }
180 }
181 }
182
183 //-----------------------------------------------------------------------------
results() const184 QList<ctkCmdLineModuleResult> ctkCmdLineModuleSignalTester::results() const
185 {
186 return Results;
187 }
188