LibreOffice Module test (master) 1
AccessibilityTools.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#pragma once
21
22#include <test/testdllapi.hxx>
23
24#include <functional>
25#include <string>
26
27#include <cppunit/TestAssert.h>
28
29#include <com/sun/star/accessibility/AccessibleEventObject.hpp>
30#include <com/sun/star/accessibility/XAccessible.hpp>
31#include <com/sun/star/accessibility/XAccessibleAction.hpp>
32#include <com/sun/star/accessibility/XAccessibleContext.hpp>
33#include <com/sun/star/accessibility/XAccessibleText.hpp>
34
36{
37public:
40 static const sal_Int32 MAX_CHILDREN = 500;
41
42 static css::uno::Reference<css::accessibility::XAccessibleContext>
44 const css::uno::Reference<css::accessibility::XAccessibleContext>& xCtx,
45 const std::function<
46 bool(const css::uno::Reference<css::accessibility::XAccessibleContext>&)>& cPredicate);
47 static css::uno::Reference<css::accessibility::XAccessibleContext>
49 const css::uno::Reference<css::accessibility::XAccessible>& xAcc,
50 const std::function<
51 bool(const css::uno::Reference<css::accessibility::XAccessibleContext>&)>& cPredicate);
52 static css::uno::Reference<css::accessibility::XAccessibleContext> getAccessibleObjectForRole(
53 const css::uno::Reference<css::accessibility::XAccessibleContext>& xCtx, sal_Int16 role);
54 static css::uno::Reference<css::accessibility::XAccessibleContext>
55 getAccessibleObjectForRole(const css::uno::Reference<css::accessibility::XAccessible>& xacc,
56 sal_Int16 role);
57
72 static css::uno::Reference<css::accessibility::XAccessibleContext> getAccessibleObjectForName(
73 const css::uno::Reference<css::accessibility::XAccessibleContext>& xCtx,
74 const sal_Int16 role, std::u16string_view name);
75 static inline css::uno::Reference<css::accessibility::XAccessibleContext>
76 getAccessibleObjectForName(const css::uno::Reference<css::accessibility::XAccessible>& xAcc,
77 const sal_Int16 role, std::u16string_view name)
78 {
79 return getAccessibleObjectForName(xAcc->getAccessibleContext(), role, name);
80 }
81
116 /* TODO: reimplement as IDDFS or BFS? Not sure the additional complexity/performance costs
117 * warrant it. */
118 template <typename... Ts>
119 static css::uno::Reference<css::accessibility::XAccessibleContext> getAccessibleObjectForName(
120 const css::uno::Reference<css::accessibility::XAccessibleContext>& xCtx,
121 const sal_Int16 role, std::u16string_view name, Ts... args)
122 {
123 auto nChildren = xCtx->getAccessibleChildCount();
124
125 // try self first
126 if (xCtx->getAccessibleRole() == role && nameEquals(xCtx, name))
127 {
128 for (decltype(nChildren) i = 0; i < nChildren && i < MAX_CHILDREN; i++)
129 {
130 if (auto xMatchChild
131 = getAccessibleObjectForName(xCtx->getAccessibleChild(i), args...))
132 return xMatchChild;
133 }
134 }
135
136 // if not found, try at a deeper level
137 for (decltype(nChildren) i = 0; i < nChildren && i < MAX_CHILDREN; i++)
138 {
139 if (auto xMatchChild
140 = getAccessibleObjectForName(xCtx->getAccessibleChild(i), role, name, args...))
141 return xMatchChild;
142 }
143
144 return nullptr;
145 }
146
147 template <typename... Ts>
148 static inline css::uno::Reference<css::accessibility::XAccessibleContext>
149 getAccessibleObjectForName(const css::uno::Reference<css::accessibility::XAccessible>& xAcc,
150 const sal_Int16 role, std::u16string_view name, Ts... args)
151 {
152 return getAccessibleObjectForName(xAcc->getAccessibleContext(), role, name, args...);
153 }
154
155 static bool equals(const css::uno::Reference<css::accessibility::XAccessible>& xacc1,
156 const css::uno::Reference<css::accessibility::XAccessible>& xacc2);
157 static bool equals(const css::uno::Reference<css::accessibility::XAccessibleContext>& xctx1,
158 const css::uno::Reference<css::accessibility::XAccessibleContext>& xctx2);
159
171 static bool nameEquals(const css::uno::Reference<css::accessibility::XAccessibleContext>& xCtx,
172 const std::u16string_view name);
173 static bool nameEquals(const css::uno::Reference<css::accessibility::XAccessible>& xAcc,
174 const std::u16string_view name)
175 {
176 return nameEquals(xAcc->getAccessibleContext(), name);
177 }
178
179 static OUString getRoleName(const sal_Int16 role);
180 static OUString getEventIdName(const sal_Int16 event_id);
181 static OUString getRelationTypeName(const sal_Int16 rel_type);
182
183 template <typename T> static std::string debugString(const css::uno::Reference<T>& x)
184 {
185 return debugString(x.get());
186 }
187
188 template <typename T> static std::string debugString(const T& x) { return debugString(&x); }
189
190 template <typename T> static std::string debugString(const T* p)
191 {
192 /* only the forwarding to debugName() might actually dereference @c p,
193 * and we rely on specializations to be as constant as possible and not
194 * violate the cast here. In practice it'll be the case for all types
195 * handle if we carefully write the specializations. In most case the
196 * specialization could take a const itself if the methods were
197 * properly marked const, but well. */
198 return debugString(const_cast<T*>(p));
199 }
200
201 template <typename T> static std::string debugString(T* p)
202 {
203 CPPUNIT_NS::OStringStream ost;
204
205 ost << "(" << static_cast<const void*>(p) << ")";
206 if (p != nullptr)
207 ost << " " << debugName(p);
208
209 return ost.str();
210 }
211
212 static OUString debugAccessibleStateSet(sal_Int64 p);
213
239 static bool Await(const std::function<bool()>& cUntilCallback, sal_uInt64 nTimeoutMs = 3000);
240
255 static void Wait(sal_uInt64 nTimeoutMs);
256
257private:
258 static OUString debugName(css::accessibility::XAccessibleContext* xctx);
259 static OUString debugName(css::accessibility::XAccessible* xacc);
260 static OUString debugName(const css::accessibility::AccessibleEventObject* evobj);
261 static OUString debugName(css::accessibility::XAccessibleAction* xAct);
262 static OUString debugName(css::accessibility::XAccessibleText* xTxt);
263};
264
265CPPUNIT_NS_BEGIN
266/* How to generate those automatically? We don't want to match all types
267 * not to mess up cppunit for types we don't support */
268#define AT_ASSERTION_TRAITS(T) \
269 template <> struct assertion_traits<css::uno::Reference<T>> \
270 { \
271 static bool equal(const css::uno::Reference<T>& x, const css::uno::Reference<T>& y) \
272 { \
273 return AccessibilityTools::equals(x, y); \
274 } \
275 \
276 static std::string toString(const css::uno::Reference<T>& x) \
277 { \
278 return AccessibilityTools::debugString(x); \
279 } \
280 }
281
282AT_ASSERTION_TRAITS(css::accessibility::XAccessible);
283AT_ASSERTION_TRAITS(css::accessibility::XAccessibleContext);
284
285#undef AT_ASSERTION_TRAITS
286
287CPPUNIT_NS_END
288
289/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
#define AT_ASSERTION_TRAITS(T)
static bool equals(const css::uno::Reference< css::accessibility::XAccessibleContext > &xctx1, const css::uno::Reference< css::accessibility::XAccessibleContext > &xctx2)
static css::uno::Reference< css::accessibility::XAccessibleContext > getAccessibleObjectForName(const css::uno::Reference< css::accessibility::XAccessible > &xAcc, const sal_Int16 role, std::u16string_view name)
static std::string debugString(const T &x)
static OUString debugName(css::accessibility::XAccessibleContext *xctx)
static css::uno::Reference< css::accessibility::XAccessibleContext > getAccessibleObjectForName(const css::uno::Reference< css::accessibility::XAccessibleContext > &xCtx, const sal_Int16 role, std::u16string_view name, Ts... args)
Gets a descendant of xCtx (or xCtx itself) that matches the last given role and name pair,...
static std::string debugString(const T *p)
static bool nameEquals(const css::uno::Reference< css::accessibility::XAccessibleContext > &xCtx, const std::u16string_view name)
Compares the accessible name against a string.
static css::uno::Reference< css::accessibility::XAccessibleContext > getAccessibleObjectForPredicate(const css::uno::Reference< css::accessibility::XAccessible > &xAcc, const std::function< bool(const css::uno::Reference< css::accessibility::XAccessibleContext > &)> &cPredicate)
static css::uno::Reference< css::accessibility::XAccessibleContext > getAccessibleObjectForRole(const css::uno::Reference< css::accessibility::XAccessibleContext > &xCtx, sal_Int16 role)
static OUString debugName(const css::accessibility::AccessibleEventObject *evobj)
static css::uno::Reference< css::accessibility::XAccessibleContext > getAccessibleObjectForName(const css::uno::Reference< css::accessibility::XAccessible > &xAcc, const sal_Int16 role, std::u16string_view name, Ts... args)
static std::string debugString(const css::uno::Reference< T > &x)
static OUString debugName(css::accessibility::XAccessibleText *xTxt)
static OUString debugName(css::accessibility::XAccessibleAction *xAct)
static std::string debugString(T *p)
static OUString debugName(css::accessibility::XAccessible *xacc)
static css::uno::Reference< css::accessibility::XAccessibleContext > getAccessibleObjectForPredicate(const css::uno::Reference< css::accessibility::XAccessibleContext > &xCtx, const std::function< bool(const css::uno::Reference< css::accessibility::XAccessibleContext > &)> &cPredicate)
static bool equals(const css::uno::Reference< css::accessibility::XAccessible > &xacc1, const css::uno::Reference< css::accessibility::XAccessible > &xacc2)
static bool nameEquals(const css::uno::Reference< css::accessibility::XAccessible > &xAcc, const std::u16string_view name)
float x
const char * name
void * p
int i
args
#define OOO_DLLPUBLIC_TEST
Definition: testdllapi.hxx:28