AlgorithmProgressPresenterTest.h 5.87 KB
Newer Older
1
2
3
4
5
6
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2020 ISIS Rutherford Appleton Laboratory UKRI,
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
7
#pragma once
8

9
#include "MantidAPI/AlgorithmFactory.h"
10
11
12
#include "MantidAPI/AlgorithmManager.h"
#include "MantidKernel/WarningSuppressions.h"
#include "MantidQtWidgets/Common/AlgorithmProgress/AlgorithmProgressPresenter.h"
13
#include "ManualProgressReporter.h"
14
15
16
17
18
19
#include "MockAlgorithmProgressWidget.h"

#include <QCoreApplication>
#include <cxxtest/TestSuite.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
20
#include <memory>
21
22
23
24
25
26
27
28
29
30

GNU_DIAG_OFF_SUGGEST_OVERRIDE

using namespace testing;
using namespace Mantid::API;
using namespace MantidQt::MantidWidgets;

class AlgorithmProgressPresenterTest : public CxxTest::TestSuite {
public:
  static AlgorithmProgressPresenterTest *createSuite() {
31
32
    AlgorithmFactory::Instance()
        .subscribe<Mantid::Algorithms::ManualProgressReporter>();
33
34
35
    return new AlgorithmProgressPresenterTest();
  }
  static void destroySuite(AlgorithmProgressPresenterTest *suite) {
36
    AlgorithmFactory::Instance().unsubscribe(NAME_MANUALRPOGRESSREPORTER, 1);
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    delete suite;
  }

  void setUp() override {
    mockView.reset();
    // The mock view also creates the presenter, because
    // so that is passes the correct type into the constructor
    mockView = std::make_unique<NiceMock<MockAlgorithmProgressWidget>>();
  }

  void testAlgorithmStart() {
    // Sets up a fake AlgorithmID - it is just a `void *`
    // so the address of an int is taken to act as one
    // what it actually points to does not matter
    int testInt = 123;
    void *algorithmIDpretender = &testInt;
    EXPECT_CALL(*mockView.get(), algorithmStarted()).Times(1);

    auto pres = mockView->m_presenter.get();
    pres->algorithmStartedSlot(algorithmIDpretender);
    TS_ASSERT(Mock::VerifyAndClearExpectations(&mockView));
  }
  void testAlgorithmStart_SecondAlgorithmStartDoesntReplaceFirst() {
    int testInt = 123;
    void *algorithmIDpretender = &testInt;
    int testInt2 = 666;
    void *secondAlgorithmID = &testInt2;
    EXPECT_CALL(*mockView.get(), algorithmStarted()).Times(1);

    auto pres = mockView->m_presenter.get();
    pres->algorithmStartedSlot(algorithmIDpretender);
    // second call should not increment the algorithm started calls
    pres->algorithmStartedSlot(secondAlgorithmID);
    TS_ASSERT(Mock::VerifyAndClearExpectations(&mockView));
  }
  void testAlgorithmEnd() {
    int testInt = 123;
    void *algorithmIDpretender = &testInt;
    EXPECT_CALL(*mockView.get(), algorithmStarted()).Times(1);
    EXPECT_CALL(*mockView.get(), algorithmEnded()).Times(1);

    auto pres = mockView->m_presenter.get();
    pres->algorithmStartedSlot(algorithmIDpretender);
    // Alg ended is from another algorithm ID,
    // it should not cancel the first one
    pres->algorithmEndedSlot(algorithmIDpretender);
    TS_ASSERT(Mock::VerifyAndClearExpectations(&mockView));
  }
  void testAlgorithmEnd_NotTrackedAlgorithmEnds() {
    int testInt = 123;
    void *algorithmIDpretender = &testInt;
    int testInt2 = 666;
    void *secondAlgorithmID = &testInt2;
    EXPECT_CALL(*mockView.get(), algorithmStarted()).Times(1);
    EXPECT_CALL(*mockView.get(), algorithmEnded()).Times(0);

    auto pres = mockView->m_presenter.get();
    pres->algorithmStartedSlot(algorithmIDpretender);
    // Alg ended is from another algorithm ID,
    // it should not cancel the first one
    pres->algorithmEndedSlot(secondAlgorithmID);
    TS_ASSERT(Mock::VerifyAndClearExpectations(&mockView));
  }
100
101
102
  /*
  TODO MUST FIX THE MOCK
  void xtestUpdateProgressBar() {
103
104
105
    int testInt = 123;
    void *algorithmIDpretender = &testInt;
    EXPECT_CALL(*mockView.get(), algorithmStarted()).Times(1);
106
    EXPECT_CALL(*mockView.get(), updateProgress(3.0, QString(""),0.,0)).Times(1);
107
108
109
110

    auto pres = mockView->m_presenter.get();
    pres->algorithmStartedSlot(algorithmIDpretender);
    // Algorithm reports a progress update
111
    pres->updateProgressBarSlot(algorithmIDpretender, 3.0, "",0.,0);
112
113
    TS_ASSERT(Mock::VerifyAndClearExpectations(&mockView));
  }
114
  void xtestUpdateProgressBar_NotUpdatedIfAlgorithmNotBeingTracked() {
115
116
117
118
119
    int testInt = 123;
    void *algorithmIDpretender = &testInt;
    int testInt2 = 666;
    void *secondAlgorithmID = &testInt2;
    EXPECT_CALL(*mockView.get(), algorithmStarted()).Times(1);
120
    EXPECT_CALL(*mockView.get(), updateProgress(3.0, QString(""),0.,0)).Times(0);
121
122
123

    auto pres = mockView->m_presenter.get();
    pres->algorithmStartedSlot(algorithmIDpretender);
124
    pres->updateProgressBarSlot(secondAlgorithmID, 3.0, "",0.,0);
125
126
    TS_ASSERT(Mock::VerifyAndClearExpectations(&mockView));
  }
127
  void xtestRealAlgorithmRunning() {
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    EXPECT_CALL(*mockView.get(), algorithmStarted()).Times(1);
    int reports = 10;
    QString emptyQString;
    // This is the only way the comparison worked,
    // incrementing a bool had too high error (in 1e-1 range)),
    // but manually writing the values works.
    // This way testing::DoubleNear doesn't seem to be necessary
    // Another thing to note: 0.0 progress is NOT reported
    for (const auto prog : {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0}) {
      EXPECT_CALL(*mockView.get(),
                  updateProgress(DoubleEq(prog), emptyQString));
    }
    EXPECT_CALL(*mockView.get(), algorithmEnded()).Times(1);

    auto alg = AlgorithmManager::Instance().create("ManualProgressReporter");
    TS_ASSERT_THROWS_NOTHING(alg->initialize());
    TS_ASSERT(alg->isInitialized());
    alg->setProperty("NumberOfProgressReports", reports);
    alg->setRethrows(true);
    alg->execute();
    QCoreApplication::processEvents();

    TS_ASSERT(Mock::VerifyAndClearExpectations(&mockView));
  }
152
  */
153
154
private:
  std::unique_ptr<NiceMock<MockAlgorithmProgressWidget>> mockView;
155
};