Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / test / source / a11y / swaccessibletestbase.cxx
blobb43d65c0cf789691376610e8f3fd15f4602dda5a
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.
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 */
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>
21 using namespace css;
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);
46 if (!children.size())
47 return children;
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();
55 if (childParent.is()
56 && AccessibilityTools::equals(xContext, childParent->getAccessibleContext()))
57 children.push_front(child);
58 child = getPreviousFlowingSibling(child);
60 /* then forward */
61 child = getNextFlowingSibling(children.back());
62 while (child.is() && children.size() < AccessibilityTools::MAX_CHILDREN)
64 auto childParent = child->getAccessibleParent();
65 if (childParent.is()
66 && AccessibilityTools::equals(xContext, childParent->getAccessibleContext()))
67 children.push_back(child);
68 child = getNextFlowingSibling(child);
71 return children;
74 void test::SwAccessibleTestBase::collectText(
75 const uno::Reference<accessibility::XAccessibleContext>& xContext, rtl::OUStringBuffer& buffer,
76 bool onlyChildren)
78 const auto& roleName = AccessibilityTools::getRoleName(xContext->getAccessibleRole());
80 std::cout << "collecting text for child of role " << roleName << "..." << std::endl;
82 if (!onlyChildren)
84 const struct
86 std::u16string_view name;
87 rtl::OUString value;
88 } attrs[] = {
89 { u"name", xContext->getAccessibleName() },
90 { u"description", xContext->getAccessibleDescription() },
93 buffer.append('<');
94 buffer.append(roleName);
95 for (auto& attr : attrs)
97 if (attr.value.getLength() == 0)
98 continue;
99 buffer.append(' ');
100 buffer.append(attr.name);
101 buffer.append(u"=\"" + attr.value.replaceAll(u"\"", u"&quot;") + "\"");
103 buffer.append('>');
105 auto openTagLength = buffer.getLength();
107 uno::Reference<accessibility::XAccessibleText> xText(xContext, uno::UNO_QUERY);
108 if (xText.is())
109 buffer.append(xText->getText());
111 for (auto& childContext : getAllChildren(xContext))
112 collectText(childContext, buffer);
114 if (!onlyChildren)
116 if (buffer.getLength() != openTagLength)
117 buffer.append("</" + roleName + ">");
118 else
120 /* there was no content, so make is a short tag for more concise output */
121 buffer[openTagLength - 1] = '/';
122 buffer.append('>');
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: */