AvaloniaPropertyTests.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 Avalonia.Data;
  5. using Xunit;
  6. namespace Avalonia.Base.UnitTests
  7. {
  8. public class AvaloniaPropertyTests
  9. {
  10. [Fact]
  11. public void Constructor_Sets_Properties()
  12. {
  13. var target = new TestProperty<string>("test", typeof(Class1));
  14. Assert.Equal("test", target.Name);
  15. Assert.Equal(typeof(string), target.PropertyType);
  16. Assert.Equal(typeof(Class1), target.OwnerType);
  17. }
  18. [Fact]
  19. public void Name_Cannot_Contain_Periods()
  20. {
  21. Assert.Throws<ArgumentException>(() => new TestProperty<string>("Foo.Bar", typeof(Class1)));
  22. }
  23. [Fact]
  24. public void GetMetadata_Returns_Supplied_Value()
  25. {
  26. var metadata = new PropertyMetadata();
  27. var target = new TestProperty<string>("test", typeof(Class1), metadata);
  28. Assert.Same(metadata, target.GetMetadata<Class1>());
  29. }
  30. [Fact]
  31. public void GetMetadata_Returns_Supplied_Value_For_Derived_Class()
  32. {
  33. var metadata = new PropertyMetadata();
  34. var target = new TestProperty<string>("test", typeof(Class1), metadata);
  35. Assert.Same(metadata, target.GetMetadata<Class2>());
  36. }
  37. [Fact]
  38. public void GetMetadata_Returns_Supplied_Value_For_Unrelated_Class()
  39. {
  40. var metadata = new PropertyMetadata();
  41. var target = new TestProperty<string>("test", typeof(Class3), metadata);
  42. Assert.Same(metadata, target.GetMetadata<Class2>());
  43. }
  44. [Fact]
  45. public void GetMetadata_Returns_Overridden_Value()
  46. {
  47. var metadata = new PropertyMetadata();
  48. var overridden = new PropertyMetadata();
  49. var target = new TestProperty<string>("test", typeof(Class1), metadata);
  50. target.OverrideMetadata<Class2>(overridden);
  51. Assert.Same(overridden, target.GetMetadata<Class2>());
  52. }
  53. [Fact]
  54. public void OverrideMetadata_Should_Merge_Values()
  55. {
  56. var metadata = new PropertyMetadata(BindingMode.TwoWay);
  57. var notify = (Action<IAvaloniaObject, bool>)((a, b) => { });
  58. var overridden = new PropertyMetadata();
  59. var target = new TestProperty<string>("test", typeof(Class1), metadata);
  60. target.OverrideMetadata<Class2>(overridden);
  61. var result = target.GetMetadata<Class2>();
  62. Assert.Equal(BindingMode.TwoWay, result.DefaultBindingMode);
  63. }
  64. [Fact]
  65. public void Changed_Observable_Fired()
  66. {
  67. var target = new Class1();
  68. string value = null;
  69. Class1.FooProperty.Changed.Subscribe(x => value = (string)x.NewValue);
  70. target.SetValue(Class1.FooProperty, "newvalue");
  71. Assert.Equal("newvalue", value);
  72. }
  73. [Fact]
  74. public void Property_Equals_Should_Handle_Null()
  75. {
  76. var p1 = new TestProperty<string>("p1", typeof(Class1));
  77. Assert.NotNull(p1);
  78. Assert.NotNull(p1);
  79. Assert.False(p1 == null);
  80. Assert.False(null == p1);
  81. Assert.False(p1.Equals(null));
  82. Assert.True((AvaloniaProperty)null == (AvaloniaProperty)null);
  83. }
  84. [Fact]
  85. public void PropertyMetadata_BindingMode_Default_Returns_OneWay()
  86. {
  87. var data = new PropertyMetadata(defaultBindingMode: BindingMode.Default);
  88. Assert.Equal(BindingMode.OneWay, data.DefaultBindingMode);
  89. }
  90. private class TestProperty<TValue> : AvaloniaProperty<TValue>
  91. {
  92. public TestProperty(string name, Type ownerType, PropertyMetadata metadata = null)
  93. : base(name, ownerType, metadata ?? new PropertyMetadata())
  94. {
  95. }
  96. public void OverrideMetadata<T>(PropertyMetadata metadata)
  97. {
  98. OverrideMetadata(typeof(T), metadata);
  99. }
  100. internal override IDisposable RouteBind(
  101. IAvaloniaObject o,
  102. IObservable<BindingValue<object>> source,
  103. BindingPriority priority)
  104. {
  105. throw new NotImplementedException();
  106. }
  107. internal override void RouteClearValue(IAvaloniaObject o)
  108. {
  109. throw new NotImplementedException();
  110. }
  111. internal override object RouteGetValue(IAvaloniaObject o)
  112. {
  113. throw new NotImplementedException();
  114. }
  115. internal override void RouteInheritanceParentChanged(AvaloniaObject o, IAvaloniaObject oldParent)
  116. {
  117. throw new NotImplementedException();
  118. }
  119. internal override void RouteSetValue(
  120. IAvaloniaObject o,
  121. object value,
  122. BindingPriority priority)
  123. {
  124. throw new NotImplementedException();
  125. }
  126. }
  127. private class Class1 : AvaloniaObject
  128. {
  129. public static readonly StyledProperty<string> FooProperty =
  130. AvaloniaProperty.Register<Class1, string>("Foo", "default");
  131. }
  132. private class Class2 : Class1
  133. {
  134. }
  135. private class Class3 : AvaloniaObject
  136. {
  137. }
  138. }
  139. }