tests/auto/qgroupbox/tst_qgroupbox.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    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 #include "../../shared/util.h"
       
    53 
       
    54 //TESTED_CLASS=
       
    55 //TESTED_FILES=
       
    56 
       
    57 class tst_QGroupBox : public QObject
       
    58 {
       
    59     Q_OBJECT
       
    60 
       
    61 public:
       
    62     tst_QGroupBox();
       
    63     virtual ~tst_QGroupBox();
       
    64 
       
    65 public slots:
       
    66     void toggledHelperSlot(bool on);
       
    67     void init();
       
    68     void clickTimestampSlot();
       
    69     void toggleTimestampSlot();
       
    70 
       
    71 private slots:
       
    72     void setTitle_data();
       
    73     void setTitle();
       
    74     void setCheckable_data();
       
    75     void setCheckable();
       
    76     void setChecked_data();
       
    77     void setChecked();
       
    78     void enabledPropagation();
       
    79     void sizeHint();
       
    80     void toggled();
       
    81     void clicked_data();
       
    82     void clicked();
       
    83     void toggledVsClicked();
       
    84     void childrenAreDisabled();
       
    85     void propagateFocus();
       
    86 
       
    87 private:
       
    88     bool checked;
       
    89     qint64 timeStamp;
       
    90     qint64 clickTimeStamp;
       
    91     qint64 toggleTimeStamp;
       
    92 
       
    93 };
       
    94 
       
    95 tst_QGroupBox::tst_QGroupBox()
       
    96 {
       
    97     checked = true;
       
    98 }
       
    99 
       
   100 tst_QGroupBox::~tst_QGroupBox()
       
   101 {
       
   102 
       
   103 }
       
   104 
       
   105 void tst_QGroupBox::init()
       
   106 {
       
   107     checked = true;
       
   108 }
       
   109 
       
   110 void tst_QGroupBox::setTitle_data()
       
   111 {
       
   112     QTest::addColumn<QString>("title");
       
   113     QTest::addColumn<QString>("expectedTitle");
       
   114     QTest::newRow( "empty_title" ) << QString("") << QString("");
       
   115     QTest::newRow( "normal_title" ) << QString("Whatisthematrix") << QString("Whatisthematrix");
       
   116     QTest::newRow( "special_chars_title" ) << QString("<>%&#/()=") << QString("<>%&#/()=");
       
   117     QTest::newRow( "spaces_title" ) << QString("  Hello  ") <<  QString("  Hello  ");
       
   118 }
       
   119 
       
   120 void tst_QGroupBox::setCheckable_data()
       
   121 {
       
   122     QTest::addColumn<bool>("checkable");
       
   123     QTest::addColumn<bool>("expectedCheckable");
       
   124     QTest::newRow( "checkable_true" ) << true << true;
       
   125     QTest::newRow( "checkable_false" ) << false << false;
       
   126 }
       
   127 
       
   128 void tst_QGroupBox::setChecked_data()
       
   129 {
       
   130     QTest::addColumn<bool>("checkable");
       
   131     QTest::addColumn<bool>("checked");
       
   132     QTest::addColumn<bool>("expectedChecked");
       
   133     QTest::newRow( "checkable_false_checked_true" ) << false << true << false;
       
   134     QTest::newRow( "checkable_true_checked_true" ) << true << true << true;
       
   135     QTest::newRow( "checkable_true_checked_false" ) << true << false << false;
       
   136 }
       
   137 
       
   138 void tst_QGroupBox::setTitle()
       
   139 {
       
   140     QFETCH( QString, title );
       
   141     QFETCH( QString, expectedTitle );
       
   142 
       
   143     QGroupBox groupBox;
       
   144 
       
   145     groupBox.setTitle( title );
       
   146 
       
   147     QCOMPARE( groupBox.title() , expectedTitle );
       
   148 }
       
   149 
       
   150 void tst_QGroupBox::setCheckable()
       
   151 {
       
   152     QFETCH( bool, checkable );
       
   153     QFETCH( bool, expectedCheckable );
       
   154 
       
   155     QGroupBox groupBox;
       
   156 
       
   157     groupBox.setCheckable( checkable );
       
   158     QCOMPARE( groupBox.isCheckable() , expectedCheckable );
       
   159 }
       
   160 
       
   161 
       
   162 void tst_QGroupBox::setChecked()
       
   163 {
       
   164     QFETCH( bool, checkable );
       
   165     QFETCH( bool, checked );
       
   166     QFETCH( bool, expectedChecked );
       
   167 
       
   168     QGroupBox groupBox;
       
   169 
       
   170     groupBox.setCheckable( checkable );
       
   171     groupBox.setChecked( checked );
       
   172     QCOMPARE( groupBox.isChecked(), expectedChecked );
       
   173 }
       
   174 
       
   175 void tst_QGroupBox::enabledPropagation()
       
   176 {
       
   177     QGroupBox *testWidget = new QGroupBox(0);
       
   178     testWidget->setCheckable(true);
       
   179     testWidget->setChecked(true);
       
   180     QWidget* childWidget = new QWidget( testWidget );
       
   181     childWidget->show();
       
   182     QVERIFY( testWidget->isEnabled() );
       
   183     QVERIFY( childWidget->isEnabled() );
       
   184 
       
   185     testWidget->setEnabled( false );
       
   186     QVERIFY( !testWidget->isEnabled() );
       
   187     QVERIFY( !childWidget->isEnabled() );
       
   188 
       
   189     testWidget->setDisabled( false );
       
   190     QVERIFY( testWidget->isEnabled() );
       
   191     QVERIFY( childWidget->isEnabled() );
       
   192 
       
   193     QWidget* grandChildWidget = new QWidget( childWidget );
       
   194     QVERIFY( grandChildWidget->isEnabled() );
       
   195 
       
   196     testWidget->setDisabled( true );
       
   197     QVERIFY( !testWidget->isEnabled() );
       
   198     QVERIFY( !childWidget->isEnabled() );
       
   199     QVERIFY( !grandChildWidget->isEnabled() );
       
   200 
       
   201     grandChildWidget->setEnabled( false );
       
   202     testWidget->setEnabled( true );
       
   203     QVERIFY( testWidget->isEnabled() );
       
   204     QVERIFY( childWidget->isEnabled() );
       
   205     QVERIFY( !grandChildWidget->isEnabled() );
       
   206 
       
   207     grandChildWidget->setEnabled( true );
       
   208     testWidget->setEnabled( false );
       
   209     childWidget->setDisabled( true );
       
   210     testWidget->setEnabled( true );
       
   211     QVERIFY( testWidget->isEnabled() );
       
   212     QVERIFY( !childWidget->isEnabled() );
       
   213     QVERIFY( !grandChildWidget->isEnabled() );
       
   214 
       
   215     // Reset state
       
   216     testWidget->setEnabled( true );
       
   217     childWidget->setEnabled( true );
       
   218     grandChildWidget->setEnabled( true );
       
   219 
       
   220     // Now check when it's disabled
       
   221     testWidget->setChecked(false);
       
   222     QVERIFY( testWidget->isEnabled() );
       
   223     QVERIFY( !childWidget->isEnabled() );
       
   224 
       
   225     testWidget->setEnabled( false );
       
   226     QVERIFY( !testWidget->isEnabled() );
       
   227     QVERIFY( !childWidget->isEnabled() );
       
   228 
       
   229     testWidget->setDisabled( false );
       
   230     QVERIFY( testWidget->isEnabled() );
       
   231     QVERIFY( !childWidget->isEnabled() );
       
   232 
       
   233     QVERIFY( !grandChildWidget->isEnabled() );
       
   234 
       
   235     testWidget->setDisabled( true );
       
   236     QVERIFY( !testWidget->isEnabled() );
       
   237     QVERIFY( !childWidget->isEnabled() );
       
   238     QVERIFY( !grandChildWidget->isEnabled() );
       
   239 
       
   240     grandChildWidget->setEnabled( false );
       
   241     testWidget->setEnabled( true );
       
   242     QVERIFY( testWidget->isEnabled() );
       
   243     QVERIFY( !childWidget->isEnabled() );
       
   244     QVERIFY( !grandChildWidget->isEnabled() );
       
   245 
       
   246     grandChildWidget->setEnabled( true );
       
   247     testWidget->setEnabled( false );
       
   248     childWidget->setDisabled( true );
       
   249     testWidget->setEnabled( true );
       
   250     QVERIFY( testWidget->isEnabled() );
       
   251     QVERIFY( !childWidget->isEnabled() );
       
   252     QVERIFY( !grandChildWidget->isEnabled() );
       
   253 
       
   254     // Reset state
       
   255     testWidget->setEnabled( true );
       
   256     childWidget->setEnabled( true );
       
   257     grandChildWidget->setEnabled( true );
       
   258 
       
   259     // Finally enable it again
       
   260     testWidget->setChecked(true);
       
   261     QVERIFY( testWidget->isEnabled() );
       
   262     QVERIFY( childWidget->isEnabled() );
       
   263 
       
   264     testWidget->setEnabled( false );
       
   265     QVERIFY( !testWidget->isEnabled() );
       
   266     QVERIFY( !childWidget->isEnabled() );
       
   267 
       
   268     testWidget->setDisabled( false );
       
   269     QVERIFY( testWidget->isEnabled() );
       
   270     QVERIFY( childWidget->isEnabled() );
       
   271     QVERIFY( grandChildWidget->isEnabled() );
       
   272 
       
   273     testWidget->setDisabled( true );
       
   274     QVERIFY( !testWidget->isEnabled() );
       
   275     QVERIFY( !childWidget->isEnabled() );
       
   276     QVERIFY( !grandChildWidget->isEnabled() );
       
   277 
       
   278     grandChildWidget->setEnabled( false );
       
   279     testWidget->setEnabled( true );
       
   280     QVERIFY( testWidget->isEnabled() );
       
   281     QVERIFY( childWidget->isEnabled() );
       
   282     QVERIFY( !grandChildWidget->isEnabled() );
       
   283 
       
   284     grandChildWidget->setEnabled( true );
       
   285     testWidget->setEnabled( false );
       
   286     childWidget->setDisabled( true );
       
   287     testWidget->setEnabled( true );
       
   288     QVERIFY( testWidget->isEnabled() );
       
   289     QVERIFY( !childWidget->isEnabled() );
       
   290     QVERIFY( !grandChildWidget->isEnabled() );
       
   291 
       
   292     delete testWidget;
       
   293 }
       
   294 
       
   295 
       
   296 void tst_QGroupBox::sizeHint()
       
   297 {
       
   298     QGroupBox testWidget1(0);
       
   299     testWidget1.setTitle("&0&0&0&0&0&0&0&0&0&0");
       
   300 
       
   301     QGroupBox testWidget2(0);
       
   302     testWidget2.setTitle("0000000000");
       
   303 
       
   304     QCOMPARE(testWidget1.sizeHint().width(), testWidget2.sizeHint().width());
       
   305 
       
   306     // if the above fails one should maybe test to see like underneath.
       
   307     // QVERIFY((QABS(testWidget1->sizeHint().width() - testWidget2->sizeHint().width()) < 10));
       
   308 }
       
   309 
       
   310 void tst_QGroupBox::toggledHelperSlot(bool on)
       
   311 {
       
   312     checked = on;
       
   313 }
       
   314 
       
   315 
       
   316 void tst_QGroupBox::toggled()
       
   317 {
       
   318     QGroupBox testWidget1(0);
       
   319     testWidget1.setCheckable(true);
       
   320     connect(&testWidget1, SIGNAL(toggled(bool)), this, SLOT(toggledHelperSlot(bool)));
       
   321     QLineEdit *edit = new QLineEdit(&testWidget1);
       
   322     QVERIFY(checked);
       
   323     testWidget1.setChecked(true);
       
   324     QVERIFY(checked);
       
   325     QVERIFY(edit->isEnabled());
       
   326     testWidget1.setChecked(false);
       
   327     QVERIFY(!checked);
       
   328     QVERIFY(!edit->isEnabled());
       
   329 }
       
   330 
       
   331 void tst_QGroupBox::clicked_data()
       
   332 {
       
   333     QTest::addColumn<bool>("checkable");
       
   334     QTest::addColumn<bool>("initialCheck");
       
   335     QTest::addColumn<int>("areaToHit");
       
   336     QTest::addColumn<int>("clickedCount");
       
   337     QTest::addColumn<bool>("finalCheck");
       
   338 
       
   339     QTest::newRow("hit nothing, not checkable") << false << false << int(QStyle::SC_None) << 0 << false;
       
   340     QTest::newRow("hit frame, not checkable") << false << false << int(QStyle::SC_GroupBoxFrame) << 0 << false;
       
   341     QTest::newRow("hit content, not checkable") << false << false << int(QStyle::SC_GroupBoxContents) << 0 << false;
       
   342     QTest::newRow("hit label, not checkable") << false << false << int(QStyle::SC_GroupBoxLabel) << 0 << false;
       
   343     QTest::newRow("hit checkbox, not checkable") << false << false << int(QStyle::SC_GroupBoxCheckBox) << 0 << false;
       
   344 
       
   345     QTest::newRow("hit nothing, checkable") << true << true << int(QStyle::SC_None) << 0 << true;
       
   346     QTest::newRow("hit frame, checkable") << true << true << int(QStyle::SC_GroupBoxFrame) << 0 << true;
       
   347     QTest::newRow("hit content, checkable") << true << true << int(QStyle::SC_GroupBoxContents) << 0 << true;
       
   348     QTest::newRow("hit label, checkable") << true << true << int(QStyle::SC_GroupBoxLabel) << 1 << false;
       
   349     QTest::newRow("hit checkbox, checkable") << true << true << int(QStyle::SC_GroupBoxCheckBox) << 1 << false;
       
   350 
       
   351     QTest::newRow("hit nothing, checkable, but unchecked") << true << false << int(QStyle::SC_None) << 0 << false;
       
   352     QTest::newRow("hit frame, checkable, but unchecked") << true << false << int(QStyle::SC_GroupBoxFrame) << 0 << false;
       
   353     QTest::newRow("hit content, checkable, but unchecked") << true << false << int(QStyle::SC_GroupBoxContents) << 0 << false;
       
   354     QTest::newRow("hit label, checkable, but unchecked") << true << false << int(QStyle::SC_GroupBoxLabel) << 1 << true;
       
   355     QTest::newRow("hit checkbox, checkable, but unchecked") << true << false << int(QStyle::SC_GroupBoxCheckBox) << 1 << true;
       
   356 }
       
   357 
       
   358 void tst_QGroupBox::clicked()
       
   359 {
       
   360     QFETCH(bool, checkable);
       
   361     QFETCH(bool, initialCheck);
       
   362     QFETCH(int, areaToHit);
       
   363     QGroupBox testWidget(QLatin1String("Testing Clicked"));
       
   364     testWidget.setCheckable(checkable);
       
   365     testWidget.setChecked(initialCheck);
       
   366     QCOMPARE(testWidget.isChecked(), initialCheck);
       
   367     testWidget.resize(200, 200);
       
   368     QSignalSpy spy(&testWidget, SIGNAL(clicked(bool)));
       
   369 
       
   370     QStyleOptionGroupBox option;
       
   371     option.initFrom(&testWidget);
       
   372     option.subControls = checkable ? QStyle::SubControls(QStyle::SC_All) : QStyle::SubControls(QStyle::SC_All & ~QStyle::SC_GroupBoxCheckBox);
       
   373     option.text = testWidget.title();
       
   374     option.textAlignment = testWidget.alignment();
       
   375 
       
   376     QRect rect = testWidget.style()->subControlRect(QStyle::CC_GroupBox, &option,
       
   377                                                     QStyle::SubControl(areaToHit), &testWidget);
       
   378 
       
   379     if (rect.isValid())
       
   380         QTest::mouseClick(&testWidget, Qt::LeftButton, 0, rect.center());
       
   381     else
       
   382         QTest::mouseClick(&testWidget, Qt::LeftButton);
       
   383 
       
   384     QTEST(spy.count(), "clickedCount");
       
   385     if (spy.count() > 0)
       
   386         QTEST(spy.at(0).at(0).toBool(), "finalCheck");
       
   387     QTEST(testWidget.isChecked(), "finalCheck");
       
   388 }
       
   389 
       
   390 void tst_QGroupBox::toggledVsClicked()
       
   391 {
       
   392     timeStamp = clickTimeStamp = toggleTimeStamp = 0;
       
   393     QGroupBox groupBox;
       
   394     groupBox.setCheckable(true);
       
   395     QSignalSpy toggleSpy(&groupBox, SIGNAL(toggled(bool)));
       
   396     QSignalSpy clickSpy(&groupBox, SIGNAL(clicked(bool)));
       
   397 
       
   398     groupBox.setChecked(!groupBox.isChecked());
       
   399     QCOMPARE(clickSpy.count(), 0);
       
   400     QCOMPARE(toggleSpy.count(), 1);
       
   401     if (toggleSpy.count() > 0)
       
   402         QCOMPARE(toggleSpy.at(0).at(0).toBool(), groupBox.isChecked());
       
   403 
       
   404     connect(&groupBox, SIGNAL(clicked(bool)), this, SLOT(clickTimestampSlot()));
       
   405     connect(&groupBox, SIGNAL(toggled(bool)), this, SLOT(toggleTimestampSlot()));
       
   406 
       
   407     QStyleOptionGroupBox option;
       
   408     option.initFrom(&groupBox);
       
   409     option.subControls = QStyle::SubControls(QStyle::SC_All);
       
   410     QRect rect = groupBox.style()->subControlRect(QStyle::CC_GroupBox, &option,
       
   411                                                   QStyle::SC_GroupBoxCheckBox, &groupBox);
       
   412 
       
   413     QTest::mouseClick(&groupBox, Qt::LeftButton, 0, rect.center());
       
   414     QCOMPARE(clickSpy.count(), 1);
       
   415     QCOMPARE(toggleSpy.count(), 2);
       
   416     QVERIFY(toggleTimeStamp < clickTimeStamp);
       
   417 }
       
   418 
       
   419 void tst_QGroupBox::clickTimestampSlot()
       
   420 {
       
   421     clickTimeStamp = ++timeStamp;
       
   422 }
       
   423 
       
   424 void tst_QGroupBox::toggleTimestampSlot()
       
   425 {
       
   426     toggleTimeStamp = ++timeStamp;
       
   427 }
       
   428 
       
   429 void tst_QGroupBox::childrenAreDisabled()
       
   430 {
       
   431     QGroupBox box;
       
   432     box.setCheckable(true);
       
   433     box.setChecked(false);
       
   434 
       
   435     QVBoxLayout *layout = new QVBoxLayout;
       
   436     layout->addWidget(new QRadioButton);
       
   437     layout->addWidget(new QRadioButton);
       
   438     layout->addWidget(new QRadioButton);
       
   439     box.setLayout(layout);
       
   440 
       
   441     foreach (QObject *object, box.children()) {
       
   442         if (QWidget *widget = qobject_cast<QWidget *>(object)) {
       
   443             QVERIFY(!widget->isEnabled());
       
   444             QVERIFY(!widget->testAttribute(Qt::WA_ForceDisabled));
       
   445         }
       
   446     }
       
   447 
       
   448     box.setChecked(true);
       
   449     foreach (QObject *object, box.children()) {
       
   450         if (QWidget *widget = qobject_cast<QWidget *>(object)) {
       
   451             QVERIFY(widget->isEnabled());
       
   452             QVERIFY(!widget->testAttribute(Qt::WA_ForceDisabled));
       
   453         }
       
   454     }
       
   455 
       
   456     box.setChecked(false);
       
   457     foreach (QObject *object, box.children()) {
       
   458         if (QWidget *widget = qobject_cast<QWidget *>(object)) {
       
   459             QVERIFY(!widget->isEnabled());
       
   460             QVERIFY(!widget->testAttribute(Qt::WA_ForceDisabled));
       
   461         }
       
   462     }
       
   463 }
       
   464 
       
   465 void tst_QGroupBox::propagateFocus()
       
   466 {
       
   467     QGroupBox box;
       
   468     QLineEdit lineEdit(&box);
       
   469     box.show();
       
   470     QApplication::setActiveWindow(&box);
       
   471     box.setFocus();
       
   472     QTest::qWait(250);
       
   473     QTRY_COMPARE(qApp->focusWidget(), static_cast<QWidget*>(&lineEdit));
       
   474 }
       
   475 
       
   476 QTEST_MAIN(tst_QGroupBox)
       
   477 #include "tst_qgroupbox.moc"