BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
graphviewportitem.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/graphviewportitem.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"
22 
23 using namespace ModelView;
24 using ::testing::_;
25 
26 //! Testing AxesItems.
27 
28 class GraphViewportItemTest : public ::testing::Test {
29 public:
31 };
32 
34 
35 //! Initial state.
36 
38 {
39  GraphViewportItem item;
40  EXPECT_EQ(item.xAxis()->modelType(), Constants::ViewportAxisItemType);
41  EXPECT_EQ(item.yAxis()->modelType(), Constants::ViewportAxisItemType);
42  EXPECT_EQ(item.graphItems().size(), 0);
43 }
44 
45 //! Add graph to viewport.
46 
48 {
49  SessionModel model;
50 
51  auto viewport_item = model.insertItem<GraphViewportItem>();
52  auto graph_item = model.insertItem<GraphItem>(viewport_item);
53  auto data_item = model.insertItem<Data1DItem>();
54 
55  const std::vector<double> expected_values = {1.0, 2.0, 3.0};
56  const std::vector<double> expected_centers = {0.5, 1.5, 2.5};
57  data_item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
58  data_item->setValues(expected_values);
59 
60  graph_item->setDataItem(data_item);
61  EXPECT_EQ(viewport_item->graphItems().size(), 1);
62 
63  // updating viewport to graph
64  viewport_item->setViewportToContent();
65 
66  // x-axis of viewport should be set to FixedBinAxis of DataItem
67  auto xaxis = viewport_item->xAxis();
68  EXPECT_DOUBLE_EQ(xaxis->property<double>(ViewportAxisItem::P_MIN), expected_centers[0]);
69  EXPECT_DOUBLE_EQ(xaxis->property<double>(ViewportAxisItem::P_MAX), expected_centers[2]);
70 
71  // y-axis of viewport should be set to min/max of expected_content
72  auto yaxis = viewport_item->yAxis();
73  auto [expected_amin, expected_amax] =
74  std::minmax_element(std::begin(expected_values), std::end(expected_values));
75  EXPECT_DOUBLE_EQ(yaxis->property<double>(ViewportAxisItem::P_MIN), *expected_amin);
76  EXPECT_DOUBLE_EQ(yaxis->property<double>(ViewportAxisItem::P_MAX), *expected_amax);
77 }
78 
79 //! Check signaling on set data item.
80 
82 {
83  SessionModel model;
84  auto viewport_item = model.insertItem<GraphViewportItem>();
85 
86  MockWidgetForItem widget(viewport_item);
87 
88  const TagRow expected_tagrow{ViewportItem::T_ITEMS, 0};
89  EXPECT_CALL(widget, onDataChange(_, _)).Times(0);
90  EXPECT_CALL(widget, onPropertyChange(_, _)).Times(0);
91  EXPECT_CALL(widget, onChildPropertyChange(_, _)).Times(0);
92  EXPECT_CALL(widget, onItemInserted(viewport_item, expected_tagrow)).Times(1);
93  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
94 
95  // triggering action
96  model.insertItem<GraphItem>(viewport_item);
97 }
98 
99 //! Check signaling on set data item.
100 
102 {
103  SessionModel model;
104  auto viewport_item = model.insertItem<GraphViewportItem>();
105 
106  // setting upda tata item
107  auto data_item = model.insertItem<Data1DItem>();
108  const std::vector<double> expected_values = {1.0, 2.0, 3.0};
109  const std::vector<double> expected_centers = {0.5, 1.5, 2.5};
110  data_item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
111  data_item->setValues(expected_values);
112 
113  // inserting graph item
114  auto graph_item = model.insertItem<GraphItem>(viewport_item);
115 
116  MockWidgetForItem widget(viewport_item);
117 
118  EXPECT_CALL(widget, onDataChange(_, _)).Times(0);
119  EXPECT_CALL(widget, onPropertyChange(_, _)).Times(0);
120  EXPECT_CALL(widget, onChildPropertyChange(graph_item, GraphItem::P_LINK)).Times(1);
121  EXPECT_CALL(widget, onItemInserted(_, _)).Times(0);
122  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
123 
124  // triggering action
125  graph_item->setDataItem(data_item);
126 }
127 
128 //! Add graph to viewport.
129 
130 TEST_F(GraphViewportItemTest, setViewportToContentWithMargins)
131 {
132  SessionModel model;
133 
134  auto viewport_item = model.insertItem<GraphViewportItem>();
135  auto graph_item = model.insertItem<GraphItem>(viewport_item);
136  auto data_item = model.insertItem<Data1DItem>();
137 
138  const std::vector<double> expected_values = {1.0, 2.0, 3.0};
139  const std::vector<double> expected_centers = {0.5, 1.5, 2.5};
140  data_item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
141  data_item->setValues(expected_values);
142 
143  graph_item->setDataItem(data_item);
144  EXPECT_EQ(viewport_item->graphItems().size(), 1);
145 
146  // updating viewport to graph
147  const double bottom{0.1}, top{0.1};
148  viewport_item->setViewportToContent(0.0, top, 0.0, bottom);
149 
150  // x-axis of viewport should be set to FixedBinAxis of DataItem
151  auto xaxis = viewport_item->xAxis();
152  EXPECT_DOUBLE_EQ(xaxis->property<double>(ViewportAxisItem::P_MIN), expected_centers[0]);
153  EXPECT_DOUBLE_EQ(xaxis->property<double>(ViewportAxisItem::P_MAX), expected_centers[2]);
154 
155  // y-axis of viewport should be set to min/max of expected_content
156  auto yaxis = viewport_item->yAxis();
157  auto [expected_amin, expected_amax] =
158  std::minmax_element(std::begin(expected_values), std::end(expected_values));
159 
160  double expected_ymin = *expected_amin - (*expected_amax - *expected_amin) * bottom;
161  double expected_ymax = *expected_amax + (*expected_amax - *expected_amin) * top;
162  EXPECT_DOUBLE_EQ(yaxis->property<double>(ViewportAxisItem::P_MIN), expected_ymin);
163  EXPECT_DOUBLE_EQ(yaxis->property<double>(ViewportAxisItem::P_MAX), expected_ymax);
164 }
Defines class CLASS?
Mock widget to test ItemMapper functionality.
Definition: mockwidgets.h:36
static const std::string P_MAX
Definition: axisitems.h:32
static const std::string P_MIN
Definition: axisitems.h:31
Represents one-dimensional data (axis and values).
Definition: data1ditem.h:30
Item to represent fixed bin axis.
Definition: axisitems.h:75
One-dimensional graph representation of Data1DItem.
Definition: graphitem.h:29
static const std::string P_LINK
Definition: graphitem.h:31
2D viewport specialized for showing multiple GraphItem's.
std::vector< GraphItem * > graphItems() const
Returns the selected graph items.
model_type modelType() const
Returns item's model type.
Definition: sessionitem.cpp:80
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
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
ViewportAxisItem * xAxis() const
static const std::string T_ITEMS
Definition: viewportitem.h:31
ViewportAxisItem * yAxis() const
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(GraphViewportItemTest, initialState)
Initial state.
Defines class CLASS?
const model_type ViewportAxisItemType
Definition: mvvm_types.h:62
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?