EnggDiffractionPresenterTest.h 35.3 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
28
                             const std::string &vanNo,
                             const std::string &ceriaNo) {
29
30
31
    doNewCalibration(outFilename, vanNo, ceriaNo);
    calibrationFinished();
  }
32
33

  void startAsyncFocusWorker(const std::string &dir,
34
35
36
37
38
39
40
41
                             const std::vector<std::string> &outFilenames,
                             const std::string &runNo,
                             const std::vector<bool> &banks,
                             const std::string &specNos,
                             const std::string &dgFile) {
    std::cerr << "focus run " << std::endl;
    doFocusRun(dir, outFilenames, runNo, banks, specNos, dgFile);
    focusingFinished();
42
  }
43
44
45
46
47
48
49
50
51
52
53
54
55

  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();
  }
56
57
};

58
59
60
class EnggDiffractionPresenterTest : public CxxTest::TestSuite {

public:
61
62
  // This pair of boilerplate methods prevent tghe suite being created
  // statically
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  // 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()));
81

82
    // default banks
83
84
    m_ex_enginx_banks.push_back(true);
    m_ex_enginx_banks.push_back(false);
Shahroz Ahmed's avatar
Shahroz Ahmed committed
85

86
87
88
89
90
91
    // 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");
Shahroz Ahmed's avatar
Shahroz Ahmed committed
92

93
94
95
    // 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
96

97
98
    m_basicCalibSettings.m_pixelCalibFilename =
        "ENGINX_full_pixel_calibration.csv";
Shahroz Ahmed's avatar
Shahroz Ahmed committed
99

100
101
102
    m_basicCalibSettings.m_templateGSAS_PRM = "GUI_calib_folder/"
                                              "template_ENGINX_241391_236516_"
                                              "North_and_South_banks.prm";
Shahroz Ahmed's avatar
Shahroz Ahmed committed
103

104
105
    m_basicCalibSettings.m_forceRecalcOverwrite = false;
    m_basicCalibSettings.m_rebinCalibrate = 1;
106
107
108
109
110
111
  }

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

112
113
114
115
116
  // 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.

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  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);
  }

132
  void test_loadExistingCalibWithWrongName() {
133
134
135
136
137
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // will need basic calibration settings from the user
    EnggDiffCalibSettings calibSettings;
138
139
140
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
        .WillOnce(Return(calibSettings));
141

142
    const std::string mockFname = "foo.par";
143
144
145
    EXPECT_CALL(mockView, askExistingCalibFilename())
        .Times(1)
        .WillOnce(Return(mockFname));
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

    // 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;
164
165
166
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
        .WillOnce(Return(calibSettings));
167
168

    const std::string mockFname = "ENGINX_111111_222222_foo_bar.par";
169
170
171
    EXPECT_CALL(mockView, askExistingCalibFilename())
        .Times(1)
        .WillOnce(Return(mockFname));
172
173
174
    EXPECT_CALL(mockView, newCalibLoaded(testing::_, testing::_, mockFname))
        .Times(1);

175
176
177
178
179
180
181
    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::LoadExistingCalib);
  }

182
183
184
185
  void test_calcCalibWithoutRunNumbers() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

186
187
188
    // 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);
189

190
191
192
193
194
195
196
197
    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));

198
    // No errors, 1 warning (no Vanadium, no Ceria run numbers given)
199
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
200
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);
201

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

206
207
208
    pres.notify(IEnggDiffractionPresenter::CalcCalib);
  }

209
  // this can start the calibration thread, so watch out
210
  void test_calcCalibWithSettingsMissing() {
211
    testing::NiceMock<MockEnggDiffractionView> mockView;
212
213
214
215

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

218
219
220
221
222
223
224
    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
225
    EnggDiffCalibSettings calibSettings;
226

227
228
229
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
        .WillOnce(Return(calibSettings));
230

231
    EXPECT_CALL(mockView, newVanadiumNo()).Times(1).WillOnce(Return(g_vanNo));
232

233
    EXPECT_CALL(mockView, newCeriaNo()).Times(1).WillOnce(Return(g_ceriaNo));
234
235
236
237
238
239
240
241
242
243
244
245

    // 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));
  }

246
247
  // this test actually starts the calibration process - which implies starting
  // the thread unless you use the mock without thread
248
249
250
251
252
253
254
255
  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);

256
257
    const std::string instr = "ENGINX";
    const std::string vanNo = "8899999988"; // use a number that won't be found!
258
259
    const std::string ceriaNo =
        "9999999999"; // use a number that won't be found!
260
261
262
263
264
265

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

270
    EXPECT_CALL(mockView, newVanadiumNo()).Times(1).WillOnce(Return(g_vanNo));
271

272
    EXPECT_CALL(mockView, newCeriaNo()).Times(1).WillOnce(Return(g_ceriaNo));
273
274
275

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

276
277
    const std::string filename =
        "UNKNOWNINST_" + vanNo + "_" + ceriaNo + "_" + "foo.prm";
278
279
280
281
    EXPECT_CALL(mockView,
                askNewCalibrationFilename("UNKNOWNINST_" + vanNo + "_" +
                                          ceriaNo + "_both_banks.prm"))
        .Times(0);
282
    //  .WillOnce(Return(filename)); // if enabled ask user output filename
283

284
285
286
287
288
289
290
    // 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);

291
    // should disable actions at the beginning of the calculations
292
    EXPECT_CALL(mockView, enableCalibrateAndFocusActions(false)).Times(1);
293
294
295

    // and should enable them again at the (unsuccessful) end - this happens
    // when a separate thread finished (here the thread is mocked)
296
    EXPECT_CALL(mockView, enableCalibrateAndFocusActions(true)).Times(1);
297
298
299
300

    // 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
301
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);
302
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
303

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

308
    // TS_ASSERT_THROWS_NOTHING(pres.notify(IEnggDiffractionPresenter::CalcCalib));
309
    pres.notify(IEnggDiffractionPresenter::CalcCalib);
310
311
  }

312
  // TODO: disabled for now, as this one would need to load files
313
  void disable_test_calcCalibOK() {
314
315
316
317
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // will need basic calibration settings from the user
318
    EXPECT_CALL(mockView, currentCalibSettings())
319
320
321
322
323
324
325
        .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));
326
327
328
329
330
331
332
333

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

    pres.notify(IEnggDiffractionPresenter::CalcCalib);
  }

334
335
336
337
338
  void test_focusWithoutRunNumber() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // empty run number!
339
340
341
    EXPECT_CALL(mockView, focusingRunNo())
        .Times(1)
        .WillOnce(Return(m_invalid_run_number));
342
343
344
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
345

346
347
348
349
    // 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);
350
351
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
352

353
354
355
356
357
358
359
360
361
362
363
    // 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);
  }

364
  void test_focusWithRunNumberButWrongBanks() {
365
366
367
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

368
369
370
    EXPECT_CALL(mockView, focusingRunNo())
        .Times(1)
        .WillOnce(Return(m_invalid_run_number));
371
    // missing bank on/off vector!
372
373
374
375
    std::vector<bool> banks;
    banks.push_back(false);
    banks.push_back(false);
    EXPECT_CALL(mockView, focusingBanks()).Times(1).WillOnce(Return(banks));
376

377
378
379
    // would needs basic calibration settings, but only if there was at least
    // one bank selected
    EXPECT_CALL(mockView, currentCalibSettings()).Times(0);
380
381
382

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

    // 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);

403
    // wrong run number!
404
405
406
    EXPECT_CALL(mockView, focusingRunNo())
        .Times(1)
        .WillOnce(Return(m_invalid_run_number));
407
408
409
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
410

411
412
413
414
415
    // 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);
416
417
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
418

419
420
421
422
423
424
425
426
427
428
429
430
    // 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
431
  void disable_test_focusOK() {
432
433
    testing::NiceMock<MockEnggDiffractionView> mockView;
    EnggDiffPresenterNoThread pres(&mockView);
434

435
436
    const std::string instr = "ENGINX";
    const std::string vanNo = "236516"; // use a number that can be found!
437

438
439
440
441
442
443
444
445
    // 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));
446

447
448
449
    EXPECT_CALL(mockView, currentInstrument())
        .Times(2)
        .WillRepeatedly(Return(instr));
450

451
452
453
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(2)
        .WillRepeatedly(Return(m_basicCalibSettings));
454

455
456
    // 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
457

458
459
    // 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
460

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

463
464
    // 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
465

466
467
468
469
470
    // 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
471

472
473
474
    // 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
475

476
    pres.notify(IEnggDiffractionPresenter::FocusRun);
477
478
479
480
481
482
483
  }

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

    // an example run available in unit test data:
484
485
486
    EXPECT_CALL(mockView, focusingRunNo())
        .Times(1)
        .WillOnce(Return(m_ex_run_number));
487
488
489
    std::vector<bool> banks;
    banks.push_back(false);
    banks.push_back(false);
490
    EXPECT_CALL(mockView, focusingBanks()).Times(1).WillOnce(Return(banks));
491
492
493

    // will need basic calibration settings from the user
    EnggDiffCalibSettings calibSettings;
494
495
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
Shahroz Ahmed's avatar
Shahroz Ahmed committed
496
        .WillOnce(Return(m_basicCalibSettings));
497

498
499
500
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);

501
502
503
504
505
506
507
    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::FocusRun);
  }

508
509
510
511
512
  void test_focusCropped_withoutRunNo() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // empty run number!
513
514
515
    EXPECT_CALL(mockView, focusingCroppedRunNo())
        .Times(1)
        .WillOnce(Return(m_ex_empty_run_num));
516
517
518
519
520
521
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs())
        .Times(1)
        .WillOnce(Return("1"));
522
523
524
525
526
527
528
529
530

    // 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);
531
532
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
533
534
535
536
537
538
539
540
541
542
543
544
545

    // 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
546
547
    EXPECT_CALL(mockView, focusingCroppedRunNo())
        .Times(1)
548
        .WillOnce(Return(m_ex_run_number));
549
550
551
552
553
554
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(std::vector<bool>()));
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs())
        .Times(1)
        .WillOnce(Return("1,5"));
555
556
557
558
559

    // 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);
560
561
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578

    // 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
579
580
    EXPECT_CALL(mockView, focusingCroppedRunNo())
        .Times(1)
581
        .WillOnce(Return(m_ex_run_number));
582
583
584
585
586
587
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs())
        .Times(1)
        .WillOnce(Return(""));
588
589
590
591
592

    // 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);
593
594
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611

    // 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!
612
613
614
    EXPECT_CALL(mockView, focusingTextureRunNo())
        .Times(1)
        .WillOnce(Return(m_ex_empty_run_num));
615
616
617
    EXPECT_CALL(mockView, focusingTextureGroupingFile())
        .Times(1)
        .WillOnce(Return(""));
618
619
620
621

    // should not try to use these ones
    EXPECT_CALL(mockView, focusingRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingBanks()).Times(0);
622
623
624
625

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

626
627
628
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);

629
630
631
632
633
634
635
636
637
638
639
640
    // 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
641
642
    EXPECT_CALL(mockView, focusingTextureRunNo())
        .Times(1)
643
        .WillOnce(Return(m_ex_run_number));
644
    EXPECT_CALL(mockView, focusingBanks()).Times(0);
645
646
647
    EXPECT_CALL(mockView, focusingTextureGroupingFile())
        .Times(1)
        .WillOnce(Return(""));
648
649
650
651
652
653
654

    // 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);

655
656
657
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);

658
659
660
661
662
663
664
665
666
667
668
669
    // 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
670
671
    EXPECT_CALL(mockView, focusingTextureRunNo())
        .Times(1)
672
        .WillOnce(Return(m_ex_run_number));
673
    // non empty but absurd csv file of detector groups
674
675
676
    EXPECT_CALL(mockView, focusingTextureGroupingFile())
        .Times(1)
        .WillOnce(Return("i_dont_exist_dont_look_for_me.csv"));
677
678
679
680

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

681
682
683
    EXPECT_CALL(mockView, focusingCroppedRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs()).Times(0);

684
685
686
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);

687
688
689
690
691
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
    // 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!
718
719
720
    EXPECT_CALL(mockView, focusingRunNo())
        .Times(1)
        .WillOnce(Return(m_ex_empty_run_num));
721
722
723
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
724
725
726
727
728
729
730
731
732
733
734
735

    // 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);
  }

736
737
738
739
740
741
742
743
  void test_preproc_event_time_bin_missing_runno() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    pres.notify(IEnggDiffractionPresenter::RebinTime);
  }

  void test_preproc_event_time_wrong_bin() {
744
745
746
747
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // inputs from user
748
749
750
    EXPECT_CALL(mockView, currentPreprocRunNo())
        .Times(1)
        .WillOnce(Return(g_eventModeRunNo));
751
752
    EXPECT_CALL(mockView, rebinningTimeBin()).Times(1).WillOnce(Return(0));

753
754
755
756
757
758
759
    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::RebinTime);
  }

760
  // this test does run Load and then Rebin //
761
762
  void test_preproc_event_time_ok() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
763
    EnggDiffPresenterNoThread pres(&mockView);
764
765
766
    // inputs from user
    EXPECT_CALL(mockView, currentPreprocRunNo())
        .Times(1)
Shahroz Ahmed's avatar
Shahroz Ahmed committed
767
        .WillRepeatedly(Return(g_eventModeRunNo));
768
769

    EXPECT_CALL(mockView, rebinningTimeBin()).Times(1).WillOnce(Return(1.0));
770

771
772
773
774
775
776
777
    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::RebinTime);
  }

778
  void test_preproc_event_multiperiod_missing_runno() {
779
780
781
782
783
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // inputs from user
    EXPECT_CALL(mockView, currentPreprocRunNo()).Times(1).WillOnce(Return(""));
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
    // 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)
        .WillOnce(Return(g_eventModeRunNo));
    EXPECT_CALL(mockView, rebinningPulsesNumberPeriods())
        .Times(1)
        .WillOnce(Return(1));
801
    EXPECT_CALL(mockView, rebinningPulsesTime()).Times(1).WillOnce(Return(0));
802

803
804
805
806
807
808
809
    // 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);
  }

810
  // this test does run Load but then RebinByPulseTimes should fail
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
  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)
        .WillOnce(Return(g_eventModeRunNo));
    EXPECT_CALL(mockView, rebinningPulsesNumberPeriods())
        .Times(1)
        .WillOnce(Return(1000));
    // 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
827
828
829
830
831
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

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

833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
  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);
  }

848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
  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);
  }

878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
  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;
905
906

  std::vector<bool> m_ex_enginx_banks;
Shahroz Ahmed's avatar
Shahroz Ahmed committed
907
  const static std::string g_eventModeRunNo;
908
  const static std::string g_validRunNo;
Shahroz Ahmed's avatar
Shahroz Ahmed committed
909
910
  EnggDiffCalibSettings m_basicCalibSettings;

911
912
913
914
915
  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;
Shahroz Ahmed's avatar
Shahroz Ahmed committed
916
};
917

918
919
920
921
922
// 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 =
923
924
925
    "ENGINX228061";

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

927
#endif // MANTID_CUSTOMINTERFACES_ENGGDIFFRACTIONPRESENTERTEST_H