Make launcher view backgrounds deterministic in visual tests

LoginView, SplashView and ServerSelectionView each picked a random
full-screen background on load, so the visual-regression baselines could
never match — every run compared against a different photo and reported a
spurious 82-94% regression. Centralize the selection in ViewBackground and
let tests disable it, so captured screenshots and baselines are stable.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Pat Hartl 2026-06-24 02:03:09 -05:00
parent 6a2b5177b0
commit 860db8ff8f
6 changed files with 60 additions and 58 deletions

View file

@ -31,6 +31,15 @@ public class ViewLayoutTests
private const int WindowWidth = 1200;
private const int WindowHeight = 800;
static ViewLayoutTests()
{
// The Login, Splash and ServerSelection views pick a random full-screen
// background on load. Disable that here so the captured screenshots — and the
// committed baselines — are deterministic; otherwise every run compares against
// a different photo and reports a spurious regression.
ViewBackground.Enabled = false;
}
// ---------------------------------------------------------------------------
// Service provider shared by all tests that need ViewModels with DI dependencies.
// Minimal: just logging — no real SDK services needed for layout-only rendering.

View file

@ -36,6 +36,10 @@
<PackageReference Include="ppy.SDL3-CS" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="LANCommander.Launcher.Tests" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LANCommander.Launcher.Services\LANCommander.Launcher.Services.csproj" />
<ProjectReference Include="..\LANCommander.Launcher.Settings\LANCommander.Launcher.Settings.csproj" />

View file

@ -1,23 +1,10 @@
using System;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
namespace LANCommander.Launcher.Views;
public partial class LoginView : UserControl
{
private static readonly string[] Backgrounds =
{
"avares://LANCommander.Launcher/Assets/backgrounds/aoe2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/ns2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/css.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/bfme2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/soldat2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/ut2004.jpg",
};
public LoginView()
{
InitializeComponent();
@ -26,12 +13,7 @@ public partial class LoginView : UserControl
private void OnLoaded(object? sender, RoutedEventArgs e)
{
try
{
var uri = new Uri(Backgrounds[Random.Shared.Next(Backgrounds.Length)]);
BackgroundImage.Source = new Bitmap(AssetLoader.Open(uri));
}
catch { /* silently ignore missing assets */ }
ViewBackground.Apply(BackgroundImage);
UsernameTextBox.Focus();
}

View file

@ -1,23 +1,10 @@
using System;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
namespace LANCommander.Launcher.Views;
public partial class ServerSelectionView : UserControl
{
private static readonly string[] Backgrounds =
{
"avares://LANCommander.Launcher/Assets/backgrounds/aoe2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/ns2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/css.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/bfme2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/soldat2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/ut2004.jpg",
};
public ServerSelectionView()
{
InitializeComponent();
@ -26,13 +13,8 @@ public partial class ServerSelectionView : UserControl
private void OnLoaded(object? sender, RoutedEventArgs e)
{
try
{
var uri = new Uri(Backgrounds[Random.Shared.Next(Backgrounds.Length)]);
BackgroundImage.Source = new Bitmap(AssetLoader.Open(uri));
}
catch { /* silently ignore missing assets */ }
ViewBackground.Apply(BackgroundImage);
ServerAddressTextBox.Focus();
}
}

View file

@ -1,23 +1,10 @@
using System;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
namespace LANCommander.Launcher.Views;
public partial class SplashView : UserControl
{
private static readonly string[] Backgrounds =
{
"avares://LANCommander.Launcher/Assets/backgrounds/aoe2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/ns2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/css.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/bfme2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/soldat2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/ut2004.jpg",
};
public SplashView()
{
InitializeComponent();
@ -26,11 +13,6 @@ public partial class SplashView : UserControl
private void OnLoaded(object? sender, RoutedEventArgs e)
{
try
{
var uri = new Uri(Backgrounds[Random.Shared.Next(Backgrounds.Length)]);
BackgroundImage.Source = new Bitmap(AssetLoader.Open(uri));
}
catch { /* silently ignore missing assets */ }
ViewBackground.Apply(BackgroundImage);
}
}

View file

@ -0,0 +1,43 @@
using System;
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
namespace LANCommander.Launcher.Views;
/// <summary>
/// Picks a random full-screen background for the Login, Splash and ServerSelection
/// views. Visual-regression tests disable the random pick via <see cref="Enabled"/>
/// so the rendered output (and therefore the committed baseline) stays deterministic.
/// </summary>
internal static class ViewBackground
{
private static readonly string[] Backgrounds =
{
"avares://LANCommander.Launcher/Assets/backgrounds/aoe2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/ns2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/css.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/bfme2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/soldat2.jpg",
"avares://LANCommander.Launcher/Assets/backgrounds/ut2004.jpg",
};
/// <summary>
/// When false, no random background is loaded. Set by visual-regression tests so
/// the rendered output is deterministic across runs.
/// </summary>
public static bool Enabled { get; set; } = true;
public static void Apply(Image target)
{
if (!Enabled)
return;
try
{
var uri = new Uri(Backgrounds[Random.Shared.Next(Backgrounds.Length)]);
target.Source = new Bitmap(AssetLoader.Open(uri));
}
catch { /* silently ignore missing assets */ }
}
}