Files
OpenNest/OpenNest.WinForms.Tests/Forms/CutSimulatorFormTests.cs
T
ajandClaude Sonnet 5.5 f9f5299d16 feat(thermal): finer heat map, on a graphics card when there is one
Split only cutting time into short steps, raise the automatic resolution,
add an optional ILGPU accelerator with CPU fallback, and estimate heat off
the UI thread.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-12-02 20:21:08 -05:00

205 lines
8.5 KiB
C#

using System.Reflection;
using System.Windows.Forms;
using OpenNest.Forms;
using OpenNest.Geometry;
namespace OpenNest.WinForms.Tests.Forms;
// The layout checks failed once on CI (Settings tab columns ~84 px wider than measured when the
// window sized itself) and passed on the same commit and runner image before and after, never
// locally. Run apart from the parallel tests so other forms on other threads cannot interfere.
[CollectionDefinition("Cut simulator layout", DisableParallelization = true)]
public class CutSimulatorLayoutCollection;
[Collection("Cut simulator layout")]
public class CutSimulatorFormTests
{
[Fact]
public void InputsSitOnTheSettingsTabAndBothTabsFitWithoutResizing() => RunSta(() =>
{
using var editor = Editor();
editor.Show();
editor.PlateManager.LoadAt(0);
editor.SimulateCutting();
var simulator = (CutSimulatorForm)typeof(EditNestForm)
.GetField("cutSimulatorForm", BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(editor)!;
try
{
var tabs = Field<TabControl>(simulator, "tabs");
Assert.Equal(new[] { "Status", "Settings" }, tabs.TabPages.Cast<TabPage>().Select(p => p.Text));
var settings = tabs.TabPages[1];
foreach (var input in new[] { "feedBox", "holeFeedBox", "rapidBox", "pierceBox", "heatPowerBox", "skipEtchBox", "machineLabel" })
Assert.Same(settings, PageOf(Field<Control>(simulator, input)));
Assert.Same(tabs.TabPages[0], PageOf(Field<Control>(simulator, "heatBox")));
// The time stays above the tabs so it can be read while settings change.
Assert.Null(PageOf(Field<Control>(simulator, "timeLabel")));
var size = simulator.Size;
foreach (TabPage page in tabs.TabPages)
{
tabs.SelectedTab = page;
simulator.PerformLayout();
Application.DoEvents();
Assert.Equal(size, simulator.Size);
foreach (var control in Descendants(page.Controls[0]).Where(c => c.Visible))
{
var bounds = page.RectangleToClient(control.RectangleToScreen(control.ClientRectangle));
Assert.True(page.ClientRectangle.Contains(bounds),
$"{control.GetType().Name} '{control.Text}' at {bounds} is clipped by the {page.Text} tab ({page.ClientRectangle}).\n"
+ DescribeLayout(tabs, page));
}
}
}
finally
{
simulator.Close();
}
});
[Fact]
public void TheSliderLeadsAndTheJumpButtonsSpanItsTrackEvenly() => RunSta(() =>
{
using var editor = Editor();
editor.Show();
editor.PlateManager.LoadAt(0);
editor.SimulateCutting();
var simulator = (CutSimulatorForm)typeof(EditNestForm)
.GetField("cutSimulatorForm", BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(editor)!;
try
{
Application.DoEvents();
var track = Field<TrackBar>(simulator, "trackBar");
var play = Field<Button>(simulator, "playButton");
Assert.True(Screen(track).Bottom <= Screen(play).Top, "The slider sits above the transport buttons.");
var jumps = new[] { "previousPartButton", "previousContourButton", "nextContourButton", "nextPartButton" }
.Select(name => Screen(Field<Button>(simulator, name))).ToArray();
Assert.True(jumps.Max(b => b.Width) - jumps.Min(b => b.Width) <= 1,
$"Jump buttons differ in width: {string.Join(", ", jumps.Select(b => b.Width))}");
// Ordinary buttons: the row never stretches them taller than the play button.
Assert.All(jumps, b => Assert.True(b.Height < Screen(play).Height, $"Jump button is {b.Height} px tall."));
var channel = new int[4];
var handle = System.Runtime.InteropServices.GCHandle.Alloc(channel, System.Runtime.InteropServices.GCHandleType.Pinned);
try
{
SendMessage(track.Handle, 0x0400 + 26, IntPtr.Zero, handle.AddrOfPinnedObject());
}
finally
{
handle.Free();
}
var start = track.PointToScreen(new System.Drawing.Point(channel[0], 0)).X;
var end = track.PointToScreen(new System.Drawing.Point(channel[2], 0)).X;
Assert.InRange(jumps[0].Left - start, -1, 1);
Assert.InRange(jumps[^1].Right - end, -1, 1);
}
finally
{
simulator.Close();
}
});
[Fact]
public void TheHeatLayerAppearsOnceItsBackgroundRunFinishes() => RunSta(() =>
{
using var editor = Editor();
editor.Nest.Thickness = 0.135;
editor.Show();
editor.PlateManager.LoadAt(0);
editor.SimulateCutting();
var simulator = (CutSimulatorForm)typeof(EditNestForm)
.GetField("cutSimulatorForm", BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(editor)!;
try
{
Field<ComboBox>(simulator, "heatBox").SelectedIndex = 1;
var overlay = editor.PlateView.CutSimulation;
var label = Field<Label>(simulator, "heatLabel");
var wait = System.Diagnostics.Stopwatch.StartNew();
while (overlay.Heat == null && wait.Elapsed < TimeSpan.FromSeconds(20))
{
Application.DoEvents();
Thread.Sleep(10);
}
Assert.NotNull(overlay.Heat);
Assert.StartsWith("Hottest", label.Text);
// A rebuilt timeline with the same moves shows the same model at once.
var shown = overlay.Heat;
typeof(CutSimulatorForm).GetMethod("Rebuild", BindingFlags.Instance | BindingFlags.NonPublic)!
.Invoke(simulator, null);
Assert.Same(shown, overlay.Heat);
}
finally
{
simulator.Close();
}
});
// Everything needed to tell which column grew, and whether the measurement or the layout moved.
private static string DescribeLayout(TabControl tabs, TabPage page)
{
var content = page.Controls[0];
var lines = new List<string>
{
$"DPI {page.DeviceDpi}; tabs {tabs.Size}, minimum {tabs.MinimumSize}; content {content.Bounds}, "
+ $"preferred {content.GetPreferredSize(System.Drawing.Size.Empty)}",
};
if (content is TableLayoutPanel table)
lines.Add($"Columns: {string.Join(", ", table.GetColumnWidths())}");
foreach (Control control in content.Controls)
{
lines.Add($" {control.GetType().Name} '{control.Text}' {control.Bounds} preferred "
+ $"{control.GetPreferredSize(System.Drawing.Size.Empty)} font {control.Font.Name} {control.Font.SizeInPoints}pt");
}
return string.Join("\n", lines);
}
private static System.Drawing.Rectangle Screen(Control control) =>
control.RectangleToScreen(control.ClientRectangle);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
private static TabPage? PageOf(Control control)
{
for (var parent = control.Parent; parent != null; parent = parent.Parent)
{
if (parent is TabPage page)
return page;
}
return null;
}
private static IEnumerable<Control> Descendants(Control root)
{
foreach (Control child in root.Controls)
{
yield return child;
foreach (var nested in Descendants(child))
yield return nested;
}
}
private static T Field<T>(object owner, string name) where T : class =>
(T)owner.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(owner)!;
private static EditNestForm Editor()
{
var nest = new Nest();
var plate = nest.CreatePlate();
plate.Size = new OpenNest.Geometry.Size(48, 48);
var drawing = new Drawing("square", TestPrograms.Square(10));
nest.Drawings.Add(drawing);
plate.Parts.Add(new Part(drawing, new Vector(5, 5)));
return new EditNestForm(nest);
}
private static void RunSta(System.Action action) =>
StaTestThread.Run(action, TimeSpan.FromSeconds(30), "Cut simulator form test timed out.");
}