1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
43
#include <QtTest/QtTest>
46
#include <QStyleOptionGroupBox>
47
#include <QVBoxLayout>
48
#include <QRadioButton>
50
#include "qgroupbox.h"
52
class tst_QGroupBox : public QObject
58
virtual ~tst_QGroupBox();
61
void toggledHelperSlot(bool on);
63
void clickTimestampSlot();
64
void toggleTimestampSlot();
69
void setCheckable_data();
71
void setChecked_data();
73
void enabledPropagation();
78
void toggledVsClicked();
79
void childrenAreDisabled();
80
void propagateFocus();
81
void task_QTBUG_19170_ignoreMouseReleseEvent();
86
qint64 clickTimeStamp;
87
qint64 toggleTimeStamp;
91
tst_QGroupBox::tst_QGroupBox()
96
tst_QGroupBox::~tst_QGroupBox()
101
void tst_QGroupBox::init()
106
void tst_QGroupBox::setTitle_data()
108
QTest::addColumn<QString>("title");
109
QTest::addColumn<QString>("expectedTitle");
110
QTest::newRow( "empty_title" ) << QString("") << QString("");
111
QTest::newRow( "normal_title" ) << QString("Whatisthematrix") << QString("Whatisthematrix");
112
QTest::newRow( "special_chars_title" ) << QString("<>%&#/()=") << QString("<>%&#/()=");
113
QTest::newRow( "spaces_title" ) << QString(" Hello ") << QString(" Hello ");
116
void tst_QGroupBox::setCheckable_data()
118
QTest::addColumn<bool>("checkable");
119
QTest::addColumn<bool>("expectedCheckable");
120
QTest::newRow( "checkable_true" ) << true << true;
121
QTest::newRow( "checkable_false" ) << false << false;
124
void tst_QGroupBox::setChecked_data()
126
QTest::addColumn<bool>("checkable");
127
QTest::addColumn<bool>("checked");
128
QTest::addColumn<bool>("expectedChecked");
129
QTest::newRow( "checkable_false_checked_true" ) << false << true << false;
130
QTest::newRow( "checkable_true_checked_true" ) << true << true << true;
131
QTest::newRow( "checkable_true_checked_false" ) << true << false << false;
134
void tst_QGroupBox::setTitle()
136
QFETCH( QString, title );
137
QFETCH( QString, expectedTitle );
141
groupBox.setTitle( title );
143
QCOMPARE( groupBox.title() , expectedTitle );
146
void tst_QGroupBox::setCheckable()
148
QFETCH( bool, checkable );
149
QFETCH( bool, expectedCheckable );
153
groupBox.setCheckable( checkable );
154
QCOMPARE( groupBox.isCheckable() , expectedCheckable );
158
void tst_QGroupBox::setChecked()
160
QFETCH( bool, checkable );
161
QFETCH( bool, checked );
162
QFETCH( bool, expectedChecked );
166
groupBox.setCheckable( checkable );
167
groupBox.setChecked( checked );
168
QCOMPARE( groupBox.isChecked(), expectedChecked );
171
void tst_QGroupBox::enabledPropagation()
173
QGroupBox *testWidget = new QGroupBox(0);
174
testWidget->setCheckable(true);
175
testWidget->setChecked(true);
176
QWidget* childWidget = new QWidget( testWidget );
178
QVERIFY( testWidget->isEnabled() );
179
QVERIFY( childWidget->isEnabled() );
181
testWidget->setEnabled( false );
182
QVERIFY( !testWidget->isEnabled() );
183
QVERIFY( !childWidget->isEnabled() );
185
testWidget->setDisabled( false );
186
QVERIFY( testWidget->isEnabled() );
187
QVERIFY( childWidget->isEnabled() );
189
QWidget* grandChildWidget = new QWidget( childWidget );
190
QVERIFY( grandChildWidget->isEnabled() );
192
testWidget->setDisabled( true );
193
QVERIFY( !testWidget->isEnabled() );
194
QVERIFY( !childWidget->isEnabled() );
195
QVERIFY( !grandChildWidget->isEnabled() );
197
grandChildWidget->setEnabled( false );
198
testWidget->setEnabled( true );
199
QVERIFY( testWidget->isEnabled() );
200
QVERIFY( childWidget->isEnabled() );
201
QVERIFY( !grandChildWidget->isEnabled() );
203
grandChildWidget->setEnabled( true );
204
testWidget->setEnabled( false );
205
childWidget->setDisabled( true );
206
testWidget->setEnabled( true );
207
QVERIFY( testWidget->isEnabled() );
208
QVERIFY( !childWidget->isEnabled() );
209
QVERIFY( !grandChildWidget->isEnabled() );
212
testWidget->setEnabled( true );
213
childWidget->setEnabled( true );
214
grandChildWidget->setEnabled( true );
216
// Now check when it's disabled
217
testWidget->setChecked(false);
218
QVERIFY( testWidget->isEnabled() );
219
QVERIFY( !childWidget->isEnabled() );
221
testWidget->setEnabled( false );
222
QVERIFY( !testWidget->isEnabled() );
223
QVERIFY( !childWidget->isEnabled() );
225
testWidget->setDisabled( false );
226
QVERIFY( testWidget->isEnabled() );
227
QVERIFY( !childWidget->isEnabled() );
229
QVERIFY( !grandChildWidget->isEnabled() );
231
testWidget->setDisabled( true );
232
QVERIFY( !testWidget->isEnabled() );
233
QVERIFY( !childWidget->isEnabled() );
234
QVERIFY( !grandChildWidget->isEnabled() );
236
grandChildWidget->setEnabled( false );
237
testWidget->setEnabled( true );
238
QVERIFY( testWidget->isEnabled() );
239
QVERIFY( !childWidget->isEnabled() );
240
QVERIFY( !grandChildWidget->isEnabled() );
242
grandChildWidget->setEnabled( true );
243
testWidget->setEnabled( false );
244
childWidget->setDisabled( true );
245
testWidget->setEnabled( true );
246
QVERIFY( testWidget->isEnabled() );
247
QVERIFY( !childWidget->isEnabled() );
248
QVERIFY( !grandChildWidget->isEnabled() );
251
testWidget->setEnabled( true );
252
childWidget->setEnabled( true );
253
grandChildWidget->setEnabled( true );
255
// Finally enable it again
256
testWidget->setChecked(true);
257
QVERIFY( testWidget->isEnabled() );
258
QVERIFY( childWidget->isEnabled() );
260
testWidget->setEnabled( false );
261
QVERIFY( !testWidget->isEnabled() );
262
QVERIFY( !childWidget->isEnabled() );
264
testWidget->setDisabled( false );
265
QVERIFY( testWidget->isEnabled() );
266
QVERIFY( childWidget->isEnabled() );
267
QVERIFY( grandChildWidget->isEnabled() );
269
testWidget->setDisabled( true );
270
QVERIFY( !testWidget->isEnabled() );
271
QVERIFY( !childWidget->isEnabled() );
272
QVERIFY( !grandChildWidget->isEnabled() );
274
grandChildWidget->setEnabled( false );
275
testWidget->setEnabled( true );
276
QVERIFY( testWidget->isEnabled() );
277
QVERIFY( childWidget->isEnabled() );
278
QVERIFY( !grandChildWidget->isEnabled() );
280
grandChildWidget->setEnabled( true );
281
testWidget->setEnabled( false );
282
childWidget->setDisabled( true );
283
testWidget->setEnabled( true );
284
QVERIFY( testWidget->isEnabled() );
285
QVERIFY( !childWidget->isEnabled() );
286
QVERIFY( !grandChildWidget->isEnabled() );
292
void tst_QGroupBox::sizeHint()
294
QGroupBox testWidget1(0);
295
testWidget1.setTitle("&0&0&0&0&0&0&0&0&0&0");
297
QGroupBox testWidget2(0);
298
testWidget2.setTitle("0000000000");
300
QCOMPARE(testWidget1.sizeHint().width(), testWidget2.sizeHint().width());
302
// if the above fails one should maybe test to see like underneath.
303
// QVERIFY((QABS(testWidget1->sizeHint().width() - testWidget2->sizeHint().width()) < 10));
306
void tst_QGroupBox::toggledHelperSlot(bool on)
312
void tst_QGroupBox::toggled()
314
QGroupBox testWidget1(0);
315
testWidget1.setCheckable(true);
316
connect(&testWidget1, SIGNAL(toggled(bool)), this, SLOT(toggledHelperSlot(bool)));
317
QLineEdit *edit = new QLineEdit(&testWidget1);
319
testWidget1.setChecked(true);
321
QVERIFY(edit->isEnabled());
322
testWidget1.setChecked(false);
324
QVERIFY(!edit->isEnabled());
327
void tst_QGroupBox::clicked_data()
329
QTest::addColumn<bool>("checkable");
330
QTest::addColumn<bool>("initialCheck");
331
QTest::addColumn<int>("areaToHit");
332
QTest::addColumn<int>("clickedCount");
333
QTest::addColumn<bool>("finalCheck");
335
QTest::newRow("hit nothing, not checkable") << false << false << int(QStyle::SC_None) << 0 << false;
336
QTest::newRow("hit frame, not checkable") << false << false << int(QStyle::SC_GroupBoxFrame) << 0 << false;
337
QTest::newRow("hit content, not checkable") << false << false << int(QStyle::SC_GroupBoxContents) << 0 << false;
338
QTest::newRow("hit label, not checkable") << false << false << int(QStyle::SC_GroupBoxLabel) << 0 << false;
339
QTest::newRow("hit checkbox, not checkable") << false << false << int(QStyle::SC_GroupBoxCheckBox) << 0 << false;
341
QTest::newRow("hit nothing, checkable") << true << true << int(QStyle::SC_None) << 0 << true;
342
QTest::newRow("hit frame, checkable") << true << true << int(QStyle::SC_GroupBoxFrame) << 0 << true;
343
QTest::newRow("hit content, checkable") << true << true << int(QStyle::SC_GroupBoxContents) << 0 << true;
344
QTest::newRow("hit label, checkable") << true << true << int(QStyle::SC_GroupBoxLabel) << 1 << false;
345
QTest::newRow("hit checkbox, checkable") << true << true << int(QStyle::SC_GroupBoxCheckBox) << 1 << false;
347
QTest::newRow("hit nothing, checkable, but unchecked") << true << false << int(QStyle::SC_None) << 0 << false;
348
QTest::newRow("hit frame, checkable, but unchecked") << true << false << int(QStyle::SC_GroupBoxFrame) << 0 << false;
349
QTest::newRow("hit content, checkable, but unchecked") << true << false << int(QStyle::SC_GroupBoxContents) << 0 << false;
350
QTest::newRow("hit label, checkable, but unchecked") << true << false << int(QStyle::SC_GroupBoxLabel) << 1 << true;
351
QTest::newRow("hit checkbox, checkable, but unchecked") << true << false << int(QStyle::SC_GroupBoxCheckBox) << 1 << true;
354
void tst_QGroupBox::clicked()
356
QFETCH(bool, checkable);
357
QFETCH(bool, initialCheck);
358
QFETCH(int, areaToHit);
359
QGroupBox testWidget(QLatin1String("Testing Clicked"));
360
testWidget.setCheckable(checkable);
361
testWidget.setChecked(initialCheck);
362
QCOMPARE(testWidget.isChecked(), initialCheck);
363
testWidget.resize(200, 200);
364
QSignalSpy spy(&testWidget, SIGNAL(clicked(bool)));
366
QStyleOptionGroupBox option;
367
option.initFrom(&testWidget);
368
option.subControls = checkable ? QStyle::SubControls(QStyle::SC_All) : QStyle::SubControls(QStyle::SC_All & ~QStyle::SC_GroupBoxCheckBox);
369
option.text = testWidget.title();
370
option.textAlignment = testWidget.alignment();
372
QRect rect = testWidget.style()->subControlRect(QStyle::CC_GroupBox, &option,
373
QStyle::SubControl(areaToHit), &testWidget);
376
QTest::mouseClick(&testWidget, Qt::LeftButton, 0, rect.center());
378
QTest::mouseClick(&testWidget, Qt::LeftButton);
380
QTEST(spy.count(), "clickedCount");
382
QTEST(spy.at(0).at(0).toBool(), "finalCheck");
383
QTEST(testWidget.isChecked(), "finalCheck");
386
void tst_QGroupBox::toggledVsClicked()
388
timeStamp = clickTimeStamp = toggleTimeStamp = 0;
390
groupBox.setCheckable(true);
391
QSignalSpy toggleSpy(&groupBox, SIGNAL(toggled(bool)));
392
QSignalSpy clickSpy(&groupBox, SIGNAL(clicked(bool)));
394
groupBox.setChecked(!groupBox.isChecked());
395
QCOMPARE(clickSpy.count(), 0);
396
QCOMPARE(toggleSpy.count(), 1);
397
if (toggleSpy.count() > 0)
398
QCOMPARE(toggleSpy.at(0).at(0).toBool(), groupBox.isChecked());
400
connect(&groupBox, SIGNAL(clicked(bool)), this, SLOT(clickTimestampSlot()));
401
connect(&groupBox, SIGNAL(toggled(bool)), this, SLOT(toggleTimestampSlot()));
403
QStyleOptionGroupBox option;
404
option.initFrom(&groupBox);
405
option.subControls = QStyle::SubControls(QStyle::SC_All);
406
QRect rect = groupBox.style()->subControlRect(QStyle::CC_GroupBox, &option,
407
QStyle::SC_GroupBoxCheckBox, &groupBox);
409
QTest::mouseClick(&groupBox, Qt::LeftButton, 0, rect.center());
410
QCOMPARE(clickSpy.count(), 1);
411
QCOMPARE(toggleSpy.count(), 2);
412
QVERIFY(toggleTimeStamp < clickTimeStamp);
415
void tst_QGroupBox::clickTimestampSlot()
417
clickTimeStamp = ++timeStamp;
420
void tst_QGroupBox::toggleTimestampSlot()
422
toggleTimeStamp = ++timeStamp;
425
void tst_QGroupBox::childrenAreDisabled()
428
box.setCheckable(true);
429
box.setChecked(false);
431
QVBoxLayout *layout = new QVBoxLayout;
432
layout->addWidget(new QRadioButton);
433
layout->addWidget(new QRadioButton);
434
layout->addWidget(new QRadioButton);
435
box.setLayout(layout);
437
foreach (QObject *object, box.children()) {
438
if (QWidget *widget = qobject_cast<QWidget *>(object)) {
439
QVERIFY(!widget->isEnabled());
440
QVERIFY(!widget->testAttribute(Qt::WA_ForceDisabled));
444
box.setChecked(true);
445
foreach (QObject *object, box.children()) {
446
if (QWidget *widget = qobject_cast<QWidget *>(object)) {
447
QVERIFY(widget->isEnabled());
448
QVERIFY(!widget->testAttribute(Qt::WA_ForceDisabled));
452
box.setChecked(false);
453
foreach (QObject *object, box.children()) {
454
if (QWidget *widget = qobject_cast<QWidget *>(object)) {
455
QVERIFY(!widget->isEnabled());
456
QVERIFY(!widget->testAttribute(Qt::WA_ForceDisabled));
461
void tst_QGroupBox::propagateFocus()
464
QLineEdit lineEdit(&box);
466
QApplication::setActiveWindow(&box);
467
QVERIFY(QTest::qWaitForWindowActive(&box));
469
QTRY_COMPARE(qApp->focusWidget(), static_cast<QWidget*>(&lineEdit));
472
void tst_QGroupBox::task_QTBUG_19170_ignoreMouseReleseEvent()
475
box.setCheckable(true);
476
box.setChecked(false);
477
box.setTitle("This is a test for QTBUG-19170");
480
QStyleOptionGroupBox option;
481
option.initFrom(&box);
482
option.subControls = QStyle::SubControls(QStyle::SC_All);
483
QRect rect = box.style()->subControlRect(QStyle::CC_GroupBox, &option,
484
QStyle::SC_GroupBoxCheckBox, &box);
486
QTest::mouseClick(&box, Qt::LeftButton, 0, rect.center());
487
QCOMPARE(box.isChecked(), true);
489
box.setChecked(false);
490
QTest::mouseRelease(&box, Qt::LeftButton, 0, rect.center());
491
QCOMPARE(box.isChecked(), false);
494
QTEST_MAIN(tst_QGroupBox)
495
#include "tst_qgroupbox.moc"