BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
linkeditem.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/linkeditem.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/itempool.h"
21 
22 using namespace ModelView;
23 using ::testing::_;
24 
25 //! LinkedItem tests.
26 
27 class LinkedItemTest : public ::testing::Test {
28 public:
30 };
31 
33 
34 //! Initial state of item when it is created outside of model context.
35 
36 TEST_F(LinkedItemTest, initialState)
37 {
38  LinkedItem item;
39  EXPECT_EQ(item.get(), nullptr);
40  EXPECT_EQ(item.data<std::string>(), std::string());
41 }
42 
43 //! Link in single model context.
44 
45 TEST_F(LinkedItemTest, sameModelContext)
46 {
47  SessionModel model;
48  auto item = model.insertItem<PropertyItem>();
49  auto link = model.insertItem<LinkedItem>();
50 
51  // no link by default
52  EXPECT_EQ(link->get(), nullptr);
53 
54  // setting the link
55  link->setLink(item);
56 
57  // now linked
58  EXPECT_EQ(link->get(), item);
59  EXPECT_EQ(link->data<std::string>(), item->identifier());
60 }
61 
62 //! Link in different model context.
63 
64 TEST_F(LinkedItemTest, differentModelContext)
65 {
66  auto pool = std::make_shared<ItemPool>();
67 
68  SessionModel model1("TestModel1", pool);
69  SessionModel model2("TestModel2", pool);
70 
71  auto item = model1.insertItem<PropertyItem>();
72  auto link = model2.insertItem<LinkedItem>();
73 
74  // no link by default
75  EXPECT_EQ(link->get(), nullptr);
76 
77  // setting the link
78  link->setLink(item);
79 
80  // now linked
81  EXPECT_EQ(link->get(), item);
82 }
83 
84 //! Signals when links is set.
85 
87 {
88  SessionModel model;
89  auto item = model.insertItem<PropertyItem>();
90  auto link = model.insertItem<LinkedItem>();
91 
92  // no link by default
93  EXPECT_EQ(link->get(), nullptr);
94 
95  MockWidgetForItem widget(link);
96 
97  auto expected_role = ItemDataRole::DATA;
98  auto expected_item = link;
99  EXPECT_CALL(widget, onDataChange(expected_item, expected_role)).Times(1);
100  EXPECT_CALL(widget, onPropertyChange(_, _)).Times(0);
101 
102  // making action
103  link->setLink(item);
104 }
105 
106 //! Link in different model context.
107 
108 TEST_F(LinkedItemTest, setNullAsLink)
109 {
110  auto pool = std::make_shared<ItemPool>();
111 
112  SessionModel model("TestModel", pool);
113  auto link = model.insertItem<LinkedItem>();
114  auto item = model.insertItem<PropertyItem>();
115 
116  // no link by default
117  EXPECT_EQ(link->get(), nullptr);
118 
119  // setting link
120  link->setLink(item);
121  EXPECT_EQ(link->get(), item);
122 
123  // still null link
124  link->setLink(nullptr);
125  EXPECT_EQ(link->get(), nullptr);
126  EXPECT_TRUE(link->data<QVariant>().isValid());
127  EXPECT_EQ(link->data<std::string>(), std::string());
128 }
LinkedItem tests.
Mock widget to test ItemMapper functionality.
Definition: mockwidgets.h:36
Item to store a persistent link to other arbitrary items.
Definition: linkeditem.h:30
T * get() const
Returns item linked to given item. Works only in model context.
Definition: linkeditem.h:41
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
Definition: sessionitem.h:148
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Definition: sessionmodel.h:104
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(LinkedItemTest, initialState)
Initial state of item when it is created outside of model context.
Defines class CLASS?
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?