AvaloniaObjectTests_Validation.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. using System;
  4. using System.Reactive.Subjects;
  5. using Xunit;
  6. namespace Avalonia.Base.UnitTests
  7. {
  8. public class AvaloniaObjectTests_Validation
  9. {
  10. [Fact]
  11. public void SetValue_Causes_Validation()
  12. {
  13. var target = new Class1();
  14. target.SetValue(Class1.QuxProperty, 5);
  15. Assert.Throws<ArgumentOutOfRangeException>(() => target.SetValue(Class1.QuxProperty, 25));
  16. Assert.Equal(5, target.GetValue(Class1.QuxProperty));
  17. }
  18. [Fact]
  19. public void SetValue_Causes_Coercion()
  20. {
  21. var target = new Class1();
  22. target.SetValue(Class1.QuxProperty, 5);
  23. Assert.Equal(5, target.GetValue(Class1.QuxProperty));
  24. target.SetValue(Class1.QuxProperty, -5);
  25. Assert.Equal(0, target.GetValue(Class1.QuxProperty));
  26. target.SetValue(Class1.QuxProperty, 15);
  27. Assert.Equal(10, target.GetValue(Class1.QuxProperty));
  28. }
  29. [Fact]
  30. public void Revalidate_Causes_Recoercion()
  31. {
  32. var target = new Class1();
  33. target.SetValue(Class1.QuxProperty, 7);
  34. Assert.Equal(7, target.GetValue(Class1.QuxProperty));
  35. target.MaxQux = 5;
  36. target.Revalidate(Class1.QuxProperty);
  37. }
  38. [Fact]
  39. public void Validation_Can_Be_Overridden()
  40. {
  41. var target = new Class2();
  42. Assert.Throws<ArgumentOutOfRangeException>(() => target.SetValue(Class1.QuxProperty, 5));
  43. }
  44. [Fact]
  45. public void Validation_Can_Be_Overridden_With_Null()
  46. {
  47. var target = new Class3();
  48. target.SetValue(Class1.QuxProperty, 50);
  49. Assert.Equal(50, target.GetValue(Class1.QuxProperty));
  50. }
  51. [Fact]
  52. public void Binding_To_UnsetValue_Doesnt_Throw()
  53. {
  54. var target = new Class1();
  55. var source = new Subject<object>();
  56. target.Bind(Class1.QuxProperty, source);
  57. source.OnNext(AvaloniaProperty.UnsetValue);
  58. }
  59. [Fact]
  60. public void Attached_Property_Should_Be_Validated()
  61. {
  62. var target = new Class2();
  63. target.SetValue(Class1.AttachedProperty, 15);
  64. Assert.Equal(10, target.GetValue(Class1.AttachedProperty));
  65. }
  66. [Fact]
  67. public void PropertyChanged_Event_Uses_Coerced_Value()
  68. {
  69. var inst = new Class1();
  70. inst.PropertyChanged += (sender, e) =>
  71. {
  72. Assert.Equal(10, e.NewValue);
  73. };
  74. inst.SetValue(Class1.QuxProperty, 15);
  75. }
  76. private class Class1 : AvaloniaObject
  77. {
  78. public static readonly StyledProperty<int> QuxProperty =
  79. AvaloniaProperty.Register<Class1, int>("Qux", validate: Validate);
  80. public static readonly AttachedProperty<int> AttachedProperty =
  81. AvaloniaProperty.RegisterAttached<Class1, Class2, int>("Attached", validate: Validate);
  82. public Class1()
  83. {
  84. MaxQux = 10;
  85. ErrorQux = 20;
  86. }
  87. public int MaxQux { get; set; }
  88. public int ErrorQux { get; }
  89. private static int Validate(Class1 instance, int value)
  90. {
  91. if (value > instance.ErrorQux)
  92. {
  93. throw new ArgumentOutOfRangeException();
  94. }
  95. return Math.Min(Math.Max(value, 0), ((Class1)instance).MaxQux);
  96. }
  97. private static int Validate(Class2 instance, int value)
  98. {
  99. return Math.Min(value, 10);
  100. }
  101. }
  102. private class Class2 : AvaloniaObject
  103. {
  104. public static readonly StyledProperty<int> QuxProperty =
  105. Class1.QuxProperty.AddOwner<Class2>();
  106. static Class2()
  107. {
  108. QuxProperty.OverrideValidation<Class2>(Validate);
  109. }
  110. private static int Validate(Class2 instance, int value)
  111. {
  112. if (value < 100)
  113. {
  114. throw new ArgumentOutOfRangeException();
  115. }
  116. return value;
  117. }
  118. }
  119. private class Class3 : Class2
  120. {
  121. static Class3()
  122. {
  123. QuxProperty.OverrideValidation<Class3>(null);
  124. }
  125. }
  126. }
  127. }