1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QtTest>
30
31#include <qmainwindow.h>
32#include <qmenu.h>
33#include <qaction.h>
34
35class tst_QActionGroup : public QObject
36{
37 Q_OBJECT
38
39private slots:
40 void cleanup() { QVERIFY(QApplication::topLevelWidgets().isEmpty()); }
41 void enabledPropagation();
42 void visiblePropagation();
43 void exclusive();
44 void exclusiveOptional();
45 void separators();
46 void testActionInTwoQActionGroup();
47 void unCheckCurrentAction();
48};
49
50void tst_QActionGroup::enabledPropagation()
51{
52 QActionGroup testActionGroup(nullptr);
53
54 QAction* childAction = new QAction( &testActionGroup );
55 QAction* anotherChildAction = new QAction( &testActionGroup );
56 QAction* freeAction = new QAction(nullptr);
57
58 QVERIFY( testActionGroup.isEnabled() );
59 QVERIFY( childAction->isEnabled() );
60
61 testActionGroup.setEnabled( false );
62 QVERIFY( !testActionGroup.isEnabled() );
63 QVERIFY( !childAction->isEnabled() );
64 QVERIFY( !anotherChildAction->isEnabled() );
65
66 childAction->setEnabled(true);
67 QVERIFY( !childAction->isEnabled());
68
69 anotherChildAction->setEnabled( false );
70
71 testActionGroup.setEnabled( true );
72 QVERIFY( testActionGroup.isEnabled() );
73 QVERIFY( childAction->isEnabled() );
74 QVERIFY( !anotherChildAction->isEnabled() );
75
76 testActionGroup.setEnabled( false );
77 QAction *lastChildAction = new QAction(&testActionGroup);
78
79 QVERIFY(!lastChildAction->isEnabled());
80 testActionGroup.setEnabled( true );
81 QVERIFY(lastChildAction->isEnabled());
82
83 freeAction->setEnabled(false);
84 testActionGroup.addAction(a: freeAction);
85 QVERIFY(!freeAction->isEnabled());
86 delete freeAction;
87}
88
89void tst_QActionGroup::visiblePropagation()
90{
91 QActionGroup testActionGroup(nullptr);
92
93 QAction* childAction = new QAction( &testActionGroup );
94 QAction* anotherChildAction = new QAction( &testActionGroup );
95 QAction* freeAction = new QAction(nullptr);
96
97 QVERIFY( testActionGroup.isVisible() );
98 QVERIFY( childAction->isVisible() );
99
100 testActionGroup.setVisible( false );
101 QVERIFY( !testActionGroup.isVisible() );
102 QVERIFY( !childAction->isVisible() );
103 QVERIFY( !anotherChildAction->isVisible() );
104
105 anotherChildAction->setVisible(false);
106
107 testActionGroup.setVisible( true );
108 QVERIFY( testActionGroup.isVisible() );
109 QVERIFY( childAction->isVisible() );
110
111 QVERIFY( !anotherChildAction->isVisible() );
112
113 testActionGroup.setVisible( false );
114 QAction *lastChildAction = new QAction(&testActionGroup);
115
116 QVERIFY(!lastChildAction->isVisible());
117 testActionGroup.setVisible( true );
118 QVERIFY(lastChildAction->isVisible());
119
120 freeAction->setVisible(false);
121 testActionGroup.addAction(a: freeAction);
122 QVERIFY(!freeAction->isVisible());
123 delete freeAction;
124}
125
126void tst_QActionGroup::exclusive()
127{
128 QActionGroup group(nullptr);
129 group.setExclusive(false);
130 QVERIFY( !group.isExclusive() );
131
132 QAction* actOne = new QAction( &group );
133 actOne->setCheckable( true );
134 QAction* actTwo = new QAction( &group );
135 actTwo->setCheckable( true );
136 QAction* actThree = new QAction( &group );
137 actThree->setCheckable( true );
138
139 group.setExclusive( true );
140 QVERIFY( !actOne->isChecked() );
141 QVERIFY( !actTwo->isChecked() );
142 QVERIFY( !actThree->isChecked() );
143
144 actOne->setChecked( true );
145 QVERIFY( actOne->isChecked() );
146 QVERIFY( !actTwo->isChecked() );
147 QVERIFY( !actThree->isChecked() );
148
149 actTwo->setChecked( true );
150 QVERIFY( !actOne->isChecked() );
151 QVERIFY( actTwo->isChecked() );
152 QVERIFY( !actThree->isChecked() );
153}
154
155void tst_QActionGroup::exclusiveOptional()
156{
157 QActionGroup group(0);
158 group.setExclusive(true);
159 QVERIFY( group.isExclusive() );
160
161 QAction* actOne = new QAction( &group );
162 actOne->setCheckable( true );
163 QAction* actTwo = new QAction( &group );
164 actTwo->setCheckable( true );
165 QAction* actThree = new QAction( &group );
166 actThree->setCheckable( true );
167
168 QVERIFY( !actOne->isChecked() );
169 QVERIFY( !actTwo->isChecked() );
170 QVERIFY( !actThree->isChecked() );
171
172 actOne->trigger();
173 QVERIFY( actOne->isChecked() );
174 QVERIFY( !actTwo->isChecked() );
175 QVERIFY( !actThree->isChecked() );
176
177 actOne->trigger();
178 QVERIFY( actOne->isChecked() );
179 QVERIFY( !actTwo->isChecked() );
180 QVERIFY( !actThree->isChecked() );
181
182 group.setExclusionPolicy( QActionGroup::ExclusionPolicy::ExclusiveOptional );
183 QVERIFY( group.isExclusive() );
184
185 actOne->trigger();
186 QVERIFY( !actOne->isChecked() );
187 QVERIFY( !actTwo->isChecked() );
188 QVERIFY( !actThree->isChecked() );
189
190 actTwo->trigger();
191 QVERIFY( !actOne->isChecked() );
192 QVERIFY( actTwo->isChecked() );
193 QVERIFY( !actThree->isChecked() );
194
195 actTwo->trigger();
196 QVERIFY( !actOne->isChecked() );
197 QVERIFY( !actTwo->isChecked() );
198 QVERIFY( !actThree->isChecked() );
199}
200
201void tst_QActionGroup::separators()
202{
203 QMainWindow mw;
204 QMenu menu(&mw);
205 QActionGroup actGroup(&mw);
206
207 mw.show();
208
209 QAction *action = new QAction(&actGroup);
210 action->setText("test one");
211
212 QAction *separator = new QAction(&actGroup);
213 separator->setSeparator(true);
214 actGroup.addAction(a: separator);
215
216 menu.addActions(actions: actGroup.actions());
217
218 QCOMPARE(menu.actions().size(), 2);
219
220 const auto removeActions = [&menu](const QList<QAction *> &actions) {
221 for (QAction *action : actions)
222 menu.removeAction(action);
223 };
224 removeActions(actGroup.actions());
225
226 QCOMPARE(menu.actions().size(), 0);
227
228 action = new QAction(&actGroup);
229 action->setText("test two");
230
231 menu.addActions(actions: actGroup.actions());
232
233 QCOMPARE(menu.actions().size(), 3);
234}
235
236void tst_QActionGroup::testActionInTwoQActionGroup()
237{
238 QAction action1("Action 1", this);
239
240 QActionGroup group1(this);
241 QActionGroup group2(this);
242
243 group1.addAction(a: &action1);
244 group2.addAction(a: &action1);
245
246 QCOMPARE(action1.actionGroup(), &group2);
247 QCOMPARE(group2.actions().first(), &action1);
248 QCOMPARE(group1.actions().isEmpty(), true);
249}
250
251void tst_QActionGroup::unCheckCurrentAction()
252{
253 QActionGroup group(nullptr);
254 QAction action1(&group) ,action2(&group);
255 action1.setCheckable(true);
256 action2.setCheckable(true);
257 QVERIFY(!action1.isChecked());
258 QVERIFY(!action2.isChecked());
259 action1.setChecked(true);
260 QVERIFY(action1.isChecked());
261 QVERIFY(!action2.isChecked());
262 QAction *current = group.checkedAction();
263 QCOMPARE(current, &action1);
264 current->setChecked(false);
265 QVERIFY(!action1.isChecked());
266 QVERIFY(!action2.isChecked());
267 QVERIFY(!group.checkedAction());
268}
269
270
271QTEST_MAIN(tst_QActionGroup)
272#include "tst_qactiongroup.moc"
273

source code of qtbase/tests/auto/widgets/kernel/qactiongroup/tst_qactiongroup.cpp