Skip to content

Commit 70dc629

Browse files
committed
tests: Add simple PropertyTarget test
1 parent 028aba1 commit 70dc629

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed

meson.build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ subdir('plugins/template')
168168
if get_option('documentation')
169169
subdir('docs')
170170
endif
171+
if get_option('tests')
172+
subdir('tests')
173+
endif
171174
subdir('po')
172175

173176
vapigen = find_program('vapigen', required: false)

meson_options.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
option ('documentation', type : 'boolean', value : false)
2+
option ('tests', type : 'boolean', value : false)
23
option ('systemd', type : 'boolean', value : true)
34
option ('systemduserunitdir', type : 'string', value : '')

tests/lib/PropertyTargetTest.vala

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright 2025 elementary, Inc. (https://elementary.io)
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
public class MockObject : Object {
7+
public int int_value { get; set; }
8+
public double double_value { get; set; }
9+
}
10+
11+
public class Gala.PropertyTargetTest : TestCase {
12+
private MockObject? target;
13+
private PropertyTarget? default_int_prop_target;
14+
15+
public PropertyTargetTest () {
16+
Object (name: "PropertyTarget");
17+
}
18+
19+
construct {
20+
add_test ("simple propagation", test_simple_propagation);
21+
add_test ("double propagation", test_double_propagation);
22+
add_test ("other actions", test_other_actions);
23+
add_test ("finalize object first", test_finalize_object_first);
24+
add_test ("finalize property target first", test_finalize_property_target_first);
25+
}
26+
27+
public override void set_up () {
28+
target = new MockObject ();
29+
default_int_prop_target = new PropertyTarget (
30+
MULTITASKING_VIEW,
31+
target,
32+
"int-value",
33+
typeof(int),
34+
0,
35+
10
36+
);
37+
}
38+
39+
public override void tear_down () {
40+
target = null;
41+
default_int_prop_target = null;
42+
}
43+
44+
private void test_simple_propagation () {
45+
assert_nonnull (&default_int_prop_target);
46+
assert_nonnull (&target);
47+
assert_cmpint (target.int_value, EQ, 0);
48+
49+
default_int_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 0.0);
50+
assert_cmpint (target.int_value, EQ, 0);
51+
52+
default_int_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 0.5);
53+
assert_cmpint (target.int_value, EQ, 5);
54+
55+
default_int_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 1.0);
56+
assert_cmpint (target.int_value, EQ, 10);
57+
58+
default_int_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 0.8);
59+
assert_cmpint (target.int_value, EQ, 8);
60+
61+
default_int_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 0.3);
62+
assert_cmpint (target.int_value, EQ, 3);
63+
64+
default_int_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 0.6);
65+
assert_cmpint (target.int_value, EQ, 6);
66+
67+
assert_finalize_object<MockObject> (ref target);
68+
assert_finalize_object<PropertyTarget> (ref default_int_prop_target);
69+
}
70+
71+
private void test_double_propagation () {
72+
var double_prop_target = new PropertyTarget (
73+
MULTITASKING_VIEW,
74+
target,
75+
"double-value",
76+
typeof(double),
77+
0.0,
78+
2.0
79+
);
80+
81+
assert_nonnull (&double_prop_target);
82+
assert_nonnull (&target);
83+
assert_cmpfloat (target.double_value, EQ, 0.0);
84+
85+
double_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 0.0);
86+
assert_cmpfloat (target.double_value, EQ, 0.0);
87+
88+
double_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 0.5);
89+
assert_cmpfloat (target.double_value, EQ, 1.0);
90+
91+
double_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 1.0);
92+
assert_cmpfloat (target.double_value, EQ, 2.0);
93+
94+
double_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 0.8);
95+
assert_cmpfloat (target.double_value, EQ, 1.6);
96+
97+
double_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 0.3);
98+
assert_cmpfloat (target.double_value, EQ, 0.6);
99+
100+
double_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 0.6);
101+
assert_cmpfloat (target.double_value, EQ, 1.2);
102+
103+
assert_finalize_object<MockObject> (ref target);
104+
assert_finalize_object<PropertyTarget> (ref double_prop_target);
105+
}
106+
107+
private void test_other_actions () {
108+
assert_nonnull (&default_int_prop_target);
109+
110+
assert_nonnull (&target);
111+
assert_cmpint (target.int_value, EQ, 0);
112+
113+
default_int_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 0.0);
114+
assert_cmpint (target.int_value, EQ, 0);
115+
116+
default_int_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 0.5);
117+
assert_cmpint (target.int_value, EQ, 5);
118+
119+
default_int_prop_target.propagate (UPDATE, SWITCH_WORKSPACE, 1.0);
120+
assert_cmpint (target.int_value, EQ, 5);
121+
122+
default_int_prop_target.propagate (UPDATE, CUSTOM, 1.0);
123+
assert_cmpint (target.int_value, EQ, 5);
124+
125+
default_int_prop_target.propagate (UPDATE, MULTITASKING_VIEW, 1.0);
126+
assert_cmpint (target.int_value, EQ, 10);
127+
128+
assert_finalize_object<MockObject> (ref target);
129+
assert_finalize_object<PropertyTarget> (ref default_int_prop_target);
130+
}
131+
132+
private void test_finalize_object_first () {
133+
assert_nonnull (&target);
134+
assert_nonnull (&default_int_prop_target);
135+
136+
// We can finalize the object before the property target because it doesn't hold a strong reference to it
137+
assert_finalize_object<MockObject> (ref target);
138+
assert_finalize_object<PropertyTarget> (ref default_int_prop_target);
139+
}
140+
141+
private void test_finalize_property_target_first () {
142+
assert_nonnull (&target);
143+
assert_nonnull (&default_int_prop_target);
144+
145+
// Finalize the property target before the object and make sure we don't have weak references
146+
// to the object (i.e. we don't crash when finalizing the object)
147+
assert_finalize_object<PropertyTarget> (ref default_int_prop_target);
148+
assert_finalize_object<MockObject> (ref target);
149+
}
150+
}
151+
152+
public int main (string[] args) {
153+
return new Gala.PropertyTargetTest ().run (args);
154+
}

tests/lib/meson.build

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
lib_test_sources = files(
2+
meson.project_source_root() / 'lib' / 'Gestures' / 'Gesture.vala',
3+
meson.project_source_root() / 'lib' / 'Gestures' / 'GestureTarget.vala',
4+
meson.project_source_root() / 'lib' / 'Gestures' / 'PropertyTarget.vala',
5+
)
6+
7+
tests = [
8+
'PropertyTargetTest',
9+
]
10+
11+
foreach test : tests
12+
test_executable = executable(
13+
test,
14+
'@[email protected]'.format(test),
15+
common_test_sources,
16+
lib_test_sources,
17+
dependencies: gala_base_dep,
18+
install: false,
19+
)
20+
21+
test(test, test_executable, suite: ['Gala', 'Gala/lib'])
22+
endforeach

tests/meson.build

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
common_test_sources = files(
2+
'TestCase.vala',
3+
)
4+
5+
subdir('lib')

0 commit comments

Comments
 (0)