PerspexObjectTests_Direct.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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.Collections.Generic;
  5. using System.Reactive.Subjects;
  6. using Xunit;
  7. namespace Perspex.Base.UnitTests
  8. {
  9. public class PerspexObjectTests_Direct
  10. {
  11. [Fact]
  12. public void GetValue_Gets_Value()
  13. {
  14. var target = new Class1();
  15. Assert.Equal("initial", target.GetValue(Class1.FooProperty));
  16. }
  17. [Fact]
  18. public void GetValue_Gets_Value_NonGeneric()
  19. {
  20. var target = new Class1();
  21. Assert.Equal("initial", target.GetValue((PerspexProperty)Class1.FooProperty));
  22. }
  23. [Fact]
  24. public void SetValue_Sets_Value()
  25. {
  26. var target = new Class1();
  27. target.SetValue(Class1.FooProperty, "newvalue");
  28. Assert.Equal("newvalue", target.Foo);
  29. }
  30. [Fact]
  31. public void SetValue_Sets_Value_NonGeneric()
  32. {
  33. var target = new Class1();
  34. target.SetValue((PerspexProperty)Class1.FooProperty, "newvalue");
  35. Assert.Equal("newvalue", target.Foo);
  36. }
  37. [Fact]
  38. public void SetValue_Raises_PropertyChanged()
  39. {
  40. var target = new Class1();
  41. bool raised = false;
  42. target.PropertyChanged += (s, e) =>
  43. raised = e.Property == Class1.FooProperty &&
  44. (string)e.OldValue == "initial" &&
  45. (string)e.NewValue == "newvalue" &&
  46. e.Priority == BindingPriority.LocalValue;
  47. target.SetValue(Class1.FooProperty, "newvalue");
  48. Assert.True(raised);
  49. }
  50. [Fact]
  51. public void SetValue_Raises_Changed()
  52. {
  53. var target = new Class1();
  54. bool raised = false;
  55. Class1.FooProperty.Changed.Subscribe(e =>
  56. raised = e.Property == Class1.FooProperty &&
  57. (string)e.OldValue == "initial" &&
  58. (string)e.NewValue == "newvalue" &&
  59. e.Priority == BindingPriority.LocalValue);
  60. target.SetValue(Class1.FooProperty, "newvalue");
  61. Assert.True(raised);
  62. }
  63. [Fact]
  64. public void GetObservable_Returns_Values()
  65. {
  66. var target = new Class1();
  67. List<string> values = new List<string>();
  68. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  69. target.Foo = "newvalue";
  70. Assert.Equal(new[] { "initial", "newvalue" }, values);
  71. }
  72. [Fact]
  73. public void Bind_Binds_Property_Value()
  74. {
  75. var target = new Class1();
  76. var source = new Subject<string>();
  77. var sub = target.Bind(Class1.FooProperty, source);
  78. Assert.Equal("initial", target.Foo);
  79. source.OnNext("first");
  80. Assert.Equal("first", target.Foo);
  81. source.OnNext("second");
  82. Assert.Equal("second", target.Foo);
  83. sub.Dispose();
  84. source.OnNext("third");
  85. Assert.Equal("second", target.Foo);
  86. }
  87. [Fact]
  88. public void Bind_Binds_Property_Value_NonGeneric()
  89. {
  90. var target = new Class1();
  91. var source = new Subject<string>();
  92. var sub = target.Bind((PerspexProperty)Class1.FooProperty, source);
  93. Assert.Equal("initial", target.Foo);
  94. source.OnNext("first");
  95. Assert.Equal("first", target.Foo);
  96. source.OnNext("second");
  97. Assert.Equal("second", target.Foo);
  98. sub.Dispose();
  99. source.OnNext("third");
  100. Assert.Equal("second", target.Foo);
  101. }
  102. [Fact]
  103. public void ReadOnly_Property_Cannot_Be_Set()
  104. {
  105. var target = new Class1();
  106. Assert.Throws<ArgumentException>(() =>
  107. target.SetValue(Class1.BarProperty, "newvalue"));
  108. }
  109. [Fact]
  110. public void ReadOnly_Property_Cannot_Be_Set_NonGeneric()
  111. {
  112. var target = new Class1();
  113. Assert.Throws<ArgumentException>(() =>
  114. target.SetValue((PerspexProperty)Class1.BarProperty, "newvalue"));
  115. }
  116. [Fact]
  117. public void ReadOnly_Property_Cannot_Be_Bound()
  118. {
  119. var target = new Class1();
  120. var source = new Subject<string>();
  121. Assert.Throws<ArgumentException>(() =>
  122. target.Bind(Class1.BarProperty, source));
  123. }
  124. [Fact]
  125. public void ReadOnly_Property_Cannot_Be_Bound_NonGeneric()
  126. {
  127. var target = new Class1();
  128. var source = new Subject<string>();
  129. Assert.Throws<ArgumentException>(() =>
  130. target.Bind(Class1.BarProperty, source));
  131. }
  132. [Fact]
  133. public void GetValue_Gets_Value_On_AddOwnered_Property()
  134. {
  135. var target = new Class2();
  136. Assert.Equal("initial2", target.GetValue(Class2.FooProperty));
  137. }
  138. [Fact]
  139. public void GetValue_Gets_Value_On_AddOwnered_Property_Using_Original()
  140. {
  141. var target = new Class2();
  142. Assert.Equal("initial2", target.GetValue(Class1.FooProperty));
  143. }
  144. [Fact]
  145. public void GetValue_Gets_Value_On_AddOwnered_Property_Using_Original_NonGeneric()
  146. {
  147. var target = new Class2();
  148. Assert.Equal("initial2", target.GetValue((PerspexProperty)Class1.FooProperty));
  149. }
  150. [Fact]
  151. public void SetValue_Sets_Value_On_AddOwnered_Property_Using_Original()
  152. {
  153. var target = new Class2();
  154. target.SetValue(Class1.FooProperty, "newvalue");
  155. Assert.Equal("newvalue", target.Foo);
  156. }
  157. [Fact]
  158. public void SetValue_Sets_Value_On_AddOwnered_Property_Using_Original_NonGeneric()
  159. {
  160. var target = new Class2();
  161. target.SetValue((PerspexProperty)Class1.FooProperty, "newvalue");
  162. Assert.Equal("newvalue", target.Foo);
  163. }
  164. [Fact]
  165. public void Bind_Binds_AddOwnered_Property_Value()
  166. {
  167. var target = new Class2();
  168. var source = new Subject<string>();
  169. var sub = target.Bind(Class1.FooProperty, source);
  170. Assert.Equal("initial2", target.Foo);
  171. source.OnNext("first");
  172. Assert.Equal("first", target.Foo);
  173. source.OnNext("second");
  174. Assert.Equal("second", target.Foo);
  175. sub.Dispose();
  176. source.OnNext("third");
  177. Assert.Equal("second", target.Foo);
  178. }
  179. [Fact]
  180. public void Bind_Binds_AddOwnered_Property_Value_NonGeneric()
  181. {
  182. var target = new Class2();
  183. var source = new Subject<string>();
  184. var sub = target.Bind((PerspexProperty)Class1.FooProperty, source);
  185. Assert.Equal("initial2", target.Foo);
  186. source.OnNext("first");
  187. Assert.Equal("first", target.Foo);
  188. source.OnNext("second");
  189. Assert.Equal("second", target.Foo);
  190. sub.Dispose();
  191. source.OnNext("third");
  192. Assert.Equal("second", target.Foo);
  193. }
  194. [Fact]
  195. public void Property_Notifies_Initialized()
  196. {
  197. Class1 target;
  198. bool raised = false;
  199. Class1.FooProperty.Initialized.Subscribe(e =>
  200. raised = e.Property == Class1.FooProperty &&
  201. e.OldValue == PerspexProperty.UnsetValue &&
  202. (string)e.NewValue == "initial" &&
  203. e.Priority == BindingPriority.Unset);
  204. target = new Class1();
  205. Assert.True(raised);
  206. }
  207. private class Class1 : PerspexObject
  208. {
  209. public static readonly PerspexProperty<string> FooProperty =
  210. PerspexProperty.RegisterDirect<Class1, string>("Foo", o => o.Foo, (o, v) => o.Foo = v);
  211. public static readonly PerspexProperty<string> BarProperty =
  212. PerspexProperty.RegisterDirect<Class1, string>("Bar", o => o.Bar);
  213. private string _foo = "initial";
  214. private string _bar = "bar";
  215. public string Foo
  216. {
  217. get { return _foo; }
  218. set { SetAndRaise(FooProperty, ref _foo, value); }
  219. }
  220. public string Bar
  221. {
  222. get { return _bar; }
  223. }
  224. }
  225. private class Class2 : PerspexObject
  226. {
  227. public static readonly PerspexProperty<string> FooProperty =
  228. Class1.FooProperty.AddOwner<Class2>(o => o.Foo, (o, v) => o.Foo = v);
  229. private string _foo = "initial2";
  230. static Class2()
  231. {
  232. }
  233. public string Foo
  234. {
  235. get { return _foo; }
  236. set { SetAndRaise(FooProperty, ref _foo, value); }
  237. }
  238. }
  239. }
  240. }