RangeBaseTests.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 System.ComponentModel;
  5. using Avalonia.Controls.Primitives;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Data;
  8. using Avalonia.Markup.Data;
  9. using Avalonia.Styling;
  10. using Avalonia.UnitTests;
  11. using Xunit;
  12. namespace Avalonia.Controls.UnitTests.Primitives
  13. {
  14. public class RangeBaseTests
  15. {
  16. [Fact]
  17. public void Maximum_Should_Be_Coerced_To_Minimum()
  18. {
  19. var target = new TestRange
  20. {
  21. Minimum = 100,
  22. Maximum = 50,
  23. };
  24. var root = new TestRoot(target);
  25. Assert.Equal(100, target.Minimum);
  26. Assert.Equal(100, target.Maximum);
  27. }
  28. [Fact]
  29. public void Value_Should_Be_Coerced_To_Range()
  30. {
  31. var target = new TestRange
  32. {
  33. Minimum = 0,
  34. Maximum = 50,
  35. Value = 100,
  36. };
  37. var root = new TestRoot(target);
  38. Assert.Equal(0, target.Minimum);
  39. Assert.Equal(50, target.Maximum);
  40. Assert.Equal(50, target.Value);
  41. }
  42. [Fact]
  43. public void Changing_Minimum_Should_Coerce_Value_And_Maximum()
  44. {
  45. var target = new TestRange
  46. {
  47. Minimum = 0,
  48. Maximum = 100,
  49. Value = 50,
  50. };
  51. var root = new TestRoot(target);
  52. target.Minimum = 200;
  53. Assert.Equal(200, target.Minimum);
  54. Assert.Equal(200, target.Maximum);
  55. Assert.Equal(200, target.Value);
  56. }
  57. [Fact]
  58. public void Changing_Maximum_Should_Coerce_Value()
  59. {
  60. var target = new TestRange
  61. {
  62. Minimum = 0,
  63. Maximum = 100,
  64. Value = 100,
  65. };
  66. var root = new TestRoot(target);
  67. target.Maximum = 50;
  68. Assert.Equal(0, target.Minimum);
  69. Assert.Equal(50, target.Maximum);
  70. Assert.Equal(50, target.Value);
  71. }
  72. [Fact]
  73. public void Properties_Should_Not_Accept_Nan_And_Inifinity()
  74. {
  75. var target = new TestRange();
  76. Assert.Throws<ArgumentException>(() => target.Minimum = double.NaN);
  77. Assert.Throws<ArgumentException>(() => target.Minimum = double.PositiveInfinity);
  78. Assert.Throws<ArgumentException>(() => target.Minimum = double.NegativeInfinity);
  79. Assert.Throws<ArgumentException>(() => target.Maximum = double.NaN);
  80. Assert.Throws<ArgumentException>(() => target.Maximum = double.PositiveInfinity);
  81. Assert.Throws<ArgumentException>(() => target.Maximum = double.NegativeInfinity);
  82. Assert.Throws<ArgumentException>(() => target.Value = double.NaN);
  83. Assert.Throws<ArgumentException>(() => target.Value = double.PositiveInfinity);
  84. Assert.Throws<ArgumentException>(() => target.Value = double.NegativeInfinity);
  85. }
  86. [Theory]
  87. [InlineData(true)]
  88. [InlineData(false)]
  89. public void SetValue_Should_Not_Cause_StackOverflow(bool useXamlBinding)
  90. {
  91. var viewModel = new TestStackOverflowViewModel()
  92. {
  93. Value = 50
  94. };
  95. Track track = null;
  96. var target = new TestRange()
  97. {
  98. Template = new FuncControlTemplate<RangeBase>((c, scope) =>
  99. {
  100. track = new Track()
  101. {
  102. Width = 100,
  103. Orientation = Orientation.Horizontal,
  104. [~~Track.MinimumProperty] = c[~~RangeBase.MinimumProperty],
  105. [~~Track.MaximumProperty] = c[~~RangeBase.MaximumProperty],
  106. Name = "PART_Track",
  107. Thumb = new Thumb()
  108. }.RegisterInNameScope(scope);
  109. if (useXamlBinding)
  110. {
  111. track.Bind(Track.ValueProperty, new Binding("Value")
  112. {
  113. Mode = BindingMode.TwoWay,
  114. Source = c,
  115. Priority = BindingPriority.Style
  116. });
  117. }
  118. else
  119. {
  120. track[~~Track.ValueProperty] = c[~~RangeBase.ValueProperty];
  121. }
  122. return track.WithNameScope(scope);
  123. }),
  124. Minimum = 0,
  125. Maximum = 100,
  126. DataContext = viewModel
  127. };
  128. target.Bind(TestRange.ValueProperty, new Binding("Value") { Mode = BindingMode.TwoWay });
  129. target.ApplyTemplate();
  130. track.Measure(new Size(100, 0));
  131. track.Arrange(new Rect(0, 0, 100, 0));
  132. Assert.Equal(1, viewModel.SetterInvokedCount);
  133. // Issues #855 and #824 were causing a StackOverflowException at this point.
  134. target.Value = 51.001;
  135. Assert.Equal(2, viewModel.SetterInvokedCount);
  136. double expected = 51;
  137. Assert.Equal(expected, viewModel.Value);
  138. Assert.Equal(expected, target.Value);
  139. Assert.Equal(expected, track.Value);
  140. }
  141. [Fact]
  142. public void Coercion_Should_Not_Be_Done_During_Initialization()
  143. {
  144. var target = new TestRange();
  145. target.BeginInit();
  146. var root = new TestRoot(target);
  147. target.Minimum = 1;
  148. Assert.Equal(0, target.Value);
  149. target.Value = 50;
  150. target.EndInit();
  151. Assert.Equal(50, target.Value);
  152. }
  153. [Fact]
  154. public void Coercion_Should_Be_Done_After_Initialization()
  155. {
  156. var target = new TestRange();
  157. target.BeginInit();
  158. var root = new TestRoot(target);
  159. target.Minimum = 1;
  160. target.EndInit();
  161. Assert.Equal(1, target.Value);
  162. }
  163. private class TestRange : RangeBase
  164. {
  165. }
  166. private class TestStackOverflowViewModel : INotifyPropertyChanged
  167. {
  168. public int SetterInvokedCount { get; private set; }
  169. public const int MaxInvokedCount = 1000;
  170. private double _value;
  171. public event PropertyChangedEventHandler PropertyChanged;
  172. public double Value
  173. {
  174. get { return _value; }
  175. set
  176. {
  177. if (_value != value)
  178. {
  179. SetterInvokedCount++;
  180. if (SetterInvokedCount < MaxInvokedCount)
  181. {
  182. _value = (int)value;
  183. if (_value > 75) _value = 75;
  184. if (_value < 25) _value = 25;
  185. }
  186. else
  187. {
  188. _value = value;
  189. }
  190. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }