BindingTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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.Linq;
  6. using System.Reactive.Linq;
  7. using Avalonia.Controls;
  8. using Avalonia.Data;
  9. using Avalonia.Markup.Data;
  10. using Avalonia.Markup.Xaml.Data;
  11. using Moq;
  12. using Xunit;
  13. using System.ComponentModel;
  14. using System.Runtime.CompilerServices;
  15. namespace Avalonia.Markup.Xaml.UnitTests.Data
  16. {
  17. public class BindingTests
  18. {
  19. [Fact]
  20. public void OneWay_Binding_Should_Be_Set_Up()
  21. {
  22. var source = new Source { Foo = "foo" };
  23. var target = new TextBlock { DataContext = source };
  24. var binding = new Binding
  25. {
  26. Path = "Foo",
  27. Mode = BindingMode.OneWay,
  28. };
  29. target.Bind(TextBox.TextProperty, binding);
  30. Assert.Equal("foo", target.Text);
  31. source.Foo = "bar";
  32. Assert.Equal("bar", target.Text);
  33. target.Text = "baz";
  34. Assert.Equal("bar", source.Foo);
  35. }
  36. [Fact]
  37. public void TwoWay_Binding_Should_Be_Set_Up()
  38. {
  39. var source = new Source { Foo = "foo" };
  40. var target = new TextBlock { DataContext = source };
  41. var binding = new Binding
  42. {
  43. Path = "Foo",
  44. Mode = BindingMode.TwoWay,
  45. };
  46. target.Bind(TextBox.TextProperty, binding);
  47. Assert.Equal("foo", target.Text);
  48. source.Foo = "bar";
  49. Assert.Equal("bar", target.Text);
  50. target.Text = "baz";
  51. Assert.Equal("baz", source.Foo);
  52. }
  53. [Fact]
  54. public void OneTime_Binding_Should_Be_Set_Up()
  55. {
  56. var source = new Source { Foo = "foo" };
  57. var target = new TextBlock { DataContext = source };
  58. var binding = new Binding
  59. {
  60. Path = "Foo",
  61. Mode = BindingMode.OneTime,
  62. };
  63. target.Bind(TextBox.TextProperty, binding);
  64. Assert.Equal("foo", target.Text);
  65. source.Foo = "bar";
  66. Assert.Equal("foo", target.Text);
  67. target.Text = "baz";
  68. Assert.Equal("bar", source.Foo);
  69. }
  70. [Fact]
  71. public void OneWayToSource_Binding_Should_Be_Set_Up()
  72. {
  73. var source = new Source { Foo = "foo" };
  74. var target = new TextBlock { DataContext = source, Text = "bar" };
  75. var binding = new Binding
  76. {
  77. Path = "Foo",
  78. Mode = BindingMode.OneWayToSource,
  79. };
  80. target.Bind(TextBox.TextProperty, binding);
  81. Assert.Equal("bar", source.Foo);
  82. target.Text = "baz";
  83. Assert.Equal("baz", source.Foo);
  84. source.Foo = "quz";
  85. Assert.Equal("baz", target.Text);
  86. }
  87. [Fact]
  88. public void Default_BindingMode_Should_Be_Used()
  89. {
  90. var source = new Source { Foo = "foo" };
  91. var target = new TwoWayBindingTest { DataContext = source };
  92. var binding = new Binding
  93. {
  94. Path = "Foo",
  95. };
  96. target.Bind(TwoWayBindingTest.TwoWayProperty, binding);
  97. Assert.Equal("foo", target.TwoWay);
  98. source.Foo = "bar";
  99. Assert.Equal("bar", target.TwoWay);
  100. target.TwoWay = "baz";
  101. Assert.Equal("baz", source.Foo);
  102. }
  103. [Fact]
  104. public void DataContext_Binding_Should_Use_Parent_DataContext()
  105. {
  106. var parentDataContext = Mock.Of<IHeadered>(x => x.Header == (object)"Foo");
  107. var parent = new Decorator
  108. {
  109. Child = new Control(),
  110. DataContext = parentDataContext,
  111. };
  112. var binding = new Binding
  113. {
  114. Path = "Header",
  115. };
  116. parent.Child.Bind(Control.DataContextProperty, binding);
  117. Assert.Equal("Foo", parent.Child.DataContext);
  118. parentDataContext = Mock.Of<IHeadered>(x => x.Header == (object)"Bar");
  119. parent.DataContext = parentDataContext;
  120. Assert.Equal("Bar", parent.Child.DataContext);
  121. }
  122. [Fact]
  123. public void DataContext_Binding_Should_Track_Parent()
  124. {
  125. var parent = new Decorator
  126. {
  127. DataContext = new { Foo = "foo" },
  128. };
  129. var child = new Control();
  130. var binding = new Binding
  131. {
  132. Path = "Foo",
  133. };
  134. child.Bind(Control.DataContextProperty, binding);
  135. Assert.Null(child.DataContext);
  136. parent.Child = child;
  137. Assert.Equal("foo", child.DataContext);
  138. }
  139. [Fact]
  140. public void DataContext_Binding_Should_Produce_Correct_Results()
  141. {
  142. var viewModel = new { Foo = "bar" };
  143. var root = new Decorator
  144. {
  145. DataContext = viewModel,
  146. };
  147. var child = new Control();
  148. var values = new List<object>();
  149. child.GetObservable(Control.DataContextProperty).Subscribe(x => values.Add(x));
  150. child.Bind(Control.DataContextProperty, new Binding("Foo"));
  151. // When binding to DataContext and the target isn't found, the binding should produce
  152. // null rather than UnsetValue in order to not propagate incorrect DataContexts from
  153. // parent controls while things are being set up. This logic is implemented in
  154. // `Avalonia.Markup.Xaml.Binding.Initiate`.
  155. Assert.True(child.IsSet(Control.DataContextProperty));
  156. root.Child = child;
  157. Assert.Equal(new[] { null, "bar" }, values);
  158. }
  159. [Fact]
  160. public void Should_Use_DefaultValueConverter_When_No_Converter_Specified()
  161. {
  162. var target = new TextBlock(); ;
  163. var binding = new Binding
  164. {
  165. Path = "Foo",
  166. };
  167. var result = binding.Initiate(target, TextBox.TextProperty).Subject;
  168. Assert.IsType<DefaultValueConverter>(((BindingExpression)result).Converter);
  169. }
  170. [Fact]
  171. public void Should_Use_Supplied_Converter()
  172. {
  173. var target = new TextBlock();
  174. var converter = new Mock<IValueConverter>();
  175. var binding = new Binding
  176. {
  177. Converter = converter.Object,
  178. Path = "Foo",
  179. };
  180. var result = binding.Initiate(target, TextBox.TextProperty).Subject;
  181. Assert.Same(converter.Object, ((BindingExpression)result).Converter);
  182. }
  183. [Fact]
  184. public void Should_Pass_ConverterParameter_To_Supplied_Converter()
  185. {
  186. var target = new TextBlock();
  187. var converter = new Mock<IValueConverter>();
  188. var binding = new Binding
  189. {
  190. Converter = converter.Object,
  191. ConverterParameter = "foo",
  192. Path = "Bar",
  193. };
  194. var result = binding.Initiate(target, TextBox.TextProperty).Subject;
  195. Assert.Same("foo", ((BindingExpression)result).ConverterParameter);
  196. }
  197. [Fact]
  198. public void Should_Return_FallbackValue_When_Path_Not_Resolved()
  199. {
  200. var target = new TextBlock();
  201. var source = new Source();
  202. var binding = new Binding
  203. {
  204. Source = source,
  205. Path = "BadPath",
  206. FallbackValue = "foofallback",
  207. };
  208. target.Bind(TextBlock.TextProperty, binding);
  209. Assert.Equal("foofallback", target.Text);
  210. }
  211. [Fact]
  212. public void Should_Return_FallbackValue_When_Invalid_Source_Type()
  213. {
  214. var target = new ProgressBar();
  215. var source = new Source { Foo = "foo" };
  216. var binding = new Binding
  217. {
  218. Source = source,
  219. Path = "Foo",
  220. FallbackValue = 42,
  221. };
  222. target.Bind(ProgressBar.ValueProperty, binding);
  223. Assert.Equal(42, target.Value);
  224. }
  225. /// <summary>
  226. /// Tests a problem discovered with ListBox with selection.
  227. /// </summary>
  228. /// <remarks>
  229. /// - Items is bound to DataContext first, followed by say SelectedIndex
  230. /// - When the ListBox is removed from the logical tree, DataContext becomes null (as it's
  231. /// inherited)
  232. /// - This changes Items to null, which changes SelectedIndex to null as there are no
  233. /// longer any items
  234. /// - However, the news that DataContext is now null hasn't yet reached the SelectedIndex
  235. /// binding and so the unselection is sent back to the ViewModel
  236. /// </remarks>
  237. [Fact]
  238. public void Should_Not_Write_To_Old_DataContext()
  239. {
  240. var vm = new OldDataContextViewModel();
  241. var target = new OldDataContextTest();
  242. var fooBinding = new Binding
  243. {
  244. Path = "Foo",
  245. Mode = BindingMode.TwoWay,
  246. };
  247. var barBinding = new Binding
  248. {
  249. Path = "Bar",
  250. Mode = BindingMode.TwoWay,
  251. };
  252. // Bind Foo and Bar to the VM.
  253. target.Bind(OldDataContextTest.FooProperty, fooBinding);
  254. target.Bind(OldDataContextTest.BarProperty, barBinding);
  255. target.DataContext = vm;
  256. // Make sure the control's Foo and Bar properties are read from the VM
  257. Assert.Equal(1, target.GetValue(OldDataContextTest.FooProperty));
  258. Assert.Equal(2, target.GetValue(OldDataContextTest.BarProperty));
  259. // Set DataContext to null.
  260. target.DataContext = null;
  261. // Foo and Bar are no longer bound so they return 0, their default value.
  262. Assert.Equal(0, target.GetValue(OldDataContextTest.FooProperty));
  263. Assert.Equal(0, target.GetValue(OldDataContextTest.BarProperty));
  264. // The problem was here - DataContext is now null, setting Foo to 0. Bar is bound to
  265. // Foo so Bar also gets set to 0. However the Bar binding still had a reference to
  266. // the VM and so vm.Bar was set to 0 erroneously.
  267. Assert.Equal(1, vm.Foo);
  268. Assert.Equal(2, vm.Bar);
  269. }
  270. [Fact]
  271. public void AvaloniaObject_this_Operator_Accepts_Binding()
  272. {
  273. var target = new ContentControl
  274. {
  275. DataContext = new { Foo = "foo" }
  276. };
  277. target[!ContentControl.ContentProperty] = new Binding("Foo");
  278. Assert.Equal("foo", target.Content);
  279. }
  280. private class TwoWayBindingTest : Control
  281. {
  282. public static readonly StyledProperty<string> TwoWayProperty =
  283. AvaloniaProperty.Register<TwoWayBindingTest, string>(
  284. "TwoWay",
  285. defaultBindingMode: BindingMode.TwoWay);
  286. public string TwoWay
  287. {
  288. get { return GetValue(TwoWayProperty); }
  289. set { SetValue(TwoWayProperty, value); }
  290. }
  291. }
  292. public class Source : INotifyPropertyChanged
  293. {
  294. private string _foo;
  295. public string Foo
  296. {
  297. get { return _foo; }
  298. set
  299. {
  300. _foo = value;
  301. RaisePropertyChanged();
  302. }
  303. }
  304. public event PropertyChangedEventHandler PropertyChanged;
  305. private void RaisePropertyChanged([CallerMemberName] string prop = "")
  306. {
  307. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
  308. }
  309. }
  310. private class OldDataContextViewModel
  311. {
  312. public int Foo { get; set; } = 1;
  313. public int Bar { get; set; } = 2;
  314. }
  315. private class OldDataContextTest : Control
  316. {
  317. public static readonly StyledProperty<int> FooProperty =
  318. AvaloniaProperty.Register<OldDataContextTest, int>("Foo");
  319. public static readonly StyledProperty<int> BarProperty =
  320. AvaloniaProperty.Register<OldDataContextTest, int>("Bar");
  321. public OldDataContextTest()
  322. {
  323. Bind(BarProperty, this.GetObservable(FooProperty));
  324. }
  325. }
  326. private class InheritanceTest : Decorator
  327. {
  328. public static readonly StyledProperty<int> BazProperty =
  329. AvaloniaProperty.Register<InheritanceTest, int>("Baz", defaultValue: 6, inherits: true);
  330. public int Baz
  331. {
  332. get { return GetValue(BazProperty); }
  333. set { SetValue(BazProperty, value); }
  334. }
  335. }
  336. }
  337. }