Browse Source

fix AvaloniaPropertyConverter Tests, added attached property binding test, fixed some tests, simplified others

pull/916/head
donandren 9 years ago
committed by Andrey Kunchev
parent
commit
8b468c174c
  1. 52
      tests/Avalonia.Markup.Xaml.UnitTests/Converters/AvaloniaPropertyConverterTest.cs
  2. 47
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs
  3. 31
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests.cs
  4. 4
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StyleTests.cs
  5. 3
      tests/Avalonia.UnitTests/TestServices.cs

52
tests/Avalonia.Markup.Xaml.UnitTests/Converters/AvaloniaPropertyConverterTest.cs

@ -2,18 +2,26 @@
// Licensed under the MIT license. See licence.md file in the project root for full license information. // Licensed under the MIT license. See licence.md file in the project root for full license information.
using System; using System;
using System.Reactive;
using Moq; using Moq;
using OmniXaml;
using OmniXaml.ObjectAssembler.Commands;
using OmniXaml.TypeConversion;
using OmniXaml.Typing;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Markup.Xaml.Converters; using Avalonia.Markup.Xaml.Converters;
using Avalonia.Styling; using Avalonia.Styling;
using Xunit; using Xunit;
#if !OMNIXAML
using Portable.Xaml.ComponentModel; using Portable.Xaml.ComponentModel;
using Portable.Xaml;
using Portable.Xaml.Markup;
#else
using OmniXaml;
using OmniXaml.ObjectAssembler.Commands;
using OmniXaml.TypeConversion;
using OmniXaml.Typing;
#endif
namespace Avalonia.Markup.Xaml.UnitTests.Converters namespace Avalonia.Markup.Xaml.UnitTests.Converters
{ {
@ -56,12 +64,38 @@ namespace Avalonia.Markup.Xaml.UnitTests.Converters
Assert.Equal(AttachedOwner.AttachedProperty, result); Assert.Equal(AttachedOwner.AttachedProperty, result);
} }
#if !OMNIXAML #if !OMNIXAML
private ITypeDescriptorContext CreateContext(Style style = null) private ITypeDescriptorContext CreateContext(Style style = null)
{ {
return null; var tdMock = new Mock<ITypeDescriptorContext>();
var xsc = new Mock<IXamlSchemaContextProvider>();
var sc = Mock.Of<XamlSchemaContext>();
var amb = new Mock<IAmbientProvider>();
var tr = new Mock<IXamlTypeResolver>();
tdMock.Setup(d => d.GetService(typeof(IAmbientProvider)))
.Returns(amb.Object);
tdMock.Setup(d => d.GetService(typeof(IXamlSchemaContextProvider)))
.Returns(xsc.Object);
tdMock.Setup(d => d.GetService(typeof(IXamlTypeResolver)))
.Returns(tr.Object);
xsc.SetupGet(v => v.SchemaContext)
.Returns(sc);
amb.Setup(v => v.GetFirstAmbientValue(It.IsAny<Portable.Xaml.XamlType>()))
.Returns(style);
amb.Setup(v => v.GetAllAmbientValues(It.IsAny<Portable.Xaml.XamlType>()))
.Returns(new object[] { style });
tr.Setup(v => v.Resolve(nameof(Class1)))
.Returns(typeof(Class1));
tr.Setup(v => v.Resolve(nameof(AttachedOwner)))
.Returns(typeof(AttachedOwner));
return tdMock.Object;
} }
#else #else
private IValueContext CreateContext(Style style = null) private IValueContext CreateContext(Style style = null)
{ {
var context = new Mock<IValueContext>(); var context = new Mock<IValueContext>();
@ -77,7 +111,9 @@ namespace Avalonia.Markup.Xaml.UnitTests.Converters
typeRepository.Setup(x => x.GetByQualifiedName("AttachedOwner")).Returns(attachedOwnerXamlType); typeRepository.Setup(x => x.GetByQualifiedName("AttachedOwner")).Returns(attachedOwnerXamlType);
return context.Object; return context.Object;
} }
#endif #endif
private class Class1 : AvaloniaObject, IStyleable private class Class1 : AvaloniaObject, IStyleable
{ {
public static readonly StyledProperty<string> FooProperty = public static readonly StyledProperty<string> FooProperty =
@ -112,4 +148,4 @@ namespace Avalonia.Markup.Xaml.UnitTests.Converters
AvaloniaProperty.RegisterAttached<AttachedOwner, Class1, string>("Attached"); AvaloniaProperty.RegisterAttached<AttachedOwner, Class1, string>("Attached");
} }
} }
} }

47
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs

@ -50,6 +50,22 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
Assert.Equal(21.0, TextBlock.GetFontSize(target)); Assert.Equal(21.0, TextBlock.GetFontSize(target));
} }
[Fact]
public void Attached_Property_Supports_Binding()
{
using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
{
var xaml =
@"<Window xmlns='https://github.com/avaloniaui' TextBlock.FontSize='{Binding}'/>";
var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
target.DataContext = 21.0;
Assert.Equal(21.0, TextBlock.GetFontSize(target));
}
}
[Fact] [Fact]
public void Attached_Property_In_Panel_Is_Set() public void Attached_Property_In_Panel_Is_Set()
{ {
@ -396,8 +412,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
[Fact] [Fact]
public void Xaml_Binding_Is_Delayed() public void Xaml_Binding_Is_Delayed()
{ {
using (UnitTestApplication.Start(TestServices.MockPlatformWrapper using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
.With(windowingPlatform: new MockWindowingPlatform())))
{ {
var xaml = var xaml =
@"<ContentControl xmlns='https://github.com/avaloniaui' Content='{Binding}'/>"; @"<ContentControl xmlns='https://github.com/avaloniaui' Content='{Binding}'/>";
@ -484,11 +499,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
Assert.Equal("Bar", bindings[1].Path); Assert.Equal("Bar", bindings[1].Path);
} }
#if OMNIXAML
[Fact(Skip ="OmniXaml doesn't support set property with base class prefix ...")]
#else
[Fact] [Fact]
#endif
public void Control_Template_Is_Operational() public void Control_Template_Is_Operational()
{ {
using (UnitTestApplication.Start(TestServices.MockPlatformWrapper using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
@ -497,12 +508,12 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
var xaml = @" var xaml = @"
<Window xmlns='https://github.com/avaloniaui' <Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<ContentControl.Template> <Window.Template>
<ControlTemplate> <ControlTemplate>
<ContentPresenter Name='PART_ContentPresenter' <ContentPresenter Name='PART_ContentPresenter'
Content='{TemplateBinding Content}'/> Content='{TemplateBinding Content}'/>
</ControlTemplate> </ControlTemplate>
</ContentControl.Template> </Window.Template>
</Window>"; </Window>";
var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml); var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
@ -595,5 +606,25 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
Assert.True(attached < widthChanged); Assert.True(attached < widthChanged);
} }
} }
[Fact]
public void Control_Is_Added_To_Parent_Before_Properties_Are_Set_Simple()
{
var xaml = @"
<ContentControl xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
<local:InitializationOrderTracker Width='100'/>
</ContentControl>";
var window = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
var tracker = (InitializationOrderTracker)window.Content;
var parentSet = tracker.Order.IndexOf("Property Parent Changed");
var widthChanged = tracker.Order.IndexOf("Property Width Changed");
Assert.Equal(0, parentSet);
Assert.Equal(1, widthChanged);
}
} }
} }

31
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests.cs

@ -83,7 +83,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
[Fact] [Fact]
public void Can_Bind_To_DataContext_Of_Anchor_On_Non_Control() public void Can_Bind_To_DataContext_Of_Anchor_On_Non_Control()
{ {
using (UnitTestApplication.Start(TestServices.StyledWindow)) using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
{ {
var xaml = @" var xaml = @"
<Window xmlns='https://github.com/avaloniaui' <Window xmlns='https://github.com/avaloniaui'
@ -146,30 +146,33 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
} }
} }
#if OMNIXAML
[Fact(Skip = "OmniXaml doesn't support nested markup extensions. #119")] [Fact(Skip = "OmniXaml doesn't support nested markup extensions. #119")]
#else
[Fact]
#endif
public void Binding_To_Self_Works() public void Binding_To_Self_Works()
{ {
using (UnitTestApplication.Start(TestServices.StyledWindow)) var xaml = @"
{ <ContentControl xmlns='https://github.com/avaloniaui'
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<TextBlock Name='textblock' Text='{Binding Tag, RelativeSource={RelativeSource Self}}'/> <TextBlock Name='textblock' Text='{Binding Tag, RelativeSource={RelativeSource Self}}'/>
</Window>"; </ContentControl>";
var loader = new AvaloniaXamlLoader();
var window = (Window)loader.Load(xaml);
var textBlock = (TextBlock)window.Content;
window.ApplyTemplate(); var window = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
var textBlock = (TextBlock)window.Content;
Assert.Equal("foo", textBlock.Text); textBlock.Tag = "foo";
}
Assert.Equal("foo", textBlock.Text);
} }
[Fact] [Fact]
public void Longform_Binding_To_Self_Works() public void Longform_Binding_To_Self_Works()
{ {
using (UnitTestApplication.Start(TestServices.StyledWindow)) using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
{ {
var xaml = @" var xaml = @"
<Window xmlns='https://github.com/avaloniaui' <Window xmlns='https://github.com/avaloniaui'
@ -190,4 +193,4 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
} }
} }
} }
} }

4
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StyleTests.cs

@ -90,7 +90,9 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
[Fact] [Fact]
public void StyleResource_Can_Be_Assigned_To_Setter() public void StyleResource_Can_Be_Assigned_To_Setter()
{ {
using (UnitTestApplication.Start(TestServices.StyledWindow)) //skip default theme and styles, they are not needed
using (UnitTestApplication.Start(TestServices.StyledWindow
.With(theme: () => new Styles())))
{ {
var xaml = @" var xaml = @"
<Window xmlns='https://github.com/avaloniaui' <Window xmlns='https://github.com/avaloniaui'

3
tests/Avalonia.UnitTests/TestServices.cs

@ -36,6 +36,9 @@ namespace Avalonia.UnitTests
public static readonly TestServices MockPlatformWrapper = new TestServices( public static readonly TestServices MockPlatformWrapper = new TestServices(
platform: Mock.Of<IRuntimePlatform>()); platform: Mock.Of<IRuntimePlatform>());
public static readonly TestServices MockWindowingPlatform = new TestServices(
windowingPlatform: new MockWindowingPlatform());
public static readonly TestServices MockStyler = new TestServices( public static readonly TestServices MockStyler = new TestServices(
styler: Mock.Of<IStyler>()); styler: Mock.Of<IStyler>());

Loading…
Cancel
Save