File indexing completed on 2022-11-29 18:25:07

0001 /*
0002     This file is part of the KDE Libraries
0003 
0004     Copyright (C) 2006 Pino Toscano <toscano.pino@tiscali.it>
0005 
0006     This library is free software; you can redistribute it and/or
0007     modify it under the terms of the GNU Library General Public
0008     License as published by the Free Software Foundation; either
0009     version 2 of the License, or (at your option) any later version.
0010 
0011     This library is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014     Library General Public License for more details.
0015 
0016     You should have received a copy of the GNU Library General Public License
0017     along with this library; see the file COPYING.LIB. If not, write to
0018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019     Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #include <QTest>
0023 
0024 #include <QTestEvent>
0025 
0026 #include <QList>
0027 #include <QRadioButton>
0028 #include <QSignalSpy>
0029 #include <QBoxLayout>
0030 
0031 #include "kbuttongrouptest.h"
0032 #include "kbuttongroup.h"
0033 
0034 KButtonGroup *kbuttongroup;
0035 QList<QRadioButton *> buttons;
0036 
0037 void KButtonGroupTest::initTestCase()
0038 {
0039     kbuttongroup = new KButtonGroup();
0040     QVBoxLayout *lay2 = new QVBoxLayout(kbuttongroup);
0041 
0042     for (int i = 0; i < 8; ++i) {
0043         QRadioButton *r = new QRadioButton(kbuttongroup);
0044         r->setText(QString("radio%1").arg(i));
0045         lay2->addWidget(r);
0046         buttons << r;
0047     }
0048 
0049     QCOMPARE(kbuttongroup->selected(), -1);
0050 }
0051 
0052 void KButtonGroupTest::directSelectionTestCase()
0053 {
0054     // test where setSelected is called before the
0055     // ensurePolished() is called.
0056     KButtonGroup *kbuttongroup2 = new KButtonGroup();
0057     kbuttongroup2->setSelected(3);
0058 
0059     QVBoxLayout *lay2 = new QVBoxLayout(kbuttongroup2);
0060     for (int i = 0; i < 8; ++i) {
0061         QRadioButton *r = new QRadioButton(kbuttongroup2);
0062         r->setText(QString("radio%1").arg(i));
0063         lay2->addWidget(r);
0064         buttons << r;
0065     }
0066 
0067     QTest::qWait(250); // events should occur.
0068     QCOMPARE(kbuttongroup2->selected(), 3);
0069 }
0070 
0071 void KButtonGroupTest::cleanupTestCase()
0072 {
0073     kbuttongroup->deleteLater();
0074 }
0075 
0076 void KButtonGroupTest::testEmptyGroup()
0077 {
0078     KButtonGroup kemptybuttongroup;
0079     QCOMPARE(kemptybuttongroup.selected(), -1);
0080 
0081     kemptybuttongroup.setSelected(5);
0082     QCOMPARE(kemptybuttongroup.selected(), -1);
0083 
0084     QRadioButton radio1("Radio button 1");
0085     QCOMPARE(kemptybuttongroup.id(&radio1), -1);
0086 }
0087 
0088 void KButtonGroupTest::testId()
0089 {
0090     QCOMPARE(kbuttongroup->id(buttons[1]), 1);
0091 }
0092 
0093 void KButtonGroupTest::testClicks()
0094 {
0095     QTest::mouseClick(buttons[3], Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(), 10);
0096     QCOMPARE(kbuttongroup->selected(), 3);
0097 
0098     QTest::mouseClick(buttons[5], Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(), 10);
0099     QCOMPARE(kbuttongroup->selected(), 5);
0100 
0101     QTest::mouseClick(buttons[7], Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(), 10);
0102     QCOMPARE(kbuttongroup->selected(), 7);
0103 
0104     QTest::mouseClick(buttons[1], Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(), 10);
0105     QCOMPARE(kbuttongroup->selected(), 1);
0106 
0107     // QRadioButton's react only to LMB click events
0108     QTest::mouseClick(buttons[5], Qt::RightButton, Qt::KeyboardModifiers(), QPoint(), 10);
0109     QCOMPARE(kbuttongroup->selected(), 1);
0110     QTest::mouseClick(buttons[5], Qt::MidButton, Qt::KeyboardModifiers(), QPoint(), 10);
0111     QCOMPARE(kbuttongroup->selected(), 1);
0112 }
0113 
0114 void KButtonGroupTest::testManualSelection()
0115 {
0116     kbuttongroup->setSelected(3);
0117     QCOMPARE(kbuttongroup->selected(), 3);
0118 
0119     kbuttongroup->setSelected(0);
0120     QCOMPARE(kbuttongroup->selected(), 0);
0121 
0122     kbuttongroup->setSelected(7);
0123     QCOMPARE(kbuttongroup->selected(), 7);
0124 
0125     kbuttongroup->setSelected(2);
0126     QCOMPARE(kbuttongroup->selected(), 2);
0127 
0128     // "bad" cases: ask for an invalid id -- the selection should not change
0129     kbuttongroup->setSelected(10);
0130     QCOMPARE(kbuttongroup->selected(), 2);
0131     kbuttongroup->setSelected(-1);
0132     QCOMPARE(kbuttongroup->selected(), 2);
0133 }
0134 
0135 void KButtonGroupTest::testSignals()
0136 {
0137     QSignalSpy spyClicked(kbuttongroup, SIGNAL(clicked(int)));
0138     QSignalSpy spyPressed(kbuttongroup, SIGNAL(pressed(int)));
0139     QSignalSpy spyReleased(kbuttongroup, SIGNAL(released(int)));
0140     QSignalSpy spyChanged(kbuttongroup, SIGNAL(changed(int)));
0141 
0142     QTest::mouseClick(buttons[2], Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(), 10);
0143     QCOMPARE(spyClicked.count(), 1);
0144     QCOMPARE(spyPressed.count(), 1);
0145     QCOMPARE(spyReleased.count(), 1);
0146     QCOMPARE(spyChanged.count(), 1);
0147     QList<QVariant> args = spyClicked.last();
0148     QList<QVariant> args2 = spyPressed.last();
0149     QList<QVariant> args3 = spyReleased.last();
0150     QCOMPARE(args.first().toInt(), 2);
0151     QCOMPARE(args2.first().toInt(), 2);
0152     QCOMPARE(args3.first().toInt(), 2);
0153     QCOMPARE(kbuttongroup->selected(), 2);
0154 
0155     QTest::mouseClick(buttons[6], Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(), 10);
0156     QCOMPARE(spyClicked.count(), 2);
0157     QCOMPARE(spyPressed.count(), 2);
0158     QCOMPARE(spyReleased.count(), 2);
0159     QCOMPARE(spyChanged.count(), 2);
0160     args = spyClicked.last();
0161     args2 = spyPressed.last();
0162     args3 = spyReleased.last();
0163     QCOMPARE(args.first().toInt(), 6);
0164     QCOMPARE(args2.first().toInt(), 6);
0165     QCOMPARE(args3.first().toInt(), 6);
0166     QCOMPARE(kbuttongroup->selected(), 6);
0167 
0168     // click with RMB on a radio -> no signal
0169     QTest::mouseClick(buttons[0], Qt::RightButton, Qt::KeyboardModifiers(), QPoint(), 10);
0170     QCOMPARE(spyClicked.count(), 2);
0171     QCOMPARE(spyPressed.count(), 2);
0172     QCOMPARE(spyReleased.count(), 2);
0173     QCOMPARE(spyChanged.count(), 2);
0174     QCOMPARE(kbuttongroup->selected(), 6);
0175 
0176     // manual selections
0177     kbuttongroup->setSelected(7);
0178     QCOMPARE(spyChanged.count(), 3);
0179     QList<QVariant> args4 = spyChanged.last();
0180     QCOMPARE(args4.first().toInt(), 7);
0181     QCOMPARE(kbuttongroup->selected(), 7);
0182 
0183     kbuttongroup->setSelected(2);
0184     QCOMPARE(spyChanged.count(), 4);
0185     args4 = spyChanged.last();
0186     QCOMPARE(args4.first().toInt(), 2);
0187     QCOMPARE(kbuttongroup->selected(), 2);
0188 
0189     // "bad" cases: ask for an invalid id -- the selection should not change
0190     kbuttongroup->setSelected(10);
0191     QCOMPARE(spyChanged.count(), 4);
0192     QCOMPARE(kbuttongroup->selected(), 2);
0193 
0194     kbuttongroup->setSelected(-1);
0195     QCOMPARE(spyChanged.count(), 4);
0196     QCOMPARE(kbuttongroup->selected(), 2);
0197 }
0198 
0199 QTEST_MAIN(KButtonGroupTest)
0200