csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.5 KiB
87 lines
2.5 KiB
using System;
|
|
using Xunit;
|
|
|
|
namespace Avalonia.Base.UnitTests
|
|
{
|
|
public class DirectPropertyTests
|
|
{
|
|
[Fact]
|
|
public void IsDirect_Property_Returns_True()
|
|
{
|
|
var target = new DirectProperty<Class1, string?>(
|
|
"test",
|
|
o => null,
|
|
null,
|
|
new DirectPropertyMetadata<string?>());
|
|
|
|
Assert.True(target.IsDirect);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddOwnered_Property_Should_Equal_Original()
|
|
{
|
|
var p1 = Class1.FooProperty;
|
|
var p2 = p1.AddOwner<Class2>(o => null, (o, v) => { });
|
|
|
|
Assert.NotSame(p1, p2);
|
|
Assert.True(p1.Equals(p2));
|
|
Assert.Equal(p1.GetHashCode(), p2.GetHashCode());
|
|
Assert.True(p1 == p2);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddOwnered_Property_Should_Have_OwnerType_Set()
|
|
{
|
|
var p1 = Class1.FooProperty;
|
|
var p2 = p1.AddOwner<Class2>(o => null, (o, v) => { });
|
|
|
|
Assert.Equal(typeof(Class2), p2.OwnerType);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddOwnered_Properties_Should_Share_Observables()
|
|
{
|
|
var p1 = Class1.FooProperty;
|
|
var p2 = p1.AddOwner<Class2>(o => null, (o, v) => { });
|
|
|
|
Assert.Same(p1.Changed, p2.Changed);
|
|
}
|
|
|
|
[Fact]
|
|
public void Default_GetMetadata_Cannot_Be_Changed()
|
|
{
|
|
var p1 = Class1.FooProperty;
|
|
var metadata = p1.GetMetadata<Class1>();
|
|
|
|
Assert.Throws<InvalidOperationException>(() => metadata.Merge(new DirectPropertyMetadata<string>(), p1));
|
|
}
|
|
|
|
[Fact]
|
|
public void AddOwnered_GetMetadata_Cannot_Be_Changed()
|
|
{
|
|
var p1 = Class1.FooProperty;
|
|
var p2 = p1.AddOwner<Class2>(_ => null, (_, _) => { });
|
|
var metadata = p2.GetMetadata<Class2>();
|
|
|
|
Assert.Throws<InvalidOperationException>(() => metadata.Merge(new DirectPropertyMetadata<string>(), p2));
|
|
}
|
|
|
|
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);
|
|
|
|
private string? _foo = "foo";
|
|
|
|
public string? Foo
|
|
{
|
|
get { return _foo; }
|
|
set { SetAndRaise(FooProperty, ref _foo, value); }
|
|
}
|
|
}
|
|
|
|
private class Class2 : AvaloniaObject
|
|
{
|
|
}
|
|
}
|
|
}
|
|
|