using System; using System.Collections.Generic; using Avalonia.Controls; using Avalonia.Media; using Avalonia.UnitTests; using Xunit; namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions { public class ResourceIncludeTests : XamlTestBase { public class StaticResourceExtensionTests : XamlTestBase { [Fact] public void ResourceInclude_Loads_ResourceDictionary() { var documents = new[] { new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Resource.xaml"), @" #ff506070 "), new RuntimeXamlLoaderDocument(@" ") }; 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()); } } [Fact] public void Missing_ResourceKey_In_ResourceInclude_Does_Not_Cause_StackOverflow() { var app = Application.Current; var documents = new[] { new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Resource.xaml"), @" "), new RuntimeXamlLoaderDocument(app, @" ") }; using (StartWithResources()) { try { AvaloniaRuntimeXamlLoader.LoadGroup(documents); } catch (KeyNotFoundException) { } } } private IDisposable StartWithResources(params (string, string)[] assets) { var assetLoader = new MockAssetLoader(assets); var services = new TestServices(assetLoader: assetLoader); return UnitTestApplication.Start(services); } } } }