BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
TestToyLayerItem.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/TestToyLayerItem.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 "mvvm/model/itemutils.h"
21 #include "toyitems.h"
22 #include "toymodel.h"
23 #include <QSignalSpy>
24 
25 using namespace ModelView;
26 
27 //! Tests for toy Layer in the context of model and view model.
28 
29 class ToyLayerItemTest : public ::testing::Test {
30 public:
32 };
33 
35 
36 //! Initial state.
37 
38 TEST_F(ToyLayerItemTest, initialState)
39 {
44 }
45 
46 //! Toy layer as prodused by toy SampleModel.
47 
49 {
51  auto layer = model.insertItem<ToyItems::LayerItem>();
52 
53  EXPECT_FALSE(layer->data<QVariant>().isValid());
54  EXPECT_EQ(layer->displayName(), ToyItems::Constants::LayerItemType);
55 }
56 
57 TEST_F(ToyLayerItemTest, inViewModel)
58 {
60  auto layerItem = model.insertItem<ToyItems::LayerItem>();
61 
62  // constructing viewModel from sample model
63  DefaultViewModel viewModel(&model);
64 
65  // root item should have one child, item looking at our layerItem
66  EXPECT_EQ(viewModel.rowCount(), 1);
67  EXPECT_EQ(viewModel.columnCount(), 2);
68 
69  // accessing to viewItem representing layerItem
70  QModelIndex layerIndex = viewModel.index(0, 0);
71  auto viewItem = dynamic_cast<ViewLabelItem*>(viewModel.itemFromIndex(layerIndex));
72  EXPECT_TRUE(viewItem != nullptr);
73  EXPECT_EQ(viewItem->item(), layerItem);
74 
75  // it has two rows and two columns, corresponding to our "thickness" and "color" properties
76  EXPECT_EQ(viewModel.rowCount(layerIndex), 2);
77  EXPECT_EQ(viewModel.columnCount(layerIndex), 2);
78 
79  // accessing to views representing label and value of thickness property
80  QModelIndex thicknessLabelIndex = viewModel.index(0, 0, layerIndex);
81  auto thicknessLabelView =
82  dynamic_cast<ViewLabelItem*>(viewModel.itemFromIndex(thicknessLabelIndex));
83  EXPECT_TRUE(thicknessLabelView != nullptr);
84 
85  QModelIndex thicknessValueIndex = viewModel.index(0, 1, layerIndex);
86  auto thicknessValueView =
87  dynamic_cast<ViewDataItem*>(viewModel.itemFromIndex(thicknessValueIndex));
88  EXPECT_TRUE(thicknessValueView != nullptr);
89 
90  // internally, views for label and data should point to single SessionItem corresponding to
91  // thickness property
92  EXPECT_EQ(thicknessLabelView->item(), layerItem->getItem(ToyItems::LayerItem::P_THICKNESS));
93  EXPECT_EQ(thicknessValueView->item(), layerItem->getItem(ToyItems::LayerItem::P_THICKNESS));
94 }
95 
96 //! Constructing ViewModel from a Layer with one "thickness" property.
97 //! Change thickness property in SessionItem, control dataChanged signals from ViewModel.
98 
99 TEST_F(ToyLayerItemTest, layerItemDataChanged)
100 {
101  ToyItems::SampleModel model;
102  auto layerItem = model.insertItem<ToyItems::LayerItem>();
103 
104  // constructing viewModel from sample model
105  DefaultViewModel viewModel(&model);
106 
107  QModelIndex layerIndex = viewModel.index(0, 0);
108  QModelIndex thicknessIndex = viewModel.index(0, 1, layerIndex);
109 
110  QSignalSpy spyDataChanged(&viewModel, &DefaultViewModel::dataChanged);
111 
112  layerItem->setProperty(ToyItems::LayerItem::P_THICKNESS, 50.0);
113  EXPECT_EQ(spyDataChanged.count(), 1);
114 
115  // dataChanged should report thicknessIndex and two roles
116  QList<QVariant> arguments = spyDataChanged.takeFirst();
117  EXPECT_EQ(arguments.size(), 3); // QModelIndex left, QModelIndex right, QVector<int> roles
118  EXPECT_EQ(arguments.at(0).value<QModelIndex>(), thicknessIndex);
119  EXPECT_EQ(arguments.at(1).value<QModelIndex>(), thicknessIndex);
120  QVector<int> expectedRoles = {Qt::DisplayRole, Qt::EditRole};
121  EXPECT_EQ(arguments.at(2).value<QVector<int>>(), expectedRoles);
122 }
123 
124 //! Validates display name
125 
126 TEST_F(ToyLayerItemTest, displayNameInMultiLayer)
127 {
128  ToyItems::SampleModel model;
129  auto multiLayer = model.insertItem<ToyItems::MultiLayerItem>();
130 
131  auto layer0 = model.insertItem<ToyItems::LayerItem>(multiLayer);
132  EXPECT_EQ(layer0->displayName(), "Layer");
133 
134  auto layer1 = model.insertItem<ToyItems::LayerItem>(multiLayer);
135  EXPECT_EQ(layer0->displayName(), "Layer0");
136  EXPECT_EQ(layer1->displayName(), "Layer1");
137 }
138 
139 //! LayerItem as rootItem.
140 
141 TEST_F(ToyLayerItemTest, setRootItemContext)
142 {
143  ToyItems::SampleModel model;
144  auto layer = model.insertItem<ToyItems::LayerItem>();
145  DefaultViewModel viewModel(&model);
146  viewModel.setRootSessionItem(layer);
147 
148  EXPECT_EQ(viewModel.rowCount(QModelIndex()), 2);
149  EXPECT_EQ(viewModel.columnCount(QModelIndex()), 2);
150 
151  // index of item representing thickness
152  QModelIndex thicknessIndex = viewModel.index(0, 0, QModelIndex());
153  EXPECT_EQ(viewModel.sessionItemFromIndex(thicknessIndex),
154  layer->getItem(ToyItems::LayerItem::P_THICKNESS));
155 }
TEST_F(ToyLayerItemTest, initialState)
Initial state.
std::string displayName() const override
Returns display name.
View model to show content of SessionModel in Qt widgets: two column tree with label/data.
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Definition: sessionmodel.h:104
Represents data role of SessionItem in any cell of Qt's trees and tables.
Represents display name of SessionItem in any cell of Qt's trees and tables.
Represents a layer, with thickness and color, and possibly populated with particles.
Definition: toyitems.h:52
static const std::string T_PARTICLES
Definition: toyitems.h:56
static const std::string P_COLOR
Definition: toyitems.h:55
static const std::string P_THICKNESS
Definition: toyitems.h:54
Represents multilayer with collection of layers.
Definition: toyitems.h:44
Tests for toy Layer in the context of model and view model.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
MVVM_MODEL_EXPORT bool IsSinglePropertyTag(const SessionItem &item, const std::string &tag)
Returns true if given item has registered tag, and it belongs to single property.
Definition: itemutils.cpp:91
materialitems.h Collection of materials to populate MaterialModel.
const ModelView::model_type LayerItemType
Definition: toyitems.h:31
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?