BindingTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 root = new Decorator
  142. {
  143. DataContext = new { Foo = "bar" },
  144. };
  145. var child = new Control();
  146. var values = new List<object>();
  147. child.GetObservable(Border.DataContextProperty).Subscribe(x => values.Add(x));
  148. child.Bind(Control.DataContextProperty, new Binding("Foo"));
  149. // When binding to DataContext and the target isn't found, the binding should produce
  150. // null rather than UnsetValue in order to not propagate incorrect DataContexts from
  151. // parent controls while things are being set up. This logic is implemented in
  152. // `Avalonia.Markup.Xaml.Binding.Initiate`.
  153. Assert.True(child.IsSet(Control.DataContextProperty));
  154. root.Child = child;
  155. Assert.Equal(new[] { null, "bar" }, values);
  156. }
  157. [Fact]
  158. public void Should_Use_DefaultValueConverter_When_No_Converter_Specified()
  159. {
  160. var target = new TextBlock(); ;
  161. var binding = new Binding
  162. {
  163. Path = "Foo",
  164. };
  165. var result = binding.Initiate(target, TextBox.TextProperty).Subject;
  166. Assert.IsType<DefaultValueConverter>(((ExpressionSubject)result).Converter);
  167. }
  168. [Fact]
  169. public void Should_Use_Supplied_Converter()
  170. {
  171. var target = new TextBlock();
  172. var converter = new Mock<IValueConverter>();
  173. var binding = new Binding
  174. {
  175. Converter = converter.Object,
  176. Path = "Foo",
  177. };
  178. var result = binding.Initiate(target, TextBox.TextProperty).Subject;
  179. Assert.Same(converter.Object, ((ExpressionSubject)result).Converter);
  180. }
  181. [Fact]
  182. public void Should_Pass_ConverterParameter_To_Supplied_Converter()
  183. {
  184. var target = new TextBlock();
  185. var converter = new Mock<IValueConverter>();
  186. var binding = new Binding
  187. {
  188. Converter = converter.Object,
  189. ConverterParameter = "foo",
  190. Path = "Bar",
  191. };
  192. var result = binding.Initiate(target, TextBox.TextProperty).Subject;
  193. Assert.Same("foo", ((ExpressionSubject)result).ConverterParameter);
  194. }
  195. [Fact]
  196. public void Should_Return_FallbackValue_When_Path_Not_Resolved()
  197. {
  198. var target = new TextBlock();
  199. var source = new Source();
  200. var binding = new Binding
  201. {
  202. Source = source,
  203. Path = "BadPath",
  204. FallbackValue = "foofallback",
  205. };
  206. target.Bind(TextBlock.TextProperty, binding);
  207. Assert.Equal("foofallback", target.Text);
  208. }
  209. [Fact]
  210. public void Should_Return_FallbackValue_When_Invalid_Source_Type()
  211. {
  212. var target = new ProgressBar();
  213. var source = new Source { Foo = "foo" };
  214. var binding = new Binding
  215. {
  216. Source = source,
  217. Path = "Foo",
  218. FallbackValue = 42,
  219. };
  220. target.Bind(ProgressBar.ValueProperty, binding);
  221. Assert.Equal(42, target.Value);
  222. }
  223. /// <summary>
  224. /// Tests a problem discovered with ListBox with selection.
  225. /// </summary>
  226. /// <remarks>
  227. /// - Items is bound to DataContext first, followed by say SelectedIndex
  228. /// - When the ListBox is removed from the logical tree, DataContext becomes null (as it's
  229. /// inherited)
  230. /// - This changes Items to null, which changes SelectedIndex to null as there are no
  231. /// longer any items
  232. /// - However, the news that DataContext is now null hasn't yet reached the SelectedIndex
  233. /// binding and so the unselection is sent back to the ViewModel
  234. /// </remarks>
  235. [Fact]
  236. public void Should_Not_Write_To_Old_DataContext()
  237. {
  238. var vm = new OldDataContextViewModel();
  239. var target = new OldDataContextTest();
  240. var fooBinding = new Binding
  241. {
  242. Path = "Foo",
  243. Mode = BindingMode.TwoWay,
  244. };
  245. var barBinding = new Binding
  246. {
  247. Path = "Bar",
  248. Mode = BindingMode.TwoWay,
  249. };
  250. // Bind Foo and Bar to the VM.
  251. target.Bind(OldDataContextTest.FooProperty, fooBinding);
  252. //target.Bind(OldDataContextTest.BarProperty, barBinding);
  253. target.DataContext = vm;
  254. // Make sure the control's Foo and Bar properties are read from the VM
  255. Assert.Equal(1, target.GetValue(OldDataContextTest.FooProperty));
  256. //Assert.Equal(2, target.GetValue(OldDataContextTest.BarProperty));
  257. // Set DataContext to null.
  258. target.DataContext = null;
  259. // Foo and Bar are no longer bound so they return 0, their default value.
  260. Assert.Equal(0, target.GetValue(OldDataContextTest.FooProperty));
  261. Assert.Equal(0, target.GetValue(OldDataContextTest.BarProperty));
  262. // The problem was here - DataContext is now null, setting Foo to 0. Bar is bound to
  263. // Foo so Bar also gets set to 0. However the Bar binding still had a reference to
  264. // the VM and so vm.Bar was set to 0 erroneously.
  265. Assert.Equal(1, vm.Foo);
  266. Assert.Equal(2, vm.Bar);
  267. }
  268. [Fact]
  269. public void AvaloniaObject_this_Operator_Accepts_Binding()
  270. {
  271. var target = new ContentControl
  272. {
  273. DataContext = new { Foo = "foo" }
  274. };
  275. target[!ContentControl.ContentProperty] = new Binding("Foo");
  276. Assert.Equal("foo", target.Content);
  277. }
  278. private class TwoWayBindingTest : Control
  279. {
  280. public static readonly StyledProperty<string> TwoWayProperty =
  281. AvaloniaProperty.Register<TwoWayBindingTest, string>(
  282. "TwoWay",
  283. defaultBindingMode: BindingMode.TwoWay);
  284. public string TwoWay
  285. {
  286. get { return GetValue(TwoWayProperty); }
  287. set { SetValue(TwoWayProperty, value); }
  288. }
  289. }
  290. public class Source : ReactiveObject
  291. {
  292. private string _foo;
  293. public string Foo
  294. {
  295. get { return _foo; }
  296. set { this.RaiseAndSetIfChanged(ref _foo, value); }
  297. }
  298. }
  299. private class OldDataContextViewModel
  300. {
  301. public int Foo { get; set; } = 1;
  302. public int Bar { get; set; } = 2;
  303. }
  304. private class OldDataContextTest : Control
  305. {
  306. public static readonly StyledProperty<int> FooProperty =
  307. AvaloniaProperty.Register<OldDataContextTest, int>("Foo");
  308. public static readonly StyledProperty<int> BarProperty =
  309. AvaloniaProperty.Register<OldDataContextTest, int>("Bar");
  310. public OldDataContextTest()
  311. {
  312. Bind(BarProperty, this.GetObservable(FooProperty));
  313. }
  314. }
  315. private class InheritanceTest : Decorator
  316. {
  317. public static readonly StyledProperty<int> BazProperty =
  318. AvaloniaProperty.Register<InheritanceTest, int>("Baz", defaultValue: 6, inherits: true);
  319. public int Baz
  320. {
  321. get { return GetValue(BazProperty); }
  322. set { SetValue(BazProperty, value); }
  323. }
  324. }
  325. }
  326. }