123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Reactive.Subjects;
- using System.Threading;
- using System.Threading.Tasks;
- using Avalonia.Data;
- using Avalonia.Logging;
- using Avalonia.Platform;
- using Avalonia.Threading;
- using Avalonia.UnitTests;
- using Moq;
- using Nito.AsyncEx;
- using Xunit;
- namespace Avalonia.Base.UnitTests
- {
- public class AvaloniaObjectTests_Direct
- {
- [Fact]
- public void GetValue_Gets_Default_Value()
- {
- var target = new Class1();
- Assert.Equal("initial", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void GetValue_Gets_Value_NonGeneric()
- {
- var target = new Class1();
- Assert.Equal("initial", target.GetValue((AvaloniaProperty)Class1.FooProperty));
- }
- [Fact]
- public void GetValue_On_Unregistered_Property_Throws_Exception()
- {
- var target = new Class2();
- Assert.Throws<ArgumentException>(() => target.GetValue(Class1.BarProperty));
- }
- [Fact]
- public void SetValue_Sets_Value()
- {
- var target = new Class1();
- target.SetValue(Class1.FooProperty, "newvalue");
- Assert.Equal("newvalue", target.Foo);
- }
- [Fact]
- public void SetValue_Sets_Value_NonGeneric()
- {
- var target = new Class1();
- target.SetValue((AvaloniaProperty)Class1.FooProperty, "newvalue");
- Assert.Equal("newvalue", target.Foo);
- }
- [Fact]
- public void SetValue_NonGeneric_Coerces_UnsetValue_To_Default_Value()
- {
- var target = new Class1();
- target.SetValue((AvaloniaProperty)Class1.BazProperty, AvaloniaProperty.UnsetValue);
- Assert.Equal(-1, target.Baz);
- }
- [Fact]
- public void SetValue_Raises_PropertyChanged()
- {
- var target = new Class1();
- bool raised = false;
- target.PropertyChanged += (s, e) =>
- raised = e.Property == Class1.FooProperty &&
- (string)e.OldValue == "initial" &&
- (string)e.NewValue == "newvalue" &&
- e.Priority == BindingPriority.LocalValue;
- target.SetValue(Class1.FooProperty, "newvalue");
- Assert.True(raised);
- }
- [Fact]
- public void SetValue_Raises_Changed()
- {
- var target = new Class1();
- bool raised = false;
- Class1.FooProperty.Changed.Subscribe(e =>
- raised = e.Property == Class1.FooProperty &&
- e.OldValue.GetValueOrDefault() == "initial" &&
- e.NewValue.GetValueOrDefault() == "newvalue" &&
- e.Priority == BindingPriority.LocalValue);
- target.SetValue(Class1.FooProperty, "newvalue");
- Assert.True(raised);
- }
- [Fact]
- public void Setting_Object_Property_To_UnsetValue_Reverts_To_Default_Value()
- {
- Class1 target = new Class1();
- target.SetValue(Class1.FrankProperty, "newvalue");
- target.SetValue(Class1.FrankProperty, AvaloniaProperty.UnsetValue);
- Assert.Equal("Kups", target.GetValue(Class1.FrankProperty));
- }
- [Fact]
- public void Setting_Object_Property_To_DoNothing_Does_Nothing()
- {
- Class1 target = new Class1();
- target.SetValue(Class1.FrankProperty, "newvalue");
- target.SetValue(Class1.FrankProperty, BindingOperations.DoNothing);
- Assert.Equal("newvalue", target.GetValue(Class1.FrankProperty));
- }
- [Fact]
- public void Bind_Raises_PropertyChanged()
- {
- var target = new Class1();
- var source = new Subject<BindingValue<string>>();
- bool raised = false;
- target.PropertyChanged += (s, e) =>
- raised = e.Property == Class1.FooProperty &&
- (string)e.OldValue == "initial" &&
- (string)e.NewValue == "newvalue" &&
- e.Priority == BindingPriority.LocalValue;
- target.Bind(Class1.FooProperty, source);
- source.OnNext("newvalue");
- Assert.True(raised);
- }
- [Fact]
- public void PropertyChanged_Not_Raised_When_Value_Unchanged()
- {
- var target = new Class1();
- var source = new Subject<BindingValue<string>>();
- var raised = 0;
- target.PropertyChanged += (s, e) => ++raised;
- target.Bind(Class1.FooProperty, source);
- source.OnNext("newvalue");
- source.OnNext("newvalue");
- Assert.Equal(1, raised);
- }
- [Fact]
- public void SetValue_On_Unregistered_Property_Throws_Exception()
- {
- var target = new Class2();
- Assert.Throws<ArgumentException>(() => target.SetValue(Class1.BarProperty, "value"));
- }
- [Fact]
- public void ClearValue_Restores_Default_value()
- {
- var target = new Class1();
- Assert.Equal("initial", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void ClearValue_Raises_PropertyChanged()
- {
- Class1 target = new Class1();
- var raised = 0;
- target.SetValue(Class1.FooProperty, "newvalue");
- target.PropertyChanged += (s, e) =>
- {
- Assert.Same(target, s);
- Assert.Equal(BindingPriority.LocalValue, e.Priority);
- Assert.Equal(Class1.FooProperty, e.Property);
- Assert.Equal("newvalue", (string)e.OldValue);
- Assert.Equal("unset", (string)e.NewValue);
- ++raised;
- };
- target.ClearValue(Class1.FooProperty);
- Assert.Equal(1, raised);
- }
- [Fact]
- public void GetObservable_Returns_Values()
- {
- var target = new Class1();
- List<string> values = new List<string>();
- target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
- target.Foo = "newvalue";
- Assert.Equal(new[] { "initial", "newvalue" }, values);
- }
- [Fact]
- public void Bind_Binds_Property_Value()
- {
- var target = new Class1();
- var source = new Subject<string>();
- var sub = target.Bind(Class1.FooProperty, source);
- Assert.Equal("initial", target.Foo);
- source.OnNext("first");
- Assert.Equal("first", target.Foo);
- source.OnNext("second");
- Assert.Equal("second", target.Foo);
- sub.Dispose();
- source.OnNext("third");
- Assert.Equal("second", target.Foo);
- }
- [Fact]
- public void Bind_Binds_Property_Value_NonGeneric()
- {
- var target = new Class1();
- var source = new Subject<string>();
- var sub = target.Bind((AvaloniaProperty)Class1.FooProperty, source);
- Assert.Equal("initial", target.Foo);
- source.OnNext("first");
- Assert.Equal("first", target.Foo);
- source.OnNext("second");
- Assert.Equal("second", target.Foo);
- sub.Dispose();
- source.OnNext("third");
- Assert.Equal("second", target.Foo);
- }
- [Fact]
- public void Bind_NonGeneric_Accepts_UnsetValue()
- {
- var target = new Class1();
- var source = new Subject<object>();
- var sub = target.Bind((AvaloniaProperty)Class1.BazProperty, source);
- Assert.Equal(5, target.Baz);
- source.OnNext(6);
- Assert.Equal(6, target.Baz);
- source.OnNext(AvaloniaProperty.UnsetValue);
- Assert.Equal(-1, target.Baz);
- }
- [Fact]
- public void Bind_Handles_Wrong_Type()
- {
- var target = new Class1();
- var source = new Subject<object>();
- var sub = target.Bind(Class1.FooProperty, source);
- source.OnNext(45);
- Assert.Equal("unset", target.Foo);
- }
- [Fact]
- public void Bind_Handles_Wrong_Value_Type()
- {
- var target = new Class1();
- var source = new Subject<object>();
- var sub = target.Bind(Class1.BazProperty, source);
- source.OnNext("foo");
- Assert.Equal(-1, target.Baz);
- }
- [Fact]
- public void ReadOnly_Property_Cannot_Be_Set()
- {
- var target = new Class1();
- Assert.Throws<ArgumentException>(() =>
- target.SetValue(Class1.BarProperty, "newvalue"));
- }
- [Fact]
- public void ReadOnly_Property_Cannot_Be_Set_NonGeneric()
- {
- var target = new Class1();
- Assert.Throws<ArgumentException>(() =>
- target.SetValue((AvaloniaProperty)Class1.BarProperty, "newvalue"));
- }
- [Fact]
- public void ReadOnly_Property_Cannot_Be_Bound()
- {
- var target = new Class1();
- var source = new Subject<string>();
- Assert.Throws<ArgumentException>(() =>
- target.Bind(Class1.BarProperty, source));
- }
- [Fact]
- public void ReadOnly_Property_Cannot_Be_Bound_NonGeneric()
- {
- var target = new Class1();
- var source = new Subject<string>();
- Assert.Throws<ArgumentException>(() =>
- target.Bind(Class1.BarProperty, source));
- }
- [Fact]
- public void GetValue_Gets_Value_On_AddOwnered_Property()
- {
- var target = new Class2();
- Assert.Equal("initial2", target.GetValue(Class2.FooProperty));
- }
- [Fact]
- public void GetValue_Gets_Value_On_AddOwnered_Property_Using_Original()
- {
- var target = new Class2();
- Assert.Equal("initial2", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void GetValue_Gets_Value_On_AddOwnered_Property_Using_Original_NonGeneric()
- {
- var target = new Class2();
- Assert.Equal("initial2", target.GetValue((AvaloniaProperty)Class1.FooProperty));
- }
- [Fact]
- public void SetValue_Sets_Value_On_AddOwnered_Property_Using_Original()
- {
- var target = new Class2();
- target.SetValue(Class1.FooProperty, "newvalue");
- Assert.Equal("newvalue", target.Foo);
- }
- [Fact]
- public void SetValue_Sets_Value_On_AddOwnered_Property_Using_Original_NonGeneric()
- {
- var target = new Class2();
- target.SetValue((AvaloniaProperty)Class1.FooProperty, "newvalue");
- Assert.Equal("newvalue", target.Foo);
- }
- [Fact]
- public void UnsetValue_Is_Used_On_AddOwnered_Property()
- {
- var target = new Class2();
- target.SetValue((AvaloniaProperty)Class1.FooProperty, AvaloniaProperty.UnsetValue);
- Assert.Equal("unset", target.Foo);
- }
- [Fact]
- public void Bind_Binds_AddOwnered_Property_Value()
- {
- var target = new Class2();
- var source = new Subject<string>();
- var sub = target.Bind(Class1.FooProperty, source);
- Assert.Equal("initial2", target.Foo);
- source.OnNext("first");
- Assert.Equal("first", target.Foo);
- source.OnNext("second");
- Assert.Equal("second", target.Foo);
- sub.Dispose();
- source.OnNext("third");
- Assert.Equal("second", target.Foo);
- }
- [Fact]
- public void Bind_Binds_AddOwnered_Property_Value_NonGeneric()
- {
- var target = new Class2();
- var source = new Subject<string>();
- var sub = target.Bind((AvaloniaProperty)Class1.FooProperty, source);
- Assert.Equal("initial2", target.Foo);
- source.OnNext("first");
- Assert.Equal("first", target.Foo);
- source.OnNext("second");
- Assert.Equal("second", target.Foo);
- sub.Dispose();
- source.OnNext("third");
- Assert.Equal("second", target.Foo);
- }
- [Fact]
- public void Binding_Error_Reverts_To_Default_Value()
- {
- var target = new Class1();
- var source = new Subject<BindingValue<string>>();
- target.Bind(Class1.FooProperty, source);
- source.OnNext("initial");
- source.OnNext(BindingValue<string>.BindingError(new InvalidOperationException("Foo")));
- Assert.Equal("unset", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void Binding_Error_With_FallbackValue_Causes_Target_Update()
- {
- var target = new Class1();
- var source = new Subject<BindingValue<string>>();
- target.Bind(Class1.FooProperty, source);
- source.OnNext("initial");
- source.OnNext(BindingValue<string>.BindingError(new InvalidOperationException("Foo"), "bar"));
- Assert.Equal("bar", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void DataValidationError_Does_Not_Cause_Target_Update()
- {
- var target = new Class1();
- var source = new Subject<BindingValue<string>>();
- target.Bind(Class1.FooProperty, source);
- source.OnNext("initial");
- source.OnNext(BindingValue<string>.DataValidationError(new InvalidOperationException("Foo")));
- Assert.Equal("initial", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void DataValidationError_With_FallbackValue_Causes_Target_Update()
- {
- var target = new Class1();
- var source = new Subject<BindingValue<string>>();
- target.Bind(Class1.FooProperty, source);
- source.OnNext("initial");
- source.OnNext(BindingValue<string>.DataValidationError(new InvalidOperationException("Foo"), "bar"));
- Assert.Equal("bar", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void BindingError_With_FallbackValue_Causes_Target_Update()
- {
- var target = new Class1();
- var source = new Subject<BindingValue<string>>();
- target.Bind(Class1.FooProperty, source);
- source.OnNext("initial");
- source.OnNext(BindingValue<string>.BindingError(new InvalidOperationException("Foo"), "fallback"));
- Assert.Equal("fallback", target.GetValue(Class1.FooProperty));
- }
- [Fact]
- public void Bind_Executes_On_UIThread()
- {
- AsyncContext.Run(async () =>
- {
- var target = new Class1();
- var source = new Subject<object>();
- var currentThreadId = Thread.CurrentThread.ManagedThreadId;
- var raised = 0;
- var dispatcherMock = new Mock<IDispatcherImpl>();
- dispatcherMock.SetupGet(mock => mock.CurrentThreadIsLoopThread)
- .Returns(() => Thread.CurrentThread.ManagedThreadId == currentThreadId);
- var services = new TestServices(
- dispatcherImpl: dispatcherMock.Object);
- target.PropertyChanged += (s, e) =>
- {
- Assert.Equal(currentThreadId, Thread.CurrentThread.ManagedThreadId);
- ++raised;
- };
- using (UnitTestApplication.Start(services))
- {
- target.Bind(Class1.FooProperty, source);
- await Task.Run(() => source.OnNext("foobar"));
- Dispatcher.UIThread.RunJobs();
- Assert.Equal("foobar", target.Foo);
- Assert.Equal(1, raised);
- }
- });
- }
- [Fact]
- public void AddOwner_Should_Inherit_DefaultBindingMode()
- {
- var foo = new DirectProperty<Class1, string>(
- "foo",
- o => "foo",
- null,
- new DirectPropertyMetadata<string>(defaultBindingMode: BindingMode.TwoWay));
- var bar = foo.AddOwner<Class2>(o => "bar");
- Assert.Equal(BindingMode.TwoWay, bar.GetMetadata<Class1>().DefaultBindingMode);
- Assert.Equal(BindingMode.TwoWay, bar.GetMetadata<Class2>().DefaultBindingMode);
- }
- [Fact]
- public void AddOwner_Can_Override_DefaultBindingMode()
- {
- var foo = new DirectProperty<Class1, string>(
- "foo",
- o => "foo",
- null,
- new DirectPropertyMetadata<string>(defaultBindingMode: BindingMode.TwoWay));
- var bar = foo.AddOwner<Class2>(o => "bar", defaultBindingMode: BindingMode.OneWayToSource);
- Assert.Equal(BindingMode.TwoWay, bar.GetMetadata<Class1>().DefaultBindingMode);
- Assert.Equal(BindingMode.OneWayToSource, bar.GetMetadata<Class2>().DefaultBindingMode);
- }
- [Fact]
- public void SetValue_Should_Not_Cause_StackOverflow_And_Have_Correct_Values()
- {
- var viewModel = new TestStackOverflowViewModel()
- {
- Value = 50
- };
- var target = new Class1();
- target.Bind(Class1.DoubleValueProperty, new Binding("Value")
- {
- Mode = BindingMode.TwoWay,
- Source = viewModel
- });
- var child = new Class1();
- child[!!Class1.DoubleValueProperty] = target[!!Class1.DoubleValueProperty];
- Assert.Equal(1, viewModel.SetterInvokedCount);
- // Issues #855 and #824 were causing a StackOverflowException at this point.
- 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);
- }
- private class Class1 : AvaloniaObject
- {
- public static readonly DirectProperty<Class1, string> FooProperty =
- AvaloniaProperty.RegisterDirect<Class1, string>(
- nameof(Foo),
- o => o.Foo,
- (o, v) => o.Foo = v,
- unsetValue: "unset");
- public static readonly DirectProperty<Class1, string> BarProperty =
- AvaloniaProperty.RegisterDirect<Class1, string>(nameof(Bar), o => o.Bar);
- public static readonly DirectProperty<Class1, int> BazProperty =
- AvaloniaProperty.RegisterDirect<Class1, int>(
- nameof(Baz),
- o => o.Baz,
- (o, v) => o.Baz = v,
- unsetValue: -1);
- public static readonly DirectProperty<Class1, double> DoubleValueProperty =
- AvaloniaProperty.RegisterDirect<Class1, double>(
- nameof(DoubleValue),
- o => o.DoubleValue,
- (o, v) => o.DoubleValue = v);
- public static readonly DirectProperty<Class1, object> FrankProperty =
- AvaloniaProperty.RegisterDirect<Class1, object>(
- nameof(Frank),
- o => o.Frank,
- (o, v) => o.Frank = v,
- unsetValue: "Kups");
- private string _foo = "initial";
- private readonly string _bar = "bar";
- private int _baz = 5;
- private double _doubleValue;
- private object _frank;
- public string Foo
- {
- get { return _foo; }
- set { SetAndRaise(FooProperty, ref _foo, value); }
- }
- public string Bar
- {
- get { return _bar; }
- }
- public int Baz
- {
- get { return _baz; }
- set { SetAndRaise(BazProperty, ref _baz, value); }
- }
- public double DoubleValue
- {
- get { return _doubleValue; }
- set { SetAndRaise(DoubleValueProperty, ref _doubleValue, value); }
- }
- public object Frank
- {
- get { return _frank; }
- set { SetAndRaise(FrankProperty, ref _frank, value); }
- }
- }
- private class Class2 : AvaloniaObject
- {
- public static readonly DirectProperty<Class2, string> FooProperty =
- Class1.FooProperty.AddOwner<Class2>(o => o.Foo, (o, v) => o.Foo = v);
- private string _foo = "initial2";
- static Class2()
- {
- }
- public string Foo
- {
- get { return _foo; }
- set { SetAndRaise(FooProperty, ref _foo, value); }
- }
- }
- 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)));
- }
- }
- }
- }
- }
- }
|