EnggDiffractionPresenterTest.h 33.7 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
                             const std::vector<std::string> &outFilenames,
                             const std::string &runNo,
36
37
38
39
                             const std::vector<bool> banks,
                             const std::string &specIDs,
                             const std::string &dgFile) {
    doFocusRun(dir, outFilenames, runNo, banks, specIDs, dgFile);
40
41
    focusingFinished();
  }
42
43
44
45
46
47
48
49
50
51
52
53
54

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

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

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

    m_ex_enginx_banks.push_back(true);
    m_ex_enginx_banks.push_back(false);
82
83
84
85
86
	m_ex_empty_run_num.push_back("");
	m_invalid_run_number.push_back("999999");
	m_ex_run_number.push_back("228061");
	g_vanNo.push_back("8899999988");
	g_ceriaNo.push_back("9999999999");
87
88
89
90
91
92
  }

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

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

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  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);
  }

113
  void test_loadExistingCalibWithWrongName() {
114
115
116
117
118
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // will need basic calibration settings from the user
    EnggDiffCalibSettings calibSettings;
119
120
121
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
        .WillOnce(Return(calibSettings));
122

123
    const std::string mockFname = "foo.par";
124
125
126
    EXPECT_CALL(mockView, askExistingCalibFilename())
        .Times(1)
        .WillOnce(Return(mockFname));
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

    // 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;
145
146
147
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
        .WillOnce(Return(calibSettings));
148
149

    const std::string mockFname = "ENGINX_111111_222222_foo_bar.par";
150
151
152
    EXPECT_CALL(mockView, askExistingCalibFilename())
        .Times(1)
        .WillOnce(Return(mockFname));
153
154
155
    EXPECT_CALL(mockView, newCalibLoaded(testing::_, testing::_, mockFname))
        .Times(1);

156
157
158
159
160
161
162
    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::LoadExistingCalib);
  }

163
164
165
166
  void test_calcCalibWithoutRunNumbers() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

167
168
    // would need basic calibration settings from the user, but it should not
    // get to that point because of early detected errors:
169
    EnggDiffCalibSettings calibSettings;
170
    EXPECT_CALL(mockView, currentCalibSettings()).Times(0);
171

172
    // No errors, 1 warning (no Vanadium, no Ceria run numbers given)
173
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
174
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);
175

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

180
181
182
    pres.notify(IEnggDiffractionPresenter::CalcCalib);
  }

183
  // this can start the calibration thread, so watch out
184
  void test_calcCalibWithSettingsMissing() {
185
    testing::NiceMock<MockEnggDiffractionView> mockView;
186
187
188
189

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

192
193
194
195
196
197
198
    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
199
    EnggDiffCalibSettings calibSettings;
200

201
202
203
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
        .WillOnce(Return(calibSettings));
204

205
    EXPECT_CALL(mockView, newVanadiumNo()).Times(1).WillOnce(Return(g_vanNo));
206

207
    EXPECT_CALL(mockView, newCeriaNo()).Times(1).WillOnce(Return(g_ceriaNo));
208
209
210
211
212
213
214
215
216
217
218
219

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

220
221
  // this test actually starts the calibration process - which implies starting
  // the thread unless you use the mock without thread
222
223
224
225
226
227
228
229
  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);

230
231
    const std::string instr = "ENGINX";
    const std::string vanNo = "8899999988"; // use a number that won't be found!
232
233
    const std::string ceriaNo =
        "9999999999"; // use a number that won't be found!
234
235
236
237
238
239

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

244
    EXPECT_CALL(mockView, newVanadiumNo()).Times(1).WillOnce(Return(g_vanNo));
245

246
    EXPECT_CALL(mockView, newCeriaNo()).Times(1).WillOnce(Return(g_ceriaNo));
247
248
249

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

250
251
    const std::string filename =
        "UNKNOWNINST_" + vanNo + "_" + ceriaNo + "_" + "foo.prm";
252
253
254
255
    EXPECT_CALL(mockView,
                askNewCalibrationFilename("UNKNOWNINST_" + vanNo + "_" +
                                          ceriaNo + "_both_banks.prm"))
        .Times(0);
256
    //  .WillOnce(Return(filename)); // if enabled ask user output filename
257

258
259
260
261
262
263
264
    // 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);

265
    // should disable actions at the beginning of the calculations
266
    EXPECT_CALL(mockView, enableCalibrateAndFocusActions(false)).Times(1);
267
268
269

    // and should enable them again at the (unsuccessful) end - this happens
    // when a separate thread finished (here the thread is mocked)
270
    EXPECT_CALL(mockView, enableCalibrateAndFocusActions(true)).Times(1);
271
272
273
274

    // 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
275
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);
276
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
277

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

282
    // TS_ASSERT_THROWS_NOTHING(pres.notify(IEnggDiffractionPresenter::CalcCalib));
283
    pres.notify(IEnggDiffractionPresenter::CalcCalib);
284
285
  }

286
287
  // TODO: disabled for now, as this one would need to load files
  void disabled_test_calcCalibOK() {
288
289
290
291
292
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // will need basic calibration settings from the user
    EnggDiffCalibSettings calibSettings;
293
294
295
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(2)
        .WillOnce(Return(calibSettings));
296
297
298
299
300
301
302
303

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

    pres.notify(IEnggDiffractionPresenter::CalcCalib);
  }

304
305
306
307
308
  void test_focusWithoutRunNumber() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // empty run number!
309
    EXPECT_CALL(mockView, focusingRunNo()).Times(1).WillOnce(Return(m_invalid_run_number));
310
311
312
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
313

314
315
316
317
    // 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);
318
319
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
320

321
322
323
324
325
326
327
328
329
330
331
    // 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);
  }

332
  void test_focusWithRunNumberButWrongBanks() {
333
334
335
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

336
    EXPECT_CALL(mockView, focusingRunNo()).Times(1).WillOnce(Return(m_invalid_run_number));
337
    // missing bank on/off vector!
338
339
340
341
    std::vector<bool> banks;
    banks.push_back(false);
    banks.push_back(false);
    EXPECT_CALL(mockView, focusingBanks()).Times(1).WillOnce(Return(banks));
342

343
344
345
    // would needs basic calibration settings, but only if there was at least
    // one bank selected
    EXPECT_CALL(mockView, currentCalibSettings()).Times(0);
346
347
348

    // should not get that far that it tries to get these parameters
    EXPECT_CALL(mockView, currentInstrument()).Times(0);
349
350
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368

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

369
    // wrong run number!
370
    EXPECT_CALL(mockView, focusingRunNo()).Times(1).WillOnce(Return(m_invalid_run_number));
371
372
373
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
374
375
376

    // needs basic calibration settings from the user to start focusing
    EnggDiffCalibSettings calibSettings;
377
378
379
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
        .WillOnce(Return(calibSettings));
380

381
382
383
384
385
    // 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);
386
387
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
388

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
    // 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
  void disabled_test_focusOK() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // an example run available in unit test data:
406
    EXPECT_CALL(mockView, focusingRunNo()).Times(1).WillOnce(Return(m_ex_run_number));
407
408
409
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
410
411
412

    // will need basic calibration settings from the user
    EnggDiffCalibSettings calibSettings;
413
414
415
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
        .WillOnce(Return(calibSettings));
416

417
    // check automatic plotting
418
419
420
    EXPECT_CALL(mockView, focusedOutWorkspace())
        .Times(1)
        .WillOnce(Return(true));
421
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(1);
422
423
    // There are two/three other tests that have the disabled_ prefix so they
    // normally run
424

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
    // 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);

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

    pres.notify(IEnggDiffractionPresenter::FocusRun);
  }

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

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

    // will need basic calibration settings from the user
    EnggDiffCalibSettings calibSettings;
451
452
453
    EXPECT_CALL(mockView, currentCalibSettings())
        .Times(1)
        .WillOnce(Return(calibSettings));
454

455
456
457
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);

458
459
460
461
462
463
464
    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::FocusRun);
  }

465
466
467
468
469
  void test_focusCropped_withoutRunNo() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // empty run number!
470
    EXPECT_CALL(mockView, focusingCroppedRunNo()).Times(1).WillOnce(Return(m_ex_empty_run_num));
471
472
473
474
475
476
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs())
        .Times(1)
        .WillOnce(Return("1"));
477
478
479
480
481
482
483
484
485

    // 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);
486
487
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
488
489
490
491
492
493
494
495
496
497
498
499
500

    // 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
501
502
    EXPECT_CALL(mockView, focusingCroppedRunNo())
        .Times(1)
503
        .WillOnce(Return(m_ex_run_number));
504
505
506
507
508
509
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(std::vector<bool>()));
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs())
        .Times(1)
        .WillOnce(Return("1,5"));
510
511
512
513
514

    // 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);
515
516
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533

    // 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
534
535
    EXPECT_CALL(mockView, focusingCroppedRunNo())
        .Times(1)
536
        .WillOnce(Return(m_ex_run_number));
537
538
539
540
541
542
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs())
        .Times(1)
        .WillOnce(Return(""));
543
544
545
546
547

    // 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);
548
549
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566

    // 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!
567
    EXPECT_CALL(mockView, focusingTextureRunNo()).Times(1).WillOnce(Return(m_ex_empty_run_num));
568
569
570
    EXPECT_CALL(mockView, focusingTextureGroupingFile())
        .Times(1)
        .WillOnce(Return(""));
571
572
573
574

    // should not try to use these ones
    EXPECT_CALL(mockView, focusingRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingBanks()).Times(0);
575
576
577
578

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

579
580
581
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);

582
583
584
585
586
587
588
589
590
591
592
593
    // 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
594
595
    EXPECT_CALL(mockView, focusingTextureRunNo())
        .Times(1)
596
        .WillOnce(Return(m_ex_run_number));
597
    EXPECT_CALL(mockView, focusingBanks()).Times(0);
598
599
600
    EXPECT_CALL(mockView, focusingTextureGroupingFile())
        .Times(1)
        .WillOnce(Return(""));
601
602
603
604
605
606
607

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

608
609
610
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);

611
612
613
614
615
616
617
618
619
620
621
622
    // 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
623
624
    EXPECT_CALL(mockView, focusingTextureRunNo())
        .Times(1)
625
        .WillOnce(Return(m_ex_run_number));
626
    // non empty but absurd csv file of detector groups
627
628
629
    EXPECT_CALL(mockView, focusingTextureGroupingFile())
        .Times(1)
        .WillOnce(Return("i_dont_exist_dont_look_for_me.csv"));
630
631
632
633

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

634
635
636
    EXPECT_CALL(mockView, focusingCroppedRunNo()).Times(0);
    EXPECT_CALL(mockView, focusingCroppedSpectrumIDs()).Times(0);

637
638
639
    EXPECT_CALL(mockView, focusedOutWorkspace()).Times(0);
    EXPECT_CALL(mockView, plotFocusedSpectrum(testing::_)).Times(0);

640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
    // 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!
671
    EXPECT_CALL(mockView, focusingRunNo()).Times(1).WillOnce(Return(m_ex_empty_run_num));
672
673
674
    EXPECT_CALL(mockView, focusingBanks())
        .Times(1)
        .WillOnce(Return(m_ex_enginx_banks));
675
676
677
678
679
680
681
682
683
684
685
686

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

687
688
689
690
691
692
693
694
  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() {
695
696
697
698
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // inputs from user
699
700
701
    EXPECT_CALL(mockView, currentPreprocRunNo())
        .Times(1)
        .WillOnce(Return(g_eventModeRunNo));
702
703
    EXPECT_CALL(mockView, rebinningTimeBin()).Times(1).WillOnce(Return(0));

704
705
706
707
708
709
710
    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(1);

    pres.notify(IEnggDiffractionPresenter::RebinTime);
  }

711
  // this test does run Load and then Rebin
712
713
  void test_preproc_event_time_ok() {
    testing::NiceMock<MockEnggDiffractionView> mockView;
714
    EnggDiffPresenterNoThread pres(&mockView);
715
716
717
718
719
720
721

    // inputs from user
    EXPECT_CALL(mockView, currentPreprocRunNo())
        .Times(1)
        .WillOnce(Return(g_eventModeRunNo));
    EXPECT_CALL(mockView, rebinningTimeBin()).Times(1).WillOnce(Return(1.0));

722
723
724
725
726
727
728
    // No errors/warnings
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

    pres.notify(IEnggDiffractionPresenter::RebinTime);
  }

729
  void test_preproc_event_multiperiod_missing_runno() {
730
731
732
733
734
    testing::NiceMock<MockEnggDiffractionView> mockView;
    MantidQt::CustomInterfaces::EnggDiffractionPresenter pres(&mockView);

    // inputs from user
    EXPECT_CALL(mockView, currentPreprocRunNo()).Times(1).WillOnce(Return(""));
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
    // 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));
752
    EXPECT_CALL(mockView, rebinningPulsesTime()).Times(1).WillOnce(Return(0));
753

754
755
756
757
758
759
760
    // 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);
  }

761
  // this test does run Load but then RebinByPulseTimes should fail
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
  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
778
779
780
781
782
    EXPECT_CALL(mockView, userError(testing::_, testing::_)).Times(0);
    EXPECT_CALL(mockView, userWarning(testing::_, testing::_)).Times(0);

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

784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
  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);
  }

799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
  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);
  }

829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
  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;
856
857

  std::vector<bool> m_ex_enginx_banks;
858
859
860
861
862
  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;
863
  const static std::string g_eventModeRunNo;
864
865
};

866

867
868
869
870
871
872
873
// 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 =
    "ENGINX228061";

874
#endif // MANTID_CUSTOMINTERFACES_ENGGDIFFRACTIONPRESENTERTEST_H