Files
ExecuteR/tests/Agent.Tests/BannerWindowLogicTests.cs
Alphaeus Mote b3716e3f38 perf(banner): plain Win32 and Native AOT, 47 MB to 1.6 MB
The Being Viewed banner was WinForms published as a self-contained single
file (49.3 MB, ~320 ms just to start and exit). It is now plain Win32 through
source-generated LibraryImport (runtime marshalling disabled, no reflection)
published with Native AOT: 1.6 MB for win-x64 and win-arm64, ~33 ms to start
and exit (the OS process floor), on screen in ~0.1 s.

- BannerWindow: a WS_EX_TOPMOST | TOOLWINDOW | NOACTIVATE popup, a STATIC per
  caption (read by screen readers, clicks fall through to drag) and an
  owner-drawn BUTTON per Stop, named for its session; the mouse is handled in
  a subclass and BM_CLICK invokes directly, so clicking or assistive-tech
  invoke never activates the banner. GDI drawing, comctl32 6 tooltips,
  PerMonitorV2 manifest with WM_DPICHANGED, AppsUseLightTheme and high
  contrast on WM_SETTINGCHANGE, DWM rounded corners and dark frame.
- Same metrics, colours and wording as the WinForms banner (checked against
  screenshots at 125%), now in window-free Logic/ (layout, palette, rows)
  that Agent.Tests compiles and tests.
- The protocol moves to src/Shared.Banner (compiled into agent and banner),
  with the exit codes; DLL hardening, pipe checks and protocol unchanged.
- Package-Agent publishes the banner with AOT when vswhere finds the MSVC
  tools for the RID (and puts vswhere on PATH for newer VS vcvarsall),
  otherwise warns and publishes a trimmed single file (~11 MB).
- README: Build Tools "Desktop development with C++" (plus ARM64 tools) as a
  prerequisite, also for the CI windows-host runner.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 16:33:17 -05:00

225 lines
8.7 KiB
C#

using Agent.Banner;
using Agent.Screen;
using Xunit;
namespace Agent.Tests;
public class BannerLayoutTests
{
[Theory]
[InlineData(10, 96, 10)]
[InlineData(10, 120, 12)] // 12.5: WinForms rounds half to even
[InlineData(6, 120, 8)] // 7.5
[InlineData(4, 120, 5)]
[InlineData(8, 120, 10)]
[InlineData(20, 144, 30)]
[InlineData(20, 192, 40)]
[InlineData(20, 0, 20)]
public void Scale_rounds_like_WinForms(int logical, int dpi, int expected)
=> Assert.Equal(expected, BannerLayout.Scale(logical, dpi));
[Fact]
public void Two_rows_at_96_dpi()
{
var layout = BannerLayout.Compute(96, [new(200, 17), new(250, 17)], [new(24, 17), new(24, 17)]);
// Border 1 + padding 10/6; the icon 4 below the top padding.
Assert.Equal(new Box(11, 11, 20, 20), layout.Icon);
// Captions padded by ceil(17 / 6) = 3 each side, after the icon column (20 + 8),
// centred in rows as tall as the buttons (17 + 2 * 4) and their margins (2 * 2).
Assert.Equal(new Box(39, 13, 206, 17), layout.Captions[0]);
Assert.Equal(new Box(39, 42, 256, 17), layout.Captions[1]);
// Buttons: text + 2 * 16 by text + 2 * 4, right-aligned after the widest caption + 12.
Assert.Equal(new Box(307, 9, 56, 25), layout.Buttons[0]);
Assert.Equal(new Box(307, 38, 56, 25), layout.Buttons[1]);
Assert.Equal(new TextSize(370, 72), layout.Window);
}
[Fact]
public void A_wider_button_widens_the_column_and_the_others_stay_right_aligned()
{
var layout = BannerLayout.Compute(96, [new(200, 17), new(200, 17)], [new(60, 17), new(24, 17)]);
Assert.Equal(layout.Buttons[0].Right, layout.Buttons[1].Right);
Assert.Equal(92, layout.Buttons[0].Width);
Assert.Equal(56, layout.Buttons[1].Width);
Assert.Equal(layout.Buttons[0].Right + 6 + 1, layout.Window.Width);
}
[Fact]
public void At_125_percent_it_matches_the_WinForms_banner()
{
// Segoe UI 9.75 pt at 120 DPI is 17 px (23 px lines): the WinForms banner put the
// icon at (13, 14), 25 px; the buttons 33 px tall at y 11 and 48; 92 px in all.
var layout = BannerLayout.Compute(120, [new(330, 23), new(341, 23)], [new(35, 23), new(35, 23)]);
Assert.Equal(new Box(13, 14, 25, 25), layout.Icon);
Assert.Equal(48, layout.Captions[0].Left);
Assert.Equal((11, 33), (layout.Buttons[0].Top, layout.Buttons[0].Height));
Assert.Equal(48, layout.Buttons[1].Top);
Assert.Equal(92, layout.Window.Height);
}
[Fact]
public void Scales_with_the_dpi()
{
var at96 = BannerLayout.Compute(96, [new(200, 17)], [new(24, 17)]);
var at192 = BannerLayout.Compute(192, [new(400, 34)], [new(48, 34)]);
Assert.Equal(new Box(21, 21, 40, 40), at192.Icon);
Assert.Equal((at96.Window.Width * 2) - 2, at192.Window.Width); // the 1 px border does not scale
Assert.Equal((at96.Window.Height * 2) - 2, at192.Window.Height);
}
[Fact]
public void No_rows_still_has_a_size()
{
var layout = BannerLayout.Compute(96, [], []);
Assert.Empty(layout.Captions);
Assert.True(layout.Window.Width > 0 && layout.Window.Height > 0);
}
[Fact]
public void Placed_at_the_top_centre_of_the_work_area()
{
var window = new TextSize(426, 72);
Assert.Equal(new Box(747, 8, 426, 72), BannerLayout.Place(new Box(0, 0, 1920, 1040), window, 96));
Assert.Equal(new Box(747, 10, 426, 72), BannerLayout.Place(new Box(0, 0, 1920, 1040), window, 120));
Assert.Equal(new Box(-1173, 48, 426, 72), BannerLayout.Place(new Box(-1920, 40, 1920, 1040), window, 96));
}
[Fact]
public void Boxes_intersect_only_when_they_overlap()
{
var screen = new Box(0, 0, 1920, 1040);
Assert.True(screen.IntersectsWith(new Box(1900, 1000, 100, 100)));
Assert.False(screen.IntersectsWith(new Box(1920, 0, 100, 100)));
Assert.False(screen.IntersectsWith(new Box(-500, -500, 100, 100)));
}
}
public class BannerPaletteTests
{
private static uint NoSystemColour(int index) => throw new InvalidOperationException($"GetSysColor({index}) outside high contrast");
[Fact]
public void Light_and_dark_are_the_WinForms_banner_colours()
{
var light = BannerPalette.For(dark: false, highContrast: false, NoSystemColour);
var dark = BannerPalette.For(dark: true, highContrast: false, NoSystemColour);
Assert.Equal(0xFFFFFFu, light.Back);
Assert.Equal(0x271811u, light.Fore); // #111827
Assert.Equal(0xDBD5D1u, light.Border); // #D1D5DB
Assert.Equal(0x37291Fu, dark.Back); // #1F2937
Assert.Equal(0xFBFAF9u, dark.Fore); // #F9FAFB
Assert.Equal(0x514137u, dark.Border); // #374151
foreach (var palette in new[] { light, dark })
{
Assert.Equal(0x2626DCu, palette.Button); // #DC2626
Assert.Equal(0x1C1CB9u, palette.ButtonHot); // #B91C1C
Assert.Equal(0xFFFFFFu, palette.ButtonText);
Assert.False(palette.ButtonFramed);
}
}
[Fact]
public void High_contrast_uses_the_system_colours_whatever_the_app_theme()
{
static uint System(int index) => (uint)(0x010000 * index);
foreach (var dark in new[] { false, true })
{
var palette = BannerPalette.For(dark, highContrast: true, System);
Assert.Equal(System(BannerPalette.ColorWindow), palette.Back);
Assert.Equal(System(BannerPalette.ColorWindowText), palette.Fore);
Assert.Equal(System(BannerPalette.ColorWindowFrame), palette.Border);
Assert.Equal(System(BannerPalette.ColorButtonFace), palette.Button);
Assert.Equal(System(BannerPalette.ColorButtonText), palette.ButtonText);
Assert.Equal(System(BannerPalette.ColorHighlight), palette.ButtonHot);
Assert.Equal(System(BannerPalette.ColorHighlightText), palette.ButtonHotText);
Assert.Equal(System(BannerPalette.ColorGrayText), palette.ButtonDisabledText);
Assert.True(palette.ButtonFramed);
}
}
[Fact]
public void Rgb_is_a_COLORREF() => Assert.Equal(0x563412u, BannerPalette.Rgb(0x12, 0x34, 0x56));
}
public class BannerRowsTests
{
private static readonly Guid Ann = Guid.Parse("6f1c2a4e-9b3d-4e57-8a21-c0d4e5f6a7b8");
private static readonly Guid Bob = Guid.Parse("0d9e8f7a-6b5c-4d3e-2f1a-0b9c8d7e6f5a");
[Fact]
public void Wording()
{
var control = new ScreenBannerItem(Ann, "Ann Smith", ViewOnly: false);
var view = new ScreenBannerItem(Bob, "Bob", ViewOnly: true);
Assert.Equal("Being Viewed by Ann Smith · Control", BannerRows.Caption(control));
Assert.Equal("Being Viewed by Bob · View Only", BannerRows.Caption(view));
Assert.Equal("Ann Smith can see and control this screen through ExecuteR Remote View.", BannerRows.CaptionTip(control));
Assert.Equal("Bob can see this screen through ExecuteR Remote View.", BannerRows.CaptionTip(view));
Assert.Equal("Stop Ann Smith's Remote View", BannerRows.StopName(control));
Assert.Equal("Stop", BannerRows.StopText(stopping: false));
Assert.Equal("Stopping…", BannerRows.StopText(stopping: true));
}
[Fact]
public void Stop_asks_once_per_session()
{
var rows = new BannerRows();
rows.Show([new(Ann, "Ann", false), new(Bob, "Bob", true)]);
Assert.Equal(Bob, rows.Press(1));
Assert.Null(rows.Press(1));
Assert.True(rows.IsStopping(1));
Assert.False(rows.IsStopping(0));
Assert.Null(rows.Press(2));
Assert.Null(rows.Press(-1));
Assert.False(rows.IsStopping(5));
}
[Fact]
public void A_session_still_shown_stays_stopping_and_one_dropped_is_forgotten()
{
var rows = new BannerRows();
rows.Show([new(Ann, "Ann", false), new(Bob, "Bob", true)]);
rows.Press(0);
rows.Press(1);
rows.Show([new(Bob, "Bob", true)]);
Assert.True(rows.IsStopping(0));
rows.Show([new(Ann, "Ann", false), new(Bob, "Bob", true)]);
Assert.False(rows.IsStopping(0));
Assert.True(rows.IsStopping(1));
Assert.Equal(Ann, rows.Press(0));
}
[Fact]
public void Shows_at_most_the_protocol_s_rows()
{
var rows = new BannerRows();
rows.Show([.. Enumerable.Range(1, 6).Select(i => new ScreenBannerItem(Guid.NewGuid(), $"Op {i}", false))]);
Assert.Equal(ScreenBannerProtocol.MaxItems, rows.Count);
Assert.Equal("Op 1", rows.Items[0].Operator);
}
[Fact]
public void Exit_codes_are_part_of_the_contract()
{
Assert.Equal(2, ScreenBannerProtocol.ExitBadArguments);
Assert.Equal(3, ScreenBannerProtocol.ExitNoService);
}
}