a11y: Don't notify about name/desc/id change if there was none

Return early if the setters are called with the same
string as is already set for the accessible name, description
or identifier. This avoids sending an event wrongly
notifying about a change when there was actually none.

Extend the `tst_QAccessibility::accessibleIdentifier`
autotest accordingly to test that no event is triggered when
setting the same ID again.

Thanks to Jan Arve Sæther for suggesting that in
the previous change introducing the accessibleIdentifier
property. (Implemented in a separate commit as
it is a preexisting issue for accessible name and
description.)

Change-Id: Id3af3f0c4769e93e4970be9db87734df9ef84212
Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
This commit is contained in:
Michael Weghorn 2024-07-16 11:31:58 +02:00
parent b8b7c58402
commit fb5e143305
2 changed files with 16 additions and 0 deletions

View File

@ -11801,6 +11801,9 @@ QString QWidget::whatsThis() const
void QWidget::setAccessibleName(const QString &name)
{
Q_D(QWidget);
if (d->accessibleName == name)
return;
d->accessibleName = name;
QAccessibleEvent event(this, QAccessible::NameChanged);
QAccessible::updateAccessibility(&event);
@ -11831,6 +11834,9 @@ QString QWidget::accessibleName() const
void QWidget::setAccessibleDescription(const QString &description)
{
Q_D(QWidget);
if (d->accessibleDescription == description)
return;
d->accessibleDescription = description;
QAccessibleEvent event(this, QAccessible::DescriptionChanged);
QAccessible::updateAccessibility(&event);
@ -11856,6 +11862,9 @@ QString QWidget::accessibleDescription() const
void QWidget::setAccessibleIdentifier(const QString &identifier)
{
Q_D(QWidget);
if (d->accessibleIdentifier == identifier)
return;
d->accessibleIdentifier = identifier;
QAccessibleEvent event(this, QAccessible::IdentifierChanged);
QAccessible::updateAccessibility(&event);

View File

@ -669,6 +669,13 @@ void tst_QAccessibility::accessibleIdentifier()
QVERIFY(QTestAccessibility::containsEvent(&event));
QCOMPARE(button.accessibleIdentifier(), id);
QCOMPARE(QAccessibleBridgeUtils::accessibleId(accessible), id);
QTestAccessibility::clearEvents();
// verify that no event gets triggered when setting the same ID again
button.setAccessibleIdentifier(id);
QVERIFY(QTestAccessibility::events().empty());
QCOMPARE(button.accessibleIdentifier(), id);
QCOMPARE(QAccessibleBridgeUtils::accessibleId(accessible), id);
QTestAccessibility::clearEvents();
}