1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <test/a11y/swaccessibletestbase.hxx>
12 #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
13 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
14 #include <com/sun/star/accessibility/XAccessibleText.hpp>
15 #include <com/sun/star/uno/Reference.hxx>
17 #include <rtl/ustrbuf.hxx>
19 #include <test/a11y/AccessibilityTools.hxx>
23 uno::Reference
<accessibility::XAccessibleContext
>
24 test::SwAccessibleTestBase::getPreviousFlowingSibling(
25 const uno::Reference
<accessibility::XAccessibleContext
>& xContext
)
27 return getFirstRelationTargetOfType(xContext
,
28 accessibility::AccessibleRelationType::CONTENT_FLOWS_FROM
);
31 uno::Reference
<accessibility::XAccessibleContext
> test::SwAccessibleTestBase::getNextFlowingSibling(
32 const uno::Reference
<accessibility::XAccessibleContext
>& xContext
)
34 return getFirstRelationTargetOfType(xContext
,
35 accessibility::AccessibleRelationType::CONTENT_FLOWS_TO
);
38 /* Care has to be taken not to walk sideways as the relation is also used
39 * with children of nested containers (possibly as the "natural"/"perceived" flow?). */
40 std::deque
<uno::Reference
<accessibility::XAccessibleContext
>>
41 test::SwAccessibleTestBase::getAllChildren(
42 const uno::Reference
<accessibility::XAccessibleContext
>& xContext
)
44 /* first, get all "natural" children */
45 auto children
= AccessibleTestBase::getAllChildren(xContext
);
49 /* then, try and find flowing siblings at the same levels that are not included in the list */
50 /* first, backwards: */
51 auto child
= getPreviousFlowingSibling(children
.front());
52 while (child
.is() && children
.size() < AccessibilityTools::MAX_CHILDREN
)
54 auto childParent
= child
->getAccessibleParent();
56 && AccessibilityTools::equals(xContext
, childParent
->getAccessibleContext()))
57 children
.push_front(child
);
58 child
= getPreviousFlowingSibling(child
);
61 child
= getNextFlowingSibling(children
.back());
62 while (child
.is() && children
.size() < AccessibilityTools::MAX_CHILDREN
)
64 auto childParent
= child
->getAccessibleParent();
66 && AccessibilityTools::equals(xContext
, childParent
->getAccessibleContext()))
67 children
.push_back(child
);
68 child
= getNextFlowingSibling(child
);
74 void test::SwAccessibleTestBase::collectText(
75 const uno::Reference
<accessibility::XAccessibleContext
>& xContext
, rtl::OUStringBuffer
& buffer
,
78 const auto& roleName
= AccessibilityTools::getRoleName(xContext
->getAccessibleRole());
80 std::cout
<< "collecting text for child of role " << roleName
<< "..." << std::endl
;
86 std::u16string_view name
;
89 { u
"name", xContext
->getAccessibleName() },
90 { u
"description", xContext
->getAccessibleDescription() },
94 buffer
.append(roleName
);
95 for (auto& attr
: attrs
)
97 if (attr
.value
.getLength() == 0)
100 buffer
.append(attr
.name
);
101 buffer
.append(u
"=\"" + attr
.value
.replaceAll(u
"\"", u
""") + "\"");
105 auto openTagLength
= buffer
.getLength();
107 uno::Reference
<accessibility::XAccessibleText
> xText(xContext
, uno::UNO_QUERY
);
109 buffer
.append(xText
->getText());
111 for (auto& childContext
: getAllChildren(xContext
))
112 collectText(childContext
, buffer
);
116 if (buffer
.getLength() != openTagLength
)
117 buffer
.append("</" + roleName
+ ">");
120 /* there was no content, so make is a short tag for more concise output */
121 buffer
[openTagLength
- 1] = '/';
127 OUString
test::SwAccessibleTestBase::collectText(
128 const uno::Reference
<accessibility::XAccessibleContext
>& xContext
)
130 rtl::OUStringBuffer buf
;
131 collectText(xContext
, buf
, isDocumentRole(xContext
->getAccessibleRole()));
132 return buf
.makeStringAndClear();
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */