AvaloniaObjectTests_Validation.cs 4.1 KB

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