|
| 1 | +using Avalonia.Controls; |
| 2 | +using Avalonia.Data; |
| 3 | +using Avalonia.Data.Converters; |
| 4 | +using Avalonia.Styling; |
| 5 | +using Avalonia.UnitTests; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Avalonia.Base.UnitTests.Data; |
| 9 | + |
| 10 | +public class BindingOperationsTests |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public void GetBindingExpressionBase_Returns_Null_When_Not_Bound() |
| 14 | + { |
| 15 | + var target = new Control(); |
| 16 | + var expression = BindingOperations.GetBindingExpressionBase(target, Control.TagProperty); |
| 17 | + Assert.Null(expression); |
| 18 | + } |
| 19 | + |
| 20 | + [Theory] |
| 21 | + [InlineData(BindingPriority.Animation)] |
| 22 | + [InlineData(BindingPriority.LocalValue)] |
| 23 | + [InlineData(BindingPriority.Style)] |
| 24 | + [InlineData(BindingPriority.StyleTrigger)] |
| 25 | + public void GetBindingExpressionBase_Returns_Expression_When_Bound(BindingPriority priority) |
| 26 | + { |
| 27 | + var data = new { Tag = "foo" }; |
| 28 | + var target = new Control { DataContext = data }; |
| 29 | + var binding = new Binding("Tag") { Priority = priority }; |
| 30 | + target.Bind(Control.TagProperty, binding); |
| 31 | + |
| 32 | + var expression = BindingOperations.GetBindingExpressionBase(target, Control.TagProperty); |
| 33 | + Assert.NotNull(expression); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void GetBindingExpressionBase_Returns_Expression_When_Bound_Locally_With_Binding_Error() |
| 38 | + { |
| 39 | + // Target has no data context so binding will fail. |
| 40 | + var target = new Control(); |
| 41 | + var binding = new Binding("Tag"); |
| 42 | + target.Bind(Control.TagProperty, binding); |
| 43 | + |
| 44 | + var expression = BindingOperations.GetBindingExpressionBase(target, Control.TagProperty); |
| 45 | + Assert.NotNull(expression); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void GetBindingExpressionBase_Returns_Expression_When_Bound_To_MultiBinding() |
| 50 | + { |
| 51 | + var data = new { Tag = "foo" }; |
| 52 | + var target = new Control { DataContext = data }; |
| 53 | + var binding = new MultiBinding |
| 54 | + { |
| 55 | + Converter = new FuncMultiValueConverter<object, string>(x => string.Join(',', x)), |
| 56 | + Bindings = |
| 57 | + { |
| 58 | + new Binding("Tag"), |
| 59 | + new Binding("Tag"), |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + target.Bind(Control.TagProperty, binding); |
| 64 | + |
| 65 | + var expression = BindingOperations.GetBindingExpressionBase(target, Control.TagProperty); |
| 66 | + Assert.NotNull(expression); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void GetBindingExpressionBase_Returns_Binding_When_Bound_Via_ControlTheme() |
| 71 | + { |
| 72 | + var target = new Control(); |
| 73 | + var binding = new Binding("Tag"); |
| 74 | + var theme = new ControlTheme(typeof(Control)) |
| 75 | + { |
| 76 | + Setters = { new Setter(Control.TagProperty, binding) }, |
| 77 | + }; |
| 78 | + |
| 79 | + target.Theme = theme; |
| 80 | + var root = new TestRoot(target); |
| 81 | + root.UpdateLayout(); |
| 82 | + |
| 83 | + var expression = BindingOperations.GetBindingExpressionBase(target, Control.TagProperty); |
| 84 | + Assert.NotNull(expression); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void GetBindingExpressionBase_Returns_Binding_When_Bound_Via_ControlTheme_TemplateBinding() |
| 89 | + { |
| 90 | + var target = new Control(); |
| 91 | + var binding = new TemplateBinding(Control.TagProperty); |
| 92 | + var theme = new ControlTheme(typeof(Control)) |
| 93 | + { |
| 94 | + Setters = { new Setter(Control.TagProperty, binding) }, |
| 95 | + }; |
| 96 | + |
| 97 | + target.Theme = theme; |
| 98 | + var root = new TestRoot(target); |
| 99 | + root.UpdateLayout(); |
| 100 | + |
| 101 | + var expression = BindingOperations.GetBindingExpressionBase(target, Control.TagProperty); |
| 102 | + Assert.NotNull(expression); |
| 103 | + } |
| 104 | + |
| 105 | + [Fact] |
| 106 | + public void GetBindingExpressionBase_Returns_Binding_When_Bound_Via_ControlTheme_Style() |
| 107 | + { |
| 108 | + var target = new Control { Classes = { "foo" } }; |
| 109 | + var binding = new Binding("Tag"); |
| 110 | + var theme = new ControlTheme(typeof(Control)) |
| 111 | + { |
| 112 | + Children = |
| 113 | + { |
| 114 | + new Style(x => x.Nesting().Class("foo")) |
| 115 | + { |
| 116 | + Setters = { new Setter(Control.TagProperty, binding) }, |
| 117 | + }, |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + target.Theme = theme; |
| 122 | + var root = new TestRoot(target); |
| 123 | + root.UpdateLayout(); |
| 124 | + |
| 125 | + var expression = BindingOperations.GetBindingExpressionBase(target, Control.TagProperty); |
| 126 | + Assert.NotNull(expression); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public void GetBindingExpressionBase_Returns_Binding_When_Bound_Via_Style() |
| 131 | + { |
| 132 | + var target = new Control(); |
| 133 | + var binding = new Binding("Tag"); |
| 134 | + var style = new Style(x => x.OfType<Control>()) |
| 135 | + { |
| 136 | + Setters = { new Setter(Control.TagProperty, binding) }, |
| 137 | + }; |
| 138 | + |
| 139 | + var root = new TestRoot(); |
| 140 | + root.Styles.Add(style); |
| 141 | + root.Child = target; |
| 142 | + root.UpdateLayout(); |
| 143 | + |
| 144 | + var expression = BindingOperations.GetBindingExpressionBase(target, Control.TagProperty); |
| 145 | + Assert.NotNull(expression); |
| 146 | + } |
| 147 | +} |
0 commit comments