BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
data1dplotcontroller.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/data1dplotcontroller.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 <algorithm>
23 #include <stdexcept>
24 
25 using namespace ModelView;
26 
27 //! Testing Data1DPlotController.
28 
29 class Data1DPlotControllerTest : public ::testing::Test {
30 public:
32 };
33 
35 
36 //! Initial state.
37 
39 {
40  // Constructor accept valid QCPGraph
41  EXPECT_THROW(Data1DPlotController(nullptr), std::runtime_error);
42 
43  auto custom_plot = std::make_unique<QCustomPlot>();
44  auto graph = custom_plot->addGraph();
45 
46  Data1DPlotController controller(graph);
47  EXPECT_EQ(controller.currentItem(), nullptr);
48 
49  // no points have been added to graph
50  EXPECT_EQ(std::vector<double>(), TestUtils::binCenters(graph));
51  EXPECT_EQ(std::vector<double>(), TestUtils::binValues(graph));
52 }
53 
54 //! Testing controller when Data1DItem is not initialized properly.
55 
56 TEST_F(Data1DPlotControllerTest, dataItemInInitialState)
57 {
58  // creating custom plot and empty graph on it
59  auto custom_plot = std::make_unique<QCustomPlot>();
60  auto graph = custom_plot->addGraph();
61 
62  // creating data item with single point
63  SessionModel model;
64  auto data_item = model.insertItem<Data1DItem>();
65 
66  // creating controller and point it to Data1DItem
67  Data1DPlotController controller(graph);
68  controller.setItem(data_item);
69 
70  EXPECT_EQ(std::vector<double>(), TestUtils::binCenters(graph));
71  EXPECT_EQ(std::vector<double>(), TestUtils::binValues(graph));
72  EXPECT_EQ(std::vector<double>(), TestUtils::binErrors(graph));
73 }
74 
75 //! Testing controller when Data1DItem get it's axis after controller setup.
76 
78 {
79  // creating custom plot and empty graph on it
80  auto custom_plot = std::make_unique<QCustomPlot>();
81  auto graph = custom_plot->addGraph();
82 
83  // creating data item with single point
84  SessionModel model;
85  auto data_item = model.insertItem<Data1DItem>();
86 
87  // creating controller and point it to Data1DItem
88  Data1DPlotController controller(graph);
89  controller.setItem(data_item);
90 
91  // setting correct axis
92  data_item->setAxis<FixedBinAxisItem>(1, 1.0, 2.0);
93  EXPECT_EQ(data_item->binCenters(), TestUtils::binCenters(graph));
94  EXPECT_EQ(data_item->binValues(), TestUtils::binValues(graph));
95  EXPECT_EQ(std::vector<double>(), TestUtils::binErrors(graph));
96 }
97 
98 //! Testing graph points update.
99 
101 {
102  // creating custom plot and empty graph on it
103  auto custom_plot = std::make_unique<QCustomPlot>();
104  auto graph = custom_plot->addGraph();
105 
106  // creating data item with single point
107  SessionModel model;
108  auto data_item = model.insertItem<Data1DItem>();
109  data_item->setAxis<FixedBinAxisItem>(1, 1.0, 2.0);
110 
111  // creating controller and point it to Data1DItem
112  Data1DPlotController controller(graph);
113  controller.setItem(data_item);
114 
115  // checking that QCPGraph now has data points as in Data1DItem
116  EXPECT_EQ(data_item->binCenters(), TestUtils::binCenters(graph));
117  EXPECT_EQ(data_item->binValues(), TestUtils::binValues(graph));
118  EXPECT_EQ(data_item->binErrors(), TestUtils::binErrors(graph));
119 
120  // Setting item to nullptr. Current convention is that graph stays intact, but points disappear.
121  controller.setItem(nullptr);
122  EXPECT_EQ(std::vector<double>(), TestUtils::binCenters(graph));
123  EXPECT_EQ(std::vector<double>(), TestUtils::binValues(graph));
124  EXPECT_EQ(std::vector<double>(), TestUtils::binErrors(graph));
125 }
126 
127 //! Testing graph errors update.
128 
130 {
131  // creating custom plot and empty graph on it
132  auto custom_plot = std::make_unique<QCustomPlot>();
133  auto graph = custom_plot->addGraph();
134 
135  // creating data item with single point
136  SessionModel model;
137  auto data_item = model.insertItem<Data1DItem>();
138  data_item->setAxis<FixedBinAxisItem>(2, 1.0, 2.0);
139 
140  // creating controller and point it to Data1DItem
141  Data1DPlotController controller(graph);
142  controller.setItem(data_item);
143 
144  std::vector<double> expected_errors = {0.1, 0.2};
145  data_item->setErrors(expected_errors);
146  EXPECT_EQ(TestUtils::binErrors(graph), expected_errors);
147 
148  // setting new errors
149  expected_errors = {0.3, 0.4};
150  data_item->setErrors(expected_errors);
151  EXPECT_EQ(TestUtils::binErrors(graph), expected_errors);
152 }
153 
154 //! Testing two graph scenario.
155 
157 {
158  // creating custom plot and empty graph on it
159  auto custom_plot = std::make_unique<QCustomPlot>();
160  auto graph = custom_plot->addGraph();
161 
162  // creating two data items
163  SessionModel model;
164  auto data_item1 = model.insertItem<Data1DItem>();
165  data_item1->setAxis<FixedBinAxisItem>(1, 1.0, 2.0);
166  auto data_item2 = model.insertItem<Data1DItem>();
167  data_item2->setAxis<FixedBinAxisItem>(2, 0.0, 2.0);
168 
169  // creating controller and point it to first item
170  Data1DPlotController controller(graph);
171  controller.setItem(data_item1);
172 
173  // checking that QCPGraph now has data points as in first data item
174  EXPECT_EQ(data_item1->binCenters(), TestUtils::binCenters(graph));
175  EXPECT_EQ(data_item1->binValues(), TestUtils::binValues(graph));
176 
177  // pointing controller to the second item
178  controller.setItem(data_item2);
179  EXPECT_EQ(data_item2->binCenters(), TestUtils::binCenters(graph));
180  EXPECT_EQ(data_item2->binValues(), TestUtils::binValues(graph));
181 }
Defines class CLASS?
Testing Data1DPlotController.
Represents one-dimensional data (axis and values).
Definition: data1ditem.h:30
T * setAxis(Args &&... args)
Inserts axis of given type.
Definition: data1ditem.h:56
Establishes communication between QCPGraph and Data1DItem.
Item to represent fixed bin axis.
Definition: axisitems.h:75
std::vector< double > binCenters() const override
Definition: axisitems.cpp:98
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(Data1DPlotControllerTest, initialState)
Initial state.
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
std::vector< double > binValues(const QCPGraph *graph)
Returns vector representing y-values on QCPgraph.
std::vector< double > binErrors(const QCPGraph *graph)
Returns vector representing bin errors of QCPGraph.
std::vector< double > binCenters(const QCPGraph *graph)
Returns vector representing bin centers on QCPgraph.
Defines class CLASS?