using Avalonia.Controls; using Avalonia.Headless.XUnit; using Avalonia.Threading; using Avalonia.VisualTree; using AvParser.Core.Parsing; using AvParser.Core.Parsing.Samples; using AvParser.Core.Proxies; using AvParser.Core.Settings; using AvParser.UI.ViewModels; using AvParser.UI.Views; using Microsoft.Extensions.Logging.Abstractions; using ReactiveUI.Primitives.Concurrency; namespace AvParser.UI.HeadlessTests; public class ParseViewTests { private static (ParseView View, ParseViewModel ViewModel, Window Window) ShowPage(params ITextParser[] extra) { var catalog = new ParserCatalog([new DelimitedTextParser(), new KeyValueTextParser(), .. extra]); var lastParser = extra.Length > 0 ? extra[0].Id : null; var viewModel = new ParseViewModel( catalog, new FakeSettingsService(new AppSettings { LastParserId = lastParser }), new ProxyPool([], new FakeProxyProbe(), new ProxyOptions()), new EmptyServiceProvider(), NullLogger.Instance, ImmediateSequencer.Instance ); var view = new ParseView { DataContext = viewModel }; var window = new Window { Width = 1400, Height = 900, Content = view, }; window.Show(); Dispatcher.UIThread.RunJobs(); return (view, viewModel, window); } private static Border Banner(ParseView view) => view.FindControl("ProxyGateBanner").ShouldNotBeNull(); [AvaloniaFact] public void No_banner_is_shown_for_a_parser_that_needs_no_network() { var (view, viewModel, _) = ShowPage(); viewModel.IsBlockedWithoutProxy.ShouldBeFalse(); Banner(view).IsVisible.ShouldBeFalse(); } [AvaloniaFact] public void A_blocked_network_parser_puts_the_banner_on_screen() { // Rendered rather than asserted on the view model: an IsVisible binding that never fires // leaves the page silently unhelpful, which is exactly the failure this guards. var (view, viewModel, _) = ShowPage(new NetworkParser()); Dispatcher.UIThread.RunJobs(); viewModel.IsBlockedWithoutProxy.ShouldBeTrue(); Banner(view).IsEffectivelyVisible.ShouldBeTrue(); } [AvaloniaFact] public void The_banner_offers_a_way_to_the_proxies_page() { var (view, viewModel, _) = ShowPage(new NetworkParser()); Dispatcher.UIThread.RunJobs(); var button = Banner(view).GetVisualDescendants().OfType