|
| 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 | +} |
0 commit comments