2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2012-09-19 12:28:29 +00:00
|
|
|
** 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
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 12:36:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
#include <qapplication.h>
|
|
|
|
#include <qlineedit.h>
|
|
|
|
#include <qmenu.h>
|
|
|
|
#include <qlabel.h>
|
|
|
|
#include <qdialog.h>
|
|
|
|
#include <qevent.h>
|
|
|
|
#include <qlineedit.h>
|
|
|
|
#include <QBoxLayout>
|
2017-07-11 06:19:36 +00:00
|
|
|
#include <QSysInfo>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QT_FORWARD_DECLARE_CLASS(QWidget)
|
|
|
|
|
|
|
|
class FocusLineEdit : public QLineEdit
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FocusLineEdit( QWidget* parent = 0, const char* name = 0 ) : QLineEdit(name, parent) {}
|
|
|
|
int focusInEventReason;
|
|
|
|
int focusOutEventReason;
|
|
|
|
bool focusInEventRecieved;
|
|
|
|
bool focusInEventGotFocus;
|
|
|
|
bool focusOutEventRecieved;
|
|
|
|
bool focusOutEventLostFocus;
|
|
|
|
protected:
|
|
|
|
virtual void keyPressEvent( QKeyEvent *e )
|
|
|
|
{
|
|
|
|
// qDebug( QString("keyPressEvent: %1").arg(e->key()) );
|
|
|
|
QLineEdit::keyPressEvent( e );
|
|
|
|
}
|
|
|
|
void focusInEvent( QFocusEvent* e )
|
|
|
|
{
|
|
|
|
QLineEdit::focusInEvent( e );
|
|
|
|
focusInEventReason = e->reason();
|
2014-01-13 14:48:44 +00:00
|
|
|
focusInEventGotFocus = e->gotFocus();
|
2011-12-30 05:51:05 +00:00
|
|
|
focusInEventRecieved = true;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
void focusOutEvent( QFocusEvent* e )
|
|
|
|
{
|
|
|
|
QLineEdit::focusOutEvent( e );
|
|
|
|
focusOutEventReason = e->reason();
|
2014-01-13 14:48:44 +00:00
|
|
|
focusOutEventLostFocus = !e->gotFocus();
|
2011-12-30 05:51:05 +00:00
|
|
|
focusOutEventRecieved = true;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class tst_QFocusEvent : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
void initWidget();
|
|
|
|
|
2015-12-03 15:59:30 +00:00
|
|
|
private slots:
|
2011-04-27 10:05:43 +00:00
|
|
|
void initTestCase();
|
|
|
|
void cleanupTestCase();
|
|
|
|
void cleanup();
|
|
|
|
void checkReason_Tab();
|
|
|
|
void checkReason_ShiftTab();
|
|
|
|
void checkReason_BackTab();
|
|
|
|
void checkReason_Popup();
|
|
|
|
void checkReason_focusWidget();
|
|
|
|
void checkReason_Shortcut();
|
|
|
|
void checkReason_ActiveWindow();
|
|
|
|
|
|
|
|
private:
|
|
|
|
QWidget* testFocusWidget;
|
|
|
|
FocusLineEdit* childFocusWidgetOne;
|
|
|
|
FocusLineEdit* childFocusWidgetTwo;
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QFocusEvent::initTestCase()
|
|
|
|
{
|
|
|
|
testFocusWidget = new QWidget( 0 );
|
|
|
|
childFocusWidgetOne = new FocusLineEdit( testFocusWidget );
|
|
|
|
childFocusWidgetOne->setGeometry( 10, 10, 180, 20 );
|
|
|
|
childFocusWidgetTwo = new FocusLineEdit( testFocusWidget );
|
|
|
|
childFocusWidgetTwo->setGeometry( 10, 50, 180, 20 );
|
|
|
|
|
|
|
|
//qApp->setMainWidget( testFocusWidget ); Qt4
|
|
|
|
testFocusWidget->resize( 200,100 );
|
|
|
|
testFocusWidget->show();
|
2012-07-24 12:29:01 +00:00
|
|
|
QVERIFY(QTest::qWaitForWindowExposed(testFocusWidget));
|
2011-04-27 10:05:43 +00:00
|
|
|
// Applications don't get focus when launched from the command line on Mac.
|
2012-01-16 11:02:48 +00:00
|
|
|
#ifdef Q_OS_MAC
|
2011-04-27 10:05:43 +00:00
|
|
|
testFocusWidget->raise();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QFocusEvent::cleanupTestCase()
|
|
|
|
{
|
|
|
|
delete testFocusWidget;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QFocusEvent::cleanup()
|
|
|
|
{
|
|
|
|
childFocusWidgetTwo->setGeometry( 10, 50, 180, 20 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QFocusEvent::initWidget()
|
|
|
|
{
|
|
|
|
// On X11 we have to ensure the event was processed before doing any checking, on Windows
|
|
|
|
// this is processed straight away.
|
2012-07-24 12:29:01 +00:00
|
|
|
QApplication::setActiveWindow(testFocusWidget);
|
|
|
|
childFocusWidgetOne->setFocus(); // The first lineedit should have focus
|
|
|
|
QVERIFY(QTest::qWaitForWindowActive(testFocusWidget));
|
|
|
|
QTRY_VERIFY(childFocusWidgetOne->hasFocus());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-12-30 05:51:05 +00:00
|
|
|
childFocusWidgetOne->focusInEventRecieved = false;
|
|
|
|
childFocusWidgetOne->focusInEventGotFocus = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
childFocusWidgetOne->focusInEventReason = -1;
|
2011-12-30 05:51:05 +00:00
|
|
|
childFocusWidgetOne->focusOutEventRecieved = false;
|
|
|
|
childFocusWidgetOne->focusOutEventLostFocus = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
childFocusWidgetOne->focusOutEventReason = -1;
|
2011-12-30 05:51:05 +00:00
|
|
|
childFocusWidgetTwo->focusInEventRecieved = false;
|
|
|
|
childFocusWidgetTwo->focusInEventGotFocus = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
childFocusWidgetTwo->focusInEventReason = -1;
|
2011-12-30 05:51:05 +00:00
|
|
|
childFocusWidgetTwo->focusOutEventRecieved = false;
|
|
|
|
childFocusWidgetTwo->focusOutEventLostFocus = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
childFocusWidgetTwo->focusOutEventReason = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QFocusEvent::checkReason_Tab()
|
|
|
|
{
|
|
|
|
initWidget();
|
|
|
|
|
|
|
|
// Now test the tab key
|
|
|
|
QTest::keyClick( childFocusWidgetOne, Qt::Key_Tab );
|
|
|
|
|
|
|
|
QVERIFY(childFocusWidgetOne->focusOutEventRecieved);
|
|
|
|
QVERIFY(childFocusWidgetTwo->focusInEventRecieved);
|
|
|
|
QVERIFY(childFocusWidgetOne->focusOutEventLostFocus);
|
|
|
|
QVERIFY(childFocusWidgetTwo->focusInEventGotFocus);
|
|
|
|
|
|
|
|
QVERIFY( childFocusWidgetTwo->hasFocus() );
|
|
|
|
QCOMPARE( childFocusWidgetOne->focusOutEventReason, (int) Qt::TabFocusReason );
|
|
|
|
QCOMPARE( childFocusWidgetTwo->focusInEventReason, (int) Qt::TabFocusReason );
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QFocusEvent::checkReason_ShiftTab()
|
|
|
|
{
|
|
|
|
initWidget();
|
|
|
|
|
|
|
|
// Now test the shift + tab key
|
|
|
|
QTest::keyClick( childFocusWidgetOne, Qt::Key_Tab, Qt::ShiftModifier );
|
|
|
|
|
|
|
|
QVERIFY(childFocusWidgetOne->focusOutEventRecieved);
|
|
|
|
QVERIFY(childFocusWidgetTwo->focusInEventRecieved);
|
|
|
|
QVERIFY(childFocusWidgetOne->focusOutEventLostFocus);
|
|
|
|
QVERIFY(childFocusWidgetTwo->focusInEventGotFocus);
|
|
|
|
|
|
|
|
QVERIFY( childFocusWidgetTwo->hasFocus() );
|
|
|
|
QCOMPARE( childFocusWidgetOne->focusOutEventReason, (int)Qt::BacktabFocusReason );
|
|
|
|
QCOMPARE( childFocusWidgetTwo->focusInEventReason, (int)Qt::BacktabFocusReason );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
In this test we verify that the Qt::KeyBacktab key is handled in a qfocusevent
|
|
|
|
*/
|
|
|
|
void tst_QFocusEvent::checkReason_BackTab()
|
|
|
|
{
|
2011-11-15 01:36:18 +00:00
|
|
|
#ifdef Q_OS_WIN32 // key is not supported on Windows
|
|
|
|
QSKIP( "Backtab is not supported on Windows");
|
|
|
|
#else
|
2011-04-27 10:05:43 +00:00
|
|
|
initWidget();
|
|
|
|
QVERIFY( childFocusWidgetOne->hasFocus() );
|
|
|
|
|
|
|
|
// Now test the backtab key
|
|
|
|
QTest::keyClick( childFocusWidgetOne, Qt::Key_Backtab );
|
|
|
|
QTest::qWait(200);
|
|
|
|
|
|
|
|
QTRY_VERIFY(childFocusWidgetOne->focusOutEventRecieved);
|
|
|
|
QVERIFY(childFocusWidgetTwo->focusInEventRecieved);
|
|
|
|
QVERIFY(childFocusWidgetOne->focusOutEventLostFocus);
|
|
|
|
QVERIFY(childFocusWidgetTwo->focusInEventGotFocus);
|
|
|
|
|
|
|
|
QVERIFY( childFocusWidgetTwo->hasFocus() );
|
|
|
|
QCOMPARE( childFocusWidgetOne->focusOutEventReason, int(Qt::BacktabFocusReason) );
|
|
|
|
QCOMPARE( childFocusWidgetTwo->focusInEventReason, int(Qt::BacktabFocusReason) );
|
2011-10-04 03:23:23 +00:00
|
|
|
#endif
|
2011-11-15 01:36:18 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
void tst_QFocusEvent::checkReason_Popup()
|
|
|
|
{
|
|
|
|
initWidget();
|
|
|
|
|
|
|
|
// Now test the popup reason
|
|
|
|
QMenu* popupMenu = new QMenu( testFocusWidget );
|
|
|
|
popupMenu->addMenu( "Test" );
|
|
|
|
popupMenu->popup( QPoint(0,0) );
|
|
|
|
QTest::qWait(50);
|
|
|
|
|
|
|
|
QTRY_VERIFY(childFocusWidgetOne->focusOutEventLostFocus);
|
|
|
|
|
|
|
|
QTRY_VERIFY( childFocusWidgetOne->hasFocus() );
|
|
|
|
QVERIFY( !childFocusWidgetOne->focusInEventRecieved );
|
|
|
|
QVERIFY( childFocusWidgetOne->focusOutEventRecieved );
|
|
|
|
QVERIFY( !childFocusWidgetTwo->focusInEventRecieved );
|
|
|
|
QVERIFY( !childFocusWidgetTwo->focusOutEventRecieved );
|
|
|
|
QCOMPARE( childFocusWidgetOne->focusOutEventReason, int(Qt::PopupFocusReason));
|
|
|
|
|
|
|
|
popupMenu->hide();
|
|
|
|
|
|
|
|
QVERIFY(childFocusWidgetOne->focusInEventRecieved);
|
|
|
|
QVERIFY(childFocusWidgetOne->focusInEventGotFocus);
|
|
|
|
|
|
|
|
QVERIFY( childFocusWidgetOne->hasFocus() );
|
|
|
|
QVERIFY( childFocusWidgetOne->focusInEventRecieved );
|
|
|
|
QVERIFY( childFocusWidgetOne->focusOutEventRecieved );
|
|
|
|
QVERIFY( !childFocusWidgetTwo->focusInEventRecieved );
|
|
|
|
QVERIFY( !childFocusWidgetTwo->focusOutEventRecieved );
|
|
|
|
}
|
|
|
|
|
2012-01-16 11:02:48 +00:00
|
|
|
#ifdef Q_OS_MAC
|
2011-04-27 10:05:43 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
extern void qt_set_sequence_auto_mnemonic(bool);
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void tst_QFocusEvent::checkReason_Shortcut()
|
|
|
|
{
|
|
|
|
initWidget();
|
2012-01-16 11:02:48 +00:00
|
|
|
#ifdef Q_OS_MAC
|
2011-04-27 10:05:43 +00:00
|
|
|
qt_set_sequence_auto_mnemonic(true);
|
|
|
|
#endif
|
|
|
|
QLabel* label = new QLabel( "&Test", testFocusWidget );
|
|
|
|
label->setBuddy( childFocusWidgetTwo );
|
|
|
|
label->setGeometry( 10, 50, 90, 20 );
|
|
|
|
childFocusWidgetTwo->setGeometry( 105, 50, 95, 20 );
|
|
|
|
label->show();
|
|
|
|
|
|
|
|
QVERIFY( childFocusWidgetOne->hasFocus() );
|
|
|
|
QVERIFY( !childFocusWidgetTwo->hasFocus() );
|
|
|
|
|
|
|
|
QTest::keyClick( label, Qt::Key_T, Qt::AltModifier );
|
|
|
|
|
|
|
|
QVERIFY(childFocusWidgetOne->focusOutEventRecieved);
|
|
|
|
QVERIFY(childFocusWidgetTwo->focusInEventRecieved);
|
|
|
|
QVERIFY(childFocusWidgetOne->focusOutEventLostFocus);
|
|
|
|
QVERIFY(childFocusWidgetTwo->focusInEventGotFocus);
|
|
|
|
|
|
|
|
QVERIFY( childFocusWidgetTwo->hasFocus() );
|
|
|
|
QVERIFY( !childFocusWidgetOne->focusInEventRecieved );
|
|
|
|
QVERIFY( childFocusWidgetOne->focusOutEventRecieved );
|
|
|
|
QCOMPARE( childFocusWidgetOne->focusOutEventReason, (int)Qt::ShortcutFocusReason );
|
|
|
|
QVERIFY( childFocusWidgetTwo->focusInEventRecieved );
|
|
|
|
QCOMPARE( childFocusWidgetTwo->focusInEventReason, (int)Qt::ShortcutFocusReason );
|
|
|
|
QVERIFY( !childFocusWidgetTwo->focusOutEventRecieved );
|
|
|
|
|
|
|
|
label->hide();
|
|
|
|
QVERIFY( childFocusWidgetTwo->hasFocus() );
|
|
|
|
QVERIFY( !childFocusWidgetOne->hasFocus() );
|
2012-01-16 11:02:48 +00:00
|
|
|
#ifdef Q_OS_MAC
|
2011-04-27 10:05:43 +00:00
|
|
|
qt_set_sequence_auto_mnemonic(false);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QFocusEvent::checkReason_focusWidget()
|
|
|
|
{
|
|
|
|
// This test checks that a widget doesn't loose
|
2017-05-29 16:14:49 +00:00
|
|
|
// its focuswidget just because the focuswidget loses focus.
|
2011-04-27 10:05:43 +00:00
|
|
|
QWidget window1;
|
|
|
|
QWidget frame1;
|
|
|
|
QWidget frame2;
|
|
|
|
QLineEdit edit1;
|
|
|
|
QLineEdit edit2;
|
|
|
|
|
|
|
|
QVBoxLayout outerLayout;
|
|
|
|
outerLayout.addWidget(&frame1);
|
|
|
|
outerLayout.addWidget(&frame2);
|
|
|
|
window1.setLayout(&outerLayout);
|
|
|
|
|
|
|
|
QVBoxLayout leftLayout;
|
|
|
|
QVBoxLayout rightLayout;
|
|
|
|
leftLayout.addWidget(&edit1);
|
|
|
|
rightLayout.addWidget(&edit2);
|
|
|
|
frame1.setLayout(&leftLayout);
|
|
|
|
frame2.setLayout(&rightLayout);
|
|
|
|
window1.show();
|
|
|
|
|
|
|
|
edit1.setFocus();
|
|
|
|
QTest::qWait(100);
|
|
|
|
edit2.setFocus();
|
|
|
|
|
|
|
|
QVERIFY(frame1.focusWidget() != 0);
|
|
|
|
QVERIFY(frame2.focusWidget() != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QFocusEvent::checkReason_ActiveWindow()
|
|
|
|
{
|
|
|
|
initWidget();
|
|
|
|
|
|
|
|
QDialog* d = new QDialog( testFocusWidget );
|
|
|
|
d->show();
|
2012-09-10 10:12:36 +00:00
|
|
|
QVERIFY(QTest::qWaitForWindowExposed(d));
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
d->activateWindow(); // ### CDE
|
|
|
|
QApplication::setActiveWindow(d);
|
2012-07-24 12:29:01 +00:00
|
|
|
QVERIFY(QTest::qWaitForWindowActive(d));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QTRY_VERIFY(childFocusWidgetOne->focusOutEventRecieved);
|
|
|
|
QVERIFY(childFocusWidgetOne->focusOutEventLostFocus);
|
|
|
|
|
2017-07-11 06:19:36 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
if (QSysInfo::kernelVersion() == "10.0.15063") {
|
|
|
|
// Activate window of testFocusWidget, focus in that window goes to childFocusWidgetOne
|
|
|
|
QWARN("Windows 10 Creators Update (10.0.15063) requires explicit activateWindow()");
|
|
|
|
testFocusWidget->activateWindow();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QVERIFY( !childFocusWidgetOne->focusInEventRecieved );
|
|
|
|
QVERIFY( childFocusWidgetOne->focusOutEventRecieved );
|
|
|
|
QCOMPARE( childFocusWidgetOne->focusOutEventReason, (int)Qt::ActiveWindowFocusReason);
|
|
|
|
QVERIFY( !childFocusWidgetOne->hasFocus() );
|
|
|
|
|
|
|
|
d->hide();
|
|
|
|
QTest::qWait(100);
|
|
|
|
|
|
|
|
#if defined(Q_OS_IRIX)
|
|
|
|
QEXPECT_FAIL("", "IRIX requires explicit activateWindow(), so this test does not make any sense.", Abort);
|
|
|
|
#endif
|
2017-04-06 07:09:11 +00:00
|
|
|
|
|
|
|
if (!QGuiApplication::platformName().compare(QLatin1String("offscreen"), Qt::CaseInsensitive)
|
|
|
|
|| !QGuiApplication::platformName().compare(QLatin1String("minimal"), Qt::CaseInsensitive)) {
|
|
|
|
// Activate window of testFocusWidget, focus in that window goes to childFocusWidgetOne
|
|
|
|
QWARN("Platforms offscreen and minimal require explicit activateWindow()");
|
|
|
|
testFocusWidget->activateWindow();
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QTRY_VERIFY(childFocusWidgetOne->focusInEventRecieved);
|
|
|
|
QVERIFY(childFocusWidgetOne->focusInEventGotFocus);
|
|
|
|
|
|
|
|
QVERIFY( childFocusWidgetOne->hasFocus() );
|
|
|
|
QVERIFY( childFocusWidgetOne->focusInEventRecieved );
|
|
|
|
QCOMPARE( childFocusWidgetOne->focusInEventReason, (int)Qt::ActiveWindowFocusReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QTEST_MAIN(tst_QFocusEvent)
|
|
|
|
#include "tst_qfocusevent.moc"
|