// 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 System.Globalization; using System.Reactive.Subjects; using Avalonia.Controls; using Avalonia.Controls.Templates; using Avalonia.Data; using Avalonia.Data.Converters; using Moq; using Xunit; namespace Avalonia.Styling.UnitTests { public class SetterTests { [Fact] public void Cannot_Assign_Control_To_Value() { var target = new Setter(); Assert.Throws(() => target.Value = new Border()); } [Fact] public void Setter_Should_Apply_Binding_To_Property() { var control = new TextBlock(); var subject = new BehaviorSubject("foo"); var descriptor = InstancedBinding.OneWay(subject); var binding = Mock.Of(x => x.Initiate(control, TextBlock.TextProperty, null, false) == descriptor); var style = Mock.Of(); var setter = new Setter(TextBlock.TextProperty, binding); setter.Apply(style, control, null); Assert.Equal("foo", control.Text); } [Fact] public void Setter_Should_Materialize_Template_To_Property() { var control = new Decorator(); var template = new FuncTemplate(() => new Canvas()); var style = Mock.Of(); var setter = new Setter(Decorator.ChildProperty, template); setter.Apply(style, control, null); Assert.IsType(control.Child); } [Fact] public void Does_Not_Call_Converter_ConvertBack_On_OneWay_Binding() { var control = new Decorator { Name = "foo" }; var style = Mock.Of(); var binding = new Binding("Name", BindingMode.OneWay) { Converter = new TestConverter(), RelativeSource = new RelativeSource(RelativeSourceMode.Self), }; var setter = new Setter(Decorator.TagProperty, binding); var activator = new BehaviorSubject(true); setter.Apply(style, control, activator); Assert.Equal("foobar", control.Tag); // Issue #1218 caused TestConverter.ConvertBack to throw here. activator.OnNext(false); Assert.Null(control.Tag); } [Fact] public void Setter_Should_Apply_Value_Without_Activator_With_Style_Priority() { var control = new Mock(); var style = Mock.Of