tests/auto/q3buttongroup/tst_q3buttongroup.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 <QApplication>
       
    44 #include <Q3ButtonGroup>
       
    45 #include <QCheckBox>
       
    46 #include <QRadioButton>
       
    47 #include <QPushButton>
       
    48 #include <QDebug>
       
    49 #include <QLayout>
       
    50 #include <QtTest/QtTest>
       
    51 
       
    52 class tst_q3buttongroup : public QObject
       
    53 {
       
    54 Q_OBJECT
       
    55 private slots:
       
    56     void exclusiveButtons();
       
    57     void nonExclusiveButtons();
       
    58     void buttonIds();
       
    59     void buttonId();
       
    60     void clickLock();
       
    61     void task198864_insert();
       
    62     void task200764_insert_data();
       
    63     void task200764_insert();
       
    64     void task238902_directChildrenOnly();
       
    65 };
       
    66 
       
    67 /*
       
    68     Test that check boxes created with a Q3ButtonGroup parent in exclusive
       
    69     mode really are exclusive.
       
    70 */
       
    71 void tst_q3buttongroup::exclusiveButtons()
       
    72 {
       
    73     Q3ButtonGroup group(1, Qt::Horizontal);
       
    74     group.setExclusive(true);
       
    75 
       
    76     QCheckBox *b1 = new QCheckBox("Hi", &group);
       
    77     QCheckBox *b2 = new QCheckBox("there", &group);
       
    78     QCheckBox *b3 = new QCheckBox("foo", &group);
       
    79 
       
    80     group.show();
       
    81 
       
    82     // Check b1 and verify that it stuck.
       
    83     b1->setCheckState(Qt::Checked);
       
    84     QCOMPARE(b1->checkState(), Qt::Checked);
       
    85 
       
    86     // Check b2 and verify that b1 is now unchecked.
       
    87     b2->setCheckState(Qt::Checked);
       
    88     QCOMPARE(b1->checkState(), Qt::Unchecked);
       
    89 
       
    90     // Check b3 and verify that b2 and b1 are now unchecked.
       
    91     b3->setCheckState(Qt::Checked);
       
    92     QCOMPARE(b1->checkState(), Qt::Unchecked);
       
    93     QCOMPARE(b2->checkState(), Qt::Unchecked);
       
    94 }
       
    95 
       
    96 /*
       
    97     Test that setting exclusive to false works.
       
    98 */
       
    99 void tst_q3buttongroup::nonExclusiveButtons()
       
   100 {
       
   101     Q3ButtonGroup group(1, Qt::Horizontal);
       
   102 
       
   103     QWidget parent;
       
   104 
       
   105     QCheckBox *b1 = new QCheckBox("Hi", &parent);
       
   106     group.insert(b1);
       
   107     QCheckBox *b2 = new QCheckBox("there", &parent);
       
   108     group.insert(b2);
       
   109     QCheckBox *b3 = new QCheckBox("foo", &parent);
       
   110     group.insert(b3);
       
   111 
       
   112     group.setExclusive(false);
       
   113     group.show();
       
   114 
       
   115     // Check b1 and verify that it stuck.
       
   116     b1->setCheckState(Qt::Checked);
       
   117     QCOMPARE(b1->checkState(), Qt::Checked);
       
   118 
       
   119     // Check b2 and verify that b1 is still checked.
       
   120     b2->setCheckState(Qt::Checked);
       
   121     QCOMPARE(b1->checkState(), Qt::Checked);
       
   122 
       
   123     // Check b3 and verify that b2 and b1 are still checked.
       
   124     b3->setCheckState(Qt::Checked);
       
   125     QCOMPARE(b1->checkState(), Qt::Checked);
       
   126     QCOMPARE(b2->checkState(), Qt::Checked);
       
   127 }
       
   128 
       
   129 /*
       
   130     Test that Ids get assigned
       
   131 */
       
   132 void tst_q3buttongroup::buttonIds()
       
   133 {
       
   134     Q3ButtonGroup group(0, Qt::Vertical, "ButtonGroup");
       
   135     group.setExclusive(true);
       
   136     QVERIFY(group.isExclusive());
       
   137 
       
   138     for (int i=0; i < 10; i++) {
       
   139         QRadioButton *button = new QRadioButton(QString("Button_%1").arg(i + 1) , &group);
       
   140         QCOMPARE(group.id(button) , i);
       
   141         int id = group.insert(button);
       
   142         QCOMPARE(id, i);
       
   143         group.setButton(id);
       
   144         QCOMPARE(group.selectedId(), id);
       
   145     }
       
   146 
       
   147     QCheckBox *button2 = new QCheckBox(QString("manuallyAdded"));
       
   148     int id = group.insert( button2 );
       
   149     QCOMPARE(id , 10 );
       
   150 
       
   151     button2->setChecked(true);
       
   152     QCOMPARE( group.selectedId() , id );
       
   153 
       
   154     group.remove(group.find(5));
       
   155     QCOMPARE(group.count() , 10);
       
   156 
       
   157     delete button2;
       
   158 }
       
   159 
       
   160 void tst_q3buttongroup::buttonId()
       
   161 {
       
   162     Q3ButtonGroup bg;
       
   163     QPushButton *button = new QPushButton("Foo", &bg);
       
   164     int id = bg.insert(button, 1);
       
   165     QApplication::instance()->processEvents();
       
   166     QCOMPARE(id, bg.id(button));
       
   167 }
       
   168 
       
   169 void tst_q3buttongroup::clickLock()
       
   170 {
       
   171     // Task 177677
       
   172     QProcess process;
       
   173     process.start(QLatin1String("clickLock/clickLock"));
       
   174     if (!process.waitForStarted(10000)) {
       
   175         QFAIL("Could not launch process.");
       
   176     }
       
   177 
       
   178     if (!process.waitForFinished(15000)) {
       
   179         process.terminate();
       
   180         QFAIL("Could not handle click events properly");
       
   181     }
       
   182 }
       
   183 
       
   184 void tst_q3buttongroup::task198864_insert()
       
   185 {
       
   186 #if 0
       
   187     Q3ButtonGroup *group = new Q3ButtonGroup;
       
   188     QWidget *parent = new QWidget;
       
   189 
       
   190     const int id1 = group->insert(new QPushButton(parent));
       
   191     QCOMPARE(id1, 0);
       
   192     const int id2 = group->insert(new QPushButton(parent));
       
   193     QCOMPARE(id2, 1);
       
   194 
       
   195     delete parent;
       
   196     parent = new QWidget;
       
   197 
       
   198     const int id3 = group->insert(new QPushButton(parent));
       
   199     QCOMPARE(id3, 0);
       
   200 #endif
       
   201 
       
   202     Q3ButtonGroup *group = new Q3ButtonGroup;
       
   203 
       
   204     QPushButton *button1 = new QPushButton;
       
   205     const int id1 = group->insert(button1);
       
   206     QCOMPARE(id1, 0);
       
   207 
       
   208     QPushButton *button2 = new QPushButton;
       
   209     const int id2 = group->insert(button2);
       
   210     QCOMPARE(id2, 1);
       
   211 
       
   212     delete button1;
       
   213     delete button2;
       
   214 
       
   215     QPushButton *button3 = new QPushButton;
       
   216     const int id3 = group->insert(button3);
       
   217     QCOMPARE(id3, 0);
       
   218 }
       
   219 
       
   220 typedef QList<int> IntList;
       
   221 Q_DECLARE_METATYPE(IntList);
       
   222 
       
   223 void tst_q3buttongroup::task200764_insert_data()
       
   224 {
       
   225     QTest::addColumn<IntList >("ids");
       
   226     QTest::newRow("1") << (IntList() << 0 << 1 << 2 << 3);
       
   227     QTest::newRow("2") << (IntList() << 0 << 3 << 2 << 1);
       
   228     QTest::newRow("3") << (IntList() << 3 << 2 << 1 << 0);
       
   229     QTest::newRow("4") << (IntList() << 3 << 1 << 0 << 2);
       
   230 }
       
   231 
       
   232 class task200764_Widget : public QWidget
       
   233 {
       
   234     Q_OBJECT
       
   235 public:
       
   236     task200764_Widget(const IntList &ids)
       
   237     {
       
   238         Q3ButtonGroup *buttonGroup = new Q3ButtonGroup;
       
   239         buttonGroup->setExclusive(true);
       
   240         QVBoxLayout *layout = new QVBoxLayout(this);
       
   241 
       
   242         for (int i = 0; i < ids.size(); ++i) {
       
   243             QPushButton *button = new QPushButton(buttonGroup);
       
   244             const int id = buttonGroup->insert(button, ids.at(i));
       
   245             //button->setText(QString("%1 (id:%2->%3)").arg(i).arg(ids.at(i)).arg(id));
       
   246             layout->addWidget(button);
       
   247             buttons << button;
       
   248             actualIds << id;
       
   249         }
       
   250 
       
   251         QObject::connect(buttonGroup, SIGNAL(clicked(int)), SLOT(clicked(int)));
       
   252     }
       
   253 
       
   254     ~task200764_Widget()
       
   255     {
       
   256         foreach (QPushButton *button, buttons)
       
   257             delete button;
       
   258     }
       
   259 
       
   260     QList<QPushButton *> buttons;
       
   261     QList<int> actualIds;
       
   262     QList<int> clickedIds;
       
   263 
       
   264 private slots:
       
   265     void clicked(int id) { clickedIds << id; }
       
   266 };
       
   267 
       
   268 void tst_q3buttongroup::task200764_insert()
       
   269 {
       
   270     QFETCH(IntList, ids);
       
   271 
       
   272     task200764_Widget widget(ids);
       
   273     widget.show();
       
   274 
       
   275     for (int i = 0; i < ids.size(); ++i) {
       
   276         widget.clickedIds.clear();
       
   277         QTest::mouseClick(widget.buttons.at(i), Qt::LeftButton);
       
   278         QCOMPARE(widget.clickedIds.size(), 1);
       
   279         QCOMPARE(widget.clickedIds.first(), widget.actualIds.at(i));
       
   280     }
       
   281 }
       
   282 
       
   283 void tst_q3buttongroup::task238902_directChildrenOnly()
       
   284 {
       
   285     Q3ButtonGroup *group = new Q3ButtonGroup;
       
   286 
       
   287     QFrame *frame = new QFrame(group);
       
   288     QPushButton *button = new QPushButton(frame);
       
   289     QSignalSpy spy(button, SIGNAL(clicked()));
       
   290     QSignalSpy spy2(group, SIGNAL(clicked(int)));
       
   291     group->show();
       
   292     QTest::qWait(500);
       
   293     QTest::mouseClick(button, Qt::LeftButton);
       
   294     QTest::qWait(500);
       
   295     QCOMPARE(spy.count(),1);
       
   296     QCOMPARE(spy2.count(),0);
       
   297     group->hide();
       
   298 
       
   299     //normal case, should work
       
   300     Q3ButtonGroup *group2 = new Q3ButtonGroup;
       
   301 
       
   302     QPushButton *button2 = new QPushButton(group2);
       
   303     QSignalSpy spy3(button2, SIGNAL(clicked()));
       
   304     QSignalSpy spy4(group2, SIGNAL(clicked(int)));
       
   305     group2->show();
       
   306     QTest::qWait(500);
       
   307     QTest::mouseClick(button2, Qt::LeftButton);
       
   308     QTest::qWait(500);
       
   309     QCOMPARE(spy3.count(),1);
       
   310     QCOMPARE(spy4.count(),1);
       
   311 }
       
   312 
       
   313 QTEST_MAIN(tst_q3buttongroup)
       
   314 #include "tst_q3buttongroup.moc"