summaryrefslogtreecommitdiffstats
path: root/tests/auto/q3checklistitem/tst_q3checklistitem.cpp
blob: f46424dbf1178c6c7772150f36474629ee9fed71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>
#include <qmap.h>
#include <qapplication.h>
#include <q3listview.h>

//TESTED_CLASS=
//TESTED_FILES=

class tst_Q3CheckListItem : public QObject
{
    Q_OBJECT

public:
    tst_Q3CheckListItem();
    virtual ~tst_Q3CheckListItem();

private slots:
    void setState_data();
    void setState();
};

tst_Q3CheckListItem::tst_Q3CheckListItem()
{

}

tst_Q3CheckListItem::~tst_Q3CheckListItem()
{

}

typedef QMap<QString, Q3CheckListItem::ToggleState> StateMap;

QDataStream &operator>>( QDataStream &s, Q3CheckListItem::ToggleState &state )
{
    int tmp;
    s >> tmp;
    state = (Q3CheckListItem::ToggleState)tmp;
    return s;
}
Q_DECLARE_METATYPE(StateMap)


void tst_Q3CheckListItem::setState_data()
{
    QTest::addColumn<StateMap>("states");
    QTest::addColumn<StateMap>("expectedStates");
    {
	StateMap s,e;
	s.insert( "item6", Q3CheckListItem::On );

	e.insert( "item5", Q3CheckListItem::NoChange );
	e.insert( "item1", Q3CheckListItem::NoChange );
	QTest::newRow( "children_affect_parent01" ) << s << e;
    }
    {
	StateMap s,e;
	s.insert( "item6", Q3CheckListItem::On );
	s.insert( "item7", Q3CheckListItem::On );
	s.insert( "item8", Q3CheckListItem::On );

	e.insert( "item5", Q3CheckListItem::On );
	e.insert( "item1", Q3CheckListItem::NoChange );
	QTest::newRow( "children_affect_parent02" ) << s << e;
    }
    {
	StateMap s,e;
	s.insert( "a_item5", Q3CheckListItem::On );
	s.insert( "b_item6", Q3CheckListItem::Off );
	s.insert( "b_item7", Q3CheckListItem::Off );
	s.insert( "b_item8", Q3CheckListItem::Off );

	e.insert( "item5", Q3CheckListItem::Off );
	QTest::newRow( "children_affect_parent03" ) << s << e;
    }
    {
	StateMap s,e;
	s.insert( "item2", Q3CheckListItem::On );
	s.insert( "item3", Q3CheckListItem::On );
	s.insert( "item4", Q3CheckListItem::On );
	s.insert( "item6", Q3CheckListItem::On );
	s.insert( "item7", Q3CheckListItem::On );
	s.insert( "item8", Q3CheckListItem::On );
	s.insert( "item10", Q3CheckListItem::On );
	s.insert( "item11", Q3CheckListItem::On );
	s.insert( "item12", Q3CheckListItem::On );

	e.insert( "item5", Q3CheckListItem::On );
	e.insert( "item9", Q3CheckListItem::On );
	e.insert( "item1", Q3CheckListItem::On );
	QTest::newRow( "children_affect_parent04" ) << s << e;
    }
    {
	StateMap s,e;
	s.insert( "a_item6", Q3CheckListItem::On );
	s.insert( "b_item6", Q3CheckListItem::Off );

	e.insert( "item6", Q3CheckListItem::Off );
	QTest::newRow( "setting_resetting01" ) << s << e;
    }
    {
	StateMap s,e;
	s.insert( "item1", Q3CheckListItem::On );

	e.insert( "item2", Q3CheckListItem::On );
	e.insert( "item3", Q3CheckListItem::On );
	e.insert( "item4", Q3CheckListItem::On );
	e.insert( "item5", Q3CheckListItem::On );
	e.insert( "item6", Q3CheckListItem::On );
	e.insert( "item7", Q3CheckListItem::On );
	e.insert( "item8", Q3CheckListItem::On );
	e.insert( "item9", Q3CheckListItem::On );
	e.insert( "item10", Q3CheckListItem::On );
	e.insert( "item11", Q3CheckListItem::On );
	e.insert( "item12", Q3CheckListItem::On );
	QTest::newRow( "parrent_affecting_children01" ) << s << e;
    }
    {
	StateMap s,e;
	s.insert( "a_item11", Q3CheckListItem::On );
	s.insert( "a_item4", Q3CheckListItem::Off );
	s.insert( "a_item7", Q3CheckListItem::On );
	s.insert( "b_item1", Q3CheckListItem::On );

	e.insert( "item2", Q3CheckListItem::On );
	e.insert( "item3", Q3CheckListItem::On );
	e.insert( "item4", Q3CheckListItem::On );
	e.insert( "item5", Q3CheckListItem::On );
	e.insert( "item6", Q3CheckListItem::On );
	e.insert( "item7", Q3CheckListItem::On );
	e.insert( "item8", Q3CheckListItem::On );
	e.insert( "item9", Q3CheckListItem::On );
	e.insert( "item10", Q3CheckListItem::On );
	e.insert( "item11", Q3CheckListItem::On );
	e.insert( "item12", Q3CheckListItem::On );
	QTest::newRow( "parrent_affecting_children02" ) << s << e;
    }
    {
	StateMap s,e;
	s.insert( "a_item7", Q3CheckListItem::On );
	s.insert( "b_item5", Q3CheckListItem::Off );

	e.insert( "item6", Q3CheckListItem::Off );
	e.insert( "item7", Q3CheckListItem::Off );
	e.insert( "item8", Q3CheckListItem::Off );
	QTest::newRow( "parrent_affecting_children03" ) << s << e;
    }
    {
	StateMap s,e;
	s.insert( "a_item7", Q3CheckListItem::On );
	s.insert( "b_item5", Q3CheckListItem::Off );

	e.insert( "item6", Q3CheckListItem::Off );
	e.insert( "item7", Q3CheckListItem::Off );
	e.insert( "item8", Q3CheckListItem::Off );
	QTest::newRow( "parrent_affecting_children04" ) << s << e;
    }
    {
	StateMap s,e;
	s.insert( "item11", Q3CheckListItem::On );

	e.insert( "item9", Q3CheckListItem::NoChange );
	QTest::newRow( "tristate_01" ) << s << e;
    }
    {
	StateMap s,e;
	s.insert( "item11", Q3CheckListItem::NoChange );

	e.insert( "item11", Q3CheckListItem::Off );
	QTest::newRow( "tristate_02" ) << s << e;
    }
    {
	StateMap s,e;
	// set children
	s.insert( "a_item10", Q3CheckListItem::Off );
	s.insert( "a_item11", Q3CheckListItem::On );
	s.insert( "a_item12", Q3CheckListItem::On );
	// set parent to On (but also saving old state)
	s.insert( "b_item9", Q3CheckListItem::On );
	// recalling old state
	s.insert( "c_item9", Q3CheckListItem::NoChange );

	e.insert( "item9", Q3CheckListItem::NoChange );
	e.insert( "item10", Q3CheckListItem::Off );
	e.insert( "item11", Q3CheckListItem::On );
	e.insert( "item12", Q3CheckListItem::On );
	QTest::newRow( "tristate_03" ) << s << e;
    }
    {
	StateMap s,e;
	// set children
	s.insert( "a_item10", Q3CheckListItem::Off );
	s.insert( "a_item11", Q3CheckListItem::On );
	s.insert( "a_item12", Q3CheckListItem::On );
	// set parent to On (but also saving old state)
	s.insert( "b_item9", Q3CheckListItem::On );
	// recalling old state
	s.insert( "c_item9", Q3CheckListItem::NoChange );
	// setting the last child to on, which should also set the history for item9 to all on
	s.insert( "d_item10", Q3CheckListItem::On );
	// recalling old state which should all be On for all children, in effect setting item9 to On as well
	s.insert( "e_item9", Q3CheckListItem::NoChange );

       	e.insert( "item9", Q3CheckListItem::On );
	e.insert( "item10", Q3CheckListItem::On );
	e.insert( "item11", Q3CheckListItem::On );
	e.insert( "item12", Q3CheckListItem::On );
	QTest::newRow( "tristate_04" ) << s << e;
    }
    {
	StateMap s,e;
	// set children
	s.insert( "a_item10", Q3CheckListItem::Off );
	s.insert( "a_item11", Q3CheckListItem::On );
	s.insert( "a_item12", Q3CheckListItem::On );
	// set parent to On (but also saving old state)
	s.insert( "b_item9", Q3CheckListItem::On );
	// recalling old state
	s.insert( "c_item9", Q3CheckListItem::NoChange );
	// setting the last two children to Off, which should also set the history for item9 to all Off
	s.insert( "d_item11", Q3CheckListItem::Off );
	s.insert( "d_item12", Q3CheckListItem::Off );
	// recalling old state which should all be Off for all children, in effect setting item9 to Off
	s.insert( "e_item9", Q3CheckListItem::NoChange );

	e.insert( "item9", Q3CheckListItem::Off );
	e.insert( "item10", Q3CheckListItem::Off );
	e.insert( "item11", Q3CheckListItem::Off );
	e.insert( "item12", Q3CheckListItem::Off );
	QTest::newRow( "tristate_05" ) << s << e;
    }
    {
	StateMap s,e;
	// set children
	s.insert( "a_item2", Q3CheckListItem::On );
	s.insert( "a_item3", Q3CheckListItem::On );
	s.insert( "a_item4", Q3CheckListItem::On );
	s.insert( "a_item6", Q3CheckListItem::On );
	s.insert( "a_item7", Q3CheckListItem::On );
	s.insert( "a_item8", Q3CheckListItem::On );
	// set item1 to On (storing previous state)
	s.insert( "b_item1", Q3CheckListItem::On );
	// bring back old state
	s.insert( "c_item1", Q3CheckListItem::NoChange );
	// set item9 (and it's children) to On, which also saves new history for the whole tree to On
	s.insert( "d_item9", Q3CheckListItem::On );
	// bring back old state once again, all should be On now
	s.insert( "e_item1", Q3CheckListItem::NoChange );

	e.insert( "item1", Q3CheckListItem::On );
	e.insert( "item2", Q3CheckListItem::On );
	e.insert( "item3", Q3CheckListItem::On );
	e.insert( "item4", Q3CheckListItem::On );
	e.insert( "item5", Q3CheckListItem::On );
	e.insert( "item6", Q3CheckListItem::On );
	e.insert( "item7", Q3CheckListItem::On );
	e.insert( "item8", Q3CheckListItem::On );
	e.insert( "item9", Q3CheckListItem::On );
	e.insert( "item10", Q3CheckListItem::On );
	e.insert( "item11", Q3CheckListItem::On );
	e.insert( "item12", Q3CheckListItem::On );
	QTest::newRow( "tristate_06" ) << s << e;
    }
}

void tst_Q3CheckListItem::setState()
{
    // makes the listview
    Q3ListView view( 0 );
    view.addColumn( "Testing" );
    view.setRootIsDecorated( TRUE );
    view.setSorting( -1 );

    Q3CheckListItem item1( &view ,"item1", Q3CheckListItem::CheckBoxController );

    Q3CheckListItem item2( &item1 ,"item2", Q3CheckListItem::CheckBox );
    Q3CheckListItem item3( &item1 ,"item3", Q3CheckListItem::CheckBox );
    Q3CheckListItem item4( &item1 ,"item4", Q3CheckListItem::CheckBox );

    Q3CheckListItem item5( &item1 ,"item5", Q3CheckListItem::CheckBoxController );

    Q3CheckListItem item6( &item5 ,"item6", Q3CheckListItem::CheckBox );
    Q3CheckListItem item7( &item5 ,"item7", Q3CheckListItem::CheckBox );
    Q3CheckListItem item8( &item5 ,"item8", Q3CheckListItem::CheckBox );

    Q3CheckListItem item9( &item1 ,"item9", Q3CheckListItem::CheckBoxController );

    Q3CheckListItem item10( &item9 ,"item10", Q3CheckListItem::CheckBox );
    Q3CheckListItem item11( &item9 ,"item11", Q3CheckListItem::CheckBox );
    Q3CheckListItem item12( &item9 ,"item12", Q3CheckListItem::CheckBox );

    view.show();

//    while ( view.isVisible() )
//	qApp->processEvents();


    // fetches the states
    QFETCH( StateMap, states );
    QFETCH( StateMap, expectedStates );

    // sets the states
    StateMap::Iterator stateIt;
    for ( stateIt = states.begin(); stateIt != states.end(); ++stateIt ) {
	QString key =  stateIt.key().section( '_', -1 );

	Q3ListViewItemIterator it( &view );
	while ( it.current() ) {
	    if ( key == it.current()->text( 0 ) && it.current()->rtti() == 1 ) {
		((Q3CheckListItem*)it.current())->setState( stateIt.data() );
		//qDebug( "setting %s to %d", key.latin1(), (int)stateIt.data() );
	    }
	    ++it;
	}
    }

    // check if we get what we expected
    Q3ListViewItemIterator it( &view );
    while ( it.current() ) {
	QString key = it.current()->text( 0 );
	if ( expectedStates.contains( key ) && it.current()->rtti() == 1 ) {
	    QCOMPARE( ((Q3CheckListItem*)it.current())->state(), expectedStates[ key ] );
	}
	++it;
    }
}

QTEST_MAIN(tst_Q3CheckListItem)
#include "tst_q3checklistitem.moc"