using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using CursorLang.Models;
using CursorLang.Services;
using CursorLang.Tests.Infrastructure;
using CursorLang.ViewModels;
using CursorLang.Views;
namespace CursorLang.Tests.Views;
///
/// The settings window as a whole: the markup, the bindings and the hook-up
/// to the theme.
///
///
/// The window has to be shown for real: before the show WPF builds no element
/// tree and computes no bindings. Full transparency keeps it out of sight.
///
public sealed class MainWindowTests
{
[Fact]
public void The_window_is_built_and_takes_its_data_from_the_view_model()
{
Sta.Run(() =>
{
using SettingsViewModel viewModel = CreateViewModel();
var theme = new FakeThemeService();
Open(viewModel, theme, window =>
{
Assert.Same(viewModel, window.DataContext);
Assert.Equal([window], theme.Registered);
});
});
}
[Fact]
public void The_window_title_comes_from_the_interface_strings()
{
Sta.Run(() =>
{
var localization = new FakeLocalizationService();
using SettingsViewModel viewModel = CreateViewModel(localization: localization);
Open(viewModel, window =>
{
Assert.Equal("en:SettingsTitle", window.Title);
// A language change goes over every binding to a string
localization.CurrentLanguage = "ru";
Assert.Equal("ru:SettingsTitle", window.Title);
});
});
}
[Fact]
public void The_window_fits_its_height_to_its_content()
{
Sta.Run(() =>
{
using SettingsViewModel viewModel = CreateViewModel();
Open(viewModel, window =>
{
Assert.Equal(SizeToContent.Height, window.SizeToContent);
Assert.Equal(ResizeMode.CanMinimize, window.ResizeMode);
Assert.True(window.ActualHeight > 0);
Assert.True(window.ActualWidth > 0);
});
});
}
// The markup asks the view model for lists and palettes: if a name drifts
// apart from the model, the binding silently shows an empty list
[Fact]
public void The_lists_in_the_window_are_filled()
{
Sta.Run(() =>
{
using SettingsViewModel viewModel = CreateViewModel();
Open(viewModel, window =>
{
List boxes = [.. FindAll(window)];
Assert.NotEmpty(boxes);
Assert.All(boxes, box => Assert.NotEmpty(box.Items));
});
});
}
[Fact]
public void The_lists_show_captions_in_the_chosen_language()
{
Sta.Run(() =>
{
using SettingsViewModel viewModel = CreateViewModel();
Open(viewModel, window =>
{
// Languages are named in themselves, while the enum options are
// named by strings from the resources: the latter are checked
List displays =
[
.. FindAll(window)
.SelectMany(box => box.Items.OfType