BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
itemconverterfactory.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/itemconverterfactory.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"
20 #include "mvvm/model/sessionitem.h"
23 #include "test_utils.h"
24 #include <QJsonObject>
25 
26 using namespace ModelView;
27 
28 //! Checks converters generated by ItemFactoryConverter.
29 
30 class ItemConverterFactoryTest : public ::testing::Test {
31 public:
32  class TestItem : public CompoundItem {
33  public:
34  TestItem() : CompoundItem("TestItem")
35  {
36  setToolTip("compound");
37  addProperty("Thickness", 42)->setToolTip("thickness")->setEditorType("abc");
38  }
39  };
40 
41  class TestModel : public SessionModel {
42  public:
43  TestModel() : SessionModel("TestModel")
44  {
45  auto catalogue = std::make_unique<ModelView::ItemCatalogue>();
46  catalogue->registerItem<TestItem>();
47  setItemCatalogue(std::move(catalogue));
48  }
49  };
50 
51  ItemConverterFactoryTest() : m_model(std::make_unique<TestModel>()) {}
52 
53  const ItemFactoryInterface* factory() const { return m_model->factory(); }
54 
55  std::unique_ptr<SessionModel> m_model;
56 };
57 
58 //! Clone converter for simple property item.
59 
60 TEST_F(ItemConverterFactoryTest, propertyItemCloneConverter)
61 {
62  auto converter = CreateItemCloneConverter(factory());
63 
64  PropertyItem item;
65  item.setToolTip("abc");
66 
67  auto object = converter->to_json(&item);
68  auto reco = converter->from_json(object);
69 
70  EXPECT_EQ(reco->modelType(), item.modelType());
71  EXPECT_EQ(reco->displayName(), item.displayName());
72  EXPECT_EQ(reco->toolTip(), std::string("abc"));
73  EXPECT_EQ(reco->itemData()->roles(), item.itemData()->roles());
74  EXPECT_TRUE(reco->identifier() == item.identifier()); // identifier preserved
75 }
76 
77 //! Copy converter for simple property item.
78 
79 TEST_F(ItemConverterFactoryTest, propertyItemCopyConverter)
80 {
81  auto converter = CreateItemCopyConverter(factory());
82 
83  PropertyItem item;
84  item.setToolTip("abc");
85 
86  auto object = converter->to_json(&item);
87  auto reco = converter->from_json(object);
88 
89  EXPECT_EQ(reco->modelType(), item.modelType());
90  EXPECT_EQ(reco->displayName(), item.displayName());
91  EXPECT_EQ(reco->toolTip(), std::string("abc"));
92  EXPECT_EQ(reco->itemData()->roles(), item.itemData()->roles());
93  EXPECT_FALSE(reco->identifier() == item.identifier()); // identifier has changed
94 }
95 
96 //! Project converter for simple property item.
97 //! It preserves only identifier and data roles.
98 
99 TEST_F(ItemConverterFactoryTest, propertyItemProjectConverter)
100 {
101  auto converter = CreateItemProjectConverter(factory());
102 
103  PropertyItem item;
104  item.setToolTip("abc");
105 
106  auto object = converter->to_json(&item);
107  auto reco = converter->from_json(object);
108 
109  EXPECT_EQ(reco->modelType(), item.modelType());
110  EXPECT_EQ(reco->displayName(), item.displayName());
111  EXPECT_EQ(reco->toolTip(), std::string()); // tooltips are not preserved
112  EXPECT_TRUE(reco->identifier() == item.identifier()); // identifier preserved
113 }
114 
115 //! Clone converter for simple property item.
116 
117 TEST_F(ItemConverterFactoryTest, testItemCloneConverter)
118 {
119  auto converter = CreateItemCloneConverter(factory());
120 
121  TestItem item;
122  item.setToolTip("abc");
123 
124  auto object = converter->to_json(&item);
125  auto reco = converter->from_json(object);
126 
127  EXPECT_EQ(reco->modelType(), item.modelType());
128  EXPECT_EQ(reco->displayName(), item.displayName());
129  EXPECT_EQ(reco->toolTip(), std::string("abc")); // updated tooltip is preserved
130  EXPECT_EQ(reco->itemData()->roles(), item.itemData()->roles());
131  EXPECT_TRUE(reco->identifier() == item.identifier()); // identifier preserved
132  EXPECT_EQ(reco->getItem("Thickness")->toolTip(), "thickness");
133  EXPECT_EQ(reco->getItem("Thickness")->identifier(), item.getItem("Thickness")->identifier());
134 }
135 
136 //! Clone converter for simple property item.
137 //! At this time
138 
139 TEST_F(ItemConverterFactoryTest, testItemProjectConverter)
140 {
141  auto converter = CreateItemProjectConverter(factory());
142 
143  TestItem item;
144  item.setToolTip("abc");
145 
146  auto object = converter->to_json(&item);
147  auto reco = converter->from_json(object);
148 
149  EXPECT_EQ(reco->modelType(), item.modelType());
150  EXPECT_EQ(reco->displayName(), item.displayName());
151  EXPECT_EQ(reco->toolTip(), std::string("compound")); // initial tooltip exist
152  EXPECT_EQ(reco->itemData()->roles(), item.itemData()->roles());
153  EXPECT_TRUE(reco->identifier() == item.identifier()); // identifier preserved
154  EXPECT_EQ(reco->getItem("Thickness")->toolTip(), "thickness");
155  EXPECT_EQ(reco->getItem("Thickness")->identifier(), item.getItem("Thickness")->identifier());
156 }
Checks converters generated by ItemFactoryConverter.
const ItemFactoryInterface * factory() const
std::unique_ptr< SessionModel > m_model
Complex item holding mixed SessionItem types (single properties and other CompountItems).
Definition: compounditem.h:28
Interface class for all factories capable of producing SessionItem's.
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
std::vector< int > roles() const
std::string identifier() const
Returns unique identifier.
Definition: sessionitem.cpp:87
SessionItem * setToolTip(const std::string &tooltip)
Sets item tooltip (fluent interface).
SessionItemData * itemData()
Returns pointer to item's data container (non-const version).
virtual std::string displayName() const
Returns display name.
Definition: sessionitem.cpp:94
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
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(ItemConverterFactoryTest, propertyItemCloneConverter)
Clone converter for simple property item.
materialitems.h Collection of materials to populate MaterialModel.
std::unique_ptr< JsonItemConverterInterface > CreateItemProjectConverter(const ItemFactoryInterface *item_factory)
Creates JSON item converter intended for saving on disk.
std::unique_ptr< JsonItemConverterInterface > CreateItemCopyConverter(const ItemFactoryInterface *item_factory)
Creates JSON item converter intended for item copying.
std::unique_ptr< JsonItemConverterInterface > CreateItemCloneConverter(const ItemFactoryInterface *item_factory)
Creates JSON item converter intended for item cloning.
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?