The Server screen tests now run first and created the process's Application through ScreenHost; the visuals tests then tried to create a second one and the process aborted. Both hosts now use the same lazily started Application. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
311 lines
13 KiB
C#
311 lines
13 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Threading;
|
|
using Titano.Testing;
|
|
using Titano.Ui.Controls;
|
|
using Titano.Ui.Theming;
|
|
using Titano.Ui.Visuals;
|
|
|
|
namespace Titano.App.Tests.Visuals;
|
|
|
|
/// <summary>
|
|
/// Loads the real theme (Controls.xaml + Visuals.xaml) on an STA dispatcher and measures, arranges and renders
|
|
/// every visual off screen in both themes: catches template, resource and layout errors that compile cleanly.
|
|
/// </summary>
|
|
[WindowsOnly]
|
|
[Serial]
|
|
public sealed class VisualsSmokeTests
|
|
{
|
|
[Test]
|
|
public void ThemeProvidesVisualStyles() => WpfHost.Run(() =>
|
|
{
|
|
Assert.True(Application.Current.TryFindResource(typeof(SequenceCard)) is Style);
|
|
Assert.True(Application.Current.TryFindResource(typeof(PreviewViewport)) is Style);
|
|
Assert.True(Application.Current.TryFindResource("ListBox.Cards") is Style);
|
|
Assert.True(Application.Current.TryFindResource("ListBoxItem.Card") is Style);
|
|
});
|
|
|
|
[Test]
|
|
public void EveryVisualRendersInBothThemes() => WpfHost.Run(() =>
|
|
{
|
|
foreach (var theme in new[] { AppTheme.Dark, AppTheme.Light })
|
|
{
|
|
ThemeManager.Apply(theme);
|
|
foreach (var (name, element, width, height) in Samples())
|
|
{
|
|
var pixels = Painted(Render(element, width, height));
|
|
Assert.Greater(pixels, 0, $"{name} drew nothing in {theme}");
|
|
}
|
|
}
|
|
ThemeManager.Apply(AppTheme.Dark);
|
|
});
|
|
|
|
[Test]
|
|
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);
|
|
});
|
|
|
|
[Test]
|
|
public void WrapPanelVirtualizesHalfAMillionItems() => WpfHost.Run(() =>
|
|
{
|
|
var list = new ListBox
|
|
{
|
|
Style = (Style)Application.Current.FindResource("ListBox.Cards"),
|
|
ItemsSource = Enumerable.Range(0, 500_000).ToArray(),
|
|
};
|
|
Render(list, 1300, 900);
|
|
var panel = Assert.NotNull(FindChild<VirtualizingWrapPanel>(list));
|
|
var columns = WrapLayout.ColumnsFor(1300 - 40, panel.ItemWidth, panel.Spacing);
|
|
var rows = (int)Math.Ceiling(900 / (panel.ItemHeight + panel.Spacing)) + 3;
|
|
Assert.LessOrEqual(panel.Children.Count, rows * columns);
|
|
Assert.Approximately(100_000 * (panel.ItemHeight + panel.Spacing) - panel.Spacing + 16, panel.ExtentHeight, 1);
|
|
|
|
var stopwatch = Stopwatch.StartNew();
|
|
for (var i = 0; i < 300; i++)
|
|
{
|
|
panel.SetVerticalOffset(panel.VerticalOffset + 37);
|
|
list.UpdateLayout();
|
|
}
|
|
stopwatch.Stop();
|
|
Assert.Less(stopwatch.Elapsed.TotalSeconds, 5, "300 scroll steps");
|
|
Assert.LessOrEqual(panel.Children.Count, rows * columns);
|
|
|
|
panel.SetVerticalOffset(panel.ExtentHeight / 2);
|
|
list.UpdateLayout();
|
|
var first = (int)((ListBoxItem)panel.Children[0]).Content;
|
|
Assert.InRange(first, 249_000, 251_000);
|
|
|
|
list.ScrollIntoView(400_000);
|
|
list.UpdateLayout();
|
|
Assert.NotNull(list.ItemContainerGenerator.ContainerFromIndex(400_000));
|
|
Assert.LessOrEqual(panel.Children.Count, rows * columns);
|
|
});
|
|
|
|
[Test]
|
|
public void PreviewZoomModesMapToDevicePixels() => WpfHost.Run(() =>
|
|
{
|
|
var preview = new PreviewViewport { Source = Bitmap(2000, 1500) };
|
|
Render(preview, 800, 450);
|
|
var dpi = VisualTreeHelper.GetDpi(preview).DpiScaleX;
|
|
Assert.Approximately(Math.Min(800 / 2000.0, 450 / 1500.0) * dpi, preview.Zoom, 1e-6);
|
|
preview.ZoomMode = PreviewZoomMode.Actual;
|
|
Assert.Approximately(1, preview.Zoom, 1e-9);
|
|
preview.Zoom = 2;
|
|
Assert.Equal(PreviewZoomMode.Custom, preview.ZoomMode);
|
|
Assert.Approximately(2 / dpi, preview.View.Scale, 1e-9);
|
|
});
|
|
|
|
[Test]
|
|
public void SegmentsKnowTheirPosition() => WpfHost.Run(() =>
|
|
{
|
|
var control = SampleSegments();
|
|
Render(control, 120, 32);
|
|
Assert.Equal(SegmentPosition.First, ((SegmentedItem)control.Items[0]).Position);
|
|
Assert.Equal(SegmentPosition.Middle, ((SegmentedItem)control.Items[1]).Position);
|
|
Assert.Equal(SegmentPosition.Last, ((SegmentedItem)control.Items[2]).Position);
|
|
});
|
|
|
|
[Test]
|
|
public void FilmstripRequestsThumbnailsAsynchronously() => WpfHost.Run(() =>
|
|
{
|
|
var source = new FakeFrames(3600);
|
|
var strip = new Filmstrip { Source = source, CurrentIndex = 412 };
|
|
Render(strip, 900, 58);
|
|
WpfHost.DoEvents();
|
|
Assert.Greater(source.Requests, 0);
|
|
Assert.Greater(source.LastPixelWidth, 0);
|
|
Render(strip, 900, 58);
|
|
Assert.Equal(412, strip.CurrentIndex);
|
|
});
|
|
|
|
private static IEnumerable<(string Name, FrameworkElement Element, double Width, double Height)> Samples()
|
|
{
|
|
var n = 120_000;
|
|
var measured = new double[n];
|
|
var target = new double[n];
|
|
var sky = new double[n];
|
|
var random = new Random(3);
|
|
for (var i = 0; i < n; i++)
|
|
{
|
|
target[i] = 0.8 - i / (double)n * 0.6;
|
|
measured[i] = target[i] + (random.NextDouble() - 0.5) * 0.05;
|
|
sky[i] = i is > 500 and < 900 ? double.NaN : target[i] + 0.1;
|
|
}
|
|
var markers = new[] { new TimelineMarker(30_000, MarkerKind.ExposureStep, "-1 EV"), new TimelineMarker(70_000, MarkerKind.Keyframe) };
|
|
yield return ("SeriesChart", new SeriesChart
|
|
{
|
|
Series =
|
|
[
|
|
new ChartSeries("measured", measured, "Brush.Sun") { Thickness = 1.4 },
|
|
new ChartSeries("target", target, "Brush.Accent") { Thickness = 2.2 },
|
|
new ChartSeries("sky", sky, "Brush.Region") { Dash = LineDash.Dashed },
|
|
],
|
|
Markers = markers,
|
|
Summary = "summary",
|
|
CurrentIndex = 1000,
|
|
XLabelFormatter = v => v.ToString("N0"),
|
|
}, 900, 200);
|
|
|
|
var items = new[] { new ChartItem("2019", 0.3), new ChartItem("2020", 0.5), new ChartItem("2021", 1.8, "1.8 TB") };
|
|
yield return ("BarChart.Horizontal", new BarChart { Items = items, MinimumOpacity = 0.5 }, 300, 120);
|
|
yield return ("BarChart.Vertical", new BarChart { Items = items, Orientation = Orientation.Vertical, ShowGrid = true }, 300, 160);
|
|
yield return ("StackedBar", new StackedBar { Items = items }, 300, 60);
|
|
|
|
var months = new[] { "J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D" };
|
|
var rows = Enumerable.Range(2019, 8).Select(y => new ChartRow(y.ToString(), Enumerable.Range(0, 12).Select(m => y == 2026 && m > 8 ? double.NaN : (double)(m * y % 97)).ToArray())).ToArray();
|
|
yield return ("HeatmapStrip", new HeatmapStrip { Rows = rows, ColumnLabels = months }, 300, 220);
|
|
yield return ("TimelineBands", new TimelineBands { Bands = rows, SlotLabels = months, CurrentBand = 1, CurrentSlot = 3 }, 600, 520);
|
|
|
|
yield return ("Filmstrip", new Filmstrip { Source = new FakeFrames(3600), Markers = markers, CurrentIndex = 500 }, 900, 58);
|
|
yield return ("CurveEditor", new CurveEditor
|
|
{
|
|
Points = new ObservableCollection<Point> { new(0, 0), new(0.5, 0.3), new(1, 1) },
|
|
Evaluator = x => x * x,
|
|
Mode = CurveEditMode.TimeRemap,
|
|
ShowDiagonal = true,
|
|
PlayheadPosition = 0.4,
|
|
SelectedIndex = 1,
|
|
}, 400, 200);
|
|
|
|
yield return ("PreviewViewport", new PreviewViewport
|
|
{
|
|
Source = Bitmap(2000, 1500),
|
|
OriginalSource = Bitmap(1000, 750),
|
|
IsCompareEnabled = true,
|
|
WipeLabel = "original | corrected",
|
|
InfoText = "0413 / 3,600",
|
|
ShowVectors = true,
|
|
MotionVectors = [new MotionVector(100, 100, 3, -2), new MotionVector(900, 700, -1, 4)],
|
|
VectorScale = 20,
|
|
ShowRegions = true,
|
|
RegionBoundary = [new Point(0, 800), new Point(1000, 760), new Point(2000, 820)],
|
|
ShowCropMask = true,
|
|
CropRect = new Rect(0.1, 0.1, 0.8, 0.8),
|
|
}, 800, 450);
|
|
|
|
yield return ("SequenceCard", SampleCard(), 236, 208);
|
|
yield return ("SegmentedControl", SampleSegments(), 120, 32);
|
|
yield return ("SplitButton", new SplitButton { Content = "Render", ItemsSource = new[] { "4K", "1080p" }, IsPrimary = true }, 200, 32);
|
|
}
|
|
|
|
private static SequenceCard SampleCard() => new()
|
|
{
|
|
Title = "Harbour sunset",
|
|
Subtitle = "3,600 · Canon EOS R5 · 2 h 00 m",
|
|
Badge = "3,600",
|
|
Thumbnail = Bitmap(320, 180),
|
|
IsSelected = true,
|
|
Statuses = new[] { new CardStatus(StatusKind.Analysed, "a"), new CardStatus(StatusKind.Rendered, "2"), new CardStatus(StatusKind.Synced) },
|
|
};
|
|
|
|
private static SegmentedControl SampleSegments()
|
|
{
|
|
var control = new SegmentedControl();
|
|
foreach (var key in new[] { "Icon.ViewGrid", "Icon.ViewList", "Icon.ViewTimeline" })
|
|
control.Items.Add(new SegmentedItem { Glyph = (IconData)Application.Current.FindResource(key), ToolTip = key });
|
|
control.SelectedIndex = 0;
|
|
return control;
|
|
}
|
|
|
|
private static BitmapSource Bitmap(int width, int height)
|
|
{
|
|
var pixels = new byte[width * height * 4];
|
|
for (var y = 0; y < height; y++)
|
|
{
|
|
for (var x = 0; x < width; x++)
|
|
{
|
|
var i = (y * width + x) * 4;
|
|
pixels[i] = (byte)(x * 255 / width);
|
|
pixels[i + 1] = (byte)(y * 255 / height);
|
|
pixels[i + 2] = 128;
|
|
pixels[i + 3] = 255;
|
|
}
|
|
}
|
|
var bitmap = BitmapSource.Create(width, height, 96, 96, PixelFormats.Bgra32, null, pixels, width * 4);
|
|
bitmap.Freeze();
|
|
return bitmap;
|
|
}
|
|
|
|
private static RenderTargetBitmap Render(FrameworkElement element, double width, double height)
|
|
{
|
|
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(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;
|
|
var pixels = new byte[stride * bitmap.PixelHeight];
|
|
bitmap.CopyPixels(pixels, stride, 0);
|
|
var count = 0;
|
|
for (var i = 3; i < pixels.Length; i += 4)
|
|
if (pixels[i] != 0) count++;
|
|
return count;
|
|
}
|
|
|
|
private static T? FindChild<T>(DependencyObject root)
|
|
where T : DependencyObject
|
|
{
|
|
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
|
|
{
|
|
var child = VisualTreeHelper.GetChild(root, i);
|
|
if (child is T match) return match;
|
|
if (FindChild<T>(child) is { } nested) return nested;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private sealed class FakeFrames(int count) : IThumbnailSource
|
|
{
|
|
private readonly BitmapSource _frame = Bitmap(64, 36);
|
|
|
|
public int Count => count;
|
|
|
|
public int Requests { get; private set; }
|
|
|
|
public int LastPixelWidth { get; private set; }
|
|
|
|
public ValueTask<ImageSource?> GetAsync(int index, int pixelWidth, CancellationToken cancellationToken)
|
|
{
|
|
Requests++;
|
|
LastPixelWidth = pixelWidth;
|
|
return ValueTask.FromResult<ImageSource?>(_frame);
|
|
}
|
|
}
|
|
|
|
/// <summary>Runs on the dispatcher of the shared WPF <see cref="Application"/>.</summary>
|
|
private static class WpfHost
|
|
{
|
|
public static void Run(Action action) => Windows.WpfApplication.Dispatcher.Invoke(action);
|
|
|
|
/// <summary>Processes queued dispatcher work (async continuations) on the current dispatcher.</summary>
|
|
public static void DoEvents()
|
|
{
|
|
var frame = new DispatcherFrame();
|
|
System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, () => frame.Continue = false);
|
|
System.Windows.Threading.Dispatcher.PushFrame(frame);
|
|
}
|
|
}
|
|
}
|