test(ui): host smoke-test visuals under a parent so their styles apply
WPF fetches implicit and theme styles only once an element is initialized (EndInit, a visual parent, or a window root). The smoke tests measured bare elements created in code, so templated controls had no template: zero-size cards, no wrap panel in the card list, an empty preview. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
1 file changed
+13
-4
@@ -48,6 +48,7 @@ public sealed class VisualsSmokeTests
|
||||
public void CardFitsTheDefaultGridSlot() => WpfHost.Run(() =>
|
||||
{
|
||||
var card = SampleCard();
|
||||
Host(card);
|
||||
card.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
||||
Assert.Approximately(236, card.DesiredSize.Width, 0.5);
|
||||
Assert.LessOrEqual(card.DesiredSize.Height, (double)VirtualizingWrapPanel.ItemHeightProperty.DefaultMetadata.DefaultValue + 0.5);
|
||||
@@ -236,14 +237,22 @@ public sealed class VisualsSmokeTests
|
||||
|
||||
private static RenderTargetBitmap Render(FrameworkElement element, double width, double height)
|
||||
{
|
||||
element.Measure(new Size(width, height));
|
||||
element.Arrange(new Rect(0, 0, width, height));
|
||||
element.UpdateLayout();
|
||||
var host = Host(element);
|
||||
host.Measure(new Size(width, height));
|
||||
host.Arrange(new Rect(0, 0, width, height));
|
||||
host.UpdateLayout();
|
||||
var bitmap = new RenderTargetBitmap((int)width, (int)height, 96, 96, PixelFormats.Pbgra32);
|
||||
bitmap.Render(element);
|
||||
bitmap.Render(host);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gives the element a visual parent, as in a real window: WPF fetches implicit and theme styles (and with them
|
||||
/// templates) only once an element is initialized, which a bare element created in code never is.
|
||||
/// </summary>
|
||||
private static Border Host(FrameworkElement element) =>
|
||||
element.Parent as Border ?? new Border { Child = element };
|
||||
|
||||
private static int Painted(BitmapSource bitmap)
|
||||
{
|
||||
var stride = bitmap.PixelWidth * 4;
|
||||
|
||||
Reference in new issue
Block a user