PriorityValueTests.cs 10 KB

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