AvaloniaObjectTests_Direct.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // Copyright (c) The Avalonia 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 Avalonia;
  7. using Avalonia.Data;
  8. using Avalonia.Logging;
  9. using Avalonia.UnitTests;
  10. using Xunit;
  11. namespace Avalonia.Base.UnitTests
  12. {
  13. public class AvaloniaObjectTests_Direct
  14. {
  15. [Fact]
  16. public void GetValue_Gets_Value()
  17. {
  18. var target = new Class1();
  19. Assert.Equal("initial", target.GetValue(Class1.FooProperty));
  20. }
  21. [Fact]
  22. public void GetValue_Gets_Value_NonGeneric()
  23. {
  24. var target = new Class1();
  25. Assert.Equal("initial", target.GetValue((AvaloniaProperty)Class1.FooProperty));
  26. }
  27. [Fact]
  28. public void GetValue_On_Unregistered_Property_Throws_Exception()
  29. {
  30. var target = new Class2();
  31. Assert.Throws<ArgumentException>(() => target.GetValue(Class1.BarProperty));
  32. }
  33. [Fact]
  34. public void SetValue_Sets_Value()
  35. {
  36. var target = new Class1();
  37. target.SetValue(Class1.FooProperty, "newvalue");
  38. Assert.Equal("newvalue", target.Foo);
  39. }
  40. [Fact]
  41. public void SetValue_Sets_Value_NonGeneric()
  42. {
  43. var target = new Class1();
  44. target.SetValue((AvaloniaProperty)Class1.FooProperty, "newvalue");
  45. Assert.Equal("newvalue", target.Foo);
  46. }
  47. [Fact]
  48. public void SetValue_NonGeneric_Coerces_UnsetValue_To_Default_Value()
  49. {
  50. var target = new Class1();
  51. target.SetValue((AvaloniaProperty)Class1.BazProperty, AvaloniaProperty.UnsetValue);
  52. Assert.Equal(-1, target.Baz);
  53. }
  54. [Fact]
  55. public void SetValue_Raises_PropertyChanged()
  56. {
  57. var target = new Class1();
  58. bool raised = false;
  59. target.PropertyChanged += (s, e) =>
  60. raised = e.Property == Class1.FooProperty &&
  61. (string)e.OldValue == "initial" &&
  62. (string)e.NewValue == "newvalue" &&
  63. e.Priority == BindingPriority.LocalValue;
  64. target.SetValue(Class1.FooProperty, "newvalue");
  65. Assert.True(raised);
  66. }
  67. [Fact]
  68. public void SetValue_Raises_Changed()
  69. {
  70. var target = new Class1();
  71. bool raised = false;
  72. Class1.FooProperty.Changed.Subscribe(e =>
  73. raised = e.Property == Class1.FooProperty &&
  74. (string)e.OldValue == "initial" &&
  75. (string)e.NewValue == "newvalue" &&
  76. e.Priority == BindingPriority.LocalValue);
  77. target.SetValue(Class1.FooProperty, "newvalue");
  78. Assert.True(raised);
  79. }
  80. [Fact]
  81. public void SetValue_On_Unregistered_Property_Throws_Exception()
  82. {
  83. var target = new Class2();
  84. Assert.Throws<ArgumentException>(() => target.SetValue(Class1.BarProperty, "value"));
  85. }
  86. [Fact]
  87. public void GetObservable_Returns_Values()
  88. {
  89. var target = new Class1();
  90. List<string> values = new List<string>();
  91. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  92. target.Foo = "newvalue";
  93. Assert.Equal(new[] { "initial", "newvalue" }, values);
  94. }
  95. [Fact]
  96. public void Bind_Binds_Property_Value()
  97. {
  98. var target = new Class1();
  99. var source = new Subject<string>();
  100. var sub = target.Bind(Class1.FooProperty, source);
  101. Assert.Equal("initial", target.Foo);
  102. source.OnNext("first");
  103. Assert.Equal("first", target.Foo);
  104. source.OnNext("second");
  105. Assert.Equal("second", target.Foo);
  106. sub.Dispose();
  107. source.OnNext("third");
  108. Assert.Equal("second", target.Foo);
  109. }
  110. [Fact]
  111. public void Bind_Binds_Property_Value_NonGeneric()
  112. {
  113. var target = new Class1();
  114. var source = new Subject<string>();
  115. var sub = target.Bind((AvaloniaProperty)Class1.FooProperty, source);
  116. Assert.Equal("initial", target.Foo);
  117. source.OnNext("first");
  118. Assert.Equal("first", target.Foo);
  119. source.OnNext("second");
  120. Assert.Equal("second", target.Foo);
  121. sub.Dispose();
  122. source.OnNext("third");
  123. Assert.Equal("second", target.Foo);
  124. }
  125. [Fact]
  126. public void Bind_NonGeneric_Uses_UnsetValue()
  127. {
  128. var target = new Class1();
  129. var source = new Subject<object>();
  130. var sub = target.Bind((AvaloniaProperty)Class1.BazProperty, source);
  131. Assert.Equal(5, target.Baz);
  132. source.OnNext(6);
  133. Assert.Equal(6, target.Baz);
  134. source.OnNext(AvaloniaProperty.UnsetValue);
  135. Assert.Equal(-1, target.Baz);
  136. }
  137. [Fact]
  138. public void Bind_Handles_Wrong_Type()
  139. {
  140. var target = new Class1();
  141. var source = new Subject<object>();
  142. var sub = target.Bind(Class1.FooProperty, source);
  143. source.OnNext(45);
  144. Assert.Equal(null, target.Foo);
  145. }
  146. [Fact]
  147. public void Bind_Handles_Wrong_Value_Type()
  148. {
  149. var target = new Class1();
  150. var source = new Subject<object>();
  151. var sub = target.Bind(Class1.BazProperty, source);
  152. source.OnNext("foo");
  153. Assert.Equal(0, target.Baz);
  154. }
  155. [Fact]
  156. public void ReadOnly_Property_Cannot_Be_Set()
  157. {
  158. var target = new Class1();
  159. Assert.Throws<ArgumentException>(() =>
  160. target.SetValue(Class1.BarProperty, "newvalue"));
  161. }
  162. [Fact]
  163. public void ReadOnly_Property_Cannot_Be_Set_NonGeneric()
  164. {
  165. var target = new Class1();
  166. Assert.Throws<ArgumentException>(() =>
  167. target.SetValue((AvaloniaProperty)Class1.BarProperty, "newvalue"));
  168. }
  169. [Fact]
  170. public void ReadOnly_Property_Cannot_Be_Bound()
  171. {
  172. var target = new Class1();
  173. var source = new Subject<string>();
  174. Assert.Throws<ArgumentException>(() =>
  175. target.Bind(Class1.BarProperty, source));
  176. }
  177. [Fact]
  178. public void ReadOnly_Property_Cannot_Be_Bound_NonGeneric()
  179. {
  180. var target = new Class1();
  181. var source = new Subject<string>();
  182. Assert.Throws<ArgumentException>(() =>
  183. target.Bind(Class1.BarProperty, source));
  184. }
  185. [Fact]
  186. public void GetValue_Gets_Value_On_AddOwnered_Property()
  187. {
  188. var target = new Class2();
  189. Assert.Equal("initial2", target.GetValue(Class2.FooProperty));
  190. }
  191. [Fact]
  192. public void GetValue_Gets_Value_On_AddOwnered_Property_Using_Original()
  193. {
  194. var target = new Class2();
  195. Assert.Equal("initial2", target.GetValue(Class1.FooProperty));
  196. }
  197. [Fact]
  198. public void GetValue_Gets_Value_On_AddOwnered_Property_Using_Original_NonGeneric()
  199. {
  200. var target = new Class2();
  201. Assert.Equal("initial2", target.GetValue((AvaloniaProperty)Class1.FooProperty));
  202. }
  203. [Fact]
  204. public void SetValue_Sets_Value_On_AddOwnered_Property_Using_Original()
  205. {
  206. var target = new Class2();
  207. target.SetValue(Class1.FooProperty, "newvalue");
  208. Assert.Equal("newvalue", target.Foo);
  209. }
  210. [Fact]
  211. public void SetValue_Sets_Value_On_AddOwnered_Property_Using_Original_NonGeneric()
  212. {
  213. var target = new Class2();
  214. target.SetValue((AvaloniaProperty)Class1.FooProperty, "newvalue");
  215. Assert.Equal("newvalue", target.Foo);
  216. }
  217. [Fact]
  218. public void UnsetValue_Is_Used_On_AddOwnered_Property()
  219. {
  220. var target = new Class2();
  221. target.SetValue((AvaloniaProperty)Class1.FooProperty, AvaloniaProperty.UnsetValue);
  222. Assert.Equal("unset", target.Foo);
  223. }
  224. [Fact]
  225. public void Bind_Binds_AddOwnered_Property_Value()
  226. {
  227. var target = new Class2();
  228. var source = new Subject<string>();
  229. var sub = target.Bind(Class1.FooProperty, source);
  230. Assert.Equal("initial2", target.Foo);
  231. source.OnNext("first");
  232. Assert.Equal("first", target.Foo);
  233. source.OnNext("second");
  234. Assert.Equal("second", target.Foo);
  235. sub.Dispose();
  236. source.OnNext("third");
  237. Assert.Equal("second", target.Foo);
  238. }
  239. [Fact]
  240. public void Bind_Binds_AddOwnered_Property_Value_NonGeneric()
  241. {
  242. var target = new Class2();
  243. var source = new Subject<string>();
  244. var sub = target.Bind((AvaloniaProperty)Class1.FooProperty, source);
  245. Assert.Equal("initial2", target.Foo);
  246. source.OnNext("first");
  247. Assert.Equal("first", target.Foo);
  248. source.OnNext("second");
  249. Assert.Equal("second", target.Foo);
  250. sub.Dispose();
  251. source.OnNext("third");
  252. Assert.Equal("second", target.Foo);
  253. }
  254. [Fact]
  255. public void Property_Notifies_Initialized()
  256. {
  257. bool raised = false;
  258. Class1.FooProperty.Initialized.Subscribe(e =>
  259. raised = e.Property == Class1.FooProperty &&
  260. e.OldValue == AvaloniaProperty.UnsetValue &&
  261. (string)e.NewValue == "initial" &&
  262. e.Priority == BindingPriority.Unset);
  263. var target = new Class1();
  264. Assert.True(raised);
  265. }
  266. [Fact]
  267. public void DataValidationError_Does_Not_Cause_Target_Update()
  268. {
  269. var target = new Class1();
  270. var source = new Subject<object>();
  271. target.Bind(Class1.FooProperty, source);
  272. source.OnNext("initial");
  273. source.OnNext(new BindingNotification(new InvalidOperationException("Foo"), BindingErrorType.DataValidationError));
  274. Assert.Equal("initial", target.GetValue(Class1.FooProperty));
  275. }
  276. [Fact]
  277. public void BindingError_With_FallbackValue_Causes_Target_Update()
  278. {
  279. var target = new Class1();
  280. var source = new Subject<object>();
  281. target.Bind(Class1.FooProperty, source);
  282. source.OnNext("initial");
  283. source.OnNext(new BindingNotification(
  284. new InvalidOperationException("Foo"),
  285. BindingErrorType.Error,
  286. "fallback"));
  287. Assert.Equal("fallback", target.GetValue(Class1.FooProperty));
  288. }
  289. [Fact]
  290. public void Binding_To_Direct_Property_Logs_BindingError()
  291. {
  292. var target = new Class1();
  293. var source = new Subject<object>();
  294. var called = false;
  295. LogCallback checkLogMessage = (level, area, src, mt, pv) =>
  296. {
  297. if (level == LogEventLevel.Warning &&
  298. area == LogArea.Binding &&
  299. mt == "Error in binding to {Target}.{Property}: {Message}" &&
  300. pv.Length == 3 &&
  301. pv[0] is Class1 &&
  302. object.ReferenceEquals(pv[1], Class1.FooProperty) &&
  303. (string)pv[2] == "Binding Error Message")
  304. {
  305. called = true;
  306. }
  307. };
  308. using (TestLogSink.Start(checkLogMessage))
  309. {
  310. target.Bind(Class1.FooProperty, source);
  311. source.OnNext("baz");
  312. source.OnNext(new BindingNotification(new InvalidOperationException("Binding Error Message"), BindingErrorType.Error));
  313. }
  314. Assert.True(called);
  315. }
  316. [Fact]
  317. public void AddOwner_Should_Inherit_DefaultBindingMode()
  318. {
  319. var foo = new DirectProperty<Class1, string>(
  320. "foo",
  321. o => "foo",
  322. null,
  323. new DirectPropertyMetadata<string>(defaultBindingMode: BindingMode.TwoWay));
  324. var bar = foo.AddOwner<Class2>(o => "bar");
  325. Assert.Equal(BindingMode.TwoWay, bar.GetMetadata<Class1>().DefaultBindingMode);
  326. Assert.Equal(BindingMode.TwoWay, bar.GetMetadata<Class2>().DefaultBindingMode);
  327. }
  328. [Fact]
  329. public void AddOwner_Can_Override_DefaultBindingMode()
  330. {
  331. var foo = new DirectProperty<Class1, string>(
  332. "foo",
  333. o => "foo",
  334. null,
  335. new DirectPropertyMetadata<string>(defaultBindingMode: BindingMode.TwoWay));
  336. var bar = foo.AddOwner<Class2>(o => "bar", defaultBindingMode: BindingMode.OneWayToSource);
  337. Assert.Equal(BindingMode.TwoWay, bar.GetMetadata<Class1>().DefaultBindingMode);
  338. Assert.Equal(BindingMode.OneWayToSource, bar.GetMetadata<Class2>().DefaultBindingMode);
  339. }
  340. private class Class1 : AvaloniaObject
  341. {
  342. public static readonly DirectProperty<Class1, string> FooProperty =
  343. AvaloniaProperty.RegisterDirect<Class1, string>(
  344. "Foo",
  345. o => o.Foo,
  346. (o, v) => o.Foo = v,
  347. unsetValue: "unset");
  348. public static readonly DirectProperty<Class1, string> BarProperty =
  349. AvaloniaProperty.RegisterDirect<Class1, string>("Bar", o => o.Bar);
  350. public static readonly DirectProperty<Class1, int> BazProperty =
  351. AvaloniaProperty.RegisterDirect<Class1, int>(
  352. "Bar",
  353. o => o.Baz,
  354. (o,v) => o.Baz = v,
  355. unsetValue: -1);
  356. private string _foo = "initial";
  357. private readonly string _bar = "bar";
  358. private int _baz = 5;
  359. public string Foo
  360. {
  361. get { return _foo; }
  362. set { SetAndRaise(FooProperty, ref _foo, value); }
  363. }
  364. public string Bar
  365. {
  366. get { return _bar; }
  367. }
  368. public int Baz
  369. {
  370. get { return _baz; }
  371. set { SetAndRaise(BazProperty, ref _baz, value); }
  372. }
  373. }
  374. private class Class2 : AvaloniaObject
  375. {
  376. public static readonly DirectProperty<Class2, string> FooProperty =
  377. Class1.FooProperty.AddOwner<Class2>(o => o.Foo, (o, v) => o.Foo = v);
  378. private string _foo = "initial2";
  379. static Class2()
  380. {
  381. }
  382. public string Foo
  383. {
  384. get { return _foo; }
  385. set { SetAndRaise(FooProperty, ref _foo, value); }
  386. }
  387. }
  388. }
  389. }