BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
moveitemcommand.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/moveitemcommand.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"
17 #include "mvvm/model/itemutils.h"
18 #include "mvvm/model/sessionitem.h"
20 #include "mvvm/model/taginfo.h"
21 
22 using namespace ModelView;
23 
24 class MoveItemCommandTest : public ::testing::Test {
25 public:
27 };
28 
30 
31 TEST_F(MoveItemCommandTest, rootContextNext)
32 {
33  SessionModel model;
34  auto item0 = model.insertItem<SessionItem>(model.rootItem()); // 0
35  auto item1 = model.insertItem<SessionItem>(model.rootItem()); // 1
36  auto item2 = model.insertItem<SessionItem>(model.rootItem()); // 2
37  auto item3 = model.insertItem<SessionItem>(model.rootItem()); // 3
38 
39  // expecting 4 items in the order of insertion
40  std::vector<SessionItem*> expected = {item0, item1, item2, item3};
41  EXPECT_EQ(model.rootItem()->children(), expected);
42 
43  // moving item1 to the next position
44  MoveItemCommand command(item1, model.rootItem(), {"", 2});
45  command.execute();
46  EXPECT_EQ(std::get<bool>(command.result()), true);
47 
48  // expecting new order of items
49  expected = {item0, item2, item1, item3};
50  EXPECT_EQ(model.rootItem()->children(), expected);
51 
52  // undoing command
53  command.undo();
54  EXPECT_EQ(std::get<bool>(command.result()), true);
55  expected = {item0, item1, item2, item3};
56  EXPECT_EQ(model.rootItem()->children(), expected);
57 
58  // redoing
59  command.execute();
60  EXPECT_EQ(std::get<bool>(command.result()), true);
61  EXPECT_EQ(command.isObsolete(), false);
62 
63  expected = {item0, item2, item1, item3};
64  EXPECT_EQ(model.rootItem()->children(), expected);
65 }
66 
67 TEST_F(MoveItemCommandTest, rootContextSamePos)
68 {
69  SessionModel model;
70  auto item0 = model.insertItem<SessionItem>(model.rootItem());
71  auto item1 = model.insertItem<SessionItem>(model.rootItem());
72  auto item2 = model.insertItem<SessionItem>(model.rootItem());
73  auto item3 = model.insertItem<SessionItem>(model.rootItem());
74 
75  // expecting 4 items in the order of insertion
76  std::vector<SessionItem*> expected = {item0, item1, item2, item3};
77  EXPECT_EQ(model.rootItem()->children(), expected);
78 
79  // moving item1 to the same position
80  MoveItemCommand command(item1, model.rootItem(), {"", 1});
81  command.execute();
82  EXPECT_EQ(std::get<bool>(command.result()), true);
83  EXPECT_EQ(command.isObsolete(), false);
84 
85  // expecting new order of items
86  expected = {item0, item1, item2, item3};
87  EXPECT_EQ(model.rootItem()->children(), expected);
88 
89  // undoing command
90  command.undo();
91  EXPECT_EQ(std::get<bool>(command.result()), true);
92  EXPECT_EQ(command.isObsolete(), false);
93 
94  expected = {item0, item1, item2, item3};
95  EXPECT_EQ(model.rootItem()->children(), expected);
96 }
97 
98 TEST_F(MoveItemCommandTest, rootContextPrev)
99 {
100  SessionModel model;
101  auto item0 = model.insertItem<SessionItem>(model.rootItem());
102  auto item1 = model.insertItem<SessionItem>(model.rootItem());
103  auto item2 = model.insertItem<SessionItem>(model.rootItem());
104  auto item3 = model.insertItem<SessionItem>(model.rootItem());
105 
106  // expecting 4 items in the order of insertion
107  std::vector<SessionItem*> expected = {item0, item1, item2, item3};
108  EXPECT_EQ(model.rootItem()->children(), expected);
109 
110  // moving item2 to item1's place
111  MoveItemCommand command(item2, model.rootItem(), {"", 1});
112  command.execute();
113  EXPECT_EQ(std::get<bool>(command.result()), true);
114  EXPECT_EQ(command.isObsolete(), false);
115 
116  // expecting new order of items
117  expected = {item0, item2, item1, item3};
118  EXPECT_EQ(model.rootItem()->children(), expected);
119 
120  // undoing command
121  command.undo();
122  EXPECT_EQ(std::get<bool>(command.result()), true);
123  EXPECT_EQ(command.isObsolete(), false);
124 
125  expected = {item0, item1, item2, item3};
126  EXPECT_EQ(model.rootItem()->children(), expected);
127 }
128 
129 TEST_F(MoveItemCommandTest, rootContextLast)
130 {
131  SessionModel model;
132  auto item0 = model.insertItem<SessionItem>(model.rootItem());
133  auto item1 = model.insertItem<SessionItem>(model.rootItem());
134  auto item2 = model.insertItem<SessionItem>(model.rootItem());
135  auto item3 = model.insertItem<SessionItem>(model.rootItem());
136 
137  // expecting 4 items in the order of insertion
138  std::vector<SessionItem*> expected = {item0, item1, item2, item3};
139  EXPECT_EQ(model.rootItem()->children(), expected);
140 
141  // moving item0 in the back of the list
142  MoveItemCommand command(item0, model.rootItem(), {"", model.rootItem()->childrenCount() - 1});
143  command.execute();
144  EXPECT_EQ(std::get<bool>(command.result()), true);
145  EXPECT_EQ(command.isObsolete(), false);
146 
147  // expecting new order of items
148  expected = {item1, item2, item3, item0};
149  EXPECT_EQ(model.rootItem()->children(), expected);
150 
151  // undoing command
152  command.undo();
153  EXPECT_EQ(std::get<bool>(command.result()), true);
154  EXPECT_EQ(command.isObsolete(), false);
155 
156  expected = {item0, item1, item2, item3};
157  EXPECT_EQ(model.rootItem()->children(), expected);
158 }
159 
160 TEST_F(MoveItemCommandTest, rootContextLast2)
161 {
162  SessionModel model;
163  auto item0 = model.insertItem<SessionItem>(model.rootItem());
164  auto item1 = model.insertItem<SessionItem>(model.rootItem());
165  auto item2 = model.insertItem<SessionItem>(model.rootItem());
166  auto item3 = model.insertItem<SessionItem>(model.rootItem());
167 
168  // expecting 4 items in the order of insertion
169  std::vector<SessionItem*> expected = {item0, item1, item2, item3};
170  EXPECT_EQ(model.rootItem()->children(), expected);
171 
172  // moving item0 in the back of the list
173  MoveItemCommand command(item0, model.rootItem(), {"", 3});
174  command.execute();
175  EXPECT_EQ(std::get<bool>(command.result()), true);
176  EXPECT_EQ(command.isObsolete(), false);
177 
178  // expecting new order of items
179  expected = {item1, item2, item3, item0};
180  EXPECT_EQ(model.rootItem()->children(), expected);
181 
182  // undoing command
183  command.undo();
184  EXPECT_EQ(std::get<bool>(command.result()), true);
185  EXPECT_EQ(command.isObsolete(), false);
186 
187  expected = {item0, item1, item2, item3};
188  EXPECT_EQ(model.rootItem()->children(), expected);
189 }
190 
191 TEST_F(MoveItemCommandTest, fromRootToParent)
192 {
193  SessionModel model;
194  auto item0 = model.insertItem<SessionItem>(model.rootItem());
195  auto parent = model.insertItem<SessionItem>(model.rootItem());
196  parent->registerTag(TagInfo::universalTag("tag1"), /*set_as_default*/ true);
197 
198  auto child0 = model.insertItem<SessionItem>(parent);
199  auto child1 = model.insertItem<SessionItem>(parent);
200 
201  // expected items for root item
202  std::vector<SessionItem*> expected = {item0, parent};
203  EXPECT_EQ(model.rootItem()->children(), expected);
204 
205  // expected items for parent
206  expected = {child0, child1};
207  EXPECT_EQ(parent->children(), expected);
208 
209  // moving item0 from root to parent
210  MoveItemCommand command(item0, parent, {"", 1});
211  command.execute();
212  EXPECT_EQ(std::get<bool>(command.result()), true);
213  EXPECT_EQ(command.isObsolete(), false);
214 
215  // expected items for root item
216  expected = {parent};
217  EXPECT_EQ(model.rootItem()->children(), expected);
218 
219  // expected items for parent
220  expected = {child0, item0, child1};
221  EXPECT_EQ(parent->children(), expected);
222 
223  // undoing command
224  command.undo();
225  EXPECT_EQ(std::get<bool>(command.result()), true);
226  EXPECT_EQ(command.isObsolete(), false);
227 
228  // expected items for root item
229  expected = {item0, parent};
230  EXPECT_EQ(model.rootItem()->children(), expected);
231 
232  // expected items for parent
233  expected = {child0, child1};
234  EXPECT_EQ(parent->children(), expected);
235 }
236 
237 TEST_F(MoveItemCommandTest, fromParentToRoot)
238 {
239  SessionModel model;
240  auto item0 = model.insertItem<SessionItem>(model.rootItem());
241  auto parent = model.insertItem<SessionItem>(model.rootItem());
242  parent->registerTag(TagInfo::universalTag("tag1"), /*set_as_default*/ true);
243 
244  auto child0 = model.insertItem<SessionItem>(parent);
245  auto child1 = model.insertItem<SessionItem>(parent);
246 
247  // expected items for root item
248  std::vector<SessionItem*> expected = {item0, parent};
249  EXPECT_EQ(model.rootItem()->children(), expected);
250 
251  // expected items for parent
252  expected = {child0, child1};
253  EXPECT_EQ(parent->children(), expected);
254 
255  // moving child0 from parent to root
256  MoveItemCommand command(child0, model.rootItem(), {"", 0});
257  command.execute();
258  EXPECT_EQ(std::get<bool>(command.result()), true);
259  EXPECT_EQ(command.isObsolete(), false);
260 
261  // expected items for root item
262  expected = {child0, item0, parent};
263  EXPECT_EQ(model.rootItem()->children(), expected);
264 
265  // expected items for parent
266  expected = {child1};
267  EXPECT_EQ(parent->children(), expected);
268 
269  // undoing command
270  command.undo();
271  EXPECT_EQ(std::get<bool>(command.result()), true);
272  EXPECT_EQ(command.isObsolete(), false);
273 
274  // expected items for root item
275  expected = {item0, parent};
276  EXPECT_EQ(model.rootItem()->children(), expected);
277 
278  // expected items for parent
279  expected = {child0, child1};
280  EXPECT_EQ(parent->children(), expected);
281 }
282 
283 TEST_F(MoveItemCommandTest, betweenParentTags)
284 {
285  SessionModel model;
286  auto parent = model.insertItem<SessionItem>(model.rootItem());
287  parent->registerTag(TagInfo::universalTag("tag1"));
288  parent->registerTag(TagInfo::universalTag("tag2"));
289 
290  auto child0 = model.insertItem<SessionItem>(parent, "tag1");
291  auto child1 = model.insertItem<SessionItem>(parent, "tag1");
292  auto child2 = model.insertItem<SessionItem>(parent, "tag2");
293  auto child3 = model.insertItem<SessionItem>(parent, "tag2");
294 
295  // expected items for root item
296  std::vector<SessionItem*> expected = {parent};
297  EXPECT_EQ(model.rootItem()->children(), expected);
298 
299  // expected items for parent
300  expected = {child0, child1, child2, child3};
301  EXPECT_EQ(parent->children(), expected);
302 
303  // moving child2 to another tag
304  MoveItemCommand command(child2, parent, {"tag1", 0});
305  command.execute();
306  EXPECT_EQ(std::get<bool>(command.result()), true);
307  EXPECT_EQ(command.isObsolete(), false);
308 
309  // expected items for root item
310  expected = {parent};
311  EXPECT_EQ(model.rootItem()->children(), expected);
312 
313  // expected items for parents tag
314  expected = {child2, child0, child1, child3};
315  EXPECT_EQ(parent->children(), expected);
316  expected = {child2, child0, child1};
317  EXPECT_EQ(parent->getItems("tag1"), expected);
318  expected = {child3};
319  EXPECT_EQ(parent->getItems("tag2"), expected);
320 
321  // undoing command
322  command.undo();
323  EXPECT_EQ(std::get<bool>(command.result()), true);
324  EXPECT_EQ(command.isObsolete(), false);
325 
326  // expected items for root item
327  expected = {parent};
328  EXPECT_EQ(model.rootItem()->children(), expected);
329 
330  // expected items for parent
331  expected = {child0, child1};
332  EXPECT_EQ(parent->getItems("tag1"), expected);
333  expected = {child2, child3};
334  EXPECT_EQ(parent->getItems("tag2"), expected);
335 }
void undo()
Undo command as it was before execution.
bool isObsolete() const
Returns whether the command is obsolete (which means that it shouldn't be kept in the stack).
Command for unddo/redo framework to move item from one parent to another.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
std::vector< SessionItem * > getItems(const std::string &tag) const
Returns all children stored at given tag.
void registerTag(const TagInfo &tagInfo, bool set_as_default=false)
Registers tag to hold items under given name.
std::vector< SessionItem * > children() const
Returns vector of children formed from all chidlren from 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
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(MoveItemCommandTest, rootContextNext)
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?