From 48235f23346bb23c379ae787f4ba0d350f3da0dc Mon Sep 17 00:00:00 2001
From: Gemma Guest <gemma.guest@stfc.ac.uk>
Date: Tue, 20 Aug 2019 15:31:25 +0100
Subject: [PATCH] Move update of instrument list until fully initialized

Setting initial state is done by the main window after adding a new
batch is complete and all presenters know their parent presenter. Move
the initialisation of the instrument list to the same place.

Re #26536
---
 .../ISISReflectometry/GUI/Batch/BatchPresenter.cpp   |  4 ++++
 .../ISISReflectometry/GUI/Batch/BatchPresenter.h     |  1 +
 .../ISISReflectometry/GUI/Batch/IBatchPresenter.h    |  1 +
 .../GUI/MainWindow/MainWindowPresenter.cpp           | 12 +++++++++---
 .../GUI/MainWindow/MainWindowPresenter.h             |  1 +
 .../ISISReflectometry/GUI/Runs/IRunsPresenter.h      |  1 +
 .../ISISReflectometry/GUI/Runs/RunsPresenter.cpp     |  5 +++--
 .../ISISReflectometry/GUI/Runs/RunsPresenter.h       |  1 +
 .../ISISReflectometry/Batch/BatchPresenterTest.h     |  7 +++++++
 .../MainWindow/MainWindowPresenterTest.h             |  1 +
 .../test/ISISReflectometry/ReflMockObjects.h         |  2 ++
 .../test/ISISReflectometry/Runs/RunsPresenterTest.h  |  5 +++--
 12 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/Batch/BatchPresenter.cpp b/qt/scientific_interfaces/ISISReflectometry/GUI/Batch/BatchPresenter.cpp
index e2aa9487ac0..4db12e9289c 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/Batch/BatchPresenter.cpp
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/Batch/BatchPresenter.cpp
@@ -69,6 +69,10 @@ void BatchPresenter::acceptMainPresenter(IMainWindowPresenter *mainPresenter) {
   m_mainPresenter = mainPresenter;
 }
 
+void BatchPresenter::initInstrumentList() {
+  m_runsPresenter->initInstrumentList();
+}
+
 bool BatchPresenter::requestClose() const { return true; }
 
 void BatchPresenter::notifyInstrumentChangedRequested(
diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/Batch/BatchPresenter.h b/qt/scientific_interfaces/ISISReflectometry/GUI/Batch/BatchPresenter.h
index 1290e6bee47..211ef620417 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/Batch/BatchPresenter.h
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/Batch/BatchPresenter.h
@@ -55,6 +55,7 @@ public:
 
   // IBatchPresenter overrides
   void acceptMainPresenter(IMainWindowPresenter *mainPresenter) override;
+  void initInstrumentList() override;
   void notifyReductionPaused() override;
   void notifyReductionResumed() override;
   void notifyAutoreductionResumed() override;
diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/Batch/IBatchPresenter.h b/qt/scientific_interfaces/ISISReflectometry/GUI/Batch/IBatchPresenter.h
index a9d95e7f9e6..3196b64433e 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/Batch/IBatchPresenter.h
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/Batch/IBatchPresenter.h
@@ -30,6 +30,7 @@ public:
   virtual ~IBatchPresenter() = default;
 
   virtual void acceptMainPresenter(IMainWindowPresenter *mainPresenter) = 0;
+  virtual void initInstrumentList() = 0;
 
   virtual void notifyReductionPaused() = 0;
   virtual void notifyReductionResumed() = 0;
diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.cpp b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.cpp
index 71c098055b2..d9d8c9b51e4 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.cpp
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.cpp
@@ -130,15 +130,21 @@ bool MainWindowPresenter::isAnyBatchAutoreducing() const {
 void MainWindowPresenter::addNewBatch(IBatchView *batchView) {
   m_batchPresenters.emplace_back(m_batchPresenterFactory->make(batchView));
   m_batchPresenters.back()->acceptMainPresenter(this);
+  initNewBatch(m_batchPresenters.back().get());
+}
+
+void MainWindowPresenter::initNewBatch(IBatchPresenter *batchPresenter) {
+
+  batchPresenter->initInstrumentList();
 
   // starts in the paused state
-  m_batchPresenters.back()->reductionPaused();
+  batchPresenter->reductionPaused();
 
   // Ensure autoreduce button is enabled/disabled correctly for the new batch
   if (isAnyBatchAutoreducing())
-    m_batchPresenters.back()->anyBatchAutoreductionResumed();
+    batchPresenter->anyBatchAutoreductionResumed();
   else
-    m_batchPresenters.back()->anyBatchAutoreductionPaused();
+    batchPresenter->anyBatchAutoreductionPaused();
 }
 
 void MainWindowPresenter::showHelp() {
diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.h b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.h
index 16de5b1ce81..aac7bed1ba9 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.h
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/MainWindow/MainWindowPresenter.h
@@ -72,6 +72,7 @@ private:
 
   void showHelp();
   void addNewBatch(IBatchView *batchView);
+  void initNewBatch(IBatchPresenter *batchPresenter);
   void changeInstrument(std::string const &instrumentName);
   void updateInstrument(const std::string &instrumentName);
 
diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/Runs/IRunsPresenter.h b/qt/scientific_interfaces/ISISReflectometry/GUI/Runs/IRunsPresenter.h
index 4411d07bdf7..1d874ad4afd 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/Runs/IRunsPresenter.h
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/Runs/IRunsPresenter.h
@@ -25,6 +25,7 @@ class IRunsPresenter {
 public:
   virtual ~IRunsPresenter() = default;
   virtual void acceptMainPresenter(IBatchPresenter *mainPresenter) = 0;
+  virtual void initInstrumentList() = 0;
   virtual RunsTable const &runsTable() const = 0;
   virtual RunsTable &mutableRunsTable() = 0;
 
diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/Runs/RunsPresenter.cpp b/qt/scientific_interfaces/ISISReflectometry/GUI/Runs/RunsPresenter.cpp
index d2082b2c987..bb75361962c 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/Runs/RunsPresenter.cpp
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/Runs/RunsPresenter.cpp
@@ -77,8 +77,9 @@ RunsPresenter::~RunsPresenter() {
  */
 void RunsPresenter::acceptMainPresenter(IBatchPresenter *mainPresenter) {
   m_mainPresenter = mainPresenter;
-  // Must do this after setting main presenter or notifications don't get
-  // through
+}
+
+void RunsPresenter::initInstrumentList() {
   m_view->setInstrumentList(m_instruments, m_defaultInstrumentIndex);
 }
 
diff --git a/qt/scientific_interfaces/ISISReflectometry/GUI/Runs/RunsPresenter.h b/qt/scientific_interfaces/ISISReflectometry/GUI/Runs/RunsPresenter.h
index 3634bb95cdb..26bff04c749 100644
--- a/qt/scientific_interfaces/ISISReflectometry/GUI/Runs/RunsPresenter.h
+++ b/qt/scientific_interfaces/ISISReflectometry/GUI/Runs/RunsPresenter.h
@@ -69,6 +69,7 @@ public:
 
   // IRunsPresenter overrides
   void acceptMainPresenter(IBatchPresenter *mainPresenter) override;
+  void initInstrumentList() override;
   RunsTable const &runsTable() const override;
   RunsTable &mutableRunsTable() override;
   bool isProcessing() const override;
diff --git a/qt/scientific_interfaces/test/ISISReflectometry/Batch/BatchPresenterTest.h b/qt/scientific_interfaces/test/ISISReflectometry/Batch/BatchPresenterTest.h
index 9cb4b2b72e0..d6fd51685b4 100644
--- a/qt/scientific_interfaces/test/ISISReflectometry/Batch/BatchPresenterTest.h
+++ b/qt/scientific_interfaces/test/ISISReflectometry/Batch/BatchPresenterTest.h
@@ -54,6 +54,13 @@ public:
     verifyAndClear();
   }
 
+  void testInitInstrumentListUpdatesRunsPresenter() {
+    auto presenter = makePresenter();
+    EXPECT_CALL(*m_runsPresenter, initInstrumentList()).Times(1);
+    presenter.initInstrumentList();
+    verifyAndClear();
+  }
+
   void testMainPresenterUpdatedWhenInstrumentChangedRequested() {
     auto presenter = makePresenter();
     auto const instrument = std::string("POLREF");
diff --git a/qt/scientific_interfaces/test/ISISReflectometry/MainWindow/MainWindowPresenterTest.h b/qt/scientific_interfaces/test/ISISReflectometry/MainWindow/MainWindowPresenterTest.h
index c61a1b126aa..892f3d48e8e 100644
--- a/qt/scientific_interfaces/test/ISISReflectometry/MainWindow/MainWindowPresenterTest.h
+++ b/qt/scientific_interfaces/test/ISISReflectometry/MainWindow/MainWindowPresenterTest.h
@@ -283,6 +283,7 @@ private:
 
   void expectBatchAdded(MockBatchPresenter *batchPresenter) {
     EXPECT_CALL(*batchPresenter, acceptMainPresenter(_)).Times(1);
+    EXPECT_CALL(*batchPresenter, initInstrumentList()).Times(1);
     EXPECT_CALL(*batchPresenter, reductionPaused()).Times(1);
     EXPECT_CALL(*batchPresenter, anyBatchAutoreductionPaused()).Times(1);
   }
diff --git a/qt/scientific_interfaces/test/ISISReflectometry/ReflMockObjects.h b/qt/scientific_interfaces/test/ISISReflectometry/ReflMockObjects.h
index e918840f1b9..76d85c9a488 100644
--- a/qt/scientific_interfaces/test/ISISReflectometry/ReflMockObjects.h
+++ b/qt/scientific_interfaces/test/ISISReflectometry/ReflMockObjects.h
@@ -55,6 +55,7 @@ public:
 class MockBatchPresenter : public IBatchPresenter {
 public:
   MOCK_METHOD1(acceptMainPresenter, void(IMainWindowPresenter *));
+  MOCK_METHOD0(initInstrumentList, void());
   MOCK_METHOD0(notifyReductionResumed, void());
   MOCK_METHOD0(notifyReductionPaused, void());
   MOCK_METHOD0(notifyAutoreductionResumed, void());
@@ -82,6 +83,7 @@ public:
 class MockRunsPresenter : public IRunsPresenter {
 public:
   MOCK_METHOD1(acceptMainPresenter, void(IBatchPresenter *));
+  MOCK_METHOD0(initInstrumentList, void());
   MOCK_CONST_METHOD0(runsTable, RunsTable const &());
   MOCK_METHOD0(mutableRunsTable, RunsTable &());
   MOCK_METHOD1(notifyInstrumentChangedRequested, void(std::string const &));
diff --git a/qt/scientific_interfaces/test/ISISReflectometry/Runs/RunsPresenterTest.h b/qt/scientific_interfaces/test/ISISReflectometry/Runs/RunsPresenterTest.h
index 52b924f46da..3fab9cc199a 100644
--- a/qt/scientific_interfaces/test/ISISReflectometry/Runs/RunsPresenterTest.h
+++ b/qt/scientific_interfaces/test/ISISReflectometry/Runs/RunsPresenterTest.h
@@ -70,12 +70,13 @@ public:
     verifyAndClear();
   }
 
-  void testCreatePresenterSetsInstrumentList() {
+  void testInitInstrumentListUpdatesView() {
     auto const defaultInstrumentIndex = 0;
+    auto presenter = makePresenter();
     EXPECT_CALL(m_view,
                 setInstrumentList(m_instruments, defaultInstrumentIndex))
         .Times(1);
-    auto presenter = makePresenter();
+    presenter.initInstrumentList();
     verifyAndClear();
   }
 
-- 
GitLab