PerspexObjectTests_Direct.cs 11 KB

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