Browse Source

Add test for #11172

pull/11265/head
Max Katz 3 years ago
parent
commit
3f5a5e6bd8
  1. 179
      tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/ResourceIncludeTests.cs

179
tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/ResourceIncludeTests.cs

@ -2,6 +2,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Metadata;
using Avalonia.Styling;
using Avalonia.UnitTests; using Avalonia.UnitTests;
using Xunit; using Xunit;
@ -9,87 +11,146 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions
{ {
public class ResourceIncludeTests : XamlTestBase public class ResourceIncludeTests : XamlTestBase
{ {
public class StaticResourceExtensionTests : XamlTestBase [Fact]
public void ResourceInclude_Loads_ResourceDictionary()
{ {
[Fact] var documents = new[]
public void ResourceInclude_Loads_ResourceDictionary()
{ {
var documents = new[] new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Resource.xaml"), @"
{
new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Resource.xaml"), @"
<ResourceDictionary xmlns='https://github.com/avaloniaui' <ResourceDictionary xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush> <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
</ResourceDictionary>"), </ResourceDictionary>"),
new RuntimeXamlLoaderDocument(@" new RuntimeXamlLoaderDocument(@"
<UserControl xmlns='https://github.com/avaloniaui' <UserControl xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<UserControl.Resources> <UserControl.Resources>
<ResourceDictionary> <ResourceDictionary>
<ResourceDictionary.MergedDictionaries> <ResourceDictionary.MergedDictionaries>
<ResourceInclude Source='avares://Tests/Resource.xaml'/> <ResourceInclude Source='avares://Tests/Resource.xaml'/>
</ResourceDictionary.MergedDictionaries> </ResourceDictionary.MergedDictionaries>
</ResourceDictionary> </ResourceDictionary>
</UserControl.Resources> </UserControl.Resources>
<Border Name='border' Background='{StaticResource brush}'/> <Border Name='border' Background='{StaticResource brush}'/>
</UserControl>") </UserControl>")
}; };
using (StartWithResources()) using (StartWithResources())
{ {
var compiled = AvaloniaRuntimeXamlLoader.LoadGroup(documents); var compiled = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
var userControl = Assert.IsType<UserControl>(compiled[1]); var userControl = Assert.IsType<UserControl>(compiled[1]);
var border = userControl.FindControl<Border>("border"); var border = userControl.FindControl<Border>("border");
var brush = (ISolidColorBrush)border.Background; var brush = (ISolidColorBrush)border.Background;
Assert.Equal(0xff506070, brush.Color.ToUInt32()); Assert.Equal(0xff506070, brush.Color.ToUInt32());
}
} }
}
[Fact] [Fact]
public void Missing_ResourceKey_In_ResourceInclude_Does_Not_Cause_StackOverflow() public void Missing_ResourceKey_In_ResourceInclude_Does_Not_Cause_StackOverflow()
{
var app = Application.Current;
var documents = new[]
{ {
var app = Application.Current; new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Resource.xaml"), @"
var documents = new[]
{
new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Resource.xaml"), @"
<ResourceDictionary xmlns='https://github.com/avaloniaui' <ResourceDictionary xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<StaticResource x:Key='brush' ResourceKey='missing' /> <StaticResource x:Key='brush' ResourceKey='missing' />
</ResourceDictionary>"), </ResourceDictionary>"),
new RuntimeXamlLoaderDocument(app, @" new RuntimeXamlLoaderDocument(app, @"
<Application xmlns='https://github.com/avaloniaui' <Application xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Application.Resources> <Application.Resources>
<ResourceDictionary> <ResourceDictionary>
<ResourceDictionary.MergedDictionaries> <ResourceDictionary.MergedDictionaries>
<ResourceInclude Source='avares://Tests/Resource.xaml'/> <ResourceInclude Source='avares://Tests/Resource.xaml'/>
</ResourceDictionary.MergedDictionaries> </ResourceDictionary.MergedDictionaries>
</ResourceDictionary> </ResourceDictionary>
</Application.Resources> </Application.Resources>
</Application>") </Application>")
}; };
using (StartWithResources()) using (StartWithResources())
{
try
{ {
try AvaloniaRuntimeXamlLoader.LoadGroup(documents);
{
AvaloniaRuntimeXamlLoader.LoadGroup(documents);
}
catch (KeyNotFoundException)
{
}
} }
catch (KeyNotFoundException)
{
}
}
}
[Fact]
public void ResourceInclude_Should_Be_Allowed_To_Have_Key_In_Custom_Container()
{
var app = Application.Current;
var documents = new[]
{
new RuntimeXamlLoaderDocument(new Uri("avares://Demo/en-us.axaml"), @"
<ResourceDictionary xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<x:String x:Key='OkButton'>OK</x:String>
</ResourceDictionary>"),
new RuntimeXamlLoaderDocument(app, @"
<ResourceDictionary xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'>
<ResourceDictionary.MergedDictionaries>
<local:LocaleCollection>
<ResourceInclude Source='avares://Demo/en-us.axaml' x:Key='English' />
</local:LocaleCollection>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>")
};
using (StartWithResources())
{
var groups = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
var res = Assert.IsType<ResourceDictionary>(groups[1]);
Assert.True(res.TryGetResource("OkButton", null, out var val));
Assert.Equal("OK", val);
} }
}
private IDisposable StartWithResources(params (string, string)[] assets) private IDisposable StartWithResources(params (string, string)[] assets)
{
var assetLoader = new MockAssetLoader(assets);
var services = new TestServices(assetLoader: assetLoader);
return UnitTestApplication.Start(services);
}
}
// See https://github.com/AvaloniaUI/Avalonia/issues/11172
public class LocaleCollection : IResourceProvider
{
private readonly Dictionary<object, IResourceProvider> _langs = new();
public IResourceHost Owner { get; private set; }
public bool HasResources => true;
public event EventHandler OwnerChanged;
public void AddOwner(IResourceHost owner) => Owner = owner;
public void RemoveOwner(IResourceHost owner) => Owner = null;
public bool TryGetResource(object key, ThemeVariant theme, out object? value)
{
if (_langs.TryGetValue("English", out var res))
{ {
var assetLoader = new MockAssetLoader(assets); return res.TryGetResource(key, theme, out value);
var services = new TestServices(assetLoader: assetLoader);
return UnitTestApplication.Start(services);
} }
value = null;
return false;
} }
// Allow Avalonia to use this class as a collection, requires x:Key on the IResourceProvider
public void Add(object k, IResourceProvider v) => _langs.Add(k, v);
} }
} }

Loading…
Cancel
Save