summaryrefslogtreecommitdiffstats
path: root/tests/auto/qmenubar/tst_qmenubar.cpp
blob: 2033bfa1517aa6c1f5ce2956662ec2622e3b8fdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>
#include <qapplication.h>
#include <qmainwindow.h>
#include <qmenubar.h>
#ifdef QT3_SUPPORT
#include <q3popupmenu.h>
#endif
#include <qstyle.h>
#include <qwindowsstyle.h>
#include <qdesktopwidget.h>
#include <qaction.h>
#include <qstyleoption.h>

#ifdef Q_WS_WIN
#include <windows.h>
#endif

#include <qobject.h>

#include "../../shared/util.h"

QT_FORWARD_DECLARE_CLASS(QMainWindow)

#include <qmenubar.h>

//TESTED_CLASS=
//TESTED_FILES=

class QtTestSlot : public QObject
{
    Q_OBJECT

public:
    QtTestSlot( QObject* parent = 0 ): QObject( parent ) { clear(); };
    virtual ~QtTestSlot() {};

    void clear() { sel_count = 0; };
    uint selCount() { return sel_count; };

public slots:
    void selected() { sel_count++; };

private:
    uint sel_count;
};

class Menu : public QMenu
{
    Q_OBJECT
        public slots:
            void addActions()
            {
                //this will change the geometry of the menu
                addAction("action1");
                addAction("action2");
            }
};

class tst_QMenuBar : public QObject
{
    Q_OBJECT
public:
    tst_QMenuBar();
    virtual ~tst_QMenuBar();

#ifdef QT3_SUPPORT
    void initSimpleMenubar();
#endif
    void initSimpleMenubar_noQt3();

#ifdef QT3_SUPPORT
    void initComplexMenubar();
#endif
    void initComplexMenubar_noQt3();

public slots:
    void initTestCase();
    void cleanupTestCase();
    void init();
private slots:
    void getSetCheck();

    void clear_noQt3();
    void removeItemAt_noQt3();
    void removeItemAt_noQt3_data();
    void removeItem_noQt3_data();
    void removeItem_noQt3();
    void count_noQt3();
    void insertItem_QString_QObject_noQt3();
    void accel_noQt3();
    void activatedCount_noQt3();
    void allowActiveAndDisabled();
#ifdef QT3_SUPPORT
    void clear();
    void removeItemAt_data();
    void removeItemAt();
    void removeItem_data();
    void removeItem();
    void count();
    void insertItem_QString_QObject();
    void accel();
    void activatedCount();
#endif

    void check_accelKeys();
    void check_cursorKeys1();
    void check_cursorKeys2();
    void check_cursorKeys3();

    void check_homeKey();
    void check_endKey();
#ifdef QT3_SUPPORT
    void check_escKey();
#endif
    void check_escKey_noQt3();

//     void check_mouse1_data();
//     void check_mouse1();
//     void check_mouse2_data();
//     void check_mouse2();

    void check_altPress();
    void check_shortcutPress();
    void check_menuPosition();
    void task223138_triggered();
    void task256322_highlight();
    void menubarSizeHint();
    void taskQTBUG4965_escapeEaten();

#if defined(QT3_SUPPORT)
    void indexBasedInsertion_data();
    void indexBasedInsertion();
#endif

protected slots:
#ifdef QT3_SUPPORT
    void onActivated( int );
#endif
    void onActivated_noQt3( QAction*);

private:

    void initTestCase_noQt3();

    QtTestSlot *menu1;
    QtTestSlot *menu2;
    QtTestSlot *menu3;
    QtTestSlot *menu4;

    QtTestSlot *item1_A;
    QtTestSlot *item1_B;
    QtTestSlot *item2_C;
    QtTestSlot *item2_D;
    QtTestSlot *item2_E;
    QtTestSlot *item2_F;
    QtTestSlot *item2_G;
    QtTestSlot *item2_H;

    void resetSlots();
    void resetCount();

    void reset() { resetSlots(); resetCount(); };

#ifdef QT3_SUPPORT
    int last_accel_id;
#endif
    QAction* last_accel_id_Qt4;
    int activated_count;

#ifdef QT3_SUPPORT
    int idAccel;
    int idAccel1;
#endif
    QAction *action;
    QAction *action1;
    QMainWindow *mw;
    QMenuBar *mb;
#ifdef QT3_SUPPORT
    Q3PopupMenu *pm1;
    Q3PopupMenu *pm2;
#endif
    QMenu *pm1_Qt4;
    QMenu *pm2_Qt4;
};

// Testing get/set functions
void tst_QMenuBar::getSetCheck()
{
    QMenuBar obj1;
    // QAction * QMenuBar::activeAction()
    // void QMenuBar::setActiveAction(QAction *)
    QAction *var1 = new QAction(0);
    obj1.setActiveAction(var1);
    QCOMPARE(var1, obj1.activeAction());
    obj1.setActiveAction((QAction *)0);
    QCOMPARE((QAction *)0, obj1.activeAction());
    delete var1;
}

////



#include <qcursor.h>

const int RESET = 0;

/*!
    Test plan:
	insertItem (all flavours and combinations)
	removing menu items
	clearing the menu

	check the common behaviour + emitted signals for:
	    accelerator keys
	    navigating tru the menu and then pressing ENTER
	    mouse clicks
	    mouse drags
	    combinations of key + mouse (if possible)
	    checked / unckecked state of menu options
	    active / inactive state

    Can't test these without pixmap comparison...
	show and hide
	icons in a menu
	pixmaps in a menu

*/

tst_QMenuBar::tst_QMenuBar()

{
    QApplication::setEffectEnabled(Qt::UI_AnimateMenu, false);


    activated_count = 0;
    mb = 0;
#ifdef QT3_SUPPORT
    pm1 = 0;
    pm2 = 0;
    last_accel_id = RESET;
#endif
    pm1_Qt4 = 0;
    pm2_Qt4 = 0;
    last_accel_id_Qt4 = 0;
}

tst_QMenuBar::~tst_QMenuBar()
{
    //delete mw; //#### cannot do that AFTER qapplication was destroyed!
    mw = 0;
}

void tst_QMenuBar::initTestCase()
{
#ifdef QT3_SUPPORT
    // create a default mainwindow
    // If you run a widget test, this will be replaced in the testcase by the
    // widget under test
    mw = new QMainWindow(0, Qt::X11BypassWindowManagerHint);
    mb = new QMenuBar( mw, "menubar" );
    connect( mb, SIGNAL(activated(int)), this, SLOT(onActivated(int)) );

    initSimpleMenubar();

    qApp->setMainWidget( mw );
    mw->show();
    qApp->setActiveWindow(mw);

    menu1 = new QtTestSlot( mw );
    menu2 = new QtTestSlot( mw );
    menu3 = new QtTestSlot( mw );
    menu4 = new QtTestSlot( mw );
    item1_A = new QtTestSlot( mw );
    item1_B = new QtTestSlot( mw );
    item2_C = new QtTestSlot( mw );
    item2_D = new QtTestSlot( mw );
    item2_E = new QtTestSlot( mw );
    item2_F = new QtTestSlot( mw );
    item2_G = new QtTestSlot( mw );
    item2_H = new QtTestSlot( mw );
#else
    initTestCase_noQt3();
#endif
}

void tst_QMenuBar::initTestCase_noQt3()
{
    // create a default mainwindow
    // If you run a widget test, this will be replaced in the testcase by the
    // widget under test
    mw = new QMainWindow(0, Qt::X11BypassWindowManagerHint);
    mb = new QMenuBar( mw );
    connect( mb, SIGNAL(triggered(QAction *)), this, SLOT(onActivated_noQt3(QAction *)) );

    initSimpleMenubar_noQt3();
    mw->show();

    menu1 = new QtTestSlot( mw );
    menu2 = new QtTestSlot( mw );
    menu3 = new QtTestSlot( mw );
    menu4 = new QtTestSlot( mw );
    item1_A = new QtTestSlot( mw );
    item1_B = new QtTestSlot( mw );
    item2_C = new QtTestSlot( mw );
    item2_D = new QtTestSlot( mw );
    item2_E = new QtTestSlot( mw );
    item2_F = new QtTestSlot( mw );
    item2_G = new QtTestSlot( mw );
    item2_H = new QtTestSlot( mw );
}


void tst_QMenuBar::cleanupTestCase()
{
    delete mw;
}

#if defined(QT3_SUPPORT)
void tst_QMenuBar::initSimpleMenubar()
{
    mb->hide();
    mb->clear();

    delete pm1;
    pm1  = new Q3PopupMenu( mb );
    idAccel = pm1->insertItem( "menu1", 123 );
//    pm->setAccel( ALT + Key_A, idAccel );
    pm1->setAccel( Qt::CTRL + Qt::Key_A, idAccel );
    mb->insertItem( "&accel", pm1 );
    connect( pm1, SIGNAL(activated(int)), this, SLOT(onActivated(int)));

    delete pm2;
    pm2 = new Q3PopupMenu( mb );
//    idAccel1 = pm2->insertItem( "&Open...", this, SLOT(onActivated(int)), Qt::Key_O, 456 );
    idAccel1 = pm2->insertItem( "&Open...", 0, 0, Qt::Key_O, 456 );
    connect(pm2, SIGNAL(activated(int)), this, SLOT(onActivated(int)));
    mb->insertItem( "accel1", pm2 );

    mb->show();
    qApp->syncX();
    qApp->processEvents();
}
#endif

void tst_QMenuBar::initSimpleMenubar_noQt3()
{
    mb->hide();
    mb->clear();

    delete pm1_Qt4;
    pm1_Qt4  = mb->addMenu("&accel");
    action = pm1_Qt4->addAction( "menu1" );
     action->setShortcut(QKeySequence("ALT+A"));
     action->setShortcut(QKeySequence("CTRL+A"));


    connect( pm1_Qt4, SIGNAL(triggered(QAction*)), this, SLOT(onActivated_noQt3(QAction*)));

    delete pm2_Qt4;
    pm2_Qt4  = mb->addMenu("accel1");

    action1 = pm2_Qt4->addAction( "&Open..." );
    action1->setShortcut(Qt::Key_O);
    connect(pm2_Qt4, SIGNAL(triggered(QAction*)), this, SLOT(onActivated_noQt3(QAction*)));

    mb->show();
    qApp->syncX();
    qApp->processEvents();
}

void tst_QMenuBar::init()
{
    resetSlots();
    resetCount();
}

void tst_QMenuBar::resetSlots()
{
    menu1->clear();
    menu2->clear();
    menu3->clear();
    menu4->clear();
    item1_A->clear();
    item1_B->clear();
    item2_C->clear();
    item2_D->clear();
    item2_E->clear();
    item2_F->clear();
    item2_G->clear();
    item2_H->clear();
}

void tst_QMenuBar::resetCount()
{
#ifdef QT3_SUPPORT
    last_accel_id = RESET;
#endif
    last_accel_id_Qt4 = 0;
    activated_count = 0;
}

#ifdef QT3_SUPPORT
void tst_QMenuBar::onActivated( int i )
{
    last_accel_id = i;
    activated_count++;
//     printf( QString("acceleratorId: %1, count: %1\n").arg( i ).arg(activated_count) );
}
#endif

void tst_QMenuBar::onActivated_noQt3( QAction* action )
{
    last_accel_id_Qt4 = action;
    activated_count++;
//     printf( QString("acceleratorId: %1, count: %1\n").arg( i ).arg(activated_count) );
}

#ifdef QT3_SUPPORT
void tst_QMenuBar::accel()
{
#ifdef Q_WS_MAC
    QSKIP("On Mac, native key events are needed to test menu action activation", SkipAll);
#endif
    // create a popup menu with menu items set the accelerators later...
    initSimpleMenubar();
//    QTest::keyClick( 0, Qt::Key_A, AltKey );
    QTest::keyClick( 0, Qt::Key_A, Qt::ControlModifier );
    QTest::qWait(300);

    QCOMPARE( last_accel_id, idAccel );
}
#endif //QT3_SUPPORT

void tst_QMenuBar::accel_noQt3()
{
#if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM)
    QSKIP("On Mac/WinCE, native key events are needed to test menu action activation", SkipAll);
#endif
#ifdef Q_OS_SYMBIAN
    QSKIP("On Symbian OS, native key events are needed to test menu action activation", SkipAll);
#endif

    // create a popup menu with menu items set the accelerators later...
    initSimpleMenubar_noQt3();
//    QTest::keyClick( 0, Qt::Key_A, AltKey );
    QTest::keyClick( 0, Qt::Key_A, Qt::ControlModifier );
    QTest::qWait(300);

    QCOMPARE( last_accel_id_Qt4, action );
}

#ifdef QT3_SUPPORT
void tst_QMenuBar::activatedCount()
{
#ifdef Q_WS_MAC
    QSKIP("On Mac, native key events are needed to test menu action activation", SkipAll);
#endif
    // create a popup menu with menu items set the accelerators later...
    initSimpleMenubar();

    QTest::keyClick( 0, Qt::Key_A, Qt::ControlModifier );
//wait(5000);
    QCOMPARE( activated_count, 2 ); //1 from the popupmenu and 1 from the menubar
}
#endif //QT3_SUPPORT

void tst_QMenuBar::activatedCount_noQt3()
{
#if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM)
    QSKIP("On Mac/WinCE, native key events are needed to test menu action activation", SkipAll);
#endif
    // create a popup menu with menu items set the accelerators later...
    initSimpleMenubar_noQt3();

    QTest::keyClick( 0, Qt::Key_A, Qt::ControlModifier );
//wait(5000);
    QCOMPARE( activated_count, 2 ); //1 from the popupmenu and 1 from the menubar
}

void tst_QMenuBar::clear_noQt3()
{
    mb->clear();
    QVERIFY( (uint) mb->actions().size() == 0 );

    mb->clear();
    for (uint i=0; i<10; i++) {
        QMenu *menu = mb->addMenu( QString("Menu %1"));
	for (uint k=0; k<i; k++)
	    menu->addAction( QString("Item %1"));
	QCOMPARE( (uint) mb->actions().size(), (uint)i+1 );
    }
    QCOMPARE( (uint) mb->actions().size(), 10u );

    mb->clear();
    QVERIFY( (uint) mb->actions().size() == 0 );
}

void tst_QMenuBar::count_noQt3()
{
    mb->clear();
    QVERIFY( mb->actions().size() == 0 );

    for (uint i=0; i<10; i++) {
	mb->addAction( QString("Menu %1"));
	QCOMPARE( (uint) mb->actions().size(), (uint) i+1 );
    }
}

void tst_QMenuBar::removeItem_noQt3_data()
{
    QTest::addColumn<int>("removeIndex");
    QTest::newRow( "first" ) << 0;
    QTest::newRow( "middle" ) << 1;
    QTest::newRow( "last" ) << 2;
}

// Basically the same test as removeItemAt, except that we remember and remove id's.
void tst_QMenuBar::removeItem_noQt3()
{
    mb->clear();

    QMenu *pm;
    pm = new QMenu( "stuff", mb );
    pm->setTitle("Menu 1");
    pm->addAction( QString("Item 10") );
    QAction* action1 = mb->addMenu( pm );

    pm = new QMenu( mb );
    pm->setTitle("Menu 2");
    pm->addAction( QString("Item 20") );
    pm->addAction( QString("Item 21") );
    QAction *action2 = mb->addMenu( pm );

    pm = new QMenu( "Menu 3", mb );
    pm->addAction( QString("Item 30") );
    pm->addAction( QString("Item 31") );
    pm->addAction( QString("Item 32") );
    QAction *action3 = mb->addMenu( pm );

    QList<QAction *> menuBarActions = mb->actions();

    QCOMPARE( action1->text(), QString("Menu 1") );
    QCOMPARE( action2->text(), QString("Menu 2") );
    QCOMPARE( action3->text(), QString("Menu 3") );

    QVERIFY( menuBarActions.at(0) == action1 );
    QVERIFY( menuBarActions.at(1) == action2 );
    QVERIFY( menuBarActions.at(2) == action3 );

    // Ok, now that we know we have created the menu we expect, lets remove an item...
    QFETCH( int, removeIndex );
    switch (removeIndex )
    {
    case 0: {
            mb->removeAction(action1);
            QList<QAction *> menuBarActions2 = mb->actions();
            QCOMPARE( menuBarActions2.at(0)->text(), QString("Menu 2") );
            QCOMPARE( menuBarActions2.at(1)->text(), QString("Menu 3") );
        }
        break;
    case 1: {
            mb->removeAction(action2);
            QList<QAction *> menuBarActions2 = mb->actions();
            QCOMPARE( menuBarActions2.at(0)->text(), QString("Menu 1") );
            QCOMPARE( menuBarActions2.at(1)->text(), QString("Menu 3") );
        }
        break;
    case 2: {
            mb->removeAction(action3);
            QList<QAction *> menuBarActions2 = mb->actions();
            QCOMPARE( menuBarActions2.at(0)->text(), QString("Menu 1") );
            QCOMPARE( menuBarActions2.at(1)->text(), QString("Menu 2") );
        }
        break;
    }
    QList<QAction *> menuBarActions2 = mb->actions();
    QVERIFY( menuBarActions2.size() == 2 );
}

void tst_QMenuBar::removeItemAt_noQt3_data()
{
    QTest::addColumn<int>("removeIndex");
    QTest::newRow( "first" ) << 0;
    QTest::newRow( "middle" ) << 1;
    QTest::newRow( "last" ) << 2;
}

void tst_QMenuBar::removeItemAt_noQt3()
{
    mb->clear();

    QMenu *pm;
    pm = new QMenu("Menu 1", mb);
    pm->addAction( QString("Item 10") );
    mb->addMenu( pm );

    pm = new QMenu( "Menu 2", mb );
    pm->addAction( QString("Item 20") );
    pm->addAction( QString("Item 21") );
    mb->addMenu( pm );

    pm = new QMenu( "Menu 3", mb );
    pm->addAction( QString("Item 30") );
    pm->addAction( QString("Item 31") );
    pm->addAction( QString("Item 32") );
    mb->addMenu( pm );

    QList<QAction *> menuBarActions = mb->actions();

    QCOMPARE( menuBarActions.at(0)->text(), QString("Menu 1") );
    QCOMPARE( menuBarActions.at(1)->text(), QString("Menu 2") );
    QCOMPARE( menuBarActions.at(2)->text(), QString("Menu 3") );

    // Ok, now that we know we have created the menu we expect, lets remove an item...
    QFETCH( int, removeIndex );
    mb->removeAction(menuBarActions.at(removeIndex));
    QList<QAction *> menuBarActions2 = mb->actions();
    switch (removeIndex )
    {
    case 0:
	QCOMPARE( menuBarActions2.at(0)->text(), QString("Menu 2") );
	QCOMPARE( menuBarActions2.at(1)->text(), QString("Menu 3") );
	break;
    case 1:
	QCOMPARE( menuBarActions2.at(0)->text(), QString("Menu 1") );
	QCOMPARE( menuBarActions2.at(1)->text(), QString("Menu 3") );
	break;
    case 2:
	QCOMPARE( menuBarActions2.at(0)->text(), QString("Menu 1") );
	QCOMPARE( menuBarActions2.at(1)->text(), QString("Menu 2") );
	break;
    }

    QVERIFY( menuBarActions2.size() == 2 );
}

#ifdef QT3_SUPPORT
void tst_QMenuBar::clear()
{
    mb->clear();
    QVERIFY( mb->count() == 0 );

    mb->clear();
    for (uint i=0; i<10; i++) {
	Q3PopupMenu *pm  = new Q3PopupMenu( mb );
	for (uint k=0; k<i; k++)
	    pm->insertItem( QString("Item %1").arg(i*10 + k) );
	mb->insertItem( QString("Menu %1").arg(i), pm );
	QCOMPARE( mb->count(), (uint)i+1 );
    }
    QCOMPARE( mb->count(), 10u );

    mb->clear();
    QVERIFY( mb->count() == 0 );
}

void tst_QMenuBar::count()
{
    mb->clear();
    QVERIFY( mb->count() == 0 );

    for (uint i=0; i<10; i++) {
	Q3PopupMenu *pm  = new Q3PopupMenu( mb );
	mb->insertItem( QString("Menu %1").arg(i), pm );
	QCOMPARE( mb->count(), i+1 );
    }
}

void tst_QMenuBar::removeItemAt_data()
{
    QTest::addColumn<int>("removeIndex");
    QTest::newRow( "first" ) << 0;
    QTest::newRow( "middle" ) << 1;
    QTest::newRow( "last" ) << 2;
}

void tst_QMenuBar::removeItemAt()
{
    mb->clear();

    Q3PopupMenu *pm;
    pm = new Q3PopupMenu( mb );
    pm->insertItem( QString("Item 10") );
    mb->insertItem( QString("Menu 1"), pm );

    pm = new Q3PopupMenu( mb );
    pm->insertItem( QString("Item 20") );
    pm->insertItem( QString("Item 21") );
    mb->insertItem( QString("Menu 2"), pm );

    pm = new Q3PopupMenu( mb );
    pm->insertItem( QString("Item 30") );
    pm->insertItem( QString("Item 31") );
    pm->insertItem( QString("Item 32") );
    mb->insertItem( QString("Menu 3"), pm );

    QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 1") );
    QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 2") );
    QCOMPARE( mb->text( mb->idAt(2) ), QString("Menu 3") );

    // Ok, now that we know we have created the menu we expect, lets remove an item...
    QFETCH( int, removeIndex );
    mb->removeItemAt( removeIndex );
    switch (removeIndex )
    {
    case 0:
	QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 2") );
	QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 3") );
	break;
    case 1:
	QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 1") );
	QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 3") );
	break;
    case 2:
	QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 1") );
	QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 2") );
	break;
    }

    QVERIFY( mb->count() == 2 );
}

void tst_QMenuBar::removeItem_data()
{
    QTest::addColumn<int>("removeIndex");
    QTest::newRow( "first" ) << 0;
    QTest::newRow( "middle" ) << 1;
    QTest::newRow( "last" ) << 2;
}

// Basically the same test as removeItemAt, except that we remember and remove id's.
void tst_QMenuBar::removeItem()
{
    mb->clear();

    Q3PopupMenu *pm;
    pm = new Q3PopupMenu( mb );
    pm->insertItem( QString("Item 10") );
    int id1 = mb->insertItem( QString("Menu 1"), pm );

    pm = new Q3PopupMenu( mb );
    pm->insertItem( QString("Item 20") );
    pm->insertItem( QString("Item 21") );
    int id2 = mb->insertItem( QString("Menu 2"), pm );

    pm = new Q3PopupMenu( mb );
    pm->insertItem( QString("Item 30") );
    pm->insertItem( QString("Item 31") );
    pm->insertItem( QString("Item 32") );
    int id3 = mb->insertItem( QString("Menu 3"), pm );

    QCOMPARE( mb->text( id1 ), QString("Menu 1") );
    QCOMPARE( mb->text( id2 ), QString("Menu 2") );
    QCOMPARE( mb->text( id3 ), QString("Menu 3") );

    QVERIFY( mb->idAt(0) == id1 );
    QVERIFY( mb->idAt(1) == id2 );
    QVERIFY( mb->idAt(2) == id3 );

    // Ok, now that we know we have created the menu we expect, lets remove an item...
    QFETCH( int, removeIndex );
    switch (removeIndex )
    {
    case 0:
	mb->removeItem( id1 );
	QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 2") );
	QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 3") );
	break;
    case 1:
	mb->removeItem( id2 );
	QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 1") );
	QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 3") );
	break;
    case 2:
	mb->removeItem( id3 );
	QCOMPARE( mb->text( mb->idAt(0) ), QString("Menu 1") );
	QCOMPARE( mb->text( mb->idAt(1) ), QString("Menu 2") );
	break;
    }

    QVERIFY( mb->count() == 2 );
}

void tst_QMenuBar::initComplexMenubar() // well, complex....
{
    mb->hide();
    mb->clear();

    delete pm1;
    pm1 = new Q3PopupMenu( mb, "popup1" );
    pm1->insertItem( QString("Item A"), item1_A, SLOT(selected()), Qt::CTRL+Qt::Key_A );
    pm1->insertItem( QString("Item B"), item1_B, SLOT(selected()), Qt::CTRL+Qt::Key_B );
    // use the form insertItem( QString, Q3PopupMenu )
    mb->insertItem( "Menu &1", pm1 );

    delete pm2;
    pm2 = new Q3PopupMenu( mb, "popup2" );
    pm2->insertItem( QString("Item C"), item2_C, SLOT(selected()), Qt::CTRL+Qt::Key_C );
    pm2->insertItem( QString("Item D"), item2_D, SLOT(selected()), Qt::CTRL+Qt::Key_D );
    pm2->insertItem( QString("Item E"), item2_E, SLOT(selected()), Qt::CTRL+Qt::Key_E );
    pm2->insertItem( QString("Item F"), item2_F, SLOT(selected()), Qt::CTRL+Qt::Key_F );
    pm2->insertSeparator();
    pm2->insertItem( QString("Item G"), item2_G, SLOT(selected()), Qt::CTRL+Qt::Key_G );
    pm2->insertItem( QString("Item H"), item2_H, SLOT(selected()), Qt::CTRL+Qt::Key_H );
    // use the form insertItem( QString, Q3PopupMenu )
    mb->insertItem( "Menu &2", pm2 );

    // use the form insertItem( QString, QObject, slot, keysequence )
    mb->insertItem( QString("M&enu 3"), menu3, SLOT(selected()), Qt::ALT+Qt::Key_J );
    mb->show();
}
#endif

void tst_QMenuBar::initComplexMenubar_noQt3() // well, complex....
{
    mb->hide();
    mb->clear();

    delete pm1_Qt4;
    pm1_Qt4 = mb->addMenu("Menu &1");
    pm1_Qt4->addAction( QString("Item A"), item1_A, SLOT(selected()), Qt::CTRL+Qt::Key_A );
    pm1_Qt4->addAction( QString("Item B"), item1_B, SLOT(selected()), Qt::CTRL+Qt::Key_B );

    delete pm2_Qt4;
    pm2_Qt4 = mb->addMenu("Menu &2");
    pm2_Qt4->addAction( QString("Item C"), item2_C, SLOT(selected()), Qt::CTRL+Qt::Key_C );
    pm2_Qt4->addAction( QString("Item D"), item2_D, SLOT(selected()), Qt::CTRL+Qt::Key_D );
    pm2_Qt4->addAction( QString("Item E"), item2_E, SLOT(selected()), Qt::CTRL+Qt::Key_E );
    pm2_Qt4->addAction( QString("Item F"), item2_F, SLOT(selected()), Qt::CTRL+Qt::Key_F );
    pm2_Qt4->addSeparator();
    pm2_Qt4->addAction( QString("Item G"), item2_G, SLOT(selected()), Qt::CTRL+Qt::Key_G );
    pm2_Qt4->addAction( QString("Item H"), item2_H, SLOT(selected()), Qt::CTRL+Qt::Key_H );

    QAction *ac = mb->addAction( QString("M&enu 3"), menu3, SLOT(selected()));
    ac->setShortcut(Qt::ALT+Qt::Key_J);
    mb->show();
}


/*
    Check the insert functions that create menu items.
    For the moment i only check the strings and pixmaps. The rest are special cases which are
    used less frequently.
*/

#ifdef QT3_SUPPORT
void tst_QMenuBar::insertItem_QString_QObject()
{
    initComplexMenubar();
    QCOMPARE( mb->text( mb->idAt( 0 ) ), QString("Menu &1") );
    QCOMPARE( mb->text( mb->idAt( 1 ) ), QString("Menu &2") );
    QCOMPARE( mb->text( mb->idAt( 2 ) ), QString("M&enu 3") );
    QCOMPARE( mb->text( mb->idAt( 3 ) ), QString() ); // there is no menu 4!
}
#endif

void tst_QMenuBar::insertItem_QString_QObject_noQt3()
{
    initComplexMenubar_noQt3();

    QList<QAction *> actions = mb->actions();

    QCOMPARE(actions.at(0)->text(), QString("Menu &1") );
    QCOMPARE(actions.at(1)->text(), QString("Menu &2") );
    QCOMPARE(actions.at(2)->text(), QString("M&enu 3") );
    QVERIFY(actions.size() < 4); // there is no menu 4!
}

void tst_QMenuBar::check_accelKeys()
{
#if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM)
    QSKIP("On Mac/WinCE, native key events are needed to test menu action activation", SkipAll);
#endif
#ifdef QT3_SUPPORT
    initComplexMenubar();
#else
    initComplexMenubar_noQt3();
#endif

    // start with a bogus key that shouldn't trigger anything
    QTest::keyClick(0, Qt::Key_I, Qt::ControlModifier);
    QCOMPARE(menu1->selCount(), 0u);
    QCOMPARE(menu2->selCount(), 0u);
    QCOMPARE(menu3->selCount(), 0u);
    QCOMPARE(menu4->selCount(), 0u);
    QCOMPARE(item1_A->selCount(), 0u);
    QCOMPARE(item1_B->selCount(), 0u);
    QCOMPARE(item2_C->selCount(), 0u);
    QCOMPARE(item2_D->selCount(), 0u);

    QTest::keyClick(0, Qt::Key_A, Qt::ControlModifier);
    QCOMPARE(menu1->selCount(), 0u);
    QCOMPARE(menu2->selCount(), 0u);
    QCOMPARE(menu3->selCount(), 0u);
    QCOMPARE(menu4->selCount(), 0u);
    QCOMPARE(item1_A->selCount(), 1u);
    QCOMPARE(item1_B->selCount(), 0u);
    QCOMPARE(item2_C->selCount(), 0u);
    QCOMPARE(item2_D->selCount(), 0u);

    QTest::keyClick(0, Qt::Key_C, Qt::ControlModifier);
    QCOMPARE(menu1->selCount(), 0u);
    QCOMPARE(menu2->selCount(), 0u);
    QCOMPARE(menu3->selCount(), 0u);
    QCOMPARE(menu4->selCount(), 0u);
    QCOMPARE(item1_A->selCount(), 1u);
    QCOMPARE(item1_B->selCount(), 0u);
    QCOMPARE(item2_C->selCount(), 1u);
    QCOMPARE(item2_D->selCount(), 0u);

    QTest::keyClick(0, Qt::Key_B, Qt::ControlModifier);
    QCOMPARE(menu1->selCount(), 0u);
    QCOMPARE(menu2->selCount(), 0u);
    QCOMPARE(menu3->selCount(), 0u);
    QCOMPARE(menu4->selCount(), 0u);
    QCOMPARE(item1_A->selCount(), 1u);
    QCOMPARE(item1_B->selCount(), 1u);
    QCOMPARE(item2_C->selCount(), 1u);
    QCOMPARE(item2_D->selCount(), 0u);

    QTest::keyClick(0, Qt::Key_D, Qt::ControlModifier);
    QCOMPARE(menu1->selCount(), 0u);
    QCOMPARE(menu2->selCount(), 0u);
    QCOMPARE(menu3->selCount(), 0u);
    QCOMPARE(menu4->selCount(), 0u);
    QCOMPARE(item1_A->selCount(), 1u);
    QCOMPARE(item1_B->selCount(), 1u);
    QCOMPARE(item2_C->selCount(), 1u);
    QCOMPARE(item2_D->selCount(), 1u);

    QTest::keyClick(0, Qt::Key_J, Qt::AltModifier);
    QCOMPARE(menu1->selCount(), 0u);
    QCOMPARE(menu2->selCount(), 0u);
    QCOMPARE(menu3->selCount(), 1u);
    QCOMPARE(menu4->selCount(), 0u);
    QCOMPARE(item1_A->selCount(), 1u);
    QCOMPARE(item1_B->selCount(), 1u);
    QCOMPARE(item2_C->selCount(), 1u);
    QCOMPARE(item2_D->selCount(), 1u);
}

void tst_QMenuBar::check_cursorKeys1()
{
#if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM)
    QSKIP("Qt/Mac,WinCE does not use the native popups/menubar", SkipAll);
#endif

#ifdef QT3_SUPPORT
    initComplexMenubar();
#else
    initComplexMenubar_noQt3();
#endif

    // start with a ALT + 1 that activates the first popupmenu
    QTest::keyClick( 0, Qt::Key_1, Qt::AltModifier );
    // the Popupmenu should be visible now
    QCOMPARE(menu3->selCount(), 0u);
    QCOMPARE(menu4->selCount(), 0u);
    QCOMPARE(item1_A->selCount(), 0u);
    QCOMPARE(item1_B->selCount(), 0u);
    QCOMPARE(item2_C->selCount(), 0u);
    QCOMPARE(item2_D->selCount(), 0u);

    // Simulate a cursor key down click
    QTest::keyClick( 0, Qt::Key_Down );
    // and an Enter key
    QTest::keyClick( 0, Qt::Key_Enter );
    // Let's see if the correct slot is called...
    QCOMPARE(menu3->selCount(), 0u);
    QCOMPARE(menu4->selCount(), 0u);
    QCOMPARE(item1_A->selCount(), 0u); // this shouldn't have been called
    QCOMPARE(item1_B->selCount(), 1u); // and this should have been called by a signal now
    QCOMPARE(item2_C->selCount(), 0u);
    QCOMPARE(item2_D->selCount(), 0u);
}

void tst_QMenuBar::check_cursorKeys2()
{
#if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM)
    QSKIP("Qt/Mac,WinCE does not use the native popups/menubar", SkipAll);
#endif

#ifdef QT3_SUPPORT
    initComplexMenubar();
#else
    initComplexMenubar_noQt3();
#endif

    // select popupmenu2
    QTest::keyClick( 0, Qt::Key_2, Qt::AltModifier );

    // Simulate some cursor keys
    QTest::keyClick( 0, Qt::Key_Left );
    QTest::keyClick( 0, Qt::Key_Down );
    QTest::keyClick( 0, Qt::Key_Right );
    QTest::keyClick( 0, Qt::Key_Down );
    // and an Enter key
    QTest::keyClick( 0, Qt::Key_Enter );
    // Let's see if the correct slot is called...
    QCOMPARE(menu3->selCount(), 0u);
    QCOMPARE(menu4->selCount(), 0u);
    QCOMPARE(item1_A->selCount(), 0u); // this shouldn't have been caled
    QCOMPARE(item1_B->selCount(), 0u); // and this should have been called by a signal ow
    QCOMPARE(item2_C->selCount(), 0u);
    QCOMPARE(item2_D->selCount(), 1u);
}

/*!
    If a popupmenu is active you can use Left to move to the menu to the left of it.
*/
void tst_QMenuBar::check_cursorKeys3()
{
#if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM)
    QSKIP("Qt/Mac,WinCE does not use the native popups/menubar", SkipAll);
#endif

#ifdef QT3_SUPPORT
    initComplexMenubar();
#else
    initComplexMenubar_noQt3();
#endif

    // select Popupmenu 2
    QTest::keyClick( 0, Qt::Key_2, Qt::AltModifier );

    // Simulate some keys
    QTest::keyClick( 0, Qt::Key_Left );
    QTest::keyClick( 0, Qt::Key_Down );
    // and press ENTER
    QTest::keyClick( 0, Qt::Key_Enter );
    // Let's see if the correct slot is called...
    QCOMPARE(menu3->selCount(), 0u);
    QCOMPARE(menu4->selCount(), 0u);
    QCOMPARE(item1_A->selCount(), 0u); // this shouldn't have been called
    QCOMPARE(item1_B->selCount(), 1u); // and this should have been called by a signal now
    QCOMPARE(item2_C->selCount(), 0u);
    QCOMPARE(item2_D->selCount(), 0u);
}

/*!
    If a popupmenu is active you can use home to go quickly to the first item in the menu.
*/
void tst_QMenuBar::check_homeKey()
{
    // I'm temporarily shutting up this testcase.
    // Seems like the behaviour i'm expecting isn't ok.
    QVERIFY( TRUE );
    return;

    QEXPECT_FAIL( "0", "Popupmenu should respond to a Home key", Abort );

#ifdef QT3_SUPPORT
    initComplexMenubar();
#else
    initComplexMenubar_noQt3();
#endif

    // select Popupmenu 2
    QTest::keyClick( 0, Qt::Key_2, Qt::AltModifier );

    // Simulate some keys
    QTest::keyClick( 0, Qt::Key_Down );
    QTest::keyClick( 0, Qt::Key_Down );
    QTest::keyClick( 0, Qt::Key_Down );
    QTest::keyClick( 0, Qt::Key_Home );
    // and press ENTER
    QTest::keyClick( 0, Qt::Key_Enter );
    // Let's see if the correct slot is called...
//    QVERIFY2( item2_C->selCount() == 1, "Popupmenu should respond to a Home key" );
    QCOMPARE(item2_C->selCount(), 1u);
    QCOMPARE(menu3->selCount(), 0u);
    QCOMPARE(menu4->selCount(), 0u);
    QCOMPARE(item1_A->selCount(), 0u);
    QCOMPARE(item1_B->selCount(), 0u);
    QCOMPARE(item2_D->selCount(), 0u);
    QCOMPARE(item2_E->selCount(), 0u);
    QCOMPARE(item2_F->selCount(), 0u);
    QCOMPARE(item2_G->selCount(), 0u);
    QCOMPARE(item2_H->selCount(), 0u);
}

/*!
    If a popupmenu is active you can use end to go quickly to the last item in the menu.
*/
void tst_QMenuBar::check_endKey()
{
    // I'm temporarily silenting this testcase.
    // Seems like the behaviour i'm expecting isn't ok.
    QVERIFY( TRUE );
    return;

    QEXPECT_FAIL( "0", "Popupmenu should respond to an End key", Abort );

#ifdef QT3_SUPPORT
    initComplexMenubar();
#else
    initComplexMenubar_noQt3();
#endif

    // select Popupmenu 2
    QTest::keyClick( 0, Qt::Key_2, Qt::AltModifier );

    // Simulate some keys
    QTest::keyClick( 0, Qt::Key_End );
    // and press ENTER
    QTest::keyClick( 0, Qt::Key_Enter );
    // Let's see if the correct slot is called...
//    QVERIFY2( item2_H->selCount() == 1, "Popupmenu should respond to an End key" );
    QCOMPARE(item2_H->selCount(), 1u);//, "Popupmenu should respond to an End key");
    QCOMPARE(menu3->selCount(), 0u);
    QCOMPARE(menu4->selCount(), 0u);
    QCOMPARE(item1_A->selCount(), 0u);
    QCOMPARE(item1_B->selCount(), 0u);
    QCOMPARE(item2_C->selCount(), 0u);
    QCOMPARE(item2_D->selCount(), 0u);
    QCOMPARE(item2_E->selCount(), 0u);
    QCOMPARE(item2_F->selCount(), 0u);
    QCOMPARE(item2_G->selCount(), 0u);
}

/*!
    If a popupmenu is active you can use esc to hide the menu and then the
    menubar should become active.
    If Down is pressed next the popup is activated again.
*/

#ifdef QT3_SUPPORT
void tst_QMenuBar::check_escKey()
{
#ifdef Q_WS_MAC
    QSKIP("Qt/Mac does not use the native popups/menubar", SkipAll);
#endif

    initComplexMenubar();

    QVERIFY( !pm1->isActiveWindow() );
    QVERIFY( !pm2->isActiveWindow() );

    // select Popupmenu 2
    QTest::keyClick( 0, Qt::Key_2, Qt::AltModifier );
    QVERIFY( !pm1->isActiveWindow() );
    QVERIFY( pm2->isActiveWindow() );

    // If we press ESC, the popup should disappear
    QTest::keyClick( 0, Qt::Key_Escape );
    QVERIFY( !pm1->isActiveWindow() );
    QVERIFY( !pm2->isActiveWindow() );

    if (!QApplication::style()->inherits("QWindowsStyle"))
        return;

    // but the menubar item should stay selected
    QVERIFY( mb->isItemActive(mb->idAt(1)) );

    // If we press Down the popupmenu should be active again
    QTest::keyClick( 0, Qt::Key_Down );
    QVERIFY( !pm1->isActiveWindow() );
    QVERIFY( pm2->isActiveWindow() );

    // and press ENTER
    QTest::keyClick( pm2, Qt::Key_Enter );
    // Let's see if the correct slot is called...
    QVERIFY2( item2_C->selCount() == 1, "Expected item 2C to be selected" );
}
#endif

void tst_QMenuBar::check_escKey_noQt3()
{
#if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM)
    QSKIP("Qt/Mac,WinCE does not use the native popups/menubar", SkipAll);
#endif

    initComplexMenubar_noQt3();

    QVERIFY( !pm1_Qt4->isActiveWindow() );
    QVERIFY( !pm2_Qt4->isActiveWindow() );

    // select Popupmenu 2
    QTest::keyClick( 0, Qt::Key_2, Qt::AltModifier );
    QVERIFY( !pm1_Qt4->isActiveWindow() );
    QVERIFY( pm2_Qt4->isActiveWindow() );

    // If we press ESC, the popup should disappear
    QTest::keyClick( 0, Qt::Key_Escape );
    QVERIFY( !pm1_Qt4->isActiveWindow() );
    QVERIFY( !pm2_Qt4->isActiveWindow() );

    if (!QApplication::style()->inherits("QWindowsStyle"))
        return;

    // If we press Down the popupmenu should be active again
    QTest::keyClick( 0, Qt::Key_Down );
    QVERIFY( !pm1_Qt4->isActiveWindow() );
    QVERIFY( pm2_Qt4->isActiveWindow() );

    // and press ENTER
    QTest::keyClick( pm2_Qt4, Qt::Key_Enter );
    // Let's see if the correct slot is called...
    QVERIFY2( item2_C->selCount() == 1, "Expected item 2C to be selected" );
}


// void tst_QMenuBar::check_mouse1_data()
// {
//     QTest::addColumn<QString>("popup_item");
//     QTest::addColumn<int>("itemA_count");
//     QTest::addColumn<int>("itemB_count");

//     QTest::newRow( "A" ) << QString( "Item A Ctrl+A" ) << 1 << 0;
//     QTest::newRow( "B" ) << QString( "Item B Ctrl+B" ) << 0 << 1;
// }

// /*!
//     Check if the correct signals are emitted if we select a popupmenu.
// */
// void tst_QMenuBar::check_mouse1()
// {
//     if (QSystem::curStyle() == "Motif")
// 	QSKIP("This fails in Motif due to a bug in the testing framework", SkipAll);
//     QFETCH( QString, popup_item );
//     QFETCH( int, itemA_count );
//     QFETCH( int, itemB_count );

//     initComplexMenubar();
//     QVERIFY( !pm1->isActiveWindow() );
//     QVERIFY( !pm2->isActiveWindow() );

//     QTest::qWait(1000);
//     QtTestMouse mouse;
//     mouse.mouseEvent( QtTestMouse::MouseClick, mb, "Menu &1", Qt::LeftButton );

//     QVERIFY( pm1->isActiveWindow() );
//     QVERIFY( !pm2->isActiveWindow() );

//     QTest::qWait(1000);
//     mouse.mouseEvent( QtTestMouse::MouseClick, pm1, popup_item, Qt::LeftButton );

//     QCOMPARE(menu3->selCount(), 0u);
//     QCOMPARE(menu4->selCount(), 0u);
//     QCOMPARE(item1_A->selCount(), (uint)itemA_count); // this option should have fired
//     QCOMPARE(item1_B->selCount(), (uint)itemB_count);
//     QCOMPARE(item2_C->selCount(), 0u);
//     QCOMPARE(item2_D->selCount(), 0u);
//     QCOMPARE(item2_E->selCount(), 0u);
//     QCOMPARE(item2_F->selCount(), 0u);
//     QCOMPARE(item2_G->selCount(), 0u);
// }

// void tst_QMenuBar::check_mouse2_data()
// {
//     QTest::addColumn<QString>("label");
//     QTest::addColumn<int>("itemA_count");
//     QTest::addColumn<int>("itemB_count");
//     QTest::addColumn<int>("itemC_count");
//     QTest::addColumn<int>("itemD_count");
//     QTest::addColumn<int>("itemE_count");
//     QTest::addColumn<int>("itemF_count");
//     QTest::addColumn<int>("itemG_count");
//     QTest::addColumn<int>("itemH_count");
//     QTest::addColumn<int>("menu3_count");

//     QTest::newRow( "A" ) << QString( "Menu &1/Item A Ctrl+A" ) << 1 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0;
//     QTest::newRow( "B" ) << QString( "Menu &1/Item B Ctrl+B" ) << 0 << 1 << 0 << 0 << 0 << 0 << 0 << 0 << 0;
//     QTest::newRow( "C" ) << QString( "Menu &2/Item C Ctrl+C" ) << 0 << 0 << 1 << 0 << 0 << 0 << 0 << 0 << 0;
//     QTest::newRow( "D" ) << QString( "Menu &2/Item D Ctrl+D" ) << 0 << 0 << 0 << 1 << 0 << 0 << 0 << 0 << 0;
//     QTest::newRow( "E" ) << QString( "Menu &2/Item E Ctrl+E" ) << 0 << 0 << 0 << 0 << 1 << 0 << 0 << 0 << 0;
//     QTest::newRow( "F" ) << QString( "Menu &2/Item F Ctrl+F" ) << 0 << 0 << 0 << 0 << 0 << 1 << 0 << 0 << 0;
//     QTest::newRow( "G" ) << QString( "Menu &2/Item G Ctrl+G" ) << 0 << 0 << 0 << 0 << 0 << 0 << 1 << 0 << 0;
//     QTest::newRow( "H" ) << QString( "Menu &2/Item H Ctrl+H" ) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 1 << 0;
//     QTest::newRow( "menu 3" ) << QString( "M&enu 3" )          << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 1;
// }

// /*!
//     Check if the correct signals are emitted if we select a popupmenu.
//     This time, we use a little bit more magic from the testframework.
// */
// void tst_QMenuBar::check_mouse2()
// {
//     if (QSystem::curStyle() == "Motif")
// 	QSKIP("This fails in Motif due to a bug in the testing framework", SkipAll);
//     QFETCH( QString, label );
//     QFETCH( int, itemA_count );
//     QFETCH( int, itemB_count );
//     QFETCH( int, itemC_count );
//     QFETCH( int, itemD_count );
//     QFETCH( int, itemE_count );
//     QFETCH( int, itemF_count );
//     QFETCH( int, itemG_count );
//     QFETCH( int, itemH_count );
//     QFETCH( int, menu3_count );

//     initComplexMenubar();
//     QtTestMouse mouse;
//     mouse.click( QtTestMouse::Menu, label, Qt::LeftButton );

//     // check if the correct signals have fired
//     QCOMPARE(menu3->selCount(), (uint)menu3_count);
//     QCOMPARE(menu4->selCount(), 0u);
//     QCOMPARE(item1_A->selCount(), (uint)itemA_count);
//     QCOMPARE(item1_B->selCount(), (uint)itemB_count);
//     QCOMPARE(item2_C->selCount(), (uint)itemC_count);
//     QCOMPARE(item2_D->selCount(), (uint)itemD_count);
//     QCOMPARE(item2_E->selCount(), (uint)itemE_count);
//     QCOMPARE(item2_F->selCount(), (uint)itemF_count);
//     QCOMPARE(item2_G->selCount(), (uint)itemG_count);
//     QCOMPARE(item2_H->selCount(), (uint)itemH_count);
// }

void
tst_QMenuBar::allowActiveAndDisabled()
{
#if !defined(Q_WS_MAC) && !defined(Q_OS_WINCE_WM)
    mb->hide();
    mb->clear();

     // Task 241043 : check that second menu is activated if only
    // disabled menu items are added

    QMenu fileMenu("&File");
    // Task 241043 : check that second menu is activated
    // if all items are disabled
    QAction *act = fileMenu.addAction("Disabled");
    act->setEnabled(false);

    mb->addMenu(&fileMenu);
    QMenu disabledMenu("Disabled");
    disabledMenu.setEnabled(false);
    QMenu activeMenu("Active");
    mb->addMenu(&disabledMenu);
    mb->addMenu(&activeMenu);
    mb->show();


    // Here we verify that AllowActiveAndDisabled correctly skips
    // the disabled menu entry
    QTest::keyClick(mb, Qt::Key_F, Qt::AltModifier );
    QTest::keyClick(&fileMenu, (Qt::Key_Right));
    if (qApp->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled))
        QCOMPARE(mb->activeAction()->text(), disabledMenu.title());
    else
        QCOMPARE(mb->activeAction()->text(), activeMenu.title());

    QTest::keyClick(mb, (Qt::Key_Left));
    if (qApp->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled))
        QCOMPARE(mb->activeAction()->text(), fileMenu.title());
    else
        QCOMPARE(mb->activeAction()->text(), fileMenu.title());

    mb->hide();
#endif //Q_WS_MAC
}

void tst_QMenuBar::check_altPress()
{
    if ( !qApp->style()->styleHint(QStyle::SH_MenuBar_AltKeyNavigation) ) {
	QSKIP( QString( "this is not supposed to work in the %1 style. Skipping." ).
	      arg( qApp->style()->objectName() ).toAscii(), SkipAll );
    }

#ifdef QT3_SUPPORT
    initSimpleMenubar();
#else
    initSimpleMenubar_noQt3();
#endif

    qApp->setActiveWindow(mw);
    mw->setFocus();

    QTest::keyClick( mw, Qt::Key_Alt );

    QVERIFY( ::qobject_cast<QMenuBar *>(qApp->focusWidget()) );
}

void tst_QMenuBar::check_shortcutPress()
{
#if defined(Q_WS_MAC) || defined(Q_OS_WINCE_WM)
    QSKIP("Qt/Mac,WinCE does not use the native popups/menubar", SkipAll);
#endif

#ifdef QT3_SUPPORT
    initComplexMenubar();
#else
    initComplexMenubar_noQt3();
#endif

    qApp->setActiveWindow(mw);
    QCOMPARE(menu3->selCount(), 0u);
    QTest::keyClick(mw, Qt::Key_E, Qt::AltModifier);
    QTest::qWait(200);
    QCOMPARE(menu3->selCount(), 1u);
    QVERIFY(!mb->activeAction());

    QTest::keyClick(mw, Qt::Key_1, Qt::AltModifier );
#ifdef QT3_SUPPORT
    QVERIFY(pm1->isActiveWindow());
#else
    QVERIFY(pm1_Qt4->isActiveWindow());
#endif
    QTest::keyClick(mb, Qt::Key_2);
#ifdef QT3_SUPPORT
    QVERIFY(pm1->isActiveWindow()); // Should still be the active window
#else
    QVERIFY(pm1_Qt4->isActiveWindow());
#endif
}

void tst_QMenuBar::check_menuPosition()
{
#ifdef Q_WS_MAC
    QSKIP("Qt/Mac does not use the native popups/menubar", SkipAll);
#endif
#ifdef Q_OS_WINCE_WM
    QSKIP("Qt/CE uses native menubar", SkipAll);
#endif
    Menu menu;
#ifdef QT3_SUPPORT
    initComplexMenubar();
#else
    initComplexMenubar_noQt3();
#endif
    menu.setTitle("&menu");
    QRect availRect = QApplication::desktop()->availableGeometry(mw);
    QRect screenRect = QApplication::desktop()->screenGeometry(mw);

    while(menu.sizeHint().height() < (screenRect.height()*2/3)) {
        menu.addAction("item");
    }

    QAction *menu_action = mw->menuBar()->addMenu(&menu);

    qApp->setActiveWindow(mw);
    qApp->processEvents();

    //the menu should be below the menubar item
    {
        mw->move(availRect.topLeft());
        QRect mbItemRect = mw->menuBar()->actionGeometry(menu_action);
        mbItemRect.moveTo(mw->menuBar()->mapToGlobal(mbItemRect.topLeft()));
        QTest::keyClick(mw, Qt::Key_M, Qt::AltModifier );
        QVERIFY(menu.isActiveWindow());
        QCOMPARE(menu.pos(), QPoint(mbItemRect.x(), mbItemRect.bottom() + 1));
        menu.close();
    }

    //the menu should be above the menubar item
    {
        mw->move(0,screenRect.bottom() - screenRect.height()/4); //just leave some place for the menubar
        QRect mbItemRect = mw->menuBar()->actionGeometry(menu_action);
        mbItemRect.moveTo(mw->menuBar()->mapToGlobal(mbItemRect.topLeft()));
        QTest::keyClick(mw, Qt::Key_M, Qt::AltModifier );
        QVERIFY(menu.isActiveWindow());
        QCOMPARE(menu.pos(), QPoint(mbItemRect.x(), mbItemRect.top() - menu.height()));
        menu.close();
    }

    //the menu should be on the side of the menubar item and should be "stuck" to the bottom of the screen
    {
        mw->move(0,screenRect.y() + screenRect.height()/2); //put it in the middle
        QRect mbItemRect = mw->menuBar()->actionGeometry(menu_action);
        mbItemRect.moveTo(mw->menuBar()->mapToGlobal(mbItemRect.topLeft()));
        QTest::keyClick(mw, Qt::Key_M, Qt::AltModifier );
        QVERIFY(menu.isActiveWindow());
        QPoint firstPoint = QPoint(mbItemRect.right()+1, screenRect.bottom() - menu.height() + 1);
        QPoint secondPoint = QPoint(mbItemRect.right()+1, availRect.bottom() - menu.height() + 1);
        QVERIFY(menu.pos() == firstPoint || menu.pos() == secondPoint);
        menu.close();
    }

    //in RTL, the menu should be stuck at the right of the action geometry
    {
        Qt::LayoutDirection dir = qApp->layoutDirection();
        qApp->setLayoutDirection(Qt::RightToLeft);
        menu.clear();
        QObject::connect(&menu, SIGNAL(aboutToShow()), &menu, SLOT(addActions()));
        QRect mbItemRect = mw->menuBar()->actionGeometry(menu_action);
        mbItemRect.moveTo(mw->menuBar()->mapToGlobal(mbItemRect.topLeft()));
        QTest::keyClick(mw, Qt::Key_M, Qt::AltModifier );
        QVERIFY(menu.isActiveWindow());
        QCOMPARE(menu.geometry().right(), mbItemRect.right());
        menu.close();
        qApp->setLayoutDirection(dir);
    }

}

void tst_QMenuBar::task223138_triggered()
{
    qRegisterMetaType<QAction *>("QAction *");
    //we create a window with submenus and we check that both menubar and menus get the triggered signal
    QMainWindow win;
    QMenu *menu = win.menuBar()->addMenu("test");
    QAction *top = menu->addAction("toplevelaction");
    QMenu *submenu = menu->addMenu("nested menu");
    QAction *action = submenu->addAction("nested action");

    QSignalSpy menubarSpy(win.menuBar(), SIGNAL(triggered(QAction*)));
    QSignalSpy menuSpy(menu, SIGNAL(triggered(QAction*)));
    QSignalSpy submenuSpy(submenu, SIGNAL(triggered(QAction*)));

    //let's trigger the first action
    top->trigger();

    QCOMPARE(menubarSpy.count(), 1);
    QCOMPARE(menuSpy.count(), 1);
    QCOMPARE(submenuSpy.count(), 0);

    menubarSpy.clear();
    menuSpy.clear();
    submenuSpy.clear();

    //let's trigger the sub action
    action->trigger();
    QCOMPARE(menubarSpy.count(), 1);
    QCOMPARE(menuSpy.count(), 1);
    QCOMPARE(submenuSpy.count(), 1);
}

void tst_QMenuBar::task256322_highlight()
{
    QMainWindow win;
	win.menuBar()->setNativeMenuBar(false);  //we can't check the geometry of native menubars
    QMenu menu;
    QAction *file = win.menuBar()->addMenu(&menu);
    file->setText("file");
    QMenu menu2;
    QAction *file2 = win.menuBar()->addMenu(&menu2);
    file2->setText("file2");
    QAction *nothing = win.menuBar()->addAction("nothing");

    win.show();
    QTest::qWait(200);

    QTest::mousePress(win.menuBar(), Qt::LeftButton, 0, win.menuBar()->actionGeometry(file).center());
    QTest::mouseMove(win.menuBar(), win.menuBar()->actionGeometry(file).center());
    QTest::mouseRelease(win.menuBar(), Qt::LeftButton, 0, win.menuBar()->actionGeometry(file).center());
    QTRY_VERIFY(menu.isVisible());
    QVERIFY(!menu2.isVisible());
    QCOMPARE(win.menuBar()->activeAction(), file);

    QTest::mousePress(win.menuBar(), Qt::LeftButton, 0, win.menuBar()->actionGeometry(file2).center());
    QTest::mouseMove(win.menuBar(), win.menuBar()->actionGeometry(file2).center());
    QTRY_VERIFY(!menu.isVisible());
    QVERIFY(menu2.isVisible());
    QCOMPARE(win.menuBar()->activeAction(), file2);
    QTest::mouseRelease(win.menuBar(), Qt::LeftButton, 0, win.menuBar()->actionGeometry(file2).center());

    QPoint nothingCenter = win.menuBar()->actionGeometry(nothing).center();
    QTest::mousePress(win.menuBar(), Qt::LeftButton, 0, nothingCenter);
    QTest::mouseMove(win.menuBar(), nothingCenter);
    QTRY_VERIFY(!menu2.isVisible());
    QVERIFY(!menu.isVisible());
    QCOMPARE(win.menuBar()->activeAction(), nothing);
    QTest::mouseRelease(win.menuBar(), Qt::LeftButton, 0, nothingCenter);
}

void tst_QMenuBar::menubarSizeHint()
{
    struct MyStyle : public QWindowsStyle
    {
        virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0 ) const
        {
            // I chose strange values (prime numbers to be more sure that the size of the menubar is correct)
            switch (metric)
            {
            case QStyle::PM_MenuBarItemSpacing:
                return 7;
            case PM_MenuBarHMargin:
                return 13;
            case PM_MenuBarVMargin:
                return 11;
            case PM_MenuBarPanelWidth:
                return 1;
            default:
              return QWindowsStyle::pixelMetric(metric, option, widget);
            }
        }
    } style;

    QMenuBar mb;
    mb.setNativeMenuBar(false); //we can't check the geometry of native menubars

    mb.setStyle(&style);
    //this is a list of arbitrary strings so that we check the geometry
    QStringList list = QStringList() << "trer" << "ezrfgtgvqd" << "sdgzgzerzerzer" << "eerzertz"  << "er";
    foreach(QString str, list)
        mb.addAction(str);

    const int panelWidth = style.pixelMetric(QStyle::PM_MenuBarPanelWidth);
    const int hmargin = style.pixelMetric(QStyle::PM_MenuBarHMargin);
    const int vmargin = style.pixelMetric(QStyle::PM_MenuBarVMargin);
    const int spacing = style.pixelMetric(QStyle::PM_MenuBarItemSpacing);

    mb.show();
    QRect result;
    foreach(QAction *action, mb.actions()) {
        const QRect actionRect = mb.actionGeometry(action);
        if (!result.isNull()) //this is the first item
            QCOMPARE(actionRect.left() - result.right() - 1, spacing);
        result |= actionRect;
        QCOMPARE(result.x(), panelWidth + hmargin + spacing);
        QCOMPARE(result.y(), panelWidth + vmargin);
    }

    //this code is copied from QMenuBar
    //there is no public member that allows to initialize a styleoption instance
    QStyleOptionMenuItem opt;
    opt.rect = mb.rect();
    opt.menuRect = mb.rect();
    opt.state = QStyle::State_None;
    opt.menuItemType = QStyleOptionMenuItem::Normal;
    opt.checkType = QStyleOptionMenuItem::NotCheckable;
    opt.palette = mb.palette();

    QSize resSize = QSize(result.x(), result.y()) + result.size()
        + QSize(panelWidth + hmargin, panelWidth + vmargin);


    resSize = style.sizeFromContents(QStyle::CT_MenuBar, &opt,
                                         resSize.expandedTo(QApplication::globalStrut()),
                                         &mb);

    QCOMPARE(resSize, mb.sizeHint());
}

void tst_QMenuBar::taskQTBUG4965_escapeEaten()
{
#ifdef Q_WS_MAC
    QSKIP("On Mac, do not test the menubar with escape key", SkipAll);
#endif
    QMenuBar menubar;
    QMenu menu("menu1");
    QAction *first = menubar.addMenu(&menu);
    menu.addAction("quit", &menubar, SLOT(close()), QKeySequence("ESC"));
    menubar.show();
    QApplication::setActiveWindow(&menubar);
    QTest::qWaitForWindowShown(&menubar);
    menubar.setActiveAction(first);
    QTRY_VERIFY(menu.isVisible());
    QCOMPARE(menubar.activeAction(), first);
    QTest::keyClick(0, Qt::Key_Escape);
    QVERIFY(!menu.isVisible());
    QTRY_VERIFY(menubar.hasFocus());
    QCOMPARE(menubar.activeAction(), first);
    QTest::keyClick(0, Qt::Key_Escape);
    QVERIFY(!menubar.activeAction());
    QTest::keyClick(0, Qt::Key_Escape); //now the action should be triggered
    QTRY_VERIFY(!menubar.isVisible());
}

#if defined(QT3_SUPPORT)
void tst_QMenuBar::indexBasedInsertion_data()
{
    QTest::addColumn<int>("indexForInsertion");
    QTest::addColumn<int>("expectedIndex");

    QTest::newRow("negative-index-appends") << -1 << 1;
    QTest::newRow("prepend") << 0 << 0;
    QTest::newRow("append") << 1 << 1;
}

void tst_QMenuBar::indexBasedInsertion()
{
    // test the compat'ed index based insertion

    QFETCH(int, indexForInsertion);
    QFETCH(int, expectedIndex);

    {
        QMenuBar menu;
        menu.addAction("Regular Item");

        menu.insertItem("New Item", -1 /*id*/, indexForInsertion);

        QAction *act = menu.actions().value(expectedIndex);
        QVERIFY(act);
        QCOMPARE(act->text(), QString("New Item"));
    }
    {
        QMenuBar menu;
        menu.addAction("Regular Item");

        menu.insertSeparator(indexForInsertion);

        QAction *act = menu.actions().value(expectedIndex);
        QVERIFY(act);
        QVERIFY(act->isSeparator());
    }
}
#endif

QTEST_MAIN(tst_QMenuBar)
#include "tst_qmenubar.moc"