AvaloniaObjectTests_SetValue.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 AvaloniaObjectTests_SetValue
  9. {
  10. [Fact]
  11. public void ClearValue_Clears_Value()
  12. {
  13. Class1 target = new Class1();
  14. target.SetValue(Class1.FooProperty, "newvalue");
  15. target.ClearValue(Class1.FooProperty);
  16. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  17. }
  18. [Fact]
  19. public void SetValue_Sets_Value()
  20. {
  21. Class1 target = new Class1();
  22. target.SetValue(Class1.FooProperty, "newvalue");
  23. Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
  24. }
  25. [Fact]
  26. public void SetValue_Raises_PropertyChanged()
  27. {
  28. Class1 target = new Class1();
  29. bool raised = false;
  30. target.PropertyChanged += (s, e) =>
  31. {
  32. raised = s == target &&
  33. e.Property == Class1.FooProperty &&
  34. (string)e.OldValue == "foodefault" &&
  35. (string)e.NewValue == "newvalue";
  36. };
  37. target.SetValue(Class1.FooProperty, "newvalue");
  38. Assert.True(raised);
  39. }
  40. [Fact]
  41. public void SetValue_Doesnt_Raise_PropertyChanged_If_Value_Not_Changed()
  42. {
  43. Class1 target = new Class1();
  44. bool raised = false;
  45. target.SetValue(Class1.FooProperty, "bar");
  46. target.PropertyChanged += (s, e) =>
  47. {
  48. raised = true;
  49. };
  50. target.SetValue(Class1.FooProperty, "bar");
  51. Assert.False(raised);
  52. }
  53. [Fact]
  54. public void SetValue_Doesnt_Raise_PropertyChanged_If_Value_Not_Changed_From_Default()
  55. {
  56. Class1 target = new Class1();
  57. bool raised = false;
  58. target.PropertyChanged += (s, e) =>
  59. {
  60. raised = true;
  61. };
  62. target.SetValue(Class1.FooProperty, "foodefault");
  63. Assert.False(raised);
  64. }
  65. [Fact]
  66. public void SetValue_Throws_Exception_For_Unregistered_Property()
  67. {
  68. Class1 target = new Class1();
  69. Assert.Throws<ArgumentException>(() =>
  70. {
  71. target.SetValue(Class2.BarProperty, "invalid");
  72. });
  73. }
  74. [Fact]
  75. public void SetValue_Throws_Exception_For_Invalid_Value_Type()
  76. {
  77. Class1 target = new Class1();
  78. Assert.Throws<ArgumentException>(() =>
  79. {
  80. target.SetValue(Class1.FooProperty, 123);
  81. });
  82. }
  83. [Fact]
  84. public void SetValue_Of_Integer_On_Double_Property_Works()
  85. {
  86. Class2 target = new Class2();
  87. target.SetValue((AvaloniaProperty)Class2.FlobProperty, 4);
  88. var value = target.GetValue(Class2.FlobProperty);
  89. Assert.IsType<double>(value);
  90. Assert.Equal(4, value);
  91. }
  92. [Fact]
  93. public void SetValue_Respects_Implicit_Conversions()
  94. {
  95. Class2 target = new Class2();
  96. target.SetValue((AvaloniaProperty)Class2.FlobProperty, new ImplictDouble(4));
  97. var value = target.GetValue(Class2.FlobProperty);
  98. Assert.IsType<double>(value);
  99. Assert.Equal(4, value);
  100. }
  101. [Fact]
  102. public void SetValue_Can_Convert_To_Nullable()
  103. {
  104. Class2 target = new Class2();
  105. target.SetValue((AvaloniaProperty)Class2.FredProperty, 4.0);
  106. var value = target.GetValue(Class2.FredProperty);
  107. Assert.IsType<double>(value);
  108. Assert.Equal(4, value);
  109. }
  110. [Fact]
  111. public void SetValue_Respects_Priority()
  112. {
  113. Class1 target = new Class1();
  114. target.SetValue(Class1.FooProperty, "one", BindingPriority.TemplatedParent);
  115. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  116. target.SetValue(Class1.FooProperty, "two", BindingPriority.Style);
  117. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  118. target.SetValue(Class1.FooProperty, "three", BindingPriority.StyleTrigger);
  119. Assert.Equal("three", target.GetValue(Class1.FooProperty));
  120. }
  121. [Fact]
  122. public void Setting_UnsetValue_Reverts_To_Default_Value()
  123. {
  124. Class1 target = new Class1();
  125. target.SetValue(Class1.FooProperty, "newvalue");
  126. target.SetValue(Class1.FooProperty, AvaloniaProperty.UnsetValue);
  127. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  128. }
  129. private class Class1 : AvaloniaObject
  130. {
  131. public static readonly StyledProperty<string> FooProperty =
  132. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  133. }
  134. private class Class2 : Class1
  135. {
  136. public static readonly StyledProperty<string> BarProperty =
  137. AvaloniaProperty.Register<Class2, string>("Bar", "bardefault");
  138. public static readonly StyledProperty<double> FlobProperty =
  139. AvaloniaProperty.Register<Class2, double>("Flob");
  140. public static readonly StyledProperty<double?> FredProperty =
  141. AvaloniaProperty.Register<Class2, double?>("Fred");
  142. public Class1 Parent
  143. {
  144. get { return (Class1)InheritanceParent; }
  145. set { InheritanceParent = value; }
  146. }
  147. }
  148. private class ImplictDouble
  149. {
  150. public ImplictDouble(double value)
  151. {
  152. Value = value;
  153. }
  154. public double Value { get; }
  155. public static implicit operator double (ImplictDouble v)
  156. {
  157. return v.Value;
  158. }
  159. }
  160. }
  161. }