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