|
| 1 | +// Copyright (c) The Avalonia Project. All rights reserved. |
| 2 | +// Licensed under the MIT license. See licence.md file in the project root for full license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Reactive.Subjects; |
| 6 | +using Avalonia.Data; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Avalonia.Base.UnitTests |
| 10 | +{ |
| 11 | + public class AvaloniaObjectTests_Coercion |
| 12 | + { |
| 13 | + [Fact] |
| 14 | + public void Coerces_Set_Value() |
| 15 | + { |
| 16 | + var target = new Class1(); |
| 17 | + |
| 18 | + target.Foo = 150; |
| 19 | + |
| 20 | + Assert.Equal(100, target.Foo); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void Coerces_Bound_Value() |
| 25 | + { |
| 26 | + var target = new Class1(); |
| 27 | + var source = new Subject<BindingValue<int>>(); |
| 28 | + |
| 29 | + target.Bind(Class1.FooProperty, source); |
| 30 | + source.OnNext(150); |
| 31 | + |
| 32 | + Assert.Equal(100, target.Foo); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void CoerceValue_Updates_Value() |
| 37 | + { |
| 38 | + var target = new Class1 { Foo = 99 }; |
| 39 | + |
| 40 | + Assert.Equal(99, target.Foo); |
| 41 | + |
| 42 | + target.MaxFoo = 50; |
| 43 | + target.CoerceValue(Class1.FooProperty); |
| 44 | + |
| 45 | + Assert.Equal(50, target.Foo); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void Coerced_Value_Can_Be_Restored_If_Limit_Changed() |
| 50 | + { |
| 51 | + var target = new Class1(); |
| 52 | + |
| 53 | + target.Foo = 150; |
| 54 | + Assert.Equal(100, target.Foo); |
| 55 | + |
| 56 | + target.MaxFoo = 200; |
| 57 | + target.CoerceValue(Class1.FooProperty); |
| 58 | + |
| 59 | + Assert.Equal(150, target.Foo); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void Coerced_Value_Can_Be_Restored_From_Previously_Active_Binding() |
| 64 | + { |
| 65 | + var target = new Class1(); |
| 66 | + var source1 = new Subject<BindingValue<int>>(); |
| 67 | + var source2 = new Subject<BindingValue<int>>(); |
| 68 | + |
| 69 | + target.Bind(Class1.FooProperty, source1); |
| 70 | + source1.OnNext(150); |
| 71 | + |
| 72 | + target.Bind(Class1.FooProperty, source2); |
| 73 | + source2.OnNext(160); |
| 74 | + |
| 75 | + Assert.Equal(100, target.Foo); |
| 76 | + |
| 77 | + target.MaxFoo = 200; |
| 78 | + source2.OnCompleted(); |
| 79 | + |
| 80 | + Assert.Equal(150, target.Foo); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public void Coercion_Can_Be_Overridden() |
| 85 | + { |
| 86 | + var target = new Class2(); |
| 87 | + |
| 88 | + target.Foo = 150; |
| 89 | + |
| 90 | + Assert.Equal(-150, target.Foo); |
| 91 | + } |
| 92 | + |
| 93 | + private class Class1 : AvaloniaObject |
| 94 | + { |
| 95 | + public static readonly StyledProperty<int> FooProperty = |
| 96 | + AvaloniaProperty.Register<Class1, int>( |
| 97 | + "Qux", |
| 98 | + defaultValue: 11, |
| 99 | + coerce: CoerceFoo); |
| 100 | + |
| 101 | + public int Foo |
| 102 | + { |
| 103 | + get => GetValue(FooProperty); |
| 104 | + set => SetValue(FooProperty, value); |
| 105 | + } |
| 106 | + |
| 107 | + public int MaxFoo { get; set; } = 100; |
| 108 | + |
| 109 | + public static int CoerceFoo(IAvaloniaObject instance, int value) |
| 110 | + { |
| 111 | + return Math.Min(((Class1)instance).MaxFoo, value); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private class Class2 : AvaloniaObject |
| 116 | + { |
| 117 | + public static readonly StyledProperty<int> FooProperty = |
| 118 | + Class1.FooProperty.AddOwner<Class2>(); |
| 119 | + |
| 120 | + static Class2() |
| 121 | + { |
| 122 | + FooProperty.OverrideMetadata<Class2>( |
| 123 | + new StyledPropertyMetadata<int>( |
| 124 | + coerce: CoerceFoo)); |
| 125 | + } |
| 126 | + |
| 127 | + public int Foo |
| 128 | + { |
| 129 | + get => GetValue(FooProperty); |
| 130 | + set => SetValue(FooProperty, value); |
| 131 | + } |
| 132 | + |
| 133 | + public static int CoerceFoo(IAvaloniaObject instance, int value) |
| 134 | + { |
| 135 | + return -value; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments