BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
modelhaschangedcontroller.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/modelhaschangedcontroller.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"
19 
20 using namespace ModelView;
21 
22 //! Tests for ModelHasChangedController class.
23 
24 class ModelHasChangedControllerTest : public ::testing::Test {
25 public:
27 };
28 
30 
31 //! Tests initial state of the controller.
32 
34 {
35  SessionModel model;
36  ModelHasChangedController controller(&model);
37  EXPECT_FALSE(controller.hasChanged());
38 }
39 
40 //! Tests if controller 'sees' item insertion.
41 
43 {
44  SessionModel model;
45  ModelHasChangedController controller(&model);
46 
47  model.insertItem<PropertyItem>();
48  EXPECT_TRUE(controller.hasChanged());
49 
50  controller.resetChanged();
51  EXPECT_FALSE(controller.hasChanged());
52 }
53 
54 //! Tests if controller sees item insertion.
55 
57 {
58  SessionModel model;
59  model.insertItem<PropertyItem>();
60 
61  ModelHasChangedController controller(&model);
62  EXPECT_FALSE(controller.hasChanged());
63 
64  model.removeItem(model.rootItem(), {"", 0});
65 
66  EXPECT_TRUE(controller.hasChanged());
67 }
68 
69 //! Tests if controller sees item data change.
70 
72 {
73  SessionModel model;
74  auto item = model.insertItem<PropertyItem>();
75 
76  ModelHasChangedController controller(&model);
77  EXPECT_FALSE(controller.hasChanged());
78 
79  item->setData(42.0);
80  EXPECT_TRUE(controller.hasChanged());
81 }
82 
83 //! Tests if controller sees model reset.
84 
86 {
87  SessionModel model;
88  model.insertItem<PropertyItem>();
89 
90  ModelHasChangedController controller(&model);
91  EXPECT_FALSE(controller.hasChanged());
92 
93  model.clear();
94  EXPECT_TRUE(controller.hasChanged());
95 }
96 
97 //! Tests callback functioning.
98 
100 {
101  int change_count{0};
102  auto on_change = [&change_count]() { change_count++; };
103 
104  SessionModel model;
105  ModelHasChangedController controller(&model, on_change);
106 
107  model.insertItem<PropertyItem>();
108  EXPECT_TRUE(controller.hasChanged());
109  EXPECT_EQ(change_count, 1);
110 
111  controller.resetChanged();
112  EXPECT_FALSE(controller.hasChanged());
113  EXPECT_EQ(change_count, 1);
114 }
115 
116 //! Tests callback functioning.
117 
119 {
120  int change_count{0};
121  auto on_change = [&change_count]() { change_count++; };
122 
123  SessionModel model;
124  auto controller = std::make_unique<ModelHasChangedController>(&model, on_change);
125 
126  // change the model, check controller
127  model.insertItem<PropertyItem>();
128  EXPECT_TRUE(controller->hasChanged());
129  EXPECT_EQ(change_count, 1);
130 
131  // remove controller, change the model
132  controller.reset();
133  model.insertItem<PropertyItem>();
134  EXPECT_EQ(change_count, 1);
135 }
Tests for ModelHasChangedController class.
bool hasChanged() const
Returns true if the model was changed since last call of resetChanged.
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
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
void clear(std::function< void(SessionItem *)> callback={})
Removes all items from the model.
void removeItem(SessionItem *parent, const TagRow &tagrow)
Removes given row from parent.
Defines class CLASS?
Defines class CLASS?
TEST_F(ModelHasChangedControllerTest, initialState)
Tests initial state of the controller.
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
Defines class CLASS?