BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
modelmapper.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/modelmapper.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"
17 #include "mvvm/model/sessionitem.h"
20 #include "mvvm/model/tagrow.h"
22 
23 using namespace ModelView;
24 using ::testing::_;
25 
26 //! Testing ModelMapper callbacks on basic model manipulations.
27 
28 class ModelMapperTest : public ::testing::Test {
29 public:
31 };
32 
34 
35 //! Setting item data and checking corresponding signal.
36 
37 TEST(ModelMapperTest, onDataChange)
38 {
39  SessionModel model;
40  MockWidgetForModel widget(&model);
41 
42  EXPECT_CALL(widget, onItemInserted(_, _));
43  auto item = model.insertItem<SessionItem>(model.rootItem());
44 
45  // expecting signal to be called once
46  const int role = ItemDataRole::DATA;
47  EXPECT_CALL(widget, onDataChange(item, role)).Times(1);
48  EXPECT_CALL(widget, onItemInserted(_, _)).Times(0);
49  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
50  EXPECT_CALL(widget, onItemRemoved(_, _)).Times(0);
51  EXPECT_CALL(widget, onModelDestroyed(_)).Times(0);
52  EXPECT_CALL(widget, onModelAboutToBeReset(_)).Times(0);
53  EXPECT_CALL(widget, onModelReset(_)).Times(0);
54  model.setData(item, 42.0, ItemDataRole::DATA); // perform action
55 
56  // setting same data shouldn't trigger the signal
57  EXPECT_CALL(widget, onDataChange(_, _)).Times(0);
58  EXPECT_CALL(widget, onItemInserted(_, _)).Times(0);
59  EXPECT_CALL(widget, onItemRemoved(_, _)).Times(0);
60  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
61  EXPECT_CALL(widget, onModelDestroyed(_)).Times(0);
62  EXPECT_CALL(widget, onModelAboutToBeReset(_)).Times(0);
63  EXPECT_CALL(widget, onModelReset(_)).Times(0);
64  model.setData(item, 42.0, ItemDataRole::DATA); // perform action
65 
66  // setting new data through item
67  EXPECT_CALL(widget, onDataChange(item, role)).Times(1);
68  EXPECT_CALL(widget, onItemInserted(_, _)).Times(0);
69  EXPECT_CALL(widget, onItemRemoved(_, _)).Times(0);
70  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
71  EXPECT_CALL(widget, onModelDestroyed(_)).Times(0);
72  EXPECT_CALL(widget, onModelAboutToBeReset(_)).Times(0);
73  EXPECT_CALL(widget, onModelReset(_)).Times(0);
74  item->setData(43.0); // perform action
75 }
76 
77 //! Testing signaling after unsubscribe.
78 
79 TEST(ModelMapperTest, onDataChangeUnsubscribe)
80 {
81  SessionModel model;
82  MockWidgetForModel widget(&model);
83 
84  EXPECT_CALL(widget, onItemInserted(_, _));
85  auto item = model.insertItem<SessionItem>(model.rootItem());
86 
87  // unsubscribing
88  widget.setModel(nullptr);
89 
90  // no calls should be done
91  EXPECT_CALL(widget, onDataChange(_, _)).Times(0);
92  EXPECT_CALL(widget, onItemInserted(_, _)).Times(0);
93  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
94  EXPECT_CALL(widget, onItemRemoved(_, _)).Times(0);
95  EXPECT_CALL(widget, onModelDestroyed(_)).Times(0);
96  EXPECT_CALL(widget, onModelAboutToBeReset(_)).Times(0);
97  EXPECT_CALL(widget, onModelReset(_)).Times(0);
98 
99  item->setData(43.0); // perform action
100 }
101 
102 //! Testing signaling after subscribe/unsubscribe twice.
103 
104 TEST(ModelMapperTest, onDataChangeMultipleUnsubscribe)
105 {
106  SessionModel model;
107  MockWidgetForModel widget(&model);
108 
109  EXPECT_CALL(widget, onItemInserted(_, _));
110  auto item = model.insertItem<SessionItem>(model.rootItem());
111 
112  // unsubscribing
113  widget.setModel(nullptr);
114  // subscribing again
115  widget.setModel(&model);
116  // unsubscribing
117  widget.setModel(nullptr);
118  // subscribing again
119  widget.setModel(&model);
120 
121  const int role = ItemDataRole::DATA;
122  EXPECT_CALL(widget, onDataChange(item, role)).Times(1);
123  EXPECT_CALL(widget, onItemInserted(_, _)).Times(0);
124  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
125  EXPECT_CALL(widget, onItemRemoved(_, _)).Times(0);
126  EXPECT_CALL(widget, onModelDestroyed(_)).Times(0);
127  EXPECT_CALL(widget, onModelAboutToBeReset(_)).Times(0);
128  EXPECT_CALL(widget, onModelReset(_)).Times(0);
129  model.setData(item, 42.0, ItemDataRole::DATA); // perform action
130 }
131 
132 //! Inserting item and checking corresponding signals.
133 
134 TEST(ModelMapperTest, onItemInserted)
135 {
136  SessionModel model;
137  MockWidgetForModel widget(&model);
138 
139  EXPECT_CALL(widget, onDataChange(_, _)).Times(0);
140  const TagRow expected_tagrow{model.rootItem()->itemTags()->defaultTag(), 0};
141  EXPECT_CALL(widget, onItemInserted(model.rootItem(), expected_tagrow)).Times(1);
142  EXPECT_CALL(widget, onItemRemoved(_, _)).Times(0);
143  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
144  EXPECT_CALL(widget, onModelDestroyed(_)).Times(0);
145  EXPECT_CALL(widget, onModelAboutToBeReset(_)).Times(0);
146  EXPECT_CALL(widget, onModelReset(_)).Times(0);
147 
148  // perform action
149  model.insertItem<SessionItem>(model.rootItem(), expected_tagrow);
150 }
151 
152 //! Inserting item and checking corresponding signals.
153 
154 TEST(ModelMapperTest, onItemRemoved)
155 {
156  SessionModel model;
157  MockWidgetForModel widget(&model);
158 
159  const TagRow expected_tagrow{model.rootItem()->itemTags()->defaultTag(), 0};
160  EXPECT_CALL(widget, onItemInserted(model.rootItem(), expected_tagrow)).Times(1);
161  model.insertItem<SessionItem>(model.rootItem(), expected_tagrow);
162 
163  EXPECT_CALL(widget, onDataChange(_, _)).Times(0);
164  EXPECT_CALL(widget, onItemInserted(_, _)).Times(0);
165  EXPECT_CALL(widget, onItemRemoved(model.rootItem(), expected_tagrow)).Times(1);
166  EXPECT_CALL(widget, onAboutToRemoveItem(model.rootItem(), expected_tagrow)).Times(1);
167  EXPECT_CALL(widget, onModelDestroyed(_)).Times(0);
168  EXPECT_CALL(widget, onModelAboutToBeReset(_)).Times(0);
169  EXPECT_CALL(widget, onModelReset(_)).Times(0);
170  // perform action
171  model.removeItem(model.rootItem(), expected_tagrow);
172 }
173 
174 //! Testing signals on model destruction.
175 
176 TEST(ModelMapperTest, onModelDestroyed)
177 {
178  auto model = std::make_unique<SessionModel>();
179  auto widget = std::make_unique<MockWidgetForModel>(model.get());
180 
181  EXPECT_CALL(*widget, onDataChange(_, _)).Times(0);
182  EXPECT_CALL(*widget, onItemInserted(_, _)).Times(0);
183  EXPECT_CALL(*widget, onItemRemoved(_, _)).Times(0);
184  EXPECT_CALL(*widget, onAboutToRemoveItem(_, _)).Times(0);
185  EXPECT_CALL(*widget, onModelAboutToBeReset(_)).Times(0);
186  EXPECT_CALL(*widget, onModelReset(_)).Times(0);
187  EXPECT_CALL(*widget, onModelDestroyed(model.get())).Times(1);
188 
189  // perform action
190  model.reset();
191 }
192 
193 //! Testing signals on model destruction.
194 
195 TEST(ModelMapperTest, onModelReset)
196 {
197  auto model = std::make_unique<SessionModel>();
198  auto widget = std::make_unique<MockWidgetForModel>(model.get());
199 
200  EXPECT_CALL(*widget, onDataChange(_, _)).Times(0);
201  EXPECT_CALL(*widget, onItemInserted(_, _)).Times(0);
202  EXPECT_CALL(*widget, onItemRemoved(_, _)).Times(0);
203  EXPECT_CALL(*widget, onAboutToRemoveItem(_, _)).Times(0);
204  EXPECT_CALL(*widget, onModelDestroyed(_)).Times(0);
205  EXPECT_CALL(*widget, onModelAboutToBeReset(_)).Times(1);
206  EXPECT_CALL(*widget, onModelReset(model.get())).Times(1);
207 
208  // perform action
209  model->clear();
210 }
211 
212 //! Testing signals on cleaning the model using rebuild function.
213 
214 TEST(ModelMapperTest, onClearRebuild)
215 {
216  auto model = std::make_unique<SessionModel>();
217 
218  auto widget = std::make_unique<MockWidgetForModel>(model.get());
219 
220  EXPECT_CALL(*widget, onDataChange(_, _)).Times(0);
221  EXPECT_CALL(*widget, onItemInserted(_, _)).Times(1);
222  EXPECT_CALL(*widget, onItemRemoved(_, _)).Times(0);
223  EXPECT_CALL(*widget, onAboutToRemoveItem(_, _)).Times(0);
224  EXPECT_CALL(*widget, onModelDestroyed(_)).Times(0);
225  EXPECT_CALL(*widget, onModelAboutToBeReset(_)).Times(1);
226  EXPECT_CALL(*widget, onModelReset(model.get())).Times(1);
227 
228  auto rebuild = [](auto item) { item->insertItem(new SessionItem, TagRow::append()); };
229  model->clear(rebuild);
230 }
Mock class to test ModelMapper functionality.
Definition: mockwidgets.h:57
void setModel(ModelView::SessionModel *model)
Definition: mockwidgets.cpp:98
Testing ModelMapper callbacks on basic model manipulations.
std::string defaultTag() const
Returns the name of the default tag.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
SessionItemTags * itemTags()
Returns pointer to internal collection of tag-registered items (non-const version).
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
bool setData(SessionItem *item, const Variant &value, int role)
Sets the data for given item.
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
void removeItem(SessionItem *parent, const TagRow &tagrow)
Removes given row from parent.
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
static TagRow append(const std::string &tag_name={})
Returns TagRow corresponding to the append to tag_name.
Definition: tagrow.cpp:36
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST(ModelMapperTest, onDataChange)
Setting item data and checking corresponding signal.
const int DATA
main data role
Definition: mvvm_types.h:30
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?