MultiBindingTests.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Reactive.Linq;
  6. using System.Threading.Tasks;
  7. using Avalonia.Controls;
  8. using Avalonia.Data;
  9. using Avalonia.Data.Converters;
  10. using Avalonia.UnitTests;
  11. using Xunit;
  12. #nullable enable
  13. namespace Avalonia.Markup.UnitTests.Data
  14. {
  15. public class MultiBindingTests : ScopedTestBase
  16. {
  17. [Fact]
  18. public void OneWay_Binding_Should_Be_Set_Up()
  19. {
  20. var source = new { A = 1, B = 2, C = 3 };
  21. var binding = new MultiBinding
  22. {
  23. Converter = new ConcatConverter(),
  24. Bindings = new[]
  25. {
  26. new Binding { Path = "A" },
  27. new Binding { Path = "B" },
  28. new Binding { Path = "C" },
  29. }
  30. };
  31. var target = new Control { DataContext = source };
  32. target.Bind(Control.TagProperty, binding);
  33. Assert.Equal("1,2,3", target.Tag);
  34. }
  35. [Fact]
  36. public void Nested_MultiBinding_Should_Be_Set_Up()
  37. {
  38. var source = new { A = 1, B = 2, C = 3 };
  39. var binding = new MultiBinding
  40. {
  41. Converter = new ConcatConverter(),
  42. Bindings =
  43. {
  44. new Binding { Path = "A" },
  45. new MultiBinding
  46. {
  47. Converter = new ConcatConverter(),
  48. Bindings =
  49. {
  50. new Binding { Path = "B"},
  51. new Binding { Path = "C"}
  52. }
  53. }
  54. }
  55. };
  56. var target = new Control { DataContext = source };
  57. target.Bind(Control.TagProperty, binding);
  58. Assert.Equal("1,2,3", target.Tag);
  59. }
  60. [Fact]
  61. public void Should_Return_FallbackValue_When_Converter_Returns_UnsetValue()
  62. {
  63. var target = new TextBlock();
  64. var source = new { A = 1, B = 2, C = 3 };
  65. var binding = new MultiBinding
  66. {
  67. Converter = new UnsetValueConverter(),
  68. Bindings = new[]
  69. {
  70. new Binding { Path = "A" },
  71. new Binding { Path = "B" },
  72. new Binding { Path = "C" },
  73. },
  74. FallbackValue = "fallback",
  75. };
  76. target.Bind(TextBlock.TextProperty, binding);
  77. Assert.NotNull(target.Text);
  78. Assert.Equal("fallback", target.Text);
  79. }
  80. [Fact]
  81. public void Should_Return_TargetNullValue_When_Value_Is_Null()
  82. {
  83. var target = new TextBlock();
  84. var binding = new MultiBinding
  85. {
  86. Converter = new NullValueConverter(),
  87. Bindings = new[]
  88. {
  89. new Binding { Path = "A" },
  90. new Binding { Path = "B" },
  91. new Binding { Path = "C" },
  92. },
  93. TargetNullValue = "(null)",
  94. };
  95. target.Bind(TextBlock.TextProperty, binding);
  96. Assert.NotNull(target.Text);
  97. Assert.Equal("(null)", target.Text);
  98. }
  99. [Fact]
  100. public void Should_Pass_UnsetValue_To_Converter_For_Broken_Binding()
  101. {
  102. var source = new { A = 1, B = 2, C = 3 };
  103. var target = new TextBlock { DataContext = source };
  104. var binding = new MultiBinding
  105. {
  106. Converter = new ConcatConverter(),
  107. Bindings = new[]
  108. {
  109. new Binding { Path = "A" },
  110. new Binding { Path = "B" },
  111. new Binding { Path = "Missing" },
  112. },
  113. };
  114. target.Bind(TextBlock.TextProperty, binding);
  115. Assert.NotNull(target.Text);
  116. Assert.Equal("1,2,(unset)", target.Text);
  117. }
  118. [Fact]
  119. public void Should_Pass_FallbackValue_To_Converter_For_Broken_Binding()
  120. {
  121. var source = new { A = 1, B = 2, C = 3 };
  122. var target = new TextBlock { DataContext = source };
  123. var binding = new MultiBinding
  124. {
  125. Converter = new ConcatConverter(),
  126. Bindings = new[]
  127. {
  128. new Binding { Path = "A" },
  129. new Binding { Path = "B" },
  130. new Binding { Path = "Missing", FallbackValue = "Fallback" },
  131. },
  132. };
  133. target.Bind(TextBlock.TextProperty, binding);
  134. Assert.NotNull(target.Text);
  135. Assert.Equal("1,2,Fallback", target.Text);
  136. }
  137. [Fact]
  138. public void MultiBinding_Without_StringFormat_And_Converter()
  139. {
  140. var source = new { A = 1, B = 2, C = 3 };
  141. var target = new ItemsControl { };
  142. var binding = new MultiBinding
  143. {
  144. Bindings = new[]
  145. {
  146. new Binding { Path = "A", Source = source },
  147. new Binding { Path = "B", Source = source },
  148. new Binding { Path = "C", Source = source },
  149. },
  150. };
  151. target.Bind(ItemsControl.ItemsSourceProperty, binding);
  152. Assert.Equal(target.ItemCount, 3);
  153. Assert.Equal(target.ItemsView[0], source.A);
  154. Assert.Equal(target.ItemsView[1], source.B);
  155. Assert.Equal(target.ItemsView[2], source.C);
  156. }
  157. [Fact]
  158. public void Converter_Can_Return_BindingNotification()
  159. {
  160. var source = new { A = 1, B = 2, C = 3 };
  161. var target = new TextBlock { DataContext = source };
  162. var binding = new MultiBinding
  163. {
  164. Converter = new BindingNotificationConverter(),
  165. Bindings = new[]
  166. {
  167. new Binding { Path = "A" },
  168. new Binding { Path = "B" },
  169. new Binding { Path = "C" },
  170. },
  171. };
  172. target.Bind(TextBlock.TextProperty, binding);
  173. Assert.Equal("1,2,3-BindingNotification", target.Text);
  174. }
  175. [Fact]
  176. public void Converter_Should_Be_Called_On_PropertyChanged_Even_If_Property_Not_Changed()
  177. {
  178. // Issue #16084
  179. var data = new TestModel();
  180. var target = new TextBlock { DataContext = data };
  181. var binding = new MultiBinding
  182. {
  183. Converter = new TestModelMemberConverter(),
  184. Bindings =
  185. {
  186. new Binding(),
  187. new Binding(nameof(data.NotifyingValue)),
  188. },
  189. };
  190. target.Bind(TextBlock.TextProperty, binding);
  191. Assert.Equal("0", target.Text);
  192. data.NonNotifyingValue = 1;
  193. Assert.Equal("0", target.Text);
  194. data.NotifyingValue = new object();
  195. Assert.Equal("1", target.Text);
  196. }
  197. private partial class TestModel : NotifyingBase
  198. {
  199. private object? _notifyingValue;
  200. public int? NonNotifyingValue { get; set; } = 0;
  201. public object? NotifyingValue
  202. {
  203. get => _notifyingValue;
  204. set => SetField(ref _notifyingValue, value);
  205. }
  206. }
  207. private class ConcatConverter : IMultiValueConverter
  208. {
  209. public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
  210. {
  211. return string.Join(",", values);
  212. }
  213. }
  214. private class UnsetValueConverter : IMultiValueConverter
  215. {
  216. public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
  217. {
  218. return AvaloniaProperty.UnsetValue;
  219. }
  220. }
  221. private class NullValueConverter : IMultiValueConverter
  222. {
  223. public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
  224. {
  225. return null;
  226. }
  227. }
  228. private class BindingNotificationConverter : IMultiValueConverter
  229. {
  230. public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
  231. {
  232. return new BindingNotification(
  233. new ArgumentException(),
  234. BindingErrorType.Error,
  235. string.Join(",", values) + "-BindingNotification");
  236. }
  237. }
  238. private class TestModelMemberConverter : IMultiValueConverter
  239. {
  240. public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
  241. {
  242. if (values[0] is not TestModel model)
  243. {
  244. return string.Empty;
  245. }
  246. return model.NonNotifyingValue.ToString();
  247. }
  248. }
  249. }
  250. }