BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
topitemsviewmodel.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/testviewmodel/topitemsviewmodel.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"
17 #include "mvvm/model/sessionitem.h"
19 #include "mvvm/model/taginfo.h"
22 #include "toyitems.h"
23 #include "toymodel.h"
24 #include <QSignalSpy>
25 
26 using namespace ModelView;
27 
28 //! Tests for TopItemsViewModel class.
29 
30 class TopItemsViewModelTest : public ::testing::Test {
31 public:
33 };
34 
36 
38 {
39  SessionModel model;
40  TopItemsViewModel viewModel(&model);
41  EXPECT_EQ(viewModel.rowCount(), 0);
42  EXPECT_EQ(viewModel.columnCount(), 0);
43  EXPECT_EQ(viewModel.sessionItemFromIndex(QModelIndex()), model.rootItem());
44 }
45 
46 //! Insert LayerItem in empty model.
47 
48 TEST_F(TopItemsViewModelTest, insertLayerThenRemove)
49 {
51  TopItemsViewModel viewmodel(&model);
52 
53  QSignalSpy spyInsert(&viewmodel, &ViewModelBase::rowsInserted);
54  QSignalSpy spyRemove(&viewmodel, &ViewModelBase::rowsRemoved);
55 
57 
58  EXPECT_EQ(viewmodel.rowCount(QModelIndex()), 1);
59  EXPECT_EQ(viewmodel.columnCount(QModelIndex()), 2);
60 
61  EXPECT_EQ(spyRemove.count(), 0);
62  EXPECT_EQ(spyInsert.count(), 1);
63 
64  model.removeItem(model.rootItem(), {"", 0});
65  EXPECT_EQ(spyRemove.count(), 1);
66  EXPECT_EQ(spyInsert.count(), 1);
67 
68  EXPECT_EQ(viewmodel.rowCount(QModelIndex()), 0);
69  EXPECT_EQ(viewmodel.columnCount(QModelIndex()), 0);
70 }
71 
72 //! Insert LayerItem in MultiLayer.
73 
74 TEST_F(TopItemsViewModelTest, insertLayerInMultiLayerThenRemove)
75 {
77  TopItemsViewModel viewmodel(&model);
78 
79  QSignalSpy spyInsert(&viewmodel, &ViewModelBase::rowsInserted);
80  QSignalSpy spyRemove(&viewmodel, &ViewModelBase::rowsRemoved);
81 
82  // inserting multilayer
83  auto multilayer = model.insertItem<ToyItems::MultiLayerItem>();
84 
85  EXPECT_EQ(viewmodel.rowCount(QModelIndex()), 1);
86  EXPECT_EQ(viewmodel.columnCount(QModelIndex()), 2);
87  EXPECT_EQ(spyRemove.count(), 0);
88  EXPECT_EQ(spyInsert.count(), 1);
89 
90  // insert layer
91  auto layer = model.insertItem<ToyItems::LayerItem>(multilayer);
92  EXPECT_EQ(viewmodel.rowCount(QModelIndex()), 1);
93  EXPECT_EQ(viewmodel.columnCount(QModelIndex()), 2);
94  EXPECT_EQ(spyRemove.count(), 0);
95  EXPECT_EQ(spyInsert.count(), 2);
96 
97  // checking their indices
98  auto multilayer_index = viewmodel.index(0, 0, QModelIndex());
99  auto layer_index = viewmodel.index(0, 0, multilayer_index);
100  EXPECT_EQ(viewmodel.sessionItemFromIndex(multilayer_index), multilayer);
101  EXPECT_EQ(viewmodel.sessionItemFromIndex(layer_index), layer);
102 
103  // checking row and columns
104  EXPECT_EQ(viewmodel.rowCount(multilayer_index), 1);
105  EXPECT_EQ(viewmodel.columnCount(multilayer_index), 2);
106  EXPECT_EQ(viewmodel.rowCount(layer_index), 0);
107  EXPECT_EQ(viewmodel.columnCount(layer_index), 0);
108 
109  // removing layer
110  model.removeItem(multilayer, {"", 0});
111  EXPECT_EQ(spyRemove.count(), 1);
112  EXPECT_EQ(spyInsert.count(), 2);
113  EXPECT_EQ(viewmodel.rowCount(multilayer_index), 0);
114  EXPECT_EQ(viewmodel.columnCount(multilayer_index), 0);
115 }
116 
117 //! Insert LayerItem in MultiLayer while multilayer is root item. Then deleting multilayer.
118 
119 TEST_F(TopItemsViewModelTest, multuLayerAsRooItem)
120 {
121  ToyItems::SampleModel model;
122  TopItemsViewModel viewmodel(&model);
123 
124  // setting up single multilayer playing the role
125  auto multilayer = model.insertItem<ToyItems::MultiLayerItem>();
126  viewmodel.setRootSessionItem(multilayer);
127  QSignalSpy spyInsert(&viewmodel, &ViewModelBase::rowsInserted);
128  QSignalSpy spyRemove(&viewmodel, &ViewModelBase::rowsRemoved);
129 
130  // initial conditions
131  EXPECT_EQ(viewmodel.rowCount(QModelIndex()), 0);
132  EXPECT_EQ(viewmodel.columnCount(QModelIndex()), 0);
133  EXPECT_EQ(viewmodel.sessionItemFromIndex(QModelIndex()), multilayer); // our new root
134 
135  // adding layer
136  model.insertItem<ToyItems::LayerItem>(multilayer);
137  EXPECT_EQ(viewmodel.rowCount(QModelIndex()), 1);
138  EXPECT_EQ(viewmodel.columnCount(QModelIndex()), 2);
139  EXPECT_EQ(spyRemove.count(), 0);
140  EXPECT_EQ(spyInsert.count(), 1);
141 
142  // removing multilayer
143  model.removeItem(model.rootItem(), {"", 0});
144 }
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.
View model to show top level items of SessionModel in Qt trees and tables.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void setRootSessionItem(SessionItem *item)
Definition: viewmodel.cpp:52
SessionItem * sessionItemFromIndex(const QModelIndex &index) const
Definition: viewmodel.cpp:59
Tests for TopItemsViewModel class.
Represents a layer, with thickness and color, and possibly populated with particles.
Definition: toyitems.h:52
Represents multilayer with collection of layers.
Definition: toyitems.h:44
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(TopItemsViewModelTest, initialState)
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?