PriorityValueTests.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.Linq;
  5. using System.Reactive.Linq;
  6. using System.Reactive.Subjects;
  7. using Moq;
  8. using Xunit;
  9. namespace Avalonia.Base.UnitTests
  10. {
  11. public class PriorityValueTests
  12. {
  13. private static readonly AvaloniaProperty TestProperty =
  14. new StyledProperty<string>(
  15. "Test",
  16. typeof(PriorityValueTests),
  17. new StyledPropertyMetadata<string>());
  18. [Fact]
  19. public void Initial_Value_Should_Be_UnsetValue()
  20. {
  21. var target = new PriorityValue(null, TestProperty, typeof(string));
  22. Assert.Same(AvaloniaProperty.UnsetValue, target.Value);
  23. }
  24. [Fact]
  25. public void First_Binding_Sets_Value()
  26. {
  27. var target = new PriorityValue(null, TestProperty, typeof(string));
  28. target.Add(Single("foo"), 0);
  29. Assert.Equal("foo", target.Value);
  30. }
  31. [Fact]
  32. public void Changing_Binding_Should_Set_Value()
  33. {
  34. var target = new PriorityValue(null, TestProperty, typeof(string));
  35. var subject = new BehaviorSubject<string>("foo");
  36. target.Add(subject, 0);
  37. Assert.Equal("foo", target.Value);
  38. subject.OnNext("bar");
  39. Assert.Equal("bar", target.Value);
  40. }
  41. [Fact]
  42. public void Setting_Direct_Value_Should_Override_Binding()
  43. {
  44. var target = new PriorityValue(null, TestProperty, typeof(string));
  45. target.Add(Single("foo"), 0);
  46. target.SetValue("bar", 0);
  47. Assert.Equal("bar", target.Value);
  48. }
  49. [Fact]
  50. public void Binding_Firing_Should_Override_Direct_Value()
  51. {
  52. var target = new PriorityValue(null, TestProperty, typeof(string));
  53. var source = new BehaviorSubject<object>("initial");
  54. target.Add(source, 0);
  55. Assert.Equal("initial", target.Value);
  56. target.SetValue("first", 0);
  57. Assert.Equal("first", target.Value);
  58. source.OnNext("second");
  59. Assert.Equal("second", target.Value);
  60. }
  61. [Fact]
  62. public void Earlier_Binding_Firing_Should_Not_Override_Later()
  63. {
  64. var target = new PriorityValue(null, TestProperty, typeof(string));
  65. var nonActive = new BehaviorSubject<object>("na");
  66. var source = new BehaviorSubject<object>("initial");
  67. target.Add(nonActive, 1);
  68. target.Add(source, 1);
  69. Assert.Equal("initial", target.Value);
  70. target.SetValue("first", 1);
  71. Assert.Equal("first", target.Value);
  72. nonActive.OnNext("second");
  73. Assert.Equal("first", target.Value);
  74. }
  75. [Fact]
  76. public void Binding_Completing_Should_Revert_To_Direct_Value()
  77. {
  78. var target = new PriorityValue(null, TestProperty, typeof(string));
  79. var source = new BehaviorSubject<object>("initial");
  80. target.Add(source, 0);
  81. Assert.Equal("initial", target.Value);
  82. target.SetValue("first", 0);
  83. Assert.Equal("first", target.Value);
  84. source.OnNext("second");
  85. Assert.Equal("second", target.Value);
  86. source.OnCompleted();
  87. Assert.Equal("first", target.Value);
  88. }
  89. [Fact]
  90. public void Binding_With_Lower_Priority_Has_Precedence()
  91. {
  92. var target = new PriorityValue(null, TestProperty, typeof(string));
  93. target.Add(Single("foo"), 1);
  94. target.Add(Single("bar"), 0);
  95. target.Add(Single("baz"), 1);
  96. Assert.Equal("bar", target.Value);
  97. }
  98. [Fact]
  99. public void Later_Binding_With_Same_Priority_Should_Take_Precedence()
  100. {
  101. var target = new PriorityValue(null, TestProperty, typeof(string));
  102. target.Add(Single("foo"), 1);
  103. target.Add(Single("bar"), 0);
  104. target.Add(Single("baz"), 0);
  105. target.Add(Single("qux"), 1);
  106. Assert.Equal("baz", target.Value);
  107. }
  108. [Fact]
  109. public void Changing_Binding_With_Lower_Priority_Should_Set_Not_Value()
  110. {
  111. var target = new PriorityValue(null, TestProperty, typeof(string));
  112. var subject = new BehaviorSubject<string>("bar");
  113. target.Add(Single("foo"), 0);
  114. target.Add(subject, 1);
  115. Assert.Equal("foo", target.Value);
  116. subject.OnNext("baz");
  117. Assert.Equal("foo", target.Value);
  118. }
  119. [Fact]
  120. public void UnsetValue_Should_Fall_Back_To_Next_Binding()
  121. {
  122. var target = new PriorityValue(null, TestProperty, typeof(string));
  123. var subject = new BehaviorSubject<object>("bar");
  124. target.Add(subject, 0);
  125. target.Add(Single("foo"), 1);
  126. Assert.Equal("bar", target.Value);
  127. subject.OnNext(AvaloniaProperty.UnsetValue);
  128. Assert.Equal("foo", target.Value);
  129. }
  130. [Fact]
  131. public void Adding_Value_Should_Call_OnNext()
  132. {
  133. var owner = new Mock<IPriorityValueOwner>();
  134. var target = new PriorityValue(owner.Object, TestProperty, typeof(string));
  135. target.Add(Single("foo"), 0);
  136. owner.Verify(x => x.Changed(target.Property, target.ValuePriority, AvaloniaProperty.UnsetValue, "foo"));
  137. }
  138. [Fact]
  139. public void Changing_Value_Should_Call_OnNext()
  140. {
  141. var owner = new Mock<IPriorityValueOwner>();
  142. var target = new PriorityValue(owner.Object, TestProperty, typeof(string));
  143. var subject = new BehaviorSubject<object>("foo");
  144. target.Add(subject, 0);
  145. subject.OnNext("bar");
  146. owner.Verify(x => x.Changed(target.Property, target.ValuePriority, "foo", "bar"));
  147. }
  148. [Fact]
  149. public void Disposing_A_Binding_Should_Revert_To_Next_Value()
  150. {
  151. var target = new PriorityValue(null, TestProperty, typeof(string));
  152. target.Add(Single("foo"), 0);
  153. var disposable = target.Add(Single("bar"), 0);
  154. Assert.Equal("bar", target.Value);
  155. disposable.Dispose();
  156. Assert.Equal("foo", target.Value);
  157. }
  158. [Fact]
  159. public void Disposing_A_Binding_Should_Remove_BindingEntry()
  160. {
  161. var target = new PriorityValue(null, TestProperty, typeof(string));
  162. target.Add(Single("foo"), 0);
  163. var disposable = target.Add(Single("bar"), 0);
  164. Assert.Equal(2, target.GetBindings().Count());
  165. disposable.Dispose();
  166. Assert.Single(target.GetBindings());
  167. }
  168. [Fact]
  169. public void Completing_A_Binding_Should_Revert_To_Previous_Binding()
  170. {
  171. var target = new PriorityValue(null, TestProperty, typeof(string));
  172. var source = new BehaviorSubject<object>("bar");
  173. target.Add(Single("foo"), 0);
  174. target.Add(source, 0);
  175. Assert.Equal("bar", target.Value);
  176. source.OnCompleted();
  177. Assert.Equal("foo", target.Value);
  178. }
  179. [Fact]
  180. public void Completing_A_Binding_Should_Revert_To_Lower_Priority()
  181. {
  182. var target = new PriorityValue(null, TestProperty, typeof(string));
  183. var source = new BehaviorSubject<object>("bar");
  184. target.Add(Single("foo"), 1);
  185. target.Add(source, 0);
  186. Assert.Equal("bar", target.Value);
  187. source.OnCompleted();
  188. Assert.Equal("foo", target.Value);
  189. }
  190. [Fact]
  191. public void Completing_A_Binding_Should_Remove_BindingEntry()
  192. {
  193. var target = new PriorityValue(null, TestProperty, typeof(string));
  194. var subject = new BehaviorSubject<object>("bar");
  195. target.Add(Single("foo"), 0);
  196. target.Add(subject, 0);
  197. Assert.Equal(2, target.GetBindings().Count());
  198. subject.OnCompleted();
  199. Assert.Single(target.GetBindings());
  200. }
  201. [Fact]
  202. public void Direct_Value_Should_Be_Coerced()
  203. {
  204. var target = new PriorityValue(null, TestProperty, typeof(int), x => Math.Min((int)x, 10));
  205. target.SetValue(5, 0);
  206. Assert.Equal(5, target.Value);
  207. target.SetValue(15, 0);
  208. Assert.Equal(10, target.Value);
  209. }
  210. [Fact]
  211. public void Bound_Value_Should_Be_Coerced()
  212. {
  213. var target = new PriorityValue(null, TestProperty, typeof(int), x => Math.Min((int)x, 10));
  214. var source = new Subject<object>();
  215. target.Add(source, 0);
  216. source.OnNext(5);
  217. Assert.Equal(5, target.Value);
  218. source.OnNext(15);
  219. Assert.Equal(10, target.Value);
  220. }
  221. [Fact]
  222. public void Revalidate_Should_ReCoerce_Value()
  223. {
  224. var max = 10;
  225. var target = new PriorityValue(null, TestProperty, typeof(int), x => Math.Min((int)x, max));
  226. var source = new Subject<object>();
  227. target.Add(source, 0);
  228. source.OnNext(5);
  229. Assert.Equal(5, target.Value);
  230. source.OnNext(15);
  231. Assert.Equal(10, target.Value);
  232. max = 12;
  233. target.Revalidate();
  234. Assert.Equal(12, target.Value);
  235. }
  236. /// <summary>
  237. /// Returns an observable that returns a single value but does not complete.
  238. /// </summary>
  239. /// <typeparam name="T">The type of the observable.</typeparam>
  240. /// <param name="value">The value.</param>
  241. /// <returns>The observable.</returns>
  242. private IObservable<T> Single<T>(T value)
  243. {
  244. return Observable.Never<T>().StartWith(value);
  245. }
  246. }
  247. }