1
/****************************************************************************
2
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the test suite of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
43
#include <QtTest/QtTest>
44
#include <qmap.h>
45
#include <qapplication.h>
46
#include <q3listview.h>
47
48
//TESTED_CLASS=
49
//TESTED_FILES=
50
51
class tst_Q3CheckListItem : public QObject
52
{
53
    Q_OBJECT
54
55
public:
56
    tst_Q3CheckListItem();
57
    virtual ~tst_Q3CheckListItem();
58
59
private slots:
60
    void setState_data();
61
    void setState();
62
};
63
64
tst_Q3CheckListItem::tst_Q3CheckListItem()
65
{
66
67
}
68
69
tst_Q3CheckListItem::~tst_Q3CheckListItem()
70
{
71
72
}
73
74
typedef QMap<QString, Q3CheckListItem::ToggleState> StateMap;
75
76
QDataStream &operator>>( QDataStream &s, Q3CheckListItem::ToggleState &state )
77
{
78
    int tmp;
79
    s >> tmp;
80
    state = (Q3CheckListItem::ToggleState)tmp;
81
    return s;
82
}
83
Q_DECLARE_METATYPE(StateMap)
84
85
86
void tst_Q3CheckListItem::setState_data()
87
{
88
    QTest::addColumn<StateMap>("states");
89
    QTest::addColumn<StateMap>("expectedStates");
90
    {
91
	StateMap s,e;
92
	s.insert( "item6", Q3CheckListItem::On );
93
94
	e.insert( "item5", Q3CheckListItem::NoChange );
95
	e.insert( "item1", Q3CheckListItem::NoChange );
96
	QTest::newRow( "children_affect_parent01" ) << s << e;
97
    }
98
    {
99
	StateMap s,e;
100
	s.insert( "item6", Q3CheckListItem::On );
101
	s.insert( "item7", Q3CheckListItem::On );
102
	s.insert( "item8", Q3CheckListItem::On );
103
104
	e.insert( "item5", Q3CheckListItem::On );
105
	e.insert( "item1", Q3CheckListItem::NoChange );
106
	QTest::newRow( "children_affect_parent02" ) << s << e;
107
    }
108
    {
109
	StateMap s,e;
110
	s.insert( "a_item5", Q3CheckListItem::On );
111
	s.insert( "b_item6", Q3CheckListItem::Off );
112
	s.insert( "b_item7", Q3CheckListItem::Off );
113
	s.insert( "b_item8", Q3CheckListItem::Off );
114
115
	e.insert( "item5", Q3CheckListItem::Off );
116
	QTest::newRow( "children_affect_parent03" ) << s << e;
117
    }
118
    {
119
	StateMap s,e;
120
	s.insert( "item2", Q3CheckListItem::On );
121
	s.insert( "item3", Q3CheckListItem::On );
122
	s.insert( "item4", Q3CheckListItem::On );
123
	s.insert( "item6", Q3CheckListItem::On );
124
	s.insert( "item7", Q3CheckListItem::On );
125
	s.insert( "item8", Q3CheckListItem::On );
126
	s.insert( "item10", Q3CheckListItem::On );
127
	s.insert( "item11", Q3CheckListItem::On );
128
	s.insert( "item12", Q3CheckListItem::On );
129
130
	e.insert( "item5", Q3CheckListItem::On );
131
	e.insert( "item9", Q3CheckListItem::On );
132
	e.insert( "item1", Q3CheckListItem::On );
133
	QTest::newRow( "children_affect_parent04" ) << s << e;
134
    }
135
    {
136
	StateMap s,e;
137
	s.insert( "a_item6", Q3CheckListItem::On );
138
	s.insert( "b_item6", Q3CheckListItem::Off );
139
140
	e.insert( "item6", Q3CheckListItem::Off );
141
	QTest::newRow( "setting_resetting01" ) << s << e;
142
    }
143
    {
144
	StateMap s,e;
145
	s.insert( "item1", Q3CheckListItem::On );
146
147
	e.insert( "item2", Q3CheckListItem::On );
148
	e.insert( "item3", Q3CheckListItem::On );
149
	e.insert( "item4", Q3CheckListItem::On );
150
	e.insert( "item5", Q3CheckListItem::On );
151
	e.insert( "item6", Q3CheckListItem::On );
152
	e.insert( "item7", Q3CheckListItem::On );
153
	e.insert( "item8", Q3CheckListItem::On );
154
	e.insert( "item9", Q3CheckListItem::On );
155
	e.insert( "item10", Q3CheckListItem::On );
156
	e.insert( "item11", Q3CheckListItem::On );
157
	e.insert( "item12", Q3CheckListItem::On );
158
	QTest::newRow( "parrent_affecting_children01" ) << s << e;
159
    }
160
    {
161
	StateMap s,e;
162
	s.insert( "a_item11", Q3CheckListItem::On );
163
	s.insert( "a_item4", Q3CheckListItem::Off );
164
	s.insert( "a_item7", Q3CheckListItem::On );
165
	s.insert( "b_item1", Q3CheckListItem::On );
166
167
	e.insert( "item2", Q3CheckListItem::On );
168
	e.insert( "item3", Q3CheckListItem::On );
169
	e.insert( "item4", Q3CheckListItem::On );
170
	e.insert( "item5", Q3CheckListItem::On );
171
	e.insert( "item6", Q3CheckListItem::On );
172
	e.insert( "item7", Q3CheckListItem::On );
173
	e.insert( "item8", Q3CheckListItem::On );
174
	e.insert( "item9", Q3CheckListItem::On );
175
	e.insert( "item10", Q3CheckListItem::On );
176
	e.insert( "item11", Q3CheckListItem::On );
177
	e.insert( "item12", Q3CheckListItem::On );
178
	QTest::newRow( "parrent_affecting_children02" ) << s << e;
179
    }
180
    {
181
	StateMap s,e;
182
	s.insert( "a_item7", Q3CheckListItem::On );
183
	s.insert( "b_item5", Q3CheckListItem::Off );
184
185
	e.insert( "item6", Q3CheckListItem::Off );
186
	e.insert( "item7", Q3CheckListItem::Off );
187
	e.insert( "item8", Q3CheckListItem::Off );
188
	QTest::newRow( "parrent_affecting_children03" ) << s << e;
189
    }
190
    {
191
	StateMap s,e;
192
	s.insert( "a_item7", Q3CheckListItem::On );
193
	s.insert( "b_item5", Q3CheckListItem::Off );
194
195
	e.insert( "item6", Q3CheckListItem::Off );
196
	e.insert( "item7", Q3CheckListItem::Off );
197
	e.insert( "item8", Q3CheckListItem::Off );
198
	QTest::newRow( "parrent_affecting_children04" ) << s << e;
199
    }
200
    {
201
	StateMap s,e;
202
	s.insert( "item11", Q3CheckListItem::On );
203
204
	e.insert( "item9", Q3CheckListItem::NoChange );
205
	QTest::newRow( "tristate_01" ) << s << e;
206
    }
207
    {
208
	StateMap s,e;
209
	s.insert( "item11", Q3CheckListItem::NoChange );
210
211
	e.insert( "item11", Q3CheckListItem::Off );
212
	QTest::newRow( "tristate_02" ) << s << e;
213
    }
214
    {
215
	StateMap s,e;
216
	// set children
217
	s.insert( "a_item10", Q3CheckListItem::Off );
218
	s.insert( "a_item11", Q3CheckListItem::On );
219
	s.insert( "a_item12", Q3CheckListItem::On );
220
	// set parent to On (but also saving old state)
221
	s.insert( "b_item9", Q3CheckListItem::On );
222
	// recalling old state
223
	s.insert( "c_item9", Q3CheckListItem::NoChange );
224
225
	e.insert( "item9", Q3CheckListItem::NoChange );
226
	e.insert( "item10", Q3CheckListItem::Off );
227
	e.insert( "item11", Q3CheckListItem::On );
228
	e.insert( "item12", Q3CheckListItem::On );
229
	QTest::newRow( "tristate_03" ) << s << e;
230
    }
231
    {
232
	StateMap s,e;
233
	// set children
234
	s.insert( "a_item10", Q3CheckListItem::Off );
235
	s.insert( "a_item11", Q3CheckListItem::On );
236
	s.insert( "a_item12", Q3CheckListItem::On );
237
	// set parent to On (but also saving old state)
238
	s.insert( "b_item9", Q3CheckListItem::On );
239
	// recalling old state
240
	s.insert( "c_item9", Q3CheckListItem::NoChange );
241
	// setting the last child to on, which should also set the history for item9 to all on
242
	s.insert( "d_item10", Q3CheckListItem::On );
243
	// recalling old state which should all be On for all children, in effect setting item9 to On as well
244
	s.insert( "e_item9", Q3CheckListItem::NoChange );
245
246
       	e.insert( "item9", Q3CheckListItem::On );
247
	e.insert( "item10", Q3CheckListItem::On );
248
	e.insert( "item11", Q3CheckListItem::On );
249
	e.insert( "item12", Q3CheckListItem::On );
250
	QTest::newRow( "tristate_04" ) << s << e;
251
    }
252
    {
253
	StateMap s,e;
254
	// set children
255
	s.insert( "a_item10", Q3CheckListItem::Off );
256
	s.insert( "a_item11", Q3CheckListItem::On );
257
	s.insert( "a_item12", Q3CheckListItem::On );
258
	// set parent to On (but also saving old state)
259
	s.insert( "b_item9", Q3CheckListItem::On );
260
	// recalling old state
261
	s.insert( "c_item9", Q3CheckListItem::NoChange );
262
	// setting the last two children to Off, which should also set the history for item9 to all Off
263
	s.insert( "d_item11", Q3CheckListItem::Off );
264
	s.insert( "d_item12", Q3CheckListItem::Off );
265
	// recalling old state which should all be Off for all children, in effect setting item9 to Off
266
	s.insert( "e_item9", Q3CheckListItem::NoChange );
267
268
	e.insert( "item9", Q3CheckListItem::Off );
269
	e.insert( "item10", Q3CheckListItem::Off );
270
	e.insert( "item11", Q3CheckListItem::Off );
271
	e.insert( "item12", Q3CheckListItem::Off );
272
	QTest::newRow( "tristate_05" ) << s << e;
273
    }
274
    {
275
	StateMap s,e;
276
	// set children
277
	s.insert( "a_item2", Q3CheckListItem::On );
278
	s.insert( "a_item3", Q3CheckListItem::On );
279
	s.insert( "a_item4", Q3CheckListItem::On );
280
	s.insert( "a_item6", Q3CheckListItem::On );
281
	s.insert( "a_item7", Q3CheckListItem::On );
282
	s.insert( "a_item8", Q3CheckListItem::On );
283
	// set item1 to On (storing previous state)
284
	s.insert( "b_item1", Q3CheckListItem::On );
285
	// bring back old state
286
	s.insert( "c_item1", Q3CheckListItem::NoChange );
287
	// set item9 (and it's children) to On, which also saves new history for the whole tree to On
288
	s.insert( "d_item9", Q3CheckListItem::On );
289
	// bring back old state once again, all should be On now
290
	s.insert( "e_item1", Q3CheckListItem::NoChange );
291
292
	e.insert( "item1", Q3CheckListItem::On );
293
	e.insert( "item2", Q3CheckListItem::On );
294
	e.insert( "item3", Q3CheckListItem::On );
295
	e.insert( "item4", Q3CheckListItem::On );
296
	e.insert( "item5", Q3CheckListItem::On );
297
	e.insert( "item6", Q3CheckListItem::On );
298
	e.insert( "item7", Q3CheckListItem::On );
299
	e.insert( "item8", Q3CheckListItem::On );
300
	e.insert( "item9", Q3CheckListItem::On );
301
	e.insert( "item10", Q3CheckListItem::On );
302
	e.insert( "item11", Q3CheckListItem::On );
303
	e.insert( "item12", Q3CheckListItem::On );
304
	QTest::newRow( "tristate_06" ) << s << e;
305
    }
306
}
307
308
void tst_Q3CheckListItem::setState()
309
{
310
    // makes the listview
311
    Q3ListView view( 0 );
312
    view.addColumn( "Testing" );
313
    view.setRootIsDecorated( TRUE );
314
    view.setSorting( -1 );
315
316
    Q3CheckListItem item1( &view ,"item1", Q3CheckListItem::CheckBoxController );
317
318
    Q3CheckListItem item2( &item1 ,"item2", Q3CheckListItem::CheckBox );
319
    Q3CheckListItem item3( &item1 ,"item3", Q3CheckListItem::CheckBox );
320
    Q3CheckListItem item4( &item1 ,"item4", Q3CheckListItem::CheckBox );
321
322
    Q3CheckListItem item5( &item1 ,"item5", Q3CheckListItem::CheckBoxController );
323
324
    Q3CheckListItem item6( &item5 ,"item6", Q3CheckListItem::CheckBox );
325
    Q3CheckListItem item7( &item5 ,"item7", Q3CheckListItem::CheckBox );
326
    Q3CheckListItem item8( &item5 ,"item8", Q3CheckListItem::CheckBox );
327
328
    Q3CheckListItem item9( &item1 ,"item9", Q3CheckListItem::CheckBoxController );
329
330
    Q3CheckListItem item10( &item9 ,"item10", Q3CheckListItem::CheckBox );
331
    Q3CheckListItem item11( &item9 ,"item11", Q3CheckListItem::CheckBox );
332
    Q3CheckListItem item12( &item9 ,"item12", Q3CheckListItem::CheckBox );
333
334
    view.show();
335
336
//    while ( view.isVisible() )
337
//	qApp->processEvents();
338
339
340
    // fetches the states
341
    QFETCH( StateMap, states );
342
    QFETCH( StateMap, expectedStates );
343
344
    // sets the states
345
    StateMap::Iterator stateIt;
346
    for ( stateIt = states.begin(); stateIt != states.end(); ++stateIt ) {
347
	QString key =  stateIt.key().section( '_', -1 );
348
349
	Q3ListViewItemIterator it( &view );
350
	while ( it.current() ) {
351
	    if ( key == it.current()->text( 0 ) && it.current()->rtti() == 1 ) {
352
		((Q3CheckListItem*)it.current())->setState( stateIt.data() );
353
		//qDebug( "setting %s to %d", key.latin1(), (int)stateIt.data() );
354
	    }
355
	    ++it;
356
	}
357
    }
358
359
    // check if we get what we expected
360
    Q3ListViewItemIterator it( &view );
361
    while ( it.current() ) {
362
	QString key = it.current()->text( 0 );
363
	if ( expectedStates.contains( key ) && it.current()->rtti() == 1 ) {
364
	    QCOMPARE( ((Q3CheckListItem*)it.current())->state(), expectedStates[ key ] );
365
	}
366
	++it;
367
    }
368
}
369
370
QTEST_MAIN(tst_Q3CheckListItem)
371
#include "tst_q3checklistitem.moc"