1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31
32#include <qtoolbox.h>
33
34QT_FORWARD_DECLARE_CLASS(QToolBox)
35
36class tst_QToolBoxPrivate;
37
38class tst_QToolBox : public QObject
39{
40 Q_OBJECT
41
42protected slots:
43 void currentChanged(int);
44
45private slots:
46 void init();
47 void cleanup();
48
49 void getSetCheck();
50 void populate();
51 void change();
52 void clear();
53
54private:
55 QToolBox *testWidget;
56 int currentIndex;
57
58 tst_QToolBoxPrivate *d;
59};
60
61// Testing get/set functions
62void tst_QToolBox::getSetCheck()
63{
64 QToolBox obj1;
65 QWidget *w1 = new QWidget;
66 QWidget *w2 = new QWidget;
67 QWidget *w3 = new QWidget;
68 QWidget *w4 = new QWidget;
69 QWidget *w5 = new QWidget;
70 obj1.addItem(item: w1, text: "Page1");
71 obj1.addItem(item: w2, text: "Page2");
72 obj1.addItem(item: w3, text: "Page3");
73 obj1.addItem(item: w4, text: "Page4");
74 obj1.addItem(item: w5, text: "Page5");
75
76 // int QToolBox::currentIndex()
77 // void QToolBox::setCurrentIndex(int)
78 obj1.setCurrentIndex(3);
79 QCOMPARE(3, obj1.currentIndex());
80 obj1.setCurrentIndex(INT_MIN);
81 QCOMPARE(3, obj1.currentIndex());
82 obj1.setCurrentIndex(INT_MAX);
83 QCOMPARE(3, obj1.currentIndex());
84 obj1.setCurrentIndex(4);
85 QCOMPARE(4, obj1.currentIndex());
86
87 // QWidget * QToolBox::currentWidget()
88 // void QToolBox::setCurrentWidget(QWidget *)
89 obj1.setCurrentWidget(w1);
90 QCOMPARE(w1, obj1.currentWidget());
91 obj1.setCurrentWidget(w3);
92 QCOMPARE(w3, obj1.currentWidget());
93
94 obj1.setCurrentWidget((QWidget *)0);
95 QCOMPARE(w3, obj1.currentWidget());
96}
97
98class tst_QToolBoxPrivate
99{
100public:
101 int count0, count1, count2, count3, count4;
102 int idx1, idx2, idx3, idx32;
103 int i0, i1, i2, i3, i4;
104 int ci0, ci1, ci2, ci3, ci4;
105 bool ci_correct;
106
107 int manual_count;
108};
109
110void tst_QToolBox::init()
111{
112 currentIndex = -1;
113 testWidget = new QToolBox;
114 connect(sender: testWidget, SIGNAL(currentChanged(int)), receiver: this, SLOT(currentChanged(int)));
115
116 d = new tst_QToolBoxPrivate;
117
118
119 d->count0 = testWidget->count();
120 d->ci0 = currentIndex;
121
122 QWidget *item1 = new QWidget( testWidget );
123 testWidget->addItem( item: item1, text: "Item1" );
124
125 d->count1 = testWidget->count();
126 d->idx1 = testWidget->indexOf(widget: item1);
127 d->ci1 = currentIndex;
128 d->ci_correct = testWidget->widget(index: testWidget->currentIndex()) == item1;
129
130 currentIndex = -1; // reset to make sure signal doesn't fire
131
132 QWidget *item3 = new QWidget( testWidget );
133 testWidget->addItem( item: item3, text: "Item3" );
134
135 d->count2 = testWidget->count();
136 d->idx3 = testWidget->indexOf(widget: item3);
137 d->ci2 = currentIndex;
138
139
140 QWidget *item2 = new QWidget( testWidget );
141 testWidget->insertItem( index: 1, item: item2, text: "Item2");
142
143 d->count3 = testWidget->count();
144 d->idx2 = testWidget->indexOf(widget: item2);
145 d->idx32 = testWidget->indexOf(widget: item3);
146 d->ci3 = currentIndex;
147
148 QWidget *item0 = new QWidget( testWidget );
149 testWidget->insertItem( index: 0, item: item0, text: "Item0");
150
151 d->count4 = testWidget->count();
152 d->i0 = testWidget->indexOf(widget: item0);
153 d->i1 = testWidget->indexOf(widget: item1);
154 d->i2 = testWidget->indexOf(widget: item2);
155 d->i3 = testWidget->indexOf(widget: item3);
156 d->ci4 = currentIndex;
157
158 d->manual_count = 4;
159}
160
161void tst_QToolBox::cleanup()
162{
163 delete testWidget;
164 delete d;
165}
166
167void tst_QToolBox::currentChanged(int index)
168{
169 currentIndex = index;
170}
171
172void tst_QToolBox::populate()
173{
174 // verify preconditions
175 QCOMPARE( d->count0, 0 );
176 QCOMPARE( d->ci0, -1 );
177 QVERIFY( d->ci_correct );
178
179 QCOMPARE( d->count1, 1 );
180 QCOMPARE( d->idx1, 0 );
181 QCOMPARE( d->ci1, 0 );
182
183 QCOMPARE( d->count2, 2 );
184 QCOMPARE( d->idx3, 1 );
185 QCOMPARE( d->ci2, -1 );
186
187 QCOMPARE( d->count3, 3 );
188 QCOMPARE( d->idx2, 1 );
189 QCOMPARE( d->idx32, 2 );
190 QCOMPARE( d->ci3, -1 );
191
192
193 QCOMPARE( d->count4, 4 );
194 QCOMPARE( d->i0, 0 );
195 QCOMPARE( d->i1, 1 );
196 QCOMPARE( d->i2, 2 );
197 QCOMPARE( d->i3, 3 );
198 QCOMPARE( d->ci4, 1 );
199
200 QCOMPARE (testWidget->count(), d->manual_count);
201 int oldcount = testWidget->count();
202
203 QWidget *item = new QWidget( testWidget );
204 testWidget->addItem( item, text: "Item");
205 d->manual_count++;
206
207 QCOMPARE( testWidget->count(), oldcount+1 );
208 QCOMPARE( testWidget->indexOf(item), oldcount );
209 QCOMPARE( testWidget->widget(oldcount), item );
210}
211
212void tst_QToolBox::change()
213{
214 QWidget *lastItem = testWidget->widget(index: testWidget->count());
215 QVERIFY( !lastItem );
216 lastItem = testWidget->widget(index: testWidget->count() - 1);
217 QVERIFY( lastItem );
218
219 for ( int c = 0; c < testWidget->count(); ++c ) {
220 QString label = "Item " + QString::number(c);
221 testWidget->setItemText(index: c, text: label);
222 QCOMPARE( testWidget->itemText(c), label );
223 }
224
225 testWidget->setCurrentIndex( 0 );
226 QCOMPARE( currentIndex, 0 );
227
228 currentIndex = -1; // reset to make sure signal doesn't fire
229 testWidget->setCurrentIndex( 0 );
230 QCOMPARE( currentIndex, -1 );
231 QCOMPARE( testWidget->currentIndex(), 0 );
232
233 testWidget->setCurrentIndex( testWidget->count() );
234 QCOMPARE( currentIndex, -1 );
235 QCOMPARE( testWidget->currentIndex(), 0 );
236
237 testWidget->setCurrentIndex( 1 );
238 QCOMPARE( currentIndex, 1 );
239 QCOMPARE( testWidget->currentIndex(), 1 );
240
241 testWidget->setItemEnabled( index: testWidget->currentIndex(), enabled: false );
242 QCOMPARE( currentIndex, 2 );
243 QCOMPARE( testWidget->currentIndex(), 2 );
244
245 currentIndex = -1;
246 testWidget->setItemEnabled( index: testWidget->indexOf(widget: lastItem), enabled: false );
247 QCOMPARE( currentIndex, -1 );
248 QCOMPARE( testWidget->currentIndex(), 2 );
249
250 testWidget->setItemEnabled( index: testWidget->currentIndex(), enabled: false );
251 QCOMPARE( currentIndex, 0 );
252
253 currentIndex = -1;
254 testWidget->setItemEnabled( index: testWidget->currentIndex(), enabled: false );
255 QCOMPARE( currentIndex, -1 );
256
257 testWidget->setItemEnabled( index: 1, enabled: true );
258}
259
260void tst_QToolBox::clear()
261{
262 // precondition: only item(1) is enabled
263 QCOMPARE( testWidget->count(), 4 );
264 testWidget->setCurrentIndex(0);
265 currentIndex = -1;
266
267 // delete current item(0)
268 QPointer<QWidget> item = testWidget->widget(index: testWidget->currentIndex());
269 testWidget->removeItem(index: testWidget->indexOf(widget: item));
270 QVERIFY(item);
271 QCOMPARE( testWidget->count(), 3 );
272 QCOMPARE( testWidget->indexOf(item), -1 );
273 QCOMPARE( testWidget->currentIndex(), 0 );
274 QCOMPARE(currentIndex, 0 );
275
276 currentIndex = -1;
277
278 item = testWidget->widget(index: 1);
279 testWidget->removeItem(index: testWidget->indexOf(widget: item));
280 QVERIFY( item );
281 QCOMPARE( currentIndex, -1 );
282 QCOMPARE( testWidget->currentIndex(), 0 );
283 QCOMPARE( testWidget->count(), 2 );
284 QCOMPARE( testWidget->indexOf(item), -1 );
285
286 item = testWidget->widget(index: 1);
287 delete item;
288 QCOMPARE( testWidget->count(), 1 );
289 QCOMPARE( currentIndex, -1 );
290 currentIndex = testWidget->currentIndex();
291
292 item = testWidget->widget(index: 0);
293 testWidget->removeItem(index: testWidget->indexOf(widget: item));
294 QCOMPARE( testWidget->count(), 0 );
295 QCOMPARE( currentIndex, -1 );
296}
297
298QTEST_MAIN(tst_QToolBox)
299#include "tst_qtoolbox.moc"
300

source code of qtbase/tests/auto/widgets/widgets/qtoolbox/tst_qtoolbox.cpp