AvaloniaObjectTests_Validation.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_Lower_Priority_If_Style_Binding_Fails_Validation()
  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(10, target.GetValue(Class1.FooProperty));
  64. }
  65. [Fact]
  66. public void Reverts_To_DefaultValue_Even_In_Presence_Of_Other_Bindings()
  67. {
  68. var target = new Class1();
  69. var source1 = new Subject<int>();
  70. var source2 = new Subject<int>();
  71. target.Bind(Class1.FooProperty, source1);
  72. target.Bind(Class1.FooProperty, source2);
  73. source1.OnNext(42);
  74. source2.OnNext(150);
  75. Assert.Equal(11, target.GetValue(Class1.FooProperty));
  76. }
  77. private class Class1 : AvaloniaObject
  78. {
  79. public static readonly StyledProperty<int> FooProperty =
  80. AvaloniaProperty.Register<Class1, int>(
  81. "Qux",
  82. defaultValue: 11,
  83. validate: ValidateFoo);
  84. public static readonly AttachedProperty<int> AttachedProperty =
  85. AvaloniaProperty.RegisterAttached<Class1, Class1, int>(
  86. "Attached",
  87. defaultValue: 11,
  88. validate: ValidateFoo);
  89. public static bool ValidateFoo(int value)
  90. {
  91. return value < 100;
  92. }
  93. }
  94. private class Class2 : AvaloniaObject
  95. {
  96. }
  97. }
  98. }