The client shipped with a PE icon Explorer showed and a window that did not:
launched from the launcher it still drew the stock Windows application icon.
Silk's Window.Create only builds the managed object. IWindow.Initialize is
what, in Silk's own words, "creates the window on the underlying platform".
Applying an icon before that throws:
after Window.Create : IsInitialized = False
SetWindowIcon BEFORE Initialize : THREW InvalidOperationException:
Window should be initialized.
after Initialize : IsInitialized = True
SetWindowIcon AFTER Initialize : returned without throwing
What made this quiet rather than obvious is the fallback. GLFW registers its
window class against a resource named GLFW_ICON and, not finding one, uses
IDI_APPLICATION - the generic Windows icon - rather than the executable's own.
So the PE icon kept showing on the file while the live window lost it, which
reads as a packaging problem and is nothing of the kind. The launcher was
unaffected because Avalonia takes a different path entirely, and that
asymmetry was the tell.
Apply now happens in OnLoad, beside the other window-dependent startup work,
and refuses with a message naming the ordering requirement if it is ever
called on an uninitialized window - the previous generic catch reported
"Window should be initialized" to a stderr nobody reads, which said nothing
about icons.
The regression guard reads the compiled call graph, because this is an
ordering edge with no observable return value: OnLoad must call Apply, and no
method that calls Window.Create may. Verified by reintroducing the bug and
watching it fail, then restoring the fix and watching it pass.
Solution builds clean; 14,408 tests pass on the standard hermetic lane filter,
0 failures.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
133 lines
5.2 KiB
C#
133 lines
5.2 KiB
C#
using System.Reflection;
|
|
using AcDream.App.Rendering;
|
|
using AcDream.App.Tests.Architecture;
|
|
using SixLabors.ImageSharp;
|
|
using SixLabors.ImageSharp.PixelFormats;
|
|
using Xunit;
|
|
|
|
namespace AcDream.App.Tests.Rendering;
|
|
|
|
/// <summary>
|
|
/// The icon resource names are coupled to <c>LogicalName</c> in
|
|
/// AcDream.App.csproj by string alone, so a rename on either side compiles
|
|
/// cleanly and only fails as a silently icon-less window at runtime. These
|
|
/// tests are that coupling's only guard.
|
|
/// </summary>
|
|
public class WindowIconLoaderTests
|
|
{
|
|
private static IReadOnlyList<string> ExpectedResourceNames()
|
|
{
|
|
var field = typeof(WindowIconLoader).GetField(
|
|
"ResourceNames", BindingFlags.NonPublic | BindingFlags.Static);
|
|
Assert.NotNull(field);
|
|
var names = (string[]?)field!.GetValue(null);
|
|
Assert.NotNull(names);
|
|
return names!;
|
|
}
|
|
|
|
[Fact]
|
|
public void EveryDeclaredIconResource_IsActuallyEmbedded()
|
|
{
|
|
var assembly = typeof(WindowIconLoader).Assembly;
|
|
string[] embedded = assembly.GetManifestResourceNames();
|
|
|
|
foreach (string name in ExpectedResourceNames())
|
|
{
|
|
Assert.True(
|
|
embedded.Contains(name),
|
|
$"WindowIconLoader expects embedded resource '{name}', but the "
|
|
+ "assembly does not contain it. Check the EmbeddedResource "
|
|
+ "LogicalName entries in AcDream.App.csproj.");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void EmbeddedIcons_DecodeToSquareRgbaImagesOfTheDeclaredSize()
|
|
{
|
|
var assembly = typeof(WindowIconLoader).Assembly;
|
|
|
|
foreach (string name in ExpectedResourceNames())
|
|
{
|
|
using Stream? stream = assembly.GetManifestResourceStream(name);
|
|
Assert.NotNull(stream);
|
|
|
|
using var image = Image.Load<Rgba32>(stream!);
|
|
Assert.Equal(image.Width, image.Height);
|
|
|
|
// The trailing "-<size>.png" must match the actual pixel size, or
|
|
// the window manager picks the wrong image for a surface.
|
|
string stem = Path.GetFileNameWithoutExtension(name);
|
|
string declared = stem[(stem.LastIndexOf('-') + 1)..];
|
|
Assert.Equal(int.Parse(declared), image.Width);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void IconSet_CoversBothSmallAndLargeSurfaces()
|
|
{
|
|
var sizes = new List<int>();
|
|
foreach (string name in ExpectedResourceNames())
|
|
{
|
|
string stem = Path.GetFileNameWithoutExtension(name);
|
|
sizes.Add(int.Parse(stem[(stem.LastIndexOf('-') + 1)..]));
|
|
}
|
|
|
|
// Title bars want ~16px and Alt-Tab/taskbar want a large one; shipping
|
|
// only one size leaves the window manager resampling badly.
|
|
Assert.Contains(16, sizes);
|
|
Assert.True(sizes.Max() >= 128, "icon set has no large size for Alt-Tab/taskbar");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Regression guard for the ordering bug that shipped once already.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Silk's <c>Window.Create</c> only builds the managed object;
|
|
/// <c>IWindow.Initialize</c> is what creates the native window. Applying an
|
|
/// icon before that throws "Window should be initialized", and GLFW then
|
|
/// falls back to the stock Windows application icon rather than the
|
|
/// executable's own — so the client shipped with a PE icon that Explorer
|
|
/// showed and the running window did not. The contract is an ordering edge
|
|
/// with no observable return value, which is exactly what
|
|
/// <see cref="CompiledCallGraph"/> exists for.
|
|
/// </remarks>
|
|
[Fact]
|
|
public void IconIsAppliedFromLoad_NotFromWindowConstruction()
|
|
{
|
|
var gameWindow = typeof(GameWindow);
|
|
MethodInfo? onLoad = gameWindow.GetMethod(
|
|
"OnLoad", BindingFlags.NonPublic | BindingFlags.Instance);
|
|
Assert.NotNull(onLoad);
|
|
|
|
bool CallsApply(MethodBase method) =>
|
|
CompiledCallGraph.Read(method).Any(
|
|
c => c.Target.DeclaringType == typeof(WindowIconLoader)
|
|
&& c.Target.Name == nameof(WindowIconLoader.Apply));
|
|
|
|
Assert.True(
|
|
CallsApply(onLoad!),
|
|
"GameWindow.OnLoad must apply the window icon: it is the first point "
|
|
+ "at which the native window exists.");
|
|
|
|
// And nowhere that runs before initialization may call it.
|
|
foreach (MethodInfo candidate in gameWindow.GetMethods(
|
|
BindingFlags.NonPublic | BindingFlags.Public
|
|
| BindingFlags.Instance | BindingFlags.Static))
|
|
{
|
|
if (candidate == onLoad || candidate.IsAbstract || candidate.ContainsGenericParameters)
|
|
continue;
|
|
|
|
bool createsWindow = CompiledCallGraph.Read(candidate).Any(
|
|
c => c.Target.Name == "Create"
|
|
&& c.Target.DeclaringType?.FullName == "Silk.NET.Windowing.Window");
|
|
if (!createsWindow)
|
|
continue;
|
|
|
|
Assert.False(
|
|
CallsApply(candidate),
|
|
$"{candidate.Name} calls Window.Create and applies the window icon in the "
|
|
+ "same method. The window is not initialized there, so the icon is "
|
|
+ "silently lost — apply it from OnLoad instead.");
|
|
}
|
|
}
|
|
}
|