1 /*
2 This file is part of the KDE Libraries
3
4 Copyright (C) 2006 Pino Toscano <toscano.pino@tiscali.it>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20 */
21
22 #include <QTest>
23
24 #include <QTestEvent>
25
26 #include <QList>
27 #include <QRadioButton>
28 #include <QSignalSpy>
29 #include <QBoxLayout>
30
31 #include "kbuttongrouptest.h"
32 #include "kbuttongroup.h"
33
34 KButtonGroup *kbuttongroup;
35 QList<QRadioButton *> buttons;
36
initTestCase()37 void KButtonGroupTest::initTestCase()
38 {
39 kbuttongroup = new KButtonGroup();
40 QVBoxLayout *lay2 = new QVBoxLayout(kbuttongroup);
41
42 for (int i = 0; i < 8; ++i) {
43 QRadioButton *r = new QRadioButton(kbuttongroup);
44 r->setText(QString("radio%1").arg(i));
45 lay2->addWidget(r);
46 buttons << r;
47 }
48
49 QCOMPARE(kbuttongroup->selected(), -1);
50 }
51
directSelectionTestCase()52 void KButtonGroupTest::directSelectionTestCase()
53 {
54 // test where setSelected is called before the
55 // ensurePolished() is called.
56 KButtonGroup *kbuttongroup2 = new KButtonGroup();
57 kbuttongroup2->setSelected(3);
58
59 QVBoxLayout *lay2 = new QVBoxLayout(kbuttongroup2);
60 for (int i = 0; i < 8; ++i) {
61 QRadioButton *r = new QRadioButton(kbuttongroup2);
62 r->setText(QString("radio%1").arg(i));
63 lay2->addWidget(r);
64 buttons << r;
65 }
66
67 QTest::qWait(250); // events should occur.
68 QCOMPARE(kbuttongroup2->selected(), 3);
69 }
70
cleanupTestCase()71 void KButtonGroupTest::cleanupTestCase()
72 {
73 kbuttongroup->deleteLater();
74 }
75
testEmptyGroup()76 void KButtonGroupTest::testEmptyGroup()
77 {
78 KButtonGroup kemptybuttongroup;
79 QCOMPARE(kemptybuttongroup.selected(), -1);
80
81 kemptybuttongroup.setSelected(5);
82 QCOMPARE(kemptybuttongroup.selected(), -1);
83
84 QRadioButton radio1("Radio button 1");
85 QCOMPARE(kemptybuttongroup.id(&radio1), -1);
86 }
87
testId()88 void KButtonGroupTest::testId()
89 {
90 QCOMPARE(kbuttongroup->id(buttons[1]), 1);
91 }
92
testClicks()93 void KButtonGroupTest::testClicks()
94 {
95 QTest::mouseClick(buttons[3], Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(), 10);
96 QCOMPARE(kbuttongroup->selected(), 3);
97
98 QTest::mouseClick(buttons[5], Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(), 10);
99 QCOMPARE(kbuttongroup->selected(), 5);
100
101 QTest::mouseClick(buttons[7], Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(), 10);
102 QCOMPARE(kbuttongroup->selected(), 7);
103
104 QTest::mouseClick(buttons[1], Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(), 10);
105 QCOMPARE(kbuttongroup->selected(), 1);
106
107 // QRadioButton's react only to LMB click events
108 QTest::mouseClick(buttons[5], Qt::RightButton, Qt::KeyboardModifiers(), QPoint(), 10);
109 QCOMPARE(kbuttongroup->selected(), 1);
110 QTest::mouseClick(buttons[5], Qt::MidButton, Qt::KeyboardModifiers(), QPoint(), 10);
111 QCOMPARE(kbuttongroup->selected(), 1);
112 }
113
testManualSelection()114 void KButtonGroupTest::testManualSelection()
115 {
116 kbuttongroup->setSelected(3);
117 QCOMPARE(kbuttongroup->selected(), 3);
118
119 kbuttongroup->setSelected(0);
120 QCOMPARE(kbuttongroup->selected(), 0);
121
122 kbuttongroup->setSelected(7);
123 QCOMPARE(kbuttongroup->selected(), 7);
124
125 kbuttongroup->setSelected(2);
126 QCOMPARE(kbuttongroup->selected(), 2);
127
128 // "bad" cases: ask for an invalid id -- the selection should not change
129 kbuttongroup->setSelected(10);
130 QCOMPARE(kbuttongroup->selected(), 2);
131 kbuttongroup->setSelected(-1);
132 QCOMPARE(kbuttongroup->selected(), 2);
133 }
134
testSignals()135 void KButtonGroupTest::testSignals()
136 {
137 QSignalSpy spyClicked(kbuttongroup, SIGNAL(clicked(int)));
138 QSignalSpy spyPressed(kbuttongroup, SIGNAL(pressed(int)));
139 QSignalSpy spyReleased(kbuttongroup, SIGNAL(released(int)));
140 QSignalSpy spyChanged(kbuttongroup, SIGNAL(changed(int)));
141
142 QTest::mouseClick(buttons[2], Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(), 10);
143 QCOMPARE(spyClicked.count(), 1);
144 QCOMPARE(spyPressed.count(), 1);
145 QCOMPARE(spyReleased.count(), 1);
146 QCOMPARE(spyChanged.count(), 1);
147 QList<QVariant> args = spyClicked.last();
148 QList<QVariant> args2 = spyPressed.last();
149 QList<QVariant> args3 = spyReleased.last();
150 QCOMPARE(args.first().toInt(), 2);
151 QCOMPARE(args2.first().toInt(), 2);
152 QCOMPARE(args3.first().toInt(), 2);
153 QCOMPARE(kbuttongroup->selected(), 2);
154
155 QTest::mouseClick(buttons[6], Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(), 10);
156 QCOMPARE(spyClicked.count(), 2);
157 QCOMPARE(spyPressed.count(), 2);
158 QCOMPARE(spyReleased.count(), 2);
159 QCOMPARE(spyChanged.count(), 2);
160 args = spyClicked.last();
161 args2 = spyPressed.last();
162 args3 = spyReleased.last();
163 QCOMPARE(args.first().toInt(), 6);
164 QCOMPARE(args2.first().toInt(), 6);
165 QCOMPARE(args3.first().toInt(), 6);
166 QCOMPARE(kbuttongroup->selected(), 6);
167
168 // click with RMB on a radio -> no signal
169 QTest::mouseClick(buttons[0], Qt::RightButton, Qt::KeyboardModifiers(), QPoint(), 10);
170 QCOMPARE(spyClicked.count(), 2);
171 QCOMPARE(spyPressed.count(), 2);
172 QCOMPARE(spyReleased.count(), 2);
173 QCOMPARE(spyChanged.count(), 2);
174 QCOMPARE(kbuttongroup->selected(), 6);
175
176 // manual selections
177 kbuttongroup->setSelected(7);
178 QCOMPARE(spyChanged.count(), 3);
179 QList<QVariant> args4 = spyChanged.last();
180 QCOMPARE(args4.first().toInt(), 7);
181 QCOMPARE(kbuttongroup->selected(), 7);
182
183 kbuttongroup->setSelected(2);
184 QCOMPARE(spyChanged.count(), 4);
185 args4 = spyChanged.last();
186 QCOMPARE(args4.first().toInt(), 2);
187 QCOMPARE(kbuttongroup->selected(), 2);
188
189 // "bad" cases: ask for an invalid id -- the selection should not change
190 kbuttongroup->setSelected(10);
191 QCOMPARE(spyChanged.count(), 4);
192 QCOMPARE(kbuttongroup->selected(), 2);
193
194 kbuttongroup->setSelected(-1);
195 QCOMPARE(spyChanged.count(), 4);
196 QCOMPARE(kbuttongroup->selected(), 2);
197 }
198
199 QTEST_MAIN(KButtonGroupTest)
200
201