|
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 #include <qapplication.h> |
|
45 #include <qmainwindow.h> |
|
46 #include <qmenubar.h> |
|
47 #ifdef QT3_SUPPORT |
|
48 #include <q3popupmenu.h> |
|
49 #endif |
|
50 #include <qstyle.h> |
|
51 #include <qwindowsstyle.h> |
|
52 #include <qdesktopwidget.h> |
|
53 #include <qaction.h> |
|
54 #include <qstyleoption.h> |
|
55 |
|
56 #ifdef Q_WS_WIN |
|
57 #include <windows.h> |
|
58 #endif |
|
59 |
|
60 #include <qobject.h> |
|
61 |
|
62 #include "../../shared/util.h" |
|
63 |
|
64 QT_FORWARD_DECLARE_CLASS(QMainWindow) |
|
65 |
|
66 #include <qmenubar.h> |
|
67 |
|
68 //TESTED_CLASS= |
|
69 //TESTED_FILES= |
|
70 |
|
71 class QtTestSlot : public QObject |
|
72 { |
|
73 Q_OBJECT |
|
74 |
|
75 public: |
|
76 QtTestSlot( QObject* parent = 0 ): QObject( parent ) { clear(); }; |
|
77 virtual ~QtTestSlot() {}; |
|
78 |
|
79 void clear() { sel_count = 0; }; |
|
80 uint selCount() { return sel_count; }; |
|
81 |
|
82 public slots: |
|
83 void selected() { sel_count++; }; |
|
84 |
|
85 private: |
|
86 uint sel_count; |
|
87 }; |
|
88 |
|
89 class tst_QMenuBar : public QObject |
|
90 { |
|
91 Q_OBJECT |
|
92 public: |
|
93 tst_QMenuBar(); |
|
94 virtual ~tst_QMenuBar(); |
|
95 |
|
96 #ifdef QT3_SUPPORT |
|
97 void initSimpleMenubar(); |
|
98 #endif |
|
99 void initSimpleMenubar_noQt3(); |
|
100 |
|
101 #ifdef QT3_SUPPORT |
|
102 void initComplexMenubar(); |
|
103 #endif |
|
104 void initComplexMenubar_noQt3(); |
|
105 |
|
106 public slots: |
|
107 void initTestCase(); |
|
108 void cleanupTestCase(); |
|
109 void init(); |
|
110 private slots: |
|
111 void getSetCheck(); |
|
112 |
|
113 void clear_noQt3(); |
|
114 void removeItemAt_noQt3(); |
|
115 void removeItemAt_noQt3_data(); |
|
116 void removeItem_noQt3_data(); |
|
117 void removeItem_noQt3(); |
|
118 void count_noQt3(); |
|
119 void insertItem_QString_QObject_noQt3(); |
|
120 void accel_noQt3(); |
|
121 void activatedCount_noQt3(); |
|
122 void allowActiveAndDisabled(); |
|
123 #ifdef QT3_SUPPORT |
|
124 void clear(); |
|
125 void removeItemAt_data(); |
|
126 void removeItemAt(); |
|
127 void removeItem_data(); |
|
128 void removeItem(); |
|
129 void count(); |
|
130 void insertItem_QString_QObject(); |
|
131 void accel(); |
|
132 void activatedCount(); |
|
133 #endif |
|
134 |
|
135 void check_accelKeys(); |
|
136 void check_cursorKeys1(); |
|
137 void check_cursorKeys2(); |
|
138 void check_cursorKeys3(); |
|
139 |
|
140 void check_homeKey(); |
|
141 void check_endKey(); |
|
142 #ifdef QT3_SUPPORT |
|
143 void check_escKey(); |
|
144 #endif |
|
145 void check_escKey_noQt3(); |
|
146 |
|
147 // void check_mouse1_data(); |
|
148 // void check_mouse1(); |
|
149 // void check_mouse2_data(); |
|
150 // void check_mouse2(); |
|
151 |
|
152 void check_altPress(); |
|
153 void check_shortcutPress(); |
|
154 void check_menuPosition(); |
|
155 void task223138_triggered(); |
|
156 void task256322_highlight(); |
|
157 void menubarSizeHint(); |
|
158 |
|
159 #if defined(QT3_SUPPORT) |
|
160 void indexBasedInsertion_data(); |
|
161 void indexBasedInsertion(); |
|
162 #endif |
|
163 |
|
164 protected slots: |
|
165 #ifdef QT3_SUPPORT |
|
166 void onActivated( int ); |
|
167 #endif |
|
168 void onActivated_noQt3( QAction*); |
|
169 |
|
170 private: |
|
171 |
|
172 void initTestCase_noQt3(); |
|
173 |
|
174 QtTestSlot *menu1; |
|
175 QtTestSlot *menu2; |
|
176 QtTestSlot *menu3; |
|
177 QtTestSlot *menu4; |
|
178 |
|
179 QtTestSlot *item1_A; |
|
180 QtTestSlot *item1_B; |
|
181 QtTestSlot *item2_C; |
|
182 QtTestSlot *item2_D; |
|
183 QtTestSlot *item2_E; |
|
184 QtTestSlot *item2_F; |
|
185 QtTestSlot *item2_G; |
|
186 QtTestSlot *item2_H; |
|
187 |
|
188 void resetSlots(); |
|
189 void resetCount(); |
|
190 |
|
191 void reset() { resetSlots(); resetCount(); }; |
|
192 |
|
193 #ifdef QT3_SUPPORT |
|
194 int last_accel_id; |
|
195 #endif |
|
196 QAction* last_accel_id_Qt4; |
|
197 int activated_count; |
|
198 |
|
199 #ifdef QT3_SUPPORT |
|
200 int idAccel; |
|
201 int idAccel1; |
|
202 #endif |
|
203 QAction *action; |
|
204 QAction *action1; |
|
205 QMainWindow *mw; |
|
206 QMenuBar *mb; |
|
207 #ifdef QT3_SUPPORT |
|
208 Q3PopupMenu *pm1; |
|
209 Q3PopupMenu *pm2; |
|
210 #endif |
|
211 QMenu *pm1_Qt4; |
|
212 QMenu *pm2_Qt4; |
|
213 }; |
|
214 |
|
215 // Testing get/set functions |
|
216 void tst_QMenuBar::getSetCheck() |
|
217 { |
|
218 QMenuBar obj1; |
|
219 // QAction * QMenuBar::activeAction() |
|
220 // void QMenuBar::setActiveAction(QAction *) |
|
221 QAction *var1 = new QAction(0); |
|
222 obj1.setActiveAction(var1); |
|
223 QCOMPARE(var1, obj1.activeAction()); |
|
224 obj1.setActiveAction((QAction *)0); |
|
225 QCOMPARE((QAction *)0, obj1.activeAction()); |
|
226 delete var1; |
|
227 } |
|
228 |
|
229 //// |
|
230 |
|
231 |
|
232 |
|
233 #include <qcursor.h> |
|
234 |
|
235 const int RESET = 0; |
|
236 |
|
237 /*! |
|
238 Test plan: |
|
239 insertItem (all flavours and combinations) |
|
240 removing menu items |
|
241 clearing the menu |
|
242 |
|
243 check the common behaviour + emitted signals for: |
|
244 accelerator keys |
|
245 navigating tru the menu and then pressing ENTER |
|
246 mouse clicks |
|
247 mouse drags |
|
248 combinations of key + mouse (if possible) |
|
249 checked / unckecked state of menu options |
|
250 active / inactive state |
|
251 |
|
252 Can't test these without pixmap comparison... |
|
253 show and hide |
|
254 icons in a menu |
|
255 pixmaps in a menu |
|
256 |
|
257 */ |
|
258 |
|
259 tst_QMenuBar::tst_QMenuBar() |
|
260 |
|
261 { |
|
262 QApplication::setEffectEnabled(Qt::UI_AnimateMenu, false); |
|
263 |
|
264 |
|
265 activated_count = 0; |
|
266 mb = 0; |
|
267 #ifdef QT3_SUPPORT |
|
268 pm1 = 0; |
|
269 pm2 = 0; |
|
270 last_accel_id = RESET; |
|
271 #endif |
|
272 pm1_Qt4 = 0; |
|
273 pm2_Qt4 = 0; |
|
274 last_accel_id_Qt4 = 0; |
|
275 } |
|
276 |
|
277 tst_QMenuBar::~tst_QMenuBar() |
|
278 { |
|
279 //delete mw; //#### cannot do that AFTER qapplication was destroyed! |
|
280 mw = 0; |
|
281 } |
|
282 |
|
283 void tst_QMenuBar::initTestCase() |
|
284 { |
|
285 #ifdef QT3_SUPPORT |
|
286 // create a default mainwindow |
|
287 // If you run a widget test, this will be replaced in the testcase by the |
|
288 // widget under test |
|
289 mw = new QMainWindow(0, Qt::X11BypassWindowManagerHint); |
|
290 mb = new QMenuBar( mw, "menubar" ); |
|
291 connect( mb, SIGNAL(activated(int)), this, SLOT(onActivated(int)) ); |
|
292 |
|
293 initSimpleMenubar(); |
|
294 |
|
295 qApp->setMainWidget( mw ); |
|
296 mw->show(); |
|
297 qApp->setActiveWindow(mw); |
|
298 |
|
299 menu1 = new QtTestSlot( mw ); |
|
300 menu2 = new QtTestSlot( mw ); |
|
301 menu3 = new QtTestSlot( mw ); |
|
302 menu4 = new QtTestSlot( mw ); |
|
303 item1_A = new QtTestSlot( mw ); |
|
304 item1_B = new QtTestSlot( mw ); |
|
305 item2_C = new QtTestSlot( mw ); |
|
306 item2_D = new QtTestSlot( mw ); |
|
307 item2_E = new QtTestSlot( mw ); |
|
308 item2_F = new QtTestSlot( mw ); |
|
309 item2_G = new QtTestSlot( mw ); |
|
310 item2_H = new QtTestSlot( mw ); |
|
311 #else |
|
312 initTestCase_noQt3(); |
|
313 #endif |
|
314 } |
|
315 |
|
316 void tst_QMenuBar::initTestCase_noQt3() |
|
317 { |
|
318 // create a default mainwindow |
|
319 // If you run a widget test, this will be replaced in the testcase by the |
|
320 // widget under test |
|
321 mw = new QMainWindow(0, Qt::X11BypassWindowManagerHint); |
|
322 mb = new QMenuBar( mw ); |
|
323 connect( mb, SIGNAL(triggered(QAction *)), this, SLOT(onActivated_noQt3(QAction *)) ); |
|
324 |
|
325 initSimpleMenubar_noQt3(); |
|
326 mw->show(); |
|
327 |
|
328 menu1 = new QtTestSlot( mw ); |
|
329 menu2 = new QtTestSlot( mw ); |
|
330 menu3 = new QtTestSlot( mw ); |
|
331 menu4 = new QtTestSlot( mw ); |
|
332 item1_A = new QtTestSlot( mw ); |
|
333 item1_B = new QtTestSlot( mw ); |
|
334 item2_C = new QtTestSlot( mw ); |
|
335 item2_D = new QtTestSlot( mw ); |
|
336 item2_E = new QtTestSlot( mw ); |
|
337 item2_F = new QtTestSlot( mw ); |
|
338 item2_G = new QtTestSlot( mw ); |
|
339 item2_H = new QtTestSlot( mw ); |
|
340 } |
|
341 |
|
342 |
|
343 void tst_QMenuBar::cleanupTestCase() |
|
344 { |
|
345 delete mw; |
|
346 } |
|
347 |
|
348 #if defined(QT3_SUPPORT) |
|
349 void tst_QMenuBar::initSimpleMenubar() |
|
350 { |
|
351 mb->hide(); |
|
352 mb->clear(); |
|
353 |
|
354 delete pm1; |
|
355 pm1 = new Q3PopupMenu( mb ); |
|
356 idAccel = pm1->insertItem( "menu1", 123 ); |
|
357 // pm->setAccel( ALT + Key_A, idAccel ); |
|
358 pm1->setAccel( Qt::CTRL + Qt::Key_A, idAccel ); |
|
359 mb->insertItem( "&accel", pm1 ); |
|
360 connect( pm1, SIGNAL(activated(int)), this, SLOT(onActivated(int))); |
|
361 |
|
362 delete pm2; |
|
363 pm2 = new Q3PopupMenu( mb ); |
|
364 // idAccel1 = pm2->insertItem( "&Open...", this, SLOT(onActivated(int)), Qt::Key_O, 456 ); |
|
365 idAccel1 = pm2->insertItem( "&Open...", 0, 0, Qt::Key_O, 456 ); |
|
366 connect(pm2, SIGNAL(activated(int)), this, SLOT(onActivated(int))); |
|
367 mb->insertItem( "accel1", pm2 ); |
|
368 |
|
369 mb->show(); |
|
370 qApp->syncX(); |
|
371 qApp->processEvents(); |
|
372 } |
|
373 #endif |
|
374 |
|
375 void tst_QMenuBar::initSimpleMenubar_noQt3() |
|
376 { |
|
377 mb->hide(); |
|
378 mb->clear(); |
|
379 |
|
380 delete pm1_Qt4; |
|
381 pm1_Qt4 = mb->addMenu("&accel"); |
|
382 action = pm1_Qt4->addAction( "menu1" ); |
|
383 action->setShortcut(QKeySequence("ALT+A")); |
|
384 action->setShortcut(QKeySequence("CTRL+A")); |
|
385 |
|
386 |
|
387 connect( pm1_Qt4, SIGNAL(triggered(QAction*)), this, SLOT(onActivated_noQt3(QAction*))); |
|
388 |
|
389 delete pm2_Qt4; |
|
390 pm2_Qt4 = mb->addMenu("accel1"); |
|
391 |
|
392 action1 = pm2_Qt4->addAction( "&Open..." ); |
|
393 action1->setShortcut(Qt::Key_O); |
|
394 connect(pm2_Qt4, SIGNAL(triggered(QAction*)), this, SLOT(onActivated_noQt3(QAction*))); |
|
395 |
|
396 mb->show(); |
|
397 qApp->syncX(); |
|
398 qApp->processEvents(); |
|
399 } |
|
400 |
|
401 void tst_QMenuBar::init() |
|
402 { |
|
403 resetSlots(); |
|
404 resetCount(); |
|
405 } |
|
406 |
|
407 void tst_QMenuBar::resetSlots() |
|
408 { |
|
409 menu1->clear(); |
|
410 menu2->clear(); |
|
411 menu3->clear(); |
|
412 menu4->clear(); |
|
413 item1_A->clear(); |
|
414 item1_B->clear(); |
|
415 item2_C->clear(); |
|
416 item2_D->clear(); |
|
417 item2_E->clear(); |
|
418 item2_F->clear(); |
|
419 item2_G->clear(); |
|
420 item2_H->clear(); |
|
421 } |
|
422 |
|
423 void tst_QMenuBar::resetCount() |
|
424 { |
|
425 #ifdef QT3_SUPPORT |
|
426 last_accel_id = RESET; |
|
427 #endif |
|
428 last_accel_id_Qt4 = 0; |
|
429 activated_count = 0; |
|
430 } |
|
431 |
|
432 #ifdef QT3_SUPPORT |
|
433 void tst_QMenuBar::onActivated( int i ) |
|
434 { |
|
435 last_accel_id = i; |
|
436 activated_count++; |
|
437 // printf( QString("acceleratorId: %1, count: %1\n").arg( i ).arg(activated_count) ); |
|
438 } |
|
439 #endif |
|
440 |
|
441 void tst_QMenuBar::onActivated_noQt3( QAction* action ) |
|
442 { |
|
443 last_accel_id_Qt4 = action; |
|
444 activated_count++; |
|
445 // printf( QString("acceleratorId: %1, count: %1\n").arg( i ).arg(activated_count) ); |
|
446 } |
|
447 |
|
448 #ifdef QT3_SUPPORT |
|
449 void tst_QMenuBar::accel() |
|
450 { |
|
451 #ifdef Q_WS_MAC |
|
452 QSKIP("On Mac, native key events are needed to test menu action activation", SkipAll); |
|
453 #endif |
|
454 // create a popup menu with menu items set the accelerators later... |
|
455 initSimpleMenubar(); |
|
456 // QTest::keyClick( 0, Qt::Key_A, AltKey ); |
|
457 QTest::keyClick( 0, Qt::Key_A, Qt::ControlModifier ); |
|
458 QTest::qWait(300); |
|
459 |
|
460 QCOMPARE( last_accel_id, idAccel ); |
|
461 } |
|
462 #endif //QT3_SUPPORT |
|
463 |
|
464 void tst_QMenuBar::accel_noQt3() |
|
465 { |
|
466 #if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM) |
|
467 QSKIP("On Mac/WinCE, native key events are needed to test menu action activation", SkipAll); |
|
468 #endif |
|
469 #ifdef Q_OS_SYMBIAN |
|
470 QSKIP("On Symbian OS, native key events are needed to test menu action activation", SkipAll); |
|
471 #endif |
|
472 |
|
473 // create a popup menu with menu items set the accelerators later... |
|
474 initSimpleMenubar_noQt3(); |
|
475 // QTest::keyClick( 0, Qt::Key_A, AltKey ); |
|
476 QTest::keyClick( 0, Qt::Key_A, Qt::ControlModifier ); |
|
477 QTest::qWait(300); |
|
478 |
|
479 QCOMPARE( last_accel_id_Qt4, action ); |
|
480 } |
|
481 |
|
482 #ifdef QT3_SUPPORT |
|
483 void tst_QMenuBar::activatedCount() |
|
484 { |
|
485 #ifdef Q_WS_MAC |
|
486 QSKIP("On Mac, native key events are needed to test menu action activation", SkipAll); |
|
487 #endif |
|
488 // create a popup menu with menu items set the accelerators later... |
|
489 initSimpleMenubar(); |
|
490 |
|
491 QTest::keyClick( 0, Qt::Key_A, Qt::ControlModifier ); |
|
492 //wait(5000); |
|
493 QCOMPARE( activated_count, 2 ); //1 from the popupmenu and 1 from the menubar |
|
494 } |
|
495 #endif //QT3_SUPPORT |
|
496 |
|
497 void tst_QMenuBar::activatedCount_noQt3() |
|
498 { |
|
499 #if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM) |
|
500 QSKIP("On Mac/WinCE, native key events are needed to test menu action activation", SkipAll); |
|
501 #endif |
|
502 // create a popup menu with menu items set the accelerators later... |
|
503 initSimpleMenubar_noQt3(); |
|
504 |
|
505 QTest::keyClick( 0, Qt::Key_A, Qt::ControlModifier ); |
|
506 //wait(5000); |
|
507 QCOMPARE( activated_count, 2 ); //1 from the popupmenu and 1 from the menubar |
|
508 } |
|
509 |
|
510 void tst_QMenuBar::clear_noQt3() |
|
511 { |
|
512 mb->clear(); |
|
513 QVERIFY( (uint) mb->actions().size() == 0 ); |
|
514 |
|
515 mb->clear(); |
|
516 for (uint i=0; i<10; i++) { |
|
517 QMenu *menu = mb->addMenu( QString("Menu %1")); |
|
518 for (uint k=0; k<i; k++) |
|
519 menu->addAction( QString("Item %1")); |
|
520 QCOMPARE( (uint) mb->actions().size(), (uint)i+1 ); |
|
521 } |
|
522 QCOMPARE( (uint) mb->actions().size(), 10u ); |
|
523 |
|
524 mb->clear(); |
|
525 QVERIFY( (uint) mb->actions().size() == 0 ); |
|
526 } |
|
527 |
|
528 void tst_QMenuBar::count_noQt3() |
|
529 { |
|
530 mb->clear(); |
|
531 QVERIFY( mb->actions().size() == 0 ); |
|
532 |
|
533 for (uint i=0; i<10; i++) { |
|
534 mb->addAction( QString("Menu %1")); |
|
535 QCOMPARE( (uint) mb->actions().size(), (uint) i+1 ); |
|
536 } |
|
537 } |
|
538 |
|
539 void tst_QMenuBar::removeItem_noQt3_data() |
|
540 { |
|
541 QTest::addColumn<int>("removeIndex"); |
|
542 QTest::newRow( "first" ) << 0; |
|
543 QTest::newRow( "middle" ) << 1; |
|
544 QTest::newRow( "last" ) << 2; |
|
545 } |
|
546 |
|
547 // Basically the same test as removeItemAt, except that we remember and remove id's. |
|
548 void tst_QMenuBar::removeItem_noQt3() |
|
549 { |
|
550 mb->clear(); |
|
551 |
|
552 QMenu *pm; |
|
553 pm = new QMenu( "stuff", mb ); |
|
554 pm->setTitle("Menu 1"); |
|
555 pm->addAction( QString("Item 10") ); |
|
556 QAction* action1 = mb->addMenu( pm ); |
|
557 |
|
558 pm = new QMenu( mb ); |
|
559 pm->setTitle("Menu 2"); |
|
560 pm->addAction( QString("Item 20") ); |
|
561 pm->addAction( QString("Item 21") ); |
|
562 QAction *action2 = mb->addMenu( pm ); |
|
563 |
|
564 pm = new QMenu( "Menu 3", mb ); |
|
565 pm->addAction( QString("Item 30") ); |
|
566 pm->addAction( QString("Item 31") ); |
|
567 pm->addAction( QString("Item 32") ); |
|
568 QAction *action3 = mb->addMenu( pm ); |
|
569 |
|
570 QList<QAction *> menuBarActions = mb->actions(); |
|
571 |
|
572 QCOMPARE( action1->text(), QString("Menu 1") ); |
|
573 QCOMPARE( action2->text(), QString("Menu 2") ); |
|
574 QCOMPARE( action3->text(), QString("Menu 3") ); |
|
575 |
|
576 QVERIFY( menuBarActions.at(0) == action1 ); |
|
577 QVERIFY( menuBarActions.at(1) == action2 ); |
|
578 QVERIFY( menuBarActions.at(2) == action3 ); |
|
579 |
|
580 // Ok, now that we know we have created the menu we expect, lets remove an item... |
|
581 QFETCH( int, removeIndex ); |
|
582 switch (removeIndex ) |
|
583 { |
|
584 case 0: { |
|
585 mb->removeAction(action1); |
|
586 QList<QAction *> menuBarActions2 = mb->actions(); |
|
587 QCOMPARE( menuBarActions2.at(0)->text(), QString("Menu 2") ); |
|
588 QCOMPARE( menuBarActions2.at(1)->text(), QString("Menu 3") ); |
|
589 } |
|
590 break; |
|
591 case 1: { |
|
592 mb->removeAction(action2); |
|
593 QList<QAction *> menuBarActions2 = mb->actions(); |
|
594 QCOMPARE( menuBarActions2.at(0)->text(), QString("Menu 1") ); |
|
595 QCOMPARE( menuBarActions2.at(1)->text(), QString("Menu 3") ); |
|
596 } |
|
597 break; |
|
598 case 2: { |
|
599 mb->removeAction(action3); |
|
600 QList<QAction *> menuBarActions2 = mb->actions(); |
|
601 QCOMPARE( menuBarActions2.at(0)->text(), QString("Menu 1") ); |
|
602 QCOMPARE( menuBarActions2.at(1)->text(), QString("Menu 2") ); |
|
603 } |
|
604 break; |
|
605 } |
|
606 QList<QAction *> menuBarActions2 = mb->actions(); |
|
607 QVERIFY( menuBarActions2.size() == 2 ); |
|
608 } |
|
609 |
|
610 void tst_QMenuBar::removeItemAt_noQt3_data() |
|
611 { |
|
612 QTest::addColumn<int>("removeIndex"); |
|
613 QTest::newRow( "first" ) << 0; |
|
614 QTest::newRow( "middle" ) << 1; |
|
615 QTest::newRow( "last" ) << 2; |
|
616 } |
|
617 |
|
618 void tst_QMenuBar::removeItemAt_noQt3() |
|
619 { |
|
620 mb->clear(); |
|
621 |
|
622 QMenu *pm; |
|
623 pm = new QMenu("Menu 1", mb); |
|
624 pm->addAction( QString("Item 10") ); |
|
625 mb->addMenu( pm ); |
|
626 |
|
627 pm = new QMenu( "Menu 2", mb ); |
|
628 pm->addAction( QString("Item 20") ); |
|
629 pm->addAction( QString("Item 21") ); |
|
630 mb->addMenu( pm ); |
|
631 |
|
632 pm = new QMenu( "Menu 3", mb ); |
|
633 pm->addAction( QString("Item 30") ); |
|
634 pm->addAction( QString("Item 31") ); |
|
635 pm->addAction( QString("Item 32") ); |
|
636 mb->addMenu( pm ); |
|
637 |
|
638 QList<QAction *> menuBarActions = mb->actions(); |
|
639 |
|
640 QCOMPARE( menuBarActions.at(0)->text(), QString("Menu 1") ); |
|
641 QCOMPARE( menuBarActions.at(1)->text(), QString("Menu 2") ); |
|
642 QCOMPARE( menuBarActions.at(2)->text(), QString("Menu 3") ); |
|
643 |
|
644 // Ok, now that we know we have created the menu we expect, lets remove an item... |
|
645 QFETCH( int, removeIndex ); |
|
646 mb->removeAction(menuBarActions.at(removeIndex)); |
|
647 QList<QAction *> menuBarActions2 = mb->actions(); |
|
648 switch (removeIndex ) |
|
649 { |
|
650 case 0: |
|
651 QCOMPARE( menuBarActions2.at(0)->text(), QString("Menu 2") ); |
|
652 QCOMPARE( menuBarActions2.at(1)->text(), QString("Menu 3") ); |
|
653 break; |
|
654 case 1: |
|
655 QCOMPARE( menuBarActions2.at(0)->text(), QString("Menu 1") ); |
|
656 QCOMPARE( menuBarActions2.at(1)->text(), QString("Menu 3") ); |
|
657 break; |
|
658 case 2: |
|
659 QCOMPARE( menuBarActions2.at(0)->text(), QString("Menu 1") ); |
|
660 QCOMPARE( menuBarActions2.at(1)->text(), QString("Menu 2") ); |
|
661 break; |
|
662 } |
|
663 |
|
664 QVERIFY( menuBarActions2.size() == 2 ); |
|
665 } |
|
666 |
|
667 #ifdef QT3_SUPPORT |
|
668 void tst_QMenuBar::clear() |
|
669 { |
|
670 mb->clear(); |
|
671 QVERIFY( mb->count() == 0 ); |
|
672 |
|
673 mb->clear(); |
|
674 for (uint i=0; i<10; i++) { |
|
675 Q3PopupMenu *pm = new Q3PopupMenu( mb ); |
|
676 for (uint k=0; k<i; k++) |
|
677 pm->insertItem( QString("Item %1").arg(i*10 + k) ); |
|
678 mb->insertItem( QString("Menu %1").arg(i), pm ); |
|
679 QCOMPARE( mb->count(), (uint)i+1 ); |
|
680 } |
|
681 QCOMPARE( mb->count(), 10u ); |
|
682 |
|
683 mb->clear(); |
|
684 QVERIFY( mb->count() == 0 ); |
|
685 } |
|
686 |
|
687 void tst_QMenuBar::count() |
|
688 { |
|
689 mb->clear(); |
|
690 QVERIFY( mb->count() == 0 ); |
|
691 |
|
692 for (uint i=0; i<10; i++) { |
|
693 Q3PopupMenu *pm = new Q3PopupMenu( mb ); |
|
694 mb->insertItem( QString("Menu %1").arg(i), pm ); |
|
695 QCOMPARE( mb->count(), i+1 ); |
|
696 } |
|
697 } |
|
698 |
|
699 void tst_QMenuBar::removeItemAt_data() |
|
700 { |
|
701 QTest::addColumn<int>("removeIndex"); |
|
702 QTest::newRow( "first" ) << 0; |
|
703 QTest::newRow( "middle" ) << 1; |
|
704 QTest::newRow( "last" ) << 2; |
|
705 } |
|
706 |
|
707 void tst_QMenuBar::removeItemAt() |
|
708 { |
|
709 mb->clear(); |
|
710 |
|
711 Q3PopupMenu *pm; |
|
712 pm = new Q3PopupMenu( mb ); |
|
713 pm->insertItem( QString("Item 10") ); |
|
714 mb->insertItem( QString("Menu 1"), pm ); |
|
715 |
|
716 pm = new Q3PopupMenu( mb ); |
|
717 pm->insertItem( QString("Item 20") ); |
|
718 pm->insertItem( QString("Item 21") ); |
|
719 mb->insertItem( QString("Menu 2"), pm ); |
|
720 |
|
721 pm = new Q3PopupMenu( mb ); |
|
722 pm->insertItem( QString("Item 30") ); |
|
723 pm->insertItem( QString("Item 31") ); |
|
724 pm->insertItem( QString("Item 32") ); |
|
725 mb->insertItem( QString("Menu 3"), pm ); |
|
726 |
|
727 QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 1") ); |
|
728 QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 2") ); |
|
729 QCOMPARE( mb->text( mb->idAt(2) ), QString("Menu 3") ); |
|
730 |
|
731 // Ok, now that we know we have created the menu we expect, lets remove an item... |
|
732 QFETCH( int, removeIndex ); |
|
733 mb->removeItemAt( removeIndex ); |
|
734 switch (removeIndex ) |
|
735 { |
|
736 case 0: |
|
737 QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 2") ); |
|
738 QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 3") ); |
|
739 break; |
|
740 case 1: |
|
741 QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 1") ); |
|
742 QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 3") ); |
|
743 break; |
|
744 case 2: |
|
745 QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 1") ); |
|
746 QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 2") ); |
|
747 break; |
|
748 } |
|
749 |
|
750 QVERIFY( mb->count() == 2 ); |
|
751 } |
|
752 |
|
753 void tst_QMenuBar::removeItem_data() |
|
754 { |
|
755 QTest::addColumn<int>("removeIndex"); |
|
756 QTest::newRow( "first" ) << 0; |
|
757 QTest::newRow( "middle" ) << 1; |
|
758 QTest::newRow( "last" ) << 2; |
|
759 } |
|
760 |
|
761 // Basically the same test as removeItemAt, except that we remember and remove id's. |
|
762 void tst_QMenuBar::removeItem() |
|
763 { |
|
764 mb->clear(); |
|
765 |
|
766 Q3PopupMenu *pm; |
|
767 pm = new Q3PopupMenu( mb ); |
|
768 pm->insertItem( QString("Item 10") ); |
|
769 int id1 = mb->insertItem( QString("Menu 1"), pm ); |
|
770 |
|
771 pm = new Q3PopupMenu( mb ); |
|
772 pm->insertItem( QString("Item 20") ); |
|
773 pm->insertItem( QString("Item 21") ); |
|
774 int id2 = mb->insertItem( QString("Menu 2"), pm ); |
|
775 |
|
776 pm = new Q3PopupMenu( mb ); |
|
777 pm->insertItem( QString("Item 30") ); |
|
778 pm->insertItem( QString("Item 31") ); |
|
779 pm->insertItem( QString("Item 32") ); |
|
780 int id3 = mb->insertItem( QString("Menu 3"), pm ); |
|
781 |
|
782 QCOMPARE( mb->text( id1 ), QString("Menu 1") ); |
|
783 QCOMPARE( mb->text( id2 ), QString("Menu 2") ); |
|
784 QCOMPARE( mb->text( id3 ), QString("Menu 3") ); |
|
785 |
|
786 QVERIFY( mb->idAt(0) == id1 ); |
|
787 QVERIFY( mb->idAt(1) == id2 ); |
|
788 QVERIFY( mb->idAt(2) == id3 ); |
|
789 |
|
790 // Ok, now that we know we have created the menu we expect, lets remove an item... |
|
791 QFETCH( int, removeIndex ); |
|
792 switch (removeIndex ) |
|
793 { |
|
794 case 0: |
|
795 mb->removeItem( id1 ); |
|
796 QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 2") ); |
|
797 QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 3") ); |
|
798 break; |
|
799 case 1: |
|
800 mb->removeItem( id2 ); |
|
801 QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 1") ); |
|
802 QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 3") ); |
|
803 break; |
|
804 case 2: |
|
805 mb->removeItem( id3 ); |
|
806 QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 1") ); |
|
807 QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 2") ); |
|
808 break; |
|
809 } |
|
810 |
|
811 QVERIFY( mb->count() == 2 ); |
|
812 } |
|
813 |
|
814 void tst_QMenuBar::initComplexMenubar() // well, complex.... |
|
815 { |
|
816 mb->hide(); |
|
817 mb->clear(); |
|
818 |
|
819 delete pm1; |
|
820 pm1 = new Q3PopupMenu( mb, "popup1" ); |
|
821 pm1->insertItem( QString("Item A"), item1_A, SLOT(selected()), Qt::CTRL+Qt::Key_A ); |
|
822 pm1->insertItem( QString("Item B"), item1_B, SLOT(selected()), Qt::CTRL+Qt::Key_B ); |
|
823 // use the form insertItem( QString, Q3PopupMenu ) |
|
824 mb->insertItem( "Menu &1", pm1 ); |
|
825 |
|
826 delete pm2; |
|
827 pm2 = new Q3PopupMenu( mb, "popup2" ); |
|
828 pm2->insertItem( QString("Item C"), item2_C, SLOT(selected()), Qt::CTRL+Qt::Key_C ); |
|
829 pm2->insertItem( QString("Item D"), item2_D, SLOT(selected()), Qt::CTRL+Qt::Key_D ); |
|
830 pm2->insertItem( QString("Item E"), item2_E, SLOT(selected()), Qt::CTRL+Qt::Key_E ); |
|
831 pm2->insertItem( QString("Item F"), item2_F, SLOT(selected()), Qt::CTRL+Qt::Key_F ); |
|
832 pm2->insertSeparator(); |
|
833 pm2->insertItem( QString("Item G"), item2_G, SLOT(selected()), Qt::CTRL+Qt::Key_G ); |
|
834 pm2->insertItem( QString("Item H"), item2_H, SLOT(selected()), Qt::CTRL+Qt::Key_H ); |
|
835 // use the form insertItem( QString, Q3PopupMenu ) |
|
836 mb->insertItem( "Menu &2", pm2 ); |
|
837 |
|
838 // use the form insertItem( QString, QObject, slot, keysequence ) |
|
839 mb->insertItem( QString("M&enu 3"), menu3, SLOT(selected()), Qt::ALT+Qt::Key_J ); |
|
840 mb->show(); |
|
841 } |
|
842 #endif |
|
843 |
|
844 void tst_QMenuBar::initComplexMenubar_noQt3() // well, complex.... |
|
845 { |
|
846 mb->hide(); |
|
847 mb->clear(); |
|
848 |
|
849 delete pm1_Qt4; |
|
850 pm1_Qt4 = mb->addMenu("Menu &1"); |
|
851 pm1_Qt4->addAction( QString("Item A"), item1_A, SLOT(selected()), Qt::CTRL+Qt::Key_A ); |
|
852 pm1_Qt4->addAction( QString("Item B"), item1_B, SLOT(selected()), Qt::CTRL+Qt::Key_B ); |
|
853 |
|
854 delete pm2_Qt4; |
|
855 pm2_Qt4 = mb->addMenu("Menu &2"); |
|
856 pm2_Qt4->addAction( QString("Item C"), item2_C, SLOT(selected()), Qt::CTRL+Qt::Key_C ); |
|
857 pm2_Qt4->addAction( QString("Item D"), item2_D, SLOT(selected()), Qt::CTRL+Qt::Key_D ); |
|
858 pm2_Qt4->addAction( QString("Item E"), item2_E, SLOT(selected()), Qt::CTRL+Qt::Key_E ); |
|
859 pm2_Qt4->addAction( QString("Item F"), item2_F, SLOT(selected()), Qt::CTRL+Qt::Key_F ); |
|
860 pm2_Qt4->addSeparator(); |
|
861 pm2_Qt4->addAction( QString("Item G"), item2_G, SLOT(selected()), Qt::CTRL+Qt::Key_G ); |
|
862 pm2_Qt4->addAction( QString("Item H"), item2_H, SLOT(selected()), Qt::CTRL+Qt::Key_H ); |
|
863 |
|
864 QAction *ac = mb->addAction( QString("M&enu 3"), menu3, SLOT(selected())); |
|
865 ac->setShortcut(Qt::ALT+Qt::Key_J); |
|
866 mb->show(); |
|
867 } |
|
868 |
|
869 |
|
870 /* |
|
871 Check the insert functions that create menu items. |
|
872 For the moment i only check the strings and pixmaps. The rest are special cases which are |
|
873 used less frequently. |
|
874 */ |
|
875 |
|
876 #ifdef QT3_SUPPORT |
|
877 void tst_QMenuBar::insertItem_QString_QObject() |
|
878 { |
|
879 initComplexMenubar(); |
|
880 QCOMPARE( mb->text( mb->idAt( 0 ) ), QString("Menu &1") ); |
|
881 QCOMPARE( mb->text( mb->idAt( 1 ) ), QString("Menu &2") ); |
|
882 QCOMPARE( mb->text( mb->idAt( 2 ) ), QString("M&enu 3") ); |
|
883 QCOMPARE( mb->text( mb->idAt( 3 ) ), QString() ); // there is no menu 4! |
|
884 } |
|
885 #endif |
|
886 |
|
887 void tst_QMenuBar::insertItem_QString_QObject_noQt3() |
|
888 { |
|
889 initComplexMenubar_noQt3(); |
|
890 |
|
891 QList<QAction *> actions = mb->actions(); |
|
892 |
|
893 QCOMPARE(actions.at(0)->text(), QString("Menu &1") ); |
|
894 QCOMPARE(actions.at(1)->text(), QString("Menu &2") ); |
|
895 QCOMPARE(actions.at(2)->text(), QString("M&enu 3") ); |
|
896 QVERIFY(actions.size() < 4); // there is no menu 4! |
|
897 } |
|
898 |
|
899 void tst_QMenuBar::check_accelKeys() |
|
900 { |
|
901 #if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM) |
|
902 QSKIP("On Mac/WinCE, native key events are needed to test menu action activation", SkipAll); |
|
903 #endif |
|
904 #ifdef QT3_SUPPORT |
|
905 initComplexMenubar(); |
|
906 #else |
|
907 initComplexMenubar_noQt3(); |
|
908 #endif |
|
909 |
|
910 // start with a bogus key that shouldn't trigger anything |
|
911 QTest::keyClick(0, Qt::Key_I, Qt::ControlModifier); |
|
912 QCOMPARE(menu1->selCount(), 0u); |
|
913 QCOMPARE(menu2->selCount(), 0u); |
|
914 QCOMPARE(menu3->selCount(), 0u); |
|
915 QCOMPARE(menu4->selCount(), 0u); |
|
916 QCOMPARE(item1_A->selCount(), 0u); |
|
917 QCOMPARE(item1_B->selCount(), 0u); |
|
918 QCOMPARE(item2_C->selCount(), 0u); |
|
919 QCOMPARE(item2_D->selCount(), 0u); |
|
920 |
|
921 QTest::keyClick(0, Qt::Key_A, Qt::ControlModifier); |
|
922 QCOMPARE(menu1->selCount(), 0u); |
|
923 QCOMPARE(menu2->selCount(), 0u); |
|
924 QCOMPARE(menu3->selCount(), 0u); |
|
925 QCOMPARE(menu4->selCount(), 0u); |
|
926 QCOMPARE(item1_A->selCount(), 1u); |
|
927 QCOMPARE(item1_B->selCount(), 0u); |
|
928 QCOMPARE(item2_C->selCount(), 0u); |
|
929 QCOMPARE(item2_D->selCount(), 0u); |
|
930 |
|
931 QTest::keyClick(0, Qt::Key_C, Qt::ControlModifier); |
|
932 QCOMPARE(menu1->selCount(), 0u); |
|
933 QCOMPARE(menu2->selCount(), 0u); |
|
934 QCOMPARE(menu3->selCount(), 0u); |
|
935 QCOMPARE(menu4->selCount(), 0u); |
|
936 QCOMPARE(item1_A->selCount(), 1u); |
|
937 QCOMPARE(item1_B->selCount(), 0u); |
|
938 QCOMPARE(item2_C->selCount(), 1u); |
|
939 QCOMPARE(item2_D->selCount(), 0u); |
|
940 |
|
941 QTest::keyClick(0, Qt::Key_B, Qt::ControlModifier); |
|
942 QCOMPARE(menu1->selCount(), 0u); |
|
943 QCOMPARE(menu2->selCount(), 0u); |
|
944 QCOMPARE(menu3->selCount(), 0u); |
|
945 QCOMPARE(menu4->selCount(), 0u); |
|
946 QCOMPARE(item1_A->selCount(), 1u); |
|
947 QCOMPARE(item1_B->selCount(), 1u); |
|
948 QCOMPARE(item2_C->selCount(), 1u); |
|
949 QCOMPARE(item2_D->selCount(), 0u); |
|
950 |
|
951 QTest::keyClick(0, Qt::Key_D, Qt::ControlModifier); |
|
952 QCOMPARE(menu1->selCount(), 0u); |
|
953 QCOMPARE(menu2->selCount(), 0u); |
|
954 QCOMPARE(menu3->selCount(), 0u); |
|
955 QCOMPARE(menu4->selCount(), 0u); |
|
956 QCOMPARE(item1_A->selCount(), 1u); |
|
957 QCOMPARE(item1_B->selCount(), 1u); |
|
958 QCOMPARE(item2_C->selCount(), 1u); |
|
959 QCOMPARE(item2_D->selCount(), 1u); |
|
960 |
|
961 QTest::keyClick(0, Qt::Key_J, Qt::AltModifier); |
|
962 QCOMPARE(menu1->selCount(), 0u); |
|
963 QCOMPARE(menu2->selCount(), 0u); |
|
964 QCOMPARE(menu3->selCount(), 1u); |
|
965 QCOMPARE(menu4->selCount(), 0u); |
|
966 QCOMPARE(item1_A->selCount(), 1u); |
|
967 QCOMPARE(item1_B->selCount(), 1u); |
|
968 QCOMPARE(item2_C->selCount(), 1u); |
|
969 QCOMPARE(item2_D->selCount(), 1u); |
|
970 } |
|
971 |
|
972 void tst_QMenuBar::check_cursorKeys1() |
|
973 { |
|
974 #if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM) |
|
975 QSKIP("Qt/Mac,WinCE does not use the native popups/menubar", SkipAll); |
|
976 #endif |
|
977 |
|
978 #ifdef QT3_SUPPORT |
|
979 initComplexMenubar(); |
|
980 #else |
|
981 initComplexMenubar_noQt3(); |
|
982 #endif |
|
983 |
|
984 // start with a ALT + 1 that activates the first popupmenu |
|
985 QTest::keyClick( 0, Qt::Key_1, Qt::AltModifier ); |
|
986 // the Popupmenu should be visible now |
|
987 QCOMPARE(menu3->selCount(), 0u); |
|
988 QCOMPARE(menu4->selCount(), 0u); |
|
989 QCOMPARE(item1_A->selCount(), 0u); |
|
990 QCOMPARE(item1_B->selCount(), 0u); |
|
991 QCOMPARE(item2_C->selCount(), 0u); |
|
992 QCOMPARE(item2_D->selCount(), 0u); |
|
993 |
|
994 // Simulate a cursor key down click |
|
995 QTest::keyClick( 0, Qt::Key_Down ); |
|
996 // and an Enter key |
|
997 QTest::keyClick( 0, Qt::Key_Enter ); |
|
998 // Let's see if the correct slot is called... |
|
999 QCOMPARE(menu3->selCount(), 0u); |
|
1000 QCOMPARE(menu4->selCount(), 0u); |
|
1001 QCOMPARE(item1_A->selCount(), 0u); // this shouldn't have been called |
|
1002 QCOMPARE(item1_B->selCount(), 1u); // and this should have been called by a signal now |
|
1003 QCOMPARE(item2_C->selCount(), 0u); |
|
1004 QCOMPARE(item2_D->selCount(), 0u); |
|
1005 } |
|
1006 |
|
1007 void tst_QMenuBar::check_cursorKeys2() |
|
1008 { |
|
1009 #if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM) |
|
1010 QSKIP("Qt/Mac,WinCE does not use the native popups/menubar", SkipAll); |
|
1011 #endif |
|
1012 |
|
1013 #ifdef QT3_SUPPORT |
|
1014 initComplexMenubar(); |
|
1015 #else |
|
1016 initComplexMenubar_noQt3(); |
|
1017 #endif |
|
1018 |
|
1019 // select popupmenu2 |
|
1020 QTest::keyClick( 0, Qt::Key_2, Qt::AltModifier ); |
|
1021 |
|
1022 // Simulate some cursor keys |
|
1023 QTest::keyClick( 0, Qt::Key_Left ); |
|
1024 QTest::keyClick( 0, Qt::Key_Down ); |
|
1025 QTest::keyClick( 0, Qt::Key_Right ); |
|
1026 QTest::keyClick( 0, Qt::Key_Down ); |
|
1027 // and an Enter key |
|
1028 QTest::keyClick( 0, Qt::Key_Enter ); |
|
1029 // Let's see if the correct slot is called... |
|
1030 QCOMPARE(menu3->selCount(), 0u); |
|
1031 QCOMPARE(menu4->selCount(), 0u); |
|
1032 QCOMPARE(item1_A->selCount(), 0u); // this shouldn't have been caled |
|
1033 QCOMPARE(item1_B->selCount(), 0u); // and this should have been called by a signal ow |
|
1034 QCOMPARE(item2_C->selCount(), 0u); |
|
1035 QCOMPARE(item2_D->selCount(), 1u); |
|
1036 } |
|
1037 |
|
1038 /*! |
|
1039 If a popupmenu is active you can use Left to move to the menu to the left of it. |
|
1040 */ |
|
1041 void tst_QMenuBar::check_cursorKeys3() |
|
1042 { |
|
1043 #if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM) |
|
1044 QSKIP("Qt/Mac,WinCE does not use the native popups/menubar", SkipAll); |
|
1045 #endif |
|
1046 |
|
1047 #ifdef QT3_SUPPORT |
|
1048 initComplexMenubar(); |
|
1049 #else |
|
1050 initComplexMenubar_noQt3(); |
|
1051 #endif |
|
1052 |
|
1053 // select Popupmenu 2 |
|
1054 QTest::keyClick( 0, Qt::Key_2, Qt::AltModifier ); |
|
1055 |
|
1056 // Simulate some keys |
|
1057 QTest::keyClick( 0, Qt::Key_Left ); |
|
1058 QTest::keyClick( 0, Qt::Key_Down ); |
|
1059 // and press ENTER |
|
1060 QTest::keyClick( 0, Qt::Key_Enter ); |
|
1061 // Let's see if the correct slot is called... |
|
1062 QCOMPARE(menu3->selCount(), 0u); |
|
1063 QCOMPARE(menu4->selCount(), 0u); |
|
1064 QCOMPARE(item1_A->selCount(), 0u); // this shouldn't have been called |
|
1065 QCOMPARE(item1_B->selCount(), 1u); // and this should have been called by a signal now |
|
1066 QCOMPARE(item2_C->selCount(), 0u); |
|
1067 QCOMPARE(item2_D->selCount(), 0u); |
|
1068 } |
|
1069 |
|
1070 /*! |
|
1071 If a popupmenu is active you can use home to go quickly to the first item in the menu. |
|
1072 */ |
|
1073 void tst_QMenuBar::check_homeKey() |
|
1074 { |
|
1075 // I'm temporarily shutting up this testcase. |
|
1076 // Seems like the behaviour i'm expecting isn't ok. |
|
1077 QVERIFY( TRUE ); |
|
1078 return; |
|
1079 |
|
1080 QEXPECT_FAIL( "0", "Popupmenu should respond to a Home key", Abort ); |
|
1081 |
|
1082 #ifdef QT3_SUPPORT |
|
1083 initComplexMenubar(); |
|
1084 #else |
|
1085 initComplexMenubar_noQt3(); |
|
1086 #endif |
|
1087 |
|
1088 // select Popupmenu 2 |
|
1089 QTest::keyClick( 0, Qt::Key_2, Qt::AltModifier ); |
|
1090 |
|
1091 // Simulate some keys |
|
1092 QTest::keyClick( 0, Qt::Key_Down ); |
|
1093 QTest::keyClick( 0, Qt::Key_Down ); |
|
1094 QTest::keyClick( 0, Qt::Key_Down ); |
|
1095 QTest::keyClick( 0, Qt::Key_Home ); |
|
1096 // and press ENTER |
|
1097 QTest::keyClick( 0, Qt::Key_Enter ); |
|
1098 // Let's see if the correct slot is called... |
|
1099 // QVERIFY2( item2_C->selCount() == 1, "Popupmenu should respond to a Home key" ); |
|
1100 QCOMPARE(item2_C->selCount(), 1u); |
|
1101 QCOMPARE(menu3->selCount(), 0u); |
|
1102 QCOMPARE(menu4->selCount(), 0u); |
|
1103 QCOMPARE(item1_A->selCount(), 0u); |
|
1104 QCOMPARE(item1_B->selCount(), 0u); |
|
1105 QCOMPARE(item2_D->selCount(), 0u); |
|
1106 QCOMPARE(item2_E->selCount(), 0u); |
|
1107 QCOMPARE(item2_F->selCount(), 0u); |
|
1108 QCOMPARE(item2_G->selCount(), 0u); |
|
1109 QCOMPARE(item2_H->selCount(), 0u); |
|
1110 } |
|
1111 |
|
1112 /*! |
|
1113 If a popupmenu is active you can use end to go quickly to the last item in the menu. |
|
1114 */ |
|
1115 void tst_QMenuBar::check_endKey() |
|
1116 { |
|
1117 // I'm temporarily silenting this testcase. |
|
1118 // Seems like the behaviour i'm expecting isn't ok. |
|
1119 QVERIFY( TRUE ); |
|
1120 return; |
|
1121 |
|
1122 QEXPECT_FAIL( "0", "Popupmenu should respond to an End key", Abort ); |
|
1123 |
|
1124 #ifdef QT3_SUPPORT |
|
1125 initComplexMenubar(); |
|
1126 #else |
|
1127 initComplexMenubar_noQt3(); |
|
1128 #endif |
|
1129 |
|
1130 // select Popupmenu 2 |
|
1131 QTest::keyClick( 0, Qt::Key_2, Qt::AltModifier ); |
|
1132 |
|
1133 // Simulate some keys |
|
1134 QTest::keyClick( 0, Qt::Key_End ); |
|
1135 // and press ENTER |
|
1136 QTest::keyClick( 0, Qt::Key_Enter ); |
|
1137 // Let's see if the correct slot is called... |
|
1138 // QVERIFY2( item2_H->selCount() == 1, "Popupmenu should respond to an End key" ); |
|
1139 QCOMPARE(item2_H->selCount(), 1u);//, "Popupmenu should respond to an End key"); |
|
1140 QCOMPARE(menu3->selCount(), 0u); |
|
1141 QCOMPARE(menu4->selCount(), 0u); |
|
1142 QCOMPARE(item1_A->selCount(), 0u); |
|
1143 QCOMPARE(item1_B->selCount(), 0u); |
|
1144 QCOMPARE(item2_C->selCount(), 0u); |
|
1145 QCOMPARE(item2_D->selCount(), 0u); |
|
1146 QCOMPARE(item2_E->selCount(), 0u); |
|
1147 QCOMPARE(item2_F->selCount(), 0u); |
|
1148 QCOMPARE(item2_G->selCount(), 0u); |
|
1149 } |
|
1150 |
|
1151 /*! |
|
1152 If a popupmenu is active you can use esc to hide the menu and then the |
|
1153 menubar should become active. |
|
1154 If Down is pressed next the popup is activated again. |
|
1155 */ |
|
1156 |
|
1157 #ifdef QT3_SUPPORT |
|
1158 void tst_QMenuBar::check_escKey() |
|
1159 { |
|
1160 #ifdef Q_WS_MAC |
|
1161 QSKIP("Qt/Mac does not use the native popups/menubar", SkipAll); |
|
1162 #endif |
|
1163 |
|
1164 initComplexMenubar(); |
|
1165 |
|
1166 QVERIFY( !pm1->isActiveWindow() ); |
|
1167 QVERIFY( !pm2->isActiveWindow() ); |
|
1168 |
|
1169 // select Popupmenu 2 |
|
1170 QTest::keyClick( 0, Qt::Key_2, Qt::AltModifier ); |
|
1171 QVERIFY( !pm1->isActiveWindow() ); |
|
1172 QVERIFY( pm2->isActiveWindow() ); |
|
1173 |
|
1174 // If we press ESC, the popup should disappear |
|
1175 QTest::keyClick( 0, Qt::Key_Escape ); |
|
1176 QVERIFY( !pm1->isActiveWindow() ); |
|
1177 QVERIFY( !pm2->isActiveWindow() ); |
|
1178 |
|
1179 if (!QApplication::style()->inherits("QWindowsStyle")) |
|
1180 return; |
|
1181 |
|
1182 // but the menubar item should stay selected |
|
1183 QVERIFY( mb->isItemActive(mb->idAt(1)) ); |
|
1184 |
|
1185 // If we press Down the popupmenu should be active again |
|
1186 QTest::keyClick( 0, Qt::Key_Down ); |
|
1187 QVERIFY( !pm1->isActiveWindow() ); |
|
1188 QVERIFY( pm2->isActiveWindow() ); |
|
1189 |
|
1190 // and press ENTER |
|
1191 QTest::keyClick( pm2, Qt::Key_Enter ); |
|
1192 // Let's see if the correct slot is called... |
|
1193 QVERIFY2( item2_C->selCount() == 1, "Expected item 2C to be selected" ); |
|
1194 } |
|
1195 #endif |
|
1196 |
|
1197 void tst_QMenuBar::check_escKey_noQt3() |
|
1198 { |
|
1199 #if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM) |
|
1200 QSKIP("Qt/Mac,WinCE does not use the native popups/menubar", SkipAll); |
|
1201 #endif |
|
1202 |
|
1203 initComplexMenubar_noQt3(); |
|
1204 |
|
1205 QVERIFY( !pm1_Qt4->isActiveWindow() ); |
|
1206 QVERIFY( !pm2_Qt4->isActiveWindow() ); |
|
1207 |
|
1208 // select Popupmenu 2 |
|
1209 QTest::keyClick( 0, Qt::Key_2, Qt::AltModifier ); |
|
1210 QVERIFY( !pm1_Qt4->isActiveWindow() ); |
|
1211 QVERIFY( pm2_Qt4->isActiveWindow() ); |
|
1212 |
|
1213 // If we press ESC, the popup should disappear |
|
1214 QTest::keyClick( 0, Qt::Key_Escape ); |
|
1215 QVERIFY( !pm1_Qt4->isActiveWindow() ); |
|
1216 QVERIFY( !pm2_Qt4->isActiveWindow() ); |
|
1217 |
|
1218 if (!QApplication::style()->inherits("QWindowsStyle")) |
|
1219 return; |
|
1220 |
|
1221 // If we press Down the popupmenu should be active again |
|
1222 QTest::keyClick( 0, Qt::Key_Down ); |
|
1223 QVERIFY( !pm1_Qt4->isActiveWindow() ); |
|
1224 QVERIFY( pm2_Qt4->isActiveWindow() ); |
|
1225 |
|
1226 // and press ENTER |
|
1227 QTest::keyClick( pm2_Qt4, Qt::Key_Enter ); |
|
1228 // Let's see if the correct slot is called... |
|
1229 QVERIFY2( item2_C->selCount() == 1, "Expected item 2C to be selected" ); |
|
1230 } |
|
1231 |
|
1232 |
|
1233 // void tst_QMenuBar::check_mouse1_data() |
|
1234 // { |
|
1235 // QTest::addColumn<QString>("popup_item"); |
|
1236 // QTest::addColumn<int>("itemA_count"); |
|
1237 // QTest::addColumn<int>("itemB_count"); |
|
1238 |
|
1239 // QTest::newRow( "A" ) << QString( "Item A Ctrl+A" ) << 1 << 0; |
|
1240 // QTest::newRow( "B" ) << QString( "Item B Ctrl+B" ) << 0 << 1; |
|
1241 // } |
|
1242 |
|
1243 // /*! |
|
1244 // Check if the correct signals are emitted if we select a popupmenu. |
|
1245 // */ |
|
1246 // void tst_QMenuBar::check_mouse1() |
|
1247 // { |
|
1248 // if (QSystem::curStyle() == "Motif") |
|
1249 // QSKIP("This fails in Motif due to a bug in the testing framework", SkipAll); |
|
1250 // QFETCH( QString, popup_item ); |
|
1251 // QFETCH( int, itemA_count ); |
|
1252 // QFETCH( int, itemB_count ); |
|
1253 |
|
1254 // initComplexMenubar(); |
|
1255 // QVERIFY( !pm1->isActiveWindow() ); |
|
1256 // QVERIFY( !pm2->isActiveWindow() ); |
|
1257 |
|
1258 // QTest::qWait(1000); |
|
1259 // QtTestMouse mouse; |
|
1260 // mouse.mouseEvent( QtTestMouse::MouseClick, mb, "Menu &1", Qt::LeftButton ); |
|
1261 |
|
1262 // QVERIFY( pm1->isActiveWindow() ); |
|
1263 // QVERIFY( !pm2->isActiveWindow() ); |
|
1264 |
|
1265 // QTest::qWait(1000); |
|
1266 // mouse.mouseEvent( QtTestMouse::MouseClick, pm1, popup_item, Qt::LeftButton ); |
|
1267 |
|
1268 // QCOMPARE(menu3->selCount(), 0u); |
|
1269 // QCOMPARE(menu4->selCount(), 0u); |
|
1270 // QCOMPARE(item1_A->selCount(), (uint)itemA_count); // this option should have fired |
|
1271 // QCOMPARE(item1_B->selCount(), (uint)itemB_count); |
|
1272 // QCOMPARE(item2_C->selCount(), 0u); |
|
1273 // QCOMPARE(item2_D->selCount(), 0u); |
|
1274 // QCOMPARE(item2_E->selCount(), 0u); |
|
1275 // QCOMPARE(item2_F->selCount(), 0u); |
|
1276 // QCOMPARE(item2_G->selCount(), 0u); |
|
1277 // } |
|
1278 |
|
1279 // void tst_QMenuBar::check_mouse2_data() |
|
1280 // { |
|
1281 // QTest::addColumn<QString>("label"); |
|
1282 // QTest::addColumn<int>("itemA_count"); |
|
1283 // QTest::addColumn<int>("itemB_count"); |
|
1284 // QTest::addColumn<int>("itemC_count"); |
|
1285 // QTest::addColumn<int>("itemD_count"); |
|
1286 // QTest::addColumn<int>("itemE_count"); |
|
1287 // QTest::addColumn<int>("itemF_count"); |
|
1288 // QTest::addColumn<int>("itemG_count"); |
|
1289 // QTest::addColumn<int>("itemH_count"); |
|
1290 // QTest::addColumn<int>("menu3_count"); |
|
1291 |
|
1292 // QTest::newRow( "A" ) << QString( "Menu &1/Item A Ctrl+A" ) << 1 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0; |
|
1293 // QTest::newRow( "B" ) << QString( "Menu &1/Item B Ctrl+B" ) << 0 << 1 << 0 << 0 << 0 << 0 << 0 << 0 << 0; |
|
1294 // QTest::newRow( "C" ) << QString( "Menu &2/Item C Ctrl+C" ) << 0 << 0 << 1 << 0 << 0 << 0 << 0 << 0 << 0; |
|
1295 // QTest::newRow( "D" ) << QString( "Menu &2/Item D Ctrl+D" ) << 0 << 0 << 0 << 1 << 0 << 0 << 0 << 0 << 0; |
|
1296 // QTest::newRow( "E" ) << QString( "Menu &2/Item E Ctrl+E" ) << 0 << 0 << 0 << 0 << 1 << 0 << 0 << 0 << 0; |
|
1297 // QTest::newRow( "F" ) << QString( "Menu &2/Item F Ctrl+F" ) << 0 << 0 << 0 << 0 << 0 << 1 << 0 << 0 << 0; |
|
1298 // QTest::newRow( "G" ) << QString( "Menu &2/Item G Ctrl+G" ) << 0 << 0 << 0 << 0 << 0 << 0 << 1 << 0 << 0; |
|
1299 // QTest::newRow( "H" ) << QString( "Menu &2/Item H Ctrl+H" ) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 1 << 0; |
|
1300 // QTest::newRow( "menu 3" ) << QString( "M&enu 3" ) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 1; |
|
1301 // } |
|
1302 |
|
1303 // /*! |
|
1304 // Check if the correct signals are emitted if we select a popupmenu. |
|
1305 // This time, we use a little bit more magic from the testframework. |
|
1306 // */ |
|
1307 // void tst_QMenuBar::check_mouse2() |
|
1308 // { |
|
1309 // if (QSystem::curStyle() == "Motif") |
|
1310 // QSKIP("This fails in Motif due to a bug in the testing framework", SkipAll); |
|
1311 // QFETCH( QString, label ); |
|
1312 // QFETCH( int, itemA_count ); |
|
1313 // QFETCH( int, itemB_count ); |
|
1314 // QFETCH( int, itemC_count ); |
|
1315 // QFETCH( int, itemD_count ); |
|
1316 // QFETCH( int, itemE_count ); |
|
1317 // QFETCH( int, itemF_count ); |
|
1318 // QFETCH( int, itemG_count ); |
|
1319 // QFETCH( int, itemH_count ); |
|
1320 // QFETCH( int, menu3_count ); |
|
1321 |
|
1322 // initComplexMenubar(); |
|
1323 // QtTestMouse mouse; |
|
1324 // mouse.click( QtTestMouse::Menu, label, Qt::LeftButton ); |
|
1325 |
|
1326 // // check if the correct signals have fired |
|
1327 // QCOMPARE(menu3->selCount(), (uint)menu3_count); |
|
1328 // QCOMPARE(menu4->selCount(), 0u); |
|
1329 // QCOMPARE(item1_A->selCount(), (uint)itemA_count); |
|
1330 // QCOMPARE(item1_B->selCount(), (uint)itemB_count); |
|
1331 // QCOMPARE(item2_C->selCount(), (uint)itemC_count); |
|
1332 // QCOMPARE(item2_D->selCount(), (uint)itemD_count); |
|
1333 // QCOMPARE(item2_E->selCount(), (uint)itemE_count); |
|
1334 // QCOMPARE(item2_F->selCount(), (uint)itemF_count); |
|
1335 // QCOMPARE(item2_G->selCount(), (uint)itemG_count); |
|
1336 // QCOMPARE(item2_H->selCount(), (uint)itemH_count); |
|
1337 // } |
|
1338 |
|
1339 void |
|
1340 tst_QMenuBar::allowActiveAndDisabled() |
|
1341 { |
|
1342 #if !defined(Q_WS_MAC) && !defined(Q_OS_WINCE_WM) |
|
1343 mb->hide(); |
|
1344 mb->clear(); |
|
1345 |
|
1346 // Task 241043 : check that second menu is activated if only |
|
1347 // disabled menu items are added |
|
1348 |
|
1349 QMenu fileMenu("&File"); |
|
1350 // Task 241043 : check that second menu is activated |
|
1351 // if all items are disabled |
|
1352 QAction *act = fileMenu.addAction("Disabled"); |
|
1353 act->setEnabled(false); |
|
1354 |
|
1355 mb->addMenu(&fileMenu); |
|
1356 QMenu disabledMenu("Disabled"); |
|
1357 disabledMenu.setEnabled(false); |
|
1358 QMenu activeMenu("Active"); |
|
1359 mb->addMenu(&disabledMenu); |
|
1360 mb->addMenu(&activeMenu); |
|
1361 mb->show(); |
|
1362 |
|
1363 |
|
1364 // Here we verify that AllowActiveAndDisabled correctly skips |
|
1365 // the disabled menu entry |
|
1366 QTest::keyClick(mb, Qt::Key_F, Qt::AltModifier ); |
|
1367 QTest::keyClick(&fileMenu, (Qt::Key_Right)); |
|
1368 if (qApp->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled)) |
|
1369 QCOMPARE(mb->activeAction()->text(), disabledMenu.title()); |
|
1370 else |
|
1371 QCOMPARE(mb->activeAction()->text(), activeMenu.title()); |
|
1372 |
|
1373 QTest::keyClick(mb, (Qt::Key_Left)); |
|
1374 if (qApp->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled)) |
|
1375 QCOMPARE(mb->activeAction()->text(), fileMenu.title()); |
|
1376 else |
|
1377 QCOMPARE(mb->activeAction()->text(), fileMenu.title()); |
|
1378 |
|
1379 mb->hide(); |
|
1380 #endif //Q_WS_MAC |
|
1381 } |
|
1382 |
|
1383 void tst_QMenuBar::check_altPress() |
|
1384 { |
|
1385 if ( !qApp->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation) ) { |
|
1386 QSKIP( QString( "this is not supposed to work in the %1 style. Skipping." ). |
|
1387 arg( qApp->style()->objectName() ).toAscii(), SkipAll ); |
|
1388 } |
|
1389 |
|
1390 #ifdef QT3_SUPPORT |
|
1391 initSimpleMenubar(); |
|
1392 #else |
|
1393 initSimpleMenubar_noQt3(); |
|
1394 #endif |
|
1395 |
|
1396 qApp->setActiveWindow(mw); |
|
1397 mw->setFocus(); |
|
1398 |
|
1399 QTest::keyClick( mw, Qt::Key_Alt ); |
|
1400 |
|
1401 QVERIFY( ::qobject_cast<QMenuBar *>(qApp->focusWidget()) ); |
|
1402 } |
|
1403 |
|
1404 void tst_QMenuBar::check_shortcutPress() |
|
1405 { |
|
1406 #if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM) |
|
1407 QSKIP("Qt/Mac,WinCE does not use the native popups/menubar", SkipAll); |
|
1408 #endif |
|
1409 |
|
1410 #ifdef QT3_SUPPORT |
|
1411 initComplexMenubar(); |
|
1412 #else |
|
1413 initComplexMenubar_noQt3(); |
|
1414 #endif |
|
1415 |
|
1416 qApp->setActiveWindow(mw); |
|
1417 QCOMPARE(menu3->selCount(), 0u); |
|
1418 QTest::keyClick(mw, Qt::Key_E, Qt::AltModifier); |
|
1419 QTest::qWait(200); |
|
1420 QCOMPARE(menu3->selCount(), 1u); |
|
1421 QVERIFY(!mb->activeAction()); |
|
1422 |
|
1423 QTest::keyClick(mw, Qt::Key_1, Qt::AltModifier ); |
|
1424 #ifdef QT3_SUPPORT |
|
1425 QVERIFY(pm1->isActiveWindow()); |
|
1426 #else |
|
1427 QVERIFY(pm1_Qt4->isActiveWindow()); |
|
1428 #endif |
|
1429 QTest::keyClick(mb, Qt::Key_2); |
|
1430 #ifdef QT3_SUPPORT |
|
1431 QVERIFY(pm1->isActiveWindow()); // Should still be the active window |
|
1432 #else |
|
1433 QVERIFY(pm1_Qt4->isActiveWindow()); |
|
1434 #endif |
|
1435 } |
|
1436 |
|
1437 void tst_QMenuBar::check_menuPosition() |
|
1438 { |
|
1439 #ifdef Q_WS_MAC |
|
1440 QSKIP("Qt/Mac does not use the native popups/menubar", SkipAll); |
|
1441 #endif |
|
1442 #ifdef Q_OS_WINCE_WM |
|
1443 QSKIP("Qt/CE uses native menubar", SkipAll); |
|
1444 #endif |
|
1445 QMenu menu; |
|
1446 #ifdef QT3_SUPPORT |
|
1447 initComplexMenubar(); |
|
1448 #else |
|
1449 initComplexMenubar_noQt3(); |
|
1450 #endif |
|
1451 menu.setTitle("&menu"); |
|
1452 QRect availRect = QApplication::desktop()->availableGeometry(mw); |
|
1453 QRect screenRect = QApplication::desktop()->screenGeometry(mw); |
|
1454 |
|
1455 while(menu.sizeHint().height() < (screenRect.height()*2/3)) { |
|
1456 menu.addAction("item"); |
|
1457 } |
|
1458 |
|
1459 QAction *menu_action = mw->menuBar()->addMenu(&menu); |
|
1460 |
|
1461 qApp->setActiveWindow(mw); |
|
1462 qApp->processEvents(); |
|
1463 |
|
1464 //the menu should be below the menubar item |
|
1465 { |
|
1466 mw->move(availRect.topLeft()); |
|
1467 QRect mbItemRect = mw->menuBar()->actionGeometry(menu_action); |
|
1468 mbItemRect.moveTo(mw->menuBar()->mapToGlobal(mbItemRect.topLeft())); |
|
1469 QTest::keyClick(mw, Qt::Key_M, Qt::AltModifier ); |
|
1470 QVERIFY(menu.isActiveWindow()); |
|
1471 QCOMPARE(menu.pos(), QPoint(mbItemRect.x(), mbItemRect.bottom() + 1)); |
|
1472 menu.close(); |
|
1473 } |
|
1474 |
|
1475 //the menu should be above the menubar item |
|
1476 { |
|
1477 mw->move(0,screenRect.bottom() - screenRect.height()/4); //just leave some place for the menubar |
|
1478 QRect mbItemRect = mw->menuBar()->actionGeometry(menu_action); |
|
1479 mbItemRect.moveTo(mw->menuBar()->mapToGlobal(mbItemRect.topLeft())); |
|
1480 QTest::keyClick(mw, Qt::Key_M, Qt::AltModifier ); |
|
1481 QVERIFY(menu.isActiveWindow()); |
|
1482 QCOMPARE(menu.pos(), QPoint(mbItemRect.x(), mbItemRect.top() - menu.height())); |
|
1483 menu.close(); |
|
1484 } |
|
1485 |
|
1486 //the menu should be on the side of the menubar item and should be "stuck" to the bottom of the screen |
|
1487 { |
|
1488 mw->move(0,screenRect.y() + screenRect.height()/2); //put it in the middle |
|
1489 QRect mbItemRect = mw->menuBar()->actionGeometry(menu_action); |
|
1490 mbItemRect.moveTo(mw->menuBar()->mapToGlobal(mbItemRect.topLeft())); |
|
1491 QTest::keyClick(mw, Qt::Key_M, Qt::AltModifier ); |
|
1492 QVERIFY(menu.isActiveWindow()); |
|
1493 QPoint firstPoint = QPoint(mbItemRect.right()+1, screenRect.bottom() - menu.height() + 1); |
|
1494 QPoint secondPoint = QPoint(mbItemRect.right()+1, availRect.bottom() - menu.height() + 1); |
|
1495 QVERIFY(menu.pos() == firstPoint || menu.pos() == secondPoint); |
|
1496 menu.close(); |
|
1497 } |
|
1498 |
|
1499 } |
|
1500 |
|
1501 void tst_QMenuBar::task223138_triggered() |
|
1502 { |
|
1503 qRegisterMetaType<QAction *>("QAction *"); |
|
1504 //we create a window with submenus and we check that both menubar and menus get the triggered signal |
|
1505 QMainWindow win; |
|
1506 QMenu *menu = win.menuBar()->addMenu("test"); |
|
1507 QAction *top = menu->addAction("toplevelaction"); |
|
1508 QMenu *submenu = menu->addMenu("nested menu"); |
|
1509 QAction *action = submenu->addAction("nested action"); |
|
1510 |
|
1511 QSignalSpy menubarSpy(win.menuBar(), SIGNAL(triggered(QAction*))); |
|
1512 QSignalSpy menuSpy(menu, SIGNAL(triggered(QAction*))); |
|
1513 QSignalSpy submenuSpy(submenu, SIGNAL(triggered(QAction*))); |
|
1514 |
|
1515 //let's trigger the first action |
|
1516 top->trigger(); |
|
1517 |
|
1518 QCOMPARE(menubarSpy.count(), 1); |
|
1519 QCOMPARE(menuSpy.count(), 1); |
|
1520 QCOMPARE(submenuSpy.count(), 0); |
|
1521 |
|
1522 menubarSpy.clear(); |
|
1523 menuSpy.clear(); |
|
1524 submenuSpy.clear(); |
|
1525 |
|
1526 //let's trigger the sub action |
|
1527 action->trigger(); |
|
1528 QCOMPARE(menubarSpy.count(), 1); |
|
1529 QCOMPARE(menuSpy.count(), 1); |
|
1530 QCOMPARE(submenuSpy.count(), 1); |
|
1531 } |
|
1532 |
|
1533 void tst_QMenuBar::task256322_highlight() |
|
1534 { |
|
1535 QMainWindow win; |
|
1536 win.menuBar()->setNativeMenuBar(false); //we can't check the geometry of native menubars |
|
1537 QMenu menu; |
|
1538 QAction *file = win.menuBar()->addMenu(&menu); |
|
1539 file->setText("file"); |
|
1540 QMenu menu2; |
|
1541 QAction *file2 = win.menuBar()->addMenu(&menu2); |
|
1542 file2->setText("file2"); |
|
1543 QAction *nothing = win.menuBar()->addAction("nothing"); |
|
1544 |
|
1545 win.show(); |
|
1546 QTest::qWait(200); |
|
1547 |
|
1548 QTest::mousePress(win.menuBar(), Qt::LeftButton, 0, win.menuBar()->actionGeometry(file).center()); |
|
1549 QTest::mouseMove(win.menuBar(), win.menuBar()->actionGeometry(file).center()); |
|
1550 QTest::mouseRelease(win.menuBar(), Qt::LeftButton, 0, win.menuBar()->actionGeometry(file).center()); |
|
1551 QTRY_VERIFY(menu.isVisible()); |
|
1552 QVERIFY(!menu2.isVisible()); |
|
1553 QCOMPARE(win.menuBar()->activeAction(), file); |
|
1554 |
|
1555 QTest::mousePress(win.menuBar(), Qt::LeftButton, 0, win.menuBar()->actionGeometry(file2).center()); |
|
1556 QTest::mouseMove(win.menuBar(), win.menuBar()->actionGeometry(file2).center()); |
|
1557 QTRY_VERIFY(!menu.isVisible()); |
|
1558 QVERIFY(menu2.isVisible()); |
|
1559 QCOMPARE(win.menuBar()->activeAction(), file2); |
|
1560 QTest::mouseRelease(win.menuBar(), Qt::LeftButton, 0, win.menuBar()->actionGeometry(file2).center()); |
|
1561 |
|
1562 QPoint nothingCenter = win.menuBar()->actionGeometry(nothing).center(); |
|
1563 QTest::mousePress(win.menuBar(), Qt::LeftButton, 0, nothingCenter); |
|
1564 QTest::mouseMove(win.menuBar(), nothingCenter); |
|
1565 QTRY_VERIFY(!menu2.isVisible()); |
|
1566 QVERIFY(!menu.isVisible()); |
|
1567 QCOMPARE(win.menuBar()->activeAction(), nothing); |
|
1568 QTest::mouseRelease(win.menuBar(), Qt::LeftButton, 0, nothingCenter); |
|
1569 } |
|
1570 |
|
1571 void tst_QMenuBar::menubarSizeHint() |
|
1572 { |
|
1573 struct MyStyle : public QWindowsStyle |
|
1574 { |
|
1575 virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0 ) const |
|
1576 { |
|
1577 // I chose strange values (prime numbers to be more sure that the size of the menubar is correct) |
|
1578 switch (metric) |
|
1579 { |
|
1580 case QStyle::PM_MenuBarItemSpacing: |
|
1581 return 7; |
|
1582 case PM_MenuBarHMargin: |
|
1583 return 13; |
|
1584 case PM_MenuBarVMargin: |
|
1585 return 11; |
|
1586 case PM_MenuBarPanelWidth: |
|
1587 return 1; |
|
1588 default: |
|
1589 return QWindowsStyle::pixelMetric(metric, option, widget); |
|
1590 } |
|
1591 } |
|
1592 } style; |
|
1593 |
|
1594 QMenuBar mb; |
|
1595 mb.setNativeMenuBar(false); //we can't check the geometry of native menubars |
|
1596 |
|
1597 mb.setStyle(&style); |
|
1598 //this is a list of arbitrary strings so that we check the geometry |
|
1599 QStringList list = QStringList() << "trer" << "ezrfgtgvqd" << "sdgzgzerzerzer" << "eerzertz" << "er"; |
|
1600 foreach(QString str, list) |
|
1601 mb.addAction(str); |
|
1602 |
|
1603 const int panelWidth = style.pixelMetric(QStyle::PM_MenuBarPanelWidth); |
|
1604 const int hmargin = style.pixelMetric(QStyle::PM_MenuBarHMargin); |
|
1605 const int vmargin = style.pixelMetric(QStyle::PM_MenuBarVMargin); |
|
1606 const int spacing = style.pixelMetric(QStyle::PM_MenuBarItemSpacing); |
|
1607 |
|
1608 mb.show(); |
|
1609 QRect result; |
|
1610 foreach(QAction *action, mb.actions()) { |
|
1611 const QRect actionRect = mb.actionGeometry(action); |
|
1612 if (!result.isNull()) //this is the first item |
|
1613 QCOMPARE(actionRect.left() - result.right() - 1, spacing); |
|
1614 result |= actionRect; |
|
1615 QCOMPARE(result.x(), panelWidth + hmargin + spacing); |
|
1616 QCOMPARE(result.y(), panelWidth + vmargin); |
|
1617 } |
|
1618 |
|
1619 //this code is copied from QMenuBar |
|
1620 //there is no public member that allows to initialize a styleoption instance |
|
1621 QStyleOptionMenuItem opt; |
|
1622 opt.rect = mb.rect(); |
|
1623 opt.menuRect = mb.rect(); |
|
1624 opt.state = QStyle::State_None; |
|
1625 opt.menuItemType = QStyleOptionMenuItem::Normal; |
|
1626 opt.checkType = QStyleOptionMenuItem::NotCheckable; |
|
1627 opt.palette = mb.palette(); |
|
1628 |
|
1629 QSize resSize = QSize(result.x(), result.y()) + result.size() |
|
1630 + QSize(panelWidth + hmargin, panelWidth + vmargin); |
|
1631 |
|
1632 |
|
1633 resSize = style.sizeFromContents(QStyle::CT_MenuBar, &opt, |
|
1634 resSize.expandedTo(QApplication::globalStrut()), |
|
1635 &mb); |
|
1636 |
|
1637 QCOMPARE(resSize, mb.sizeHint()); |
|
1638 } |
|
1639 |
|
1640 |
|
1641 #if defined(QT3_SUPPORT) |
|
1642 void tst_QMenuBar::indexBasedInsertion_data() |
|
1643 { |
|
1644 QTest::addColumn<int>("indexForInsertion"); |
|
1645 QTest::addColumn<int>("expectedIndex"); |
|
1646 |
|
1647 QTest::newRow("negative-index-appends") << -1 << 1; |
|
1648 QTest::newRow("prepend") << 0 << 0; |
|
1649 QTest::newRow("append") << 1 << 1; |
|
1650 } |
|
1651 |
|
1652 void tst_QMenuBar::indexBasedInsertion() |
|
1653 { |
|
1654 // test the compat'ed index based insertion |
|
1655 |
|
1656 QFETCH(int, indexForInsertion); |
|
1657 QFETCH(int, expectedIndex); |
|
1658 |
|
1659 { |
|
1660 QMenuBar menu; |
|
1661 menu.addAction("Regular Item"); |
|
1662 |
|
1663 menu.insertItem("New Item", -1 /*id*/, indexForInsertion); |
|
1664 |
|
1665 QAction *act = menu.actions().value(expectedIndex); |
|
1666 QVERIFY(act); |
|
1667 QCOMPARE(act->text(), QString("New Item")); |
|
1668 } |
|
1669 { |
|
1670 QMenuBar menu; |
|
1671 menu.addAction("Regular Item"); |
|
1672 |
|
1673 menu.insertSeparator(indexForInsertion); |
|
1674 |
|
1675 QAction *act = menu.actions().value(expectedIndex); |
|
1676 QVERIFY(act); |
|
1677 QVERIFY(act->isSeparator()); |
|
1678 } |
|
1679 } |
|
1680 #endif |
|
1681 |
|
1682 QTEST_MAIN(tst_QMenuBar) |
|
1683 #include "tst_qmenubar.moc" |