summaryrefslogtreecommitdiffstats
path: root/tests/auto/q3buttongroup/tst_q3buttongroup.cpp
blob: e7d757f970cb49e78979487e4cbd08687bde1467 (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
/****************************************************************************
**
** Copyright (C) 2009 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$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QApplication>
#include <Q3ButtonGroup>
#include <QCheckBox>
#include <QRadioButton>
#include <QPushButton>
#include <QDebug>
#include <QLayout>
#include <QtTest/QtTest>

class tst_q3buttongroup : public QObject
{
Q_OBJECT
private slots:
    void exclusiveButtons();
    void nonExclusiveButtons();
    void buttonIds();
    void buttonId();
    void clickLock();
    void task198864_insert();
    void task200764_insert_data();
    void task200764_insert();
    void task238902_directChildrenOnly();
};

/*
    Test that check boxes created with a Q3ButtonGroup parent in exclusive
    mode really are exclusive.
*/
void tst_q3buttongroup::exclusiveButtons()
{
    Q3ButtonGroup group(1, Qt::Horizontal);
    group.setExclusive(true);

    QCheckBox *b1 = new QCheckBox("Hi", &group);
    QCheckBox *b2 = new QCheckBox("there", &group);
    QCheckBox *b3 = new QCheckBox("foo", &group);

    group.show();

    // Check b1 and verify that it stuck.
    b1->setCheckState(Qt::Checked);
    QCOMPARE(b1->checkState(), Qt::Checked);

    // Check b2 and verify that b1 is now unchecked.
    b2->setCheckState(Qt::Checked);
    QCOMPARE(b1->checkState(), Qt::Unchecked);

    // Check b3 and verify that b2 and b1 are now unchecked.
    b3->setCheckState(Qt::Checked);
    QCOMPARE(b1->checkState(), Qt::Unchecked);
    QCOMPARE(b2->checkState(), Qt::Unchecked);
}

/*
    Test that setting exclusive to false works.
*/
void tst_q3buttongroup::nonExclusiveButtons()
{
    Q3ButtonGroup group(1, Qt::Horizontal);

    QWidget parent;

    QCheckBox *b1 = new QCheckBox("Hi", &parent);
    group.insert(b1);
    QCheckBox *b2 = new QCheckBox("there", &parent);
    group.insert(b2);
    QCheckBox *b3 = new QCheckBox("foo", &parent);
    group.insert(b3);

    group.setExclusive(false);
    group.show();

    // Check b1 and verify that it stuck.
    b1->setCheckState(Qt::Checked);
    QCOMPARE(b1->checkState(), Qt::Checked);

    // Check b2 and verify that b1 is still checked.
    b2->setCheckState(Qt::Checked);
    QCOMPARE(b1->checkState(), Qt::Checked);

    // Check b3 and verify that b2 and b1 are still checked.
    b3->setCheckState(Qt::Checked);
    QCOMPARE(b1->checkState(), Qt::Checked);
    QCOMPARE(b2->checkState(), Qt::Checked);
}

/*
    Test that Ids get assigned
*/
void tst_q3buttongroup::buttonIds()
{
    Q3ButtonGroup group(0, Qt::Vertical, "ButtonGroup");
    group.setExclusive(true);
    QVERIFY(group.isExclusive());

    for (int i=0; i < 10; i++) {
        QRadioButton *button = new QRadioButton(QString("Button_%1").arg(i + 1) , &group);
        QCOMPARE(group.id(button) , i);
        int id = group.insert(button);
        QCOMPARE(id, i);
        group.setButton(id);
        QCOMPARE(group.selectedId(), id);
    }

    QCheckBox *button2 = new QCheckBox(QString("manuallyAdded"));
    int id = group.insert( button2 );
    QCOMPARE(id , 10 );

    button2->setChecked(true);
    QCOMPARE( group.selectedId() , id );

    group.remove(group.find(5));
    QCOMPARE(group.count() , 10);

    delete button2;
}

void tst_q3buttongroup::buttonId()
{
    Q3ButtonGroup bg;
    QPushButton *button = new QPushButton("Foo", &bg);
    int id = bg.insert(button, 1);
    QApplication::instance()->processEvents();
    QCOMPARE(id, bg.id(button));
}

void tst_q3buttongroup::clickLock()
{
    // Task 177677
    QProcess process;
    process.start(QLatin1String("clickLock/clickLock"));
    if (!process.waitForStarted(10000)) {
        QFAIL("Could not launch process.");
    }

    if (!process.waitForFinished(15000)) {
        process.terminate();
        QFAIL("Could not handle click events properly");
    }
}

void tst_q3buttongroup::task198864_insert()
{
#if 0
    Q3ButtonGroup *group = new Q3ButtonGroup;
    QWidget *parent = new QWidget;

    const int id1 = group->insert(new QPushButton(parent));
    QCOMPARE(id1, 0);
    const int id2 = group->insert(new QPushButton(parent));
    QCOMPARE(id2, 1);

    delete parent;
    parent = new QWidget;

    const int id3 = group->insert(new QPushButton(parent));
    QCOMPARE(id3, 0);
#endif

    Q3ButtonGroup *group = new Q3ButtonGroup;

    QPushButton *button1 = new QPushButton;
    const int id1 = group->insert(button1);
    QCOMPARE(id1, 0);

    QPushButton *button2 = new QPushButton;
    const int id2 = group->insert(button2);
    QCOMPARE(id2, 1);

    delete button1;
    delete button2;

    QPushButton *button3 = new QPushButton;
    const int id3 = group->insert(button3);
    QCOMPARE(id3, 0);
}

typedef QList<int> IntList;
Q_DECLARE_METATYPE(IntList);

void tst_q3buttongroup::task200764_insert_data()
{
    QTest::addColumn<IntList >("ids");
    QTest::newRow("1") << (IntList() << 0 << 1 << 2 << 3);
    QTest::newRow("2") << (IntList() << 0 << 3 << 2 << 1);
    QTest::newRow("3") << (IntList() << 3 << 2 << 1 << 0);
    QTest::newRow("4") << (IntList() << 3 << 1 << 0 << 2);
}

class task200764_Widget : public QWidget
{
    Q_OBJECT
public:
    task200764_Widget(const IntList &ids)
    {
        Q3ButtonGroup *buttonGroup = new Q3ButtonGroup;
        buttonGroup->setExclusive(true);
        QVBoxLayout *layout = new QVBoxLayout(this);

        for (int i = 0; i < ids.size(); ++i) {
            QPushButton *button = new QPushButton(buttonGroup);
            const int id = buttonGroup->insert(button, ids.at(i));
            //button->setText(QString("%1 (id:%2->%3)").arg(i).arg(ids.at(i)).arg(id));
            layout->addWidget(button);
            buttons << button;
            actualIds << id;
        }

        QObject::connect(buttonGroup, SIGNAL(clicked(int)), SLOT(clicked(int)));
    }

    ~task200764_Widget()
    {
        foreach (QPushButton *button, buttons)
            delete button;
    }

    QList<QPushButton *> buttons;
    QList<int> actualIds;
    QList<int> clickedIds;

private slots:
    void clicked(int id) { clickedIds << id; }
};

void tst_q3buttongroup::task200764_insert()
{
    QFETCH(IntList, ids);

    task200764_Widget widget(ids);
    widget.show();

    for (int i = 0; i < ids.size(); ++i) {
        widget.clickedIds.clear();
        QTest::mouseClick(widget.buttons.at(i), Qt::LeftButton);
        QCOMPARE(widget.clickedIds.size(), 1);
        QCOMPARE(widget.clickedIds.first(), widget.actualIds.at(i));
    }
}

void tst_q3buttongroup::task238902_directChildrenOnly()
{
    Q3ButtonGroup *group = new Q3ButtonGroup;

    QFrame *frame = new QFrame(group);
    QPushButton *button = new QPushButton(frame);
    QSignalSpy spy(button, SIGNAL(clicked()));
    QSignalSpy spy2(group, SIGNAL(clicked(int)));
    group->show();
    QTest::qWait(500);
    QTest::mouseClick(button, Qt::LeftButton);
    QTest::qWait(500);
    QCOMPARE(spy.count(),1);
    QCOMPARE(spy2.count(),0);
    group->hide();

    //normal case, should work
    Q3ButtonGroup *group2 = new Q3ButtonGroup;

    QPushButton *button2 = new QPushButton(group2);
    QSignalSpy spy3(button2, SIGNAL(clicked()));
    QSignalSpy spy4(group2, SIGNAL(clicked(int)));
    group2->show();
    QTest::qWait(500);
    QTest::mouseClick(button2, Qt::LeftButton);
    QTest::qWait(500);
    QCOMPARE(spy3.count(),1);
    QCOMPARE(spy4.count(),1);
}

QTEST_MAIN(tst_q3buttongroup)
#include "tst_q3buttongroup.moc"