summaryrefslogtreecommitdiffstats
path: root/tests/auto/q3popupmenu/tst_q3popupmenu.cpp
blob: 7d02e0729544ea8b9597a6b7c9721c88438d2181 (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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** 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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company 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.
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <qglobal.h>
#ifdef Q_WS_MAC
#include <private/qt_mac_p.h>
#undef verify
#endif

#include <QtTest/QtTest>
#include <qapplication.h>
#include <qmessagebox.h>
#include <qmenubar.h>

#include <qdebug.h>

#include <q3popupmenu.h>
#include <qmainwindow.h>

//TESTED_CLASS=
//TESTED_FILES=

class tst_Q3PopupMenu : public QObject
{
Q_OBJECT

public:
    tst_Q3PopupMenu();
    virtual ~tst_Q3PopupMenu();

public slots:
    void initTestCase();
    void cleanupTestCase();
    void init();
    void cleanup();

private slots:
    void task177490_highlighted();
    void task177490_activated();

    void testAccels();
    void fontPropagation();
    void accel_data();
    void accel();
    void testItemParameter();

protected slots:
    void onExclItem();
    void onShiftItem();
    void onSpicy();
    void onSubItem();
    // Needed to slience QObject about non existent slot
    void dummySlot() {}
    void itemParameterChanged(int p = 0){itemParameter = p; }

private:
    QMainWindow *testWidget;
    Q3PopupMenu *popup;
    Q3PopupMenu *subPopup;
    bool excl, shft, spicy, subItem;
    int itemParameter;
};

tst_Q3PopupMenu::tst_Q3PopupMenu()
{
}

tst_Q3PopupMenu::~tst_Q3PopupMenu()
{
}

void tst_Q3PopupMenu::task177490_highlighted()
{
#ifdef Q_WS_MAC
    QSKIP("On Mac, native mouse events are needed for this test", SkipAll);
#endif
#ifdef Q_WS_QWS
    QApplication::processEvents();
#endif
    Q3PopupMenu menu1;
    //don't let the window manager move the popup while we are testing
    menu1.setWindowFlags(Qt::X11BypassWindowManagerHint);
    menu1.insertItem("Item 1");

    Q3PopupMenu menu2;
    menu2.insertItem("Item 2");
    menu1.insertItem("Menu 2", &menu2);

    menu1.show();

    const int y1 = menu1.itemGeometry(0).center().y();
    const int y2 = menu1.itemGeometry(1).center().y();
    const int x = menu1.itemGeometry(0).center().x();

    QSignalSpy spy(&menu1, SIGNAL(highlighted(int)));

    QTest::mouseMove(&menu1, QPoint(x, y1));
    QTest::mouseMove(&menu1, QPoint(x, y1 + 1));
    QTest::qWait(1000);

    QTest::mouseMove(&menu1, QPoint(x, y2));
    QTest::mouseMove(&menu1, QPoint(x, y2 + 1));
    QTest::qWait(1000);

    QCOMPARE(spy.count(), 2); // one per menu item
}

void tst_Q3PopupMenu::task177490_activated()
{
#ifdef Q_WS_MAC
    QSKIP("On Mac, native mouse events are needed for this test", SkipAll);
#endif
    Q3PopupMenu menu1;
    menu1.insertItem("Item 1");

    Q3PopupMenu menu2;
    menu2.insertItem("Item 2");
    menu1.insertItem("Menu 2", &menu2);

    Q3PopupMenu menu3;
    menu3.insertItem("Item 3");
    menu2.insertItem("Menu 3", &menu3);

    Q3PopupMenu menu4;
    menu4.insertItem("Item 4");
    menu3.insertItem("Menu 4", &menu4);

    menu1.show();

    QTest::mouseMove(&menu1, QPoint());

    QPoint pos;

    pos = menu1.itemGeometry(1).center();
    QTest::mouseMove(&menu1, pos);
    QTest::mouseMove(&menu1, QPoint(pos.x() + 1, pos.y()));
    QTest::qWait(1000);

    pos = menu2.itemGeometry(1).center();
    QTest::mouseMove(&menu2, pos);
    QTest::mouseMove(&menu2, QPoint(pos.x() + 1, pos.y()));
    QTest::qWait(1000);

    pos = menu3.itemGeometry(1).center();
    QTest::mouseMove(&menu3, pos);
    QTest::mouseMove(&menu3, QPoint(pos.x() + 1, pos.y()));
    QTest::qWait(1000);

    pos = menu4.itemGeometry(0).center();
    QTest::mouseMove(&menu4, pos);
    QTest::mouseMove(&menu4, QPoint(pos.x() + 1, pos.y()));
    QTest::qWait(1000);
    const bool menu4_wasVisible = menu4.isVisible();

    QSignalSpy spy1(&menu1, SIGNAL(activated(int)));
    QSignalSpy spy2(&menu2, SIGNAL(activated(int)));
    QSignalSpy spy3(&menu3, SIGNAL(activated(int)));
    QSignalSpy spy4(&menu4, SIGNAL(activated(int)));

    QTest::mouseClick(&menu4, Qt::LeftButton, 0, menu4.itemGeometry(0).center());

    QCOMPARE(spy1.count(), 0);
    QCOMPARE(spy2.count(), 0);
    QCOMPARE(spy3.count(), 0);
    if (!menu4_wasVisible)
        QEXPECT_FAIL(
            "", "expected failure due to visibilty/focus problem; to be investigated later",
            Abort);
    QCOMPARE(spy4.count(), 1);
}

Q_DECLARE_METATYPE(QKeySequence)

void tst_Q3PopupMenu::initTestCase()
{
    // Create the test class

    testWidget = new QMainWindow(0);
    popup = new Q3PopupMenu( testWidget->menuBar(), "popup" );
    testWidget->menuBar()->insertItem( "menu&bar", popup );
    excl = FALSE;
    shft = FALSE;
    spicy = FALSE;
    subItem = FALSE;
    qApp->setMainWidget(testWidget);
    testWidget->resize( 200, 200 );
    testWidget->show();
    popup->insertItem( tr("Men&u"), this, SLOT(onShiftItem()) );
    popup->insertItem( tr("thing&!"), this, SLOT(onExclItem()) );
    popup->insertItem( tr("Hot && Spic&y" ), this, SLOT(onSpicy()) );

    subPopup = new Q3PopupMenu( popup, "subpopup" );
    subPopup->insertItem( "sub menu &item", this, SLOT(onSubItem()) );

    popup->insertItem( "&sub Popup", subPopup );
}

void tst_Q3PopupMenu::cleanupTestCase()
{
    delete testWidget;
}

void tst_Q3PopupMenu::init()
{
    QApplication::setActiveWindow(testWidget);
    QApplication::processEvents();
}

void tst_Q3PopupMenu::cleanup()
{
    QApplication::processEvents();
}
void tst_Q3PopupMenu::onExclItem()
{
    excl = TRUE;
}

void tst_Q3PopupMenu::onShiftItem()
{
    shft = TRUE;
}

void tst_Q3PopupMenu::onSpicy()
{
    spicy = TRUE;
}

void tst_Q3PopupMenu::onSubItem()
{
    subItem = TRUE;
}

void tst_Q3PopupMenu::testAccels()
{
#if !defined(Q_WS_MAC)
    QTest::keyClick( testWidget, Qt::Key_B, Qt::AltModifier );
    while (!popup->isVisible())
        QApplication::processEvents();
    QTest::keyClick( popup, Qt::Key_S );
    while (!subPopup->isVisible())
        QApplication::processEvents();
    QTest::keyClick( subPopup, Qt::Key_I );
    QVERIFY( subItem );
    QTest::keyClick( testWidget, Qt::Key_B, Qt::AltModifier );
    while (!popup->isVisible())
        QApplication::processEvents();
    QTest::keyClick( popup, 'U' );
    QVERIFY( shft );

    QTest::keyClick( testWidget, Qt::Key_B, Qt::AltModifier );
    QTest::keyClick( popup, '!' );
    QVERIFY( excl );

    QTest::keyClick( testWidget, Qt::Key_B, Qt::AltModifier );
    QTest::keyClick( popup, 'Y' );
    QVERIFY( spicy );
#else
    QSKIP("Mac OS X doesn't use mnemonics", SkipAll);
#endif

}

void tst_Q3PopupMenu::fontPropagation()
{
    QFont newfont = QFont( "times", 24 );
    QFont originalFont = popup->font();
    testWidget->setFont( QFont( "times", 24 ) );
    QVERIFY( !popup->ownFont() );
    QVERIFY( !(popup->font() == newfont) );
    QApplication::setFont( newfont, TRUE );
    QVERIFY( !popup->ownFont() );
    QVERIFY( popup->font() == newfont );
}

void tst_Q3PopupMenu::accel_data()
{
    QTest::addColumn<QKeySequence>("accelerator");
    QTest::addColumn<int>("id");
    QTest::addColumn<QString>("accelString");

#ifndef Q_WS_MAC
    QTest::newRow("simple_accel") << QKeySequence("CTRL+C") << 1 << QString("Ctrl+C");
    QTest::newRow("complex_accel") << QKeySequence("CTRL+ALT+SHIFT+T") << 2 << QString("Ctrl+Alt+Shift+T");
#else
    QTest::newRow("simple_accel") << QKeySequence("CTRL+C") << 1
                               << QString(QChar(kCommandUnicode) + 'C');
    QTest::newRow("complex_accel") << QKeySequence("CTRL+ALT+SHIFT+T") << 2
                                << QString(QChar(kOptionUnicode) + QString(QChar(kShiftUnicode))
                                   + QString(QChar(kCommandUnicode)) + 'T');
#endif
}

void tst_Q3PopupMenu::accel()
{
    QFETCH(QKeySequence, accelerator);
    QFETCH(int, id);
    QFETCH(QString, accelString);

    popup->insertItem("Dummy item", this, SLOT(dummySlot()), accelerator, id);
    QCOMPARE(accelString, (QString)popup->accel(id));
}

void tst_Q3PopupMenu::testItemParameter()
{
#if !defined(Q_WS_MAC)
    itemParameter = 0;
    int id = popup->insertItem( tr("&ItemParameter"), this, SLOT(itemParameterChanged(int)));
    popup->setItemParameter(id, 17);
    QTest::keyClick( testWidget, Qt::Key_B, Qt::AltModifier );
    while (!popup->isVisible())
        QApplication::processEvents();
    QTest::keyClick( popup, 'I' );
    QCOMPARE(itemParameter, 17);
#else
    QSKIP("Mac OS X doesn't use mnemonics", SkipAll);
#endif
}


QTEST_MAIN(tst_Q3PopupMenu)
#include "tst_q3popupmenu.moc"