Multi-scene webcam (schema v3/v4): singleton Webcam + per-scene WebcamSceneConfig, right-click OBS-style border/context menu, persisted round-to-rect restore + legacy-square 16:9 heal, dark MenuItem template, tests (25 passing)
This commit is contained in:
@@ -9,37 +9,79 @@ namespace ytLive.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The layout DB is a full rewrite on every save (DELETE all scenes/sources,
|
||||
/// re-insert from memory). This guards the round trip: sources the user deletes
|
||||
/// in the UI must not come back after a save + reload.
|
||||
/// re-insert from memory). This guards the round trip: webcam configs the user
|
||||
/// removes in the UI must not come back after a save + reload.
|
||||
/// </summary>
|
||||
public class LayoutStorePersistenceTests
|
||||
{
|
||||
[Fact]
|
||||
public void Deleted_Webcam_Source_Does_Not_Return_After_Save_And_Reload()
|
||||
public void Deleted_Webcam_Config_Does_Not_Return_After_Save_And_Reload()
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), $"ytLlive-layout-{Guid.NewGuid():N}.db");
|
||||
try
|
||||
{
|
||||
using var store = new LayoutStore(path);
|
||||
var webcam = new Webcam { DeviceId = "real-device", Name = "Logitech" };
|
||||
var scene = new Scene { Name = "Starting" };
|
||||
scene.Sources.Add(new Source
|
||||
scene.Elements.Add(new WebcamSceneConfig
|
||||
{
|
||||
Name = "Webcam",
|
||||
Type = SourceType.Webcam,
|
||||
DeviceId = "real-device",
|
||||
WebcamId = webcam.Id,
|
||||
Name = webcam.Name,
|
||||
Width = 480,
|
||||
Height = 270,
|
||||
});
|
||||
store.Save(new[] { scene });
|
||||
store.Save(new[] { scene }, webcam);
|
||||
|
||||
var reloaded = store.Load();
|
||||
Assert.Single(reloaded[0].Sources);
|
||||
var config = Assert.Single(reloaded[0].Elements);
|
||||
Assert.IsType<WebcamSceneConfig>(config);
|
||||
|
||||
reloaded[0].Sources.RemoveAt(0);
|
||||
store.Save(reloaded);
|
||||
reloaded[0].Elements.RemoveAt(0);
|
||||
store.Save(reloaded, store.Webcam);
|
||||
|
||||
var afterDelete = store.Load();
|
||||
Assert.Empty(afterDelete[0].Sources);
|
||||
Assert.Empty(afterDelete[0].Elements);
|
||||
}
|
||||
finally
|
||||
{
|
||||
SqliteConnection.ClearAllPools();
|
||||
try { File.Delete(path); } catch { /* best-effort cleanup */ }
|
||||
}
|
||||
}
|
||||
|
||||
// Round-to-rect restore is persisted (schema v4): a Round webcam resized to a
|
||||
// square saves its pre-Round rect dims, and a reloaded config restores them on
|
||||
// toggle-back instead of staying square.
|
||||
[Fact]
|
||||
public void Pre_Round_Rect_Dims_Survive_Save_And_Reload()
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), $"ytLlive-layout-{Guid.NewGuid():N}.db");
|
||||
try
|
||||
{
|
||||
using var store = new LayoutStore(path);
|
||||
var webcam = new Webcam { DeviceId = "real-device", Name = "Logitech" };
|
||||
var scene = new Scene { Name = "Starting" };
|
||||
scene.Elements.Add(new WebcamSceneConfig
|
||||
{
|
||||
WebcamId = webcam.Id,
|
||||
Name = webcam.Name,
|
||||
Width = 400,
|
||||
Height = 400,
|
||||
ClipShape = ClipShape.Round,
|
||||
RectWidth = 480,
|
||||
RectHeight = 270,
|
||||
});
|
||||
store.Save(new[] { scene }, webcam);
|
||||
|
||||
var reloaded = store.Load();
|
||||
var config = Assert.IsType<WebcamSceneConfig>(Assert.Single(reloaded[0].Elements));
|
||||
|
||||
config.ToggleClipShape();
|
||||
Assert.Equal(ClipShape.Traditional, config.ClipShape);
|
||||
Assert.Equal(480, config.Width);
|
||||
Assert.Equal(270, config.Height);
|
||||
Assert.Null(config.RectWidth);
|
||||
Assert.Null(config.RectHeight);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -61,29 +61,28 @@ public sealed class RoundClipInteractionTests
|
||||
window.UpdateLayout();
|
||||
|
||||
var scene = vm.ActiveScene!;
|
||||
var source = new Source
|
||||
var webcam = new WebcamSceneConfig
|
||||
{
|
||||
WebcamId = "test-camera",
|
||||
Name = "Webcam",
|
||||
Type = SourceType.Webcam,
|
||||
DeviceId = "test-camera",
|
||||
X = 1408,
|
||||
Y = 778,
|
||||
Width = 480,
|
||||
Height = 270,
|
||||
};
|
||||
scene.Sources.Add(source);
|
||||
vm.SelectedSource = source;
|
||||
scene.Elements.Add(webcam);
|
||||
vm.SelectedElement = webcam;
|
||||
window.UpdateLayout();
|
||||
|
||||
var previewGrid = (Grid)window.FindName("PreviewGrid")!;
|
||||
var canvasGrid = (Grid)window.FindName("CanvasGrid")!;
|
||||
var toWindow = canvasGrid.TransformToVisual(window);
|
||||
|
||||
var corner = new Point(source.X + source.Width, source.Y + source.Height);
|
||||
var corner = new Point(webcam.X + webcam.Width, webcam.Y + webcam.Height);
|
||||
|
||||
foreach (var shape in new[] { ClipShape.Traditional, ClipShape.Round })
|
||||
{
|
||||
source.ClipShape = shape;
|
||||
webcam.ClipShape = shape;
|
||||
window.UpdateLayout();
|
||||
|
||||
var cornerInWindow = toWindow.Transform(corner);
|
||||
@@ -100,7 +99,7 @@ public sealed class RoundClipInteractionTests
|
||||
}
|
||||
|
||||
// The round clip must render as a circle (square bounding box), not an oval.
|
||||
source.ClipShape = ClipShape.Round;
|
||||
webcam.ClipShape = ClipShape.Round;
|
||||
window.UpdateLayout();
|
||||
var ellipse = FindRoundEllipse(window);
|
||||
Assert.NotNull(ellipse);
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
using Xunit;
|
||||
using ytLive.Models;
|
||||
using ytLive.ViewModels;
|
||||
|
||||
namespace ytLive.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The webcam size safeguard: no scene placement may exceed half the 1920×1080
|
||||
/// master frame (960×540), nor drop below 10% of it (192×108). Enforced at
|
||||
/// resize (MainWindow) and defensively again on every layout load.
|
||||
/// </summary>
|
||||
public class WebcamSafeguardTests
|
||||
{
|
||||
[Fact]
|
||||
public void Oversize_Webcam_Is_Capped_To_Half_The_Frame()
|
||||
{
|
||||
var webcam = new WebcamSceneConfig { Width = 1920, Height = 1080 };
|
||||
MainViewModel.ClampWebcamToBounds(webcam);
|
||||
Assert.Equal(MainViewModel.WebcamMaxWidth, webcam.Width);
|
||||
Assert.Equal(MainViewModel.WebcamMaxHeight, webcam.Height);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wide_Webcam_Scales_To_Fit_Both_Dimensions()
|
||||
{
|
||||
var webcam = new WebcamSceneConfig { Width = 1000, Height = 500 };
|
||||
MainViewModel.ClampWebcamToBounds(webcam);
|
||||
Assert.Equal(960, webcam.Width);
|
||||
Assert.Equal(480, webcam.Height);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tiny_Webcam_Is_Brought_Up_To_The_Minimum()
|
||||
{
|
||||
var webcam = new WebcamSceneConfig { Width = 10, Height = 10 };
|
||||
MainViewModel.ClampWebcamToBounds(webcam);
|
||||
Assert.Equal(MainViewModel.WebcamMinWidth, webcam.Width);
|
||||
Assert.Equal(MainViewModel.WebcamMinWidth, webcam.Height);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Short_Wide_Webcam_Meets_Both_Minimums()
|
||||
{
|
||||
var webcam = new WebcamSceneConfig { Width = 100, Height = 20 };
|
||||
MainViewModel.ClampWebcamToBounds(webcam);
|
||||
Assert.True(webcam.Width >= MainViewModel.WebcamMinWidth);
|
||||
Assert.Equal(MainViewModel.WebcamMinHeight, webcam.Height);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Round_Border_Size_Tracks_The_Shorter_Clamped_Dimension()
|
||||
{
|
||||
var webcam = new WebcamSceneConfig { Width = 1920, Height = 500 };
|
||||
MainViewModel.ClampWebcamToBounds(webcam);
|
||||
Assert.Equal(960, webcam.Width);
|
||||
Assert.Equal(250, webcam.Height);
|
||||
Assert.Equal(250, webcam.RoundBorderSize);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Round_Then_Back_To_Rect_Restores_The_Original_Aspect()
|
||||
{
|
||||
var webcam = new WebcamSceneConfig { Width = 480, Height = 270 };
|
||||
webcam.ToggleClipShape();
|
||||
Assert.Equal(ClipShape.Round, webcam.ClipShape);
|
||||
Assert.Equal(480, webcam.Width);
|
||||
Assert.Equal(270, webcam.Height);
|
||||
Assert.Equal(480, webcam.RectWidth);
|
||||
Assert.Equal(270, webcam.RectHeight);
|
||||
|
||||
webcam.ToggleClipShape();
|
||||
Assert.Equal(ClipShape.Traditional, webcam.ClipShape);
|
||||
Assert.Equal(480, webcam.Width);
|
||||
Assert.Equal(270, webcam.Height);
|
||||
Assert.Null(webcam.RectWidth);
|
||||
Assert.Null(webcam.RectHeight);
|
||||
}
|
||||
|
||||
// The persisted-reload repro: Round + square dims (from a resize while Round),
|
||||
// no in-memory snapshot — exactly what a relaunch produces. The persisted rect
|
||||
// dims must restore the 16:9 on toggle-back.
|
||||
[Fact]
|
||||
public void Reloaded_Round_Config_Restores_Persisted_Rect_Dims()
|
||||
{
|
||||
var webcam = new WebcamSceneConfig
|
||||
{
|
||||
Width = 400,
|
||||
Height = 400,
|
||||
ClipShape = ClipShape.Round,
|
||||
RectWidth = 480,
|
||||
RectHeight = 270,
|
||||
};
|
||||
|
||||
webcam.ToggleClipShape();
|
||||
Assert.Equal(ClipShape.Traditional, webcam.ClipShape);
|
||||
Assert.Equal(480, webcam.Width);
|
||||
Assert.Equal(270, webcam.Height);
|
||||
Assert.Null(webcam.RectWidth);
|
||||
Assert.Null(webcam.RectHeight);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Legacy_Square_Rect_Is_Widened_To_16x9()
|
||||
{
|
||||
var webcam = new WebcamSceneConfig { Width = 414.92, Height = 414.92 };
|
||||
MainViewModel.HealLegacySquareRect(webcam);
|
||||
Assert.Equal(738, webcam.Width);
|
||||
Assert.Equal(414.92, webcam.Height);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Legacy_Square_Heal_Leaves_Non_Square_Untouched()
|
||||
{
|
||||
var webcam = new WebcamSceneConfig { Width = 480, Height = 270 };
|
||||
MainViewModel.HealLegacySquareRect(webcam);
|
||||
Assert.Equal(480, webcam.Width);
|
||||
Assert.Equal(270, webcam.Height);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Legacy_Square_Heal_Leaves_Round_Untouched()
|
||||
{
|
||||
var webcam = new WebcamSceneConfig { Width = 400, Height = 400, ClipShape = ClipShape.Round };
|
||||
MainViewModel.HealLegacySquareRect(webcam);
|
||||
Assert.Equal(400, webcam.Width);
|
||||
Assert.Equal(400, webcam.Height);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Legacy_Square_Heal_Respects_Explicit_Rect_Dims()
|
||||
{
|
||||
var webcam = new WebcamSceneConfig { Width = 400, Height = 400, RectWidth = 480, RectHeight = 270 };
|
||||
MainViewModel.HealLegacySquareRect(webcam);
|
||||
Assert.Equal(400, webcam.Width);
|
||||
Assert.Equal(400, webcam.Height);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user