EnggDiffractionPresenterTest.h 35.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef MANTID_CUSTOMINTERFACES_ENGGDIFFRACTIONPRESENTERTEST_H
#define MANTID_CUSTOMINTERFACES_ENGGDIFFRACTIONPRESENTERTEST_H

#include "MantidAPI/FrameworkManager.h"
#include "MantidQtCustomInterfaces/EnggDiffraction/EnggDiffractionPresenter.h"

#include <cxxtest/TestSuite.h>
#include "EnggDiffractionViewMock.h"

using namespace MantidQt::CustomInterfaces;
using testing::TypedEq;
using testing::Return;

14
15
// Use this mocked presenter for tests that will start the calibration,
// focusing, rebinning, etc. workers/threads.
16
17
18
19
20
21
22
23
24
25
// Otherwise you'll run into trouble with issues like "QEventLoop: Cannot be
// used without QApplication", as there is not Qt application here and the
// normal Qt thread used by the presenter uses signals/slots.
class EnggDiffPresenterNoThread : public EnggDiffractionPresenter {
public:
  EnggDiffPresenterNoThread(IEnggDiffractionView *view)
      : EnggDiffractionPresenter(view) {}

private:
  // not async at all
26
  void startAsyncCalibWorker(const std::string &outFilename,
27
                             const std::string &vanNo,
28
29
                             const std::string &ceriaNo,
                             const std::string &specNos) {
30
    doNewCalibration(outFilename, vanNo, ceriaNo, specNos);
31
32
    calibrationFinished();
  }
33
34

  void startAsyncFocusWorker(const std::string &dir,
Shahroz Ahmed's avatar
Shahroz Ahmed committed
35
                             const std::vector<std::string> &multi_RunNo,
36
37
38
39
                             const std::vector<bool> &banks,
                             const std::string &specNos,
                             const std::string &dgFile) {
    std::cerr << "focus run " << std::endl;
Shahroz Ahmed's avatar
Shahroz Ahmed committed
40
41
42

    std::string runNo = multi_RunNo[0];

Shahroz Ahmed's avatar
Shahroz Ahmed committed
43
    doFocusRun(dir, runNo, banks, specNos, dgFile);
Shahroz Ahmed's avatar
Shahroz Ahmed committed
44

45
    focusingFinished();
46
  }
47
48
49
50
51
52
53
54
55
56
57
58
59

  void startAsyncRebinningTimeWorker(const std::string &runNo, double bin,
                                     const std::string &outWSName) {
    doRebinningTime(runNo, bin, outWSName);
    rebinningFinished();
  }

  void startAsyncRebinningPulsesWorker(const std::string &runNo,
                                       size_t nperiods, double timeStep,
                                       const std::string &outWSName) {
    doRebinningPulses(runNo, nperiods, timeStep, outWSName);
    rebinningFinished();
  }
60
61
};

62
63
64
class EnggDiffractionPresenterTest : public CxxTest::TestSuite {

public:
65
66
  // This pair of boilerplate methods prevent tghe suite being created
  // statically
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  // This means the constructor isn't called when running other tests
  static EnggDiffractionPresenterTest *createSuite() {
    return new EnggDiffractionPresenterTest();
  }

  static void destroySuite(EnggDiffractionPresenterTest *suite) {
    delete suite;
  }

  EnggDiffractionPresenterTest() {
    Mantid::API::FrameworkManager::Instance(); // make sure framework is
                                               // initialized
  }

  void setUp() {
    m_view.reset(new testing::NiceMock<MockEnggDiffractionView>());
    m_presenter.reset(
        new MantidQt::CustomInterfaces::EnggDiffractionPresenter(m_view.get()));
85

86
    // default banks
87
88
    m_ex_enginx_banks.push_back(true);
    m_ex_enginx_banks.push_back(false);
Shahroz Ahmed's avatar
Shahroz Ahmed committed
89

90
91
92
93
94
95
    // default run number
    m_ex_empty_run_num.push_back("");
    m_invalid_run_number.push_back("");
    m_ex_run_number.push_back(g_validRunNo);
    g_vanNo.push_back("8899999988");
    g_ceriaNo.push_back("9999999999");
96
    g_rebinRunNo.push_back(g_eventModeRunNo);
Shahroz Ahmed's avatar
Shahroz Ahmed committed
97

98
99
100
    // provide personal directories in order to carry out the full disable tests
    m_basicCalibSettings.m_inputDirCalib = "GUI_calib_folder/";
    m_basicCalibSettings.m_inputDirRaw = "GUI_calib_folder/";
Shahroz Ahmed's avatar
Shahroz Ahmed committed
101

102
103
    m_basicCalibSettings.m_pixelCalibFilename =
        "ENGINX_full_pixel_calibration.csv";
Shahroz Ahmed's avatar
Shahroz Ahmed committed
104

105
106
107
    m_basicCalibSettings.m_templateGSAS_PRM = "GUI_calib_folder/"
                                              "template_ENGINX_241391_236516_"
                                              "North_and_South_banks.prm";
Shahroz Ahmed's avatar
Shahroz Ahmed committed
108

109
110
    m_basicCalibSettings.m_forceRecalcOverwrite = false;
    m_basicCalibSettings.m_rebinCalibrate = 1;
111
112
113
114
115
116
  }

  void tearDown() {
    TS_ASSERT(testing::Mock::VerifyAndClearExpectations(m_view.get()));
  }

117
118
119
120
121
  // TODO: There should be a few basic tests on the presenter here, including
  // methods like: parseCalibrateFilename, buildCalibrateSuggestedFilename, etc.
  // Several of these are indirectly tested through some of the GUI-mock-based
  // tests below but should be tested as isolated methods here at the beginning.

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  void test_start() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    std::vector<std::string> sv;
    sv.push_back("dummy msg");
    EXPECT_CALL(mockView, logMsgs()).Times(1).WillOnce(Return(sv));

    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::LogMsg);
  }

137
  void test_loadExistingCalibWithWrongName() {
138
139
140
141
142
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // will need basic calibration settings from the user
    EnggDiffCalibSettings calibSettings;
143
144
145
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
        .WillOnce(Return(calibSettings));
146

147
    const std::string mockFname = "foo.par";
148
149
150
    EXPECT_CALL(mockView, askExistingCalibFilename())
        .Times(1)
        .WillOnce(Return(mockFname));
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

    // should not get to the point where the calibration is calculated
    EXPECT_CALL(mockView, newCalibLoaded(testing::_, testing::_, mockFname))
        .Times(0);

    // Should show a warning but no errors
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::LoadExistingCalib);
  }

  void test_loadExistingCalibWithAcceptableName() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // will need basic calibration settings from the user
    EnggDiffCalibSettings calibSettings;
169
170
171
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
        .WillOnce(Return(calibSettings));
172
173

    const std::string mockFname = "ENGINX_111111_222222_foo_bar.par";
174
175
176
    EXPECT_CALL(mockView, askExistingCalibFilename())
        .Times(1)
        .WillOnce(Return(mockFname));
177
178
179
    EXPECT_CALL(mockView, newCalibLoaded(testing::_, testing::_, mockFname))
        .Times(1);

180
181
182
183
184
185
186
    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::LoadExistingCalib);
  }

187
188
189
190
  void test_calcCalibWithoutRunNumbers() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

191
192
193
    // would need basic calibration settings from the user, but it should not
    // get to that point because of early detected errors:
    EXPECT_CALL(mockView, currentCalibSettings()).Times(0);
194

195
196
197
198
199
200
201
202
    EXPECT_CALL(mockView, newVanadiumNo())
        .Times(1)
        .WillOnce(Return(m_ex_empty_run_num));

    EXPECT_CALL(mockView, newCeriaNo())
        .Times(1)
        .WillOnce(Return(m_ex_empty_run_num));

203
    // No errors, 1 warning (no Vanadium, no Ceria run numbers given)
204
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
205
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);
206

207
208
209
210
    // does not update the current calibration as it must have failed
    EXPECT_CALL(mockView, newCalibLoaded(testing::_, testing::_, testing::_))
        .Times(0);

211
212
213
    pres.notify(IEnggDiffractionPresenter::CalcCalib);
  }

214
  // this can start the calibration thread, so watch out
215
  void test_calcCalibWithSettingsMissing() {
216
    testing::NiceMock<MockEnggDiffractionView> mockView;
217
218
219
220

    // this test can start a Qt thread that needs signals/slots
    // Don't do: MantidQt::CustomInterfaces::EnggDiffractionPresenter
    // pres(&mockView);
221
222
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

223
224
225
226
227
228
229
    const std::string instr = "FAKEINSTR";
    const std::string vanNo = "9999999999"; // use a number that won't be found!
    const std::string ceriaNo =
        "9999999999"; // use a number that won't be found!

    // will need basic calibration settings from the user - but I forget to set
    // them
230
    EnggDiffCalibSettings calibSettings;
231

232
233
234
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
        .WillOnce(Return(calibSettings));
235

236
    EXPECT_CALL(mockView, newVanadiumNo()).Times(1).WillOnce(Return(g_vanNo));
237

238
    EXPECT_CALL(mockView, newCeriaNo()).Times(1).WillOnce(Return(g_ceriaNo));
239
240
241
242
243
244
245
246
247
248
249
250

    // 1 warning because some required settings are missing/empty
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);

    // does not update the current calibration as it must have failed
    EXPECT_CALL(mockView, newCalibLoaded(testing::_, testing::_, testing::_))
        .Times(0);

    TS_ASSERT_THROWS_NOTHING(pres.notify(IEnggDiffractionPresenter::CalcCalib));
  }

251
252
  // this test actually starts the calibration process - which implies starting
  // the thread unless you use the mock without thread
253
254
255
256
257
258
259
260
  void test_calcCalibWithRunNumbersButError() {
    testing::NiceMock<MockEnggDiffractionView> mockView;

    // this test would start a Qt thread that needs signals/slots
    // Don't do: MantidQt::CustomInterfaces::EnggDiffractionPresenter
    // pres(&mockView);
    EnggDiffPresenterNoThread pres(&mockView);

261
262
    const std::string instr = "ENGINX";
    const std::string vanNo = "8899999988"; // use a number that won't be found!
263
264
    const std::string ceriaNo =
        "9999999999"; // use a number that won't be found!
265
266
267
268
269
270

    // will need basic calibration settings from the user
    EnggDiffCalibSettings calibSettings;
    calibSettings.m_pixelCalibFilename =
        instr + "_" + vanNo + "_" + ceriaNo + ".prm";
    calibSettings.m_templateGSAS_PRM = "fake.prm";
271
272
273
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(2)
        .WillRepeatedly(Return(calibSettings));
274

275
    EXPECT_CALL(mockView, newVanadiumNo()).Times(1).WillOnce(Return(g_vanNo));
276

277
    EXPECT_CALL(mockView, newCeriaNo()).Times(1).WillOnce(Return(g_ceriaNo));
278
279
280

    EXPECT_CALL(mockView, currentInstrument()).Times(1).WillOnce(Return(instr));

281
282
    const std::string filename =
        "UNKNOWNINST_" + vanNo + "_" + ceriaNo + "_" + "foo.prm";
283
284
285
286
    EXPECT_CALL(mockView,
                askNewCalibrationFilename("UNKNOWNINST_" + vanNo + "_" +
                                          ceriaNo + "_both_banks.prm"))
        .Times(0);
287
    //  .WillOnce(Return(filename)); // if enabled ask user output filename
288

289
290
291
292
293
294
295
    // Should not try to use options for focusing
    EXPECT_CALL(mockView, focusingRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingCroppedRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingTextureRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs()).Times(0);
    EXPECT_CALL(mockView, focusingTextureGroupingFile()).Times(0);

296
    // should disable actions at the beginning of the calculations
297
    EXPECT_CALL(mockView, enableCalibrateAndFocusActions(false)).Times(1);
298
299
300

    // and should enable them again at the (unsuccessful) end - this happens
    // when a separate thread finished (here the thread is mocked)
301
    EXPECT_CALL(mockView, enableCalibrateAndFocusActions(true)).Times(1);
302
303
304
305

    // No warnings/error pop-ups: some exception(s) are thrown (because there
    // are missing settings and/or files) but these must be caught
    // and error messages logged
306
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);
307
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
308

309
310
311
312
    // does not update the current calibration as it must have failed
    EXPECT_CALL(mockView, newCalibLoaded(testing::_, testing::_, testing::_))
        .Times(0);

313
    // TS_ASSERT_THROWS_NOTHING(pres.notify(IEnggDiffractionPresenter::CalcCalib));
314
    pres.notify(IEnggDiffractionPresenter::CalcCalib);
315
316
  }

317
  // TODO: disabled for now, as this one would need to load files
318
  void disable_test_calcCalibOK() {
319
320
321
322
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // will need basic calibration settings from the user
323
    EXPECT_CALL(mockView, currentCalibSettings())
324
325
326
327
328
329
330
        .Times(1)
        .WillOnce(Return(m_basicCalibSettings));

    // As this is a positive test, personal directory/files should be
    // provided here instead
    EXPECT_CALL(mockView, newVanadiumNo()).Times(1).WillOnce(Return(g_vanNo));
    EXPECT_CALL(mockView, newCeriaNo()).Times(1).WillOnce(Return(g_ceriaNo));
331
332
333
334
335
336
337
338

    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::CalcCalib);
  }

339
340
341
342
343
  void test_focusWithoutRunNumber() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // empty run number!
344
345
346
    EXPECT_CALL(mockView, focusingRunNo())
        .Times(1)
        .WillOnce(Return(m_invalid_run_number));
347
348
349
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
350

351
352
353
354
    // should not try to use these ones
    EXPECT_CALL(mockView, focusingCroppedRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs()).Times(0);
    EXPECT_CALL(mockView, focusingTextureGroupingFile()).Times(0);
355
356
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
357

358
359
360
361
362
363
364
365
366
367
368
    // should not get that far that it tries to get these parameters
    EXPECT_CALL(mockView, currentInstrument()).Times(0);
    EXPECT_CALL(mockView, currentCalibSettings()).Times(0);

    // 1 warning pop-up to user, 0 errors
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::FocusRun);
  }

369
  void test_focusWithRunNumberButWrongBanks() {
370
371
372
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

373
374
375
    EXPECT_CALL(mockView, focusingRunNo())
        .Times(1)
        .WillOnce(Return(m_invalid_run_number));
376
    // missing bank on/off vector!
377
378
379
380
    std::vector<bool> banks;
    banks.push_back(false);
    banks.push_back(false);
    EXPECT_CALL(mockView, focusingBanks()).Times(1).WillOnce(Return(banks));
381

382
383
384
    // would needs basic calibration settings, but only if there was at least
    // one bank selected
    EXPECT_CALL(mockView, currentCalibSettings()).Times(0);
385
386
387

    // should not get that far that it tries to get these parameters
    EXPECT_CALL(mockView, currentInstrument()).Times(0);
388
389
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407

    // 1 warning pop-up to user, 0 errors
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::FocusRun);
  }

  // the focusing process starts but the input run number cannot be found
  void test_focusWithNumbersButError() {
    testing::NiceMock<MockEnggDiffractionView> mockView;

    // this test starts the focusing process which would start a Qt
    // thread that needs signals/slots Don't do:
    // MantidQt::CustomInterfaces::EnggDiffractionPresenter
    // pres(&mockView);
    EnggDiffPresenterNoThread pres(&mockView);

408
    // wrong run number!
409
410
411
    EXPECT_CALL(mockView, focusingRunNo())
        .Times(1)
        .WillOnce(Return(m_invalid_run_number));
412
413
414
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
415

416
417
418
419
420
    // Should not try to use options for other types of focusing
    EXPECT_CALL(mockView, focusingCroppedRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingTextureRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs()).Times(0);
    EXPECT_CALL(mockView, focusingTextureGroupingFile()).Times(0);
421
422
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
423

424
425
426
427
428
429
430
431
432
433
434
435
    // it should not get there
    EXPECT_CALL(mockView, enableCalibrateAndFocusActions(false)).Times(0);
    EXPECT_CALL(mockView, enableCalibrateAndFocusActions(true)).Times(0);

    // 0 errors, 1 warning error pop-up to user
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::FocusRun);
  }

  // TODO: disabled for now, as this one would need to load files
Shahroz Ahmed's avatar
Shahroz Ahmed committed
436
  void disable_test_focusOK() {
437
438
    testing::NiceMock<MockEnggDiffractionView> mockView;
    EnggDiffPresenterNoThread pres(&mockView);
439

440
441
    const std::string instr = "ENGINX";
    const std::string vanNo = "236516"; // use a number that can be found!
442

443
444
445
446
447
448
449
450
    // an example run available in unit test data:
    EXPECT_CALL(mockView, focusingRunNo())
        .Times(1)
        .WillOnce(Return(m_ex_run_number));
    std::vector<bool> banks;
    banks.push_back(true); // 1 bank used
    banks.push_back(false);
    EXPECT_CALL(mockView, focusingBanks()).Times(1).WillOnce(Return(banks));
451

452
453
454
    EXPECT_CALL(mockView, currentInstrument())
        .Times(2)
        .WillRepeatedly(Return(instr));
455

456
457
458
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(2)
        .WillRepeatedly(Return(m_basicCalibSettings));
459

460
461
    // when two banks are used then it will utlise currentVanadiumNo two times
    EXPECT_CALL(mockView, currentVanadiumNo()).Times(1).WillOnce(Return(vanNo));
Shahroz Ahmed's avatar
Shahroz Ahmed committed
462

463
464
    // it will not reach here on wards, would finish with a Warning message
    // "The Calibration did not finish correctly"
Shahroz Ahmed's avatar
Shahroz Ahmed committed
465

466
    // this is because of the python algorithm cannot be used with in c++ test
Shahroz Ahmed's avatar
Shahroz Ahmed committed
467

468
469
    // the test will not be able to read the python algorithm from here on
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
Shahroz Ahmed's avatar
Shahroz Ahmed committed
470

471
472
473
474
475
    // Should not try to use options for other types of focusing
    EXPECT_CALL(mockView, focusingCroppedRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingTextureRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs()).Times(0);
    EXPECT_CALL(mockView, focusingTextureGroupingFile()).Times(0);
Shahroz Ahmed's avatar
Shahroz Ahmed committed
476

477
478
479
    // 0 errors/ 0 warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);
Shahroz Ahmed's avatar
Shahroz Ahmed committed
480

481
    pres.notify(IEnggDiffractionPresenter::FocusRun);
482
483
484
485
486
487
488
  }

  void disabled_test_focusOK_allBanksOff() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // an example run available in unit test data:
489
490
491
    EXPECT_CALL(mockView, focusingRunNo())
        .Times(1)
        .WillOnce(Return(m_ex_run_number));
492
493
494
    std::vector<bool> banks;
    banks.push_back(false);
    banks.push_back(false);
495
    EXPECT_CALL(mockView, focusingBanks()).Times(1).WillOnce(Return(banks));
496
497
498

    // will need basic calibration settings from the user
    EnggDiffCalibSettings calibSettings;
499
500
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
Shahroz Ahmed's avatar
Shahroz Ahmed committed
501
        .WillOnce(Return(m_basicCalibSettings));
502

503
504
505
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);

506
507
508
509
510
511
512
    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::FocusRun);
  }

513
514
515
516
517
  void test_focusCropped_withoutRunNo() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // empty run number!
518
519
520
    EXPECT_CALL(mockView, focusingCroppedRunNo())
        .Times(1)
        .WillOnce(Return(m_ex_empty_run_num));
521
522
523
524
525
526
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs())
        .Times(1)
        .WillOnce(Return("1"));
527
528
529
530
531
532
533
534
535

    // should not try to use these ones
    EXPECT_CALL(mockView, focusingRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingTextureRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingTextureGroupingFile()).Times(0);

    // should not get that far that it tries to get these parameters
    EXPECT_CALL(mockView, currentInstrument()).Times(0);
    EXPECT_CALL(mockView, currentCalibSettings()).Times(0);
536
537
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
538
539
540
541
542
543
544
545
546
547
548
549
550

    // 1 warning pop-up to user, 0 errors
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::FocusCropped);
  }

  void test_focusCropped_withoutBanks() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // ok run number
551
552
    EXPECT_CALL(mockView, focusingCroppedRunNo())
        .Times(1)
553
        .WillOnce(Return(m_ex_run_number));
554
555
556
557
558
559
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(std::vector<bool>()));
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs())
        .Times(1)
        .WillOnce(Return("1,5"));
560
561
562
563
564

    // should not try to use these ones
    EXPECT_CALL(mockView, focusingRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingTextureRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingTextureGroupingFile()).Times(0);
565
566
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583

    // should not get that far that it tries to get these parameters
    EXPECT_CALL(mockView, currentInstrument()).Times(0);
    EXPECT_CALL(mockView, currentCalibSettings()).Times(0);

    // 1 warning pop-up to user, 0 errors
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::FocusCropped);
  }

  void test_focusCropped_withoutSpectrumIDs() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // ok run number
584
585
    EXPECT_CALL(mockView, focusingCroppedRunNo())
        .Times(1)
586
        .WillOnce(Return(m_ex_run_number));
587
588
589
590
591
592
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs())
        .Times(1)
        .WillOnce(Return(""));
593
594
595
596
597

    // should not try to use these ones
    EXPECT_CALL(mockView, focusingRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingTextureRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingTextureGroupingFile()).Times(0);
598
599
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616

    // should not get that far that it tries to get these parameters
    EXPECT_CALL(mockView, currentInstrument()).Times(0);
    EXPECT_CALL(mockView, currentCalibSettings()).Times(0);

    // 1 warning pop-up to user, 0 errors
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::FocusCropped);
  }

  void test_focusTexture_withoutRunNo() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // empty run number!
617
618
619
    EXPECT_CALL(mockView, focusingTextureRunNo())
        .Times(1)
        .WillOnce(Return(m_ex_empty_run_num));
620
621
622
    EXPECT_CALL(mockView, focusingTextureGroupingFile())
        .Times(1)
        .WillOnce(Return(""));
623
624
625
626

    // should not try to use these ones
    EXPECT_CALL(mockView, focusingRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingBanks()).Times(0);
627
628
629
630

    EXPECT_CALL(mockView, focusingCroppedRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs()).Times(0);

631
632
633
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);

634
635
636
637
638
639
640
641
642
643
644
645
    // 1 warning pop-up to user, 0 errors
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::FocusTexture);
  }

  void test_focusTexture_withoutFilename() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // goo run number
646
647
    EXPECT_CALL(mockView, focusingTextureRunNo())
        .Times(1)
648
        .WillOnce(Return(m_ex_run_number));
649
    EXPECT_CALL(mockView, focusingBanks()).Times(0);
650
651
652
    EXPECT_CALL(mockView, focusingTextureGroupingFile())
        .Times(1)
        .WillOnce(Return(""));
653
654
655
656
657
658
659

    // should not try to use these ones
    EXPECT_CALL(mockView, focusingRunNo()).Times(0);

    EXPECT_CALL(mockView, focusingCroppedRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs()).Times(0);

660
661
662
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);

663
664
665
666
667
668
669
670
671
672
673
674
    // 1 warning pop-up to user, 0 errors
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::FocusTexture);
  }

  void test_focusTexture_withInexistentTextureFile() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // goo run number
675
676
    EXPECT_CALL(mockView, focusingTextureRunNo())
        .Times(1)
677
        .WillOnce(Return(m_ex_run_number));
678
    // non empty but absurd csv file of detector groups
679
680
681
    EXPECT_CALL(mockView, focusingTextureGroupingFile())
        .Times(1)
        .WillOnce(Return("i_dont_exist_dont_look_for_me.csv"));
682
683
684
685

    // should not try to use these ones
    EXPECT_CALL(mockView, focusingRunNo()).Times(0);

686
687
688
    EXPECT_CALL(mockView, focusingCroppedRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs()).Times(0);

689
690
691
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);

692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
    // 1 warning pop-up to user, 0 errors
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::FocusTexture);
  }

  void test_resetFocus() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    EXPECT_CALL(mockView, resetFocus()).Times(1);

    // No errors/warnings when resetting options
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::ResetFocus);
  }

  void test_resetFocus_thenFocus() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // No errors/warnings when resetting options
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::ResetFocus);

    // empty run number!
723
724
725
    EXPECT_CALL(mockView, focusingRunNo())
        .Times(1)
        .WillOnce(Return(m_ex_empty_run_num));
726
727
728
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
729
730
731
732
733
734
735
736
737
738
739
740

    // should not get that far that it tries to get these parameters
    EXPECT_CALL(mockView, currentInstrument()).Times(0);
    EXPECT_CALL(mockView, currentCalibSettings()).Times(0);

    // Now one error shown as a warning-pop-up cause inputs and options are
    // empty
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::FocusRun);
  }

741
742
743
744
  void test_preproc_event_time_bin_missing_runno() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

745
746
747
748
749
750
751
752
753
754
    // inputs from user
    EXPECT_CALL(mockView, currentPreprocRunNo())
        .Times(1)
        .WillOnce(Return(m_ex_empty_run_num));
    EXPECT_CALL(mockView, rebinningTimeBin()).Times(1).WillOnce(Return(0));

    // No errors/1 warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

755
756
757
758
    pres.notify(IEnggDiffractionPresenter::RebinTime);
  }

  void test_preproc_event_time_wrong_bin() {
759
760
761
762
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // inputs from user
763
764
    EXPECT_CALL(mockView, currentPreprocRunNo())
        .Times(1)
765
        .WillOnce(Return(g_rebinRunNo));
766
767
    EXPECT_CALL(mockView, rebinningTimeBin()).Times(1).WillOnce(Return(0));

768
769
770
771
772
773
774
    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::RebinTime);
  }

775
  // this test does run Load and then Rebin //
776
777
  void test_preproc_event_time_ok() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
778
    EnggDiffPresenterNoThread pres(&mockView);
779
780
    // inputs from user
    EXPECT_CALL(mockView, currentPreprocRunNo())
781
        .Times(2)
782
        .WillRepeatedly(Return(g_rebinRunNo));
783

784
785
786
    EXPECT_CALL(mockView, rebinningTimeBin())
        .Times(1)
        .WillRepeatedly(Return(0.100000));
787

788
789
790
791
792
793
794
    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::RebinTime);
  }

795
  void test_preproc_event_multiperiod_missing_runno() {
796
797
798
799
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // inputs from user
800
801
802
    EXPECT_CALL(mockView, currentPreprocRunNo())
        .Times(1)
        .WillOnce(Return(m_ex_empty_run_num));
803
804
805
806
807
808
809
810
811
812
813
814
815
    // should not even call this one when the run number is obviously wrong
    EXPECT_CALL(mockView, rebinningTimeBin()).Times(0);

    pres.notify(IEnggDiffractionPresenter::RebinMultiperiod);
  }

  void test_preproc_event_multiperiod_wrong_bin() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // inputs from user
    EXPECT_CALL(mockView, currentPreprocRunNo())
        .Times(1)
816
        .WillOnce(Return(g_rebinRunNo));
817
818
819
    EXPECT_CALL(mockView, rebinningPulsesNumberPeriods())
        .Times(1)
        .WillOnce(Return(1));
820
    EXPECT_CALL(mockView, rebinningPulsesTime()).Times(1).WillOnce(Return(0));
821

822
823
824
825
826
827
828
    // No errors, warning because of wrong bin
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::RebinMultiperiod);
  }

829
  // this test does run Load but then RebinByPulseTimes should fail
830
831
832
833
834
835
836
837
  void test_preproc_event_multiperiod_file_wrong_type() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    EnggDiffPresenterNoThread pres(&mockView);

    // inputs from user
    // This file will be found but it is not a valid file for this re-binning
    EXPECT_CALL(mockView, currentPreprocRunNo())
        .Times(1)
838
        .WillOnce(Return(g_rebinRunNo));
839
840
    EXPECT_CALL(mockView, rebinningPulsesNumberPeriods())
        .Times(1)
841
        .WillOnce(Return(0.100000));
842
843
844
845
    // 1s is big enough
    EXPECT_CALL(mockView, rebinningPulsesTime()).Times(1).WillOnce(Return(1));

    // No errors/warnings. There will be an error log from the algorithms
846
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
847
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);
848
849
850

    pres.notify(IEnggDiffractionPresenter::RebinMultiperiod);
  }
851

852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
  void test_logMsg() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    std::vector<std::string> sv;
    sv.push_back("dummy log");
    EXPECT_CALL(mockView, logMsgs()).Times(1).WillOnce(Return(sv));

    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::LogMsg);
  }

867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
  void test_RBNumberChange_ok() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // as if the user has set an empty RB Number that looks correct
    EXPECT_CALL(mockView, getRBNumber()).Times(1).WillOnce(Return("RB000xxxx"));
    EXPECT_CALL(mockView, enableTabs(true)).Times(1);

    // no errors/ warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::RBNumberChange);
  }

  void test_RBNumberChange_empty() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // as if the user has set an empty RB Number
    EXPECT_CALL(mockView, getRBNumber()).Times(1).WillOnce(Return(""));
    EXPECT_CALL(mockView, enableTabs(false)).Times(1);

    // no errors/ warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::RBNumberChange);
  }

897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
  void test_instChange() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // 1 error, no warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(1);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::InstrumentChange);
  }

  void test_shutDown() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    EXPECT_CALL(mockView, saveSettings()).Times(1);
    // No errors, no warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::ShutDown);
  }

private:
  boost::scoped_ptr<testing::NiceMock<MockEnggDiffractionView>> m_view;
  boost::scoped_ptr<MantidQt::CustomInterfaces::EnggDiffractionPresenter>
      m_presenter;
924
925

  std::vector<bool> m_ex_enginx_banks;
Shahroz Ahmed's avatar
Shahroz Ahmed committed
926
  const static std::string g_eventModeRunNo;
927
  const static std::string g_validRunNo;
Shahroz Ahmed's avatar
Shahroz Ahmed committed
928
929
  EnggDiffCalibSettings m_basicCalibSettings;

930
931
932
933
934
  std::vector<std::string> m_ex_empty_run_num;
  std::vector<std::string> m_invalid_run_number;
  std::vector<std::string> m_ex_run_number;
  std::vector<std::string> g_vanNo;
  std::vector<std::string> g_ceriaNo;
935
  std::vector<std::string> g_rebinRunNo;
Shahroz Ahmed's avatar
Shahroz Ahmed committed
936
};
937

938
939
940
941
942
// Note this is not a correct event mode run number. Using it here just
// as a run number that is found.
// A possible event mode file would be: 197019, but it is too big for
// unit test data. TODO: find a small one or crop a big one.
const std::string EnggDiffractionPresenterTest::g_eventModeRunNo =
943
944
945
    "ENGINX228061";

const std::string EnggDiffractionPresenterTest::g_validRunNo = "228061";
946

947
#endif // MANTID_CUSTOMINTERFACES_ENGGDIFFRACTIONPRESENTERTEST_H