Skip to content

Commit

Permalink
Added passing tests for #2725.
Browse files Browse the repository at this point in the history
Including a repro (`EnumToEnumerable`) from a customer. Looks like the problem can't be reproduced in unit tests.
  • Loading branch information
grokys committed Jan 13, 2022
1 parent f29ed1a commit bad8e8b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
21 changes: 21 additions & 0 deletions tests/Avalonia.Markup.Xaml.UnitTests/EnumToEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Avalonia.Markup.Xaml.UnitTests
{
public class EnumToEnumerable : MarkupExtension
{
private readonly Type _enumType;

public EnumToEnumerable(Type type)
{
if (!type.IsEnum)
throw new ArgumentException(nameof(type));
_enumType = type;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return Enum.GetValues(_enumType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections;
using Avalonia.Controls;
using Avalonia.UnitTests;
using Xunit;

namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions
{
public class MarkupExtensionTests : XamlTestBase
{
[Fact]
public void Markup_Extension_Can_Accept_Nested_Type()
{
using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests;assembly=Avalonia.Markup.Xaml.UnitTests'>
<ListBox Items='{local:EnumToEnumerable {x:Type local:TestViewModel+NestedEnum}}'/>
</Window>";

var window = AvaloniaRuntimeXamlLoader.Parse<Window>(xaml);
var listBox = (ListBox)window.Content;
var items = (IList)listBox.Items;

Assert.Equal(2, items.Count);
Assert.Equal(TestViewModel.NestedEnum.Item1, items[0]);
Assert.Equal(TestViewModel.NestedEnum.Item2, items[1]);
}
}
}
}
13 changes: 12 additions & 1 deletion tests/Avalonia.Markup.Xaml.UnitTests/TestViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,16 @@ public TestViewModel Child
RaisePropertyChanged();
}
}

public class NestedType
{
public string String { get; set; }
}

public enum NestedEnum
{
Item1,
Item2,
}
}
}
}
43 changes: 43 additions & 0 deletions tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Markup.Xaml.Templates;
using Avalonia.UnitTests;
using Xunit;

Expand Down Expand Up @@ -132,5 +133,47 @@ public void Can_Set_DataContext_In_DataTemplate()
Assert.Same(viewModel.Child.Child, canvas.DataContext);
}
}

[Fact]
public void DataType_Can_Be_Nested_Type()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:sys='clr-namespace:System;assembly=netstandard'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests;assembly=Avalonia.Markup.Xaml.UnitTests'>
<Window.DataTemplates>
<DataTemplate DataType='{x:Type local:TestViewModel+NestedType}'>
<TextBlock Text='{Binding String}' Tag='NestedType'/>
</DataTemplate>
</Window.DataTemplates>
<ContentControl Name='target' Content='{Binding}'/>
</Window>";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
var dataTemplate = (DataTemplate)window.DataTemplates[0];
var target = window.FindControl<ContentControl>("target");

window.DataContext = new TestViewModel.NestedType
{
String = "Nested"
};

window.ApplyTemplate();
target.ApplyTemplate();
((ContentPresenter)target.Presenter).UpdateChild();

Assert.Equal(typeof(TestViewModel.NestedType), dataTemplate.DataType);

var child = Assert.IsType<TextBlock>(target.Presenter.Child);
Assert.Equal("Nested", child.Text);
Assert.Equal("NestedType", child.Tag);
}
}

public class NestedType
{
}
}
}

0 comments on commit bad8e8b

Please sign in to comment.