Scaffold AvParser: Avalonia 12 shell with adaptive layout
Greenfield skeleton for a parser desktop app. The domain is deliberately a placeholder — IParser<TIn,TOut> plus two sample parsers — so the shell is runnable and verifiable end to end before real logic lands. Layers run one way: Core (no Avalonia, no IO) <- Infrastructure <- UI <- Desktop. UI is a class library rather than the exe so headless tests build real views without dragging in Program.cs, Serilog or the container. Adaptive layout is built from what Avalonia actually offers, since it has no AdaptiveTrigger or media queries: ResponsiveLayout observes Visual.Bounds and projects a breakpoint onto both an attached property and :compact/:medium/ :expanded pseudoclasses, with 24px hysteresis so dragging a window edge cannot make the layout flap. Pane state lives in the view model because a style setter loses to a local value permanently; styles own only the visual variance. Stack notes worth remembering: Avalonia.ReactiveUI is deprecated in favour of ReactiveUI.Avalonia, and ReactiveUI 24 runs on the Primitives engine (RxVoid, ISequencer, Signal<T>) and no longer self-initialises. Avalonia.Headless.XUnit 12.x requires xUnit v3. InvariantGlobalization must stay false or Semi.Avalonia throws in its static constructor. 102 tests across three projects, including headless guards for the two failures that are otherwise completely silent: a stylesheet whose selectors match nothing, and a light palette too low-contrast for cards to read. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<RootNamespace>AvParser.UI.HeadlessTests</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\AvParser.Core\AvParser.Core.csproj" />
|
||||
<ProjectReference Include="..\..\src\AvParser.UI\AvParser.UI.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia.Headless" />
|
||||
<PackageReference Include="Avalonia.Headless.XUnit" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
|
||||
<PackageReference Include="ReactiveUI.Avalonia" />
|
||||
<PackageReference Include="Semi.Avalonia" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="ReactiveUI.Primitives" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,41 @@
|
||||
using AvParser.Core.Settings;
|
||||
using AvParser.UI.Services;
|
||||
using AvParser.UI.ViewModels;
|
||||
using ReactiveUI.Primitives.Signals;
|
||||
|
||||
namespace AvParser.UI.HeadlessTests;
|
||||
|
||||
/// <summary>
|
||||
/// Test doubles for the headless tests.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Intentionally duplicated from <c>AvParser.UI.Tests</c> rather than shared through a fourth
|
||||
/// project: these are a few lines of trivial stand-ins, and a shared test-kit assembly would have
|
||||
/// to opt out of the <c>tests/</c> conventions (self-executing xUnit exe) to build at all.
|
||||
/// </remarks>
|
||||
internal sealed class FakePage(string title, string iconKey = "IconHome") : PageViewModel
|
||||
{
|
||||
public override string Title { get; } = title;
|
||||
|
||||
public override string IconKey { get; } = iconKey;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IThemeService" />
|
||||
internal sealed class FakeThemeService(AppTheme initial = AppTheme.System) : IThemeService, IDisposable
|
||||
{
|
||||
private readonly BehaviorSignal<AppTheme> _current = new(initial);
|
||||
|
||||
public AppTheme Current => _current.Value;
|
||||
|
||||
public IObservable<AppTheme> Changes => _current;
|
||||
|
||||
public void Apply(AppTheme theme)
|
||||
{
|
||||
if (theme != _current.Value)
|
||||
{
|
||||
_current.OnNext(theme);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() => _current.Dispose();
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Headless.XUnit;
|
||||
using Avalonia.Threading;
|
||||
using AvParser.UI.Responsive;
|
||||
|
||||
namespace AvParser.UI.HeadlessTests;
|
||||
|
||||
public class ResponsiveTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(320, Breakpoint.Compact)]
|
||||
[InlineData(719, Breakpoint.Compact)]
|
||||
[InlineData(720, Breakpoint.Medium)]
|
||||
[InlineData(1000, Breakpoint.Medium)]
|
||||
[InlineData(1100, Breakpoint.Expanded)]
|
||||
[InlineData(1920, Breakpoint.Expanded)]
|
||||
public void Classify_maps_width_to_a_breakpoint(double width, Breakpoint expected) =>
|
||||
ResponsiveLayout.Classify(width).ShouldBe(expected);
|
||||
|
||||
[Fact]
|
||||
public void Hysteresis_widens_whichever_band_we_are_already_in()
|
||||
{
|
||||
// Sitting just under the medium threshold, a nudge upward must not flip the layout...
|
||||
ResponsiveLayout
|
||||
.Classify(ResponsiveLayout.MediumMinWidth + 10, Breakpoint.Compact)
|
||||
.ShouldBe(Breakpoint.Compact);
|
||||
// ...but a decisive move past the deadband must.
|
||||
ResponsiveLayout
|
||||
.Classify(ResponsiveLayout.MediumMinWidth + ResponsiveLayout.Hysteresis, Breakpoint.Compact)
|
||||
.ShouldBe(Breakpoint.Medium);
|
||||
|
||||
// Symmetrically on the way down from expanded.
|
||||
ResponsiveLayout
|
||||
.Classify(ResponsiveLayout.ExpandedMinWidth - 10, Breakpoint.Expanded)
|
||||
.ShouldBe(Breakpoint.Expanded);
|
||||
ResponsiveLayout
|
||||
.Classify(ResponsiveLayout.ExpandedMinWidth - ResponsiveLayout.Hysteresis - 1, Breakpoint.Expanded)
|
||||
.ShouldBe(Breakpoint.Medium);
|
||||
}
|
||||
|
||||
[AvaloniaTheory]
|
||||
[InlineData(500, Breakpoint.Compact, ":compact")]
|
||||
[InlineData(900, Breakpoint.Medium, ":medium")]
|
||||
[InlineData(1400, Breakpoint.Expanded, ":expanded")]
|
||||
public void Laying_out_a_control_sets_the_breakpoint_and_its_pseudoclass(
|
||||
double width,
|
||||
Breakpoint expected,
|
||||
string pseudoClass
|
||||
)
|
||||
{
|
||||
var host = new Border();
|
||||
ResponsiveLayout.SetIsEnabled(host, true);
|
||||
|
||||
// Measure/Arrange directly rather than resizing a Window: the headless window manager's
|
||||
// resize path is the flakiest thing available, and this is what actually drives Bounds.
|
||||
host.Measure(new Size(width, 800));
|
||||
host.Arrange(new Rect(0, 0, width, 800));
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
|
||||
ResponsiveLayout.GetBreakpoint(host).ShouldBe(expected);
|
||||
host.Classes.Contains(pseudoClass).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Only_one_breakpoint_pseudoclass_is_active_at_a_time()
|
||||
{
|
||||
var host = new Border();
|
||||
ResponsiveLayout.SetIsEnabled(host, true);
|
||||
|
||||
host.Measure(new Size(400, 600));
|
||||
host.Arrange(new Rect(0, 0, 400, 600));
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
|
||||
host.Classes.Contains(":compact").ShouldBeTrue();
|
||||
host.Classes.Contains(":medium").ShouldBeFalse();
|
||||
host.Classes.Contains(":expanded").ShouldBeFalse();
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Disabling_the_behaviour_stops_further_updates()
|
||||
{
|
||||
var host = new Border();
|
||||
ResponsiveLayout.SetIsEnabled(host, true);
|
||||
host.Measure(new Size(400, 600));
|
||||
host.Arrange(new Rect(0, 0, 400, 600));
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
|
||||
ResponsiveLayout.SetIsEnabled(host, false);
|
||||
host.Measure(new Size(1400, 600));
|
||||
host.Arrange(new Rect(0, 0, 1400, 600));
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
|
||||
ResponsiveLayout.GetBreakpoint(host).ShouldBe(Breakpoint.Compact);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Headless.XUnit;
|
||||
using Avalonia.Styling;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
using AvParser.Core.Settings;
|
||||
using AvParser.UI.Navigation;
|
||||
using AvParser.UI.Responsive;
|
||||
using AvParser.UI.Services;
|
||||
using AvParser.UI.ViewModels;
|
||||
using AvParser.UI.Views;
|
||||
using ReactiveUI.Primitives.Concurrency;
|
||||
|
||||
namespace AvParser.UI.HeadlessTests;
|
||||
|
||||
public class ShellViewTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Shows the shell inside a window of the requested width.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A real <see cref="Window"/> is required — a detached control never builds its visual tree,
|
||||
/// so there would be no <see cref="SplitView"/> to assert on. The width is set on the window
|
||||
/// rather than by calling Measure/Arrange by hand: a manual arrange is undone by the window's
|
||||
/// own next layout pass, which made these assertions depend on pump ordering.
|
||||
/// </remarks>
|
||||
private static (ShellView View, ShellViewModel ViewModel, Window Window) ShowShell(double width)
|
||||
{
|
||||
var navigation = new NavigationService([new FakePage("First"), new FakePage("Second", "IconDocument")]);
|
||||
|
||||
// ImmediateSequencer, not the real main-thread one: derived properties then settle within
|
||||
// the same layout pass, so a single RunJobs() is enough for the bindings to catch up.
|
||||
var viewModel = new ShellViewModel(navigation, new FakeThemeService(), ImmediateSequencer.Instance);
|
||||
var view = new ShellView { DataContext = viewModel };
|
||||
var window = new Window
|
||||
{
|
||||
Width = width,
|
||||
Height = 800,
|
||||
Content = view,
|
||||
};
|
||||
|
||||
window.Show();
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
|
||||
return (view, viewModel, window);
|
||||
}
|
||||
|
||||
private static void Resize(Window window, double width)
|
||||
{
|
||||
window.Width = width;
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
}
|
||||
|
||||
private static SplitView NavPaneOf(Visual view) => view.GetVisualDescendants().OfType<SplitView>().Single();
|
||||
|
||||
[AvaloniaTheory]
|
||||
[InlineData(500, SplitViewDisplayMode.Overlay)]
|
||||
[InlineData(900, SplitViewDisplayMode.CompactInline)]
|
||||
[InlineData(1400, SplitViewDisplayMode.Inline)]
|
||||
public void The_navigation_pane_adapts_to_the_shell_width(double width, SplitViewDisplayMode expected)
|
||||
{
|
||||
var (view, _, _) = ShowShell(width);
|
||||
|
||||
NavPaneOf(view).DisplayMode.ShouldBe(expected);
|
||||
}
|
||||
|
||||
[AvaloniaTheory]
|
||||
[InlineData(500, Breakpoint.Compact)]
|
||||
[InlineData(900, Breakpoint.Medium)]
|
||||
[InlineData(1400, Breakpoint.Expanded)]
|
||||
public void The_shell_hands_its_breakpoint_to_the_view_model(double width, Breakpoint expected)
|
||||
{
|
||||
var (_, viewModel, _) = ShowShell(width);
|
||||
|
||||
viewModel.Breakpoint.ShouldBe(expected);
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Narrowing_the_shell_closes_the_pane_and_widening_reopens_it()
|
||||
{
|
||||
var (_, viewModel, window) = ShowShell(1400);
|
||||
viewModel.IsPaneOpen.ShouldBeTrue();
|
||||
|
||||
Resize(window, 480);
|
||||
viewModel.IsPaneOpen.ShouldBeFalse();
|
||||
|
||||
Resize(window, 1400);
|
||||
viewModel.IsPaneOpen.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void The_rail_lists_every_registered_page()
|
||||
{
|
||||
var (view, _, _) = ShowShell(1400);
|
||||
|
||||
view.GetVisualDescendants().OfType<ListBox>().Single().ItemCount.ShouldBe(2);
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Selecting_a_rail_entry_swaps_the_hosted_page()
|
||||
{
|
||||
var (view, viewModel, _) = ShowShell(1400);
|
||||
var second = viewModel.Pages[1];
|
||||
|
||||
viewModel.SelectedPage = second;
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
|
||||
var host = view.GetVisualDescendants().OfType<TransitioningContentControl>().Single();
|
||||
host.Content.ShouldBeSameAs(second);
|
||||
viewModel.Title.ShouldBe("Second");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Guards against the shell stylesheet silently matching nothing.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Avalonia type selectors match the exact type, so <c>UserControl.shell</c> does not match
|
||||
/// <see cref="ShellView"/> (which derives from <c>ReactiveUserControl<T></c>). That
|
||||
/// failure is completely silent — the app renders, just with default metrics — so it needs an
|
||||
/// explicit assertion on a value only the stylesheet can produce.
|
||||
/// </remarks>
|
||||
[AvaloniaFact]
|
||||
public void The_shell_stylesheet_is_actually_applied()
|
||||
{
|
||||
var (view, _, _) = ShowShell(1400);
|
||||
|
||||
// 248 comes from the NavPaneWidth token; SplitView's own default is 320.
|
||||
NavPaneOf(view).OpenPaneLength.ShouldBe(248d);
|
||||
NavPaneOf(view).CompactPaneLength.ShouldBe(56d);
|
||||
|
||||
var pageHost = view.GetVisualDescendants().OfType<Border>().Single(b => b.Name == "PageHost");
|
||||
pageHost.Padding.ShouldBe(new Thickness(24));
|
||||
|
||||
var paneToggle = view.GetVisualDescendants().OfType<Button>().Single(b => b.Name == "PaneToggle");
|
||||
paneToggle.IsVisible.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void The_hamburger_reappears_once_the_pane_stops_being_inline()
|
||||
{
|
||||
var (view, _, _) = ShowShell(520);
|
||||
|
||||
var paneToggle = view.GetVisualDescendants().OfType<Button>().Single(b => b.Name == "PaneToggle");
|
||||
paneToggle.IsVisible.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Labels_collapse_away_on_the_icon_rail()
|
||||
{
|
||||
var (view, _, _) = ShowShell(900);
|
||||
|
||||
var labels = view.GetVisualDescendants()
|
||||
.OfType<TextBlock>()
|
||||
.Where(t => t.Classes.Contains("navLabel"))
|
||||
.ToList();
|
||||
|
||||
labels.ShouldNotBeEmpty();
|
||||
labels.ShouldAllBe(label => !label.IsVisible);
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void The_theme_tokens_actually_change_with_the_variant()
|
||||
{
|
||||
var application = Application.Current.ShouldNotBeNull();
|
||||
|
||||
application.TryFindResource("AppSurfaceBrush", ThemeVariant.Light, out var light).ShouldBeTrue();
|
||||
application.TryFindResource("AppSurfaceBrush", ThemeVariant.Dark, out var dark).ShouldBeTrue();
|
||||
|
||||
// If this fails, Styles/Index.axaml was not loaded by the test app and every other style
|
||||
// assertion in this assembly is meaningless.
|
||||
dark!.ToString().ShouldNotBe(light!.ToString());
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Theme_service_maps_the_app_theme_onto_an_avalonia_variant()
|
||||
{
|
||||
ThemeService.ToVariant(AppTheme.Light).ShouldBe(ThemeVariant.Light);
|
||||
ThemeService.ToVariant(AppTheme.Dark).ShouldBe(ThemeVariant.Dark);
|
||||
ThemeService.ToVariant(AppTheme.System).ShouldBe(ThemeVariant.Default);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Presenters;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Headless.XUnit;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Styling;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
|
||||
namespace AvParser.UI.HeadlessTests;
|
||||
|
||||
public class StyleTests
|
||||
{
|
||||
private static T Show<T>(T control, ThemeVariant variant)
|
||||
where T : Control
|
||||
{
|
||||
Application.Current!.RequestedThemeVariant = variant;
|
||||
|
||||
var window = new Window
|
||||
{
|
||||
Width = 400,
|
||||
Height = 200,
|
||||
Content = control,
|
||||
};
|
||||
window.Show();
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
|
||||
return control;
|
||||
}
|
||||
|
||||
private static Color TokenColor(string key, ThemeVariant variant)
|
||||
{
|
||||
Application.Current!.TryFindResource(key, variant, out var value).ShouldBeTrue();
|
||||
return value.ShouldBeOfType<SolidColorBrush>().Color;
|
||||
}
|
||||
|
||||
private static Color? RenderedBackground(Control control) =>
|
||||
(
|
||||
control.GetVisualDescendants().OfType<ContentPresenter>().FirstOrDefault()?.Background
|
||||
?? control.GetValue(TemplatedControl.BackgroundProperty)
|
||||
)
|
||||
is SolidColorBrush brush
|
||||
? brush.Color
|
||||
: null;
|
||||
|
||||
[AvaloniaTheory]
|
||||
[InlineData("Light")]
|
||||
[InlineData("Dark")]
|
||||
public void The_primary_button_is_filled_with_the_accent_colour(string variantName)
|
||||
{
|
||||
var variant = variantName == "Light" ? ThemeVariant.Light : ThemeVariant.Dark;
|
||||
var button = Show(new Button { Classes = { "primary" }, Content = "Go" }, variant);
|
||||
|
||||
// If this fails the button paints itself white-on-white and simply disappears.
|
||||
RenderedBackground(button).ShouldBe(TokenColor("AppAccentBrush", variant));
|
||||
}
|
||||
|
||||
[AvaloniaTheory]
|
||||
[InlineData("Light")]
|
||||
[InlineData("Dark")]
|
||||
public void A_card_is_distinguishable_from_the_page_behind_it(string variantName)
|
||||
{
|
||||
var variant = variantName == "Light" ? ThemeVariant.Light : ThemeVariant.Dark;
|
||||
|
||||
var page = TokenColor("AppSurfaceBrush", variant);
|
||||
var card = TokenColor("AppSurfaceRaisedBrush", variant);
|
||||
var border = TokenColor("AppBorderBrush", variant);
|
||||
|
||||
// Summed across three channels, 24 is roughly a 3% per-channel step — the point at which
|
||||
// a card stops being a rectangle you have to squint for. The first light palette here
|
||||
// scored 33 and was still visually indistinguishable, so the bar is deliberately high.
|
||||
Distance(page, card).ShouldBeGreaterThan(30);
|
||||
Distance(page, border).ShouldBeGreaterThan(24);
|
||||
}
|
||||
|
||||
[AvaloniaTheory]
|
||||
[InlineData("Light")]
|
||||
[InlineData("Dark")]
|
||||
public void The_card_style_reaches_a_bare_border(string variantName)
|
||||
{
|
||||
var variant = variantName == "Light" ? ThemeVariant.Light : ThemeVariant.Dark;
|
||||
var card = Show(new Border { Classes = { "card" } }, variant);
|
||||
|
||||
card.BorderThickness.ShouldBe(new Thickness(1));
|
||||
card.Padding.ShouldBe(new Thickness(16));
|
||||
|
||||
// The value a DynamicResource actually resolved to for this element's variant — not what
|
||||
// TryFindResource can dig out when asked for a variant explicitly.
|
||||
(card.Background as SolidColorBrush)?.Color.ShouldBe(TokenColor("AppSurfaceRaisedBrush", variant));
|
||||
}
|
||||
|
||||
private static int Distance(Color a, Color b) => Math.Abs(a.R - b.R) + Math.Abs(a.G - b.G) + Math.Abs(a.B - b.B);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Headless;
|
||||
using Avalonia.Markup.Xaml.Styling;
|
||||
using AvParser.UI.HeadlessTests;
|
||||
using ReactiveUI.Avalonia;
|
||||
using Semi.Avalonia;
|
||||
|
||||
[assembly: AvaloniaTestApplication(typeof(TestAppBuilder))]
|
||||
// The headless platform is one dispatcher per assembly; running collections in parallel produces
|
||||
// intermittent "call from invalid thread" failures rather than useful signal.
|
||||
[assembly: CollectionBehavior(DisableTestParallelization = true)]
|
||||
|
||||
namespace AvParser.UI.HeadlessTests;
|
||||
|
||||
/// <summary>Boots a headless Avalonia application for <c>[AvaloniaFact]</c> tests.</summary>
|
||||
public static class TestAppBuilder
|
||||
{
|
||||
/// <summary>Called by Avalonia.Headless.XUnit once per test assembly.</summary>
|
||||
public static AppBuilder BuildAvaloniaApp() =>
|
||||
AppBuilder
|
||||
.Configure<HeadlessTestApp>()
|
||||
.UseHeadless(new AvaloniaHeadlessPlatformOptions())
|
||||
.UseReactiveUI(_ => { });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A minimal application that loads the real styles but never touches the DI container.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Deliberately not <c>AvParser.Desktop.App</c>: these tests should exercise the views and the
|
||||
/// stylesheet, not Serilog, the settings file or the composition root. Styles are added in code
|
||||
/// rather than XAML so the test project needs no Avalonia XAML compilation of its own.
|
||||
/// </remarks>
|
||||
public sealed class HeadlessTestApp : Application
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
Styles.Add(new SemiTheme());
|
||||
|
||||
var index = new Uri("avares://AvParser.UI/Styles/Index.axaml");
|
||||
Styles.Add(new StyleInclude(index) { Source = index });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Headless.XUnit;
|
||||
using AvParser.UI;
|
||||
using AvParser.UI.ViewModels;
|
||||
using AvParser.UI.Views;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace AvParser.UI.HeadlessTests;
|
||||
|
||||
public class ViewLocatorTests
|
||||
{
|
||||
private static ViewLocator Locator(Action<IServiceCollection>? configure = null)
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
configure?.Invoke(services);
|
||||
return new ViewLocator(services.BuildServiceProvider());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Matches_view_models_only()
|
||||
{
|
||||
var locator = Locator();
|
||||
|
||||
locator.Match(new FakePage("x")).ShouldBeTrue();
|
||||
locator.Match("a string").ShouldBeFalse();
|
||||
locator.Match(null).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Resolves_a_view_by_naming_convention()
|
||||
{
|
||||
var locator = Locator();
|
||||
var viewModel = new AboutViewModel(new TestPaths());
|
||||
|
||||
var view = locator.Build(viewModel);
|
||||
|
||||
view.ShouldBeOfType<AboutView>();
|
||||
view.DataContext.ShouldBeSameAs(viewModel);
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Prefers_a_registered_view_over_activation()
|
||||
{
|
||||
var registered = new AboutView();
|
||||
var locator = Locator(services => services.AddSingleton(registered));
|
||||
|
||||
var view = locator.Build(new AboutViewModel(new TestPaths()));
|
||||
|
||||
view.ShouldBeSameAs(registered);
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void Reports_a_missing_view_instead_of_throwing()
|
||||
{
|
||||
var locator = Locator();
|
||||
|
||||
// "FakePage" matches neither half of the convention, so the locator must report a miss
|
||||
// rather than resolving the view model as its own view and failing to activate it.
|
||||
var view = locator.Build(new FakePage("x"));
|
||||
|
||||
view.ShouldBeOfType<TextBlock>().Text!.ShouldContain("View not found");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Builds_a_placeholder_for_a_null_view_model() => Locator().Build(null).ShouldBeOfType<TextBlock>();
|
||||
|
||||
private sealed class TestPaths : Infrastructure.Storage.IAppPaths
|
||||
{
|
||||
public string DataDirectory => Path.Combine(Path.GetTempPath(), "AvParserTests");
|
||||
|
||||
public string SettingsFile => Path.Combine(DataDirectory, "settings.json");
|
||||
|
||||
public string LogDirectory => Path.Combine(DataDirectory, "logs");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user