|
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 |
|
45 #include <qapplication.h> |
|
46 #include <qtoolbar.h> |
|
47 #include <qcombobox.h> |
|
48 #include <qwidgetaction.h> |
|
49 #include <qlabel.h> |
|
50 #include <qmenu.h> |
|
51 #include <qmainwindow.h> |
|
52 #include <qmenubar.h> |
|
53 |
|
54 //TESTED_CLASS= |
|
55 //TESTED_FILES= |
|
56 |
|
57 class tst_QWidgetAction : public QObject |
|
58 { |
|
59 Q_OBJECT |
|
60 private slots: |
|
61 void defaultWidget(); |
|
62 void visibilityUpdate(); |
|
63 void customWidget(); |
|
64 void keepOwnership(); |
|
65 void visibility(); |
|
66 void setEnabled(); |
|
67 void popup(); |
|
68 void releaseWidgetCrash(); |
|
69 }; |
|
70 |
|
71 void tst_QWidgetAction::defaultWidget() |
|
72 { |
|
73 { |
|
74 QToolBar tb1; |
|
75 |
|
76 QPointer<QComboBox> combo = new QComboBox(&tb1); |
|
77 |
|
78 QWidgetAction *action = new QWidgetAction(0); |
|
79 action->setDefaultWidget(combo); |
|
80 |
|
81 QVERIFY(!combo->isVisible()); |
|
82 QVERIFY(!combo->parent()); |
|
83 QVERIFY(action->isVisible()); |
|
84 |
|
85 delete action; |
|
86 QVERIFY(!combo); |
|
87 } |
|
88 { |
|
89 QToolBar tb1; |
|
90 |
|
91 QPointer<QComboBox> combo = new QComboBox(&tb1); |
|
92 combo->hide(); |
|
93 |
|
94 QWidgetAction *action = new QWidgetAction(0); |
|
95 action->setDefaultWidget(combo); |
|
96 |
|
97 // explicitly hidden widgets should also set the action invisible |
|
98 QVERIFY(!action->isVisible()); |
|
99 |
|
100 delete action; |
|
101 } |
|
102 { |
|
103 QPointer<QComboBox> combo = new QComboBox(0); |
|
104 combo->show(); |
|
105 |
|
106 QWidgetAction *action = new QWidgetAction(0); |
|
107 action->setDefaultWidget(combo); |
|
108 |
|
109 QVERIFY(action->isVisible()); |
|
110 QVERIFY(!combo->isVisible()); |
|
111 |
|
112 delete action; |
|
113 } |
|
114 { |
|
115 QToolBar tb1; |
|
116 tb1.show(); |
|
117 QToolBar tb2; |
|
118 tb2.show(); |
|
119 |
|
120 QPointer<QComboBox> combo = new QComboBox(0); |
|
121 |
|
122 QWidgetAction *action = new QWidgetAction(0); |
|
123 action->setDefaultWidget(combo); |
|
124 |
|
125 tb1.addAction(action); |
|
126 QVERIFY(combo->parent() == &tb1); |
|
127 qApp->processEvents(); |
|
128 qApp->processEvents(); |
|
129 QVERIFY(combo->isVisible()); |
|
130 |
|
131 // not supported, not supposed to work, hence the parent() check |
|
132 tb2.addAction(action); |
|
133 QVERIFY(combo->parent() == &tb1); |
|
134 |
|
135 tb2.removeAction(action); |
|
136 tb1.removeAction(action); |
|
137 |
|
138 qApp->processEvents(); //the call to hide is delayd by the toolbar layout |
|
139 QVERIFY(!combo->isVisible()); |
|
140 |
|
141 tb2.addAction(action); |
|
142 qApp->processEvents(); //the call to hide is delayd by the toolbar layout |
|
143 qApp->processEvents(); |
|
144 QVERIFY(combo->parent() == &tb2); |
|
145 QVERIFY(combo->isVisible()); |
|
146 |
|
147 tb1.addAction(action); |
|
148 QVERIFY(combo->parent() == &tb2); |
|
149 |
|
150 delete action; |
|
151 QVERIFY(!combo); |
|
152 } |
|
153 { |
|
154 QWidgetAction *a = new QWidgetAction(0); |
|
155 QVERIFY(!a->defaultWidget()); |
|
156 |
|
157 QPointer<QComboBox> combo1 = new QComboBox; |
|
158 a->setDefaultWidget(combo1); |
|
159 QVERIFY(a->defaultWidget() == combo1); |
|
160 a->setDefaultWidget(combo1); |
|
161 QVERIFY(combo1); |
|
162 QVERIFY(a->defaultWidget() == combo1); |
|
163 |
|
164 QPointer<QComboBox> combo2 = new QComboBox; |
|
165 QVERIFY(combo1 != combo2); |
|
166 |
|
167 a->setDefaultWidget(combo2); |
|
168 QVERIFY(!combo1); |
|
169 QVERIFY(a->defaultWidget() == combo2); |
|
170 |
|
171 delete a; |
|
172 QVERIFY(!combo2); |
|
173 } |
|
174 } |
|
175 |
|
176 void tst_QWidgetAction::visibilityUpdate() |
|
177 { |
|
178 // actually keeping the widget's state in sync with the |
|
179 // action in terms of visibility is QToolBar's responsibility. |
|
180 QToolBar tb; |
|
181 tb.show(); |
|
182 |
|
183 QComboBox *combo = new QComboBox(0); |
|
184 QWidgetAction *action = new QWidgetAction(0); |
|
185 action->setDefaultWidget(combo); |
|
186 |
|
187 tb.addAction(action); |
|
188 qApp->processEvents(); //the call to show is delayed by the toolbar layout |
|
189 QVERIFY(combo->isVisible()); |
|
190 QVERIFY(action->isVisible()); |
|
191 |
|
192 action->setVisible(false); |
|
193 QTest::qWait(100); //the call to hide is delayed by the toolbar layout |
|
194 QVERIFY(!combo->isVisible()); |
|
195 |
|
196 delete action; |
|
197 // action also deletes combo |
|
198 } |
|
199 |
|
200 class ComboAction : public QWidgetAction |
|
201 { |
|
202 public: |
|
203 inline ComboAction(QObject *parent) : QWidgetAction(parent) {} |
|
204 |
|
205 QList<QWidget *> createdWidgets() const { return QWidgetAction::createdWidgets(); } |
|
206 |
|
207 protected: |
|
208 virtual QWidget *createWidget(QWidget *parent); |
|
209 }; |
|
210 |
|
211 QWidget *ComboAction::createWidget(QWidget *parent) |
|
212 { |
|
213 return new QComboBox(parent); |
|
214 } |
|
215 |
|
216 void tst_QWidgetAction::customWidget() |
|
217 { |
|
218 QToolBar tb1; |
|
219 tb1.show(); |
|
220 QToolBar tb2; |
|
221 tb2.show(); |
|
222 |
|
223 ComboAction *action = new ComboAction(0); |
|
224 |
|
225 tb1.addAction(action); |
|
226 |
|
227 QList<QWidget *> combos = action->createdWidgets(); |
|
228 QCOMPARE(combos.count(), 1); |
|
229 |
|
230 QPointer<QComboBox> combo1 = qobject_cast<QComboBox *>(combos.at(0)); |
|
231 QVERIFY(combo1); |
|
232 |
|
233 tb2.addAction(action); |
|
234 |
|
235 combos = action->createdWidgets(); |
|
236 QCOMPARE(combos.count(), 2); |
|
237 |
|
238 QVERIFY(combos.at(0) == combo1); |
|
239 QPointer<QComboBox> combo2 = qobject_cast<QComboBox *>(combos.at(1)); |
|
240 QVERIFY(combo2); |
|
241 |
|
242 tb2.removeAction(action); |
|
243 QVERIFY(combo2); |
|
244 // widget is deleted using deleteLater(), so process that posted event |
|
245 QCoreApplication::sendPostedEvents(combo2, QEvent::DeferredDelete); |
|
246 QVERIFY(!combo2); |
|
247 |
|
248 delete action; |
|
249 QVERIFY(!combo1); |
|
250 QVERIFY(!combo2); |
|
251 } |
|
252 |
|
253 void tst_QWidgetAction::keepOwnership() |
|
254 { |
|
255 QPointer<QComboBox> combo = new QComboBox; |
|
256 QWidgetAction *action = new QWidgetAction(0); |
|
257 action->setDefaultWidget(combo); |
|
258 |
|
259 { |
|
260 QToolBar *tb = new QToolBar; |
|
261 tb->addAction(action); |
|
262 QVERIFY(combo->parent() == tb); |
|
263 delete tb; |
|
264 } |
|
265 |
|
266 QVERIFY(combo); |
|
267 delete action; |
|
268 QVERIFY(!combo); |
|
269 } |
|
270 |
|
271 void tst_QWidgetAction::visibility() |
|
272 { |
|
273 { |
|
274 QWidgetAction *a = new QWidgetAction(0); |
|
275 QComboBox *combo = new QComboBox; |
|
276 a->setDefaultWidget(combo); |
|
277 |
|
278 QToolBar *tb = new QToolBar; |
|
279 tb->addAction(a); |
|
280 QVERIFY(!combo->isVisible()); |
|
281 tb->show(); |
|
282 QVERIFY(combo->isVisible()); |
|
283 |
|
284 delete tb; |
|
285 |
|
286 delete a; |
|
287 } |
|
288 { |
|
289 QWidgetAction *a = new QWidgetAction(0); |
|
290 QComboBox *combo = new QComboBox; |
|
291 a->setDefaultWidget(combo); |
|
292 |
|
293 QToolBar *tb = new QToolBar; |
|
294 tb->addAction(a); |
|
295 QVERIFY(!combo->isVisible()); |
|
296 |
|
297 QToolBar *tb2 = new QToolBar; |
|
298 tb->removeAction(a); |
|
299 tb2->addAction(a); |
|
300 QVERIFY(!combo->isVisible()); |
|
301 tb2->show(); |
|
302 QVERIFY(combo->isVisible()); |
|
303 |
|
304 delete tb; |
|
305 delete tb2; |
|
306 |
|
307 delete a; |
|
308 } |
|
309 } |
|
310 |
|
311 void tst_QWidgetAction::setEnabled() |
|
312 { |
|
313 QToolBar toolbar; |
|
314 QComboBox *combobox = new QComboBox; |
|
315 QAction *action = toolbar.addWidget(combobox); |
|
316 toolbar.show(); |
|
317 |
|
318 QVERIFY(action->isEnabled()); |
|
319 QVERIFY(combobox->isEnabled()); |
|
320 |
|
321 action->setEnabled(false); |
|
322 QVERIFY(!action->isEnabled()); |
|
323 QVERIFY(!combobox->isEnabled()); |
|
324 |
|
325 action->setEnabled(true); |
|
326 QVERIFY(action->isEnabled()); |
|
327 QVERIFY(combobox->isEnabled()); |
|
328 |
|
329 combobox->setEnabled(false); |
|
330 QVERIFY(!combobox->isEnabled()); |
|
331 |
|
332 combobox->setEnabled(true); |
|
333 QVERIFY(action->isEnabled()); |
|
334 QVERIFY(combobox->isEnabled()); |
|
335 |
|
336 |
|
337 QWidgetAction aw(0); |
|
338 aw.setEnabled(false); |
|
339 QVERIFY(!aw.isEnabled()); |
|
340 |
|
341 combobox = new QComboBox; |
|
342 aw.setDefaultWidget(combobox); |
|
343 QVERIFY(!aw.isEnabled()); |
|
344 QVERIFY(!combobox->isEnabled()); |
|
345 |
|
346 // Make sure we don't change the default widget's Qt::WA_ForceDisabled attribute |
|
347 // during a normal disable/enable operation (task 207433). |
|
348 { |
|
349 QToolBar toolBar; |
|
350 QWidget widget; |
|
351 toolBar.addWidget(&widget); // creates a QWidgetAction and sets 'widget' as the default widget. |
|
352 QVERIFY(!widget.testAttribute(Qt::WA_ForceDisabled)); |
|
353 |
|
354 toolBar.setEnabled(false); |
|
355 QVERIFY(toolBar.testAttribute(Qt::WA_ForceDisabled)); |
|
356 QVERIFY(!widget.isEnabled()); |
|
357 QVERIFY(!widget.testAttribute(Qt::WA_ForceDisabled)); |
|
358 |
|
359 toolBar.setEnabled(true); |
|
360 QVERIFY(widget.isEnabled()); |
|
361 QVERIFY(!widget.testAttribute(Qt::WA_ForceDisabled)); |
|
362 } |
|
363 } |
|
364 |
|
365 void tst_QWidgetAction::popup() |
|
366 { |
|
367 QPointer<QLabel> l = new QLabel("test"); |
|
368 QWidgetAction action(0); |
|
369 action.setDefaultWidget(l); |
|
370 |
|
371 { |
|
372 QMenu menu; |
|
373 menu.addAction(&action); |
|
374 QTimer::singleShot(0, &menu, SLOT(close())); |
|
375 menu.exec(); |
|
376 } |
|
377 |
|
378 QVERIFY(!l.isNull()); |
|
379 delete l; |
|
380 } |
|
381 |
|
382 class CrashedAction : public QWidgetAction |
|
383 { |
|
384 public: |
|
385 inline CrashedAction(QObject *parent) : QWidgetAction(parent) { } |
|
386 |
|
387 virtual QWidget *createWidget(QWidget *parent) { |
|
388 return new QWidget(parent); |
|
389 } |
|
390 }; |
|
391 |
|
392 void tst_QWidgetAction::releaseWidgetCrash() |
|
393 { |
|
394 // this should not crash! |
|
395 QMainWindow *w = new QMainWindow; |
|
396 QAction *a = new CrashedAction(w); |
|
397 QMenu *menu = w->menuBar()->addMenu("Test"); |
|
398 menu->addAction(a); |
|
399 delete w; |
|
400 } |
|
401 |
|
402 QTEST_MAIN(tst_QWidgetAction) |
|
403 #include "tst_qwidgetaction.moc" |