diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/ResourceIncludeTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/ResourceIncludeTests.cs index a6b930962e..9a008503f5 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/ResourceIncludeTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/ResourceIncludeTests.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using Avalonia.Controls; using Avalonia.Media; +using Avalonia.Metadata; +using Avalonia.Styling; using Avalonia.UnitTests; using Xunit; @@ -9,87 +11,146 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions { public class ResourceIncludeTests : XamlTestBase { - public class StaticResourceExtensionTests : XamlTestBase + [Fact] + public void ResourceInclude_Loads_ResourceDictionary() { - [Fact] - public void ResourceInclude_Loads_ResourceDictionary() + var documents = new[] { - var documents = new[] - { - new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Resource.xaml"), @" + new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Resource.xaml"), @" - #ff506070 + xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> +#ff506070 "), - new RuntimeXamlLoaderDocument(@" + new RuntimeXamlLoaderDocument(@" - - - - - - - - - + xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> + + + + + + + + + ") - }; + }; - using (StartWithResources()) - { - var compiled = AvaloniaRuntimeXamlLoader.LoadGroup(documents); - var userControl = Assert.IsType(compiled[1]); - var border = userControl.FindControl("border"); + using (StartWithResources()) + { + var compiled = AvaloniaRuntimeXamlLoader.LoadGroup(documents); + var userControl = Assert.IsType(compiled[1]); + var border = userControl.FindControl("border"); - var brush = (ISolidColorBrush)border.Background; - Assert.Equal(0xff506070, brush.Color.ToUInt32()); - } + var brush = (ISolidColorBrush)border.Background; + Assert.Equal(0xff506070, brush.Color.ToUInt32()); } + } - [Fact] - public void Missing_ResourceKey_In_ResourceInclude_Does_Not_Cause_StackOverflow() + [Fact] + public void Missing_ResourceKey_In_ResourceInclude_Does_Not_Cause_StackOverflow() + { + var app = Application.Current; + var documents = new[] { - var app = Application.Current; - var documents = new[] - { - new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Resource.xaml"), @" + new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Resource.xaml"), @" - + xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> + "), - new RuntimeXamlLoaderDocument(app, @" + new RuntimeXamlLoaderDocument(app, @" - - - - - - - + xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> + + + + + + + ") - }; + }; - using (StartWithResources()) + using (StartWithResources()) + { + try { - try - { - AvaloniaRuntimeXamlLoader.LoadGroup(documents); - } - catch (KeyNotFoundException) - { - - } + AvaloniaRuntimeXamlLoader.LoadGroup(documents); } + 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"), @" + + OK +"), + new RuntimeXamlLoaderDocument(app, @" + + + + + + +") + }; + + using (StartWithResources()) + { + var groups = AvaloniaRuntimeXamlLoader.LoadGroup(documents); + var res = Assert.IsType(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 _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); - var services = new TestServices(assetLoader: assetLoader); - return UnitTestApplication.Start(services); + return res.TryGetResource(key, theme, out value); } + 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); } }