A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

66 lines
2.6 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Avalonia.Markup.Xaml.XamlIl.Runtime;
using Avalonia.Platform;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Xunit;
namespace Avalonia.Themes.UnitTests.Utilities;
public abstract class ThemeTestBase(Type typeEntryPoint) : IDisposable
{
private readonly HeadlessUnitTestApplication.Scope _headlessScope = HeadlessUnitTestApplication.Start();
static ThemeTestBase()
{
StandardAssetLoader.RegisterResUriParsers();
}
protected Assembly TestAssembly { get; } = typeEntryPoint.Assembly;
protected Type EntryPointClass { get; } = typeEntryPoint;
protected IEnumerable<MethodInfo> FindXamlPopulateMethods() =>
TestAssembly.GetType("CompiledAvaloniaXaml.!AvaloniaResources", throwOnError: true)!
.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)
.Where(m => m.Name.StartsWith("Populate:"));
protected IEnumerable<MethodInfo> FindXamlBuildMethods() =>
TestAssembly.GetType("CompiledAvaloniaXaml.!AvaloniaResources", throwOnError: true)!
.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)
.Where(m => m.Name.StartsWith("Build:"));
protected MethodInfo FindXamlLoader()
{
var loaderType = TestAssembly.GetType("CompiledAvaloniaXaml.!XamlLoader", throwOnError: true)!;
var tryLoadMethod = loaderType.GetMethod("TryLoad", BindingFlags.Public | BindingFlags.Static, [typeof(IServiceProvider), typeof(string)]);
Assert.NotNull(tryLoadMethod);
return tryLoadMethod;
}
protected Styles CreateAttachedTheme(IStyleHost? host = null)
{
host ??= Application.Current ?? throw new InvalidOperationException("Application.Current should not be null.");
var sp = XamlIlRuntimeHelpers.CreateRootServiceProviderV2();
var theme = CallCtor(sp);
host.Styles.Add(theme);
return theme;
}
protected Styles CallCtor(IServiceProvider? sp = null)
{
var theme = Activator.CreateInstance(EntryPointClass, sp);
return Assert.IsAssignableFrom<Styles>(theme);
}
protected object? CallTryLoad(string relativePath, IServiceProvider? sp = null)
{
var fullPath = $"avares://{TestAssembly.GetName().Name}/{relativePath}";
Assert.True(Uri.TryCreate(fullPath, UriKind.Absolute, out _), "Invalid TryLoad URL");
return FindXamlLoader().Invoke(null, [sp, fullPath]);
}
public void Dispose() => _headlessScope.Dispose();
}