From 5104ffeb81f91a7f2ddcdfbf695d52497c5a0421 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Thu, 9 Jul 2026 22:17:56 +0100 Subject: [PATCH] postIt: wire DarkMode, drop dead themeVariant, add UI tests for the banner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three related changes that close the loop on the DarkMode field and lay the first stone of a UI test scaffold for PostIt. 1. Settings.DarkMode was previously a dead field. It round- tripped through postit-settings.json and the SettingsPage CheckBox, OnDarkModeChanged flipped IsDirty, and that was it — no consumer ever read the value, so toggling the CheckBox had no visible effect. The fix is in App.OnFrameworkInitializationCompleted: read the value Load() just populated and set Application.Current.RequestedThemeVariant accordingly (so a dark-mode user lands on a dark window on first launch, not on a default-light window that flips after the user touches the toggle), then subscribe to settings.PropertyChanged and update the theme on every DarkMode change. The consumer lives in App.axaml.cs, not in Settings, so the Settings model stays free of any Avalonia.Application dependency and the SettingsLoadTests (which construct Settings outside an Avalonia host) still pass unchanged. 2. MainPageViewModel had a vestigial [ObservableProperty] ThemeVariant themeVariant = ThemeVariant.Default that no XAML, no code, and no test ever read. It was the start of a half-finished attempt to expose the theme variant on the page VM. The dark-mode wiring above makes it irrelevant: the theme is now driven by Application, not by a VM property. The field is removed, along with the using Avalonia.Styling; it pulled in (now unused). 3. SessionStatusBannerTests adds the first set of UI tests for PostIt. They mount a real MainWindow via the headless Avalonia host declared in TestApp.cs, attach a SessionStatusViewModel as the banner's DataContext, and assert the actual visual tree contents: three buttons render (Se déconnecter, Se connecter, Paramètres), the Login button is visible when logged out, the Logout button is hidden when logged out, the Paramètres button is visible regardless of session, and the session label text reflects the VM. The pattern follows what UnitTest1.MainPage_Should_Load already established: [AvaloniaFact] (from Avalonia.Headless.XUnit) plus new MainWindow() / window.Show(). A plain [Fact] cannot drive Window..ctor() because the headless platform's PlatformManager.CreateWindow() has no service registered outside a dispatcher-aware test context; the AvaloniaFact attribute provides that context. The DataContext is set on the banner directly because App.OnFrameworkInitializationCompleted is not called in a unit test (production wiring is exercised by the manual launch, not here). Build: 0 errors. Tests: 5/5 SessionStatusBannerTests, 3/3 SettingsLoadTests, 1/1 MainPageTests (the existing scaffold test, unchanged). The other PostIt.Tests suites depend on the OIDC stub WebApplicationFactory and time out on this network-restricted host. --- src/PostIt.Tests/SessionStatusBannerTests.cs | 122 ++++++++++++++++++ src/PostIt/PostIt/App.axaml.cs | 27 +++- .../PostIt/ViewModels/MainPageViewModel.cs | 4 - 3 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 src/PostIt.Tests/SessionStatusBannerTests.cs diff --git a/src/PostIt.Tests/SessionStatusBannerTests.cs b/src/PostIt.Tests/SessionStatusBannerTests.cs new file mode 100644 index 00000000..c1c569a8 --- /dev/null +++ b/src/PostIt.Tests/SessionStatusBannerTests.cs @@ -0,0 +1,122 @@ +using Avalonia.Controls; +using Avalonia.Headless.XUnit; +using Avalonia.VisualTree; +using PostIt.ViewModels; +using PostIt.Views; + +namespace PostIt.Tests; + +/// +/// UI tests for . Mounted inside +/// a real via the headless Avalonia +/// platform declared in TestApp.cs. +/// +/// The pattern is the one that UnitTest1.MainPage_Should_Load +/// established: a test attribute [AvaloniaFact] (from +/// Avalonia.Headless.XUnit) instead of plain [Fact], +/// new MainWindow(), window.Show(). The AvaloniaFact +/// attribute schedules the test body inside a dispatcher, which +/// is the precondition for the headless Window's +/// PlatformManager.CreateWindow() to find a registered +/// service. A plain [Fact] test that calls +/// new Window().Show() throws because the harness has not +/// been initialised for that thread. +/// +/// The session banner's DataContext is not wired in +/// these tests: App.OnFrameworkInitializationCompleted is +/// not called in a unit test, so we set the DataContext on the +/// banner directly. The production code path is exercised +/// end-to-end by the manual launch, not here. +/// +public class SessionStatusBannerTests +{ + [AvaloniaFact] + public void Banner_renders_three_buttons_in_the_visual_tree() + { + var window = new MainWindow(); + window.SessionBanner.DataContext = new SessionStatusViewModel(); + window.Show(); + + var buttons = window.SessionBanner.GetVisualDescendants() + .OfType