/
ALCBaselineModellingPresenterTest.h
352 lines (261 loc) · 11.6 KB
/
ALCBaselineModellingPresenterTest.h
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#ifndef MANTIDQT_CUSTOMINTERFACES_ALCBASELINEMODELLINGTEST_H_
#define MANTIDQT_CUSTOMINTERFACES_ALCBASELINEMODELLINGTEST_H_
#include <cxxtest/TestSuite.h>
#include <gmock/gmock.h>
#include <boost/scoped_ptr.hpp>
#include <boost/assign/list_of.hpp>
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidQtCustomInterfaces/Muon/ALCBaselineModellingPresenter.h"
using namespace MantidQt::CustomInterfaces;
using namespace testing;
using boost::scoped_ptr;
class MockALCBaselineModellingView : public IALCBaselineModellingView
{
public:
void requestFit() { emit fitRequested(); }
void requestAddSection() { emit addSectionRequested(); }
void requestRemoveSection(int row) { emit removeSectionRequested(row); }
void modifySectionRow(int row) { emit sectionRowModified(row); }
void modifySectionSelector(int index) { emit sectionSelectorModified(index); }
MOCK_METHOD0(initialize, void());
MOCK_CONST_METHOD0(function, QString());
MOCK_CONST_METHOD1(sectionRow, SectionRow(int));
MOCK_METHOD1(setDataCurve, void(const QwtData&));
MOCK_METHOD1(setCorrectedCurve, void(const QwtData&));
MOCK_METHOD1(setBaselineCurve, void(const QwtData&));
MOCK_METHOD1(setFunction, void(const QString&));
MOCK_CONST_METHOD0(noOfSectionRows, int());
MOCK_METHOD1(setNoOfSectionRows, void(int));
MOCK_METHOD2(setSectionRow, void(int, SectionRow));
MOCK_CONST_METHOD1(sectionSelector, SectionSelector(int));
MOCK_METHOD2(addSectionSelector, void(int, SectionSelector));
MOCK_METHOD2(updateSectionSelector, void(int, SectionSelector));
MOCK_METHOD1(deleteSectionSelector, void(int));
MOCK_METHOD1(setSectionSelectors, void(const std::vector<SectionSelector>&));
MOCK_METHOD3(updateSectionSelector, void(size_t, double, double));
MOCK_METHOD1(displayError, void(const QString&));
MOCK_METHOD0(help, void());
};
class MockALCBaselineModellingModel : public IALCBaselineModellingModel
{
public:
void changeData() { emit dataChanged(); }
void changeFittedFunction() { emit fittedFunctionChanged(); }
void changeCorrectedData() { emit correctedDataChanged(); }
MOCK_CONST_METHOD0(fittedFunction, IFunction_const_sptr());
MOCK_CONST_METHOD0(correctedData, MatrixWorkspace_const_sptr());
MOCK_CONST_METHOD0(data, MatrixWorkspace_const_sptr());
MOCK_METHOD2(fit, void(IFunction_const_sptr, const std::vector<Section>&));
};
MATCHER_P(FunctionName, name, "") { return arg->name() == name; }
MATCHER_P3(FunctionParameter, param, value, delta, "")
{
return fabs(arg->getParameter(param) - value) < delta;
}
MATCHER_P3(QwtDataX, i, value, delta, "") { return fabs(arg.x(i) - value) < delta; }
MATCHER_P3(QwtDataY, i, value, delta, "") { return fabs(arg.y(i) - value) < delta; }
class ALCBaselineModellingPresenterTest : public CxxTest::TestSuite
{
MockALCBaselineModellingView* m_view;
MockALCBaselineModellingModel* m_model;
ALCBaselineModellingPresenter* m_presenter;
// To save myself some typing
IALCBaselineModellingView::SectionRow sectionRow(double min, double max)
{
return std::make_pair(QString::number(min), QString::number(max));
}
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running otherl tests
static ALCBaselineModellingPresenterTest *createSuite() { return new ALCBaselineModellingPresenterTest(); }
static void destroySuite( ALCBaselineModellingPresenterTest *suite ) { delete suite; }
ALCBaselineModellingPresenterTest()
{
FrameworkManager::Instance(); // To make sure everything is initialized
}
void setUp()
{
m_view = new NiceMock<MockALCBaselineModellingView>();
m_model = new NiceMock<MockALCBaselineModellingModel>();
m_presenter = new ALCBaselineModellingPresenter(m_view, m_model);
m_presenter->initialize();
}
void tearDown()
{
TS_ASSERT(Mock::VerifyAndClearExpectations(m_view));
TS_ASSERT(Mock::VerifyAndClearExpectations(m_model));
delete m_presenter;
delete m_model;
delete m_view;
}
// Creates a workspace with x = [1,2,3...size], y = x + deltaY and e = 1
MatrixWorkspace_sptr createTestWs(size_t size, double deltaY = 0)
{
MatrixWorkspace_sptr ws = WorkspaceFactory::Instance().create("Workspace2D", 1, size, size);
for (size_t i = 0; i < size; ++i)
{
ws->dataX(0)[i] = static_cast<double>(i + 1);
ws->dataY(0)[i] = ws->dataX(0)[i] + deltaY;
ws->dataE(0)[i] = 1;
}
return ws;
}
void test_initialize()
{
// Not using m_view and m_present, because they are already initialized after setUp()
MockALCBaselineModellingView view;
MockALCBaselineModellingModel model;
ALCBaselineModellingPresenter presenter(&view, &model);
EXPECT_CALL(view, initialize()).Times(1);
presenter.initialize();
}
void test_dataChanged()
{
ON_CALL(*m_view, noOfSectionRows()).WillByDefault(Return(3));
ON_CALL(*m_model, data()).WillByDefault(Return(createTestWs(3, 1)));
EXPECT_CALL(*m_view, setDataCurve(AllOf(Property(&QwtData::size, 3),
QwtDataX(0, 1, 1E-8), QwtDataX(2, 3, 1E-8),
QwtDataY(0, 2, 1E-8), QwtDataY(2, 4, 1E-8))));
// Data changed -> clear sections
EXPECT_CALL(*m_view, setNoOfSectionRows(0));
EXPECT_CALL(*m_view, deleteSectionSelector(0));
EXPECT_CALL(*m_view, deleteSectionSelector(1));
EXPECT_CALL(*m_view, deleteSectionSelector(2));
m_model->changeData();
}
void test_correctedChanged()
{
ON_CALL(*m_model, correctedData()).WillByDefault(Return(createTestWs(3, 2)));
EXPECT_CALL(*m_view, setCorrectedCurve(AllOf(Property(&QwtData::size, 3),
QwtDataX(0, 1, 1E-8), QwtDataX(2, 3, 1E-8),
QwtDataY(0, 3, 1E-8), QwtDataY(2, 5, 1E-8))));
m_model->changeCorrectedData();
}
void test_correctedChanged_toEmpty()
{
ON_CALL(*m_model, correctedData()).WillByDefault(Return(MatrixWorkspace_const_sptr()));
EXPECT_CALL(*m_view, setCorrectedCurve(Property(&QwtData::size, 0)));
m_model->changeCorrectedData();
}
void test_fittedFunctionChanged()
{
auto f = FunctionFactory::Instance().createInitialized("name=FlatBackground,A0=5");
ON_CALL(*m_model, fittedFunction()).WillByDefault(Return(f));
ON_CALL(*m_model, data()).WillByDefault(Return(createTestWs(3)));
EXPECT_CALL(*m_view, setFunction(QString::fromStdString(f->asString())));
EXPECT_CALL(*m_view, setBaselineCurve(AllOf(Property(&QwtData::size, 3),
QwtDataX(0, 1, 1E-8), QwtDataX(2, 3, 1E-8),
QwtDataY(0, 5, 1E-8), QwtDataY(2, 5, 1E-8))));
m_model->changeFittedFunction();
}
void test_fittedFunctionChanged_toEmpty()
{
ON_CALL(*m_model, fittedFunction()).WillByDefault(Return(IFunction_const_sptr()));
EXPECT_CALL(*m_view, setFunction(QString("")));
EXPECT_CALL(*m_view, setBaselineCurve(Property(&QwtData::size, 0)));
m_model->changeFittedFunction();
}
void test_addSection()
{
ON_CALL(*m_model, data()).WillByDefault(Return(createTestWs(10)));
ON_CALL(*m_view, noOfSectionRows()).WillByDefault(Return(1));
Expectation tableExtended = EXPECT_CALL(*m_view, setNoOfSectionRows(2));
EXPECT_CALL(*m_view, setSectionRow(1, Pair(QString("1"), QString("10")))).After(tableExtended);
EXPECT_CALL(*m_view, addSectionSelector(1, Pair(1,10)));
m_view->requestAddSection();
}
void test_removeSection()
{
ON_CALL(*m_view, noOfSectionRows()).WillByDefault(Return(3));
ON_CALL(*m_view, sectionRow(0)).WillByDefault(Return(sectionRow(1,2)));
ON_CALL(*m_view, sectionRow(1)).WillByDefault(Return(sectionRow(3,4)));
ON_CALL(*m_view, sectionRow(2)).WillByDefault(Return(sectionRow(5,6)));
Expectation tableShrinked = EXPECT_CALL(*m_view, setNoOfSectionRows(2));
EXPECT_CALL(*m_view, setSectionRow(0, Pair(QString("1"), QString("2")))).After(tableShrinked);
EXPECT_CALL(*m_view, setSectionRow(1, Pair(QString("5"), QString("6")))).After(tableShrinked);
ExpectationSet selectorsCleared;
selectorsCleared += EXPECT_CALL(*m_view, deleteSectionSelector(0));
selectorsCleared += EXPECT_CALL(*m_view, deleteSectionSelector(1));
selectorsCleared += EXPECT_CALL(*m_view, deleteSectionSelector(2));
EXPECT_CALL(*m_view, addSectionSelector(0, Pair(1,2))).After(selectorsCleared);
EXPECT_CALL(*m_view, addSectionSelector(1, Pair(5,6))).After(selectorsCleared);
m_view->requestRemoveSection(1);
}
void test_onSectionSelectorModified()
{
ON_CALL(*m_view, sectionSelector(5)).WillByDefault(Return(std::make_pair(1,2)));
EXPECT_CALL(*m_view, setSectionRow(5, Pair(QString("1"), QString("2"))));
m_view->modifySectionSelector(5);
}
void test_onSectionRowModified()
{
IALCBaselineModellingView::SectionRow row(QString("3"), QString("4"));
ON_CALL(*m_view, sectionRow(4)).WillByDefault(Return(row));
EXPECT_CALL(*m_view, updateSectionSelector(4, Pair(3,4)));
m_view->modifySectionRow(4);
}
void test_fit()
{
ON_CALL(*m_view, noOfSectionRows()).WillByDefault(Return(2));
ON_CALL(*m_view, sectionRow(0)).WillByDefault(Return(sectionRow(10,20)));
ON_CALL(*m_view, sectionRow(1)).WillByDefault(Return(sectionRow(40,55)));
ON_CALL(*m_view, function()).WillByDefault(Return(QString("name=FlatBackground,A0=3")));
EXPECT_CALL(*m_model, fit(AllOf(FunctionName("FlatBackground"),
FunctionParameter("A0", 3, 1E-8)),
ElementsAre(Pair(10, 20),
Pair(40, 55))));
m_view->requestFit();
}
void test_fit_exception()
{
// Any valid set of sections
ON_CALL(*m_view, noOfSectionRows()).WillByDefault(Return(1));
ON_CALL(*m_view, sectionRow(0)).WillByDefault(Return(sectionRow(1,2)));
// Valid function
ON_CALL(*m_view, function()).WillByDefault(Return(QString("name=FlatBackground,A0=3")));
std::string errorMsg = "Bad error";
ON_CALL(*m_model, fit(_,_)).WillByDefault(Throw(std::runtime_error(errorMsg)));
EXPECT_CALL(*m_view, displayError(QString::fromStdString(errorMsg)));
m_view->requestFit();
}
void test_fit_badFunction()
{
// Any valid set of sections
ON_CALL(*m_view, noOfSectionRows()).WillByDefault(Return(1));
ON_CALL(*m_view, sectionRow(0)).WillByDefault(Return(sectionRow(1,2)));
// Invalid function
ON_CALL(*m_view, function()).WillByDefault(Return(QString("bla-bla")));
EXPECT_CALL(*m_model, fit(_,_)).Times(0);
EXPECT_CALL(*m_view, displayError(_));
m_view->requestFit();
}
void test_fit_emptyFunction()
{
// Any valid set of sections
ON_CALL(*m_view, noOfSectionRows()).WillByDefault(Return(1));
ON_CALL(*m_view, sectionRow(0)).WillByDefault(Return(sectionRow(1,2)));
// Empty function
ON_CALL(*m_view, function()).WillByDefault(Return(QString("")));
EXPECT_CALL(*m_model, fit(_,_)).Times(0);
EXPECT_CALL(*m_view, displayError(_));
m_view->requestFit();
}
void test_fit_noSections()
{
// No sections
ON_CALL(*m_view, noOfSectionRows()).WillByDefault(Return(0));
// Any valid function
ON_CALL(*m_view, function()).WillByDefault(Return(QString("name=FlatBackground,A0=0")));
EXPECT_CALL(*m_model, fit(_,_)).Times(0);
EXPECT_CALL(*m_view, displayError(_));
m_view->requestFit();
}
void test_helpPage ()
{
EXPECT_CALL(*m_view, help()).Times(1);
m_view->help();
}
};
#endif /* MANTIDQT_CUSTOMINTERFACES_ALCBASELINEMODELLINGTEST_H_ */