using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Headless.XUnit;
using Avalonia.Media;
using Avalonia.Threading;
using Xunit;
using Xunit.Abstractions;
namespace IFSRenderer.AvaloniaShell.Tests;
/// <summary>
/// Theme-owned surfaces must agree with the ones the app paints itself.
/// </summary>
/// <remarks>
/// The port hardcoded the values sampled from the Windows screenshots (paper `#323232`, card
/// `#454545`) while leaving Material's own (`#303030`, `#424242`) in place for every surface the
/// theme draws — flyouts, expanders, menu popups. Two units apart is invisible in isolation and
/// obvious where the two meet. `App.axaml` now overrides the theme keys, so this pins that they
/// actually took effect: a silently-misspelled resource key would be a no-op.
/// </remarks>
public class ThemeSurfaceTests
{
private readonly ITestOutputHelper _out;
public ThemeSurfaceTests(ITestOutputHelper output) => _out = output;
private static Color? Resolve(string key)
{
object found = null;
Application.Current?.TryGetResource(key, Application.Current.ActualThemeVariant, out found);
return (found as ISolidColorBrush)?.Color;
}
[AvaloniaFact]
public void The_theme_paper_and_card_match_the_measured_windows_values()
{
Color? paper = Resolve("MaterialDesignPaper");
Color? card = Resolve("MaterialDesignCardBackground");
_out.WriteLine($"MaterialDesignPaper = {paper}");
_out.WriteLine($"MaterialDesignCardBackground = {card}");
Assert.NotNull(paper);
Assert.NotNull(card);
// Sampled from the reference Windows screenshots; see labs#3's palette table.
Assert.Equal(Color.Parse("#323232"), paper.Value);
Assert.Equal(Color.Parse("#454545"), card.Value);
}
/// <summary>
/// The window ground and the theme paper must be the same colour, or every popup that opens
/// over the window shows a seam.
/// </summary>
[AvaloniaFact]
public void The_window_ground_matches_the_theme_paper()
{
(MainWindow window, _) = MainWindowHarness.Start();
Dispatcher.UIThread.RunJobs();
// XAML yields an ImmutableSolidColorBrush, which does not derive from SolidColorBrush.
var ground = window.Background as ISolidColorBrush;
Assert.NotNull(ground);
Assert.Equal(Resolve("MaterialDesignPaper"), ground.Color);
}
}