BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
data2dplotcontroller.test.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/tests/testview/data2dplotcontroller.test.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "customplot_test_utils.h"
16 #include "google_test.h"
21 #include "qcustomplot.h"
22 #include <QSignalSpy>
23 #include <stdexcept>
24 
25 using namespace ModelView;
26 
27 //! Testing Data1DPlotController.
28 
29 class Data2DPlotControllerTest : public ::testing::Test {
30 public:
32 };
33 
35 
36 //! Initial state.
37 
39 {
40  // Constructor accept valid QCPColorMap
41  EXPECT_THROW(Data2DPlotController(nullptr), std::runtime_error);
42 
43  auto custom_plot = std::make_unique<QCustomPlot>();
44  auto color_map = new QCPColorMap(custom_plot->xAxis, custom_plot->yAxis);
45  color_map->data()->clear(); // to remove default values defined in QCPColorMap
46 
47  Data2DPlotController controller(color_map);
48  EXPECT_EQ(controller.currentItem(), nullptr);
49 
50  EXPECT_EQ(color_map->data()->keySize(), 0);
51  EXPECT_EQ(color_map->data()->valueSize(), 0);
52 }
53 
54 //! Testing controller when Data2DItem is not initialized properly.
55 
56 TEST_F(Data2DPlotControllerTest, dataItemInInitialState)
57 {
58  // creating custom plot and empty graph on it
59  auto custom_plot = std::make_unique<QCustomPlot>();
60  auto color_map = new QCPColorMap(custom_plot->xAxis, custom_plot->yAxis);
61 
62  // creating data item with single point
63  SessionModel model;
64  auto data_item = model.insertItem<Data2DItem>();
65 
66  // creating controller and point it to Data2DItem
67  Data2DPlotController controller(color_map);
68  EXPECT_NO_THROW(controller.setItem(data_item));
69 
70  // Since data item doesn't contain axes defined, should be no points on colormap.
71  EXPECT_EQ(color_map->data()->keySize(), 0);
72  EXPECT_EQ(color_map->data()->valueSize(), 0);
73 }
74 
75 //! Testing controller when Data2DItem got it's axes after controller was set.
76 
78 {
79  // creating custom plot and empty graph on it
80  auto custom_plot = std::make_unique<QCustomPlot>();
81  auto color_map = new QCPColorMap(custom_plot->xAxis, custom_plot->yAxis);
82 
83  // creating data item with single point
84  SessionModel model;
85  auto data_item = model.insertItem<Data2DItem>();
86 
87  // creating controller and point it to Data2DItem
88  Data2DPlotController controller(color_map);
89  EXPECT_NO_THROW(controller.setItem(data_item));
90 
91  // setting axes after
92  const int nx = 3, ny = 2;
93  data_item->setAxes(FixedBinAxisItem::create(nx, 0.0, 3.0),
94  FixedBinAxisItem::create(ny, 0.0, 2.0));
95 
96  // color map should get shape of axes
97  EXPECT_EQ(color_map->data()->keySize(), nx);
98  EXPECT_EQ(color_map->data()->valueSize(), ny);
99  EXPECT_EQ(color_map->data()->cell(0, 0), 0.0);
100  EXPECT_EQ(color_map->data()->cell(nx - 1, ny - 1), 0.0);
101 }
102 
103 //! Testing data points.
104 
106 {
107  // creating custom plot and empty graph on it
108  auto custom_plot = std::make_unique<QCustomPlot>();
109  auto color_map = new QCPColorMap(custom_plot->xAxis, custom_plot->yAxis);
110 
111  // creating data item with single point
112  SessionModel model;
113  auto data_item = model.insertItem<Data2DItem>();
114  const int nx = 3, ny = 2;
115  data_item->setAxes(FixedBinAxisItem::create(nx, 0.0, 3.0),
116  FixedBinAxisItem::create(ny, 0.0, 2.0));
117 
118  // creating controller and point it to Data2DItem
119  Data2DPlotController controller(color_map);
120  controller.setItem(data_item);
121 
122  EXPECT_EQ(color_map->data()->keySize(), nx);
123  EXPECT_EQ(color_map->data()->valueSize(), ny);
124  EXPECT_EQ(color_map->data()->cell(0, 0), 0.0);
125  EXPECT_EQ(color_map->data()->cell(nx - 1, ny - 1), 0.0);
126 
127  // setting data points
128  std::vector<double> expected = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
129  data_item->setContent(expected);
130  EXPECT_EQ(color_map->data()->cell(0, 0), 1.0);
131  EXPECT_EQ(color_map->data()->cell(nx - 1, ny - 1), 6.0);
132 
133  // Setting item to nullptr. Current convention is that QCPColorMap loses its data.
134  controller.setItem(nullptr);
135  EXPECT_EQ(color_map->data()->keySize(), 0);
136  EXPECT_EQ(color_map->data()->valueSize(), 0);
137 }
138 
139 //! Testing two colormap scenario.
140 
142 {
143  // creating custom plot and empty graph on it
144  auto custom_plot = std::make_unique<QCustomPlot>();
145  auto color_map = new QCPColorMap(custom_plot->xAxis, custom_plot->yAxis);
146 
147  // creating data item with single point
148  SessionModel model;
149  auto data_item1 = model.insertItem<Data2DItem>();
150  const int nx1 = 3, ny1 = 2;
151  std::vector<double> expected1 = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
152  data_item1->setAxes(FixedBinAxisItem::create(nx1, 0.0, 3.0),
153  FixedBinAxisItem::create(ny1, 0.0, 2.0));
154  data_item1->setContent(expected1);
155  auto data_item2 = model.insertItem<Data2DItem>();
156  const int nx2 = 2, ny2 = 1;
157  std::vector<double> expected2 = {10.0, 20.0};
158  data_item2->setAxes(FixedBinAxisItem::create(nx2, 0.0, 3.0),
159  FixedBinAxisItem::create(ny2, 0.0, 2.0));
160  data_item2->setContent(expected2);
161 
162  // creating controller and point it to first Data2DItem
163  Data2DPlotController controller(color_map);
164  controller.setItem(data_item1);
165 
166  EXPECT_EQ(color_map->data()->keySize(), nx1);
167  EXPECT_EQ(color_map->data()->valueSize(), ny1);
168  EXPECT_EQ(color_map->data()->cell(0, 0), 1.0);
169  EXPECT_EQ(color_map->data()->cell(nx1 - 1, ny1 - 1), 6.0);
170 
171  // pointing controller to the second Data2DItem
172  controller.setItem(data_item2);
173 
174  EXPECT_EQ(color_map->data()->keySize(), nx2);
175  EXPECT_EQ(color_map->data()->valueSize(), ny2);
176  EXPECT_EQ(color_map->data()->cell(0, 0), 10.0);
177  EXPECT_EQ(color_map->data()->cell(nx2 - 1, ny2 - 1), 20.0);
178 }
179 
180 //! Testing data range.
181 
183 {
184  // creating custom plot and empty graph on it
185  auto custom_plot = std::make_unique<QCustomPlot>();
186  auto color_map = new QCPColorMap(custom_plot->xAxis, custom_plot->yAxis);
187 
188  // creating data item with single point
189  SessionModel model;
190  auto data_item = model.insertItem<Data2DItem>();
191  const int nx = 3, ny = 2;
192  data_item->setAxes(FixedBinAxisItem::create(nx, 0.0, 3.0),
193  FixedBinAxisItem::create(ny, 0.0, 2.0));
194  std::vector<double> expected = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
195  data_item->setContent(expected);
196 
197  // creating controller and point it to Data2DItem
198  Data2DPlotController controller(color_map);
199  controller.setItem(data_item);
200 
201  QSignalSpy spy(color_map, &QCPColorMap::dataRangeChanged);
202 
203  auto range = color_map->dataRange();
204  EXPECT_EQ(spy.count(), 0);
205  EXPECT_EQ(range.lower, 1.0);
206  EXPECT_EQ(range.upper, 6.0);
207 }
Defines class CLASS?
Testing Data1DPlotController.
Represents two-dimensional data (axes definition and 2d array of values).
Definition: data2ditem.h:29
void setAxes(std::unique_ptr< BinnedAxisItem > x_axis, std::unique_ptr< BinnedAxisItem > y_axis)
Sets axes and put data points to zero.
Definition: data2ditem.cpp:41
Establish communication between QCPColorMap and Data2DItem.
static std::unique_ptr< FixedBinAxisItem > create(int nbins, double xmin, double xmax)
Definition: axisitems.cpp:81
void setItem(SessionItem *item)
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Definition: sessionmodel.h:104
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(Data2DPlotControllerTest, initialState)
Initial state.
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?