BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
modellistener.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/modellistener.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 #include <memory>
20 
21 using namespace ModelView;
22 
23 //! Tests of ModelListener class.
24 
25 class ModelListenerTest : public ::testing::Test {
26 public:
27  class TestListener : public ModelListener<SessionModel> {
28  public:
31  };
32 
34 };
35 
38 
39 //! Initial state.
40 
41 TEST_F(ModelListenerTest, initialState)
42 {
43  SessionModel model;
44  TestListener listener(&model);
45  EXPECT_EQ(listener.model(), &model);
46 }
47 
48 TEST_F(ModelListenerTest, onDataChange)
49 {
50  auto model = std::make_unique<SessionModel>();
51  auto listener = std::make_unique<TestListener>(model.get());
52 
53  int counter{0};
54  auto on_data_change = [&counter](SessionItem*, int) { counter++; };
55  listener->setOnDataChange(on_data_change);
56 
57  auto item = model->insertItem<PropertyItem>();
58  item->setData(42.0);
59 
60  EXPECT_EQ(counter, 1);
61 }
62 
63 //! Check that controller aware of item deletion.
64 
65 TEST_F(ModelListenerTest, modelDeletedBeforeListener)
66 {
67  auto model = std::make_unique<SessionModel>();
68  auto listener = std::make_unique<TestListener>(model.get());
69 
70  EXPECT_EQ(listener->model(), model.get());
71 
72  model.reset();
73  EXPECT_EQ(listener->model(), nullptr);
74 }
75 
76 //! Checks that the listenerr can be deleted before the model.
77 
78 TEST_F(ModelListenerTest, listenerDeletedBeforeTheModel)
79 {
80  // create model and its listener
81  auto model = std::make_unique<SessionModel>();
82  auto listener = std::make_unique<TestListener>(model.get());
83 
84  // assign to data-changed event
85  int counter{0};
86  auto on_data_change = [&counter](SessionItem*, int) { counter++; };
87  listener->setOnDataChange(on_data_change);
88 
89  // changing the data and checking the listener
90  auto item = model->insertItem<PropertyItem>();
91  item->setData(42.0);
92  EXPECT_EQ(counter, 1);
93 
94  // deleting the listener and trying to change the data again
95  listener.reset();
96  item->setData(43.0);
97  EXPECT_EQ(counter, 1);
98 }
Tests of ModelListener class.
Templated class for all objects willing to listen for changes in concrete SessionModel.
Definition: modellistener.h:26
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
Defines class CLASS?
Defines class CLASS?
TEST_F(ModelListenerTest, initialState)
Initial state.
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
Defines class CLASS?