// Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; using Moq; using Avalonia.Controls; using Avalonia.Data; using Avalonia.Markup.Xaml.Data; using Avalonia.Styling; using Xunit; using System.Reactive.Disposables; namespace Avalonia.Markup.Xaml.UnitTests.Data { public class BindingTests_Self { [Fact] public void Binding_To_Property_On_Self_Should_Work() { var target = new TextBlock { Tag = "Hello World!", [!TextBlock.TextProperty] = new Binding("Tag") { RelativeSource = new RelativeSource(RelativeSourceMode.Self) }, }; Assert.Equal("Hello World!", target.Text); } [Fact] public void TwoWay_Binding_To_Property_On_Self_Should_Work() { var target = new TextBlock { Tag = "Hello World!", [!TextBlock.TextProperty] = new Binding("Tag", BindingMode.TwoWay) { RelativeSource = new RelativeSource(RelativeSourceMode.Self) }, }; Assert.Equal("Hello World!", target.Text); target.Text = "Goodbye cruel world :("; Assert.Equal("Goodbye cruel world :(", target.Text); } private Mock CreateTarget( ITemplatedControl templatedParent = null, string text = null) { var result = new Mock(); result.Setup(x => x.GetValue(Control.TemplatedParentProperty)).Returns(templatedParent); result.Setup(x => x.GetValue((AvaloniaProperty)Control.TemplatedParentProperty)).Returns(templatedParent); result.Setup(x => x.GetValue((AvaloniaProperty)TextBox.TextProperty)).Returns(text); result.Setup(x => x.Bind(It.IsAny(), It.IsAny>(), It.IsAny())) .Returns(Disposable.Empty); return result; } } }