using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using Avalonia.Controls; using Avalonia.Data; using Avalonia.Markup.Xaml.Styling; using Avalonia.Markup.Xaml.XamlIl.Runtime; using Avalonia.Media; using Avalonia.Platform; using Avalonia.Styling; using Avalonia.Themes.Simple; using Avalonia.UnitTests; using Xunit; namespace Avalonia.Markup.Xaml.UnitTests.Xaml; public class StyleIncludeTests { static StyleIncludeTests() { RuntimeHelpers.RunClassConstructor(typeof(RelativeSource).TypeHandle); AssetLoader.RegisterResUriParsers(); } [Fact] public void StyleInclude_Is_Built() { using (UnitTestApplication.Start(TestServices.StyledWindow .With(theme: () => new Styles()))) { var xaml = @" "; var window = AvaloniaRuntimeXamlLoader.Parse(xaml); Assert.IsType"), new RuntimeXamlLoaderDocument(@" ") }; var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents); var style = Assert.IsType"), new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Subfolder/Folder/Root.xaml"), @" ") }; var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents); var style = Assert.IsType"), new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Root.xaml"), @" ") }; var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents); var style = Assert.IsType"), new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Root.xaml"), @" ") }; var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents); var style = Assert.IsType"), new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Root.xaml"), @" ") }; var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents); var style = Assert.IsType"), new RuntimeXamlLoaderDocument(@" ") }; var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents); var style = Assert.IsType"), new RuntimeXamlLoaderDocument(@" ") }; try { _ = AvaloniaRuntimeXamlLoader.LoadGroup(documents); } catch (KeyNotFoundException) { } } [Fact] public void StyleInclude_Should_Be_Replaced_With_Direct_Call() { using var _ = UnitTestApplication.Start(TestServices.StyledWindow); var control = (ContentControl)AvaloniaRuntimeXamlLoader.Load(@" "); Assert.IsType(control.Styles[0]); Assert.IsType(control.Styles[1]); } [Fact] public void Style_Inside_Resources_Should_Produce_Warning() { using var _ = UnitTestApplication.Start(TestServices.StyledWindow); var diagnostics = new List(); var control = (ContentControl)AvaloniaRuntimeXamlLoader.Load(new RuntimeXamlLoaderDocument(@" "), new RuntimeXamlLoaderConfiguration { DiagnosticHandler = diagnostic => { diagnostics.Add(diagnostic); return diagnostic.Severity; } }); Assert.IsAssignableFrom(((ResourceDictionary)control.Resources)!.MergedDictionaries[0]); var warning = Assert.Single(diagnostics); Assert.Equal(RuntimeXamlDiagnosticSeverity.Warning, warning.Severity); } [Fact] public void StyleInclude_From_CodeBehind_Resolves_Compiled() { using var locatorScope = AvaloniaLocator.EnterScope(); AvaloniaLocator.CurrentMutable.BindToSelf(new StandardAssetLoader(GetType().Assembly)); var sp = new TestServiceProvider(); var styleInclude = new StyleInclude(sp) { Source = new Uri("avares://Avalonia.Markup.Xaml.UnitTests/Xaml/StyleWithServiceProvider.xaml") }; var loaded = Assert.IsType(styleInclude.Loaded); Assert.Equal( sp.GetService().Parents, loaded.ServiceProvider.GetService().Parents); } } public class TestServiceProvider : IServiceProvider, IUriContext, IAvaloniaXamlIlEagerParentStackProvider { private IServiceProvider _root = XamlIlRuntimeHelpers.CreateRootServiceProviderV2(); public object GetService(Type serviceType) { if (serviceType == typeof(IUriContext)) { return this; } if (serviceType == typeof(IAvaloniaXamlIlParentStackProvider)) { return this; } return _root.GetService(serviceType); } public Uri BaseUri { get; set; } public List ParentsStack { get; set; } = [new ContentControl()]; IEnumerable IAvaloniaXamlIlParentStackProvider.Parents => ParentsStack.AsEnumerable().Reverse(); IReadOnlyList IAvaloniaXamlIlEagerParentStackProvider.DirectParentsStack => ParentsStack; IAvaloniaXamlIlEagerParentStackProvider IAvaloniaXamlIlEagerParentStackProvider.ParentProvider => null; }