PerspexObjectTests_Direct.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 ReadOnly_Property_Cannot_Be_Set()
  134. {
  135. var target = new Class1();
  136. Assert.Throws<ArgumentException>(() =>
  137. target.SetValue(Class1.BarProperty, "newvalue"));
  138. }
  139. [Fact]
  140. public void ReadOnly_Property_Cannot_Be_Set_NonGeneric()
  141. {
  142. var target = new Class1();
  143. Assert.Throws<ArgumentException>(() =>
  144. target.SetValue((PerspexProperty)Class1.BarProperty, "newvalue"));
  145. }
  146. [Fact]
  147. public void ReadOnly_Property_Cannot_Be_Bound()
  148. {
  149. var target = new Class1();
  150. var source = new Subject<string>();
  151. Assert.Throws<ArgumentException>(() =>
  152. target.Bind(Class1.BarProperty, source));
  153. }
  154. [Fact]
  155. public void ReadOnly_Property_Cannot_Be_Bound_NonGeneric()
  156. {
  157. var target = new Class1();
  158. var source = new Subject<string>();
  159. Assert.Throws<ArgumentException>(() =>
  160. target.Bind(Class1.BarProperty, source));
  161. }
  162. [Fact]
  163. public void GetValue_Gets_Value_On_AddOwnered_Property()
  164. {
  165. var target = new Class2();
  166. Assert.Equal("initial2", target.GetValue(Class2.FooProperty));
  167. }
  168. [Fact]
  169. public void GetValue_Gets_Value_On_AddOwnered_Property_Using_Original()
  170. {
  171. var target = new Class2();
  172. Assert.Equal("initial2", target.GetValue(Class1.FooProperty));
  173. }
  174. [Fact]
  175. public void GetValue_Gets_Value_On_AddOwnered_Property_Using_Original_NonGeneric()
  176. {
  177. var target = new Class2();
  178. Assert.Equal("initial2", target.GetValue((PerspexProperty)Class1.FooProperty));
  179. }
  180. [Fact]
  181. public void SetValue_Sets_Value_On_AddOwnered_Property_Using_Original()
  182. {
  183. var target = new Class2();
  184. target.SetValue(Class1.FooProperty, "newvalue");
  185. Assert.Equal("newvalue", target.Foo);
  186. }
  187. [Fact]
  188. public void SetValue_Sets_Value_On_AddOwnered_Property_Using_Original_NonGeneric()
  189. {
  190. var target = new Class2();
  191. target.SetValue((PerspexProperty)Class1.FooProperty, "newvalue");
  192. Assert.Equal("newvalue", target.Foo);
  193. }
  194. [Fact]
  195. public void Bind_Binds_AddOwnered_Property_Value()
  196. {
  197. var target = new Class2();
  198. var source = new Subject<string>();
  199. var sub = target.Bind(Class1.FooProperty, source);
  200. Assert.Equal("initial2", target.Foo);
  201. source.OnNext("first");
  202. Assert.Equal("first", target.Foo);
  203. source.OnNext("second");
  204. Assert.Equal("second", target.Foo);
  205. sub.Dispose();
  206. source.OnNext("third");
  207. Assert.Equal("second", target.Foo);
  208. }
  209. [Fact]
  210. public void Bind_Binds_AddOwnered_Property_Value_NonGeneric()
  211. {
  212. var target = new Class2();
  213. var source = new Subject<string>();
  214. var sub = target.Bind((PerspexProperty)Class1.FooProperty, source);
  215. Assert.Equal("initial2", target.Foo);
  216. source.OnNext("first");
  217. Assert.Equal("first", target.Foo);
  218. source.OnNext("second");
  219. Assert.Equal("second", target.Foo);
  220. sub.Dispose();
  221. source.OnNext("third");
  222. Assert.Equal("second", target.Foo);
  223. }
  224. [Fact]
  225. public void Property_Notifies_Initialized()
  226. {
  227. Class1 target;
  228. bool raised = false;
  229. Class1.FooProperty.Initialized.Subscribe(e =>
  230. raised = e.Property == Class1.FooProperty &&
  231. e.OldValue == PerspexProperty.UnsetValue &&
  232. (string)e.NewValue == "initial" &&
  233. e.Priority == BindingPriority.Unset);
  234. target = new Class1();
  235. Assert.True(raised);
  236. }
  237. private class Class1 : PerspexObject
  238. {
  239. public static readonly PerspexProperty<string> FooProperty =
  240. PerspexProperty.RegisterDirect<Class1, string>("Foo", o => o.Foo, (o, v) => o.Foo = v);
  241. public static readonly PerspexProperty<string> BarProperty =
  242. PerspexProperty.RegisterDirect<Class1, string>("Bar", o => o.Bar);
  243. public static readonly PerspexProperty<int> BazProperty =
  244. PerspexProperty.RegisterDirect<Class1, int>("Bar", o => o.Baz, (o,v) => o.Baz = v);
  245. private string _foo = "initial";
  246. private string _bar = "bar";
  247. private int _baz = 5;
  248. public string Foo
  249. {
  250. get { return _foo; }
  251. set { SetAndRaise(FooProperty, ref _foo, value); }
  252. }
  253. public string Bar
  254. {
  255. get { return _bar; }
  256. }
  257. public int Baz
  258. {
  259. get { return _baz; }
  260. set { SetAndRaise(BazProperty, ref _baz, value); }
  261. }
  262. }
  263. private class Class2 : PerspexObject
  264. {
  265. public static readonly PerspexProperty<string> FooProperty =
  266. Class1.FooProperty.AddOwner<Class2>(o => o.Foo, (o, v) => o.Foo = v);
  267. private string _foo = "initial2";
  268. static Class2()
  269. {
  270. }
  271. public string Foo
  272. {
  273. get { return _foo; }
  274. set { SetAndRaise(FooProperty, ref _foo, value); }
  275. }
  276. }
  277. }
  278. }