LibreOffice Module test (master) 1
accessibletestbase.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
10#pragma once
11
12#include <test/testdllapi.hxx>
13
14#include <deque>
15#include <string>
16
17#include <com/sun/star/accessibility/AccessibleRole.hpp>
18#include <com/sun/star/accessibility/XAccessible.hpp>
19#include <com/sun/star/accessibility/XAccessibleAction.hpp>
20#include <com/sun/star/accessibility/XAccessibleContext.hpp>
21#include <com/sun/star/awt/XDialog2.hpp>
22#include <com/sun/star/awt/XWindow.hpp>
23#include <com/sun/star/frame/Desktop.hpp>
24#include <com/sun/star/lang/XComponent.hpp>
25#include <com/sun/star/uno/Reference.hxx>
26
28#include <vcl/window.hxx>
29
30#include <rtl/ustring.hxx>
33
35
36namespace test
37{
39{
40protected:
41 css::uno::Reference<css::frame::XDesktop2> mxDesktop;
42 css::uno::Reference<css::lang::XComponent> mxDocument;
43 css::uno::Reference<css::awt::XWindow> mxWindow;
44
45 static bool isDocumentRole(const sal_Int16 role);
46
47 virtual void load(const rtl::OUString& sURL);
48 virtual void loadFromSrc(const rtl::OUString& sSrcPath);
49 void close();
50 css::uno::Reference<css::accessibility::XAccessibleContext> getWindowAccessibleContext();
51 virtual css::uno::Reference<css::accessibility::XAccessibleContext>
52 getDocumentAccessibleContext();
53
54 void documentPostKeyEvent(int nType, int nCharCode, int nKeyCode)
55 {
56 vcl::ITiledRenderable* pTiledRenderable
57 = dynamic_cast<vcl::ITiledRenderable*>(mxDocument.get());
58 CPPUNIT_ASSERT(pTiledRenderable);
59 pTiledRenderable->postKeyEvent(nType, nCharCode, nKeyCode);
60 }
61
62 static css::uno::Reference<css::accessibility::XAccessibleContext> getFirstRelationTargetOfType(
63 const css::uno::Reference<css::accessibility::XAccessibleContext>& xContext,
64 sal_Int16 relationType);
65
79 virtual std::deque<css::uno::Reference<css::accessibility::XAccessibleContext>>
80 getAllChildren(const css::uno::Reference<css::accessibility::XAccessibleContext>& xContext);
81
82 void dumpA11YTree(const css::uno::Reference<css::accessibility::XAccessibleContext>& xContext,
83 const int depth = 0);
84
85 css::uno::Reference<css::accessibility::XAccessibleContext>
86 getItemFromName(const css::uno::Reference<css::accessibility::XAccessibleContext>& xMenuCtx,
87 std::u16string_view name);
88 bool
89 activateMenuItem(const css::uno::Reference<css::accessibility::XAccessibleAction>& xAction);
90 /* just convenience not to have to query accessibility::XAccessibleAction manually */
91 bool activateMenuItem(const css::uno::Reference<css::accessibility::XAccessibleContext>& xCtx)
92 {
93 return activateMenuItem(css::uno::Reference<css::accessibility::XAccessibleAction>(
94 xCtx, css::uno::UNO_QUERY_THROW));
95 }
96
97 /* convenience to get a menu item from a list of menu item names. Unlike
98 * getItemFromName(context, name), this requires subsequently found items to implement
99 * XAccessibleAction, as each but the last item will be activated before looking for
100 * the next one, to account for the fact menus might not be fully populated before being
101 * activated. */
102 template <typename... Ts>
103 css::uno::Reference<css::accessibility::XAccessibleContext>
104 getItemFromName(const css::uno::Reference<css::accessibility::XAccessibleContext>& xMenuCtx,
105 std::u16string_view name, Ts... names)
106 {
107 auto item = getItemFromName(xMenuCtx, name);
108 CPPUNIT_ASSERT(item.is());
109 activateMenuItem(item);
110 return getItemFromName(item, names...);
111 }
112
113 /* convenience to activate an item by its name and all its parent menus up to xMenuCtx.
114 * @see getItemFromName() */
115 template <typename... Ts>
116 bool
117 activateMenuItem(const css::uno::Reference<css::accessibility::XAccessibleContext>& xMenuCtx,
118 Ts... names)
119 {
120 auto item = getItemFromName(xMenuCtx, names...);
121 CPPUNIT_ASSERT(item.is());
122 return activateMenuItem(item);
123 }
124
125 /* convenience to activate an item by its name and all its parent menus up to the main window
126 * menu bar */
127 template <typename... Ts> bool activateMenuItem(Ts... names)
128 {
130 getWindowAccessibleContext(), css::accessibility::AccessibleRole::MENU_BAR);
131 CPPUNIT_ASSERT(menuBar.is());
132 return activateMenuItem(menuBar, names...);
133 }
134
146 static css::uno::Reference<css::accessibility::XAccessibleContext>
147 getFocusedObject(const css::uno::Reference<css::accessibility::XAccessibleContext>& xCtx);
148
149 static inline css::uno::Reference<css::accessibility::XAccessibleContext>
150 getFocusedObject(const css::uno::Reference<css::accessibility::XAccessible>& xAcc)
151 {
152 return getFocusedObject(xAcc->getAccessibleContext());
153 }
154
175 static css::uno::Reference<css::accessibility::XAccessibleContext>
176 tabTo(const css::uno::Reference<css::accessibility::XAccessible>& xRoot, const sal_Int16 role,
177 const std::u16string_view name,
178 const EventPosterHelperBase* pEventPosterHelper = nullptr);
179
180 static bool tabTo(const css::uno::Reference<css::accessibility::XAccessible>& xRoot,
181 const css::uno::Reference<css::accessibility::XAccessibleContext>& xChild,
182 const EventPosterHelperBase* pEventPosterHelper = nullptr);
183
184#if !defined(MACOSX)
185 /* Dialog handling */
187 {
188 private:
190 css::uno::Reference<css::awt::XDialog2> mxDialog2;
191 css::uno::Reference<css::accessibility::XAccessible> mxAccessible;
192
193 public:
194 Dialog(css::uno::Reference<css::awt::XDialog2>& xDialog2, bool bAutoClose = true);
195 virtual ~Dialog();
196
197 void setAutoClose(bool bAutoClose) { mbAutoClose = bAutoClose; }
198
199 css::uno::Reference<css::accessibility::XAccessible> getAccessible() const
200 {
201 return mxAccessible;
202 }
203
204 void close(sal_Int32 result = VclResponseType::RET_CANCEL);
205
206 css::uno::Reference<css::accessibility::XAccessibleContext>
207 tabTo(const sal_Int16 role, const std::u16string_view name)
208 {
209 return AccessibleTestBase::tabTo(getAccessible(), role, name, this);
210 }
211
212 bool tabTo(const css::uno::Reference<css::accessibility::XAccessibleContext>& xChild)
213 {
214 return AccessibleTestBase::tabTo(getAccessible(), xChild, this);
215 }
216 };
217
219 {
220 public:
221 virtual ~DialogWaiter() {}
222
232 virtual bool waitEndDialog(sal_uInt64 nTimeoutMs = 3000) = 0;
233 };
234
287 static std::shared_ptr<DialogWaiter> awaitDialog(const std::u16string_view name,
288 std::function<void(Dialog&)> callback,
289 bool bAutoClose = true);
290#endif //defined(MACOSX)
291
292public:
293 virtual void setUp() override;
294 virtual void tearDown() override;
295};
296}
297
298/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
static css::uno::Reference< css::accessibility::XAccessibleContext > getAccessibleObjectForRole(const css::uno::Reference< css::accessibility::XAccessibleContext > &xCtx, sal_Int16 role)
Accessibility-specialized helper to send events to a window.
virtual bool waitEndDialog(sal_uInt64 nTimeoutMs=3000)=0
Waits for the associated dialog to close.
css::uno::Reference< css::accessibility::XAccessibleContext > tabTo(const sal_Int16 role, const std::u16string_view name)
css::uno::Reference< css::accessibility::XAccessible > mxAccessible
css::uno::Reference< css::awt::XDialog2 > mxDialog2
css::uno::Reference< css::accessibility::XAccessible > getAccessible() const
bool tabTo(const css::uno::Reference< css::accessibility::XAccessibleContext > &xChild)
bool activateMenuItem(const css::uno::Reference< css::accessibility::XAccessibleContext > &xMenuCtx, Ts... names)
bool activateMenuItem(Ts... names)
static css::uno::Reference< css::accessibility::XAccessibleContext > getFocusedObject(const css::uno::Reference< css::accessibility::XAccessibleContext > &xCtx)
Gets the focused accessible object at xAcc level or below.
css::uno::Reference< css::lang::XComponent > mxDocument
bool activateMenuItem(const css::uno::Reference< css::accessibility::XAccessibleContext > &xCtx)
bool activateMenuItem(const css::uno::Reference< css::accessibility::XAccessibleAction > &xAction)
static css::uno::Reference< css::accessibility::XAccessibleContext > getFocusedObject(const css::uno::Reference< css::accessibility::XAccessible > &xAcc)
css::uno::Reference< css::frame::XDesktop2 > mxDesktop
void documentPostKeyEvent(int nType, int nCharCode, int nKeyCode)
static bool tabTo(const css::uno::Reference< css::accessibility::XAccessible > &xRoot, const css::uno::Reference< css::accessibility::XAccessibleContext > &xChild, const EventPosterHelperBase *pEventPosterHelper=nullptr)
static css::uno::Reference< css::accessibility::XAccessibleContext > tabTo(const css::uno::Reference< css::accessibility::XAccessible > &xRoot, const sal_Int16 role, const std::u16string_view name, const EventPosterHelperBase *pEventPosterHelper=nullptr)
Navigates through focusable elements using the Tab keyboard shortcut.
css::uno::Reference< css::awt::XWindow > mxWindow
css::uno::Reference< css::accessibility::XAccessibleContext > getItemFromName(const css::uno::Reference< css::accessibility::XAccessibleContext > &xMenuCtx, std::u16string_view name, Ts... names)
css::uno::Reference< css::accessibility::XAccessibleContext > getItemFromName(const css::uno::Reference< css::accessibility::XAccessibleContext > &xMenuCtx, std::u16string_view name)
Base helper class to send events to a window.
Definition: eventposter.hxx:35
bool close
const char * name
QPRO_FUNC_TYPE nType
#define OOO_DLLPUBLIC_TEST
Definition: testdllapi.hxx:28