BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
viewmodelfactory.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/viewmodelfactory.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"
22 #include "test_utils.h"
23 
24 using namespace ModelView;
25 
26 namespace {
27 std::unique_ptr<ViewModelController> createController(SessionModel* model, ViewModelBase* viewModel)
28 {
29  return Factory::CreateController<TopItemsStrategy, LabelDataRowStrategy>(model, viewModel);
30 }
31 } // namespace
32 
33 class ViewModelFactoryTest : public ::testing::Test {
34 public:
36 
37  class CustomModel : public ViewModel {
38  public:
39  CustomModel(SessionModel* model) : ViewModel(createController(model, this), nullptr) {}
40  };
41 };
42 
44 
45 //! Creating DefaultViewModel using strategies.
46 
47 TEST_F(ViewModelFactoryTest, createDefaultViewModelInitial)
48 {
49  SessionModel model;
50 
51  auto viewModel = Factory::CreateViewModel<AllChildrenStrategy, LabelDataRowStrategy>(&model);
52  EXPECT_EQ(viewModel->rowCount(), 0);
53  EXPECT_EQ(viewModel->columnCount(), 0);
54  EXPECT_EQ(viewModel->sessionItemFromIndex(QModelIndex()), model.rootItem());
55 }
56 
57 //! Creating DefaultViewModel using strategies, validating behaviour on single item in SessionModel.
58 
59 TEST_F(ViewModelFactoryTest, createDefaultViewModelUseProperty)
60 {
61  SessionModel model;
62  auto propertyItem = model.insertItem<PropertyItem>();
63  propertyItem->setData(42.0);
64 
65  auto viewModel = Factory::CreateViewModel<AllChildrenStrategy, LabelDataRowStrategy>(&model);
66 
67  EXPECT_EQ(viewModel->rowCount(), 1);
68  EXPECT_EQ(viewModel->columnCount(), 2);
69 
70  // accessing first child under the root item
71  QModelIndex labelIndex = viewModel->index(0, 0);
72  QModelIndex dataIndex = viewModel->index(0, 1);
73 
74  // it should be ViewLabelItem looking at our PropertyItem item
75  auto labelItem = dynamic_cast<ViewLabelItem*>(viewModel->itemFromIndex(labelIndex));
76  ASSERT_TRUE(labelItem != nullptr);
77  EXPECT_EQ(labelItem->item(), propertyItem);
78 
79  auto dataItem = dynamic_cast<ViewDataItem*>(viewModel->itemFromIndex(dataIndex));
80  ASSERT_TRUE(dataItem != nullptr);
81  EXPECT_EQ(dataItem->item(), propertyItem);
82 }
83 
84 //! Creating DefaultViewModel using strategies, validating behaviour on single item in SessionModel.
85 
86 TEST_F(ViewModelFactoryTest, createCustomViewModel)
87 {
88  SessionModel model;
89  auto propertyItem = model.insertItem<PropertyItem>();
90  propertyItem->setData(42.0);
91 
92  CustomModel viewModel(&model);
93 
94  EXPECT_EQ(viewModel.rowCount(), 1);
95  EXPECT_EQ(viewModel.columnCount(), 2);
96 
97  // accessing first child under the root item
98  QModelIndex labelIndex = viewModel.index(0, 0);
99  QModelIndex dataIndex = viewModel.index(0, 1);
100 
101  // it should be ViewLabelItem looking at our PropertyItem item
102  auto labelItem = dynamic_cast<ViewLabelItem*>(viewModel.itemFromIndex(labelIndex));
103  ASSERT_TRUE(labelItem != nullptr);
104  EXPECT_EQ(labelItem->item(), propertyItem);
105 
106  auto dataItem = dynamic_cast<ViewDataItem*>(viewModel.itemFromIndex(dataIndex));
107  ASSERT_TRUE(dataItem != nullptr);
108  EXPECT_EQ(dataItem->item(), propertyItem);
109 }
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
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
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.
Base class for all view models to show content of SessionModel in Qt views.
Definition: viewmodelbase.h:31
Main class to represent content of SessionModel in Qt's trees and tables.
Definition: viewmodel.h:29
Defines class CLASS?
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?
Defines class CLASS?
TEST_F(ViewModelFactoryTest, createDefaultViewModelInitial)
Creating DefaultViewModel using strategies.