C++ testing API: Intreoduce the ElementHandle

This commit is contained in:
Olivier Goffart 2024-04-19 18:18:35 +02:00
parent 13fe59cc2e
commit 475ced0a62
15 changed files with 225 additions and 25 deletions

View file

@ -0,0 +1,130 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.2 OR LicenseRef-Slint-commercial
#include "slint.h"
#include "slint_testing_internal.h"
#include <optional>
#include <string_view>
#ifdef SLINT_FEATURE_TESTING
namespace slint::testing {
/// Init the testing backend.
/// Should be called before any other Slint function that can access the platform.
/// Then future windows will not appear on the screen anymore
inline void init()
{
cbindgen_private::slint_testing_init_backend();
}
/// A Handle to an element to query accessible property for testing purposes.
///
/// Use find_by_accessible_label() to obtain all elements matching the given accessible label.
class ElementHandle
{
cbindgen_private::ItemRc inner;
public:
/// Find all elements matching the given accessible label.
template<typename T>
static SharedVector<ElementHandle> find_by_accessible_label(const ComponentHandle<T> &component,
std::string_view label)
{
cbindgen_private::Slice<uint8_t> label_view {
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(label.data())),
label.size()
};
auto vrc = component.into_dyn();
SharedVector<ElementHandle> result;
cbindgen_private::slint_testing_element_find_by_accessible_label(
&vrc, &label_view,
reinterpret_cast<SharedVector<cbindgen_private::ItemRc> *>(&result));
return result;
}
/// Returns the accessible-label of that element, if any.
std::optional<SharedString> accessible_label() const
{
SharedString result;
if (inner.item_tree.vtable()->accessible_string_property(
inner.item_tree.borrow(), inner.index,
cbindgen_private::AccessibleStringProperty::Label, &result)) {
return result;
} else {
return std::nullopt;
}
}
/// Returns the accessible-value of that element, if any.
std::optional<SharedString> accessible_value() const
{
SharedString result;
if (inner.item_tree.vtable()->accessible_string_property(
inner.item_tree.borrow(), inner.index,
cbindgen_private::AccessibleStringProperty::Value, &result)) {
return result;
} else {
return std::nullopt;
}
}
/// Sets the accessible-value of that element.
///
/// Setting the value will invoke the `accessible-action-set-value` callback.
void set_accessible_value(SharedString value) const
{
union SetValueHelper {
cbindgen_private::AccessibilityAction action;
SetValueHelper(SharedString value)
// : action { .set_value = { cbindgen_private::AccessibilityAction::Tag::SetValue,
// std::move(value) } }
{
new (&action.set_value) cbindgen_private::AccessibilityAction::SetValue_Body {
cbindgen_private::AccessibilityAction::Tag::SetValue, std::move(value)
};
}
~SetValueHelper() { action.set_value.~SetValue_Body(); }
} action(std::move(value));
inner.item_tree.vtable()->accessibility_action(inner.item_tree.borrow(), inner.index,
&action.action);
}
/// Invokes the default accessibility action of that element (`accessible-action-default`).
void invoke_default_action() const
{
union DefaultActionHelper {
cbindgen_private::AccessibilityAction action;
DefaultActionHelper()
//: action { .tag = cbindgen_private::AccessibilityAction::Tag::Default }
{
action.tag = cbindgen_private::AccessibilityAction::Tag::Default;
}
~DefaultActionHelper() { }
} action;
inner.item_tree.vtable()->accessibility_action(inner.item_tree.borrow(), inner.index,
&action.action);
}
/// Returns the size of this element
LogicalSize size() const
{
auto rect = inner.item_tree.vtable()->item_geometry(inner.item_tree.borrow(), inner.index);
return LogicalSize({ rect.width, rect.height });
}
/// Returns the absolute position of this element
LogicalPosition absolute_position() const
{
cbindgen_private::LogicalRect rect =
inner.item_tree.vtable()->item_geometry(inner.item_tree.borrow(), inner.index);
cbindgen_private::LogicalPoint abs = slint::cbindgen_private::slint_item_absolute_position(
&inner.item_tree, inner.index);
return LogicalPosition({ abs.x + rect.x, abs.y + rect.y });
}
};
}
#endif // SLINT_FEATURE_TESTING