| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- // 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.ComponentModel;
- using System.Reactive.Concurrency;
- using System.Reactive.Linq;
- using System.Reactive.Subjects;
- using System.Threading;
- using System.Threading.Tasks;
- using Avalonia.Data;
- using Avalonia.Logging;
- using Avalonia.Markup.Xaml.Data;
- using Avalonia.Platform;
- using Avalonia.Threading;
- using Avalonia.UnitTests;
- using Microsoft.Reactive.Testing;
- using Moq;
- using Xunit;
- namespace Avalonia.Base.UnitTests
- {
- public class AvaloniaObjectTests_Binding
- {
- [Fact]
- public void Bind_Sets_Current_Value()
- {
- Class1 target = new Class1();
- Class1 source = new Class1();
- source.SetValue(Class1.FooProperty, "initial");
- target.Bind(Class1.FooProperty, source.GetObservable(Class1.FooProperty));
- Assert.Equal("initial", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void Bind_NonGeneric_Sets_Current_Value()
- {
- Class1 target = new Class1();
- Class1 source = new Class1();
- source.SetValue(Class1.FooProperty, "initial");
- target.Bind((AvaloniaProperty)Class1.FooProperty, source.GetObservable(Class1.FooProperty));
- Assert.Equal("initial", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void Bind_To_ValueType_Accepts_UnsetValue()
- {
- var target = new Class1();
- var source = new Subject<object>();
- target.Bind(Class1.QuxProperty, source);
- source.OnNext(6.7);
- source.OnNext(AvaloniaProperty.UnsetValue);
- Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
- Assert.False(target.IsSet(Class1.QuxProperty));
- }
- [Fact]
- public void OneTime_Binding_Ignores_UnsetValue()
- {
- var target = new Class1();
- var source = new Subject<object>();
- target.Bind(Class1.QuxProperty, new TestOneTimeBinding(source));
- source.OnNext(AvaloniaProperty.UnsetValue);
- Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
- source.OnNext(6.7);
- Assert.Equal(6.7, target.GetValue(Class1.QuxProperty));
- }
- [Fact]
- public void OneTime_Binding_Ignores_Binding_Errors()
- {
- var target = new Class1();
- var source = new Subject<object>();
- target.Bind(Class1.QuxProperty, new TestOneTimeBinding(source));
- source.OnNext(new BindingNotification(new Exception(), BindingErrorType.Error));
- Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
- source.OnNext(6.7);
- Assert.Equal(6.7, target.GetValue(Class1.QuxProperty));
- }
- [Fact]
- public void Bind_Throws_Exception_For_Unregistered_Property()
- {
- Class1 target = new Class1();
- Assert.Throws<ArgumentException>(() =>
- {
- target.Bind(Class2.BarProperty, Observable.Return("foo"));
- });
- }
- [Fact]
- public void Bind_Sets_Subsequent_Value()
- {
- Class1 target = new Class1();
- Class1 source = new Class1();
- source.SetValue(Class1.FooProperty, "initial");
- target.Bind(Class1.FooProperty, source.GetObservable(Class1.FooProperty));
- source.SetValue(Class1.FooProperty, "subsequent");
- Assert.Equal("subsequent", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void Bind_Ignores_Invalid_Value_Type()
- {
- Class1 target = new Class1();
- target.Bind((AvaloniaProperty)Class1.FooProperty, Observable.Return((object)123));
- Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void Observable_Is_Unsubscribed_When_Subscription_Disposed()
- {
- var scheduler = new TestScheduler();
- var source = scheduler.CreateColdObservable<object>();
- var target = new Class1();
- var subscription = target.Bind(Class1.FooProperty, source);
- Assert.Equal(1, source.Subscriptions.Count);
- Assert.Equal(Subscription.Infinite, source.Subscriptions[0].Unsubscribe);
- subscription.Dispose();
- Assert.Equal(1, source.Subscriptions.Count);
- Assert.Equal(0, source.Subscriptions[0].Unsubscribe);
- }
- [Fact]
- public void Two_Way_Separate_Binding_Works()
- {
- Class1 obj1 = new Class1();
- Class1 obj2 = new Class1();
- obj1.SetValue(Class1.FooProperty, "initial1");
- obj2.SetValue(Class1.FooProperty, "initial2");
- obj1.Bind(Class1.FooProperty, obj2.GetObservable(Class1.FooProperty));
- obj2.Bind(Class1.FooProperty, obj1.GetObservable(Class1.FooProperty));
- Assert.Equal("initial2", obj1.GetValue(Class1.FooProperty));
- Assert.Equal("initial2", obj2.GetValue(Class1.FooProperty));
- obj1.SetValue(Class1.FooProperty, "first");
- Assert.Equal("first", obj1.GetValue(Class1.FooProperty));
- Assert.Equal("first", obj2.GetValue(Class1.FooProperty));
- obj2.SetValue(Class1.FooProperty, "second");
- Assert.Equal("second", obj1.GetValue(Class1.FooProperty));
- Assert.Equal("second", obj2.GetValue(Class1.FooProperty));
- obj1.SetValue(Class1.FooProperty, "third");
- Assert.Equal("third", obj1.GetValue(Class1.FooProperty));
- Assert.Equal("third", obj2.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void Two_Way_Binding_With_Priority_Works()
- {
- Class1 obj1 = new Class1();
- Class1 obj2 = new Class1();
- obj1.SetValue(Class1.FooProperty, "initial1", BindingPriority.Style);
- obj2.SetValue(Class1.FooProperty, "initial2", BindingPriority.Style);
- obj1.Bind(Class1.FooProperty, obj2.GetObservable(Class1.FooProperty), BindingPriority.Style);
- obj2.Bind(Class1.FooProperty, obj1.GetObservable(Class1.FooProperty), BindingPriority.Style);
- Assert.Equal("initial2", obj1.GetValue(Class1.FooProperty));
- Assert.Equal("initial2", obj2.GetValue(Class1.FooProperty));
- obj1.SetValue(Class1.FooProperty, "first", BindingPriority.Style);
- Assert.Equal("first", obj1.GetValue(Class1.FooProperty));
- Assert.Equal("first", obj2.GetValue(Class1.FooProperty));
- obj2.SetValue(Class1.FooProperty, "second", BindingPriority.Style);
- Assert.Equal("second", obj1.GetValue(Class1.FooProperty));
- Assert.Equal("second", obj2.GetValue(Class1.FooProperty));
- obj1.SetValue(Class1.FooProperty, "third", BindingPriority.Style);
- Assert.Equal("third", obj1.GetValue(Class1.FooProperty));
- Assert.Equal("third", obj2.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void Local_Binding_Overwrites_Local_Value()
- {
- var target = new Class1();
- var binding = new Subject<string>();
- target.Bind(Class1.FooProperty, binding);
- binding.OnNext("first");
- Assert.Equal("first", target.GetValue(Class1.FooProperty));
- target.SetValue(Class1.FooProperty, "second");
- Assert.Equal("second", target.GetValue(Class1.FooProperty));
- binding.OnNext("third");
- Assert.Equal("third", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void StyleBinding_Overrides_Default_Value()
- {
- Class1 target = new Class1();
- target.Bind(Class1.FooProperty, Single("stylevalue"), BindingPriority.Style);
- Assert.Equal("stylevalue", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void this_Operator_Returns_Value_Property()
- {
- Class1 target = new Class1();
- target.SetValue(Class1.FooProperty, "newvalue");
- Assert.Equal("newvalue", target[Class1.FooProperty]);
- }
- [Fact]
- public void this_Operator_Sets_Value_Property()
- {
- Class1 target = new Class1();
- target[Class1.FooProperty] = "newvalue";
- Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void this_Operator_Doesnt_Accept_Observable()
- {
- Class1 target = new Class1();
- Assert.Throws<ArgumentException>(() =>
- {
- target[Class1.FooProperty] = Observable.Return("newvalue");
- });
- }
- [Fact]
- public void this_Operator_Binds_One_Way()
- {
- Class1 target1 = new Class1();
- Class2 target2 = new Class2();
- IndexerDescriptor binding = Class2.BarProperty.Bind().WithMode(BindingMode.OneWay);
- target1.SetValue(Class1.FooProperty, "first");
- target2[binding] = target1[!Class1.FooProperty];
- target1.SetValue(Class1.FooProperty, "second");
- Assert.Equal("second", target2.GetValue(Class2.BarProperty));
- }
- [Fact]
- public void this_Operator_Binds_Two_Way()
- {
- Class1 target1 = new Class1();
- Class1 target2 = new Class1();
- target1.SetValue(Class1.FooProperty, "first");
- target2[!Class1.FooProperty] = target1[!!Class1.FooProperty];
- Assert.Equal("first", target2.GetValue(Class1.FooProperty));
- target1.SetValue(Class1.FooProperty, "second");
- Assert.Equal("second", target2.GetValue(Class1.FooProperty));
- target2.SetValue(Class1.FooProperty, "third");
- Assert.Equal("third", target1.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void this_Operator_Binds_One_Time()
- {
- Class1 target1 = new Class1();
- Class1 target2 = new Class1();
- target1.SetValue(Class1.FooProperty, "first");
- target2[!Class1.FooProperty] = target1[Class1.FooProperty.Bind().WithMode(BindingMode.OneTime)];
- target1.SetValue(Class1.FooProperty, "second");
- Assert.Equal("first", target2.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void BindingError_Does_Not_Cause_Target_Update()
- {
- var target = new Class1();
- var source = new Subject<object>();
- target.Bind(Class1.QuxProperty, source);
- source.OnNext(6.7);
- source.OnNext(new BindingNotification(
- new InvalidOperationException("Foo"),
- BindingErrorType.Error));
- Assert.Equal(6.7, target.GetValue(Class1.QuxProperty));
- }
- [Fact]
- public void BindingNotification_With_FallbackValue_Causes_Target_Update()
- {
- var target = new Class1();
- var source = new Subject<object>();
- target.Bind(Class1.QuxProperty, source);
- source.OnNext(6.7);
- source.OnNext(new BindingNotification(
- new InvalidOperationException("Foo"),
- BindingErrorType.Error,
- 8.9));
- Assert.Equal(8.9, target.GetValue(Class1.QuxProperty));
- }
- [Fact]
- public void Bind_Logs_Binding_Error()
- {
- var target = new Class1();
- var source = new Subject<object>();
- var called = false;
- var expectedMessageTemplate = "Error in binding to {Target}.{Property}: {Message}";
- LogCallback checkLogMessage = (level, area, src, mt, pv) =>
- {
- if (level == LogEventLevel.Error &&
- area == LogArea.Binding &&
- mt == expectedMessageTemplate)
- {
- called = true;
- }
- };
- using (TestLogSink.Start(checkLogMessage))
- {
- target.Bind(Class1.QuxProperty, source);
- source.OnNext(6.7);
- source.OnNext(new BindingNotification(
- new InvalidOperationException("Foo"),
- BindingErrorType.Error));
- Assert.Equal(6.7, target.GetValue(Class1.QuxProperty));
- Assert.True(called);
- }
- }
- [Fact]
- public async void Bind_With_Scheduler_Executes_On_Scheduler()
- {
- var target = new Class1();
- var source = new Subject<object>();
- var currentThreadId = Thread.CurrentThread.ManagedThreadId;
- var threadingInterfaceMock = new Mock<IPlatformThreadingInterface>();
- threadingInterfaceMock.SetupGet(mock => mock.CurrentThreadIsLoopThread)
- .Returns(() => Thread.CurrentThread.ManagedThreadId == currentThreadId);
- using (AvaloniaLocator.EnterScope())
- {
- AvaloniaLocator.CurrentMutable.Bind<IPlatformThreadingInterface>().ToConstant(threadingInterfaceMock.Object);
- AvaloniaLocator.CurrentMutable.Bind<IScheduler>().ToConstant(AvaloniaScheduler.Instance);
- target.Bind(Class1.QuxProperty, source);
- await Task.Run(() => source.OnNext(6.7));
- }
- }
- [Theory]
- [InlineData(true)]
- [InlineData(false)]
- public void SetValue_Should_Not_Cause_StackOverflow_And_Have_Correct_Values(bool useXamlBinding)
- {
- var viewModel = new TestStackOverflowViewModel()
- {
- Value = 50
- };
- var target = new Class1();
- //note: if the initialization of the child binding is here target/child binding work fine!!!
- //var child = new Class1()
- //{
- // [~~Class1.DoubleValueProperty] = target[~~Class1.DoubleValueProperty]
- //};
- target.Bind(Class1.DoubleValueProperty,
- new Binding("Value") { Mode = BindingMode.TwoWay, Source = viewModel });
- var child = new Class1();
- if (useXamlBinding)
- {
- child.Bind(Class1.DoubleValueProperty,
- new Binding("DoubleValue")
- {
- Mode = BindingMode.TwoWay,
- Source = target
- });
- }
- else
- {
- child[!!Class1.DoubleValueProperty] = target[!!Class1.DoubleValueProperty];
- }
- Assert.Equal(1, viewModel.SetterInvokedCount);
- //here in real life stack overflow exception is thrown issue #855 and #824
- target.DoubleValue = 51.001;
- Assert.Equal(2, viewModel.SetterInvokedCount);
- double expected = 51;
- Assert.Equal(expected, viewModel.Value);
- Assert.Equal(expected, target.DoubleValue);
- Assert.Equal(expected, child.DoubleValue);
- }
- /// <summary>
- /// Returns an observable that returns a single value but does not complete.
- /// </summary>
- /// <typeparam name="T">The type of the observable.</typeparam>
- /// <param name="value">The value.</param>
- /// <returns>The observable.</returns>
- private IObservable<T> Single<T>(T value)
- {
- return Observable.Never<T>().StartWith(value);
- }
- private class Class1 : AvaloniaObject
- {
- public static readonly StyledProperty<string> FooProperty =
- AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
- public static readonly StyledProperty<double> QuxProperty =
- AvaloniaProperty.Register<Class1, double>("Qux", 5.6);
- public static readonly StyledProperty<double> DoubleValueProperty =
- AvaloniaProperty.Register<Class1, double>(nameof(DoubleValue));
- public double DoubleValue
- {
- get { return GetValue(DoubleValueProperty); }
- set { SetValue(DoubleValueProperty, value); }
- }
- }
- private class Class2 : Class1
- {
- public static readonly StyledProperty<string> BarProperty =
- AvaloniaProperty.Register<Class2, string>("Bar", "bardefault");
- }
- private class TestOneTimeBinding : IBinding
- {
- private IObservable<object> _source;
- public TestOneTimeBinding(IObservable<object> source)
- {
- _source = source;
- }
- public InstancedBinding Initiate(
- IAvaloniaObject target,
- AvaloniaProperty targetProperty,
- object anchor = null,
- bool enableDataValidation = false)
- {
- return new InstancedBinding(_source, BindingMode.OneTime);
- }
- }
- private class TestStackOverflowViewModel : INotifyPropertyChanged
- {
- public int SetterInvokedCount { get; private set; }
- public const int MaxInvokedCount = 1000;
- private double _value;
- public event PropertyChangedEventHandler PropertyChanged;
- public double Value
- {
- get { return _value; }
- set
- {
- if (_value != value)
- {
- SetterInvokedCount++;
- if (SetterInvokedCount < MaxInvokedCount)
- {
- _value = (int)value;
- if (_value > 75) _value = 75;
- if (_value < 25) _value = 25;
- }
- else
- {
- _value = value;
- }
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
- }
- }
- }
- }
- }
- }
|