PerspexObjectTests_Direct.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 GetValue_On_Unregistered_Property_Throws_Exception()
  25. {
  26. var target = new Class2();
  27. Assert.Throws<ArgumentException>(() => target.GetValue(Class1.BarProperty));
  28. }
  29. [Fact]
  30. public void SetValue_Sets_Value()
  31. {
  32. var target = new Class1();
  33. target.SetValue(Class1.FooProperty, "newvalue");
  34. Assert.Equal("newvalue", target.Foo);
  35. }
  36. [Fact]
  37. public void SetValue_Sets_Value_NonGeneric()
  38. {
  39. var target = new Class1();
  40. target.SetValue((PerspexProperty)Class1.FooProperty, "newvalue");
  41. Assert.Equal("newvalue", target.Foo);
  42. }
  43. [Fact]
  44. public void SetValue_NonGeneric_Coerces_UnsetValue_To_Default_Value()
  45. {
  46. var target = new Class1();
  47. target.SetValue((PerspexProperty)Class1.BazProperty, PerspexProperty.UnsetValue);
  48. Assert.Equal(0, target.Baz);
  49. }
  50. [Fact]
  51. public void SetValue_Raises_PropertyChanged()
  52. {
  53. var target = new Class1();
  54. bool raised = false;
  55. target.PropertyChanged += (s, 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 SetValue_Raises_Changed()
  65. {
  66. var target = new Class1();
  67. bool raised = false;
  68. Class1.FooProperty.Changed.Subscribe(e =>
  69. raised = e.Property == Class1.FooProperty &&
  70. (string)e.OldValue == "initial" &&
  71. (string)e.NewValue == "newvalue" &&
  72. e.Priority == BindingPriority.LocalValue);
  73. target.SetValue(Class1.FooProperty, "newvalue");
  74. Assert.True(raised);
  75. }
  76. [Fact]
  77. public void SetValue_On_Unregistered_Property_Throws_Exception()
  78. {
  79. var target = new Class2();
  80. Assert.Throws<ArgumentException>(() => target.SetValue(Class1.BarProperty, "value"));
  81. }
  82. [Fact]
  83. public void GetObservable_Returns_Values()
  84. {
  85. var target = new Class1();
  86. List<string> values = new List<string>();
  87. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  88. target.Foo = "newvalue";
  89. Assert.Equal(new[] { "initial", "newvalue" }, values);
  90. }
  91. [Fact]
  92. public void Bind_Binds_Property_Value()
  93. {
  94. var target = new Class1();
  95. var source = new Subject<string>();
  96. var sub = target.Bind(Class1.FooProperty, source);
  97. Assert.Equal("initial", target.Foo);
  98. source.OnNext("first");
  99. Assert.Equal("first", target.Foo);
  100. source.OnNext("second");
  101. Assert.Equal("second", target.Foo);
  102. sub.Dispose();
  103. source.OnNext("third");
  104. Assert.Equal("second", target.Foo);
  105. }
  106. [Fact]
  107. public void Bind_Binds_Property_Value_NonGeneric()
  108. {
  109. var target = new Class1();
  110. var source = new Subject<string>();
  111. var sub = target.Bind((PerspexProperty)Class1.FooProperty, source);
  112. Assert.Equal("initial", target.Foo);
  113. source.OnNext("first");
  114. Assert.Equal("first", target.Foo);
  115. source.OnNext("second");
  116. Assert.Equal("second", target.Foo);
  117. sub.Dispose();
  118. source.OnNext("third");
  119. Assert.Equal("second", target.Foo);
  120. }
  121. [Fact]
  122. public void Bind_NonGeneric_Coerces_UnsetValue()
  123. {
  124. var target = new Class1();
  125. var source = new Subject<object>();
  126. var sub = target.Bind((PerspexProperty)Class1.BazProperty, source);
  127. Assert.Equal(5, target.Baz);
  128. source.OnNext(6);
  129. Assert.Equal(6, target.Baz);
  130. source.OnNext(PerspexProperty.UnsetValue);
  131. Assert.Equal(0, target.Baz);
  132. }
  133. [Fact]
  134. public void Bind_Handles_Wrong_Type()
  135. {
  136. var target = new Class1();
  137. var source = new Subject<object>();
  138. var sub = target.Bind(Class1.FooProperty, source);
  139. source.OnNext(45);
  140. Assert.Equal(null, target.Foo);
  141. }
  142. [Fact]
  143. public void Bind_Handles_Wrong_Value_Type()
  144. {
  145. var target = new Class1();
  146. var source = new Subject<object>();
  147. var sub = target.Bind(Class1.BazProperty, source);
  148. source.OnNext("foo");
  149. Assert.Equal(0, target.Baz);
  150. }
  151. [Fact]
  152. public void ReadOnly_Property_Cannot_Be_Set()
  153. {
  154. var target = new Class1();
  155. Assert.Throws<ArgumentException>(() =>
  156. target.SetValue(Class1.BarProperty, "newvalue"));
  157. }
  158. [Fact]
  159. public void ReadOnly_Property_Cannot_Be_Set_NonGeneric()
  160. {
  161. var target = new Class1();
  162. Assert.Throws<ArgumentException>(() =>
  163. target.SetValue((PerspexProperty)Class1.BarProperty, "newvalue"));
  164. }
  165. [Fact]
  166. public void ReadOnly_Property_Cannot_Be_Bound()
  167. {
  168. var target = new Class1();
  169. var source = new Subject<string>();
  170. Assert.Throws<ArgumentException>(() =>
  171. target.Bind(Class1.BarProperty, source));
  172. }
  173. [Fact]
  174. public void ReadOnly_Property_Cannot_Be_Bound_NonGeneric()
  175. {
  176. var target = new Class1();
  177. var source = new Subject<string>();
  178. Assert.Throws<ArgumentException>(() =>
  179. target.Bind(Class1.BarProperty, source));
  180. }
  181. [Fact]
  182. public void GetValue_Gets_Value_On_AddOwnered_Property()
  183. {
  184. var target = new Class2();
  185. Assert.Equal("initial2", target.GetValue(Class2.FooProperty));
  186. }
  187. [Fact]
  188. public void GetValue_Gets_Value_On_AddOwnered_Property_Using_Original()
  189. {
  190. var target = new Class2();
  191. Assert.Equal("initial2", target.GetValue(Class1.FooProperty));
  192. }
  193. [Fact]
  194. public void GetValue_Gets_Value_On_AddOwnered_Property_Using_Original_NonGeneric()
  195. {
  196. var target = new Class2();
  197. Assert.Equal("initial2", target.GetValue((PerspexProperty)Class1.FooProperty));
  198. }
  199. [Fact]
  200. public void SetValue_Sets_Value_On_AddOwnered_Property_Using_Original()
  201. {
  202. var target = new Class2();
  203. target.SetValue(Class1.FooProperty, "newvalue");
  204. Assert.Equal("newvalue", target.Foo);
  205. }
  206. [Fact]
  207. public void SetValue_Sets_Value_On_AddOwnered_Property_Using_Original_NonGeneric()
  208. {
  209. var target = new Class2();
  210. target.SetValue((PerspexProperty)Class1.FooProperty, "newvalue");
  211. Assert.Equal("newvalue", target.Foo);
  212. }
  213. [Fact]
  214. public void Bind_Binds_AddOwnered_Property_Value()
  215. {
  216. var target = new Class2();
  217. var source = new Subject<string>();
  218. var sub = target.Bind(Class1.FooProperty, source);
  219. Assert.Equal("initial2", target.Foo);
  220. source.OnNext("first");
  221. Assert.Equal("first", target.Foo);
  222. source.OnNext("second");
  223. Assert.Equal("second", target.Foo);
  224. sub.Dispose();
  225. source.OnNext("third");
  226. Assert.Equal("second", target.Foo);
  227. }
  228. [Fact]
  229. public void Bind_Binds_AddOwnered_Property_Value_NonGeneric()
  230. {
  231. var target = new Class2();
  232. var source = new Subject<string>();
  233. var sub = target.Bind((PerspexProperty)Class1.FooProperty, source);
  234. Assert.Equal("initial2", target.Foo);
  235. source.OnNext("first");
  236. Assert.Equal("first", target.Foo);
  237. source.OnNext("second");
  238. Assert.Equal("second", target.Foo);
  239. sub.Dispose();
  240. source.OnNext("third");
  241. Assert.Equal("second", target.Foo);
  242. }
  243. [Fact]
  244. public void Property_Notifies_Initialized()
  245. {
  246. Class1 target;
  247. bool raised = false;
  248. Class1.FooProperty.Initialized.Subscribe(e =>
  249. raised = e.Property == Class1.FooProperty &&
  250. e.OldValue == PerspexProperty.UnsetValue &&
  251. (string)e.NewValue == "initial" &&
  252. e.Priority == BindingPriority.Unset);
  253. target = new Class1();
  254. Assert.True(raised);
  255. }
  256. private class Class1 : PerspexObject
  257. {
  258. public static readonly PerspexProperty<string> FooProperty =
  259. PerspexProperty.RegisterDirect<Class1, string>("Foo", o => o.Foo, (o, v) => o.Foo = v);
  260. public static readonly PerspexProperty<string> BarProperty =
  261. PerspexProperty.RegisterDirect<Class1, string>("Bar", o => o.Bar);
  262. public static readonly PerspexProperty<int> BazProperty =
  263. PerspexProperty.RegisterDirect<Class1, int>("Bar", o => o.Baz, (o,v) => o.Baz = v);
  264. private string _foo = "initial";
  265. private string _bar = "bar";
  266. private int _baz = 5;
  267. public string Foo
  268. {
  269. get { return _foo; }
  270. set { SetAndRaise(FooProperty, ref _foo, value); }
  271. }
  272. public string Bar
  273. {
  274. get { return _bar; }
  275. }
  276. public int Baz
  277. {
  278. get { return _baz; }
  279. set { SetAndRaise(BazProperty, ref _baz, value); }
  280. }
  281. }
  282. private class Class2 : PerspexObject
  283. {
  284. public static readonly PerspexProperty<string> FooProperty =
  285. Class1.FooProperty.AddOwner<Class2>(o => o.Foo, (o, v) => o.Foo = v);
  286. private string _foo = "initial2";
  287. static Class2()
  288. {
  289. }
  290. public string Foo
  291. {
  292. get { return _foo; }
  293. set { SetAndRaise(FooProperty, ref _foo, value); }
  294. }
  295. }
  296. }
  297. }