using System; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; using Microsoft.Data.Sqlite; using Xunit; using ytLive.Models; using ytLive.ViewModels; namespace ytLive.Tests; /// /// Verifies the zoom box created by a right-click in the live preview (creator- /// corrected model, 2026-10-08): every ZoomView shape procures the right bounding /// box CENTERED on the click and a FIXED capture window (the box/2 region centred /// on the click). Dragging the box relocates it WITHOUT changing the capture — the /// box always shows the same anchored content magnified 2x. The rectangle is 4:3 /// ("more of a 640x480 aspect ratio"). The box is a real composited layer drawn /// LAST (a ZoomSceneElement placeholder sits at z-index 1 whose rect + capture /// mirror the box; Kind flips Dynamic only while active), and the output crop /// stays the tier (the box is never the crop source). /// [Collection("RealApp")] public sealed class ZoomBoxInteractionTests { private readonly RealAppHost _app; public ZoomBoxInteractionTests(RealAppHost app) { _app = app; } [Fact] public void Zoom_Shape_Controls_Box_Aspect_Anchor_Layer_And_Render() { _app.Run(Run); } private void Run() { var tempDb = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"ytLlive-test-{Guid.NewGuid():N}.db"); MainViewModel.LayoutPathOverride = tempDb; var window = new MainWindow(); var vm = (MainViewModel)window.DataContext; try { window.Show(); window.UpdateLayout(); Assert.NotNull(vm.StagedScene); // The healed zoom placeholder sits at z-index 1, inert. var placeholder = (ZoomSceneElement)vm.StagedScene.Elements[1]; Assert.False(placeholder.IsZoomActive); Assert.Equal(ElementKind.Static, placeholder.Kind); vm.IsRecording = true; var pane = (UserControl)window.FindName("PreviewPane")!; var zoomBox = (Grid)pane.FindName("ZoomBox")!; var rectOutline = (Rectangle)pane.FindName("ZoomRectOutline")!; var circleOutline = (Ellipse)pane.FindName("ZoomCircleOutline")!; // Circle: square box, click point is the CENTER of box AND capture. vm.ZoomShape = ZoomShape.Circle; vm.ActivateZoom(1000, 500); Assert.True(vm.IsZoomActive); Assert.Equal(vm.ZoomState.Width, vm.ZoomState.Height, 3); Assert.Equal(1000 - vm.ZoomState.Width / 2, vm.ZoomState.X, 3); Assert.Equal(500 - vm.ZoomState.Height / 2, vm.ZoomState.Y, 3); Assert.Equal(1000 - vm.ZoomState.Width / 4, vm.ZoomState.CaptureX, 3); Assert.Equal(500 - vm.ZoomState.Height / 4, vm.ZoomState.CaptureY, 3); AssertTrueActiveLayer(vm); window.UpdateLayout(); Assert.Equal(Visibility.Visible, zoomBox.Visibility); Assert.IsType(zoomBox.Clip); Assert.Equal(Visibility.Visible, circleOutline.Visibility); Assert.Equal(Visibility.Collapsed, rectOutline.Visibility); // Phone (vertical rectangle): tall 9:16 box; capture centred on the click. vm.ZoomShape = ZoomShape.VerticalRectangle; vm.ActivateZoom(960, 540); Assert.True(vm.ZoomState.Height > vm.ZoomState.Width); Assert.Equal(9.0 / 16.0, vm.ZoomState.Width / vm.ZoomState.Height, 3); Assert.Equal(960 - vm.ZoomState.Width / 2, vm.ZoomState.X, 3); Assert.Equal(540 - vm.ZoomState.Height / 2, vm.ZoomState.Y, 3); Assert.Equal(960 - vm.ZoomState.Width / 4, vm.ZoomState.CaptureX, 3); Assert.Equal(540 - vm.ZoomState.Height / 4, vm.ZoomState.CaptureY, 3); AssertTrueActiveLayer(vm); window.UpdateLayout(); Assert.Null(zoomBox.Clip); Assert.Equal(Visibility.Collapsed, circleOutline.Visibility); Assert.Equal(Visibility.Visible, rectOutline.Visibility); // Tablet (horizontal rectangle): 4:3 wide box (creator: "more of a // 640x480 aspect ratio" — the first-cut 16:9 read as a square). vm.ZoomShape = ZoomShape.HorizontalRectangle; vm.ActivateZoom(960, 540); Assert.True(vm.ZoomState.Width > vm.ZoomState.Height); Assert.Equal(4.0 / 3.0, vm.ZoomState.Width / vm.ZoomState.Height, 3); Assert.Equal(960 - vm.ZoomState.Width / 2, vm.ZoomState.X, 3); Assert.Equal(540 - vm.ZoomState.Height / 2, vm.ZoomState.Y, 3); Assert.Equal(960 - vm.ZoomState.Width / 4, vm.ZoomState.CaptureX, 3); Assert.Equal(540 - vm.ZoomState.Height / 4, vm.ZoomState.CaptureY, 3); AssertTrueActiveLayer(vm); window.UpdateLayout(); Assert.Null(zoomBox.Clip); Assert.Equal(Visibility.Collapsed, circleOutline.Visibility); Assert.Equal(Visibility.Visible, rectOutline.Visibility); // THE core semantic: dragging the box moves only the box. The capture — // and therefore the content the box shows — never changes. (Box is still // the un-clamped tablet box [456,162] at this point with room to move.) var boxX = vm.ZoomState.X; var boxY = vm.ZoomState.Y; var captureX = vm.ZoomState.CaptureX; var captureY = vm.ZoomState.CaptureY; vm.StartZoomMove(800, 400); vm.UpdateZoomMove(1200, 600); vm.EndZoomMove(); Assert.True(Math.Abs(vm.ZoomState.X - boxX) >= 100, "the box must have moved"); Assert.True(Math.Abs(vm.ZoomState.Y - boxY) >= 100, "the box must have moved"); Assert.Equal(captureX, vm.ZoomState.CaptureX); Assert.Equal(captureY, vm.ZoomState.CaptureY); var zoom = Assert.IsType(vm.StagedScene.Elements[1]); Assert.Equal(vm.ZoomState.X, zoom.X, 3); Assert.Equal(vm.ZoomState.Y, zoom.Y, 3); Assert.Equal(captureX, zoom.CaptureX, 3); Assert.Equal(captureY, zoom.CaptureY, 3); // Clamp keeps the box AND the capture fully inside the 1920x1080 master // even for a corner click that would otherwise overhang both. vm.ActivateZoom(2000, 2000); const double frameW = 1920, frameH = 1080; Assert.True(vm.ZoomState.X >= 0 && vm.ZoomState.Y >= 0); Assert.True(vm.ZoomState.X + vm.ZoomState.Width <= frameW); Assert.True(vm.ZoomState.Y + vm.ZoomState.Height <= frameH); Assert.True(vm.ZoomState.CaptureX >= 0 && vm.ZoomState.CaptureY >= 0); Assert.True(vm.ZoomState.CaptureX + vm.ZoomState.Width / 2 <= frameW); Assert.True(vm.ZoomState.CaptureY + vm.ZoomState.Height / 2 <= frameH); AssertTrueActiveLayer(vm); // The box is a composited layer, NEVER the crop: output stays the tier. var opts = vm.BuildCompositorOptions(); Assert.True(opts.SourceRectWidth > vm.ZoomState.Width); Assert.Equal((int)Math.Round(vm.OutputRectWidth), opts.SourceRectWidth); Assert.Equal((int)Math.Round(vm.OutputRectHeight), opts.SourceRectHeight); // Dismiss restores the inert placeholder (Kind back to Static). vm.DismissZoom(); Assert.False(vm.IsZoomActive); Assert.False(placeholder.IsZoomActive); Assert.False(placeholder.IsVisible); Assert.Equal(ElementKind.Static, placeholder.Kind); } finally { window.Close(); MainViewModel.LayoutPathOverride = null; SqliteConnection.ClearAllPools(); try { System.IO.File.Delete(tempDb); } catch { } } } private static void AssertTrueActiveLayer(MainViewModel vm) { var zoom = Assert.IsType(vm.StagedScene!.Elements[1]); Assert.True(zoom.IsZoomActive); Assert.True(zoom.IsVisible); Assert.Equal(ElementKind.Dynamic, zoom.Kind); Assert.Equal(vm.ZoomState.X, zoom.X, 3); Assert.Equal(vm.ZoomState.Y, zoom.Y, 3); Assert.Equal(vm.ZoomState.Width, zoom.Width, 3); Assert.Equal(vm.ZoomState.Height, zoom.Height, 3); Assert.Equal(vm.ZoomState.CaptureX, zoom.CaptureX, 3); Assert.Equal(vm.ZoomState.CaptureY, zoom.CaptureY, 3); Assert.Equal(vm.ZoomShape == ZoomShape.Circle ? ClipShape.Round : ClipShape.Traditional, zoom.ClipShape); } }