~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to tests/auto/widgets/widgets/qgroupbox/tst_qgroupbox.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
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.
 
16
**
 
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.
 
24
**
 
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.
 
28
**
 
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.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
 
 
43
#include <QtTest/QtTest>
 
44
#include <QLineEdit>
 
45
#include <QStyle>
 
46
#include <QStyleOptionGroupBox>
 
47
#include <QVBoxLayout>
 
48
#include <QRadioButton>
 
49
 
 
50
#include "qgroupbox.h"
 
51
 
 
52
class tst_QGroupBox : public QObject
 
53
{
 
54
    Q_OBJECT
 
55
 
 
56
public:
 
57
    tst_QGroupBox();
 
58
    virtual ~tst_QGroupBox();
 
59
 
 
60
public slots:
 
61
    void toggledHelperSlot(bool on);
 
62
    void init();
 
63
    void clickTimestampSlot();
 
64
    void toggleTimestampSlot();
 
65
 
 
66
private slots:
 
67
    void setTitle_data();
 
68
    void setTitle();
 
69
    void setCheckable_data();
 
70
    void setCheckable();
 
71
    void setChecked_data();
 
72
    void setChecked();
 
73
    void enabledPropagation();
 
74
    void sizeHint();
 
75
    void toggled();
 
76
    void clicked_data();
 
77
    void clicked();
 
78
    void toggledVsClicked();
 
79
    void childrenAreDisabled();
 
80
    void propagateFocus();
 
81
    void task_QTBUG_19170_ignoreMouseReleseEvent();
 
82
 
 
83
private:
 
84
    bool checked;
 
85
    qint64 timeStamp;
 
86
    qint64 clickTimeStamp;
 
87
    qint64 toggleTimeStamp;
 
88
 
 
89
};
 
90
 
 
91
tst_QGroupBox::tst_QGroupBox()
 
92
{
 
93
    checked = true;
 
94
}
 
95
 
 
96
tst_QGroupBox::~tst_QGroupBox()
 
97
{
 
98
 
 
99
}
 
100
 
 
101
void tst_QGroupBox::init()
 
102
{
 
103
    checked = true;
 
104
}
 
105
 
 
106
void tst_QGroupBox::setTitle_data()
 
107
{
 
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  ");
 
114
}
 
115
 
 
116
void tst_QGroupBox::setCheckable_data()
 
117
{
 
118
    QTest::addColumn<bool>("checkable");
 
119
    QTest::addColumn<bool>("expectedCheckable");
 
120
    QTest::newRow( "checkable_true" ) << true << true;
 
121
    QTest::newRow( "checkable_false" ) << false << false;
 
122
}
 
123
 
 
124
void tst_QGroupBox::setChecked_data()
 
125
{
 
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;
 
132
}
 
133
 
 
134
void tst_QGroupBox::setTitle()
 
135
{
 
136
    QFETCH( QString, title );
 
137
    QFETCH( QString, expectedTitle );
 
138
 
 
139
    QGroupBox groupBox;
 
140
 
 
141
    groupBox.setTitle( title );
 
142
 
 
143
    QCOMPARE( groupBox.title() , expectedTitle );
 
144
}
 
145
 
 
146
void tst_QGroupBox::setCheckable()
 
147
{
 
148
    QFETCH( bool, checkable );
 
149
    QFETCH( bool, expectedCheckable );
 
150
 
 
151
    QGroupBox groupBox;
 
152
 
 
153
    groupBox.setCheckable( checkable );
 
154
    QCOMPARE( groupBox.isCheckable() , expectedCheckable );
 
155
}
 
156
 
 
157
 
 
158
void tst_QGroupBox::setChecked()
 
159
{
 
160
    QFETCH( bool, checkable );
 
161
    QFETCH( bool, checked );
 
162
    QFETCH( bool, expectedChecked );
 
163
 
 
164
    QGroupBox groupBox;
 
165
 
 
166
    groupBox.setCheckable( checkable );
 
167
    groupBox.setChecked( checked );
 
168
    QCOMPARE( groupBox.isChecked(), expectedChecked );
 
169
}
 
170
 
 
171
void tst_QGroupBox::enabledPropagation()
 
172
{
 
173
    QGroupBox *testWidget = new QGroupBox(0);
 
174
    testWidget->setCheckable(true);
 
175
    testWidget->setChecked(true);
 
176
    QWidget* childWidget = new QWidget( testWidget );
 
177
    childWidget->show();
 
178
    QVERIFY( testWidget->isEnabled() );
 
179
    QVERIFY( childWidget->isEnabled() );
 
180
 
 
181
    testWidget->setEnabled( false );
 
182
    QVERIFY( !testWidget->isEnabled() );
 
183
    QVERIFY( !childWidget->isEnabled() );
 
184
 
 
185
    testWidget->setDisabled( false );
 
186
    QVERIFY( testWidget->isEnabled() );
 
187
    QVERIFY( childWidget->isEnabled() );
 
188
 
 
189
    QWidget* grandChildWidget = new QWidget( childWidget );
 
190
    QVERIFY( grandChildWidget->isEnabled() );
 
191
 
 
192
    testWidget->setDisabled( true );
 
193
    QVERIFY( !testWidget->isEnabled() );
 
194
    QVERIFY( !childWidget->isEnabled() );
 
195
    QVERIFY( !grandChildWidget->isEnabled() );
 
196
 
 
197
    grandChildWidget->setEnabled( false );
 
198
    testWidget->setEnabled( true );
 
199
    QVERIFY( testWidget->isEnabled() );
 
200
    QVERIFY( childWidget->isEnabled() );
 
201
    QVERIFY( !grandChildWidget->isEnabled() );
 
202
 
 
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() );
 
210
 
 
211
    // Reset state
 
212
    testWidget->setEnabled( true );
 
213
    childWidget->setEnabled( true );
 
214
    grandChildWidget->setEnabled( true );
 
215
 
 
216
    // Now check when it's disabled
 
217
    testWidget->setChecked(false);
 
218
    QVERIFY( testWidget->isEnabled() );
 
219
    QVERIFY( !childWidget->isEnabled() );
 
220
 
 
221
    testWidget->setEnabled( false );
 
222
    QVERIFY( !testWidget->isEnabled() );
 
223
    QVERIFY( !childWidget->isEnabled() );
 
224
 
 
225
    testWidget->setDisabled( false );
 
226
    QVERIFY( testWidget->isEnabled() );
 
227
    QVERIFY( !childWidget->isEnabled() );
 
228
 
 
229
    QVERIFY( !grandChildWidget->isEnabled() );
 
230
 
 
231
    testWidget->setDisabled( true );
 
232
    QVERIFY( !testWidget->isEnabled() );
 
233
    QVERIFY( !childWidget->isEnabled() );
 
234
    QVERIFY( !grandChildWidget->isEnabled() );
 
235
 
 
236
    grandChildWidget->setEnabled( false );
 
237
    testWidget->setEnabled( true );
 
238
    QVERIFY( testWidget->isEnabled() );
 
239
    QVERIFY( !childWidget->isEnabled() );
 
240
    QVERIFY( !grandChildWidget->isEnabled() );
 
241
 
 
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() );
 
249
 
 
250
    // Reset state
 
251
    testWidget->setEnabled( true );
 
252
    childWidget->setEnabled( true );
 
253
    grandChildWidget->setEnabled( true );
 
254
 
 
255
    // Finally enable it again
 
256
    testWidget->setChecked(true);
 
257
    QVERIFY( testWidget->isEnabled() );
 
258
    QVERIFY( childWidget->isEnabled() );
 
259
 
 
260
    testWidget->setEnabled( false );
 
261
    QVERIFY( !testWidget->isEnabled() );
 
262
    QVERIFY( !childWidget->isEnabled() );
 
263
 
 
264
    testWidget->setDisabled( false );
 
265
    QVERIFY( testWidget->isEnabled() );
 
266
    QVERIFY( childWidget->isEnabled() );
 
267
    QVERIFY( grandChildWidget->isEnabled() );
 
268
 
 
269
    testWidget->setDisabled( true );
 
270
    QVERIFY( !testWidget->isEnabled() );
 
271
    QVERIFY( !childWidget->isEnabled() );
 
272
    QVERIFY( !grandChildWidget->isEnabled() );
 
273
 
 
274
    grandChildWidget->setEnabled( false );
 
275
    testWidget->setEnabled( true );
 
276
    QVERIFY( testWidget->isEnabled() );
 
277
    QVERIFY( childWidget->isEnabled() );
 
278
    QVERIFY( !grandChildWidget->isEnabled() );
 
279
 
 
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() );
 
287
 
 
288
    delete testWidget;
 
289
}
 
290
 
 
291
 
 
292
void tst_QGroupBox::sizeHint()
 
293
{
 
294
    QGroupBox testWidget1(0);
 
295
    testWidget1.setTitle("&0&0&0&0&0&0&0&0&0&0");
 
296
 
 
297
    QGroupBox testWidget2(0);
 
298
    testWidget2.setTitle("0000000000");
 
299
 
 
300
    QCOMPARE(testWidget1.sizeHint().width(), testWidget2.sizeHint().width());
 
301
 
 
302
    // if the above fails one should maybe test to see like underneath.
 
303
    // QVERIFY((QABS(testWidget1->sizeHint().width() - testWidget2->sizeHint().width()) < 10));
 
304
}
 
305
 
 
306
void tst_QGroupBox::toggledHelperSlot(bool on)
 
307
{
 
308
    checked = on;
 
309
}
 
310
 
 
311
 
 
312
void tst_QGroupBox::toggled()
 
313
{
 
314
    QGroupBox testWidget1(0);
 
315
    testWidget1.setCheckable(true);
 
316
    connect(&testWidget1, SIGNAL(toggled(bool)), this, SLOT(toggledHelperSlot(bool)));
 
317
    QLineEdit *edit = new QLineEdit(&testWidget1);
 
318
    QVERIFY(checked);
 
319
    testWidget1.setChecked(true);
 
320
    QVERIFY(checked);
 
321
    QVERIFY(edit->isEnabled());
 
322
    testWidget1.setChecked(false);
 
323
    QVERIFY(!checked);
 
324
    QVERIFY(!edit->isEnabled());
 
325
}
 
326
 
 
327
void tst_QGroupBox::clicked_data()
 
328
{
 
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");
 
334
 
 
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;
 
340
 
 
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;
 
346
 
 
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;
 
352
}
 
353
 
 
354
void tst_QGroupBox::clicked()
 
355
{
 
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)));
 
365
 
 
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();
 
371
 
 
372
    QRect rect = testWidget.style()->subControlRect(QStyle::CC_GroupBox, &option,
 
373
                                                    QStyle::SubControl(areaToHit), &testWidget);
 
374
 
 
375
    if (rect.isValid())
 
376
        QTest::mouseClick(&testWidget, Qt::LeftButton, 0, rect.center());
 
377
    else
 
378
        QTest::mouseClick(&testWidget, Qt::LeftButton);
 
379
 
 
380
    QTEST(spy.count(), "clickedCount");
 
381
    if (spy.count() > 0)
 
382
        QTEST(spy.at(0).at(0).toBool(), "finalCheck");
 
383
    QTEST(testWidget.isChecked(), "finalCheck");
 
384
}
 
385
 
 
386
void tst_QGroupBox::toggledVsClicked()
 
387
{
 
388
    timeStamp = clickTimeStamp = toggleTimeStamp = 0;
 
389
    QGroupBox groupBox;
 
390
    groupBox.setCheckable(true);
 
391
    QSignalSpy toggleSpy(&groupBox, SIGNAL(toggled(bool)));
 
392
    QSignalSpy clickSpy(&groupBox, SIGNAL(clicked(bool)));
 
393
 
 
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());
 
399
 
 
400
    connect(&groupBox, SIGNAL(clicked(bool)), this, SLOT(clickTimestampSlot()));
 
401
    connect(&groupBox, SIGNAL(toggled(bool)), this, SLOT(toggleTimestampSlot()));
 
402
 
 
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);
 
408
 
 
409
    QTest::mouseClick(&groupBox, Qt::LeftButton, 0, rect.center());
 
410
    QCOMPARE(clickSpy.count(), 1);
 
411
    QCOMPARE(toggleSpy.count(), 2);
 
412
    QVERIFY(toggleTimeStamp < clickTimeStamp);
 
413
}
 
414
 
 
415
void tst_QGroupBox::clickTimestampSlot()
 
416
{
 
417
    clickTimeStamp = ++timeStamp;
 
418
}
 
419
 
 
420
void tst_QGroupBox::toggleTimestampSlot()
 
421
{
 
422
    toggleTimeStamp = ++timeStamp;
 
423
}
 
424
 
 
425
void tst_QGroupBox::childrenAreDisabled()
 
426
{
 
427
    QGroupBox box;
 
428
    box.setCheckable(true);
 
429
    box.setChecked(false);
 
430
 
 
431
    QVBoxLayout *layout = new QVBoxLayout;
 
432
    layout->addWidget(new QRadioButton);
 
433
    layout->addWidget(new QRadioButton);
 
434
    layout->addWidget(new QRadioButton);
 
435
    box.setLayout(layout);
 
436
 
 
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));
 
441
        }
 
442
    }
 
443
 
 
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));
 
449
        }
 
450
    }
 
451
 
 
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));
 
457
        }
 
458
    }
 
459
}
 
460
 
 
461
void tst_QGroupBox::propagateFocus()
 
462
{
 
463
    QGroupBox box;
 
464
    QLineEdit lineEdit(&box);
 
465
    box.show();
 
466
    QApplication::setActiveWindow(&box);
 
467
    QVERIFY(QTest::qWaitForWindowActive(&box));
 
468
    box.setFocus();
 
469
    QTRY_COMPARE(qApp->focusWidget(), static_cast<QWidget*>(&lineEdit));
 
470
}
 
471
 
 
472
void tst_QGroupBox::task_QTBUG_19170_ignoreMouseReleseEvent()
 
473
{
 
474
    QGroupBox box;
 
475
    box.setCheckable(true);
 
476
    box.setChecked(false);
 
477
    box.setTitle("This is a test for QTBUG-19170");
 
478
    box.show();
 
479
 
 
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);
 
485
 
 
486
    QTest::mouseClick(&box, Qt::LeftButton, 0, rect.center());
 
487
    QCOMPARE(box.isChecked(), true);
 
488
 
 
489
    box.setChecked(false);
 
490
    QTest::mouseRelease(&box, Qt::LeftButton, 0, rect.center());
 
491
    QCOMPARE(box.isChecked(), false);
 
492
}
 
493
 
 
494
QTEST_MAIN(tst_QGroupBox)
 
495
#include "tst_qgroupbox.moc"