1 | #![allow (deprecated)]slint::slint!{#[include_path=r#"/input/slint/tests/driver/driverlib/../../cases/models"# ] |
2 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
3 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial |
4 | |
5 | export global Glob { |
6 | in-out property <[string]> model: ["Hello" , "World" ]; |
7 | in-out property <bool> condition: true; |
8 | callback clicked(string); |
9 | clicked => { |
10 | model = ["oops" ]; |
11 | } |
12 | in-out property <string> value; |
13 | } |
14 | |
15 | component Button { |
16 | callback clicked(); |
17 | in property <string> text; |
18 | Rectangle { |
19 | background: yellow; |
20 | Text { text: text; } |
21 | TouchArea { |
22 | clicked => {clicked();} |
23 | } |
24 | } |
25 | } |
26 | |
27 | |
28 | component TestCase inherits Window { |
29 | width: 300px; |
30 | height: 300px; |
31 | VerticalLayout { |
32 | for string in Glob.model: Rectangle { |
33 | if Glob.condition: Button { |
34 | text: string; |
35 | width: 80%; |
36 | height: 80%; |
37 | clicked => { |
38 | Glob.value = string; |
39 | Glob.clicked(string); |
40 | //Glob.value = string; // FIXME: this still crashes (#3464) |
41 | } |
42 | } |
43 | } |
44 | Rectangle {} |
45 | } |
46 | } |
47 | |
48 | /* |
49 | ```rust |
50 | let instance = TestCase::new().unwrap(); |
51 | let model = std::rc::Rc::new(slint::VecModel::<slint::SharedString>::from( |
52 | vec!["hello".into(), "world".into()])); |
53 | instance.global::<Glob<'_>>().set_model(model.clone().into()); |
54 | instance.global::<Glob<'_>>().on_clicked(move |val|{ |
55 | assert_eq!(val, "world"); |
56 | model.remove(1); |
57 | }); |
58 | slint_testing::send_mouse_click(&instance, 150., 150.); |
59 | assert_eq!(instance.global::<Glob<'_>>().get_value(), "world"); |
60 | ``` |
61 | |
62 | ```cpp |
63 | auto handle = TestCase::create(); |
64 | TestCase &instance = *handle; |
65 | |
66 | std::vector<slint::SharedString> array; |
67 | array.push_back("hello"); |
68 | array.push_back("world"); |
69 | auto model = std::make_shared<slint::VectorModel<slint::SharedString>>(std::move(array)); |
70 | instance.global<Glob>().set_model(model); |
71 | instance.global<Glob>().on_clicked([=](slint::SharedString val){ |
72 | assert_eq(val, "world"); |
73 | model->erase(1); |
74 | }); |
75 | slint_testing::send_mouse_click(&instance, 150., 150.); |
76 | assert_eq(instance.global<Glob>().get_value(), "world"); |
77 | ``` |
78 | |
79 | |
80 | ```js |
81 | var instance = new slint.TestCase({}); |
82 | let model = new slintlib.ArrayModel(["hello", "world"]); |
83 | instance.Glob.model = model; |
84 | instance.Glob.clicked = (val) => { |
85 | assert.equal(val, "world"); |
86 | model.remove(1, 1); |
87 | }; |
88 | slintlib.private_api.send_mouse_click(instance, 150., 150.); |
89 | assert.equal(instance.Glob.value, "world"); |
90 | ``` |
91 | |
92 | |
93 | */ |
94 | } |
95 | |
96 | #[test ] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> { |
97 | use i_slint_backend_testing as slint_testing; |
98 | slint_testing::init(); |
99 | let instance = TestCase::new().unwrap(); |
100 | let model: Rc> = std::rc::Rc::new(slint::VecModel::<slint::SharedString>::from( |
101 | vec!["hello" .into(), "world" .into()])); |
102 | instance.global::<Glob<'_>>().set_model(model.clone().into()); |
103 | instance.global::<Glob<'_>>().on_clicked(move |val|{ |
104 | assert_eq!(val, "world" ); |
105 | model.remove(index:1); |
106 | }); |
107 | slint_testing::send_mouse_click(&instance, x:150., y:150.); |
108 | assert_eq!(instance.global::<Glob<'_>>().get_value(), "world" ); |
109 | Ok(()) |
110 | } |