BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
graphitem.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/testmodel/graphitem.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 "google_test.h"
16 #include "mockwidgets.h"
23 #include <QColor>
24 
25 using namespace ModelView;
26 using ::testing::_;
27 
28 //! Testing GraphItem.
29 
30 class GraphItemTest : public ::testing::Test {
31 public:
33 };
34 
36 
37 //! Initial state.
38 
39 TEST_F(GraphItemTest, initialState)
40 {
41  GraphItem item;
42  EXPECT_TRUE(item.dataItem() == nullptr);
43  EXPECT_EQ(item.binCenters(), std::vector<double>{});
44  EXPECT_EQ(item.binValues(), std::vector<double>{});
45  EXPECT_EQ(item.binErrors(), std::vector<double>{});
46  EXPECT_EQ(item.colorName(), std::string("#000000"));
47 }
48 
49 //! Setting dataItem in model context.
50 
51 TEST_F(GraphItemTest, setDataItem)
52 {
53  SessionModel model;
54  auto data_item = model.insertItem<Data1DItem>();
55  auto graph_item = model.insertItem<GraphItem>();
56 
57  graph_item->setDataItem(data_item);
58 
59  EXPECT_EQ(graph_item->dataItem(), data_item);
60 }
61 
62 //! Setting dataItem in model context.
63 
65 {
66  SessionModel model;
67  auto data_item = model.insertItem<Data1DItem>();
68  auto graph_item = model.insertItem<GraphItem>();
69 
70  std::vector<double> expected_values = {1.0, 2.0, 3.0};
71  std::vector<double> expected_centers = {0.5, 1.5, 2.5};
72  data_item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
73  data_item->setValues(expected_values);
74 
75  graph_item->setDataItem(data_item);
76 
77  EXPECT_EQ(graph_item->binValues(), expected_values);
78  EXPECT_EQ(graph_item->binCenters(), expected_centers);
79 }
80 
81 //! Setting dataItem with errors
82 
84 {
85  SessionModel model;
86  auto data_item = model.insertItem<Data1DItem>();
87  auto graph_item = model.insertItem<GraphItem>();
88 
89  std::vector<double> expected_values = {1.0, 2.0, 3.0};
90  std::vector<double> expected_centers = {0.5, 1.5, 2.5};
91  std::vector<double> expected_errors = {0.1, 0.2, 0.3};
92  data_item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
93  data_item->setValues(expected_values);
94  data_item->setErrors(expected_errors);
95 
96  graph_item->setDataItem(data_item);
97 
98  EXPECT_EQ(graph_item->binValues(), expected_values);
99  EXPECT_EQ(graph_item->binCenters(), expected_centers);
100  EXPECT_EQ(graph_item->binErrors(), expected_errors);
101 }
102 
103 //! Check unlinking when nullptr is set as Data1DItem.
104 
105 TEST_F(GraphItemTest, setNullData)
106 {
107  SessionModel model;
108  auto data_item = model.insertItem<Data1DItem>();
109  auto graph_item = model.insertItem<GraphItem>();
110 
111  // preparing data item
112  std::vector<double> expected_values = {1.0, 2.0, 3.0};
113  std::vector<double> expected_centers = {0.5, 1.5, 2.5};
114  data_item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
115  data_item->setValues(expected_values);
116 
117  graph_item->setDataItem(data_item);
118  EXPECT_EQ(graph_item->dataItem(), data_item);
119 
120  // setting null as data item
121  graph_item->setDataItem(nullptr);
122  EXPECT_TRUE(graph_item->dataItem() == nullptr);
123  EXPECT_EQ(graph_item->binCenters(), std::vector<double>{});
124  EXPECT_EQ(graph_item->binValues(), std::vector<double>{});
125  EXPECT_EQ(graph_item->binErrors(), std::vector<double>{});
126 }
127 
128 //! Check signaling on set data item.
129 
130 TEST_F(GraphItemTest, onSetDataItem)
131 {
132  SessionModel model;
133  auto data_item = model.insertItem<Data1DItem>();
134  auto graph_item = model.insertItem<GraphItem>();
135 
136  MockWidgetForItem widget(graph_item);
137 
138  EXPECT_CALL(widget, onDataChange(_, _)).Times(0);
139  EXPECT_CALL(widget, onPropertyChange(graph_item, GraphItem::P_LINK)).Times(1);
140  EXPECT_CALL(widget, onChildPropertyChange(_, _)).Times(0);
141  EXPECT_CALL(widget, onItemInserted(_, _)).Times(0);
142  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
143 
144  // performing action
145  graph_item->setDataItem(data_item);
146 }
147 
148 //! Sets GraphItem from another GraphItem
149 
150 TEST_F(GraphItemTest, setFromGraphItem)
151 {
152  SessionModel model;
153  auto data_item = model.insertItem<Data1DItem>();
154  auto graph_item = model.insertItem<GraphItem>();
155  auto graph_item2 = model.insertItem<GraphItem>();
156 
157  std::vector<double> expected_values = {1.0, 2.0, 3.0};
158  std::vector<double> expected_centers = {0.5, 1.5, 2.5};
159  data_item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
160  data_item->setValues(expected_values);
161 
162  graph_item->setDataItem(data_item);
163  graph_item->penItem()->setProperty(PenItem::P_COLOR, QColor(Qt::red));
164 
165  graph_item2->setFromGraphItem(graph_item);
166 
167  EXPECT_EQ(graph_item2->binValues(), expected_values);
168  EXPECT_EQ(graph_item2->binCenters(), expected_centers);
169  EXPECT_EQ(graph_item2->penItem()->property<QColor>(PenItem::P_COLOR), QColor(Qt::red));
170 }
171 
172 TEST_F(GraphItemTest, penItem_setNamedColor)
173 {
174  GraphItem item;
175  item.setNamedColor("mediumaquamarine");
176  EXPECT_EQ(item.colorName(), std::string("#66cdaa"));
177 }
Defines class CLASS?
Testing GraphItem.
Mock widget to test ItemMapper functionality.
Definition: mockwidgets.h:36
Represents one-dimensional data (axis and values).
Definition: data1ditem.h:30
std::vector< double > binValues() const
Returns values stored in bins.
Definition: data1ditem.cpp:76
std::vector< double > binCenters() const
Sets axis. Bin content will be set to zero.
Definition: data1ditem.cpp:57
std::vector< double > binErrors() const
Returns value errors stored in bins.
Definition: data1ditem.cpp:93
Item to represent fixed bin axis.
Definition: axisitems.h:75
One-dimensional graph representation of Data1DItem.
Definition: graphitem.h:29
std::string colorName() const
Returns color name in #RRGGBB format.
Definition: graphitem.cpp:76
std::vector< double > binValues() const
Definition: graphitem.cpp:64
Data1DItem * dataItem() const
Returns data item linked to the given GraphItem.
Definition: graphitem.cpp:54
PenItem * penItem() const
Definition: graphitem.cpp:89
std::vector< double > binCenters() const
Definition: graphitem.cpp:59
void setFromGraphItem(const GraphItem *graph_item)
Update item from the content of given graph.
Definition: graphitem.cpp:42
void setNamedColor(const std::string &named_color)
Sets named color following schema from https://www.w3.org/TR/css-color-3/#svg-color.
Definition: graphitem.cpp:84
static const std::string P_LINK
Definition: graphitem.h:31
std::vector< double > binErrors() const
Definition: graphitem.cpp:69
static const std::string P_COLOR
T property(const std::string &tag) const
Returns data stored in property item.
Definition: sessionitem.h:181
void setProperty(const std::string &tag, const T &value)
Sets value to property item.
Definition: sessionitem.h:190
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(GraphItemTest, initialState)
Initial state.
Defines class CLASS?
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.
Defines class CLASS?
Defines class CLASS?