From 4a886753a75c7c4d66f1fa9cab5a6c5a03240df3 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Tue, 9 Oct 2018 09:12:07 +0200 Subject: Accessible: Return StaticText if there is no explicit role set Before, it would assume that StaticText was the role if the item could be cast to a QQuickText. It should only do this if the role is not explicitly set, in case it is not really StaticText in the attached property. Change-Id: I800810f1347fc9aa412c4ca5d180f78d27a89b38 Reviewed-by: Andre de la Rocha --- src/quick/accessible/qaccessiblequickitem.cpp | 10 ++-- .../qquickaccessible/tst_qquickaccessible.cpp | 70 +++++++++++++++++++++- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/quick/accessible/qaccessiblequickitem.cpp b/src/quick/accessible/qaccessiblequickitem.cpp index 87e581384b..98e7663c96 100644 --- a/src/quick/accessible/qaccessiblequickitem.cpp +++ b/src/quick/accessible/qaccessiblequickitem.cpp @@ -205,14 +205,16 @@ QAccessible::Role QAccessibleQuickItem::role() const // Workaround for setAccessibleRole() not working for // Text items. Text items are special since they are defined // entirely from C++ (setting the role from QML works.) - if (qobject_cast(const_cast(item()))) - return QAccessible::StaticText; QAccessible::Role role = QAccessible::NoRole; if (item()) role = QQuickItemPrivate::get(item())->accessibleRole(); - if (role == QAccessible::NoRole) - role = QAccessible::Client; + if (role == QAccessible::NoRole) { + if (qobject_cast(const_cast(item()))) + role = QAccessible::StaticText; + else + role = QAccessible::Client; + } return role; } diff --git a/tests/auto/quick/qquickaccessible/tst_qquickaccessible.cpp b/tests/auto/quick/qquickaccessible/tst_qquickaccessible.cpp index 0ee78fae54..1bfeb94161 100644 --- a/tests/auto/quick/qquickaccessible/tst_qquickaccessible.cpp +++ b/tests/auto/quick/qquickaccessible/tst_qquickaccessible.cpp @@ -42,9 +42,11 @@ #include #include #include +#include +#include #include "../../shared/util.h" - +#include "../shared/visualtestutil.h" #define EXPECT(cond) \ do { \ @@ -224,6 +226,72 @@ void tst_QQuickAccessible::quickAttachedProperties() } delete object; } + + // Check overriding of attached role for Text + { + QQmlEngine engine; + QQmlComponent component(&engine); + component.setData("import QtQuick 2.0\nText {\n" + "Accessible.role: Accessible.Button\n" + "Accessible.name: \"TextButton\"\n" + "Accessible.description: \"Text Button\"\n" + "}", QUrl()); + QObject *object = component.create(); + QVERIFY(object != nullptr); + + QObject *attachedObject = QQuickAccessibleAttached::attachedProperties(object); + QVERIFY(attachedObject); + if (attachedObject) { + QVariant p = attachedObject->property("role"); + QCOMPARE(p.isNull(), false); + QCOMPARE(p.toInt(), int(QAccessible::PushButton)); + p = attachedObject->property("name"); + QCOMPARE(p.isNull(), false); + QCOMPARE(p.toString(), QLatin1String("TextButton")); + p = attachedObject->property("description"); + QCOMPARE(p.isNull(), false); + QCOMPARE(p.toString(), QLatin1String("Text Button")); + } + delete object; + } + // Check overriding of attached role for Text + { + QQmlEngine engine; + QQmlComponent component(&engine); + component.setData("import QtQuick 2.0\nListView {\n" + "id: list\n" + "model: 5\n" + "delegate: Text {\n" + "objectName: \"acc_text\"\n" + "Accessible.role: Accessible.Button\n" + "Accessible.name: \"TextButton\"\n" + "Accessible.description: \"Text Button\"\n" + "}\n" + "}", QUrl()); + QObject *object = component.create(); + QVERIFY(object != nullptr); + + QQuickListView *listview = qobject_cast(object); + QVERIFY(listview != nullptr); + QQuickItem *contentItem = listview->contentItem(); + QQuickText *childItem = QQuickVisualTestUtil::findItem(contentItem, "acc_text"); + QVERIFY(childItem != nullptr); + + QObject *attachedObject = QQuickAccessibleAttached::attachedProperties(childItem); + QVERIFY(attachedObject); + if (attachedObject) { + QVariant p = attachedObject->property("role"); + QCOMPARE(p.isNull(), false); + QCOMPARE(p.toInt(), int(QAccessible::PushButton)); + p = attachedObject->property("name"); + QCOMPARE(p.isNull(), false); + QCOMPARE(p.toString(), QLatin1String("TextButton")); + p = attachedObject->property("description"); + QCOMPARE(p.isNull(), false); + QCOMPARE(p.toString(), QLatin1String("Text Button")); + } + delete object; + } QTestAccessibility::clearEvents(); } -- cgit v1.2.3