Files
LlamaCasty/ytLive.Tests/WebView2ManagerTests.cs
T
gramps ab29ec801d Web source: container == content — element grid + selection box sized to the cropped frame's canvas extent
- FindContentBounds already trimmed the bitmap; the grid/box still rendered at the uncropped user
  size, so Fill stretched the tight widget into a bigger frame = dead-between-content-and-border.
- ContentCanvasSize maps the crop rect to canvas units (1920/pixW per axis, DPI-correct) and the
  preview event now carries (bitmap, canvasW, canvasH).
- OnWebView2PreviewBitmapChanged sets the element Width/Height to that size; the grid and the
  SelectionOverlay (both bound to the element's Width/Height) now equal the widget exactly.
- IsPreviewDragging (set in MainWindow drag/resize) suppresses auto-fit mid-gesture.
- Tests 10/10 (alpha bounds, canvas-size mapping incl. DPI, control size); 0 warnings.
2026-08-28 12:08:04 -07:00

179 lines
4.9 KiB
C#

using System.Drawing;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Threading;
using Microsoft.Web.WebView2.Wpf;
using Xunit;
using ytLive.Models;
using ytLive.Services;
namespace ytLive.Tests;
[Collection("RealApp")]
public sealed class WebView2ManagerTests
{
private readonly RealAppHost _app;
public WebView2ManagerTests(RealAppHost app)
{
_app = app;
}
[Fact]
public void WebView2_Control_Size_Is_Canvas_Resolution_Regardless_Of_Source()
{
_app.Run(() =>
{
var panel = new Canvas();
var source = new Source
{
Type = SourceType.WebSource,
Width = 640,
Height = 480,
};
using var manager = new WebView2Manager(panel, Dispatcher.CurrentDispatcher);
manager.Register(source);
var webView = panel.Children.OfType<WebView2>().First();
Assert.Equal(1920.0, webView.Width);
Assert.Equal(1080.0, webView.Height);
});
}
[Fact]
public void WebView2_Control_Size_Does_Not_Track_Box_After_Register()
{
_app.Run(() =>
{
var panel = new Canvas();
var source = new Source
{
Type = SourceType.WebSource,
Width = 640,
Height = 480,
};
using var manager = new WebView2Manager(panel, Dispatcher.CurrentDispatcher);
manager.Register(source);
var webView = panel.Children.OfType<WebView2>().First();
source.Width = 1024;
source.Height = 768;
Assert.Equal(1920.0, webView.Width);
Assert.Equal(1080.0, webView.Height);
});
}
[Fact]
public void WebView2_Control_Size_Ignores_Zero_Sized_Source()
{
_app.Run(() =>
{
var panel = new Canvas();
var source = new Source
{
Type = SourceType.WebSource,
Width = 0,
Height = 0,
};
using var manager = new WebView2Manager(panel, Dispatcher.CurrentDispatcher);
manager.Register(source);
var webView = panel.Children.OfType<WebView2>().First();
Assert.Equal(1920.0, webView.Width);
Assert.Equal(1080.0, webView.Height);
});
}
[Fact]
public void WebView2_DefaultBackgroundColor_Is_Transparent()
{
_app.Run(() =>
{
var panel = new Canvas();
var source = new Source
{
Type = SourceType.WebSource,
Width = 640,
Height = 480,
};
using var manager = new WebView2Manager(panel, Dispatcher.CurrentDispatcher);
manager.Register(source);
var webView = panel.Children.OfType<WebView2>().First();
Assert.Equal(System.Drawing.Color.Transparent, webView.DefaultBackgroundColor);
});
}
[Fact]
public void FindContentBounds_Returns_Bounding_Box_Of_NonTransparent_Pixels()
{
const int w = 10, h = 6;
var pixels = new byte[w * h * 4];
for (int yy = 2; yy <= 4; yy++)
for (int xx = 3; xx <= 6; xx++)
{
var i = (yy * w + xx) * 4;
pixels[i] = 255; // B
pixels[i + 1] = 0; // G
pixels[i + 2] = 0; // R
pixels[i + 3] = 255; // A
}
var (x, y, bw, bh) = WebView2Manager.FindContentBounds(pixels, w, h, w * 4);
Assert.Equal((3, 2, 4, 3), (x, y, bw, bh));
}
[Fact]
public void FindContentBounds_No_Alpha_Means_No_Crop()
{
const int w = 10, h = 6;
var pixels = new byte[w * h * 4];
var (x, y, bw, bh) = WebView2Manager.FindContentBounds(pixels, w, h, w * 4);
Assert.Equal((0, 0, w, h), (x, y, bw, bh));
}
[Fact]
public void FindContentBounds_Full_Opacity_Means_No_Crop()
{
const int w = 10, h = 6;
var pixels = new byte[w * h * 4];
for (var i = 3; i < pixels.Length; i += 4)
pixels[i] = 255;
var (x, y, bw, bh) = WebView2Manager.FindContentBounds(pixels, w, h, w * 4);
Assert.Equal((0, 0, w, h), (x, y, bw, bh));
}
[Fact]
public void ContentCanvasSize_Full_Frame_Maps_To_Canvas()
{
var (cw, ch) = WebView2Manager.ContentCanvasSize(1920, 1080, 1920, 1080);
Assert.Equal((1920.0, 1080.0), (cw, ch));
}
[Fact]
public void ContentCanvasSize_Half_Widget_Is_Half_Canvas()
{
var (cw, ch) = WebView2Manager.ContentCanvasSize(960, 540, 1920, 1080);
Assert.Equal((960.0, 540.0), (cw, ch));
}
[Fact]
public void ContentCanvasSize_Corrects_For_Dpi_Scale()
{
var (cw, ch) = WebView2Manager.ContentCanvasSize(1200, 675, 2400, 1350);
Assert.Equal((960.0, 540.0), (cw, ch));
}
}