Files
OpenNest/OpenNest.WinForms.Tests/Controls/ActionCloneTests.cs
T
ajandClaude Opus 5.5 5b67d78ac7 fix(ui): return the placing preview to the cursor after a settled click
Shift/Ctrl+click settles the preview onto the plate, then copies it. The
preview stayed on the settled copy until the mouse moved, so clicking again
without moving settled from inside that copy and stacked the next part on it.
The preview now goes back to where it was clicked, keeping any arrow nudge.

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

183 lines
7.7 KiB
C#

using System.Reflection;
using System.Windows.Forms;
using OpenNest.Actions;
using OpenNest.Controls;
using OpenNest.Geometry;
using static OpenNest.WinForms.Tests.FormTesting;
namespace OpenNest.WinForms.Tests.Controls;
/// <summary>
/// Placing or cloning parts: the previews follow the cursor as the selection and must keep
/// their selected look until placed, whatever keys the user presses meanwhile.
/// </summary>
public class ActionCloneTests
{
private static Drawing Square(string name)
{
var program = new CNC.Program();
program.Codes.Add(new CNC.RapidMove(new Vector(0, 0)));
program.Codes.Add(new CNC.LinearMove(new Vector(4, 0)));
program.Codes.Add(new CNC.LinearMove(new Vector(4, 4)));
program.Codes.Add(new CNC.LinearMove(new Vector(0, 4)));
program.Codes.Add(new CNC.LinearMove(new Vector(0, 0)));
return new Drawing(name, program);
}
[Fact]
public void DeleteWhilePlacingADrawingKeepsThePreviewSelected() => StaTestThread.Run(() =>
{
var plate = new Plate(48, 96);
var placed = new Part(Square("placed"));
plate.Parts.Add(placed);
using var view = new PlateView { Plate = plate, Size = new System.Drawing.Size(600, 600) };
view.ZoomToFit();
view.SetAction(typeof(ActionClone), Square("new"));
Raise(view, "OnKeyDown", new KeyEventArgs(Keys.Delete));
var preview = Assert.Single(view.SelectedParts);
Assert.True(preview.IsSelected);
Assert.IsType<ActionClone>(view.Actions.CurrentAction);
Assert.Same(placed, Assert.Single(plate.Parts));
}, TimeSpan.FromMinutes(1), "The STA test did not complete.");
[Fact]
public void DeleteWhileCloningPlacedPartsRemovesTheOriginals() => StaTestThread.Run(() =>
{
var plate = new Plate(48, 96);
var original = new Part(Square("original"));
plate.Parts.Add(original);
using var view = new PlateView { Plate = plate, Size = new System.Drawing.Size(600, 600) };
view.ZoomToFit();
view.SetAction(typeof(ActionClone), new List<Part> { original });
Raise(view, "OnKeyDown", new KeyEventArgs(Keys.Delete));
Assert.Empty(plate.Parts);
Assert.True(Assert.Single(view.SelectedParts).IsSelected);
}, TimeSpan.FromMinutes(1), "The STA test did not complete.");
[Fact]
public void DeleteAfterSelectAllRemovesPlacedPartsAndKeepsPlacing() => StaTestThread.Run(() =>
{
var plate = new Plate(48, 96);
var drawing = Square("filled");
plate.Parts.Add(new Part(drawing));
plate.Parts.Add(new Part(drawing));
using var view = new PlateView { Plate = plate, Size = new System.Drawing.Size(600, 600) };
view.ZoomToFit();
view.SetAction(typeof(ActionClone), drawing);
var preview = Assert.Single(view.SelectedParts);
view.SelectAll();
Raise(view, "OnKeyDown", new KeyEventArgs(Keys.Delete));
Assert.Empty(plate.Parts);
Assert.Same(preview, Assert.Single(view.SelectedParts));
Assert.True(preview.IsSelected);
Assert.IsType<ActionClone>(view.Actions.CurrentAction);
}, TimeSpan.FromMinutes(1), "The STA test did not complete.");
[Fact]
public void F1SetsThePartInPlace() => StaTestThread.Run(() =>
{
var plate = new Plate(48, 96);
using var view = new PlateView { Plate = plate, Size = new System.Drawing.Size(600, 600) };
view.ZoomToFit();
view.SetAction(typeof(ActionClone), Square("new"));
Raise(view, "OnKeyDown", new KeyEventArgs(Keys.F1));
Assert.Single(plate.Parts);
}, TimeSpan.FromMinutes(1), "The STA test did not complete.");
[Theory]
[InlineData(Keys.Shift)]
[InlineData(Keys.Control)]
public void ModifiedClickPushesTheCopyAgainstPlacedParts(Keys modifiers) => StaTestThread.Run(() =>
{
var plate = new Plate(48, 96) { PartSpacing = 0 };
plate.Parts.Add(new Part(Square("placed")));
using var view = new PlateView { Plate = plate, Size = new System.Drawing.Size(600, 600) };
view.ZoomToFit();
view.SetAction(typeof(ActionClone), Square("new"));
var action = Assert.IsType<ActionClone>(view.Actions.CurrentAction);
action.PlacementOrder = OpenNest.Engine.Fill.SettleOrder.VerticalFirst;
var preview = Assert.Single(view.SelectedParts);
preview.BasePart.Location = new Vector(20, 1);
action.Apply(modifiers);
var copy = plate.Parts[1].BoundingBox;
Assert.InRange(copy.Left, 4, 4.01);
Assert.Equal(0, copy.Bottom, 6);
}, TimeSpan.FromMinutes(1), "The STA test did not complete.");
[Theory]
[InlineData(Keys.Shift)]
[InlineData(Keys.Control)]
public void RepeatedModifiedClicksWithoutMovingStackNoCopies(Keys modifiers) => StaTestThread.Run(() =>
{
var plate = new Plate(48, 96) { PartSpacing = 0 };
using var view = new PlateView { Plate = plate, Size = new System.Drawing.Size(600, 600) };
view.ZoomToFit();
view.SetAction(typeof(ActionClone), Square("new"));
var action = Assert.IsType<ActionClone>(view.Actions.CurrentAction);
action.PlacementOrder = OpenNest.Engine.Fill.SettleOrder.VerticalFirst;
var preview = Assert.Single(view.SelectedParts);
preview.BasePart.Location = new Vector(1, 20);
action.Apply(modifiers);
Assert.Equal(new Vector(1, 20), preview.BasePart.BoundingBox.Location);
action.Apply(modifiers);
action.Apply(modifiers);
Assert.Equal(3, plate.Parts.Count);
var bottoms = plate.Parts.Select(p => p.BoundingBox.Bottom).OrderBy(y => y).ToList();
Assert.Equal(0, bottoms[0], 6);
Assert.InRange(bottoms[1], 4, 4.01);
Assert.InRange(bottoms[2], 8, 8.02);
}, TimeSpan.FromMinutes(1), "The STA test did not complete.");
[Fact]
public void ADialogsFakeMouseMoveLeavesNudgedPreviewsInPlace() => StaTestThread.Run(() =>
{
using var view = new PlateView { Plate = new Plate(48, 96), Size = new System.Drawing.Size(600, 600) };
view.ZoomToFit();
view.SetAction(typeof(ActionClone), Square("new"));
var preview = Assert.Single(view.SelectedParts);
MoveMouse(view, new System.Drawing.Point(300, 300));
var followed = preview.BasePart.BoundingBox.Location;
Nudge(view);
var nudged = preview.BasePart.BoundingBox.Location;
Assert.Equal(followed.X + view.OffsetIncrementDistance, nudged.X, 6);
// A repost of the last position, with no focus change.
MoveMouse(view, new System.Drawing.Point(300, 300));
Assert.Equal(nudged, preview.BasePart.BoundingBox.Location);
// Opening the dialog, then closing it: each post arrives where the cursor rests.
var rest = view.PointToClient(Cursor.Position);
Raise(view, "OnLostFocus", EventArgs.Empty);
MoveMouse(view, rest);
Assert.Equal(nudged, preview.BasePart.BoundingBox.Location);
Raise(view, "OnGotFocus", EventArgs.Empty);
MoveMouse(view, rest);
Assert.Equal(nudged, preview.BasePart.BoundingBox.Location);
// The next real move takes the previews back to the cursor.
MoveMouse(view, new System.Drawing.Point(rest.X + 20, rest.Y + 20));
Assert.NotEqual(nudged, preview.BasePart.BoundingBox.Location);
}, TimeSpan.FromMinutes(1), "The STA test did not complete.");
private static void MoveMouse(PlateView view, System.Drawing.Point point) =>
Raise(view, "OnMouseMove", new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0));
private static void Nudge(PlateView view) =>
typeof(PlateView).GetMethod("ProcessDialogKey", BindingFlags.Instance | BindingFlags.NonPublic)!
.Invoke(view, new object[] { Keys.Right });
}