qt 6.5.1 original

This commit is contained in:
kleuter
2023-10-29 23:33:08 +01:00
parent 71d22ab6b0
commit 85d238dfda
21202 changed files with 5499099 additions and 0 deletions

View File

@ -0,0 +1,48 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
if(NOT CMAKE_CROSSCOMPILING)
# add_subdirectory(atwrapper) <- does not exist
endif()
if(TARGET Qt::Widgets)
add_subdirectory(gestures)
add_subdirectory(languagechange)
add_subdirectory(qfocusevent)
add_subdirectory(qsharedpointer_and_qwidget)
# add_subdirectory(windowsmobile) <- does not exist
endif()
if(TARGET Qt::Network AND TARGET Qt::Widgets)
add_subdirectory(qnetworkaccessmanager_and_qprogressdialog)
endif()
if(MACOS AND TARGET Qt::Gui AND TARGET Qt::Widgets)
add_subdirectory(macgui)
add_subdirectory(macplist)
add_subdirectory(qaccessibilitymac)
endif()
if(TARGET Qt::Network)
add_subdirectory(networkselftest)
endif()
if(QT_FEATURE_accessibility AND TARGET Qt::Gui AND TARGET Qt::Widgets)
add_subdirectory(qaccessibility)
endif()
if(TARGET Qt::Gui)
add_subdirectory(qcomplextext)
endif()
add_subdirectory(qobjectrace)
add_subdirectory(toolsupport)
# QTBUG-87670
if(QT_FEATURE_process AND TARGET Qt::Gui AND NOT ANDROID)
add_subdirectory(qprocess_and_guieventloop)
endif()
if(QT_FEATURE_accessibility_atspi_bridge AND TARGET Qt::Gui AND TARGET Qt::Widgets)
#add_subdirectory(qaccessibilitylinux) # special case # This test is broken
endif()
if(MACOS AND TARGET Qt::Gui)
# add_subdirectory(macnativeevents) # special case it's disabled in qmake too
endif()
if(embedded)
add_subdirectory(qdirectpainter)
endif()
if(QT_FEATURE_xkbcommon AND TARGET Qt::Gui)
add_subdirectory(xkbkeyboard)
endif()

View File

@ -0,0 +1,62 @@
[panelPropagation]
ubuntu-20.04
ubuntu-22.04 ci
[panelStacksBehindParent]
ubuntu-20.04
ubuntu-22.04 ci
# QTBUG-108402
[gestureOverChildGraphicsItem]
macOS arm
# QTBUG-108402
[conflictingGesturesInGraphicsView]
macOS arm
# QTBUG-108402
[graphicsItemGesture]
macOS arm
# QTBUG-108402
[graphicsView]
macOS arm
# QTBUG-108402
[graphicsItemTreeGesture]
macOS arm
# QTBUG-108402
[explicitGraphicsObjectTarget]
macOS arm
# QTBUG-108402
[gestureOverChildGraphicsItem]
macOS arm
# QTBUG-108402
[autoCancelGestures2]
macOS arm
# QTBUG-108402
[graphicsViewParentPropagation]
macOS arm
# QTBUG-108402
[panelPropagation]
macOS arm
# QTBUG-108402
[panelStacksBehindParent]
macOS arm
# QTBUG-108402
[viewportCoordinates]
macOS arm
# QTBUG-108402
[partialGesturePropagation]
macOS arm
# QTBUG-108402
[testReuseCanceledGestures]
macOS arm

View File

@ -0,0 +1,14 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_gestures Test:
#####################################################################
qt_internal_add_test(tst_gestures
SOURCES
tst_gestures.cpp
LIBRARIES
Qt::Gui
Qt::Widgets
)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,16 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_languagechange Test:
#####################################################################
qt_internal_add_test(tst_languagechange
SOURCES
tst_languagechange.cpp
LIBRARIES
Qt::CorePrivate
Qt::Gui
Qt::GuiPrivate
Qt::Widgets
)

View File

@ -0,0 +1,286 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTest>
#include <QTimer>
#include <qapplication.h>
#include <private/qguiapplication_p.h>
#include <QtCore/QSet>
#include <QtCore/QFile>
#include <QtCore/QTranslator>
#include <QtCore/QTemporaryDir>
#include <private/qthread_p.h>
#include <qpa/qplatformtheme.h>
#include <QtWidgets/QInputDialog>
#include <QtWidgets/QColorDialog>
#include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QFileDialog>
class tst_languageChange : public QObject
{
Q_OBJECT
public:
tst_languageChange();
public slots:
void initTestCase();
void cleanupTestCase();
private slots:
void retranslatability_data();
void retranslatability();
private:
QDialogButtonBox::ButtonLayout m_layout;
};
tst_languageChange::tst_languageChange() :
m_layout(QDialogButtonBox::WinLayout)
{
if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
m_layout = static_cast<QDialogButtonBox::ButtonLayout>(theme->themeHint(QPlatformTheme::DialogButtonBoxLayout).toInt());
}
void tst_languageChange::initTestCase()
{
}
void tst_languageChange::cleanupTestCase()
{
}
/**
* Records all calls to translate()
*/
class TransformTranslator : public QTranslator
{
Q_OBJECT
public:
TransformTranslator() : QTranslator() {}
TransformTranslator(QObject *parent) : QTranslator(parent) {}
QString translate(const char *context, const char *sourceText,
const char *disambiguation = 0, int = -1) const override
{
QByteArray total(context);
total.append("::");
total.append(sourceText);
if (disambiguation) {
total.append("::");
total.append(disambiguation);
}
m_translations.insert(total);
QString res;
for (int i = 0; i < int(qstrlen(sourceText)); ++i) {
QChar ch = QLatin1Char(sourceText[i]);
if (ch.isLower()) {
res.append(ch.toUpper());
} else if (ch.isUpper()) {
res.append(ch.toLower());
} else {
res.append(ch);
}
}
return res;
}
virtual bool isEmpty() const override { return false; }
public:
mutable QSet<QByteArray> m_translations;
};
// Install the translator and close all application windows after a while to
// quit the event loop.
class LanguageTestStateMachine : public QObject
{
Q_OBJECT
public:
LanguageTestStateMachine(QTranslator *translator);
void start() { m_timer.start(); }
private slots:
void timeout();
private:
enum State { InstallTranslator, CloseDialog };
QTimer m_timer;
QTranslator *m_translator;
State m_state;
};
LanguageTestStateMachine::LanguageTestStateMachine(QTranslator *translator) :
m_translator(translator), m_state(InstallTranslator)
{
m_timer.setInterval(500);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
}
void LanguageTestStateMachine::timeout()
{
switch (m_state) {
case InstallTranslator:
m_timer.stop();
QCoreApplication::installTranslator(m_translator);
m_timer.setInterval(2500);
m_timer.start();
m_state = CloseDialog;
break;
case CloseDialog: // Close repeatedly in case file dialog is slow.
QApplication::closeAllWindows();
break;
}
}
enum DialogType {
InputDialog = 1,
ColorDialog,
FileDialog
};
typedef QSet<QByteArray> TranslationSet;
Q_DECLARE_METATYPE(TranslationSet)
void tst_languageChange::retranslatability_data()
{
QTest::addColumn<int>("dialogType");
QTest::addColumn<TranslationSet >("expected");
//next we fill it with data
QTest::newRow( "QInputDialog" )
<< int(InputDialog) << (QSet<QByteArray>()
<< "QPlatformTheme::Cancel");
QTest::newRow( "QColorDialog" )
<< int(ColorDialog) << (QSet<QByteArray>()
<< "QPlatformTheme::Cancel"
<< "QColorDialog::&Sat:"
<< "QColorDialog::&Add to Custom Colors"
<< "QColorDialog::&Green:"
<< "QColorDialog::&Red:"
<< "QColorDialog::Bl&ue:"
<< "QColorDialog::A&lpha channel:"
<< "QColorDialog::&Basic colors"
<< "QColorDialog::&Custom colors"
<< "QColorDialog::&Val:"
<< "QColorDialog::Hu&e:");
QTest::newRow( "QFileDialog" )
<< int(FileDialog) << (QSet<QByteArray>()
<< "QFileDialog::All Files (*)"
<< "QFileDialog::Back"
<< "QFileDialog::Create New Folder"
<< "QFileDialog::Detail View"
<< "QFileDialog::Files of type:"
<< "QFileDialog::Forward"
<< "QFileDialog::List View"
<< "QFileDialog::Look in:"
<< "QFileDialog::Open"
<< "QFileDialog::Parent Directory"
<< "QFileDialog::Show "
<< "QFileDialog::Show &hidden files"
<< "QFileDialog::&Delete"
<< "QFileDialog::&New Folder"
<< "QFileDialog::&Rename"
<< "QFileSystemModel::Date Modified"
#ifdef Q_OS_WIN
<< "QFileSystemModel::My Computer"
#else
<< "QFileSystemModel::Computer"
#endif
<< "QFileSystemModel::Size"
#ifdef Q_OS_MAC
<< "QFileSystemModel::Kind::Match OS X Finder"
#else
<< "QFileSystemModel::Type::All other platforms"
#endif
// << "QFileSystemModel::%1 KB"
<< "QPlatformTheme::Cancel"
<< "QPlatformTheme::Open"
<< "QFileDialog::File &name:");
}
void tst_languageChange::retranslatability()
{
QFETCH( int, dialogType);
QFETCH( TranslationSet, expected);
if (m_layout == QDialogButtonBox::GnomeLayout)
QSKIP("The input data are not suitable for this layout (QDialogButtonBox::GnomeLayout)");
// This will always be queried for when a language changes
expected.insert("QGuiApplication::QT_LAYOUT_DIRECTION::Translate this string to the string 'LTR' in left-to-right "
"languages or to 'RTL' in right-to-left languages (such as Hebrew and Arabic) to "
"get proper widget layout.");
TransformTranslator translator;
LanguageTestStateMachine stateMachine(&translator);
switch (dialogType) {
case InputDialog:
stateMachine.start();
QInputDialog::getInt(0, QLatin1String("title"), QLatin1String("label"));
break;
case ColorDialog:
#ifdef Q_OS_MAC
QSKIP("The native color dialog is used on Mac OS");
#else
stateMachine.start();
QColorDialog::getColor();
#endif
break;
case FileDialog: {
QFileDialog dlg;
dlg.setOption(QFileDialog::DontUseNativeDialog);
QString tempDirPattern = QDir::tempPath();
if (!tempDirPattern.endsWith(QLatin1Char('/')))
tempDirPattern += QLatin1Char('/');
tempDirPattern += QStringLiteral("languagechangetestdirXXXXXX");
QTemporaryDir temporaryDir(tempDirPattern);
temporaryDir.setAutoRemove(true);
QVERIFY2(temporaryDir.isValid(), qPrintable(temporaryDir.errorString()));
const QString finalDir = temporaryDir.path() + QStringLiteral("/finaldir");
const QString fooName = temporaryDir.path() + QStringLiteral("/foo");
QDir dir;
QVERIFY(dir.mkpath(finalDir));
QFile fooFile(fooName);
QVERIFY(fooFile.open(QIODevice::WriteOnly|QIODevice::Text));
fooFile.write("test");
fooFile.close();
dlg.setDirectory(temporaryDir.path());
dlg.setFileMode(QFileDialog::ExistingFiles);
dlg.setViewMode(QFileDialog::Detail);
stateMachine.start();
dlg.exec();
QTest::qWait(3000);
break; }
}
// In case we use a Color dialog, we do not want to test for
// strings non existing in the dialog and which do not get
// translated.
const QSize desktopSize = QGuiApplication::primaryScreen()->size();
if (dialogType == ColorDialog && (desktopSize.width() < 480 || desktopSize.height() < 350)) {
expected.remove("QColorDialog::&Basic colors");
expected.remove("QColorDialog::&Custom colors");
expected.remove("QColorDialog::&Define Custom Colors >>");
expected.remove("QColorDialog::&Add to Custom Colors");
}
// see if all of our *expected* translations was translated.
// (There might be more, but thats not that bad)
QSet<QByteArray> commonTranslations = expected;
commonTranslations.intersect(translator.m_translations);
if (!expected.subtract(commonTranslations).isEmpty()) {
qDebug() << "Missing:" << expected;
if (!translator.m_translations.subtract(commonTranslations).isEmpty())
qDebug() << "Unexpected:" << translator.m_translations;
}
QVERIFY(expected.isEmpty());
}
QTEST_MAIN(tst_languageChange)
#include "tst_languagechange.moc"

View File

@ -0,0 +1,5 @@
[nonModalOrder]
osx
[scrollbarPainting]
macos

View File

@ -0,0 +1,30 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
if(NOT APPLE)
return()
endif()
if(NOT TARGET Qt::Widgets)
return()
endif()
#####################################################################
## tst_macgui Test:
#####################################################################
qt_internal_add_test(tst_macgui
SOURCES
guitest.cpp guitest.h
tst_macgui.cpp
LIBRARIES
Qt::CorePrivate
Qt::WidgetsPrivate
)
## Scopes:
#####################################################################
qt_internal_extend_target(tst_macgui CONDITION MACOS
LIBRARIES
${FWApplicationServices}
)

View File

@ -0,0 +1,300 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "guitest.h"
#include <QDebug>
#include <QWidget>
#include <QStack>
#include <QTimer>
#include <QTest>
#include <QTestEventLoop>
#ifdef Q_OS_MAC
# include <ApplicationServices/ApplicationServices.h>
#endif
/*
Not really a test, just prints interface info.
*/
class PrintTest : public TestBase
{
public:
bool operator()(QAccessibleInterface *candidate)
{
qDebug() << "";
qDebug() << "Name" << candidate->text(QAccessible::Name);
qDebug() << "Pos" << candidate->rect();
qDebug() << "Number of children" << candidate->childCount();
return false;
}
};
class NameTest : public TestBase
{
public:
NameTest(const QString &text, QAccessible::Text textType) : text(text), textType(textType) {}
QString text;
QAccessible::Text textType;
bool operator()(QAccessibleInterface *candidate)
{
return (candidate->text(textType) == text);
}
};
void WidgetNavigator::printAll(QWidget *widget)
{
QAccessibleInterface * const iface = QAccessible::queryAccessibleInterface(widget);
printAll(iface);
}
void WidgetNavigator::printAll(QAccessibleInterface *interface)
{
PrintTest printTest;
recursiveSearch(&printTest, interface);
}
QAccessibleInterface *WidgetNavigator::find(QAccessible::Text textType, const QString &text, QWidget *start)
{
QAccessibleInterface *const iface = QAccessible::queryAccessibleInterface(start);
return find(textType, text, iface);
}
QAccessibleInterface *WidgetNavigator::find(QAccessible::Text textType, const QString &text, QAccessibleInterface *start)
{
NameTest nameTest(text, textType);
return recursiveSearch(&nameTest, start);
}
/*
Recursiveley navigates the accessible hiearchy looking for an interface that
passsed the Test (meaning it returns true).
*/
QAccessibleInterface *WidgetNavigator::recursiveSearch(TestBase *test, QAccessibleInterface *iface)
{
QStack<QAccessibleInterface *> todoInterfaces;
todoInterfaces.push(iface);
while (todoInterfaces.isEmpty() == false) {
QAccessibleInterface *testInterface = todoInterfaces.pop();
if ((*test)(testInterface))
return testInterface;
const int numChildren = testInterface->childCount();
for (int i = 0; i < numChildren; ++i) {
QAccessibleInterface *childInterface = testInterface->child(i);
if (childInterface) {
todoInterfaces.push(childInterface);
}
}
}
return 0;
}
QWidget *WidgetNavigator::getWidget(QAccessibleInterface *interface)
{
return qobject_cast<QWidget *>(interface->object());
}
WidgetNavigator::~WidgetNavigator()
{
}
///////////////////////////////////////////////////////////////////////////////
namespace NativeEvents {
#ifdef Q_OS_MAC
void mouseClick(const QPoint &globalPos, Qt::MouseButtons buttons)
{
CGPoint position;
position.x = globalPos.x();
position.y = globalPos.y();
CGEventType mouseDownType = (buttons & Qt::LeftButton) ? kCGEventLeftMouseDown :
(buttons & Qt::RightButton) ? kCGEventRightMouseDown :
kCGEventOtherMouseDown;
// The mouseButton argument to CGEventCreateMouseEvent() is ignored unless the type
// is kCGEventOtherMouseDown, so defaulting to kCGMouseButtonLeft is fine.
CGMouseButton mouseButton = mouseDownType == kCGEventOtherMouseDown ? kCGMouseButtonCenter : kCGMouseButtonLeft;
CGEventRef mouseEvent = CGEventCreateMouseEvent(NULL, mouseDownType, position, mouseButton);
CGEventPost(kCGHIDEventTap, mouseEvent);
CGEventType mouseUpType = (buttons & Qt::LeftButton) ? kCGEventLeftMouseUp :
(buttons & Qt::RightButton) ? kCGEventRightMouseUp :
kCGEventOtherMouseUp;
CGEventSetType(mouseEvent, mouseUpType);
CGEventPost(kCGHIDEventTap, mouseEvent);
CFRelease(mouseEvent);
}
#else
# error Oops, NativeEvents::mouseClick() is not implemented on this platform.
#endif
};
///////////////////////////////////////////////////////////////////////////////
GuiTester::GuiTester()
{
clearSequence();
}
GuiTester::~GuiTester()
{
foreach(DelayedAction *action, actions)
delete action;
}
bool checkPixel(QColor pixel, QColor expected)
{
const int allowedDiff = 20;
return !(qAbs(pixel.red() - expected.red()) > allowedDiff ||
qAbs(pixel.green() - expected.green()) > allowedDiff ||
qAbs(pixel.blue() - expected.blue()) > allowedDiff);
}
/*
Tests that the pixels inside rect in image all have the given color.
*/
bool GuiTester::isFilled(const QImage image, const QRect &rect, const QColor &color)
{
for (int y = rect.top(); y <= rect.bottom(); ++y)
for (int x = rect.left(); x <= rect.right(); ++x) {
const QColor pixel = image.pixel(x, y);
if (checkPixel(pixel, color) == false) {
// qDebug()<< "Wrong pixel value at" << x << y << pixel.red() << pixel.green() << pixel.blue();
return false;
}
}
return true;
}
/*
Tests that stuff is painted to the pixels inside rect.
This test fails if any lines in the given direction have pixels
of only one color.
*/
bool GuiTester::isContent(const QImage image, const QRect &rect, Directions directions)
{
if (directions & Horizontal) {
for (int y = rect.top(); y <= rect.bottom(); ++y) {
QColor currentColor = image.pixel(rect.left(), y);
bool fullRun = true;
for (int x = rect.left() + 1; x <= rect.right(); ++x) {
if (checkPixel(image.pixel(x, y), currentColor) == false) {
fullRun = false;
break;
}
}
if (fullRun) {
// qDebug() << "Single-color line at horizontal line " << y << currentColor;
return false;
}
}
return true;
}
if (directions & Vertical) {
for (int x = rect.left(); x <= rect.right(); ++x) {
QRgb currentColor = image.pixel(x, rect.top());
bool fullRun = true;
for (int y = rect.top() + 1; y <= rect.bottom(); ++y) {
if (checkPixel(image.pixel(x, y), currentColor) == false) {
fullRun = false;
break;
}
}
if (fullRun) {
// qDebug() << "Single-color line at vertical line" << x << currentColor;
return false;
}
}
return true;
}
return false; // shut the compiler up.
}
void DelayedAction::run()
{
if (next)
QTimer::singleShot(next->delay, next, SLOT(run()));
};
/*
Schedules a mouse click at an interface using a singleShot timer.
Only one click can be scheduled at a time.
*/
ClickLaterAction::ClickLaterAction(QAccessibleInterface *interface, Qt::MouseButtons buttons)
{
this->useInterface = true;
this->interface = interface;
this->buttons = buttons;
}
/*
Schedules a mouse click at a widget using a singleShot timer.
Only one click can be scheduled at a time.
*/
ClickLaterAction::ClickLaterAction(QWidget *widget, Qt::MouseButtons buttons)
{
this->useInterface = false;
this->widget = widget;
this->buttons = buttons;
}
void ClickLaterAction::run()
{
if (useInterface) {
const QPoint globalCenter = interface->rect().center();
NativeEvents::mouseClick(globalCenter, buttons);
} else { // use widget
const QSize halfSize = widget->size() / 2;
const QPoint globalCenter = widget->mapToGlobal(QPoint(halfSize.width(), halfSize.height()));
NativeEvents::mouseClick(globalCenter, buttons);
}
DelayedAction::run();
}
void GuiTester::clickLater(QAccessibleInterface *interface, Qt::MouseButtons buttons, int delay)
{
clearSequence();
addToSequence(new ClickLaterAction(interface, buttons), delay);
runSequence();
}
void GuiTester::clickLater(QWidget *widget, Qt::MouseButtons buttons, int delay)
{
clearSequence();
addToSequence(new ClickLaterAction(widget, buttons), delay);
runSequence();
}
void GuiTester::clearSequence()
{
startAction = new DelayedAction();
actions.insert(startAction);
lastAction = startAction;
}
void GuiTester::addToSequence(DelayedAction *action, int delay)
{
actions.insert(action);
action->delay = delay;
lastAction->next = action;
lastAction = action;
}
void GuiTester::runSequence()
{
QTimer::singleShot(0, startAction, SLOT(run()));
}
void GuiTester::exitLoopSlot()
{
QTestEventLoop::instance().exitLoop();
}

View File

@ -0,0 +1,132 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef GUITEST_H
#define GUITEST_H
#include <QAccessibleInterface>
#include <QSet>
#include <QWidget>
#include <QPainter>
QT_USE_NAMESPACE
/*
GuiTest provides tools for:
- navigating the Qt Widget hiearchy using the accessibilty APIs.
- Simulating platform mouse and keybord events.
*/
class TestBase {
public:
virtual bool operator()(QAccessibleInterface *candidate) = 0;
virtual ~TestBase() {}
};
/*
WidgetNavigator navigates a Qt GUI hierarchy using the QAccessibility APIs.
*/
class WidgetNavigator {
public:
WidgetNavigator() {}
~WidgetNavigator();
void printAll(QWidget *widget);
void printAll(QAccessibleInterface *interface);
QAccessibleInterface *find(QAccessible::Text textType, const QString &text, QWidget *start);
QAccessibleInterface *find(QAccessible::Text textType, const QString &text, QAccessibleInterface *start);
QAccessibleInterface *recursiveSearch(TestBase *test, QAccessibleInterface *iface);
static QWidget *getWidget(QAccessibleInterface *interface);
private:
QSet<QAccessibleInterface *> interfaces;
};
/*
NativeEvents contains platform-specific code for simulating mouse and keybord events.
(Implemented so far: mouseClick on Mac)
*/
namespace NativeEvents {
/*
Simulates a mouse click with button at globalPos.
*/
void mouseClick(const QPoint &globalPos, Qt::MouseButtons buttons);
};
class ColorWidget : public QWidget
{
public:
ColorWidget(QWidget *parent = nullptr, QColor color = QColor(Qt::red))
: QWidget(parent), color(color) {}
QColor color;
protected:
void paintEvent(QPaintEvent *)
{
QPainter p(this);
p.fillRect(this->rect(), color);
}
};
class DelayedAction : public QObject
{
Q_OBJECT
public:
DelayedAction() : delay(0), next(0) {}
virtual ~DelayedAction(){}
public slots:
virtual void run();
public:
int delay;
DelayedAction *next;
};
class ClickLaterAction : public DelayedAction
{
Q_OBJECT
public:
ClickLaterAction(QAccessibleInterface *interface, Qt::MouseButtons buttons = Qt::LeftButton);
ClickLaterAction(QWidget *widget, Qt::MouseButtons buttons = Qt::LeftButton);
protected slots:
void run();
private:
bool useInterface;
QAccessibleInterface *interface;
QWidget *widget;
Qt::MouseButtons buttons;
};
/*
*/
class GuiTester : public QObject
{
Q_OBJECT
public:
GuiTester();
~GuiTester();
enum Direction {Horizontal = 1, Vertical = 2, HorizontalAndVertical = 3};
Q_DECLARE_FLAGS(Directions, Direction)
bool isFilled(const QImage image, const QRect &rect, const QColor &color);
bool isContent(const QImage image, const QRect &rect, Directions directions = HorizontalAndVertical);
protected slots:
void exitLoopSlot();
protected:
void clickLater(QAccessibleInterface *interface, Qt::MouseButtons buttons = Qt::LeftButton, int delay = 300);
void clickLater(QWidget *widget, Qt::MouseButtons buttons = Qt::LeftButton, int delay = 300);
void clearSequence();
void addToSequence(DelayedAction *action, int delay = 0);
void runSequence();
WidgetNavigator wn;
private:
QSet<DelayedAction *> actions;
DelayedAction *startAction;
DelayedAction *lastAction;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(GuiTester::Directions)
#endif

View File

@ -0,0 +1,215 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QApplication>
#include <QMessageBox>
#include <QTest>
#include <QSplashScreen>
#include <QScrollBar>
#include <QProgressDialog>
#include <QSpinBox>
#include <QScreen>
#include <QTestEventLoop>
#include <QTimer>
#include <guitest.h>
class tst_MacGui : public GuiTester
{
Q_OBJECT
private slots:
void scrollbarPainting();
void dummy();
void splashScreenModality();
void nonModalOrder();
void spinBoxArrowButtons();
};
QPixmap grabWindowContents(QWidget * widget)
{
QScreen *screen = widget->window()->windowHandle()->screen();
if (!screen) {
qWarning() << "Grabbing pixmap failed, no QScreen for" << widget;
return QPixmap();
}
return screen->grabWindow(widget->winId());
}
/*
Test that vertical and horizontal mac-style scrollbars paint their
entire area.
*/
void tst_MacGui::scrollbarPainting()
{
ColorWidget colorWidget;
colorWidget.resize(400, 400);
QSize scrollBarSize;
QScrollBar verticalScrollbar(&colorWidget);
verticalScrollbar.move(10, 10);
scrollBarSize = verticalScrollbar.sizeHint();
scrollBarSize.setHeight(200);
verticalScrollbar.resize(scrollBarSize);
QScrollBar horizontalScrollbar(&colorWidget);
horizontalScrollbar.move(30, 10);
horizontalScrollbar.setOrientation(Qt::Horizontal);
scrollBarSize = horizontalScrollbar.sizeHint();
scrollBarSize.setWidth(200);
horizontalScrollbar.resize(scrollBarSize);
colorWidget.show();
colorWidget.raise();
QTest::qWait(100);
QPixmap pixmap = grabWindowContents(&colorWidget);
QVERIFY(isContent(pixmap.toImage(), verticalScrollbar.geometry(), GuiTester::Horizontal));
QVERIFY(isContent(pixmap.toImage(), horizontalScrollbar.geometry(), GuiTester::Vertical));
}
// When running the auto-tests on scruffy, the first enter-the-event-loop-and-wait-for-a-click
// test that runs always times out, so we have this dummy test.
void tst_MacGui::dummy()
{
QPixmap pix(100, 100);
QSplashScreen splash(pix);
splash.show();
QMessageBox *box = new QMessageBox();
box->setText("accessible?");
box->show();
// Find the "OK" button and schedule a press.
QAccessibleInterface *interface = wn.find(QAccessible::Name, "OK", box);
QVERIFY(interface);
const int delay = 1000;
clickLater(interface, Qt::LeftButton, delay);
// Show dialog and enter event loop.
connect(wn.getWidget(interface), SIGNAL(clicked()), SLOT(exitLoopSlot()));
const int timeout = 4;
QTestEventLoop::instance().enterLoop(timeout);
}
/*
Test that a message box pops up in front of a QSplashScreen.
*/
void tst_MacGui::splashScreenModality()
{
QPixmap pix(300, 300);
QSplashScreen splash(pix);
splash.show();
QMessageBox box;
//box.setWindowFlags(box.windowFlags() | Qt::WindowStaysOnTopHint);
box.setText("accessible?");
box.show();
QSKIP("QTBUG-35169");
// Find the "OK" button and schedule a press.
QAccessibleInterface *interface = wn.find(QAccessible::Name, "OK", &box);
QVERIFY(interface);
const int delay = 1000;
clickLater(interface, Qt::LeftButton, delay);
// Show dialog and enter event loop.
connect(wn.getWidget(interface), SIGNAL(clicked()), SLOT(exitLoopSlot()));
const int timeout = 4;
QTestEventLoop::instance().enterLoop(timeout);
QVERIFY(!QTestEventLoop::instance().timeout());
}
class PrimaryWindowDialog : public QDialog
{
Q_OBJECT
public:
PrimaryWindowDialog();
QWidget *secondaryWindow;
QWidget *frontWidget;
public slots:
void showSecondaryWindow();
void test();
};
PrimaryWindowDialog::PrimaryWindowDialog() : QDialog(0)
{
frontWidget = 0;
secondaryWindow = new ColorWidget(this);
secondaryWindow->setWindowFlags(Qt::Window);
secondaryWindow->resize(400, 400);
secondaryWindow->move(100, 100);
QTimer::singleShot(1000, this, SLOT(showSecondaryWindow()));
QTimer::singleShot(2000, this, SLOT(test()));
QTimer::singleShot(3000, this, SLOT(close()));
}
void PrimaryWindowDialog::showSecondaryWindow()
{
secondaryWindow->show();
}
void PrimaryWindowDialog::test()
{
frontWidget = QApplication::widgetAt(secondaryWindow->mapToGlobal(QPoint(100, 100)));
}
/*
Test that a non-modal child window of a modal dialog is shown in front
of the dialog even if the dialog becomes modal after the child window
is created.
*/
void tst_MacGui::nonModalOrder()
{
clearSequence();
PrimaryWindowDialog primary;
primary.resize(400, 400);
primary.move(100, 100);
primary.exec();
QCOMPARE(primary.frontWidget, primary.secondaryWindow);
}
/*
Test that the QSpinBox buttons are correctly positioned with the Mac style.
*/
void tst_MacGui::spinBoxArrowButtons()
{
ColorWidget colorWidget;
colorWidget.resize(200, 200);
QSpinBox spinBox(&colorWidget);
QSpinBox spinBox2(&colorWidget);
spinBox2.move(0, 100);
colorWidget.show();
QTest::qWait(100);
// Grab an unfocused spin box.
const QImage noFocus = grabWindowContents(&colorWidget).toImage();
// Set focus by clicking the less button.
QAccessibleInterface *lessInterface = wn.find(QAccessible::Name, "Less", &spinBox);
QEXPECT_FAIL("", "QTBUG-26372", Abort);
QVERIFY(lessInterface);
const int delay = 500;
clickLater(lessInterface, Qt::LeftButton, delay);
const int timeout = 1;
QTestEventLoop::instance().enterLoop(timeout);
// Grab a focused spin box.
const QImage focus = grabWindowContents(&colorWidget).toImage();
// Compare the arrow area of the less button to see if it moved.
const QRect lessRect = lessInterface->rect();
const QRect lessLocalRect(colorWidget.mapFromGlobal(lessRect.topLeft()), colorWidget.mapFromGlobal(lessRect.bottomRight()));
const QRect compareRect = lessLocalRect.adjusted(5, 3, -5, -7);
QCOMPARE(noFocus.copy(compareRect), focus.copy(compareRect));
}
QTEST_MAIN(tst_MacGui)
#include "tst_macgui.moc"

View File

@ -0,0 +1,39 @@
# QTBUG-22775
[testDragWindow]
osx
[testMouseEnter]
osx
[testChildDialogInFrontOfModalParent]
osx
[testChildWindowInFrontOfStaysOnTopParentWindow]
osx
[testMouseMoveLocation]
osx
[testMouseLeftDoubleClick]
osx
[stressTestMouseLeftDoubleClick]
osx
[testMouseDragInside]
osx
[testMouseDragOutside]
osx
[testMouseDragToNonClientArea]
osx
macos ci
# The following key tests fail after switching to synchronous
# expose events, and we don't know why yet. QTBUG-62042
[testKeyPressOnToplevel]
osx
[testModifierShift]
osx
[testModifierAlt]
osx
[testModifierCtrl]
osx
# QTQAINFRA-1292
[testPushButtonPressRelease]
macos ci
# QTQAINFRA-1292
[testModifierCtrlWithDontSwapCtrlAndMeta]
macos ci

View File

@ -0,0 +1,23 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
if(NOT APPLE)
return()
endif()
#####################################################################
## tst_macnativeevents Test:
#####################################################################
qt_internal_add_test(tst_macnativeevents
SOURCES
expectedeventlist.cpp expectedeventlist.h
nativeeventlist.cpp nativeeventlist.h
qnativeevents.cpp qnativeevents.h
qnativeevents_mac.cpp
tst_macnativeevents.cpp
LIBRARIES
${FWAppKit}
Qt::Gui
Qt::Widgets
)

View File

@ -0,0 +1,176 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "expectedeventlist.h"
#include <QDebug>
#include <QCoreApplication>
#include <QAbstractEventDispatcher>
#include <QTest>
ExpectedEventList::ExpectedEventList(QObject *target)
: QObject(target), eventCount(0)
{
target->installEventFilter(this);
debug = qgetenv("NATIVEDEBUG").toInt();
if (debug > 0)
qDebug() << "Debug level sat to:" << debug;
}
ExpectedEventList::~ExpectedEventList()
{
qDeleteAll(eventList);
}
void ExpectedEventList::append(QEvent *e)
{
eventList.append(e);
++eventCount;
}
void ExpectedEventList::timerEvent(QTimerEvent *)
{
timer.stop();
QAbstractEventDispatcher::instance()->interrupt();
}
bool ExpectedEventList::waitForAllEvents(int maxEventWaitTime)
{
if (eventList.isEmpty())
return true;
int eventCount = eventList.size();
timer.start(maxEventWaitTime, this);
while (timer.isActive()) {
QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
if (eventList.isEmpty())
return true;
if (eventCount < eventList.size()){
eventCount = eventList.size();
timer.start(maxEventWaitTime, this);
}
}
int eventListNr = eventCount - eventList.size() + 1;
qWarning() << "Stopped waiting for expected event nr" << eventListNr;
return false;
}
void ExpectedEventList::compareMouseEvents(QEvent *received, QEvent *expected)
{
QMouseEvent *e1 = static_cast<QMouseEvent *>(received);
QMouseEvent *e2 = static_cast<QMouseEvent *>(expected);
// Do a manual check first to be able to write more sensible
// debug output if we know we're going to fail:
if (e1->pos() == e2->pos()
&& (e1->globalPos() == e2->globalPos())
&& (e1->button() == e2->button())
&& (e1->buttons() == e2->buttons())
&& (e1->modifiers() == e2->modifiers())) {
if (debug > 0)
qDebug() << " Received (OK):" << e1 << e1->globalPos();
return; // equal
}
// INVARIANT: The two events are not equal. So we fail. Depending
// on whether debug mode is no or not, we let QTest fail. Otherwise
// we let the test continue for debugging puposes.
int eventListNr = eventCount - eventList.size();
if (debug == 0) {
qWarning() << "Expected event" << eventListNr << "differs from received event:";
QCOMPARE(e1->pos(), e2->pos());
QCOMPARE(e1->globalPos(), e2->globalPos());
QCOMPARE(e1->button(), e2->button());
QCOMPARE(e1->buttons(), e2->buttons());
QCOMPARE(e1->modifiers(), e2->modifiers());
} else {
qWarning() << "*** FAIL *** : Expected event" << eventListNr << "differs from received event:";
qWarning() << "Received:" << e1 << e1->globalPos();
qWarning() << "Expected:" << e2 << e2->globalPos();
}
}
void ExpectedEventList::compareKeyEvents(QEvent *received, QEvent *expected)
{
QKeyEvent *e1 = static_cast<QKeyEvent *>(received);
QKeyEvent *e2 = static_cast<QKeyEvent *>(expected);
// Do a manual check first to be able to write more sensible
// debug output if we know we're going to fail:
if (e1->key() == e2->key()
&& (e1->modifiers() == e2->modifiers())
&& (e1->count() == e2->count())
&& (e1->isAutoRepeat() == e2->isAutoRepeat())) {
if (debug > 0)
qDebug() << " Received (OK):" << e1 << QKeySequence(e1->key()).toString(QKeySequence::NativeText);
return; // equal
}
// INVARIANT: The two events are not equal. So we fail. Depending
// on whether debug mode is no or not, we let QTest fail. Otherwise
// we let the test continue for debugging puposes.
int eventListNr = eventCount - eventList.size();
if (debug == 0) {
qWarning() << "Expected event" << eventListNr << "differs from received event:";
QCOMPARE(e1->key(), e2->key());
QCOMPARE(e1->modifiers(), e2->modifiers());
QCOMPARE(e1->count(), e2->count());
QCOMPARE(e1->isAutoRepeat(), e2->isAutoRepeat());
} else {
qWarning() << "*** FAIL *** : Expected event" << eventListNr << "differs from received event:";
qWarning() << "Received:" << e1 << QKeySequence(e1->key()).toString(QKeySequence::NativeText);
qWarning() << "Expected:" << e2 << QKeySequence(e2->key()).toString(QKeySequence::NativeText);
}
}
bool ExpectedEventList::eventFilter(QObject *, QEvent *received)
{
if (debug > 1)
qDebug() << received;
if (eventList.isEmpty())
return false;
bool eat = false;
QEvent *expected = eventList.first();
if (expected->type() == received->type()) {
eventList.removeFirst();
switch (received->type()) {
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseMove:
case QEvent::MouseButtonDblClick:
case QEvent::NonClientAreaMouseButtonPress:
case QEvent::NonClientAreaMouseButtonRelease:
case QEvent::NonClientAreaMouseButtonDblClick:
case QEvent::NonClientAreaMouseMove: {
compareMouseEvents(received, expected);
eat = true;
break;
}
case QEvent::KeyPress:
case QEvent::KeyRelease: {
compareKeyEvents(received, expected);
eat = true;
break;
}
case QEvent::Resize: {
break;
}
case QEvent::WindowActivate: {
break;
}
case QEvent::WindowDeactivate: {
break;
}
default:
break;
}
if (eventList.isEmpty())
QAbstractEventDispatcher::instance()->interrupt();
}
return eat;
}

View File

@ -0,0 +1,33 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef EVENTFILTER
#define EVENTFILTER
#include <QWidget>
#include <QList>
#include <QEvent>
#include <QBasicTimer>
class ExpectedEventList : public QObject
{
QList<QEvent *> eventList;
QBasicTimer timer;
int debug;
int eventCount;
void timerEvent(QTimerEvent *);
public:
ExpectedEventList(QObject *target);
~ExpectedEventList();
void append(QEvent *e);
bool waitForAllEvents(int timeoutPerEvent = 2000);
bool eventFilter(QObject *obj, QEvent *event);
private:
void compareMouseEvents(QEvent *event1, QEvent *event2);
void compareKeyEvents(QEvent *event1, QEvent *event2);
};
#endif

View File

@ -0,0 +1,76 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "nativeeventlist.h"
NativeEventList::NativeEventList(int defaultWaitMs)
: playbackMultiplier(1.0)
, currIndex(-1)
, wait(false)
, defaultWaitMs(defaultWaitMs)
{
debug = qgetenv("NATIVEDEBUG").toInt();
QString multiplier = qgetenv("NATIVEDEBUGSPEED");
if (!multiplier.isEmpty())
setTimeMultiplier(multiplier.toFloat());
}
NativeEventList::~NativeEventList()
{
for (int i=0; i<eventList.size(); i++)
delete eventList.takeAt(i).second;
}
void NativeEventList::sendNextEvent()
{
QNativeEvent *e = eventList.at(currIndex).second;
if (e) {
if (debug > 0)
qDebug() << "Sending:" << *e;
QNativeInput::sendNativeEvent(*e);
}
waitNextEvent();
}
void NativeEventList::waitNextEvent()
{
if (++currIndex >= eventList.size()){
emit done();
stop();
return;
}
int interval = eventList.at(currIndex).first;
QTimer::singleShot(interval * playbackMultiplier, this, SLOT(sendNextEvent()));
}
void NativeEventList::append(QNativeEvent *event)
{
eventList.append(QPair<int, QNativeEvent *>(defaultWaitMs, event));
}
void NativeEventList::append(int waitMs, QNativeEvent *event)
{
eventList.append(QPair<int, QNativeEvent *>(waitMs, event));
}
void NativeEventList::play(Playback playback)
{
waitNextEvent();
wait = (playback == WaitUntilFinished);
while (wait)
QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
}
void NativeEventList::stop()
{
wait = false;
QAbstractEventDispatcher::instance()->interrupt();
}
void NativeEventList::setTimeMultiplier(float multiplier)
{
playbackMultiplier = multiplier;
}

View File

@ -0,0 +1,44 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef Q_NATIVE_PLAYBACK
#define Q_NATIVE_PLAYBACK
#include <QtCore>
#include "qnativeevents.h"
class NativeEventList : public QObject
{
Q_OBJECT;
public:
enum Playback {ReturnImmediately, WaitUntilFinished};
NativeEventList(int defaultWaitMs = 20);
~NativeEventList();
void append(QNativeEvent *event);
void append(int waitMs, QNativeEvent *event = nullptr);
void play(Playback playback = WaitUntilFinished);
void stop();
void setTimeMultiplier(float multiplier);
signals:
void done();
private slots:
void sendNextEvent();
private:
void waitNextEvent();
QList<QPair<int, QNativeEvent *> > eventList;
float playbackMultiplier;
int currIndex;
bool wait;
int defaultWaitMs;
int debug;
};
#endif

View File

@ -0,0 +1,340 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "qnativeevents.h"
QNativeInput::QNativeInput(bool subscribe)
{
if (subscribe)
subscribeForNativeEvents();
}
QNativeInput::~QNativeInput()
{
unsubscribeForNativeEvents();
}
void QNativeInput::notify(QNativeEvent *event)
{
nativeEvent(event);
}
void QNativeInput::nativeEvent(QNativeEvent *event)
{
switch (event->id()){
case QNativeMouseButtonEvent::eventId:{
QNativeMouseButtonEvent *e = static_cast<QNativeMouseButtonEvent *>(event);
(e->clickCount > 0) ? nativeMousePressEvent(e) : nativeMouseReleaseEvent(e);
break; }
case QNativeMouseMoveEvent::eventId:
nativeMouseMoveEvent(static_cast<QNativeMouseMoveEvent *>(event));
break;
case QNativeMouseDragEvent::eventId:
nativeMouseDragEvent(static_cast<QNativeMouseDragEvent *>(event));
break;
case QNativeMouseWheelEvent::eventId:
nativeMouseWheelEvent(static_cast<QNativeMouseWheelEvent *>(event));
break;
case QNativeKeyEvent::eventId:{
QNativeKeyEvent *e = static_cast<QNativeKeyEvent *>(event);
e->press ? nativeKeyPressEvent(e) : nativeKeyReleaseEvent(e);
break; }
case QNativeModifierEvent::eventId:
nativeModifierEvent(static_cast<QNativeModifierEvent *>(event));
break;
default:
break;
}
}
Qt::Native::Status QNativeInput::sendNativeEvent(const QNativeEvent &event)
{
switch (event.id()){
case QNativeMouseMoveEvent::eventId:
return sendNativeMouseMoveEvent(static_cast<const QNativeMouseMoveEvent &>(event));
case QNativeMouseButtonEvent::eventId:
return sendNativeMouseButtonEvent(static_cast<const QNativeMouseButtonEvent &>(event));
case QNativeMouseDragEvent::eventId:
return sendNativeMouseDragEvent(static_cast<const QNativeMouseDragEvent &>(event));
case QNativeMouseWheelEvent::eventId:
return sendNativeMouseWheelEvent(static_cast<const QNativeMouseWheelEvent &>(event));
case QNativeKeyEvent::eventId:
return sendNativeKeyEvent(static_cast<const QNativeKeyEvent &>(event));
case QNativeModifierEvent::eventId:
return sendNativeModifierEvent(static_cast<const QNativeModifierEvent &>(event));
case QNativeEvent::eventId:
qWarning() << "Warning: Cannot send a pure native event. Use a sub class.";
default:
return Qt::Native::Failure;
}
}
QNativeEvent::QNativeEvent(Qt::KeyboardModifiers modifiers)
: modifiers(modifiers){}
QNativeMouseEvent::QNativeMouseEvent(QPoint pos, Qt::KeyboardModifiers modifiers)
: QNativeEvent(modifiers), globalPos(pos){}
QNativeMouseMoveEvent::QNativeMouseMoveEvent(QPoint pos, Qt::KeyboardModifiers modifiers)
: QNativeMouseEvent(pos, modifiers){}
QNativeMouseButtonEvent::QNativeMouseButtonEvent(QPoint globalPos, Qt::MouseButton button, int clickCount, Qt::KeyboardModifiers modifiers)
: QNativeMouseEvent(globalPos, modifiers), button(button), clickCount(clickCount){}
QNativeMouseDragEvent::QNativeMouseDragEvent(QPoint globalPos, Qt::MouseButton button, Qt::KeyboardModifiers modifiers)
: QNativeMouseButtonEvent(globalPos, button, true, modifiers){}
QNativeMouseWheelEvent::QNativeMouseWheelEvent(QPoint globalPos, int delta, Qt::KeyboardModifiers modifiers)
: QNativeMouseEvent(globalPos, modifiers), delta(delta){}
QNativeKeyEvent::QNativeKeyEvent(int nativeKeyCode, bool press, Qt::KeyboardModifiers modifiers)
: QNativeEvent(modifiers), nativeKeyCode(nativeKeyCode), press(press), character(QChar()){}
QNativeModifierEvent::QNativeModifierEvent(Qt::KeyboardModifiers modifiers, int nativeKeyCode)
: QNativeEvent(modifiers), nativeKeyCode(nativeKeyCode){}
QNativeKeyEvent::QNativeKeyEvent(int nativeKeyCode, bool press, QChar character, Qt::KeyboardModifiers modifiers)
: QNativeEvent(modifiers), nativeKeyCode(nativeKeyCode), press(press), character(character){}
static QString getButtonAsString(const QNativeMouseButtonEvent *e)
{
switch (e->button){
case Qt::LeftButton:
return "button = LeftButton";
break;
case Qt::RightButton:
return "button = RightButton";
break;
case Qt::MiddleButton:
return "button = MiddleButton";
break;
default:
return "button = Other";
break;
}
}
static QString getModifiersAsString(const QNativeEvent *e)
{
if (e->modifiers == 0)
return "modifiers = none";
QString tmp = "modifiers = ";
if (e->modifiers.testFlag(Qt::ShiftModifier))
tmp += "Shift";
if (e->modifiers.testFlag(Qt::ControlModifier))
tmp += "Control";
if (e->modifiers.testFlag(Qt::AltModifier))
tmp += "Alt";
if (e->modifiers.testFlag(Qt::MetaModifier))
tmp += "Meta";
return tmp;
}
static QString getPosAsString(QPoint pos)
{
return QString("QPoint(%1, %2)").arg(pos.x()).arg(pos.y());
}
static QString getBoolAsString(bool b)
{
return b ? QString("true") : QString("false");
}
QString QNativeMouseMoveEvent::toString() const
{
return QString("QNativeMouseMoveEvent(globalPos = %1 %2)").arg(getPosAsString(globalPos))
.arg(getModifiersAsString(this));
}
QString QNativeMouseButtonEvent::toString() const
{
return QString("QNativeMouseButtonEvent(globalPos = %1, %2, clickCount = %3, %4)").arg(getPosAsString(globalPos))
.arg(getButtonAsString(this)).arg(clickCount).arg(getModifiersAsString(this));
}
QString QNativeMouseDragEvent::toString() const
{
return QString("QNativeMouseDragEvent(globalPos = %1, %2, clickCount = %3, %4)").arg(getPosAsString(globalPos))
.arg(getButtonAsString(this)).arg(clickCount).arg(getModifiersAsString(this));
}
QString QNativeMouseWheelEvent::toString() const
{
return QString("QNativeMouseWheelEvent(globalPos = %1, delta = %2, %3)").arg(getPosAsString(globalPos))
.arg(delta).arg(getModifiersAsString(this));
}
QString QNativeKeyEvent::toString() const
{
return QString("QNativeKeyEvent(press = %1, native key code = %2, character = %3, %4)").arg(getBoolAsString(press))
.arg(nativeKeyCode).arg(character.isPrint() ? character : QString("<no char>"))
.arg(getModifiersAsString(this));
}
QString QNativeModifierEvent::toString() const
{
return QString("QNativeModifierEvent(%1, native key code = %2)").arg(getModifiersAsString(this))
.arg(nativeKeyCode);
}
QDebug operator<<(QDebug d, QNativeEvent *e)
{
Q_UNUSED(e);
return d << e->toString();
}
QDebug operator<<(QDebug d, const QNativeEvent &e)
{
Q_UNUSED(e);
return d << e.toString();
}
QTextStream &operator<<(QTextStream &s, QNativeEvent *e)
{
return s << e->eventId << ' ' << e->modifiers << " QNativeEvent";
}
QTextStream &operator<<(QTextStream &s, QNativeMouseEvent *e)
{
return s << e->eventId << ' ' << e->globalPos.x() << ' ' << e->globalPos.y() << ' ' << e->modifiers << ' ' << e->toString();
}
QTextStream &operator<<(QTextStream &s, QNativeMouseMoveEvent *e)
{
return s << e->eventId << ' ' << e->globalPos.x() << ' ' << e->globalPos.y() << ' ' << e->modifiers << ' ' << e->toString();
}
QTextStream &operator<<(QTextStream &s, QNativeMouseButtonEvent *e)
{
return s << e->eventId << ' ' << e->globalPos.x() << ' ' << e->globalPos.y() << ' ' << e->button
<< ' ' << e->clickCount << ' ' << e->modifiers << ' ' << e->toString();
}
QTextStream &operator<<(QTextStream &s, QNativeMouseDragEvent *e)
{
return s << e->eventId << ' ' << e->globalPos.x() << ' ' << e->globalPos.y() << ' ' << e->button << ' ' << e->clickCount
<< ' ' << e->modifiers << ' ' << e->toString();
}
QTextStream &operator<<(QTextStream &s, QNativeMouseWheelEvent *e)
{
return s << e->eventId << ' ' << e->globalPos.x() << ' ' << e->globalPos.y() << ' ' << e->delta
<< ' ' << e->modifiers << ' ' << e->toString();
}
QTextStream &operator<<(QTextStream &s, QNativeKeyEvent *e)
{
return s << e->eventId << ' ' << e->press << ' ' << e->nativeKeyCode << ' ' << e->character
<< ' ' << e->modifiers << ' ' << e->toString();
}
QTextStream &operator<<(QTextStream &s, QNativeModifierEvent *e)
{
return s << e->eventId << ' ' << e->modifiers << ' ' << e->nativeKeyCode << ' ' << e->toString();
}
QTextStream &operator>>(QTextStream &s, QNativeMouseMoveEvent *e)
{
// Skip reading eventId.
QString humanReadable;
int x, y, modifiers;
s >> x >> y >> modifiers >> humanReadable;
e->globalPos.setX(x);
e->globalPos.setY(y);
e->modifiers = Qt::KeyboardModifiers(modifiers);
return s;
}
QTextStream &operator>>(QTextStream &s, QNativeMouseButtonEvent *e)
{
// Skip reading eventId.
QString humanReadable;
int x, y, button, clickCount, modifiers;
s >> x >> y >> button >> clickCount >> modifiers >> humanReadable;
e->globalPos.setX(x);
e->globalPos.setY(y);
e->clickCount = clickCount;
e->modifiers = Qt::KeyboardModifiers(modifiers);
switch (button){
case 1:
e->button = Qt::LeftButton;
break;
case 2:
e->button = Qt::RightButton;
break;
case 3:
e->button = Qt::MiddleButton;
break;
default:
e->button = Qt::NoButton;
break;
}
return s;
}
QTextStream &operator>>(QTextStream &s, QNativeMouseDragEvent *e)
{
// Skip reading eventId.
QString humanReadable;
int x, y, button, clickCount, modifiers;
s >> x >> y >> button >> clickCount >> modifiers >> humanReadable;
e->globalPos.setX(x);
e->globalPos.setY(y);
e->clickCount = clickCount;
e->modifiers = Qt::KeyboardModifiers(modifiers);
switch (button){
case 1:
e->button = Qt::LeftButton;
break;
case 2:
e->button = Qt::RightButton;
break;
case 3:
e->button = Qt::MiddleButton;
break;
default:
e->button = Qt::NoButton;
break;
}
return s;
}
QTextStream &operator>>(QTextStream &s, QNativeMouseWheelEvent *e)
{
// Skip reading eventId.
QString humanReadable;
int x, y, modifiers;
s >> x >> y >> e->delta >> modifiers >> humanReadable;
e->globalPos.setX(x);
e->globalPos.setY(y);
e->modifiers = Qt::KeyboardModifiers(modifiers);
return s;
}
QTextStream &operator>>(QTextStream &s, QNativeKeyEvent *e)
{
// Skip reading eventId.
QString humanReadable;
int press, modifiers;
QString character;
s >> press >> e->nativeKeyCode >> character >> modifiers >> humanReadable;
e->press = bool(press);
e->character = character[0];
e->modifiers = Qt::KeyboardModifiers(modifiers);
return s;
}
QTextStream &operator>>(QTextStream &s, QNativeModifierEvent *e)
{
// Skip reading eventId.
QString humanReadable;
int modifiers;
s >> modifiers >> e->nativeKeyCode >> humanReadable;
e->modifiers = Qt::KeyboardModifiers(modifiers);
return s;
}

View File

@ -0,0 +1,192 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef Q_NATIVE_INPUT
#define Q_NATIVE_INPUT
#include <QtCore>
QT_BEGIN_NAMESPACE
namespace Qt {
namespace Native {
enum Status {Success, Failure};
}}
QT_END_NAMESPACE
// ----------------------------------------------------------------------------
// Declare a set of native events that can be used to communicate with
// client applications in an platform independent way
// ----------------------------------------------------------------------------
class QNativeEvent
{
public:
static const int eventId = 1;
QNativeEvent(Qt::KeyboardModifiers modifiers = Qt::NoModifier);
virtual ~QNativeEvent() {}
virtual int id() const { return eventId; }
virtual QString toString() const = 0;
Qt::KeyboardModifiers modifiers; // Yields for mouse events too.
};
class QNativeMouseEvent : public QNativeEvent {
public:
static const int eventId = 2;
QNativeMouseEvent() {}
QNativeMouseEvent(QPoint globalPos, Qt::KeyboardModifiers modifiers = Qt::NoModifier);
virtual ~QNativeMouseEvent() {}
virtual int id() const { return eventId; }
QPoint globalPos;
};
class QNativeMouseMoveEvent : public QNativeMouseEvent {
public:
static const int eventId = 4;
QNativeMouseMoveEvent() {}
QNativeMouseMoveEvent(QPoint globalPos, Qt::KeyboardModifiers modifiers = Qt::NoModifier);
virtual ~QNativeMouseMoveEvent() {}
virtual int id() const { return eventId; }
virtual QString toString() const;
};
class QNativeMouseButtonEvent : public QNativeMouseEvent {
public:
static const int eventId = 8;
QNativeMouseButtonEvent() {}
QNativeMouseButtonEvent(QPoint globalPos, Qt::MouseButton button, int clickCount, Qt::KeyboardModifiers modifiers = Qt::NoModifier);
virtual ~QNativeMouseButtonEvent() {}
virtual int id() const { return eventId; }
virtual QString toString() const;
Qt::MouseButton button;
int clickCount;
};
class QNativeMouseDragEvent : public QNativeMouseButtonEvent {
public:
static const int eventId = 16;
QNativeMouseDragEvent() {}
QNativeMouseDragEvent(QPoint globalPos, Qt::MouseButton button, Qt::KeyboardModifiers modifiers = Qt::NoModifier);
virtual ~QNativeMouseDragEvent() {}
virtual int id() const { return eventId; }
virtual QString toString() const;
};
class QNativeMouseWheelEvent : public QNativeMouseEvent {
public:
static const int eventId = 32;
QNativeMouseWheelEvent() {}
QNativeMouseWheelEvent(QPoint globalPos, int delta, Qt::KeyboardModifiers modifiers = Qt::NoModifier);
virtual ~QNativeMouseWheelEvent() {}
virtual int id() const { return eventId; }
virtual QString toString() const;
int delta;
};
class QNativeKeyEvent : public QNativeEvent {
public:
static const int eventId = 64;
QNativeKeyEvent() {}
QNativeKeyEvent(int nativeKeyCode, bool press, Qt::KeyboardModifiers modifiers = Qt::NoModifier);
QNativeKeyEvent(int nativeKeyCode, bool press, QChar character, Qt::KeyboardModifiers modifiers);
virtual ~QNativeKeyEvent() {}
virtual int id() const { return eventId; }
virtual QString toString() const;
int nativeKeyCode;
bool press;
QChar character;
// Some Qt to Native mappings:
static int Key_A;
static int Key_B;
static int Key_C;
static int Key_1;
static int Key_Backspace;
static int Key_Enter;
static int Key_Del;
};
class QNativeModifierEvent : public QNativeEvent {
public:
static const int eventId = 128;
QNativeModifierEvent(Qt::KeyboardModifiers modifiers = Qt::NoModifier, int nativeKeyCode = 0);
virtual ~QNativeModifierEvent() {}
virtual int id() const { return eventId; }
virtual QString toString() const;
int nativeKeyCode;
};
// ----------------------------------------------------------------------------
// Declare a set of related output / input functions for convenience:
// ----------------------------------------------------------------------------
QDebug operator<<(QDebug d, QNativeEvent *e);
QDebug operator<<(QDebug d, const QNativeEvent &e);
QTextStream &operator<<(QTextStream &s, QNativeEvent *e);
QTextStream &operator<<(QTextStream &s, QNativeMouseEvent *e);
QTextStream &operator<<(QTextStream &s, QNativeMouseMoveEvent *e);
QTextStream &operator<<(QTextStream &s, QNativeMouseButtonEvent *e);
QTextStream &operator<<(QTextStream &s, QNativeMouseDragEvent *e);
QTextStream &operator<<(QTextStream &s, QNativeMouseWheelEvent *e);
QTextStream &operator<<(QTextStream &s, QNativeKeyEvent *e);
QTextStream &operator<<(QTextStream &s, QNativeModifierEvent *e);
QTextStream &operator>>(QTextStream &s, QNativeMouseMoveEvent *e);
QTextStream &operator>>(QTextStream &s, QNativeMouseButtonEvent *e);
QTextStream &operator>>(QTextStream &s, QNativeMouseDragEvent *e);
QTextStream &operator>>(QTextStream &s, QNativeMouseWheelEvent *e);
QTextStream &operator>>(QTextStream &s, QNativeKeyEvent *e);
QTextStream &operator>>(QTextStream &s, QNativeModifierEvent *e);
// ----------------------------------------------------------------------------
// Declare the main class that is supposed to be sub-classed by components
// that are to receive native events
// ----------------------------------------------------------------------------
class QNativeInput
{
public:
QNativeInput(bool subscribe = true);
virtual ~QNativeInput();
// Callback methods. Should be implemented by interested sub-classes:
void notify(QNativeEvent *event);
virtual void nativeEvent(QNativeEvent *event);
virtual void nativeMousePressEvent(QNativeMouseButtonEvent *) {}
virtual void nativeMouseReleaseEvent(QNativeMouseButtonEvent *) {}
virtual void nativeMouseMoveEvent(QNativeMouseMoveEvent *) {}
virtual void nativeMouseDragEvent(QNativeMouseDragEvent *) {}
virtual void nativeMouseWheelEvent(QNativeMouseWheelEvent *) {}
virtual void nativeKeyPressEvent(QNativeKeyEvent *) {}
virtual void nativeKeyReleaseEvent(QNativeKeyEvent *) {}
virtual void nativeModifierEvent(QNativeModifierEvent *) {}
// The following methods will differ in implementation from OS to OS:
static Qt::Native::Status sendNativeMouseButtonEvent(const QNativeMouseButtonEvent &event);
static Qt::Native::Status sendNativeMouseMoveEvent(const QNativeMouseMoveEvent &event);
static Qt::Native::Status sendNativeMouseDragEvent(const QNativeMouseDragEvent &event);
static Qt::Native::Status sendNativeMouseWheelEvent(const QNativeMouseWheelEvent &event);
static Qt::Native::Status sendNativeKeyEvent(const QNativeKeyEvent &event);
static Qt::Native::Status sendNativeModifierEvent(const QNativeModifierEvent &event);
// sendNativeEvent will NOT differ from OS to OS.
static Qt::Native::Status sendNativeEvent(const QNativeEvent &event);
// The following methods will differ in implementation from OS to OS:
Qt::Native::Status subscribeForNativeEvents();
Qt::Native::Status unsubscribeForNativeEvents();
};
#endif // Q_NATIVE_INPUT

View File

@ -0,0 +1,327 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "qnativeevents.h"
#include <CoreGraphics/CoreGraphics.h>
#include <QtCore>
// ************************************************************
// Quartz
// ************************************************************
static Qt::KeyboardModifiers getModifiersFromQuartzEvent(CGEventRef inEvent)
{
Qt::KeyboardModifiers m;
CGEventFlags flags = CGEventGetFlags(inEvent);
if (flags & kCGEventFlagMaskShift || flags & kCGEventFlagMaskAlphaShift)
m |= Qt::ShiftModifier;
if (flags & kCGEventFlagMaskControl)
m |= Qt::ControlModifier;
if (flags & kCGEventFlagMaskAlternate)
m |= Qt::AltModifier;
if (flags & kCGEventFlagMaskCommand)
m |= Qt::MetaModifier;
return m;
}
static void setModifiersFromQNativeEvent(CGEventRef inEvent, const QNativeEvent &event)
{
CGEventFlags flags = CGEventFlags(0);
if (event.modifiers.testFlag(Qt::ShiftModifier))
flags = CGEventFlags(flags | kCGEventFlagMaskShift);
if (event.modifiers.testFlag(Qt::ControlModifier))
flags = CGEventFlags(flags | kCGEventFlagMaskControl);
if (event.modifiers.testFlag(Qt::AltModifier))
flags = CGEventFlags(flags | kCGEventFlagMaskAlternate);
if (event.modifiers.testFlag(Qt::MetaModifier))
flags = CGEventFlags(flags | kCGEventFlagMaskCommand);
CGEventSetFlags(inEvent, flags);
}
static QPoint getMouseLocationFromQuartzEvent(CGEventRef inEvent)
{
CGPoint pos = CGEventGetLocation(inEvent);
QPoint tmp;
tmp.setX(pos.x);
tmp.setY(pos.y);
return tmp;
}
static QChar getCharFromQuartzEvent(CGEventRef inEvent)
{
UniCharCount count = 0;
UniChar c;
CGEventKeyboardGetUnicodeString(inEvent, 1, &count, &c);
return QChar(c);
}
static CGEventRef EventHandler_Quartz(CGEventTapProxy proxy, CGEventType type, CGEventRef inEvent, void *refCon)
{
Q_UNUSED(proxy);
QNativeInput *nativeInput = static_cast<QNativeInput *>(refCon);
switch (type){
case kCGEventKeyDown:{
QNativeKeyEvent e;
e.modifiers = getModifiersFromQuartzEvent(inEvent);
e.nativeKeyCode = CGEventGetIntegerValueField(inEvent, kCGKeyboardEventKeycode);
e.character = getCharFromQuartzEvent(inEvent);
e.press = true;
nativeInput->notify(&e);
break;
}
case kCGEventKeyUp:{
QNativeKeyEvent e;
e.modifiers = getModifiersFromQuartzEvent(inEvent);
e.nativeKeyCode = CGEventGetIntegerValueField(inEvent, kCGKeyboardEventKeycode);
e.character = getCharFromQuartzEvent(inEvent);
e.press = false;
nativeInput->notify(&e);
break;
}
case kCGEventLeftMouseDown:{
QNativeMouseButtonEvent e;
e.modifiers = getModifiersFromQuartzEvent(inEvent);
e.globalPos = getMouseLocationFromQuartzEvent(inEvent);
e.clickCount = CGEventGetIntegerValueField(inEvent, kCGMouseEventClickState);
e.button = Qt::LeftButton;
nativeInput->notify(&e);
break;
}
case kCGEventLeftMouseUp:{
QNativeMouseButtonEvent e;
e.modifiers = getModifiersFromQuartzEvent(inEvent);
e.globalPos = getMouseLocationFromQuartzEvent(inEvent);
e.clickCount = 0;
e.button = Qt::LeftButton;
nativeInput->notify(&e);
break;
}
case kCGEventRightMouseDown:{
QNativeMouseButtonEvent e;
e.modifiers = getModifiersFromQuartzEvent(inEvent);
e.globalPos = getMouseLocationFromQuartzEvent(inEvent);
e.clickCount = CGEventGetIntegerValueField(inEvent, kCGMouseEventClickState);
e.button = Qt::RightButton;
nativeInput->notify(&e);
break;
}
case kCGEventRightMouseUp:{
QNativeMouseButtonEvent e;
e.modifiers = getModifiersFromQuartzEvent(inEvent);
e.globalPos = getMouseLocationFromQuartzEvent(inEvent);
e.clickCount = 0;
e.button = Qt::RightButton;
nativeInput->notify(&e);
break;
}
case kCGEventMouseMoved:{
QNativeMouseMoveEvent e;
e.modifiers = getModifiersFromQuartzEvent(inEvent);
e.globalPos = getMouseLocationFromQuartzEvent(inEvent);
nativeInput->notify(&e);
break;
}
case kCGEventLeftMouseDragged:{
QNativeMouseDragEvent e;
e.modifiers = getModifiersFromQuartzEvent(inEvent);
e.globalPos = getMouseLocationFromQuartzEvent(inEvent);
e.clickCount = CGEventGetIntegerValueField(inEvent, kCGMouseEventClickState);
e.button = Qt::LeftButton;
nativeInput->notify(&e);
break;
}
case kCGEventScrollWheel:{
QNativeMouseWheelEvent e;
e.modifiers = getModifiersFromQuartzEvent(inEvent);
e.delta = CGEventGetIntegerValueField(inEvent, kCGScrollWheelEventDeltaAxis1);
e.globalPos = getMouseLocationFromQuartzEvent(inEvent);
nativeInput->notify(&e);
break;
}
case kCGEventFlagsChanged:{
QNativeModifierEvent e;
e.modifiers = getModifiersFromQuartzEvent(inEvent);
e.nativeKeyCode = CGEventGetIntegerValueField(inEvent, kCGKeyboardEventKeycode);
nativeInput->notify(&e);
break;
}
}
return inEvent;
}
Qt::Native::Status insertEventHandler_Quartz(QNativeInput *nativeInput)
{
uid_t uid = geteuid();
if (uid != 0)
qWarning("MacNativeEvents: You must be root to listen for key events!");
CFMachPortRef port = CGEventTapCreate(kCGHIDEventTap,
kCGHeadInsertEventTap, kCGEventTapOptionListenOnly,
kCGEventMaskForAllEvents, EventHandler_Quartz, nativeInput);
CFRunLoopSourceRef eventSrc = CFMachPortCreateRunLoopSource(NULL, port, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), eventSrc, kCFRunLoopCommonModes);
return Qt::Native::Success;
}
Qt::Native::Status removeEventHandler_Quartz()
{
return Qt::Native::Success; // ToDo:
}
Qt::Native::Status sendNativeKeyEvent_Quartz(const QNativeKeyEvent &event)
{
CGEventRef e = CGEventCreateKeyboardEvent(0, (uint)event.nativeKeyCode, event.press);
setModifiersFromQNativeEvent(e, event);
CGEventPost(kCGHIDEventTap, e);
CFRelease(e);
return Qt::Native::Success;
}
Qt::Native::Status sendNativeMouseMoveEvent_Quartz(const QNativeMouseMoveEvent &event)
{
CGPoint pos;
pos.x = event.globalPos.x();
pos.y = event.globalPos.y();
CGEventRef e = CGEventCreateMouseEvent(0, kCGEventMouseMoved, pos, kCGMouseButtonLeft /* ignored */);
setModifiersFromQNativeEvent(e, event);
CGEventPost(kCGHIDEventTap, e);
CFRelease(e);
return Qt::Native::Success;
}
Qt::Native::Status sendNativeMouseButtonEvent_Quartz(const QNativeMouseButtonEvent &event)
{
CGPoint pos;
pos.x = event.globalPos.x();
pos.y = event.globalPos.y();
CGEventType type = kCGEventNull;
if (event.button == Qt::LeftButton)
type = (event.clickCount > 0) ? kCGEventLeftMouseDown : kCGEventLeftMouseUp;
else if (event.button == Qt::RightButton)
type = (event.clickCount > 0) ? kCGEventRightMouseDown : kCGEventRightMouseUp;
else
type = (event.clickCount > 0) ? kCGEventOtherMouseDown : kCGEventOtherMouseUp;
// The mouseButton argument to CGEventCreateMouseEvent() is ignored unless the type
// is kCGEventOtherSomething, so defaulting to kCGMouseButtonLeft is fine.
CGMouseButton mouseButton = (type == kCGEventOtherMouseDown || type == kCGEventOtherMouseUp) ?
kCGMouseButtonCenter : kCGMouseButtonLeft;
CGEventRef e = CGEventCreateMouseEvent(0, type, pos, mouseButton);
setModifiersFromQNativeEvent(e, event);
CGEventSetIntegerValueField(e, kCGMouseEventClickState, event.clickCount);
CGEventPost(kCGHIDEventTap, e);
CFRelease(e);
return Qt::Native::Success;
}
Qt::Native::Status sendNativeMouseDragEvent_Quartz(const QNativeMouseDragEvent &event)
{
CGPoint pos;
pos.x = event.globalPos.x();
pos.y = event.globalPos.y();
CGEventType type = kCGEventNull;
if (event.button == Qt::LeftButton)
type = kCGEventLeftMouseDragged;
else if (event.button == Qt::RightButton)
type = kCGEventRightMouseDragged;
else
type = kCGEventOtherMouseDragged;
// The mouseButton argument to CGEventCreateMouseEvent() is ignored unless the type
// is kCGEventOtherSomething, so defaulting to kCGMouseButtonLeft is fine.
CGMouseButton mouseButton = type == kCGEventOtherMouseDragged ? kCGMouseButtonCenter : kCGMouseButtonLeft;
CGEventRef e = CGEventCreateMouseEvent(0, type, pos, mouseButton);
setModifiersFromQNativeEvent(e, event);
CGEventPost(kCGHIDEventTap, e);
CFRelease(e);
return Qt::Native::Success;
}
Qt::Native::Status sendNativeMouseWheelEvent_Quartz(const QNativeMouseWheelEvent &event)
{
CGPoint pos;
pos.x = event.globalPos.x();
pos.y = event.globalPos.y();
CGEventRef e = CGEventCreateScrollWheelEvent(0, kCGScrollEventUnitPixel, 1, 0);
CGEventSetIntegerValueField(e, kCGScrollWheelEventDeltaAxis1, event.delta);
CGEventSetLocation(e, pos);
setModifiersFromQNativeEvent(e, event);
CGEventPost(kCGHIDEventTap, e);
CFRelease(e);
return Qt::Native::Success;
}
Qt::Native::Status sendNativeModifierEvent_Quartz(const QNativeModifierEvent &event)
{
CGEventRef e = CGEventCreateKeyboardEvent(0, (uint)event.nativeKeyCode, 0);
CGEventSetType(e, kCGEventFlagsChanged);
setModifiersFromQNativeEvent(e, event);
CGEventPost(kCGHIDEventTap, e);
CFRelease(e);
return Qt::Native::Success;
}
// ************************************************************
// QNativeInput methods:
// ************************************************************
Qt::Native::Status QNativeInput::sendNativeMouseButtonEvent(const QNativeMouseButtonEvent &event)
{
return sendNativeMouseButtonEvent_Quartz(event);
}
Qt::Native::Status QNativeInput::sendNativeMouseMoveEvent(const QNativeMouseMoveEvent &event)
{
return sendNativeMouseMoveEvent_Quartz(event);
}
Qt::Native::Status QNativeInput::sendNativeMouseDragEvent(const QNativeMouseDragEvent &event)
{
return sendNativeMouseDragEvent_Quartz(event);
}
Qt::Native::Status QNativeInput::sendNativeMouseWheelEvent(const QNativeMouseWheelEvent &event)
{
return sendNativeMouseWheelEvent_Quartz(event);
}
Qt::Native::Status QNativeInput::sendNativeKeyEvent(const QNativeKeyEvent &event)
{
return sendNativeKeyEvent_Quartz(event);
}
Qt::Native::Status QNativeInput::sendNativeModifierEvent(const QNativeModifierEvent &event)
{
return sendNativeModifierEvent_Quartz(event);
}
Qt::Native::Status QNativeInput::subscribeForNativeEvents()
{
return insertEventHandler_Quartz(this);
}
Qt::Native::Status QNativeInput::unsubscribeForNativeEvents()
{
return removeEventHandler_Quartz();
}
// Some Qt to Mac mappings:
int QNativeKeyEvent::Key_A = 0;
int QNativeKeyEvent::Key_B = 11;
int QNativeKeyEvent::Key_C = 8;
int QNativeKeyEvent::Key_1 = 18;
int QNativeKeyEvent::Key_Backspace = 51;
int QNativeKeyEvent::Key_Enter = 36;
int QNativeKeyEvent::Key_Del = 117;

View File

@ -0,0 +1,490 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QApplication>
#include <QWidget>
#include <QDialog>
#include <QPushButton>
#include <QTest>
#include "qnativeevents.h"
#include "nativeeventlist.h"
#include "expectedeventlist.h"
QT_USE_NAMESPACE
// Unicode code points for the glyphs associated with these keys
// Defined by Carbon headers but not anywhere in Cocoa
static const int kControlUnicode = 0x2303;
static const int kCommandUnicode = 0x2318;
class tst_MacNativeEvents : public QObject
{
Q_OBJECT
private slots:
void testMouseMoveLocation();
void testPushButtonPressRelease();
void testMouseLeftDoubleClick();
void stressTestMouseLeftDoubleClick();
void testMouseDragInside();
void testMouseDragOutside();
void testMouseDragToNonClientArea();
void testDragWindow();
void testMouseEnter();
void testChildDialogInFrontOfModalParent();
// void testChildWindowInFrontOfParentWindow();
// void testChildToolWindowInFrontOfChildNormalWindow();
void testChildWindowInFrontOfStaysOnTopParentWindow();
void testKeyPressOnToplevel();
void testModifierShift();
void testModifierAlt();
void testModifierCtrl();
void testModifierCtrlWithDontSwapCtrlAndMeta();
};
void tst_MacNativeEvents::testMouseMoveLocation()
{
QWidget w;
w.setMouseTracking(true);
w.show();
QPoint p = w.geometry().center();
NativeEventList native;
native.append(new QNativeMouseMoveEvent(p, Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(p), p, Qt::NoButton, Qt::NoButton, Qt::NoModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::testPushButtonPressRelease()
{
// Check that a native mouse press and release generates the
// same qevents on a pushbutton:
QPushButton w("click me");
w.show();
QPoint p = w.geometry().center();
NativeEventList native;
native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::testMouseLeftDoubleClick()
{
// Check that a native double click makes
// the test widget receive a press-release-click-release:
QWidget w;
w.show();
QPoint p = w.geometry().center();
NativeEventList native;
native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 2, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseButtonDblClick, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::stressTestMouseLeftDoubleClick()
{
// Check that multiple, fast, double clicks makes
// the test widget receive correct click events
QWidget w;
w.show();
QPoint p = w.geometry().center();
NativeEventList native;
ExpectedEventList expected(&w);
for (int i=0; i<10; ++i){
native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 2, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseButtonDblClick, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
}
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::testMouseDragInside()
{
// Check that a mouse drag inside a widget
// will cause press-move-release events to be delivered
QWidget w;
w.show();
QPoint p1 = w.geometry().center();
QPoint p2 = p1 - QPoint(10, 0);
QPoint p3 = p1 - QPoint(20, 0);
QPoint p4 = p1 - QPoint(30, 0);
NativeEventList native;
native.append(new QNativeMouseButtonEvent(p1, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseDragEvent(p2, Qt::LeftButton, Qt::NoModifier));
native.append(new QNativeMouseDragEvent(p3, Qt::LeftButton, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(p4, Qt::LeftButton, 0, Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(p1), p1, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(p2), p2, Qt::NoButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(p3), p3, Qt::NoButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p4), p4, Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::testMouseDragOutside()
{
// Check that if we drag the mouse from inside the
// widget, and release it outside, we still get mouse move
// and release events when the mouse is outside the widget.
QWidget w;
w.show();
QPoint inside1 = w.geometry().center();
QPoint inside2 = inside1 - QPoint(10, 0);
QPoint outside1 = w.geometry().topLeft() - QPoint(50, 0);
QPoint outside2 = outside1 - QPoint(10, 0);
NativeEventList native;
native.append(new QNativeMouseButtonEvent(inside1, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseDragEvent(inside2, Qt::LeftButton, Qt::NoModifier));
native.append(new QNativeMouseDragEvent(outside1, Qt::LeftButton, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(outside2, Qt::LeftButton, 0, Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(inside1), inside1, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(inside2), inside2, Qt::NoButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(outside1), outside1, Qt::NoButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(outside2), outside2, Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::testMouseDragToNonClientArea()
{
// Check that if we drag the mouse from inside the
// widget, and release it on the title bar, we still get mouse move
// and release events when the mouse is on the title bar
QWidget w;
w.show();
QPoint inside1 = w.geometry().center();
QPoint inside2 = inside1 - QPoint(10, 0);
QPoint titlebar1 = w.geometry().topLeft() - QPoint(-100, 10);
QPoint titlebar2 = titlebar1 - QPoint(10, 0);
NativeEventList native;
native.append(new QNativeMouseButtonEvent(inside1, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseDragEvent(inside2, Qt::LeftButton, Qt::NoModifier));
native.append(new QNativeMouseDragEvent(titlebar1, Qt::LeftButton, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(titlebar2, Qt::LeftButton, 0, Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(inside1), inside1, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(inside2), inside2, Qt::NoButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(titlebar1), titlebar1, Qt::NoButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(titlebar2), titlebar2, Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::testDragWindow()
{
// Check that if we drag the mouse from inside the
// widgets title bar, we get a move event on the window
QWidget w;
w.show();
QPoint titlebar = w.geometry().topLeft() - QPoint(-100, 10);
QPoint moveTo = titlebar + QPoint(100, 0);
NativeEventList native;
native.append(new QNativeMouseButtonEvent(titlebar, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseDragEvent(moveTo, Qt::LeftButton, Qt::NoModifier));
native.append(500, new QNativeMouseButtonEvent(moveTo, Qt::LeftButton, 0, Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QMouseEvent(QEvent::NonClientAreaMouseButtonPress, w.mapFromGlobal(titlebar), titlebar, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));
expected.append(new QMouseEvent(QEvent::NonClientAreaMouseButtonRelease, w.mapFromGlobal(titlebar), moveTo, Qt::LeftButton, Qt::NoButton, Qt::NoModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::testMouseEnter()
{
// When a mouse enters a widget, both a mouse enter events and a
// mouse move event should be sent. Let's test this:
QWidget w;
w.setMouseTracking(true);
w.show();
QPoint outside = w.geometry().topLeft() - QPoint(50, 0);
QPoint inside = w.geometry().center();
NativeEventList native;
native.append(new QNativeMouseMoveEvent(outside, Qt::NoModifier));
native.append(new QNativeMouseMoveEvent(inside, Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QEvent(QEvent::Enter));
expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(inside), inside, Qt::NoButton, Qt::NoButton, Qt::NoModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::testChildDialogInFrontOfModalParent()
{
QSKIP("Modal dialog causes later tests to fail, see QTBUG-58474");
// Test that a child dialog of a modal parent dialog is
// in front of the parent, and active:
QDialog parent;
parent.setWindowModality(Qt::ApplicationModal);
QDialog child(&parent);
QPushButton button("close", &child);
connect(&button, SIGNAL(clicked()), &child, SLOT(close()));
parent.show();
child.show();
QPoint inside = button.mapToGlobal(button.geometry().center());
// Post a click on the button to close the child dialog:
NativeEventList native;
native.append(new QNativeMouseButtonEvent(inside, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(inside, Qt::LeftButton, 0, Qt::NoModifier));
native.play();
QTest::qWait(100);
QVERIFY(!child.isVisible());
}
#if 0
// This test is disabled as of Qt-4.7.4 because we cannot do it
// unless we use the Cocoa sub window API. But using that opens up
// a world of side effects that we cannot live with. So we rather
// not support child-on-top-of-parent instead.
void tst_MacNativeEvents::testChildWindowInFrontOfParentWindow()
{
// Test that a child window always stacks in front of its parent window.
// Do this by first click on the parent, then on the child window button.
QWidget parent;
QPushButton child("a button", &parent);
child.setWindowFlags(Qt::Window);
connect(&child, SIGNAL(clicked()), &child, SLOT(close()));
parent.show();
child.show();
QPoint parent_p = parent.geometry().bottomLeft() + QPoint(20, -20);
QPoint child_p = child.geometry().center();
NativeEventList native;
native.append(new QNativeMouseButtonEvent(parent_p, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(parent_p, Qt::LeftButton, 0, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(child_p, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(child_p, Qt::LeftButton, 0, Qt::NoModifier));
native.play();
QTest::qWait(100);
QVERIFY(!child.isVisible());
}
#endif
/* This test can be enabled once setStackingOrder has been fixed in qwidget_mac.mm
void tst_MacNativeEvents::testChildToolWindowInFrontOfChildNormalWindow()
{
// Test that a child tool window always stacks in front of normal sibling windows.
// Do this by first click on the sibling, then on the tool window button.
QWidget parent;
QWidget normalChild(&parent, Qt::Window);
QPushButton toolChild("a button", &parent);
toolChild.setWindowFlags(Qt::Tool);
connect(&toolChild, SIGNAL(clicked()), &toolChild, SLOT(close()));
parent.show();
normalChild.show();
toolChild.show();
QPoint normalChild_p = normalChild.geometry().bottomLeft() + QPoint(20, -20);
QPoint toolChild_p = toolChild.geometry().center();
NativeEventList native;
native.append(new QNativeMouseButtonEvent(normalChild_p, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(normalChild_p, Qt::LeftButton, 0, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(toolChild_p, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(toolChild_p, Qt::LeftButton, 0, Qt::NoModifier));
native.play();
QTest::qWait(100);
QVERIFY(!toolChild.isVisible());
}
*/
void tst_MacNativeEvents::testChildWindowInFrontOfStaysOnTopParentWindow()
{
// Test that a child window stacks on top of a stays-on-top parent.
QWidget parent(0, Qt::WindowStaysOnTopHint);
QPushButton button("close", &parent);
button.setWindowFlags(Qt::Window);
connect(&button, SIGNAL(clicked()), &button, SLOT(close()));
parent.show();
button.show();
QPoint inside = button.geometry().center();
// Post a click on the button to close the child dialog:
NativeEventList native;
native.append(new QNativeMouseButtonEvent(inside, Qt::LeftButton, 1, Qt::NoModifier));
native.append(new QNativeMouseButtonEvent(inside, Qt::LeftButton, 0, Qt::NoModifier));
native.play();
QTest::qWait(100);
QVERIFY(!button.isVisible());
}
void tst_MacNativeEvents::testKeyPressOnToplevel()
{
// Check that we receive keyevents for
// toplevel widgets. For leagacy reasons, and according to Qt on
// other platforms (carbon port + linux), we should get these events
// even when the focus policy is set to Qt::NoFocus when there is no
// other focus widget on screen:
QWidget w;
w.show();
NativeEventList native;
native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, true, Qt::NoModifier));
native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, false, Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier));
expected.append(new QKeyEvent(QEvent::KeyRelease, Qt::Key_A, Qt::NoModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::testModifierShift()
{
QWidget w;
w.show();
NativeEventList native;
native.append(new QNativeModifierEvent(Qt::ShiftModifier));
native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, true, Qt::ShiftModifier));
native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, false, Qt::ShiftModifier));
native.append(new QNativeModifierEvent(Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QKeyEvent(QEvent::KeyPress, Qt::Key_Shift, Qt::NoModifier));
expected.append(new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::ShiftModifier));
expected.append(new QKeyEvent(QEvent::KeyRelease, Qt::Key_A, Qt::ShiftModifier));
expected.append(new QKeyEvent(QEvent::KeyRelease, Qt::Key_Shift, Qt::ShiftModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::testModifierAlt()
{
QWidget w;
w.show();
NativeEventList native;
native.append(new QNativeModifierEvent(Qt::AltModifier));
native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, true, Qt::AltModifier));
native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, false, Qt::AltModifier));
native.append(new QNativeModifierEvent(Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QKeyEvent(QEvent::KeyPress, Qt::Key_Alt, Qt::NoModifier));
expected.append(new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::AltModifier));
expected.append(new QKeyEvent(QEvent::KeyRelease, Qt::Key_A, Qt::AltModifier));
expected.append(new QKeyEvent(QEvent::KeyRelease, Qt::Key_Alt, Qt::AltModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::testModifierCtrl()
{
// On Mac, we switch the Command and Control modifier by default, so that Command
// means Meta, and Control means Command. Lets check that this works:
QWidget w;
w.show();
QCOMPARE(ushort(kControlUnicode), QKeySequence(Qt::Key_Meta).toString(QKeySequence::NativeText).at(0).unicode());
QCOMPARE(ushort(kCommandUnicode), QKeySequence(Qt::Key_Control).toString(QKeySequence::NativeText).at(0).unicode());
NativeEventList native;
native.append(new QNativeModifierEvent(Qt::ControlModifier));
native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, true, Qt::ControlModifier));
native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, false, Qt::ControlModifier));
native.append(new QNativeModifierEvent(Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QKeyEvent(QEvent::KeyPress, Qt::Key_Meta, Qt::NoModifier));
expected.append(new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::MetaModifier));
expected.append(new QKeyEvent(QEvent::KeyRelease, Qt::Key_A, Qt::MetaModifier));
expected.append(new QKeyEvent(QEvent::KeyRelease, Qt::Key_Meta, Qt::MetaModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
}
void tst_MacNativeEvents::testModifierCtrlWithDontSwapCtrlAndMeta()
{
// On Mac, we switch the Command and Control modifier by default, so that Command
// means Meta, and Control means Command. Lets check that the flag to swith off
// this behaviour works. While working on this test I realized that we actually
// don't (and never have) respected this flag for raw key events. Only for
// menus, through QKeySequence. I don't want to change this behaviour now, at
// least not until someone complains. So I choose to let the test just stop
// any unintended regressions instead. If we decide to resepect the flag at one
// point, fix the test.
QCoreApplication::setAttribute(Qt::AA_MacDontSwapCtrlAndMeta);
QWidget w;
w.show();
QCOMPARE(ushort(kCommandUnicode), QKeySequence(Qt::Key_Meta).toString(QKeySequence::NativeText).at(0).unicode());
QCOMPARE(ushort(kControlUnicode), QKeySequence(Qt::Key_Control).toString(QKeySequence::NativeText).at(0).unicode());
NativeEventList native;
native.append(new QNativeModifierEvent(Qt::ControlModifier));
native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, true, Qt::ControlModifier));
native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, false, Qt::ControlModifier));
native.append(new QNativeModifierEvent(Qt::NoModifier));
ExpectedEventList expected(&w);
expected.append(new QKeyEvent(QEvent::KeyPress, Qt::Key_Control, Qt::NoModifier));
expected.append(new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::ControlModifier));
expected.append(new QKeyEvent(QEvent::KeyRelease, Qt::Key_A, Qt::ControlModifier));
expected.append(new QKeyEvent(QEvent::KeyRelease, Qt::Key_Control, Qt::ControlModifier));
native.play();
QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!");
QCoreApplication::setAttribute(Qt::AA_MacDontSwapCtrlAndMeta, false);
}
QTEST_MAIN(tst_MacNativeEvents)
#include "tst_macnativeevents.moc"

View File

@ -0,0 +1,8 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
if(NOT TARGET Qt::Widgets)
return()
endif()
add_subdirectory(app)
add_subdirectory(test)

View File

@ -0,0 +1,16 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## app Binary:
#####################################################################
qt_internal_add_executable(app
GUI
OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
SOURCES
main.cpp
LIBRARIES
Qt::Gui
Qt::Widgets
)

View File

@ -0,0 +1,12 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QtWidgets/QApplication>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
return 0;
}

View File

@ -0,0 +1,17 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_macplist Test:
#####################################################################
qt_internal_add_test(tst_macplist
OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../"
SOURCES
../tst_macplist.cpp
LIBRARIES
Qt::Gui
)
## Scopes:
#####################################################################

View File

@ -0,0 +1,149 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTest>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QProcess>
class tst_MacPlist : public QObject
{
Q_OBJECT
public:
tst_MacPlist() {}
private slots:
#ifdef Q_OS_MAC
void test_plist_data();
void test_plist();
#endif
};
#ifdef Q_OS_MAC
void tst_MacPlist::test_plist_data()
{
QTest::addColumn<QString>("test_plist");
QTest::newRow("control") << QString::fromLatin1(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
"<plist version=\"1.0\">\n"
"<dict>\n"
" <key>CFBundleIconFile</key>\n"
" <string></string>\n"
" <key>CFBundlePackageType</key>\n"
" <string>APPL</string>\n"
" <key>CFBundleExecutable</key>\n"
" <string>app</string>\n"
" <key>CFBundleIdentifier</key>\n"
" <string>com.yourcompany.app</string>\n"
"</dict>\n"
"</plist>\n");
QTest::newRow("LSUIElement-as-string") << QString::fromLatin1(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
"<plist version=\"1.0\">\n"
"<dict>\n"
" <key>CFBundleIconFile</key>\n"
" <string></string>\n"
" <key>CFBundlePackageType</key>\n"
" <string>APPL</string>\n"
" <key>CFBundleExecutable</key>\n"
" <string>app</string>\n"
" <key>CFBundleIdentifier</key>\n"
" <string>com.yourcompany.app</string>\n"
" <key>LSUIElement</key>\n"
" <string>false</string>\n"
"</dict>\n"
"</plist>\n");
QTest::newRow("LSUIElement-as-bool") << QString::fromLatin1(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
"<plist version=\"1.0\">\n"
"<dict>\n"
" <key>CFBundleIconFile</key>\n"
" <string></string>\n"
" <key>CFBundlePackageType</key>\n"
" <string>APPL</string>\n"
" <key>CFBundleExecutable</key>\n"
" <string>app</string>\n"
" <key>CFBundleIdentifier</key>\n"
" <string>com.yourcompany.app</string>\n"
" <key>LSUIElement</key>\n"
" <false/>\n"
"</dict>\n"
"</plist>\n");
QTest::newRow("LSUIElement-as-int") << QString::fromLatin1(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
"<plist version=\"1.0\">\n"
"<dict>\n"
" <key>CFBundleIconFile</key>\n"
" <string></string>\n"
" <key>CFBundlePackageType</key>\n"
" <string>APPL</string>\n"
" <key>CFBundleExecutable</key>\n"
" <string>app</string>\n"
" <key>CFBundleIdentifier</key>\n"
" <string>com.yourcompany.app</string>\n"
" <key>LSUIElement</key>\n"
" <real>0</real>\n"
"</dict>\n"
"</plist>\n");
QTest::newRow("LSUIElement-as-garbage") << QString::fromLatin1(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
"<plist version=\"1.0\">\n"
"<dict>\n"
" <key>CFBundleIconFile</key>\n"
" <string></string>\n"
" <key>CFBundlePackageType</key>\n"
" <string>APPL</string>\n"
" <key>CFBundleExecutable</key>\n"
" <string>app</string>\n"
" <key>CFBundleIdentifier</key>\n"
" <string>com.yourcompany.app</string>\n"
" <key>LSUIElement</key>\n"
" <badkey>0</badkey>\n"
"</dict>\n"
"</plist>\n");
}
void tst_MacPlist::test_plist()
{
QFETCH(QString, test_plist);
QString infoPlist = QLatin1String("Info.plist");
QDir dir(QCoreApplication::applicationDirPath());
#ifndef Q_OS_MACOS
// macOS builds tests as single executables, iOS/tvOS/watchOS does not
QVERIFY(dir.cdUp());
QVERIFY(dir.cdUp());
QVERIFY(dir.cdUp());
#endif
QVERIFY(dir.cd(QLatin1String("app")));
QVERIFY(dir.cd(QLatin1String("app.app")));
QVERIFY(dir.cd(QLatin1String("Contents")));
QVERIFY(dir.exists(infoPlist));
{
QFile file(dir.filePath(infoPlist));
QVERIFY(file.open(QIODevice::WriteOnly));
QByteArray ba = test_plist.toUtf8();
QCOMPARE(file.write(ba), qint64(ba.size()));
}
QVERIFY(dir.cd(QLatin1String("MacOS")));
QVERIFY(dir.exists(QLatin1String("app")));
QProcess process;
process.start(dir.filePath("app"));
QCOMPARE(process.waitForFinished(), true);
QCOMPARE(process.exitStatus(), QProcess::NormalExit);
}
#endif
QTEST_MAIN(tst_MacPlist)
#include "tst_macplist.moc"

View File

@ -0,0 +1,6 @@
# QTBUG-27571
[ftpProxyServer]
windows-7sp1
windows-10
[smbServer]
opensuse-leap

View File

@ -0,0 +1,15 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_networkselftest Test:
#####################################################################
qt_internal_add_test(tst_networkselftest
SOURCES
tst_networkselftest.cpp
LIBRARIES
Qt::CorePrivate
Qt::Network
QT_TEST_SERVER_LIST "danted" "squid" "apache2" "ftp-proxy" "vsftpd" "cyrus" "echo"
)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,343 @@
// Copyright (C) 2009 Stephen Kelly <steveire@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "dynamictreemodel.h"
#include <QtCore/QHash>
#include <QtCore/QList>
#include <QtCore/QTimer>
#include <QtCore/QDebug>
DynamicTreeModel::DynamicTreeModel(QObject *parent) :
QAbstractItemModel(parent),
nextId(1)
{
}
QModelIndex DynamicTreeModel::index(int row, int column, const QModelIndex &parent) const
{
// if (column != 0)
// return QModelIndex();
if (column < 0 || row < 0)
return QModelIndex();
QList<QList<qint64> > childIdColumns = m_childItems.value(parent.internalId());
const qint64 grandParent = findParentId(parent.internalId());
if (grandParent >= 0) {
QList<QList<qint64> > parentTable = m_childItems.value(grandParent);
if (parent.column() >= parentTable.size())
qFatal("%s: parent.column() must be less than parentTable.size()", Q_FUNC_INFO);
QList<qint64> parentSiblings = parentTable.at(parent.column());
if (parent.row() >= parentSiblings.size())
qFatal("%s: parent.row() must be less than parentSiblings.size()", Q_FUNC_INFO);
}
if (childIdColumns.size() == 0)
return QModelIndex();
if (column >= childIdColumns.size())
return QModelIndex();
QList<qint64> rowIds = childIdColumns.at(column);
if (row >= rowIds.size())
return QModelIndex();
qint64 id = rowIds.at(row);
return createIndex(row, column, reinterpret_cast<void *>(id));
}
qint64 DynamicTreeModel::findParentId(qint64 searchId) const
{
if (searchId <= 0)
return -1;
for (auto i = m_childItems.cbegin(), end = m_childItems.cend(); i != end; ++i) {
for (const auto &list : i.value()) {
if (list.contains(searchId))
return i.key();
}
}
return -1;
}
QModelIndex DynamicTreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
qint64 searchId = index.internalId();
qint64 parentId = findParentId(searchId);
// Will never happen for valid index, but what the hey...
if (parentId <= 0)
return QModelIndex();
qint64 grandParentId = findParentId(parentId);
if (grandParentId < 0)
grandParentId = 0;
int column = 0;
QList<qint64> childList = m_childItems.value(grandParentId).at(column);
int row = childList.indexOf(parentId);
return createIndex(row, column, reinterpret_cast<void *>(parentId));
}
int DynamicTreeModel::rowCount(const QModelIndex &index) const
{
QList<QList<qint64> > cols = m_childItems.value(index.internalId());
if (cols.size() == 0)
return 0;
if (index.column() > 0)
return 0;
return cols.at(0).size();
}
int DynamicTreeModel::columnCount(const QModelIndex &index) const
{
// Q_UNUSED(index);
return m_childItems.value(index.internalId()).size();
}
QVariant DynamicTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (Qt::DisplayRole == role)
return m_items.value(index.internalId());
return QVariant();
}
void DynamicTreeModel::clear()
{
beginResetModel();
m_items.clear();
m_childItems.clear();
nextId = 1;
endResetModel();
}
ModelChangeCommand::ModelChangeCommand(DynamicTreeModel *model, QObject *parent) :
QObject(parent),
m_model(model),
m_numCols(1),
m_startRow(-1),
m_endRow(-1)
{
}
QModelIndex ModelChangeCommand::findIndex(const QList<int> &rows) const
{
const int col = 0;
QModelIndex parent = QModelIndex();
for (int row : rows) {
parent = m_model->index(row, col, parent);
if (!parent.isValid())
qFatal("%s: parent must be valid", Q_FUNC_INFO);
}
return parent;
}
ModelInsertCommand::ModelInsertCommand(DynamicTreeModel *model, QObject *parent) :
ModelChangeCommand(model, parent)
{
}
void ModelInsertCommand::doCommand()
{
QModelIndex parent = findIndex(m_rowNumbers);
m_model->beginInsertRows(parent, m_startRow, m_endRow);
qint64 parentId = parent.internalId();
for (int row = m_startRow; row <= m_endRow; row++) {
for (int col = 0; col < m_numCols; col++) {
if (m_model->m_childItems[parentId].size() <= col)
m_model->m_childItems[parentId].append(QList<qint64>());
// QString name = QUuid::createUuid().toString();
qint64 id = m_model->newId();
QString name = QString::number(id);
m_model->m_items.insert(id, name);
m_model->m_childItems[parentId][col].insert(row, id);
}
}
m_model->endInsertRows();
}
ModelMoveCommand::ModelMoveCommand(DynamicTreeModel *model, QObject *parent) :
ModelChangeCommand(model, parent)
{
}
bool ModelMoveCommand::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd,
const QModelIndex &destParent, int destRow)
{
return m_model->beginMoveRows(srcParent, srcStart, srcEnd, destParent, destRow);
}
void ModelMoveCommand::doCommand()
{
QModelIndex srcParent = findIndex(m_rowNumbers);
QModelIndex destParent = findIndex(m_destRowNumbers);
if (!emitPreSignal(srcParent, m_startRow, m_endRow, destParent, m_destRow))
return;
for (int column = 0; column < m_numCols; ++column) {
QList<qint64> l = m_model->m_childItems.value(srcParent.internalId())[column].mid(
m_startRow, m_endRow - m_startRow + 1);
for (int i = m_startRow; i <= m_endRow; i++)
m_model->m_childItems[srcParent.internalId()][column].removeAt(m_startRow);
int d;
if (m_destRow < m_startRow) {
d = m_destRow;
} else {
if (srcParent == destParent)
d = m_destRow - (m_endRow - m_startRow + 1);
else
d = m_destRow;
}
foreach (const qint64 id, l)
m_model->m_childItems[destParent.internalId()][column].insert(d++, id);
}
emitPostSignal();
}
void ModelMoveCommand::emitPostSignal()
{
m_model->endMoveRows();
}
ModelResetCommand::ModelResetCommand(DynamicTreeModel *model, QObject *parent) :
ModelMoveCommand(model, parent)
{
}
ModelResetCommand::~ModelResetCommand()
{
}
bool ModelResetCommand::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd,
const QModelIndex &destParent, int destRow)
{
Q_UNUSED(srcParent);
Q_UNUSED(srcStart);
Q_UNUSED(srcEnd);
Q_UNUSED(destParent);
Q_UNUSED(destRow);
return true;
}
void ModelResetCommand::emitPostSignal()
{
m_model->beginResetModel();
m_model->endResetModel();
}
ModelResetCommandFixed::ModelResetCommandFixed(DynamicTreeModel *model, QObject *parent) :
ModelMoveCommand(model, parent)
{
}
ModelResetCommandFixed::~ModelResetCommandFixed()
{
}
bool ModelResetCommandFixed::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd,
const QModelIndex &destParent, int destRow)
{
Q_UNUSED(srcParent);
Q_UNUSED(srcStart);
Q_UNUSED(srcEnd);
Q_UNUSED(destParent);
Q_UNUSED(destRow);
m_model->beginResetModel();
return true;
}
void ModelResetCommandFixed::emitPostSignal()
{
m_model->endResetModel();
}
ModelChangeChildrenLayoutsCommand::ModelChangeChildrenLayoutsCommand(DynamicTreeModel *model,
QObject *parent) :
ModelChangeCommand(model, parent)
{
}
void ModelChangeChildrenLayoutsCommand::doCommand()
{
const QPersistentModelIndex parent1 = findIndex(m_rowNumbers);
const QPersistentModelIndex parent2 = findIndex(m_secondRowNumbers);
QList<QPersistentModelIndex> parents;
parents << parent1;
parents << parent2;
emit m_model->layoutAboutToBeChanged(parents);
int rowSize1 = -1;
int rowSize2 = -1;
for (int column = 0; column < m_numCols; ++column) {
{
QList<qint64> &l = m_model->m_childItems[parent1.internalId()][column];
rowSize1 = l.size();
l.prepend(l.takeLast());
}
{
QList<qint64> &l = m_model->m_childItems[parent2.internalId()][column];
rowSize2 = l.size();
l.append(l.takeFirst());
}
}
// If we're changing one of the parent indexes, we need to ensure that we do that before
// changing any children of that parent. The reason is that we're keeping parent1 and parent2
// around as QPersistentModelIndex instances, and we query idx.parent() in the loop.
QModelIndexList persistent = m_model->persistentIndexList();
foreach (const QModelIndex &parent, parents) {
int idx = persistent.indexOf(parent);
if (idx != -1)
persistent.move(idx, 0);
}
foreach (const QModelIndex &idx, persistent) {
if (idx.parent() == parent1) {
if (idx.row() == rowSize1 - 1) {
m_model->changePersistentIndex(idx,
m_model->createIndex(0, idx.column(),
idx.internalPointer()));
} else {
m_model->changePersistentIndex(idx,
m_model->createIndex(idx.row() + 1, idx.column(),
idx.internalPointer()));
}
} else if (idx.parent() == parent2) {
if (idx.row() == 0) {
m_model->changePersistentIndex(idx,
m_model->createIndex(rowSize2 - 1, idx.column(),
idx.internalPointer()));
} else {
m_model->changePersistentIndex(idx,
m_model->createIndex(idx.row() - 1, idx.column(),
idx.internalPointer()));
}
}
}
emit m_model->layoutChanged(parents);
}

View File

@ -0,0 +1,204 @@
// Copyright (C) 2009 Stephen Kelly <steveire@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef DYNAMICTREEMODEL_H
#define DYNAMICTREEMODEL_H
#include <QtCore/QAbstractItemModel>
#include <QtCore/QHash>
#include <QtCore/QList>
class DynamicTreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
DynamicTreeModel(QObject *parent = nullptr);
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &index = QModelIndex()) const override;
int columnCount(const QModelIndex &index = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
void clear();
protected slots:
/**
Finds the parent id of the string with id @p searchId.
Returns -1 if not found.
*/
qint64 findParentId(qint64 searchId) const;
private:
QHash<qint64, QString> m_items;
QHash<qint64, QList<QList<qint64> > > m_childItems;
qint64 nextId;
qint64 newId()
{
return nextId++;
}
QModelIndex m_nextParentIndex;
int m_nextRow;
int m_depth;
int maxDepth;
friend class ModelInsertCommand;
friend class ModelMoveCommand;
friend class ModelResetCommand;
friend class ModelResetCommandFixed;
friend class ModelChangeChildrenLayoutsCommand;
};
class ModelChangeCommand : public QObject
{
Q_OBJECT
public:
ModelChangeCommand(DynamicTreeModel *model, QObject *parent = nullptr);
virtual ~ModelChangeCommand()
{
}
void setAncestorRowNumbers(QList<int> rowNumbers)
{
m_rowNumbers = rowNumbers;
}
QModelIndex findIndex(const QList<int> &rows) const;
void setStartRow(int row)
{
m_startRow = row;
}
void setEndRow(int row)
{
m_endRow = row;
}
void setNumCols(int cols)
{
m_numCols = cols;
}
virtual void doCommand() = 0;
protected:
DynamicTreeModel *m_model;
QList<int> m_rowNumbers;
int m_numCols;
int m_startRow;
int m_endRow;
};
typedef QList<ModelChangeCommand *> ModelChangeCommandList;
class ModelInsertCommand : public ModelChangeCommand
{
Q_OBJECT
public:
ModelInsertCommand(DynamicTreeModel *model, QObject *parent = nullptr);
virtual ~ModelInsertCommand()
{
}
virtual void doCommand() override;
};
class ModelMoveCommand : public ModelChangeCommand
{
Q_OBJECT
public:
ModelMoveCommand(DynamicTreeModel *model, QObject *parent);
virtual ~ModelMoveCommand()
{
}
virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd,
const QModelIndex &destParent, int destRow);
virtual void doCommand() override;
virtual void emitPostSignal();
void setDestAncestors(QList<int> rows)
{
m_destRowNumbers = rows;
}
void setDestRow(int row)
{
m_destRow = row;
}
protected:
QList<int> m_destRowNumbers;
int m_destRow;
};
/**
A command which does a move and emits a reset signal.
*/
class ModelResetCommand : public ModelMoveCommand
{
Q_OBJECT
public:
ModelResetCommand(DynamicTreeModel *model, QObject *parent = nullptr);
virtual ~ModelResetCommand();
virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd,
const QModelIndex &destParent, int destRow) override;
virtual void emitPostSignal() override;
};
/**
A command which does a move and emits a beginResetModel and endResetModel signals.
*/
class ModelResetCommandFixed : public ModelMoveCommand
{
Q_OBJECT
public:
ModelResetCommandFixed(DynamicTreeModel *model, QObject *parent = nullptr);
virtual ~ModelResetCommandFixed();
virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd,
const QModelIndex &destParent, int destRow) override;
virtual void emitPostSignal() override;
};
class ModelChangeChildrenLayoutsCommand : public ModelChangeCommand
{
Q_OBJECT
public:
ModelChangeChildrenLayoutsCommand(DynamicTreeModel *model, QObject *parent);
virtual ~ModelChangeChildrenLayoutsCommand()
{
}
virtual void doCommand() override;
void setSecondAncestorRowNumbers(QList<int> rows)
{
m_secondRowNumbers = rows;
}
protected:
QList<int> m_secondRowNumbers;
int m_destRow;
};
#endif

View File

@ -0,0 +1,38 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
if(NOT QT_FEATURE_accessibility)
return()
endif()
#####################################################################
## tst_qaccessibility Test:
#####################################################################
qt_internal_add_test(tst_qaccessibility
SOURCES
accessiblewidgets.h
tst_qaccessibility.cpp
LIBRARIES
Qt::CorePrivate
Qt::Gui
Qt::GuiPrivate
Qt::TestPrivate
Qt::WidgetsPrivate
)
## Scopes:
#####################################################################
qt_internal_extend_target(tst_qaccessibility CONDITION UNIX AND NOT APPLE AND NOT HAIKU AND NOT INTEGRITY
LIBRARIES
m
)
qt_internal_extend_target(tst_qaccessibility CONDITION WIN32
LIBRARIES
ole32
oleacc
oleaut32
uuid
)

View File

@ -0,0 +1,130 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef ACCESSIBLEWIDGETS_H
#define ACCESSIBLEWIDGETS_H
#include <QtWidgets/qaccessiblewidget.h>
#include <QtWidgets/qpushbutton.h>
class QtTestAccessibleWidget: public QWidget
{
Q_OBJECT
public:
QtTestAccessibleWidget(QWidget *parent, const char *name): QWidget(parent)
{
setObjectName(name);
}
};
class QtTestAccessibleWidgetIface: public QAccessibleWidget
{
public:
QtTestAccessibleWidgetIface(QtTestAccessibleWidget *w): QAccessibleWidget(w) {}
QString text(QAccessible::Text t) const override
{
if (t == QAccessible::Help)
return QString::fromLatin1("Help yourself");
return QAccessibleWidget::text(t);
}
static QAccessibleInterface *ifaceFactory(const QString &key, QObject *o)
{
if (key == "QtTestAccessibleWidget")
return new QtTestAccessibleWidgetIface(static_cast<QtTestAccessibleWidget*>(o));
return 0;
}
};
class QtTestAccessibleWidgetSubclass: public QtTestAccessibleWidget
{
Q_OBJECT
public:
QtTestAccessibleWidgetSubclass(QWidget *parent, const char *name): QtTestAccessibleWidget(parent, name)
{}
};
class KFooButton: public QPushButton
{
Q_OBJECT
public:
KFooButton(const QString &text, QWidget *parent = nullptr)
: QPushButton(text, parent)
{}
};
class CustomTextWidget : public QWidget
{
Q_OBJECT
public:
int cursorPosition;
QString text;
};
class CustomTextWidgetIface: public QAccessibleWidget, public QAccessibleTextInterface
{
public:
static QAccessibleInterface *ifaceFactory(const QString &key, QObject *o)
{
if (key == "CustomTextWidget")
return new CustomTextWidgetIface(static_cast<CustomTextWidget*>(o));
return 0;
}
CustomTextWidgetIface(CustomTextWidget *w): QAccessibleWidget(w) {}
void *interface_cast(QAccessible::InterfaceType t) override
{
if (t == QAccessible::TextInterface)
return static_cast<QAccessibleTextInterface*>(this);
return 0;
}
// this is mostly to test the base implementation for textBefore/At/After
QString text(QAccessible::Text t) const override
{
if (t == QAccessible::Value)
return textWidget()->text;
return QAccessibleWidget::text(t);
}
QString textBeforeOffset(int offset, QAccessible::TextBoundaryType boundaryType, int *startOffset, int *endOffset) const override
{
if (offset == -2)
offset = textWidget()->cursorPosition;
return QAccessibleTextInterface::textBeforeOffset(offset, boundaryType, startOffset, endOffset);
}
QString textAtOffset(int offset, QAccessible::TextBoundaryType boundaryType, int *startOffset, int *endOffset) const override
{
if (offset == -2)
offset = textWidget()->cursorPosition;
return QAccessibleTextInterface::textAtOffset(offset, boundaryType, startOffset, endOffset);
}
QString textAfterOffset(int offset, QAccessible::TextBoundaryType boundaryType, int *startOffset, int *endOffset) const override
{
if (offset == -2)
offset = textWidget()->cursorPosition;
return QAccessibleTextInterface::textAfterOffset(offset, boundaryType, startOffset, endOffset);
}
void selection(int, int *startOffset, int *endOffset) const override
{ *startOffset = *endOffset = -1; }
int selectionCount() const override { return 0; }
void addSelection(int, int) override {}
void removeSelection(int) override {}
void setSelection(int, int, int) override {}
int cursorPosition() const override { return textWidget()->cursorPosition; }
void setCursorPosition(int position) override { textWidget()->cursorPosition = position; }
QString text(int startOffset, int endOffset) const override { return textWidget()->text.mid(startOffset, endOffset); }
int characterCount() const override { return textWidget()->text.size(); }
QRect characterRect(int) const override { return QRect(); }
int offsetAtPoint(const QPoint &) const override { return 0; }
void scrollToSubstring(int, int) override {}
QString attributes(int, int *, int *) const override
{ return QString(); }
private:
CustomTextWidget *textWidget() const { return qobject_cast<CustomTextWidget *>(widget()); }
};
#endif // ACCESSIBLEWIDGETS_H

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,29 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
if (NOT QT_FEATURE_accessibility OR NOT TARGET Qt::LinuxAccessibilitySupportPrivate
OR (QT_BUILD_STANDALONE_TESTS AND QT_WILL_INSTALL))
return()
endif()
#####################################################################
## tst_qaccessibilitylinux Test:
#####################################################################
qt_internal_add_test(tst_qaccessibilitylinux
SOURCES
tst_qaccessibilitylinux.cpp
DBUS_INTERFACE_SOURCES
../../../../src/platformsupport/linuxaccessibility/dbusxml/Bus.xml
LIBRARIES
Qt::DBus
Qt::Gui
Qt::GuiPrivate
Qt::LinuxAccessibilitySupportPrivate
Qt::Widgets
)
# require for struct_marshallers_p.h which is included dbus_interface.h
target_include_directories(tst_qaccessibilitylinux PRIVATE
../../../../src/platformsupport/linuxaccessibility
)

View File

@ -0,0 +1,537 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTest>
#include <QtGui>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QListWidget>
#include <QtWidgets/QTreeWidget>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QPushButton>
#include <QDBusArgument>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusInterface>
#include <QDBusReply>
#include <atspi/atspi-constants.h>
#include <private/dbusconnection_p.h>
#include <private/struct_marshallers_p.h>
#include "bus_interface.h"
#define COMPARE3(v1, v2, v3) QCOMPARE(v1, v3); QCOMPARE(v2, v3);
class AccessibleTestWindow : public QWidget
{
Q_OBJECT
public:
AccessibleTestWindow()
{
new QHBoxLayout(this);
}
void addWidget(QWidget* widget)
{
layout()->addWidget(widget);
widget->show();
QVERIFY(QTest::qWaitForWindowExposed(widget));
}
void clearChildren()
{
qDeleteAll(children());
new QHBoxLayout(this);
}
};
class tst_QAccessibilityLinux : public QObject
{
Q_OBJECT
public:
tst_QAccessibilityLinux() : m_window(0), root(0), rootApplication(0), mainWindow(0)
{
qputenv("QT_LINUX_ACCESSIBILITY_ALWAYS_ON", "1");
dbus = new DBusConnection();
}
~tst_QAccessibilityLinux()
{
delete dbus;
}
private slots:
void initTestCase();
void testLabel();
void testLineEdit();
void testListWidget();
void testTreeWidget();
void testTextEdit();
void testSlider();
void testFocus();
void cleanupTestCase();
private:
void registerDbus();
static QString getParent(QDBusInterface *interface);
static QStringList getChildren(QDBusInterface *interface);
QDBusInterface *getInterface(const QString &path, const QString &interfaceName);
AccessibleTestWindow *m_window;
QString address;
QDBusInterface *root; // the root object on dbus (for the app)
QDBusInterface *rootApplication;
QDBusInterface *mainWindow;
DBusConnection *dbus;
};
// helper to find children of a dbus object
QStringList tst_QAccessibilityLinux::getChildren(QDBusInterface *interface)
{
QSpiObjectReferenceArray list;
const QList<QVariant> args = interface->call(QDBus::Block, "GetChildren").arguments();
Q_ASSERT(args.size() == 1);
Q_ASSERT(args.first().isValid());
args.first().value<QDBusArgument>() >> list;
Q_ASSERT(interface->property("ChildCount").toInt() == list.count());
QStringList children;
Q_FOREACH (const QSpiObjectReference &ref, list)
children << ref.path.path();
return children;
}
QString tst_QAccessibilityLinux::getParent(QDBusInterface *interface)
{
if (!interface->isValid())
return QString();
QVariant var = interface->property("Parent");
if (!var.canConvert<QSpiObjectReference>()) {
qWarning() << "Invalid parent";
return QString();
}
QSpiObjectReference parent = var.value<QSpiObjectReference>();
return parent.path.path();
}
// helper to get dbus object
QDBusInterface *tst_QAccessibilityLinux::getInterface(const QString &path, const QString &interfaceName)
{
return new QDBusInterface(address, path, interfaceName, dbus->connection(), this);
}
void tst_QAccessibilityLinux::initTestCase()
{
// Oxygen style creates many extra items, it's simply unusable here
qApp->setStyle("fusion");
qApp->setApplicationName("tst_QAccessibilityLinux app");
// trigger launching of at-spi if it isn't running already
QDBusConnection c = QDBusConnection::sessionBus();
OrgA11yStatusInterface *a11yStatus = new OrgA11yStatusInterface(QStringLiteral("org.a11y.Bus"), QStringLiteral("/org/a11y/bus"), c, this);
// don't care about the result, calling any function on "org.a11y.Bus" will launch the service
a11yStatus->isEnabled();
for (int i = 0; i < 5; ++i) {
if (!dbus->isEnabled())
QTest::qWait(100);
}
if (!dbus->isEnabled())
QSKIP("Could not connect to AT-SPI, make sure lib atspi2 is installed.");
QTRY_VERIFY(dbus->isEnabled());
QTRY_VERIFY(dbus->connection().isConnected());
address = dbus->connection().baseService().toLatin1().data();
QVERIFY(!address.isEmpty());
m_window = new AccessibleTestWindow();
m_window->setObjectName("mainWindow"_L1);
m_window->show();
QVERIFY(QTest::qWaitForWindowExposed(m_window));
registerDbus();
}
void tst_QAccessibilityLinux::cleanupTestCase()
{
delete mainWindow;
delete rootApplication;
delete root;
delete m_window;
}
void tst_QAccessibilityLinux::registerDbus()
{
QVERIFY(dbus->connection().isConnected());
root = getInterface("/org/a11y/atspi/accessible/root",
"org.a11y.atspi.Accessible");
rootApplication = getInterface("/org/a11y/atspi/accessible/root",
"org.a11y.atspi.Application");
QVERIFY(root->isValid());
QVERIFY(rootApplication->isValid());
QStringList appChildren = getChildren(root);
QString window = appChildren.at(0);
mainWindow = getInterface(window, "org.a11y.atspi.Accessible");
}
quint64 getAtspiState(QDBusInterface *interface)
{
QDBusMessage msg = interface->call(QDBus::Block, "GetState");
const QDBusArgument arg = msg.arguments().at(0).value<QDBusArgument>();
quint32 state1 = 0;
quint64 state2 = 0;
arg.beginArray();
arg >> state1;
arg >> state2;
arg.endArray();
state2 = state2 << 32;
return state2 | state1;
}
bool hasState(QDBusInterface *interface, AtspiStateType state)
{
quint64 intState = quint64(1) << state;
return getAtspiState(interface) & intState;
}
#define ROOTPATH "/org/a11y/atspi/accessible"
void tst_QAccessibilityLinux::testLabel()
{
QLabel *l = new QLabel(m_window);
l->setObjectName("theObjectName"_L1);
l->setText("Hello A11y");
m_window->addWidget(l);
auto a11yEmpty = new QLabel(m_window);
m_window->addWidget(l);
// Application
QCOMPARE(getParent(mainWindow), QLatin1String(ATSPI_DBUS_PATH_ROOT));
QStringList children = getChildren(mainWindow);
QDBusInterface *labelInterface = getInterface(children.at(0), "org.a11y.atspi.Accessible");
QVERIFY(labelInterface->isValid());
QCOMPARE(labelInterface->property("Name").toString(), QLatin1String("Hello A11y"));
QCOMPARE(getChildren(labelInterface).count(), 0);
QCOMPARE(labelInterface->call(QDBus::Block, "GetRoleName").arguments().first().toString(), QLatin1String("label"));
QCOMPARE(labelInterface->call(QDBus::Block, "GetRole").arguments().first().toUInt(), 29u);
QCOMPARE(labelInterface->call(QDBus::Block, "GetAccessibleId").arguments().first().toString(),
"mainWindow.theObjectName"_L1);
QCOMPARE(getParent(labelInterface), mainWindow->path());
QVERIFY(!hasState(labelInterface, ATSPI_STATE_EDITABLE));
QVERIFY(hasState(labelInterface, ATSPI_STATE_READ_ONLY));
l->setText("New text");
QCOMPARE(labelInterface->property("Name").toString(), l->text());
auto *a11yEmptyInterface = getInterface(children.at(1), "org.a11y.atspi.Accessible");
QCOMPARE(a11yEmptyInterface->call(QDBus::Block, "GetAccessibleId").arguments().first().toString(),
"mainWindow.QLabel"_L1);
m_window->clearChildren();
delete a11yEmptyInterface;
delete labelInterface;
}
void tst_QAccessibilityLinux::testLineEdit()
{
QLineEdit *lineEdit = new QLineEdit(m_window);
lineEdit->setText("a11y test QLineEdit");
m_window->addWidget(lineEdit);
QStringList children = getChildren(mainWindow);
QDBusInterface *accessibleInterface = getInterface(children.at(0), "org.a11y.atspi.Accessible");
QDBusInterface *editableTextInterface = getInterface(children.at(0), "org.a11y.atspi.EditableText");
QDBusInterface *textInterface = getInterface(children.at(0), "org.a11y.atspi.Text");
QVERIFY(accessibleInterface->isValid());
QVERIFY(editableTextInterface->isValid());
QVERIFY(textInterface->isValid());
QCOMPARE(accessibleInterface->call(QDBus::Block, "GetRoleName").arguments().first().toString(), QLatin1String("text"));
QCOMPARE(textInterface->call(QDBus::Block,"GetText", 5, -1).arguments().first().toString(), QLatin1String("test QLineEdit"));
QString newText = "Text has changed!";
editableTextInterface->call(QDBus::Block, "SetTextContents", newText);
COMPARE3(lineEdit->text(), textInterface->call(QDBus::Block, "GetText", 0, -1).arguments().first().toString(), newText);
QCOMPARE(textInterface->call(QDBus::Block, "GetText", 0, 4).arguments().first().toString(), QLatin1String("Text"));
editableTextInterface->call(QDBus::Block, "DeleteText", 4, 8);
COMPARE3(lineEdit->text(), "Te" + textInterface->call(QDBus::Block, "GetText", 2, 10).arguments().first().toString() + "ed!", QLatin1String("Text changed!"));
editableTextInterface->call(QDBus::Block, "InsertText", 12, " again ", 6);
QCOMPARE(lineEdit->text(), QLatin1String("Text changed again!"));
COMPARE3(lineEdit->text().length(), textInterface->property("CharacterCount").toInt(), 19);
textInterface->call(QDBus::Block, "SetCaretOffset", 4);
COMPARE3(lineEdit->cursorPosition(), textInterface->property("CaretOffset").toInt(), 4);
textInterface->call(QDBus::Block, "AddSelection", 1, 4);
QList<QVariant> data = textInterface->call(QDBus::Block, "GetSelection", 0).arguments();
COMPARE3(data.at(0).toInt(), lineEdit->selectionStart(), 1);
QCOMPARE(data.at(1).toInt(), 4);
QCOMPARE(lineEdit->selectedText().length(), 3);
QCOMPARE(textInterface->call(QDBus::Block, "GetNSelections").arguments().first().toInt(), 1);
textInterface->call(QDBus::Block, "SetSelection", 0, 0, 5);
data = textInterface->call(QDBus::Block, "GetSelection", 0).arguments();
COMPARE3(data.at(0).toInt(), lineEdit->selectionStart(), 0);
COMPARE3(data.at(1).toInt(), lineEdit->selectedText().length(), 5);
textInterface->call(QDBus::Block, "RemoveSelection", 0);
QCOMPARE(lineEdit->selectionStart(), -1);
QCOMPARE(textInterface->call(QDBus::Block, "GetNSelections").arguments().first().toInt(), 0);
QVERIFY(hasState(accessibleInterface, ATSPI_STATE_EDITABLE));
QVERIFY(!hasState(accessibleInterface, ATSPI_STATE_READ_ONLY));
lineEdit->setReadOnly(true);
QVERIFY(hasState(accessibleInterface, ATSPI_STATE_EDITABLE));
QVERIFY(hasState(accessibleInterface, ATSPI_STATE_READ_ONLY));
m_window->clearChildren();
delete accessibleInterface;
delete textInterface;
delete editableTextInterface;
}
void tst_QAccessibilityLinux::testListWidget()
{
QListWidget *lw = new QListWidget;
lw->addItem("Hello");
lw->addItem("Good morning");
lw->addItem("Good bye");
m_window->addWidget(lw);
QStringList children = getChildren(mainWindow);
QDBusInterface *listIface = getInterface(children.at(0), "org.a11y.atspi.Accessible");
QCOMPARE(listIface->call(QDBus::Block, "GetRoleName").arguments().first().toString(), QLatin1String("list"));
QStringList tableChildren = getChildren(listIface);
QCOMPARE(tableChildren.size(), 3);
QDBusInterface *cell1 = getInterface(tableChildren.at(0), "org.a11y.atspi.Accessible");
QCOMPARE(cell1->call(QDBus::Block, "GetRoleName").arguments().first().toString(), QLatin1String("list item"));
QCOMPARE(cell1->property("Name").toString(), QLatin1String("Hello"));
QDBusInterface *cell2 = getInterface(tableChildren.at(1), "org.a11y.atspi.Accessible");
QCOMPARE(cell2->call(QDBus::Block, "GetRoleName").arguments().first().toString(), QLatin1String("list item"));
QCOMPARE(cell2->property("Name").toString(), QLatin1String("Good morning"));
QDBusInterface *cell3 = getInterface(tableChildren.at(2), "org.a11y.atspi.Accessible");
QCOMPARE(cell3->call(QDBus::Block, "GetRoleName").arguments().first().toString(), QLatin1String("list item"));
QCOMPARE(cell3->property("Name").toString(), QLatin1String("Good bye"));
delete cell1; delete cell2; delete cell3;
m_window->clearChildren();
delete listIface;
}
void tst_QAccessibilityLinux::testTreeWidget()
{
QTreeWidget *tree = new QTreeWidget;
tree->setColumnCount(2);
tree->setHeaderLabels(QStringList() << "Header 1" << "Header 2");
QTreeWidgetItem *top1 = new QTreeWidgetItem(QStringList() << "0.0" << "0.1");
tree->addTopLevelItem(top1);
QTreeWidgetItem *top2 = new QTreeWidgetItem(QStringList() << "1.0" << "1.1");
tree->addTopLevelItem(top2);
QTreeWidgetItem *child1 = new QTreeWidgetItem(QStringList() << "1.0 0.0" << "1.0 0.1");
top2->addChild(child1);
m_window->addWidget(tree);
QStringList children = getChildren(mainWindow);
QDBusInterface *treeIface = getInterface(children.at(0), "org.a11y.atspi.Accessible");
QCOMPARE(treeIface->call(QDBus::Block, "GetRoleName").arguments().first().toString(), QLatin1String("tree"));
QStringList tableChildren = getChildren(treeIface);
QCOMPARE(tableChildren.size(), 6);
QDBusInterface *cell1 = getInterface(tableChildren.at(0), "org.a11y.atspi.Accessible");
QCOMPARE(cell1->call(QDBus::Block, "GetRoleName").arguments().first().toString(), QLatin1String("column header"));
QCOMPARE(cell1->property("Name").toString(), QLatin1String("Header 1"));
QDBusInterface *cell2 = getInterface(tableChildren.at(1), "org.a11y.atspi.Accessible");
QCOMPARE(cell2->call(QDBus::Block, "GetRoleName").arguments().first().toString(), QLatin1String("column header"));
QCOMPARE(cell2->property("Name").toString(), QLatin1String("Header 2"));
QDBusInterface *cell3 = getInterface(tableChildren.at(2), "org.a11y.atspi.Accessible");
QCOMPARE(cell3->property("Name").toString(), QLatin1String("0.0"));
QVERIFY(!hasState(cell3, ATSPI_STATE_EXPANDABLE));
QVERIFY(!hasState(cell3, ATSPI_STATE_EXPANDED));
QDBusInterface *cell4 = getInterface(tableChildren.at(3), "org.a11y.atspi.Accessible");
QCOMPARE(cell4->property("Name").toString(), QLatin1String("0.1"));
QDBusInterface *dbus_top2 = getInterface(tableChildren.at(4), "org.a11y.atspi.Accessible");
QCOMPARE(dbus_top2->property("Name").toString(), QLatin1String("1.0"));
QVERIFY(hasState(dbus_top2, ATSPI_STATE_EXPANDABLE));
QVERIFY(!hasState(dbus_top2, ATSPI_STATE_EXPANDED));
tree->expandItem(top2);
tableChildren = getChildren(treeIface);
QCOMPARE(tableChildren.size(), 8);
QVERIFY(hasState(dbus_top2, ATSPI_STATE_EXPANDED));
QDBusInterface *cell5 = getInterface(tableChildren.at(6), "org.a11y.atspi.Accessible");
QCOMPARE(cell5->property("Name").toString(), QLatin1String("1.0 0.0"));
QDBusInterface *cell6 = getInterface(tableChildren.at(7), "org.a11y.atspi.Accessible");
QCOMPARE(cell6->property("Name").toString(), QLatin1String("1.0 0.1"));
QDBusInterface *treeTableIface = getInterface(children.at(0), "org.a11y.atspi.Table");
QCOMPARE(treeTableIface->call(QDBus::Block, "GetRowAtIndex", 0).arguments().first().toInt(), -1);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetRowAtIndex", 1).arguments().first().toInt(), -1);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetRowAtIndex", 2).arguments().first().toInt(), 0);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetRowAtIndex", 3).arguments().first().toInt(), 0);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetRowAtIndex", 4).arguments().first().toInt(), 1);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetRowAtIndex", 5).arguments().first().toInt(), 1);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetRowAtIndex", 6).arguments().first().toInt(), 2);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetRowAtIndex", 7).arguments().first().toInt(), 2);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetColumnAtIndex", 0).arguments().first().toInt(), 0);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetColumnAtIndex", 1).arguments().first().toInt(), 1);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetColumnAtIndex", 2).arguments().first().toInt(), 0);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetColumnAtIndex", 3).arguments().first().toInt(), 1);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetColumnAtIndex", 4).arguments().first().toInt(), 0);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetColumnAtIndex", 5).arguments().first().toInt(), 1);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetColumnAtIndex", 6).arguments().first().toInt(), 0);
QCOMPARE(treeTableIface->call(QDBus::Block, "GetColumnAtIndex", 7).arguments().first().toInt(), 1);
delete treeTableIface;
delete cell1; delete cell2; delete cell3; delete cell4; delete cell5; delete cell6;
m_window->clearChildren();
delete treeIface;
}
void tst_QAccessibilityLinux::testTextEdit()
{
QTextEdit *textEdit = new QTextEdit(m_window);
textEdit->setText("<html><head></head><body>This is a <b>sample</b> text.<br />"
"How are you today</body></html>");
textEdit->show();
m_window->addWidget(textEdit);
QStringList children = getChildren(mainWindow);
QDBusInterface *accessibleInterface = getInterface(children.at(0), "org.a11y.atspi.Accessible");
QDBusInterface *editableTextInterface = getInterface(children.at(0), "org.a11y.atspi.EditableText");
QDBusInterface *textInterface = getInterface(children.at(0), "org.a11y.atspi.Text");
QVERIFY(accessibleInterface->isValid());
QVERIFY(editableTextInterface->isValid());
QVERIFY(textInterface->isValid());
QList<QVariant> callResult;
QDBusMessage msg = textInterface->call(QDBus::Block, "GetText", 0, 5);
callResult = msg.arguments();
QCOMPARE(callResult.at(0).toString(), QLatin1String("This "));
msg = textInterface->call(QDBus::Block, "GetTextAtOffset", 12, (uint) ATSPI_TEXT_BOUNDARY_WORD_START);
callResult = msg.arguments();
QEXPECT_FAIL("", "Word should contain space at end according to atspi.", Continue);
QCOMPARE(callResult.at(0).toString(), QLatin1String("sample "));
QCOMPARE(callResult.at(1).toInt(), 10);
QEXPECT_FAIL("", "Due to missing space the count is off by one.", Continue);
QCOMPARE(callResult.at(2).toInt(), 17);
// Check if at least CharacterExtents and RangeExtents give a consistent result
QDBusMessage replyRect20 = textInterface->call(QDBus::Block, "GetCharacterExtents", 20, ATSPI_COORD_TYPE_SCREEN);
QCOMPARE(replyRect20.type(), QDBusMessage::ReplyMessage);
QCOMPARE(replyRect20.signature(), QStringLiteral("iiii"));
callResult = replyRect20.arguments();
QRect r1 = QRect(callResult.at(0).toInt(), callResult.at(1).toInt(), callResult.at(2).toInt(), callResult.at(3).toInt());
QDBusMessage replyRect21 = textInterface->call(QDBus::Block, "GetCharacterExtents", 21, ATSPI_COORD_TYPE_SCREEN);
QCOMPARE(replyRect21.type(), QDBusMessage::ReplyMessage);
QCOMPARE(replyRect21.signature(), QStringLiteral("iiii"));
callResult = replyRect21.arguments();
QRect r2 = QRect(callResult.at(0).toInt(), callResult.at(1).toInt(), callResult.at(2).toInt(), callResult.at(3).toInt());
QDBusMessage replyRange = textInterface->call(QDBus::Block, "GetRangeExtents", 20, 21, ATSPI_COORD_TYPE_SCREEN);
callResult = replyRange.arguments();
QRect rectRangeExtents = QRect(callResult.at(0).toInt(), callResult.at(1).toInt(), callResult.at(2).toInt(), callResult.at(3).toInt());
QCOMPARE(rectRangeExtents, r1|r2);
m_window->clearChildren();
delete textInterface;
}
void tst_QAccessibilityLinux::testSlider()
{
QSlider *slider = new QSlider(m_window);
slider->setMinimum(2);
slider->setMaximum(5);
slider->setValue(3);
m_window->addWidget(slider);
QStringList children = getChildren(mainWindow);
QDBusInterface *accessibleInterface = getInterface(children.at(0), "org.a11y.atspi.Accessible");
QDBusInterface *valueInterface = getInterface(children.at(0), "org.a11y.atspi.Value");
QVERIFY(accessibleInterface->isValid());
QVERIFY(valueInterface->isValid());
QCOMPARE(valueInterface->property("CurrentValue").toInt(), 3);
QCOMPARE(valueInterface->property("MinimumValue").toInt(), 2);
QCOMPARE(valueInterface->property("MaximumValue").toInt(), 5);
valueInterface->setProperty("CurrentValue", 4);
QCOMPARE(valueInterface->property("CurrentValue").toInt(), 4);
m_window->clearChildren();
}
void tst_QAccessibilityLinux::testFocus()
{
m_window->activateWindow();
QVERIFY(QTest::qWaitForWindowActive(m_window));
QLineEdit *lineEdit1 = new QLineEdit(m_window);
lineEdit1->setText("lineEdit 1");
QLineEdit *lineEdit2 = new QLineEdit(m_window);
lineEdit2->setText("lineEdit 2");
m_window->addWidget(lineEdit1);
m_window->addWidget(lineEdit2);
lineEdit1->setFocus();
QStringList children = getChildren(mainWindow);
QCOMPARE(children.length(), 2);
QDBusInterface *accessibleInterfaceLineEdit1 = getInterface(children.at(0), "org.a11y.atspi.Accessible");
QVERIFY(accessibleInterfaceLineEdit1->isValid());
QDBusInterface *accessibleInterfaceLineEdit2 = getInterface(children.at(1), "org.a11y.atspi.Accessible");
QVERIFY(accessibleInterfaceLineEdit2->isValid());
QDBusInterface *componentInterfaceLineEdit1 = getInterface(children.at(0), "org.a11y.atspi.Component");
QVERIFY(componentInterfaceLineEdit1->isValid());
QDBusInterface *componentInterfaceLineEdit2 = getInterface(children.at(1), "org.a11y.atspi.Component");
QVERIFY(componentInterfaceLineEdit2->isValid());
QVERIFY(hasState(accessibleInterfaceLineEdit1, ATSPI_STATE_FOCUSED));
QVERIFY(!hasState(accessibleInterfaceLineEdit2, ATSPI_STATE_FOCUSED));
QDBusMessage focusReply = componentInterfaceLineEdit2->call(QDBus::Block, "GrabFocus");
QVERIFY(focusReply.arguments().at(0).toBool());
QVERIFY(lineEdit2->hasFocus());
QVERIFY(!hasState(accessibleInterfaceLineEdit1, ATSPI_STATE_FOCUSED));
QVERIFY(hasState(accessibleInterfaceLineEdit2, ATSPI_STATE_FOCUSED));
m_window->clearChildren();
delete accessibleInterfaceLineEdit1;
delete accessibleInterfaceLineEdit2;
delete componentInterfaceLineEdit1;
delete componentInterfaceLineEdit2;
}
QTEST_MAIN(tst_QAccessibilityLinux)
#include "tst_qaccessibilitylinux.moc"

View File

@ -0,0 +1,17 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
if(NOT APPLE)
return()
endif()
qt_internal_add_test(tst_qaccessibilitymac
SOURCES
tst_qaccessibilitymac.mm
LIBRARIES
Qt::Gui
Qt::Widgets
${FWAppKit}
${FWApplicationServices}
${FWSecurity}
)

View File

@ -0,0 +1,736 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QApplication>
#include <QtWidgets>
#include <QTest>
#include <QtCore/qcoreapplication.h>
// some versions of CALayer.h use 'slots' as an identifier
#define QT_NO_KEYWORDS
#include <QtWidgets/qapplication.h>
#include <QtWidgets/qlineedit.h>
#include <QtWidgets/qpushbutton.h>
#include <QtWidgets>
#include <QTest>
#include <unistd.h>
#import <AppKit/AppKit.h>
#import <ApplicationServices/ApplicationServices.h>
QT_USE_NAMESPACE
struct AXErrorTag {
AXError err;
explicit AXErrorTag(AXError theErr) : err(theErr) {}
};
QDebug operator<<(QDebug dbg, AXErrorTag err)
{
QDebugStateSaver saver(dbg);
const char *errDesc = 0;
const char *errName = 0;
switch (err.err) {
#define HANDLE_ERR(error, desc) case kAXError##error: errName = "kAXError" #error; errDesc = desc; break
HANDLE_ERR(Success, "Success");
HANDLE_ERR(Failure, "A system error occurred, such as the failure to allocate an object.");
HANDLE_ERR(IllegalArgument, "An illegal argument was passed to the function.");
HANDLE_ERR(InvalidUIElement, "The AXUIElementRef passed to the function is invalid.");
HANDLE_ERR(InvalidUIElementObserver, "The AXObserverRef passed to the function is not a valid observer.");
HANDLE_ERR(CannotComplete, "The function cannot complete because messaging failed in some way or because the application with which the function is communicating is busy or unresponsive.");
HANDLE_ERR(AttributeUnsupported, "The attribute is not supported by the AXUIElementRef.");
HANDLE_ERR(ActionUnsupported, "The action is not supported by the AXUIElementRef.");
HANDLE_ERR(NotificationUnsupported, "The notification is not supported by the AXUIElementRef.");
HANDLE_ERR(NotImplemented, "Indicates that the function or method is not implemented (this can be returned if a process does not support the accessibility API).");
HANDLE_ERR(NotificationAlreadyRegistered, "This notification has already been registered for.");
HANDLE_ERR(NotificationNotRegistered, "Indicates that a notification is not registered yet.");
HANDLE_ERR(APIDisabled, "The accessibility API is disabled (as when, for example, the user deselects \"Enable access for assistive devices\" in Universal Access Preferences).");
HANDLE_ERR(NoValue, "The requested value or AXUIElementRef does not exist.");
HANDLE_ERR(ParameterizedAttributeUnsupported, "The parameterized attribute is not supported by the AXUIElementRef.");
HANDLE_ERR(NotEnoughPrecision, "Not enough precision.");
default: errName = "<unknown error>"; errDesc = "UNKNOWN ERROR"; break;
}
#undef HANDLE_ERR
dbg.nospace() << "AXError(value=" << err.err << ", name=" << errName << ", description=\"" << errDesc << "\")";
return dbg;
}
@interface TestAXObject : NSObject
{
AXUIElementRef reference;
}
@property (readonly) NSString *role;
@property (readonly) NSString *title;
@property (readonly) NSString *description;
@property (readonly) NSString *value;
@property (readonly) CGRect rect;
@property (readonly) NSArray *actions;
@end
@implementation TestAXObject
- (instancetype)initWithAXUIElementRef:(AXUIElementRef)ref {
if ((self = [super init])) {
reference = ref;
}
return self;
}
- (AXUIElementRef) ref { return reference; }
- (void) print {
NSLog(@"Accessible Object role: '%@', title: '%@', description: '%@', value: '%@', rect: '%@'", self.role, self.title, self.description, self.value, NSStringFromRect(NSRectFromCGRect(self.rect)));
NSLog(@" Children: %ld", [[self childList] count]);
}
- (NSArray*) windowList
{
NSArray *list;
AXUIElementCopyAttributeValues(
reference,
kAXWindowsAttribute,
0, 100, /*min, max*/
(CFArrayRef *) &list);
return list;
}
- (NSArray*) childList
{
NSArray *list;
AXUIElementCopyAttributeValues(
reference,
kAXChildrenAttribute,
0, 100, /*min, max*/
(CFArrayRef *) &list);
return list;
}
- (NSArray *)tableRows
{
NSArray *arr;
AXUIElementCopyAttributeValues(
reference,
kAXRowsAttribute,
0, 100, /*min, max*/
(CFArrayRef *) &arr);
return arr;
}
- (NSArray *)tableColumns
{
NSArray *arr;
AXUIElementCopyAttributeValues(
reference,
kAXColumnsAttribute,
0, 100, /*min, max*/
(CFArrayRef *) &arr);
return arr;
}
- (AXUIElementRef) findDirectChildByRole: (CFStringRef) role
{
TestAXObject *result = nil;
NSArray *childList = [self childList];
for (id child in childList) {
TestAXObject *childObject = [[TestAXObject alloc] initWithAXUIElementRef:(AXUIElementRef)child];
if ([childObject.role isEqualToString:(NSString*)role]) {
result = childObject;
break;
}
}
AXUIElementRef ret = [result ref];
[result release];
return ret;
}
+ (TestAXObject *) getApplicationAXObject
{
pid_t pid = getpid();
AXUIElementRef appRef = AXUIElementCreateApplication(pid);
TestAXObject *appObject = [[TestAXObject alloc] initWithAXUIElementRef: appRef];
return appObject;
}
+ (NSInteger)_numberFromValue:(CFTypeRef)value
{
NSInteger number = -1;
if (!CFNumberGetValue((CFNumberRef)value, kCFNumberNSIntegerType, &number))
{
qDebug() << "Could not get NSInteger value out of CFNumberRef";
}
return number;
}
+ (BOOL)_boolFromValue:(CFTypeRef)value
{
return CFBooleanGetValue((CFBooleanRef)value);
}
+ (NSRange)_rangeFromValue:(CFTypeRef)value
{
CFRange cfRange;
NSRange range = NSMakeRange(0, 0);
if (!AXValueGetValue(AXValueRef(value), AXValueType(kAXValueCFRangeType), &cfRange))
qDebug() << "Could not get CFRange value out of AXValueRef";
else if (cfRange.location < 0 || cfRange.length < 0)
qDebug() << "Cannot convert CFRange with negative location or length to NSRange";
else if (static_cast<uintmax_t>(cfRange.location) > NSUIntegerMax || static_cast<uintmax_t>(cfRange.length) > NSUIntegerMax)
qDebug() << "Cannot convert CFRange with location or length out of bounds for NSUInteger";
else
{
range.length = static_cast<NSUInteger>(cfRange.length);
range.location = static_cast<NSUInteger>(cfRange.location);
}
return range;
}
+ (NSRect)_rectFromValue:(CFTypeRef)value
{
NSRect rect = NSMakeRect(0, 0, 0, 0);
if (!AXValueGetValue(AXValueRef(value), AXValueType(kAXValueCGRectType), reinterpret_cast<CGRect*>(&rect)))
{
qDebug() << "Could not get CGRect value out of AXValueRef";
}
return rect;
}
+ (NSPoint)_pointFromValue:(CFTypeRef)value
{
NSPoint point = NSMakePoint(0, 0);
if (!AXValueGetValue(AXValueRef(value), AXValueType(kAXValueCGPointType), reinterpret_cast<CGPoint*>(&point)))
{
qDebug() << "Could not get CGPoint value out of AXValueRef";
}
return point;
}
+ (NSSize)_sizeFromValue:(CFTypeRef)value
{
NSSize size = NSMakeSize(0, 0);
if (!AXValueGetValue(AXValueRef(value), AXValueType(kAXValueCGSizeType), reinterpret_cast<CGSize*>(&size)))
{
qDebug() << "Could not get CGSize value out of AXValueRef";
}
return size;
}
- (CFTypeRef)_attributeValue:(CFStringRef)attribute
{
CFTypeRef value = NULL;
AXError err;
if (kAXErrorSuccess != (err = AXUIElementCopyAttributeValue(reference, attribute, &value)))
{
qDebug() << "AXUIElementCopyAttributeValue(" << QString::fromCFString(attribute) << ") returned error = " << AXErrorTag(err);
}
return value;
}
- (NSString*)_stringAttributeValue:(CFStringRef)attribute
{
return (NSString*)[self _attributeValue:attribute];
}
- (NSInteger)_numberAttributeValue:(CFStringRef)attribute
{
return [[self class] _numberFromValue:[self _attributeValue:attribute]];
}
- (BOOL)_boolAttributeValue:(CFStringRef)attribute
{
return [[self class] _boolFromValue:[self _attributeValue:attribute]];
}
- (NSRange)_rangeAttributeValue:(CFStringRef)attribute
{
return [[self class] _rangeFromValue:[self _attributeValue:attribute]];
}
- (NSRect)_rectAttributeValue:(CFStringRef)attribute
{
return [[self class] _rectFromValue:[self _attributeValue:attribute]];
}
- (NSPoint)_pointAttributeValue:(CFStringRef)attribute
{
return [[self class] _pointFromValue:[self _attributeValue:attribute]];
}
- (NSSize)_sizeAttributeValue:(CFStringRef)attribute
{
return [[self class] _sizeFromValue:[self _attributeValue:attribute]];
}
- (CFTypeRef)_attributeValue:(CFStringRef)attribute forParameter:(CFTypeRef)parameter
{
CFTypeRef value = NULL;
AXError err;
if (kAXErrorSuccess != (err = AXUIElementCopyParameterizedAttributeValue(reference, attribute, parameter, &value)))
{
CFStringRef description = CFCopyDescription(parameter);
qDebug() << "AXUIElementCopyParameterizedAttributeValue(" << QString::fromCFString(attribute) << ", parameter=" << QString::fromCFString(description) << ") returned error = " << AXErrorTag(err);
CFRelease(description);
}
return value;
}
- (CFTypeRef)_attributeValue:(CFStringRef)attribute forRange:(NSRange)aRange
{
CFRange cfRange = CFRangeMake(aRange.location, aRange.length);
AXValueRef range = AXValueCreate(AXValueType(kAXValueCFRangeType), &cfRange);
CFTypeRef value = [self _attributeValue:attribute forParameter:range];
CFRelease(range);
return value;
}
- (CFTypeRef)_attributeValue:(CFStringRef)attribute forNumber:(NSInteger)aNumber
{
CFNumberRef number = CFNumberCreate(NULL, kCFNumberNSIntegerType, &aNumber);
CFTypeRef value = [self _attributeValue:attribute forParameter:number];
CFRelease(number);
return value;
}
- (CFTypeRef)_attributeValue:(CFStringRef)attribute forPoint:(CGPoint)aPoint
{
AXValueRef point = AXValueCreate(AXValueType(kAXValueCGPointType), &aPoint);
CFTypeRef value = [self _attributeValue:attribute forParameter:point];
CFRelease(point);
return value;
}
- (NSArray*)actions
{
AXError err;
CFArrayRef actions;
if (kAXErrorSuccess != (err = AXUIElementCopyActionNames(reference, &actions)))
{
qDebug() << "AXUIElementCopyActionNames(...) returned error = " << AXErrorTag(err);
}
return (NSArray*)actions;
}
- (void)performAction:(CFStringRef)action
{
AXError err;
if (kAXErrorSuccess != (err = AXUIElementPerformAction(reference, action)))
{
qDebug() << "AXUIElementPerformAction(" << QString::fromCFString(action) << ") returned error = " << AXErrorTag(err);
}
}
- (NSString*) role { return [self _stringAttributeValue:kAXRoleAttribute]; }
- (NSString*) title { return [self _stringAttributeValue:kAXTitleAttribute]; }
- (NSString*) description { return [self _stringAttributeValue:kAXDescriptionAttribute]; }
- (NSString*) value { return [self _stringAttributeValue:kAXValueAttribute]; }
- (NSInteger) valueNumber { return [self _numberAttributeValue:kAXValueAttribute]; }
- (NSRect) rect
{
NSRect rect;
rect.origin = [self _pointAttributeValue:kAXPositionAttribute];
rect.size = [self _sizeAttributeValue:kAXSizeAttribute];
return rect;
}
- (AXUIElementRef) parent { return (AXUIElementRef)[self _attributeValue:kAXParentAttribute]; }
- (BOOL) focused { return [self _boolAttributeValue:kAXFocusedAttribute]; }
- (NSInteger) numberOfCharacters { return [self _numberAttributeValue:kAXNumberOfCharactersAttribute]; }
- (NSString*) selectedText { return [self _stringAttributeValue:kAXSelectedTextAttribute]; }
- (NSRange) selectedTextRange { return [self _rangeAttributeValue:kAXSelectedTextRangeAttribute]; }
- (NSRange) visibleCharacterRange { return [self _rangeAttributeValue:kAXVisibleCharacterRangeAttribute]; }
- (NSString*) help { return [self _stringAttributeValue:kAXHelpAttribute]; }
- (NSInteger) insertionPointLineNumber { return [self _numberAttributeValue:kAXInsertionPointLineNumberAttribute]; }
- (NSInteger) lineForIndex:(NSInteger)index { return [[self class] _numberFromValue:[self _attributeValue:kAXLineForIndexParameterizedAttribute forNumber:index]]; }
- (NSRange) rangeForLine:(NSInteger)line { return [[self class] _rangeFromValue:[self _attributeValue:kAXRangeForLineParameterizedAttribute forNumber:line]]; }
- (NSString*) stringForRange:(NSRange)range { return (NSString*)[self _attributeValue:kAXStringForRangeParameterizedAttribute forRange:range]; }
- (NSAttributedString*) attributedStringForRange:(NSRange)range { return (NSAttributedString*)[self _attributeValue:kAXAttributedStringForRangeParameterizedAttribute forRange:range]; }
- (NSRect) boundsForRange:(NSRange)range { return [[self class] _rectFromValue:[self _attributeValue:kAXBoundsForRangeParameterizedAttribute forRange:range]]; }
- (NSRange) styleRangeForIndex:(NSInteger)index { return [[self class] _rangeFromValue:[self _attributeValue:kAXStyleRangeForIndexParameterizedAttribute forNumber:index]]; }
@end
QVector<int> notificationList;
void observerCallback(AXObserverRef /*observer*/, AXUIElementRef /*element*/, CFStringRef notification, void *)
{
if ([(NSString*)notification isEqualToString: NSAccessibilityFocusedUIElementChangedNotification])
notificationList.append(QAccessible::Focus);
else if ([(NSString*)notification isEqualToString: NSAccessibilityValueChangedNotification])
notificationList.append(QAccessible::ValueChanged);
else
notificationList.append(-1);
}
class AccessibleTestWindow : public QWidget
{
Q_OBJECT
public:
AccessibleTestWindow()
{
new QHBoxLayout(this);
}
void addWidget(QWidget* widget)
{
layout()->addWidget(widget);
widget->show();
QVERIFY(QTest::qWaitForWindowExposed(widget));
}
void clearChildren()
{
qDeleteAll(children());
new QHBoxLayout(this);
}
};
class tst_QAccessibilityMac : public QObject
{
Q_OBJECT
private Q_SLOTS:
void init();
void cleanup();
void singleWidgetTest();
void lineEditTest();
void hierarchyTest();
void notificationsTest();
void checkBoxTest();
void tableViewTest();
private:
AccessibleTestWindow *m_window;
};
void tst_QAccessibilityMac::init()
{
m_window = new AccessibleTestWindow();
m_window->setWindowTitle("Test window");
m_window->show();
m_window->resize(400, 400);
QVERIFY(QTest::qWaitForWindowExposed(m_window));
}
void tst_QAccessibilityMac::cleanup()
{
delete m_window;
}
void tst_QAccessibilityMac::singleWidgetTest()
{
delete m_window;
m_window = 0;
QLineEdit *le = new QLineEdit();
le->setText("button");
le->show();
QVERIFY(QTest::qWaitForWindowExposed(le));
QCoreApplication::processEvents();
TestAXObject *appObject = [TestAXObject getApplicationAXObject];
QVERIFY(appObject);
QTRY_VERIFY(appObject.windowList.count == 1);
AXUIElementRef windowRef = (AXUIElementRef) [appObject.windowList objectAtIndex: 0];
QVERIFY(windowRef != nil);
TestAXObject *window = [[TestAXObject alloc] initWithAXUIElementRef: windowRef];
AXUIElementRef lineEditRef = [window findDirectChildByRole: kAXTextFieldRole];
QVERIFY(lineEditRef != nil);
TestAXObject *lineEdit = [[TestAXObject alloc] initWithAXUIElementRef: lineEditRef];
QVERIFY([[lineEdit value] isEqualToString:@"button"]);
// Access invalid reference, should return empty value
delete le;
QCoreApplication::processEvents();
TestAXObject *lineEditInvalid = [[TestAXObject alloc] initWithAXUIElementRef: lineEditRef];
QVERIFY([[lineEditInvalid value] length] == 0);
}
void tst_QAccessibilityMac::lineEditTest()
{
QLineEdit *lineEdit = new QLineEdit(m_window);
lineEdit->setText("a11y test QLineEdit");
m_window->addWidget(lineEdit);
QVERIFY(QTest::qWaitForWindowExposed(m_window));
QCoreApplication::processEvents();
TestAXObject *appObject = [TestAXObject getApplicationAXObject];
QVERIFY(appObject);
// one window
QTRY_VERIFY(appObject.windowList.count == 1);
AXUIElementRef windowRef = (AXUIElementRef) [appObject.windowList objectAtIndex: 0];
QVERIFY(windowRef != nil);
TestAXObject *window = [[TestAXObject alloc] initWithAXUIElementRef: windowRef];
QVERIFY([window rect].size.width == 400);
// height of window includes title bar
QVERIFY([window rect].size.height >= 400);
QVERIFY([window.title isEqualToString:@"Test window"]);
// children of window:
AXUIElementRef lineEditElement = [window findDirectChildByRole: kAXTextFieldRole];
QVERIFY(lineEditElement != nil);
TestAXObject *le = [[TestAXObject alloc] initWithAXUIElementRef: lineEditElement];
NSString *value = @"a11y test QLineEdit";
QVERIFY([le.value isEqualToString:value]);
QVERIFY(value.length <= NSIntegerMax);
QVERIFY(le.numberOfCharacters == static_cast<NSInteger>(value.length));
const NSRange ranges[] = {
{ 0, 0},
{ 0, 1},
{ 0, 5},
{ 5, 0},
{ 5, 1},
{ 0, value.length},
{ value.length, 0},
};
for (size_t i = 0; i < sizeof(ranges)/sizeof(ranges[0]); ++i) {
NSRange range = ranges[i];
NSString *expectedSubstring = [value substringWithRange:range];
NSString *actualSubstring = [le stringForRange:range];
NSString *actualAttributedSubstring = [le attributedStringForRange:range].string;
QVERIFY([actualSubstring isEqualTo:expectedSubstring]);
QVERIFY([actualAttributedSubstring isEqualTo:expectedSubstring]);
}
}
void tst_QAccessibilityMac::hierarchyTest()
{
QWidget *w = new QWidget(m_window);
m_window->addWidget(w);
w->setLayout(new QVBoxLayout());
QPushButton *b = new QPushButton(w);
w->layout()->addWidget(b);
b->setText("I am a button");
QPushButton *b2 = new QPushButton(w);
w->layout()->addWidget(b2);
b2->setText("Button 2");
QVERIFY(QTest::qWaitForWindowExposed(m_window));
QCoreApplication::processEvents();
TestAXObject *appObject = [TestAXObject getApplicationAXObject];
QVERIFY(appObject);
// one window
QTRY_VERIFY(appObject.windowList.count == 1);
AXUIElementRef windowRef = (AXUIElementRef) [appObject.windowList objectAtIndex: 0];
QVERIFY(windowRef != nil);
TestAXObject *window = [[TestAXObject alloc] initWithAXUIElementRef: windowRef];
// Because the plain widget is filtered out of the hierarchy, we expect the button
// to be a direct child of the window
AXUIElementRef buttonRef = [window findDirectChildByRole: kAXButtonRole];
QVERIFY(buttonRef != nil);
TestAXObject *buttonObject = [[TestAXObject alloc] initWithAXUIElementRef: buttonRef];
TestAXObject *parentObject = [[TestAXObject alloc] initWithAXUIElementRef: [buttonObject parent]];
// check that the parent is a window
QVERIFY([[parentObject role] isEqualToString: NSAccessibilityWindowRole]);
// test the focus
// child 0 is the layout, then button1 and 2
QPushButton *button1 = qobject_cast<QPushButton*>(w->children().at(1));
QVERIFY(button1);
QPushButton *button2 = qobject_cast<QPushButton*>(w->children().at(2));
QVERIFY(button2);
button2->setFocus();
AXUIElementRef systemWideElement = AXUIElementCreateSystemWide();
AXUIElementRef focussedElement = NULL;
AXError error = AXUIElementCopyAttributeValue(systemWideElement,
(CFStringRef)NSAccessibilityFocusedUIElementAttribute, (CFTypeRef*)&focussedElement);
QVERIFY(!error);
QVERIFY(focussedElement);
TestAXObject *focusButton2 = [[TestAXObject alloc] initWithAXUIElementRef: focussedElement];
QVERIFY([[focusButton2 role] isEqualToString: NSAccessibilityButtonRole]);
QVERIFY([[focusButton2 title] isEqualToString: @"Button 2"]);
button1->setFocus();
error = AXUIElementCopyAttributeValue(systemWideElement,
(CFStringRef)NSAccessibilityFocusedUIElementAttribute, (CFTypeRef*)&focussedElement);
QVERIFY(!error);
QVERIFY(focussedElement);
TestAXObject *focusButton1 = [[TestAXObject alloc] initWithAXUIElementRef: focussedElement];
QVERIFY([[focusButton1 role] isEqualToString: NSAccessibilityButtonRole]);
QVERIFY([[focusButton1 title] isEqualToString: @"I am a button"]);
}
void tst_QAccessibilityMac::notificationsTest()
{
auto *w = m_window;
QLineEdit *le1 = new QLineEdit(w);
QLineEdit *le2 = new QLineEdit(w);
w->layout()->addWidget(le1);
w->layout()->addWidget(le2);
QCoreApplication::processEvents();
QTest::qWait(100);
TestAXObject *appObject = [TestAXObject getApplicationAXObject];
QVERIFY(appObject);
// one window
QTRY_VERIFY(appObject.windowList.count == 1);
AXUIElementRef windowRef = (AXUIElementRef) [appObject.windowList objectAtIndex: 0];
QVERIFY(windowRef != nil);
TestAXObject *window = [[TestAXObject alloc] initWithAXUIElementRef: windowRef];
AXUIElementRef lineEdit1 = [window findDirectChildByRole: kAXTextFieldRole];
QVERIFY(lineEdit1 != nil);
AXObserverRef observer = 0;
AXError err = AXObserverCreate(getpid(), observerCallback, &observer);
QVERIFY(!err);
AXObserverAddNotification(observer, appObject.ref, kAXFocusedUIElementChangedNotification, 0);
AXObserverAddNotification(observer, lineEdit1, kAXValueChangedNotification, 0);
CFRunLoopAddSource( [[NSRunLoop currentRunLoop] getCFRunLoop], AXObserverGetRunLoopSource(observer), kCFRunLoopDefaultMode);
QVERIFY(notificationList.length() == 0);
le2->setFocus();
QTRY_VERIFY(notificationList.length() == 1);
QTRY_VERIFY(notificationList.at(0) == QAccessible::Focus);
le1->setFocus();
QTRY_VERIFY(notificationList.length() == 2);
QTRY_VERIFY(notificationList.at(1) == QAccessible::Focus);
le1->setText("hello");
QTRY_VERIFY(notificationList.length() == 3);
QTRY_VERIFY(notificationList.at(2) == QAccessible::ValueChanged);
le1->setText("foo");
QTRY_VERIFY(notificationList.length() == 4);
QTRY_VERIFY(notificationList.at(3) == QAccessible::ValueChanged);
}
void tst_QAccessibilityMac::checkBoxTest()
{
QCheckBox *ckBox = new QCheckBox(m_window);
ckBox->setText("Great option");
m_window->addWidget(ckBox);
QVERIFY(QTest::qWaitForWindowExposed(m_window));
QCoreApplication::processEvents();
TestAXObject *appObject = [TestAXObject getApplicationAXObject];
QVERIFY(appObject);
// one window
QTRY_VERIFY(appObject.windowList.count == 1);
AXUIElementRef windowRef = (AXUIElementRef) [appObject.windowList objectAtIndex: 0];
QVERIFY(windowRef != nil);
TestAXObject *window = [[TestAXObject alloc] initWithAXUIElementRef: windowRef];
// children of window:
AXUIElementRef checkBox = [window findDirectChildByRole: kAXCheckBoxRole];
QVERIFY(checkBox != nil);
TestAXObject *cb = [[TestAXObject alloc] initWithAXUIElementRef: checkBox];
// here start actual checkbox tests
QVERIFY([cb valueNumber] == 0);
QVERIFY([cb.title isEqualToString:@"Great option"]);
// EXPECT(cb.description == nil); // currently returns "" instead of nil
QVERIFY([cb.actions containsObject:(NSString*)kAXPressAction]);
[cb performAction:kAXPressAction];
QVERIFY([cb valueNumber] == 1);
[cb performAction:kAXPressAction];
QVERIFY([cb valueNumber] == 0);
ckBox->setCheckState(Qt::PartiallyChecked);
QVERIFY([cb valueNumber] == 2);
}
void tst_QAccessibilityMac::tableViewTest()
{
QTableWidget *tw = new QTableWidget(3, 2, m_window);
struct Person
{
const char *name;
const char *address;
};
const Person contents[] = { { "Socrates", "Greece" },
{ "Confucius", "China" },
{ "Kant", "Preussia" }
};
for (int i = 0; i < int(sizeof(contents) / sizeof(Person)); ++i) {
Person p = contents[i];
QTableWidgetItem *name = new QTableWidgetItem(QString::fromLatin1(p.name));
tw->setItem(i, 0, name);
QTableWidgetItem *address = new QTableWidgetItem(QString::fromLatin1(p.address));
tw->setItem(i, 1, address);
}
m_window->addWidget(tw);
QVERIFY(QTest::qWaitForWindowExposed(m_window));
QCoreApplication::processEvents();
TestAXObject *appObject = [TestAXObject getApplicationAXObject];
QVERIFY(appObject);
NSArray *windowList = [appObject windowList];
// one window
QVERIFY([windowList count] == 1);
AXUIElementRef windowRef = (AXUIElementRef)[windowList objectAtIndex:0];
QVERIFY(windowRef != nil);
TestAXObject *window = [[TestAXObject alloc] initWithAXUIElementRef:windowRef];
// children of window:
AXUIElementRef tableView = [window findDirectChildByRole:kAXTableRole];
QVERIFY(tableView != nil);
TestAXObject *tv = [[TestAXObject alloc] initWithAXUIElementRef:tableView];
// here start actual tableview tests
// Should have 2 columns
const unsigned int columnCount = 2;
NSArray *columnArray = [tv tableColumns];
QCOMPARE([columnArray count], columnCount);
// should have 3 rows
const unsigned int rowCount = 3;
NSArray *rowArray = [tv tableRows];
QCOMPARE([rowArray count], rowCount);
// The individual cells are children of the rows
TestAXObject *row = [[TestAXObject alloc] initWithAXUIElementRef:(AXUIElementRef)rowArray[0]];
TestAXObject *cell = [[TestAXObject alloc] initWithAXUIElementRef:(AXUIElementRef)[row childList][0]];
QVERIFY([cell.title isEqualToString:@"Socrates"]);
row = [[TestAXObject alloc] initWithAXUIElementRef:(AXUIElementRef)rowArray[2]];
cell = [[TestAXObject alloc] initWithAXUIElementRef:(AXUIElementRef)[row childList][1]];
QVERIFY([cell.title isEqualToString:@"Preussia"]);
// both rows and columns are direct children of the table
NSArray *childList = [tv childList];
QCOMPARE([childList count], columnCount + rowCount);
for (id child in childList) {
TestAXObject *childObject = [[TestAXObject alloc] initWithAXUIElementRef:(AXUIElementRef)child];
QVERIFY([childObject.role isEqualToString:NSAccessibilityRowRole] ||
[childObject.role isEqualToString:NSAccessibilityColumnRole]);
}
}
QTEST_MAIN(tst_QAccessibilityMac)
#include "tst_qaccessibilitymac.moc"

View File

@ -0,0 +1,42 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qcomplextext Test:
#####################################################################
# Collect test data
list(APPEND test_data "data")
qt_internal_add_test(tst_qcomplextext
SOURCES
tst_qcomplextext.cpp
LIBRARIES
Qt::CorePrivate
Qt::Gui
Qt::GuiPrivate
TESTDATA ${test_data}
)
## Scopes:
#####################################################################
if(ANDROID)
# Resources:
set(android_testdata_resource_files
"data/BidiCharacterTest.txt"
"data/BidiTest.txt"
)
qt_internal_add_resource(tst_qcomplextext "android_testdata"
PREFIX
"/android_testdata"
FILES
${android_testdata_resource_files}
)
endif()
qt_internal_extend_target(tst_qcomplextext CONDITION builtin_testdata
DEFINES
BUILTIN_TESTDATA
)

View File

@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/android_testdata">
<file>data/BidiCharacterTest.txt</file>
<file>data/BidiTest.txt</file>
</qresource>
</RCC>

View File

@ -0,0 +1,142 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
struct LV {
const char *name;
const char *logical;
const char *visual;
int basicDir;
};
const LV logical_visual[] = {
{ "data0", "Hello", "Hello", QChar::DirL },
{ "data1", "\327\251\327\234\327\225\327\235", "\327\235\327\225\327\234\327\251", QChar::DirR },
{ "data2", "Hello \327\251\327\234\327\225\327\235", "Hello \327\235\327\225\327\234\327\251", QChar::DirL },
{ "data3", "car is \327\236\327\233\327\225\327\240\327\231\327\252 \327\220\327\225\327\230\327\225 in hebrew", "car is \327\225\327\230\327\225\327\220 \327\252\327\231\327\240\327\225\327\233\327\236 in hebrew", QChar::DirL },
{ "data4", "\327\236\327\233\327\225\327\240\327\231\327\252 \327\224\327\231\327\220 the car \327\221\327\251\327\244\327\224 \327\224\327\220\327\240\327\222\327\234\327\231\327\252", "\327\252\327\231\327\234\327\222\327\240\327\220\327\224 \327\224\327\244\327\251\327\221 the car \327\220\327\231\327\224 \327\252\327\231\327\240\327\225\327\233\327\236", QChar::DirR },
{ "data5", "he said \"\327\226\327\224 \327\251\327\225\327\225\327\224 123, 456, \327\221\327\241\327\223\327\250\" ", "he said \"\327\250\327\223\327\241\327\221 ,456 ,123 \327\224\327\225\327\225\327\251 \327\224\327\226\" ", QChar::DirL },
{ "data6", "he said \"\327\226\327\224 \327\251\327\225\327\225\327\224 (123, 456), \327\221\327\241\327\223\327\250\"", "he said \"\327\250\327\223\327\241\327\221 ,(456 ,123) \327\224\327\225\327\225\327\251 \327\224\327\226\"", QChar::DirL },
{ "data7", "he said \"\327\226\327\224 \327\251\327\225\327\225\327\224 123,456, \327\221\327\241\327\223\327\250\"", "he said \"\327\250\327\223\327\241\327\221 ,123,456 \327\224\327\225\327\225\327\251 \327\224\327\226\"", QChar::DirL },
{ "data8", "he said \"\327\226\327\224 \327\251\327\225\327\225\327\224 ,(123,456) \327\221\327\241\327\223\327\250\"", "he said \"\327\250\327\223\327\241\327\221 (123,456), \327\224\327\225\327\225\327\251 \327\224\327\226\"", QChar::DirL },
{ "data9", "\327\224\327\225\327\220 \327\220\327\236\327\250 \"it is 123, 456, ok\"", "\"it is 123, 456, ok\" \327\250\327\236\327\220 \327\220\327\225\327\224", QChar::DirR },
{ "data10", "<\327\233123>shalom</\327\233123>", "<123\327\233/>shalom<123\327\233>", QChar::DirR },
{ "data11", "<h123>\327\251\327\234\327\225\327\235</h123>", "<h123>\327\235\327\225\327\234\327\251</h123>", QChar::DirL },
{ "data12", "\327\224\327\225\327\220 \327\220\327\236\327\250 \"it is a car!\" \327\225\327\220\327\226 \327\250\327\245", "\327\245\327\250 \327\226\327\220\327\225 \"!it is a car\" \327\250\327\236\327\220 \327\220\327\225\327\224", QChar::DirR },
{ "data13", "\327\224\327\225\327\220 \327\220\327\236\327\250 \"it is a car!x\" \327\225\327\220\327\226 \327\250\327\245", "\327\245\327\250 \327\226\327\220\327\225 \"it is a car!x\" \327\250\327\236\327\220 \327\220\327\225\327\224", QChar::DirR },
{ "data14", "-2 \327\236\327\242\327\234\327\225\327\252 \327\226\327\224 \327\247\327\250", "\327\250\327\247 \327\224\327\226 \327\252\327\225\327\234\327\242\327\236 2-", QChar::DirR },
{ "data15", "-10% \327\251\327\231\327\240\327\225\327\231", "\327\231\327\225\327\240\327\231\327\251 10%-", QChar::DirR },
{ "data16", "\327\224\327\230\327\225\327\225\327\227 \327\224\327\231\327\240\327\225 2.5..5", "5..2.5 \327\225\327\240\327\231\327\224 \327\227\327\225\327\225\327\230\327\224", QChar::DirR },
{ "data17", "he said \"\327\226\327\225 \327\236\327\233\327\225\327\240\327\231\327\252!\"", "he said \"\327\252\327\231\327\240\327\225\327\233\327\236 \327\225\327\226!\"", QChar::DirL },
{ "data18", "he said \"\327\226\327\225 \327\236\327\233\327\225\327\240\327\231\327\252!\327\251\"", "he said \"\327\251!\327\252\327\231\327\240\327\225\327\233\327\236 \327\225\327\226\"", QChar::DirL },
{ "data19", "(\327\240\327\231\327\241\327\225\327\231) abc", "abc (\327\231\327\225\327\241\327\231\327\240)", QChar::DirR },
{ "data20", "abc (\327\240\327\231\327\241\327\225\327\231)", "abc (\327\231\327\225\327\241\327\231\327\240)", QChar::DirL },
{ "data21", "\327\240\327\231\327\241\327\225\327\231 23 \327\231\327\227\327\231\327\223 abc", "abc \327\223\327\231\327\227\327\231 23 \327\231\327\225\327\241\327\231\327\240", QChar::DirR },
{ "data22", "#@$ \327\240\327\231\327\241\327\225\327\231", "\327\231\327\225\327\241\327\231\327\240 $@#", QChar::DirR },
{ "data23", "\327\240\327\231\327\241\327\225\327\231 ~~~23%%% \327\231\327\227\327\231\327\223 abc ", " abc \327\223\327\231\327\227\327\231 23%%%~~~ \327\231\327\225\327\241\327\231\327\240", QChar::DirR },
{ "data24", "\327\240\327\231\327\241\327\225\327\231 abc ~~~23%%% \327\231\327\227\327\231\327\223 abc", "abc \327\223\327\231\327\227\327\231 abc ~~~23%%% \327\231\327\225\327\241\327\231\327\240", QChar::DirR },
{ "data25", "\327\240\327\231\327\241\327\225\327\231 abc@23@cde \327\231\327\227\327\231\327\223", "\327\223\327\231\327\227\327\231 abc@23@cde \327\231\327\225\327\241\327\231\327\240", QChar::DirR },
{ "data26", "\327\240\327\231\327\241\327\225\327\231 abc 23 cde \327\231\327\227\327\231\327\223", "\327\223\327\231\327\227\327\231 abc 23 cde \327\231\327\225\327\241\327\231\327\240", QChar::DirR },
{ "data27", "\327\240\327\231\327\241\327\225\327\231 abc 23 \327\231\327\227\327\231\327\223 cde", "cde \327\223\327\231\327\227\327\231 abc 23 \327\231\327\225\327\241\327\231\327\240", QChar::DirR },
{ "data28", "\327\222a 2 \327\251", "\327\251 a 2\327\222", QChar::DirR },
{ "data29", "\327\244\327\252\327\225\327\250 1*5 1-5 1/5 1+5", "1+5 1/5 1-5 5*1 \327\250\327\225\327\252\327\244", QChar::DirR },
{ "data30", "\330\272 1*5 1-5 1/5 1+5", "5+1 1/5 5-1 5*1 \330\272", QChar::DirR },
{ "data31", "\330\250 \333\261\333\262.\333\263", "\333\261\333\262.\333\263 \330\250", QChar::DirR },
{ "data32", "\330\250 12.3", "12.3 \330\250", QChar::DirR },
{ "data33", "\330\250 1.23", "1.23 \330\250", QChar::DirR },
{ "data34", "\331\276 \333\261.\333\262\333\263", "\333\261.\333\262\333\263 \331\276", QChar::DirR },
{ "data35", "\331\276\333\261.\333\262\333\263", "\333\261.\333\262\333\263\331\276", QChar::DirR },
{ "data36", "1) \327\251", "\327\251 (1", QChar::DirR },
{ "data37", "1) \327\251", "1) \327\251", QChar::DirL },
{ "data38", "\327\224-w3c", "w3c-\327\224", QChar::DirR },
{ "data39", "\327\224-w3c", "\327\224-w3c", QChar::DirL },
{ "data40", "17:25, foo", "17:25, foo", QChar::DirL },
{ "data41", "5, foo", "foo ,5", QChar::DirR },
{ "data42", "foo\nfoo", "foo\nfoo", QChar::DirL },
{ "data43", "\327\251\327\234\327\225\327\235\n\327\251\327\234\327\225\327\235", "\327\235\327\225\327\234\327\251\n\327\235\327\225\327\234\327\251", QChar::DirR },
{ "data44", "foo\n\327\251\327\234\327\225\327\235", "foo\n\327\235\327\225\327\234\327\251", QChar::DirL },
{ "data45", "\327\251\327\234\327\225\327\235\nfoo", "foo\n\327\235\327\225\327\234\327\251", QChar::DirR },
{ "data46", "\330\250 1.23 \330\250", "\330\250 1.23 \330\250", QChar::DirR },
{ "data47", "\331\204\330\250 1.23 \331\202\330\250", "\330\250\331\202 1.23 \330\250\331\204", QChar::DirR },
{ "data48", "\330\250 1.2 \330\250", "\330\250 1.2 \330\250", QChar::DirR },
{ "data49", "\331\204\330\250 1.2 \331\202\330\250", "\330\250\331\202 1.2 \330\250\331\204", QChar::DirR },
{ "data50", "a\331\204\330\250 1.2 \331\202\330\250", "a\330\250\331\202 1.2 \330\250\331\204", QChar::DirL },
{ "data51", "ab(\327\240\327\231)cd", "ab(\327\231\327\240)cd", QChar::DirL },
{ "data52", "ab(\327\240\327\231)cd", "cd(\327\231\327\240)ab", QChar::DirR },
{ "data53", "a(\327\231)c", "a(\327\231)c", QChar::DirL },
{ "data54", "a(\327\231)c", "c(\327\231)a", QChar::DirR },
{ "data55", "\"[\327\220]\"", "\"[\327\220]\"", QChar::DirR },
{ "data56", "\"[\327\220]\"", "\"[\327\220]\"", QChar::DirL },
{ "data57", "\331\204\330\250 \331\202\330\250", "\330\250\331\202 \330\250\331\204", QChar::DirR },
{ "data58", "\331\204\330\250 \331\202\330\250", "\330\250\331\202 \330\250\331\204", QChar::DirL },
{ "data59", "3layout", "3layout", QChar::DirL },
{ "data60", "3layout", "3layout", QChar::DirR },
{ "data61", "3l", "3l", QChar::DirR },
{ "data62", "3la", "3la", QChar::DirR },
{ "data63", "3lay", "3lay", QChar::DirR },
// explicit levels
// LRE: \342\200\252
// RLE: \342\200\253
// PDF: \342\200\254
// LRO: \342\200\255
// RLO: \342\200\256
{ "override1", "\342\200\256hello\342\200\254", "\342\200\256olleh\342\200\254", QChar::DirL },
{ "override2", "\342\200\255hello\342\200\254", "\342\200\255hello\342\200\254", QChar::DirL },
{ "override3", "\342\200\255\327\251\327\234\327\225\327\235\342\200\254", "\342\200\255\327\251\327\234\327\225\327\235\342\200\254", QChar::DirL },
{ "override4", "\342\200\256\327\251\327\234\327\225\327\235\342\200\254", "\342\200\256\327\235\327\225\327\234\327\251\342\200\254", QChar::DirL },
{ "override5", "\342\200\256hello\342\200\254", "\342\200\254olleh\342\200\256", QChar::DirR },
{ "override6", "\342\200\255hello\342\200\254", "\342\200\254hello\342\200\255", QChar::DirR },
{ "override7", "\342\200\255\327\251\327\234\327\225\327\235\342\200\254", "\342\200\254\327\251\327\234\327\225\327\235\342\200\255", QChar::DirR },
{ "override8", "\342\200\256\327\251\327\234\327\225\327\235\342\200\254", "\342\200\254\327\235\327\225\327\234\327\251\342\200\256", QChar::DirR },
{ "override9", "\327\224\342\200\255\327\251\327\234\342\200\256hello\342\200\254\327\225\327\235\342\200\254",
"\327\251\327\234\342\200\256olleh\342\200\254\327\225\327\235\342\200\255\327\224\342\200\254", QChar::DirL },
{ "override10", "\327\224\342\200\255\327\251\327\234\342\200\256hello\342\200\254\327\225\327\235\342\200\254",
"\342\200\254\327\251\327\234\342\200\256olleh\342\200\254\327\225\327\235\342\200\255\327\224", QChar::DirR },
{ "embed1", "\342\200\252hello\342\200\254", "\342\200\252hello\342\200\254", QChar::DirL },
{ "embed2", "\342\200\253hello\342\200\254", "\342\200\253hello\342\200\254", QChar::DirL },
{ "embed3", "\342\200\252hello\342\200\254", "\342\200\254hello\342\200\252", QChar::DirR },
{ "embed4", "\342\200\253hello\342\200\254", "\342\200\254hello\342\200\253", QChar::DirR },
{ "embed5", "\342\200\252\327\251\327\234\327\225\327\235\342\200\254", "\342\200\252\327\235\327\225\327\234\327\251\342\200\254", QChar::DirL },
{ "embed6", "\342\200\253\327\251\327\234\327\225\327\235\342\200\254", "\342\200\253\327\235\327\225\327\234\327\251\342\200\254", QChar::DirL },
{ "embed7", "\342\200\252\327\251\327\234\327\225\327\235\342\200\254", "\342\200\254\327\235\327\225\327\234\327\251\342\200\252", QChar::DirR },
{ "embed8", "\342\200\253\327\251\327\234\327\225\327\235\342\200\254", "\342\200\254\327\235\327\225\327\234\327\251\342\200\253", QChar::DirR },
{ "embed9", "\342\200\252x \327\251\327\234\327\225\327\235 y\342\200\254", "\342\200\252x \327\235\327\225\327\234\327\251 y\342\200\254", QChar::DirL },
{ "embed10", "\342\200\253x \327\251\327\234\327\225\327\235 y\342\200\254", "\342\200\253y \327\235\327\225\327\234\327\251 x\342\200\254", QChar::DirL },
{ "embed11", "\342\200\252x \327\251\327\234\327\225\327\235 y\342\200\254", "\342\200\254x \327\235\327\225\327\234\327\251 y\342\200\252", QChar::DirR },
{ "embed12", "\342\200\253x \327\251\327\234\327\225\327\235 y\342\200\254", "\342\200\254y \327\235\327\225\327\234\327\251 x\342\200\253", QChar::DirR },
{ "zwsp", "+0\342\200\213f-1", "+0\342\200\213f-1", QChar::DirL },
// Alef: \xD7\x90
{ "bracketpair_1_ltr", "\xD7\x90(\xD7\x90[&a]!)a", "\xD7\x90(\xD7\x90[&a]!)a", QChar::DirL },
{ "bracketpair_1_rtl", "\xD7\x90(\xD7\x90[&a]!)a", "a(![a&]\xD7\x90)\xD7\x90", QChar::DirR },
{ "bracketpair_2_ltr", "a(\xD7\x90[&a]!)\xD7\x90", "a(\xD7\x90[&a]!)\xD7\x90", QChar::DirL },
{ "bracketpair_2_rtl", "a(\xD7\x90[&a]!)\xD7\x90", "\xD7\x90(![a&]\xD7\x90)a", QChar::DirR },
{ "bracketpair_3_ltr", "\xD7\x90(a[&\xD7\x90]!)a", "\xD7\x90(a[&\xD7\x90]!)a", QChar::DirL },
{ "bracketpair_3_rtl", "\xD7\x90(a[&\xD7\x90]!)a", "a(![\xD7\x90&]a)\xD7\x90", QChar::DirR },
{ "bracketpair_4_ltr", "a (a \xD7\x90) \xD7\x90", "a (a \xD7\x90) \xD7\x90", QChar::DirL },
{ "bracketpair_4_rtl", "a (a \xD7\x90) \xD7\x90", "\xD7\x90 (\xD7\x90 a) a", QChar::DirR },
{ "bracketpair_5_ltr", "\xD7\x90 (a \xD7\x90) a", "\xD7\x90 (a \xD7\x90) a", QChar::DirL },
{ "bracketpair_5_rtl", "\xD7\x90 (a \xD7\x90) a", "a (\xD7\x90 a) \xD7\x90", QChar::DirR },
{ "bracketpair_6_ltr", "a (\xD7\x90 a) \xD7\x90", "a (\xD7\x90 a) \xD7\x90", QChar::DirL },
{ "bracketpair_6_rtl", "a (\xD7\x90 a) \xD7\x90", "\xD7\x90 (a \xD7\x90) a", QChar::DirR },
{ "bracketpair_7_ltr", "\xD7\x90\xD7\x90 book(s)", "\xD7\x90\xD7\x90 book(s)", QChar::DirL },
{ "bracketpair_7_rtl", "\xD7\x90\xD7\x90 book(s)", "book(s) \xD7\x90\xD7\x90", QChar::DirR },
{ "bracketpair_8_ltr", "a \xD7\x90\xD7\x90(\xD7\x90)", "a (\xD7\x90)\xD7\x90\xD7\x90", QChar::DirL },
{ "bracketpair_8_rtl", "a \xD7\x90\xD7\x90(\xD7\x90)", "(\xD7\x90)\xD7\x90\xD7\x90 a", QChar::DirR },
{ 0, 0, 0, QChar::DirON }
};

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,515 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTest>
#include <QtGui/QtGui>
#include <private/qtextengine_p.h>
#include "bidireorderstring.h"
class tst_QComplexText : public QObject
{
Q_OBJECT
private slots:
void bidiReorderString_data();
void bidiReorderString();
void bidiCursor_qtbug2795();
void bidiCursor_PDF();
void bidiCursorMovement_data();
void bidiCursorMovement();
void bidiCursorLogicalMovement_data();
void bidiCursorLogicalMovement();
void bidiInvalidCursorNoMovement_data();
void bidiInvalidCursorNoMovement();
void bidiCharacterTest();
void bidiTest();
};
void tst_QComplexText::bidiReorderString_data()
{
QTest::addColumn<QString>("logical");
QTest::addColumn<QString>("VISUAL");
QTest::addColumn<int>("basicDir");
const LV *data = logical_visual;
while ( data->name ) {
//next we fill it with data
QTest::newRow( data->name )
<< QString::fromUtf8( data->logical )
<< QString::fromUtf8( data->visual )
<< (int) data->basicDir;
QTest::newRow( QByteArray(data->name) + " (doubled)" )
<< (QString::fromUtf8( data->logical ) + QChar(QChar::ParagraphSeparator) + QString::fromUtf8( data->logical ))
<< (QString::fromUtf8( data->visual ) + QChar(QChar::ParagraphSeparator) + QString::fromUtf8( data->visual ))
<< (int) data->basicDir;
data++;
}
QString isolateAndBoundary = QString(QChar(0x2068 /* DirFSI */)) + QChar(0x1c /* DirB */) + QChar(0x2069 /* DirPDI */);
QTest::newRow( "isolateAndBoundary" )
<< QString::fromUtf8( data->logical )
<< QString::fromUtf8( data->visual )
<< (int) QChar::DirL;
}
void tst_QComplexText::bidiReorderString()
{
QFETCH( QString, logical );
QFETCH( int, basicDir );
// replace \n with Unicode newline. The new algorithm ignores \n
logical.replace(QChar('\n'), QChar(0x2028));
QTextEngine e(logical, QFont());
e.option.setTextDirection((QChar::Direction)basicDir == QChar::DirL ? Qt::LeftToRight : Qt::RightToLeft);
e.itemize();
quint8 levels[256];
int visualOrder[256];
int nitems = e.layoutData->items.size();
int i;
for (i = 0; i < nitems; ++i) {
//qDebug("item %d bidiLevel=%d", i, e.items[i].analysis.bidiLevel);
levels[i] = e.layoutData->items[i].analysis.bidiLevel;
}
e.bidiReorder(nitems, levels, visualOrder);
QString visual;
for (i = 0; i < nitems; ++i) {
QScriptItem &si = e.layoutData->items[visualOrder[i]];
QString sub = logical.mid(si.position, e.length(visualOrder[i]));
if (si.analysis.bidiLevel % 2) {
// reverse sub
QChar *a = sub.data();
QChar *b = a + sub.size() - 1;
while (a < b) {
QChar tmp = *a;
*a = *b;
*b = tmp;
++a;
--b;
}
a = (QChar *)sub.unicode();
b = a + sub.size();
while (a<b) {
*a = a->mirroredChar();
++a;
}
}
visual += sub;
}
// replace Unicode newline back with \n to compare.
visual.replace(QChar(0x2028), QChar('\n'));
QTEST(visual, "VISUAL");
}
void tst_QComplexText::bidiCursor_qtbug2795()
{
QString str = QString::fromUtf8("الجزيرة نت");
QTextLayout l1(str);
l1.beginLayout();
l1.setCacheEnabled(true);
QTextLine line1 = l1.createLine();
l1.endLayout();
qreal x1 = line1.cursorToX(0) - line1.cursorToX(str.size());
str.append("1");
QTextLayout l2(str);
l2.setCacheEnabled(true);
l2.beginLayout();
QTextLine line2 = l2.createLine();
l2.endLayout();
qreal x2 = line2.cursorToX(0) - line2.cursorToX(str.size());
// The cursor should remain at the same position after a digit is appended
QCOMPARE(x1, x2);
}
void tst_QComplexText::bidiCursorMovement_data()
{
QTest::addColumn<QString>("logical");
QTest::addColumn<int>("basicDir");
const LV *data = logical_visual;
while ( data->name ) {
//next we fill it with data
QTest::newRow( data->name )
<< QString::fromUtf8( data->logical )
<< (int) data->basicDir;
data++;
}
}
void tst_QComplexText::bidiCursorMovement()
{
QFETCH(QString, logical);
QFETCH(int, basicDir);
QTextLayout layout(logical);
layout.setCacheEnabled(true);
QTextOption option = layout.textOption();
option.setTextDirection(basicDir == QChar::DirL ? Qt::LeftToRight : Qt::RightToLeft);
layout.setTextOption(option);
layout.setCursorMoveStyle(Qt::VisualMoveStyle);
bool moved;
int oldPos, newPos = 0;
qreal x, newX;
layout.beginLayout();
QTextLine line = layout.createLine();
layout.endLayout();
newX = line.cursorToX(0);
do {
oldPos = newPos;
x = newX;
newX = line.cursorToX(oldPos);
if (basicDir == QChar::DirL) {
QVERIFY(newX >= x);
newPos = layout.rightCursorPosition(oldPos);
} else
{
QVERIFY(newX <= x);
newPos = layout.leftCursorPosition(oldPos);
}
moved = (oldPos != newPos);
} while (moved);
}
void tst_QComplexText::bidiCursorLogicalMovement_data()
{
bidiCursorMovement_data();
}
void tst_QComplexText::bidiCursorLogicalMovement()
{
QFETCH(QString, logical);
QFETCH(int, basicDir);
QTextLayout layout(logical);
QTextOption option = layout.textOption();
option.setTextDirection(basicDir == QChar::DirL ? Qt::LeftToRight : Qt::RightToLeft);
layout.setTextOption(option);
bool moved;
int oldPos, newPos = 0;
do {
oldPos = newPos;
newPos = layout.nextCursorPosition(oldPos);
QVERIFY(newPos >= oldPos);
moved = (oldPos != newPos);
} while (moved);
do {
oldPos = newPos;
newPos = layout.previousCursorPosition(oldPos);
QVERIFY(newPos <= oldPos);
moved = (oldPos != newPos);
} while (moved);
}
void tst_QComplexText::bidiInvalidCursorNoMovement_data()
{
bidiCursorMovement_data();
}
void tst_QComplexText::bidiInvalidCursorNoMovement()
{
QFETCH(QString, logical);
QFETCH(int, basicDir);
QTextLayout layout(logical);
QTextOption option = layout.textOption();
option.setTextDirection(basicDir == QChar::DirL ? Qt::LeftToRight : Qt::RightToLeft);
layout.setTextOption(option);
// visual
QCOMPARE(layout.rightCursorPosition(-1000), -1000);
QCOMPARE(layout.rightCursorPosition(1000), 1000);
QCOMPARE(layout.leftCursorPosition(-1000), -1000);
QCOMPARE(layout.leftCursorPosition(1000), 1000);
// logical
QCOMPARE(layout.nextCursorPosition(-1000), -1000);
QCOMPARE(layout.nextCursorPosition(1000), 1000);
QCOMPARE(layout.previousCursorPosition(-1000), -1000);
QCOMPARE(layout.previousCursorPosition(1000), 1000);
}
void tst_QComplexText::bidiCursor_PDF()
{
QString str = QString::fromUtf8("\342\200\252hello\342\200\254");
QTextLayout layout(str);
layout.setCacheEnabled(true);
layout.beginLayout();
QTextLine line = layout.createLine();
layout.endLayout();
int size = str.size();
QVERIFY(line.cursorToX(size) == line.cursorToX(size - 1));
}
static void testBidiString(const QString &data, int paragraphDirection,
const QList<int> &resolvedLevels, const QList<int> &visualOrder)
{
Q_UNUSED(resolvedLevels);
QTextEngine e(data, QFont());
Qt::LayoutDirection pDir = Qt::LeftToRight;
if (paragraphDirection == 1)
pDir = Qt::RightToLeft;
else if (paragraphDirection == 2)
pDir = Qt::LayoutDirectionAuto;
e.option.setTextDirection(pDir);
e.itemize();
quint8 levels[1024];
int visual[1024];
int nitems = e.layoutData->items.size();
int i;
for (i = 0; i < nitems; ++i) {
//qDebug("item %d bidiLevel=%d", i, e.items[i].analysis.bidiLevel);
levels[i] = e.layoutData->items[i].analysis.bidiLevel;
}
e.bidiReorder(nitems, levels, visual);
QString visualString;
for (i = 0; i < nitems; ++i) {
QScriptItem &si = e.layoutData->items[visual[i]];
QString sub;
for (int j = si.position; j < si.position + e.length(visual[i]); ++j) {
switch (data.at(j).direction()) {
case QChar::DirLRE:
case QChar::DirRLE:
case QChar::DirLRO:
case QChar::DirRLO:
case QChar::DirPDF:
case QChar::DirBN:
continue;
default:
break;
}
sub += data.at(j);
}
// remove explicit embedding characters, as the test data has them removed as well
sub.remove(QChar(0x202a));
sub.remove(QChar(0x202b));
sub.remove(QChar(0x202c));
sub.remove(QChar(0x202d));
sub.remove(QChar(0x202e));
if (si.analysis.bidiLevel % 2) {
// reverse sub
QChar *a = sub.data();
QChar *b = a + sub.size() - 1;
while (a < b) {
QChar tmp = *a;
*a = *b;
*b = tmp;
++a;
--b;
}
a = (QChar *)sub.unicode();
b = a + sub.size();
// while (a<b) {
// *a = a->mirroredChar();
// ++a;
// }
}
visualString += sub;
}
QString expected;
// qDebug() << "expected visual order";
for (int i : visualOrder) {
// qDebug() << " " << i << hex << data[i].unicode();
expected.append(data[i]);
}
QCOMPARE(visualString, expected);
}
void tst_QComplexText::bidiCharacterTest()
{
QString testFile = QFINDTESTDATA("data/BidiCharacterTest.txt");
QFile f(testFile);
QVERIFY(f.exists());
f.open(QIODevice::ReadOnly);
int linenum = 0;
while (!f.atEnd()) {
linenum++;
QByteArray line = f.readLine().simplified();
if (line.startsWith('#') || line.isEmpty())
continue;
QVERIFY(!line.contains('#'));
QList<QByteArray> parts = line.split(';');
QVERIFY(parts.size() == 5);
QString data;
QList<QByteArray> dataParts = parts.at(0).split(' ');
for (const auto &p : dataParts) {
bool ok;
data += QChar((ushort)p.toInt(&ok, 16));
QVERIFY(ok);
}
int paragraphDirection = parts.at(1).toInt();
// int resolvedParagraphLevel = parts.at(2).toInt();
QList<int> resolvedLevels;
QList<QByteArray> levelParts = parts.at(3).split(' ');
for (const auto &p : levelParts) {
if (p == "x") {
resolvedLevels += -1;
} else {
bool ok;
resolvedLevels += p.toInt(&ok);
QVERIFY(ok);
}
}
QList<int> visualOrder;
QList<QByteArray> orderParts = parts.at(4).split(' ');
for (const auto &p : orderParts) {
bool ok;
visualOrder += p.toInt(&ok);
QVERIFY(ok);
}
const QByteArray nm = "line #" + QByteArray::number(linenum);
testBidiString(data, paragraphDirection, resolvedLevels, visualOrder);
}
}
ushort unicodeForDirection(const QByteArray &direction)
{
struct {
const char *string;
ushort unicode;
} dirToUnicode[] = {
{ "L", 0x41 },
{ "R", 0x5d0 },
{ "EN", 0x30 },
{ "ES", 0x2b },
{ "ET", 0x24 },
{ "AN", 0x660 },
{ "CS", 0x2c },
{ "B", '\n' },
{ "S", 0x9 },
{ "WS", 0x20 },
{ "ON", 0x2a },
{ "LRE", 0x202a },
{ "LRO", 0x202d },
{ "AL", 0x627 },
{ "RLE", 0x202b },
{ "RLO", 0x202e },
{ "PDF", 0x202c },
{ "NSM", 0x300 },
{ "BN", 0xad },
{ "LRI", 0x2066 },
{ "RLI", 0x2067 },
{ "FSI", 0x2068 },
{ "PDI", 0x2069 }
};
for (const auto &e : dirToUnicode) {
if (e.string == direction)
return e.unicode;
}
Q_UNREACHABLE();
}
void tst_QComplexText::bidiTest()
{
QString testFile = QFINDTESTDATA("data/BidiTest.txt");
QFile f(testFile);
QVERIFY(f.exists());
f.open(QIODevice::ReadOnly);
int linenum = 0;
QList<int> resolvedLevels;
QList<int> visualOrder;
while (!f.atEnd()) {
linenum++;
QByteArray line = f.readLine().simplified();
if (line.startsWith('#') || line.isEmpty())
continue;
QVERIFY(!line.contains('#'));
if (line.startsWith("@Levels:")) {
line = line.mid(strlen("@Levels:")).simplified();
resolvedLevels.clear();
QList<QByteArray> levelParts = line.split(' ');
for (const auto &p : levelParts) {
if (p == "x") {
resolvedLevels += -1;
} else {
bool ok;
resolvedLevels += p.toInt(&ok);
QVERIFY(ok);
}
}
continue;
} else if (line.startsWith("@Reorder:")) {
line = line.mid(strlen("@Reorder:")).simplified();
visualOrder.clear();
QList<QByteArray> orderParts = line.split(' ');
for (const auto &p : orderParts) {
if (p.isEmpty())
continue;
bool ok;
visualOrder += p.toInt(&ok);
QVERIFY(ok);
}
continue;
}
QList<QByteArray> parts = line.split(';');
Q_ASSERT(parts.size() == 2);
QString data;
QList<QByteArray> dataParts = parts.at(0).split(' ');
for (const auto &p : dataParts) {
ushort uc = unicodeForDirection(p);
data += QChar(uc);
}
int paragraphDirections = parts.at(1).toInt();
const QByteArray nm = "line #" + QByteArray::number(linenum);
if (paragraphDirections & 1)
testBidiString(data, 2, resolvedLevels, visualOrder);
if (paragraphDirections & 2)
testBidiString(data, 0, resolvedLevels, visualOrder);
if (paragraphDirections & 4)
testBidiString(data, 1, resolvedLevels, visualOrder);
}
}
QTEST_MAIN(tst_QComplexText)
#include "tst_qcomplextext.moc"

View File

@ -0,0 +1,3 @@
# QTBUG-87395
[checkReason_Popup]
android

View File

@ -0,0 +1,16 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qfocusevent Test:
#####################################################################
qt_internal_add_test(tst_qfocusevent
SOURCES
tst_qfocusevent.cpp
LIBRARIES
Qt::Gui
Qt::GuiPrivate
Qt::Widgets
Qt::WidgetsPrivate
)

View File

@ -0,0 +1,390 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTest>
#include <qapplication.h>
#include <qlineedit.h>
#include <qmenu.h>
#include <qlabel.h>
#include <qdialog.h>
#include <qevent.h>
#include <qlineedit.h>
#include <QBoxLayout>
#include <QSysInfo>
#include <qpa/qplatformintegration.h>
#include <private/qguiapplication_p.h>
#include <QtWidgets/private/qapplication_p.h>
QT_FORWARD_DECLARE_CLASS(QWidget)
class FocusLineEdit : public QLineEdit
{
public:
FocusLineEdit(QWidget *parent = nullptr, const char *name = nullptr ) : QLineEdit(name, parent) {}
int focusInEventReason;
int focusOutEventReason;
bool focusInEventRecieved;
bool focusInEventGotFocus;
bool focusOutEventRecieved;
bool focusOutEventLostFocus;
protected:
virtual void keyPressEvent( QKeyEvent *e ) override
{
// qDebug( QString("keyPressEvent: %1").arg(e->key()) );
QLineEdit::keyPressEvent( e );
}
void focusInEvent( QFocusEvent* e ) override
{
QLineEdit::focusInEvent( e );
focusInEventReason = e->reason();
focusInEventGotFocus = e->gotFocus();
focusInEventRecieved = true;
}
void focusOutEvent( QFocusEvent* e ) override
{
QLineEdit::focusOutEvent( e );
focusOutEventReason = e->reason();
focusOutEventLostFocus = !e->gotFocus();
focusOutEventRecieved = true;
}
};
class tst_QFocusEvent : public QObject
{
Q_OBJECT
public:
void initWidget();
private slots:
void initTestCase();
void cleanupTestCase();
void cleanup();
void checkReason_Tab();
void checkReason_ShiftTab();
void checkReason_BackTab();
void checkReason_Popup();
void checkReason_focusWidget();
#if QT_CONFIG(shortcut)
void checkReason_Shortcut();
#endif
void checkReason_ActiveWindow();
private:
QWidget* testFocusWidget = nullptr;
FocusLineEdit* childFocusWidgetOne;
FocusLineEdit* childFocusWidgetTwo;
};
void tst_QFocusEvent::initTestCase()
{
if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation))
QSKIP("QWindow::requestActivate() is not supported on this platform.");
testFocusWidget = new QWidget( nullptr );
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();
QVERIFY(QTest::qWaitForWindowExposed(testFocusWidget));
// Applications don't get focus when launched from the command line on Mac.
#ifdef Q_OS_MAC
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.
QApplicationPrivate::setActiveWindow(testFocusWidget);
childFocusWidgetOne->setFocus(); // The first lineedit should have focus
QVERIFY(QTest::qWaitForWindowActive(testFocusWidget));
QTRY_VERIFY(childFocusWidgetOne->hasFocus());
childFocusWidgetOne->focusInEventRecieved = false;
childFocusWidgetOne->focusInEventGotFocus = false;
childFocusWidgetOne->focusInEventReason = -1;
childFocusWidgetOne->focusOutEventRecieved = false;
childFocusWidgetOne->focusOutEventLostFocus = false;
childFocusWidgetOne->focusOutEventReason = -1;
childFocusWidgetTwo->focusInEventRecieved = false;
childFocusWidgetTwo->focusInEventGotFocus = false;
childFocusWidgetTwo->focusInEventReason = -1;
childFocusWidgetTwo->focusOutEventRecieved = false;
childFocusWidgetTwo->focusOutEventLostFocus = false;
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()
{
#ifdef Q_OS_WIN32 // key is not supported on Windows
QSKIP( "Backtab is not supported on Windows");
#else
initWidget();
QVERIFY( childFocusWidgetOne->hasFocus() );
// Now test the backtab key
QTest::keyClick( childFocusWidgetOne, Qt::Key_Backtab );
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) );
#endif
}
void tst_QFocusEvent::checkReason_Popup()
{
initWidget();
// Now test the popup reason
QMenu* popupMenu = new QMenu( testFocusWidget );
popupMenu->addMenu( "Test" );
popupMenu->popup( QPoint(0,0) );
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 );
}
#ifdef Q_OS_MAC
QT_BEGIN_NAMESPACE
extern void qt_set_sequence_auto_mnemonic(bool);
QT_END_NAMESPACE
#endif
#if QT_CONFIG(shortcut)
void tst_QFocusEvent::checkReason_Shortcut()
{
initWidget();
#ifdef Q_OS_MAC
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() );
#ifdef Q_OS_MAC
qt_set_sequence_auto_mnemonic(false);
#endif
}
#endif // QT_CONFIG(shortcut)
void tst_QFocusEvent::checkReason_focusWidget()
{
// This test checks that a widget doesn't loose
// its focuswidget just because the focuswidget loses focus.
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();
QVERIFY(QTest::qWaitForWindowActive(&window1));
edit1.setFocus();
QTRY_VERIFY(edit1.hasFocus());
edit2.setFocus();
QVERIFY(frame1.focusWidget() != nullptr);
QVERIFY(frame2.focusWidget() != nullptr);
}
void tst_QFocusEvent::checkReason_ActiveWindow()
{
initWidget();
QDialog* d = new QDialog( testFocusWidget );
d->show();
QVERIFY(QTest::qWaitForWindowExposed(d));
d->activateWindow(); // ### CDE
QApplicationPrivate::setActiveWindow(d);
QVERIFY(QTest::qWaitForWindowActive(d));
QTRY_VERIFY(childFocusWidgetOne->focusOutEventRecieved);
QVERIFY(childFocusWidgetOne->focusOutEventLostFocus);
#if defined(Q_OS_WIN)
if (QSysInfo::kernelVersion() == "10.0.15063") {
// Activate window of testFocusWidget, focus in that window goes to childFocusWidgetOne
qWarning("Windows 10 Creators Update (10.0.15063) requires explicit activateWindow()");
testFocusWidget->activateWindow();
}
#endif
QVERIFY( !childFocusWidgetOne->focusInEventRecieved );
QVERIFY( childFocusWidgetOne->focusOutEventRecieved );
QCOMPARE( childFocusWidgetOne->focusOutEventReason, (int)Qt::ActiveWindowFocusReason);
QVERIFY( !childFocusWidgetOne->hasFocus() );
d->hide();
if (!QGuiApplication::platformName().compare(QLatin1String("offscreen"), Qt::CaseInsensitive)
|| !QGuiApplication::platformName().compare(QLatin1String("minimal"), Qt::CaseInsensitive)
|| !QGuiApplication::platformName().compare(QLatin1String("cocoa"), Qt::CaseInsensitive)) {
// Activate window of testFocusWidget, focus in that window goes to childFocusWidgetOne
qWarning("Platforms offscreen, minimal and macOS require explicit activateWindow()");
testFocusWidget->activateWindow();
}
QTRY_VERIFY(childFocusWidgetOne->focusInEventRecieved);
QVERIFY(childFocusWidgetOne->focusInEventGotFocus);
QVERIFY( childFocusWidgetOne->hasFocus() );
QVERIFY( childFocusWidgetOne->focusInEventRecieved );
QCOMPARE( childFocusWidgetOne->focusInEventReason, (int)Qt::ActiveWindowFocusReason);
const bool windowActivationReasonFail =
QGuiApplication::platformName().toLower() == "minimal";
struct Window : public QWindow
{
Qt::FocusReason lastReason = Qt::NoFocusReason;
protected:
void focusInEvent(QFocusEvent *event) override
{
lastReason = event->reason();
}
void focusOutEvent(QFocusEvent *event) override
{
lastReason = event->reason();
}
};
Window window;
window.show();
window.requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&window));
if (windowActivationReasonFail)
QEXPECT_FAIL("", "Platform doesn't set window activation reason for QWindow", Continue);
QCOMPARE(window.lastReason, Qt::ActiveWindowFocusReason);
window.lastReason = Qt::NoFocusReason;
Window window2;
window2.show();
window2.requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&window2));
if (windowActivationReasonFail)
QEXPECT_FAIL("", "Platform doesn't set window activation reason for QWindow", Continue);
QCOMPARE(window.lastReason, Qt::ActiveWindowFocusReason);
}
QTEST_MAIN(tst_QFocusEvent)
#include "tst_qfocusevent.moc"

View File

@ -0,0 +1,4 @@
[downloadCheck]
windows-10
[downloadCheck:with-zeroCopy]
windows

View File

@ -0,0 +1,16 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qnetworkaccessmanager_and_qprogressdialog Test:
#####################################################################
qt_internal_add_test(tst_qnetworkaccessmanager_and_qprogressdialog
SOURCES
tst_qnetworkaccessmanager_and_qprogressdialog.cpp
LIBRARIES
Qt::Gui
Qt::Network
Qt::Widgets
QT_TEST_SERVER_LIST "apache2"
)

View File

@ -0,0 +1,131 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTest>
#include <QtGui>
#include <QtWidgets>
#include <QtCore>
#include <QTestEventLoop>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <qdebug.h>
#include "../../network-settings.h"
class tst_QNetworkAccessManager_And_QProgressDialog : public QObject
{
Q_OBJECT
public:
tst_QNetworkAccessManager_And_QProgressDialog();
private slots:
void initTestCase();
void downloadCheck();
void downloadCheck_data();
};
class DownloadCheckWidget : public QWidget
{
Q_OBJECT
public:
DownloadCheckWidget(QWidget *parent = nullptr) :
QWidget(parent), lateReadyRead(true), zeroCopy(false),
progressDlg(this), netmanager(this)
{
progressDlg.setRange(1, 100);
QMetaObject::invokeMethod(this, "go", Qt::QueuedConnection);
}
bool lateReadyRead;
bool zeroCopy;
public slots:
void go()
{
QNetworkRequest request(QUrl("http://" + QtNetworkSettings::httpServerName() + "/qtest/bigfile"));
if (zeroCopy)
request.setAttribute(QNetworkRequest::MaximumDownloadBufferSizeAttribute, 10*1024*1024);
QNetworkReply *reply = netmanager.get(
QNetworkRequest(
QUrl("http://" + QtNetworkSettings::httpServerName() + "/qtest/bigfile")
));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(dataReadProgress(qint64,qint64)));
connect(reply, SIGNAL(readyRead()),
this, SLOT(dataReadyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(finishedFromReply()));
progressDlg.exec();
}
void dataReadProgress(qint64 done, qint64 total)
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
Q_UNUSED(reply);
progressDlg.setMaximum(total);
progressDlg.setValue(done);
}
void dataReadyRead()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
Q_UNUSED(reply);
lateReadyRead = true;
}
void finishedFromReply()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
lateReadyRead = false;
reply->deleteLater();
QTestEventLoop::instance().exitLoop();
}
private:
QProgressDialog progressDlg;
QNetworkAccessManager netmanager;
};
tst_QNetworkAccessManager_And_QProgressDialog::tst_QNetworkAccessManager_And_QProgressDialog()
{
}
void tst_QNetworkAccessManager_And_QProgressDialog::initTestCase()
{
#ifdef QT_TEST_SERVER
if (!QtNetworkSettings::verifyConnection(QtNetworkSettings::httpServerName(), 80))
QSKIP("No network test server available");
#else
if (!QtNetworkSettings::verifyTestNetworkSettings())
QSKIP("No network test server available");
#endif
}
void tst_QNetworkAccessManager_And_QProgressDialog::downloadCheck_data()
{
QTest::addColumn<bool>("useZeroCopy");
QTest::newRow("with-zeroCopy") << true;
QTest::newRow("without-zeroCopy") << false;
}
void tst_QNetworkAccessManager_And_QProgressDialog::downloadCheck()
{
QFETCH(bool, useZeroCopy);
DownloadCheckWidget widget;
widget.zeroCopy = useZeroCopy;
widget.show();
// run and exit on finished()
QTestEventLoop::instance().enterLoop(10);
QVERIFY(!QTestEventLoop::instance().timeout());
// run some more to catch the late readyRead() (or: to not catch it)
QTestEventLoop::instance().enterLoop(1);
QVERIFY(QTestEventLoop::instance().timeout());
// the following fails when a readyRead() was received after finished()
QVERIFY(!widget.lateReadyRead);
}
QTEST_MAIN(tst_QNetworkAccessManager_And_QProgressDialog)
#include "tst_qnetworkaccessmanager_and_qprogressdialog.moc"

View File

@ -0,0 +1,13 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## qobjectrace Test:
#####################################################################
qt_internal_add_test(tst_qobjectrace
SOURCES
tst_qobjectrace.cpp
LIBRARIES
Qt::TestPrivate
)

View File

@ -0,0 +1,592 @@
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QtCore>
#include <QTest>
#include <QtTest/private/qemulationdetector_p.h>
#include <optional>
enum { OneMinute = 60 * 1000,
TwoMinutes = OneMinute * 2 };
struct Functor
{
void operator()() const {};
};
class tst_QObjectRace: public QObject
{
Q_OBJECT
public:
tst_QObjectRace()
: ThreadCount(QThread::idealThreadCount())
{}
private slots:
void moveToThreadRace();
void destroyRace();
void blockingQueuedDestroyRace();
void disconnectRace();
void disconnectRace2();
private:
const int ThreadCount;
};
class RaceObject : public QObject
{
Q_OBJECT
QList<QThread *> threads;
int count;
public:
RaceObject()
: count(0)
{ }
void addThread(QThread *thread)
{ threads.append(thread); }
public slots:
void theSlot()
{
enum { step = 35 };
if ((++count % step) == 0) {
QThread *nextThread = threads.at((count / step) % threads.size());
moveToThread(nextThread);
}
}
void destroSlot() {
emit theSignal();
}
signals:
void theSignal();
};
class RaceThread : public QThread
{
Q_OBJECT
RaceObject *object;
QElapsedTimer stopWatch;
public:
RaceThread()
: object(0)
{ }
void setObject(RaceObject *o)
{
object = o;
object->addThread(this);
}
void start() {
stopWatch.start();
QThread::start();
}
void run() override
{
QTimer zeroTimer;
connect(&zeroTimer, SIGNAL(timeout()), object, SLOT(theSlot()));
connect(&zeroTimer, SIGNAL(timeout()), this, SLOT(checkStopWatch()), Qt::DirectConnection);
zeroTimer.start(0);
(void) exec();
}
signals:
void theSignal();
private slots:
void checkStopWatch()
{
if (stopWatch.elapsed() >= 5000)
quit();
QObject o;
connect(&o, SIGNAL(destroyed()) , object, SLOT(destroSlot()));
connect(object, SIGNAL(destroyed()) , &o, SLOT(deleteLater()));
}
};
void tst_QObjectRace::moveToThreadRace()
{
RaceObject *object = new RaceObject;
QVarLengthArray<RaceThread *, 16> threads(ThreadCount);
for (int i = 0; i < ThreadCount; ++i) {
threads[i] = new RaceThread;
threads[i]->setObject(object);
}
object->moveToThread(threads[0]);
for (int i = 0; i < ThreadCount; ++i)
threads[i]->start();
while(!threads[0]->isFinished()) {
QPointer<RaceObject> foo (object);
QObject o;
connect(&o, SIGNAL(destroyed()) , object, SLOT(destroSlot()));
connect(object, SIGNAL(destroyed()) , &o, SLOT(deleteLater()));
QTest::qWait(10);
}
// the other threads should finish pretty quickly now
for (int i = 1; i < ThreadCount; ++i)
QVERIFY(threads[i]->wait(300));
for (int i = 0; i < ThreadCount; ++i)
delete threads[i];
delete object;
}
class MyObject : public QObject
{ Q_OBJECT
bool ok;
public:
MyObject() : ok(true) {}
~MyObject() { Q_ASSERT(ok); ok = false; }
public slots:
void slot1() { Q_ASSERT(ok); }
void slot2() { Q_ASSERT(ok); }
void slot3() { Q_ASSERT(ok); }
void slot4() { Q_ASSERT(ok); }
void slot5() { Q_ASSERT(ok); }
void slot6() { Q_ASSERT(ok); }
void slot7() { Q_ASSERT(ok); }
signals:
void signal1();
void signal2();
void signal3();
void signal4();
void signal5();
void signal6();
void signal7();
};
namespace {
const char *_slots[] = { SLOT(slot1()) , SLOT(slot2()) , SLOT(slot3()),
SLOT(slot4()) , SLOT(slot5()) , SLOT(slot6()),
SLOT(slot7()) };
const char *_signals[] = { SIGNAL(signal1()), SIGNAL(signal2()), SIGNAL(signal3()),
SIGNAL(signal4()), SIGNAL(signal5()), SIGNAL(signal6()),
SIGNAL(signal7()) };
typedef void (MyObject::*PMFType)();
const PMFType _slotsPMF[] = { &MyObject::slot1, &MyObject::slot2, &MyObject::slot3,
&MyObject::slot4, &MyObject::slot5, &MyObject::slot6,
&MyObject::slot7 };
const PMFType _signalsPMF[] = { &MyObject::signal1, &MyObject::signal2, &MyObject::signal3,
&MyObject::signal4, &MyObject::signal5, &MyObject::signal6,
&MyObject::signal7 };
}
class DestroyThread : public QThread
{
Q_OBJECT
MyObject **objects;
int number;
public:
void setObjects(MyObject **o, int n)
{
objects = o;
number = n;
for(int i = 0; i < number; i++)
objects[i]->moveToThread(this);
}
void run() override
{
for (int i = number-1; i >= 0; --i) {
/* Do some more connection and disconnection between object in this thread that have not been destroyed yet */
const int nAlive = i+1;
connect (objects[((i+1)*31) % nAlive], _signals[(12*i)%7], objects[((i+2)*37) % nAlive], _slots[(15*i+2)%7] );
disconnect(objects[((i+1)*31) % nAlive], _signals[(12*i)%7], objects[((i+2)*37) % nAlive], _slots[(15*i+2)%7] );
connect (objects[((i+4)*41) % nAlive], _signalsPMF[(18*i)%7], objects[((i+5)*43) % nAlive], _slotsPMF[(19*i+2)%7] );
disconnect(objects[((i+4)*41) % nAlive], _signalsPMF[(18*i)%7], objects[((i+5)*43) % nAlive], _slotsPMF[(19*i+2)%7] );
QMetaObject::Connection c = connect(objects[((i+5)*43) % nAlive], _signalsPMF[(9*i+1)%7], Functor());
for (int f = 0; f < 7; ++f)
emit (objects[i]->*_signalsPMF[f])();
disconnect(c);
disconnect(objects[i], _signalsPMF[(10*i+5)%7], 0, 0);
disconnect(objects[i], _signals[(11*i+6)%7], 0, 0);
disconnect(objects[i], 0, objects[(i*17+6) % nAlive], 0);
if (i%4 == 1) {
disconnect(objects[i], 0, 0, 0);
}
delete objects[i];
}
//run the possible queued slots
qApp->processEvents();
}
};
#define EXTRA_THREAD_WAIT 3000
#define MAIN_THREAD_WAIT TwoMinutes
void tst_QObjectRace::destroyRace()
{
if (QTestPrivate::isRunningArmOnX86())
QSKIP("Test is too slow to run on emulator");
constexpr int ObjectCountPerThread = 2777;
const int ObjectCount = ThreadCount * ObjectCountPerThread;
QVarLengthArray<MyObject *, ObjectCountPerThread * 10> objects(ObjectCount);
for (int i = 0; i < ObjectCount; ++i)
objects[i] = new MyObject;
for (int i = 0; i < ObjectCount * 17; ++i) {
connect(objects[(i*13) % ObjectCount], _signals[(2*i)%7],
objects[((i+2)*17) % ObjectCount], _slots[(3*i+2)%7] );
connect(objects[((i+6)*23) % ObjectCount], _signals[(5*i+4)%7],
objects[((i+8)*41) % ObjectCount], _slots[(i+6)%7] );
connect(objects[(i*67) % ObjectCount], _signalsPMF[(2*i)%7],
objects[((i+1)*71) % ObjectCount], _slotsPMF[(3*i+2)%7] );
connect(objects[((i+3)*73) % ObjectCount], _signalsPMF[(5*i+4)%7],
objects[((i+5)*79) % ObjectCount], Functor() );
}
QVarLengthArray<DestroyThread *, 16> threads(ThreadCount);
for (int i = 0; i < ThreadCount; ++i) {
threads[i] = new DestroyThread;
threads[i]->setObjects(objects.data() + i*ObjectCountPerThread, ObjectCountPerThread);
}
for (int i = 0; i < ThreadCount; ++i)
threads[i]->start();
QVERIFY(threads[0]->wait(MAIN_THREAD_WAIT));
// the other threads should finish pretty quickly now
for (int i = 1; i < ThreadCount; ++i)
QVERIFY(threads[i]->wait(EXTRA_THREAD_WAIT));
for (int i = 0; i < ThreadCount; ++i)
delete threads[i];
}
class BlockingQueuedDestroyRaceObject : public QObject
{
Q_OBJECT
public:
enum class Behavior { Normal, Crash };
explicit BlockingQueuedDestroyRaceObject(Behavior b = Behavior::Normal)
: m_behavior(b) {}
signals:
bool aSignal();
public slots:
bool aSlot()
{
switch (m_behavior) {
case Behavior::Normal:
return true;
case Behavior::Crash:
qFatal("Race detected in a blocking queued connection");
break;
}
Q_UNREACHABLE_RETURN(false);
}
private:
Behavior m_behavior;
};
void tst_QObjectRace::blockingQueuedDestroyRace()
{
#if !QT_CONFIG(cxx11_future)
QSKIP("This test requires QThread::create");
#else
enum { MinIterations = 100, MinTime = 3000, WaitTime = 25 };
BlockingQueuedDestroyRaceObject sender;
QDeadlineTimer timer(MinTime);
int iteration = 0;
while (iteration++ < MinIterations || !timer.hasExpired()) {
// Manually allocate some storage, and create a receiver in there
std::optional<BlockingQueuedDestroyRaceObject> receiver;
receiver.emplace(BlockingQueuedDestroyRaceObject::Behavior::Normal);
// Connect it to the sender via BlockingQueuedConnection
QVERIFY(connect(&sender, &BlockingQueuedDestroyRaceObject::aSignal,
&*receiver, &BlockingQueuedDestroyRaceObject::aSlot,
Qt::BlockingQueuedConnection));
const auto emitUntilDestroyed = [&sender] {
// Hack: as long as the receiver is alive and the connection
// established, the signal will return true (from the slot).
// When the receiver gets destroyed, the signal is disconnected
// and therefore the emission returns false.
while (emit sender.aSignal())
;
};
std::unique_ptr<QThread> thread(QThread::create(emitUntilDestroyed));
thread->start();
QTest::qWait(WaitTime);
// Destroy the receiver, and immediately allocate a new one at
// the same address. In case of a race, this might cause:
// - the metacall event to be posted to a destroyed object;
// - the metacall event to be posted to the wrong object.
// In both cases we hope to catch the race by crashing.
receiver.reset();
receiver.emplace(BlockingQueuedDestroyRaceObject::Behavior::Crash);
// Flush events
QTest::qWait(0);
thread->wait();
}
#endif
}
static QAtomicInteger<unsigned> countedStructObjectsCount;
struct CountedFunctor
{
CountedFunctor() : destroyed(false) { countedStructObjectsCount.fetchAndAddRelaxed(1); }
CountedFunctor(const CountedFunctor &) : destroyed(false) { countedStructObjectsCount.fetchAndAddRelaxed(1); }
CountedFunctor &operator=(const CountedFunctor &) { return *this; }
~CountedFunctor() { destroyed = true; countedStructObjectsCount.fetchAndAddRelaxed(-1);}
void operator()() const {QCOMPARE(destroyed, false);}
private:
bool destroyed;
};
class DisconnectRaceSenderObject : public QObject
{
Q_OBJECT
signals:
void theSignal();
};
class DisconnectRaceThread : public QThread
{
Q_OBJECT
DisconnectRaceSenderObject *sender;
bool emitSignal;
public:
DisconnectRaceThread(DisconnectRaceSenderObject *s, bool emitIt)
: QThread(), sender(s), emitSignal(emitIt)
{
}
void run() override
{
while (!isInterruptionRequested()) {
QMetaObject::Connection conn = connect(sender, &DisconnectRaceSenderObject::theSignal,
sender, CountedFunctor(), Qt::BlockingQueuedConnection);
if (emitSignal)
emit sender->theSignal();
disconnect(conn);
yieldCurrentThread();
}
}
};
class DeleteReceiverRaceSenderThread : public QThread
{
Q_OBJECT
DisconnectRaceSenderObject *sender;
public:
DeleteReceiverRaceSenderThread(DisconnectRaceSenderObject *s)
: QThread(), sender(s)
{
}
void run() override
{
while (!isInterruptionRequested()) {
emit sender->theSignal();
yieldCurrentThread();
}
}
};
class DeleteReceiverRaceReceiver : public QObject
{
Q_OBJECT
DisconnectRaceSenderObject *sender;
QObject *receiver;
QTimer *timer;
public:
DeleteReceiverRaceReceiver(DisconnectRaceSenderObject *s)
: QObject(), sender(s), receiver(0)
{
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &DeleteReceiverRaceReceiver::onTimeout);
timer->start(1);
}
~DeleteReceiverRaceReceiver()
{
delete receiver;
}
void onTimeout()
{
if (receiver)
delete receiver;
receiver = new QObject;
connect(sender, &DisconnectRaceSenderObject::theSignal, receiver, CountedFunctor(), Qt::BlockingQueuedConnection);
}
};
class DeleteReceiverRaceReceiverThread : public QThread
{
Q_OBJECT
DisconnectRaceSenderObject *sender;
public:
DeleteReceiverRaceReceiverThread(DisconnectRaceSenderObject *s)
: QThread(), sender(s)
{
}
void run() override
{
QScopedPointer<DeleteReceiverRaceReceiver> receiver(new DeleteReceiverRaceReceiver(sender));
exec();
}
};
void tst_QObjectRace::disconnectRace()
{
enum { TimeLimit = 3000 };
QCOMPARE(countedStructObjectsCount.loadRelaxed(), 0u);
{
QScopedPointer<DisconnectRaceSenderObject> sender(new DisconnectRaceSenderObject());
QScopedPointer<QThread> senderThread(new QThread());
senderThread->start();
sender->moveToThread(senderThread.data());
QVarLengthArray<DisconnectRaceThread *, 16> threads(ThreadCount);
for (int i = 0; i < ThreadCount; ++i) {
threads[i] = new DisconnectRaceThread(sender.data(), !(i % 10));
threads[i]->start();
}
QTest::qWait(TimeLimit);
for (int i = 0; i < ThreadCount; ++i) {
threads[i]->requestInterruption();
QVERIFY(threads[i]->wait());
delete threads[i];
}
senderThread->quit();
QVERIFY(senderThread->wait());
}
QCOMPARE(countedStructObjectsCount.loadRelaxed(), 0u);
{
QScopedPointer<DisconnectRaceSenderObject> sender(new DisconnectRaceSenderObject());
QScopedPointer<DeleteReceiverRaceSenderThread> senderThread(new DeleteReceiverRaceSenderThread(sender.data()));
senderThread->start();
sender->moveToThread(senderThread.data());
QVarLengthArray<DeleteReceiverRaceReceiverThread *, 16> threads(ThreadCount);
for (int i = 0; i < ThreadCount; ++i) {
threads[i] = new DeleteReceiverRaceReceiverThread(sender.data());
threads[i]->start();
}
QTest::qWait(TimeLimit);
senderThread->requestInterruption();
QVERIFY(senderThread->wait());
for (int i = 0; i < ThreadCount; ++i) {
threads[i]->quit();
QVERIFY(threads[i]->wait());
delete threads[i];
}
}
QCOMPARE(countedStructObjectsCount.loadRelaxed(), 0u);
}
void tst_QObjectRace::disconnectRace2()
{
enum { IterationCount = 100, ConnectionCount = 100, YieldCount = 100 };
QAtomicPointer<MyObject> ptr;
QSemaphore createSemaphore(0);
QSemaphore proceedSemaphore(0);
std::unique_ptr<QThread> t1(QThread::create([&]() {
for (int i = 0; i < IterationCount; ++i) {
MyObject sender;
ptr.storeRelease(&sender);
createSemaphore.release();
proceedSemaphore.acquire();
ptr.storeRelaxed(nullptr);
for (int i = 0; i < YieldCount; ++i)
QThread::yieldCurrentThread();
}
}));
t1->start();
std::unique_ptr<QThread> t2(QThread::create([&]() {
auto connections = std::make_unique<QMetaObject::Connection[]>(ConnectionCount);
for (int i = 0; i < IterationCount; ++i) {
MyObject receiver;
MyObject *sender = nullptr;
createSemaphore.acquire();
while (!(sender = ptr.loadAcquire()))
;
for (int i = 0; i < ConnectionCount; ++i)
connections[i] = QObject::connect(sender, &MyObject::signal1, &receiver, &MyObject::slot1);
proceedSemaphore.release();
for (int i = 0; i < ConnectionCount; ++i)
QObject::disconnect(connections[i]);
}
}));
t2->start();
t1->wait();
t2->wait();
}
QTEST_MAIN(tst_QObjectRace)
#include "tst_qobjectrace.moc"

View File

@ -0,0 +1,14 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qprocess_and_guieventloop Test:
#####################################################################
qt_internal_add_test(tst_qprocess_and_guieventloop
SOURCES
tst_qprocess_and_guieventloop.cpp
LIBRARIES
Qt::Gui
)
add_subdirectory(write-read-write)

View File

@ -0,0 +1,58 @@
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2016 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QtGui/QGuiApplication>
#include <QTest>
#include <QSignalSpy>
#include <QtCore/QProcess>
class tst_QProcess_and_GuiEventLoop : public QObject
{
Q_OBJECT
private slots:
void waitForAndEventLoop();
};
void tst_QProcess_and_GuiEventLoop::waitForAndEventLoop()
{
#ifdef Q_OS_ANDROID
QSKIP("Not supported on Android");
#else
// based on testcase provided in QTBUG-39488
QByteArray msg = "Hello World";
QProcess process;
process.start("write-read-write/write-read-write", QStringList() << msg);
QVERIFY(process.waitForStarted(5000));
QVERIFY(process.waitForReadyRead(5000));
QCOMPARE(process.readAll().trimmed(), msg);
// run the GUI event dispatcher once
QSignalSpy spy(&process, SIGNAL(readyRead()));
qApp->processEvents(QEventLoop::AllEvents, 100);
// we mustn't have read anything in the event loop
QCOMPARE(spy.size(), 0);
// ensure the process hasn't died
QVERIFY(!process.waitForFinished(250));
// we mustn't have read anything during waitForFinished either
QCOMPARE(spy.size(), 0);
// release the child for the second write
process.write("\n");
QVERIFY(process.waitForFinished(5000));
QCOMPARE(int(process.exitStatus()), int(QProcess::NormalExit));
QCOMPARE(process.exitCode(), 0);
QCOMPARE(spy.size(), 1);
QCOMPARE(process.readAll().trimmed(), msg);
#endif
}
QTEST_MAIN(tst_QProcess_and_GuiEventLoop)
#include "tst_qprocess_and_guieventloop.moc"

View File

@ -0,0 +1,12 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## write-read-write Binary:
#####################################################################
qt_internal_add_executable(write-read-write
OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/"
SOURCES
main.cpp
)

View File

@ -0,0 +1,22 @@
// Copyright (C) 2016 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <stdio.h>
int main(int, char **argv)
{
const char *msg = argv[1];
char buf[2];
puts(msg);
fflush(stdout);
// wait for a newline
const char *result = fgets(buf, sizeof buf, stdin);
if (result != buf)
return -1;
puts(msg);
fflush(stdout);
return 0;
}

View File

@ -0,0 +1,14 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qsharedpointer_and_qwidget Test:
#####################################################################
qt_internal_add_test(tst_qsharedpointer_and_qwidget
SOURCES
tst_qsharedpointer_and_qwidget.cpp
LIBRARIES
Qt::Gui
Qt::Widgets
)

View File

@ -0,0 +1,114 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QtWidgets/QWidget>
#include <QtWidgets/QPushButton>
#include <QTest>
QT_BEGIN_NAMESPACE
namespace QtSharedPointer {
Q_CORE_EXPORT void internalSafetyCheckCleanCheck();
}
QT_END_NAMESPACE
class tst_QSharedPointer_and_QWidget: public QObject
{
Q_OBJECT
private slots:
void weak_externalDelete();
void weak_parentDelete();
void weak_parentDelete_setParent();
void strong_weak();
void strong_sharedptrDelete();
public slots:
void cleanup() { safetyCheck(); }
public:
inline void safetyCheck()
{
#ifdef QT_BUILD_INTERNAL
QtSharedPointer::internalSafetyCheckCleanCheck();
#endif
}
};
void tst_QSharedPointer_and_QWidget::weak_externalDelete()
{
QWidget *w = new QWidget;
QPointer<QWidget> ptr = w;
QVERIFY(!ptr.isNull());
delete w;
QVERIFY(ptr.isNull());
}
void tst_QSharedPointer_and_QWidget::weak_parentDelete()
{
QWidget *parent = new QWidget;
QWidget *w = new QWidget(parent);
QPointer<QWidget> ptr = w;
QVERIFY(!ptr.isNull());
delete parent;
QVERIFY(ptr.isNull());
}
void tst_QSharedPointer_and_QWidget::weak_parentDelete_setParent()
{
QWidget *parent = new QWidget;
QWidget *w = new QWidget;
QPointer<QWidget> ptr = w;
w->setParent(parent);
QVERIFY(!ptr.isNull());
delete parent;
QVERIFY(ptr.isNull());
}
// -- mixed --
void tst_QSharedPointer_and_QWidget::strong_weak()
{
QSharedPointer<QWidget> ptr(new QWidget);
QPointer<QWidget> weak = ptr.data();
QWeakPointer<QWidget> weak2 = ptr;
QVERIFY(!weak.isNull());
QVERIFY(!weak2.isNull());
ptr.clear(); // deletes
QVERIFY(weak.isNull());
QVERIFY(weak2.isNull());
}
// ---- strong management ----
void tst_QSharedPointer_and_QWidget::strong_sharedptrDelete()
{
QWidget *parent = new QWidget;
QSharedPointer<QWidget> ptr(new QWidget(parent));
QWeakPointer<QWidget> weak = ptr;
QPointer<QWidget> check = ptr.data();
QVERIFY(!check.isNull());
QVERIFY(!weak.isNull());
ptr.clear(); // deletes
QVERIFY(check.isNull());
QVERIFY(weak.isNull());
delete parent; // mustn't crash
}
QTEST_MAIN(tst_QSharedPointer_and_QWidget)
#include "tst_qsharedpointer_and_qwidget.moc"

View File

@ -0,0 +1,157 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#ifndef TST_QVARIANT_COMMON
#define TST_QVARIANT_COMMON
#include <QString>
class MessageHandler {
public:
MessageHandler(const int typeId, QtMessageHandler msgHandler = handler)
: oldMsgHandler(qInstallMessageHandler(msgHandler))
{
currentId = typeId;
}
~MessageHandler()
{
qInstallMessageHandler(oldMsgHandler);
}
bool testPassed() const
{
return ok;
}
protected:
static void handler(QtMsgType, const QMessageLogContext &, const QString &msg)
{
// Format itself is not important, but basic data as a type name should be included in the output
ok = msg.startsWith("QVariant(");
QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
ok &= (currentId == QMetaType::UnknownType
? msg.contains("Invalid")
: msg.contains(QMetaType(currentId).name()));
QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
if (currentId == QMetaType::Char || currentId == QMetaType::QChar) {
// Chars insert '\0' into the qdebug stream, it is not possible to find a real string length
return;
}
if (QMetaType(currentId).flags() & QMetaType::IsPointer) {
QByteArray currentName = QMetaType(currentId).name();
ok &= msg.contains(currentName + ", 0x");
}
ok &= msg.endsWith(QLatin1Char(')'));
QVERIFY2(ok, (QString::fromLatin1("Message is not correctly finished: '") + msg + '\'').toLatin1().constData());
}
QtMessageHandler oldMsgHandler;
inline static int currentId = {};
inline static bool ok = {};
};
#define TST_QVARIANT_CANCONVERT_DATATABLE_HEADERS \
QTest::addColumn<QVariant>("val"); \
QTest::addColumn<bool>("BitArrayCast"); \
QTest::addColumn<bool>("BitmapCast"); \
QTest::addColumn<bool>("BoolCast"); \
QTest::addColumn<bool>("BrushCast"); \
QTest::addColumn<bool>("ByteArrayCast"); \
QTest::addColumn<bool>("ColorCast"); \
QTest::addColumn<bool>("CursorCast"); \
QTest::addColumn<bool>("DateCast"); \
QTest::addColumn<bool>("DateTimeCast"); \
QTest::addColumn<bool>("DoubleCast"); \
QTest::addColumn<bool>("FontCast"); \
QTest::addColumn<bool>("ImageCast"); \
QTest::addColumn<bool>("IntCast"); \
QTest::addColumn<bool>("InvalidCast"); \
QTest::addColumn<bool>("KeySequenceCast"); \
QTest::addColumn<bool>("ListCast"); \
QTest::addColumn<bool>("LongLongCast"); \
QTest::addColumn<bool>("MapCast"); \
QTest::addColumn<bool>("PaletteCast"); \
QTest::addColumn<bool>("PenCast"); \
QTest::addColumn<bool>("PixmapCast"); \
QTest::addColumn<bool>("PointCast"); \
QTest::addColumn<bool>("RectCast"); \
QTest::addColumn<bool>("RegionCast"); \
QTest::addColumn<bool>("SizeCast"); \
QTest::addColumn<bool>("SizePolicyCast"); \
QTest::addColumn<bool>("StringCast"); \
QTest::addColumn<bool>("StringListCast"); \
QTest::addColumn<bool>("TimeCast"); \
QTest::addColumn<bool>("UIntCast"); \
QTest::addColumn<bool>("ULongLongCast");
#define TST_QVARIANT_CANCONVERT_FETCH_DATA \
QFETCH(QVariant, val); \
QFETCH(bool, BitArrayCast); \
QFETCH(bool, BitmapCast); \
QFETCH(bool, BoolCast); \
QFETCH(bool, BrushCast); \
QFETCH(bool, ByteArrayCast); \
QFETCH(bool, ColorCast); \
QFETCH(bool, CursorCast); \
QFETCH(bool, DateCast); \
QFETCH(bool, DateTimeCast); \
QFETCH(bool, DoubleCast); \
QFETCH(bool, FontCast); \
QFETCH(bool, ImageCast); \
QFETCH(bool, IntCast); \
QFETCH(bool, InvalidCast); \
QFETCH(bool, KeySequenceCast); \
QFETCH(bool, ListCast); \
QFETCH(bool, LongLongCast); \
QFETCH(bool, MapCast); \
QFETCH(bool, PaletteCast); \
QFETCH(bool, PenCast); \
QFETCH(bool, PixmapCast); \
QFETCH(bool, PointCast); \
QFETCH(bool, RectCast); \
QFETCH(bool, RegionCast); \
QFETCH(bool, SizeCast); \
QFETCH(bool, SizePolicyCast); \
QFETCH(bool, StringCast); \
QFETCH(bool, StringListCast); \
QFETCH(bool, TimeCast); \
QFETCH(bool, UIntCast); \
QFETCH(bool, ULongLongCast);
#define TST_QVARIANT_CANCONVERT_COMPARE_DATA \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QBitArray)), BitArrayCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QBitmap)), BitmapCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::Bool)), BoolCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QBrush)), BrushCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QByteArray)), ByteArrayCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QColor)), ColorCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QCursor)), CursorCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QDate)), DateCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QDateTime)), DateTimeCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::Double)), DoubleCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::Float)), DoubleCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QFont)), FontCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QImage)), ImageCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::Int)), IntCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::UnknownType)), InvalidCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QKeySequence)), KeySequenceCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QVariantList)), ListCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::LongLong)), LongLongCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QVariantMap)), MapCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QPalette)), PaletteCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QPen)), PenCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QPixmap)), PixmapCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QPoint)), PointCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QRect)), RectCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QRegion)), RegionCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QSize)), SizeCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QSizePolicy)), SizePolicyCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QString)), StringCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QStringList)), StringListCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::QTime)), TimeCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::UInt)), UIntCast); \
QCOMPARE(val.canConvert(QMetaType(QMetaType::ULongLong)), ULongLongCast);
#endif

View File

@ -0,0 +1,36 @@
// Copyright (C) 2019 Samuel Gaist <samuel.gaist@idiap.ch>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTest>
#include <QSignalSpy>
#include <QSessionManager>
#include <AppKit/AppKit.h>
// Q_DECLARE_METATYPE(QSessionManager)
class tst_SessionManagement_macOS : public QObject
{
Q_OBJECT
private slots:
void stopApplication();
};
/*
Test that session handling code is properly called
*/
void tst_SessionManagement_macOS::stopApplication()
{
int argc = 0;
QGuiApplication app(argc, nullptr);
QSignalSpy spy(&app, &QGuiApplication::commitDataRequest);
QTimer::singleShot(1000, []() {
[NSApp terminate:nil];
});
app.exec();
QCOMPARE(spy.count(), 1);
}
QTEST_APPLESS_MAIN(tst_SessionManagement_macOS)
#include "tst_sessionmanagement_macos.moc"

View File

@ -0,0 +1,13 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_toolsupport Test:
#####################################################################
qt_internal_add_test(tst_toolsupport
SOURCES
tst_toolsupport.cpp
LIBRARIES
Qt::CorePrivate
)

View File

@ -0,0 +1,147 @@
// Copyright (C) 2015 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTest>
//
// Note:
//
// When this test here fails and the change leading to the failure
// intentionally changed a private class, adjust the test here and bump
// the TypeInformationVersion field in src/corelib/global/qhooks.cpp
// in the same commit as the modification to the private class.
//
// Please also notify downstream users of the information checked here
// such as Qt Creator developers, of such a change by putting them
// on Cc: on the respective change on gerrit.
//
// Don't do this at home. This is test code, not production.
#define protected public
#define private public
#include <private/qdatetime_p.h>
#include <private/qfile_p.h>
#include <private/qfileinfo_p.h>
#include <private/qobject_p.h>
#include <qobject.h>
#if defined(Q_CC_GNU) || defined(Q_CC_MSVC)
#define RUN_MEMBER_OFFSET_TEST 1
#else
#define RUN_MEMBER_OFFSET_TEST 0
#endif
#if RUN_MEMBER_OFFSET_TEST
template <typename T, typename K>
size_t pmm_to_offsetof(T K:: *pmm)
{
#ifdef Q_CC_MSVC
// Even on 64 bit MSVC uses 4 byte offsets.
quint32 ret;
#else
size_t ret;
#endif
static_assert(sizeof(ret) == sizeof(pmm));
memcpy(&ret, &pmm, sizeof(ret));
return ret;
}
#endif
class tst_toolsupport : public QObject
{
Q_OBJECT
private slots:
void offsets();
void offsets_data();
};
void tst_toolsupport::offsets()
{
QFETCH(size_t, actual);
QFETCH(int, expected32);
QFETCH(int, expected64);
size_t expect = sizeof(void *) == 4 ? expected32 : expected64;
QCOMPARE(actual, expect);
}
void tst_toolsupport::offsets_data()
{
QTest::addColumn<size_t>("actual");
QTest::addColumn<int>("expected32");
QTest::addColumn<int>("expected64");
{
QTestData &data = QTest::newRow("sizeof(QObjectData)")
<< sizeof(QObjectData);
// Please heed the comment at the top of this file when changing this line:
data << 44 << 80; // vptr + 2 ptr + (2*ptr + int) + 2 int + ptr
}
{
QTestData &data = QTest::newRow("sizeof(QObjectPrivate::ExtraData)")
<< sizeof(QObjectPrivate::ExtraData);
// Please heed the comment at the top of this file when changing this line:
data << 64 << 128; // 4 * QList + 1 * QString + ptr
}
#if RUN_MEMBER_OFFSET_TEST
{
QTestData &data = QTest::newRow("QObjectPrivate::extraData")
<< pmm_to_offsetof(&QObjectPrivate::extraData);
// Please heed the comment at the top of this file when changing this line:
data << 44 << 80; // sizeof(QObjectData)
}
{
QTestData &data = QTest::newRow("QFileInfoPrivate::fileEntry")
<< pmm_to_offsetof(&QFileInfoPrivate::fileEntry);
// Please heed the comment at the top of this file when changing this line:
data << 4 << 8;
}
{
QTestData &data = QTest::newRow("QFileSystemEntry::filePath")
<< pmm_to_offsetof(&QFileSystemEntry::m_filePath);
// Please heed the comment at the top of this file when changing this line:
data << 0 << 0;
}
#ifdef Q_OS_LINUX
{
QTestData &data = QTest::newRow("QFilePrivate::fileName")
<< pmm_to_offsetof(&QFilePrivate::fileName);
// Please heed the comment at the top of this file when changing one of these lines:
#ifdef Q_PROCESSOR_X86
// x86 32-bit has weird alignment rules. Refer to QtPrivate::AlignOf in
// qglobal.h for more details.
data << 264 << 424;
#else
data << 300 << 424;
#endif
}
#endif
{
// Please heed the comment at the top of this file when changing one of these lines:
QTest::newRow("QDateTimePrivate::m_msecs")
<< pmm_to_offsetof(&QDateTimePrivate::m_msecs) << 8 << 8;
QTest::newRow("QDateTimePrivate::m_status")
<< pmm_to_offsetof(&QDateTimePrivate::m_status) << 4 << 4;
QTest::newRow("QDateTimePrivate::m_offsetFromUtc")
<< pmm_to_offsetof(&QDateTimePrivate::m_offsetFromUtc) << 16 << 16;
#if QT_CONFIG(timezone)
QTest::newRow("QDateTimePrivate::m_timeZone")
<< pmm_to_offsetof(&QDateTimePrivate::m_timeZone) << 20 << 24;
#endif
}
#endif // RUN_MEMBER_OFFSET_TEST
}
QTEST_APPLESS_MAIN(tst_toolsupport);
#include "tst_toolsupport.moc"

View File

@ -0,0 +1,14 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_xkbkeyboard Test:
#####################################################################
qt_internal_add_test(tst_xkbkeyboard
SOURCES
tst_xkbkeyboard.cpp
LIBRARIES
Qt::CorePrivate
Qt::GuiPrivate
)

View File

@ -0,0 +1,35 @@
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QtCore>
#include <QtGui>
#include <QTest>
#include <qpa/qplatforminputcontextfactory_p.h>
#include <qpa/qplatforminputcontext.h>
class tst_XkbKeyboard : public QObject
{
Q_OBJECT
private slots:
void verifyComposeInputContextInterface();
};
void tst_XkbKeyboard::verifyComposeInputContextInterface()
{
QPlatformInputContext *inputContext = QPlatformInputContextFactory::create(QStringLiteral("compose"));
QVERIFY(inputContext);
const char *const inputContextClassName = "QComposeInputContext";
const char *const normalizedSignature = "setXkbContext(xkb_context*)";
QVERIFY(inputContext->objectName() == QLatin1String(inputContextClassName));
int methodIndex = inputContext->metaObject()->indexOfMethod(normalizedSignature);
QMetaMethod method = inputContext->metaObject()->method(methodIndex);
Q_ASSERT(method.isValid());
}
QTEST_MAIN(tst_XkbKeyboard)
#include "tst_xkbkeyboard.moc"