BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
modelmapper.test.cpp File Reference

Implements class CLASS? More...

Include dependency graph for modelmapper.test.cpp:

Go to the source code of this file.

Classes

class  ModelMapperTest
 Testing ModelMapper callbacks on basic model manipulations. More...
 

Functions

 TEST (ModelMapperTest, onClearRebuild)
 Testing signals on cleaning the model using rebuild function. More...
 
 TEST (ModelMapperTest, onDataChange)
 Setting item data and checking corresponding signal. More...
 
 TEST (ModelMapperTest, onDataChangeMultipleUnsubscribe)
 Testing signaling after subscribe/unsubscribe twice. More...
 
 TEST (ModelMapperTest, onDataChangeUnsubscribe)
 Testing signaling after unsubscribe. More...
 
 TEST (ModelMapperTest, onItemInserted)
 Inserting item and checking corresponding signals. More...
 
 TEST (ModelMapperTest, onItemRemoved)
 Inserting item and checking corresponding signals. More...
 
 TEST (ModelMapperTest, onModelDestroyed)
 Testing signals on model destruction. More...
 
 TEST (ModelMapperTest, onModelReset)
 Testing signals on model destruction. More...
 

Detailed Description

Implements class CLASS?

Homepage:\n http://www.bornagainproject.org
License:\n GNU General Public License v3 or higher (see COPYING)
Authors
Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)

Definition in file modelmapper.test.cpp.

Function Documentation

◆ TEST() [1/8]

TEST ( ModelMapperTest  ,
onClearRebuild   
)

Testing signals on cleaning the model using rebuild function.

Definition at line 214 of file modelmapper.test.cpp.

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 }
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38

References ModelView::TagRow::append().

Here is the call graph for this function:

◆ TEST() [2/8]

TEST ( ModelMapperTest  ,
onDataChange   
)

Setting item data and checking corresponding signal.

Definition at line 37 of file modelmapper.test.cpp.

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 }
Mock class to test ModelMapper functionality.
Definition: mockwidgets.h:57
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
const int DATA
main data role
Definition: mvvm_types.h:30

References ModelView::ItemDataRole::DATA, ModelView::SessionModel::insertItem(), ModelView::SessionModel::rootItem(), and ModelView::SessionModel::setData().

Here is the call graph for this function:

◆ TEST() [3/8]

TEST ( ModelMapperTest  ,
onDataChangeMultipleUnsubscribe   
)

Testing signaling after subscribe/unsubscribe twice.

Definition at line 104 of file modelmapper.test.cpp.

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 }
void setModel(SessionModel *model)

References ModelView::ItemDataRole::DATA, ModelView::SessionModel::insertItem(), ModelView::SessionModel::rootItem(), ModelView::SessionModel::setData(), and MockWidgetForModel::setModel().

Here is the call graph for this function:

◆ TEST() [4/8]

TEST ( ModelMapperTest  ,
onDataChangeUnsubscribe   
)

Testing signaling after unsubscribe.

Definition at line 79 of file modelmapper.test.cpp.

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 }

References ModelView::SessionModel::insertItem(), ModelView::SessionModel::rootItem(), and MockWidgetForModel::setModel().

Here is the call graph for this function:

◆ TEST() [5/8]

TEST ( ModelMapperTest  ,
onItemInserted   
)

Inserting item and checking corresponding signals.

Definition at line 134 of file modelmapper.test.cpp.

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 }
std::string defaultTag() const
Returns the name of the default tag.
SessionItemTags * itemTags()
Returns pointer to internal collection of tag-registered items (non-const version).
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25

References ModelView::SessionItemTags::defaultTag(), ModelView::SessionModel::insertItem(), ModelView::SessionItem::itemTags(), and ModelView::SessionModel::rootItem().

Here is the call graph for this function:

◆ TEST() [6/8]

TEST ( ModelMapperTest  ,
onItemRemoved   
)

Inserting item and checking corresponding signals.

Definition at line 154 of file modelmapper.test.cpp.

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 }
void removeItem(SessionItem *parent, const TagRow &tagrow)
Removes given row from parent.

References ModelView::SessionItemTags::defaultTag(), ModelView::SessionModel::insertItem(), ModelView::SessionItem::itemTags(), ModelView::SessionModel::removeItem(), and ModelView::SessionModel::rootItem().

Here is the call graph for this function:

◆ TEST() [7/8]

TEST ( ModelMapperTest  ,
onModelDestroyed   
)

Testing signals on model destruction.

Definition at line 176 of file modelmapper.test.cpp.

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 }

◆ TEST() [8/8]

TEST ( ModelMapperTest  ,
onModelReset   
)

Testing signals on model destruction.

Definition at line 195 of file modelmapper.test.cpp.

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 }