BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
itemlistener.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/itemlistener.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 //! Testing ItemListener.
24 
25 class ItemListenerTest : public ::testing::Test {
26 public:
27  class TestController : public ItemListener<PropertyItem> {
28  public:
30  size_t ondata_change_call_count{0};
31  size_t on_unsubscribe_call_count{0};
32  void subscribe()
33  {
34  auto on_data_change = [this](SessionItem*, int) { ondata_change_call_count++; };
35  setOnDataChange(on_data_change);
36  }
37 
38  void unsubscribe() { on_unsubscribe_call_count++; }
39  };
40 
42 };
43 
46 
47 //! Initial state.
48 
49 TEST_F(ItemListenerTest, initialState)
50 {
51  TestController controller;
52  EXPECT_EQ(controller.currentItem(), nullptr);
53 }
54 
55 //! Check that controller aware of item deletion.
56 
57 TEST_F(ItemListenerTest, itemDeletedBeforeController)
58 {
59  SessionModel model;
60  auto item = model.insertItem<PropertyItem>();
61 
62  auto controller = std::make_unique<TestController>();
63  controller->setItem(item);
64  item->setData(42.0);
65 
66  EXPECT_EQ(controller->currentItem(), item);
67  EXPECT_EQ(controller->ondata_change_call_count, 1);
68 
69  model.removeItem(model.rootItem(), {"", 0});
70  EXPECT_EQ(controller->currentItem(), nullptr);
71 }
72 
73 //! Checks unsubscribe scenario.
74 
75 TEST_F(ItemListenerTest, unsubscribeScenario)
76 {
77  SessionModel model;
78  auto item = model.insertItem<PropertyItem>();
79 
80  auto controller = std::make_unique<TestController>();
81  controller->setItem(item);
82  item->setData(42.0);
83 
84  EXPECT_EQ(controller->currentItem(), item);
85  EXPECT_EQ(controller->ondata_change_call_count, 1);
86  EXPECT_EQ(controller->on_unsubscribe_call_count, 0);
87 
88  // setting item to nullptr
89  controller->setItem(nullptr);
90  EXPECT_EQ(controller->currentItem(), nullptr);
91  EXPECT_EQ(controller->on_unsubscribe_call_count, 1);
92  // change in data shouldn't lead to update
93  item->setData(45.0);
94  EXPECT_EQ(controller->ondata_change_call_count, 1);
95 }
96 
97 //! Checks that controller can be deleted before item.
98 
99 TEST_F(ItemListenerTest, controllerDeletedBeforeItem)
100 {
101  SessionModel model;
102  auto item = model.insertItem<PropertyItem>();
103 
104  auto controller = std::make_unique<TestController>();
105  controller->setItem(item);
106  EXPECT_EQ(controller->currentItem(), item);
107 
108  controller.reset();
109  item->setData(42.0);
110 }
void unsubscribe()
For necessary manipulations on new item.
Testing ItemListener.
Base class to subscribe to signals generated by SessionItem of certin type.
Definition: itemlistener.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
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.
Defines class CLASS?
Defines class CLASS?
TEST_F(ItemListenerTest, initialState)
Initial state.
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
Defines class CLASS?