AvaloniaObjectTests_Validation.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Reactive.Subjects;
  3. using Avalonia.Controls;
  4. using Avalonia.Data;
  5. using Xunit;
  6. namespace Avalonia.Base.UnitTests
  7. {
  8. public class AvaloniaObjectTests_Validation
  9. {
  10. [Fact]
  11. public void Registration_Throws_If_DefaultValue_Fails_Validation()
  12. {
  13. Assert.Throws<ArgumentException>(() =>
  14. new StyledProperty<int>(
  15. "BadDefault",
  16. typeof(Class1),
  17. typeof(Class1),
  18. new StyledPropertyMetadata<int>(101),
  19. validate: Class1.ValidateFoo));
  20. }
  21. [Fact]
  22. public void Metadata_Override_Throws_If_DefaultValue_Fails_Validation()
  23. {
  24. Assert.Throws<ArgumentException>(() => Class1.FooProperty.OverrideDefaultValue<Class2>(101));
  25. }
  26. [Fact]
  27. public void SetValue_Throws_If_Fails_Validation()
  28. {
  29. var target = new Class1();
  30. Assert.Throws<ArgumentException>(() => target.SetValue(Class1.FooProperty, 101));
  31. }
  32. [Fact]
  33. public void SetValue_Throws_If_Fails_Validation_Attached()
  34. {
  35. var target = new Class1();
  36. Assert.Throws<ArgumentException>(() => target.SetValue(Class1.AttachedProperty, 101));
  37. }
  38. [Fact]
  39. public void Reverts_To_DefaultValue_If_LocalValue_Binding_Fails_Validation()
  40. {
  41. var target = new Class1();
  42. var source = new Subject<int>();
  43. target.Bind(Class1.FooProperty, source);
  44. source.OnNext(150);
  45. Assert.Equal(11, target.GetValue(Class1.FooProperty));
  46. }
  47. [Fact]
  48. public void Reverts_To_DefaultValue_If_Style_Binding_Fails_Validation()
  49. {
  50. var target = new Class1();
  51. var source = new Subject<int>();
  52. target.Bind(Class1.FooProperty, source, BindingPriority.Style);
  53. source.OnNext(150);
  54. Assert.Equal(11, target.GetValue(Class1.FooProperty));
  55. }
  56. [Fact]
  57. public void Reverts_To_DefaultValue_If_Style_Binding_Fails_Validation_2()
  58. {
  59. var target = new Class1();
  60. var source = new Subject<int>();
  61. target.SetValue(Class1.FooProperty, 10, BindingPriority.Style);
  62. target.Bind(Class1.FooProperty, source, BindingPriority.StyleTrigger);
  63. source.OnNext(150);
  64. Assert.Equal(11, target.GetValue(Class1.FooProperty));
  65. }
  66. [Theory]
  67. [InlineData(BindingPriority.LocalValue)]
  68. [InlineData(BindingPriority.Style)]
  69. public void Reverts_To_DefaultValue_If_Style_Binding_Fails_Validation_3(BindingPriority priority)
  70. {
  71. var target = new Class1();
  72. var source = new Subject<BindingValue<int>>();
  73. target.Bind(Class1.FooProperty, source, priority);
  74. source.OnNext(150);
  75. Assert.Equal(11, target.GetValue(Class1.FooProperty));
  76. }
  77. [Fact]
  78. public void Reverts_To_DefaultValue_Even_In_Presence_Of_Other_Bindings()
  79. {
  80. var target = new Class1();
  81. var source1 = new Subject<int>();
  82. var source2 = new Subject<int>();
  83. target.Bind(Class1.FooProperty, source1);
  84. target.Bind(Class1.FooProperty, source2);
  85. source1.OnNext(42);
  86. source2.OnNext(150);
  87. Assert.Equal(11, target.GetValue(Class1.FooProperty));
  88. }
  89. private class Class1 : AvaloniaObject
  90. {
  91. public static readonly StyledProperty<int> FooProperty =
  92. AvaloniaProperty.Register<Class1, int>(
  93. "Qux",
  94. defaultValue: 11,
  95. validate: ValidateFoo);
  96. public static readonly AttachedProperty<int> AttachedProperty =
  97. AvaloniaProperty.RegisterAttached<Class1, Class1, int>(
  98. "Attached",
  99. defaultValue: 11,
  100. validate: ValidateFoo);
  101. public static bool ValidateFoo(int value)
  102. {
  103. return value < 100;
  104. }
  105. }
  106. private class Class2 : AvaloniaObject
  107. {
  108. }
  109. }
  110. }