include/test/a11y/AccessibilityTools.hxx | 19 ++++++++++++++ test/Library_subsequenttest.mk | 1 test/source/a11y/AccessibilityTools.cxx | 41 +++++++++++++++++++++++++++++++ test/source/a11y/accessibletestbase.cxx | 28 --------------------- 4 files changed, 62 insertions(+), 27 deletions(-)
New commits: commit 293db294e3408eda3f3133925fd307011b13c8d8 Author: Colomban Wendling <cwendl...@hypra.fr> AuthorDate: Wed Oct 19 18:31:43 2022 +0200 Commit: Michael Weghorn <m.wegh...@posteo.de> CommitDate: Wed Jan 25 08:22:01 2023 +0000 test: Add AccessibilityTools::nameEquals() Some accessible names contain a suffix when OSL_DEBUG_LEVEL > 0, which makes it tricky to compare to a known value. To fix that, introduce a helper that knows how and when to deal with the suffix properly that can then be used to easily check accessible names. As we already have a similar, yet private, helper for menu items on Windows (that include the keybinding label as a suffix), merge the two together in a unified solution for comparing names. Change-Id: I7e67edbc7817218ef3e097062d25888172056c21 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/142257 Tested-by: Jenkins Reviewed-by: Michael Weghorn <m.wegh...@posteo.de> diff --git a/include/test/a11y/AccessibilityTools.hxx b/include/test/a11y/AccessibilityTools.hxx index 1efd0b9f2960..65116a2d19d4 100644 --- a/include/test/a11y/AccessibilityTools.hxx +++ b/include/test/a11y/AccessibilityTools.hxx @@ -58,6 +58,25 @@ public: static bool equals(const css::uno::Reference<css::accessibility::XAccessibleContext>& xctx1, const css::uno::Reference<css::accessibility::XAccessibleContext>& xctx2); + /** + * @brief Compares the accessible name against a string + * @param xCtx A XAccessibleContext on which compare the name + * @param name The string to compare to + * @returns @c true if @p xCtx name matches @p name. + * + * This is conceptually equivalent to @code xCtx->getAccessibleName() == name @endcode, but + * handles the case OSL debugging is active and inserts a type suffix. Unless you know for + * sure the accessible you are comparing is not subject to those suffixes under debugging, + * always use this function instead of direct comparison. + */ + static bool nameEquals(const css::uno::Reference<css::accessibility::XAccessibleContext>& xCtx, + const std::u16string_view name); + static bool nameEquals(const css::uno::Reference<css::accessibility::XAccessible>& xAcc, + const std::u16string_view name) + { + return nameEquals(xAcc->getAccessibleContext(), name); + } + static OUString getRoleName(const sal_Int16 role); static OUString getStateName(const sal_Int16 state); static OUString getEventIdName(const sal_Int16 event_id); diff --git a/test/Library_subsequenttest.mk b/test/Library_subsequenttest.mk index 98eb17a5a557..1e94db958852 100644 --- a/test/Library_subsequenttest.mk +++ b/test/Library_subsequenttest.mk @@ -29,6 +29,7 @@ $(eval $(call gb_Library_use_libraries,subsequenttest,\ sal \ sfx \ test \ + tk \ tl \ utl \ unotest \ diff --git a/test/source/a11y/AccessibilityTools.cxx b/test/source/a11y/AccessibilityTools.cxx index 7a89ac2ac80f..8afc1687c889 100644 --- a/test/source/a11y/AccessibilityTools.cxx +++ b/test/source/a11y/AccessibilityTools.cxx @@ -27,8 +27,10 @@ #include <com/sun/star/accessibility/XAccessibleContext.hpp> #include <sal/log.hxx> +#include <toolkit/awt/vclxaccessiblecomponent.hxx> #include <vcl/scheduler.hxx> #include <vcl/timer.hxx> +#include <vcl/window.hxx> using namespace css; @@ -136,6 +138,45 @@ bool AccessibilityTools::equals(const uno::Reference<accessibility::XAccessibleC return equals(xctx1->getAccessibleParent(), xctx2->getAccessibleParent()); } +bool AccessibilityTools::nameEquals(const uno::Reference<accessibility::XAccessibleContext>& xCtx, + const std::u16string_view name) +{ + auto ctxName = xCtx->getAccessibleName(); + OUString rest; + + if (!ctxName.startsWith(name, &rest)) + return false; + if (rest == u"") + return true; + +#if defined(_WIN32) + // see OAccessibleMenuItemComponent::GetAccessibleName(): + // on Win32, ignore a \tSHORTCUT suffix on a menu item + switch (xCtx->getAccessibleRole()) + { + case accessibility::AccessibleRole::MENU_ITEM: + case accessibility::AccessibleRole::RADIO_MENU_ITEM: + case accessibility::AccessibleRole::CHECK_MENU_ITEM: + return rest[0] == '\t'; + + default: + break; + } +#endif + +#if OSL_DEBUG_LEVEL > 0 + // see VCLXAccessibleComponent::getAccessibleName() + auto pVCLXAccessibleComponent = dynamic_cast<VCLXAccessibleComponent*>(xCtx.get()); + if (pVCLXAccessibleComponent) + { + auto windowType = pVCLXAccessibleComponent->GetWindow()->GetType(); + if (rest == u" (Type = " + OUString::number(static_cast<sal_Int32>(windowType)) + ")") + return true; + } +#endif + return false; +} + static OUString unknownName(const sal_Int64 value) { return "unknown (" + OUString::number(value) + ")"; diff --git a/test/source/a11y/accessibletestbase.cxx b/test/source/a11y/accessibletestbase.cxx index 64151644348c..e7732e0d6a7d 100644 --- a/test/source/a11y/accessibletestbase.cxx +++ b/test/source/a11y/accessibletestbase.cxx @@ -193,32 +193,6 @@ void test::AccessibleTestBase::dumpA11YTree( } } -/* see OAccessibleMenuItemComponent::GetAccessibleName() */ -static bool accessibleNameMatches(const uno::Reference<accessibility::XAccessibleContext>& xContext, - std::u16string_view name) -{ - const OUString actualName = xContext->getAccessibleName(); - - if (actualName == name) - return true; - -#if defined(_WIN32) - /* on Win32, ignore a \tSHORTCUT suffix on a menu item */ - switch (xContext->getAccessibleRole()) - { - case accessibility::AccessibleRole::MENU_ITEM: - case accessibility::AccessibleRole::RADIO_MENU_ITEM: - case accessibility::AccessibleRole::CHECK_MENU_ITEM: - return actualName.startsWith(name) && actualName[name.length()] == '\t'; - - default: - break; - } -#endif - - return false; -} - /** Gets a child by name (usually in a menu) */ uno::Reference<accessibility::XAccessibleContext> test::AccessibleTestBase::getItemFromName( const uno::Reference<accessibility::XAccessibleContext>& xMenuCtx, std::u16string_view name) @@ -230,7 +204,7 @@ uno::Reference<accessibility::XAccessibleContext> test::AccessibleTestBase::getI for (sal_Int64 i = 0; i < childCount && i < AccessibilityTools::MAX_CHILDREN; i++) { auto item = xMenuCtx->getAccessibleChild(i)->getAccessibleContext(); - if (accessibleNameMatches(item, name)) + if (AccessibilityTools::nameEquals(item, name)) { std::cout << "-> found " << AccessibilityTools::debugString(item) << std::endl; return item;