BindingTests.cs 12 KB

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