Debian Codegraph @

plugins/dockers/storyboarddocker/tests/StoryboardModelTest.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2020 Saurabh Kumar <[email protected]>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "StoryboardModelTest.h"
8
9 #include <simpletest.h>
10 #include <StoryboardModel.h>
11 #include <CommentModel.h>
12 #include <QAbstractItemModelTester>
13
14 void StoryboardModelTest::init()
15 {
16 /* TODO: Seems like it's wrong to be testing the storyboard model without image data.
17 * We should reconsider this later down the line. The model should depend on having an image
18 * to be valid! */
19
20 m_commentModel = new StoryboardCommentModel(this);
21 m_storyboardModel = new StoryboardModel(this);
22 m_storyboardModel->setCommentModel(m_commentModel);
23
24 m_commentModel->insertRows(m_commentModel->rowCount(),1);
25 int pos = m_storyboardModel->rowCount();
26 m_storyboardModel->insertRows(pos,1);
27 m_storyboardModel->insertChildRows(pos);
28 QCOMPARE(m_commentModel->rowCount(), 1);
29 }
30
31
32 void StoryboardModelTest::cleanup()
33 {
34 m_storyboardModel->removeRows(0, m_storyboardModel->rowCount());
35 m_commentModel->removeRows(0, m_commentModel->rowCount());
36
37 delete m_storyboardModel;
38 delete m_commentModel;
39 }
40
41 void StoryboardModelTest::testAddComment()
42 {
43
44 auto sbTester = new QAbstractItemModelTester(m_storyboardModel, this);
45 auto commentTester = new QAbstractItemModelTester(m_commentModel, this);
46 Q_UNUSED(sbTester);
47 Q_UNUSED(commentTester);
48
49 QModelIndex storyboardItem = m_storyboardModel->index(0,0);
50 int rowStoryboard = m_storyboardModel->rowCount(storyboardItem);
51 int commentCount = m_commentModel->rowCount();
52
53 QCOMPARE(rowStoryboard, commentCount + 4);
54 m_commentModel->insertRows(m_commentModel->rowCount(),1);
55
56 QCOMPARE(commentCount + 1, m_commentModel->rowCount());
57 QCOMPARE(m_storyboardModel->rowCount(m_storyboardModel->index(0,0)), m_commentModel->rowCount() + 4);
58
59 #if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
60 // add at an invalid position
61 // if assertions are enabled, this will make Qt crash in QAbstractItemModel::beginInsertRows
62 m_commentModel->insertRows(-1, 1);
63
64 QCOMPARE(commentCount + 1, m_commentModel->rowCount());
65 QCOMPARE(m_storyboardModel->rowCount(m_storyboardModel->index(0,0)), m_commentModel->rowCount() + 4);
66 #endif
67 }
68
69 void StoryboardModelTest::testRemoveComment()
70 {
71
72 auto sbTester = new QAbstractItemModelTester(m_storyboardModel, this);
73 auto commentTester = new QAbstractItemModelTester(m_commentModel, this);
74 Q_UNUSED(sbTester);
75 Q_UNUSED(commentTester);
76
77 int rowStoryboard = m_storyboardModel->rowCount(m_storyboardModel->index(0,0));
78 int rowsComment = m_commentModel->rowCount();
79
80 QCOMPARE(rowStoryboard, rowsComment + 4);
81
82 m_commentModel->removeRows(m_commentModel->rowCount() - 1,1);
83
84 QCOMPARE(rowsComment - 1, m_commentModel->rowCount());
85 QCOMPARE(m_storyboardModel->rowCount(m_storyboardModel->index(0,0)), m_commentModel->rowCount() + 4);
86
87 #if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
88 // if assertions are enabled, this will make Qt crash in QAbstractItemModel::beginRemoveRows
89 m_commentModel->removeRows(-1,1);
90
91 QCOMPARE(rowsComment - 1, m_commentModel->rowCount());
92 QCOMPARE(m_storyboardModel->rowCount(m_storyboardModel->index(0,0)), m_commentModel->rowCount() + 4);
93 #endif
94 }
95
96 void StoryboardModelTest::testCommentNameChanged()
97 {
98 auto tester = new QAbstractItemModelTester(m_commentModel, this);
99 Q_UNUSED(tester);
100 QModelIndex index = m_commentModel->index(m_commentModel->rowCount() - 1, 0);
101 QVariant value = QVariant(QString("newValue"));
102 m_commentModel->setData(index, value);
103
104 QCOMPARE(QString("newValue"), m_commentModel->data(index));
105 }
106
107 void StoryboardModelTest::testFrameAdded()
108 {
109 int rows = m_storyboardModel->rowCount();
110 auto tester = new QAbstractItemModelTester(m_storyboardModel, 0);
111 Q_UNUSED(tester);
112 int pos = m_storyboardModel->rowCount();
113 m_storyboardModel->insertRows(pos, 1);
114 m_storyboardModel->insertChildRows(pos);
115
116 QCOMPARE(rows + 1, m_storyboardModel->rowCount());
117 }
118
119 void StoryboardModelTest::testFrameRemoved()
120 {
121 int rows = m_storyboardModel->rowCount();
122 auto tester = new QAbstractItemModelTester(m_storyboardModel, 0);
123 Q_UNUSED(tester);
124 m_storyboardModel->removeRows(m_storyboardModel->rowCount() - 1,1);
125
126 QCOMPARE(rows-1, m_storyboardModel->rowCount());
127 }
128
129
130 void StoryboardModelTest::testFrameChanged()
131 {
132
133 auto tester = new QAbstractItemModelTester(m_storyboardModel, 0);
134 Q_UNUSED(tester);
135 QModelIndex parentIndex = m_storyboardModel->index(m_storyboardModel->rowCount() - 1, 0);
136 QModelIndex frameIndex = m_storyboardModel->index(0, 0, parentIndex);
137 QVariant value = QVariant(100);
138 m_storyboardModel->setData(frameIndex, value, Qt::EditRole);
139
140 QCOMPARE(m_storyboardModel->data(frameIndex).toInt(), 100);
141
142 //invalid value shouldn't change anything
143 QVariant invalidValue = QVariant(-100);
144 m_storyboardModel->setData(frameIndex, invalidValue, Qt::EditRole);
145
146 QCOMPARE(m_storyboardModel->data(frameIndex).toInt(), 100);
147 }
148
149 void StoryboardModelTest::testDurationChanged()
150 {
151 auto tester = new QAbstractItemModelTester(m_storyboardModel, 0);
152 Q_UNUSED(tester);
153 QModelIndex parentIndex = m_storyboardModel->index(m_storyboardModel->rowCount() - 1, 0);
154 QModelIndex secIndex = m_storyboardModel->index(2, 0, parentIndex);
155 QVariant value = QVariant(100);
156 m_storyboardModel->setData(secIndex, value, Qt::EditRole);
157
158 QCOMPARE(m_storyboardModel->data(secIndex).toInt(), 100);
159
160 //invalid value shouldn't change anything
161 QVariant invalidValue = QVariant(-100);
162 m_storyboardModel->setData(secIndex, invalidValue, Qt::EditRole);
163
164 QCOMPARE(m_storyboardModel->data(secIndex).toInt(), 0);
165 }
166
167 void StoryboardModelTest::testCommentChanged()
168 {
169 auto tester = new QAbstractItemModelTester(m_storyboardModel, 0);
170 Q_UNUSED(tester);
171 QModelIndex parentIndex = m_storyboardModel->index(m_storyboardModel->rowCount() - 1, 0);
172 QModelIndex commentIndex = m_storyboardModel->index(4, 0, parentIndex);
173 QVariant value = QVariant(QString("newComment"));
174 m_storyboardModel->setData(commentIndex, value, Qt::EditRole);
175
176 QCOMPARE(m_storyboardModel->data(commentIndex).toString(), "newComment");
177 }
178
179 SIMPLE_TEST_MAIN(StoryboardModelTest)