AvaloniaObjectTests_Binding.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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.ComponentModel;
  5. using System.Reactive.Concurrency;
  6. using System.Reactive.Linq;
  7. using System.Reactive.Subjects;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Avalonia.Data;
  11. using Avalonia.Logging;
  12. using Avalonia.Markup.Xaml.Data;
  13. using Avalonia.Platform;
  14. using Avalonia.Threading;
  15. using Avalonia.UnitTests;
  16. using Avalonia.Diagnostics;
  17. using Microsoft.Reactive.Testing;
  18. using Moq;
  19. using Xunit;
  20. namespace Avalonia.Base.UnitTests
  21. {
  22. public class AvaloniaObjectTests_Binding
  23. {
  24. [Fact]
  25. public void Bind_Sets_Current_Value()
  26. {
  27. Class1 target = new Class1();
  28. Class1 source = new Class1();
  29. source.SetValue(Class1.FooProperty, "initial");
  30. target.Bind(Class1.FooProperty, source.GetObservable(Class1.FooProperty));
  31. Assert.Equal("initial", target.GetValue(Class1.FooProperty));
  32. }
  33. [Fact]
  34. public void Bind_NonGeneric_Sets_Current_Value()
  35. {
  36. Class1 target = new Class1();
  37. Class1 source = new Class1();
  38. source.SetValue(Class1.FooProperty, "initial");
  39. target.Bind((AvaloniaProperty)Class1.FooProperty, source.GetObservable(Class1.FooProperty));
  40. Assert.Equal("initial", target.GetValue(Class1.FooProperty));
  41. }
  42. [Fact]
  43. public void Bind_To_ValueType_Accepts_UnsetValue()
  44. {
  45. var target = new Class1();
  46. var source = new Subject<object>();
  47. target.Bind(Class1.QuxProperty, source);
  48. source.OnNext(6.7);
  49. source.OnNext(AvaloniaProperty.UnsetValue);
  50. Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
  51. Assert.False(target.IsSet(Class1.QuxProperty));
  52. }
  53. [Fact]
  54. public void OneTime_Binding_Ignores_UnsetValue()
  55. {
  56. var target = new Class1();
  57. var source = new Subject<object>();
  58. target.Bind(Class1.QuxProperty, new TestOneTimeBinding(source));
  59. source.OnNext(AvaloniaProperty.UnsetValue);
  60. Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
  61. source.OnNext(6.7);
  62. Assert.Equal(6.7, target.GetValue(Class1.QuxProperty));
  63. }
  64. [Fact]
  65. public void OneTime_Binding_Ignores_Binding_Errors()
  66. {
  67. var target = new Class1();
  68. var source = new Subject<object>();
  69. target.Bind(Class1.QuxProperty, new TestOneTimeBinding(source));
  70. source.OnNext(new BindingNotification(new Exception(), BindingErrorType.Error));
  71. Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
  72. source.OnNext(6.7);
  73. Assert.Equal(6.7, target.GetValue(Class1.QuxProperty));
  74. }
  75. [Fact]
  76. public void Bind_Does_Not_Throw_Exception_For_Unregistered_Property()
  77. {
  78. Class1 target = new Class1();
  79. target.Bind(Class2.BarProperty, Observable.Never<string>().StartWith("foo"));
  80. Assert.Equal("foo", target.GetValue(Class2.BarProperty));
  81. }
  82. [Fact]
  83. public void Bind_Sets_Subsequent_Value()
  84. {
  85. Class1 target = new Class1();
  86. Class1 source = new Class1();
  87. source.SetValue(Class1.FooProperty, "initial");
  88. target.Bind(Class1.FooProperty, source.GetObservable(Class1.FooProperty));
  89. source.SetValue(Class1.FooProperty, "subsequent");
  90. Assert.Equal("subsequent", target.GetValue(Class1.FooProperty));
  91. }
  92. [Fact]
  93. public void Bind_Ignores_Invalid_Value_Type()
  94. {
  95. Class1 target = new Class1();
  96. target.Bind((AvaloniaProperty)Class1.FooProperty, Observable.Return((object)123));
  97. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  98. }
  99. [Fact]
  100. public void Observable_Is_Unsubscribed_When_Subscription_Disposed()
  101. {
  102. var scheduler = new TestScheduler();
  103. var source = scheduler.CreateColdObservable<object>();
  104. var target = new Class1();
  105. var subscription = target.Bind(Class1.FooProperty, source);
  106. Assert.Equal(1, source.Subscriptions.Count);
  107. Assert.Equal(Subscription.Infinite, source.Subscriptions[0].Unsubscribe);
  108. subscription.Dispose();
  109. Assert.Equal(1, source.Subscriptions.Count);
  110. Assert.Equal(0, source.Subscriptions[0].Unsubscribe);
  111. }
  112. [Fact]
  113. public void Two_Way_Separate_Binding_Works()
  114. {
  115. Class1 obj1 = new Class1();
  116. Class1 obj2 = new Class1();
  117. obj1.SetValue(Class1.FooProperty, "initial1");
  118. obj2.SetValue(Class1.FooProperty, "initial2");
  119. obj1.Bind(Class1.FooProperty, obj2.GetObservable(Class1.FooProperty));
  120. obj2.Bind(Class1.FooProperty, obj1.GetObservable(Class1.FooProperty));
  121. Assert.Equal("initial2", obj1.GetValue(Class1.FooProperty));
  122. Assert.Equal("initial2", obj2.GetValue(Class1.FooProperty));
  123. obj1.SetValue(Class1.FooProperty, "first");
  124. Assert.Equal("first", obj1.GetValue(Class1.FooProperty));
  125. Assert.Equal("first", obj2.GetValue(Class1.FooProperty));
  126. obj2.SetValue(Class1.FooProperty, "second");
  127. Assert.Equal("second", obj1.GetValue(Class1.FooProperty));
  128. Assert.Equal("second", obj2.GetValue(Class1.FooProperty));
  129. obj1.SetValue(Class1.FooProperty, "third");
  130. Assert.Equal("third", obj1.GetValue(Class1.FooProperty));
  131. Assert.Equal("third", obj2.GetValue(Class1.FooProperty));
  132. }
  133. [Fact]
  134. public void Two_Way_Binding_With_Priority_Works()
  135. {
  136. Class1 obj1 = new Class1();
  137. Class1 obj2 = new Class1();
  138. obj1.SetValue(Class1.FooProperty, "initial1", BindingPriority.Style);
  139. obj2.SetValue(Class1.FooProperty, "initial2", BindingPriority.Style);
  140. obj1.Bind(Class1.FooProperty, obj2.GetObservable(Class1.FooProperty), BindingPriority.Style);
  141. obj2.Bind(Class1.FooProperty, obj1.GetObservable(Class1.FooProperty), BindingPriority.Style);
  142. Assert.Equal("initial2", obj1.GetValue(Class1.FooProperty));
  143. Assert.Equal("initial2", obj2.GetValue(Class1.FooProperty));
  144. obj1.SetValue(Class1.FooProperty, "first", BindingPriority.Style);
  145. Assert.Equal("first", obj1.GetValue(Class1.FooProperty));
  146. Assert.Equal("first", obj2.GetValue(Class1.FooProperty));
  147. obj2.SetValue(Class1.FooProperty, "second", BindingPriority.Style);
  148. Assert.Equal("second", obj1.GetValue(Class1.FooProperty));
  149. Assert.Equal("second", obj2.GetValue(Class1.FooProperty));
  150. obj1.SetValue(Class1.FooProperty, "third", BindingPriority.Style);
  151. Assert.Equal("third", obj1.GetValue(Class1.FooProperty));
  152. Assert.Equal("third", obj2.GetValue(Class1.FooProperty));
  153. }
  154. [Fact]
  155. public void Local_Binding_Overwrites_Local_Value()
  156. {
  157. var target = new Class1();
  158. var binding = new Subject<string>();
  159. target.Bind(Class1.FooProperty, binding);
  160. binding.OnNext("first");
  161. Assert.Equal("first", target.GetValue(Class1.FooProperty));
  162. target.SetValue(Class1.FooProperty, "second");
  163. Assert.Equal("second", target.GetValue(Class1.FooProperty));
  164. binding.OnNext("third");
  165. Assert.Equal("third", target.GetValue(Class1.FooProperty));
  166. }
  167. [Fact]
  168. public void StyleBinding_Overrides_Default_Value()
  169. {
  170. Class1 target = new Class1();
  171. target.Bind(Class1.FooProperty, Single("stylevalue"), BindingPriority.Style);
  172. Assert.Equal("stylevalue", target.GetValue(Class1.FooProperty));
  173. }
  174. [Fact]
  175. public void this_Operator_Returns_Value_Property()
  176. {
  177. Class1 target = new Class1();
  178. target.SetValue(Class1.FooProperty, "newvalue");
  179. Assert.Equal("newvalue", target[Class1.FooProperty]);
  180. }
  181. [Fact]
  182. public void this_Operator_Sets_Value_Property()
  183. {
  184. Class1 target = new Class1();
  185. target[Class1.FooProperty] = "newvalue";
  186. Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
  187. }
  188. [Fact]
  189. public void this_Operator_Doesnt_Accept_Observable()
  190. {
  191. Class1 target = new Class1();
  192. Assert.Throws<ArgumentException>(() =>
  193. {
  194. target[Class1.FooProperty] = Observable.Return("newvalue");
  195. });
  196. }
  197. [Fact]
  198. public void this_Operator_Binds_One_Way()
  199. {
  200. Class1 target1 = new Class1();
  201. Class2 target2 = new Class2();
  202. IndexerDescriptor binding = Class2.BarProperty.Bind().WithMode(BindingMode.OneWay);
  203. target1.SetValue(Class1.FooProperty, "first");
  204. target2[binding] = target1[!Class1.FooProperty];
  205. target1.SetValue(Class1.FooProperty, "second");
  206. Assert.Equal("second", target2.GetValue(Class2.BarProperty));
  207. }
  208. [Fact]
  209. public void this_Operator_Binds_Two_Way()
  210. {
  211. Class1 target1 = new Class1();
  212. Class1 target2 = new Class1();
  213. target1.SetValue(Class1.FooProperty, "first");
  214. target2[!Class1.FooProperty] = target1[!!Class1.FooProperty];
  215. Assert.Equal("first", target2.GetValue(Class1.FooProperty));
  216. target1.SetValue(Class1.FooProperty, "second");
  217. Assert.Equal("second", target2.GetValue(Class1.FooProperty));
  218. target2.SetValue(Class1.FooProperty, "third");
  219. Assert.Equal("third", target1.GetValue(Class1.FooProperty));
  220. }
  221. [Fact]
  222. public void this_Operator_Binds_One_Time()
  223. {
  224. Class1 target1 = new Class1();
  225. Class1 target2 = new Class1();
  226. target1.SetValue(Class1.FooProperty, "first");
  227. target2[!Class1.FooProperty] = target1[Class1.FooProperty.Bind().WithMode(BindingMode.OneTime)];
  228. target1.SetValue(Class1.FooProperty, "second");
  229. Assert.Equal("first", target2.GetValue(Class1.FooProperty));
  230. }
  231. [Fact]
  232. public void BindingError_Does_Not_Cause_Target_Update()
  233. {
  234. var target = new Class1();
  235. var source = new Subject<object>();
  236. target.Bind(Class1.QuxProperty, source);
  237. source.OnNext(6.7);
  238. source.OnNext(new BindingNotification(
  239. new InvalidOperationException("Foo"),
  240. BindingErrorType.Error));
  241. Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
  242. }
  243. [Fact]
  244. public void BindingNotification_With_FallbackValue_Causes_Target_Update()
  245. {
  246. var target = new Class1();
  247. var source = new Subject<object>();
  248. target.Bind(Class1.QuxProperty, source);
  249. source.OnNext(6.7);
  250. source.OnNext(new BindingNotification(
  251. new InvalidOperationException("Foo"),
  252. BindingErrorType.Error,
  253. 8.9));
  254. Assert.Equal(8.9, target.GetValue(Class1.QuxProperty));
  255. }
  256. [Fact]
  257. public void Bind_Logs_Binding_Error()
  258. {
  259. var target = new Class1();
  260. var source = new Subject<object>();
  261. var called = false;
  262. var expectedMessageTemplate = "Error in binding to {Target}.{Property}: {Message}";
  263. LogCallback checkLogMessage = (level, area, src, mt, pv) =>
  264. {
  265. if (level == LogEventLevel.Warning &&
  266. area == LogArea.Binding &&
  267. mt == expectedMessageTemplate)
  268. {
  269. called = true;
  270. }
  271. };
  272. using (TestLogSink.Start(checkLogMessage))
  273. {
  274. target.Bind(Class1.QuxProperty, source);
  275. source.OnNext(6.7);
  276. source.OnNext(new BindingNotification(
  277. new InvalidOperationException("Foo"),
  278. BindingErrorType.Error));
  279. Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
  280. Assert.True(called);
  281. }
  282. }
  283. [Fact]
  284. public async Task Bind_With_Scheduler_Executes_On_Scheduler()
  285. {
  286. var target = new Class1();
  287. var source = new Subject<object>();
  288. var currentThreadId = Thread.CurrentThread.ManagedThreadId;
  289. var threadingInterfaceMock = new Mock<IPlatformThreadingInterface>();
  290. threadingInterfaceMock.SetupGet(mock => mock.CurrentThreadIsLoopThread)
  291. .Returns(() => Thread.CurrentThread.ManagedThreadId == currentThreadId);
  292. var services = new TestServices(
  293. scheduler: AvaloniaScheduler.Instance,
  294. threadingInterface: threadingInterfaceMock.Object);
  295. using (UnitTestApplication.Start(services))
  296. {
  297. target.Bind(Class1.QuxProperty, source);
  298. await Task.Run(() => source.OnNext(6.7));
  299. }
  300. }
  301. [Fact]
  302. public void SetValue_Should_Not_Cause_StackOverflow_And_Have_Correct_Values()
  303. {
  304. var viewModel = new TestStackOverflowViewModel()
  305. {
  306. Value = 50
  307. };
  308. var target = new Class1();
  309. target.Bind(Class1.DoubleValueProperty,
  310. new Binding("Value") { Mode = BindingMode.TwoWay, Source = viewModel });
  311. var child = new Class1();
  312. child[!!Class1.DoubleValueProperty] = target[!!Class1.DoubleValueProperty];
  313. Assert.Equal(1, viewModel.SetterInvokedCount);
  314. // Issues #855 and #824 were causing a StackOverflowException at this point.
  315. target.DoubleValue = 51.001;
  316. Assert.Equal(2, viewModel.SetterInvokedCount);
  317. double expected = 51;
  318. Assert.Equal(expected, viewModel.Value);
  319. Assert.Equal(expected, target.DoubleValue);
  320. Assert.Equal(expected, child.DoubleValue);
  321. }
  322. [Fact]
  323. public void IsAnimating_On_Property_With_No_Value_Returns_False()
  324. {
  325. var target = new Class1();
  326. Assert.False(target.IsAnimating(Class1.FooProperty));
  327. }
  328. [Fact]
  329. public void IsAnimating_On_Property_With_Animation_Value_Returns_False()
  330. {
  331. var target = new Class1();
  332. target.SetValue(Class1.FooProperty, "foo", BindingPriority.Animation);
  333. Assert.False(target.IsAnimating(Class1.FooProperty));
  334. }
  335. [Fact]
  336. public void IsAnimating_On_Property_With_Non_Animation_Binding_Returns_False()
  337. {
  338. var target = new Class1();
  339. var source = new Subject<string>();
  340. target.Bind(Class1.FooProperty, source, BindingPriority.LocalValue);
  341. Assert.False(target.IsAnimating(Class1.FooProperty));
  342. }
  343. [Fact]
  344. public void IsAnimating_On_Property_With_Animation_Binding_Returns_True()
  345. {
  346. var target = new Class1();
  347. var source = new BehaviorSubject<string>("foo");
  348. target.Bind(Class1.FooProperty, source, BindingPriority.Animation);
  349. Assert.True(target.IsAnimating(Class1.FooProperty));
  350. }
  351. /// <summary>
  352. /// Returns an observable that returns a single value but does not complete.
  353. /// </summary>
  354. /// <typeparam name="T">The type of the observable.</typeparam>
  355. /// <param name="value">The value.</param>
  356. /// <returns>The observable.</returns>
  357. private IObservable<T> Single<T>(T value)
  358. {
  359. return Observable.Never<T>().StartWith(value);
  360. }
  361. private class Class1 : AvaloniaObject
  362. {
  363. public static readonly StyledProperty<string> FooProperty =
  364. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  365. public static readonly StyledProperty<double> QuxProperty =
  366. AvaloniaProperty.Register<Class1, double>("Qux", 5.6);
  367. public static readonly StyledProperty<double> DoubleValueProperty =
  368. AvaloniaProperty.Register<Class1, double>(nameof(DoubleValue));
  369. public double DoubleValue
  370. {
  371. get { return GetValue(DoubleValueProperty); }
  372. set { SetValue(DoubleValueProperty, value); }
  373. }
  374. }
  375. private class Class2 : Class1
  376. {
  377. public static readonly StyledProperty<string> BarProperty =
  378. AvaloniaProperty.Register<Class2, string>("Bar", "bardefault");
  379. }
  380. private class TestOneTimeBinding : IBinding
  381. {
  382. private IObservable<object> _source;
  383. public TestOneTimeBinding(IObservable<object> source)
  384. {
  385. _source = source;
  386. }
  387. public InstancedBinding Initiate(
  388. IAvaloniaObject target,
  389. AvaloniaProperty targetProperty,
  390. object anchor = null,
  391. bool enableDataValidation = false)
  392. {
  393. return InstancedBinding.OneTime(_source);
  394. }
  395. }
  396. private class TestStackOverflowViewModel : INotifyPropertyChanged
  397. {
  398. public int SetterInvokedCount { get; private set; }
  399. public const int MaxInvokedCount = 1000;
  400. private double _value;
  401. public event PropertyChangedEventHandler PropertyChanged;
  402. public double Value
  403. {
  404. get { return _value; }
  405. set
  406. {
  407. if (_value != value)
  408. {
  409. SetterInvokedCount++;
  410. if (SetterInvokedCount < MaxInvokedCount)
  411. {
  412. _value = (int)value;
  413. if (_value > 75) _value = 75;
  414. if (_value < 25) _value = 25;
  415. }
  416. else
  417. {
  418. _value = value;
  419. }
  420. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
  421. }
  422. }
  423. }
  424. }
  425. }
  426. }