AvaloniaObjectTests_Coercion.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Reactive.Subjects;
  3. using Avalonia.Data;
  4. using Xunit;
  5. namespace Avalonia.Base.UnitTests
  6. {
  7. public class AvaloniaObjectTests_Coercion
  8. {
  9. [Fact]
  10. public void Coerces_Set_Value()
  11. {
  12. var target = new Class1();
  13. target.Foo = 150;
  14. Assert.Equal(100, target.Foo);
  15. }
  16. [Fact]
  17. public void Coerces_Set_Value_Attached()
  18. {
  19. var target = new Class1();
  20. target.SetValue(Class1.AttachedProperty, 150);
  21. Assert.Equal(100, target.GetValue(Class1.AttachedProperty));
  22. }
  23. [Fact]
  24. public void Coerces_Bound_Value()
  25. {
  26. var target = new Class1();
  27. var source = new Subject<BindingValue<int>>();
  28. target.Bind(Class1.FooProperty, source);
  29. source.OnNext(150);
  30. Assert.Equal(100, target.Foo);
  31. }
  32. [Fact]
  33. public void CoerceValue_Updates_Value()
  34. {
  35. var target = new Class1 { Foo = 99 };
  36. Assert.Equal(99, target.Foo);
  37. target.MaxFoo = 50;
  38. target.CoerceValue(Class1.FooProperty);
  39. Assert.Equal(50, target.Foo);
  40. }
  41. [Fact]
  42. public void Coerced_Value_Can_Be_Restored_If_Limit_Changed()
  43. {
  44. var target = new Class1();
  45. target.Foo = 150;
  46. Assert.Equal(100, target.Foo);
  47. target.MaxFoo = 200;
  48. target.CoerceValue(Class1.FooProperty);
  49. Assert.Equal(150, target.Foo);
  50. }
  51. [Fact]
  52. public void Coerced_Value_Can_Be_Restored_From_Previously_Active_Binding()
  53. {
  54. var target = new Class1();
  55. var source1 = new Subject<BindingValue<int>>();
  56. var source2 = new Subject<BindingValue<int>>();
  57. target.Bind(Class1.FooProperty, source1);
  58. source1.OnNext(150);
  59. target.Bind(Class1.FooProperty, source2);
  60. source2.OnNext(160);
  61. Assert.Equal(100, target.Foo);
  62. target.MaxFoo = 200;
  63. source2.OnCompleted();
  64. Assert.Equal(150, target.Foo);
  65. }
  66. [Fact]
  67. public void Coercion_Can_Be_Overridden()
  68. {
  69. var target = new Class2();
  70. target.Foo = 150;
  71. Assert.Equal(-150, target.Foo);
  72. }
  73. private class Class1 : AvaloniaObject
  74. {
  75. public static readonly StyledProperty<int> FooProperty =
  76. AvaloniaProperty.Register<Class1, int>(
  77. "Qux",
  78. defaultValue: 11,
  79. coerce: CoerceFoo);
  80. public static readonly AttachedProperty<int> AttachedProperty =
  81. AvaloniaProperty.RegisterAttached<Class1, Class1, int>(
  82. "Attached",
  83. defaultValue: 11,
  84. coerce: CoerceFoo);
  85. public int Foo
  86. {
  87. get => GetValue(FooProperty);
  88. set => SetValue(FooProperty, value);
  89. }
  90. public int MaxFoo { get; set; } = 100;
  91. public static int CoerceFoo(IAvaloniaObject instance, int value)
  92. {
  93. return Math.Min(((Class1)instance).MaxFoo, value);
  94. }
  95. }
  96. private class Class2 : AvaloniaObject
  97. {
  98. public static readonly StyledProperty<int> FooProperty =
  99. Class1.FooProperty.AddOwner<Class2>();
  100. static Class2()
  101. {
  102. FooProperty.OverrideMetadata<Class2>(
  103. new StyledPropertyMetadata<int>(
  104. coerce: CoerceFoo));
  105. }
  106. public int Foo
  107. {
  108. get => GetValue(FooProperty);
  109. set => SetValue(FooProperty, value);
  110. }
  111. public static int CoerceFoo(IAvaloniaObject instance, int value)
  112. {
  113. return -value;
  114. }
  115. }
  116. }
  117. }