PerspexObjectTests_Direct.cs 11 KB

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