1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <qglobal.h>
44 #ifdef Q_WS_MAC
45 #include <private/qt_mac_p.h>
46 #undef verify
47 #endif
48
49 #include <QtTest/QtTest>
50 #include <qapplication.h>
51 #include <qmessagebox.h>
52 #include <qmenubar.h>
53
54 #include <qdebug.h>
55
56 #include <q3popupmenu.h>
57 #include <qmainwindow.h>
58
59 //TESTED_CLASS=
60 //TESTED_FILES=
61
62 class tst_Q3PopupMenu : public QObject
63 {
64 Q_OBJECT
65
66 public:
67 tst_Q3PopupMenu();
68 virtual ~tst_Q3PopupMenu();
69
70 public slots:
71 void initTestCase();
72 void cleanupTestCase();
73 void init();
74 void cleanup();
75
76 private slots:
77 void task177490_highlighted();
78 void task177490_activated();
79
80 void testAccels();
81 void fontPropagation();
82 void accel_data();
83 void accel();
84 void testItemParameter();
85
86 protected slots:
87 void onExclItem();
88 void onShiftItem();
89 void onSpicy();
90 void onSubItem();
91 // Needed to slience QObject about non existent slot
dummySlot()92 void dummySlot() {}
itemParameterChanged(int p=0)93 void itemParameterChanged(int p = 0){itemParameter = p; }
94
95 private:
96 QMainWindow *testWidget;
97 Q3PopupMenu *popup;
98 Q3PopupMenu *subPopup;
99 bool excl, shft, spicy, subItem;
100 int itemParameter;
101 };
102
tst_Q3PopupMenu()103 tst_Q3PopupMenu::tst_Q3PopupMenu()
104 {
105 }
106
~tst_Q3PopupMenu()107 tst_Q3PopupMenu::~tst_Q3PopupMenu()
108 {
109 }
110
task177490_highlighted()111 void tst_Q3PopupMenu::task177490_highlighted()
112 {
113 #ifdef Q_WS_MAC
114 QSKIP("On Mac, native mouse events are needed for this test", SkipAll);
115 #endif
116 #ifdef Q_WS_QWS
117 QApplication::processEvents();
118 #endif
119 Q3PopupMenu menu1;
120 //don't let the window manager move the popup while we are testing
121 menu1.setWindowFlags(Qt::X11BypassWindowManagerHint);
122 menu1.insertItem("Item 1");
123
124 Q3PopupMenu menu2;
125 menu2.insertItem("Item 2");
126 menu1.insertItem("Menu 2", &menu2);
127
128 menu1.show();
129
130 const int y1 = menu1.itemGeometry(0).center().y();
131 const int y2 = menu1.itemGeometry(1).center().y();
132 const int x = menu1.itemGeometry(0).center().x();
133
134 QSignalSpy spy(&menu1, SIGNAL(highlighted(int)));
135
136 QTest::mouseMove(&menu1, QPoint(x, y1));
137 QTest::mouseMove(&menu1, QPoint(x, y1 + 1));
138 QTest::qWait(1000);
139
140 QTest::mouseMove(&menu1, QPoint(x, y2));
141 QTest::mouseMove(&menu1, QPoint(x, y2 + 1));
142 QTest::qWait(1000);
143
144 QCOMPARE(spy.count(), 2); // one per menu item
145 }
146
task177490_activated()147 void tst_Q3PopupMenu::task177490_activated()
148 {
149 #ifdef Q_WS_MAC
150 QSKIP("On Mac, native mouse events are needed for this test", SkipAll);
151 #endif
152 Q3PopupMenu menu1;
153 menu1.insertItem("Item 1");
154
155 Q3PopupMenu menu2;
156 menu2.insertItem("Item 2");
157 menu1.insertItem("Menu 2", &menu2);
158
159 Q3PopupMenu menu3;
160 menu3.insertItem("Item 3");
161 menu2.insertItem("Menu 3", &menu3);
162
163 Q3PopupMenu menu4;
164 menu4.insertItem("Item 4");
165 menu3.insertItem("Menu 4", &menu4);
166
167 menu1.show();
168
169 QTest::mouseMove(&menu1, QPoint());
170
171 QPoint pos;
172
173 pos = menu1.itemGeometry(1).center();
174 QTest::mouseMove(&menu1, pos);
175 QTest::mouseMove(&menu1, QPoint(pos.x() + 1, pos.y()));
176 QTest::qWait(1000);
177
178 pos = menu2.itemGeometry(1).center();
179 QTest::mouseMove(&menu2, pos);
180 QTest::mouseMove(&menu2, QPoint(pos.x() + 1, pos.y()));
181 QTest::qWait(1000);
182
183 pos = menu3.itemGeometry(1).center();
184 QTest::mouseMove(&menu3, pos);
185 QTest::mouseMove(&menu3, QPoint(pos.x() + 1, pos.y()));
186 QTest::qWait(1000);
187
188 pos = menu4.itemGeometry(0).center();
189 QTest::mouseMove(&menu4, pos);
190 QTest::mouseMove(&menu4, QPoint(pos.x() + 1, pos.y()));
191 QTest::qWait(1000);
192 const bool menu4_wasVisible = menu4.isVisible();
193
194 QSignalSpy spy1(&menu1, SIGNAL(activated(int)));
195 QSignalSpy spy2(&menu2, SIGNAL(activated(int)));
196 QSignalSpy spy3(&menu3, SIGNAL(activated(int)));
197 QSignalSpy spy4(&menu4, SIGNAL(activated(int)));
198
199 QTest::mouseClick(&menu4, Qt::LeftButton, 0, menu4.itemGeometry(0).center());
200
201 QCOMPARE(spy1.count(), 0);
202 QCOMPARE(spy2.count(), 0);
203 QCOMPARE(spy3.count(), 0);
204 if (!menu4_wasVisible)
205 QEXPECT_FAIL(
206 "", "expected failure due to visibilty/focus problem; to be investigated later",
207 Abort);
208 QCOMPARE(spy4.count(), 1);
209 }
210
Q_DECLARE_METATYPE(QKeySequence)211 Q_DECLARE_METATYPE(QKeySequence)
212
213 void tst_Q3PopupMenu::initTestCase()
214 {
215 // Create the test class
216
217 testWidget = new QMainWindow(0);
218 popup = new Q3PopupMenu( testWidget->menuBar(), "popup" );
219 testWidget->menuBar()->insertItem( "menu&bar", popup );
220 excl = FALSE;
221 shft = FALSE;
222 spicy = FALSE;
223 subItem = FALSE;
224 qApp->setMainWidget(testWidget);
225 testWidget->resize( 200, 200 );
226 testWidget->show();
227 popup->insertItem( tr("Men&u"), this, SLOT(onShiftItem()) );
228 popup->insertItem( tr("thing&!"), this, SLOT(onExclItem()) );
229 popup->insertItem( tr("Hot && Spic&y" ), this, SLOT(onSpicy()) );
230
231 subPopup = new Q3PopupMenu( popup, "subpopup" );
232 subPopup->insertItem( "sub menu &item", this, SLOT(onSubItem()) );
233
234 popup->insertItem( "&sub Popup", subPopup );
235 }
236
cleanupTestCase()237 void tst_Q3PopupMenu::cleanupTestCase()
238 {
239 delete testWidget;
240 }
241
init()242 void tst_Q3PopupMenu::init()
243 {
244 QApplication::setActiveWindow(testWidget);
245 QApplication::processEvents();
246 }
247
cleanup()248 void tst_Q3PopupMenu::cleanup()
249 {
250 QApplication::processEvents();
251 }
onExclItem()252 void tst_Q3PopupMenu::onExclItem()
253 {
254 excl = TRUE;
255 }
256
onShiftItem()257 void tst_Q3PopupMenu::onShiftItem()
258 {
259 shft = TRUE;
260 }
261
onSpicy()262 void tst_Q3PopupMenu::onSpicy()
263 {
264 spicy = TRUE;
265 }
266
onSubItem()267 void tst_Q3PopupMenu::onSubItem()
268 {
269 subItem = TRUE;
270 }
271
testAccels()272 void tst_Q3PopupMenu::testAccels()
273 {
274 #if !defined(Q_WS_MAC)
275 QTest::keyClick( testWidget, Qt::Key_B, Qt::AltModifier );
276 while (!popup->isVisible())
277 QApplication::processEvents();
278 QTest::keyClick( popup, Qt::Key_S );
279 while (!subPopup->isVisible())
280 QApplication::processEvents();
281 QTest::keyClick( subPopup, Qt::Key_I );
282 QVERIFY( subItem );
283 QTest::keyClick( testWidget, Qt::Key_B, Qt::AltModifier );
284 while (!popup->isVisible())
285 QApplication::processEvents();
286 QTest::keyClick( popup, 'U' );
287 QVERIFY( shft );
288
289 QTest::keyClick( testWidget, Qt::Key_B, Qt::AltModifier );
290 QTest::keyClick( popup, '!' );
291 QVERIFY( excl );
292
293 QTest::keyClick( testWidget, Qt::Key_B, Qt::AltModifier );
294 QTest::keyClick( popup, 'Y' );
295 QVERIFY( spicy );
296 #else
297 QSKIP("Mac OS X doesn't use mnemonics", SkipAll);
298 #endif
299
300 }
301
fontPropagation()302 void tst_Q3PopupMenu::fontPropagation()
303 {
304 QFont newfont = QFont( "times", 24 );
305 QFont originalFont = popup->font();
306 testWidget->setFont( QFont( "times", 24 ) );
307 QVERIFY( !popup->ownFont() );
308 QVERIFY( !(popup->font() == newfont) );
309 QApplication::setFont( newfont, TRUE );
310 QVERIFY( !popup->ownFont() );
311 QVERIFY( popup->font() == newfont );
312 }
313
accel_data()314 void tst_Q3PopupMenu::accel_data()
315 {
316 QTest::addColumn<QKeySequence>("accelerator");
317 QTest::addColumn<int>("id");
318 QTest::addColumn<QString>("accelString");
319
320 #ifndef Q_WS_MAC
321 QTest::newRow("simple_accel") << QKeySequence("CTRL+C") << 1 << QString("Ctrl+C");
322 QTest::newRow("complex_accel") << QKeySequence("CTRL+ALT+SHIFT+T") << 2 << QString("Ctrl+Alt+Shift+T");
323 #else
324 QTest::newRow("simple_accel") << QKeySequence("CTRL+C") << 1
325 << QString(QChar(kCommandUnicode) + 'C');
326 QTest::newRow("complex_accel") << QKeySequence("CTRL+ALT+SHIFT+T") << 2
327 << QString(QChar(kOptionUnicode) + QString(QChar(kShiftUnicode))
328 + QString(QChar(kCommandUnicode)) + 'T');
329 #endif
330 }
331
accel()332 void tst_Q3PopupMenu::accel()
333 {
334 QFETCH(QKeySequence, accelerator);
335 QFETCH(int, id);
336 QFETCH(QString, accelString);
337
338 popup->insertItem("Dummy item", this, SLOT(dummySlot()), accelerator, id);
339 QCOMPARE(accelString, (QString)popup->accel(id));
340 }
341
testItemParameter()342 void tst_Q3PopupMenu::testItemParameter()
343 {
344 #if !defined(Q_WS_MAC)
345 itemParameter = 0;
346 int id = popup->insertItem( tr("&ItemParameter"), this, SLOT(itemParameterChanged(int)));
347 popup->setItemParameter(id, 17);
348 QTest::keyClick( testWidget, Qt::Key_B, Qt::AltModifier );
349 while (!popup->isVisible())
350 QApplication::processEvents();
351 QTest::keyClick( popup, 'I' );
352 QCOMPARE(itemParameter, 17);
353 #else
354 QSKIP("Mac OS X doesn't use mnemonics", SkipAll);
355 #endif
356 }
357
358
359 QTEST_MAIN(tst_Q3PopupMenu)
360 #include "tst_q3popupmenu.moc"
361