BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
viewitem.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/viewitem.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 "test_utils.h"
18 #include <stdexcept>
19 
20 using namespace ModelView;
21 
22 //! Tests for ViewItem class.
23 
24 class ViewItemTest : public ::testing::Test {
25 public:
27  class TestItem : public ViewItem {
28  public:
29  TestItem() : ViewItem(nullptr, 0) {}
30  ~TestItem() override;
31  };
32 
33  using children_t = std::vector<std::unique_ptr<ViewItem>>;
34  using expected_t = std::vector<ViewItem*>;
35 
36  //! Helper function to get two vectors, each ncolumns length, in the form of a pair.
37  //! First vector contains unique_ptr objects, second vector bare pointers to same objects.
38  //! First vector is intended to be moved inside a model, second vector is to validate
39  //! the content of a model after the move.
40 
41  std::pair<children_t, expected_t> test_data(int ncolumns)
42  {
43  auto vector_of_unique = TestUtils::create_row<ViewItem, TestItem>(ncolumns);
44  auto vector_of_pointers = TestUtils::create_pointers(vector_of_unique);
45  return std::make_pair(std::move(vector_of_unique), std::move(vector_of_pointers));
46  }
47 };
48 
49 ViewItemTest::~ViewItemTest() = default;
51 
52 //! Initial state of RefViewItem.
53 
54 TEST_F(ViewItemTest, initialState)
55 {
56  TestItem view_item;
57 
58  EXPECT_EQ(view_item.rowCount(), 0);
59  EXPECT_EQ(view_item.columnCount(), 0);
60  EXPECT_EQ(view_item.row(), -1);
61  EXPECT_EQ(view_item.column(), -1);
62  EXPECT_EQ(view_item.parent(), nullptr);
63  EXPECT_THROW(view_item.child(0, 0), std::runtime_error);
64  EXPECT_EQ(view_item.item(), nullptr);
65  EXPECT_EQ(view_item.item_role(), 0);
66 }
67 
68 //! Append single item as row.
69 
70 TEST_F(ViewItemTest, appendRow)
71 {
72  auto [children, expected] = test_data(/*ncolumns*/ 1);
73 
74  // appending row with single item
75  TestItem view_item;
76  view_item.appendRow(std::move(children));
77 
78  // checking parent
79  EXPECT_EQ(view_item.rowCount(), 1);
80  EXPECT_EQ(view_item.columnCount(), 1);
81  EXPECT_EQ(view_item.child(0, 0), expected[0]);
82  EXPECT_THROW(view_item.child(0, 1), std::runtime_error);
83 
84  // checking appended child
85  EXPECT_EQ(expected[0]->parent(), &view_item);
86  EXPECT_EQ(expected[0]->row(), 0);
87  EXPECT_EQ(expected[0]->column(), 0);
88 }
89 
90 //! Remove row.
91 
93 {
94  auto [children, expected] = test_data(/*ncolumns*/ 1);
95 
96  // appending row with single item
97  TestItem view_item;
98  view_item.appendRow(std::move(children));
99  view_item.removeRow(0);
100 
101  // checking parent
102  EXPECT_EQ(view_item.rowCount(), 0);
103  EXPECT_EQ(view_item.columnCount(), 0);
104 }
105 
106 //! Append two rows with two items each.
107 
108 TEST_F(ViewItemTest, appendTwoRows)
109 {
110  // preparing two rows of children, two columns each
111  auto [children_row0, expected_row0] = test_data(/*ncolumns*/ 2);
112  auto [children_row1, expected_row1] = test_data(/*ncolumns*/ 2);
113 
114  // appending rows
115  TestItem view_item;
116  view_item.appendRow(std::move(children_row0));
117  view_item.appendRow(std::move(children_row1));
118 
119  EXPECT_EQ(view_item.rowCount(), 2);
120  EXPECT_EQ(view_item.columnCount(), 2);
121  EXPECT_EQ(view_item.child(0, 0), expected_row0[0]);
122  EXPECT_EQ(view_item.child(0, 1), expected_row0[1]);
123  EXPECT_EQ(view_item.child(1, 0), expected_row1[0]);
124  EXPECT_EQ(view_item.child(1, 1), expected_row1[1]);
125  EXPECT_THROW(view_item.child(2, 2), std::runtime_error);
126 
127  // checking parents
128  EXPECT_EQ(expected_row0[0]->parent(), &view_item);
129  EXPECT_EQ(expected_row0[1]->parent(), &view_item);
130  EXPECT_EQ(expected_row1[0]->parent(), &view_item);
131  EXPECT_EQ(expected_row1[1]->parent(), &view_item);
132 
133  // checking row and column of children
134  EXPECT_EQ(expected_row0[0]->row(), 0);
135  EXPECT_EQ(expected_row0[1]->row(), 0);
136  EXPECT_EQ(expected_row1[0]->row(), 1);
137  EXPECT_EQ(expected_row1[1]->row(), 1);
138  EXPECT_EQ(expected_row0[0]->column(), 0);
139  EXPECT_EQ(expected_row0[1]->column(), 1);
140  EXPECT_EQ(expected_row1[0]->column(), 0);
141  EXPECT_EQ(expected_row1[1]->column(), 1);
142 
143  // attempt to add row with different amount of children should fail
144  auto [children_row2, expected_row2] = test_data(/*ncolumns*/ 1);
145  EXPECT_THROW(view_item.appendRow(std::move(children_row2)), std::runtime_error);
146  EXPECT_EQ(view_item.rowCount(), 2);
147  EXPECT_EQ(view_item.columnCount(), 2);
148 }
149 
150 //! Append two rows with two items each.
151 
152 TEST_F(ViewItemTest, insertRowsThenRemove)
153 {
154  // preparing two rows of children, two columns each
155  auto [children_row0, expected_row0] = test_data(/*ncolumns*/ 2);
156  auto [children_row1, expected_row1] = test_data(/*ncolumns*/ 2);
157  auto [children_row2, expected_row2] = test_data(/*ncolumns*/ 2);
158 
159  // appending rows
160  TestItem view_item;
161  view_item.appendRow(std::move(children_row0));
162  view_item.appendRow(std::move(children_row1));
163  view_item.insertRow(1, std::move(children_row2)); // inserting in-between
164 
165  EXPECT_EQ(view_item.rowCount(), 3);
166  EXPECT_EQ(view_item.columnCount(), 2);
167  EXPECT_EQ(view_item.child(0, 0), expected_row0[0]);
168  EXPECT_EQ(view_item.child(0, 1), expected_row0[1]);
169  EXPECT_EQ(view_item.child(1, 0), expected_row2[0]);
170  EXPECT_EQ(view_item.child(1, 1), expected_row2[1]);
171  EXPECT_EQ(view_item.child(2, 0), expected_row1[0]);
172  EXPECT_EQ(view_item.child(2, 1), expected_row1[1]);
173 
174  // checking parents
175  EXPECT_EQ(expected_row0[0]->parent(), &view_item);
176  EXPECT_EQ(expected_row0[1]->parent(), &view_item);
177  EXPECT_EQ(expected_row1[0]->parent(), &view_item);
178  EXPECT_EQ(expected_row1[1]->parent(), &view_item);
179  EXPECT_EQ(expected_row2[0]->parent(), &view_item);
180  EXPECT_EQ(expected_row2[1]->parent(), &view_item);
181 
182  // checking row and column of children
183  EXPECT_EQ(expected_row0[0]->row(), 0);
184  EXPECT_EQ(expected_row0[1]->row(), 0);
185  EXPECT_EQ(expected_row1[0]->row(), 2);
186  EXPECT_EQ(expected_row1[1]->row(), 2);
187  EXPECT_EQ(expected_row2[0]->row(), 1);
188  EXPECT_EQ(expected_row2[1]->row(), 1);
189  EXPECT_EQ(expected_row0[0]->column(), 0);
190  EXPECT_EQ(expected_row0[1]->column(), 1);
191  EXPECT_EQ(expected_row1[0]->column(), 0);
192  EXPECT_EQ(expected_row1[1]->column(), 1);
193  EXPECT_EQ(expected_row2[0]->column(), 0);
194  EXPECT_EQ(expected_row2[1]->column(), 1);
195 
196  // removing middle row
197  view_item.removeRow(1);
198  EXPECT_EQ(view_item.child(0, 0), expected_row0[0]);
199  EXPECT_EQ(view_item.child(0, 1), expected_row0[1]);
200  EXPECT_EQ(view_item.child(1, 0), expected_row1[0]);
201  EXPECT_EQ(view_item.child(1, 1), expected_row1[1]);
202 }
203 
204 //! Clean item's children.
205 
207 {
208  auto [children, expected] = test_data(/*ncolumns*/ 1);
209 
210  TestItem view_item;
211  view_item.appendRow(std::move(children));
212  view_item.clear();
213 
214  EXPECT_EQ(view_item.rowCount(), 0);
215  EXPECT_EQ(view_item.columnCount(), 0);
216 }
217 
219 {
220  auto [children_row0, expected_row0] = test_data(/*ncolumns*/ 2);
221  auto [children_row1, expected_row1] = test_data(/*ncolumns*/ 2);
222 
223  TestItem view_item;
224  view_item.appendRow(std::move(children_row0));
225  view_item.appendRow(std::move(children_row1));
226 
227  std::vector<ViewItem*> expected;
228  std::copy(expected_row0.begin(), expected_row0.end(), std::back_inserter(expected));
229  std::copy(expected_row1.begin(), expected_row1.end(), std::back_inserter(expected));
230 
231  EXPECT_EQ(view_item.children(), expected);
232 }
Represents the view of SessionItem's data in a single cell of ViewModel.
Definition: viewitem.h:29
Tests for ViewItem class.
std::vector< std::unique_ptr< ViewItem > > children_t
std::vector< ViewItem * > expected_t
std::pair< children_t, expected_t > test_data(int ncolumns)
Helper function to get two vectors, each ncolumns length, in the form of a pair.
Defines class CLASS?
void removeRow(QGridLayout *layout, int row, bool deleteWidgets=true)
Removes row from grid layout (important: doesn't change row count).
Definition: LayoutUtils.cpp:58
materialitems.h Collection of materials to populate MaterialModel.
auto create_pointers(const std::vector< std::unique_ptr< T >> &vec)
Creates vector of pointers from vector of unique_ptr.
Definition: test_utils.h:90
Defines class CLASS?
Defines class CLASS?
TEST_F(ViewItemTest, initialState)
Initial state of RefViewItem.