From fb5e1433055f8c309ed6943078f558b8cd72ddba Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Tue, 16 Jul 2024 11:31:58 +0200 Subject: [PATCH] a11y: Don't notify about name/desc/id change if there was none MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/widgets/kernel/qwidget.cpp | 9 +++++++++ tests/auto/other/qaccessibility/tst_qaccessibility.cpp | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp index afa3438b682..7d8539bcdbc 100644 --- a/src/widgets/kernel/qwidget.cpp +++ b/src/widgets/kernel/qwidget.cpp @@ -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); diff --git a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp index 5eb331b9300..09ce243145c 100644 --- a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp +++ b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp @@ -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(); }