Skip to content

Commit

Permalink
Refs #6541. Adding rebinning presenter tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Reuter committed Feb 21, 2013
1 parent 7eaed0a commit 3424501
Showing 1 changed file with 139 additions and 1 deletion.
140 changes: 139 additions & 1 deletion Code/Mantid/Vates/VatesAPI/test/MDEWRebinningPresenterTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include "MantidVatesAPI/RebinningKnowledgeSerializer.h"
#include "MantidVatesAPI/ADSWorkspaceProvider.h"
#include "MantidVatesAPI/vtkMDHistoHexFactory.h"
#include "MantidVatesAPI/vtkMDHistoHex4DFactory.h"
#include "MantidVatesAPI/NoThresholdRange.h"
#include "MantidVatesAPI/TimeToTimeStep.h"
#include "MantidVatesAPI/EscalatingRebinningActionManager.h"
#include "MantidVatesAPI/vtkDataSetToGeometry.h"
#include "MantidTestHelpers/MDEventsTestHelper.h"
Expand Down Expand Up @@ -462,7 +464,143 @@ class MDEWRebinningPresenterTest : public CxxTest::TestSuite
// NOTE. if you wanted to Extract the field data. You could do that here by fetching it off output product vtkDataSet.
}

void testAxisLabelsAfterRebinFor3DData()
{
// Create a MDWorkspace and put it into the ADS.
const std::string wsName = "TestMDEW";
auto someMDEW = Mantid::MDEvents::MDEventsTestHelper::makeAnyMDEW<MDLeanEvent<3>,3>(10,0,10);
someMDEW->setName(wsName);
Mantid::API::AnalysisDataService::Instance().addOrReplace(wsName, someMDEW);

// Generate xml relating to the dimensionality etc. by querying this workspace.
RebinningKnowledgeSerializer serializer(LocationNotRequired);
serializer.setWorkspace(someMDEW);
std::string creationalXML = serializer.createXMLString();

// Create an empty dataset and attach that xml as field data.
vtkUnstructuredGrid* dataSet = vtkUnstructuredGrid::New();
dataSet->SetFieldData(createFieldDataWithCharArray(creationalXML));

/*
The vtkFilter is the view in our MVP set up.
We can't actually create an instance of the vtkFilter for testing, which would otherwise make it impossible to test any of this functionality.
However, we can mock a View, because both the real filter and our Mock inherite from MDRebinningView. Using this view we 'simulate' real user inputs.
*/
MockMDRebinningView* view = new MockMDRebinningView;
EXPECT_CALL(*view, getOutputHistogramWS()).WillRepeatedly(Return(true));
EXPECT_CALL(*view, getTimeStep()).WillRepeatedly(Return(0));
EXPECT_CALL(*view, getMaxThreshold()).WillRepeatedly(Return(0));
EXPECT_CALL(*view, getMinThreshold()).WillRepeatedly(Return(0));
EXPECT_CALL(*view, getApplyClip()).WillRepeatedly(Return(false));
/*
The following is the critical method. It returns the xml from the current state of the view.
At the moment, the view is going to return the same xml as it was originally given. However, we can override this behaviour and provide
any xml we wish. So for example, simulating that the user has increased the number of bins.
*/
std::string viewXML = constructGeometryOnlyXMLForMDEvHelperData("Axis2",
"Axis0",
"Axis1",
"");
EXPECT_CALL(*view, getAppliedGeometryXML()).WillRepeatedly(Return(viewXML.c_str()));

// The workspace provider is a proxy to the Analysis Data Service.
ADSWorkspaceProvider<Mantid::API::IMDEventWorkspace> workspaceProvider;

// The request is the way that the rebinner stores what action to take when the user hits 'Apply'/
RebinningActionManager* pRequest = new EscalatingRebinningActionManager;

// Create a presenter which binds the Model and the View together.
MDEWRebinningPresenter presenter(dataSet, pRequest, view, workspaceProvider);
pRequest->ask(Mantid::VATES::RecalculateAll); // Force it to rebin. Usually we call updateModel first, which figures out what action needs to be taken (rebin, just redraw e.t.c).

// A progress action is the mechanism by which progress is reported.
NiceMock<MockProgressAction> progressAction;
// Create a factory for visualising the output workspace after rebinning. We only provide one here, because we know that it's going to be a 3D MDHistoWorkspace from BinMD.
auto vtkFactory = new vtkMDHistoHexFactory(ThresholdRange_scptr(new NoThresholdRange), "signal");
vtkDataSet* product = presenter.execute(vtkFactory, progressAction, progressAction);

// NOTE. if you wanted to Extract the field data. You could do that here by fetching it off output product vtkDataSet.
TSM_ASSERT_THROWS_NOTHING("Should pass", presenter.setAxisLabels(product));
TSM_ASSERT_EQUALS("X Label should match exactly",
getStringFieldDataValue(product, "AxisTitleForX"),
"Axis2 (m)");
TSM_ASSERT_EQUALS("Y Label should match exactly",
getStringFieldDataValue(product, "AxisTitleForY"),
"Axis0 (m)");
TSM_ASSERT_EQUALS("Z Label should match exactly",
getStringFieldDataValue(product, "AxisTitleForZ"),
"Axis1 (m)");
}

void testAxisLabelsAfterRebinFor4DData()
{
// Create a MDWorkspace and put it into the ADS.
const std::string wsName = "TestMDEW";
auto someMDEW = Mantid::MDEvents::MDEventsTestHelper::makeAnyMDEW<MDLeanEvent<4>,4>(10,0,10);
someMDEW->setName(wsName);
Mantid::API::AnalysisDataService::Instance().addOrReplace(wsName, someMDEW);

// Generate xml relating to the dimensionality etc. by querying this workspace.
RebinningKnowledgeSerializer serializer(LocationNotRequired);
serializer.setWorkspace(someMDEW);
std::string creationalXML = serializer.createXMLString();

// Create an empty dataset and attach that xml as field data.
vtkUnstructuredGrid* dataSet = vtkUnstructuredGrid::New();
dataSet->SetFieldData(createFieldDataWithCharArray(creationalXML));

/*
The vtkFilter is the view in our MVP set up.
We can't actually create an instance of the vtkFilter for testing, which would otherwise make it impossible to test any of this functionality.
However, we can mock a View, because both the real filter and our Mock inherite from MDRebinningView. Using this view we 'simulate' real user inputs.
*/
MockMDRebinningView* view = new MockMDRebinningView;
EXPECT_CALL(*view, getOutputHistogramWS()).WillRepeatedly(Return(true));
EXPECT_CALL(*view, getTimeStep()).WillRepeatedly(Return(0));
EXPECT_CALL(*view, getMaxThreshold()).WillRepeatedly(Return(0));
EXPECT_CALL(*view, getMinThreshold()).WillRepeatedly(Return(0));
EXPECT_CALL(*view, getApplyClip()).WillRepeatedly(Return(false));
/*
The following is the critical method. It returns the xml from the current state of the view.
At the moment, the view is going to return the same xml as it was originally given. However, we can override this behaviour and provide
any xml we wish. So for example, simulating that the user has increased the number of bins.
*/
std::string viewXML = constructGeometryOnlyXMLForMDEvHelperData("Axis3",
"Axis2",
"Axis1",
"Axis0");
EXPECT_CALL(*view, getAppliedGeometryXML()).WillRepeatedly(Return(viewXML.c_str()));

// The workspace provider is a proxy to the Analysis Data Service.
ADSWorkspaceProvider<Mantid::API::IMDEventWorkspace> workspaceProvider;

// The request is the way that the rebinner stores what action to take when the user hits 'Apply'/
RebinningActionManager* pRequest = new EscalatingRebinningActionManager;

// Create a presenter which binds the Model and the View together.
MDEWRebinningPresenter presenter(dataSet, pRequest, view, workspaceProvider);
pRequest->ask(Mantid::VATES::RecalculateAll); // Force it to rebin. Usually we call updateModel first, which figures out what action needs to be taken (rebin, just redraw e.t.c).

// A progress action is the mechanism by which progress is reported.
NiceMock<MockProgressAction> progressAction;
// Create a factory for visualising the output workspace after rebinning. We only provide one here, because we know that it's going to be a 3D MDHistoWorkspace from BinMD.
auto vtkFactory = new vtkMDHistoHex4DFactory<TimeToTimeStep>(ThresholdRange_scptr(new NoThresholdRange), "signal", 0.0);
vtkDataSet* product = presenter.execute(vtkFactory, progressAction, progressAction);

// NOTE. if you wanted to Extract the field data. You could do that here by fetching it off output product vtkDataSet.
TSM_ASSERT_THROWS_NOTHING("Should pass", presenter.setAxisLabels(product));
TSM_ASSERT_EQUALS("X Label should match exactly",
getStringFieldDataValue(product, "AxisTitleForX"),
"Axis3 (s)");
TSM_ASSERT_EQUALS("Y Label should match exactly",
getStringFieldDataValue(product, "AxisTitleForY"),
"Axis2 (m)");
TSM_ASSERT_EQUALS("Z Label should match exactly",
getStringFieldDataValue(product, "AxisTitleForZ"),
"Axis1 (m)");
}
};

#endif
#endif

0 comments on commit 3424501

Please sign in to comment.