BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
standarditemserialization.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/testintegration/standarditemserialization.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 "mvvm/model/modelutils.h"
19 #include <QDebug>
20 #include <QJsonObject>
21 
22 using namespace ModelView;
23 
24 //! Testing serialization of ToyItems using json converters.
25 
26 class StandardItemsSerializationTest : public ::testing::Test {
27 public:
29 };
30 
32 
33 //! Checking that serialization works (not crashing) for all defined standard items.
34 
36 {
37  SessionModel model;
38  model.insertItem<ColorMapItem>();
40  model.insertItem<CompoundItem>();
41  model.insertItem<ContainerItem>();
42  model.insertItem<Data1DItem>();
43  model.insertItem<Data2DItem>();
45  model.insertItem<GraphItem>();
47  model.insertItem<LinkedItem>();
48  model.insertItem<PenItem>();
50  model.insertItem<PropertyItem>();
51  model.insertItem<SessionItem>();
52  model.insertItem<TextItem>();
53  model.insertItem<VectorItem>();
55 
56  auto modelCopy = Utils::CreateCopy(model);
57  EXPECT_EQ(model.rootItem()->childrenCount(), modelCopy->rootItem()->childrenCount());
58 }
59 
60 //! Creating graph with data. It has to be identical after serialization.
61 
62 TEST_F(StandardItemsSerializationTest, GraphItemAndDataSerialization)
63 {
64  // preparing model, data item and graph pointing to it
65  SessionModel model;
66  auto graph_item = model.insertItem<GraphItem>();
67  auto data_item = model.insertItem<Data1DItem>();
68  const std::vector<double> expected_values = {1.0, 2.0, 3.0};
69  const std::vector<double> expected_centers = {0.5, 1.5, 2.5};
70  data_item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
71  data_item->setValues(expected_values);
72  graph_item->setDataItem(data_item);
73 
74  // accessing copied items
75  auto modelClone = Utils::CreateClone(model);
76  EXPECT_EQ(model.rootItem()->childrenCount(), modelClone->rootItem()->childrenCount());
77 
78  auto graphClone = modelClone->topItem<GraphItem>();
79  auto dataClone = modelClone->topItem<Data1DItem>();
80 
81  // analyzing copies
82  EXPECT_EQ(graphClone->dataItem(), dataClone);
83  EXPECT_EQ(dataClone->binCenters(), expected_centers);
84  EXPECT_EQ(dataClone->binValues(), expected_values);
85 }
86 
87 //! Creating viewport with one graph. Serializing and restoring the model.
88 
89 TEST_F(StandardItemsSerializationTest, graphViewPortItemSerialization)
90 {
91  SessionModel model;
92  auto viewport_item = model.insertItem<GraphViewportItem>();
93  auto graph_item = model.insertItem<GraphItem>(viewport_item);
94  auto data_item = model.insertItem<Data1DItem>();
95 
96  const std::vector<double> expected_values = {1.0, 2.0, 3.0};
97  const std::vector<double> expected_centers = {0.5, 1.5, 2.5};
98  data_item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
99  data_item->setValues(expected_values);
100 
101  graph_item->setDataItem(data_item);
102  EXPECT_EQ(viewport_item->graphItems().size(), 1);
103 
104  // updating viewport to graph
105  viewport_item->setViewportToContent();
106 
107  // accessing cloned items
108  auto modelClone = Utils::CreateClone(model);
109  EXPECT_EQ(model.rootItem()->childrenCount(), modelClone->rootItem()->childrenCount());
110  auto viewportCopy = modelClone->topItem<GraphViewportItem>();
111  ASSERT_EQ(viewportCopy->graphItems().size(), 1);
112  auto graphClone = viewportCopy->graphItems().at(0);
113  auto dataClone = modelClone->topItem<Data1DItem>();
114 
115  // analyzing clones
116  EXPECT_EQ(graphClone->dataItem(), dataClone);
117  EXPECT_EQ(graphClone->dataItem(), dataClone);
118  EXPECT_EQ(dataClone->binCenters(), expected_centers);
119  EXPECT_EQ(dataClone->binValues(), expected_values);
120 
121  EXPECT_DOUBLE_EQ(viewportCopy->xAxis()->property<double>(ViewportAxisItem::P_MIN),
122  expected_centers[0]);
123  EXPECT_DOUBLE_EQ(viewportCopy->xAxis()->property<double>(ViewportAxisItem::P_MAX),
124  expected_centers[2]);
125 }
static const std::string P_MAX
Definition: axisitems.h:32
static const std::string P_MIN
Definition: axisitems.h:31
Two-dimensional color map representation of Data2DItem.
Definition: colormapitem.h:28
Container with viewport and collection of ColorMapItem's to plot.
Complex item holding mixed SessionItem types (single properties and other CompountItems).
Definition: compounditem.h:28
Simple container to store any type of children.
Definition: containeritem.h:25
Represents one-dimensional data (axis and values).
Definition: data1ditem.h:30
Represents two-dimensional data (axes definition and 2d array of values).
Definition: data2ditem.h:29
Item to represent fixed bin axis.
Definition: axisitems.h:75
One-dimensional graph representation of Data1DItem.
Definition: graphitem.h:29
std::vector< double > binValues() const
Definition: graphitem.cpp:64
void setDataItem(const Data1DItem *item)
Sets link to the data item.
Definition: graphitem.cpp:34
std::vector< double > binCenters() const
Definition: graphitem.cpp:59
2D viewport specialized for showing multiple GraphItem's.
Item to store a persistent link to other arbitrary items.
Definition: linkeditem.h:30
Represents basics settings of QPen.
Item to represent pointwise axis.
Definition: axisitems.h:94
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
int childrenCount() const
Returns total number of children in all tags.
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
SessionItem * rootItem() const
Returns root item of the model.
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Definition: sessionmodel.h:104
Represent text item on plot.
Vector item with three x,y,z property items.
Definition: vectoritem.h:24
Item to represent viewport axis.
Definition: axisitems.h:43
Testing serialization of ToyItems using json converters.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
std::unique_ptr< T > CreateCopy(const T &model)
Creates full deep copy of given model. All item's ID will be generated.
Definition: modelutils.h:81
std::unique_ptr< T > CreateClone(const T &model)
Creates exact clone of given model. All item's ID will be preserved.
Definition: modelutils.h:90
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
TEST_F(StandardItemsSerializationTest, allItems)
Checking that serialization works (not crashing) for all defined standard items.