using System; using System.Collections.Generic; using System.Linq; 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 { public StyleIncludeTests() { System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(RelativeSource).TypeHandle); } [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() { var control = (ContentControl)AvaloniaRuntimeXamlLoader.Load(@" "); Assert.IsType(control.Styles[0]); Assert.IsType(control.Styles[1]); } [Fact] public void StyleInclude_From_CodeBehind_Resolves_Compiled() { using var locatorScope = AvaloniaLocator.EnterScope(); AvaloniaLocator.CurrentMutable.BindToSelf(new AssetLoader(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, IAvaloniaXamlIlParentStackProvider { 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 Parents { get; set; } = new List { new ContentControl() }; IEnumerable IAvaloniaXamlIlParentStackProvider.Parents => Parents; }