| 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 | export global Glob { |
| 5 | in-out property <int> v: 55; |
| 6 | in-out property <string> r; |
| 7 | changed v => { |
| 8 | r += "|" + v; |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | |
| 13 | component Chaining { |
| 14 | |
| 15 | public function do-change() { |
| 16 | chain-a +=1; |
| 17 | chain-f +=1; |
| 18 | chain-i +=1; |
| 19 | } |
| 20 | |
| 21 | property <int> chain-a; |
| 22 | out property <int> chain-a-count; |
| 23 | changed chain-a => { chain-a-count += 1; } |
| 24 | property <int> chain-b; |
| 25 | changed chain-b => { chain-a += 1; } |
| 26 | property <int> chain-c; |
| 27 | changed chain-c => { chain-b += 1; } |
| 28 | property <int> chain-d; |
| 29 | changed chain-d => { chain-c += 1; } |
| 30 | property <int> chain-e; |
| 31 | changed chain-e => { chain-d += 1; } |
| 32 | property <int> chain-f; |
| 33 | changed chain-f => { chain-e += 1; } |
| 34 | property <int> chain-g; |
| 35 | changed chain-g => { chain-f += 1; } |
| 36 | property <int> chain-h; |
| 37 | changed chain-h => { chain-g += 1; } |
| 38 | property <int> chain-i; |
| 39 | changed chain-i => { chain-h += 1; } |
| 40 | } |
| 41 | |
| 42 | component SubCompo { |
| 43 | in-out property <int> v: 456; |
| 44 | in-out property <string> result; |
| 45 | changed v => { |
| 46 | result += "sub(" +v+")" ; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | component SubCompoInline { |
| 51 | in-out property <int> v: 456; |
| 52 | in-out property <string> result; |
| 53 | changed v => { |
| 54 | result += "sub2(" +v+")" ; |
| 55 | } |
| 56 | @children |
| 57 | } |
| 58 | |
| 59 | component WithAliasToNative { |
| 60 | out property has-focus <=> ti.has_focus; |
| 61 | out property text <=> ti.text; |
| 62 | ti := TextInput {} |
| 63 | } |
| 64 | |
| 65 | |
| 66 | export component TestCase inherits Window { |
| 67 | in-out property <string> result; |
| 68 | in property <int> value: 56; |
| 69 | changed value => { |
| 70 | if false { return; } |
| 71 | result += "value(" + value + ")" ; |
| 72 | } |
| 73 | property <int> other: clamp(value + 1, 50, 100); |
| 74 | changed other => { |
| 75 | result += "other(" + other + ")" ; |
| 76 | debug("Other changed" ); |
| 77 | } |
| 78 | |
| 79 | out property<int> count; |
| 80 | changed result => { |
| 81 | count += 1; |
| 82 | } |
| 83 | |
| 84 | WithAliasToNative { |
| 85 | // just make sure this compiles despite has_focus being unused otherwise |
| 86 | changed has_focus => { debug(self.text); } |
| 87 | } |
| 88 | |
| 89 | chaining := Chaining {} |
| 90 | public function chaining-do-change() { chaining.do-change(); } |
| 91 | out property chaining-a-count <=> chaining.chain-a-count; |
| 92 | |
| 93 | sub2 := SubCompoInline { |
| 94 | v: 123; |
| 95 | changed v => { |
| 96 | self.result += "root2(" +self.v+")" ; |
| 97 | } |
| 98 | result <=> sub.result; |
| 99 | sub := SubCompo { |
| 100 | v: 789; |
| 101 | changed v => { |
| 102 | self.result += "root(" +self.v+")" ; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | public function sub-do-change() { sub.v += 1; sub2.v += 1; } |
| 107 | out property sub-result <=> sub.result; |
| 108 | changed sub-result => { |
| 109 | result += "||" + sub-result; |
| 110 | } |
| 111 | |
| 112 | Rectangle { |
| 113 | probably-optimized := Rectangle { |
| 114 | property <int> foo: other; |
| 115 | changed foo => { |
| 116 | result += "foo," ; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /* |
| 124 | |
| 125 | |
| 126 | ```rust |
| 127 | let instance = TestCase::new().unwrap(); |
| 128 | slint_testing::mock_elapsed_time(1000); |
| 129 | assert_eq!(instance.get_result(), ""); |
| 130 | instance.set_value(56); |
| 131 | slint_testing::mock_elapsed_time(1000); |
| 132 | assert_eq!(instance.get_result(), ""); // so far, nothing have changed |
| 133 | assert_eq!(instance.get_count(), 0); |
| 134 | instance.set_value(142); |
| 135 | assert_eq!(instance.get_result(), ""); |
| 136 | assert_eq!(instance.get_count(), 0); |
| 137 | slint_testing::mock_elapsed_time(1); |
| 138 | assert_eq!(instance.get_result(), "other(100)foo,value(142)"); |
| 139 | assert_eq!(instance.get_count(), 1); |
| 140 | instance.set_value(8); // this one is going to be merged in the other |
| 141 | instance.set_value(141); |
| 142 | slint_testing::mock_elapsed_time(1); |
| 143 | assert_eq!(instance.get_result(), "other(100)foo,value(142)value(141)"); |
| 144 | assert_eq!(instance.get_count(), 2); |
| 145 | |
| 146 | // Changing a value and back doesn't have effect |
| 147 | instance.set_value(85); |
| 148 | instance.set_value(141); |
| 149 | slint_testing::mock_elapsed_time(1); |
| 150 | assert_eq!(instance.get_result(), "other(100)foo,value(142)value(141)"); |
| 151 | assert_eq!(instance.get_count(), 2); |
| 152 | |
| 153 | instance.set_result("".into()); |
| 154 | instance.invoke_chaining_do_change(); |
| 155 | slint_testing::mock_elapsed_time(1); |
| 156 | assert_eq!(instance.get_chaining_a_count(), 3); |
| 157 | |
| 158 | assert_eq!(instance.get_sub_result(), ""); |
| 159 | instance.invoke_sub_do_change(); |
| 160 | slint_testing::mock_elapsed_time(100); |
| 161 | assert_eq!(instance.get_sub_result(), "sub2(124)root2(124)sub(790)root(790)"); |
| 162 | assert_eq!(instance.get_result(), "||sub2(124)root2(124)sub(790)root(790)"); |
| 163 | |
| 164 | // Global |
| 165 | instance.global::<Glob<'_>>().set_v(88); |
| 166 | assert_eq!(instance.global::<Glob<'_>>().get_r(), ""); |
| 167 | slint_testing::mock_elapsed_time(100); |
| 168 | assert_eq!(instance.global::<Glob<'_>>().get_r(), "|88"); |
| 169 | ``` |
| 170 | |
| 171 | ```cpp |
| 172 | auto handle = TestCase::create(); |
| 173 | const TestCase &instance = *handle; |
| 174 | slint_testing::mock_elapsed_time(1000); |
| 175 | assert_eq(instance.get_result(), ""); |
| 176 | instance.set_value(56); |
| 177 | slint_testing::mock_elapsed_time(1000); |
| 178 | assert_eq(instance.get_result(), ""); // so far, nothing have changed |
| 179 | assert_eq(instance.get_count(), 0); |
| 180 | instance.set_value(142); |
| 181 | assert_eq(instance.get_result(), ""); |
| 182 | assert_eq(instance.get_count(), 0); |
| 183 | slint_testing::mock_elapsed_time(1); |
| 184 | assert_eq(instance.get_result(), "other(100)foo,value(142)"); |
| 185 | assert_eq(instance.get_count(), 1); |
| 186 | instance.set_value(8); // this one is going to be merged in the other |
| 187 | instance.set_value(141); |
| 188 | slint_testing::mock_elapsed_time(1); |
| 189 | assert_eq(instance.get_result(), "other(100)foo,value(142)value(141)"); |
| 190 | assert_eq(instance.get_count(), 2); |
| 191 | |
| 192 | // Changing a value and back doesn't have effect |
| 193 | instance.set_value(85); |
| 194 | instance.set_value(141); |
| 195 | slint_testing::mock_elapsed_time(1); |
| 196 | assert_eq(instance.get_result(), "other(100)foo,value(142)value(141)"); |
| 197 | assert_eq(instance.get_count(), 2); |
| 198 | |
| 199 | instance.set_result(""); |
| 200 | instance.invoke_chaining_do_change(); |
| 201 | slint_testing::mock_elapsed_time(1); |
| 202 | assert_eq(instance.get_chaining_a_count(), 3); |
| 203 | |
| 204 | assert_eq(instance.get_sub_result(), ""); |
| 205 | instance.invoke_sub_do_change(); |
| 206 | slint_testing::mock_elapsed_time(100); |
| 207 | assert_eq(instance.get_sub_result(), "sub2(124)root2(124)sub(790)root(790)"); |
| 208 | assert_eq(instance.get_result(), "||sub2(124)root2(124)sub(790)root(790)"); |
| 209 | |
| 210 | // Global |
| 211 | instance.global<Glob>().set_v(88); |
| 212 | assert_eq(instance.global<Glob>().get_r(), ""); |
| 213 | slint_testing::mock_elapsed_time(100); |
| 214 | assert_eq(instance.global<Glob>().get_r(), "|88"); |
| 215 | ``` |
| 216 | |
| 217 | ```js |
| 218 | var instance = new slint.TestCase({}); |
| 219 | slintlib.private_api.mock_elapsed_time(1000); |
| 220 | assert.equal(instance.result, ""); |
| 221 | instance.value = 56; |
| 222 | slintlib.private_api.mock_elapsed_time(1000); |
| 223 | assert.equal(instance.result, ""); // so far, nothing have changed |
| 224 | instance.value = 142; |
| 225 | assert.equal(instance.result, ""); |
| 226 | slintlib.private_api.mock_elapsed_time(1); |
| 227 | assert.equal(instance.result, "other(100)foo,value(142)"); |
| 228 | instance.value = 8; // this one is going to be merged in the other |
| 229 | instance.value = 141; |
| 230 | slintlib.private_api.mock_elapsed_time(1); |
| 231 | assert.equal(instance.result, "other(100)foo,value(142)value(141)"); |
| 232 | |
| 233 | // Changing a value and back doesn't have effect |
| 234 | instance.value = 85; |
| 235 | instance.value = 141; |
| 236 | slintlib.private_api.mock_elapsed_time(1); |
| 237 | assert.equal(instance.result, "other(100)foo,value(142)value(141)"); |
| 238 | |
| 239 | instance.result = ""; |
| 240 | instance.chaining_do_change(); |
| 241 | slintlib.private_api.mock_elapsed_time(1); |
| 242 | assert.equal(instance.chaining_a_count, 3); |
| 243 | |
| 244 | assert.equal(instance.sub_result, ""); |
| 245 | instance.sub_do_change(); |
| 246 | slintlib.private_api.mock_elapsed_time(100); |
| 247 | assert.equal(instance.sub_result, "sub2(124)root2(124)sub(790)root(790)"); |
| 248 | assert.equal(instance.result, "||sub2(124)root2(124)sub(790)root(790)"); |
| 249 | |
| 250 | // Global |
| 251 | instance.Glob.v = 88; |
| 252 | assert.equal(instance.Glob.r, ""); |
| 253 | slintlib.private_api.mock_elapsed_time(100); |
| 254 | assert.equal(instance.Glob.r, "|88"); |
| 255 | ``` |
| 256 | |
| 257 | */ |
| 258 | |