1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4#define CATCH_CONFIG_MAIN
5#include "catch2/catch.hpp"
6
7#include <slint.h>
8#include <slint-interpreter.h>
9#include <slint-testing.h>
10
11SCENARIO("ElementHandle")
12{
13 using namespace slint::interpreter;
14 using namespace slint;
15
16 ComponentCompiler compiler;
17
18 auto result = compiler.build_from_source(
19 R"(
20 component ButtonBase {
21 @children
22 }
23 component PushButton inherits ButtonBase {
24 accessible-role: button;
25 in property <string> text <=> label.text;
26 label := Text {}
27 }
28 export component App {
29 VerticalLayout {
30 PushButton { text: "first"; }
31 second := PushButton { text: "second"; }
32 }
33 }
34 )",
35 "");
36 for (auto &&x : compiler.diagnostics())
37 std::cerr << x.message << std::endl;
38 REQUIRE(result.has_value());
39 auto component_definition = *result;
40
41 auto instance = component_definition.create();
42
43 SECTION("Find by accessible label")
44 {
45 auto elements = slint::testing::ElementHandle::find_by_accessible_label(instance, "first");
46 REQUIRE(elements.size() == 1);
47 REQUIRE(*elements[0].accessible_label() == "first");
48 REQUIRE(*elements[0].id() == "PushButton::label");
49 REQUIRE(*elements[0].type_name() == "Text");
50 REQUIRE((*elements[0].bases()).size() == 0);
51 }
52
53 SECTION("Find by id")
54 {
55 auto elements = slint::testing::ElementHandle::find_by_element_id(instance, "App::second");
56 REQUIRE(elements.size() == 1);
57 REQUIRE(*elements[0].id() == "App::second");
58 REQUIRE(*elements[0].type_name() == "PushButton");
59 REQUIRE((*elements[0].bases()).size() == 1);
60 REQUIRE((*elements[0].bases())[0] == "ButtonBase");
61 REQUIRE(*elements[0].accessible_role() == slint::testing::AccessibleRole::Button);
62 }
63
64 SECTION("Find by type name")
65 {
66 auto elements =
67 slint::testing::ElementHandle::find_by_element_type_name(instance, "PushButton");
68 REQUIRE(elements.size() == 2);
69 REQUIRE(*elements[0].id() == "");
70 REQUIRE(*elements[1].id() == "App::second");
71 }
72}
73

source code of slint/api/cpp/tests/testing.cpp