AvaloniaObjectTests_Direct.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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.ComponentModel;
  6. using System.Reactive.Subjects;
  7. using Avalonia.Data;
  8. using Avalonia.Logging;
  9. using Avalonia.Markup.Xaml.Data;
  10. using Avalonia.UnitTests;
  11. using Xunit;
  12. namespace Avalonia.Base.UnitTests
  13. {
  14. public class AvaloniaObjectTests_Direct
  15. {
  16. [Fact]
  17. public void GetValue_Gets_Value()
  18. {
  19. var target = new Class1();
  20. Assert.Equal("initial", target.GetValue(Class1.FooProperty));
  21. }
  22. [Fact]
  23. public void GetValue_Gets_Value_NonGeneric()
  24. {
  25. var target = new Class1();
  26. Assert.Equal("initial", target.GetValue((AvaloniaProperty)Class1.FooProperty));
  27. }
  28. [Fact]
  29. public void GetValue_On_Unregistered_Property_Throws_Exception()
  30. {
  31. var target = new Class2();
  32. Assert.Throws<ArgumentException>(() => target.GetValue(Class1.BarProperty));
  33. }
  34. [Fact]
  35. public void SetValue_Sets_Value()
  36. {
  37. var target = new Class1();
  38. target.SetValue(Class1.FooProperty, "newvalue");
  39. Assert.Equal("newvalue", target.Foo);
  40. }
  41. [Fact]
  42. public void SetValue_Sets_Value_NonGeneric()
  43. {
  44. var target = new Class1();
  45. target.SetValue((AvaloniaProperty)Class1.FooProperty, "newvalue");
  46. Assert.Equal("newvalue", target.Foo);
  47. }
  48. [Fact]
  49. public void SetValue_NonGeneric_Coerces_UnsetValue_To_Default_Value()
  50. {
  51. var target = new Class1();
  52. target.SetValue((AvaloniaProperty)Class1.BazProperty, AvaloniaProperty.UnsetValue);
  53. Assert.Equal(-1, target.Baz);
  54. }
  55. [Fact]
  56. public void SetValue_Raises_PropertyChanged()
  57. {
  58. var target = new Class1();
  59. bool raised = false;
  60. target.PropertyChanged += (s, e) =>
  61. raised = e.Property == Class1.FooProperty &&
  62. (string)e.OldValue == "initial" &&
  63. (string)e.NewValue == "newvalue" &&
  64. e.Priority == BindingPriority.LocalValue;
  65. target.SetValue(Class1.FooProperty, "newvalue");
  66. Assert.True(raised);
  67. }
  68. [Fact]
  69. public void SetValue_Raises_Changed()
  70. {
  71. var target = new Class1();
  72. bool raised = false;
  73. Class1.FooProperty.Changed.Subscribe(e =>
  74. raised = e.Property == Class1.FooProperty &&
  75. (string)e.OldValue == "initial" &&
  76. (string)e.NewValue == "newvalue" &&
  77. e.Priority == BindingPriority.LocalValue);
  78. target.SetValue(Class1.FooProperty, "newvalue");
  79. Assert.True(raised);
  80. }
  81. [Fact]
  82. public void SetValue_On_Unregistered_Property_Throws_Exception()
  83. {
  84. var target = new Class2();
  85. Assert.Throws<ArgumentException>(() => target.SetValue(Class1.BarProperty, "value"));
  86. }
  87. [Fact]
  88. public void GetObservable_Returns_Values()
  89. {
  90. var target = new Class1();
  91. List<string> values = new List<string>();
  92. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  93. target.Foo = "newvalue";
  94. Assert.Equal(new[] { "initial", "newvalue" }, values);
  95. }
  96. [Fact]
  97. public void Bind_Binds_Property_Value()
  98. {
  99. var target = new Class1();
  100. var source = new Subject<string>();
  101. var sub = target.Bind(Class1.FooProperty, source);
  102. Assert.Equal("initial", target.Foo);
  103. source.OnNext("first");
  104. Assert.Equal("first", target.Foo);
  105. source.OnNext("second");
  106. Assert.Equal("second", target.Foo);
  107. sub.Dispose();
  108. source.OnNext("third");
  109. Assert.Equal("second", target.Foo);
  110. }
  111. [Fact]
  112. public void Bind_Binds_Property_Value_NonGeneric()
  113. {
  114. var target = new Class1();
  115. var source = new Subject<string>();
  116. var sub = target.Bind((AvaloniaProperty)Class1.FooProperty, source);
  117. Assert.Equal("initial", target.Foo);
  118. source.OnNext("first");
  119. Assert.Equal("first", target.Foo);
  120. source.OnNext("second");
  121. Assert.Equal("second", target.Foo);
  122. sub.Dispose();
  123. source.OnNext("third");
  124. Assert.Equal("second", target.Foo);
  125. }
  126. [Fact]
  127. public void Bind_NonGeneric_Uses_UnsetValue()
  128. {
  129. var target = new Class1();
  130. var source = new Subject<object>();
  131. var sub = target.Bind((AvaloniaProperty)Class1.BazProperty, source);
  132. Assert.Equal(5, target.Baz);
  133. source.OnNext(6);
  134. Assert.Equal(6, target.Baz);
  135. source.OnNext(AvaloniaProperty.UnsetValue);
  136. Assert.Equal(-1, target.Baz);
  137. }
  138. [Fact]
  139. public void Bind_Handles_Wrong_Type()
  140. {
  141. var target = new Class1();
  142. var source = new Subject<object>();
  143. var sub = target.Bind(Class1.FooProperty, source);
  144. source.OnNext(45);
  145. Assert.Equal(null, target.Foo);
  146. }
  147. [Fact]
  148. public void Bind_Handles_Wrong_Value_Type()
  149. {
  150. var target = new Class1();
  151. var source = new Subject<object>();
  152. var sub = target.Bind(Class1.BazProperty, source);
  153. source.OnNext("foo");
  154. Assert.Equal(0, target.Baz);
  155. }
  156. [Fact]
  157. public void ReadOnly_Property_Cannot_Be_Set()
  158. {
  159. var target = new Class1();
  160. Assert.Throws<ArgumentException>(() =>
  161. target.SetValue(Class1.BarProperty, "newvalue"));
  162. }
  163. [Fact]
  164. public void ReadOnly_Property_Cannot_Be_Set_NonGeneric()
  165. {
  166. var target = new Class1();
  167. Assert.Throws<ArgumentException>(() =>
  168. target.SetValue((AvaloniaProperty)Class1.BarProperty, "newvalue"));
  169. }
  170. [Fact]
  171. public void ReadOnly_Property_Cannot_Be_Bound()
  172. {
  173. var target = new Class1();
  174. var source = new Subject<string>();
  175. Assert.Throws<ArgumentException>(() =>
  176. target.Bind(Class1.BarProperty, source));
  177. }
  178. [Fact]
  179. public void ReadOnly_Property_Cannot_Be_Bound_NonGeneric()
  180. {
  181. var target = new Class1();
  182. var source = new Subject<string>();
  183. Assert.Throws<ArgumentException>(() =>
  184. target.Bind(Class1.BarProperty, source));
  185. }
  186. [Fact]
  187. public void GetValue_Gets_Value_On_AddOwnered_Property()
  188. {
  189. var target = new Class2();
  190. Assert.Equal("initial2", target.GetValue(Class2.FooProperty));
  191. }
  192. [Fact]
  193. public void GetValue_Gets_Value_On_AddOwnered_Property_Using_Original()
  194. {
  195. var target = new Class2();
  196. Assert.Equal("initial2", target.GetValue(Class1.FooProperty));
  197. }
  198. [Fact]
  199. public void GetValue_Gets_Value_On_AddOwnered_Property_Using_Original_NonGeneric()
  200. {
  201. var target = new Class2();
  202. Assert.Equal("initial2", target.GetValue((AvaloniaProperty)Class1.FooProperty));
  203. }
  204. [Fact]
  205. public void SetValue_Sets_Value_On_AddOwnered_Property_Using_Original()
  206. {
  207. var target = new Class2();
  208. target.SetValue(Class1.FooProperty, "newvalue");
  209. Assert.Equal("newvalue", target.Foo);
  210. }
  211. [Fact]
  212. public void SetValue_Sets_Value_On_AddOwnered_Property_Using_Original_NonGeneric()
  213. {
  214. var target = new Class2();
  215. target.SetValue((AvaloniaProperty)Class1.FooProperty, "newvalue");
  216. Assert.Equal("newvalue", target.Foo);
  217. }
  218. [Fact]
  219. public void UnsetValue_Is_Used_On_AddOwnered_Property()
  220. {
  221. var target = new Class2();
  222. target.SetValue((AvaloniaProperty)Class1.FooProperty, AvaloniaProperty.UnsetValue);
  223. Assert.Equal("unset", target.Foo);
  224. }
  225. [Fact]
  226. public void Bind_Binds_AddOwnered_Property_Value()
  227. {
  228. var target = new Class2();
  229. var source = new Subject<string>();
  230. var sub = target.Bind(Class1.FooProperty, source);
  231. Assert.Equal("initial2", target.Foo);
  232. source.OnNext("first");
  233. Assert.Equal("first", target.Foo);
  234. source.OnNext("second");
  235. Assert.Equal("second", target.Foo);
  236. sub.Dispose();
  237. source.OnNext("third");
  238. Assert.Equal("second", target.Foo);
  239. }
  240. [Fact]
  241. public void Bind_Binds_AddOwnered_Property_Value_NonGeneric()
  242. {
  243. var target = new Class2();
  244. var source = new Subject<string>();
  245. var sub = target.Bind((AvaloniaProperty)Class1.FooProperty, source);
  246. Assert.Equal("initial2", target.Foo);
  247. source.OnNext("first");
  248. Assert.Equal("first", target.Foo);
  249. source.OnNext("second");
  250. Assert.Equal("second", target.Foo);
  251. sub.Dispose();
  252. source.OnNext("third");
  253. Assert.Equal("second", target.Foo);
  254. }
  255. [Fact]
  256. public void Property_Notifies_Initialized()
  257. {
  258. bool raised = false;
  259. Class1.FooProperty.Initialized.Subscribe(e =>
  260. raised = e.Property == Class1.FooProperty &&
  261. e.OldValue == AvaloniaProperty.UnsetValue &&
  262. (string)e.NewValue == "initial" &&
  263. e.Priority == BindingPriority.Unset);
  264. var target = new Class1();
  265. Assert.True(raised);
  266. }
  267. [Fact]
  268. public void BindingError_Does_Not_Cause_Target_Update()
  269. {
  270. var target = new Class1();
  271. var source = new Subject<object>();
  272. target.Bind(Class1.FooProperty, source);
  273. source.OnNext("initial");
  274. source.OnNext(new BindingNotification(new InvalidOperationException("Foo"), BindingErrorType.Error));
  275. Assert.Equal("initial", target.GetValue(Class1.FooProperty));
  276. }
  277. [Fact]
  278. public void BindingError_With_FallbackValue_Causes_Target_Update()
  279. {
  280. var target = new Class1();
  281. var source = new Subject<object>();
  282. target.Bind(Class1.FooProperty, source);
  283. source.OnNext("initial");
  284. source.OnNext(new BindingNotification(
  285. new InvalidOperationException("Foo"),
  286. BindingErrorType.Error,
  287. "fallback"));
  288. Assert.Equal("fallback", target.GetValue(Class1.FooProperty));
  289. }
  290. [Fact]
  291. public void Binding_To_Direct_Property_Logs_BindingError()
  292. {
  293. var target = new Class1();
  294. var source = new Subject<object>();
  295. var called = false;
  296. LogCallback checkLogMessage = (level, area, src, mt, pv) =>
  297. {
  298. if (level == LogEventLevel.Error &&
  299. area == LogArea.Binding &&
  300. mt == "Error in binding to {Target}.{Property}: {Message}" &&
  301. pv.Length == 3 &&
  302. pv[0] is Class1 &&
  303. object.ReferenceEquals(pv[1], Class1.FooProperty) &&
  304. (string)pv[2] == "Binding Error Message")
  305. {
  306. called = true;
  307. }
  308. };
  309. using (TestLogSink.Start(checkLogMessage))
  310. {
  311. target.Bind(Class1.FooProperty, source);
  312. source.OnNext("baz");
  313. source.OnNext(new BindingNotification(new InvalidOperationException("Binding Error Message"), BindingErrorType.Error));
  314. }
  315. Assert.True(called);
  316. }
  317. [Fact]
  318. public void AddOwner_Should_Inherit_DefaultBindingMode()
  319. {
  320. var foo = new DirectProperty<Class1, string>(
  321. "foo",
  322. o => "foo",
  323. null,
  324. new DirectPropertyMetadata<string>(defaultBindingMode: BindingMode.TwoWay));
  325. var bar = foo.AddOwner<Class2>(o => "bar");
  326. Assert.Equal(BindingMode.TwoWay, bar.GetMetadata<Class1>().DefaultBindingMode);
  327. Assert.Equal(BindingMode.TwoWay, bar.GetMetadata<Class2>().DefaultBindingMode);
  328. }
  329. [Fact]
  330. public void AddOwner_Can_Override_DefaultBindingMode()
  331. {
  332. var foo = new DirectProperty<Class1, string>(
  333. "foo",
  334. o => "foo",
  335. null,
  336. new DirectPropertyMetadata<string>(defaultBindingMode: BindingMode.TwoWay));
  337. var bar = foo.AddOwner<Class2>(o => "bar", defaultBindingMode: BindingMode.OneWayToSource);
  338. Assert.Equal(BindingMode.TwoWay, bar.GetMetadata<Class1>().DefaultBindingMode);
  339. Assert.Equal(BindingMode.OneWayToSource, bar.GetMetadata<Class2>().DefaultBindingMode);
  340. }
  341. [Theory]
  342. [InlineData(true)]
  343. [InlineData(false)]
  344. public void SetValue_Should_Not_Cause_StackOverflow_And_Have_Correct_Values(bool useXamlBinding)
  345. {
  346. var viewModel = new TestStackOverflowViewModel()
  347. {
  348. Value = 50
  349. };
  350. var target = new Class1();
  351. //note: if the initialization of the child binding is here there is no stackoverflow!!!
  352. //var child = new Class1()
  353. //{
  354. // [~~Class1.DoubleValueProperty] = target[~~Class1.DoubleValueProperty]
  355. //};
  356. target.Bind(Class1.DoubleValueProperty, new Binding("Value")
  357. {
  358. Mode = BindingMode.TwoWay,
  359. Source = viewModel
  360. });
  361. var child = new Class1();
  362. if (useXamlBinding)
  363. {
  364. child.Bind(Class1.DoubleValueProperty,
  365. new Binding("DoubleValue")
  366. {
  367. Mode = BindingMode.TwoWay,
  368. Source = target
  369. });
  370. }
  371. else
  372. {
  373. child[!!Class1.DoubleValueProperty] = target[!!Class1.DoubleValueProperty];
  374. }
  375. Assert.Equal(1, viewModel.SetterInvokedCount);
  376. //here in real life stack overflow exception is thrown issue #855 and #824
  377. target.DoubleValue = 51.001;
  378. Assert.Equal(2, viewModel.SetterInvokedCount);
  379. double expected = 51;
  380. Assert.Equal(expected, viewModel.Value);
  381. Assert.Equal(expected, target.DoubleValue);
  382. Assert.Equal(expected, child.DoubleValue);
  383. }
  384. private class Class1 : AvaloniaObject
  385. {
  386. public static readonly DirectProperty<Class1, string> FooProperty =
  387. AvaloniaProperty.RegisterDirect<Class1, string>(
  388. "Foo",
  389. o => o.Foo,
  390. (o, v) => o.Foo = v,
  391. unsetValue: "unset");
  392. public static readonly DirectProperty<Class1, string> BarProperty =
  393. AvaloniaProperty.RegisterDirect<Class1, string>("Bar", o => o.Bar);
  394. public static readonly DirectProperty<Class1, int> BazProperty =
  395. AvaloniaProperty.RegisterDirect<Class1, int>(
  396. "Bar",
  397. o => o.Baz,
  398. (o, v) => o.Baz = v,
  399. unsetValue: -1);
  400. public static readonly DirectProperty<Class1, double> DoubleValueProperty =
  401. AvaloniaProperty.RegisterDirect<Class1, double>(
  402. nameof(DoubleValue),
  403. o => o.DoubleValue,
  404. (o, v) => o.DoubleValue = v);
  405. private string _foo = "initial";
  406. private readonly string _bar = "bar";
  407. private int _baz = 5;
  408. private double _doubleValue;
  409. public string Foo
  410. {
  411. get { return _foo; }
  412. set { SetAndRaise(FooProperty, ref _foo, value); }
  413. }
  414. public string Bar
  415. {
  416. get { return _bar; }
  417. }
  418. public int Baz
  419. {
  420. get { return _baz; }
  421. set { SetAndRaise(BazProperty, ref _baz, value); }
  422. }
  423. public double DoubleValue
  424. {
  425. get { return _doubleValue; }
  426. set { SetAndRaise(DoubleValueProperty, ref _doubleValue, value); }
  427. }
  428. }
  429. private class Class2 : AvaloniaObject
  430. {
  431. public static readonly DirectProperty<Class2, string> FooProperty =
  432. Class1.FooProperty.AddOwner<Class2>(o => o.Foo, (o, v) => o.Foo = v);
  433. private string _foo = "initial2";
  434. static Class2()
  435. {
  436. }
  437. public string Foo
  438. {
  439. get { return _foo; }
  440. set { SetAndRaise(FooProperty, ref _foo, value); }
  441. }
  442. }
  443. private class TestStackOverflowViewModel : INotifyPropertyChanged
  444. {
  445. public int SetterInvokedCount { get; private set; }
  446. public const int MaxInvokedCount = 1000;
  447. private double _value;
  448. public event PropertyChangedEventHandler PropertyChanged;
  449. public double Value
  450. {
  451. get { return _value; }
  452. set
  453. {
  454. if (_value != value)
  455. {
  456. SetterInvokedCount++;
  457. if (SetterInvokedCount < MaxInvokedCount)
  458. {
  459. _value = (int)value;
  460. if (_value > 75) _value = 75;
  461. if (_value < 25) _value = 25;
  462. }
  463. else
  464. {
  465. _value = value;
  466. }
  467. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
  468. }
  469. }
  470. }
  471. }
  472. }
  473. }