BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
removeitemcommand.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/removeitemcommand.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"
18 #include "mvvm/model/itemutils.h"
19 #include "mvvm/model/sessionitem.h"
21 #include "mvvm/model/taginfo.h"
22 
23 using namespace ModelView;
24 
25 class RemoveItemCommandTest : public ::testing::Test {
26 public:
28 };
29 
31 
32 TEST_F(RemoveItemCommandTest, removeAtCommand)
33 {
34  SessionModel model;
35  auto item = model.insertItem<SessionItem>(model.rootItem());
36 
37  auto item_identifier = item->identifier();
38 
39  // command to insert parent in the model
40  auto command = std::make_unique<RemoveItemCommand>(model.rootItem(), TagRow{"", 0});
41  command->execute(); // removal
42 
43  EXPECT_EQ(std::get<bool>(command->result()), true);
44  EXPECT_EQ(model.rootItem()->childrenCount(), 0);
45  EXPECT_FALSE(command->isObsolete());
46 
47  // undo command
48  command->undo();
49  EXPECT_FALSE(command->isObsolete());
50  EXPECT_EQ(model.rootItem()->childrenCount(), 1);
51  auto restored = Utils::ChildAt(model.rootItem(), 0);
52  EXPECT_EQ(restored->identifier(), item_identifier);
53 }
54 
55 TEST_F(RemoveItemCommandTest, removeAtCommandChild)
56 {
57  SessionModel model;
58  auto parent = model.insertItem<SessionItem>(model.rootItem());
59  parent->registerTag(TagInfo::universalTag("tag1"), /*set_as_default*/ true);
60 
61  auto child1 = model.insertItem<SessionItem>(parent);
62  child1->setData(42.0);
63  model.insertItem<SessionItem>(parent);
64 
65  auto child1_identifier = child1->identifier();
66 
67  // command to remove one child
68  auto command = std::make_unique<RemoveItemCommand>(parent, TagRow{"", 0});
69  command->execute(); // removal
70 
71  // check that one child was removed
72  EXPECT_FALSE(command->isObsolete());
73  EXPECT_EQ(std::get<bool>(command->result()), true);
74  EXPECT_EQ(parent->childrenCount(), 1);
75 
76  // undo command
77  command->undo();
78  EXPECT_FALSE(command->isObsolete());
79  EXPECT_EQ(parent->childrenCount(), 2);
80  auto restored = Utils::ChildAt(parent, 0);
81  EXPECT_EQ(restored->identifier(), child1_identifier);
82 
83  // checking the data of restored item
84  EXPECT_EQ(restored->data<double>(), 42.0);
85 }
86 
87 TEST_F(RemoveItemCommandTest, removeAtCommandParentWithChild)
88 {
89  SessionModel model;
90  auto parent = model.insertItem<SessionItem>(model.rootItem());
91  parent->registerTag(TagInfo::universalTag("tag1"), /*set_as_default*/ true);
92 
93  auto child1 = model.insertItem<SessionItem>(parent);
94  child1->setData(42.0);
95 
96  auto parent_identifier = parent->identifier();
97  auto child1_identifier = child1->identifier();
98 
99  // command to remove parent
100  auto command = std::make_unique<RemoveItemCommand>(model.rootItem(), TagRow{"", 0});
101  command->execute(); // removal
102  EXPECT_FALSE(command->isObsolete());
103 
104  // check that one child was removed
105  EXPECT_EQ(std::get<bool>(command->result()), true);
106  EXPECT_EQ(model.rootItem()->childrenCount(), 0);
107 
108  // undo command
109  command->undo();
110  EXPECT_FALSE(command->isObsolete());
111  EXPECT_EQ(model.rootItem()->childrenCount(), 1);
112  auto restored_parent = Utils::ChildAt(model.rootItem(), 0);
113  auto restored_child = Utils::ChildAt(restored_parent, 0);
114 
115  EXPECT_EQ(restored_parent->identifier(), parent_identifier);
116  EXPECT_EQ(restored_child->identifier(), child1_identifier);
117 
118  // checking the data of restored item
119  EXPECT_EQ(restored_child->data<double>(), 42.0);
120 }
121 
122 //! RemoveAtCommand in multitag context
123 
124 TEST_F(RemoveItemCommandTest, removeAtCommandMultitag)
125 {
126  SessionModel model;
127  auto parent = model.insertItem<SessionItem>(model.rootItem());
128  parent->registerTag(TagInfo::universalTag("tag1"));
129  parent->registerTag(TagInfo::universalTag("tag2"));
130 
131  auto child1 = model.insertItem<SessionItem>(parent, "tag1");
132  child1->setData(41.0);
133 
134  auto child2 = model.insertItem<SessionItem>(parent, "tag1");
135  child2->setData(42.0);
136 
137  auto child3 = model.insertItem<SessionItem>(parent, "tag2");
138  child3->setData(43.0);
139 
140  auto parent_identifier = parent->identifier();
141  auto child1_identifier = child1->identifier();
142  auto child2_identifier = child2->identifier();
143  auto child3_identifier = child3->identifier();
144 
145  // command to remove parent
146  auto command = std::make_unique<RemoveItemCommand>(parent, TagRow{"tag1", 1});
147  command->execute(); // removal
148 
149  // check that one child was removed
150  EXPECT_FALSE(command->isObsolete());
151  EXPECT_EQ(std::get<bool>(command->result()), true);
152  EXPECT_EQ(parent->childrenCount(), 2);
153 
154  // undo command
155  command->undo();
156  EXPECT_FALSE(command->isObsolete());
157  EXPECT_EQ(parent->childrenCount(), 3);
158  auto restored_parent = Utils::ChildAt(model.rootItem(), 0);
159  auto restored_child2 = Utils::ChildAt(restored_parent, 1);
160 
161  EXPECT_EQ(restored_parent->identifier(), parent_identifier);
162  EXPECT_EQ(restored_child2->identifier(), child2_identifier);
163 
164  // checking the data of restored item
165  EXPECT_EQ(restored_child2->data<double>(), 42.0);
166 }
167 
168 //! Attempt to remove property item.
169 
170 TEST_F(RemoveItemCommandTest, attemptToRemoveItem)
171 {
172  SessionModel model;
173  auto parent = model.insertItem<CompoundItem>(model.rootItem());
174  parent->addProperty("thickness", 42.0);
175 
176  auto command = std::make_unique<RemoveItemCommand>(parent, TagRow{"thickness", 0});
177  command->execute();
178 
179  EXPECT_TRUE(command->isObsolete());
180  EXPECT_EQ(std::get<bool>(command->result()), false);
181 }
Complex item holding mixed SessionItem types (single properties and other CompountItems).
Definition: compounditem.h:28
T * addProperty(const std::string &name)
Adds property item of given type.
Definition: compounditem.h:43
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
std::string identifier() const
Returns unique identifier.
Definition: sessionitem.cpp:87
void registerTag(const TagInfo &tagInfo, bool set_as_default=false)
Registers tag to hold items under given name.
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141
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
static TagInfo universalTag(std::string name, std::vector< std::string > modelTypes={})
Constructs universal tag intended for unlimited amount of various items.
Definition: taginfo.cpp:34
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
MVVM_MODEL_EXPORT SessionItem * ChildAt(const SessionItem *parent, int index)
Returns child at given index of parent.
Definition: itemutils.cpp:70
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
TEST_F(RemoveItemCommandTest, removeAtCommand)
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?